module.c 66 KB

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