module.c 81 KB

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