module.c 72 KB

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