module.c 68 KB

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