module.c 68 KB

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