module.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823
  1. /*
  2. Copyright (C) 2002 Richard Henderson
  3. Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/module.h>
  17. #include <linux/moduleloader.h>
  18. #include <linux/init.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/fs.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/elf.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/capability.h>
  32. #include <linux/cpu.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/errno.h>
  35. #include <linux/err.h>
  36. #include <linux/vermagic.h>
  37. #include <linux/notifier.h>
  38. #include <linux/sched.h>
  39. #include <linux/stop_machine.h>
  40. #include <linux/device.h>
  41. #include <linux/string.h>
  42. #include <linux/mutex.h>
  43. #include <linux/unwind.h>
  44. #include <linux/rculist.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/cacheflush.h>
  47. #include <linux/license.h>
  48. #include <asm/sections.h>
  49. #include <linux/tracepoint.h>
  50. #include <linux/ftrace.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. if (mutex_lock_interruptible(&module_mutex) != 0)
  663. return -EINTR;
  664. mod = find_module(name);
  665. if (!mod) {
  666. ret = -ENOENT;
  667. goto out;
  668. }
  669. if (!list_empty(&mod->modules_which_use_me)) {
  670. /* Other modules depend on us: get rid of them first. */
  671. ret = -EWOULDBLOCK;
  672. goto out;
  673. }
  674. /* Doing init or already dying? */
  675. if (mod->state != MODULE_STATE_LIVE) {
  676. /* FIXME: if (force), slam module count and wake up
  677. waiter --RR */
  678. DEBUGP("%s already dying\n", mod->name);
  679. ret = -EBUSY;
  680. goto out;
  681. }
  682. /* If it has an init func, it must have an exit func to unload */
  683. if (mod->init && !mod->exit) {
  684. forced = try_force_unload(flags);
  685. if (!forced) {
  686. /* This module can't be removed */
  687. ret = -EBUSY;
  688. goto out;
  689. }
  690. }
  691. /* Set this up before setting mod->state */
  692. mod->waiter = current;
  693. /* Stop the machine so refcounts can't move and disable module. */
  694. ret = try_stop_module(mod, flags, &forced);
  695. if (ret != 0)
  696. goto out;
  697. /* Never wait if forced. */
  698. if (!forced && module_refcount(mod) != 0)
  699. wait_for_zero_refcount(mod);
  700. mutex_unlock(&module_mutex);
  701. /* Final destruction now noone is using it. */
  702. if (mod->exit != NULL)
  703. mod->exit();
  704. blocking_notifier_call_chain(&module_notify_list,
  705. MODULE_STATE_GOING, mod);
  706. mutex_lock(&module_mutex);
  707. /* Store the name of the last unloaded module for diagnostic purposes */
  708. strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
  709. unregister_dynamic_debug_module(mod->name);
  710. free_module(mod);
  711. out:
  712. mutex_unlock(&module_mutex);
  713. return ret;
  714. }
  715. static void print_unload_info(struct seq_file *m, struct module *mod)
  716. {
  717. struct module_use *use;
  718. int printed_something = 0;
  719. seq_printf(m, " %u ", module_refcount(mod));
  720. /* Always include a trailing , so userspace can differentiate
  721. between this and the old multi-field proc format. */
  722. list_for_each_entry(use, &mod->modules_which_use_me, list) {
  723. printed_something = 1;
  724. seq_printf(m, "%s,", use->module_which_uses->name);
  725. }
  726. if (mod->init != NULL && mod->exit == NULL) {
  727. printed_something = 1;
  728. seq_printf(m, "[permanent],");
  729. }
  730. if (!printed_something)
  731. seq_printf(m, "-");
  732. }
  733. void __symbol_put(const char *symbol)
  734. {
  735. struct module *owner;
  736. preempt_disable();
  737. if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
  738. BUG();
  739. module_put(owner);
  740. preempt_enable();
  741. }
  742. EXPORT_SYMBOL(__symbol_put);
  743. void symbol_put_addr(void *addr)
  744. {
  745. struct module *modaddr;
  746. if (core_kernel_text((unsigned long)addr))
  747. return;
  748. if (!(modaddr = module_text_address((unsigned long)addr)))
  749. BUG();
  750. module_put(modaddr);
  751. }
  752. EXPORT_SYMBOL_GPL(symbol_put_addr);
  753. static ssize_t show_refcnt(struct module_attribute *mattr,
  754. struct module *mod, char *buffer)
  755. {
  756. return sprintf(buffer, "%u\n", module_refcount(mod));
  757. }
  758. static struct module_attribute refcnt = {
  759. .attr = { .name = "refcnt", .mode = 0444 },
  760. .show = show_refcnt,
  761. };
  762. void module_put(struct module *module)
  763. {
  764. if (module) {
  765. unsigned int cpu = get_cpu();
  766. local_dec(&module->ref[cpu].count);
  767. /* Maybe they're waiting for us to drop reference? */
  768. if (unlikely(!module_is_live(module)))
  769. wake_up_process(module->waiter);
  770. put_cpu();
  771. }
  772. }
  773. EXPORT_SYMBOL(module_put);
  774. #else /* !CONFIG_MODULE_UNLOAD */
  775. static void print_unload_info(struct seq_file *m, struct module *mod)
  776. {
  777. /* We don't know the usage count, or what modules are using. */
  778. seq_printf(m, " - -");
  779. }
  780. static inline void module_unload_free(struct module *mod)
  781. {
  782. }
  783. static inline int use_module(struct module *a, struct module *b)
  784. {
  785. return strong_try_module_get(b) == 0;
  786. }
  787. static inline void module_unload_init(struct module *mod)
  788. {
  789. }
  790. #endif /* CONFIG_MODULE_UNLOAD */
  791. static ssize_t show_initstate(struct module_attribute *mattr,
  792. struct module *mod, char *buffer)
  793. {
  794. const char *state = "unknown";
  795. switch (mod->state) {
  796. case MODULE_STATE_LIVE:
  797. state = "live";
  798. break;
  799. case MODULE_STATE_COMING:
  800. state = "coming";
  801. break;
  802. case MODULE_STATE_GOING:
  803. state = "going";
  804. break;
  805. }
  806. return sprintf(buffer, "%s\n", state);
  807. }
  808. static struct module_attribute initstate = {
  809. .attr = { .name = "initstate", .mode = 0444 },
  810. .show = show_initstate,
  811. };
  812. static struct module_attribute *modinfo_attrs[] = {
  813. &modinfo_version,
  814. &modinfo_srcversion,
  815. &initstate,
  816. #ifdef CONFIG_MODULE_UNLOAD
  817. &refcnt,
  818. #endif
  819. NULL,
  820. };
  821. static const char vermagic[] = VERMAGIC_STRING;
  822. static int try_to_force_load(struct module *mod, const char *symname)
  823. {
  824. #ifdef CONFIG_MODULE_FORCE_LOAD
  825. if (!test_taint(TAINT_FORCED_MODULE))
  826. printk("%s: no version for \"%s\" found: kernel tainted.\n",
  827. mod->name, symname);
  828. add_taint_module(mod, TAINT_FORCED_MODULE);
  829. return 0;
  830. #else
  831. return -ENOEXEC;
  832. #endif
  833. }
  834. #ifdef CONFIG_MODVERSIONS
  835. static int check_version(Elf_Shdr *sechdrs,
  836. unsigned int versindex,
  837. const char *symname,
  838. struct module *mod,
  839. const unsigned long *crc)
  840. {
  841. unsigned int i, num_versions;
  842. struct modversion_info *versions;
  843. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  844. if (!crc)
  845. return 1;
  846. /* No versions at all? modprobe --force does this. */
  847. if (versindex == 0)
  848. return try_to_force_load(mod, symname) == 0;
  849. versions = (void *) sechdrs[versindex].sh_addr;
  850. num_versions = sechdrs[versindex].sh_size
  851. / sizeof(struct modversion_info);
  852. for (i = 0; i < num_versions; i++) {
  853. if (strcmp(versions[i].name, symname) != 0)
  854. continue;
  855. if (versions[i].crc == *crc)
  856. return 1;
  857. DEBUGP("Found checksum %lX vs module %lX\n",
  858. *crc, versions[i].crc);
  859. goto bad_version;
  860. }
  861. printk(KERN_WARNING "%s: no symbol version for %s\n",
  862. mod->name, symname);
  863. return 0;
  864. bad_version:
  865. printk("%s: disagrees about version of symbol %s\n",
  866. mod->name, symname);
  867. return 0;
  868. }
  869. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  870. unsigned int versindex,
  871. struct module *mod)
  872. {
  873. const unsigned long *crc;
  874. if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
  875. BUG();
  876. return check_version(sechdrs, versindex, "struct_module", mod, crc);
  877. }
  878. /* First part is kernel version, which we ignore if module has crcs. */
  879. static inline int same_magic(const char *amagic, const char *bmagic,
  880. bool has_crcs)
  881. {
  882. if (has_crcs) {
  883. amagic += strcspn(amagic, " ");
  884. bmagic += strcspn(bmagic, " ");
  885. }
  886. return strcmp(amagic, bmagic) == 0;
  887. }
  888. #else
  889. static inline int check_version(Elf_Shdr *sechdrs,
  890. unsigned int versindex,
  891. const char *symname,
  892. struct module *mod,
  893. const unsigned long *crc)
  894. {
  895. return 1;
  896. }
  897. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  898. unsigned int versindex,
  899. struct module *mod)
  900. {
  901. return 1;
  902. }
  903. static inline int same_magic(const char *amagic, const char *bmagic,
  904. bool has_crcs)
  905. {
  906. return strcmp(amagic, bmagic) == 0;
  907. }
  908. #endif /* CONFIG_MODVERSIONS */
  909. /* Resolve a symbol for this module. I.e. if we find one, record usage.
  910. Must be holding module_mutex. */
  911. static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
  912. unsigned int versindex,
  913. const char *name,
  914. struct module *mod)
  915. {
  916. struct module *owner;
  917. unsigned long ret;
  918. const unsigned long *crc;
  919. ret = find_symbol(name, &owner, &crc,
  920. !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
  921. if (!IS_ERR_VALUE(ret)) {
  922. /* use_module can fail due to OOM,
  923. or module initialization or unloading */
  924. if (!check_version(sechdrs, versindex, name, mod, crc) ||
  925. !use_module(mod, owner))
  926. ret = -EINVAL;
  927. }
  928. return ret;
  929. }
  930. /*
  931. * /sys/module/foo/sections stuff
  932. * J. Corbet <corbet@lwn.net>
  933. */
  934. #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
  935. struct module_sect_attr
  936. {
  937. struct module_attribute mattr;
  938. char *name;
  939. unsigned long address;
  940. };
  941. struct module_sect_attrs
  942. {
  943. struct attribute_group grp;
  944. unsigned int nsections;
  945. struct module_sect_attr attrs[0];
  946. };
  947. static ssize_t module_sect_show(struct module_attribute *mattr,
  948. struct module *mod, char *buf)
  949. {
  950. struct module_sect_attr *sattr =
  951. container_of(mattr, struct module_sect_attr, mattr);
  952. return sprintf(buf, "0x%lx\n", sattr->address);
  953. }
  954. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  955. {
  956. unsigned int section;
  957. for (section = 0; section < sect_attrs->nsections; section++)
  958. kfree(sect_attrs->attrs[section].name);
  959. kfree(sect_attrs);
  960. }
  961. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  962. char *secstrings, Elf_Shdr *sechdrs)
  963. {
  964. unsigned int nloaded = 0, i, size[2];
  965. struct module_sect_attrs *sect_attrs;
  966. struct module_sect_attr *sattr;
  967. struct attribute **gattr;
  968. /* Count loaded sections and allocate structures */
  969. for (i = 0; i < nsect; i++)
  970. if (sechdrs[i].sh_flags & SHF_ALLOC)
  971. nloaded++;
  972. size[0] = ALIGN(sizeof(*sect_attrs)
  973. + nloaded * sizeof(sect_attrs->attrs[0]),
  974. sizeof(sect_attrs->grp.attrs[0]));
  975. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  976. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  977. if (sect_attrs == NULL)
  978. return;
  979. /* Setup section attributes. */
  980. sect_attrs->grp.name = "sections";
  981. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  982. sect_attrs->nsections = 0;
  983. sattr = &sect_attrs->attrs[0];
  984. gattr = &sect_attrs->grp.attrs[0];
  985. for (i = 0; i < nsect; i++) {
  986. if (! (sechdrs[i].sh_flags & SHF_ALLOC))
  987. continue;
  988. sattr->address = sechdrs[i].sh_addr;
  989. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  990. GFP_KERNEL);
  991. if (sattr->name == NULL)
  992. goto out;
  993. sect_attrs->nsections++;
  994. sattr->mattr.show = module_sect_show;
  995. sattr->mattr.store = NULL;
  996. sattr->mattr.attr.name = sattr->name;
  997. sattr->mattr.attr.mode = S_IRUGO;
  998. *(gattr++) = &(sattr++)->mattr.attr;
  999. }
  1000. *gattr = NULL;
  1001. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  1002. goto out;
  1003. mod->sect_attrs = sect_attrs;
  1004. return;
  1005. out:
  1006. free_sect_attrs(sect_attrs);
  1007. }
  1008. static void remove_sect_attrs(struct module *mod)
  1009. {
  1010. if (mod->sect_attrs) {
  1011. sysfs_remove_group(&mod->mkobj.kobj,
  1012. &mod->sect_attrs->grp);
  1013. /* We are positive that no one is using any sect attrs
  1014. * at this point. Deallocate immediately. */
  1015. free_sect_attrs(mod->sect_attrs);
  1016. mod->sect_attrs = NULL;
  1017. }
  1018. }
  1019. /*
  1020. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  1021. */
  1022. struct module_notes_attrs {
  1023. struct kobject *dir;
  1024. unsigned int notes;
  1025. struct bin_attribute attrs[0];
  1026. };
  1027. static ssize_t module_notes_read(struct kobject *kobj,
  1028. struct bin_attribute *bin_attr,
  1029. char *buf, loff_t pos, size_t count)
  1030. {
  1031. /*
  1032. * The caller checked the pos and count against our size.
  1033. */
  1034. memcpy(buf, bin_attr->private + pos, count);
  1035. return count;
  1036. }
  1037. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  1038. unsigned int i)
  1039. {
  1040. if (notes_attrs->dir) {
  1041. while (i-- > 0)
  1042. sysfs_remove_bin_file(notes_attrs->dir,
  1043. &notes_attrs->attrs[i]);
  1044. kobject_put(notes_attrs->dir);
  1045. }
  1046. kfree(notes_attrs);
  1047. }
  1048. static void add_notes_attrs(struct module *mod, unsigned int nsect,
  1049. char *secstrings, Elf_Shdr *sechdrs)
  1050. {
  1051. unsigned int notes, loaded, i;
  1052. struct module_notes_attrs *notes_attrs;
  1053. struct bin_attribute *nattr;
  1054. /* Count notes sections and allocate structures. */
  1055. notes = 0;
  1056. for (i = 0; i < nsect; i++)
  1057. if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
  1058. (sechdrs[i].sh_type == SHT_NOTE))
  1059. ++notes;
  1060. if (notes == 0)
  1061. return;
  1062. notes_attrs = kzalloc(sizeof(*notes_attrs)
  1063. + notes * sizeof(notes_attrs->attrs[0]),
  1064. GFP_KERNEL);
  1065. if (notes_attrs == NULL)
  1066. return;
  1067. notes_attrs->notes = notes;
  1068. nattr = &notes_attrs->attrs[0];
  1069. for (loaded = i = 0; i < nsect; ++i) {
  1070. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1071. continue;
  1072. if (sechdrs[i].sh_type == SHT_NOTE) {
  1073. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  1074. nattr->attr.mode = S_IRUGO;
  1075. nattr->size = sechdrs[i].sh_size;
  1076. nattr->private = (void *) sechdrs[i].sh_addr;
  1077. nattr->read = module_notes_read;
  1078. ++nattr;
  1079. }
  1080. ++loaded;
  1081. }
  1082. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  1083. if (!notes_attrs->dir)
  1084. goto out;
  1085. for (i = 0; i < notes; ++i)
  1086. if (sysfs_create_bin_file(notes_attrs->dir,
  1087. &notes_attrs->attrs[i]))
  1088. goto out;
  1089. mod->notes_attrs = notes_attrs;
  1090. return;
  1091. out:
  1092. free_notes_attrs(notes_attrs, i);
  1093. }
  1094. static void remove_notes_attrs(struct module *mod)
  1095. {
  1096. if (mod->notes_attrs)
  1097. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1098. }
  1099. #else
  1100. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  1101. char *sectstrings, Elf_Shdr *sechdrs)
  1102. {
  1103. }
  1104. static inline void remove_sect_attrs(struct module *mod)
  1105. {
  1106. }
  1107. static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
  1108. char *sectstrings, Elf_Shdr *sechdrs)
  1109. {
  1110. }
  1111. static inline void remove_notes_attrs(struct module *mod)
  1112. {
  1113. }
  1114. #endif
  1115. #ifdef CONFIG_SYSFS
  1116. int module_add_modinfo_attrs(struct module *mod)
  1117. {
  1118. struct module_attribute *attr;
  1119. struct module_attribute *temp_attr;
  1120. int error = 0;
  1121. int i;
  1122. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1123. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1124. GFP_KERNEL);
  1125. if (!mod->modinfo_attrs)
  1126. return -ENOMEM;
  1127. temp_attr = mod->modinfo_attrs;
  1128. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1129. if (!attr->test ||
  1130. (attr->test && attr->test(mod))) {
  1131. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1132. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  1133. ++temp_attr;
  1134. }
  1135. }
  1136. return error;
  1137. }
  1138. void module_remove_modinfo_attrs(struct module *mod)
  1139. {
  1140. struct module_attribute *attr;
  1141. int i;
  1142. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1143. /* pick a field to test for end of list */
  1144. if (!attr->attr.name)
  1145. break;
  1146. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  1147. if (attr->free)
  1148. attr->free(mod);
  1149. }
  1150. kfree(mod->modinfo_attrs);
  1151. }
  1152. int mod_sysfs_init(struct module *mod)
  1153. {
  1154. int err;
  1155. struct kobject *kobj;
  1156. if (!module_sysfs_initialized) {
  1157. printk(KERN_ERR "%s: module sysfs not initialized\n",
  1158. mod->name);
  1159. err = -EINVAL;
  1160. goto out;
  1161. }
  1162. kobj = kset_find_obj(module_kset, mod->name);
  1163. if (kobj) {
  1164. printk(KERN_ERR "%s: module is already loaded\n", mod->name);
  1165. kobject_put(kobj);
  1166. err = -EINVAL;
  1167. goto out;
  1168. }
  1169. mod->mkobj.mod = mod;
  1170. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1171. mod->mkobj.kobj.kset = module_kset;
  1172. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1173. "%s", mod->name);
  1174. if (err)
  1175. kobject_put(&mod->mkobj.kobj);
  1176. /* delay uevent until full sysfs population */
  1177. out:
  1178. return err;
  1179. }
  1180. int mod_sysfs_setup(struct module *mod,
  1181. struct kernel_param *kparam,
  1182. unsigned int num_params)
  1183. {
  1184. int err;
  1185. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1186. if (!mod->holders_dir) {
  1187. err = -ENOMEM;
  1188. goto out_unreg;
  1189. }
  1190. err = module_param_sysfs_setup(mod, kparam, num_params);
  1191. if (err)
  1192. goto out_unreg_holders;
  1193. err = module_add_modinfo_attrs(mod);
  1194. if (err)
  1195. goto out_unreg_param;
  1196. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1197. return 0;
  1198. out_unreg_param:
  1199. module_param_sysfs_remove(mod);
  1200. out_unreg_holders:
  1201. kobject_put(mod->holders_dir);
  1202. out_unreg:
  1203. kobject_put(&mod->mkobj.kobj);
  1204. return err;
  1205. }
  1206. static void mod_sysfs_fini(struct module *mod)
  1207. {
  1208. kobject_put(&mod->mkobj.kobj);
  1209. }
  1210. #else /* CONFIG_SYSFS */
  1211. static void mod_sysfs_fini(struct module *mod)
  1212. {
  1213. }
  1214. #endif /* CONFIG_SYSFS */
  1215. static void mod_kobject_remove(struct module *mod)
  1216. {
  1217. module_remove_modinfo_attrs(mod);
  1218. module_param_sysfs_remove(mod);
  1219. kobject_put(mod->mkobj.drivers_dir);
  1220. kobject_put(mod->holders_dir);
  1221. mod_sysfs_fini(mod);
  1222. }
  1223. /*
  1224. * unlink the module with the whole machine is stopped with interrupts off
  1225. * - this defends against kallsyms not taking locks
  1226. */
  1227. static int __unlink_module(void *_mod)
  1228. {
  1229. struct module *mod = _mod;
  1230. list_del(&mod->list);
  1231. return 0;
  1232. }
  1233. /* Free a module, remove from lists, etc (must hold module_mutex). */
  1234. static void free_module(struct module *mod)
  1235. {
  1236. /* Delete from various lists */
  1237. stop_machine(__unlink_module, mod, NULL);
  1238. remove_notes_attrs(mod);
  1239. remove_sect_attrs(mod);
  1240. mod_kobject_remove(mod);
  1241. unwind_remove_table(mod->unwind_info, 0);
  1242. /* Arch-specific cleanup. */
  1243. module_arch_cleanup(mod);
  1244. /* Module unload stuff */
  1245. module_unload_free(mod);
  1246. /* release any pointers to mcount in this module */
  1247. ftrace_release(mod->module_core, mod->core_size);
  1248. /* This may be NULL, but that's OK */
  1249. module_free(mod, mod->module_init);
  1250. kfree(mod->args);
  1251. if (mod->percpu)
  1252. percpu_modfree(mod->percpu);
  1253. /* Free lock-classes: */
  1254. lockdep_free_key_range(mod->module_core, mod->core_size);
  1255. /* Finally, free the core (containing the module structure) */
  1256. module_free(mod, mod->module_core);
  1257. }
  1258. void *__symbol_get(const char *symbol)
  1259. {
  1260. struct module *owner;
  1261. unsigned long value;
  1262. preempt_disable();
  1263. value = find_symbol(symbol, &owner, NULL, true, true);
  1264. if (IS_ERR_VALUE(value))
  1265. value = 0;
  1266. else if (strong_try_module_get(owner))
  1267. value = 0;
  1268. preempt_enable();
  1269. return (void *)value;
  1270. }
  1271. EXPORT_SYMBOL_GPL(__symbol_get);
  1272. /*
  1273. * Ensure that an exported symbol [global namespace] does not already exist
  1274. * in the kernel or in some other module's exported symbol table.
  1275. */
  1276. static int verify_export_symbols(struct module *mod)
  1277. {
  1278. unsigned int i;
  1279. struct module *owner;
  1280. const struct kernel_symbol *s;
  1281. struct {
  1282. const struct kernel_symbol *sym;
  1283. unsigned int num;
  1284. } arr[] = {
  1285. { mod->syms, mod->num_syms },
  1286. { mod->gpl_syms, mod->num_gpl_syms },
  1287. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1288. #ifdef CONFIG_UNUSED_SYMBOLS
  1289. { mod->unused_syms, mod->num_unused_syms },
  1290. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1291. #endif
  1292. };
  1293. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1294. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1295. if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
  1296. NULL, true, false))) {
  1297. printk(KERN_ERR
  1298. "%s: exports duplicate symbol %s"
  1299. " (owned by %s)\n",
  1300. mod->name, s->name, module_name(owner));
  1301. return -ENOEXEC;
  1302. }
  1303. }
  1304. }
  1305. return 0;
  1306. }
  1307. /* Change all symbols so that st_value encodes the pointer directly. */
  1308. static int simplify_symbols(Elf_Shdr *sechdrs,
  1309. unsigned int symindex,
  1310. const char *strtab,
  1311. unsigned int versindex,
  1312. unsigned int pcpuindex,
  1313. struct module *mod)
  1314. {
  1315. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1316. unsigned long secbase;
  1317. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1318. int ret = 0;
  1319. for (i = 1; i < n; i++) {
  1320. switch (sym[i].st_shndx) {
  1321. case SHN_COMMON:
  1322. /* We compiled with -fno-common. These are not
  1323. supposed to happen. */
  1324. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1325. printk("%s: please compile with -fno-common\n",
  1326. mod->name);
  1327. ret = -ENOEXEC;
  1328. break;
  1329. case SHN_ABS:
  1330. /* Don't need to do anything */
  1331. DEBUGP("Absolute symbol: 0x%08lx\n",
  1332. (long)sym[i].st_value);
  1333. break;
  1334. case SHN_UNDEF:
  1335. sym[i].st_value
  1336. = resolve_symbol(sechdrs, versindex,
  1337. strtab + sym[i].st_name, mod);
  1338. /* Ok if resolved. */
  1339. if (!IS_ERR_VALUE(sym[i].st_value))
  1340. break;
  1341. /* Ok if weak. */
  1342. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1343. break;
  1344. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1345. mod->name, strtab + sym[i].st_name);
  1346. ret = -ENOENT;
  1347. break;
  1348. default:
  1349. /* Divert to percpu allocation if a percpu var. */
  1350. if (sym[i].st_shndx == pcpuindex)
  1351. secbase = (unsigned long)mod->percpu;
  1352. else
  1353. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1354. sym[i].st_value += secbase;
  1355. break;
  1356. }
  1357. }
  1358. return ret;
  1359. }
  1360. /* Update size with this section: return offset. */
  1361. static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
  1362. {
  1363. long ret;
  1364. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1365. *size = ret + sechdr->sh_size;
  1366. return ret;
  1367. }
  1368. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1369. might -- code, read-only data, read-write data, small data. Tally
  1370. sizes, and place the offsets into sh_entsize fields: high bit means it
  1371. belongs in init. */
  1372. static void layout_sections(struct module *mod,
  1373. const Elf_Ehdr *hdr,
  1374. Elf_Shdr *sechdrs,
  1375. const char *secstrings)
  1376. {
  1377. static unsigned long const masks[][2] = {
  1378. /* NOTE: all executable code must be the first section
  1379. * in this array; otherwise modify the text_size
  1380. * finder in the two loops below */
  1381. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1382. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1383. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1384. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1385. };
  1386. unsigned int m, i;
  1387. for (i = 0; i < hdr->e_shnum; i++)
  1388. sechdrs[i].sh_entsize = ~0UL;
  1389. DEBUGP("Core section allocation order:\n");
  1390. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1391. for (i = 0; i < hdr->e_shnum; ++i) {
  1392. Elf_Shdr *s = &sechdrs[i];
  1393. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1394. || (s->sh_flags & masks[m][1])
  1395. || s->sh_entsize != ~0UL
  1396. || strncmp(secstrings + s->sh_name,
  1397. ".init", 5) == 0)
  1398. continue;
  1399. s->sh_entsize = get_offset(&mod->core_size, s);
  1400. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1401. }
  1402. if (m == 0)
  1403. mod->core_text_size = mod->core_size;
  1404. }
  1405. DEBUGP("Init section allocation order:\n");
  1406. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1407. for (i = 0; i < hdr->e_shnum; ++i) {
  1408. Elf_Shdr *s = &sechdrs[i];
  1409. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1410. || (s->sh_flags & masks[m][1])
  1411. || s->sh_entsize != ~0UL
  1412. || strncmp(secstrings + s->sh_name,
  1413. ".init", 5) != 0)
  1414. continue;
  1415. s->sh_entsize = (get_offset(&mod->init_size, s)
  1416. | INIT_OFFSET_MASK);
  1417. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1418. }
  1419. if (m == 0)
  1420. mod->init_text_size = mod->init_size;
  1421. }
  1422. }
  1423. static void set_license(struct module *mod, const char *license)
  1424. {
  1425. if (!license)
  1426. license = "unspecified";
  1427. if (!license_is_gpl_compatible(license)) {
  1428. if (!test_taint(TAINT_PROPRIETARY_MODULE))
  1429. printk(KERN_WARNING "%s: module license '%s' taints "
  1430. "kernel.\n", mod->name, license);
  1431. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1432. }
  1433. }
  1434. /* Parse tag=value strings from .modinfo section */
  1435. static char *next_string(char *string, unsigned long *secsize)
  1436. {
  1437. /* Skip non-zero chars */
  1438. while (string[0]) {
  1439. string++;
  1440. if ((*secsize)-- <= 1)
  1441. return NULL;
  1442. }
  1443. /* Skip any zero padding. */
  1444. while (!string[0]) {
  1445. string++;
  1446. if ((*secsize)-- <= 1)
  1447. return NULL;
  1448. }
  1449. return string;
  1450. }
  1451. static char *get_modinfo(Elf_Shdr *sechdrs,
  1452. unsigned int info,
  1453. const char *tag)
  1454. {
  1455. char *p;
  1456. unsigned int taglen = strlen(tag);
  1457. unsigned long size = sechdrs[info].sh_size;
  1458. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1459. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1460. return p + taglen + 1;
  1461. }
  1462. return NULL;
  1463. }
  1464. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1465. unsigned int infoindex)
  1466. {
  1467. struct module_attribute *attr;
  1468. int i;
  1469. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1470. if (attr->setup)
  1471. attr->setup(mod,
  1472. get_modinfo(sechdrs,
  1473. infoindex,
  1474. attr->attr.name));
  1475. }
  1476. }
  1477. #ifdef CONFIG_KALLSYMS
  1478. /* lookup symbol in given range of kernel_symbols */
  1479. static const struct kernel_symbol *lookup_symbol(const char *name,
  1480. const struct kernel_symbol *start,
  1481. const struct kernel_symbol *stop)
  1482. {
  1483. const struct kernel_symbol *ks = start;
  1484. for (; ks < stop; ks++)
  1485. if (strcmp(ks->name, name) == 0)
  1486. return ks;
  1487. return NULL;
  1488. }
  1489. static int is_exported(const char *name, const struct module *mod)
  1490. {
  1491. if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
  1492. return 1;
  1493. else
  1494. if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
  1495. return 1;
  1496. else
  1497. return 0;
  1498. }
  1499. /* As per nm */
  1500. static char elf_type(const Elf_Sym *sym,
  1501. Elf_Shdr *sechdrs,
  1502. const char *secstrings,
  1503. struct module *mod)
  1504. {
  1505. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1506. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1507. return 'v';
  1508. else
  1509. return 'w';
  1510. }
  1511. if (sym->st_shndx == SHN_UNDEF)
  1512. return 'U';
  1513. if (sym->st_shndx == SHN_ABS)
  1514. return 'a';
  1515. if (sym->st_shndx >= SHN_LORESERVE)
  1516. return '?';
  1517. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1518. return 't';
  1519. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1520. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1521. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1522. return 'r';
  1523. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1524. return 'g';
  1525. else
  1526. return 'd';
  1527. }
  1528. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1529. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1530. return 's';
  1531. else
  1532. return 'b';
  1533. }
  1534. if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
  1535. ".debug", strlen(".debug")) == 0)
  1536. return 'n';
  1537. return '?';
  1538. }
  1539. static void add_kallsyms(struct module *mod,
  1540. Elf_Shdr *sechdrs,
  1541. unsigned int symindex,
  1542. unsigned int strindex,
  1543. const char *secstrings)
  1544. {
  1545. unsigned int i;
  1546. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1547. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1548. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1549. /* Set types up while we still have access to sections. */
  1550. for (i = 0; i < mod->num_symtab; i++)
  1551. mod->symtab[i].st_info
  1552. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1553. }
  1554. #else
  1555. static inline void add_kallsyms(struct module *mod,
  1556. Elf_Shdr *sechdrs,
  1557. unsigned int symindex,
  1558. unsigned int strindex,
  1559. const char *secstrings)
  1560. {
  1561. }
  1562. #endif /* CONFIG_KALLSYMS */
  1563. static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
  1564. {
  1565. #ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
  1566. unsigned int i;
  1567. for (i = 0; i < num; i++) {
  1568. register_dynamic_debug_module(debug[i].modname,
  1569. debug[i].type,
  1570. debug[i].logical_modname,
  1571. debug[i].flag_names,
  1572. debug[i].hash, debug[i].hash2);
  1573. }
  1574. #endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
  1575. }
  1576. static void *module_alloc_update_bounds(unsigned long size)
  1577. {
  1578. void *ret = module_alloc(size);
  1579. if (ret) {
  1580. /* Update module bounds. */
  1581. if ((unsigned long)ret < module_addr_min)
  1582. module_addr_min = (unsigned long)ret;
  1583. if ((unsigned long)ret + size > module_addr_max)
  1584. module_addr_max = (unsigned long)ret + size;
  1585. }
  1586. return ret;
  1587. }
  1588. /* Allocate and load the module: note that size of section 0 is always
  1589. zero, and we rely on this for optional sections. */
  1590. static noinline struct module *load_module(void __user *umod,
  1591. unsigned long len,
  1592. const char __user *uargs)
  1593. {
  1594. Elf_Ehdr *hdr;
  1595. Elf_Shdr *sechdrs;
  1596. char *secstrings, *args, *modmagic, *strtab = NULL;
  1597. char *staging;
  1598. unsigned int i;
  1599. unsigned int symindex = 0;
  1600. unsigned int strindex = 0;
  1601. unsigned int modindex, versindex, infoindex, pcpuindex;
  1602. unsigned int unwindex = 0;
  1603. unsigned int num_kp, num_mcount;
  1604. struct kernel_param *kp;
  1605. struct module *mod;
  1606. long err = 0;
  1607. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1608. unsigned long *mseg;
  1609. mm_segment_t old_fs;
  1610. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1611. umod, len, uargs);
  1612. if (len < sizeof(*hdr))
  1613. return ERR_PTR(-ENOEXEC);
  1614. /* Suck in entire file: we'll want most of it. */
  1615. /* vmalloc barfs on "unusual" numbers. Check here */
  1616. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1617. return ERR_PTR(-ENOMEM);
  1618. if (copy_from_user(hdr, umod, len) != 0) {
  1619. err = -EFAULT;
  1620. goto free_hdr;
  1621. }
  1622. /* Sanity checks against insmoding binaries or wrong arch,
  1623. weird elf version */
  1624. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
  1625. || hdr->e_type != ET_REL
  1626. || !elf_check_arch(hdr)
  1627. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1628. err = -ENOEXEC;
  1629. goto free_hdr;
  1630. }
  1631. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1632. goto truncated;
  1633. /* Convenience variables */
  1634. sechdrs = (void *)hdr + hdr->e_shoff;
  1635. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1636. sechdrs[0].sh_addr = 0;
  1637. for (i = 1; i < hdr->e_shnum; i++) {
  1638. if (sechdrs[i].sh_type != SHT_NOBITS
  1639. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1640. goto truncated;
  1641. /* Mark all sections sh_addr with their address in the
  1642. temporary image. */
  1643. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1644. /* Internal symbols and strings. */
  1645. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1646. symindex = i;
  1647. strindex = sechdrs[i].sh_link;
  1648. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1649. }
  1650. #ifndef CONFIG_MODULE_UNLOAD
  1651. /* Don't load .exit sections */
  1652. if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
  1653. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1654. #endif
  1655. }
  1656. modindex = find_sec(hdr, sechdrs, secstrings,
  1657. ".gnu.linkonce.this_module");
  1658. if (!modindex) {
  1659. printk(KERN_WARNING "No module found in object\n");
  1660. err = -ENOEXEC;
  1661. goto free_hdr;
  1662. }
  1663. /* This is temporary: point mod into copy of data. */
  1664. mod = (void *)sechdrs[modindex].sh_addr;
  1665. if (symindex == 0) {
  1666. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1667. mod->name);
  1668. err = -ENOEXEC;
  1669. goto free_hdr;
  1670. }
  1671. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1672. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1673. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1674. #ifdef ARCH_UNWIND_SECTION_NAME
  1675. unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
  1676. #endif
  1677. /* Don't keep modinfo and version sections. */
  1678. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1679. sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1680. #ifdef CONFIG_KALLSYMS
  1681. /* Keep symbol and string tables for decoding later. */
  1682. sechdrs[symindex].sh_flags |= SHF_ALLOC;
  1683. sechdrs[strindex].sh_flags |= SHF_ALLOC;
  1684. #endif
  1685. if (unwindex)
  1686. sechdrs[unwindex].sh_flags |= SHF_ALLOC;
  1687. /* Check module struct version now, before we try to use module. */
  1688. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1689. err = -ENOEXEC;
  1690. goto free_hdr;
  1691. }
  1692. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1693. /* This is allowed: modprobe --force will invalidate it. */
  1694. if (!modmagic) {
  1695. err = try_to_force_load(mod, "magic");
  1696. if (err)
  1697. goto free_hdr;
  1698. } else if (!same_magic(modmagic, vermagic, versindex)) {
  1699. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1700. mod->name, modmagic, vermagic);
  1701. err = -ENOEXEC;
  1702. goto free_hdr;
  1703. }
  1704. staging = get_modinfo(sechdrs, infoindex, "staging");
  1705. if (staging) {
  1706. add_taint_module(mod, TAINT_CRAP);
  1707. printk(KERN_WARNING "%s: module is from the staging directory,"
  1708. " the quality is unknown, you have been warned.\n",
  1709. mod->name);
  1710. }
  1711. /* Now copy in args */
  1712. args = strndup_user(uargs, ~0UL >> 1);
  1713. if (IS_ERR(args)) {
  1714. err = PTR_ERR(args);
  1715. goto free_hdr;
  1716. }
  1717. if (find_module(mod->name)) {
  1718. err = -EEXIST;
  1719. goto free_mod;
  1720. }
  1721. mod->state = MODULE_STATE_COMING;
  1722. /* Allow arches to frob section contents and sizes. */
  1723. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1724. if (err < 0)
  1725. goto free_mod;
  1726. if (pcpuindex) {
  1727. /* We have a special allocation for this section. */
  1728. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1729. sechdrs[pcpuindex].sh_addralign,
  1730. mod->name);
  1731. if (!percpu) {
  1732. err = -ENOMEM;
  1733. goto free_mod;
  1734. }
  1735. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1736. mod->percpu = percpu;
  1737. }
  1738. /* Determine total sizes, and put offsets in sh_entsize. For now
  1739. this is done generically; there doesn't appear to be any
  1740. special cases for the architectures. */
  1741. layout_sections(mod, hdr, sechdrs, secstrings);
  1742. /* Do the allocs. */
  1743. ptr = module_alloc_update_bounds(mod->core_size);
  1744. if (!ptr) {
  1745. err = -ENOMEM;
  1746. goto free_percpu;
  1747. }
  1748. memset(ptr, 0, mod->core_size);
  1749. mod->module_core = ptr;
  1750. ptr = module_alloc_update_bounds(mod->init_size);
  1751. if (!ptr && mod->init_size) {
  1752. err = -ENOMEM;
  1753. goto free_core;
  1754. }
  1755. memset(ptr, 0, mod->init_size);
  1756. mod->module_init = ptr;
  1757. /* Transfer each section which specifies SHF_ALLOC */
  1758. DEBUGP("final section addresses:\n");
  1759. for (i = 0; i < hdr->e_shnum; i++) {
  1760. void *dest;
  1761. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1762. continue;
  1763. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1764. dest = mod->module_init
  1765. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1766. else
  1767. dest = mod->module_core + sechdrs[i].sh_entsize;
  1768. if (sechdrs[i].sh_type != SHT_NOBITS)
  1769. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1770. sechdrs[i].sh_size);
  1771. /* Update sh_addr to point to copy in image. */
  1772. sechdrs[i].sh_addr = (unsigned long)dest;
  1773. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1774. }
  1775. /* Module has been moved. */
  1776. mod = (void *)sechdrs[modindex].sh_addr;
  1777. /* Now we've moved module, initialize linked lists, etc. */
  1778. module_unload_init(mod);
  1779. /* add kobject, so we can reference it. */
  1780. err = mod_sysfs_init(mod);
  1781. if (err)
  1782. goto free_unload;
  1783. /* Set up license info based on the info section */
  1784. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1785. /*
  1786. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  1787. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  1788. * using GPL-only symbols it needs.
  1789. */
  1790. if (strcmp(mod->name, "ndiswrapper") == 0)
  1791. add_taint(TAINT_PROPRIETARY_MODULE);
  1792. /* driverloader was caught wrongly pretending to be under GPL */
  1793. if (strcmp(mod->name, "driverloader") == 0)
  1794. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1795. /* Set up MODINFO_ATTR fields */
  1796. setup_modinfo(mod, sechdrs, infoindex);
  1797. /* Fix up syms, so that st_value is a pointer to location. */
  1798. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1799. mod);
  1800. if (err < 0)
  1801. goto cleanup;
  1802. /* Now we've got everything in the final locations, we can
  1803. * find optional sections. */
  1804. kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
  1805. &num_kp);
  1806. mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
  1807. sizeof(*mod->syms), &mod->num_syms);
  1808. mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
  1809. mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
  1810. sizeof(*mod->gpl_syms),
  1811. &mod->num_gpl_syms);
  1812. mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1813. mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
  1814. "__ksymtab_gpl_future",
  1815. sizeof(*mod->gpl_future_syms),
  1816. &mod->num_gpl_future_syms);
  1817. mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
  1818. "__kcrctab_gpl_future");
  1819. #ifdef CONFIG_UNUSED_SYMBOLS
  1820. mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
  1821. "__ksymtab_unused",
  1822. sizeof(*mod->unused_syms),
  1823. &mod->num_unused_syms);
  1824. mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
  1825. "__kcrctab_unused");
  1826. mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
  1827. "__ksymtab_unused_gpl",
  1828. sizeof(*mod->unused_gpl_syms),
  1829. &mod->num_unused_gpl_syms);
  1830. mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
  1831. "__kcrctab_unused_gpl");
  1832. #endif
  1833. #ifdef CONFIG_MARKERS
  1834. mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
  1835. sizeof(*mod->markers), &mod->num_markers);
  1836. #endif
  1837. #ifdef CONFIG_TRACEPOINTS
  1838. mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
  1839. "__tracepoints",
  1840. sizeof(*mod->tracepoints),
  1841. &mod->num_tracepoints);
  1842. #endif
  1843. #ifdef CONFIG_MODVERSIONS
  1844. if ((mod->num_syms && !mod->crcs)
  1845. || (mod->num_gpl_syms && !mod->gpl_crcs)
  1846. || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
  1847. #ifdef CONFIG_UNUSED_SYMBOLS
  1848. || (mod->num_unused_syms && !mod->unused_crcs)
  1849. || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
  1850. #endif
  1851. ) {
  1852. printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
  1853. err = try_to_force_load(mod, "nocrc");
  1854. if (err)
  1855. goto cleanup;
  1856. }
  1857. #endif
  1858. /* Now do relocations. */
  1859. for (i = 1; i < hdr->e_shnum; i++) {
  1860. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1861. unsigned int info = sechdrs[i].sh_info;
  1862. /* Not a valid relocation section? */
  1863. if (info >= hdr->e_shnum)
  1864. continue;
  1865. /* Don't bother with non-allocated sections */
  1866. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1867. continue;
  1868. if (sechdrs[i].sh_type == SHT_REL)
  1869. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1870. else if (sechdrs[i].sh_type == SHT_RELA)
  1871. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1872. mod);
  1873. if (err < 0)
  1874. goto cleanup;
  1875. }
  1876. /* Find duplicate symbols */
  1877. err = verify_export_symbols(mod);
  1878. if (err < 0)
  1879. goto cleanup;
  1880. /* Set up and sort exception table */
  1881. mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
  1882. sizeof(*mod->extable), &mod->num_exentries);
  1883. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  1884. /* Finally, copy percpu area over. */
  1885. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1886. sechdrs[pcpuindex].sh_size);
  1887. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1888. if (!mod->taints) {
  1889. struct mod_debug *debug;
  1890. unsigned int num_debug;
  1891. #ifdef CONFIG_MARKERS
  1892. marker_update_probe_range(mod->markers,
  1893. mod->markers + mod->num_markers);
  1894. #endif
  1895. debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
  1896. sizeof(*debug), &num_debug);
  1897. dynamic_printk_setup(debug, num_debug);
  1898. #ifdef CONFIG_TRACEPOINTS
  1899. tracepoint_update_probe_range(mod->tracepoints,
  1900. mod->tracepoints + mod->num_tracepoints);
  1901. #endif
  1902. }
  1903. /* sechdrs[0].sh_size is always zero */
  1904. mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
  1905. sizeof(*mseg), &num_mcount);
  1906. ftrace_init_module(mseg, mseg + num_mcount);
  1907. err = module_finalize(hdr, sechdrs, mod);
  1908. if (err < 0)
  1909. goto cleanup;
  1910. /* flush the icache in correct context */
  1911. old_fs = get_fs();
  1912. set_fs(KERNEL_DS);
  1913. /*
  1914. * Flush the instruction cache, since we've played with text.
  1915. * Do it before processing of module parameters, so the module
  1916. * can provide parameter accessor functions of its own.
  1917. */
  1918. if (mod->module_init)
  1919. flush_icache_range((unsigned long)mod->module_init,
  1920. (unsigned long)mod->module_init
  1921. + mod->init_size);
  1922. flush_icache_range((unsigned long)mod->module_core,
  1923. (unsigned long)mod->module_core + mod->core_size);
  1924. set_fs(old_fs);
  1925. mod->args = args;
  1926. if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
  1927. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1928. mod->name);
  1929. /* Now sew it into the lists so we can get lockdep and oops
  1930. * info during argument parsing. Noone should access us, since
  1931. * strong_try_module_get() will fail.
  1932. * lockdep/oops can run asynchronous, so use the RCU list insertion
  1933. * function to insert in a way safe to concurrent readers.
  1934. * The mutex protects against concurrent writers.
  1935. */
  1936. list_add_rcu(&mod->list, &modules);
  1937. err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
  1938. if (err < 0)
  1939. goto unlink;
  1940. err = mod_sysfs_setup(mod, kp, num_kp);
  1941. if (err < 0)
  1942. goto unlink;
  1943. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1944. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1945. /* Size of section 0 is 0, so this works well if no unwind info. */
  1946. mod->unwind_info = unwind_add_table(mod,
  1947. (void *)sechdrs[unwindex].sh_addr,
  1948. sechdrs[unwindex].sh_size);
  1949. /* Get rid of temporary copy */
  1950. vfree(hdr);
  1951. /* Done! */
  1952. return mod;
  1953. unlink:
  1954. stop_machine(__unlink_module, mod, NULL);
  1955. module_arch_cleanup(mod);
  1956. cleanup:
  1957. kobject_del(&mod->mkobj.kobj);
  1958. kobject_put(&mod->mkobj.kobj);
  1959. ftrace_release(mod->module_core, mod->core_size);
  1960. free_unload:
  1961. module_unload_free(mod);
  1962. module_free(mod, mod->module_init);
  1963. free_core:
  1964. module_free(mod, mod->module_core);
  1965. free_percpu:
  1966. if (percpu)
  1967. percpu_modfree(percpu);
  1968. free_mod:
  1969. kfree(args);
  1970. free_hdr:
  1971. vfree(hdr);
  1972. return ERR_PTR(err);
  1973. truncated:
  1974. printk(KERN_ERR "Module len %lu truncated\n", len);
  1975. err = -ENOEXEC;
  1976. goto free_hdr;
  1977. }
  1978. /* This is where the real work happens */
  1979. asmlinkage long
  1980. sys_init_module(void __user *umod,
  1981. unsigned long len,
  1982. const char __user *uargs)
  1983. {
  1984. struct module *mod;
  1985. int ret = 0;
  1986. /* Must have permission */
  1987. if (!capable(CAP_SYS_MODULE))
  1988. return -EPERM;
  1989. /* Only one module load at a time, please */
  1990. if (mutex_lock_interruptible(&module_mutex) != 0)
  1991. return -EINTR;
  1992. /* Do all the hard work */
  1993. mod = load_module(umod, len, uargs);
  1994. if (IS_ERR(mod)) {
  1995. mutex_unlock(&module_mutex);
  1996. return PTR_ERR(mod);
  1997. }
  1998. /* Drop lock so they can recurse */
  1999. mutex_unlock(&module_mutex);
  2000. blocking_notifier_call_chain(&module_notify_list,
  2001. MODULE_STATE_COMING, mod);
  2002. /* Start the module */
  2003. if (mod->init != NULL)
  2004. ret = do_one_initcall(mod->init);
  2005. if (ret < 0) {
  2006. /* Init routine failed: abort. Try to protect us from
  2007. buggy refcounters. */
  2008. mod->state = MODULE_STATE_GOING;
  2009. synchronize_sched();
  2010. module_put(mod);
  2011. blocking_notifier_call_chain(&module_notify_list,
  2012. MODULE_STATE_GOING, mod);
  2013. mutex_lock(&module_mutex);
  2014. free_module(mod);
  2015. mutex_unlock(&module_mutex);
  2016. wake_up(&module_wq);
  2017. return ret;
  2018. }
  2019. if (ret > 0) {
  2020. printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
  2021. "it should follow 0/-E convention\n"
  2022. KERN_WARNING "%s: loading module anyway...\n",
  2023. __func__, mod->name, ret,
  2024. __func__);
  2025. dump_stack();
  2026. }
  2027. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  2028. mod->state = MODULE_STATE_LIVE;
  2029. wake_up(&module_wq);
  2030. mutex_lock(&module_mutex);
  2031. /* Drop initial reference. */
  2032. module_put(mod);
  2033. unwind_remove_table(mod->unwind_info, 1);
  2034. module_free(mod, mod->module_init);
  2035. mod->module_init = NULL;
  2036. mod->init_size = 0;
  2037. mod->init_text_size = 0;
  2038. mutex_unlock(&module_mutex);
  2039. return 0;
  2040. }
  2041. static inline int within(unsigned long addr, void *start, unsigned long size)
  2042. {
  2043. return ((void *)addr >= start && (void *)addr < start + size);
  2044. }
  2045. #ifdef CONFIG_KALLSYMS
  2046. /*
  2047. * This ignores the intensely annoying "mapping symbols" found
  2048. * in ARM ELF files: $a, $t and $d.
  2049. */
  2050. static inline int is_arm_mapping_symbol(const char *str)
  2051. {
  2052. return str[0] == '$' && strchr("atd", str[1])
  2053. && (str[2] == '\0' || str[2] == '.');
  2054. }
  2055. static const char *get_ksymbol(struct module *mod,
  2056. unsigned long addr,
  2057. unsigned long *size,
  2058. unsigned long *offset)
  2059. {
  2060. unsigned int i, best = 0;
  2061. unsigned long nextval;
  2062. /* At worse, next value is at end of module */
  2063. if (within(addr, mod->module_init, mod->init_size))
  2064. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  2065. else
  2066. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  2067. /* Scan for closest preceeding symbol, and next symbol. (ELF
  2068. starts real symbols at 1). */
  2069. for (i = 1; i < mod->num_symtab; i++) {
  2070. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  2071. continue;
  2072. /* We ignore unnamed symbols: they're uninformative
  2073. * and inserted at a whim. */
  2074. if (mod->symtab[i].st_value <= addr
  2075. && mod->symtab[i].st_value > mod->symtab[best].st_value
  2076. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2077. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2078. best = i;
  2079. if (mod->symtab[i].st_value > addr
  2080. && mod->symtab[i].st_value < nextval
  2081. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2082. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2083. nextval = mod->symtab[i].st_value;
  2084. }
  2085. if (!best)
  2086. return NULL;
  2087. if (size)
  2088. *size = nextval - mod->symtab[best].st_value;
  2089. if (offset)
  2090. *offset = addr - mod->symtab[best].st_value;
  2091. return mod->strtab + mod->symtab[best].st_name;
  2092. }
  2093. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  2094. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  2095. const char *module_address_lookup(unsigned long addr,
  2096. unsigned long *size,
  2097. unsigned long *offset,
  2098. char **modname,
  2099. char *namebuf)
  2100. {
  2101. struct module *mod;
  2102. const char *ret = NULL;
  2103. preempt_disable();
  2104. list_for_each_entry_rcu(mod, &modules, list) {
  2105. if (within(addr, mod->module_init, mod->init_size)
  2106. || within(addr, mod->module_core, mod->core_size)) {
  2107. if (modname)
  2108. *modname = mod->name;
  2109. ret = get_ksymbol(mod, addr, size, offset);
  2110. break;
  2111. }
  2112. }
  2113. /* Make a copy in here where it's safe */
  2114. if (ret) {
  2115. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2116. ret = namebuf;
  2117. }
  2118. preempt_enable();
  2119. return ret;
  2120. }
  2121. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2122. {
  2123. struct module *mod;
  2124. preempt_disable();
  2125. list_for_each_entry_rcu(mod, &modules, list) {
  2126. if (within(addr, mod->module_init, mod->init_size) ||
  2127. within(addr, mod->module_core, mod->core_size)) {
  2128. const char *sym;
  2129. sym = get_ksymbol(mod, addr, NULL, NULL);
  2130. if (!sym)
  2131. goto out;
  2132. strlcpy(symname, sym, KSYM_NAME_LEN);
  2133. preempt_enable();
  2134. return 0;
  2135. }
  2136. }
  2137. out:
  2138. preempt_enable();
  2139. return -ERANGE;
  2140. }
  2141. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2142. unsigned long *offset, char *modname, char *name)
  2143. {
  2144. struct module *mod;
  2145. preempt_disable();
  2146. list_for_each_entry_rcu(mod, &modules, list) {
  2147. if (within(addr, mod->module_init, mod->init_size) ||
  2148. within(addr, mod->module_core, mod->core_size)) {
  2149. const char *sym;
  2150. sym = get_ksymbol(mod, addr, size, offset);
  2151. if (!sym)
  2152. goto out;
  2153. if (modname)
  2154. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2155. if (name)
  2156. strlcpy(name, sym, KSYM_NAME_LEN);
  2157. preempt_enable();
  2158. return 0;
  2159. }
  2160. }
  2161. out:
  2162. preempt_enable();
  2163. return -ERANGE;
  2164. }
  2165. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2166. char *name, char *module_name, int *exported)
  2167. {
  2168. struct module *mod;
  2169. preempt_disable();
  2170. list_for_each_entry_rcu(mod, &modules, list) {
  2171. if (symnum < mod->num_symtab) {
  2172. *value = mod->symtab[symnum].st_value;
  2173. *type = mod->symtab[symnum].st_info;
  2174. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2175. KSYM_NAME_LEN);
  2176. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2177. *exported = is_exported(name, mod);
  2178. preempt_enable();
  2179. return 0;
  2180. }
  2181. symnum -= mod->num_symtab;
  2182. }
  2183. preempt_enable();
  2184. return -ERANGE;
  2185. }
  2186. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2187. {
  2188. unsigned int i;
  2189. for (i = 0; i < mod->num_symtab; i++)
  2190. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2191. mod->symtab[i].st_info != 'U')
  2192. return mod->symtab[i].st_value;
  2193. return 0;
  2194. }
  2195. /* Look for this name: can be of form module:name. */
  2196. unsigned long module_kallsyms_lookup_name(const char *name)
  2197. {
  2198. struct module *mod;
  2199. char *colon;
  2200. unsigned long ret = 0;
  2201. /* Don't lock: we're in enough trouble already. */
  2202. preempt_disable();
  2203. if ((colon = strchr(name, ':')) != NULL) {
  2204. *colon = '\0';
  2205. if ((mod = find_module(name)) != NULL)
  2206. ret = mod_find_symname(mod, colon+1);
  2207. *colon = ':';
  2208. } else {
  2209. list_for_each_entry_rcu(mod, &modules, list)
  2210. if ((ret = mod_find_symname(mod, name)) != 0)
  2211. break;
  2212. }
  2213. preempt_enable();
  2214. return ret;
  2215. }
  2216. #endif /* CONFIG_KALLSYMS */
  2217. static char *module_flags(struct module *mod, char *buf)
  2218. {
  2219. int bx = 0;
  2220. if (mod->taints ||
  2221. mod->state == MODULE_STATE_GOING ||
  2222. mod->state == MODULE_STATE_COMING) {
  2223. buf[bx++] = '(';
  2224. if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
  2225. buf[bx++] = 'P';
  2226. if (mod->taints & (1 << TAINT_FORCED_MODULE))
  2227. buf[bx++] = 'F';
  2228. if (mod->taints & (1 << TAINT_CRAP))
  2229. buf[bx++] = 'C';
  2230. /*
  2231. * TAINT_FORCED_RMMOD: could be added.
  2232. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2233. * apply to modules.
  2234. */
  2235. /* Show a - for module-is-being-unloaded */
  2236. if (mod->state == MODULE_STATE_GOING)
  2237. buf[bx++] = '-';
  2238. /* Show a + for module-is-being-loaded */
  2239. if (mod->state == MODULE_STATE_COMING)
  2240. buf[bx++] = '+';
  2241. buf[bx++] = ')';
  2242. }
  2243. buf[bx] = '\0';
  2244. return buf;
  2245. }
  2246. #ifdef CONFIG_PROC_FS
  2247. /* Called by the /proc file system to return a list of modules. */
  2248. static void *m_start(struct seq_file *m, loff_t *pos)
  2249. {
  2250. mutex_lock(&module_mutex);
  2251. return seq_list_start(&modules, *pos);
  2252. }
  2253. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2254. {
  2255. return seq_list_next(p, &modules, pos);
  2256. }
  2257. static void m_stop(struct seq_file *m, void *p)
  2258. {
  2259. mutex_unlock(&module_mutex);
  2260. }
  2261. static int m_show(struct seq_file *m, void *p)
  2262. {
  2263. struct module *mod = list_entry(p, struct module, list);
  2264. char buf[8];
  2265. seq_printf(m, "%s %u",
  2266. mod->name, mod->init_size + mod->core_size);
  2267. print_unload_info(m, mod);
  2268. /* Informative for users. */
  2269. seq_printf(m, " %s",
  2270. mod->state == MODULE_STATE_GOING ? "Unloading":
  2271. mod->state == MODULE_STATE_COMING ? "Loading":
  2272. "Live");
  2273. /* Used by oprofile and other similar tools. */
  2274. seq_printf(m, " 0x%p", mod->module_core);
  2275. /* Taints info */
  2276. if (mod->taints)
  2277. seq_printf(m, " %s", module_flags(mod, buf));
  2278. seq_printf(m, "\n");
  2279. return 0;
  2280. }
  2281. /* Format: modulename size refcount deps address
  2282. Where refcount is a number or -, and deps is a comma-separated list
  2283. of depends or -.
  2284. */
  2285. static const struct seq_operations modules_op = {
  2286. .start = m_start,
  2287. .next = m_next,
  2288. .stop = m_stop,
  2289. .show = m_show
  2290. };
  2291. static int modules_open(struct inode *inode, struct file *file)
  2292. {
  2293. return seq_open(file, &modules_op);
  2294. }
  2295. static const struct file_operations proc_modules_operations = {
  2296. .open = modules_open,
  2297. .read = seq_read,
  2298. .llseek = seq_lseek,
  2299. .release = seq_release,
  2300. };
  2301. static int __init proc_modules_init(void)
  2302. {
  2303. proc_create("modules", 0, NULL, &proc_modules_operations);
  2304. return 0;
  2305. }
  2306. module_init(proc_modules_init);
  2307. #endif
  2308. /* Given an address, look for it in the module exception tables. */
  2309. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2310. {
  2311. const struct exception_table_entry *e = NULL;
  2312. struct module *mod;
  2313. preempt_disable();
  2314. list_for_each_entry_rcu(mod, &modules, list) {
  2315. if (mod->num_exentries == 0)
  2316. continue;
  2317. e = search_extable(mod->extable,
  2318. mod->extable + mod->num_exentries - 1,
  2319. addr);
  2320. if (e)
  2321. break;
  2322. }
  2323. preempt_enable();
  2324. /* Now, if we found one, we are running inside it now, hence
  2325. we cannot unload the module, hence no refcnt needed. */
  2326. return e;
  2327. }
  2328. /*
  2329. * Is this a valid module address?
  2330. */
  2331. int is_module_address(unsigned long addr)
  2332. {
  2333. struct module *mod;
  2334. preempt_disable();
  2335. list_for_each_entry_rcu(mod, &modules, list) {
  2336. if (within(addr, mod->module_core, mod->core_size)) {
  2337. preempt_enable();
  2338. return 1;
  2339. }
  2340. }
  2341. preempt_enable();
  2342. return 0;
  2343. }
  2344. /* Is this a valid kernel address? */
  2345. struct module *__module_text_address(unsigned long addr)
  2346. {
  2347. struct module *mod;
  2348. if (addr < module_addr_min || addr > module_addr_max)
  2349. return NULL;
  2350. list_for_each_entry_rcu(mod, &modules, list)
  2351. if (within(addr, mod->module_init, mod->init_text_size)
  2352. || within(addr, mod->module_core, mod->core_text_size))
  2353. return mod;
  2354. return NULL;
  2355. }
  2356. struct module *module_text_address(unsigned long addr)
  2357. {
  2358. struct module *mod;
  2359. preempt_disable();
  2360. mod = __module_text_address(addr);
  2361. preempt_enable();
  2362. return mod;
  2363. }
  2364. /* Don't grab lock, we're oopsing. */
  2365. void print_modules(void)
  2366. {
  2367. struct module *mod;
  2368. char buf[8];
  2369. printk("Modules linked in:");
  2370. /* Most callers should already have preempt disabled, but make sure */
  2371. preempt_disable();
  2372. list_for_each_entry_rcu(mod, &modules, list)
  2373. printk(" %s%s", mod->name, module_flags(mod, buf));
  2374. preempt_enable();
  2375. if (last_unloaded_module[0])
  2376. printk(" [last unloaded: %s]", last_unloaded_module);
  2377. printk("\n");
  2378. }
  2379. #ifdef CONFIG_MODVERSIONS
  2380. /* Generate the signature for struct module here, too, for modversions. */
  2381. void struct_module(struct module *mod) { return; }
  2382. EXPORT_SYMBOL(struct_module);
  2383. #endif
  2384. #ifdef CONFIG_MARKERS
  2385. void module_update_markers(void)
  2386. {
  2387. struct module *mod;
  2388. mutex_lock(&module_mutex);
  2389. list_for_each_entry(mod, &modules, list)
  2390. if (!mod->taints)
  2391. marker_update_probe_range(mod->markers,
  2392. mod->markers + mod->num_markers);
  2393. mutex_unlock(&module_mutex);
  2394. }
  2395. #endif
  2396. #ifdef CONFIG_TRACEPOINTS
  2397. void module_update_tracepoints(void)
  2398. {
  2399. struct module *mod;
  2400. mutex_lock(&module_mutex);
  2401. list_for_each_entry(mod, &modules, list)
  2402. if (!mod->taints)
  2403. tracepoint_update_probe_range(mod->tracepoints,
  2404. mod->tracepoints + mod->num_tracepoints);
  2405. mutex_unlock(&module_mutex);
  2406. }
  2407. /*
  2408. * Returns 0 if current not found.
  2409. * Returns 1 if current found.
  2410. */
  2411. int module_get_iter_tracepoints(struct tracepoint_iter *iter)
  2412. {
  2413. struct module *iter_mod;
  2414. int found = 0;
  2415. mutex_lock(&module_mutex);
  2416. list_for_each_entry(iter_mod, &modules, list) {
  2417. if (!iter_mod->taints) {
  2418. /*
  2419. * Sorted module list
  2420. */
  2421. if (iter_mod < iter->module)
  2422. continue;
  2423. else if (iter_mod > iter->module)
  2424. iter->tracepoint = NULL;
  2425. found = tracepoint_get_iter_range(&iter->tracepoint,
  2426. iter_mod->tracepoints,
  2427. iter_mod->tracepoints
  2428. + iter_mod->num_tracepoints);
  2429. if (found) {
  2430. iter->module = iter_mod;
  2431. break;
  2432. }
  2433. }
  2434. }
  2435. mutex_unlock(&module_mutex);
  2436. return found;
  2437. }
  2438. #endif