module.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  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. /* Final destruction now noone is using it. */
  651. if (mod->exit != NULL) {
  652. mutex_unlock(&module_mutex);
  653. mod->exit();
  654. mutex_lock(&module_mutex);
  655. }
  656. /* Store the name of the last unloaded module for diagnostic purposes */
  657. strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
  658. free_module(mod);
  659. out:
  660. mutex_unlock(&module_mutex);
  661. return ret;
  662. }
  663. static void print_unload_info(struct seq_file *m, struct module *mod)
  664. {
  665. struct module_use *use;
  666. int printed_something = 0;
  667. seq_printf(m, " %u ", module_refcount(mod));
  668. /* Always include a trailing , so userspace can differentiate
  669. between this and the old multi-field proc format. */
  670. list_for_each_entry(use, &mod->modules_which_use_me, list) {
  671. printed_something = 1;
  672. seq_printf(m, "%s,", use->module_which_uses->name);
  673. }
  674. if (mod->init != NULL && mod->exit == NULL) {
  675. printed_something = 1;
  676. seq_printf(m, "[permanent],");
  677. }
  678. if (!printed_something)
  679. seq_printf(m, "-");
  680. }
  681. void __symbol_put(const char *symbol)
  682. {
  683. struct module *owner;
  684. preempt_disable();
  685. if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
  686. BUG();
  687. module_put(owner);
  688. preempt_enable();
  689. }
  690. EXPORT_SYMBOL(__symbol_put);
  691. void symbol_put_addr(void *addr)
  692. {
  693. struct module *modaddr;
  694. if (core_kernel_text((unsigned long)addr))
  695. return;
  696. if (!(modaddr = module_text_address((unsigned long)addr)))
  697. BUG();
  698. module_put(modaddr);
  699. }
  700. EXPORT_SYMBOL_GPL(symbol_put_addr);
  701. static ssize_t show_refcnt(struct module_attribute *mattr,
  702. struct module *mod, char *buffer)
  703. {
  704. return sprintf(buffer, "%u\n", module_refcount(mod));
  705. }
  706. static struct module_attribute refcnt = {
  707. .attr = { .name = "refcnt", .mode = 0444 },
  708. .show = show_refcnt,
  709. };
  710. void module_put(struct module *module)
  711. {
  712. if (module) {
  713. unsigned int cpu = get_cpu();
  714. local_dec(&module->ref[cpu].count);
  715. /* Maybe they're waiting for us to drop reference? */
  716. if (unlikely(!module_is_live(module)))
  717. wake_up_process(module->waiter);
  718. put_cpu();
  719. }
  720. }
  721. EXPORT_SYMBOL(module_put);
  722. #else /* !CONFIG_MODULE_UNLOAD */
  723. static void print_unload_info(struct seq_file *m, struct module *mod)
  724. {
  725. /* We don't know the usage count, or what modules are using. */
  726. seq_printf(m, " - -");
  727. }
  728. static inline void module_unload_free(struct module *mod)
  729. {
  730. }
  731. static inline int use_module(struct module *a, struct module *b)
  732. {
  733. return strong_try_module_get(b) == 0;
  734. }
  735. static inline void module_unload_init(struct module *mod)
  736. {
  737. }
  738. #endif /* CONFIG_MODULE_UNLOAD */
  739. static ssize_t show_initstate(struct module_attribute *mattr,
  740. struct module *mod, char *buffer)
  741. {
  742. const char *state = "unknown";
  743. switch (mod->state) {
  744. case MODULE_STATE_LIVE:
  745. state = "live";
  746. break;
  747. case MODULE_STATE_COMING:
  748. state = "coming";
  749. break;
  750. case MODULE_STATE_GOING:
  751. state = "going";
  752. break;
  753. }
  754. return sprintf(buffer, "%s\n", state);
  755. }
  756. static struct module_attribute initstate = {
  757. .attr = { .name = "initstate", .mode = 0444 },
  758. .show = show_initstate,
  759. };
  760. static struct module_attribute *modinfo_attrs[] = {
  761. &modinfo_version,
  762. &modinfo_srcversion,
  763. &initstate,
  764. #ifdef CONFIG_MODULE_UNLOAD
  765. &refcnt,
  766. #endif
  767. NULL,
  768. };
  769. static const char vermagic[] = VERMAGIC_STRING;
  770. #ifdef CONFIG_MODVERSIONS
  771. static int check_version(Elf_Shdr *sechdrs,
  772. unsigned int versindex,
  773. const char *symname,
  774. struct module *mod,
  775. const unsigned long *crc)
  776. {
  777. unsigned int i, num_versions;
  778. struct modversion_info *versions;
  779. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  780. if (!crc)
  781. return 1;
  782. versions = (void *) sechdrs[versindex].sh_addr;
  783. num_versions = sechdrs[versindex].sh_size
  784. / sizeof(struct modversion_info);
  785. for (i = 0; i < num_versions; i++) {
  786. if (strcmp(versions[i].name, symname) != 0)
  787. continue;
  788. if (versions[i].crc == *crc)
  789. return 1;
  790. printk("%s: disagrees about version of symbol %s\n",
  791. mod->name, symname);
  792. DEBUGP("Found checksum %lX vs module %lX\n",
  793. *crc, versions[i].crc);
  794. return 0;
  795. }
  796. /* Not in module's version table. OK, but that taints the kernel. */
  797. if (!(tainted & TAINT_FORCED_MODULE))
  798. printk("%s: no version for \"%s\" found: kernel tainted.\n",
  799. mod->name, symname);
  800. add_taint_module(mod, TAINT_FORCED_MODULE);
  801. return 1;
  802. }
  803. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  804. unsigned int versindex,
  805. struct module *mod)
  806. {
  807. const unsigned long *crc;
  808. if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
  809. BUG();
  810. return check_version(sechdrs, versindex, "struct_module", mod, crc);
  811. }
  812. /* First part is kernel version, which we ignore. */
  813. static inline int same_magic(const char *amagic, const char *bmagic)
  814. {
  815. amagic += strcspn(amagic, " ");
  816. bmagic += strcspn(bmagic, " ");
  817. return strcmp(amagic, bmagic) == 0;
  818. }
  819. #else
  820. static inline int check_version(Elf_Shdr *sechdrs,
  821. unsigned int versindex,
  822. const char *symname,
  823. struct module *mod,
  824. const unsigned long *crc)
  825. {
  826. return 1;
  827. }
  828. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  829. unsigned int versindex,
  830. struct module *mod)
  831. {
  832. return 1;
  833. }
  834. static inline int same_magic(const char *amagic, const char *bmagic)
  835. {
  836. return strcmp(amagic, bmagic) == 0;
  837. }
  838. #endif /* CONFIG_MODVERSIONS */
  839. /* Resolve a symbol for this module. I.e. if we find one, record usage.
  840. Must be holding module_mutex. */
  841. static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
  842. unsigned int versindex,
  843. const char *name,
  844. struct module *mod)
  845. {
  846. struct module *owner;
  847. unsigned long ret;
  848. const unsigned long *crc;
  849. ret = find_symbol(name, &owner, &crc,
  850. !(mod->taints & TAINT_PROPRIETARY_MODULE), true);
  851. if (!IS_ERR_VALUE(ret)) {
  852. /* use_module can fail due to OOM,
  853. or module initialization or unloading */
  854. if (!check_version(sechdrs, versindex, name, mod, crc) ||
  855. !use_module(mod, owner))
  856. ret = -EINVAL;
  857. }
  858. return ret;
  859. }
  860. /*
  861. * /sys/module/foo/sections stuff
  862. * J. Corbet <corbet@lwn.net>
  863. */
  864. #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
  865. struct module_sect_attr
  866. {
  867. struct module_attribute mattr;
  868. char *name;
  869. unsigned long address;
  870. };
  871. struct module_sect_attrs
  872. {
  873. struct attribute_group grp;
  874. unsigned int nsections;
  875. struct module_sect_attr attrs[0];
  876. };
  877. static ssize_t module_sect_show(struct module_attribute *mattr,
  878. struct module *mod, char *buf)
  879. {
  880. struct module_sect_attr *sattr =
  881. container_of(mattr, struct module_sect_attr, mattr);
  882. return sprintf(buf, "0x%lx\n", sattr->address);
  883. }
  884. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  885. {
  886. unsigned int section;
  887. for (section = 0; section < sect_attrs->nsections; section++)
  888. kfree(sect_attrs->attrs[section].name);
  889. kfree(sect_attrs);
  890. }
  891. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  892. char *secstrings, Elf_Shdr *sechdrs)
  893. {
  894. unsigned int nloaded = 0, i, size[2];
  895. struct module_sect_attrs *sect_attrs;
  896. struct module_sect_attr *sattr;
  897. struct attribute **gattr;
  898. /* Count loaded sections and allocate structures */
  899. for (i = 0; i < nsect; i++)
  900. if (sechdrs[i].sh_flags & SHF_ALLOC)
  901. nloaded++;
  902. size[0] = ALIGN(sizeof(*sect_attrs)
  903. + nloaded * sizeof(sect_attrs->attrs[0]),
  904. sizeof(sect_attrs->grp.attrs[0]));
  905. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  906. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  907. if (sect_attrs == NULL)
  908. return;
  909. /* Setup section attributes. */
  910. sect_attrs->grp.name = "sections";
  911. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  912. sect_attrs->nsections = 0;
  913. sattr = &sect_attrs->attrs[0];
  914. gattr = &sect_attrs->grp.attrs[0];
  915. for (i = 0; i < nsect; i++) {
  916. if (! (sechdrs[i].sh_flags & SHF_ALLOC))
  917. continue;
  918. sattr->address = sechdrs[i].sh_addr;
  919. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  920. GFP_KERNEL);
  921. if (sattr->name == NULL)
  922. goto out;
  923. sect_attrs->nsections++;
  924. sattr->mattr.show = module_sect_show;
  925. sattr->mattr.store = NULL;
  926. sattr->mattr.attr.name = sattr->name;
  927. sattr->mattr.attr.mode = S_IRUGO;
  928. *(gattr++) = &(sattr++)->mattr.attr;
  929. }
  930. *gattr = NULL;
  931. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  932. goto out;
  933. mod->sect_attrs = sect_attrs;
  934. return;
  935. out:
  936. free_sect_attrs(sect_attrs);
  937. }
  938. static void remove_sect_attrs(struct module *mod)
  939. {
  940. if (mod->sect_attrs) {
  941. sysfs_remove_group(&mod->mkobj.kobj,
  942. &mod->sect_attrs->grp);
  943. /* We are positive that no one is using any sect attrs
  944. * at this point. Deallocate immediately. */
  945. free_sect_attrs(mod->sect_attrs);
  946. mod->sect_attrs = NULL;
  947. }
  948. }
  949. /*
  950. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  951. */
  952. struct module_notes_attrs {
  953. struct kobject *dir;
  954. unsigned int notes;
  955. struct bin_attribute attrs[0];
  956. };
  957. static ssize_t module_notes_read(struct kobject *kobj,
  958. struct bin_attribute *bin_attr,
  959. char *buf, loff_t pos, size_t count)
  960. {
  961. /*
  962. * The caller checked the pos and count against our size.
  963. */
  964. memcpy(buf, bin_attr->private + pos, count);
  965. return count;
  966. }
  967. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  968. unsigned int i)
  969. {
  970. if (notes_attrs->dir) {
  971. while (i-- > 0)
  972. sysfs_remove_bin_file(notes_attrs->dir,
  973. &notes_attrs->attrs[i]);
  974. kobject_del(notes_attrs->dir);
  975. }
  976. kfree(notes_attrs);
  977. }
  978. static void add_notes_attrs(struct module *mod, unsigned int nsect,
  979. char *secstrings, Elf_Shdr *sechdrs)
  980. {
  981. unsigned int notes, loaded, i;
  982. struct module_notes_attrs *notes_attrs;
  983. struct bin_attribute *nattr;
  984. /* Count notes sections and allocate structures. */
  985. notes = 0;
  986. for (i = 0; i < nsect; i++)
  987. if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
  988. (sechdrs[i].sh_type == SHT_NOTE))
  989. ++notes;
  990. if (notes == 0)
  991. return;
  992. notes_attrs = kzalloc(sizeof(*notes_attrs)
  993. + notes * sizeof(notes_attrs->attrs[0]),
  994. GFP_KERNEL);
  995. if (notes_attrs == NULL)
  996. return;
  997. notes_attrs->notes = notes;
  998. nattr = &notes_attrs->attrs[0];
  999. for (loaded = i = 0; i < nsect; ++i) {
  1000. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1001. continue;
  1002. if (sechdrs[i].sh_type == SHT_NOTE) {
  1003. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  1004. nattr->attr.mode = S_IRUGO;
  1005. nattr->size = sechdrs[i].sh_size;
  1006. nattr->private = (void *) sechdrs[i].sh_addr;
  1007. nattr->read = module_notes_read;
  1008. ++nattr;
  1009. }
  1010. ++loaded;
  1011. }
  1012. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  1013. if (!notes_attrs->dir)
  1014. goto out;
  1015. for (i = 0; i < notes; ++i)
  1016. if (sysfs_create_bin_file(notes_attrs->dir,
  1017. &notes_attrs->attrs[i]))
  1018. goto out;
  1019. mod->notes_attrs = notes_attrs;
  1020. return;
  1021. out:
  1022. free_notes_attrs(notes_attrs, i);
  1023. }
  1024. static void remove_notes_attrs(struct module *mod)
  1025. {
  1026. if (mod->notes_attrs)
  1027. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1028. }
  1029. #else
  1030. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  1031. char *sectstrings, Elf_Shdr *sechdrs)
  1032. {
  1033. }
  1034. static inline void remove_sect_attrs(struct module *mod)
  1035. {
  1036. }
  1037. static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
  1038. char *sectstrings, Elf_Shdr *sechdrs)
  1039. {
  1040. }
  1041. static inline void remove_notes_attrs(struct module *mod)
  1042. {
  1043. }
  1044. #endif
  1045. #ifdef CONFIG_SYSFS
  1046. int module_add_modinfo_attrs(struct module *mod)
  1047. {
  1048. struct module_attribute *attr;
  1049. struct module_attribute *temp_attr;
  1050. int error = 0;
  1051. int i;
  1052. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1053. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1054. GFP_KERNEL);
  1055. if (!mod->modinfo_attrs)
  1056. return -ENOMEM;
  1057. temp_attr = mod->modinfo_attrs;
  1058. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1059. if (!attr->test ||
  1060. (attr->test && attr->test(mod))) {
  1061. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1062. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  1063. ++temp_attr;
  1064. }
  1065. }
  1066. return error;
  1067. }
  1068. void module_remove_modinfo_attrs(struct module *mod)
  1069. {
  1070. struct module_attribute *attr;
  1071. int i;
  1072. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1073. /* pick a field to test for end of list */
  1074. if (!attr->attr.name)
  1075. break;
  1076. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  1077. if (attr->free)
  1078. attr->free(mod);
  1079. }
  1080. kfree(mod->modinfo_attrs);
  1081. }
  1082. int mod_sysfs_init(struct module *mod)
  1083. {
  1084. int err;
  1085. struct kobject *kobj;
  1086. if (!module_sysfs_initialized) {
  1087. printk(KERN_ERR "%s: module sysfs not initialized\n",
  1088. mod->name);
  1089. err = -EINVAL;
  1090. goto out;
  1091. }
  1092. kobj = kset_find_obj(module_kset, mod->name);
  1093. if (kobj) {
  1094. printk(KERN_ERR "%s: module is already loaded\n", mod->name);
  1095. kobject_put(kobj);
  1096. err = -EINVAL;
  1097. goto out;
  1098. }
  1099. mod->mkobj.mod = mod;
  1100. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1101. mod->mkobj.kobj.kset = module_kset;
  1102. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1103. "%s", mod->name);
  1104. if (err)
  1105. kobject_put(&mod->mkobj.kobj);
  1106. /* delay uevent until full sysfs population */
  1107. out:
  1108. return err;
  1109. }
  1110. int mod_sysfs_setup(struct module *mod,
  1111. struct kernel_param *kparam,
  1112. unsigned int num_params)
  1113. {
  1114. int err;
  1115. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1116. if (!mod->holders_dir) {
  1117. err = -ENOMEM;
  1118. goto out_unreg;
  1119. }
  1120. err = module_param_sysfs_setup(mod, kparam, num_params);
  1121. if (err)
  1122. goto out_unreg_holders;
  1123. err = module_add_modinfo_attrs(mod);
  1124. if (err)
  1125. goto out_unreg_param;
  1126. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1127. return 0;
  1128. out_unreg_param:
  1129. module_param_sysfs_remove(mod);
  1130. out_unreg_holders:
  1131. kobject_put(mod->holders_dir);
  1132. out_unreg:
  1133. kobject_put(&mod->mkobj.kobj);
  1134. return err;
  1135. }
  1136. #endif
  1137. static void mod_kobject_remove(struct module *mod)
  1138. {
  1139. module_remove_modinfo_attrs(mod);
  1140. module_param_sysfs_remove(mod);
  1141. kobject_put(mod->mkobj.drivers_dir);
  1142. kobject_put(mod->holders_dir);
  1143. kobject_put(&mod->mkobj.kobj);
  1144. }
  1145. /*
  1146. * link the module with the whole machine is stopped with interrupts off
  1147. * - this defends against kallsyms not taking locks
  1148. */
  1149. static int __link_module(void *_mod)
  1150. {
  1151. struct module *mod = _mod;
  1152. list_add(&mod->list, &modules);
  1153. return 0;
  1154. }
  1155. /*
  1156. * unlink the module with the whole machine is stopped with interrupts off
  1157. * - this defends against kallsyms not taking locks
  1158. */
  1159. static int __unlink_module(void *_mod)
  1160. {
  1161. struct module *mod = _mod;
  1162. list_del(&mod->list);
  1163. return 0;
  1164. }
  1165. /* Free a module, remove from lists, etc (must hold module_mutex). */
  1166. static void free_module(struct module *mod)
  1167. {
  1168. /* Delete from various lists */
  1169. stop_machine_run(__unlink_module, mod, NR_CPUS);
  1170. remove_notes_attrs(mod);
  1171. remove_sect_attrs(mod);
  1172. mod_kobject_remove(mod);
  1173. unwind_remove_table(mod->unwind_info, 0);
  1174. /* Arch-specific cleanup. */
  1175. module_arch_cleanup(mod);
  1176. /* Module unload stuff */
  1177. module_unload_free(mod);
  1178. /* This may be NULL, but that's OK */
  1179. module_free(mod, mod->module_init);
  1180. kfree(mod->args);
  1181. if (mod->percpu)
  1182. percpu_modfree(mod->percpu);
  1183. /* Free lock-classes: */
  1184. lockdep_free_key_range(mod->module_core, mod->core_size);
  1185. /* Finally, free the core (containing the module structure) */
  1186. module_free(mod, mod->module_core);
  1187. }
  1188. void *__symbol_get(const char *symbol)
  1189. {
  1190. struct module *owner;
  1191. unsigned long value;
  1192. preempt_disable();
  1193. value = find_symbol(symbol, &owner, NULL, true, true);
  1194. if (IS_ERR_VALUE(value))
  1195. value = 0;
  1196. else if (strong_try_module_get(owner))
  1197. value = 0;
  1198. preempt_enable();
  1199. return (void *)value;
  1200. }
  1201. EXPORT_SYMBOL_GPL(__symbol_get);
  1202. /*
  1203. * Ensure that an exported symbol [global namespace] does not already exist
  1204. * in the kernel or in some other module's exported symbol table.
  1205. */
  1206. static int verify_export_symbols(struct module *mod)
  1207. {
  1208. unsigned int i;
  1209. struct module *owner;
  1210. const struct kernel_symbol *s;
  1211. struct {
  1212. const struct kernel_symbol *sym;
  1213. unsigned int num;
  1214. } arr[] = {
  1215. { mod->syms, mod->num_syms },
  1216. { mod->gpl_syms, mod->num_gpl_syms },
  1217. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1218. { mod->unused_syms, mod->num_unused_syms },
  1219. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1220. };
  1221. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1222. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1223. if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
  1224. NULL, true, false))) {
  1225. printk(KERN_ERR
  1226. "%s: exports duplicate symbol %s"
  1227. " (owned by %s)\n",
  1228. mod->name, s->name, module_name(owner));
  1229. return -ENOEXEC;
  1230. }
  1231. }
  1232. }
  1233. return 0;
  1234. }
  1235. /* Change all symbols so that st_value encodes the pointer directly. */
  1236. static int simplify_symbols(Elf_Shdr *sechdrs,
  1237. unsigned int symindex,
  1238. const char *strtab,
  1239. unsigned int versindex,
  1240. unsigned int pcpuindex,
  1241. struct module *mod)
  1242. {
  1243. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1244. unsigned long secbase;
  1245. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1246. int ret = 0;
  1247. for (i = 1; i < n; i++) {
  1248. switch (sym[i].st_shndx) {
  1249. case SHN_COMMON:
  1250. /* We compiled with -fno-common. These are not
  1251. supposed to happen. */
  1252. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1253. printk("%s: please compile with -fno-common\n",
  1254. mod->name);
  1255. ret = -ENOEXEC;
  1256. break;
  1257. case SHN_ABS:
  1258. /* Don't need to do anything */
  1259. DEBUGP("Absolute symbol: 0x%08lx\n",
  1260. (long)sym[i].st_value);
  1261. break;
  1262. case SHN_UNDEF:
  1263. sym[i].st_value
  1264. = resolve_symbol(sechdrs, versindex,
  1265. strtab + sym[i].st_name, mod);
  1266. /* Ok if resolved. */
  1267. if (!IS_ERR_VALUE(sym[i].st_value))
  1268. break;
  1269. /* Ok if weak. */
  1270. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1271. break;
  1272. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1273. mod->name, strtab + sym[i].st_name);
  1274. ret = -ENOENT;
  1275. break;
  1276. default:
  1277. /* Divert to percpu allocation if a percpu var. */
  1278. if (sym[i].st_shndx == pcpuindex)
  1279. secbase = (unsigned long)mod->percpu;
  1280. else
  1281. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1282. sym[i].st_value += secbase;
  1283. break;
  1284. }
  1285. }
  1286. return ret;
  1287. }
  1288. /* Update size with this section: return offset. */
  1289. static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
  1290. {
  1291. long ret;
  1292. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1293. *size = ret + sechdr->sh_size;
  1294. return ret;
  1295. }
  1296. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1297. might -- code, read-only data, read-write data, small data. Tally
  1298. sizes, and place the offsets into sh_entsize fields: high bit means it
  1299. belongs in init. */
  1300. static void layout_sections(struct module *mod,
  1301. const Elf_Ehdr *hdr,
  1302. Elf_Shdr *sechdrs,
  1303. const char *secstrings)
  1304. {
  1305. static unsigned long const masks[][2] = {
  1306. /* NOTE: all executable code must be the first section
  1307. * in this array; otherwise modify the text_size
  1308. * finder in the two loops below */
  1309. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1310. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1311. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1312. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1313. };
  1314. unsigned int m, i;
  1315. for (i = 0; i < hdr->e_shnum; i++)
  1316. sechdrs[i].sh_entsize = ~0UL;
  1317. DEBUGP("Core section allocation order:\n");
  1318. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1319. for (i = 0; i < hdr->e_shnum; ++i) {
  1320. Elf_Shdr *s = &sechdrs[i];
  1321. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1322. || (s->sh_flags & masks[m][1])
  1323. || s->sh_entsize != ~0UL
  1324. || strncmp(secstrings + s->sh_name,
  1325. ".init", 5) == 0)
  1326. continue;
  1327. s->sh_entsize = get_offset(&mod->core_size, s);
  1328. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1329. }
  1330. if (m == 0)
  1331. mod->core_text_size = mod->core_size;
  1332. }
  1333. DEBUGP("Init section allocation order:\n");
  1334. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1335. for (i = 0; i < hdr->e_shnum; ++i) {
  1336. Elf_Shdr *s = &sechdrs[i];
  1337. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1338. || (s->sh_flags & masks[m][1])
  1339. || s->sh_entsize != ~0UL
  1340. || strncmp(secstrings + s->sh_name,
  1341. ".init", 5) != 0)
  1342. continue;
  1343. s->sh_entsize = (get_offset(&mod->init_size, s)
  1344. | INIT_OFFSET_MASK);
  1345. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1346. }
  1347. if (m == 0)
  1348. mod->init_text_size = mod->init_size;
  1349. }
  1350. }
  1351. static void set_license(struct module *mod, const char *license)
  1352. {
  1353. if (!license)
  1354. license = "unspecified";
  1355. if (!license_is_gpl_compatible(license)) {
  1356. if (!(tainted & TAINT_PROPRIETARY_MODULE))
  1357. printk(KERN_WARNING "%s: module license '%s' taints "
  1358. "kernel.\n", mod->name, license);
  1359. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1360. }
  1361. }
  1362. /* Parse tag=value strings from .modinfo section */
  1363. static char *next_string(char *string, unsigned long *secsize)
  1364. {
  1365. /* Skip non-zero chars */
  1366. while (string[0]) {
  1367. string++;
  1368. if ((*secsize)-- <= 1)
  1369. return NULL;
  1370. }
  1371. /* Skip any zero padding. */
  1372. while (!string[0]) {
  1373. string++;
  1374. if ((*secsize)-- <= 1)
  1375. return NULL;
  1376. }
  1377. return string;
  1378. }
  1379. static char *get_modinfo(Elf_Shdr *sechdrs,
  1380. unsigned int info,
  1381. const char *tag)
  1382. {
  1383. char *p;
  1384. unsigned int taglen = strlen(tag);
  1385. unsigned long size = sechdrs[info].sh_size;
  1386. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1387. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1388. return p + taglen + 1;
  1389. }
  1390. return NULL;
  1391. }
  1392. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1393. unsigned int infoindex)
  1394. {
  1395. struct module_attribute *attr;
  1396. int i;
  1397. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1398. if (attr->setup)
  1399. attr->setup(mod,
  1400. get_modinfo(sechdrs,
  1401. infoindex,
  1402. attr->attr.name));
  1403. }
  1404. }
  1405. #ifdef CONFIG_KALLSYMS
  1406. static int is_exported(const char *name, const struct module *mod)
  1407. {
  1408. if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
  1409. return 1;
  1410. else
  1411. if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
  1412. return 1;
  1413. else
  1414. return 0;
  1415. }
  1416. /* As per nm */
  1417. static char elf_type(const Elf_Sym *sym,
  1418. Elf_Shdr *sechdrs,
  1419. const char *secstrings,
  1420. struct module *mod)
  1421. {
  1422. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1423. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1424. return 'v';
  1425. else
  1426. return 'w';
  1427. }
  1428. if (sym->st_shndx == SHN_UNDEF)
  1429. return 'U';
  1430. if (sym->st_shndx == SHN_ABS)
  1431. return 'a';
  1432. if (sym->st_shndx >= SHN_LORESERVE)
  1433. return '?';
  1434. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1435. return 't';
  1436. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1437. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1438. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1439. return 'r';
  1440. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1441. return 'g';
  1442. else
  1443. return 'd';
  1444. }
  1445. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1446. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1447. return 's';
  1448. else
  1449. return 'b';
  1450. }
  1451. if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
  1452. ".debug", strlen(".debug")) == 0)
  1453. return 'n';
  1454. return '?';
  1455. }
  1456. static void add_kallsyms(struct module *mod,
  1457. Elf_Shdr *sechdrs,
  1458. unsigned int symindex,
  1459. unsigned int strindex,
  1460. const char *secstrings)
  1461. {
  1462. unsigned int i;
  1463. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1464. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1465. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1466. /* Set types up while we still have access to sections. */
  1467. for (i = 0; i < mod->num_symtab; i++)
  1468. mod->symtab[i].st_info
  1469. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1470. }
  1471. #else
  1472. static inline void add_kallsyms(struct module *mod,
  1473. Elf_Shdr *sechdrs,
  1474. unsigned int symindex,
  1475. unsigned int strindex,
  1476. const char *secstrings)
  1477. {
  1478. }
  1479. #endif /* CONFIG_KALLSYMS */
  1480. /* Allocate and load the module: note that size of section 0 is always
  1481. zero, and we rely on this for optional sections. */
  1482. static struct module *load_module(void __user *umod,
  1483. unsigned long len,
  1484. const char __user *uargs)
  1485. {
  1486. Elf_Ehdr *hdr;
  1487. Elf_Shdr *sechdrs;
  1488. char *secstrings, *args, *modmagic, *strtab = NULL;
  1489. unsigned int i;
  1490. unsigned int symindex = 0;
  1491. unsigned int strindex = 0;
  1492. unsigned int setupindex;
  1493. unsigned int exindex;
  1494. unsigned int exportindex;
  1495. unsigned int modindex;
  1496. unsigned int obsparmindex;
  1497. unsigned int infoindex;
  1498. unsigned int gplindex;
  1499. unsigned int crcindex;
  1500. unsigned int gplcrcindex;
  1501. unsigned int versindex;
  1502. unsigned int pcpuindex;
  1503. unsigned int gplfutureindex;
  1504. unsigned int gplfuturecrcindex;
  1505. unsigned int unwindex = 0;
  1506. unsigned int unusedindex;
  1507. unsigned int unusedcrcindex;
  1508. unsigned int unusedgplindex;
  1509. unsigned int unusedgplcrcindex;
  1510. unsigned int markersindex;
  1511. unsigned int markersstringsindex;
  1512. struct module *mod;
  1513. long err = 0;
  1514. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1515. struct exception_table_entry *extable;
  1516. mm_segment_t old_fs;
  1517. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1518. umod, len, uargs);
  1519. if (len < sizeof(*hdr))
  1520. return ERR_PTR(-ENOEXEC);
  1521. /* Suck in entire file: we'll want most of it. */
  1522. /* vmalloc barfs on "unusual" numbers. Check here */
  1523. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1524. return ERR_PTR(-ENOMEM);
  1525. if (copy_from_user(hdr, umod, len) != 0) {
  1526. err = -EFAULT;
  1527. goto free_hdr;
  1528. }
  1529. /* Sanity checks against insmoding binaries or wrong arch,
  1530. weird elf version */
  1531. if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
  1532. || hdr->e_type != ET_REL
  1533. || !elf_check_arch(hdr)
  1534. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1535. err = -ENOEXEC;
  1536. goto free_hdr;
  1537. }
  1538. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1539. goto truncated;
  1540. /* Convenience variables */
  1541. sechdrs = (void *)hdr + hdr->e_shoff;
  1542. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1543. sechdrs[0].sh_addr = 0;
  1544. for (i = 1; i < hdr->e_shnum; i++) {
  1545. if (sechdrs[i].sh_type != SHT_NOBITS
  1546. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1547. goto truncated;
  1548. /* Mark all sections sh_addr with their address in the
  1549. temporary image. */
  1550. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1551. /* Internal symbols and strings. */
  1552. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1553. symindex = i;
  1554. strindex = sechdrs[i].sh_link;
  1555. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1556. }
  1557. #ifndef CONFIG_MODULE_UNLOAD
  1558. /* Don't load .exit sections */
  1559. if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
  1560. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1561. #endif
  1562. }
  1563. modindex = find_sec(hdr, sechdrs, secstrings,
  1564. ".gnu.linkonce.this_module");
  1565. if (!modindex) {
  1566. printk(KERN_WARNING "No module found in object\n");
  1567. err = -ENOEXEC;
  1568. goto free_hdr;
  1569. }
  1570. mod = (void *)sechdrs[modindex].sh_addr;
  1571. if (symindex == 0) {
  1572. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1573. mod->name);
  1574. err = -ENOEXEC;
  1575. goto free_hdr;
  1576. }
  1577. /* Optional sections */
  1578. exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
  1579. gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
  1580. gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
  1581. unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
  1582. unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
  1583. crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
  1584. gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1585. gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
  1586. unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
  1587. unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
  1588. setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
  1589. exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
  1590. obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
  1591. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1592. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1593. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1594. #ifdef ARCH_UNWIND_SECTION_NAME
  1595. unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
  1596. #endif
  1597. /* Don't keep modinfo and version sections. */
  1598. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1599. sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1600. #ifdef CONFIG_KALLSYMS
  1601. /* Keep symbol and string tables for decoding later. */
  1602. sechdrs[symindex].sh_flags |= SHF_ALLOC;
  1603. sechdrs[strindex].sh_flags |= SHF_ALLOC;
  1604. #endif
  1605. if (unwindex)
  1606. sechdrs[unwindex].sh_flags |= SHF_ALLOC;
  1607. /* Check module struct version now, before we try to use module. */
  1608. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1609. err = -ENOEXEC;
  1610. goto free_hdr;
  1611. }
  1612. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1613. /* This is allowed: modprobe --force will invalidate it. */
  1614. if (!modmagic) {
  1615. add_taint_module(mod, TAINT_FORCED_MODULE);
  1616. printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
  1617. mod->name);
  1618. } else if (!same_magic(modmagic, vermagic)) {
  1619. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1620. mod->name, modmagic, vermagic);
  1621. err = -ENOEXEC;
  1622. goto free_hdr;
  1623. }
  1624. /* Now copy in args */
  1625. args = strndup_user(uargs, ~0UL >> 1);
  1626. if (IS_ERR(args)) {
  1627. err = PTR_ERR(args);
  1628. goto free_hdr;
  1629. }
  1630. if (find_module(mod->name)) {
  1631. err = -EEXIST;
  1632. goto free_mod;
  1633. }
  1634. mod->state = MODULE_STATE_COMING;
  1635. /* Allow arches to frob section contents and sizes. */
  1636. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1637. if (err < 0)
  1638. goto free_mod;
  1639. if (pcpuindex) {
  1640. /* We have a special allocation for this section. */
  1641. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1642. sechdrs[pcpuindex].sh_addralign,
  1643. mod->name);
  1644. if (!percpu) {
  1645. err = -ENOMEM;
  1646. goto free_mod;
  1647. }
  1648. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1649. mod->percpu = percpu;
  1650. }
  1651. /* Determine total sizes, and put offsets in sh_entsize. For now
  1652. this is done generically; there doesn't appear to be any
  1653. special cases for the architectures. */
  1654. layout_sections(mod, hdr, sechdrs, secstrings);
  1655. /* Do the allocs. */
  1656. ptr = module_alloc(mod->core_size);
  1657. if (!ptr) {
  1658. err = -ENOMEM;
  1659. goto free_percpu;
  1660. }
  1661. memset(ptr, 0, mod->core_size);
  1662. mod->module_core = ptr;
  1663. ptr = module_alloc(mod->init_size);
  1664. if (!ptr && mod->init_size) {
  1665. err = -ENOMEM;
  1666. goto free_core;
  1667. }
  1668. memset(ptr, 0, mod->init_size);
  1669. mod->module_init = ptr;
  1670. /* Transfer each section which specifies SHF_ALLOC */
  1671. DEBUGP("final section addresses:\n");
  1672. for (i = 0; i < hdr->e_shnum; i++) {
  1673. void *dest;
  1674. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1675. continue;
  1676. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1677. dest = mod->module_init
  1678. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1679. else
  1680. dest = mod->module_core + sechdrs[i].sh_entsize;
  1681. if (sechdrs[i].sh_type != SHT_NOBITS)
  1682. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1683. sechdrs[i].sh_size);
  1684. /* Update sh_addr to point to copy in image. */
  1685. sechdrs[i].sh_addr = (unsigned long)dest;
  1686. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1687. }
  1688. /* Module has been moved. */
  1689. mod = (void *)sechdrs[modindex].sh_addr;
  1690. /* Now we've moved module, initialize linked lists, etc. */
  1691. module_unload_init(mod);
  1692. /* add kobject, so we can reference it. */
  1693. err = mod_sysfs_init(mod);
  1694. if (err)
  1695. goto free_unload;
  1696. /* Set up license info based on the info section */
  1697. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1698. /*
  1699. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  1700. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  1701. * using GPL-only symbols it needs.
  1702. */
  1703. if (strcmp(mod->name, "ndiswrapper") == 0)
  1704. add_taint(TAINT_PROPRIETARY_MODULE);
  1705. /* driverloader was caught wrongly pretending to be under GPL */
  1706. if (strcmp(mod->name, "driverloader") == 0)
  1707. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1708. /* Set up MODINFO_ATTR fields */
  1709. setup_modinfo(mod, sechdrs, infoindex);
  1710. /* Fix up syms, so that st_value is a pointer to location. */
  1711. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1712. mod);
  1713. if (err < 0)
  1714. goto cleanup;
  1715. /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
  1716. mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
  1717. mod->syms = (void *)sechdrs[exportindex].sh_addr;
  1718. if (crcindex)
  1719. mod->crcs = (void *)sechdrs[crcindex].sh_addr;
  1720. mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
  1721. mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
  1722. if (gplcrcindex)
  1723. mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
  1724. mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
  1725. sizeof(*mod->gpl_future_syms);
  1726. mod->num_unused_syms = sechdrs[unusedindex].sh_size /
  1727. sizeof(*mod->unused_syms);
  1728. mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
  1729. sizeof(*mod->unused_gpl_syms);
  1730. mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
  1731. if (gplfuturecrcindex)
  1732. mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
  1733. mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
  1734. if (unusedcrcindex)
  1735. mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
  1736. mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
  1737. if (unusedgplcrcindex)
  1738. mod->unused_gpl_crcs
  1739. = (void *)sechdrs[unusedgplcrcindex].sh_addr;
  1740. #ifdef CONFIG_MODVERSIONS
  1741. if ((mod->num_syms && !crcindex) ||
  1742. (mod->num_gpl_syms && !gplcrcindex) ||
  1743. (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
  1744. (mod->num_unused_syms && !unusedcrcindex) ||
  1745. (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
  1746. printk(KERN_WARNING "%s: No versions for exported symbols."
  1747. " Tainting kernel.\n", mod->name);
  1748. add_taint_module(mod, TAINT_FORCED_MODULE);
  1749. }
  1750. #endif
  1751. markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
  1752. markersstringsindex = find_sec(hdr, sechdrs, secstrings,
  1753. "__markers_strings");
  1754. /* Now do relocations. */
  1755. for (i = 1; i < hdr->e_shnum; i++) {
  1756. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1757. unsigned int info = sechdrs[i].sh_info;
  1758. /* Not a valid relocation section? */
  1759. if (info >= hdr->e_shnum)
  1760. continue;
  1761. /* Don't bother with non-allocated sections */
  1762. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1763. continue;
  1764. if (sechdrs[i].sh_type == SHT_REL)
  1765. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1766. else if (sechdrs[i].sh_type == SHT_RELA)
  1767. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1768. mod);
  1769. if (err < 0)
  1770. goto cleanup;
  1771. }
  1772. #ifdef CONFIG_MARKERS
  1773. mod->markers = (void *)sechdrs[markersindex].sh_addr;
  1774. mod->num_markers =
  1775. sechdrs[markersindex].sh_size / sizeof(*mod->markers);
  1776. #endif
  1777. /* Find duplicate symbols */
  1778. err = verify_export_symbols(mod);
  1779. if (err < 0)
  1780. goto cleanup;
  1781. /* Set up and sort exception table */
  1782. mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
  1783. mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
  1784. sort_extable(extable, extable + mod->num_exentries);
  1785. /* Finally, copy percpu area over. */
  1786. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1787. sechdrs[pcpuindex].sh_size);
  1788. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1789. #ifdef CONFIG_MARKERS
  1790. if (!mod->taints)
  1791. marker_update_probe_range(mod->markers,
  1792. mod->markers + mod->num_markers);
  1793. #endif
  1794. err = module_finalize(hdr, sechdrs, mod);
  1795. if (err < 0)
  1796. goto cleanup;
  1797. /* flush the icache in correct context */
  1798. old_fs = get_fs();
  1799. set_fs(KERNEL_DS);
  1800. /*
  1801. * Flush the instruction cache, since we've played with text.
  1802. * Do it before processing of module parameters, so the module
  1803. * can provide parameter accessor functions of its own.
  1804. */
  1805. if (mod->module_init)
  1806. flush_icache_range((unsigned long)mod->module_init,
  1807. (unsigned long)mod->module_init
  1808. + mod->init_size);
  1809. flush_icache_range((unsigned long)mod->module_core,
  1810. (unsigned long)mod->module_core + mod->core_size);
  1811. set_fs(old_fs);
  1812. mod->args = args;
  1813. if (obsparmindex)
  1814. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1815. mod->name);
  1816. /* Now sew it into the lists so we can get lockdep and oops
  1817. * info during argument parsing. Noone should access us, since
  1818. * strong_try_module_get() will fail. */
  1819. stop_machine_run(__link_module, mod, NR_CPUS);
  1820. /* Size of section 0 is 0, so this works well if no params */
  1821. err = parse_args(mod->name, mod->args,
  1822. (struct kernel_param *)
  1823. sechdrs[setupindex].sh_addr,
  1824. sechdrs[setupindex].sh_size
  1825. / sizeof(struct kernel_param),
  1826. NULL);
  1827. if (err < 0)
  1828. goto unlink;
  1829. err = mod_sysfs_setup(mod,
  1830. (struct kernel_param *)
  1831. sechdrs[setupindex].sh_addr,
  1832. sechdrs[setupindex].sh_size
  1833. / sizeof(struct kernel_param));
  1834. if (err < 0)
  1835. goto unlink;
  1836. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1837. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1838. /* Size of section 0 is 0, so this works well if no unwind info. */
  1839. mod->unwind_info = unwind_add_table(mod,
  1840. (void *)sechdrs[unwindex].sh_addr,
  1841. sechdrs[unwindex].sh_size);
  1842. /* Get rid of temporary copy */
  1843. vfree(hdr);
  1844. /* Done! */
  1845. return mod;
  1846. unlink:
  1847. stop_machine_run(__unlink_module, mod, NR_CPUS);
  1848. module_arch_cleanup(mod);
  1849. cleanup:
  1850. kobject_del(&mod->mkobj.kobj);
  1851. kobject_put(&mod->mkobj.kobj);
  1852. free_unload:
  1853. module_unload_free(mod);
  1854. module_free(mod, mod->module_init);
  1855. free_core:
  1856. module_free(mod, mod->module_core);
  1857. free_percpu:
  1858. if (percpu)
  1859. percpu_modfree(percpu);
  1860. free_mod:
  1861. kfree(args);
  1862. free_hdr:
  1863. vfree(hdr);
  1864. return ERR_PTR(err);
  1865. truncated:
  1866. printk(KERN_ERR "Module len %lu truncated\n", len);
  1867. err = -ENOEXEC;
  1868. goto free_hdr;
  1869. }
  1870. /* This is where the real work happens */
  1871. asmlinkage long
  1872. sys_init_module(void __user *umod,
  1873. unsigned long len,
  1874. const char __user *uargs)
  1875. {
  1876. struct module *mod;
  1877. int ret = 0;
  1878. /* Must have permission */
  1879. if (!capable(CAP_SYS_MODULE))
  1880. return -EPERM;
  1881. /* Only one module load at a time, please */
  1882. if (mutex_lock_interruptible(&module_mutex) != 0)
  1883. return -EINTR;
  1884. /* Do all the hard work */
  1885. mod = load_module(umod, len, uargs);
  1886. if (IS_ERR(mod)) {
  1887. mutex_unlock(&module_mutex);
  1888. return PTR_ERR(mod);
  1889. }
  1890. /* Drop lock so they can recurse */
  1891. mutex_unlock(&module_mutex);
  1892. blocking_notifier_call_chain(&module_notify_list,
  1893. MODULE_STATE_COMING, mod);
  1894. /* Start the module */
  1895. if (mod->init != NULL)
  1896. ret = mod->init();
  1897. if (ret < 0) {
  1898. /* Init routine failed: abort. Try to protect us from
  1899. buggy refcounters. */
  1900. mod->state = MODULE_STATE_GOING;
  1901. synchronize_sched();
  1902. module_put(mod);
  1903. mutex_lock(&module_mutex);
  1904. free_module(mod);
  1905. mutex_unlock(&module_mutex);
  1906. wake_up(&module_wq);
  1907. return ret;
  1908. }
  1909. if (ret > 0) {
  1910. printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
  1911. "it should follow 0/-E convention\n"
  1912. KERN_WARNING "%s: loading module anyway...\n",
  1913. __func__, mod->name, ret,
  1914. __func__);
  1915. dump_stack();
  1916. }
  1917. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  1918. mod->state = MODULE_STATE_LIVE;
  1919. wake_up(&module_wq);
  1920. mutex_lock(&module_mutex);
  1921. /* Drop initial reference. */
  1922. module_put(mod);
  1923. unwind_remove_table(mod->unwind_info, 1);
  1924. module_free(mod, mod->module_init);
  1925. mod->module_init = NULL;
  1926. mod->init_size = 0;
  1927. mod->init_text_size = 0;
  1928. mutex_unlock(&module_mutex);
  1929. return 0;
  1930. }
  1931. static inline int within(unsigned long addr, void *start, unsigned long size)
  1932. {
  1933. return ((void *)addr >= start && (void *)addr < start + size);
  1934. }
  1935. #ifdef CONFIG_KALLSYMS
  1936. /*
  1937. * This ignores the intensely annoying "mapping symbols" found
  1938. * in ARM ELF files: $a, $t and $d.
  1939. */
  1940. static inline int is_arm_mapping_symbol(const char *str)
  1941. {
  1942. return str[0] == '$' && strchr("atd", str[1])
  1943. && (str[2] == '\0' || str[2] == '.');
  1944. }
  1945. static const char *get_ksymbol(struct module *mod,
  1946. unsigned long addr,
  1947. unsigned long *size,
  1948. unsigned long *offset)
  1949. {
  1950. unsigned int i, best = 0;
  1951. unsigned long nextval;
  1952. /* At worse, next value is at end of module */
  1953. if (within(addr, mod->module_init, mod->init_size))
  1954. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  1955. else
  1956. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  1957. /* Scan for closest preceeding symbol, and next symbol. (ELF
  1958. starts real symbols at 1). */
  1959. for (i = 1; i < mod->num_symtab; i++) {
  1960. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  1961. continue;
  1962. /* We ignore unnamed symbols: they're uninformative
  1963. * and inserted at a whim. */
  1964. if (mod->symtab[i].st_value <= addr
  1965. && mod->symtab[i].st_value > mod->symtab[best].st_value
  1966. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1967. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1968. best = i;
  1969. if (mod->symtab[i].st_value > addr
  1970. && mod->symtab[i].st_value < nextval
  1971. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1972. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1973. nextval = mod->symtab[i].st_value;
  1974. }
  1975. if (!best)
  1976. return NULL;
  1977. if (size)
  1978. *size = nextval - mod->symtab[best].st_value;
  1979. if (offset)
  1980. *offset = addr - mod->symtab[best].st_value;
  1981. return mod->strtab + mod->symtab[best].st_name;
  1982. }
  1983. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  1984. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  1985. const char *module_address_lookup(unsigned long addr,
  1986. unsigned long *size,
  1987. unsigned long *offset,
  1988. char **modname,
  1989. char *namebuf)
  1990. {
  1991. struct module *mod;
  1992. const char *ret = NULL;
  1993. preempt_disable();
  1994. list_for_each_entry(mod, &modules, list) {
  1995. if (within(addr, mod->module_init, mod->init_size)
  1996. || within(addr, mod->module_core, mod->core_size)) {
  1997. if (modname)
  1998. *modname = mod->name;
  1999. ret = get_ksymbol(mod, addr, size, offset);
  2000. break;
  2001. }
  2002. }
  2003. /* Make a copy in here where it's safe */
  2004. if (ret) {
  2005. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2006. ret = namebuf;
  2007. }
  2008. preempt_enable();
  2009. return ret;
  2010. }
  2011. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2012. {
  2013. struct module *mod;
  2014. preempt_disable();
  2015. list_for_each_entry(mod, &modules, list) {
  2016. if (within(addr, mod->module_init, mod->init_size) ||
  2017. within(addr, mod->module_core, mod->core_size)) {
  2018. const char *sym;
  2019. sym = get_ksymbol(mod, addr, NULL, NULL);
  2020. if (!sym)
  2021. goto out;
  2022. strlcpy(symname, sym, KSYM_NAME_LEN);
  2023. preempt_enable();
  2024. return 0;
  2025. }
  2026. }
  2027. out:
  2028. preempt_enable();
  2029. return -ERANGE;
  2030. }
  2031. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2032. unsigned long *offset, char *modname, char *name)
  2033. {
  2034. struct module *mod;
  2035. preempt_disable();
  2036. list_for_each_entry(mod, &modules, list) {
  2037. if (within(addr, mod->module_init, mod->init_size) ||
  2038. within(addr, mod->module_core, mod->core_size)) {
  2039. const char *sym;
  2040. sym = get_ksymbol(mod, addr, size, offset);
  2041. if (!sym)
  2042. goto out;
  2043. if (modname)
  2044. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2045. if (name)
  2046. strlcpy(name, sym, KSYM_NAME_LEN);
  2047. preempt_enable();
  2048. return 0;
  2049. }
  2050. }
  2051. out:
  2052. preempt_enable();
  2053. return -ERANGE;
  2054. }
  2055. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2056. char *name, char *module_name, int *exported)
  2057. {
  2058. struct module *mod;
  2059. preempt_disable();
  2060. list_for_each_entry(mod, &modules, list) {
  2061. if (symnum < mod->num_symtab) {
  2062. *value = mod->symtab[symnum].st_value;
  2063. *type = mod->symtab[symnum].st_info;
  2064. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2065. KSYM_NAME_LEN);
  2066. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2067. *exported = is_exported(name, mod);
  2068. preempt_enable();
  2069. return 0;
  2070. }
  2071. symnum -= mod->num_symtab;
  2072. }
  2073. preempt_enable();
  2074. return -ERANGE;
  2075. }
  2076. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2077. {
  2078. unsigned int i;
  2079. for (i = 0; i < mod->num_symtab; i++)
  2080. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2081. mod->symtab[i].st_info != 'U')
  2082. return mod->symtab[i].st_value;
  2083. return 0;
  2084. }
  2085. /* Look for this name: can be of form module:name. */
  2086. unsigned long module_kallsyms_lookup_name(const char *name)
  2087. {
  2088. struct module *mod;
  2089. char *colon;
  2090. unsigned long ret = 0;
  2091. /* Don't lock: we're in enough trouble already. */
  2092. preempt_disable();
  2093. if ((colon = strchr(name, ':')) != NULL) {
  2094. *colon = '\0';
  2095. if ((mod = find_module(name)) != NULL)
  2096. ret = mod_find_symname(mod, colon+1);
  2097. *colon = ':';
  2098. } else {
  2099. list_for_each_entry(mod, &modules, list)
  2100. if ((ret = mod_find_symname(mod, name)) != 0)
  2101. break;
  2102. }
  2103. preempt_enable();
  2104. return ret;
  2105. }
  2106. #endif /* CONFIG_KALLSYMS */
  2107. /* Called by the /proc file system to return a list of modules. */
  2108. static void *m_start(struct seq_file *m, loff_t *pos)
  2109. {
  2110. mutex_lock(&module_mutex);
  2111. return seq_list_start(&modules, *pos);
  2112. }
  2113. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2114. {
  2115. return seq_list_next(p, &modules, pos);
  2116. }
  2117. static void m_stop(struct seq_file *m, void *p)
  2118. {
  2119. mutex_unlock(&module_mutex);
  2120. }
  2121. static char *module_flags(struct module *mod, char *buf)
  2122. {
  2123. int bx = 0;
  2124. if (mod->taints ||
  2125. mod->state == MODULE_STATE_GOING ||
  2126. mod->state == MODULE_STATE_COMING) {
  2127. buf[bx++] = '(';
  2128. if (mod->taints & TAINT_PROPRIETARY_MODULE)
  2129. buf[bx++] = 'P';
  2130. if (mod->taints & TAINT_FORCED_MODULE)
  2131. buf[bx++] = 'F';
  2132. /*
  2133. * TAINT_FORCED_RMMOD: could be added.
  2134. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2135. * apply to modules.
  2136. */
  2137. /* Show a - for module-is-being-unloaded */
  2138. if (mod->state == MODULE_STATE_GOING)
  2139. buf[bx++] = '-';
  2140. /* Show a + for module-is-being-loaded */
  2141. if (mod->state == MODULE_STATE_COMING)
  2142. buf[bx++] = '+';
  2143. buf[bx++] = ')';
  2144. }
  2145. buf[bx] = '\0';
  2146. return buf;
  2147. }
  2148. static int m_show(struct seq_file *m, void *p)
  2149. {
  2150. struct module *mod = list_entry(p, struct module, list);
  2151. char buf[8];
  2152. seq_printf(m, "%s %lu",
  2153. mod->name, mod->init_size + mod->core_size);
  2154. print_unload_info(m, mod);
  2155. /* Informative for users. */
  2156. seq_printf(m, " %s",
  2157. mod->state == MODULE_STATE_GOING ? "Unloading":
  2158. mod->state == MODULE_STATE_COMING ? "Loading":
  2159. "Live");
  2160. /* Used by oprofile and other similar tools. */
  2161. seq_printf(m, " 0x%p", mod->module_core);
  2162. /* Taints info */
  2163. if (mod->taints)
  2164. seq_printf(m, " %s", module_flags(mod, buf));
  2165. seq_printf(m, "\n");
  2166. return 0;
  2167. }
  2168. /* Format: modulename size refcount deps address
  2169. Where refcount is a number or -, and deps is a comma-separated list
  2170. of depends or -.
  2171. */
  2172. const struct seq_operations modules_op = {
  2173. .start = m_start,
  2174. .next = m_next,
  2175. .stop = m_stop,
  2176. .show = m_show
  2177. };
  2178. /* Given an address, look for it in the module exception tables. */
  2179. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2180. {
  2181. const struct exception_table_entry *e = NULL;
  2182. struct module *mod;
  2183. preempt_disable();
  2184. list_for_each_entry(mod, &modules, list) {
  2185. if (mod->num_exentries == 0)
  2186. continue;
  2187. e = search_extable(mod->extable,
  2188. mod->extable + mod->num_exentries - 1,
  2189. addr);
  2190. if (e)
  2191. break;
  2192. }
  2193. preempt_enable();
  2194. /* Now, if we found one, we are running inside it now, hence
  2195. we cannot unload the module, hence no refcnt needed. */
  2196. return e;
  2197. }
  2198. /*
  2199. * Is this a valid module address?
  2200. */
  2201. int is_module_address(unsigned long addr)
  2202. {
  2203. struct module *mod;
  2204. preempt_disable();
  2205. list_for_each_entry(mod, &modules, list) {
  2206. if (within(addr, mod->module_core, mod->core_size)) {
  2207. preempt_enable();
  2208. return 1;
  2209. }
  2210. }
  2211. preempt_enable();
  2212. return 0;
  2213. }
  2214. /* Is this a valid kernel address? */
  2215. struct module *__module_text_address(unsigned long addr)
  2216. {
  2217. struct module *mod;
  2218. list_for_each_entry(mod, &modules, list)
  2219. if (within(addr, mod->module_init, mod->init_text_size)
  2220. || within(addr, mod->module_core, mod->core_text_size))
  2221. return mod;
  2222. return NULL;
  2223. }
  2224. struct module *module_text_address(unsigned long addr)
  2225. {
  2226. struct module *mod;
  2227. preempt_disable();
  2228. mod = __module_text_address(addr);
  2229. preempt_enable();
  2230. return mod;
  2231. }
  2232. /* Don't grab lock, we're oopsing. */
  2233. void print_modules(void)
  2234. {
  2235. struct module *mod;
  2236. char buf[8];
  2237. printk("Modules linked in:");
  2238. list_for_each_entry(mod, &modules, list)
  2239. printk(" %s%s", mod->name, module_flags(mod, buf));
  2240. if (last_unloaded_module[0])
  2241. printk(" [last unloaded: %s]", last_unloaded_module);
  2242. printk("\n");
  2243. }
  2244. #ifdef CONFIG_MODVERSIONS
  2245. /* Generate the signature for struct module here, too, for modversions. */
  2246. void struct_module(struct module *mod) { return; }
  2247. EXPORT_SYMBOL(struct_module);
  2248. #endif
  2249. #ifdef CONFIG_MARKERS
  2250. void module_update_markers(void)
  2251. {
  2252. struct module *mod;
  2253. mutex_lock(&module_mutex);
  2254. list_for_each_entry(mod, &modules, list)
  2255. if (!mod->taints)
  2256. marker_update_probe_range(mod->markers,
  2257. mod->markers + mod->num_markers);
  2258. mutex_unlock(&module_mutex);
  2259. }
  2260. #endif