module.c 74 KB

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