module.c 81 KB

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