module.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133
  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/ftrace_event.h>
  19. #include <linux/init.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/fs.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/elf.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/rcupdate.h>
  32. #include <linux/capability.h>
  33. #include <linux/cpu.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/errno.h>
  36. #include <linux/err.h>
  37. #include <linux/vermagic.h>
  38. #include <linux/notifier.h>
  39. #include <linux/sched.h>
  40. #include <linux/stop_machine.h>
  41. #include <linux/device.h>
  42. #include <linux/string.h>
  43. #include <linux/mutex.h>
  44. #include <linux/rculist.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/cacheflush.h>
  47. #include <asm/mmu_context.h>
  48. #include <linux/license.h>
  49. #include <asm/sections.h>
  50. #include <linux/tracepoint.h>
  51. #include <linux/ftrace.h>
  52. #include <linux/async.h>
  53. #include <linux/percpu.h>
  54. #include <linux/kmemleak.h>
  55. #define CREATE_TRACE_POINTS
  56. #include <trace/events/module.h>
  57. #if 0
  58. #define DEBUGP printk
  59. #else
  60. #define DEBUGP(fmt , a...)
  61. #endif
  62. #ifndef ARCH_SHF_SMALL
  63. #define ARCH_SHF_SMALL 0
  64. #endif
  65. /* If this is set, the section belongs in the init part of the module */
  66. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  67. /* List of modules, protected by module_mutex or preempt_disable
  68. * (delete uses stop_machine/add uses RCU list operations). */
  69. DEFINE_MUTEX(module_mutex);
  70. EXPORT_SYMBOL_GPL(module_mutex);
  71. static LIST_HEAD(modules);
  72. #ifdef CONFIG_KGDB_KDB
  73. struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
  74. #endif /* CONFIG_KGDB_KDB */
  75. /* Block module loading/unloading? */
  76. int modules_disabled = 0;
  77. /* Waiting for a module to finish initializing? */
  78. static DECLARE_WAIT_QUEUE_HEAD(module_wq);
  79. static BLOCKING_NOTIFIER_HEAD(module_notify_list);
  80. /* Bounds of module allocation, for speeding __module_address */
  81. static unsigned long module_addr_min = -1UL, module_addr_max = 0;
  82. int register_module_notifier(struct notifier_block * nb)
  83. {
  84. return blocking_notifier_chain_register(&module_notify_list, nb);
  85. }
  86. EXPORT_SYMBOL(register_module_notifier);
  87. int unregister_module_notifier(struct notifier_block * nb)
  88. {
  89. return blocking_notifier_chain_unregister(&module_notify_list, nb);
  90. }
  91. EXPORT_SYMBOL(unregister_module_notifier);
  92. /* We require a truly strong try_module_get(): 0 means failure due to
  93. ongoing or failed initialization etc. */
  94. static inline int strong_try_module_get(struct module *mod)
  95. {
  96. if (mod && mod->state == MODULE_STATE_COMING)
  97. return -EBUSY;
  98. if (try_module_get(mod))
  99. return 0;
  100. else
  101. return -ENOENT;
  102. }
  103. static inline void add_taint_module(struct module *mod, unsigned flag)
  104. {
  105. add_taint(flag);
  106. mod->taints |= (1U << flag);
  107. }
  108. /*
  109. * A thread that wants to hold a reference to a module only while it
  110. * is running can call this to safely exit. nfsd and lockd use this.
  111. */
  112. void __module_put_and_exit(struct module *mod, long code)
  113. {
  114. module_put(mod);
  115. do_exit(code);
  116. }
  117. EXPORT_SYMBOL(__module_put_and_exit);
  118. /* Find a module section: 0 means not found. */
  119. static unsigned int find_sec(Elf_Ehdr *hdr,
  120. Elf_Shdr *sechdrs,
  121. const char *secstrings,
  122. const char *name)
  123. {
  124. unsigned int i;
  125. for (i = 1; i < hdr->e_shnum; i++)
  126. /* Alloc bit cleared means "ignore it." */
  127. if ((sechdrs[i].sh_flags & SHF_ALLOC)
  128. && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
  129. return i;
  130. return 0;
  131. }
  132. /* Find a module section, or NULL. */
  133. static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
  134. const char *secstrings, const char *name)
  135. {
  136. /* Section 0 has sh_addr 0. */
  137. return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
  138. }
  139. /* Find a module section, or NULL. Fill in number of "objects" in section. */
  140. static void *section_objs(Elf_Ehdr *hdr,
  141. Elf_Shdr *sechdrs,
  142. const char *secstrings,
  143. const char *name,
  144. size_t object_size,
  145. unsigned int *num)
  146. {
  147. unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
  148. /* Section 0 has sh_addr 0 and sh_size 0. */
  149. *num = sechdrs[sec].sh_size / object_size;
  150. return (void *)sechdrs[sec].sh_addr;
  151. }
  152. /* Provided by the linker */
  153. extern const struct kernel_symbol __start___ksymtab[];
  154. extern const struct kernel_symbol __stop___ksymtab[];
  155. extern const struct kernel_symbol __start___ksymtab_gpl[];
  156. extern const struct kernel_symbol __stop___ksymtab_gpl[];
  157. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  158. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  159. extern const unsigned long __start___kcrctab[];
  160. extern const unsigned long __start___kcrctab_gpl[];
  161. extern const unsigned long __start___kcrctab_gpl_future[];
  162. #ifdef CONFIG_UNUSED_SYMBOLS
  163. extern const struct kernel_symbol __start___ksymtab_unused[];
  164. extern const struct kernel_symbol __stop___ksymtab_unused[];
  165. extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
  166. extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
  167. extern const unsigned long __start___kcrctab_unused[];
  168. extern const unsigned long __start___kcrctab_unused_gpl[];
  169. #endif
  170. #ifndef CONFIG_MODVERSIONS
  171. #define symversion(base, idx) NULL
  172. #else
  173. #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
  174. #endif
  175. static bool each_symbol_in_section(const struct symsearch *arr,
  176. unsigned int arrsize,
  177. struct module *owner,
  178. bool (*fn)(const struct symsearch *syms,
  179. struct module *owner,
  180. unsigned int symnum, void *data),
  181. void *data)
  182. {
  183. unsigned int i, j;
  184. for (j = 0; j < arrsize; j++) {
  185. for (i = 0; i < arr[j].stop - arr[j].start; i++)
  186. if (fn(&arr[j], owner, i, data))
  187. return true;
  188. }
  189. return false;
  190. }
  191. /* Returns true as soon as fn returns true, otherwise false. */
  192. bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
  193. unsigned int symnum, void *data), void *data)
  194. {
  195. struct module *mod;
  196. const struct symsearch arr[] = {
  197. { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
  198. NOT_GPL_ONLY, false },
  199. { __start___ksymtab_gpl, __stop___ksymtab_gpl,
  200. __start___kcrctab_gpl,
  201. GPL_ONLY, false },
  202. { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
  203. __start___kcrctab_gpl_future,
  204. WILL_BE_GPL_ONLY, false },
  205. #ifdef CONFIG_UNUSED_SYMBOLS
  206. { __start___ksymtab_unused, __stop___ksymtab_unused,
  207. __start___kcrctab_unused,
  208. NOT_GPL_ONLY, true },
  209. { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
  210. __start___kcrctab_unused_gpl,
  211. GPL_ONLY, true },
  212. #endif
  213. };
  214. if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
  215. return true;
  216. list_for_each_entry_rcu(mod, &modules, list) {
  217. struct symsearch arr[] = {
  218. { mod->syms, mod->syms + mod->num_syms, mod->crcs,
  219. NOT_GPL_ONLY, false },
  220. { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
  221. mod->gpl_crcs,
  222. GPL_ONLY, false },
  223. { mod->gpl_future_syms,
  224. mod->gpl_future_syms + mod->num_gpl_future_syms,
  225. mod->gpl_future_crcs,
  226. WILL_BE_GPL_ONLY, false },
  227. #ifdef CONFIG_UNUSED_SYMBOLS
  228. { mod->unused_syms,
  229. mod->unused_syms + mod->num_unused_syms,
  230. mod->unused_crcs,
  231. NOT_GPL_ONLY, true },
  232. { mod->unused_gpl_syms,
  233. mod->unused_gpl_syms + mod->num_unused_gpl_syms,
  234. mod->unused_gpl_crcs,
  235. GPL_ONLY, true },
  236. #endif
  237. };
  238. if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
  239. return true;
  240. }
  241. return false;
  242. }
  243. EXPORT_SYMBOL_GPL(each_symbol);
  244. struct find_symbol_arg {
  245. /* Input */
  246. const char *name;
  247. bool gplok;
  248. bool warn;
  249. /* Output */
  250. struct module *owner;
  251. const unsigned long *crc;
  252. const struct kernel_symbol *sym;
  253. };
  254. static bool find_symbol_in_section(const struct symsearch *syms,
  255. struct module *owner,
  256. unsigned int symnum, void *data)
  257. {
  258. struct find_symbol_arg *fsa = data;
  259. if (strcmp(syms->start[symnum].name, fsa->name) != 0)
  260. return false;
  261. if (!fsa->gplok) {
  262. if (syms->licence == GPL_ONLY)
  263. return false;
  264. if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
  265. printk(KERN_WARNING "Symbol %s is being used "
  266. "by a non-GPL module, which will not "
  267. "be allowed in the future\n", fsa->name);
  268. printk(KERN_WARNING "Please see the file "
  269. "Documentation/feature-removal-schedule.txt "
  270. "in the kernel source tree for more details.\n");
  271. }
  272. }
  273. #ifdef CONFIG_UNUSED_SYMBOLS
  274. if (syms->unused && fsa->warn) {
  275. printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
  276. "however this module is using it.\n", fsa->name);
  277. printk(KERN_WARNING
  278. "This symbol will go away in the future.\n");
  279. printk(KERN_WARNING
  280. "Please evalute if this is the right api to use and if "
  281. "it really is, submit a report the linux kernel "
  282. "mailinglist together with submitting your code for "
  283. "inclusion.\n");
  284. }
  285. #endif
  286. fsa->owner = owner;
  287. fsa->crc = symversion(syms->crcs, symnum);
  288. fsa->sym = &syms->start[symnum];
  289. return true;
  290. }
  291. /* Find a symbol and return it, along with, (optional) crc and
  292. * (optional) module which owns it */
  293. const struct kernel_symbol *find_symbol(const char *name,
  294. struct module **owner,
  295. const unsigned long **crc,
  296. bool gplok,
  297. bool warn)
  298. {
  299. struct find_symbol_arg fsa;
  300. fsa.name = name;
  301. fsa.gplok = gplok;
  302. fsa.warn = warn;
  303. if (each_symbol(find_symbol_in_section, &fsa)) {
  304. if (owner)
  305. *owner = fsa.owner;
  306. if (crc)
  307. *crc = fsa.crc;
  308. return fsa.sym;
  309. }
  310. DEBUGP("Failed to find symbol %s\n", name);
  311. return NULL;
  312. }
  313. EXPORT_SYMBOL_GPL(find_symbol);
  314. /* Search for module by name: must hold module_mutex. */
  315. struct module *find_module(const char *name)
  316. {
  317. struct module *mod;
  318. list_for_each_entry(mod, &modules, list) {
  319. if (strcmp(mod->name, name) == 0)
  320. return mod;
  321. }
  322. return NULL;
  323. }
  324. EXPORT_SYMBOL_GPL(find_module);
  325. #ifdef CONFIG_SMP
  326. static inline void __percpu *mod_percpu(struct module *mod)
  327. {
  328. return mod->percpu;
  329. }
  330. static int percpu_modalloc(struct module *mod,
  331. unsigned long size, unsigned long align)
  332. {
  333. if (align > PAGE_SIZE) {
  334. printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
  335. mod->name, align, PAGE_SIZE);
  336. align = PAGE_SIZE;
  337. }
  338. mod->percpu = __alloc_reserved_percpu(size, align);
  339. if (!mod->percpu) {
  340. printk(KERN_WARNING
  341. "Could not allocate %lu bytes percpu data\n", size);
  342. return -ENOMEM;
  343. }
  344. mod->percpu_size = size;
  345. return 0;
  346. }
  347. static void percpu_modfree(struct module *mod)
  348. {
  349. free_percpu(mod->percpu);
  350. }
  351. static unsigned int find_pcpusec(Elf_Ehdr *hdr,
  352. Elf_Shdr *sechdrs,
  353. const char *secstrings)
  354. {
  355. return find_sec(hdr, sechdrs, secstrings, ".data..percpu");
  356. }
  357. static void percpu_modcopy(struct module *mod,
  358. const void *from, unsigned long size)
  359. {
  360. int cpu;
  361. for_each_possible_cpu(cpu)
  362. memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
  363. }
  364. /**
  365. * is_module_percpu_address - test whether address is from module static percpu
  366. * @addr: address to test
  367. *
  368. * Test whether @addr belongs to module static percpu area.
  369. *
  370. * RETURNS:
  371. * %true if @addr is from module static percpu area
  372. */
  373. bool is_module_percpu_address(unsigned long addr)
  374. {
  375. struct module *mod;
  376. unsigned int cpu;
  377. preempt_disable();
  378. list_for_each_entry_rcu(mod, &modules, list) {
  379. if (!mod->percpu_size)
  380. continue;
  381. for_each_possible_cpu(cpu) {
  382. void *start = per_cpu_ptr(mod->percpu, cpu);
  383. if ((void *)addr >= start &&
  384. (void *)addr < start + mod->percpu_size) {
  385. preempt_enable();
  386. return true;
  387. }
  388. }
  389. }
  390. preempt_enable();
  391. return false;
  392. }
  393. #else /* ... !CONFIG_SMP */
  394. static inline void __percpu *mod_percpu(struct module *mod)
  395. {
  396. return NULL;
  397. }
  398. static inline int percpu_modalloc(struct module *mod,
  399. unsigned long size, unsigned long align)
  400. {
  401. return -ENOMEM;
  402. }
  403. static inline void percpu_modfree(struct module *mod)
  404. {
  405. }
  406. static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
  407. Elf_Shdr *sechdrs,
  408. const char *secstrings)
  409. {
  410. return 0;
  411. }
  412. static inline void percpu_modcopy(struct module *mod,
  413. const void *from, unsigned long size)
  414. {
  415. /* pcpusec should be 0, and size of that section should be 0. */
  416. BUG_ON(size != 0);
  417. }
  418. bool is_module_percpu_address(unsigned long addr)
  419. {
  420. return false;
  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. EXPORT_TRACEPOINT_SYMBOL(module_get);
  454. /* Init the unload section of the module. */
  455. static void module_unload_init(struct module *mod)
  456. {
  457. int cpu;
  458. INIT_LIST_HEAD(&mod->source_list);
  459. INIT_LIST_HEAD(&mod->target_list);
  460. for_each_possible_cpu(cpu) {
  461. per_cpu_ptr(mod->refptr, cpu)->incs = 0;
  462. per_cpu_ptr(mod->refptr, cpu)->decs = 0;
  463. }
  464. /* Hold reference count during initialization. */
  465. __this_cpu_write(mod->refptr->incs, 1);
  466. /* Backwards compatibility macros put refcount during init. */
  467. mod->waiter = current;
  468. }
  469. /* Does a already use b? */
  470. static int already_uses(struct module *a, struct module *b)
  471. {
  472. struct module_use *use;
  473. list_for_each_entry(use, &b->source_list, source_list) {
  474. if (use->source == a) {
  475. DEBUGP("%s uses %s!\n", a->name, b->name);
  476. return 1;
  477. }
  478. }
  479. DEBUGP("%s does not use %s!\n", a->name, b->name);
  480. return 0;
  481. }
  482. /*
  483. * Module a uses b
  484. * - we add 'a' as a "source", 'b' as a "target" of module use
  485. * - the module_use is added to the list of 'b' sources (so
  486. * 'b' can walk the list to see who sourced them), and of 'a'
  487. * targets (so 'a' can see what modules it targets).
  488. */
  489. static int add_module_usage(struct module *a, struct module *b)
  490. {
  491. struct module_use *use;
  492. DEBUGP("Allocating new usage for %s.\n", a->name);
  493. use = kmalloc(sizeof(*use), GFP_ATOMIC);
  494. if (!use) {
  495. printk(KERN_WARNING "%s: out of memory loading\n", a->name);
  496. return -ENOMEM;
  497. }
  498. use->source = a;
  499. use->target = b;
  500. list_add(&use->source_list, &b->source_list);
  501. list_add(&use->target_list, &a->target_list);
  502. return 0;
  503. }
  504. /* Module a uses b */
  505. int use_module(struct module *a, struct module *b)
  506. {
  507. int err;
  508. if (b == NULL || already_uses(a, b)) return 1;
  509. /* If we're interrupted or time out, we fail. */
  510. if (wait_event_interruptible_timeout(
  511. module_wq, (err = strong_try_module_get(b)) != -EBUSY,
  512. 30 * HZ) <= 0) {
  513. printk("%s: gave up waiting for init of module %s.\n",
  514. a->name, b->name);
  515. return 0;
  516. }
  517. /* If strong_try_module_get() returned a different error, we fail. */
  518. if (err)
  519. return 0;
  520. err = add_module_usage(a, b);
  521. if (err) {
  522. module_put(b);
  523. return 0;
  524. }
  525. return 1;
  526. }
  527. EXPORT_SYMBOL_GPL(use_module);
  528. /* Clear the unload stuff of the module. */
  529. static void module_unload_free(struct module *mod)
  530. {
  531. struct module_use *use, *tmp;
  532. list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
  533. struct module *i = use->target;
  534. DEBUGP("%s unusing %s\n", mod->name, i->name);
  535. module_put(i);
  536. list_del(&use->source_list);
  537. list_del(&use->target_list);
  538. kfree(use);
  539. }
  540. }
  541. #ifdef CONFIG_MODULE_FORCE_UNLOAD
  542. static inline int try_force_unload(unsigned int flags)
  543. {
  544. int ret = (flags & O_TRUNC);
  545. if (ret)
  546. add_taint(TAINT_FORCED_RMMOD);
  547. return ret;
  548. }
  549. #else
  550. static inline int try_force_unload(unsigned int flags)
  551. {
  552. return 0;
  553. }
  554. #endif /* CONFIG_MODULE_FORCE_UNLOAD */
  555. struct stopref
  556. {
  557. struct module *mod;
  558. int flags;
  559. int *forced;
  560. };
  561. /* Whole machine is stopped with interrupts off when this runs. */
  562. static int __try_stop_module(void *_sref)
  563. {
  564. struct stopref *sref = _sref;
  565. /* If it's not unused, quit unless we're forcing. */
  566. if (module_refcount(sref->mod) != 0) {
  567. if (!(*sref->forced = try_force_unload(sref->flags)))
  568. return -EWOULDBLOCK;
  569. }
  570. /* Mark it as dying. */
  571. sref->mod->state = MODULE_STATE_GOING;
  572. return 0;
  573. }
  574. static int try_stop_module(struct module *mod, int flags, int *forced)
  575. {
  576. if (flags & O_NONBLOCK) {
  577. struct stopref sref = { mod, flags, forced };
  578. return stop_machine(__try_stop_module, &sref, NULL);
  579. } else {
  580. /* We don't need to stop the machine for this. */
  581. mod->state = MODULE_STATE_GOING;
  582. synchronize_sched();
  583. return 0;
  584. }
  585. }
  586. unsigned int module_refcount(struct module *mod)
  587. {
  588. unsigned int incs = 0, decs = 0;
  589. int cpu;
  590. for_each_possible_cpu(cpu)
  591. decs += per_cpu_ptr(mod->refptr, cpu)->decs;
  592. /*
  593. * ensure the incs are added up after the decs.
  594. * module_put ensures incs are visible before decs with smp_wmb.
  595. *
  596. * This 2-count scheme avoids the situation where the refcount
  597. * for CPU0 is read, then CPU0 increments the module refcount,
  598. * then CPU1 drops that refcount, then the refcount for CPU1 is
  599. * read. We would record a decrement but not its corresponding
  600. * increment so we would see a low count (disaster).
  601. *
  602. * Rare situation? But module_refcount can be preempted, and we
  603. * might be tallying up 4096+ CPUs. So it is not impossible.
  604. */
  605. smp_rmb();
  606. for_each_possible_cpu(cpu)
  607. incs += per_cpu_ptr(mod->refptr, cpu)->incs;
  608. return incs - decs;
  609. }
  610. EXPORT_SYMBOL(module_refcount);
  611. /* This exists whether we can unload or not */
  612. static void free_module(struct module *mod);
  613. static void wait_for_zero_refcount(struct module *mod)
  614. {
  615. /* Since we might sleep for some time, release the mutex first */
  616. mutex_unlock(&module_mutex);
  617. for (;;) {
  618. DEBUGP("Looking at refcount...\n");
  619. set_current_state(TASK_UNINTERRUPTIBLE);
  620. if (module_refcount(mod) == 0)
  621. break;
  622. schedule();
  623. }
  624. current->state = TASK_RUNNING;
  625. mutex_lock(&module_mutex);
  626. }
  627. SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
  628. unsigned int, flags)
  629. {
  630. struct module *mod;
  631. char name[MODULE_NAME_LEN];
  632. int ret, forced = 0;
  633. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  634. return -EPERM;
  635. if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
  636. return -EFAULT;
  637. name[MODULE_NAME_LEN-1] = '\0';
  638. if (mutex_lock_interruptible(&module_mutex) != 0)
  639. return -EINTR;
  640. mod = find_module(name);
  641. if (!mod) {
  642. ret = -ENOENT;
  643. goto out;
  644. }
  645. if (!list_empty(&mod->source_list)) {
  646. /* Other modules depend on us: get rid of them first. */
  647. ret = -EWOULDBLOCK;
  648. goto out;
  649. }
  650. /* Doing init or already dying? */
  651. if (mod->state != MODULE_STATE_LIVE) {
  652. /* FIXME: if (force), slam module count and wake up
  653. waiter --RR */
  654. DEBUGP("%s already dying\n", mod->name);
  655. ret = -EBUSY;
  656. goto out;
  657. }
  658. /* If it has an init func, it must have an exit func to unload */
  659. if (mod->init && !mod->exit) {
  660. forced = try_force_unload(flags);
  661. if (!forced) {
  662. /* This module can't be removed */
  663. ret = -EBUSY;
  664. goto out;
  665. }
  666. }
  667. /* Set this up before setting mod->state */
  668. mod->waiter = current;
  669. /* Stop the machine so refcounts can't move and disable module. */
  670. ret = try_stop_module(mod, flags, &forced);
  671. if (ret != 0)
  672. goto out;
  673. /* Never wait if forced. */
  674. if (!forced && module_refcount(mod) != 0)
  675. wait_for_zero_refcount(mod);
  676. mutex_unlock(&module_mutex);
  677. /* Final destruction now noone is using it. */
  678. if (mod->exit != NULL)
  679. mod->exit();
  680. blocking_notifier_call_chain(&module_notify_list,
  681. MODULE_STATE_GOING, mod);
  682. async_synchronize_full();
  683. mutex_lock(&module_mutex);
  684. /* Store the name of the last unloaded module for diagnostic purposes */
  685. strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
  686. ddebug_remove_module(mod->name);
  687. free_module(mod);
  688. out:
  689. mutex_unlock(&module_mutex);
  690. return ret;
  691. }
  692. static inline void print_unload_info(struct seq_file *m, struct module *mod)
  693. {
  694. struct module_use *use;
  695. int printed_something = 0;
  696. seq_printf(m, " %u ", module_refcount(mod));
  697. /* Always include a trailing , so userspace can differentiate
  698. between this and the old multi-field proc format. */
  699. list_for_each_entry(use, &mod->source_list, source_list) {
  700. printed_something = 1;
  701. seq_printf(m, "%s,", use->source->name);
  702. }
  703. if (mod->init != NULL && mod->exit == NULL) {
  704. printed_something = 1;
  705. seq_printf(m, "[permanent],");
  706. }
  707. if (!printed_something)
  708. seq_printf(m, "-");
  709. }
  710. void __symbol_put(const char *symbol)
  711. {
  712. struct module *owner;
  713. preempt_disable();
  714. if (!find_symbol(symbol, &owner, NULL, true, false))
  715. BUG();
  716. module_put(owner);
  717. preempt_enable();
  718. }
  719. EXPORT_SYMBOL(__symbol_put);
  720. /* Note this assumes addr is a function, which it currently always is. */
  721. void symbol_put_addr(void *addr)
  722. {
  723. struct module *modaddr;
  724. unsigned long a = (unsigned long)dereference_function_descriptor(addr);
  725. if (core_kernel_text(a))
  726. return;
  727. /* module_text_address is safe here: we're supposed to have reference
  728. * to module from symbol_get, so it can't go away. */
  729. modaddr = __module_text_address(a);
  730. BUG_ON(!modaddr);
  731. module_put(modaddr);
  732. }
  733. EXPORT_SYMBOL_GPL(symbol_put_addr);
  734. static ssize_t show_refcnt(struct module_attribute *mattr,
  735. struct module *mod, char *buffer)
  736. {
  737. return sprintf(buffer, "%u\n", module_refcount(mod));
  738. }
  739. static struct module_attribute refcnt = {
  740. .attr = { .name = "refcnt", .mode = 0444 },
  741. .show = show_refcnt,
  742. };
  743. void module_put(struct module *module)
  744. {
  745. if (module) {
  746. preempt_disable();
  747. smp_wmb(); /* see comment in module_refcount */
  748. __this_cpu_inc(module->refptr->decs);
  749. trace_module_put(module, _RET_IP_);
  750. /* Maybe they're waiting for us to drop reference? */
  751. if (unlikely(!module_is_live(module)))
  752. wake_up_process(module->waiter);
  753. preempt_enable();
  754. }
  755. }
  756. EXPORT_SYMBOL(module_put);
  757. #else /* !CONFIG_MODULE_UNLOAD */
  758. static inline void print_unload_info(struct seq_file *m, struct module *mod)
  759. {
  760. /* We don't know the usage count, or what modules are using. */
  761. seq_printf(m, " - -");
  762. }
  763. static inline void module_unload_free(struct module *mod)
  764. {
  765. }
  766. int use_module(struct module *a, struct module *b)
  767. {
  768. return strong_try_module_get(b) == 0;
  769. }
  770. EXPORT_SYMBOL_GPL(use_module);
  771. static inline void module_unload_init(struct module *mod)
  772. {
  773. }
  774. #endif /* CONFIG_MODULE_UNLOAD */
  775. static ssize_t show_initstate(struct module_attribute *mattr,
  776. struct module *mod, char *buffer)
  777. {
  778. const char *state = "unknown";
  779. switch (mod->state) {
  780. case MODULE_STATE_LIVE:
  781. state = "live";
  782. break;
  783. case MODULE_STATE_COMING:
  784. state = "coming";
  785. break;
  786. case MODULE_STATE_GOING:
  787. state = "going";
  788. break;
  789. }
  790. return sprintf(buffer, "%s\n", state);
  791. }
  792. static struct module_attribute initstate = {
  793. .attr = { .name = "initstate", .mode = 0444 },
  794. .show = show_initstate,
  795. };
  796. static struct module_attribute *modinfo_attrs[] = {
  797. &modinfo_version,
  798. &modinfo_srcversion,
  799. &initstate,
  800. #ifdef CONFIG_MODULE_UNLOAD
  801. &refcnt,
  802. #endif
  803. NULL,
  804. };
  805. static const char vermagic[] = VERMAGIC_STRING;
  806. static int try_to_force_load(struct module *mod, const char *reason)
  807. {
  808. #ifdef CONFIG_MODULE_FORCE_LOAD
  809. if (!test_taint(TAINT_FORCED_MODULE))
  810. printk(KERN_WARNING "%s: %s: kernel tainted.\n",
  811. mod->name, reason);
  812. add_taint_module(mod, TAINT_FORCED_MODULE);
  813. return 0;
  814. #else
  815. return -ENOEXEC;
  816. #endif
  817. }
  818. #ifdef CONFIG_MODVERSIONS
  819. /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
  820. static unsigned long maybe_relocated(unsigned long crc,
  821. const struct module *crc_owner)
  822. {
  823. #ifdef ARCH_RELOCATES_KCRCTAB
  824. if (crc_owner == NULL)
  825. return crc - (unsigned long)reloc_start;
  826. #endif
  827. return crc;
  828. }
  829. static int check_version(Elf_Shdr *sechdrs,
  830. unsigned int versindex,
  831. const char *symname,
  832. struct module *mod,
  833. const unsigned long *crc,
  834. const struct module *crc_owner)
  835. {
  836. unsigned int i, num_versions;
  837. struct modversion_info *versions;
  838. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  839. if (!crc)
  840. return 1;
  841. /* No versions at all? modprobe --force does this. */
  842. if (versindex == 0)
  843. return try_to_force_load(mod, symname) == 0;
  844. versions = (void *) sechdrs[versindex].sh_addr;
  845. num_versions = sechdrs[versindex].sh_size
  846. / sizeof(struct modversion_info);
  847. for (i = 0; i < num_versions; i++) {
  848. if (strcmp(versions[i].name, symname) != 0)
  849. continue;
  850. if (versions[i].crc == maybe_relocated(*crc, crc_owner))
  851. return 1;
  852. DEBUGP("Found checksum %lX vs module %lX\n",
  853. maybe_relocated(*crc, crc_owner), versions[i].crc);
  854. goto bad_version;
  855. }
  856. printk(KERN_WARNING "%s: no symbol version for %s\n",
  857. mod->name, symname);
  858. return 0;
  859. bad_version:
  860. printk("%s: disagrees about version of symbol %s\n",
  861. mod->name, symname);
  862. return 0;
  863. }
  864. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  865. unsigned int versindex,
  866. struct module *mod)
  867. {
  868. const unsigned long *crc;
  869. if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
  870. &crc, true, false))
  871. BUG();
  872. return check_version(sechdrs, versindex, "module_layout", mod, crc,
  873. NULL);
  874. }
  875. /* First part is kernel version, which we ignore if module has crcs. */
  876. static inline int same_magic(const char *amagic, const char *bmagic,
  877. bool has_crcs)
  878. {
  879. if (has_crcs) {
  880. amagic += strcspn(amagic, " ");
  881. bmagic += strcspn(bmagic, " ");
  882. }
  883. return strcmp(amagic, bmagic) == 0;
  884. }
  885. #else
  886. static inline int check_version(Elf_Shdr *sechdrs,
  887. unsigned int versindex,
  888. const char *symname,
  889. struct module *mod,
  890. const unsigned long *crc,
  891. const struct module *crc_owner)
  892. {
  893. return 1;
  894. }
  895. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  896. unsigned int versindex,
  897. struct module *mod)
  898. {
  899. return 1;
  900. }
  901. static inline int same_magic(const char *amagic, const char *bmagic,
  902. bool has_crcs)
  903. {
  904. return strcmp(amagic, bmagic) == 0;
  905. }
  906. #endif /* CONFIG_MODVERSIONS */
  907. /* Resolve a symbol for this module. I.e. if we find one, record usage.
  908. Must be holding module_mutex. */
  909. static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
  910. unsigned int versindex,
  911. const char *name,
  912. struct module *mod)
  913. {
  914. struct module *owner;
  915. const struct kernel_symbol *sym;
  916. const unsigned long *crc;
  917. sym = find_symbol(name, &owner, &crc,
  918. !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
  919. /* use_module can fail due to OOM,
  920. or module initialization or unloading */
  921. if (sym) {
  922. if (!check_version(sechdrs, versindex, name, mod, crc, owner)
  923. || !use_module(mod, owner))
  924. sym = NULL;
  925. }
  926. return sym;
  927. }
  928. /*
  929. * /sys/module/foo/sections stuff
  930. * J. Corbet <corbet@lwn.net>
  931. */
  932. #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
  933. static inline bool sect_empty(const Elf_Shdr *sect)
  934. {
  935. return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
  936. }
  937. struct module_sect_attr
  938. {
  939. struct module_attribute mattr;
  940. char *name;
  941. unsigned long address;
  942. };
  943. struct module_sect_attrs
  944. {
  945. struct attribute_group grp;
  946. unsigned int nsections;
  947. struct module_sect_attr attrs[0];
  948. };
  949. static ssize_t module_sect_show(struct module_attribute *mattr,
  950. struct module *mod, char *buf)
  951. {
  952. struct module_sect_attr *sattr =
  953. container_of(mattr, struct module_sect_attr, mattr);
  954. return sprintf(buf, "0x%lx\n", sattr->address);
  955. }
  956. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  957. {
  958. unsigned int section;
  959. for (section = 0; section < sect_attrs->nsections; section++)
  960. kfree(sect_attrs->attrs[section].name);
  961. kfree(sect_attrs);
  962. }
  963. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  964. char *secstrings, Elf_Shdr *sechdrs)
  965. {
  966. unsigned int nloaded = 0, i, size[2];
  967. struct module_sect_attrs *sect_attrs;
  968. struct module_sect_attr *sattr;
  969. struct attribute **gattr;
  970. /* Count loaded sections and allocate structures */
  971. for (i = 0; i < nsect; i++)
  972. if (!sect_empty(&sechdrs[i]))
  973. nloaded++;
  974. size[0] = ALIGN(sizeof(*sect_attrs)
  975. + nloaded * sizeof(sect_attrs->attrs[0]),
  976. sizeof(sect_attrs->grp.attrs[0]));
  977. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  978. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  979. if (sect_attrs == NULL)
  980. return;
  981. /* Setup section attributes. */
  982. sect_attrs->grp.name = "sections";
  983. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  984. sect_attrs->nsections = 0;
  985. sattr = &sect_attrs->attrs[0];
  986. gattr = &sect_attrs->grp.attrs[0];
  987. for (i = 0; i < nsect; i++) {
  988. if (sect_empty(&sechdrs[i]))
  989. continue;
  990. sattr->address = sechdrs[i].sh_addr;
  991. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  992. GFP_KERNEL);
  993. if (sattr->name == NULL)
  994. goto out;
  995. sect_attrs->nsections++;
  996. sysfs_attr_init(&sattr->mattr.attr);
  997. sattr->mattr.show = module_sect_show;
  998. sattr->mattr.store = NULL;
  999. sattr->mattr.attr.name = sattr->name;
  1000. sattr->mattr.attr.mode = S_IRUGO;
  1001. *(gattr++) = &(sattr++)->mattr.attr;
  1002. }
  1003. *gattr = NULL;
  1004. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  1005. goto out;
  1006. mod->sect_attrs = sect_attrs;
  1007. return;
  1008. out:
  1009. free_sect_attrs(sect_attrs);
  1010. }
  1011. static void remove_sect_attrs(struct module *mod)
  1012. {
  1013. if (mod->sect_attrs) {
  1014. sysfs_remove_group(&mod->mkobj.kobj,
  1015. &mod->sect_attrs->grp);
  1016. /* We are positive that no one is using any sect attrs
  1017. * at this point. Deallocate immediately. */
  1018. free_sect_attrs(mod->sect_attrs);
  1019. mod->sect_attrs = NULL;
  1020. }
  1021. }
  1022. /*
  1023. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  1024. */
  1025. struct module_notes_attrs {
  1026. struct kobject *dir;
  1027. unsigned int notes;
  1028. struct bin_attribute attrs[0];
  1029. };
  1030. static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
  1031. struct bin_attribute *bin_attr,
  1032. char *buf, loff_t pos, size_t count)
  1033. {
  1034. /*
  1035. * The caller checked the pos and count against our size.
  1036. */
  1037. memcpy(buf, bin_attr->private + pos, count);
  1038. return count;
  1039. }
  1040. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  1041. unsigned int i)
  1042. {
  1043. if (notes_attrs->dir) {
  1044. while (i-- > 0)
  1045. sysfs_remove_bin_file(notes_attrs->dir,
  1046. &notes_attrs->attrs[i]);
  1047. kobject_put(notes_attrs->dir);
  1048. }
  1049. kfree(notes_attrs);
  1050. }
  1051. static void add_notes_attrs(struct module *mod, unsigned int nsect,
  1052. char *secstrings, Elf_Shdr *sechdrs)
  1053. {
  1054. unsigned int notes, loaded, i;
  1055. struct module_notes_attrs *notes_attrs;
  1056. struct bin_attribute *nattr;
  1057. /* failed to create section attributes, so can't create notes */
  1058. if (!mod->sect_attrs)
  1059. return;
  1060. /* Count notes sections and allocate structures. */
  1061. notes = 0;
  1062. for (i = 0; i < nsect; i++)
  1063. if (!sect_empty(&sechdrs[i]) &&
  1064. (sechdrs[i].sh_type == SHT_NOTE))
  1065. ++notes;
  1066. if (notes == 0)
  1067. return;
  1068. notes_attrs = kzalloc(sizeof(*notes_attrs)
  1069. + notes * sizeof(notes_attrs->attrs[0]),
  1070. GFP_KERNEL);
  1071. if (notes_attrs == NULL)
  1072. return;
  1073. notes_attrs->notes = notes;
  1074. nattr = &notes_attrs->attrs[0];
  1075. for (loaded = i = 0; i < nsect; ++i) {
  1076. if (sect_empty(&sechdrs[i]))
  1077. continue;
  1078. if (sechdrs[i].sh_type == SHT_NOTE) {
  1079. sysfs_bin_attr_init(nattr);
  1080. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  1081. nattr->attr.mode = S_IRUGO;
  1082. nattr->size = sechdrs[i].sh_size;
  1083. nattr->private = (void *) sechdrs[i].sh_addr;
  1084. nattr->read = module_notes_read;
  1085. ++nattr;
  1086. }
  1087. ++loaded;
  1088. }
  1089. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  1090. if (!notes_attrs->dir)
  1091. goto out;
  1092. for (i = 0; i < notes; ++i)
  1093. if (sysfs_create_bin_file(notes_attrs->dir,
  1094. &notes_attrs->attrs[i]))
  1095. goto out;
  1096. mod->notes_attrs = notes_attrs;
  1097. return;
  1098. out:
  1099. free_notes_attrs(notes_attrs, i);
  1100. }
  1101. static void remove_notes_attrs(struct module *mod)
  1102. {
  1103. if (mod->notes_attrs)
  1104. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1105. }
  1106. #else
  1107. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  1108. char *sectstrings, Elf_Shdr *sechdrs)
  1109. {
  1110. }
  1111. static inline void remove_sect_attrs(struct module *mod)
  1112. {
  1113. }
  1114. static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
  1115. char *sectstrings, Elf_Shdr *sechdrs)
  1116. {
  1117. }
  1118. static inline void remove_notes_attrs(struct module *mod)
  1119. {
  1120. }
  1121. #endif
  1122. #ifdef CONFIG_SYSFS
  1123. static void add_usage_links(struct module *mod)
  1124. {
  1125. #ifdef CONFIG_MODULE_UNLOAD
  1126. struct module_use *use;
  1127. int nowarn;
  1128. list_for_each_entry(use, &mod->target_list, target_list) {
  1129. nowarn = sysfs_create_link(use->target->holders_dir,
  1130. &mod->mkobj.kobj, mod->name);
  1131. }
  1132. #endif
  1133. }
  1134. static void del_usage_links(struct module *mod)
  1135. {
  1136. #ifdef CONFIG_MODULE_UNLOAD
  1137. struct module_use *use;
  1138. list_for_each_entry(use, &mod->target_list, target_list)
  1139. sysfs_remove_link(use->target->holders_dir, mod->name);
  1140. #endif
  1141. }
  1142. static int module_add_modinfo_attrs(struct module *mod)
  1143. {
  1144. struct module_attribute *attr;
  1145. struct module_attribute *temp_attr;
  1146. int error = 0;
  1147. int i;
  1148. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1149. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1150. GFP_KERNEL);
  1151. if (!mod->modinfo_attrs)
  1152. return -ENOMEM;
  1153. temp_attr = mod->modinfo_attrs;
  1154. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1155. if (!attr->test ||
  1156. (attr->test && attr->test(mod))) {
  1157. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1158. sysfs_attr_init(&temp_attr->attr);
  1159. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  1160. ++temp_attr;
  1161. }
  1162. }
  1163. return error;
  1164. }
  1165. static void module_remove_modinfo_attrs(struct module *mod)
  1166. {
  1167. struct module_attribute *attr;
  1168. int i;
  1169. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1170. /* pick a field to test for end of list */
  1171. if (!attr->attr.name)
  1172. break;
  1173. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  1174. if (attr->free)
  1175. attr->free(mod);
  1176. }
  1177. kfree(mod->modinfo_attrs);
  1178. }
  1179. static int mod_sysfs_init(struct module *mod)
  1180. {
  1181. int err;
  1182. struct kobject *kobj;
  1183. if (!module_sysfs_initialized) {
  1184. printk(KERN_ERR "%s: module sysfs not initialized\n",
  1185. mod->name);
  1186. err = -EINVAL;
  1187. goto out;
  1188. }
  1189. kobj = kset_find_obj(module_kset, mod->name);
  1190. if (kobj) {
  1191. printk(KERN_ERR "%s: module is already loaded\n", mod->name);
  1192. kobject_put(kobj);
  1193. err = -EINVAL;
  1194. goto out;
  1195. }
  1196. mod->mkobj.mod = mod;
  1197. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1198. mod->mkobj.kobj.kset = module_kset;
  1199. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1200. "%s", mod->name);
  1201. if (err)
  1202. kobject_put(&mod->mkobj.kobj);
  1203. /* delay uevent until full sysfs population */
  1204. out:
  1205. return err;
  1206. }
  1207. static int mod_sysfs_setup(struct module *mod,
  1208. struct kernel_param *kparam,
  1209. unsigned int num_params)
  1210. {
  1211. int err;
  1212. err = mod_sysfs_init(mod);
  1213. if (err)
  1214. goto out;
  1215. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1216. if (!mod->holders_dir) {
  1217. err = -ENOMEM;
  1218. goto out_unreg;
  1219. }
  1220. err = module_param_sysfs_setup(mod, kparam, num_params);
  1221. if (err)
  1222. goto out_unreg_holders;
  1223. err = module_add_modinfo_attrs(mod);
  1224. if (err)
  1225. goto out_unreg_param;
  1226. add_usage_links(mod);
  1227. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1228. return 0;
  1229. out_unreg_param:
  1230. module_param_sysfs_remove(mod);
  1231. out_unreg_holders:
  1232. kobject_put(mod->holders_dir);
  1233. out_unreg:
  1234. kobject_put(&mod->mkobj.kobj);
  1235. out:
  1236. return err;
  1237. }
  1238. static void mod_sysfs_fini(struct module *mod)
  1239. {
  1240. kobject_put(&mod->mkobj.kobj);
  1241. }
  1242. #else /* CONFIG_SYSFS */
  1243. static inline int mod_sysfs_init(struct module *mod)
  1244. {
  1245. return 0;
  1246. }
  1247. static inline int mod_sysfs_setup(struct module *mod,
  1248. struct kernel_param *kparam,
  1249. unsigned int num_params)
  1250. {
  1251. return 0;
  1252. }
  1253. static inline int module_add_modinfo_attrs(struct module *mod)
  1254. {
  1255. return 0;
  1256. }
  1257. static inline void module_remove_modinfo_attrs(struct module *mod)
  1258. {
  1259. }
  1260. static void mod_sysfs_fini(struct module *mod)
  1261. {
  1262. }
  1263. static void del_usage_links(struct module *mod)
  1264. {
  1265. }
  1266. #endif /* CONFIG_SYSFS */
  1267. static void mod_kobject_remove(struct module *mod)
  1268. {
  1269. del_usage_links(mod);
  1270. module_remove_modinfo_attrs(mod);
  1271. module_param_sysfs_remove(mod);
  1272. kobject_put(mod->mkobj.drivers_dir);
  1273. kobject_put(mod->holders_dir);
  1274. mod_sysfs_fini(mod);
  1275. }
  1276. /*
  1277. * unlink the module with the whole machine is stopped with interrupts off
  1278. * - this defends against kallsyms not taking locks
  1279. */
  1280. static int __unlink_module(void *_mod)
  1281. {
  1282. struct module *mod = _mod;
  1283. list_del(&mod->list);
  1284. return 0;
  1285. }
  1286. /* Free a module, remove from lists, etc (must hold module_mutex). */
  1287. static void free_module(struct module *mod)
  1288. {
  1289. trace_module_free(mod);
  1290. /* Delete from various lists */
  1291. stop_machine(__unlink_module, mod, NULL);
  1292. remove_notes_attrs(mod);
  1293. remove_sect_attrs(mod);
  1294. mod_kobject_remove(mod);
  1295. /* Arch-specific cleanup. */
  1296. module_arch_cleanup(mod);
  1297. /* Module unload stuff */
  1298. module_unload_free(mod);
  1299. /* Free any allocated parameters. */
  1300. destroy_params(mod->kp, mod->num_kp);
  1301. /* This may be NULL, but that's OK */
  1302. module_free(mod, mod->module_init);
  1303. kfree(mod->args);
  1304. percpu_modfree(mod);
  1305. #if defined(CONFIG_MODULE_UNLOAD)
  1306. if (mod->refptr)
  1307. free_percpu(mod->refptr);
  1308. #endif
  1309. /* Free lock-classes: */
  1310. lockdep_free_key_range(mod->module_core, mod->core_size);
  1311. /* Finally, free the core (containing the module structure) */
  1312. module_free(mod, mod->module_core);
  1313. #ifdef CONFIG_MPU
  1314. update_protections(current->mm);
  1315. #endif
  1316. }
  1317. void *__symbol_get(const char *symbol)
  1318. {
  1319. struct module *owner;
  1320. const struct kernel_symbol *sym;
  1321. preempt_disable();
  1322. sym = find_symbol(symbol, &owner, NULL, true, true);
  1323. if (sym && strong_try_module_get(owner))
  1324. sym = NULL;
  1325. preempt_enable();
  1326. return sym ? (void *)sym->value : NULL;
  1327. }
  1328. EXPORT_SYMBOL_GPL(__symbol_get);
  1329. /*
  1330. * Ensure that an exported symbol [global namespace] does not already exist
  1331. * in the kernel or in some other module's exported symbol table.
  1332. */
  1333. static int verify_export_symbols(struct module *mod)
  1334. {
  1335. unsigned int i;
  1336. struct module *owner;
  1337. const struct kernel_symbol *s;
  1338. struct {
  1339. const struct kernel_symbol *sym;
  1340. unsigned int num;
  1341. } arr[] = {
  1342. { mod->syms, mod->num_syms },
  1343. { mod->gpl_syms, mod->num_gpl_syms },
  1344. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1345. #ifdef CONFIG_UNUSED_SYMBOLS
  1346. { mod->unused_syms, mod->num_unused_syms },
  1347. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1348. #endif
  1349. };
  1350. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1351. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1352. if (find_symbol(s->name, &owner, NULL, true, false)) {
  1353. printk(KERN_ERR
  1354. "%s: exports duplicate symbol %s"
  1355. " (owned by %s)\n",
  1356. mod->name, s->name, module_name(owner));
  1357. return -ENOEXEC;
  1358. }
  1359. }
  1360. }
  1361. return 0;
  1362. }
  1363. /* Change all symbols so that st_value encodes the pointer directly. */
  1364. static int simplify_symbols(Elf_Shdr *sechdrs,
  1365. unsigned int symindex,
  1366. const char *strtab,
  1367. unsigned int versindex,
  1368. unsigned int pcpuindex,
  1369. struct module *mod)
  1370. {
  1371. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1372. unsigned long secbase;
  1373. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1374. int ret = 0;
  1375. const struct kernel_symbol *ksym;
  1376. for (i = 1; i < n; i++) {
  1377. switch (sym[i].st_shndx) {
  1378. case SHN_COMMON:
  1379. /* We compiled with -fno-common. These are not
  1380. supposed to happen. */
  1381. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1382. printk("%s: please compile with -fno-common\n",
  1383. mod->name);
  1384. ret = -ENOEXEC;
  1385. break;
  1386. case SHN_ABS:
  1387. /* Don't need to do anything */
  1388. DEBUGP("Absolute symbol: 0x%08lx\n",
  1389. (long)sym[i].st_value);
  1390. break;
  1391. case SHN_UNDEF:
  1392. ksym = resolve_symbol(sechdrs, versindex,
  1393. strtab + sym[i].st_name, mod);
  1394. /* Ok if resolved. */
  1395. if (ksym) {
  1396. sym[i].st_value = ksym->value;
  1397. break;
  1398. }
  1399. /* Ok if weak. */
  1400. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1401. break;
  1402. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1403. mod->name, strtab + sym[i].st_name);
  1404. ret = -ENOENT;
  1405. break;
  1406. default:
  1407. /* Divert to percpu allocation if a percpu var. */
  1408. if (sym[i].st_shndx == pcpuindex)
  1409. secbase = (unsigned long)mod_percpu(mod);
  1410. else
  1411. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1412. sym[i].st_value += secbase;
  1413. break;
  1414. }
  1415. }
  1416. return ret;
  1417. }
  1418. /* Additional bytes needed by arch in front of individual sections */
  1419. unsigned int __weak arch_mod_section_prepend(struct module *mod,
  1420. unsigned int section)
  1421. {
  1422. /* default implementation just returns zero */
  1423. return 0;
  1424. }
  1425. /* Update size with this section: return offset. */
  1426. static long get_offset(struct module *mod, unsigned int *size,
  1427. Elf_Shdr *sechdr, unsigned int section)
  1428. {
  1429. long ret;
  1430. *size += arch_mod_section_prepend(mod, section);
  1431. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1432. *size = ret + sechdr->sh_size;
  1433. return ret;
  1434. }
  1435. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1436. might -- code, read-only data, read-write data, small data. Tally
  1437. sizes, and place the offsets into sh_entsize fields: high bit means it
  1438. belongs in init. */
  1439. static void layout_sections(struct module *mod,
  1440. const Elf_Ehdr *hdr,
  1441. Elf_Shdr *sechdrs,
  1442. const char *secstrings)
  1443. {
  1444. static unsigned long const masks[][2] = {
  1445. /* NOTE: all executable code must be the first section
  1446. * in this array; otherwise modify the text_size
  1447. * finder in the two loops below */
  1448. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1449. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1450. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1451. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1452. };
  1453. unsigned int m, i;
  1454. for (i = 0; i < hdr->e_shnum; i++)
  1455. sechdrs[i].sh_entsize = ~0UL;
  1456. DEBUGP("Core section allocation order:\n");
  1457. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1458. for (i = 0; i < hdr->e_shnum; ++i) {
  1459. Elf_Shdr *s = &sechdrs[i];
  1460. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1461. || (s->sh_flags & masks[m][1])
  1462. || s->sh_entsize != ~0UL
  1463. || strstarts(secstrings + s->sh_name, ".init"))
  1464. continue;
  1465. s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
  1466. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1467. }
  1468. if (m == 0)
  1469. mod->core_text_size = mod->core_size;
  1470. }
  1471. DEBUGP("Init section allocation order:\n");
  1472. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1473. for (i = 0; i < hdr->e_shnum; ++i) {
  1474. Elf_Shdr *s = &sechdrs[i];
  1475. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1476. || (s->sh_flags & masks[m][1])
  1477. || s->sh_entsize != ~0UL
  1478. || !strstarts(secstrings + s->sh_name, ".init"))
  1479. continue;
  1480. s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
  1481. | INIT_OFFSET_MASK);
  1482. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1483. }
  1484. if (m == 0)
  1485. mod->init_text_size = mod->init_size;
  1486. }
  1487. }
  1488. static void set_license(struct module *mod, const char *license)
  1489. {
  1490. if (!license)
  1491. license = "unspecified";
  1492. if (!license_is_gpl_compatible(license)) {
  1493. if (!test_taint(TAINT_PROPRIETARY_MODULE))
  1494. printk(KERN_WARNING "%s: module license '%s' taints "
  1495. "kernel.\n", mod->name, license);
  1496. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1497. }
  1498. }
  1499. /* Parse tag=value strings from .modinfo section */
  1500. static char *next_string(char *string, unsigned long *secsize)
  1501. {
  1502. /* Skip non-zero chars */
  1503. while (string[0]) {
  1504. string++;
  1505. if ((*secsize)-- <= 1)
  1506. return NULL;
  1507. }
  1508. /* Skip any zero padding. */
  1509. while (!string[0]) {
  1510. string++;
  1511. if ((*secsize)-- <= 1)
  1512. return NULL;
  1513. }
  1514. return string;
  1515. }
  1516. static char *get_modinfo(Elf_Shdr *sechdrs,
  1517. unsigned int info,
  1518. const char *tag)
  1519. {
  1520. char *p;
  1521. unsigned int taglen = strlen(tag);
  1522. unsigned long size = sechdrs[info].sh_size;
  1523. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1524. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1525. return p + taglen + 1;
  1526. }
  1527. return NULL;
  1528. }
  1529. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1530. unsigned int infoindex)
  1531. {
  1532. struct module_attribute *attr;
  1533. int i;
  1534. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1535. if (attr->setup)
  1536. attr->setup(mod,
  1537. get_modinfo(sechdrs,
  1538. infoindex,
  1539. attr->attr.name));
  1540. }
  1541. }
  1542. static void free_modinfo(struct module *mod)
  1543. {
  1544. struct module_attribute *attr;
  1545. int i;
  1546. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1547. if (attr->free)
  1548. attr->free(mod);
  1549. }
  1550. }
  1551. #ifdef CONFIG_KALLSYMS
  1552. /* lookup symbol in given range of kernel_symbols */
  1553. static const struct kernel_symbol *lookup_symbol(const char *name,
  1554. const struct kernel_symbol *start,
  1555. const struct kernel_symbol *stop)
  1556. {
  1557. const struct kernel_symbol *ks = start;
  1558. for (; ks < stop; ks++)
  1559. if (strcmp(ks->name, name) == 0)
  1560. return ks;
  1561. return NULL;
  1562. }
  1563. static int is_exported(const char *name, unsigned long value,
  1564. const struct module *mod)
  1565. {
  1566. const struct kernel_symbol *ks;
  1567. if (!mod)
  1568. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  1569. else
  1570. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  1571. return ks != NULL && ks->value == value;
  1572. }
  1573. /* As per nm */
  1574. static char elf_type(const Elf_Sym *sym,
  1575. Elf_Shdr *sechdrs,
  1576. const char *secstrings,
  1577. struct module *mod)
  1578. {
  1579. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1580. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1581. return 'v';
  1582. else
  1583. return 'w';
  1584. }
  1585. if (sym->st_shndx == SHN_UNDEF)
  1586. return 'U';
  1587. if (sym->st_shndx == SHN_ABS)
  1588. return 'a';
  1589. if (sym->st_shndx >= SHN_LORESERVE)
  1590. return '?';
  1591. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1592. return 't';
  1593. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1594. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1595. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1596. return 'r';
  1597. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1598. return 'g';
  1599. else
  1600. return 'd';
  1601. }
  1602. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1603. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1604. return 's';
  1605. else
  1606. return 'b';
  1607. }
  1608. if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
  1609. return 'n';
  1610. return '?';
  1611. }
  1612. static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
  1613. unsigned int shnum)
  1614. {
  1615. const Elf_Shdr *sec;
  1616. if (src->st_shndx == SHN_UNDEF
  1617. || src->st_shndx >= shnum
  1618. || !src->st_name)
  1619. return false;
  1620. sec = sechdrs + src->st_shndx;
  1621. if (!(sec->sh_flags & SHF_ALLOC)
  1622. #ifndef CONFIG_KALLSYMS_ALL
  1623. || !(sec->sh_flags & SHF_EXECINSTR)
  1624. #endif
  1625. || (sec->sh_entsize & INIT_OFFSET_MASK))
  1626. return false;
  1627. return true;
  1628. }
  1629. static unsigned long layout_symtab(struct module *mod,
  1630. Elf_Shdr *sechdrs,
  1631. unsigned int symindex,
  1632. unsigned int strindex,
  1633. const Elf_Ehdr *hdr,
  1634. const char *secstrings,
  1635. unsigned long *pstroffs,
  1636. unsigned long *strmap)
  1637. {
  1638. unsigned long symoffs;
  1639. Elf_Shdr *symsect = sechdrs + symindex;
  1640. Elf_Shdr *strsect = sechdrs + strindex;
  1641. const Elf_Sym *src;
  1642. const char *strtab;
  1643. unsigned int i, nsrc, ndst;
  1644. /* Put symbol section at end of init part of module. */
  1645. symsect->sh_flags |= SHF_ALLOC;
  1646. symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
  1647. symindex) | INIT_OFFSET_MASK;
  1648. DEBUGP("\t%s\n", secstrings + symsect->sh_name);
  1649. src = (void *)hdr + symsect->sh_offset;
  1650. nsrc = symsect->sh_size / sizeof(*src);
  1651. strtab = (void *)hdr + strsect->sh_offset;
  1652. for (ndst = i = 1; i < nsrc; ++i, ++src)
  1653. if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
  1654. unsigned int j = src->st_name;
  1655. while(!__test_and_set_bit(j, strmap) && strtab[j])
  1656. ++j;
  1657. ++ndst;
  1658. }
  1659. /* Append room for core symbols at end of core part. */
  1660. symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
  1661. mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
  1662. /* Put string table section at end of init part of module. */
  1663. strsect->sh_flags |= SHF_ALLOC;
  1664. strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
  1665. strindex) | INIT_OFFSET_MASK;
  1666. DEBUGP("\t%s\n", secstrings + strsect->sh_name);
  1667. /* Append room for core symbols' strings at end of core part. */
  1668. *pstroffs = mod->core_size;
  1669. __set_bit(0, strmap);
  1670. mod->core_size += bitmap_weight(strmap, strsect->sh_size);
  1671. return symoffs;
  1672. }
  1673. static void add_kallsyms(struct module *mod,
  1674. Elf_Shdr *sechdrs,
  1675. unsigned int shnum,
  1676. unsigned int symindex,
  1677. unsigned int strindex,
  1678. unsigned long symoffs,
  1679. unsigned long stroffs,
  1680. const char *secstrings,
  1681. unsigned long *strmap)
  1682. {
  1683. unsigned int i, ndst;
  1684. const Elf_Sym *src;
  1685. Elf_Sym *dst;
  1686. char *s;
  1687. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1688. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1689. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1690. /* Set types up while we still have access to sections. */
  1691. for (i = 0; i < mod->num_symtab; i++)
  1692. mod->symtab[i].st_info
  1693. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1694. mod->core_symtab = dst = mod->module_core + symoffs;
  1695. src = mod->symtab;
  1696. *dst = *src;
  1697. for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
  1698. if (!is_core_symbol(src, sechdrs, shnum))
  1699. continue;
  1700. dst[ndst] = *src;
  1701. dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
  1702. ++ndst;
  1703. }
  1704. mod->core_num_syms = ndst;
  1705. mod->core_strtab = s = mod->module_core + stroffs;
  1706. for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
  1707. if (test_bit(i, strmap))
  1708. *++s = mod->strtab[i];
  1709. }
  1710. #else
  1711. static inline unsigned long layout_symtab(struct module *mod,
  1712. Elf_Shdr *sechdrs,
  1713. unsigned int symindex,
  1714. unsigned int strindex,
  1715. const Elf_Ehdr *hdr,
  1716. const char *secstrings,
  1717. unsigned long *pstroffs,
  1718. unsigned long *strmap)
  1719. {
  1720. return 0;
  1721. }
  1722. static inline void add_kallsyms(struct module *mod,
  1723. Elf_Shdr *sechdrs,
  1724. unsigned int shnum,
  1725. unsigned int symindex,
  1726. unsigned int strindex,
  1727. unsigned long symoffs,
  1728. unsigned long stroffs,
  1729. const char *secstrings,
  1730. const unsigned long *strmap)
  1731. {
  1732. }
  1733. #endif /* CONFIG_KALLSYMS */
  1734. static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
  1735. {
  1736. #ifdef CONFIG_DYNAMIC_DEBUG
  1737. if (ddebug_add_module(debug, num, debug->modname))
  1738. printk(KERN_ERR "dynamic debug error adding module: %s\n",
  1739. debug->modname);
  1740. #endif
  1741. }
  1742. static void *module_alloc_update_bounds(unsigned long size)
  1743. {
  1744. void *ret = module_alloc(size);
  1745. if (ret) {
  1746. /* Update module bounds. */
  1747. if ((unsigned long)ret < module_addr_min)
  1748. module_addr_min = (unsigned long)ret;
  1749. if ((unsigned long)ret + size > module_addr_max)
  1750. module_addr_max = (unsigned long)ret + size;
  1751. }
  1752. return ret;
  1753. }
  1754. #ifdef CONFIG_DEBUG_KMEMLEAK
  1755. static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
  1756. Elf_Shdr *sechdrs, char *secstrings)
  1757. {
  1758. unsigned int i;
  1759. /* only scan the sections containing data */
  1760. kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
  1761. for (i = 1; i < hdr->e_shnum; i++) {
  1762. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1763. continue;
  1764. if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
  1765. && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
  1766. continue;
  1767. kmemleak_scan_area((void *)sechdrs[i].sh_addr,
  1768. sechdrs[i].sh_size, GFP_KERNEL);
  1769. }
  1770. }
  1771. #else
  1772. static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
  1773. Elf_Shdr *sechdrs, char *secstrings)
  1774. {
  1775. }
  1776. #endif
  1777. /* Allocate and load the module: note that size of section 0 is always
  1778. zero, and we rely on this for optional sections. */
  1779. static noinline struct module *load_module(void __user *umod,
  1780. unsigned long len,
  1781. const char __user *uargs)
  1782. {
  1783. Elf_Ehdr *hdr;
  1784. Elf_Shdr *sechdrs;
  1785. char *secstrings, *args, *modmagic, *strtab = NULL;
  1786. char *staging;
  1787. unsigned int i;
  1788. unsigned int symindex = 0;
  1789. unsigned int strindex = 0;
  1790. unsigned int modindex, versindex, infoindex, pcpuindex;
  1791. struct module *mod;
  1792. long err = 0;
  1793. void *ptr = NULL; /* Stops spurious gcc warning */
  1794. unsigned long symoffs, stroffs, *strmap;
  1795. void __percpu *percpu;
  1796. mm_segment_t old_fs;
  1797. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1798. umod, len, uargs);
  1799. if (len < sizeof(*hdr))
  1800. return ERR_PTR(-ENOEXEC);
  1801. /* Suck in entire file: we'll want most of it. */
  1802. /* vmalloc barfs on "unusual" numbers. Check here */
  1803. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1804. return ERR_PTR(-ENOMEM);
  1805. if (copy_from_user(hdr, umod, len) != 0) {
  1806. err = -EFAULT;
  1807. goto free_hdr;
  1808. }
  1809. /* Sanity checks against insmoding binaries or wrong arch,
  1810. weird elf version */
  1811. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
  1812. || hdr->e_type != ET_REL
  1813. || !elf_check_arch(hdr)
  1814. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1815. err = -ENOEXEC;
  1816. goto free_hdr;
  1817. }
  1818. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1819. goto truncated;
  1820. /* Convenience variables */
  1821. sechdrs = (void *)hdr + hdr->e_shoff;
  1822. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1823. sechdrs[0].sh_addr = 0;
  1824. for (i = 1; i < hdr->e_shnum; i++) {
  1825. if (sechdrs[i].sh_type != SHT_NOBITS
  1826. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1827. goto truncated;
  1828. /* Mark all sections sh_addr with their address in the
  1829. temporary image. */
  1830. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1831. /* Internal symbols and strings. */
  1832. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1833. symindex = i;
  1834. strindex = sechdrs[i].sh_link;
  1835. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1836. }
  1837. #ifndef CONFIG_MODULE_UNLOAD
  1838. /* Don't load .exit sections */
  1839. if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
  1840. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1841. #endif
  1842. }
  1843. modindex = find_sec(hdr, sechdrs, secstrings,
  1844. ".gnu.linkonce.this_module");
  1845. if (!modindex) {
  1846. printk(KERN_WARNING "No module found in object\n");
  1847. err = -ENOEXEC;
  1848. goto free_hdr;
  1849. }
  1850. /* This is temporary: point mod into copy of data. */
  1851. mod = (void *)sechdrs[modindex].sh_addr;
  1852. if (symindex == 0) {
  1853. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1854. mod->name);
  1855. err = -ENOEXEC;
  1856. goto free_hdr;
  1857. }
  1858. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1859. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1860. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1861. /* Don't keep modinfo and version sections. */
  1862. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1863. sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1864. /* Check module struct version now, before we try to use module. */
  1865. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1866. err = -ENOEXEC;
  1867. goto free_hdr;
  1868. }
  1869. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1870. /* This is allowed: modprobe --force will invalidate it. */
  1871. if (!modmagic) {
  1872. err = try_to_force_load(mod, "bad vermagic");
  1873. if (err)
  1874. goto free_hdr;
  1875. } else if (!same_magic(modmagic, vermagic, versindex)) {
  1876. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1877. mod->name, modmagic, vermagic);
  1878. err = -ENOEXEC;
  1879. goto free_hdr;
  1880. }
  1881. staging = get_modinfo(sechdrs, infoindex, "staging");
  1882. if (staging) {
  1883. add_taint_module(mod, TAINT_CRAP);
  1884. printk(KERN_WARNING "%s: module is from the staging directory,"
  1885. " the quality is unknown, you have been warned.\n",
  1886. mod->name);
  1887. }
  1888. /* Now copy in args */
  1889. args = strndup_user(uargs, ~0UL >> 1);
  1890. if (IS_ERR(args)) {
  1891. err = PTR_ERR(args);
  1892. goto free_hdr;
  1893. }
  1894. strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
  1895. * sizeof(long), GFP_KERNEL);
  1896. if (!strmap) {
  1897. err = -ENOMEM;
  1898. goto free_mod;
  1899. }
  1900. if (find_module(mod->name)) {
  1901. err = -EEXIST;
  1902. goto free_mod;
  1903. }
  1904. mod->state = MODULE_STATE_COMING;
  1905. /* Allow arches to frob section contents and sizes. */
  1906. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1907. if (err < 0)
  1908. goto free_mod;
  1909. if (pcpuindex) {
  1910. /* We have a special allocation for this section. */
  1911. err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
  1912. sechdrs[pcpuindex].sh_addralign);
  1913. if (err)
  1914. goto free_mod;
  1915. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1916. }
  1917. /* Keep this around for failure path. */
  1918. percpu = mod_percpu(mod);
  1919. /* Determine total sizes, and put offsets in sh_entsize. For now
  1920. this is done generically; there doesn't appear to be any
  1921. special cases for the architectures. */
  1922. layout_sections(mod, hdr, sechdrs, secstrings);
  1923. symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
  1924. secstrings, &stroffs, strmap);
  1925. /* Do the allocs. */
  1926. ptr = module_alloc_update_bounds(mod->core_size);
  1927. /*
  1928. * The pointer to this block is stored in the module structure
  1929. * which is inside the block. Just mark it as not being a
  1930. * leak.
  1931. */
  1932. kmemleak_not_leak(ptr);
  1933. if (!ptr) {
  1934. err = -ENOMEM;
  1935. goto free_percpu;
  1936. }
  1937. memset(ptr, 0, mod->core_size);
  1938. mod->module_core = ptr;
  1939. ptr = module_alloc_update_bounds(mod->init_size);
  1940. /*
  1941. * The pointer to this block is stored in the module structure
  1942. * which is inside the block. This block doesn't need to be
  1943. * scanned as it contains data and code that will be freed
  1944. * after the module is initialized.
  1945. */
  1946. kmemleak_ignore(ptr);
  1947. if (!ptr && mod->init_size) {
  1948. err = -ENOMEM;
  1949. goto free_core;
  1950. }
  1951. memset(ptr, 0, mod->init_size);
  1952. mod->module_init = ptr;
  1953. /* Transfer each section which specifies SHF_ALLOC */
  1954. DEBUGP("final section addresses:\n");
  1955. for (i = 0; i < hdr->e_shnum; i++) {
  1956. void *dest;
  1957. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1958. continue;
  1959. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1960. dest = mod->module_init
  1961. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1962. else
  1963. dest = mod->module_core + sechdrs[i].sh_entsize;
  1964. if (sechdrs[i].sh_type != SHT_NOBITS)
  1965. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1966. sechdrs[i].sh_size);
  1967. /* Update sh_addr to point to copy in image. */
  1968. sechdrs[i].sh_addr = (unsigned long)dest;
  1969. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1970. }
  1971. /* Module has been moved. */
  1972. mod = (void *)sechdrs[modindex].sh_addr;
  1973. kmemleak_load_module(mod, hdr, sechdrs, secstrings);
  1974. #if defined(CONFIG_MODULE_UNLOAD)
  1975. mod->refptr = alloc_percpu(struct module_ref);
  1976. if (!mod->refptr) {
  1977. err = -ENOMEM;
  1978. goto free_init;
  1979. }
  1980. #endif
  1981. /* Now we've moved module, initialize linked lists, etc. */
  1982. module_unload_init(mod);
  1983. /* Set up license info based on the info section */
  1984. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1985. /*
  1986. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  1987. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  1988. * using GPL-only symbols it needs.
  1989. */
  1990. if (strcmp(mod->name, "ndiswrapper") == 0)
  1991. add_taint(TAINT_PROPRIETARY_MODULE);
  1992. /* driverloader was caught wrongly pretending to be under GPL */
  1993. if (strcmp(mod->name, "driverloader") == 0)
  1994. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1995. /* Set up MODINFO_ATTR fields */
  1996. setup_modinfo(mod, sechdrs, infoindex);
  1997. /* Fix up syms, so that st_value is a pointer to location. */
  1998. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1999. mod);
  2000. if (err < 0)
  2001. goto cleanup;
  2002. /* Now we've got everything in the final locations, we can
  2003. * find optional sections. */
  2004. mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
  2005. sizeof(*mod->kp), &mod->num_kp);
  2006. mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
  2007. sizeof(*mod->syms), &mod->num_syms);
  2008. mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
  2009. mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
  2010. sizeof(*mod->gpl_syms),
  2011. &mod->num_gpl_syms);
  2012. mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  2013. mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
  2014. "__ksymtab_gpl_future",
  2015. sizeof(*mod->gpl_future_syms),
  2016. &mod->num_gpl_future_syms);
  2017. mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
  2018. "__kcrctab_gpl_future");
  2019. #ifdef CONFIG_UNUSED_SYMBOLS
  2020. mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
  2021. "__ksymtab_unused",
  2022. sizeof(*mod->unused_syms),
  2023. &mod->num_unused_syms);
  2024. mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
  2025. "__kcrctab_unused");
  2026. mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
  2027. "__ksymtab_unused_gpl",
  2028. sizeof(*mod->unused_gpl_syms),
  2029. &mod->num_unused_gpl_syms);
  2030. mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
  2031. "__kcrctab_unused_gpl");
  2032. #endif
  2033. #ifdef CONFIG_CONSTRUCTORS
  2034. mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
  2035. sizeof(*mod->ctors), &mod->num_ctors);
  2036. #endif
  2037. #ifdef CONFIG_TRACEPOINTS
  2038. mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
  2039. "__tracepoints",
  2040. sizeof(*mod->tracepoints),
  2041. &mod->num_tracepoints);
  2042. #endif
  2043. #ifdef CONFIG_EVENT_TRACING
  2044. mod->trace_events = section_objs(hdr, sechdrs, secstrings,
  2045. "_ftrace_events",
  2046. sizeof(*mod->trace_events),
  2047. &mod->num_trace_events);
  2048. /*
  2049. * This section contains pointers to allocated objects in the trace
  2050. * code and not scanning it leads to false positives.
  2051. */
  2052. kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
  2053. mod->num_trace_events, GFP_KERNEL);
  2054. #endif
  2055. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  2056. /* sechdrs[0].sh_size is always zero */
  2057. mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
  2058. "__mcount_loc",
  2059. sizeof(*mod->ftrace_callsites),
  2060. &mod->num_ftrace_callsites);
  2061. #endif
  2062. #ifdef CONFIG_MODVERSIONS
  2063. if ((mod->num_syms && !mod->crcs)
  2064. || (mod->num_gpl_syms && !mod->gpl_crcs)
  2065. || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
  2066. #ifdef CONFIG_UNUSED_SYMBOLS
  2067. || (mod->num_unused_syms && !mod->unused_crcs)
  2068. || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
  2069. #endif
  2070. ) {
  2071. err = try_to_force_load(mod,
  2072. "no versions for exported symbols");
  2073. if (err)
  2074. goto cleanup;
  2075. }
  2076. #endif
  2077. /* Now do relocations. */
  2078. for (i = 1; i < hdr->e_shnum; i++) {
  2079. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  2080. unsigned int info = sechdrs[i].sh_info;
  2081. /* Not a valid relocation section? */
  2082. if (info >= hdr->e_shnum)
  2083. continue;
  2084. /* Don't bother with non-allocated sections */
  2085. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  2086. continue;
  2087. if (sechdrs[i].sh_type == SHT_REL)
  2088. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  2089. else if (sechdrs[i].sh_type == SHT_RELA)
  2090. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  2091. mod);
  2092. if (err < 0)
  2093. goto cleanup;
  2094. }
  2095. /* Find duplicate symbols */
  2096. err = verify_export_symbols(mod);
  2097. if (err < 0)
  2098. goto cleanup;
  2099. /* Set up and sort exception table */
  2100. mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
  2101. sizeof(*mod->extable), &mod->num_exentries);
  2102. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  2103. /* Finally, copy percpu area over. */
  2104. percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
  2105. sechdrs[pcpuindex].sh_size);
  2106. add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
  2107. symoffs, stroffs, secstrings, strmap);
  2108. kfree(strmap);
  2109. strmap = NULL;
  2110. if (!mod->taints) {
  2111. struct _ddebug *debug;
  2112. unsigned int num_debug;
  2113. debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
  2114. sizeof(*debug), &num_debug);
  2115. if (debug)
  2116. dynamic_debug_setup(debug, num_debug);
  2117. }
  2118. err = module_finalize(hdr, sechdrs, mod);
  2119. if (err < 0)
  2120. goto cleanup;
  2121. /* flush the icache in correct context */
  2122. old_fs = get_fs();
  2123. set_fs(KERNEL_DS);
  2124. /*
  2125. * Flush the instruction cache, since we've played with text.
  2126. * Do it before processing of module parameters, so the module
  2127. * can provide parameter accessor functions of its own.
  2128. */
  2129. if (mod->module_init)
  2130. flush_icache_range((unsigned long)mod->module_init,
  2131. (unsigned long)mod->module_init
  2132. + mod->init_size);
  2133. flush_icache_range((unsigned long)mod->module_core,
  2134. (unsigned long)mod->module_core + mod->core_size);
  2135. set_fs(old_fs);
  2136. mod->args = args;
  2137. if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
  2138. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  2139. mod->name);
  2140. /* Now sew it into the lists so we can get lockdep and oops
  2141. * info during argument parsing. Noone should access us, since
  2142. * strong_try_module_get() will fail.
  2143. * lockdep/oops can run asynchronous, so use the RCU list insertion
  2144. * function to insert in a way safe to concurrent readers.
  2145. * The mutex protects against concurrent writers.
  2146. */
  2147. list_add_rcu(&mod->list, &modules);
  2148. err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
  2149. if (err < 0)
  2150. goto unlink;
  2151. err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
  2152. if (err < 0)
  2153. goto unlink;
  2154. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  2155. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  2156. /* Get rid of temporary copy */
  2157. vfree(hdr);
  2158. trace_module_load(mod);
  2159. /* Done! */
  2160. return mod;
  2161. unlink:
  2162. /* Unlink carefully: kallsyms could be walking list. */
  2163. list_del_rcu(&mod->list);
  2164. synchronize_sched();
  2165. module_arch_cleanup(mod);
  2166. cleanup:
  2167. free_modinfo(mod);
  2168. module_unload_free(mod);
  2169. #if defined(CONFIG_MODULE_UNLOAD)
  2170. free_percpu(mod->refptr);
  2171. free_init:
  2172. #endif
  2173. module_free(mod, mod->module_init);
  2174. free_core:
  2175. module_free(mod, mod->module_core);
  2176. /* mod will be freed with core. Don't access it beyond this line! */
  2177. free_percpu:
  2178. free_percpu(percpu);
  2179. free_mod:
  2180. kfree(args);
  2181. kfree(strmap);
  2182. free_hdr:
  2183. vfree(hdr);
  2184. return ERR_PTR(err);
  2185. truncated:
  2186. printk(KERN_ERR "Module len %lu truncated\n", len);
  2187. err = -ENOEXEC;
  2188. goto free_hdr;
  2189. }
  2190. /* Call module constructors. */
  2191. static void do_mod_ctors(struct module *mod)
  2192. {
  2193. #ifdef CONFIG_CONSTRUCTORS
  2194. unsigned long i;
  2195. for (i = 0; i < mod->num_ctors; i++)
  2196. mod->ctors[i]();
  2197. #endif
  2198. }
  2199. /* This is where the real work happens */
  2200. SYSCALL_DEFINE3(init_module, void __user *, umod,
  2201. unsigned long, len, const char __user *, uargs)
  2202. {
  2203. struct module *mod;
  2204. int ret = 0;
  2205. /* Must have permission */
  2206. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  2207. return -EPERM;
  2208. /* Only one module load at a time, please */
  2209. if (mutex_lock_interruptible(&module_mutex) != 0)
  2210. return -EINTR;
  2211. /* Do all the hard work */
  2212. mod = load_module(umod, len, uargs);
  2213. if (IS_ERR(mod)) {
  2214. mutex_unlock(&module_mutex);
  2215. return PTR_ERR(mod);
  2216. }
  2217. /* Drop lock so they can recurse */
  2218. mutex_unlock(&module_mutex);
  2219. blocking_notifier_call_chain(&module_notify_list,
  2220. MODULE_STATE_COMING, mod);
  2221. do_mod_ctors(mod);
  2222. /* Start the module */
  2223. if (mod->init != NULL)
  2224. ret = do_one_initcall(mod->init);
  2225. if (ret < 0) {
  2226. /* Init routine failed: abort. Try to protect us from
  2227. buggy refcounters. */
  2228. mod->state = MODULE_STATE_GOING;
  2229. synchronize_sched();
  2230. module_put(mod);
  2231. blocking_notifier_call_chain(&module_notify_list,
  2232. MODULE_STATE_GOING, mod);
  2233. mutex_lock(&module_mutex);
  2234. free_module(mod);
  2235. mutex_unlock(&module_mutex);
  2236. wake_up(&module_wq);
  2237. return ret;
  2238. }
  2239. if (ret > 0) {
  2240. printk(KERN_WARNING
  2241. "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
  2242. "%s: loading module anyway...\n",
  2243. __func__, mod->name, ret,
  2244. __func__);
  2245. dump_stack();
  2246. }
  2247. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  2248. mod->state = MODULE_STATE_LIVE;
  2249. wake_up(&module_wq);
  2250. blocking_notifier_call_chain(&module_notify_list,
  2251. MODULE_STATE_LIVE, mod);
  2252. /* We need to finish all async code before the module init sequence is done */
  2253. async_synchronize_full();
  2254. mutex_lock(&module_mutex);
  2255. /* Drop initial reference. */
  2256. module_put(mod);
  2257. trim_init_extable(mod);
  2258. #ifdef CONFIG_KALLSYMS
  2259. mod->num_symtab = mod->core_num_syms;
  2260. mod->symtab = mod->core_symtab;
  2261. mod->strtab = mod->core_strtab;
  2262. #endif
  2263. module_free(mod, mod->module_init);
  2264. mod->module_init = NULL;
  2265. mod->init_size = 0;
  2266. mod->init_text_size = 0;
  2267. mutex_unlock(&module_mutex);
  2268. return 0;
  2269. }
  2270. static inline int within(unsigned long addr, void *start, unsigned long size)
  2271. {
  2272. return ((void *)addr >= start && (void *)addr < start + size);
  2273. }
  2274. #ifdef CONFIG_KALLSYMS
  2275. /*
  2276. * This ignores the intensely annoying "mapping symbols" found
  2277. * in ARM ELF files: $a, $t and $d.
  2278. */
  2279. static inline int is_arm_mapping_symbol(const char *str)
  2280. {
  2281. return str[0] == '$' && strchr("atd", str[1])
  2282. && (str[2] == '\0' || str[2] == '.');
  2283. }
  2284. static const char *get_ksymbol(struct module *mod,
  2285. unsigned long addr,
  2286. unsigned long *size,
  2287. unsigned long *offset)
  2288. {
  2289. unsigned int i, best = 0;
  2290. unsigned long nextval;
  2291. /* At worse, next value is at end of module */
  2292. if (within_module_init(addr, mod))
  2293. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  2294. else
  2295. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  2296. /* Scan for closest preceeding symbol, and next symbol. (ELF
  2297. starts real symbols at 1). */
  2298. for (i = 1; i < mod->num_symtab; i++) {
  2299. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  2300. continue;
  2301. /* We ignore unnamed symbols: they're uninformative
  2302. * and inserted at a whim. */
  2303. if (mod->symtab[i].st_value <= addr
  2304. && mod->symtab[i].st_value > mod->symtab[best].st_value
  2305. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2306. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2307. best = i;
  2308. if (mod->symtab[i].st_value > addr
  2309. && mod->symtab[i].st_value < nextval
  2310. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2311. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2312. nextval = mod->symtab[i].st_value;
  2313. }
  2314. if (!best)
  2315. return NULL;
  2316. if (size)
  2317. *size = nextval - mod->symtab[best].st_value;
  2318. if (offset)
  2319. *offset = addr - mod->symtab[best].st_value;
  2320. return mod->strtab + mod->symtab[best].st_name;
  2321. }
  2322. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  2323. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  2324. const char *module_address_lookup(unsigned long addr,
  2325. unsigned long *size,
  2326. unsigned long *offset,
  2327. char **modname,
  2328. char *namebuf)
  2329. {
  2330. struct module *mod;
  2331. const char *ret = NULL;
  2332. preempt_disable();
  2333. list_for_each_entry_rcu(mod, &modules, list) {
  2334. if (within_module_init(addr, mod) ||
  2335. within_module_core(addr, mod)) {
  2336. if (modname)
  2337. *modname = mod->name;
  2338. ret = get_ksymbol(mod, addr, size, offset);
  2339. break;
  2340. }
  2341. }
  2342. /* Make a copy in here where it's safe */
  2343. if (ret) {
  2344. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2345. ret = namebuf;
  2346. }
  2347. preempt_enable();
  2348. return ret;
  2349. }
  2350. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2351. {
  2352. struct module *mod;
  2353. preempt_disable();
  2354. list_for_each_entry_rcu(mod, &modules, list) {
  2355. if (within_module_init(addr, mod) ||
  2356. within_module_core(addr, mod)) {
  2357. const char *sym;
  2358. sym = get_ksymbol(mod, addr, NULL, NULL);
  2359. if (!sym)
  2360. goto out;
  2361. strlcpy(symname, sym, KSYM_NAME_LEN);
  2362. preempt_enable();
  2363. return 0;
  2364. }
  2365. }
  2366. out:
  2367. preempt_enable();
  2368. return -ERANGE;
  2369. }
  2370. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2371. unsigned long *offset, char *modname, char *name)
  2372. {
  2373. struct module *mod;
  2374. preempt_disable();
  2375. list_for_each_entry_rcu(mod, &modules, list) {
  2376. if (within_module_init(addr, mod) ||
  2377. within_module_core(addr, mod)) {
  2378. const char *sym;
  2379. sym = get_ksymbol(mod, addr, size, offset);
  2380. if (!sym)
  2381. goto out;
  2382. if (modname)
  2383. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2384. if (name)
  2385. strlcpy(name, sym, KSYM_NAME_LEN);
  2386. preempt_enable();
  2387. return 0;
  2388. }
  2389. }
  2390. out:
  2391. preempt_enable();
  2392. return -ERANGE;
  2393. }
  2394. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2395. char *name, char *module_name, int *exported)
  2396. {
  2397. struct module *mod;
  2398. preempt_disable();
  2399. list_for_each_entry_rcu(mod, &modules, list) {
  2400. if (symnum < mod->num_symtab) {
  2401. *value = mod->symtab[symnum].st_value;
  2402. *type = mod->symtab[symnum].st_info;
  2403. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2404. KSYM_NAME_LEN);
  2405. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2406. *exported = is_exported(name, *value, mod);
  2407. preempt_enable();
  2408. return 0;
  2409. }
  2410. symnum -= mod->num_symtab;
  2411. }
  2412. preempt_enable();
  2413. return -ERANGE;
  2414. }
  2415. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2416. {
  2417. unsigned int i;
  2418. for (i = 0; i < mod->num_symtab; i++)
  2419. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2420. mod->symtab[i].st_info != 'U')
  2421. return mod->symtab[i].st_value;
  2422. return 0;
  2423. }
  2424. /* Look for this name: can be of form module:name. */
  2425. unsigned long module_kallsyms_lookup_name(const char *name)
  2426. {
  2427. struct module *mod;
  2428. char *colon;
  2429. unsigned long ret = 0;
  2430. /* Don't lock: we're in enough trouble already. */
  2431. preempt_disable();
  2432. if ((colon = strchr(name, ':')) != NULL) {
  2433. *colon = '\0';
  2434. if ((mod = find_module(name)) != NULL)
  2435. ret = mod_find_symname(mod, colon+1);
  2436. *colon = ':';
  2437. } else {
  2438. list_for_each_entry_rcu(mod, &modules, list)
  2439. if ((ret = mod_find_symname(mod, name)) != 0)
  2440. break;
  2441. }
  2442. preempt_enable();
  2443. return ret;
  2444. }
  2445. int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  2446. struct module *, unsigned long),
  2447. void *data)
  2448. {
  2449. struct module *mod;
  2450. unsigned int i;
  2451. int ret;
  2452. list_for_each_entry(mod, &modules, list) {
  2453. for (i = 0; i < mod->num_symtab; i++) {
  2454. ret = fn(data, mod->strtab + mod->symtab[i].st_name,
  2455. mod, mod->symtab[i].st_value);
  2456. if (ret != 0)
  2457. return ret;
  2458. }
  2459. }
  2460. return 0;
  2461. }
  2462. #endif /* CONFIG_KALLSYMS */
  2463. static char *module_flags(struct module *mod, char *buf)
  2464. {
  2465. int bx = 0;
  2466. if (mod->taints ||
  2467. mod->state == MODULE_STATE_GOING ||
  2468. mod->state == MODULE_STATE_COMING) {
  2469. buf[bx++] = '(';
  2470. if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
  2471. buf[bx++] = 'P';
  2472. if (mod->taints & (1 << TAINT_FORCED_MODULE))
  2473. buf[bx++] = 'F';
  2474. if (mod->taints & (1 << TAINT_CRAP))
  2475. buf[bx++] = 'C';
  2476. /*
  2477. * TAINT_FORCED_RMMOD: could be added.
  2478. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2479. * apply to modules.
  2480. */
  2481. /* Show a - for module-is-being-unloaded */
  2482. if (mod->state == MODULE_STATE_GOING)
  2483. buf[bx++] = '-';
  2484. /* Show a + for module-is-being-loaded */
  2485. if (mod->state == MODULE_STATE_COMING)
  2486. buf[bx++] = '+';
  2487. buf[bx++] = ')';
  2488. }
  2489. buf[bx] = '\0';
  2490. return buf;
  2491. }
  2492. #ifdef CONFIG_PROC_FS
  2493. /* Called by the /proc file system to return a list of modules. */
  2494. static void *m_start(struct seq_file *m, loff_t *pos)
  2495. {
  2496. mutex_lock(&module_mutex);
  2497. return seq_list_start(&modules, *pos);
  2498. }
  2499. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2500. {
  2501. return seq_list_next(p, &modules, pos);
  2502. }
  2503. static void m_stop(struct seq_file *m, void *p)
  2504. {
  2505. mutex_unlock(&module_mutex);
  2506. }
  2507. static int m_show(struct seq_file *m, void *p)
  2508. {
  2509. struct module *mod = list_entry(p, struct module, list);
  2510. char buf[8];
  2511. seq_printf(m, "%s %u",
  2512. mod->name, mod->init_size + mod->core_size);
  2513. print_unload_info(m, mod);
  2514. /* Informative for users. */
  2515. seq_printf(m, " %s",
  2516. mod->state == MODULE_STATE_GOING ? "Unloading":
  2517. mod->state == MODULE_STATE_COMING ? "Loading":
  2518. "Live");
  2519. /* Used by oprofile and other similar tools. */
  2520. seq_printf(m, " 0x%p", mod->module_core);
  2521. /* Taints info */
  2522. if (mod->taints)
  2523. seq_printf(m, " %s", module_flags(mod, buf));
  2524. seq_printf(m, "\n");
  2525. return 0;
  2526. }
  2527. /* Format: modulename size refcount deps address
  2528. Where refcount is a number or -, and deps is a comma-separated list
  2529. of depends or -.
  2530. */
  2531. static const struct seq_operations modules_op = {
  2532. .start = m_start,
  2533. .next = m_next,
  2534. .stop = m_stop,
  2535. .show = m_show
  2536. };
  2537. static int modules_open(struct inode *inode, struct file *file)
  2538. {
  2539. return seq_open(file, &modules_op);
  2540. }
  2541. static const struct file_operations proc_modules_operations = {
  2542. .open = modules_open,
  2543. .read = seq_read,
  2544. .llseek = seq_lseek,
  2545. .release = seq_release,
  2546. };
  2547. static int __init proc_modules_init(void)
  2548. {
  2549. proc_create("modules", 0, NULL, &proc_modules_operations);
  2550. return 0;
  2551. }
  2552. module_init(proc_modules_init);
  2553. #endif
  2554. /* Given an address, look for it in the module exception tables. */
  2555. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2556. {
  2557. const struct exception_table_entry *e = NULL;
  2558. struct module *mod;
  2559. preempt_disable();
  2560. list_for_each_entry_rcu(mod, &modules, list) {
  2561. if (mod->num_exentries == 0)
  2562. continue;
  2563. e = search_extable(mod->extable,
  2564. mod->extable + mod->num_exentries - 1,
  2565. addr);
  2566. if (e)
  2567. break;
  2568. }
  2569. preempt_enable();
  2570. /* Now, if we found one, we are running inside it now, hence
  2571. we cannot unload the module, hence no refcnt needed. */
  2572. return e;
  2573. }
  2574. /*
  2575. * is_module_address - is this address inside a module?
  2576. * @addr: the address to check.
  2577. *
  2578. * See is_module_text_address() if you simply want to see if the address
  2579. * is code (not data).
  2580. */
  2581. bool is_module_address(unsigned long addr)
  2582. {
  2583. bool ret;
  2584. preempt_disable();
  2585. ret = __module_address(addr) != NULL;
  2586. preempt_enable();
  2587. return ret;
  2588. }
  2589. /*
  2590. * __module_address - get the module which contains an address.
  2591. * @addr: the address.
  2592. *
  2593. * Must be called with preempt disabled or module mutex held so that
  2594. * module doesn't get freed during this.
  2595. */
  2596. struct module *__module_address(unsigned long addr)
  2597. {
  2598. struct module *mod;
  2599. if (addr < module_addr_min || addr > module_addr_max)
  2600. return NULL;
  2601. list_for_each_entry_rcu(mod, &modules, list)
  2602. if (within_module_core(addr, mod)
  2603. || within_module_init(addr, mod))
  2604. return mod;
  2605. return NULL;
  2606. }
  2607. EXPORT_SYMBOL_GPL(__module_address);
  2608. /*
  2609. * is_module_text_address - is this address inside module code?
  2610. * @addr: the address to check.
  2611. *
  2612. * See is_module_address() if you simply want to see if the address is
  2613. * anywhere in a module. See kernel_text_address() for testing if an
  2614. * address corresponds to kernel or module code.
  2615. */
  2616. bool is_module_text_address(unsigned long addr)
  2617. {
  2618. bool ret;
  2619. preempt_disable();
  2620. ret = __module_text_address(addr) != NULL;
  2621. preempt_enable();
  2622. return ret;
  2623. }
  2624. /*
  2625. * __module_text_address - get the module whose code contains an address.
  2626. * @addr: the address.
  2627. *
  2628. * Must be called with preempt disabled or module mutex held so that
  2629. * module doesn't get freed during this.
  2630. */
  2631. struct module *__module_text_address(unsigned long addr)
  2632. {
  2633. struct module *mod = __module_address(addr);
  2634. if (mod) {
  2635. /* Make sure it's within the text section. */
  2636. if (!within(addr, mod->module_init, mod->init_text_size)
  2637. && !within(addr, mod->module_core, mod->core_text_size))
  2638. mod = NULL;
  2639. }
  2640. return mod;
  2641. }
  2642. EXPORT_SYMBOL_GPL(__module_text_address);
  2643. /* Don't grab lock, we're oopsing. */
  2644. void print_modules(void)
  2645. {
  2646. struct module *mod;
  2647. char buf[8];
  2648. printk(KERN_DEFAULT "Modules linked in:");
  2649. /* Most callers should already have preempt disabled, but make sure */
  2650. preempt_disable();
  2651. list_for_each_entry_rcu(mod, &modules, list)
  2652. printk(" %s%s", mod->name, module_flags(mod, buf));
  2653. preempt_enable();
  2654. if (last_unloaded_module[0])
  2655. printk(" [last unloaded: %s]", last_unloaded_module);
  2656. printk("\n");
  2657. }
  2658. #ifdef CONFIG_MODVERSIONS
  2659. /* Generate the signature for all relevant module structures here.
  2660. * If these change, we don't want to try to parse the module. */
  2661. void module_layout(struct module *mod,
  2662. struct modversion_info *ver,
  2663. struct kernel_param *kp,
  2664. struct kernel_symbol *ks,
  2665. struct tracepoint *tp)
  2666. {
  2667. }
  2668. EXPORT_SYMBOL(module_layout);
  2669. #endif
  2670. #ifdef CONFIG_TRACEPOINTS
  2671. void module_update_tracepoints(void)
  2672. {
  2673. struct module *mod;
  2674. mutex_lock(&module_mutex);
  2675. list_for_each_entry(mod, &modules, list)
  2676. if (!mod->taints)
  2677. tracepoint_update_probe_range(mod->tracepoints,
  2678. mod->tracepoints + mod->num_tracepoints);
  2679. mutex_unlock(&module_mutex);
  2680. }
  2681. /*
  2682. * Returns 0 if current not found.
  2683. * Returns 1 if current found.
  2684. */
  2685. int module_get_iter_tracepoints(struct tracepoint_iter *iter)
  2686. {
  2687. struct module *iter_mod;
  2688. int found = 0;
  2689. mutex_lock(&module_mutex);
  2690. list_for_each_entry(iter_mod, &modules, list) {
  2691. if (!iter_mod->taints) {
  2692. /*
  2693. * Sorted module list
  2694. */
  2695. if (iter_mod < iter->module)
  2696. continue;
  2697. else if (iter_mod > iter->module)
  2698. iter->tracepoint = NULL;
  2699. found = tracepoint_get_iter_range(&iter->tracepoint,
  2700. iter_mod->tracepoints,
  2701. iter_mod->tracepoints
  2702. + iter_mod->num_tracepoints);
  2703. if (found) {
  2704. iter->module = iter_mod;
  2705. break;
  2706. }
  2707. }
  2708. }
  2709. mutex_unlock(&module_mutex);
  2710. return found;
  2711. }
  2712. #endif