module.c 71 KB

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