module.c 67 KB

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