module.c 67 KB

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