module.c 73 KB

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