module.c 82 KB

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