module.c 74 KB

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