module.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554
  1. /*
  2. Copyright (C) 2002 Richard Henderson
  3. Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/module.h>
  17. #include <linux/moduleloader.h>
  18. #include <linux/init.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/elf.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/capability.h>
  30. #include <linux/cpu.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/errno.h>
  33. #include <linux/err.h>
  34. #include <linux/vermagic.h>
  35. #include <linux/notifier.h>
  36. #include <linux/sched.h>
  37. #include <linux/stop_machine.h>
  38. #include <linux/device.h>
  39. #include <linux/string.h>
  40. #include <linux/mutex.h>
  41. #include <linux/unwind.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/semaphore.h>
  44. #include <asm/cacheflush.h>
  45. #include <linux/license.h>
  46. #if 0
  47. #define DEBUGP printk
  48. #else
  49. #define DEBUGP(fmt , a...)
  50. #endif
  51. #ifndef ARCH_SHF_SMALL
  52. #define ARCH_SHF_SMALL 0
  53. #endif
  54. /* If this is set, the section belongs in the init part of the module */
  55. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  56. /* List of modules, protected by module_mutex or preempt_disable
  57. * (add/delete uses stop_machine). */
  58. static DEFINE_MUTEX(module_mutex);
  59. static LIST_HEAD(modules);
  60. /* Waiting for a module to finish initializing? */
  61. static DECLARE_WAIT_QUEUE_HEAD(module_wq);
  62. static BLOCKING_NOTIFIER_HEAD(module_notify_list);
  63. int register_module_notifier(struct notifier_block * nb)
  64. {
  65. return blocking_notifier_chain_register(&module_notify_list, nb);
  66. }
  67. EXPORT_SYMBOL(register_module_notifier);
  68. int unregister_module_notifier(struct notifier_block * nb)
  69. {
  70. return blocking_notifier_chain_unregister(&module_notify_list, nb);
  71. }
  72. EXPORT_SYMBOL(unregister_module_notifier);
  73. /* We require a truly strong try_module_get(): 0 means failure due to
  74. ongoing or failed initialization etc. */
  75. static inline int strong_try_module_get(struct module *mod)
  76. {
  77. if (mod && mod->state == MODULE_STATE_COMING)
  78. return -EBUSY;
  79. if (try_module_get(mod))
  80. return 0;
  81. else
  82. return -ENOENT;
  83. }
  84. static inline void add_taint_module(struct module *mod, unsigned flag)
  85. {
  86. add_taint(flag);
  87. mod->taints |= flag;
  88. }
  89. /*
  90. * A thread that wants to hold a reference to a module only while it
  91. * is running can call this to safely exit. nfsd and lockd use this.
  92. */
  93. void __module_put_and_exit(struct module *mod, long code)
  94. {
  95. module_put(mod);
  96. do_exit(code);
  97. }
  98. EXPORT_SYMBOL(__module_put_and_exit);
  99. /* Find a module section: 0 means not found. */
  100. static unsigned int find_sec(Elf_Ehdr *hdr,
  101. Elf_Shdr *sechdrs,
  102. const char *secstrings,
  103. const char *name)
  104. {
  105. unsigned int i;
  106. for (i = 1; i < hdr->e_shnum; i++)
  107. /* Alloc bit cleared means "ignore it." */
  108. if ((sechdrs[i].sh_flags & SHF_ALLOC)
  109. && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
  110. return i;
  111. return 0;
  112. }
  113. /* Provided by the linker */
  114. extern const struct kernel_symbol __start___ksymtab[];
  115. extern const struct kernel_symbol __stop___ksymtab[];
  116. extern const struct kernel_symbol __start___ksymtab_gpl[];
  117. extern const struct kernel_symbol __stop___ksymtab_gpl[];
  118. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  119. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  120. extern const struct kernel_symbol __start___ksymtab_unused[];
  121. extern const struct kernel_symbol __stop___ksymtab_unused[];
  122. extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
  123. extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
  124. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  125. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  126. extern const unsigned long __start___kcrctab[];
  127. extern const unsigned long __start___kcrctab_gpl[];
  128. extern const unsigned long __start___kcrctab_gpl_future[];
  129. extern const unsigned long __start___kcrctab_unused[];
  130. extern const unsigned long __start___kcrctab_unused_gpl[];
  131. #ifndef CONFIG_MODVERSIONS
  132. #define symversion(base, idx) NULL
  133. #else
  134. #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
  135. #endif
  136. /* lookup symbol in given range of kernel_symbols */
  137. static const struct kernel_symbol *lookup_symbol(const char *name,
  138. const struct kernel_symbol *start,
  139. const struct kernel_symbol *stop)
  140. {
  141. const struct kernel_symbol *ks = start;
  142. for (; ks < stop; ks++)
  143. if (strcmp(ks->name, name) == 0)
  144. return ks;
  145. return NULL;
  146. }
  147. static void printk_unused_warning(const char *name)
  148. {
  149. printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
  150. "however this module is using it.\n", name);
  151. printk(KERN_WARNING "This symbol will go away in the future.\n");
  152. printk(KERN_WARNING "Please evalute if this is the right api to use, "
  153. "and if it really is, submit a report the linux kernel "
  154. "mailinglist together with submitting your code for "
  155. "inclusion.\n");
  156. }
  157. /* Find a symbol, return value, crc and module which owns it */
  158. static unsigned long __find_symbol(const char *name,
  159. struct module **owner,
  160. const unsigned long **crc,
  161. int gplok)
  162. {
  163. struct module *mod;
  164. const struct kernel_symbol *ks;
  165. /* Core kernel first. */
  166. *owner = NULL;
  167. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  168. if (ks) {
  169. *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
  170. return ks->value;
  171. }
  172. if (gplok) {
  173. ks = lookup_symbol(name, __start___ksymtab_gpl,
  174. __stop___ksymtab_gpl);
  175. if (ks) {
  176. *crc = symversion(__start___kcrctab_gpl,
  177. (ks - __start___ksymtab_gpl));
  178. return ks->value;
  179. }
  180. }
  181. ks = lookup_symbol(name, __start___ksymtab_gpl_future,
  182. __stop___ksymtab_gpl_future);
  183. if (ks) {
  184. if (!gplok) {
  185. printk(KERN_WARNING "Symbol %s is being used "
  186. "by a non-GPL module, which will not "
  187. "be allowed in the future\n", name);
  188. printk(KERN_WARNING "Please see the file "
  189. "Documentation/feature-removal-schedule.txt "
  190. "in the kernel source tree for more "
  191. "details.\n");
  192. }
  193. *crc = symversion(__start___kcrctab_gpl_future,
  194. (ks - __start___ksymtab_gpl_future));
  195. return ks->value;
  196. }
  197. ks = lookup_symbol(name, __start___ksymtab_unused,
  198. __stop___ksymtab_unused);
  199. if (ks) {
  200. printk_unused_warning(name);
  201. *crc = symversion(__start___kcrctab_unused,
  202. (ks - __start___ksymtab_unused));
  203. return ks->value;
  204. }
  205. if (gplok)
  206. ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
  207. __stop___ksymtab_unused_gpl);
  208. if (ks) {
  209. printk_unused_warning(name);
  210. *crc = symversion(__start___kcrctab_unused_gpl,
  211. (ks - __start___ksymtab_unused_gpl));
  212. return ks->value;
  213. }
  214. /* Now try modules. */
  215. list_for_each_entry(mod, &modules, list) {
  216. *owner = mod;
  217. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  218. if (ks) {
  219. *crc = symversion(mod->crcs, (ks - mod->syms));
  220. return ks->value;
  221. }
  222. if (gplok) {
  223. ks = lookup_symbol(name, mod->gpl_syms,
  224. mod->gpl_syms + mod->num_gpl_syms);
  225. if (ks) {
  226. *crc = symversion(mod->gpl_crcs,
  227. (ks - mod->gpl_syms));
  228. return ks->value;
  229. }
  230. }
  231. ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
  232. if (ks) {
  233. printk_unused_warning(name);
  234. *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
  235. return ks->value;
  236. }
  237. if (gplok) {
  238. ks = lookup_symbol(name, mod->unused_gpl_syms,
  239. mod->unused_gpl_syms + mod->num_unused_gpl_syms);
  240. if (ks) {
  241. printk_unused_warning(name);
  242. *crc = symversion(mod->unused_gpl_crcs,
  243. (ks - mod->unused_gpl_syms));
  244. return ks->value;
  245. }
  246. }
  247. ks = lookup_symbol(name, mod->gpl_future_syms,
  248. (mod->gpl_future_syms +
  249. mod->num_gpl_future_syms));
  250. if (ks) {
  251. if (!gplok) {
  252. printk(KERN_WARNING "Symbol %s is being used "
  253. "by a non-GPL module, which will not "
  254. "be allowed in the future\n", name);
  255. printk(KERN_WARNING "Please see the file "
  256. "Documentation/feature-removal-schedule.txt "
  257. "in the kernel source tree for more "
  258. "details.\n");
  259. }
  260. *crc = symversion(mod->gpl_future_crcs,
  261. (ks - mod->gpl_future_syms));
  262. return ks->value;
  263. }
  264. }
  265. DEBUGP("Failed to find symbol %s\n", name);
  266. return 0;
  267. }
  268. /* Search for module by name: must hold module_mutex. */
  269. static struct module *find_module(const char *name)
  270. {
  271. struct module *mod;
  272. list_for_each_entry(mod, &modules, list) {
  273. if (strcmp(mod->name, name) == 0)
  274. return mod;
  275. }
  276. return NULL;
  277. }
  278. #ifdef CONFIG_SMP
  279. /* Number of blocks used and allocated. */
  280. static unsigned int pcpu_num_used, pcpu_num_allocated;
  281. /* Size of each block. -ve means used. */
  282. static int *pcpu_size;
  283. static int split_block(unsigned int i, unsigned short size)
  284. {
  285. /* Reallocation required? */
  286. if (pcpu_num_used + 1 > pcpu_num_allocated) {
  287. int *new;
  288. new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
  289. GFP_KERNEL);
  290. if (!new)
  291. return 0;
  292. pcpu_num_allocated *= 2;
  293. pcpu_size = new;
  294. }
  295. /* Insert a new subblock */
  296. memmove(&pcpu_size[i+1], &pcpu_size[i],
  297. sizeof(pcpu_size[0]) * (pcpu_num_used - i));
  298. pcpu_num_used++;
  299. pcpu_size[i+1] -= size;
  300. pcpu_size[i] = size;
  301. return 1;
  302. }
  303. static inline unsigned int block_size(int val)
  304. {
  305. if (val < 0)
  306. return -val;
  307. return val;
  308. }
  309. /* Created by linker magic */
  310. extern char __per_cpu_start[], __per_cpu_end[];
  311. static void *percpu_modalloc(unsigned long size, unsigned long align,
  312. const char *name)
  313. {
  314. unsigned long extra;
  315. unsigned int i;
  316. void *ptr;
  317. if (align > PAGE_SIZE) {
  318. printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
  319. name, align, PAGE_SIZE);
  320. align = PAGE_SIZE;
  321. }
  322. ptr = __per_cpu_start;
  323. for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
  324. /* Extra for alignment requirement. */
  325. extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
  326. BUG_ON(i == 0 && extra != 0);
  327. if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
  328. continue;
  329. /* Transfer extra to previous block. */
  330. if (pcpu_size[i-1] < 0)
  331. pcpu_size[i-1] -= extra;
  332. else
  333. pcpu_size[i-1] += extra;
  334. pcpu_size[i] -= extra;
  335. ptr += extra;
  336. /* Split block if warranted */
  337. if (pcpu_size[i] - size > sizeof(unsigned long))
  338. if (!split_block(i, size))
  339. return NULL;
  340. /* Mark allocated */
  341. pcpu_size[i] = -pcpu_size[i];
  342. return ptr;
  343. }
  344. printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
  345. size);
  346. return NULL;
  347. }
  348. static void percpu_modfree(void *freeme)
  349. {
  350. unsigned int i;
  351. void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
  352. /* First entry is core kernel percpu data. */
  353. for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
  354. if (ptr == freeme) {
  355. pcpu_size[i] = -pcpu_size[i];
  356. goto free;
  357. }
  358. }
  359. BUG();
  360. free:
  361. /* Merge with previous? */
  362. if (pcpu_size[i-1] >= 0) {
  363. pcpu_size[i-1] += pcpu_size[i];
  364. pcpu_num_used--;
  365. memmove(&pcpu_size[i], &pcpu_size[i+1],
  366. (pcpu_num_used - i) * sizeof(pcpu_size[0]));
  367. i--;
  368. }
  369. /* Merge with next? */
  370. if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
  371. pcpu_size[i] += pcpu_size[i+1];
  372. pcpu_num_used--;
  373. memmove(&pcpu_size[i+1], &pcpu_size[i+2],
  374. (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
  375. }
  376. }
  377. static unsigned int find_pcpusec(Elf_Ehdr *hdr,
  378. Elf_Shdr *sechdrs,
  379. const char *secstrings)
  380. {
  381. return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
  382. }
  383. static int percpu_modinit(void)
  384. {
  385. pcpu_num_used = 2;
  386. pcpu_num_allocated = 2;
  387. pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
  388. GFP_KERNEL);
  389. /* Static in-kernel percpu data (used). */
  390. pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
  391. /* Free room. */
  392. pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
  393. if (pcpu_size[1] < 0) {
  394. printk(KERN_ERR "No per-cpu room for modules.\n");
  395. pcpu_num_used = 1;
  396. }
  397. return 0;
  398. }
  399. __initcall(percpu_modinit);
  400. #else /* ... !CONFIG_SMP */
  401. static inline void *percpu_modalloc(unsigned long size, unsigned long align,
  402. const char *name)
  403. {
  404. return NULL;
  405. }
  406. static inline void percpu_modfree(void *pcpuptr)
  407. {
  408. BUG();
  409. }
  410. static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
  411. Elf_Shdr *sechdrs,
  412. const char *secstrings)
  413. {
  414. return 0;
  415. }
  416. static inline void percpu_modcopy(void *pcpudst, const void *src,
  417. unsigned long size)
  418. {
  419. /* pcpusec should be 0, and size of that section should be 0. */
  420. BUG_ON(size != 0);
  421. }
  422. #endif /* CONFIG_SMP */
  423. #define MODINFO_ATTR(field) \
  424. static void setup_modinfo_##field(struct module *mod, const char *s) \
  425. { \
  426. mod->field = kstrdup(s, GFP_KERNEL); \
  427. } \
  428. static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
  429. struct module *mod, char *buffer) \
  430. { \
  431. return sprintf(buffer, "%s\n", mod->field); \
  432. } \
  433. static int modinfo_##field##_exists(struct module *mod) \
  434. { \
  435. return mod->field != NULL; \
  436. } \
  437. static void free_modinfo_##field(struct module *mod) \
  438. { \
  439. kfree(mod->field); \
  440. mod->field = NULL; \
  441. } \
  442. static struct module_attribute modinfo_##field = { \
  443. .attr = { .name = __stringify(field), .mode = 0444 }, \
  444. .show = show_modinfo_##field, \
  445. .setup = setup_modinfo_##field, \
  446. .test = modinfo_##field##_exists, \
  447. .free = free_modinfo_##field, \
  448. };
  449. MODINFO_ATTR(version);
  450. MODINFO_ATTR(srcversion);
  451. static char last_unloaded_module[MODULE_NAME_LEN+1];
  452. #ifdef CONFIG_MODULE_UNLOAD
  453. /* Init the unload section of the module. */
  454. static void module_unload_init(struct module *mod)
  455. {
  456. unsigned int i;
  457. INIT_LIST_HEAD(&mod->modules_which_use_me);
  458. for (i = 0; i < NR_CPUS; i++)
  459. local_set(&mod->ref[i].count, 0);
  460. /* Hold reference count during initialization. */
  461. local_set(&mod->ref[raw_smp_processor_id()].count, 1);
  462. /* Backwards compatibility macros put refcount during init. */
  463. mod->waiter = current;
  464. }
  465. /* modules using other modules */
  466. struct module_use
  467. {
  468. struct list_head list;
  469. struct module *module_which_uses;
  470. };
  471. /* Does a already use b? */
  472. static int already_uses(struct module *a, struct module *b)
  473. {
  474. struct module_use *use;
  475. list_for_each_entry(use, &b->modules_which_use_me, list) {
  476. if (use->module_which_uses == a) {
  477. DEBUGP("%s uses %s!\n", a->name, b->name);
  478. return 1;
  479. }
  480. }
  481. DEBUGP("%s does not use %s!\n", a->name, b->name);
  482. return 0;
  483. }
  484. /* Module a uses b */
  485. static int use_module(struct module *a, struct module *b)
  486. {
  487. struct module_use *use;
  488. int no_warn, err;
  489. if (b == NULL || already_uses(a, b)) return 1;
  490. /* If we're interrupted or time out, we fail. */
  491. if (wait_event_interruptible_timeout(
  492. module_wq, (err = strong_try_module_get(b)) != -EBUSY,
  493. 30 * HZ) <= 0) {
  494. printk("%s: gave up waiting for init of module %s.\n",
  495. a->name, b->name);
  496. return 0;
  497. }
  498. /* If strong_try_module_get() returned a different error, we fail. */
  499. if (err)
  500. return 0;
  501. DEBUGP("Allocating new usage for %s.\n", a->name);
  502. use = kmalloc(sizeof(*use), GFP_ATOMIC);
  503. if (!use) {
  504. printk("%s: out of memory loading\n", a->name);
  505. module_put(b);
  506. return 0;
  507. }
  508. use->module_which_uses = a;
  509. list_add(&use->list, &b->modules_which_use_me);
  510. no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
  511. return 1;
  512. }
  513. /* Clear the unload stuff of the module. */
  514. static void module_unload_free(struct module *mod)
  515. {
  516. struct module *i;
  517. list_for_each_entry(i, &modules, list) {
  518. struct module_use *use;
  519. list_for_each_entry(use, &i->modules_which_use_me, list) {
  520. if (use->module_which_uses == mod) {
  521. DEBUGP("%s unusing %s\n", mod->name, i->name);
  522. module_put(i);
  523. list_del(&use->list);
  524. kfree(use);
  525. sysfs_remove_link(i->holders_dir, mod->name);
  526. /* There can be at most one match. */
  527. break;
  528. }
  529. }
  530. }
  531. }
  532. #ifdef CONFIG_MODULE_FORCE_UNLOAD
  533. static inline int try_force_unload(unsigned int flags)
  534. {
  535. int ret = (flags & O_TRUNC);
  536. if (ret)
  537. add_taint(TAINT_FORCED_RMMOD);
  538. return ret;
  539. }
  540. #else
  541. static inline int try_force_unload(unsigned int flags)
  542. {
  543. return 0;
  544. }
  545. #endif /* CONFIG_MODULE_FORCE_UNLOAD */
  546. struct stopref
  547. {
  548. struct module *mod;
  549. int flags;
  550. int *forced;
  551. };
  552. /* Whole machine is stopped with interrupts off when this runs. */
  553. static int __try_stop_module(void *_sref)
  554. {
  555. struct stopref *sref = _sref;
  556. /* If it's not unused, quit unless we are told to block. */
  557. if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
  558. if (!(*sref->forced = try_force_unload(sref->flags)))
  559. return -EWOULDBLOCK;
  560. }
  561. /* Mark it as dying. */
  562. sref->mod->state = MODULE_STATE_GOING;
  563. return 0;
  564. }
  565. static int try_stop_module(struct module *mod, int flags, int *forced)
  566. {
  567. struct stopref sref = { mod, flags, forced };
  568. return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
  569. }
  570. unsigned int module_refcount(struct module *mod)
  571. {
  572. unsigned int i, total = 0;
  573. for (i = 0; i < NR_CPUS; i++)
  574. total += local_read(&mod->ref[i].count);
  575. return total;
  576. }
  577. EXPORT_SYMBOL(module_refcount);
  578. /* This exists whether we can unload or not */
  579. static void free_module(struct module *mod);
  580. static void wait_for_zero_refcount(struct module *mod)
  581. {
  582. /* Since we might sleep for some time, drop the semaphore first */
  583. mutex_unlock(&module_mutex);
  584. for (;;) {
  585. DEBUGP("Looking at refcount...\n");
  586. set_current_state(TASK_UNINTERRUPTIBLE);
  587. if (module_refcount(mod) == 0)
  588. break;
  589. schedule();
  590. }
  591. current->state = TASK_RUNNING;
  592. mutex_lock(&module_mutex);
  593. }
  594. asmlinkage long
  595. sys_delete_module(const char __user *name_user, unsigned int flags)
  596. {
  597. struct module *mod;
  598. char name[MODULE_NAME_LEN];
  599. int ret, forced = 0;
  600. if (!capable(CAP_SYS_MODULE))
  601. return -EPERM;
  602. if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
  603. return -EFAULT;
  604. name[MODULE_NAME_LEN-1] = '\0';
  605. if (mutex_lock_interruptible(&module_mutex) != 0)
  606. return -EINTR;
  607. mod = find_module(name);
  608. if (!mod) {
  609. ret = -ENOENT;
  610. goto out;
  611. }
  612. if (!list_empty(&mod->modules_which_use_me)) {
  613. /* Other modules depend on us: get rid of them first. */
  614. ret = -EWOULDBLOCK;
  615. goto out;
  616. }
  617. /* Doing init or already dying? */
  618. if (mod->state != MODULE_STATE_LIVE) {
  619. /* FIXME: if (force), slam module count and wake up
  620. waiter --RR */
  621. DEBUGP("%s already dying\n", mod->name);
  622. ret = -EBUSY;
  623. goto out;
  624. }
  625. /* If it has an init func, it must have an exit func to unload */
  626. if (mod->init && !mod->exit) {
  627. forced = try_force_unload(flags);
  628. if (!forced) {
  629. /* This module can't be removed */
  630. ret = -EBUSY;
  631. goto out;
  632. }
  633. }
  634. /* Set this up before setting mod->state */
  635. mod->waiter = current;
  636. /* Stop the machine so refcounts can't move and disable module. */
  637. ret = try_stop_module(mod, flags, &forced);
  638. if (ret != 0)
  639. goto out;
  640. /* Never wait if forced. */
  641. if (!forced && module_refcount(mod) != 0)
  642. wait_for_zero_refcount(mod);
  643. /* Final destruction now noone is using it. */
  644. if (mod->exit != NULL) {
  645. mutex_unlock(&module_mutex);
  646. mod->exit();
  647. mutex_lock(&module_mutex);
  648. }
  649. /* Store the name of the last unloaded module for diagnostic purposes */
  650. strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
  651. free_module(mod);
  652. out:
  653. mutex_unlock(&module_mutex);
  654. return ret;
  655. }
  656. static void print_unload_info(struct seq_file *m, struct module *mod)
  657. {
  658. struct module_use *use;
  659. int printed_something = 0;
  660. seq_printf(m, " %u ", module_refcount(mod));
  661. /* Always include a trailing , so userspace can differentiate
  662. between this and the old multi-field proc format. */
  663. list_for_each_entry(use, &mod->modules_which_use_me, list) {
  664. printed_something = 1;
  665. seq_printf(m, "%s,", use->module_which_uses->name);
  666. }
  667. if (mod->init != NULL && mod->exit == NULL) {
  668. printed_something = 1;
  669. seq_printf(m, "[permanent],");
  670. }
  671. if (!printed_something)
  672. seq_printf(m, "-");
  673. }
  674. void __symbol_put(const char *symbol)
  675. {
  676. struct module *owner;
  677. const unsigned long *crc;
  678. preempt_disable();
  679. if (!__find_symbol(symbol, &owner, &crc, 1))
  680. BUG();
  681. module_put(owner);
  682. preempt_enable();
  683. }
  684. EXPORT_SYMBOL(__symbol_put);
  685. void symbol_put_addr(void *addr)
  686. {
  687. struct module *modaddr;
  688. if (core_kernel_text((unsigned long)addr))
  689. return;
  690. if (!(modaddr = module_text_address((unsigned long)addr)))
  691. BUG();
  692. module_put(modaddr);
  693. }
  694. EXPORT_SYMBOL_GPL(symbol_put_addr);
  695. static ssize_t show_refcnt(struct module_attribute *mattr,
  696. struct module *mod, char *buffer)
  697. {
  698. return sprintf(buffer, "%u\n", module_refcount(mod));
  699. }
  700. static struct module_attribute refcnt = {
  701. .attr = { .name = "refcnt", .mode = 0444 },
  702. .show = show_refcnt,
  703. };
  704. void module_put(struct module *module)
  705. {
  706. if (module) {
  707. unsigned int cpu = get_cpu();
  708. local_dec(&module->ref[cpu].count);
  709. /* Maybe they're waiting for us to drop reference? */
  710. if (unlikely(!module_is_live(module)))
  711. wake_up_process(module->waiter);
  712. put_cpu();
  713. }
  714. }
  715. EXPORT_SYMBOL(module_put);
  716. #else /* !CONFIG_MODULE_UNLOAD */
  717. static void print_unload_info(struct seq_file *m, struct module *mod)
  718. {
  719. /* We don't know the usage count, or what modules are using. */
  720. seq_printf(m, " - -");
  721. }
  722. static inline void module_unload_free(struct module *mod)
  723. {
  724. }
  725. static inline int use_module(struct module *a, struct module *b)
  726. {
  727. return strong_try_module_get(b) == 0;
  728. }
  729. static inline void module_unload_init(struct module *mod)
  730. {
  731. }
  732. #endif /* CONFIG_MODULE_UNLOAD */
  733. static ssize_t show_initstate(struct module_attribute *mattr,
  734. struct module *mod, char *buffer)
  735. {
  736. const char *state = "unknown";
  737. switch (mod->state) {
  738. case MODULE_STATE_LIVE:
  739. state = "live";
  740. break;
  741. case MODULE_STATE_COMING:
  742. state = "coming";
  743. break;
  744. case MODULE_STATE_GOING:
  745. state = "going";
  746. break;
  747. }
  748. return sprintf(buffer, "%s\n", state);
  749. }
  750. static struct module_attribute initstate = {
  751. .attr = { .name = "initstate", .mode = 0444 },
  752. .show = show_initstate,
  753. };
  754. static struct module_attribute *modinfo_attrs[] = {
  755. &modinfo_version,
  756. &modinfo_srcversion,
  757. &initstate,
  758. #ifdef CONFIG_MODULE_UNLOAD
  759. &refcnt,
  760. #endif
  761. NULL,
  762. };
  763. static const char vermagic[] = VERMAGIC_STRING;
  764. #ifdef CONFIG_MODVERSIONS
  765. static int check_version(Elf_Shdr *sechdrs,
  766. unsigned int versindex,
  767. const char *symname,
  768. struct module *mod,
  769. const unsigned long *crc)
  770. {
  771. unsigned int i, num_versions;
  772. struct modversion_info *versions;
  773. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  774. if (!crc)
  775. return 1;
  776. versions = (void *) sechdrs[versindex].sh_addr;
  777. num_versions = sechdrs[versindex].sh_size
  778. / sizeof(struct modversion_info);
  779. for (i = 0; i < num_versions; i++) {
  780. if (strcmp(versions[i].name, symname) != 0)
  781. continue;
  782. if (versions[i].crc == *crc)
  783. return 1;
  784. printk("%s: disagrees about version of symbol %s\n",
  785. mod->name, symname);
  786. DEBUGP("Found checksum %lX vs module %lX\n",
  787. *crc, versions[i].crc);
  788. return 0;
  789. }
  790. /* Not in module's version table. OK, but that taints the kernel. */
  791. if (!(tainted & TAINT_FORCED_MODULE))
  792. printk("%s: no version for \"%s\" found: kernel tainted.\n",
  793. mod->name, symname);
  794. add_taint_module(mod, TAINT_FORCED_MODULE);
  795. return 1;
  796. }
  797. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  798. unsigned int versindex,
  799. struct module *mod)
  800. {
  801. const unsigned long *crc;
  802. struct module *owner;
  803. if (!__find_symbol("struct_module", &owner, &crc, 1))
  804. BUG();
  805. return check_version(sechdrs, versindex, "struct_module", mod,
  806. crc);
  807. }
  808. /* First part is kernel version, which we ignore. */
  809. static inline int same_magic(const char *amagic, const char *bmagic)
  810. {
  811. amagic += strcspn(amagic, " ");
  812. bmagic += strcspn(bmagic, " ");
  813. return strcmp(amagic, bmagic) == 0;
  814. }
  815. #else
  816. static inline int check_version(Elf_Shdr *sechdrs,
  817. unsigned int versindex,
  818. const char *symname,
  819. struct module *mod,
  820. const unsigned long *crc)
  821. {
  822. return 1;
  823. }
  824. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  825. unsigned int versindex,
  826. struct module *mod)
  827. {
  828. return 1;
  829. }
  830. static inline int same_magic(const char *amagic, const char *bmagic)
  831. {
  832. return strcmp(amagic, bmagic) == 0;
  833. }
  834. #endif /* CONFIG_MODVERSIONS */
  835. /* Resolve a symbol for this module. I.e. if we find one, record usage.
  836. Must be holding module_mutex. */
  837. static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
  838. unsigned int versindex,
  839. const char *name,
  840. struct module *mod)
  841. {
  842. struct module *owner;
  843. unsigned long ret;
  844. const unsigned long *crc;
  845. ret = __find_symbol(name, &owner, &crc,
  846. !(mod->taints & TAINT_PROPRIETARY_MODULE));
  847. if (ret) {
  848. /* use_module can fail due to OOM,
  849. or module initialization or unloading */
  850. if (!check_version(sechdrs, versindex, name, mod, crc) ||
  851. !use_module(mod, owner))
  852. ret = 0;
  853. }
  854. return ret;
  855. }
  856. /*
  857. * /sys/module/foo/sections stuff
  858. * J. Corbet <corbet@lwn.net>
  859. */
  860. #ifdef CONFIG_KALLSYMS
  861. static ssize_t module_sect_show(struct module_attribute *mattr,
  862. struct module *mod, char *buf)
  863. {
  864. struct module_sect_attr *sattr =
  865. container_of(mattr, struct module_sect_attr, mattr);
  866. return sprintf(buf, "0x%lx\n", sattr->address);
  867. }
  868. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  869. {
  870. int section;
  871. for (section = 0; section < sect_attrs->nsections; section++)
  872. kfree(sect_attrs->attrs[section].name);
  873. kfree(sect_attrs);
  874. }
  875. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  876. char *secstrings, Elf_Shdr *sechdrs)
  877. {
  878. unsigned int nloaded = 0, i, size[2];
  879. struct module_sect_attrs *sect_attrs;
  880. struct module_sect_attr *sattr;
  881. struct attribute **gattr;
  882. /* Count loaded sections and allocate structures */
  883. for (i = 0; i < nsect; i++)
  884. if (sechdrs[i].sh_flags & SHF_ALLOC)
  885. nloaded++;
  886. size[0] = ALIGN(sizeof(*sect_attrs)
  887. + nloaded * sizeof(sect_attrs->attrs[0]),
  888. sizeof(sect_attrs->grp.attrs[0]));
  889. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  890. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  891. if (sect_attrs == NULL)
  892. return;
  893. /* Setup section attributes. */
  894. sect_attrs->grp.name = "sections";
  895. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  896. sect_attrs->nsections = 0;
  897. sattr = &sect_attrs->attrs[0];
  898. gattr = &sect_attrs->grp.attrs[0];
  899. for (i = 0; i < nsect; i++) {
  900. if (! (sechdrs[i].sh_flags & SHF_ALLOC))
  901. continue;
  902. sattr->address = sechdrs[i].sh_addr;
  903. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  904. GFP_KERNEL);
  905. if (sattr->name == NULL)
  906. goto out;
  907. sect_attrs->nsections++;
  908. sattr->mattr.show = module_sect_show;
  909. sattr->mattr.store = NULL;
  910. sattr->mattr.attr.name = sattr->name;
  911. sattr->mattr.attr.mode = S_IRUGO;
  912. *(gattr++) = &(sattr++)->mattr.attr;
  913. }
  914. *gattr = NULL;
  915. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  916. goto out;
  917. mod->sect_attrs = sect_attrs;
  918. return;
  919. out:
  920. free_sect_attrs(sect_attrs);
  921. }
  922. static void remove_sect_attrs(struct module *mod)
  923. {
  924. if (mod->sect_attrs) {
  925. sysfs_remove_group(&mod->mkobj.kobj,
  926. &mod->sect_attrs->grp);
  927. /* We are positive that no one is using any sect attrs
  928. * at this point. Deallocate immediately. */
  929. free_sect_attrs(mod->sect_attrs);
  930. mod->sect_attrs = NULL;
  931. }
  932. }
  933. /*
  934. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  935. */
  936. struct module_notes_attrs {
  937. struct kobject *dir;
  938. unsigned int notes;
  939. struct bin_attribute attrs[0];
  940. };
  941. static ssize_t module_notes_read(struct kobject *kobj,
  942. struct bin_attribute *bin_attr,
  943. char *buf, loff_t pos, size_t count)
  944. {
  945. /*
  946. * The caller checked the pos and count against our size.
  947. */
  948. memcpy(buf, bin_attr->private + pos, count);
  949. return count;
  950. }
  951. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  952. unsigned int i)
  953. {
  954. if (notes_attrs->dir) {
  955. while (i-- > 0)
  956. sysfs_remove_bin_file(notes_attrs->dir,
  957. &notes_attrs->attrs[i]);
  958. kobject_del(notes_attrs->dir);
  959. }
  960. kfree(notes_attrs);
  961. }
  962. static void add_notes_attrs(struct module *mod, unsigned int nsect,
  963. char *secstrings, Elf_Shdr *sechdrs)
  964. {
  965. unsigned int notes, loaded, i;
  966. struct module_notes_attrs *notes_attrs;
  967. struct bin_attribute *nattr;
  968. /* Count notes sections and allocate structures. */
  969. notes = 0;
  970. for (i = 0; i < nsect; i++)
  971. if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
  972. (sechdrs[i].sh_type == SHT_NOTE))
  973. ++notes;
  974. if (notes == 0)
  975. return;
  976. notes_attrs = kzalloc(sizeof(*notes_attrs)
  977. + notes * sizeof(notes_attrs->attrs[0]),
  978. GFP_KERNEL);
  979. if (notes_attrs == NULL)
  980. return;
  981. notes_attrs->notes = notes;
  982. nattr = &notes_attrs->attrs[0];
  983. for (loaded = i = 0; i < nsect; ++i) {
  984. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  985. continue;
  986. if (sechdrs[i].sh_type == SHT_NOTE) {
  987. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  988. nattr->attr.mode = S_IRUGO;
  989. nattr->size = sechdrs[i].sh_size;
  990. nattr->private = (void *) sechdrs[i].sh_addr;
  991. nattr->read = module_notes_read;
  992. ++nattr;
  993. }
  994. ++loaded;
  995. }
  996. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  997. if (!notes_attrs->dir)
  998. goto out;
  999. for (i = 0; i < notes; ++i)
  1000. if (sysfs_create_bin_file(notes_attrs->dir,
  1001. &notes_attrs->attrs[i]))
  1002. goto out;
  1003. mod->notes_attrs = notes_attrs;
  1004. return;
  1005. out:
  1006. free_notes_attrs(notes_attrs, i);
  1007. }
  1008. static void remove_notes_attrs(struct module *mod)
  1009. {
  1010. if (mod->notes_attrs)
  1011. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1012. }
  1013. #else
  1014. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  1015. char *sectstrings, Elf_Shdr *sechdrs)
  1016. {
  1017. }
  1018. static inline void remove_sect_attrs(struct module *mod)
  1019. {
  1020. }
  1021. static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
  1022. char *sectstrings, Elf_Shdr *sechdrs)
  1023. {
  1024. }
  1025. static inline void remove_notes_attrs(struct module *mod)
  1026. {
  1027. }
  1028. #endif /* CONFIG_KALLSYMS */
  1029. #ifdef CONFIG_SYSFS
  1030. int module_add_modinfo_attrs(struct module *mod)
  1031. {
  1032. struct module_attribute *attr;
  1033. struct module_attribute *temp_attr;
  1034. int error = 0;
  1035. int i;
  1036. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1037. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1038. GFP_KERNEL);
  1039. if (!mod->modinfo_attrs)
  1040. return -ENOMEM;
  1041. temp_attr = mod->modinfo_attrs;
  1042. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1043. if (!attr->test ||
  1044. (attr->test && attr->test(mod))) {
  1045. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1046. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  1047. ++temp_attr;
  1048. }
  1049. }
  1050. return error;
  1051. }
  1052. void module_remove_modinfo_attrs(struct module *mod)
  1053. {
  1054. struct module_attribute *attr;
  1055. int i;
  1056. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1057. /* pick a field to test for end of list */
  1058. if (!attr->attr.name)
  1059. break;
  1060. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  1061. if (attr->free)
  1062. attr->free(mod);
  1063. }
  1064. kfree(mod->modinfo_attrs);
  1065. }
  1066. #endif
  1067. #ifdef CONFIG_SYSFS
  1068. int mod_sysfs_init(struct module *mod)
  1069. {
  1070. int err;
  1071. if (!module_sysfs_initialized) {
  1072. printk(KERN_ERR "%s: module sysfs not initialized\n",
  1073. mod->name);
  1074. err = -EINVAL;
  1075. goto out;
  1076. }
  1077. mod->mkobj.mod = mod;
  1078. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1079. mod->mkobj.kobj.kset = module_kset;
  1080. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1081. "%s", mod->name);
  1082. if (err)
  1083. kobject_put(&mod->mkobj.kobj);
  1084. /* delay uevent until full sysfs population */
  1085. out:
  1086. return err;
  1087. }
  1088. int mod_sysfs_setup(struct module *mod,
  1089. struct kernel_param *kparam,
  1090. unsigned int num_params)
  1091. {
  1092. int err;
  1093. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1094. if (!mod->holders_dir) {
  1095. err = -ENOMEM;
  1096. goto out_unreg;
  1097. }
  1098. err = module_param_sysfs_setup(mod, kparam, num_params);
  1099. if (err)
  1100. goto out_unreg_holders;
  1101. err = module_add_modinfo_attrs(mod);
  1102. if (err)
  1103. goto out_unreg_param;
  1104. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1105. return 0;
  1106. out_unreg_param:
  1107. module_param_sysfs_remove(mod);
  1108. out_unreg_holders:
  1109. kobject_put(mod->holders_dir);
  1110. out_unreg:
  1111. kobject_put(&mod->mkobj.kobj);
  1112. return err;
  1113. }
  1114. #endif
  1115. static void mod_kobject_remove(struct module *mod)
  1116. {
  1117. module_remove_modinfo_attrs(mod);
  1118. module_param_sysfs_remove(mod);
  1119. kobject_put(mod->mkobj.drivers_dir);
  1120. kobject_put(mod->holders_dir);
  1121. kobject_put(&mod->mkobj.kobj);
  1122. }
  1123. /*
  1124. * link the module with the whole machine is stopped with interrupts off
  1125. * - this defends against kallsyms not taking locks
  1126. */
  1127. static int __link_module(void *_mod)
  1128. {
  1129. struct module *mod = _mod;
  1130. list_add(&mod->list, &modules);
  1131. return 0;
  1132. }
  1133. /*
  1134. * unlink the module with the whole machine is stopped with interrupts off
  1135. * - this defends against kallsyms not taking locks
  1136. */
  1137. static int __unlink_module(void *_mod)
  1138. {
  1139. struct module *mod = _mod;
  1140. list_del(&mod->list);
  1141. return 0;
  1142. }
  1143. /* Free a module, remove from lists, etc (must hold module_mutex). */
  1144. static void free_module(struct module *mod)
  1145. {
  1146. /* Delete from various lists */
  1147. stop_machine_run(__unlink_module, mod, NR_CPUS);
  1148. remove_notes_attrs(mod);
  1149. remove_sect_attrs(mod);
  1150. mod_kobject_remove(mod);
  1151. unwind_remove_table(mod->unwind_info, 0);
  1152. /* Arch-specific cleanup. */
  1153. module_arch_cleanup(mod);
  1154. /* Module unload stuff */
  1155. module_unload_free(mod);
  1156. /* This may be NULL, but that's OK */
  1157. module_free(mod, mod->module_init);
  1158. kfree(mod->args);
  1159. if (mod->percpu)
  1160. percpu_modfree(mod->percpu);
  1161. /* Free lock-classes: */
  1162. lockdep_free_key_range(mod->module_core, mod->core_size);
  1163. /* Finally, free the core (containing the module structure) */
  1164. module_free(mod, mod->module_core);
  1165. }
  1166. void *__symbol_get(const char *symbol)
  1167. {
  1168. struct module *owner;
  1169. unsigned long value;
  1170. const unsigned long *crc;
  1171. preempt_disable();
  1172. value = __find_symbol(symbol, &owner, &crc, 1);
  1173. if (value && strong_try_module_get(owner) != 0)
  1174. value = 0;
  1175. preempt_enable();
  1176. return (void *)value;
  1177. }
  1178. EXPORT_SYMBOL_GPL(__symbol_get);
  1179. /*
  1180. * Ensure that an exported symbol [global namespace] does not already exist
  1181. * in the kernel or in some other module's exported symbol table.
  1182. */
  1183. static int verify_export_symbols(struct module *mod)
  1184. {
  1185. const char *name = NULL;
  1186. unsigned long i, ret = 0;
  1187. struct module *owner;
  1188. const unsigned long *crc;
  1189. for (i = 0; i < mod->num_syms; i++)
  1190. if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
  1191. name = mod->syms[i].name;
  1192. ret = -ENOEXEC;
  1193. goto dup;
  1194. }
  1195. for (i = 0; i < mod->num_gpl_syms; i++)
  1196. if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
  1197. name = mod->gpl_syms[i].name;
  1198. ret = -ENOEXEC;
  1199. goto dup;
  1200. }
  1201. dup:
  1202. if (ret)
  1203. printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
  1204. mod->name, name, module_name(owner));
  1205. return ret;
  1206. }
  1207. /* Change all symbols so that st_value encodes the pointer directly. */
  1208. static int simplify_symbols(Elf_Shdr *sechdrs,
  1209. unsigned int symindex,
  1210. const char *strtab,
  1211. unsigned int versindex,
  1212. unsigned int pcpuindex,
  1213. struct module *mod)
  1214. {
  1215. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1216. unsigned long secbase;
  1217. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1218. int ret = 0;
  1219. for (i = 1; i < n; i++) {
  1220. switch (sym[i].st_shndx) {
  1221. case SHN_COMMON:
  1222. /* We compiled with -fno-common. These are not
  1223. supposed to happen. */
  1224. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1225. printk("%s: please compile with -fno-common\n",
  1226. mod->name);
  1227. ret = -ENOEXEC;
  1228. break;
  1229. case SHN_ABS:
  1230. /* Don't need to do anything */
  1231. DEBUGP("Absolute symbol: 0x%08lx\n",
  1232. (long)sym[i].st_value);
  1233. break;
  1234. case SHN_UNDEF:
  1235. sym[i].st_value
  1236. = resolve_symbol(sechdrs, versindex,
  1237. strtab + sym[i].st_name, mod);
  1238. /* Ok if resolved. */
  1239. if (sym[i].st_value != 0)
  1240. break;
  1241. /* Ok if weak. */
  1242. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1243. break;
  1244. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1245. mod->name, strtab + sym[i].st_name);
  1246. ret = -ENOENT;
  1247. break;
  1248. default:
  1249. /* Divert to percpu allocation if a percpu var. */
  1250. if (sym[i].st_shndx == pcpuindex)
  1251. secbase = (unsigned long)mod->percpu;
  1252. else
  1253. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1254. sym[i].st_value += secbase;
  1255. break;
  1256. }
  1257. }
  1258. return ret;
  1259. }
  1260. /* Update size with this section: return offset. */
  1261. static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
  1262. {
  1263. long ret;
  1264. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1265. *size = ret + sechdr->sh_size;
  1266. return ret;
  1267. }
  1268. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1269. might -- code, read-only data, read-write data, small data. Tally
  1270. sizes, and place the offsets into sh_entsize fields: high bit means it
  1271. belongs in init. */
  1272. static void layout_sections(struct module *mod,
  1273. const Elf_Ehdr *hdr,
  1274. Elf_Shdr *sechdrs,
  1275. const char *secstrings)
  1276. {
  1277. static unsigned long const masks[][2] = {
  1278. /* NOTE: all executable code must be the first section
  1279. * in this array; otherwise modify the text_size
  1280. * finder in the two loops below */
  1281. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1282. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1283. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1284. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1285. };
  1286. unsigned int m, i;
  1287. for (i = 0; i < hdr->e_shnum; i++)
  1288. sechdrs[i].sh_entsize = ~0UL;
  1289. DEBUGP("Core section allocation order:\n");
  1290. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1291. for (i = 0; i < hdr->e_shnum; ++i) {
  1292. Elf_Shdr *s = &sechdrs[i];
  1293. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1294. || (s->sh_flags & masks[m][1])
  1295. || s->sh_entsize != ~0UL
  1296. || strncmp(secstrings + s->sh_name,
  1297. ".init", 5) == 0)
  1298. continue;
  1299. s->sh_entsize = get_offset(&mod->core_size, s);
  1300. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1301. }
  1302. if (m == 0)
  1303. mod->core_text_size = mod->core_size;
  1304. }
  1305. DEBUGP("Init section allocation order:\n");
  1306. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1307. for (i = 0; i < hdr->e_shnum; ++i) {
  1308. Elf_Shdr *s = &sechdrs[i];
  1309. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1310. || (s->sh_flags & masks[m][1])
  1311. || s->sh_entsize != ~0UL
  1312. || strncmp(secstrings + s->sh_name,
  1313. ".init", 5) != 0)
  1314. continue;
  1315. s->sh_entsize = (get_offset(&mod->init_size, s)
  1316. | INIT_OFFSET_MASK);
  1317. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1318. }
  1319. if (m == 0)
  1320. mod->init_text_size = mod->init_size;
  1321. }
  1322. }
  1323. static void set_license(struct module *mod, const char *license)
  1324. {
  1325. if (!license)
  1326. license = "unspecified";
  1327. if (!license_is_gpl_compatible(license)) {
  1328. if (!(tainted & TAINT_PROPRIETARY_MODULE))
  1329. printk(KERN_WARNING "%s: module license '%s' taints "
  1330. "kernel.\n", mod->name, license);
  1331. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1332. }
  1333. }
  1334. /* Parse tag=value strings from .modinfo section */
  1335. static char *next_string(char *string, unsigned long *secsize)
  1336. {
  1337. /* Skip non-zero chars */
  1338. while (string[0]) {
  1339. string++;
  1340. if ((*secsize)-- <= 1)
  1341. return NULL;
  1342. }
  1343. /* Skip any zero padding. */
  1344. while (!string[0]) {
  1345. string++;
  1346. if ((*secsize)-- <= 1)
  1347. return NULL;
  1348. }
  1349. return string;
  1350. }
  1351. static char *get_modinfo(Elf_Shdr *sechdrs,
  1352. unsigned int info,
  1353. const char *tag)
  1354. {
  1355. char *p;
  1356. unsigned int taglen = strlen(tag);
  1357. unsigned long size = sechdrs[info].sh_size;
  1358. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1359. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1360. return p + taglen + 1;
  1361. }
  1362. return NULL;
  1363. }
  1364. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1365. unsigned int infoindex)
  1366. {
  1367. struct module_attribute *attr;
  1368. int i;
  1369. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1370. if (attr->setup)
  1371. attr->setup(mod,
  1372. get_modinfo(sechdrs,
  1373. infoindex,
  1374. attr->attr.name));
  1375. }
  1376. }
  1377. #ifdef CONFIG_KALLSYMS
  1378. static int is_exported(const char *name, const struct module *mod)
  1379. {
  1380. if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
  1381. return 1;
  1382. else
  1383. if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
  1384. return 1;
  1385. else
  1386. return 0;
  1387. }
  1388. /* As per nm */
  1389. static char elf_type(const Elf_Sym *sym,
  1390. Elf_Shdr *sechdrs,
  1391. const char *secstrings,
  1392. struct module *mod)
  1393. {
  1394. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1395. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1396. return 'v';
  1397. else
  1398. return 'w';
  1399. }
  1400. if (sym->st_shndx == SHN_UNDEF)
  1401. return 'U';
  1402. if (sym->st_shndx == SHN_ABS)
  1403. return 'a';
  1404. if (sym->st_shndx >= SHN_LORESERVE)
  1405. return '?';
  1406. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1407. return 't';
  1408. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1409. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1410. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1411. return 'r';
  1412. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1413. return 'g';
  1414. else
  1415. return 'd';
  1416. }
  1417. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1418. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1419. return 's';
  1420. else
  1421. return 'b';
  1422. }
  1423. if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
  1424. ".debug", strlen(".debug")) == 0)
  1425. return 'n';
  1426. return '?';
  1427. }
  1428. static void add_kallsyms(struct module *mod,
  1429. Elf_Shdr *sechdrs,
  1430. unsigned int symindex,
  1431. unsigned int strindex,
  1432. const char *secstrings)
  1433. {
  1434. unsigned int i;
  1435. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1436. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1437. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1438. /* Set types up while we still have access to sections. */
  1439. for (i = 0; i < mod->num_symtab; i++)
  1440. mod->symtab[i].st_info
  1441. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1442. }
  1443. #else
  1444. static inline void add_kallsyms(struct module *mod,
  1445. Elf_Shdr *sechdrs,
  1446. unsigned int symindex,
  1447. unsigned int strindex,
  1448. const char *secstrings)
  1449. {
  1450. }
  1451. #endif /* CONFIG_KALLSYMS */
  1452. /* Allocate and load the module: note that size of section 0 is always
  1453. zero, and we rely on this for optional sections. */
  1454. static struct module *load_module(void __user *umod,
  1455. unsigned long len,
  1456. const char __user *uargs)
  1457. {
  1458. Elf_Ehdr *hdr;
  1459. Elf_Shdr *sechdrs;
  1460. char *secstrings, *args, *modmagic, *strtab = NULL;
  1461. unsigned int i;
  1462. unsigned int symindex = 0;
  1463. unsigned int strindex = 0;
  1464. unsigned int setupindex;
  1465. unsigned int exindex;
  1466. unsigned int exportindex;
  1467. unsigned int modindex;
  1468. unsigned int obsparmindex;
  1469. unsigned int infoindex;
  1470. unsigned int gplindex;
  1471. unsigned int crcindex;
  1472. unsigned int gplcrcindex;
  1473. unsigned int versindex;
  1474. unsigned int pcpuindex;
  1475. unsigned int gplfutureindex;
  1476. unsigned int gplfuturecrcindex;
  1477. unsigned int unwindex = 0;
  1478. unsigned int unusedindex;
  1479. unsigned int unusedcrcindex;
  1480. unsigned int unusedgplindex;
  1481. unsigned int unusedgplcrcindex;
  1482. unsigned int markersindex;
  1483. unsigned int markersstringsindex;
  1484. struct module *mod;
  1485. long err = 0;
  1486. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1487. struct exception_table_entry *extable;
  1488. mm_segment_t old_fs;
  1489. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1490. umod, len, uargs);
  1491. if (len < sizeof(*hdr))
  1492. return ERR_PTR(-ENOEXEC);
  1493. /* Suck in entire file: we'll want most of it. */
  1494. /* vmalloc barfs on "unusual" numbers. Check here */
  1495. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1496. return ERR_PTR(-ENOMEM);
  1497. if (copy_from_user(hdr, umod, len) != 0) {
  1498. err = -EFAULT;
  1499. goto free_hdr;
  1500. }
  1501. /* Sanity checks against insmoding binaries or wrong arch,
  1502. weird elf version */
  1503. if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
  1504. || hdr->e_type != ET_REL
  1505. || !elf_check_arch(hdr)
  1506. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1507. err = -ENOEXEC;
  1508. goto free_hdr;
  1509. }
  1510. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1511. goto truncated;
  1512. /* Convenience variables */
  1513. sechdrs = (void *)hdr + hdr->e_shoff;
  1514. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1515. sechdrs[0].sh_addr = 0;
  1516. for (i = 1; i < hdr->e_shnum; i++) {
  1517. if (sechdrs[i].sh_type != SHT_NOBITS
  1518. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1519. goto truncated;
  1520. /* Mark all sections sh_addr with their address in the
  1521. temporary image. */
  1522. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1523. /* Internal symbols and strings. */
  1524. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1525. symindex = i;
  1526. strindex = sechdrs[i].sh_link;
  1527. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1528. }
  1529. #ifndef CONFIG_MODULE_UNLOAD
  1530. /* Don't load .exit sections */
  1531. if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
  1532. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1533. #endif
  1534. }
  1535. modindex = find_sec(hdr, sechdrs, secstrings,
  1536. ".gnu.linkonce.this_module");
  1537. if (!modindex) {
  1538. printk(KERN_WARNING "No module found in object\n");
  1539. err = -ENOEXEC;
  1540. goto free_hdr;
  1541. }
  1542. mod = (void *)sechdrs[modindex].sh_addr;
  1543. if (symindex == 0) {
  1544. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1545. mod->name);
  1546. err = -ENOEXEC;
  1547. goto free_hdr;
  1548. }
  1549. /* Optional sections */
  1550. exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
  1551. gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
  1552. gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
  1553. unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
  1554. unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
  1555. crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
  1556. gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1557. gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
  1558. unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
  1559. unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
  1560. setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
  1561. exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
  1562. obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
  1563. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1564. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1565. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1566. #ifdef ARCH_UNWIND_SECTION_NAME
  1567. unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
  1568. #endif
  1569. /* Don't keep modinfo section */
  1570. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1571. #ifdef CONFIG_KALLSYMS
  1572. /* Keep symbol and string tables for decoding later. */
  1573. sechdrs[symindex].sh_flags |= SHF_ALLOC;
  1574. sechdrs[strindex].sh_flags |= SHF_ALLOC;
  1575. #endif
  1576. if (unwindex)
  1577. sechdrs[unwindex].sh_flags |= SHF_ALLOC;
  1578. /* Check module struct version now, before we try to use module. */
  1579. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1580. err = -ENOEXEC;
  1581. goto free_hdr;
  1582. }
  1583. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1584. /* This is allowed: modprobe --force will invalidate it. */
  1585. if (!modmagic) {
  1586. add_taint_module(mod, TAINT_FORCED_MODULE);
  1587. printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
  1588. mod->name);
  1589. } else if (!same_magic(modmagic, vermagic)) {
  1590. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1591. mod->name, modmagic, vermagic);
  1592. err = -ENOEXEC;
  1593. goto free_hdr;
  1594. }
  1595. /* Now copy in args */
  1596. args = strndup_user(uargs, ~0UL >> 1);
  1597. if (IS_ERR(args)) {
  1598. err = PTR_ERR(args);
  1599. goto free_hdr;
  1600. }
  1601. if (find_module(mod->name)) {
  1602. err = -EEXIST;
  1603. goto free_mod;
  1604. }
  1605. mod->state = MODULE_STATE_COMING;
  1606. /* Allow arches to frob section contents and sizes. */
  1607. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1608. if (err < 0)
  1609. goto free_mod;
  1610. if (pcpuindex) {
  1611. /* We have a special allocation for this section. */
  1612. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1613. sechdrs[pcpuindex].sh_addralign,
  1614. mod->name);
  1615. if (!percpu) {
  1616. err = -ENOMEM;
  1617. goto free_mod;
  1618. }
  1619. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1620. mod->percpu = percpu;
  1621. }
  1622. /* Determine total sizes, and put offsets in sh_entsize. For now
  1623. this is done generically; there doesn't appear to be any
  1624. special cases for the architectures. */
  1625. layout_sections(mod, hdr, sechdrs, secstrings);
  1626. /* Do the allocs. */
  1627. ptr = module_alloc(mod->core_size);
  1628. if (!ptr) {
  1629. err = -ENOMEM;
  1630. goto free_percpu;
  1631. }
  1632. memset(ptr, 0, mod->core_size);
  1633. mod->module_core = ptr;
  1634. ptr = module_alloc(mod->init_size);
  1635. if (!ptr && mod->init_size) {
  1636. err = -ENOMEM;
  1637. goto free_core;
  1638. }
  1639. memset(ptr, 0, mod->init_size);
  1640. mod->module_init = ptr;
  1641. /* Transfer each section which specifies SHF_ALLOC */
  1642. DEBUGP("final section addresses:\n");
  1643. for (i = 0; i < hdr->e_shnum; i++) {
  1644. void *dest;
  1645. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1646. continue;
  1647. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1648. dest = mod->module_init
  1649. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1650. else
  1651. dest = mod->module_core + sechdrs[i].sh_entsize;
  1652. if (sechdrs[i].sh_type != SHT_NOBITS)
  1653. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1654. sechdrs[i].sh_size);
  1655. /* Update sh_addr to point to copy in image. */
  1656. sechdrs[i].sh_addr = (unsigned long)dest;
  1657. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1658. }
  1659. /* Module has been moved. */
  1660. mod = (void *)sechdrs[modindex].sh_addr;
  1661. /* Now we've moved module, initialize linked lists, etc. */
  1662. module_unload_init(mod);
  1663. /* add kobject, so we can reference it. */
  1664. err = mod_sysfs_init(mod);
  1665. if (err)
  1666. goto free_unload;
  1667. /* Set up license info based on the info section */
  1668. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1669. if (strcmp(mod->name, "ndiswrapper") == 0)
  1670. add_taint(TAINT_PROPRIETARY_MODULE);
  1671. if (strcmp(mod->name, "driverloader") == 0)
  1672. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1673. /* Set up MODINFO_ATTR fields */
  1674. setup_modinfo(mod, sechdrs, infoindex);
  1675. /* Fix up syms, so that st_value is a pointer to location. */
  1676. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1677. mod);
  1678. if (err < 0)
  1679. goto cleanup;
  1680. /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
  1681. mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
  1682. mod->syms = (void *)sechdrs[exportindex].sh_addr;
  1683. if (crcindex)
  1684. mod->crcs = (void *)sechdrs[crcindex].sh_addr;
  1685. mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
  1686. mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
  1687. if (gplcrcindex)
  1688. mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
  1689. mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
  1690. sizeof(*mod->gpl_future_syms);
  1691. mod->num_unused_syms = sechdrs[unusedindex].sh_size /
  1692. sizeof(*mod->unused_syms);
  1693. mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
  1694. sizeof(*mod->unused_gpl_syms);
  1695. mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
  1696. if (gplfuturecrcindex)
  1697. mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
  1698. mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
  1699. if (unusedcrcindex)
  1700. mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
  1701. mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
  1702. if (unusedgplcrcindex)
  1703. mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
  1704. #ifdef CONFIG_MODVERSIONS
  1705. if ((mod->num_syms && !crcindex) ||
  1706. (mod->num_gpl_syms && !gplcrcindex) ||
  1707. (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
  1708. (mod->num_unused_syms && !unusedcrcindex) ||
  1709. (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
  1710. printk(KERN_WARNING "%s: No versions for exported symbols."
  1711. " Tainting kernel.\n", mod->name);
  1712. add_taint_module(mod, TAINT_FORCED_MODULE);
  1713. }
  1714. #endif
  1715. markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
  1716. markersstringsindex = find_sec(hdr, sechdrs, secstrings,
  1717. "__markers_strings");
  1718. /* Now do relocations. */
  1719. for (i = 1; i < hdr->e_shnum; i++) {
  1720. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1721. unsigned int info = sechdrs[i].sh_info;
  1722. /* Not a valid relocation section? */
  1723. if (info >= hdr->e_shnum)
  1724. continue;
  1725. /* Don't bother with non-allocated sections */
  1726. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1727. continue;
  1728. if (sechdrs[i].sh_type == SHT_REL)
  1729. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1730. else if (sechdrs[i].sh_type == SHT_RELA)
  1731. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1732. mod);
  1733. if (err < 0)
  1734. goto cleanup;
  1735. }
  1736. #ifdef CONFIG_MARKERS
  1737. mod->markers = (void *)sechdrs[markersindex].sh_addr;
  1738. mod->num_markers =
  1739. sechdrs[markersindex].sh_size / sizeof(*mod->markers);
  1740. #endif
  1741. /* Find duplicate symbols */
  1742. err = verify_export_symbols(mod);
  1743. if (err < 0)
  1744. goto cleanup;
  1745. /* Set up and sort exception table */
  1746. mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
  1747. mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
  1748. sort_extable(extable, extable + mod->num_exentries);
  1749. /* Finally, copy percpu area over. */
  1750. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1751. sechdrs[pcpuindex].sh_size);
  1752. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1753. #ifdef CONFIG_MARKERS
  1754. if (!mod->taints)
  1755. marker_update_probe_range(mod->markers,
  1756. mod->markers + mod->num_markers, NULL, NULL);
  1757. #endif
  1758. err = module_finalize(hdr, sechdrs, mod);
  1759. if (err < 0)
  1760. goto cleanup;
  1761. /* flush the icache in correct context */
  1762. old_fs = get_fs();
  1763. set_fs(KERNEL_DS);
  1764. /*
  1765. * Flush the instruction cache, since we've played with text.
  1766. * Do it before processing of module parameters, so the module
  1767. * can provide parameter accessor functions of its own.
  1768. */
  1769. if (mod->module_init)
  1770. flush_icache_range((unsigned long)mod->module_init,
  1771. (unsigned long)mod->module_init
  1772. + mod->init_size);
  1773. flush_icache_range((unsigned long)mod->module_core,
  1774. (unsigned long)mod->module_core + mod->core_size);
  1775. set_fs(old_fs);
  1776. mod->args = args;
  1777. if (obsparmindex)
  1778. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1779. mod->name);
  1780. /* Now sew it into the lists so we can get lockdep and oops
  1781. * info during argument parsing. Noone should access us, since
  1782. * strong_try_module_get() will fail. */
  1783. stop_machine_run(__link_module, mod, NR_CPUS);
  1784. /* Size of section 0 is 0, so this works well if no params */
  1785. err = parse_args(mod->name, mod->args,
  1786. (struct kernel_param *)
  1787. sechdrs[setupindex].sh_addr,
  1788. sechdrs[setupindex].sh_size
  1789. / sizeof(struct kernel_param),
  1790. NULL);
  1791. if (err < 0)
  1792. goto unlink;
  1793. err = mod_sysfs_setup(mod,
  1794. (struct kernel_param *)
  1795. sechdrs[setupindex].sh_addr,
  1796. sechdrs[setupindex].sh_size
  1797. / sizeof(struct kernel_param));
  1798. if (err < 0)
  1799. goto unlink;
  1800. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1801. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1802. /* Size of section 0 is 0, so this works well if no unwind info. */
  1803. mod->unwind_info = unwind_add_table(mod,
  1804. (void *)sechdrs[unwindex].sh_addr,
  1805. sechdrs[unwindex].sh_size);
  1806. /* Get rid of temporary copy */
  1807. vfree(hdr);
  1808. /* Done! */
  1809. return mod;
  1810. unlink:
  1811. stop_machine_run(__unlink_module, mod, NR_CPUS);
  1812. module_arch_cleanup(mod);
  1813. cleanup:
  1814. kobject_del(&mod->mkobj.kobj);
  1815. kobject_put(&mod->mkobj.kobj);
  1816. free_unload:
  1817. module_unload_free(mod);
  1818. module_free(mod, mod->module_init);
  1819. free_core:
  1820. module_free(mod, mod->module_core);
  1821. free_percpu:
  1822. if (percpu)
  1823. percpu_modfree(percpu);
  1824. free_mod:
  1825. kfree(args);
  1826. free_hdr:
  1827. vfree(hdr);
  1828. return ERR_PTR(err);
  1829. truncated:
  1830. printk(KERN_ERR "Module len %lu truncated\n", len);
  1831. err = -ENOEXEC;
  1832. goto free_hdr;
  1833. }
  1834. /* This is where the real work happens */
  1835. asmlinkage long
  1836. sys_init_module(void __user *umod,
  1837. unsigned long len,
  1838. const char __user *uargs)
  1839. {
  1840. struct module *mod;
  1841. int ret = 0;
  1842. /* Must have permission */
  1843. if (!capable(CAP_SYS_MODULE))
  1844. return -EPERM;
  1845. /* Only one module load at a time, please */
  1846. if (mutex_lock_interruptible(&module_mutex) != 0)
  1847. return -EINTR;
  1848. /* Do all the hard work */
  1849. mod = load_module(umod, len, uargs);
  1850. if (IS_ERR(mod)) {
  1851. mutex_unlock(&module_mutex);
  1852. return PTR_ERR(mod);
  1853. }
  1854. /* Drop lock so they can recurse */
  1855. mutex_unlock(&module_mutex);
  1856. blocking_notifier_call_chain(&module_notify_list,
  1857. MODULE_STATE_COMING, mod);
  1858. /* Start the module */
  1859. if (mod->init != NULL)
  1860. ret = mod->init();
  1861. if (ret < 0) {
  1862. /* Init routine failed: abort. Try to protect us from
  1863. buggy refcounters. */
  1864. mod->state = MODULE_STATE_GOING;
  1865. synchronize_sched();
  1866. module_put(mod);
  1867. mutex_lock(&module_mutex);
  1868. free_module(mod);
  1869. mutex_unlock(&module_mutex);
  1870. wake_up(&module_wq);
  1871. return ret;
  1872. }
  1873. /* Now it's a first class citizen! */
  1874. mutex_lock(&module_mutex);
  1875. mod->state = MODULE_STATE_LIVE;
  1876. /* Drop initial reference. */
  1877. module_put(mod);
  1878. unwind_remove_table(mod->unwind_info, 1);
  1879. module_free(mod, mod->module_init);
  1880. mod->module_init = NULL;
  1881. mod->init_size = 0;
  1882. mod->init_text_size = 0;
  1883. mutex_unlock(&module_mutex);
  1884. wake_up(&module_wq);
  1885. return 0;
  1886. }
  1887. static inline int within(unsigned long addr, void *start, unsigned long size)
  1888. {
  1889. return ((void *)addr >= start && (void *)addr < start + size);
  1890. }
  1891. #ifdef CONFIG_KALLSYMS
  1892. /*
  1893. * This ignores the intensely annoying "mapping symbols" found
  1894. * in ARM ELF files: $a, $t and $d.
  1895. */
  1896. static inline int is_arm_mapping_symbol(const char *str)
  1897. {
  1898. return str[0] == '$' && strchr("atd", str[1])
  1899. && (str[2] == '\0' || str[2] == '.');
  1900. }
  1901. static const char *get_ksymbol(struct module *mod,
  1902. unsigned long addr,
  1903. unsigned long *size,
  1904. unsigned long *offset)
  1905. {
  1906. unsigned int i, best = 0;
  1907. unsigned long nextval;
  1908. /* At worse, next value is at end of module */
  1909. if (within(addr, mod->module_init, mod->init_size))
  1910. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  1911. else
  1912. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  1913. /* Scan for closest preceeding symbol, and next symbol. (ELF
  1914. starts real symbols at 1). */
  1915. for (i = 1; i < mod->num_symtab; i++) {
  1916. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  1917. continue;
  1918. /* We ignore unnamed symbols: they're uninformative
  1919. * and inserted at a whim. */
  1920. if (mod->symtab[i].st_value <= addr
  1921. && mod->symtab[i].st_value > mod->symtab[best].st_value
  1922. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1923. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1924. best = i;
  1925. if (mod->symtab[i].st_value > addr
  1926. && mod->symtab[i].st_value < nextval
  1927. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1928. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1929. nextval = mod->symtab[i].st_value;
  1930. }
  1931. if (!best)
  1932. return NULL;
  1933. if (size)
  1934. *size = nextval - mod->symtab[best].st_value;
  1935. if (offset)
  1936. *offset = addr - mod->symtab[best].st_value;
  1937. return mod->strtab + mod->symtab[best].st_name;
  1938. }
  1939. /* For kallsyms to ask for address resolution. NULL means not found.
  1940. We don't lock, as this is used for oops resolution and races are a
  1941. lesser concern. */
  1942. /* FIXME: Risky: returns a pointer into a module w/o lock */
  1943. const char *module_address_lookup(unsigned long addr,
  1944. unsigned long *size,
  1945. unsigned long *offset,
  1946. char **modname)
  1947. {
  1948. struct module *mod;
  1949. const char *ret = NULL;
  1950. preempt_disable();
  1951. list_for_each_entry(mod, &modules, list) {
  1952. if (within(addr, mod->module_init, mod->init_size)
  1953. || within(addr, mod->module_core, mod->core_size)) {
  1954. if (modname)
  1955. *modname = mod->name;
  1956. ret = get_ksymbol(mod, addr, size, offset);
  1957. break;
  1958. }
  1959. }
  1960. preempt_enable();
  1961. return ret;
  1962. }
  1963. int lookup_module_symbol_name(unsigned long addr, char *symname)
  1964. {
  1965. struct module *mod;
  1966. preempt_disable();
  1967. list_for_each_entry(mod, &modules, list) {
  1968. if (within(addr, mod->module_init, mod->init_size) ||
  1969. within(addr, mod->module_core, mod->core_size)) {
  1970. const char *sym;
  1971. sym = get_ksymbol(mod, addr, NULL, NULL);
  1972. if (!sym)
  1973. goto out;
  1974. strlcpy(symname, sym, KSYM_NAME_LEN);
  1975. preempt_enable();
  1976. return 0;
  1977. }
  1978. }
  1979. out:
  1980. preempt_enable();
  1981. return -ERANGE;
  1982. }
  1983. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  1984. unsigned long *offset, char *modname, char *name)
  1985. {
  1986. struct module *mod;
  1987. preempt_disable();
  1988. list_for_each_entry(mod, &modules, list) {
  1989. if (within(addr, mod->module_init, mod->init_size) ||
  1990. within(addr, mod->module_core, mod->core_size)) {
  1991. const char *sym;
  1992. sym = get_ksymbol(mod, addr, size, offset);
  1993. if (!sym)
  1994. goto out;
  1995. if (modname)
  1996. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  1997. if (name)
  1998. strlcpy(name, sym, KSYM_NAME_LEN);
  1999. preempt_enable();
  2000. return 0;
  2001. }
  2002. }
  2003. out:
  2004. preempt_enable();
  2005. return -ERANGE;
  2006. }
  2007. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2008. char *name, char *module_name, int *exported)
  2009. {
  2010. struct module *mod;
  2011. preempt_disable();
  2012. list_for_each_entry(mod, &modules, list) {
  2013. if (symnum < mod->num_symtab) {
  2014. *value = mod->symtab[symnum].st_value;
  2015. *type = mod->symtab[symnum].st_info;
  2016. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2017. KSYM_NAME_LEN);
  2018. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2019. *exported = is_exported(name, mod);
  2020. preempt_enable();
  2021. return 0;
  2022. }
  2023. symnum -= mod->num_symtab;
  2024. }
  2025. preempt_enable();
  2026. return -ERANGE;
  2027. }
  2028. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2029. {
  2030. unsigned int i;
  2031. for (i = 0; i < mod->num_symtab; i++)
  2032. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2033. mod->symtab[i].st_info != 'U')
  2034. return mod->symtab[i].st_value;
  2035. return 0;
  2036. }
  2037. /* Look for this name: can be of form module:name. */
  2038. unsigned long module_kallsyms_lookup_name(const char *name)
  2039. {
  2040. struct module *mod;
  2041. char *colon;
  2042. unsigned long ret = 0;
  2043. /* Don't lock: we're in enough trouble already. */
  2044. preempt_disable();
  2045. if ((colon = strchr(name, ':')) != NULL) {
  2046. *colon = '\0';
  2047. if ((mod = find_module(name)) != NULL)
  2048. ret = mod_find_symname(mod, colon+1);
  2049. *colon = ':';
  2050. } else {
  2051. list_for_each_entry(mod, &modules, list)
  2052. if ((ret = mod_find_symname(mod, name)) != 0)
  2053. break;
  2054. }
  2055. preempt_enable();
  2056. return ret;
  2057. }
  2058. #endif /* CONFIG_KALLSYMS */
  2059. /* Called by the /proc file system to return a list of modules. */
  2060. static void *m_start(struct seq_file *m, loff_t *pos)
  2061. {
  2062. mutex_lock(&module_mutex);
  2063. return seq_list_start(&modules, *pos);
  2064. }
  2065. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2066. {
  2067. return seq_list_next(p, &modules, pos);
  2068. }
  2069. static void m_stop(struct seq_file *m, void *p)
  2070. {
  2071. mutex_unlock(&module_mutex);
  2072. }
  2073. static char *module_flags(struct module *mod, char *buf)
  2074. {
  2075. int bx = 0;
  2076. if (mod->taints ||
  2077. mod->state == MODULE_STATE_GOING ||
  2078. mod->state == MODULE_STATE_COMING) {
  2079. buf[bx++] = '(';
  2080. if (mod->taints & TAINT_PROPRIETARY_MODULE)
  2081. buf[bx++] = 'P';
  2082. if (mod->taints & TAINT_FORCED_MODULE)
  2083. buf[bx++] = 'F';
  2084. /*
  2085. * TAINT_FORCED_RMMOD: could be added.
  2086. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2087. * apply to modules.
  2088. */
  2089. /* Show a - for module-is-being-unloaded */
  2090. if (mod->state == MODULE_STATE_GOING)
  2091. buf[bx++] = '-';
  2092. /* Show a + for module-is-being-loaded */
  2093. if (mod->state == MODULE_STATE_COMING)
  2094. buf[bx++] = '+';
  2095. buf[bx++] = ')';
  2096. }
  2097. buf[bx] = '\0';
  2098. return buf;
  2099. }
  2100. static int m_show(struct seq_file *m, void *p)
  2101. {
  2102. struct module *mod = list_entry(p, struct module, list);
  2103. char buf[8];
  2104. seq_printf(m, "%s %lu",
  2105. mod->name, mod->init_size + mod->core_size);
  2106. print_unload_info(m, mod);
  2107. /* Informative for users. */
  2108. seq_printf(m, " %s",
  2109. mod->state == MODULE_STATE_GOING ? "Unloading":
  2110. mod->state == MODULE_STATE_COMING ? "Loading":
  2111. "Live");
  2112. /* Used by oprofile and other similar tools. */
  2113. seq_printf(m, " 0x%p", mod->module_core);
  2114. /* Taints info */
  2115. if (mod->taints)
  2116. seq_printf(m, " %s", module_flags(mod, buf));
  2117. seq_printf(m, "\n");
  2118. return 0;
  2119. }
  2120. /* Format: modulename size refcount deps address
  2121. Where refcount is a number or -, and deps is a comma-separated list
  2122. of depends or -.
  2123. */
  2124. const struct seq_operations modules_op = {
  2125. .start = m_start,
  2126. .next = m_next,
  2127. .stop = m_stop,
  2128. .show = m_show
  2129. };
  2130. /* Given an address, look for it in the module exception tables. */
  2131. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2132. {
  2133. const struct exception_table_entry *e = NULL;
  2134. struct module *mod;
  2135. preempt_disable();
  2136. list_for_each_entry(mod, &modules, list) {
  2137. if (mod->num_exentries == 0)
  2138. continue;
  2139. e = search_extable(mod->extable,
  2140. mod->extable + mod->num_exentries - 1,
  2141. addr);
  2142. if (e)
  2143. break;
  2144. }
  2145. preempt_enable();
  2146. /* Now, if we found one, we are running inside it now, hence
  2147. we cannot unload the module, hence no refcnt needed. */
  2148. return e;
  2149. }
  2150. /*
  2151. * Is this a valid module address?
  2152. */
  2153. int is_module_address(unsigned long addr)
  2154. {
  2155. struct module *mod;
  2156. preempt_disable();
  2157. list_for_each_entry(mod, &modules, list) {
  2158. if (within(addr, mod->module_core, mod->core_size)) {
  2159. preempt_enable();
  2160. return 1;
  2161. }
  2162. }
  2163. preempt_enable();
  2164. return 0;
  2165. }
  2166. /* Is this a valid kernel address? */
  2167. struct module *__module_text_address(unsigned long addr)
  2168. {
  2169. struct module *mod;
  2170. list_for_each_entry(mod, &modules, list)
  2171. if (within(addr, mod->module_init, mod->init_text_size)
  2172. || within(addr, mod->module_core, mod->core_text_size))
  2173. return mod;
  2174. return NULL;
  2175. }
  2176. struct module *module_text_address(unsigned long addr)
  2177. {
  2178. struct module *mod;
  2179. preempt_disable();
  2180. mod = __module_text_address(addr);
  2181. preempt_enable();
  2182. return mod;
  2183. }
  2184. /* Don't grab lock, we're oopsing. */
  2185. void print_modules(void)
  2186. {
  2187. struct module *mod;
  2188. char buf[8];
  2189. printk("Modules linked in:");
  2190. list_for_each_entry(mod, &modules, list)
  2191. printk(" %s%s", mod->name, module_flags(mod, buf));
  2192. if (last_unloaded_module[0])
  2193. printk(" [last unloaded: %s]", last_unloaded_module);
  2194. printk("\n");
  2195. }
  2196. #ifdef CONFIG_MODVERSIONS
  2197. /* Generate the signature for struct module here, too, for modversions. */
  2198. void struct_module(struct module *mod) { return; }
  2199. EXPORT_SYMBOL(struct_module);
  2200. #endif
  2201. #ifdef CONFIG_MARKERS
  2202. void module_update_markers(struct module *probe_module, int *refcount)
  2203. {
  2204. struct module *mod;
  2205. mutex_lock(&module_mutex);
  2206. list_for_each_entry(mod, &modules, list)
  2207. if (!mod->taints)
  2208. marker_update_probe_range(mod->markers,
  2209. mod->markers + mod->num_markers,
  2210. probe_module, refcount);
  2211. mutex_unlock(&module_mutex);
  2212. }
  2213. #endif