module.c 71 KB

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