module.c 73 KB

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