module.c 65 KB

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