module.c 82 KB

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