module.c 75 KB

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