module.c 77 KB

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