module.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201
  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. const Elf_Shdr *sechdrs,
  1801. const char *secstrings)
  1802. {
  1803. unsigned int i;
  1804. /* only scan the sections containing data */
  1805. kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
  1806. for (i = 1; i < hdr->e_shnum; i++) {
  1807. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1808. continue;
  1809. if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
  1810. && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
  1811. continue;
  1812. kmemleak_scan_area((void *)sechdrs[i].sh_addr,
  1813. sechdrs[i].sh_size, GFP_KERNEL);
  1814. }
  1815. }
  1816. #else
  1817. static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
  1818. Elf_Shdr *sechdrs,
  1819. const char *secstrings)
  1820. {
  1821. }
  1822. #endif
  1823. static void find_module_sections(struct module *mod, Elf_Ehdr *hdr,
  1824. Elf_Shdr *sechdrs, const char *secstrings)
  1825. {
  1826. mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
  1827. sizeof(*mod->kp), &mod->num_kp);
  1828. mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
  1829. sizeof(*mod->syms), &mod->num_syms);
  1830. mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
  1831. mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
  1832. sizeof(*mod->gpl_syms),
  1833. &mod->num_gpl_syms);
  1834. mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1835. mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
  1836. "__ksymtab_gpl_future",
  1837. sizeof(*mod->gpl_future_syms),
  1838. &mod->num_gpl_future_syms);
  1839. mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
  1840. "__kcrctab_gpl_future");
  1841. #ifdef CONFIG_UNUSED_SYMBOLS
  1842. mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
  1843. "__ksymtab_unused",
  1844. sizeof(*mod->unused_syms),
  1845. &mod->num_unused_syms);
  1846. mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
  1847. "__kcrctab_unused");
  1848. mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
  1849. "__ksymtab_unused_gpl",
  1850. sizeof(*mod->unused_gpl_syms),
  1851. &mod->num_unused_gpl_syms);
  1852. mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
  1853. "__kcrctab_unused_gpl");
  1854. #endif
  1855. #ifdef CONFIG_CONSTRUCTORS
  1856. mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
  1857. sizeof(*mod->ctors), &mod->num_ctors);
  1858. #endif
  1859. #ifdef CONFIG_TRACEPOINTS
  1860. mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
  1861. "__tracepoints",
  1862. sizeof(*mod->tracepoints),
  1863. &mod->num_tracepoints);
  1864. #endif
  1865. #ifdef CONFIG_EVENT_TRACING
  1866. mod->trace_events = section_objs(hdr, sechdrs, secstrings,
  1867. "_ftrace_events",
  1868. sizeof(*mod->trace_events),
  1869. &mod->num_trace_events);
  1870. /*
  1871. * This section contains pointers to allocated objects in the trace
  1872. * code and not scanning it leads to false positives.
  1873. */
  1874. kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
  1875. mod->num_trace_events, GFP_KERNEL);
  1876. #endif
  1877. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  1878. /* sechdrs[0].sh_size is always zero */
  1879. mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
  1880. "__mcount_loc",
  1881. sizeof(*mod->ftrace_callsites),
  1882. &mod->num_ftrace_callsites);
  1883. #endif
  1884. }
  1885. static struct module *move_module(struct module *mod,
  1886. Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  1887. const char *secstrings, unsigned modindex)
  1888. {
  1889. int i;
  1890. void *ptr;
  1891. /* Do the allocs. */
  1892. ptr = module_alloc_update_bounds(mod->core_size);
  1893. /*
  1894. * The pointer to this block is stored in the module structure
  1895. * which is inside the block. Just mark it as not being a
  1896. * leak.
  1897. */
  1898. kmemleak_not_leak(ptr);
  1899. if (!ptr)
  1900. return ERR_PTR(-ENOMEM);
  1901. memset(ptr, 0, mod->core_size);
  1902. mod->module_core = ptr;
  1903. ptr = module_alloc_update_bounds(mod->init_size);
  1904. /*
  1905. * The pointer to this block is stored in the module structure
  1906. * which is inside the block. This block doesn't need to be
  1907. * scanned as it contains data and code that will be freed
  1908. * after the module is initialized.
  1909. */
  1910. kmemleak_ignore(ptr);
  1911. if (!ptr && mod->init_size) {
  1912. module_free(mod, mod->module_core);
  1913. return ERR_PTR(-ENOMEM);
  1914. }
  1915. memset(ptr, 0, mod->init_size);
  1916. mod->module_init = ptr;
  1917. /* Transfer each section which specifies SHF_ALLOC */
  1918. DEBUGP("final section addresses:\n");
  1919. for (i = 0; i < hdr->e_shnum; i++) {
  1920. void *dest;
  1921. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1922. continue;
  1923. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1924. dest = mod->module_init
  1925. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1926. else
  1927. dest = mod->module_core + sechdrs[i].sh_entsize;
  1928. if (sechdrs[i].sh_type != SHT_NOBITS)
  1929. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1930. sechdrs[i].sh_size);
  1931. /* Update sh_addr to point to copy in image. */
  1932. sechdrs[i].sh_addr = (unsigned long)dest;
  1933. DEBUGP("\t0x%lx %s\n",
  1934. sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1935. }
  1936. /* Module has been moved. */
  1937. mod = (void *)sechdrs[modindex].sh_addr;
  1938. kmemleak_load_module(mod, hdr, sechdrs, secstrings);
  1939. return mod;
  1940. }
  1941. /* Allocate and load the module: note that size of section 0 is always
  1942. zero, and we rely on this for optional sections. */
  1943. static noinline struct module *load_module(void __user *umod,
  1944. unsigned long len,
  1945. const char __user *uargs)
  1946. {
  1947. Elf_Ehdr *hdr;
  1948. Elf_Shdr *sechdrs;
  1949. char *secstrings, *args, *modmagic, *strtab = NULL;
  1950. char *staging;
  1951. unsigned int i;
  1952. unsigned int symindex = 0;
  1953. unsigned int strindex = 0;
  1954. unsigned int modindex, versindex, infoindex, pcpuindex;
  1955. struct module *mod;
  1956. long err = 0;
  1957. unsigned long symoffs, stroffs, *strmap;
  1958. void __percpu *percpu;
  1959. struct _ddebug *debug = NULL;
  1960. unsigned int num_debug = 0;
  1961. mm_segment_t old_fs;
  1962. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1963. umod, len, uargs);
  1964. if (len < sizeof(*hdr))
  1965. return ERR_PTR(-ENOEXEC);
  1966. /* Suck in entire file: we'll want most of it. */
  1967. /* vmalloc barfs on "unusual" numbers. Check here */
  1968. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1969. return ERR_PTR(-ENOMEM);
  1970. if (copy_from_user(hdr, umod, len) != 0) {
  1971. err = -EFAULT;
  1972. goto free_hdr;
  1973. }
  1974. /* Sanity checks against insmoding binaries or wrong arch,
  1975. weird elf version */
  1976. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
  1977. || hdr->e_type != ET_REL
  1978. || !elf_check_arch(hdr)
  1979. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1980. err = -ENOEXEC;
  1981. goto free_hdr;
  1982. }
  1983. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1984. goto truncated;
  1985. /* Convenience variables */
  1986. sechdrs = (void *)hdr + hdr->e_shoff;
  1987. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1988. sechdrs[0].sh_addr = 0;
  1989. for (i = 1; i < hdr->e_shnum; i++) {
  1990. if (sechdrs[i].sh_type != SHT_NOBITS
  1991. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1992. goto truncated;
  1993. /* Mark all sections sh_addr with their address in the
  1994. temporary image. */
  1995. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1996. /* Internal symbols and strings. */
  1997. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1998. symindex = i;
  1999. strindex = sechdrs[i].sh_link;
  2000. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  2001. }
  2002. #ifndef CONFIG_MODULE_UNLOAD
  2003. /* Don't load .exit sections */
  2004. if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
  2005. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2006. #endif
  2007. }
  2008. modindex = find_sec(hdr, sechdrs, secstrings,
  2009. ".gnu.linkonce.this_module");
  2010. if (!modindex) {
  2011. printk(KERN_WARNING "No module found in object\n");
  2012. err = -ENOEXEC;
  2013. goto free_hdr;
  2014. }
  2015. /* This is temporary: point mod into copy of data. */
  2016. mod = (void *)sechdrs[modindex].sh_addr;
  2017. if (symindex == 0) {
  2018. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  2019. mod->name);
  2020. err = -ENOEXEC;
  2021. goto free_hdr;
  2022. }
  2023. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  2024. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  2025. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  2026. /* Don't keep modinfo and version sections. */
  2027. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2028. sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2029. /* Check module struct version now, before we try to use module. */
  2030. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  2031. err = -ENOEXEC;
  2032. goto free_hdr;
  2033. }
  2034. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  2035. /* This is allowed: modprobe --force will invalidate it. */
  2036. if (!modmagic) {
  2037. err = try_to_force_load(mod, "bad vermagic");
  2038. if (err)
  2039. goto free_hdr;
  2040. } else if (!same_magic(modmagic, vermagic, versindex)) {
  2041. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  2042. mod->name, modmagic, vermagic);
  2043. err = -ENOEXEC;
  2044. goto free_hdr;
  2045. }
  2046. staging = get_modinfo(sechdrs, infoindex, "staging");
  2047. if (staging) {
  2048. add_taint_module(mod, TAINT_CRAP);
  2049. printk(KERN_WARNING "%s: module is from the staging directory,"
  2050. " the quality is unknown, you have been warned.\n",
  2051. mod->name);
  2052. }
  2053. /* Now copy in args */
  2054. args = strndup_user(uargs, ~0UL >> 1);
  2055. if (IS_ERR(args)) {
  2056. err = PTR_ERR(args);
  2057. goto free_hdr;
  2058. }
  2059. strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
  2060. * sizeof(long), GFP_KERNEL);
  2061. if (!strmap) {
  2062. err = -ENOMEM;
  2063. goto free_mod;
  2064. }
  2065. mod->state = MODULE_STATE_COMING;
  2066. /* Allow arches to frob section contents and sizes. */
  2067. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  2068. if (err < 0)
  2069. goto free_mod;
  2070. if (pcpuindex) {
  2071. /* We have a special allocation for this section. */
  2072. err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
  2073. sechdrs[pcpuindex].sh_addralign);
  2074. if (err)
  2075. goto free_mod;
  2076. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2077. }
  2078. /* Keep this around for failure path. */
  2079. percpu = mod_percpu(mod);
  2080. /* Determine total sizes, and put offsets in sh_entsize. For now
  2081. this is done generically; there doesn't appear to be any
  2082. special cases for the architectures. */
  2083. layout_sections(mod, hdr, sechdrs, secstrings);
  2084. symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
  2085. secstrings, &stroffs, strmap);
  2086. /* Allocate and move to the final place */
  2087. mod = move_module(mod, hdr, sechdrs, secstrings, modindex);
  2088. if (IS_ERR(mod)) {
  2089. err = PTR_ERR(mod);
  2090. goto free_percpu;
  2091. }
  2092. #if defined(CONFIG_MODULE_UNLOAD)
  2093. mod->refptr = alloc_percpu(struct module_ref);
  2094. if (!mod->refptr) {
  2095. err = -ENOMEM;
  2096. goto free_init;
  2097. }
  2098. #endif
  2099. /* Now we've moved module, initialize linked lists, etc. */
  2100. module_unload_init(mod);
  2101. /* Set up license info based on the info section */
  2102. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  2103. /*
  2104. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  2105. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  2106. * using GPL-only symbols it needs.
  2107. */
  2108. if (strcmp(mod->name, "ndiswrapper") == 0)
  2109. add_taint(TAINT_PROPRIETARY_MODULE);
  2110. /* driverloader was caught wrongly pretending to be under GPL */
  2111. if (strcmp(mod->name, "driverloader") == 0)
  2112. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  2113. /* Set up MODINFO_ATTR fields */
  2114. setup_modinfo(mod, sechdrs, infoindex);
  2115. /* Fix up syms, so that st_value is a pointer to location. */
  2116. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  2117. mod);
  2118. if (err < 0)
  2119. goto cleanup;
  2120. /* Now we've got everything in the final locations, we can
  2121. * find optional sections. */
  2122. find_module_sections(mod, hdr, sechdrs, secstrings);
  2123. #ifdef CONFIG_MODVERSIONS
  2124. if ((mod->num_syms && !mod->crcs)
  2125. || (mod->num_gpl_syms && !mod->gpl_crcs)
  2126. || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
  2127. #ifdef CONFIG_UNUSED_SYMBOLS
  2128. || (mod->num_unused_syms && !mod->unused_crcs)
  2129. || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
  2130. #endif
  2131. ) {
  2132. err = try_to_force_load(mod,
  2133. "no versions for exported symbols");
  2134. if (err)
  2135. goto cleanup;
  2136. }
  2137. #endif
  2138. /* Now do relocations. */
  2139. for (i = 1; i < hdr->e_shnum; i++) {
  2140. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  2141. unsigned int info = sechdrs[i].sh_info;
  2142. /* Not a valid relocation section? */
  2143. if (info >= hdr->e_shnum)
  2144. continue;
  2145. /* Don't bother with non-allocated sections */
  2146. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  2147. continue;
  2148. if (sechdrs[i].sh_type == SHT_REL)
  2149. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  2150. else if (sechdrs[i].sh_type == SHT_RELA)
  2151. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  2152. mod);
  2153. if (err < 0)
  2154. goto cleanup;
  2155. }
  2156. /* Set up and sort exception table */
  2157. mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
  2158. sizeof(*mod->extable), &mod->num_exentries);
  2159. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  2160. /* Finally, copy percpu area over. */
  2161. percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
  2162. sechdrs[pcpuindex].sh_size);
  2163. add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
  2164. symoffs, stroffs, secstrings, strmap);
  2165. kfree(strmap);
  2166. strmap = NULL;
  2167. if (!mod->taints)
  2168. debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
  2169. sizeof(*debug), &num_debug);
  2170. err = module_finalize(hdr, sechdrs, mod);
  2171. if (err < 0)
  2172. goto cleanup;
  2173. /* flush the icache in correct context */
  2174. old_fs = get_fs();
  2175. set_fs(KERNEL_DS);
  2176. /*
  2177. * Flush the instruction cache, since we've played with text.
  2178. * Do it before processing of module parameters, so the module
  2179. * can provide parameter accessor functions of its own.
  2180. */
  2181. if (mod->module_init)
  2182. flush_icache_range((unsigned long)mod->module_init,
  2183. (unsigned long)mod->module_init
  2184. + mod->init_size);
  2185. flush_icache_range((unsigned long)mod->module_core,
  2186. (unsigned long)mod->module_core + mod->core_size);
  2187. set_fs(old_fs);
  2188. mod->args = args;
  2189. if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
  2190. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  2191. mod->name);
  2192. /* Now sew it into the lists so we can get lockdep and oops
  2193. * info during argument parsing. Noone should access us, since
  2194. * strong_try_module_get() will fail.
  2195. * lockdep/oops can run asynchronous, so use the RCU list insertion
  2196. * function to insert in a way safe to concurrent readers.
  2197. * The mutex protects against concurrent writers.
  2198. */
  2199. mutex_lock(&module_mutex);
  2200. if (find_module(mod->name)) {
  2201. err = -EEXIST;
  2202. goto unlock;
  2203. }
  2204. if (debug)
  2205. dynamic_debug_setup(debug, num_debug);
  2206. /* Find duplicate symbols */
  2207. err = verify_export_symbols(mod);
  2208. if (err < 0)
  2209. goto ddebug;
  2210. list_add_rcu(&mod->list, &modules);
  2211. mutex_unlock(&module_mutex);
  2212. err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
  2213. if (err < 0)
  2214. goto unlink;
  2215. err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
  2216. if (err < 0)
  2217. goto unlink;
  2218. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  2219. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  2220. /* Get rid of temporary copy */
  2221. vfree(hdr);
  2222. trace_module_load(mod);
  2223. /* Done! */
  2224. return mod;
  2225. unlink:
  2226. mutex_lock(&module_mutex);
  2227. /* Unlink carefully: kallsyms could be walking list. */
  2228. list_del_rcu(&mod->list);
  2229. ddebug:
  2230. dynamic_debug_remove(debug);
  2231. unlock:
  2232. mutex_unlock(&module_mutex);
  2233. synchronize_sched();
  2234. module_arch_cleanup(mod);
  2235. cleanup:
  2236. free_modinfo(mod);
  2237. module_unload_free(mod);
  2238. #if defined(CONFIG_MODULE_UNLOAD)
  2239. free_percpu(mod->refptr);
  2240. free_init:
  2241. #endif
  2242. module_free(mod, mod->module_init);
  2243. module_free(mod, mod->module_core);
  2244. /* mod will be freed with core. Don't access it beyond this line! */
  2245. free_percpu:
  2246. free_percpu(percpu);
  2247. free_mod:
  2248. kfree(args);
  2249. kfree(strmap);
  2250. free_hdr:
  2251. vfree(hdr);
  2252. return ERR_PTR(err);
  2253. truncated:
  2254. printk(KERN_ERR "Module len %lu truncated\n", len);
  2255. err = -ENOEXEC;
  2256. goto free_hdr;
  2257. }
  2258. /* Call module constructors. */
  2259. static void do_mod_ctors(struct module *mod)
  2260. {
  2261. #ifdef CONFIG_CONSTRUCTORS
  2262. unsigned long i;
  2263. for (i = 0; i < mod->num_ctors; i++)
  2264. mod->ctors[i]();
  2265. #endif
  2266. }
  2267. /* This is where the real work happens */
  2268. SYSCALL_DEFINE3(init_module, void __user *, umod,
  2269. unsigned long, len, const char __user *, uargs)
  2270. {
  2271. struct module *mod;
  2272. int ret = 0;
  2273. /* Must have permission */
  2274. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  2275. return -EPERM;
  2276. /* Do all the hard work */
  2277. mod = load_module(umod, len, uargs);
  2278. if (IS_ERR(mod))
  2279. return PTR_ERR(mod);
  2280. blocking_notifier_call_chain(&module_notify_list,
  2281. MODULE_STATE_COMING, mod);
  2282. do_mod_ctors(mod);
  2283. /* Start the module */
  2284. if (mod->init != NULL)
  2285. ret = do_one_initcall(mod->init);
  2286. if (ret < 0) {
  2287. /* Init routine failed: abort. Try to protect us from
  2288. buggy refcounters. */
  2289. mod->state = MODULE_STATE_GOING;
  2290. synchronize_sched();
  2291. module_put(mod);
  2292. blocking_notifier_call_chain(&module_notify_list,
  2293. MODULE_STATE_GOING, mod);
  2294. free_module(mod);
  2295. wake_up(&module_wq);
  2296. return ret;
  2297. }
  2298. if (ret > 0) {
  2299. printk(KERN_WARNING
  2300. "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
  2301. "%s: loading module anyway...\n",
  2302. __func__, mod->name, ret,
  2303. __func__);
  2304. dump_stack();
  2305. }
  2306. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  2307. mod->state = MODULE_STATE_LIVE;
  2308. wake_up(&module_wq);
  2309. blocking_notifier_call_chain(&module_notify_list,
  2310. MODULE_STATE_LIVE, mod);
  2311. /* We need to finish all async code before the module init sequence is done */
  2312. async_synchronize_full();
  2313. mutex_lock(&module_mutex);
  2314. /* Drop initial reference. */
  2315. module_put(mod);
  2316. trim_init_extable(mod);
  2317. #ifdef CONFIG_KALLSYMS
  2318. mod->num_symtab = mod->core_num_syms;
  2319. mod->symtab = mod->core_symtab;
  2320. mod->strtab = mod->core_strtab;
  2321. #endif
  2322. module_free(mod, mod->module_init);
  2323. mod->module_init = NULL;
  2324. mod->init_size = 0;
  2325. mod->init_text_size = 0;
  2326. mutex_unlock(&module_mutex);
  2327. return 0;
  2328. }
  2329. static inline int within(unsigned long addr, void *start, unsigned long size)
  2330. {
  2331. return ((void *)addr >= start && (void *)addr < start + size);
  2332. }
  2333. #ifdef CONFIG_KALLSYMS
  2334. /*
  2335. * This ignores the intensely annoying "mapping symbols" found
  2336. * in ARM ELF files: $a, $t and $d.
  2337. */
  2338. static inline int is_arm_mapping_symbol(const char *str)
  2339. {
  2340. return str[0] == '$' && strchr("atd", str[1])
  2341. && (str[2] == '\0' || str[2] == '.');
  2342. }
  2343. static const char *get_ksymbol(struct module *mod,
  2344. unsigned long addr,
  2345. unsigned long *size,
  2346. unsigned long *offset)
  2347. {
  2348. unsigned int i, best = 0;
  2349. unsigned long nextval;
  2350. /* At worse, next value is at end of module */
  2351. if (within_module_init(addr, mod))
  2352. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  2353. else
  2354. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  2355. /* Scan for closest preceeding symbol, and next symbol. (ELF
  2356. starts real symbols at 1). */
  2357. for (i = 1; i < mod->num_symtab; i++) {
  2358. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  2359. continue;
  2360. /* We ignore unnamed symbols: they're uninformative
  2361. * and inserted at a whim. */
  2362. if (mod->symtab[i].st_value <= addr
  2363. && mod->symtab[i].st_value > mod->symtab[best].st_value
  2364. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2365. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2366. best = i;
  2367. if (mod->symtab[i].st_value > addr
  2368. && mod->symtab[i].st_value < nextval
  2369. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2370. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2371. nextval = mod->symtab[i].st_value;
  2372. }
  2373. if (!best)
  2374. return NULL;
  2375. if (size)
  2376. *size = nextval - mod->symtab[best].st_value;
  2377. if (offset)
  2378. *offset = addr - mod->symtab[best].st_value;
  2379. return mod->strtab + mod->symtab[best].st_name;
  2380. }
  2381. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  2382. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  2383. const char *module_address_lookup(unsigned long addr,
  2384. unsigned long *size,
  2385. unsigned long *offset,
  2386. char **modname,
  2387. char *namebuf)
  2388. {
  2389. struct module *mod;
  2390. const char *ret = NULL;
  2391. preempt_disable();
  2392. list_for_each_entry_rcu(mod, &modules, list) {
  2393. if (within_module_init(addr, mod) ||
  2394. within_module_core(addr, mod)) {
  2395. if (modname)
  2396. *modname = mod->name;
  2397. ret = get_ksymbol(mod, addr, size, offset);
  2398. break;
  2399. }
  2400. }
  2401. /* Make a copy in here where it's safe */
  2402. if (ret) {
  2403. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2404. ret = namebuf;
  2405. }
  2406. preempt_enable();
  2407. return ret;
  2408. }
  2409. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2410. {
  2411. struct module *mod;
  2412. preempt_disable();
  2413. list_for_each_entry_rcu(mod, &modules, list) {
  2414. if (within_module_init(addr, mod) ||
  2415. within_module_core(addr, mod)) {
  2416. const char *sym;
  2417. sym = get_ksymbol(mod, addr, NULL, NULL);
  2418. if (!sym)
  2419. goto out;
  2420. strlcpy(symname, sym, KSYM_NAME_LEN);
  2421. preempt_enable();
  2422. return 0;
  2423. }
  2424. }
  2425. out:
  2426. preempt_enable();
  2427. return -ERANGE;
  2428. }
  2429. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2430. unsigned long *offset, char *modname, char *name)
  2431. {
  2432. struct module *mod;
  2433. preempt_disable();
  2434. list_for_each_entry_rcu(mod, &modules, list) {
  2435. if (within_module_init(addr, mod) ||
  2436. within_module_core(addr, mod)) {
  2437. const char *sym;
  2438. sym = get_ksymbol(mod, addr, size, offset);
  2439. if (!sym)
  2440. goto out;
  2441. if (modname)
  2442. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2443. if (name)
  2444. strlcpy(name, sym, KSYM_NAME_LEN);
  2445. preempt_enable();
  2446. return 0;
  2447. }
  2448. }
  2449. out:
  2450. preempt_enable();
  2451. return -ERANGE;
  2452. }
  2453. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2454. char *name, char *module_name, int *exported)
  2455. {
  2456. struct module *mod;
  2457. preempt_disable();
  2458. list_for_each_entry_rcu(mod, &modules, list) {
  2459. if (symnum < mod->num_symtab) {
  2460. *value = mod->symtab[symnum].st_value;
  2461. *type = mod->symtab[symnum].st_info;
  2462. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2463. KSYM_NAME_LEN);
  2464. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2465. *exported = is_exported(name, *value, mod);
  2466. preempt_enable();
  2467. return 0;
  2468. }
  2469. symnum -= mod->num_symtab;
  2470. }
  2471. preempt_enable();
  2472. return -ERANGE;
  2473. }
  2474. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2475. {
  2476. unsigned int i;
  2477. for (i = 0; i < mod->num_symtab; i++)
  2478. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2479. mod->symtab[i].st_info != 'U')
  2480. return mod->symtab[i].st_value;
  2481. return 0;
  2482. }
  2483. /* Look for this name: can be of form module:name. */
  2484. unsigned long module_kallsyms_lookup_name(const char *name)
  2485. {
  2486. struct module *mod;
  2487. char *colon;
  2488. unsigned long ret = 0;
  2489. /* Don't lock: we're in enough trouble already. */
  2490. preempt_disable();
  2491. if ((colon = strchr(name, ':')) != NULL) {
  2492. *colon = '\0';
  2493. if ((mod = find_module(name)) != NULL)
  2494. ret = mod_find_symname(mod, colon+1);
  2495. *colon = ':';
  2496. } else {
  2497. list_for_each_entry_rcu(mod, &modules, list)
  2498. if ((ret = mod_find_symname(mod, name)) != 0)
  2499. break;
  2500. }
  2501. preempt_enable();
  2502. return ret;
  2503. }
  2504. int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  2505. struct module *, unsigned long),
  2506. void *data)
  2507. {
  2508. struct module *mod;
  2509. unsigned int i;
  2510. int ret;
  2511. list_for_each_entry(mod, &modules, list) {
  2512. for (i = 0; i < mod->num_symtab; i++) {
  2513. ret = fn(data, mod->strtab + mod->symtab[i].st_name,
  2514. mod, mod->symtab[i].st_value);
  2515. if (ret != 0)
  2516. return ret;
  2517. }
  2518. }
  2519. return 0;
  2520. }
  2521. #endif /* CONFIG_KALLSYMS */
  2522. static char *module_flags(struct module *mod, char *buf)
  2523. {
  2524. int bx = 0;
  2525. if (mod->taints ||
  2526. mod->state == MODULE_STATE_GOING ||
  2527. mod->state == MODULE_STATE_COMING) {
  2528. buf[bx++] = '(';
  2529. if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
  2530. buf[bx++] = 'P';
  2531. if (mod->taints & (1 << TAINT_FORCED_MODULE))
  2532. buf[bx++] = 'F';
  2533. if (mod->taints & (1 << TAINT_CRAP))
  2534. buf[bx++] = 'C';
  2535. /*
  2536. * TAINT_FORCED_RMMOD: could be added.
  2537. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2538. * apply to modules.
  2539. */
  2540. /* Show a - for module-is-being-unloaded */
  2541. if (mod->state == MODULE_STATE_GOING)
  2542. buf[bx++] = '-';
  2543. /* Show a + for module-is-being-loaded */
  2544. if (mod->state == MODULE_STATE_COMING)
  2545. buf[bx++] = '+';
  2546. buf[bx++] = ')';
  2547. }
  2548. buf[bx] = '\0';
  2549. return buf;
  2550. }
  2551. #ifdef CONFIG_PROC_FS
  2552. /* Called by the /proc file system to return a list of modules. */
  2553. static void *m_start(struct seq_file *m, loff_t *pos)
  2554. {
  2555. mutex_lock(&module_mutex);
  2556. return seq_list_start(&modules, *pos);
  2557. }
  2558. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2559. {
  2560. return seq_list_next(p, &modules, pos);
  2561. }
  2562. static void m_stop(struct seq_file *m, void *p)
  2563. {
  2564. mutex_unlock(&module_mutex);
  2565. }
  2566. static int m_show(struct seq_file *m, void *p)
  2567. {
  2568. struct module *mod = list_entry(p, struct module, list);
  2569. char buf[8];
  2570. seq_printf(m, "%s %u",
  2571. mod->name, mod->init_size + mod->core_size);
  2572. print_unload_info(m, mod);
  2573. /* Informative for users. */
  2574. seq_printf(m, " %s",
  2575. mod->state == MODULE_STATE_GOING ? "Unloading":
  2576. mod->state == MODULE_STATE_COMING ? "Loading":
  2577. "Live");
  2578. /* Used by oprofile and other similar tools. */
  2579. seq_printf(m, " 0x%p", mod->module_core);
  2580. /* Taints info */
  2581. if (mod->taints)
  2582. seq_printf(m, " %s", module_flags(mod, buf));
  2583. seq_printf(m, "\n");
  2584. return 0;
  2585. }
  2586. /* Format: modulename size refcount deps address
  2587. Where refcount is a number or -, and deps is a comma-separated list
  2588. of depends or -.
  2589. */
  2590. static const struct seq_operations modules_op = {
  2591. .start = m_start,
  2592. .next = m_next,
  2593. .stop = m_stop,
  2594. .show = m_show
  2595. };
  2596. static int modules_open(struct inode *inode, struct file *file)
  2597. {
  2598. return seq_open(file, &modules_op);
  2599. }
  2600. static const struct file_operations proc_modules_operations = {
  2601. .open = modules_open,
  2602. .read = seq_read,
  2603. .llseek = seq_lseek,
  2604. .release = seq_release,
  2605. };
  2606. static int __init proc_modules_init(void)
  2607. {
  2608. proc_create("modules", 0, NULL, &proc_modules_operations);
  2609. return 0;
  2610. }
  2611. module_init(proc_modules_init);
  2612. #endif
  2613. /* Given an address, look for it in the module exception tables. */
  2614. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2615. {
  2616. const struct exception_table_entry *e = NULL;
  2617. struct module *mod;
  2618. preempt_disable();
  2619. list_for_each_entry_rcu(mod, &modules, list) {
  2620. if (mod->num_exentries == 0)
  2621. continue;
  2622. e = search_extable(mod->extable,
  2623. mod->extable + mod->num_exentries - 1,
  2624. addr);
  2625. if (e)
  2626. break;
  2627. }
  2628. preempt_enable();
  2629. /* Now, if we found one, we are running inside it now, hence
  2630. we cannot unload the module, hence no refcnt needed. */
  2631. return e;
  2632. }
  2633. /*
  2634. * is_module_address - is this address inside a module?
  2635. * @addr: the address to check.
  2636. *
  2637. * See is_module_text_address() if you simply want to see if the address
  2638. * is code (not data).
  2639. */
  2640. bool is_module_address(unsigned long addr)
  2641. {
  2642. bool ret;
  2643. preempt_disable();
  2644. ret = __module_address(addr) != NULL;
  2645. preempt_enable();
  2646. return ret;
  2647. }
  2648. /*
  2649. * __module_address - get the module which contains an address.
  2650. * @addr: the address.
  2651. *
  2652. * Must be called with preempt disabled or module mutex held so that
  2653. * module doesn't get freed during this.
  2654. */
  2655. struct module *__module_address(unsigned long addr)
  2656. {
  2657. struct module *mod;
  2658. if (addr < module_addr_min || addr > module_addr_max)
  2659. return NULL;
  2660. list_for_each_entry_rcu(mod, &modules, list)
  2661. if (within_module_core(addr, mod)
  2662. || within_module_init(addr, mod))
  2663. return mod;
  2664. return NULL;
  2665. }
  2666. EXPORT_SYMBOL_GPL(__module_address);
  2667. /*
  2668. * is_module_text_address - is this address inside module code?
  2669. * @addr: the address to check.
  2670. *
  2671. * See is_module_address() if you simply want to see if the address is
  2672. * anywhere in a module. See kernel_text_address() for testing if an
  2673. * address corresponds to kernel or module code.
  2674. */
  2675. bool is_module_text_address(unsigned long addr)
  2676. {
  2677. bool ret;
  2678. preempt_disable();
  2679. ret = __module_text_address(addr) != NULL;
  2680. preempt_enable();
  2681. return ret;
  2682. }
  2683. /*
  2684. * __module_text_address - get the module whose code contains an address.
  2685. * @addr: the address.
  2686. *
  2687. * Must be called with preempt disabled or module mutex held so that
  2688. * module doesn't get freed during this.
  2689. */
  2690. struct module *__module_text_address(unsigned long addr)
  2691. {
  2692. struct module *mod = __module_address(addr);
  2693. if (mod) {
  2694. /* Make sure it's within the text section. */
  2695. if (!within(addr, mod->module_init, mod->init_text_size)
  2696. && !within(addr, mod->module_core, mod->core_text_size))
  2697. mod = NULL;
  2698. }
  2699. return mod;
  2700. }
  2701. EXPORT_SYMBOL_GPL(__module_text_address);
  2702. /* Don't grab lock, we're oopsing. */
  2703. void print_modules(void)
  2704. {
  2705. struct module *mod;
  2706. char buf[8];
  2707. printk(KERN_DEFAULT "Modules linked in:");
  2708. /* Most callers should already have preempt disabled, but make sure */
  2709. preempt_disable();
  2710. list_for_each_entry_rcu(mod, &modules, list)
  2711. printk(" %s%s", mod->name, module_flags(mod, buf));
  2712. preempt_enable();
  2713. if (last_unloaded_module[0])
  2714. printk(" [last unloaded: %s]", last_unloaded_module);
  2715. printk("\n");
  2716. }
  2717. #ifdef CONFIG_MODVERSIONS
  2718. /* Generate the signature for all relevant module structures here.
  2719. * If these change, we don't want to try to parse the module. */
  2720. void module_layout(struct module *mod,
  2721. struct modversion_info *ver,
  2722. struct kernel_param *kp,
  2723. struct kernel_symbol *ks,
  2724. struct tracepoint *tp)
  2725. {
  2726. }
  2727. EXPORT_SYMBOL(module_layout);
  2728. #endif
  2729. #ifdef CONFIG_TRACEPOINTS
  2730. void module_update_tracepoints(void)
  2731. {
  2732. struct module *mod;
  2733. mutex_lock(&module_mutex);
  2734. list_for_each_entry(mod, &modules, list)
  2735. if (!mod->taints)
  2736. tracepoint_update_probe_range(mod->tracepoints,
  2737. mod->tracepoints + mod->num_tracepoints);
  2738. mutex_unlock(&module_mutex);
  2739. }
  2740. /*
  2741. * Returns 0 if current not found.
  2742. * Returns 1 if current found.
  2743. */
  2744. int module_get_iter_tracepoints(struct tracepoint_iter *iter)
  2745. {
  2746. struct module *iter_mod;
  2747. int found = 0;
  2748. mutex_lock(&module_mutex);
  2749. list_for_each_entry(iter_mod, &modules, list) {
  2750. if (!iter_mod->taints) {
  2751. /*
  2752. * Sorted module list
  2753. */
  2754. if (iter_mod < iter->module)
  2755. continue;
  2756. else if (iter_mod > iter->module)
  2757. iter->tracepoint = NULL;
  2758. found = tracepoint_get_iter_range(&iter->tracepoint,
  2759. iter_mod->tracepoints,
  2760. iter_mod->tracepoints
  2761. + iter_mod->num_tracepoints);
  2762. if (found) {
  2763. iter->module = iter_mod;
  2764. break;
  2765. }
  2766. }
  2767. }
  2768. mutex_unlock(&module_mutex);
  2769. return found;
  2770. }
  2771. #endif