module.c 59 KB

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