module.c 63 KB

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