module.c 72 KB

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