module.c 66 KB

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