module.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830
  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. unsigned int i;
  507. INIT_LIST_HEAD(&mod->modules_which_use_me);
  508. for (i = 0; i < NR_CPUS; i++)
  509. local_set(&mod->ref[i].count, 0);
  510. /* Hold reference count during initialization. */
  511. local_set(&mod->ref[raw_smp_processor_id()].count, 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 i, total = 0;
  630. for (i = 0; i < NR_CPUS; i++)
  631. total += local_read(&mod->ref[i].count);
  632. return total;
  633. }
  634. EXPORT_SYMBOL(module_refcount);
  635. /* This exists whether we can unload or not */
  636. static void free_module(struct module *mod);
  637. static void wait_for_zero_refcount(struct module *mod)
  638. {
  639. /* Since we might sleep for some time, release the mutex first */
  640. mutex_unlock(&module_mutex);
  641. for (;;) {
  642. DEBUGP("Looking at refcount...\n");
  643. set_current_state(TASK_UNINTERRUPTIBLE);
  644. if (module_refcount(mod) == 0)
  645. break;
  646. schedule();
  647. }
  648. current->state = TASK_RUNNING;
  649. mutex_lock(&module_mutex);
  650. }
  651. SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
  652. unsigned int, flags)
  653. {
  654. struct module *mod;
  655. char name[MODULE_NAME_LEN];
  656. int ret, forced = 0;
  657. if (!capable(CAP_SYS_MODULE))
  658. return -EPERM;
  659. if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
  660. return -EFAULT;
  661. name[MODULE_NAME_LEN-1] = '\0';
  662. /* Create stop_machine threads since free_module relies on
  663. * a non-failing stop_machine call. */
  664. ret = stop_machine_create();
  665. if (ret)
  666. return ret;
  667. if (mutex_lock_interruptible(&module_mutex) != 0) {
  668. ret = -EINTR;
  669. goto out_stop;
  670. }
  671. mod = find_module(name);
  672. if (!mod) {
  673. ret = -ENOENT;
  674. goto out;
  675. }
  676. if (!list_empty(&mod->modules_which_use_me)) {
  677. /* Other modules depend on us: get rid of them first. */
  678. ret = -EWOULDBLOCK;
  679. goto out;
  680. }
  681. /* Doing init or already dying? */
  682. if (mod->state != MODULE_STATE_LIVE) {
  683. /* FIXME: if (force), slam module count and wake up
  684. waiter --RR */
  685. DEBUGP("%s already dying\n", mod->name);
  686. ret = -EBUSY;
  687. goto out;
  688. }
  689. /* If it has an init func, it must have an exit func to unload */
  690. if (mod->init && !mod->exit) {
  691. forced = try_force_unload(flags);
  692. if (!forced) {
  693. /* This module can't be removed */
  694. ret = -EBUSY;
  695. goto out;
  696. }
  697. }
  698. /* Set this up before setting mod->state */
  699. mod->waiter = current;
  700. /* Stop the machine so refcounts can't move and disable module. */
  701. ret = try_stop_module(mod, flags, &forced);
  702. if (ret != 0)
  703. goto out;
  704. /* Never wait if forced. */
  705. if (!forced && module_refcount(mod) != 0)
  706. wait_for_zero_refcount(mod);
  707. mutex_unlock(&module_mutex);
  708. /* Final destruction now noone is using it. */
  709. if (mod->exit != NULL)
  710. mod->exit();
  711. blocking_notifier_call_chain(&module_notify_list,
  712. MODULE_STATE_GOING, mod);
  713. async_synchronize_full();
  714. mutex_lock(&module_mutex);
  715. /* Store the name of the last unloaded module for diagnostic purposes */
  716. strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
  717. unregister_dynamic_debug_module(mod->name);
  718. free_module(mod);
  719. out:
  720. mutex_unlock(&module_mutex);
  721. out_stop:
  722. stop_machine_destroy();
  723. return ret;
  724. }
  725. static inline void print_unload_info(struct seq_file *m, struct module *mod)
  726. {
  727. struct module_use *use;
  728. int printed_something = 0;
  729. seq_printf(m, " %u ", module_refcount(mod));
  730. /* Always include a trailing , so userspace can differentiate
  731. between this and the old multi-field proc format. */
  732. list_for_each_entry(use, &mod->modules_which_use_me, list) {
  733. printed_something = 1;
  734. seq_printf(m, "%s,", use->module_which_uses->name);
  735. }
  736. if (mod->init != NULL && mod->exit == NULL) {
  737. printed_something = 1;
  738. seq_printf(m, "[permanent],");
  739. }
  740. if (!printed_something)
  741. seq_printf(m, "-");
  742. }
  743. void __symbol_put(const char *symbol)
  744. {
  745. struct module *owner;
  746. preempt_disable();
  747. if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
  748. BUG();
  749. module_put(owner);
  750. preempt_enable();
  751. }
  752. EXPORT_SYMBOL(__symbol_put);
  753. void symbol_put_addr(void *addr)
  754. {
  755. struct module *modaddr;
  756. if (core_kernel_text((unsigned long)addr))
  757. return;
  758. if (!(modaddr = module_text_address((unsigned long)addr)))
  759. BUG();
  760. module_put(modaddr);
  761. }
  762. EXPORT_SYMBOL_GPL(symbol_put_addr);
  763. static ssize_t show_refcnt(struct module_attribute *mattr,
  764. struct module *mod, char *buffer)
  765. {
  766. return sprintf(buffer, "%u\n", module_refcount(mod));
  767. }
  768. static struct module_attribute refcnt = {
  769. .attr = { .name = "refcnt", .mode = 0444 },
  770. .show = show_refcnt,
  771. };
  772. void module_put(struct module *module)
  773. {
  774. if (module) {
  775. unsigned int cpu = get_cpu();
  776. local_dec(&module->ref[cpu].count);
  777. /* Maybe they're waiting for us to drop reference? */
  778. if (unlikely(!module_is_live(module)))
  779. wake_up_process(module->waiter);
  780. put_cpu();
  781. }
  782. }
  783. EXPORT_SYMBOL(module_put);
  784. #else /* !CONFIG_MODULE_UNLOAD */
  785. static inline void print_unload_info(struct seq_file *m, struct module *mod)
  786. {
  787. /* We don't know the usage count, or what modules are using. */
  788. seq_printf(m, " - -");
  789. }
  790. static inline void module_unload_free(struct module *mod)
  791. {
  792. }
  793. static inline int use_module(struct module *a, struct module *b)
  794. {
  795. return strong_try_module_get(b) == 0;
  796. }
  797. static inline void module_unload_init(struct module *mod)
  798. {
  799. }
  800. #endif /* CONFIG_MODULE_UNLOAD */
  801. static ssize_t show_initstate(struct module_attribute *mattr,
  802. struct module *mod, char *buffer)
  803. {
  804. const char *state = "unknown";
  805. switch (mod->state) {
  806. case MODULE_STATE_LIVE:
  807. state = "live";
  808. break;
  809. case MODULE_STATE_COMING:
  810. state = "coming";
  811. break;
  812. case MODULE_STATE_GOING:
  813. state = "going";
  814. break;
  815. }
  816. return sprintf(buffer, "%s\n", state);
  817. }
  818. static struct module_attribute initstate = {
  819. .attr = { .name = "initstate", .mode = 0444 },
  820. .show = show_initstate,
  821. };
  822. static struct module_attribute *modinfo_attrs[] = {
  823. &modinfo_version,
  824. &modinfo_srcversion,
  825. &initstate,
  826. #ifdef CONFIG_MODULE_UNLOAD
  827. &refcnt,
  828. #endif
  829. NULL,
  830. };
  831. static const char vermagic[] = VERMAGIC_STRING;
  832. static int try_to_force_load(struct module *mod, const char *symname)
  833. {
  834. #ifdef CONFIG_MODULE_FORCE_LOAD
  835. if (!test_taint(TAINT_FORCED_MODULE))
  836. printk("%s: no version for \"%s\" found: kernel tainted.\n",
  837. mod->name, symname);
  838. add_taint_module(mod, TAINT_FORCED_MODULE);
  839. return 0;
  840. #else
  841. return -ENOEXEC;
  842. #endif
  843. }
  844. #ifdef CONFIG_MODVERSIONS
  845. static int check_version(Elf_Shdr *sechdrs,
  846. unsigned int versindex,
  847. const char *symname,
  848. struct module *mod,
  849. const unsigned long *crc)
  850. {
  851. unsigned int i, num_versions;
  852. struct modversion_info *versions;
  853. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  854. if (!crc)
  855. return 1;
  856. /* No versions at all? modprobe --force does this. */
  857. if (versindex == 0)
  858. return try_to_force_load(mod, symname) == 0;
  859. versions = (void *) sechdrs[versindex].sh_addr;
  860. num_versions = sechdrs[versindex].sh_size
  861. / sizeof(struct modversion_info);
  862. for (i = 0; i < num_versions; i++) {
  863. if (strcmp(versions[i].name, symname) != 0)
  864. continue;
  865. if (versions[i].crc == *crc)
  866. return 1;
  867. DEBUGP("Found checksum %lX vs module %lX\n",
  868. *crc, versions[i].crc);
  869. goto bad_version;
  870. }
  871. printk(KERN_WARNING "%s: no symbol version for %s\n",
  872. mod->name, symname);
  873. return 0;
  874. bad_version:
  875. printk("%s: disagrees about version of symbol %s\n",
  876. mod->name, symname);
  877. return 0;
  878. }
  879. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  880. unsigned int versindex,
  881. struct module *mod)
  882. {
  883. const unsigned long *crc;
  884. if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
  885. BUG();
  886. return check_version(sechdrs, versindex, "struct_module", mod, crc);
  887. }
  888. /* First part is kernel version, which we ignore if module has crcs. */
  889. static inline int same_magic(const char *amagic, const char *bmagic,
  890. bool has_crcs)
  891. {
  892. if (has_crcs) {
  893. amagic += strcspn(amagic, " ");
  894. bmagic += strcspn(bmagic, " ");
  895. }
  896. return strcmp(amagic, bmagic) == 0;
  897. }
  898. #else
  899. static inline int check_version(Elf_Shdr *sechdrs,
  900. unsigned int versindex,
  901. const char *symname,
  902. struct module *mod,
  903. const unsigned long *crc)
  904. {
  905. return 1;
  906. }
  907. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  908. unsigned int versindex,
  909. struct module *mod)
  910. {
  911. return 1;
  912. }
  913. static inline int same_magic(const char *amagic, const char *bmagic,
  914. bool has_crcs)
  915. {
  916. return strcmp(amagic, bmagic) == 0;
  917. }
  918. #endif /* CONFIG_MODVERSIONS */
  919. /* Resolve a symbol for this module. I.e. if we find one, record usage.
  920. Must be holding module_mutex. */
  921. static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
  922. unsigned int versindex,
  923. const char *name,
  924. struct module *mod)
  925. {
  926. struct module *owner;
  927. unsigned long ret;
  928. const unsigned long *crc;
  929. ret = find_symbol(name, &owner, &crc,
  930. !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
  931. if (!IS_ERR_VALUE(ret)) {
  932. /* use_module can fail due to OOM,
  933. or module initialization or unloading */
  934. if (!check_version(sechdrs, versindex, name, mod, crc) ||
  935. !use_module(mod, owner))
  936. ret = -EINVAL;
  937. }
  938. return ret;
  939. }
  940. /*
  941. * /sys/module/foo/sections stuff
  942. * J. Corbet <corbet@lwn.net>
  943. */
  944. #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
  945. struct module_sect_attr
  946. {
  947. struct module_attribute mattr;
  948. char *name;
  949. unsigned long address;
  950. };
  951. struct module_sect_attrs
  952. {
  953. struct attribute_group grp;
  954. unsigned int nsections;
  955. struct module_sect_attr attrs[0];
  956. };
  957. static ssize_t module_sect_show(struct module_attribute *mattr,
  958. struct module *mod, char *buf)
  959. {
  960. struct module_sect_attr *sattr =
  961. container_of(mattr, struct module_sect_attr, mattr);
  962. return sprintf(buf, "0x%lx\n", sattr->address);
  963. }
  964. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  965. {
  966. unsigned int section;
  967. for (section = 0; section < sect_attrs->nsections; section++)
  968. kfree(sect_attrs->attrs[section].name);
  969. kfree(sect_attrs);
  970. }
  971. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  972. char *secstrings, Elf_Shdr *sechdrs)
  973. {
  974. unsigned int nloaded = 0, i, size[2];
  975. struct module_sect_attrs *sect_attrs;
  976. struct module_sect_attr *sattr;
  977. struct attribute **gattr;
  978. /* Count loaded sections and allocate structures */
  979. for (i = 0; i < nsect; i++)
  980. if (sechdrs[i].sh_flags & SHF_ALLOC)
  981. nloaded++;
  982. size[0] = ALIGN(sizeof(*sect_attrs)
  983. + nloaded * sizeof(sect_attrs->attrs[0]),
  984. sizeof(sect_attrs->grp.attrs[0]));
  985. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  986. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  987. if (sect_attrs == NULL)
  988. return;
  989. /* Setup section attributes. */
  990. sect_attrs->grp.name = "sections";
  991. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  992. sect_attrs->nsections = 0;
  993. sattr = &sect_attrs->attrs[0];
  994. gattr = &sect_attrs->grp.attrs[0];
  995. for (i = 0; i < nsect; i++) {
  996. if (! (sechdrs[i].sh_flags & SHF_ALLOC))
  997. continue;
  998. sattr->address = sechdrs[i].sh_addr;
  999. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  1000. GFP_KERNEL);
  1001. if (sattr->name == NULL)
  1002. goto out;
  1003. sect_attrs->nsections++;
  1004. sattr->mattr.show = module_sect_show;
  1005. sattr->mattr.store = NULL;
  1006. sattr->mattr.attr.name = sattr->name;
  1007. sattr->mattr.attr.mode = S_IRUGO;
  1008. *(gattr++) = &(sattr++)->mattr.attr;
  1009. }
  1010. *gattr = NULL;
  1011. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  1012. goto out;
  1013. mod->sect_attrs = sect_attrs;
  1014. return;
  1015. out:
  1016. free_sect_attrs(sect_attrs);
  1017. }
  1018. static void remove_sect_attrs(struct module *mod)
  1019. {
  1020. if (mod->sect_attrs) {
  1021. sysfs_remove_group(&mod->mkobj.kobj,
  1022. &mod->sect_attrs->grp);
  1023. /* We are positive that no one is using any sect attrs
  1024. * at this point. Deallocate immediately. */
  1025. free_sect_attrs(mod->sect_attrs);
  1026. mod->sect_attrs = NULL;
  1027. }
  1028. }
  1029. /*
  1030. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  1031. */
  1032. struct module_notes_attrs {
  1033. struct kobject *dir;
  1034. unsigned int notes;
  1035. struct bin_attribute attrs[0];
  1036. };
  1037. static ssize_t module_notes_read(struct kobject *kobj,
  1038. struct bin_attribute *bin_attr,
  1039. char *buf, loff_t pos, size_t count)
  1040. {
  1041. /*
  1042. * The caller checked the pos and count against our size.
  1043. */
  1044. memcpy(buf, bin_attr->private + pos, count);
  1045. return count;
  1046. }
  1047. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  1048. unsigned int i)
  1049. {
  1050. if (notes_attrs->dir) {
  1051. while (i-- > 0)
  1052. sysfs_remove_bin_file(notes_attrs->dir,
  1053. &notes_attrs->attrs[i]);
  1054. kobject_put(notes_attrs->dir);
  1055. }
  1056. kfree(notes_attrs);
  1057. }
  1058. static void add_notes_attrs(struct module *mod, unsigned int nsect,
  1059. char *secstrings, Elf_Shdr *sechdrs)
  1060. {
  1061. unsigned int notes, loaded, i;
  1062. struct module_notes_attrs *notes_attrs;
  1063. struct bin_attribute *nattr;
  1064. /* Count notes sections and allocate structures. */
  1065. notes = 0;
  1066. for (i = 0; i < nsect; i++)
  1067. if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
  1068. (sechdrs[i].sh_type == SHT_NOTE))
  1069. ++notes;
  1070. if (notes == 0)
  1071. return;
  1072. notes_attrs = kzalloc(sizeof(*notes_attrs)
  1073. + notes * sizeof(notes_attrs->attrs[0]),
  1074. GFP_KERNEL);
  1075. if (notes_attrs == NULL)
  1076. return;
  1077. notes_attrs->notes = notes;
  1078. nattr = &notes_attrs->attrs[0];
  1079. for (loaded = i = 0; i < nsect; ++i) {
  1080. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1081. continue;
  1082. if (sechdrs[i].sh_type == SHT_NOTE) {
  1083. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  1084. nattr->attr.mode = S_IRUGO;
  1085. nattr->size = sechdrs[i].sh_size;
  1086. nattr->private = (void *) sechdrs[i].sh_addr;
  1087. nattr->read = module_notes_read;
  1088. ++nattr;
  1089. }
  1090. ++loaded;
  1091. }
  1092. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  1093. if (!notes_attrs->dir)
  1094. goto out;
  1095. for (i = 0; i < notes; ++i)
  1096. if (sysfs_create_bin_file(notes_attrs->dir,
  1097. &notes_attrs->attrs[i]))
  1098. goto out;
  1099. mod->notes_attrs = notes_attrs;
  1100. return;
  1101. out:
  1102. free_notes_attrs(notes_attrs, i);
  1103. }
  1104. static void remove_notes_attrs(struct module *mod)
  1105. {
  1106. if (mod->notes_attrs)
  1107. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1108. }
  1109. #else
  1110. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  1111. char *sectstrings, Elf_Shdr *sechdrs)
  1112. {
  1113. }
  1114. static inline void remove_sect_attrs(struct module *mod)
  1115. {
  1116. }
  1117. static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
  1118. char *sectstrings, Elf_Shdr *sechdrs)
  1119. {
  1120. }
  1121. static inline void remove_notes_attrs(struct module *mod)
  1122. {
  1123. }
  1124. #endif
  1125. #ifdef CONFIG_SYSFS
  1126. int module_add_modinfo_attrs(struct module *mod)
  1127. {
  1128. struct module_attribute *attr;
  1129. struct module_attribute *temp_attr;
  1130. int error = 0;
  1131. int i;
  1132. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1133. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1134. GFP_KERNEL);
  1135. if (!mod->modinfo_attrs)
  1136. return -ENOMEM;
  1137. temp_attr = mod->modinfo_attrs;
  1138. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1139. if (!attr->test ||
  1140. (attr->test && attr->test(mod))) {
  1141. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1142. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  1143. ++temp_attr;
  1144. }
  1145. }
  1146. return error;
  1147. }
  1148. void module_remove_modinfo_attrs(struct module *mod)
  1149. {
  1150. struct module_attribute *attr;
  1151. int i;
  1152. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1153. /* pick a field to test for end of list */
  1154. if (!attr->attr.name)
  1155. break;
  1156. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  1157. if (attr->free)
  1158. attr->free(mod);
  1159. }
  1160. kfree(mod->modinfo_attrs);
  1161. }
  1162. int mod_sysfs_init(struct module *mod)
  1163. {
  1164. int err;
  1165. struct kobject *kobj;
  1166. if (!module_sysfs_initialized) {
  1167. printk(KERN_ERR "%s: module sysfs not initialized\n",
  1168. mod->name);
  1169. err = -EINVAL;
  1170. goto out;
  1171. }
  1172. kobj = kset_find_obj(module_kset, mod->name);
  1173. if (kobj) {
  1174. printk(KERN_ERR "%s: module is already loaded\n", mod->name);
  1175. kobject_put(kobj);
  1176. err = -EINVAL;
  1177. goto out;
  1178. }
  1179. mod->mkobj.mod = mod;
  1180. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1181. mod->mkobj.kobj.kset = module_kset;
  1182. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1183. "%s", mod->name);
  1184. if (err)
  1185. kobject_put(&mod->mkobj.kobj);
  1186. /* delay uevent until full sysfs population */
  1187. out:
  1188. return err;
  1189. }
  1190. int mod_sysfs_setup(struct module *mod,
  1191. struct kernel_param *kparam,
  1192. unsigned int num_params)
  1193. {
  1194. int err;
  1195. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1196. if (!mod->holders_dir) {
  1197. err = -ENOMEM;
  1198. goto out_unreg;
  1199. }
  1200. err = module_param_sysfs_setup(mod, kparam, num_params);
  1201. if (err)
  1202. goto out_unreg_holders;
  1203. err = module_add_modinfo_attrs(mod);
  1204. if (err)
  1205. goto out_unreg_param;
  1206. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1207. return 0;
  1208. out_unreg_param:
  1209. module_param_sysfs_remove(mod);
  1210. out_unreg_holders:
  1211. kobject_put(mod->holders_dir);
  1212. out_unreg:
  1213. kobject_put(&mod->mkobj.kobj);
  1214. return err;
  1215. }
  1216. static void mod_sysfs_fini(struct module *mod)
  1217. {
  1218. kobject_put(&mod->mkobj.kobj);
  1219. }
  1220. #else /* CONFIG_SYSFS */
  1221. static void mod_sysfs_fini(struct module *mod)
  1222. {
  1223. }
  1224. #endif /* CONFIG_SYSFS */
  1225. static void mod_kobject_remove(struct module *mod)
  1226. {
  1227. module_remove_modinfo_attrs(mod);
  1228. module_param_sysfs_remove(mod);
  1229. kobject_put(mod->mkobj.drivers_dir);
  1230. kobject_put(mod->holders_dir);
  1231. mod_sysfs_fini(mod);
  1232. }
  1233. /*
  1234. * unlink the module with the whole machine is stopped with interrupts off
  1235. * - this defends against kallsyms not taking locks
  1236. */
  1237. static int __unlink_module(void *_mod)
  1238. {
  1239. struct module *mod = _mod;
  1240. list_del(&mod->list);
  1241. return 0;
  1242. }
  1243. /* Free a module, remove from lists, etc (must hold module_mutex). */
  1244. static void free_module(struct module *mod)
  1245. {
  1246. /* Delete from various lists */
  1247. stop_machine(__unlink_module, mod, NULL);
  1248. remove_notes_attrs(mod);
  1249. remove_sect_attrs(mod);
  1250. mod_kobject_remove(mod);
  1251. /* Arch-specific cleanup. */
  1252. module_arch_cleanup(mod);
  1253. /* Module unload stuff */
  1254. module_unload_free(mod);
  1255. /* release any pointers to mcount in this module */
  1256. ftrace_release(mod->module_core, mod->core_size);
  1257. /* This may be NULL, but that's OK */
  1258. module_free(mod, mod->module_init);
  1259. kfree(mod->args);
  1260. if (mod->percpu)
  1261. percpu_modfree(mod->percpu);
  1262. /* Free lock-classes: */
  1263. lockdep_free_key_range(mod->module_core, mod->core_size);
  1264. /* Finally, free the core (containing the module structure) */
  1265. module_free(mod, mod->module_core);
  1266. }
  1267. void *__symbol_get(const char *symbol)
  1268. {
  1269. struct module *owner;
  1270. unsigned long value;
  1271. preempt_disable();
  1272. value = find_symbol(symbol, &owner, NULL, true, true);
  1273. if (IS_ERR_VALUE(value))
  1274. value = 0;
  1275. else if (strong_try_module_get(owner))
  1276. value = 0;
  1277. preempt_enable();
  1278. return (void *)value;
  1279. }
  1280. EXPORT_SYMBOL_GPL(__symbol_get);
  1281. /*
  1282. * Ensure that an exported symbol [global namespace] does not already exist
  1283. * in the kernel or in some other module's exported symbol table.
  1284. */
  1285. static int verify_export_symbols(struct module *mod)
  1286. {
  1287. unsigned int i;
  1288. struct module *owner;
  1289. const struct kernel_symbol *s;
  1290. struct {
  1291. const struct kernel_symbol *sym;
  1292. unsigned int num;
  1293. } arr[] = {
  1294. { mod->syms, mod->num_syms },
  1295. { mod->gpl_syms, mod->num_gpl_syms },
  1296. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1297. #ifdef CONFIG_UNUSED_SYMBOLS
  1298. { mod->unused_syms, mod->num_unused_syms },
  1299. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1300. #endif
  1301. };
  1302. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1303. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1304. if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
  1305. NULL, true, false))) {
  1306. printk(KERN_ERR
  1307. "%s: exports duplicate symbol %s"
  1308. " (owned by %s)\n",
  1309. mod->name, s->name, module_name(owner));
  1310. return -ENOEXEC;
  1311. }
  1312. }
  1313. }
  1314. return 0;
  1315. }
  1316. /* Change all symbols so that st_value encodes the pointer directly. */
  1317. static int simplify_symbols(Elf_Shdr *sechdrs,
  1318. unsigned int symindex,
  1319. const char *strtab,
  1320. unsigned int versindex,
  1321. unsigned int pcpuindex,
  1322. struct module *mod)
  1323. {
  1324. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1325. unsigned long secbase;
  1326. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1327. int ret = 0;
  1328. for (i = 1; i < n; i++) {
  1329. switch (sym[i].st_shndx) {
  1330. case SHN_COMMON:
  1331. /* We compiled with -fno-common. These are not
  1332. supposed to happen. */
  1333. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1334. printk("%s: please compile with -fno-common\n",
  1335. mod->name);
  1336. ret = -ENOEXEC;
  1337. break;
  1338. case SHN_ABS:
  1339. /* Don't need to do anything */
  1340. DEBUGP("Absolute symbol: 0x%08lx\n",
  1341. (long)sym[i].st_value);
  1342. break;
  1343. case SHN_UNDEF:
  1344. sym[i].st_value
  1345. = resolve_symbol(sechdrs, versindex,
  1346. strtab + sym[i].st_name, mod);
  1347. /* Ok if resolved. */
  1348. if (!IS_ERR_VALUE(sym[i].st_value))
  1349. break;
  1350. /* Ok if weak. */
  1351. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1352. break;
  1353. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1354. mod->name, strtab + sym[i].st_name);
  1355. ret = -ENOENT;
  1356. break;
  1357. default:
  1358. /* Divert to percpu allocation if a percpu var. */
  1359. if (sym[i].st_shndx == pcpuindex)
  1360. secbase = (unsigned long)mod->percpu;
  1361. else
  1362. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1363. sym[i].st_value += secbase;
  1364. break;
  1365. }
  1366. }
  1367. return ret;
  1368. }
  1369. /* Additional bytes needed by arch in front of individual sections */
  1370. unsigned int __weak arch_mod_section_prepend(struct module *mod,
  1371. unsigned int section)
  1372. {
  1373. /* default implementation just returns zero */
  1374. return 0;
  1375. }
  1376. /* Update size with this section: return offset. */
  1377. static long get_offset(struct module *mod, unsigned int *size,
  1378. Elf_Shdr *sechdr, unsigned int section)
  1379. {
  1380. long ret;
  1381. *size += arch_mod_section_prepend(mod, section);
  1382. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1383. *size = ret + sechdr->sh_size;
  1384. return ret;
  1385. }
  1386. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1387. might -- code, read-only data, read-write data, small data. Tally
  1388. sizes, and place the offsets into sh_entsize fields: high bit means it
  1389. belongs in init. */
  1390. static void layout_sections(struct module *mod,
  1391. const Elf_Ehdr *hdr,
  1392. Elf_Shdr *sechdrs,
  1393. const char *secstrings)
  1394. {
  1395. static unsigned long const masks[][2] = {
  1396. /* NOTE: all executable code must be the first section
  1397. * in this array; otherwise modify the text_size
  1398. * finder in the two loops below */
  1399. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1400. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1401. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1402. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1403. };
  1404. unsigned int m, i;
  1405. for (i = 0; i < hdr->e_shnum; i++)
  1406. sechdrs[i].sh_entsize = ~0UL;
  1407. DEBUGP("Core section allocation order:\n");
  1408. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1409. for (i = 0; i < hdr->e_shnum; ++i) {
  1410. Elf_Shdr *s = &sechdrs[i];
  1411. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1412. || (s->sh_flags & masks[m][1])
  1413. || s->sh_entsize != ~0UL
  1414. || strncmp(secstrings + s->sh_name,
  1415. ".init", 5) == 0)
  1416. continue;
  1417. s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
  1418. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1419. }
  1420. if (m == 0)
  1421. mod->core_text_size = mod->core_size;
  1422. }
  1423. DEBUGP("Init section allocation order:\n");
  1424. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1425. for (i = 0; i < hdr->e_shnum; ++i) {
  1426. Elf_Shdr *s = &sechdrs[i];
  1427. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1428. || (s->sh_flags & masks[m][1])
  1429. || s->sh_entsize != ~0UL
  1430. || strncmp(secstrings + s->sh_name,
  1431. ".init", 5) != 0)
  1432. continue;
  1433. s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
  1434. | INIT_OFFSET_MASK);
  1435. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1436. }
  1437. if (m == 0)
  1438. mod->init_text_size = mod->init_size;
  1439. }
  1440. }
  1441. static void set_license(struct module *mod, const char *license)
  1442. {
  1443. if (!license)
  1444. license = "unspecified";
  1445. if (!license_is_gpl_compatible(license)) {
  1446. if (!test_taint(TAINT_PROPRIETARY_MODULE))
  1447. printk(KERN_WARNING "%s: module license '%s' taints "
  1448. "kernel.\n", mod->name, license);
  1449. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1450. }
  1451. }
  1452. /* Parse tag=value strings from .modinfo section */
  1453. static char *next_string(char *string, unsigned long *secsize)
  1454. {
  1455. /* Skip non-zero chars */
  1456. while (string[0]) {
  1457. string++;
  1458. if ((*secsize)-- <= 1)
  1459. return NULL;
  1460. }
  1461. /* Skip any zero padding. */
  1462. while (!string[0]) {
  1463. string++;
  1464. if ((*secsize)-- <= 1)
  1465. return NULL;
  1466. }
  1467. return string;
  1468. }
  1469. static char *get_modinfo(Elf_Shdr *sechdrs,
  1470. unsigned int info,
  1471. const char *tag)
  1472. {
  1473. char *p;
  1474. unsigned int taglen = strlen(tag);
  1475. unsigned long size = sechdrs[info].sh_size;
  1476. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1477. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1478. return p + taglen + 1;
  1479. }
  1480. return NULL;
  1481. }
  1482. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1483. unsigned int infoindex)
  1484. {
  1485. struct module_attribute *attr;
  1486. int i;
  1487. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1488. if (attr->setup)
  1489. attr->setup(mod,
  1490. get_modinfo(sechdrs,
  1491. infoindex,
  1492. attr->attr.name));
  1493. }
  1494. }
  1495. #ifdef CONFIG_KALLSYMS
  1496. /* lookup symbol in given range of kernel_symbols */
  1497. static const struct kernel_symbol *lookup_symbol(const char *name,
  1498. const struct kernel_symbol *start,
  1499. const struct kernel_symbol *stop)
  1500. {
  1501. const struct kernel_symbol *ks = start;
  1502. for (; ks < stop; ks++)
  1503. if (strcmp(ks->name, name) == 0)
  1504. return ks;
  1505. return NULL;
  1506. }
  1507. static int is_exported(const char *name, unsigned long value,
  1508. const struct module *mod)
  1509. {
  1510. const struct kernel_symbol *ks;
  1511. if (!mod)
  1512. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  1513. else
  1514. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  1515. return ks != NULL && ks->value == value;
  1516. }
  1517. /* As per nm */
  1518. static char elf_type(const Elf_Sym *sym,
  1519. Elf_Shdr *sechdrs,
  1520. const char *secstrings,
  1521. struct module *mod)
  1522. {
  1523. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1524. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1525. return 'v';
  1526. else
  1527. return 'w';
  1528. }
  1529. if (sym->st_shndx == SHN_UNDEF)
  1530. return 'U';
  1531. if (sym->st_shndx == SHN_ABS)
  1532. return 'a';
  1533. if (sym->st_shndx >= SHN_LORESERVE)
  1534. return '?';
  1535. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1536. return 't';
  1537. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1538. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1539. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1540. return 'r';
  1541. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1542. return 'g';
  1543. else
  1544. return 'd';
  1545. }
  1546. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1547. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1548. return 's';
  1549. else
  1550. return 'b';
  1551. }
  1552. if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
  1553. ".debug", strlen(".debug")) == 0)
  1554. return 'n';
  1555. return '?';
  1556. }
  1557. static void add_kallsyms(struct module *mod,
  1558. Elf_Shdr *sechdrs,
  1559. unsigned int symindex,
  1560. unsigned int strindex,
  1561. const char *secstrings)
  1562. {
  1563. unsigned int i;
  1564. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1565. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1566. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1567. /* Set types up while we still have access to sections. */
  1568. for (i = 0; i < mod->num_symtab; i++)
  1569. mod->symtab[i].st_info
  1570. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1571. }
  1572. #else
  1573. static inline void add_kallsyms(struct module *mod,
  1574. Elf_Shdr *sechdrs,
  1575. unsigned int symindex,
  1576. unsigned int strindex,
  1577. const char *secstrings)
  1578. {
  1579. }
  1580. #endif /* CONFIG_KALLSYMS */
  1581. static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
  1582. {
  1583. #ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
  1584. unsigned int i;
  1585. for (i = 0; i < num; i++) {
  1586. register_dynamic_debug_module(debug[i].modname,
  1587. debug[i].type,
  1588. debug[i].logical_modname,
  1589. debug[i].flag_names,
  1590. debug[i].hash, debug[i].hash2);
  1591. }
  1592. #endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
  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. /* Now we've moved module, initialize linked lists, etc. */
  1795. module_unload_init(mod);
  1796. /* add kobject, so we can reference it. */
  1797. err = mod_sysfs_init(mod);
  1798. if (err)
  1799. goto free_unload;
  1800. /* Set up license info based on the info section */
  1801. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1802. /*
  1803. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  1804. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  1805. * using GPL-only symbols it needs.
  1806. */
  1807. if (strcmp(mod->name, "ndiswrapper") == 0)
  1808. add_taint(TAINT_PROPRIETARY_MODULE);
  1809. /* driverloader was caught wrongly pretending to be under GPL */
  1810. if (strcmp(mod->name, "driverloader") == 0)
  1811. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1812. /* Set up MODINFO_ATTR fields */
  1813. setup_modinfo(mod, sechdrs, infoindex);
  1814. /* Fix up syms, so that st_value is a pointer to location. */
  1815. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1816. mod);
  1817. if (err < 0)
  1818. goto cleanup;
  1819. /* Now we've got everything in the final locations, we can
  1820. * find optional sections. */
  1821. kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
  1822. &num_kp);
  1823. mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
  1824. sizeof(*mod->syms), &mod->num_syms);
  1825. mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
  1826. mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
  1827. sizeof(*mod->gpl_syms),
  1828. &mod->num_gpl_syms);
  1829. mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1830. mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
  1831. "__ksymtab_gpl_future",
  1832. sizeof(*mod->gpl_future_syms),
  1833. &mod->num_gpl_future_syms);
  1834. mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
  1835. "__kcrctab_gpl_future");
  1836. #ifdef CONFIG_UNUSED_SYMBOLS
  1837. mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
  1838. "__ksymtab_unused",
  1839. sizeof(*mod->unused_syms),
  1840. &mod->num_unused_syms);
  1841. mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
  1842. "__kcrctab_unused");
  1843. mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
  1844. "__ksymtab_unused_gpl",
  1845. sizeof(*mod->unused_gpl_syms),
  1846. &mod->num_unused_gpl_syms);
  1847. mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
  1848. "__kcrctab_unused_gpl");
  1849. #endif
  1850. #ifdef CONFIG_MARKERS
  1851. mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
  1852. sizeof(*mod->markers), &mod->num_markers);
  1853. #endif
  1854. #ifdef CONFIG_TRACEPOINTS
  1855. mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
  1856. "__tracepoints",
  1857. sizeof(*mod->tracepoints),
  1858. &mod->num_tracepoints);
  1859. #endif
  1860. #ifdef CONFIG_MODVERSIONS
  1861. if ((mod->num_syms && !mod->crcs)
  1862. || (mod->num_gpl_syms && !mod->gpl_crcs)
  1863. || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
  1864. #ifdef CONFIG_UNUSED_SYMBOLS
  1865. || (mod->num_unused_syms && !mod->unused_crcs)
  1866. || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
  1867. #endif
  1868. ) {
  1869. printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
  1870. err = try_to_force_load(mod, "nocrc");
  1871. if (err)
  1872. goto cleanup;
  1873. }
  1874. #endif
  1875. /* Now do relocations. */
  1876. for (i = 1; i < hdr->e_shnum; i++) {
  1877. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1878. unsigned int info = sechdrs[i].sh_info;
  1879. /* Not a valid relocation section? */
  1880. if (info >= hdr->e_shnum)
  1881. continue;
  1882. /* Don't bother with non-allocated sections */
  1883. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1884. continue;
  1885. if (sechdrs[i].sh_type == SHT_REL)
  1886. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1887. else if (sechdrs[i].sh_type == SHT_RELA)
  1888. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1889. mod);
  1890. if (err < 0)
  1891. goto cleanup;
  1892. }
  1893. /* Find duplicate symbols */
  1894. err = verify_export_symbols(mod);
  1895. if (err < 0)
  1896. goto cleanup;
  1897. /* Set up and sort exception table */
  1898. mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
  1899. sizeof(*mod->extable), &mod->num_exentries);
  1900. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  1901. /* Finally, copy percpu area over. */
  1902. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1903. sechdrs[pcpuindex].sh_size);
  1904. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1905. if (!mod->taints) {
  1906. struct mod_debug *debug;
  1907. unsigned int num_debug;
  1908. debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
  1909. sizeof(*debug), &num_debug);
  1910. dynamic_printk_setup(debug, num_debug);
  1911. }
  1912. /* sechdrs[0].sh_size is always zero */
  1913. mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
  1914. sizeof(*mseg), &num_mcount);
  1915. ftrace_init_module(mod, mseg, mseg + num_mcount);
  1916. err = module_finalize(hdr, sechdrs, mod);
  1917. if (err < 0)
  1918. goto cleanup;
  1919. /* flush the icache in correct context */
  1920. old_fs = get_fs();
  1921. set_fs(KERNEL_DS);
  1922. /*
  1923. * Flush the instruction cache, since we've played with text.
  1924. * Do it before processing of module parameters, so the module
  1925. * can provide parameter accessor functions of its own.
  1926. */
  1927. if (mod->module_init)
  1928. flush_icache_range((unsigned long)mod->module_init,
  1929. (unsigned long)mod->module_init
  1930. + mod->init_size);
  1931. flush_icache_range((unsigned long)mod->module_core,
  1932. (unsigned long)mod->module_core + mod->core_size);
  1933. set_fs(old_fs);
  1934. mod->args = args;
  1935. if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
  1936. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1937. mod->name);
  1938. /* Now sew it into the lists so we can get lockdep and oops
  1939. * info during argument parsing. Noone should access us, since
  1940. * strong_try_module_get() will fail.
  1941. * lockdep/oops can run asynchronous, so use the RCU list insertion
  1942. * function to insert in a way safe to concurrent readers.
  1943. * The mutex protects against concurrent writers.
  1944. */
  1945. list_add_rcu(&mod->list, &modules);
  1946. err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
  1947. if (err < 0)
  1948. goto unlink;
  1949. err = mod_sysfs_setup(mod, kp, num_kp);
  1950. if (err < 0)
  1951. goto unlink;
  1952. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1953. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1954. /* Get rid of temporary copy */
  1955. vfree(hdr);
  1956. stop_machine_destroy();
  1957. /* Done! */
  1958. return mod;
  1959. unlink:
  1960. stop_machine(__unlink_module, mod, NULL);
  1961. module_arch_cleanup(mod);
  1962. cleanup:
  1963. kobject_del(&mod->mkobj.kobj);
  1964. kobject_put(&mod->mkobj.kobj);
  1965. ftrace_release(mod->module_core, mod->core_size);
  1966. free_unload:
  1967. module_unload_free(mod);
  1968. module_free(mod, mod->module_init);
  1969. free_core:
  1970. module_free(mod, mod->module_core);
  1971. free_percpu:
  1972. if (percpu)
  1973. percpu_modfree(percpu);
  1974. free_mod:
  1975. kfree(args);
  1976. free_hdr:
  1977. vfree(hdr);
  1978. stop_machine_destroy();
  1979. return ERR_PTR(err);
  1980. truncated:
  1981. printk(KERN_ERR "Module len %lu truncated\n", len);
  1982. err = -ENOEXEC;
  1983. goto free_hdr;
  1984. }
  1985. /* This is where the real work happens */
  1986. SYSCALL_DEFINE3(init_module, void __user *, umod,
  1987. unsigned long, len, const char __user *, uargs)
  1988. {
  1989. struct module *mod;
  1990. int ret = 0;
  1991. /* Must have permission */
  1992. if (!capable(CAP_SYS_MODULE))
  1993. return -EPERM;
  1994. /* Only one module load at a time, please */
  1995. if (mutex_lock_interruptible(&module_mutex) != 0)
  1996. return -EINTR;
  1997. /* Do all the hard work */
  1998. mod = load_module(umod, len, uargs);
  1999. if (IS_ERR(mod)) {
  2000. mutex_unlock(&module_mutex);
  2001. return PTR_ERR(mod);
  2002. }
  2003. /* Drop lock so they can recurse */
  2004. mutex_unlock(&module_mutex);
  2005. blocking_notifier_call_chain(&module_notify_list,
  2006. MODULE_STATE_COMING, mod);
  2007. /* Start the module */
  2008. if (mod->init != NULL)
  2009. ret = do_one_initcall(mod->init);
  2010. if (ret < 0) {
  2011. /* Init routine failed: abort. Try to protect us from
  2012. buggy refcounters. */
  2013. mod->state = MODULE_STATE_GOING;
  2014. synchronize_sched();
  2015. module_put(mod);
  2016. blocking_notifier_call_chain(&module_notify_list,
  2017. MODULE_STATE_GOING, mod);
  2018. mutex_lock(&module_mutex);
  2019. free_module(mod);
  2020. mutex_unlock(&module_mutex);
  2021. wake_up(&module_wq);
  2022. return ret;
  2023. }
  2024. if (ret > 0) {
  2025. printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
  2026. "it should follow 0/-E convention\n"
  2027. KERN_WARNING "%s: loading module anyway...\n",
  2028. __func__, mod->name, ret,
  2029. __func__);
  2030. dump_stack();
  2031. }
  2032. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  2033. mod->state = MODULE_STATE_LIVE;
  2034. wake_up(&module_wq);
  2035. blocking_notifier_call_chain(&module_notify_list,
  2036. MODULE_STATE_LIVE, mod);
  2037. mutex_lock(&module_mutex);
  2038. /* Drop initial reference. */
  2039. module_put(mod);
  2040. module_free(mod, mod->module_init);
  2041. mod->module_init = NULL;
  2042. mod->init_size = 0;
  2043. mod->init_text_size = 0;
  2044. mutex_unlock(&module_mutex);
  2045. return 0;
  2046. }
  2047. static inline int within(unsigned long addr, void *start, unsigned long size)
  2048. {
  2049. return ((void *)addr >= start && (void *)addr < start + size);
  2050. }
  2051. #ifdef CONFIG_KALLSYMS
  2052. /*
  2053. * This ignores the intensely annoying "mapping symbols" found
  2054. * in ARM ELF files: $a, $t and $d.
  2055. */
  2056. static inline int is_arm_mapping_symbol(const char *str)
  2057. {
  2058. return str[0] == '$' && strchr("atd", str[1])
  2059. && (str[2] == '\0' || str[2] == '.');
  2060. }
  2061. static const char *get_ksymbol(struct module *mod,
  2062. unsigned long addr,
  2063. unsigned long *size,
  2064. unsigned long *offset)
  2065. {
  2066. unsigned int i, best = 0;
  2067. unsigned long nextval;
  2068. /* At worse, next value is at end of module */
  2069. if (within_module_init(addr, mod))
  2070. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  2071. else
  2072. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  2073. /* Scan for closest preceeding symbol, and next symbol. (ELF
  2074. starts real symbols at 1). */
  2075. for (i = 1; i < mod->num_symtab; i++) {
  2076. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  2077. continue;
  2078. /* We ignore unnamed symbols: they're uninformative
  2079. * and inserted at a whim. */
  2080. if (mod->symtab[i].st_value <= addr
  2081. && mod->symtab[i].st_value > mod->symtab[best].st_value
  2082. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2083. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2084. best = i;
  2085. if (mod->symtab[i].st_value > addr
  2086. && mod->symtab[i].st_value < nextval
  2087. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2088. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2089. nextval = mod->symtab[i].st_value;
  2090. }
  2091. if (!best)
  2092. return NULL;
  2093. if (size)
  2094. *size = nextval - mod->symtab[best].st_value;
  2095. if (offset)
  2096. *offset = addr - mod->symtab[best].st_value;
  2097. return mod->strtab + mod->symtab[best].st_name;
  2098. }
  2099. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  2100. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  2101. const char *module_address_lookup(unsigned long addr,
  2102. unsigned long *size,
  2103. unsigned long *offset,
  2104. char **modname,
  2105. char *namebuf)
  2106. {
  2107. struct module *mod;
  2108. const char *ret = NULL;
  2109. preempt_disable();
  2110. list_for_each_entry_rcu(mod, &modules, list) {
  2111. if (within_module_init(addr, mod) ||
  2112. within_module_core(addr, mod)) {
  2113. if (modname)
  2114. *modname = mod->name;
  2115. ret = get_ksymbol(mod, addr, size, offset);
  2116. break;
  2117. }
  2118. }
  2119. /* Make a copy in here where it's safe */
  2120. if (ret) {
  2121. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2122. ret = namebuf;
  2123. }
  2124. preempt_enable();
  2125. return ret;
  2126. }
  2127. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2128. {
  2129. struct module *mod;
  2130. preempt_disable();
  2131. list_for_each_entry_rcu(mod, &modules, list) {
  2132. if (within_module_init(addr, mod) ||
  2133. within_module_core(addr, mod)) {
  2134. const char *sym;
  2135. sym = get_ksymbol(mod, addr, NULL, NULL);
  2136. if (!sym)
  2137. goto out;
  2138. strlcpy(symname, sym, KSYM_NAME_LEN);
  2139. preempt_enable();
  2140. return 0;
  2141. }
  2142. }
  2143. out:
  2144. preempt_enable();
  2145. return -ERANGE;
  2146. }
  2147. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2148. unsigned long *offset, char *modname, char *name)
  2149. {
  2150. struct module *mod;
  2151. preempt_disable();
  2152. list_for_each_entry_rcu(mod, &modules, list) {
  2153. if (within_module_init(addr, mod) ||
  2154. within_module_core(addr, mod)) {
  2155. const char *sym;
  2156. sym = get_ksymbol(mod, addr, size, offset);
  2157. if (!sym)
  2158. goto out;
  2159. if (modname)
  2160. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2161. if (name)
  2162. strlcpy(name, sym, KSYM_NAME_LEN);
  2163. preempt_enable();
  2164. return 0;
  2165. }
  2166. }
  2167. out:
  2168. preempt_enable();
  2169. return -ERANGE;
  2170. }
  2171. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2172. char *name, char *module_name, int *exported)
  2173. {
  2174. struct module *mod;
  2175. preempt_disable();
  2176. list_for_each_entry_rcu(mod, &modules, list) {
  2177. if (symnum < mod->num_symtab) {
  2178. *value = mod->symtab[symnum].st_value;
  2179. *type = mod->symtab[symnum].st_info;
  2180. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2181. KSYM_NAME_LEN);
  2182. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2183. *exported = is_exported(name, *value, mod);
  2184. preempt_enable();
  2185. return 0;
  2186. }
  2187. symnum -= mod->num_symtab;
  2188. }
  2189. preempt_enable();
  2190. return -ERANGE;
  2191. }
  2192. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2193. {
  2194. unsigned int i;
  2195. for (i = 0; i < mod->num_symtab; i++)
  2196. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2197. mod->symtab[i].st_info != 'U')
  2198. return mod->symtab[i].st_value;
  2199. return 0;
  2200. }
  2201. /* Look for this name: can be of form module:name. */
  2202. unsigned long module_kallsyms_lookup_name(const char *name)
  2203. {
  2204. struct module *mod;
  2205. char *colon;
  2206. unsigned long ret = 0;
  2207. /* Don't lock: we're in enough trouble already. */
  2208. preempt_disable();
  2209. if ((colon = strchr(name, ':')) != NULL) {
  2210. *colon = '\0';
  2211. if ((mod = find_module(name)) != NULL)
  2212. ret = mod_find_symname(mod, colon+1);
  2213. *colon = ':';
  2214. } else {
  2215. list_for_each_entry_rcu(mod, &modules, list)
  2216. if ((ret = mod_find_symname(mod, name)) != 0)
  2217. break;
  2218. }
  2219. preempt_enable();
  2220. return ret;
  2221. }
  2222. #endif /* CONFIG_KALLSYMS */
  2223. static char *module_flags(struct module *mod, char *buf)
  2224. {
  2225. int bx = 0;
  2226. if (mod->taints ||
  2227. mod->state == MODULE_STATE_GOING ||
  2228. mod->state == MODULE_STATE_COMING) {
  2229. buf[bx++] = '(';
  2230. if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
  2231. buf[bx++] = 'P';
  2232. if (mod->taints & (1 << TAINT_FORCED_MODULE))
  2233. buf[bx++] = 'F';
  2234. if (mod->taints & (1 << TAINT_CRAP))
  2235. buf[bx++] = 'C';
  2236. /*
  2237. * TAINT_FORCED_RMMOD: could be added.
  2238. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2239. * apply to modules.
  2240. */
  2241. /* Show a - for module-is-being-unloaded */
  2242. if (mod->state == MODULE_STATE_GOING)
  2243. buf[bx++] = '-';
  2244. /* Show a + for module-is-being-loaded */
  2245. if (mod->state == MODULE_STATE_COMING)
  2246. buf[bx++] = '+';
  2247. buf[bx++] = ')';
  2248. }
  2249. buf[bx] = '\0';
  2250. return buf;
  2251. }
  2252. #ifdef CONFIG_PROC_FS
  2253. /* Called by the /proc file system to return a list of modules. */
  2254. static void *m_start(struct seq_file *m, loff_t *pos)
  2255. {
  2256. mutex_lock(&module_mutex);
  2257. return seq_list_start(&modules, *pos);
  2258. }
  2259. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2260. {
  2261. return seq_list_next(p, &modules, pos);
  2262. }
  2263. static void m_stop(struct seq_file *m, void *p)
  2264. {
  2265. mutex_unlock(&module_mutex);
  2266. }
  2267. static int m_show(struct seq_file *m, void *p)
  2268. {
  2269. struct module *mod = list_entry(p, struct module, list);
  2270. char buf[8];
  2271. seq_printf(m, "%s %u",
  2272. mod->name, mod->init_size + mod->core_size);
  2273. print_unload_info(m, mod);
  2274. /* Informative for users. */
  2275. seq_printf(m, " %s",
  2276. mod->state == MODULE_STATE_GOING ? "Unloading":
  2277. mod->state == MODULE_STATE_COMING ? "Loading":
  2278. "Live");
  2279. /* Used by oprofile and other similar tools. */
  2280. seq_printf(m, " 0x%p", mod->module_core);
  2281. /* Taints info */
  2282. if (mod->taints)
  2283. seq_printf(m, " %s", module_flags(mod, buf));
  2284. seq_printf(m, "\n");
  2285. return 0;
  2286. }
  2287. /* Format: modulename size refcount deps address
  2288. Where refcount is a number or -, and deps is a comma-separated list
  2289. of depends or -.
  2290. */
  2291. static const struct seq_operations modules_op = {
  2292. .start = m_start,
  2293. .next = m_next,
  2294. .stop = m_stop,
  2295. .show = m_show
  2296. };
  2297. static int modules_open(struct inode *inode, struct file *file)
  2298. {
  2299. return seq_open(file, &modules_op);
  2300. }
  2301. static const struct file_operations proc_modules_operations = {
  2302. .open = modules_open,
  2303. .read = seq_read,
  2304. .llseek = seq_lseek,
  2305. .release = seq_release,
  2306. };
  2307. static int __init proc_modules_init(void)
  2308. {
  2309. proc_create("modules", 0, NULL, &proc_modules_operations);
  2310. return 0;
  2311. }
  2312. module_init(proc_modules_init);
  2313. #endif
  2314. /* Given an address, look for it in the module exception tables. */
  2315. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2316. {
  2317. const struct exception_table_entry *e = NULL;
  2318. struct module *mod;
  2319. preempt_disable();
  2320. list_for_each_entry_rcu(mod, &modules, list) {
  2321. if (mod->num_exentries == 0)
  2322. continue;
  2323. e = search_extable(mod->extable,
  2324. mod->extable + mod->num_exentries - 1,
  2325. addr);
  2326. if (e)
  2327. break;
  2328. }
  2329. preempt_enable();
  2330. /* Now, if we found one, we are running inside it now, hence
  2331. we cannot unload the module, hence no refcnt needed. */
  2332. return e;
  2333. }
  2334. /*
  2335. * Is this a valid module address?
  2336. */
  2337. int is_module_address(unsigned long addr)
  2338. {
  2339. struct module *mod;
  2340. preempt_disable();
  2341. list_for_each_entry_rcu(mod, &modules, list) {
  2342. if (within_module_core(addr, mod)) {
  2343. preempt_enable();
  2344. return 1;
  2345. }
  2346. }
  2347. preempt_enable();
  2348. return 0;
  2349. }
  2350. /* Is this a valid kernel address? */
  2351. __notrace_funcgraph struct module *__module_text_address(unsigned long addr)
  2352. {
  2353. struct module *mod;
  2354. if (addr < module_addr_min || addr > module_addr_max)
  2355. return NULL;
  2356. list_for_each_entry_rcu(mod, &modules, list)
  2357. if (within(addr, mod->module_init, mod->init_text_size)
  2358. || within(addr, mod->module_core, mod->core_text_size))
  2359. return mod;
  2360. return NULL;
  2361. }
  2362. struct module *module_text_address(unsigned long addr)
  2363. {
  2364. struct module *mod;
  2365. preempt_disable();
  2366. mod = __module_text_address(addr);
  2367. preempt_enable();
  2368. return mod;
  2369. }
  2370. /* Don't grab lock, we're oopsing. */
  2371. void print_modules(void)
  2372. {
  2373. struct module *mod;
  2374. char buf[8];
  2375. printk("Modules linked in:");
  2376. /* Most callers should already have preempt disabled, but make sure */
  2377. preempt_disable();
  2378. list_for_each_entry_rcu(mod, &modules, list)
  2379. printk(" %s%s", mod->name, module_flags(mod, buf));
  2380. preempt_enable();
  2381. if (last_unloaded_module[0])
  2382. printk(" [last unloaded: %s]", last_unloaded_module);
  2383. printk("\n");
  2384. }
  2385. #ifdef CONFIG_MODVERSIONS
  2386. /* Generate the signature for struct module here, too, for modversions. */
  2387. void struct_module(struct module *mod) { return; }
  2388. EXPORT_SYMBOL(struct_module);
  2389. #endif
  2390. #ifdef CONFIG_MARKERS
  2391. void module_update_markers(void)
  2392. {
  2393. struct module *mod;
  2394. mutex_lock(&module_mutex);
  2395. list_for_each_entry(mod, &modules, list)
  2396. if (!mod->taints)
  2397. marker_update_probe_range(mod->markers,
  2398. mod->markers + mod->num_markers);
  2399. mutex_unlock(&module_mutex);
  2400. }
  2401. #endif
  2402. #ifdef CONFIG_TRACEPOINTS
  2403. void module_update_tracepoints(void)
  2404. {
  2405. struct module *mod;
  2406. mutex_lock(&module_mutex);
  2407. list_for_each_entry(mod, &modules, list)
  2408. if (!mod->taints)
  2409. tracepoint_update_probe_range(mod->tracepoints,
  2410. mod->tracepoints + mod->num_tracepoints);
  2411. mutex_unlock(&module_mutex);
  2412. }
  2413. /*
  2414. * Returns 0 if current not found.
  2415. * Returns 1 if current found.
  2416. */
  2417. int module_get_iter_tracepoints(struct tracepoint_iter *iter)
  2418. {
  2419. struct module *iter_mod;
  2420. int found = 0;
  2421. mutex_lock(&module_mutex);
  2422. list_for_each_entry(iter_mod, &modules, list) {
  2423. if (!iter_mod->taints) {
  2424. /*
  2425. * Sorted module list
  2426. */
  2427. if (iter_mod < iter->module)
  2428. continue;
  2429. else if (iter_mod > iter->module)
  2430. iter->tracepoint = NULL;
  2431. found = tracepoint_get_iter_range(&iter->tracepoint,
  2432. iter_mod->tracepoints,
  2433. iter_mod->tracepoints
  2434. + iter_mod->num_tracepoints);
  2435. if (found) {
  2436. iter->module = iter_mod;
  2437. break;
  2438. }
  2439. }
  2440. }
  2441. mutex_unlock(&module_mutex);
  2442. return found;
  2443. }
  2444. #endif