module.c 66 KB

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