module.c 63 KB

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