module.c 66 KB

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