module.c 73 KB

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