module.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  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. sprintf(last_unloaded_module, mod->name);
  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. * unlink the module with the whole machine is stopped with interrupts off
  1125. * - this defends against kallsyms not taking locks
  1126. */
  1127. static int __unlink_module(void *_mod)
  1128. {
  1129. struct module *mod = _mod;
  1130. list_del(&mod->list);
  1131. return 0;
  1132. }
  1133. /* Free a module, remove from lists, etc (must hold module_mutex). */
  1134. static void free_module(struct module *mod)
  1135. {
  1136. /* Delete from various lists */
  1137. stop_machine_run(__unlink_module, mod, NR_CPUS);
  1138. remove_notes_attrs(mod);
  1139. remove_sect_attrs(mod);
  1140. mod_kobject_remove(mod);
  1141. unwind_remove_table(mod->unwind_info, 0);
  1142. /* Arch-specific cleanup. */
  1143. module_arch_cleanup(mod);
  1144. /* Module unload stuff */
  1145. module_unload_free(mod);
  1146. /* This may be NULL, but that's OK */
  1147. module_free(mod, mod->module_init);
  1148. kfree(mod->args);
  1149. if (mod->percpu)
  1150. percpu_modfree(mod->percpu);
  1151. /* Free lock-classes: */
  1152. lockdep_free_key_range(mod->module_core, mod->core_size);
  1153. /* Finally, free the core (containing the module structure) */
  1154. module_free(mod, mod->module_core);
  1155. }
  1156. void *__symbol_get(const char *symbol)
  1157. {
  1158. struct module *owner;
  1159. unsigned long value;
  1160. const unsigned long *crc;
  1161. preempt_disable();
  1162. value = __find_symbol(symbol, &owner, &crc, 1);
  1163. if (value && strong_try_module_get(owner) != 0)
  1164. value = 0;
  1165. preempt_enable();
  1166. return (void *)value;
  1167. }
  1168. EXPORT_SYMBOL_GPL(__symbol_get);
  1169. /*
  1170. * Ensure that an exported symbol [global namespace] does not already exist
  1171. * in the kernel or in some other module's exported symbol table.
  1172. */
  1173. static int verify_export_symbols(struct module *mod)
  1174. {
  1175. const char *name = NULL;
  1176. unsigned long i, ret = 0;
  1177. struct module *owner;
  1178. const unsigned long *crc;
  1179. for (i = 0; i < mod->num_syms; i++)
  1180. if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
  1181. name = mod->syms[i].name;
  1182. ret = -ENOEXEC;
  1183. goto dup;
  1184. }
  1185. for (i = 0; i < mod->num_gpl_syms; i++)
  1186. if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
  1187. name = mod->gpl_syms[i].name;
  1188. ret = -ENOEXEC;
  1189. goto dup;
  1190. }
  1191. dup:
  1192. if (ret)
  1193. printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
  1194. mod->name, name, module_name(owner));
  1195. return ret;
  1196. }
  1197. /* Change all symbols so that st_value encodes the pointer directly. */
  1198. static int simplify_symbols(Elf_Shdr *sechdrs,
  1199. unsigned int symindex,
  1200. const char *strtab,
  1201. unsigned int versindex,
  1202. unsigned int pcpuindex,
  1203. struct module *mod)
  1204. {
  1205. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1206. unsigned long secbase;
  1207. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1208. int ret = 0;
  1209. for (i = 1; i < n; i++) {
  1210. switch (sym[i].st_shndx) {
  1211. case SHN_COMMON:
  1212. /* We compiled with -fno-common. These are not
  1213. supposed to happen. */
  1214. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1215. printk("%s: please compile with -fno-common\n",
  1216. mod->name);
  1217. ret = -ENOEXEC;
  1218. break;
  1219. case SHN_ABS:
  1220. /* Don't need to do anything */
  1221. DEBUGP("Absolute symbol: 0x%08lx\n",
  1222. (long)sym[i].st_value);
  1223. break;
  1224. case SHN_UNDEF:
  1225. sym[i].st_value
  1226. = resolve_symbol(sechdrs, versindex,
  1227. strtab + sym[i].st_name, mod);
  1228. /* Ok if resolved. */
  1229. if (sym[i].st_value != 0)
  1230. break;
  1231. /* Ok if weak. */
  1232. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1233. break;
  1234. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1235. mod->name, strtab + sym[i].st_name);
  1236. ret = -ENOENT;
  1237. break;
  1238. default:
  1239. /* Divert to percpu allocation if a percpu var. */
  1240. if (sym[i].st_shndx == pcpuindex)
  1241. secbase = (unsigned long)mod->percpu;
  1242. else
  1243. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1244. sym[i].st_value += secbase;
  1245. break;
  1246. }
  1247. }
  1248. return ret;
  1249. }
  1250. /* Update size with this section: return offset. */
  1251. static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
  1252. {
  1253. long ret;
  1254. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1255. *size = ret + sechdr->sh_size;
  1256. return ret;
  1257. }
  1258. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1259. might -- code, read-only data, read-write data, small data. Tally
  1260. sizes, and place the offsets into sh_entsize fields: high bit means it
  1261. belongs in init. */
  1262. static void layout_sections(struct module *mod,
  1263. const Elf_Ehdr *hdr,
  1264. Elf_Shdr *sechdrs,
  1265. const char *secstrings)
  1266. {
  1267. static unsigned long const masks[][2] = {
  1268. /* NOTE: all executable code must be the first section
  1269. * in this array; otherwise modify the text_size
  1270. * finder in the two loops below */
  1271. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1272. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1273. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1274. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1275. };
  1276. unsigned int m, i;
  1277. for (i = 0; i < hdr->e_shnum; i++)
  1278. sechdrs[i].sh_entsize = ~0UL;
  1279. DEBUGP("Core section allocation order:\n");
  1280. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1281. for (i = 0; i < hdr->e_shnum; ++i) {
  1282. Elf_Shdr *s = &sechdrs[i];
  1283. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1284. || (s->sh_flags & masks[m][1])
  1285. || s->sh_entsize != ~0UL
  1286. || strncmp(secstrings + s->sh_name,
  1287. ".init", 5) == 0)
  1288. continue;
  1289. s->sh_entsize = get_offset(&mod->core_size, s);
  1290. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1291. }
  1292. if (m == 0)
  1293. mod->core_text_size = mod->core_size;
  1294. }
  1295. DEBUGP("Init section allocation order:\n");
  1296. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1297. for (i = 0; i < hdr->e_shnum; ++i) {
  1298. Elf_Shdr *s = &sechdrs[i];
  1299. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1300. || (s->sh_flags & masks[m][1])
  1301. || s->sh_entsize != ~0UL
  1302. || strncmp(secstrings + s->sh_name,
  1303. ".init", 5) != 0)
  1304. continue;
  1305. s->sh_entsize = (get_offset(&mod->init_size, s)
  1306. | INIT_OFFSET_MASK);
  1307. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1308. }
  1309. if (m == 0)
  1310. mod->init_text_size = mod->init_size;
  1311. }
  1312. }
  1313. static void set_license(struct module *mod, const char *license)
  1314. {
  1315. if (!license)
  1316. license = "unspecified";
  1317. if (!license_is_gpl_compatible(license)) {
  1318. if (!(tainted & TAINT_PROPRIETARY_MODULE))
  1319. printk(KERN_WARNING "%s: module license '%s' taints "
  1320. "kernel.\n", mod->name, license);
  1321. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1322. }
  1323. }
  1324. /* Parse tag=value strings from .modinfo section */
  1325. static char *next_string(char *string, unsigned long *secsize)
  1326. {
  1327. /* Skip non-zero chars */
  1328. while (string[0]) {
  1329. string++;
  1330. if ((*secsize)-- <= 1)
  1331. return NULL;
  1332. }
  1333. /* Skip any zero padding. */
  1334. while (!string[0]) {
  1335. string++;
  1336. if ((*secsize)-- <= 1)
  1337. return NULL;
  1338. }
  1339. return string;
  1340. }
  1341. static char *get_modinfo(Elf_Shdr *sechdrs,
  1342. unsigned int info,
  1343. const char *tag)
  1344. {
  1345. char *p;
  1346. unsigned int taglen = strlen(tag);
  1347. unsigned long size = sechdrs[info].sh_size;
  1348. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1349. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1350. return p + taglen + 1;
  1351. }
  1352. return NULL;
  1353. }
  1354. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1355. unsigned int infoindex)
  1356. {
  1357. struct module_attribute *attr;
  1358. int i;
  1359. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1360. if (attr->setup)
  1361. attr->setup(mod,
  1362. get_modinfo(sechdrs,
  1363. infoindex,
  1364. attr->attr.name));
  1365. }
  1366. }
  1367. #ifdef CONFIG_KALLSYMS
  1368. static int is_exported(const char *name, const struct module *mod)
  1369. {
  1370. if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
  1371. return 1;
  1372. else
  1373. if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
  1374. return 1;
  1375. else
  1376. return 0;
  1377. }
  1378. /* As per nm */
  1379. static char elf_type(const Elf_Sym *sym,
  1380. Elf_Shdr *sechdrs,
  1381. const char *secstrings,
  1382. struct module *mod)
  1383. {
  1384. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1385. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1386. return 'v';
  1387. else
  1388. return 'w';
  1389. }
  1390. if (sym->st_shndx == SHN_UNDEF)
  1391. return 'U';
  1392. if (sym->st_shndx == SHN_ABS)
  1393. return 'a';
  1394. if (sym->st_shndx >= SHN_LORESERVE)
  1395. return '?';
  1396. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1397. return 't';
  1398. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1399. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1400. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1401. return 'r';
  1402. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1403. return 'g';
  1404. else
  1405. return 'd';
  1406. }
  1407. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1408. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1409. return 's';
  1410. else
  1411. return 'b';
  1412. }
  1413. if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
  1414. ".debug", strlen(".debug")) == 0)
  1415. return 'n';
  1416. return '?';
  1417. }
  1418. static void add_kallsyms(struct module *mod,
  1419. Elf_Shdr *sechdrs,
  1420. unsigned int symindex,
  1421. unsigned int strindex,
  1422. const char *secstrings)
  1423. {
  1424. unsigned int i;
  1425. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1426. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1427. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1428. /* Set types up while we still have access to sections. */
  1429. for (i = 0; i < mod->num_symtab; i++)
  1430. mod->symtab[i].st_info
  1431. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1432. }
  1433. #else
  1434. static inline void add_kallsyms(struct module *mod,
  1435. Elf_Shdr *sechdrs,
  1436. unsigned int symindex,
  1437. unsigned int strindex,
  1438. const char *secstrings)
  1439. {
  1440. }
  1441. #endif /* CONFIG_KALLSYMS */
  1442. /* Allocate and load the module: note that size of section 0 is always
  1443. zero, and we rely on this for optional sections. */
  1444. static struct module *load_module(void __user *umod,
  1445. unsigned long len,
  1446. const char __user *uargs)
  1447. {
  1448. Elf_Ehdr *hdr;
  1449. Elf_Shdr *sechdrs;
  1450. char *secstrings, *args, *modmagic, *strtab = NULL;
  1451. unsigned int i;
  1452. unsigned int symindex = 0;
  1453. unsigned int strindex = 0;
  1454. unsigned int setupindex;
  1455. unsigned int exindex;
  1456. unsigned int exportindex;
  1457. unsigned int modindex;
  1458. unsigned int obsparmindex;
  1459. unsigned int infoindex;
  1460. unsigned int gplindex;
  1461. unsigned int crcindex;
  1462. unsigned int gplcrcindex;
  1463. unsigned int versindex;
  1464. unsigned int pcpuindex;
  1465. unsigned int gplfutureindex;
  1466. unsigned int gplfuturecrcindex;
  1467. unsigned int unwindex = 0;
  1468. unsigned int unusedindex;
  1469. unsigned int unusedcrcindex;
  1470. unsigned int unusedgplindex;
  1471. unsigned int unusedgplcrcindex;
  1472. unsigned int markersindex;
  1473. unsigned int markersstringsindex;
  1474. struct module *mod;
  1475. long err = 0;
  1476. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1477. struct exception_table_entry *extable;
  1478. mm_segment_t old_fs;
  1479. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1480. umod, len, uargs);
  1481. if (len < sizeof(*hdr))
  1482. return ERR_PTR(-ENOEXEC);
  1483. /* Suck in entire file: we'll want most of it. */
  1484. /* vmalloc barfs on "unusual" numbers. Check here */
  1485. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1486. return ERR_PTR(-ENOMEM);
  1487. if (copy_from_user(hdr, umod, len) != 0) {
  1488. err = -EFAULT;
  1489. goto free_hdr;
  1490. }
  1491. /* Sanity checks against insmoding binaries or wrong arch,
  1492. weird elf version */
  1493. if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
  1494. || hdr->e_type != ET_REL
  1495. || !elf_check_arch(hdr)
  1496. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1497. err = -ENOEXEC;
  1498. goto free_hdr;
  1499. }
  1500. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1501. goto truncated;
  1502. /* Convenience variables */
  1503. sechdrs = (void *)hdr + hdr->e_shoff;
  1504. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1505. sechdrs[0].sh_addr = 0;
  1506. for (i = 1; i < hdr->e_shnum; i++) {
  1507. if (sechdrs[i].sh_type != SHT_NOBITS
  1508. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1509. goto truncated;
  1510. /* Mark all sections sh_addr with their address in the
  1511. temporary image. */
  1512. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1513. /* Internal symbols and strings. */
  1514. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1515. symindex = i;
  1516. strindex = sechdrs[i].sh_link;
  1517. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1518. }
  1519. #ifndef CONFIG_MODULE_UNLOAD
  1520. /* Don't load .exit sections */
  1521. if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
  1522. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1523. #endif
  1524. }
  1525. modindex = find_sec(hdr, sechdrs, secstrings,
  1526. ".gnu.linkonce.this_module");
  1527. if (!modindex) {
  1528. printk(KERN_WARNING "No module found in object\n");
  1529. err = -ENOEXEC;
  1530. goto free_hdr;
  1531. }
  1532. mod = (void *)sechdrs[modindex].sh_addr;
  1533. if (symindex == 0) {
  1534. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1535. mod->name);
  1536. err = -ENOEXEC;
  1537. goto free_hdr;
  1538. }
  1539. /* Optional sections */
  1540. exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
  1541. gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
  1542. gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
  1543. unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
  1544. unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
  1545. crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
  1546. gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1547. gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
  1548. unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
  1549. unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
  1550. setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
  1551. exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
  1552. obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
  1553. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1554. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1555. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1556. #ifdef ARCH_UNWIND_SECTION_NAME
  1557. unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
  1558. #endif
  1559. /* Don't keep modinfo section */
  1560. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1561. #ifdef CONFIG_KALLSYMS
  1562. /* Keep symbol and string tables for decoding later. */
  1563. sechdrs[symindex].sh_flags |= SHF_ALLOC;
  1564. sechdrs[strindex].sh_flags |= SHF_ALLOC;
  1565. #endif
  1566. if (unwindex)
  1567. sechdrs[unwindex].sh_flags |= SHF_ALLOC;
  1568. /* Check module struct version now, before we try to use module. */
  1569. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1570. err = -ENOEXEC;
  1571. goto free_hdr;
  1572. }
  1573. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1574. /* This is allowed: modprobe --force will invalidate it. */
  1575. if (!modmagic) {
  1576. add_taint_module(mod, TAINT_FORCED_MODULE);
  1577. printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
  1578. mod->name);
  1579. } else if (!same_magic(modmagic, vermagic)) {
  1580. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1581. mod->name, modmagic, vermagic);
  1582. err = -ENOEXEC;
  1583. goto free_hdr;
  1584. }
  1585. /* Now copy in args */
  1586. args = strndup_user(uargs, ~0UL >> 1);
  1587. if (IS_ERR(args)) {
  1588. err = PTR_ERR(args);
  1589. goto free_hdr;
  1590. }
  1591. if (find_module(mod->name)) {
  1592. err = -EEXIST;
  1593. goto free_mod;
  1594. }
  1595. mod->state = MODULE_STATE_COMING;
  1596. /* Allow arches to frob section contents and sizes. */
  1597. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1598. if (err < 0)
  1599. goto free_mod;
  1600. if (pcpuindex) {
  1601. /* We have a special allocation for this section. */
  1602. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1603. sechdrs[pcpuindex].sh_addralign,
  1604. mod->name);
  1605. if (!percpu) {
  1606. err = -ENOMEM;
  1607. goto free_mod;
  1608. }
  1609. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1610. mod->percpu = percpu;
  1611. }
  1612. /* Determine total sizes, and put offsets in sh_entsize. For now
  1613. this is done generically; there doesn't appear to be any
  1614. special cases for the architectures. */
  1615. layout_sections(mod, hdr, sechdrs, secstrings);
  1616. /* Do the allocs. */
  1617. ptr = module_alloc(mod->core_size);
  1618. if (!ptr) {
  1619. err = -ENOMEM;
  1620. goto free_percpu;
  1621. }
  1622. memset(ptr, 0, mod->core_size);
  1623. mod->module_core = ptr;
  1624. ptr = module_alloc(mod->init_size);
  1625. if (!ptr && mod->init_size) {
  1626. err = -ENOMEM;
  1627. goto free_core;
  1628. }
  1629. memset(ptr, 0, mod->init_size);
  1630. mod->module_init = ptr;
  1631. /* Transfer each section which specifies SHF_ALLOC */
  1632. DEBUGP("final section addresses:\n");
  1633. for (i = 0; i < hdr->e_shnum; i++) {
  1634. void *dest;
  1635. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1636. continue;
  1637. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1638. dest = mod->module_init
  1639. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1640. else
  1641. dest = mod->module_core + sechdrs[i].sh_entsize;
  1642. if (sechdrs[i].sh_type != SHT_NOBITS)
  1643. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1644. sechdrs[i].sh_size);
  1645. /* Update sh_addr to point to copy in image. */
  1646. sechdrs[i].sh_addr = (unsigned long)dest;
  1647. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1648. }
  1649. /* Module has been moved. */
  1650. mod = (void *)sechdrs[modindex].sh_addr;
  1651. /* Now we've moved module, initialize linked lists, etc. */
  1652. module_unload_init(mod);
  1653. /* add kobject, so we can reference it. */
  1654. err = mod_sysfs_init(mod);
  1655. if (err)
  1656. goto free_unload;
  1657. /* Set up license info based on the info section */
  1658. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1659. if (strcmp(mod->name, "ndiswrapper") == 0)
  1660. add_taint(TAINT_PROPRIETARY_MODULE);
  1661. if (strcmp(mod->name, "driverloader") == 0)
  1662. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1663. /* Set up MODINFO_ATTR fields */
  1664. setup_modinfo(mod, sechdrs, infoindex);
  1665. /* Fix up syms, so that st_value is a pointer to location. */
  1666. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1667. mod);
  1668. if (err < 0)
  1669. goto cleanup;
  1670. /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
  1671. mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
  1672. mod->syms = (void *)sechdrs[exportindex].sh_addr;
  1673. if (crcindex)
  1674. mod->crcs = (void *)sechdrs[crcindex].sh_addr;
  1675. mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
  1676. mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
  1677. if (gplcrcindex)
  1678. mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
  1679. mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
  1680. sizeof(*mod->gpl_future_syms);
  1681. mod->num_unused_syms = sechdrs[unusedindex].sh_size /
  1682. sizeof(*mod->unused_syms);
  1683. mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
  1684. sizeof(*mod->unused_gpl_syms);
  1685. mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
  1686. if (gplfuturecrcindex)
  1687. mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
  1688. mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
  1689. if (unusedcrcindex)
  1690. mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
  1691. mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
  1692. if (unusedgplcrcindex)
  1693. mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
  1694. #ifdef CONFIG_MODVERSIONS
  1695. if ((mod->num_syms && !crcindex) ||
  1696. (mod->num_gpl_syms && !gplcrcindex) ||
  1697. (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
  1698. (mod->num_unused_syms && !unusedcrcindex) ||
  1699. (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
  1700. printk(KERN_WARNING "%s: No versions for exported symbols."
  1701. " Tainting kernel.\n", mod->name);
  1702. add_taint_module(mod, TAINT_FORCED_MODULE);
  1703. }
  1704. #endif
  1705. markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
  1706. markersstringsindex = find_sec(hdr, sechdrs, secstrings,
  1707. "__markers_strings");
  1708. /* Now do relocations. */
  1709. for (i = 1; i < hdr->e_shnum; i++) {
  1710. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1711. unsigned int info = sechdrs[i].sh_info;
  1712. /* Not a valid relocation section? */
  1713. if (info >= hdr->e_shnum)
  1714. continue;
  1715. /* Don't bother with non-allocated sections */
  1716. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1717. continue;
  1718. if (sechdrs[i].sh_type == SHT_REL)
  1719. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1720. else if (sechdrs[i].sh_type == SHT_RELA)
  1721. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1722. mod);
  1723. if (err < 0)
  1724. goto cleanup;
  1725. }
  1726. #ifdef CONFIG_MARKERS
  1727. mod->markers = (void *)sechdrs[markersindex].sh_addr;
  1728. mod->num_markers =
  1729. sechdrs[markersindex].sh_size / sizeof(*mod->markers);
  1730. #endif
  1731. /* Find duplicate symbols */
  1732. err = verify_export_symbols(mod);
  1733. if (err < 0)
  1734. goto cleanup;
  1735. /* Set up and sort exception table */
  1736. mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
  1737. mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
  1738. sort_extable(extable, extable + mod->num_exentries);
  1739. /* Finally, copy percpu area over. */
  1740. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1741. sechdrs[pcpuindex].sh_size);
  1742. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1743. #ifdef CONFIG_MARKERS
  1744. if (!mod->taints)
  1745. marker_update_probe_range(mod->markers,
  1746. mod->markers + mod->num_markers, NULL, NULL);
  1747. #endif
  1748. err = module_finalize(hdr, sechdrs, mod);
  1749. if (err < 0)
  1750. goto cleanup;
  1751. /* flush the icache in correct context */
  1752. old_fs = get_fs();
  1753. set_fs(KERNEL_DS);
  1754. /*
  1755. * Flush the instruction cache, since we've played with text.
  1756. * Do it before processing of module parameters, so the module
  1757. * can provide parameter accessor functions of its own.
  1758. */
  1759. if (mod->module_init)
  1760. flush_icache_range((unsigned long)mod->module_init,
  1761. (unsigned long)mod->module_init
  1762. + mod->init_size);
  1763. flush_icache_range((unsigned long)mod->module_core,
  1764. (unsigned long)mod->module_core + mod->core_size);
  1765. set_fs(old_fs);
  1766. mod->args = args;
  1767. if (obsparmindex)
  1768. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1769. mod->name);
  1770. /* Size of section 0 is 0, so this works well if no params */
  1771. err = parse_args(mod->name, mod->args,
  1772. (struct kernel_param *)
  1773. sechdrs[setupindex].sh_addr,
  1774. sechdrs[setupindex].sh_size
  1775. / sizeof(struct kernel_param),
  1776. NULL);
  1777. if (err < 0)
  1778. goto arch_cleanup;
  1779. err = mod_sysfs_setup(mod,
  1780. (struct kernel_param *)
  1781. sechdrs[setupindex].sh_addr,
  1782. sechdrs[setupindex].sh_size
  1783. / sizeof(struct kernel_param));
  1784. if (err < 0)
  1785. goto arch_cleanup;
  1786. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1787. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1788. /* Size of section 0 is 0, so this works well if no unwind info. */
  1789. mod->unwind_info = unwind_add_table(mod,
  1790. (void *)sechdrs[unwindex].sh_addr,
  1791. sechdrs[unwindex].sh_size);
  1792. /* Get rid of temporary copy */
  1793. vfree(hdr);
  1794. /* Done! */
  1795. return mod;
  1796. arch_cleanup:
  1797. module_arch_cleanup(mod);
  1798. cleanup:
  1799. kobject_del(&mod->mkobj.kobj);
  1800. kobject_put(&mod->mkobj.kobj);
  1801. free_unload:
  1802. module_unload_free(mod);
  1803. module_free(mod, mod->module_init);
  1804. free_core:
  1805. module_free(mod, mod->module_core);
  1806. free_percpu:
  1807. if (percpu)
  1808. percpu_modfree(percpu);
  1809. free_mod:
  1810. kfree(args);
  1811. free_hdr:
  1812. vfree(hdr);
  1813. return ERR_PTR(err);
  1814. truncated:
  1815. printk(KERN_ERR "Module len %lu truncated\n", len);
  1816. err = -ENOEXEC;
  1817. goto free_hdr;
  1818. }
  1819. /*
  1820. * link the module with the whole machine is stopped with interrupts off
  1821. * - this defends against kallsyms not taking locks
  1822. */
  1823. static int __link_module(void *_mod)
  1824. {
  1825. struct module *mod = _mod;
  1826. list_add(&mod->list, &modules);
  1827. return 0;
  1828. }
  1829. /* This is where the real work happens */
  1830. asmlinkage long
  1831. sys_init_module(void __user *umod,
  1832. unsigned long len,
  1833. const char __user *uargs)
  1834. {
  1835. struct module *mod;
  1836. int ret = 0;
  1837. /* Must have permission */
  1838. if (!capable(CAP_SYS_MODULE))
  1839. return -EPERM;
  1840. /* Only one module load at a time, please */
  1841. if (mutex_lock_interruptible(&module_mutex) != 0)
  1842. return -EINTR;
  1843. /* Do all the hard work */
  1844. mod = load_module(umod, len, uargs);
  1845. if (IS_ERR(mod)) {
  1846. mutex_unlock(&module_mutex);
  1847. return PTR_ERR(mod);
  1848. }
  1849. /* Now sew it into the lists. They won't access us, since
  1850. strong_try_module_get() will fail. */
  1851. stop_machine_run(__link_module, mod, NR_CPUS);
  1852. /* Drop lock so they can recurse */
  1853. mutex_unlock(&module_mutex);
  1854. blocking_notifier_call_chain(&module_notify_list,
  1855. MODULE_STATE_COMING, mod);
  1856. /* Start the module */
  1857. if (mod->init != NULL)
  1858. ret = mod->init();
  1859. if (ret < 0) {
  1860. /* Init routine failed: abort. Try to protect us from
  1861. buggy refcounters. */
  1862. mod->state = MODULE_STATE_GOING;
  1863. synchronize_sched();
  1864. module_put(mod);
  1865. mutex_lock(&module_mutex);
  1866. free_module(mod);
  1867. mutex_unlock(&module_mutex);
  1868. wake_up(&module_wq);
  1869. return ret;
  1870. }
  1871. /* Now it's a first class citizen! */
  1872. mutex_lock(&module_mutex);
  1873. mod->state = MODULE_STATE_LIVE;
  1874. /* Drop initial reference. */
  1875. module_put(mod);
  1876. unwind_remove_table(mod->unwind_info, 1);
  1877. module_free(mod, mod->module_init);
  1878. mod->module_init = NULL;
  1879. mod->init_size = 0;
  1880. mod->init_text_size = 0;
  1881. mutex_unlock(&module_mutex);
  1882. wake_up(&module_wq);
  1883. return 0;
  1884. }
  1885. static inline int within(unsigned long addr, void *start, unsigned long size)
  1886. {
  1887. return ((void *)addr >= start && (void *)addr < start + size);
  1888. }
  1889. #ifdef CONFIG_KALLSYMS
  1890. /*
  1891. * This ignores the intensely annoying "mapping symbols" found
  1892. * in ARM ELF files: $a, $t and $d.
  1893. */
  1894. static inline int is_arm_mapping_symbol(const char *str)
  1895. {
  1896. return str[0] == '$' && strchr("atd", str[1])
  1897. && (str[2] == '\0' || str[2] == '.');
  1898. }
  1899. static const char *get_ksymbol(struct module *mod,
  1900. unsigned long addr,
  1901. unsigned long *size,
  1902. unsigned long *offset)
  1903. {
  1904. unsigned int i, best = 0;
  1905. unsigned long nextval;
  1906. /* At worse, next value is at end of module */
  1907. if (within(addr, mod->module_init, mod->init_size))
  1908. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  1909. else
  1910. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  1911. /* Scan for closest preceeding symbol, and next symbol. (ELF
  1912. starts real symbols at 1). */
  1913. for (i = 1; i < mod->num_symtab; i++) {
  1914. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  1915. continue;
  1916. /* We ignore unnamed symbols: they're uninformative
  1917. * and inserted at a whim. */
  1918. if (mod->symtab[i].st_value <= addr
  1919. && mod->symtab[i].st_value > mod->symtab[best].st_value
  1920. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1921. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1922. best = i;
  1923. if (mod->symtab[i].st_value > addr
  1924. && mod->symtab[i].st_value < nextval
  1925. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1926. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1927. nextval = mod->symtab[i].st_value;
  1928. }
  1929. if (!best)
  1930. return NULL;
  1931. if (size)
  1932. *size = nextval - mod->symtab[best].st_value;
  1933. if (offset)
  1934. *offset = addr - mod->symtab[best].st_value;
  1935. return mod->strtab + mod->symtab[best].st_name;
  1936. }
  1937. /* For kallsyms to ask for address resolution. NULL means not found.
  1938. We don't lock, as this is used for oops resolution and races are a
  1939. lesser concern. */
  1940. /* FIXME: Risky: returns a pointer into a module w/o lock */
  1941. const char *module_address_lookup(unsigned long addr,
  1942. unsigned long *size,
  1943. unsigned long *offset,
  1944. char **modname)
  1945. {
  1946. struct module *mod;
  1947. const char *ret = NULL;
  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. if (modname)
  1953. *modname = mod->name;
  1954. ret = get_ksymbol(mod, addr, size, offset);
  1955. break;
  1956. }
  1957. }
  1958. preempt_enable();
  1959. return ret;
  1960. }
  1961. int lookup_module_symbol_name(unsigned long addr, char *symname)
  1962. {
  1963. struct module *mod;
  1964. preempt_disable();
  1965. list_for_each_entry(mod, &modules, list) {
  1966. if (within(addr, mod->module_init, mod->init_size) ||
  1967. within(addr, mod->module_core, mod->core_size)) {
  1968. const char *sym;
  1969. sym = get_ksymbol(mod, addr, NULL, NULL);
  1970. if (!sym)
  1971. goto out;
  1972. strlcpy(symname, sym, KSYM_NAME_LEN);
  1973. preempt_enable();
  1974. return 0;
  1975. }
  1976. }
  1977. out:
  1978. preempt_enable();
  1979. return -ERANGE;
  1980. }
  1981. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  1982. unsigned long *offset, char *modname, char *name)
  1983. {
  1984. struct module *mod;
  1985. preempt_disable();
  1986. list_for_each_entry(mod, &modules, list) {
  1987. if (within(addr, mod->module_init, mod->init_size) ||
  1988. within(addr, mod->module_core, mod->core_size)) {
  1989. const char *sym;
  1990. sym = get_ksymbol(mod, addr, size, offset);
  1991. if (!sym)
  1992. goto out;
  1993. if (modname)
  1994. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  1995. if (name)
  1996. strlcpy(name, sym, KSYM_NAME_LEN);
  1997. preempt_enable();
  1998. return 0;
  1999. }
  2000. }
  2001. out:
  2002. preempt_enable();
  2003. return -ERANGE;
  2004. }
  2005. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2006. char *name, char *module_name, int *exported)
  2007. {
  2008. struct module *mod;
  2009. preempt_disable();
  2010. list_for_each_entry(mod, &modules, list) {
  2011. if (symnum < mod->num_symtab) {
  2012. *value = mod->symtab[symnum].st_value;
  2013. *type = mod->symtab[symnum].st_info;
  2014. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2015. KSYM_NAME_LEN);
  2016. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2017. *exported = is_exported(name, mod);
  2018. preempt_enable();
  2019. return 0;
  2020. }
  2021. symnum -= mod->num_symtab;
  2022. }
  2023. preempt_enable();
  2024. return -ERANGE;
  2025. }
  2026. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2027. {
  2028. unsigned int i;
  2029. for (i = 0; i < mod->num_symtab; i++)
  2030. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2031. mod->symtab[i].st_info != 'U')
  2032. return mod->symtab[i].st_value;
  2033. return 0;
  2034. }
  2035. /* Look for this name: can be of form module:name. */
  2036. unsigned long module_kallsyms_lookup_name(const char *name)
  2037. {
  2038. struct module *mod;
  2039. char *colon;
  2040. unsigned long ret = 0;
  2041. /* Don't lock: we're in enough trouble already. */
  2042. preempt_disable();
  2043. if ((colon = strchr(name, ':')) != NULL) {
  2044. *colon = '\0';
  2045. if ((mod = find_module(name)) != NULL)
  2046. ret = mod_find_symname(mod, colon+1);
  2047. *colon = ':';
  2048. } else {
  2049. list_for_each_entry(mod, &modules, list)
  2050. if ((ret = mod_find_symname(mod, name)) != 0)
  2051. break;
  2052. }
  2053. preempt_enable();
  2054. return ret;
  2055. }
  2056. #endif /* CONFIG_KALLSYMS */
  2057. /* Called by the /proc file system to return a list of modules. */
  2058. static void *m_start(struct seq_file *m, loff_t *pos)
  2059. {
  2060. mutex_lock(&module_mutex);
  2061. return seq_list_start(&modules, *pos);
  2062. }
  2063. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2064. {
  2065. return seq_list_next(p, &modules, pos);
  2066. }
  2067. static void m_stop(struct seq_file *m, void *p)
  2068. {
  2069. mutex_unlock(&module_mutex);
  2070. }
  2071. static char *module_flags(struct module *mod, char *buf)
  2072. {
  2073. int bx = 0;
  2074. if (mod->taints ||
  2075. mod->state == MODULE_STATE_GOING ||
  2076. mod->state == MODULE_STATE_COMING) {
  2077. buf[bx++] = '(';
  2078. if (mod->taints & TAINT_PROPRIETARY_MODULE)
  2079. buf[bx++] = 'P';
  2080. if (mod->taints & TAINT_FORCED_MODULE)
  2081. buf[bx++] = 'F';
  2082. /*
  2083. * TAINT_FORCED_RMMOD: could be added.
  2084. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2085. * apply to modules.
  2086. */
  2087. /* Show a - for module-is-being-unloaded */
  2088. if (mod->state == MODULE_STATE_GOING)
  2089. buf[bx++] = '-';
  2090. /* Show a + for module-is-being-loaded */
  2091. if (mod->state == MODULE_STATE_COMING)
  2092. buf[bx++] = '+';
  2093. buf[bx++] = ')';
  2094. }
  2095. buf[bx] = '\0';
  2096. return buf;
  2097. }
  2098. static int m_show(struct seq_file *m, void *p)
  2099. {
  2100. struct module *mod = list_entry(p, struct module, list);
  2101. char buf[8];
  2102. seq_printf(m, "%s %lu",
  2103. mod->name, mod->init_size + mod->core_size);
  2104. print_unload_info(m, mod);
  2105. /* Informative for users. */
  2106. seq_printf(m, " %s",
  2107. mod->state == MODULE_STATE_GOING ? "Unloading":
  2108. mod->state == MODULE_STATE_COMING ? "Loading":
  2109. "Live");
  2110. /* Used by oprofile and other similar tools. */
  2111. seq_printf(m, " 0x%p", mod->module_core);
  2112. /* Taints info */
  2113. if (mod->taints)
  2114. seq_printf(m, " %s", module_flags(mod, buf));
  2115. seq_printf(m, "\n");
  2116. return 0;
  2117. }
  2118. /* Format: modulename size refcount deps address
  2119. Where refcount is a number or -, and deps is a comma-separated list
  2120. of depends or -.
  2121. */
  2122. const struct seq_operations modules_op = {
  2123. .start = m_start,
  2124. .next = m_next,
  2125. .stop = m_stop,
  2126. .show = m_show
  2127. };
  2128. /* Given an address, look for it in the module exception tables. */
  2129. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2130. {
  2131. const struct exception_table_entry *e = NULL;
  2132. struct module *mod;
  2133. preempt_disable();
  2134. list_for_each_entry(mod, &modules, list) {
  2135. if (mod->num_exentries == 0)
  2136. continue;
  2137. e = search_extable(mod->extable,
  2138. mod->extable + mod->num_exentries - 1,
  2139. addr);
  2140. if (e)
  2141. break;
  2142. }
  2143. preempt_enable();
  2144. /* Now, if we found one, we are running inside it now, hence
  2145. we cannot unload the module, hence no refcnt needed. */
  2146. return e;
  2147. }
  2148. /*
  2149. * Is this a valid module address?
  2150. */
  2151. int is_module_address(unsigned long addr)
  2152. {
  2153. struct module *mod;
  2154. preempt_disable();
  2155. list_for_each_entry(mod, &modules, list) {
  2156. if (within(addr, mod->module_core, mod->core_size)) {
  2157. preempt_enable();
  2158. return 1;
  2159. }
  2160. }
  2161. preempt_enable();
  2162. return 0;
  2163. }
  2164. /* Is this a valid kernel address? */
  2165. struct module *__module_text_address(unsigned long addr)
  2166. {
  2167. struct module *mod;
  2168. list_for_each_entry(mod, &modules, list)
  2169. if (within(addr, mod->module_init, mod->init_text_size)
  2170. || within(addr, mod->module_core, mod->core_text_size))
  2171. return mod;
  2172. return NULL;
  2173. }
  2174. struct module *module_text_address(unsigned long addr)
  2175. {
  2176. struct module *mod;
  2177. preempt_disable();
  2178. mod = __module_text_address(addr);
  2179. preempt_enable();
  2180. return mod;
  2181. }
  2182. /* Don't grab lock, we're oopsing. */
  2183. void print_modules(void)
  2184. {
  2185. struct module *mod;
  2186. char buf[8];
  2187. printk("Modules linked in:");
  2188. list_for_each_entry(mod, &modules, list)
  2189. printk(" %s%s", mod->name, module_flags(mod, buf));
  2190. if (last_unloaded_module[0])
  2191. printk(" [last unloaded: %s]", last_unloaded_module);
  2192. printk("\n");
  2193. }
  2194. #ifdef CONFIG_MODVERSIONS
  2195. /* Generate the signature for struct module here, too, for modversions. */
  2196. void struct_module(struct module *mod) { return; }
  2197. EXPORT_SYMBOL(struct_module);
  2198. #endif
  2199. #ifdef CONFIG_MARKERS
  2200. void module_update_markers(struct module *probe_module, int *refcount)
  2201. {
  2202. struct module *mod;
  2203. mutex_lock(&module_mutex);
  2204. list_for_each_entry(mod, &modules, list)
  2205. if (!mod->taints)
  2206. marker_update_probe_range(mod->markers,
  2207. mod->markers + mod->num_markers,
  2208. probe_module, refcount);
  2209. mutex_unlock(&module_mutex);
  2210. }
  2211. #endif