module.c 76 KB

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