module.c 65 KB

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