module.c 59 KB

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