module.c 66 KB

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