module.c 69 KB

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