module.c 68 KB

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