module.c 63 KB

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