module.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  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. #if 0
  47. #define DEBUGP printk
  48. #else
  49. #define DEBUGP(fmt , a...)
  50. #endif
  51. #ifndef ARCH_SHF_SMALL
  52. #define ARCH_SHF_SMALL 0
  53. #endif
  54. /* If this is set, the section belongs in the init part of the module */
  55. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  56. /* List of modules, protected by module_mutex or preempt_disable
  57. * (add/delete uses stop_machine). */
  58. static DEFINE_MUTEX(module_mutex);
  59. static LIST_HEAD(modules);
  60. static BLOCKING_NOTIFIER_HEAD(module_notify_list);
  61. int register_module_notifier(struct notifier_block * nb)
  62. {
  63. return blocking_notifier_chain_register(&module_notify_list, nb);
  64. }
  65. EXPORT_SYMBOL(register_module_notifier);
  66. int unregister_module_notifier(struct notifier_block * nb)
  67. {
  68. return blocking_notifier_chain_unregister(&module_notify_list, nb);
  69. }
  70. EXPORT_SYMBOL(unregister_module_notifier);
  71. /* We require a truly strong try_module_get(): 0 means failure due to
  72. ongoing or failed initialization etc. */
  73. static inline int strong_try_module_get(struct module *mod)
  74. {
  75. if (mod && mod->state == MODULE_STATE_COMING)
  76. return 0;
  77. return try_module_get(mod);
  78. }
  79. static inline void add_taint_module(struct module *mod, unsigned flag)
  80. {
  81. add_taint(flag);
  82. mod->taints |= flag;
  83. }
  84. /*
  85. * A thread that wants to hold a reference to a module only while it
  86. * is running can call this to safely exit. nfsd and lockd use this.
  87. */
  88. void __module_put_and_exit(struct module *mod, long code)
  89. {
  90. module_put(mod);
  91. do_exit(code);
  92. }
  93. EXPORT_SYMBOL(__module_put_and_exit);
  94. /* Find a module section: 0 means not found. */
  95. static unsigned int find_sec(Elf_Ehdr *hdr,
  96. Elf_Shdr *sechdrs,
  97. const char *secstrings,
  98. const char *name)
  99. {
  100. unsigned int i;
  101. for (i = 1; i < hdr->e_shnum; i++)
  102. /* Alloc bit cleared means "ignore it." */
  103. if ((sechdrs[i].sh_flags & SHF_ALLOC)
  104. && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
  105. return i;
  106. return 0;
  107. }
  108. /* Provided by the linker */
  109. extern const struct kernel_symbol __start___ksymtab[];
  110. extern const struct kernel_symbol __stop___ksymtab[];
  111. extern const struct kernel_symbol __start___ksymtab_gpl[];
  112. extern const struct kernel_symbol __stop___ksymtab_gpl[];
  113. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  114. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  115. extern const struct kernel_symbol __start___ksymtab_unused[];
  116. extern const struct kernel_symbol __stop___ksymtab_unused[];
  117. extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
  118. extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
  119. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  120. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  121. extern const unsigned long __start___kcrctab[];
  122. extern const unsigned long __start___kcrctab_gpl[];
  123. extern const unsigned long __start___kcrctab_gpl_future[];
  124. extern const unsigned long __start___kcrctab_unused[];
  125. extern const unsigned long __start___kcrctab_unused_gpl[];
  126. #ifndef CONFIG_MODVERSIONS
  127. #define symversion(base, idx) NULL
  128. #else
  129. #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
  130. #endif
  131. /* lookup symbol in given range of kernel_symbols */
  132. static const struct kernel_symbol *lookup_symbol(const char *name,
  133. const struct kernel_symbol *start,
  134. const struct kernel_symbol *stop)
  135. {
  136. const struct kernel_symbol *ks = start;
  137. for (; ks < stop; ks++)
  138. if (strcmp(ks->name, name) == 0)
  139. return ks;
  140. return NULL;
  141. }
  142. static void printk_unused_warning(const char *name)
  143. {
  144. printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
  145. "however this module is using it.\n", name);
  146. printk(KERN_WARNING "This symbol will go away in the future.\n");
  147. printk(KERN_WARNING "Please evalute if this is the right api to use, "
  148. "and if it really is, submit a report the linux kernel "
  149. "mailinglist together with submitting your code for "
  150. "inclusion.\n");
  151. }
  152. /* Find a symbol, return value, crc and module which owns it */
  153. static unsigned long __find_symbol(const char *name,
  154. struct module **owner,
  155. const unsigned long **crc,
  156. int gplok)
  157. {
  158. struct module *mod;
  159. const struct kernel_symbol *ks;
  160. /* Core kernel first. */
  161. *owner = NULL;
  162. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  163. if (ks) {
  164. *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
  165. return ks->value;
  166. }
  167. if (gplok) {
  168. ks = lookup_symbol(name, __start___ksymtab_gpl,
  169. __stop___ksymtab_gpl);
  170. if (ks) {
  171. *crc = symversion(__start___kcrctab_gpl,
  172. (ks - __start___ksymtab_gpl));
  173. return ks->value;
  174. }
  175. }
  176. ks = lookup_symbol(name, __start___ksymtab_gpl_future,
  177. __stop___ksymtab_gpl_future);
  178. if (ks) {
  179. if (!gplok) {
  180. printk(KERN_WARNING "Symbol %s is being used "
  181. "by a non-GPL module, which will not "
  182. "be allowed in the future\n", name);
  183. printk(KERN_WARNING "Please see the file "
  184. "Documentation/feature-removal-schedule.txt "
  185. "in the kernel source tree for more "
  186. "details.\n");
  187. }
  188. *crc = symversion(__start___kcrctab_gpl_future,
  189. (ks - __start___ksymtab_gpl_future));
  190. return ks->value;
  191. }
  192. ks = lookup_symbol(name, __start___ksymtab_unused,
  193. __stop___ksymtab_unused);
  194. if (ks) {
  195. printk_unused_warning(name);
  196. *crc = symversion(__start___kcrctab_unused,
  197. (ks - __start___ksymtab_unused));
  198. return ks->value;
  199. }
  200. if (gplok)
  201. ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
  202. __stop___ksymtab_unused_gpl);
  203. if (ks) {
  204. printk_unused_warning(name);
  205. *crc = symversion(__start___kcrctab_unused_gpl,
  206. (ks - __start___ksymtab_unused_gpl));
  207. return ks->value;
  208. }
  209. /* Now try modules. */
  210. list_for_each_entry(mod, &modules, list) {
  211. *owner = mod;
  212. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  213. if (ks) {
  214. *crc = symversion(mod->crcs, (ks - mod->syms));
  215. return ks->value;
  216. }
  217. if (gplok) {
  218. ks = lookup_symbol(name, mod->gpl_syms,
  219. mod->gpl_syms + mod->num_gpl_syms);
  220. if (ks) {
  221. *crc = symversion(mod->gpl_crcs,
  222. (ks - mod->gpl_syms));
  223. return ks->value;
  224. }
  225. }
  226. ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
  227. if (ks) {
  228. printk_unused_warning(name);
  229. *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
  230. return ks->value;
  231. }
  232. if (gplok) {
  233. ks = lookup_symbol(name, mod->unused_gpl_syms,
  234. mod->unused_gpl_syms + mod->num_unused_gpl_syms);
  235. if (ks) {
  236. printk_unused_warning(name);
  237. *crc = symversion(mod->unused_gpl_crcs,
  238. (ks - mod->unused_gpl_syms));
  239. return ks->value;
  240. }
  241. }
  242. ks = lookup_symbol(name, mod->gpl_future_syms,
  243. (mod->gpl_future_syms +
  244. mod->num_gpl_future_syms));
  245. if (ks) {
  246. if (!gplok) {
  247. printk(KERN_WARNING "Symbol %s is being used "
  248. "by a non-GPL module, which will not "
  249. "be allowed in the future\n", name);
  250. printk(KERN_WARNING "Please see the file "
  251. "Documentation/feature-removal-schedule.txt "
  252. "in the kernel source tree for more "
  253. "details.\n");
  254. }
  255. *crc = symversion(mod->gpl_future_crcs,
  256. (ks - mod->gpl_future_syms));
  257. return ks->value;
  258. }
  259. }
  260. DEBUGP("Failed to find symbol %s\n", name);
  261. return 0;
  262. }
  263. /* Search for module by name: must hold module_mutex. */
  264. static struct module *find_module(const char *name)
  265. {
  266. struct module *mod;
  267. list_for_each_entry(mod, &modules, list) {
  268. if (strcmp(mod->name, name) == 0)
  269. return mod;
  270. }
  271. return NULL;
  272. }
  273. #ifdef CONFIG_SMP
  274. /* Number of blocks used and allocated. */
  275. static unsigned int pcpu_num_used, pcpu_num_allocated;
  276. /* Size of each block. -ve means used. */
  277. static int *pcpu_size;
  278. static int split_block(unsigned int i, unsigned short size)
  279. {
  280. /* Reallocation required? */
  281. if (pcpu_num_used + 1 > pcpu_num_allocated) {
  282. int *new;
  283. new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
  284. GFP_KERNEL);
  285. if (!new)
  286. return 0;
  287. pcpu_num_allocated *= 2;
  288. pcpu_size = new;
  289. }
  290. /* Insert a new subblock */
  291. memmove(&pcpu_size[i+1], &pcpu_size[i],
  292. sizeof(pcpu_size[0]) * (pcpu_num_used - i));
  293. pcpu_num_used++;
  294. pcpu_size[i+1] -= size;
  295. pcpu_size[i] = size;
  296. return 1;
  297. }
  298. static inline unsigned int block_size(int val)
  299. {
  300. if (val < 0)
  301. return -val;
  302. return val;
  303. }
  304. /* Created by linker magic */
  305. extern char __per_cpu_start[], __per_cpu_end[];
  306. static void *percpu_modalloc(unsigned long size, unsigned long align,
  307. const char *name)
  308. {
  309. unsigned long extra;
  310. unsigned int i;
  311. void *ptr;
  312. if (align > PAGE_SIZE) {
  313. printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
  314. name, align, PAGE_SIZE);
  315. align = PAGE_SIZE;
  316. }
  317. ptr = __per_cpu_start;
  318. for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
  319. /* Extra for alignment requirement. */
  320. extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
  321. BUG_ON(i == 0 && extra != 0);
  322. if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
  323. continue;
  324. /* Transfer extra to previous block. */
  325. if (pcpu_size[i-1] < 0)
  326. pcpu_size[i-1] -= extra;
  327. else
  328. pcpu_size[i-1] += extra;
  329. pcpu_size[i] -= extra;
  330. ptr += extra;
  331. /* Split block if warranted */
  332. if (pcpu_size[i] - size > sizeof(unsigned long))
  333. if (!split_block(i, size))
  334. return NULL;
  335. /* Mark allocated */
  336. pcpu_size[i] = -pcpu_size[i];
  337. return ptr;
  338. }
  339. printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
  340. size);
  341. return NULL;
  342. }
  343. static void percpu_modfree(void *freeme)
  344. {
  345. unsigned int i;
  346. void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
  347. /* First entry is core kernel percpu data. */
  348. for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
  349. if (ptr == freeme) {
  350. pcpu_size[i] = -pcpu_size[i];
  351. goto free;
  352. }
  353. }
  354. BUG();
  355. free:
  356. /* Merge with previous? */
  357. if (pcpu_size[i-1] >= 0) {
  358. pcpu_size[i-1] += pcpu_size[i];
  359. pcpu_num_used--;
  360. memmove(&pcpu_size[i], &pcpu_size[i+1],
  361. (pcpu_num_used - i) * sizeof(pcpu_size[0]));
  362. i--;
  363. }
  364. /* Merge with next? */
  365. if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
  366. pcpu_size[i] += pcpu_size[i+1];
  367. pcpu_num_used--;
  368. memmove(&pcpu_size[i+1], &pcpu_size[i+2],
  369. (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
  370. }
  371. }
  372. static unsigned int find_pcpusec(Elf_Ehdr *hdr,
  373. Elf_Shdr *sechdrs,
  374. const char *secstrings)
  375. {
  376. return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
  377. }
  378. static int percpu_modinit(void)
  379. {
  380. pcpu_num_used = 2;
  381. pcpu_num_allocated = 2;
  382. pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
  383. GFP_KERNEL);
  384. /* Static in-kernel percpu data (used). */
  385. pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
  386. /* Free room. */
  387. pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
  388. if (pcpu_size[1] < 0) {
  389. printk(KERN_ERR "No per-cpu room for modules.\n");
  390. pcpu_num_used = 1;
  391. }
  392. return 0;
  393. }
  394. __initcall(percpu_modinit);
  395. #else /* ... !CONFIG_SMP */
  396. static inline void *percpu_modalloc(unsigned long size, unsigned long align,
  397. const char *name)
  398. {
  399. return NULL;
  400. }
  401. static inline void percpu_modfree(void *pcpuptr)
  402. {
  403. BUG();
  404. }
  405. static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
  406. Elf_Shdr *sechdrs,
  407. const char *secstrings)
  408. {
  409. return 0;
  410. }
  411. static inline void percpu_modcopy(void *pcpudst, const void *src,
  412. unsigned long size)
  413. {
  414. /* pcpusec should be 0, and size of that section should be 0. */
  415. BUG_ON(size != 0);
  416. }
  417. #endif /* CONFIG_SMP */
  418. #define MODINFO_ATTR(field) \
  419. static void setup_modinfo_##field(struct module *mod, const char *s) \
  420. { \
  421. mod->field = kstrdup(s, GFP_KERNEL); \
  422. } \
  423. static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
  424. struct module *mod, char *buffer) \
  425. { \
  426. return sprintf(buffer, "%s\n", mod->field); \
  427. } \
  428. static int modinfo_##field##_exists(struct module *mod) \
  429. { \
  430. return mod->field != NULL; \
  431. } \
  432. static void free_modinfo_##field(struct module *mod) \
  433. { \
  434. kfree(mod->field); \
  435. mod->field = NULL; \
  436. } \
  437. static struct module_attribute modinfo_##field = { \
  438. .attr = { .name = __stringify(field), .mode = 0444 }, \
  439. .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 && !mod->exit) {
  612. forced = try_force_unload(flags);
  613. if (!forced) {
  614. /* This module can't be removed */
  615. ret = -EBUSY;
  616. goto out;
  617. }
  618. }
  619. /* Set this up before setting mod->state */
  620. mod->waiter = current;
  621. /* Stop the machine so refcounts can't move and disable module. */
  622. ret = try_stop_module(mod, flags, &forced);
  623. if (ret != 0)
  624. goto out;
  625. /* Never wait if forced. */
  626. if (!forced && module_refcount(mod) != 0)
  627. wait_for_zero_refcount(mod);
  628. /* Final destruction now noone is using it. */
  629. if (mod->exit != NULL) {
  630. mutex_unlock(&module_mutex);
  631. mod->exit();
  632. mutex_lock(&module_mutex);
  633. }
  634. free_module(mod);
  635. out:
  636. mutex_unlock(&module_mutex);
  637. return ret;
  638. }
  639. static void print_unload_info(struct seq_file *m, struct module *mod)
  640. {
  641. struct module_use *use;
  642. int printed_something = 0;
  643. seq_printf(m, " %u ", module_refcount(mod));
  644. /* Always include a trailing , so userspace can differentiate
  645. between this and the old multi-field proc format. */
  646. list_for_each_entry(use, &mod->modules_which_use_me, list) {
  647. printed_something = 1;
  648. seq_printf(m, "%s,", use->module_which_uses->name);
  649. }
  650. if (mod->init != NULL && mod->exit == NULL) {
  651. printed_something = 1;
  652. seq_printf(m, "[permanent],");
  653. }
  654. if (!printed_something)
  655. seq_printf(m, "-");
  656. }
  657. void __symbol_put(const char *symbol)
  658. {
  659. struct module *owner;
  660. const unsigned long *crc;
  661. preempt_disable();
  662. if (!__find_symbol(symbol, &owner, &crc, 1))
  663. BUG();
  664. module_put(owner);
  665. preempt_enable();
  666. }
  667. EXPORT_SYMBOL(__symbol_put);
  668. void symbol_put_addr(void *addr)
  669. {
  670. struct module *modaddr;
  671. if (core_kernel_text((unsigned long)addr))
  672. return;
  673. if (!(modaddr = module_text_address((unsigned long)addr)))
  674. BUG();
  675. module_put(modaddr);
  676. }
  677. EXPORT_SYMBOL_GPL(symbol_put_addr);
  678. static ssize_t show_refcnt(struct module_attribute *mattr,
  679. struct module *mod, char *buffer)
  680. {
  681. return sprintf(buffer, "%u\n", module_refcount(mod));
  682. }
  683. static struct module_attribute refcnt = {
  684. .attr = { .name = "refcnt", .mode = 0444 },
  685. .show = show_refcnt,
  686. };
  687. void module_put(struct module *module)
  688. {
  689. if (module) {
  690. unsigned int cpu = get_cpu();
  691. local_dec(&module->ref[cpu].count);
  692. /* Maybe they're waiting for us to drop reference? */
  693. if (unlikely(!module_is_live(module)))
  694. wake_up_process(module->waiter);
  695. put_cpu();
  696. }
  697. }
  698. EXPORT_SYMBOL(module_put);
  699. #else /* !CONFIG_MODULE_UNLOAD */
  700. static void print_unload_info(struct seq_file *m, struct module *mod)
  701. {
  702. /* We don't know the usage count, or what modules are using. */
  703. seq_printf(m, " - -");
  704. }
  705. static inline void module_unload_free(struct module *mod)
  706. {
  707. }
  708. static inline int use_module(struct module *a, struct module *b)
  709. {
  710. return strong_try_module_get(b);
  711. }
  712. static inline void module_unload_init(struct module *mod)
  713. {
  714. }
  715. #endif /* CONFIG_MODULE_UNLOAD */
  716. static ssize_t show_initstate(struct module_attribute *mattr,
  717. struct module *mod, char *buffer)
  718. {
  719. const char *state = "unknown";
  720. switch (mod->state) {
  721. case MODULE_STATE_LIVE:
  722. state = "live";
  723. break;
  724. case MODULE_STATE_COMING:
  725. state = "coming";
  726. break;
  727. case MODULE_STATE_GOING:
  728. state = "going";
  729. break;
  730. }
  731. return sprintf(buffer, "%s\n", state);
  732. }
  733. static struct module_attribute initstate = {
  734. .attr = { .name = "initstate", .mode = 0444 },
  735. .show = show_initstate,
  736. };
  737. static struct module_attribute *modinfo_attrs[] = {
  738. &modinfo_version,
  739. &modinfo_srcversion,
  740. &initstate,
  741. #ifdef CONFIG_MODULE_UNLOAD
  742. &refcnt,
  743. #endif
  744. NULL,
  745. };
  746. static const char vermagic[] = VERMAGIC_STRING;
  747. #ifdef CONFIG_MODVERSIONS
  748. static int check_version(Elf_Shdr *sechdrs,
  749. unsigned int versindex,
  750. const char *symname,
  751. struct module *mod,
  752. const unsigned long *crc)
  753. {
  754. unsigned int i, num_versions;
  755. struct modversion_info *versions;
  756. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  757. if (!crc)
  758. return 1;
  759. versions = (void *) sechdrs[versindex].sh_addr;
  760. num_versions = sechdrs[versindex].sh_size
  761. / sizeof(struct modversion_info);
  762. for (i = 0; i < num_versions; i++) {
  763. if (strcmp(versions[i].name, symname) != 0)
  764. continue;
  765. if (versions[i].crc == *crc)
  766. return 1;
  767. printk("%s: disagrees about version of symbol %s\n",
  768. mod->name, symname);
  769. DEBUGP("Found checksum %lX vs module %lX\n",
  770. *crc, versions[i].crc);
  771. return 0;
  772. }
  773. /* Not in module's version table. OK, but that taints the kernel. */
  774. if (!(tainted & TAINT_FORCED_MODULE))
  775. printk("%s: no version for \"%s\" found: kernel tainted.\n",
  776. mod->name, symname);
  777. add_taint_module(mod, TAINT_FORCED_MODULE);
  778. return 1;
  779. }
  780. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  781. unsigned int versindex,
  782. struct module *mod)
  783. {
  784. const unsigned long *crc;
  785. struct module *owner;
  786. if (!__find_symbol("struct_module", &owner, &crc, 1))
  787. BUG();
  788. return check_version(sechdrs, versindex, "struct_module", mod,
  789. crc);
  790. }
  791. /* First part is kernel version, which we ignore. */
  792. static inline int same_magic(const char *amagic, const char *bmagic)
  793. {
  794. amagic += strcspn(amagic, " ");
  795. bmagic += strcspn(bmagic, " ");
  796. return strcmp(amagic, bmagic) == 0;
  797. }
  798. #else
  799. static inline int check_version(Elf_Shdr *sechdrs,
  800. unsigned int versindex,
  801. const char *symname,
  802. struct module *mod,
  803. const unsigned long *crc)
  804. {
  805. return 1;
  806. }
  807. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  808. unsigned int versindex,
  809. struct module *mod)
  810. {
  811. return 1;
  812. }
  813. static inline int same_magic(const char *amagic, const char *bmagic)
  814. {
  815. return strcmp(amagic, bmagic) == 0;
  816. }
  817. #endif /* CONFIG_MODVERSIONS */
  818. /* Resolve a symbol for this module. I.e. if we find one, record usage.
  819. Must be holding module_mutex. */
  820. static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
  821. unsigned int versindex,
  822. const char *name,
  823. struct module *mod)
  824. {
  825. struct module *owner;
  826. unsigned long ret;
  827. const unsigned long *crc;
  828. ret = __find_symbol(name, &owner, &crc,
  829. !(mod->taints & TAINT_PROPRIETARY_MODULE));
  830. if (ret) {
  831. /* use_module can fail due to OOM,
  832. or module initialization or unloading */
  833. if (!check_version(sechdrs, versindex, name, mod, crc) ||
  834. !use_module(mod, owner))
  835. ret = 0;
  836. }
  837. return ret;
  838. }
  839. /*
  840. * /sys/module/foo/sections stuff
  841. * J. Corbet <corbet@lwn.net>
  842. */
  843. #ifdef CONFIG_KALLSYMS
  844. static ssize_t module_sect_show(struct module_attribute *mattr,
  845. struct module *mod, char *buf)
  846. {
  847. struct module_sect_attr *sattr =
  848. container_of(mattr, struct module_sect_attr, mattr);
  849. return sprintf(buf, "0x%lx\n", sattr->address);
  850. }
  851. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  852. {
  853. int section;
  854. for (section = 0; section < sect_attrs->nsections; section++)
  855. kfree(sect_attrs->attrs[section].name);
  856. kfree(sect_attrs);
  857. }
  858. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  859. char *secstrings, Elf_Shdr *sechdrs)
  860. {
  861. unsigned int nloaded = 0, i, size[2];
  862. struct module_sect_attrs *sect_attrs;
  863. struct module_sect_attr *sattr;
  864. struct attribute **gattr;
  865. /* Count loaded sections and allocate structures */
  866. for (i = 0; i < nsect; i++)
  867. if (sechdrs[i].sh_flags & SHF_ALLOC)
  868. nloaded++;
  869. size[0] = ALIGN(sizeof(*sect_attrs)
  870. + nloaded * sizeof(sect_attrs->attrs[0]),
  871. sizeof(sect_attrs->grp.attrs[0]));
  872. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  873. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  874. if (sect_attrs == NULL)
  875. return;
  876. /* Setup section attributes. */
  877. sect_attrs->grp.name = "sections";
  878. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  879. sect_attrs->nsections = 0;
  880. sattr = &sect_attrs->attrs[0];
  881. gattr = &sect_attrs->grp.attrs[0];
  882. for (i = 0; i < nsect; i++) {
  883. if (! (sechdrs[i].sh_flags & SHF_ALLOC))
  884. continue;
  885. sattr->address = sechdrs[i].sh_addr;
  886. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  887. GFP_KERNEL);
  888. if (sattr->name == NULL)
  889. goto out;
  890. sect_attrs->nsections++;
  891. sattr->mattr.show = module_sect_show;
  892. sattr->mattr.store = NULL;
  893. sattr->mattr.attr.name = sattr->name;
  894. sattr->mattr.attr.mode = S_IRUGO;
  895. *(gattr++) = &(sattr++)->mattr.attr;
  896. }
  897. *gattr = NULL;
  898. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  899. goto out;
  900. mod->sect_attrs = sect_attrs;
  901. return;
  902. out:
  903. free_sect_attrs(sect_attrs);
  904. }
  905. static void remove_sect_attrs(struct module *mod)
  906. {
  907. if (mod->sect_attrs) {
  908. sysfs_remove_group(&mod->mkobj.kobj,
  909. &mod->sect_attrs->grp);
  910. /* We are positive that no one is using any sect attrs
  911. * at this point. Deallocate immediately. */
  912. free_sect_attrs(mod->sect_attrs);
  913. mod->sect_attrs = NULL;
  914. }
  915. }
  916. /*
  917. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  918. */
  919. struct module_notes_attrs {
  920. struct kobject *dir;
  921. unsigned int notes;
  922. struct bin_attribute attrs[0];
  923. };
  924. static ssize_t module_notes_read(struct kobject *kobj,
  925. struct bin_attribute *bin_attr,
  926. char *buf, loff_t pos, size_t count)
  927. {
  928. /*
  929. * The caller checked the pos and count against our size.
  930. */
  931. memcpy(buf, bin_attr->private + pos, count);
  932. return count;
  933. }
  934. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  935. unsigned int i)
  936. {
  937. if (notes_attrs->dir) {
  938. while (i-- > 0)
  939. sysfs_remove_bin_file(notes_attrs->dir,
  940. &notes_attrs->attrs[i]);
  941. kobject_del(notes_attrs->dir);
  942. }
  943. kfree(notes_attrs);
  944. }
  945. static void add_notes_attrs(struct module *mod, unsigned int nsect,
  946. char *secstrings, Elf_Shdr *sechdrs)
  947. {
  948. unsigned int notes, loaded, i;
  949. struct module_notes_attrs *notes_attrs;
  950. struct bin_attribute *nattr;
  951. /* Count notes sections and allocate structures. */
  952. notes = 0;
  953. for (i = 0; i < nsect; i++)
  954. if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
  955. (sechdrs[i].sh_type == SHT_NOTE))
  956. ++notes;
  957. if (notes == 0)
  958. return;
  959. notes_attrs = kzalloc(sizeof(*notes_attrs)
  960. + notes * sizeof(notes_attrs->attrs[0]),
  961. GFP_KERNEL);
  962. if (notes_attrs == NULL)
  963. return;
  964. notes_attrs->notes = notes;
  965. nattr = &notes_attrs->attrs[0];
  966. for (loaded = i = 0; i < nsect; ++i) {
  967. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  968. continue;
  969. if (sechdrs[i].sh_type == SHT_NOTE) {
  970. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  971. nattr->attr.mode = S_IRUGO;
  972. nattr->size = sechdrs[i].sh_size;
  973. nattr->private = (void *) sechdrs[i].sh_addr;
  974. nattr->read = module_notes_read;
  975. ++nattr;
  976. }
  977. ++loaded;
  978. }
  979. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  980. if (!notes_attrs->dir)
  981. goto out;
  982. for (i = 0; i < notes; ++i)
  983. if (sysfs_create_bin_file(notes_attrs->dir,
  984. &notes_attrs->attrs[i]))
  985. goto out;
  986. mod->notes_attrs = notes_attrs;
  987. return;
  988. out:
  989. free_notes_attrs(notes_attrs, i);
  990. }
  991. static void remove_notes_attrs(struct module *mod)
  992. {
  993. if (mod->notes_attrs)
  994. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  995. }
  996. #else
  997. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  998. char *sectstrings, Elf_Shdr *sechdrs)
  999. {
  1000. }
  1001. static inline void remove_sect_attrs(struct module *mod)
  1002. {
  1003. }
  1004. static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
  1005. char *sectstrings, Elf_Shdr *sechdrs)
  1006. {
  1007. }
  1008. static inline void remove_notes_attrs(struct module *mod)
  1009. {
  1010. }
  1011. #endif /* CONFIG_KALLSYMS */
  1012. #ifdef CONFIG_SYSFS
  1013. int module_add_modinfo_attrs(struct module *mod)
  1014. {
  1015. struct module_attribute *attr;
  1016. struct module_attribute *temp_attr;
  1017. int error = 0;
  1018. int i;
  1019. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1020. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1021. GFP_KERNEL);
  1022. if (!mod->modinfo_attrs)
  1023. return -ENOMEM;
  1024. temp_attr = mod->modinfo_attrs;
  1025. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1026. if (!attr->test ||
  1027. (attr->test && attr->test(mod))) {
  1028. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1029. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  1030. ++temp_attr;
  1031. }
  1032. }
  1033. return error;
  1034. }
  1035. void module_remove_modinfo_attrs(struct module *mod)
  1036. {
  1037. struct module_attribute *attr;
  1038. int i;
  1039. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1040. /* pick a field to test for end of list */
  1041. if (!attr->attr.name)
  1042. break;
  1043. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  1044. if (attr->free)
  1045. attr->free(mod);
  1046. }
  1047. kfree(mod->modinfo_attrs);
  1048. }
  1049. #endif
  1050. #ifdef CONFIG_SYSFS
  1051. int mod_sysfs_init(struct module *mod)
  1052. {
  1053. int err;
  1054. if (!module_sysfs_initialized) {
  1055. printk(KERN_ERR "%s: module sysfs not initialized\n",
  1056. mod->name);
  1057. err = -EINVAL;
  1058. goto out;
  1059. }
  1060. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1061. err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
  1062. if (err)
  1063. goto out;
  1064. mod->mkobj.kobj.kset = module_kset;
  1065. mod->mkobj.kobj.ktype = &module_ktype;
  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_create_and_add("holders", &mod->mkobj.kobj);
  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. /* FIXME: Risky: returns a pointer into a module w/o lock */
  1925. const char *module_address_lookup(unsigned long addr,
  1926. unsigned long *size,
  1927. unsigned long *offset,
  1928. char **modname)
  1929. {
  1930. struct module *mod;
  1931. const char *ret = NULL;
  1932. preempt_disable();
  1933. list_for_each_entry(mod, &modules, list) {
  1934. if (within(addr, mod->module_init, mod->init_size)
  1935. || within(addr, mod->module_core, mod->core_size)) {
  1936. if (modname)
  1937. *modname = mod->name;
  1938. ret = get_ksymbol(mod, addr, size, offset);
  1939. break;
  1940. }
  1941. }
  1942. preempt_enable();
  1943. return ret;
  1944. }
  1945. int lookup_module_symbol_name(unsigned long addr, char *symname)
  1946. {
  1947. struct module *mod;
  1948. preempt_disable();
  1949. list_for_each_entry(mod, &modules, list) {
  1950. if (within(addr, mod->module_init, mod->init_size) ||
  1951. within(addr, mod->module_core, mod->core_size)) {
  1952. const char *sym;
  1953. sym = get_ksymbol(mod, addr, NULL, NULL);
  1954. if (!sym)
  1955. goto out;
  1956. strlcpy(symname, sym, KSYM_NAME_LEN);
  1957. preempt_enable();
  1958. return 0;
  1959. }
  1960. }
  1961. out:
  1962. preempt_enable();
  1963. return -ERANGE;
  1964. }
  1965. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  1966. unsigned long *offset, char *modname, char *name)
  1967. {
  1968. struct module *mod;
  1969. preempt_disable();
  1970. list_for_each_entry(mod, &modules, list) {
  1971. if (within(addr, mod->module_init, mod->init_size) ||
  1972. within(addr, mod->module_core, mod->core_size)) {
  1973. const char *sym;
  1974. sym = get_ksymbol(mod, addr, size, offset);
  1975. if (!sym)
  1976. goto out;
  1977. if (modname)
  1978. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  1979. if (name)
  1980. strlcpy(name, sym, KSYM_NAME_LEN);
  1981. preempt_enable();
  1982. return 0;
  1983. }
  1984. }
  1985. out:
  1986. preempt_enable();
  1987. return -ERANGE;
  1988. }
  1989. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  1990. char *name, char *module_name, int *exported)
  1991. {
  1992. struct module *mod;
  1993. preempt_disable();
  1994. list_for_each_entry(mod, &modules, list) {
  1995. if (symnum < mod->num_symtab) {
  1996. *value = mod->symtab[symnum].st_value;
  1997. *type = mod->symtab[symnum].st_info;
  1998. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  1999. KSYM_NAME_LEN);
  2000. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2001. *exported = is_exported(name, mod);
  2002. preempt_enable();
  2003. return 0;
  2004. }
  2005. symnum -= mod->num_symtab;
  2006. }
  2007. preempt_enable();
  2008. return -ERANGE;
  2009. }
  2010. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2011. {
  2012. unsigned int i;
  2013. for (i = 0; i < mod->num_symtab; i++)
  2014. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2015. mod->symtab[i].st_info != 'U')
  2016. return mod->symtab[i].st_value;
  2017. return 0;
  2018. }
  2019. /* Look for this name: can be of form module:name. */
  2020. unsigned long module_kallsyms_lookup_name(const char *name)
  2021. {
  2022. struct module *mod;
  2023. char *colon;
  2024. unsigned long ret = 0;
  2025. /* Don't lock: we're in enough trouble already. */
  2026. preempt_disable();
  2027. if ((colon = strchr(name, ':')) != NULL) {
  2028. *colon = '\0';
  2029. if ((mod = find_module(name)) != NULL)
  2030. ret = mod_find_symname(mod, colon+1);
  2031. *colon = ':';
  2032. } else {
  2033. list_for_each_entry(mod, &modules, list)
  2034. if ((ret = mod_find_symname(mod, name)) != 0)
  2035. break;
  2036. }
  2037. preempt_enable();
  2038. return ret;
  2039. }
  2040. #endif /* CONFIG_KALLSYMS */
  2041. /* Called by the /proc file system to return a list of modules. */
  2042. static void *m_start(struct seq_file *m, loff_t *pos)
  2043. {
  2044. mutex_lock(&module_mutex);
  2045. return seq_list_start(&modules, *pos);
  2046. }
  2047. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2048. {
  2049. return seq_list_next(p, &modules, pos);
  2050. }
  2051. static void m_stop(struct seq_file *m, void *p)
  2052. {
  2053. mutex_unlock(&module_mutex);
  2054. }
  2055. static char *taint_flags(unsigned int taints, char *buf)
  2056. {
  2057. int bx = 0;
  2058. if (taints) {
  2059. buf[bx++] = '(';
  2060. if (taints & TAINT_PROPRIETARY_MODULE)
  2061. buf[bx++] = 'P';
  2062. if (taints & TAINT_FORCED_MODULE)
  2063. buf[bx++] = 'F';
  2064. /*
  2065. * TAINT_FORCED_RMMOD: could be added.
  2066. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2067. * apply to modules.
  2068. */
  2069. buf[bx++] = ')';
  2070. }
  2071. buf[bx] = '\0';
  2072. return buf;
  2073. }
  2074. static int m_show(struct seq_file *m, void *p)
  2075. {
  2076. struct module *mod = list_entry(p, struct module, list);
  2077. char buf[8];
  2078. seq_printf(m, "%s %lu",
  2079. mod->name, mod->init_size + mod->core_size);
  2080. print_unload_info(m, mod);
  2081. /* Informative for users. */
  2082. seq_printf(m, " %s",
  2083. mod->state == MODULE_STATE_GOING ? "Unloading":
  2084. mod->state == MODULE_STATE_COMING ? "Loading":
  2085. "Live");
  2086. /* Used by oprofile and other similar tools. */
  2087. seq_printf(m, " 0x%p", mod->module_core);
  2088. /* Taints info */
  2089. if (mod->taints)
  2090. seq_printf(m, " %s", taint_flags(mod->taints, buf));
  2091. seq_printf(m, "\n");
  2092. return 0;
  2093. }
  2094. /* Format: modulename size refcount deps address
  2095. Where refcount is a number or -, and deps is a comma-separated list
  2096. of depends or -.
  2097. */
  2098. const struct seq_operations modules_op = {
  2099. .start = m_start,
  2100. .next = m_next,
  2101. .stop = m_stop,
  2102. .show = m_show
  2103. };
  2104. /* Given an address, look for it in the module exception tables. */
  2105. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2106. {
  2107. const struct exception_table_entry *e = NULL;
  2108. struct module *mod;
  2109. preempt_disable();
  2110. list_for_each_entry(mod, &modules, list) {
  2111. if (mod->num_exentries == 0)
  2112. continue;
  2113. e = search_extable(mod->extable,
  2114. mod->extable + mod->num_exentries - 1,
  2115. addr);
  2116. if (e)
  2117. break;
  2118. }
  2119. preempt_enable();
  2120. /* Now, if we found one, we are running inside it now, hence
  2121. we cannot unload the module, hence no refcnt needed. */
  2122. return e;
  2123. }
  2124. /*
  2125. * Is this a valid module address?
  2126. */
  2127. int is_module_address(unsigned long addr)
  2128. {
  2129. struct module *mod;
  2130. preempt_disable();
  2131. list_for_each_entry(mod, &modules, list) {
  2132. if (within(addr, mod->module_core, mod->core_size)) {
  2133. preempt_enable();
  2134. return 1;
  2135. }
  2136. }
  2137. preempt_enable();
  2138. return 0;
  2139. }
  2140. /* Is this a valid kernel address? */
  2141. struct module *__module_text_address(unsigned long addr)
  2142. {
  2143. struct module *mod;
  2144. list_for_each_entry(mod, &modules, list)
  2145. if (within(addr, mod->module_init, mod->init_text_size)
  2146. || within(addr, mod->module_core, mod->core_text_size))
  2147. return mod;
  2148. return NULL;
  2149. }
  2150. struct module *module_text_address(unsigned long addr)
  2151. {
  2152. struct module *mod;
  2153. preempt_disable();
  2154. mod = __module_text_address(addr);
  2155. preempt_enable();
  2156. return mod;
  2157. }
  2158. /* Don't grab lock, we're oopsing. */
  2159. void print_modules(void)
  2160. {
  2161. struct module *mod;
  2162. char buf[8];
  2163. printk("Modules linked in:");
  2164. list_for_each_entry(mod, &modules, list)
  2165. printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
  2166. printk("\n");
  2167. }
  2168. #ifdef CONFIG_SYSFS
  2169. static char *make_driver_name(struct device_driver *drv)
  2170. {
  2171. char *driver_name;
  2172. driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
  2173. GFP_KERNEL);
  2174. if (!driver_name)
  2175. return NULL;
  2176. sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
  2177. return driver_name;
  2178. }
  2179. static void module_create_drivers_dir(struct module_kobject *mk)
  2180. {
  2181. if (!mk || mk->drivers_dir)
  2182. return;
  2183. mk->drivers_dir = kobject_create_and_add("drivers", &mk->kobj);
  2184. }
  2185. void module_add_driver(struct module *mod, struct device_driver *drv)
  2186. {
  2187. char *driver_name;
  2188. int no_warn;
  2189. struct module_kobject *mk = NULL;
  2190. if (!drv)
  2191. return;
  2192. if (mod)
  2193. mk = &mod->mkobj;
  2194. else if (drv->mod_name) {
  2195. struct kobject *mkobj;
  2196. /* Lookup built-in module entry in /sys/modules */
  2197. mkobj = kset_find_obj(module_kset, drv->mod_name);
  2198. if (mkobj) {
  2199. mk = container_of(mkobj, struct module_kobject, kobj);
  2200. /* remember our module structure */
  2201. drv->mkobj = mk;
  2202. /* kset_find_obj took a reference */
  2203. kobject_put(mkobj);
  2204. }
  2205. }
  2206. if (!mk)
  2207. return;
  2208. /* Don't check return codes; these calls are idempotent */
  2209. no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
  2210. driver_name = make_driver_name(drv);
  2211. if (driver_name) {
  2212. module_create_drivers_dir(mk);
  2213. no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
  2214. driver_name);
  2215. kfree(driver_name);
  2216. }
  2217. }
  2218. EXPORT_SYMBOL(module_add_driver);
  2219. void module_remove_driver(struct device_driver *drv)
  2220. {
  2221. struct module_kobject *mk = NULL;
  2222. char *driver_name;
  2223. if (!drv)
  2224. return;
  2225. sysfs_remove_link(&drv->kobj, "module");
  2226. if (drv->owner)
  2227. mk = &drv->owner->mkobj;
  2228. else if (drv->mkobj)
  2229. mk = drv->mkobj;
  2230. if (mk && mk->drivers_dir) {
  2231. driver_name = make_driver_name(drv);
  2232. if (driver_name) {
  2233. sysfs_remove_link(mk->drivers_dir, driver_name);
  2234. kfree(driver_name);
  2235. }
  2236. }
  2237. }
  2238. EXPORT_SYMBOL(module_remove_driver);
  2239. #endif
  2240. #ifdef CONFIG_MODVERSIONS
  2241. /* Generate the signature for struct module here, too, for modversions. */
  2242. void struct_module(struct module *mod) { return; }
  2243. EXPORT_SYMBOL(struct_module);
  2244. #endif
  2245. #ifdef CONFIG_MARKERS
  2246. void module_update_markers(struct module *probe_module, int *refcount)
  2247. {
  2248. struct module *mod;
  2249. mutex_lock(&module_mutex);
  2250. list_for_each_entry(mod, &modules, list)
  2251. if (!mod->taints)
  2252. marker_update_probe_range(mod->markers,
  2253. mod->markers + mod->num_markers,
  2254. probe_module, refcount);
  2255. mutex_unlock(&module_mutex);
  2256. }
  2257. #endif