module.c 76 KB

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