module.c 82 KB

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