module.c 64 KB

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