module.c 69 KB

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