module.c 82 KB

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