module.c 63 KB

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