module.c 63 KB

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