module.c 71 KB

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