module.c 66 KB

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