module.c 67 KB

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