module.c 63 KB

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