module.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648
  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. static void mod_sysfs_fini(struct module *mod)
  1156. {
  1157. kobject_put(&mod->mkobj.kobj);
  1158. }
  1159. #else /* CONFIG_SYSFS */
  1160. static void mod_sysfs_fini(struct module *mod)
  1161. {
  1162. }
  1163. #endif /* CONFIG_SYSFS */
  1164. static void mod_kobject_remove(struct module *mod)
  1165. {
  1166. module_remove_modinfo_attrs(mod);
  1167. module_param_sysfs_remove(mod);
  1168. kobject_put(mod->mkobj.drivers_dir);
  1169. kobject_put(mod->holders_dir);
  1170. mod_sysfs_fini(mod);
  1171. }
  1172. /*
  1173. * link the module with the whole machine is stopped with interrupts off
  1174. * - this defends against kallsyms not taking locks
  1175. */
  1176. static int __link_module(void *_mod)
  1177. {
  1178. struct module *mod = _mod;
  1179. list_add(&mod->list, &modules);
  1180. return 0;
  1181. }
  1182. /*
  1183. * unlink the module with the whole machine is stopped with interrupts off
  1184. * - this defends against kallsyms not taking locks
  1185. */
  1186. static int __unlink_module(void *_mod)
  1187. {
  1188. struct module *mod = _mod;
  1189. list_del(&mod->list);
  1190. return 0;
  1191. }
  1192. /* Free a module, remove from lists, etc (must hold module_mutex). */
  1193. static void free_module(struct module *mod)
  1194. {
  1195. /* Delete from various lists */
  1196. stop_machine_run(__unlink_module, mod, NR_CPUS);
  1197. remove_notes_attrs(mod);
  1198. remove_sect_attrs(mod);
  1199. mod_kobject_remove(mod);
  1200. unwind_remove_table(mod->unwind_info, 0);
  1201. /* Arch-specific cleanup. */
  1202. module_arch_cleanup(mod);
  1203. /* Module unload stuff */
  1204. module_unload_free(mod);
  1205. /* This may be NULL, but that's OK */
  1206. module_free(mod, mod->module_init);
  1207. kfree(mod->args);
  1208. if (mod->percpu)
  1209. percpu_modfree(mod->percpu);
  1210. /* Free lock-classes: */
  1211. lockdep_free_key_range(mod->module_core, mod->core_size);
  1212. /* Finally, free the core (containing the module structure) */
  1213. module_free(mod, mod->module_core);
  1214. }
  1215. void *__symbol_get(const char *symbol)
  1216. {
  1217. struct module *owner;
  1218. unsigned long value;
  1219. preempt_disable();
  1220. value = find_symbol(symbol, &owner, NULL, true, true);
  1221. if (IS_ERR_VALUE(value))
  1222. value = 0;
  1223. else if (strong_try_module_get(owner))
  1224. value = 0;
  1225. preempt_enable();
  1226. return (void *)value;
  1227. }
  1228. EXPORT_SYMBOL_GPL(__symbol_get);
  1229. /*
  1230. * Ensure that an exported symbol [global namespace] does not already exist
  1231. * in the kernel or in some other module's exported symbol table.
  1232. */
  1233. static int verify_export_symbols(struct module *mod)
  1234. {
  1235. unsigned int i;
  1236. struct module *owner;
  1237. const struct kernel_symbol *s;
  1238. struct {
  1239. const struct kernel_symbol *sym;
  1240. unsigned int num;
  1241. } arr[] = {
  1242. { mod->syms, mod->num_syms },
  1243. { mod->gpl_syms, mod->num_gpl_syms },
  1244. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1245. { mod->unused_syms, mod->num_unused_syms },
  1246. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1247. };
  1248. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1249. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1250. if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
  1251. NULL, true, false))) {
  1252. printk(KERN_ERR
  1253. "%s: exports duplicate symbol %s"
  1254. " (owned by %s)\n",
  1255. mod->name, s->name, module_name(owner));
  1256. return -ENOEXEC;
  1257. }
  1258. }
  1259. }
  1260. return 0;
  1261. }
  1262. /* Change all symbols so that st_value encodes the pointer directly. */
  1263. static int simplify_symbols(Elf_Shdr *sechdrs,
  1264. unsigned int symindex,
  1265. const char *strtab,
  1266. unsigned int versindex,
  1267. unsigned int pcpuindex,
  1268. struct module *mod)
  1269. {
  1270. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1271. unsigned long secbase;
  1272. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1273. int ret = 0;
  1274. for (i = 1; i < n; i++) {
  1275. switch (sym[i].st_shndx) {
  1276. case SHN_COMMON:
  1277. /* We compiled with -fno-common. These are not
  1278. supposed to happen. */
  1279. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1280. printk("%s: please compile with -fno-common\n",
  1281. mod->name);
  1282. ret = -ENOEXEC;
  1283. break;
  1284. case SHN_ABS:
  1285. /* Don't need to do anything */
  1286. DEBUGP("Absolute symbol: 0x%08lx\n",
  1287. (long)sym[i].st_value);
  1288. break;
  1289. case SHN_UNDEF:
  1290. sym[i].st_value
  1291. = resolve_symbol(sechdrs, versindex,
  1292. strtab + sym[i].st_name, mod);
  1293. /* Ok if resolved. */
  1294. if (!IS_ERR_VALUE(sym[i].st_value))
  1295. break;
  1296. /* Ok if weak. */
  1297. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1298. break;
  1299. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1300. mod->name, strtab + sym[i].st_name);
  1301. ret = -ENOENT;
  1302. break;
  1303. default:
  1304. /* Divert to percpu allocation if a percpu var. */
  1305. if (sym[i].st_shndx == pcpuindex)
  1306. secbase = (unsigned long)mod->percpu;
  1307. else
  1308. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1309. sym[i].st_value += secbase;
  1310. break;
  1311. }
  1312. }
  1313. return ret;
  1314. }
  1315. /* Update size with this section: return offset. */
  1316. static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
  1317. {
  1318. long ret;
  1319. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1320. *size = ret + sechdr->sh_size;
  1321. return ret;
  1322. }
  1323. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1324. might -- code, read-only data, read-write data, small data. Tally
  1325. sizes, and place the offsets into sh_entsize fields: high bit means it
  1326. belongs in init. */
  1327. static void layout_sections(struct module *mod,
  1328. const Elf_Ehdr *hdr,
  1329. Elf_Shdr *sechdrs,
  1330. const char *secstrings)
  1331. {
  1332. static unsigned long const masks[][2] = {
  1333. /* NOTE: all executable code must be the first section
  1334. * in this array; otherwise modify the text_size
  1335. * finder in the two loops below */
  1336. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1337. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1338. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1339. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1340. };
  1341. unsigned int m, i;
  1342. for (i = 0; i < hdr->e_shnum; i++)
  1343. sechdrs[i].sh_entsize = ~0UL;
  1344. DEBUGP("Core section allocation order:\n");
  1345. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1346. for (i = 0; i < hdr->e_shnum; ++i) {
  1347. Elf_Shdr *s = &sechdrs[i];
  1348. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1349. || (s->sh_flags & masks[m][1])
  1350. || s->sh_entsize != ~0UL
  1351. || strncmp(secstrings + s->sh_name,
  1352. ".init", 5) == 0)
  1353. continue;
  1354. s->sh_entsize = get_offset(&mod->core_size, s);
  1355. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1356. }
  1357. if (m == 0)
  1358. mod->core_text_size = mod->core_size;
  1359. }
  1360. DEBUGP("Init section allocation order:\n");
  1361. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1362. for (i = 0; i < hdr->e_shnum; ++i) {
  1363. Elf_Shdr *s = &sechdrs[i];
  1364. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1365. || (s->sh_flags & masks[m][1])
  1366. || s->sh_entsize != ~0UL
  1367. || strncmp(secstrings + s->sh_name,
  1368. ".init", 5) != 0)
  1369. continue;
  1370. s->sh_entsize = (get_offset(&mod->init_size, s)
  1371. | INIT_OFFSET_MASK);
  1372. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1373. }
  1374. if (m == 0)
  1375. mod->init_text_size = mod->init_size;
  1376. }
  1377. }
  1378. static void set_license(struct module *mod, const char *license)
  1379. {
  1380. if (!license)
  1381. license = "unspecified";
  1382. if (!license_is_gpl_compatible(license)) {
  1383. if (!(tainted & TAINT_PROPRIETARY_MODULE))
  1384. printk(KERN_WARNING "%s: module license '%s' taints "
  1385. "kernel.\n", mod->name, license);
  1386. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1387. }
  1388. }
  1389. /* Parse tag=value strings from .modinfo section */
  1390. static char *next_string(char *string, unsigned long *secsize)
  1391. {
  1392. /* Skip non-zero chars */
  1393. while (string[0]) {
  1394. string++;
  1395. if ((*secsize)-- <= 1)
  1396. return NULL;
  1397. }
  1398. /* Skip any zero padding. */
  1399. while (!string[0]) {
  1400. string++;
  1401. if ((*secsize)-- <= 1)
  1402. return NULL;
  1403. }
  1404. return string;
  1405. }
  1406. static char *get_modinfo(Elf_Shdr *sechdrs,
  1407. unsigned int info,
  1408. const char *tag)
  1409. {
  1410. char *p;
  1411. unsigned int taglen = strlen(tag);
  1412. unsigned long size = sechdrs[info].sh_size;
  1413. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1414. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1415. return p + taglen + 1;
  1416. }
  1417. return NULL;
  1418. }
  1419. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1420. unsigned int infoindex)
  1421. {
  1422. struct module_attribute *attr;
  1423. int i;
  1424. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1425. if (attr->setup)
  1426. attr->setup(mod,
  1427. get_modinfo(sechdrs,
  1428. infoindex,
  1429. attr->attr.name));
  1430. }
  1431. }
  1432. #ifdef CONFIG_KALLSYMS
  1433. static int is_exported(const char *name, const struct module *mod)
  1434. {
  1435. if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
  1436. return 1;
  1437. else
  1438. if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
  1439. return 1;
  1440. else
  1441. return 0;
  1442. }
  1443. /* As per nm */
  1444. static char elf_type(const Elf_Sym *sym,
  1445. Elf_Shdr *sechdrs,
  1446. const char *secstrings,
  1447. struct module *mod)
  1448. {
  1449. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1450. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1451. return 'v';
  1452. else
  1453. return 'w';
  1454. }
  1455. if (sym->st_shndx == SHN_UNDEF)
  1456. return 'U';
  1457. if (sym->st_shndx == SHN_ABS)
  1458. return 'a';
  1459. if (sym->st_shndx >= SHN_LORESERVE)
  1460. return '?';
  1461. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1462. return 't';
  1463. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1464. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1465. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1466. return 'r';
  1467. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1468. return 'g';
  1469. else
  1470. return 'd';
  1471. }
  1472. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1473. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1474. return 's';
  1475. else
  1476. return 'b';
  1477. }
  1478. if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
  1479. ".debug", strlen(".debug")) == 0)
  1480. return 'n';
  1481. return '?';
  1482. }
  1483. static void add_kallsyms(struct module *mod,
  1484. Elf_Shdr *sechdrs,
  1485. unsigned int symindex,
  1486. unsigned int strindex,
  1487. const char *secstrings)
  1488. {
  1489. unsigned int i;
  1490. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1491. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1492. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1493. /* Set types up while we still have access to sections. */
  1494. for (i = 0; i < mod->num_symtab; i++)
  1495. mod->symtab[i].st_info
  1496. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1497. }
  1498. #else
  1499. static inline void add_kallsyms(struct module *mod,
  1500. Elf_Shdr *sechdrs,
  1501. unsigned int symindex,
  1502. unsigned int strindex,
  1503. const char *secstrings)
  1504. {
  1505. }
  1506. #endif /* CONFIG_KALLSYMS */
  1507. /* Allocate and load the module: note that size of section 0 is always
  1508. zero, and we rely on this for optional sections. */
  1509. static struct module *load_module(void __user *umod,
  1510. unsigned long len,
  1511. const char __user *uargs)
  1512. {
  1513. Elf_Ehdr *hdr;
  1514. Elf_Shdr *sechdrs;
  1515. char *secstrings, *args, *modmagic, *strtab = NULL;
  1516. unsigned int i;
  1517. unsigned int symindex = 0;
  1518. unsigned int strindex = 0;
  1519. unsigned int setupindex;
  1520. unsigned int exindex;
  1521. unsigned int exportindex;
  1522. unsigned int modindex;
  1523. unsigned int obsparmindex;
  1524. unsigned int infoindex;
  1525. unsigned int gplindex;
  1526. unsigned int crcindex;
  1527. unsigned int gplcrcindex;
  1528. unsigned int versindex;
  1529. unsigned int pcpuindex;
  1530. unsigned int gplfutureindex;
  1531. unsigned int gplfuturecrcindex;
  1532. unsigned int unwindex = 0;
  1533. unsigned int unusedindex;
  1534. unsigned int unusedcrcindex;
  1535. unsigned int unusedgplindex;
  1536. unsigned int unusedgplcrcindex;
  1537. unsigned int markersindex;
  1538. unsigned int markersstringsindex;
  1539. struct module *mod;
  1540. long err = 0;
  1541. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1542. struct exception_table_entry *extable;
  1543. mm_segment_t old_fs;
  1544. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1545. umod, len, uargs);
  1546. if (len < sizeof(*hdr))
  1547. return ERR_PTR(-ENOEXEC);
  1548. /* Suck in entire file: we'll want most of it. */
  1549. /* vmalloc barfs on "unusual" numbers. Check here */
  1550. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1551. return ERR_PTR(-ENOMEM);
  1552. if (copy_from_user(hdr, umod, len) != 0) {
  1553. err = -EFAULT;
  1554. goto free_hdr;
  1555. }
  1556. /* Sanity checks against insmoding binaries or wrong arch,
  1557. weird elf version */
  1558. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
  1559. || hdr->e_type != ET_REL
  1560. || !elf_check_arch(hdr)
  1561. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1562. err = -ENOEXEC;
  1563. goto free_hdr;
  1564. }
  1565. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1566. goto truncated;
  1567. /* Convenience variables */
  1568. sechdrs = (void *)hdr + hdr->e_shoff;
  1569. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1570. sechdrs[0].sh_addr = 0;
  1571. for (i = 1; i < hdr->e_shnum; i++) {
  1572. if (sechdrs[i].sh_type != SHT_NOBITS
  1573. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1574. goto truncated;
  1575. /* Mark all sections sh_addr with their address in the
  1576. temporary image. */
  1577. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1578. /* Internal symbols and strings. */
  1579. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1580. symindex = i;
  1581. strindex = sechdrs[i].sh_link;
  1582. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1583. }
  1584. #ifndef CONFIG_MODULE_UNLOAD
  1585. /* Don't load .exit sections */
  1586. if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
  1587. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1588. #endif
  1589. }
  1590. modindex = find_sec(hdr, sechdrs, secstrings,
  1591. ".gnu.linkonce.this_module");
  1592. if (!modindex) {
  1593. printk(KERN_WARNING "No module found in object\n");
  1594. err = -ENOEXEC;
  1595. goto free_hdr;
  1596. }
  1597. mod = (void *)sechdrs[modindex].sh_addr;
  1598. if (symindex == 0) {
  1599. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1600. mod->name);
  1601. err = -ENOEXEC;
  1602. goto free_hdr;
  1603. }
  1604. /* Optional sections */
  1605. exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
  1606. gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
  1607. gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
  1608. unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
  1609. unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
  1610. crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
  1611. gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1612. gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
  1613. unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
  1614. unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
  1615. setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
  1616. exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
  1617. obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
  1618. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1619. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1620. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1621. #ifdef ARCH_UNWIND_SECTION_NAME
  1622. unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
  1623. #endif
  1624. /* Don't keep modinfo and version sections. */
  1625. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1626. sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1627. #ifdef CONFIG_KALLSYMS
  1628. /* Keep symbol and string tables for decoding later. */
  1629. sechdrs[symindex].sh_flags |= SHF_ALLOC;
  1630. sechdrs[strindex].sh_flags |= SHF_ALLOC;
  1631. #endif
  1632. if (unwindex)
  1633. sechdrs[unwindex].sh_flags |= SHF_ALLOC;
  1634. /* Check module struct version now, before we try to use module. */
  1635. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1636. err = -ENOEXEC;
  1637. goto free_hdr;
  1638. }
  1639. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1640. /* This is allowed: modprobe --force will invalidate it. */
  1641. if (!modmagic) {
  1642. err = try_to_force_load(mod, "magic");
  1643. if (err)
  1644. goto free_hdr;
  1645. } else if (!same_magic(modmagic, vermagic, versindex)) {
  1646. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1647. mod->name, modmagic, vermagic);
  1648. err = -ENOEXEC;
  1649. goto free_hdr;
  1650. }
  1651. /* Now copy in args */
  1652. args = strndup_user(uargs, ~0UL >> 1);
  1653. if (IS_ERR(args)) {
  1654. err = PTR_ERR(args);
  1655. goto free_hdr;
  1656. }
  1657. if (find_module(mod->name)) {
  1658. err = -EEXIST;
  1659. goto free_mod;
  1660. }
  1661. mod->state = MODULE_STATE_COMING;
  1662. /* Allow arches to frob section contents and sizes. */
  1663. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1664. if (err < 0)
  1665. goto free_mod;
  1666. if (pcpuindex) {
  1667. /* We have a special allocation for this section. */
  1668. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1669. sechdrs[pcpuindex].sh_addralign,
  1670. mod->name);
  1671. if (!percpu) {
  1672. err = -ENOMEM;
  1673. goto free_mod;
  1674. }
  1675. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1676. mod->percpu = percpu;
  1677. }
  1678. /* Determine total sizes, and put offsets in sh_entsize. For now
  1679. this is done generically; there doesn't appear to be any
  1680. special cases for the architectures. */
  1681. layout_sections(mod, hdr, sechdrs, secstrings);
  1682. /* Do the allocs. */
  1683. ptr = module_alloc(mod->core_size);
  1684. if (!ptr) {
  1685. err = -ENOMEM;
  1686. goto free_percpu;
  1687. }
  1688. memset(ptr, 0, mod->core_size);
  1689. mod->module_core = ptr;
  1690. ptr = module_alloc(mod->init_size);
  1691. if (!ptr && mod->init_size) {
  1692. err = -ENOMEM;
  1693. goto free_core;
  1694. }
  1695. memset(ptr, 0, mod->init_size);
  1696. mod->module_init = ptr;
  1697. /* Transfer each section which specifies SHF_ALLOC */
  1698. DEBUGP("final section addresses:\n");
  1699. for (i = 0; i < hdr->e_shnum; i++) {
  1700. void *dest;
  1701. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1702. continue;
  1703. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1704. dest = mod->module_init
  1705. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1706. else
  1707. dest = mod->module_core + sechdrs[i].sh_entsize;
  1708. if (sechdrs[i].sh_type != SHT_NOBITS)
  1709. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1710. sechdrs[i].sh_size);
  1711. /* Update sh_addr to point to copy in image. */
  1712. sechdrs[i].sh_addr = (unsigned long)dest;
  1713. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1714. }
  1715. /* Module has been moved. */
  1716. mod = (void *)sechdrs[modindex].sh_addr;
  1717. /* Now we've moved module, initialize linked lists, etc. */
  1718. module_unload_init(mod);
  1719. /* add kobject, so we can reference it. */
  1720. err = mod_sysfs_init(mod);
  1721. if (err)
  1722. goto free_unload;
  1723. /* Set up license info based on the info section */
  1724. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1725. /*
  1726. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  1727. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  1728. * using GPL-only symbols it needs.
  1729. */
  1730. if (strcmp(mod->name, "ndiswrapper") == 0)
  1731. add_taint(TAINT_PROPRIETARY_MODULE);
  1732. /* driverloader was caught wrongly pretending to be under GPL */
  1733. if (strcmp(mod->name, "driverloader") == 0)
  1734. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1735. /* Set up MODINFO_ATTR fields */
  1736. setup_modinfo(mod, sechdrs, infoindex);
  1737. /* Fix up syms, so that st_value is a pointer to location. */
  1738. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1739. mod);
  1740. if (err < 0)
  1741. goto cleanup;
  1742. /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
  1743. mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
  1744. mod->syms = (void *)sechdrs[exportindex].sh_addr;
  1745. if (crcindex)
  1746. mod->crcs = (void *)sechdrs[crcindex].sh_addr;
  1747. mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
  1748. mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
  1749. if (gplcrcindex)
  1750. mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
  1751. mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
  1752. sizeof(*mod->gpl_future_syms);
  1753. mod->num_unused_syms = sechdrs[unusedindex].sh_size /
  1754. sizeof(*mod->unused_syms);
  1755. mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
  1756. sizeof(*mod->unused_gpl_syms);
  1757. mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
  1758. if (gplfuturecrcindex)
  1759. mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
  1760. mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
  1761. if (unusedcrcindex)
  1762. mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
  1763. mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
  1764. if (unusedgplcrcindex)
  1765. mod->unused_gpl_crcs
  1766. = (void *)sechdrs[unusedgplcrcindex].sh_addr;
  1767. #ifdef CONFIG_MODVERSIONS
  1768. if ((mod->num_syms && !crcindex) ||
  1769. (mod->num_gpl_syms && !gplcrcindex) ||
  1770. (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
  1771. (mod->num_unused_syms && !unusedcrcindex) ||
  1772. (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
  1773. printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
  1774. err = try_to_force_load(mod, "nocrc");
  1775. if (err)
  1776. goto cleanup;
  1777. }
  1778. #endif
  1779. markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
  1780. markersstringsindex = find_sec(hdr, sechdrs, secstrings,
  1781. "__markers_strings");
  1782. /* Now do relocations. */
  1783. for (i = 1; i < hdr->e_shnum; i++) {
  1784. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1785. unsigned int info = sechdrs[i].sh_info;
  1786. /* Not a valid relocation section? */
  1787. if (info >= hdr->e_shnum)
  1788. continue;
  1789. /* Don't bother with non-allocated sections */
  1790. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1791. continue;
  1792. if (sechdrs[i].sh_type == SHT_REL)
  1793. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1794. else if (sechdrs[i].sh_type == SHT_RELA)
  1795. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1796. mod);
  1797. if (err < 0)
  1798. goto cleanup;
  1799. }
  1800. #ifdef CONFIG_MARKERS
  1801. mod->markers = (void *)sechdrs[markersindex].sh_addr;
  1802. mod->num_markers =
  1803. sechdrs[markersindex].sh_size / sizeof(*mod->markers);
  1804. #endif
  1805. /* Find duplicate symbols */
  1806. err = verify_export_symbols(mod);
  1807. if (err < 0)
  1808. goto cleanup;
  1809. /* Set up and sort exception table */
  1810. mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
  1811. mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
  1812. sort_extable(extable, extable + mod->num_exentries);
  1813. /* Finally, copy percpu area over. */
  1814. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1815. sechdrs[pcpuindex].sh_size);
  1816. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1817. #ifdef CONFIG_MARKERS
  1818. if (!mod->taints)
  1819. marker_update_probe_range(mod->markers,
  1820. mod->markers + mod->num_markers);
  1821. #endif
  1822. err = module_finalize(hdr, sechdrs, mod);
  1823. if (err < 0)
  1824. goto cleanup;
  1825. /* flush the icache in correct context */
  1826. old_fs = get_fs();
  1827. set_fs(KERNEL_DS);
  1828. /*
  1829. * Flush the instruction cache, since we've played with text.
  1830. * Do it before processing of module parameters, so the module
  1831. * can provide parameter accessor functions of its own.
  1832. */
  1833. if (mod->module_init)
  1834. flush_icache_range((unsigned long)mod->module_init,
  1835. (unsigned long)mod->module_init
  1836. + mod->init_size);
  1837. flush_icache_range((unsigned long)mod->module_core,
  1838. (unsigned long)mod->module_core + mod->core_size);
  1839. set_fs(old_fs);
  1840. mod->args = args;
  1841. if (obsparmindex)
  1842. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1843. mod->name);
  1844. /* Now sew it into the lists so we can get lockdep and oops
  1845. * info during argument parsing. Noone should access us, since
  1846. * strong_try_module_get() will fail. */
  1847. stop_machine_run(__link_module, mod, NR_CPUS);
  1848. /* Size of section 0 is 0, so this works well if no params */
  1849. err = parse_args(mod->name, mod->args,
  1850. (struct kernel_param *)
  1851. sechdrs[setupindex].sh_addr,
  1852. sechdrs[setupindex].sh_size
  1853. / sizeof(struct kernel_param),
  1854. NULL);
  1855. if (err < 0)
  1856. goto unlink;
  1857. err = mod_sysfs_setup(mod,
  1858. (struct kernel_param *)
  1859. sechdrs[setupindex].sh_addr,
  1860. sechdrs[setupindex].sh_size
  1861. / sizeof(struct kernel_param));
  1862. if (err < 0)
  1863. goto unlink;
  1864. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1865. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1866. /* Size of section 0 is 0, so this works well if no unwind info. */
  1867. mod->unwind_info = unwind_add_table(mod,
  1868. (void *)sechdrs[unwindex].sh_addr,
  1869. sechdrs[unwindex].sh_size);
  1870. /* Get rid of temporary copy */
  1871. vfree(hdr);
  1872. /* Done! */
  1873. return mod;
  1874. unlink:
  1875. stop_machine_run(__unlink_module, mod, NR_CPUS);
  1876. module_arch_cleanup(mod);
  1877. cleanup:
  1878. kobject_del(&mod->mkobj.kobj);
  1879. kobject_put(&mod->mkobj.kobj);
  1880. free_unload:
  1881. module_unload_free(mod);
  1882. module_free(mod, mod->module_init);
  1883. free_core:
  1884. module_free(mod, mod->module_core);
  1885. free_percpu:
  1886. if (percpu)
  1887. percpu_modfree(percpu);
  1888. free_mod:
  1889. kfree(args);
  1890. free_hdr:
  1891. vfree(hdr);
  1892. return ERR_PTR(err);
  1893. truncated:
  1894. printk(KERN_ERR "Module len %lu truncated\n", len);
  1895. err = -ENOEXEC;
  1896. goto free_hdr;
  1897. }
  1898. /* This is where the real work happens */
  1899. asmlinkage long
  1900. sys_init_module(void __user *umod,
  1901. unsigned long len,
  1902. const char __user *uargs)
  1903. {
  1904. struct module *mod;
  1905. int ret = 0;
  1906. /* Must have permission */
  1907. if (!capable(CAP_SYS_MODULE))
  1908. return -EPERM;
  1909. /* Only one module load at a time, please */
  1910. if (mutex_lock_interruptible(&module_mutex) != 0)
  1911. return -EINTR;
  1912. /* Do all the hard work */
  1913. mod = load_module(umod, len, uargs);
  1914. if (IS_ERR(mod)) {
  1915. mutex_unlock(&module_mutex);
  1916. return PTR_ERR(mod);
  1917. }
  1918. /* Drop lock so they can recurse */
  1919. mutex_unlock(&module_mutex);
  1920. blocking_notifier_call_chain(&module_notify_list,
  1921. MODULE_STATE_COMING, mod);
  1922. /* Start the module */
  1923. if (mod->init != NULL)
  1924. ret = mod->init();
  1925. if (ret < 0) {
  1926. /* Init routine failed: abort. Try to protect us from
  1927. buggy refcounters. */
  1928. mod->state = MODULE_STATE_GOING;
  1929. synchronize_sched();
  1930. module_put(mod);
  1931. blocking_notifier_call_chain(&module_notify_list,
  1932. MODULE_STATE_GOING, mod);
  1933. mutex_lock(&module_mutex);
  1934. free_module(mod);
  1935. mutex_unlock(&module_mutex);
  1936. wake_up(&module_wq);
  1937. return ret;
  1938. }
  1939. if (ret > 0) {
  1940. printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
  1941. "it should follow 0/-E convention\n"
  1942. KERN_WARNING "%s: loading module anyway...\n",
  1943. __func__, mod->name, ret,
  1944. __func__);
  1945. dump_stack();
  1946. }
  1947. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  1948. mod->state = MODULE_STATE_LIVE;
  1949. wake_up(&module_wq);
  1950. mutex_lock(&module_mutex);
  1951. /* Drop initial reference. */
  1952. module_put(mod);
  1953. unwind_remove_table(mod->unwind_info, 1);
  1954. module_free(mod, mod->module_init);
  1955. mod->module_init = NULL;
  1956. mod->init_size = 0;
  1957. mod->init_text_size = 0;
  1958. mutex_unlock(&module_mutex);
  1959. return 0;
  1960. }
  1961. static inline int within(unsigned long addr, void *start, unsigned long size)
  1962. {
  1963. return ((void *)addr >= start && (void *)addr < start + size);
  1964. }
  1965. #ifdef CONFIG_KALLSYMS
  1966. /*
  1967. * This ignores the intensely annoying "mapping symbols" found
  1968. * in ARM ELF files: $a, $t and $d.
  1969. */
  1970. static inline int is_arm_mapping_symbol(const char *str)
  1971. {
  1972. return str[0] == '$' && strchr("atd", str[1])
  1973. && (str[2] == '\0' || str[2] == '.');
  1974. }
  1975. static const char *get_ksymbol(struct module *mod,
  1976. unsigned long addr,
  1977. unsigned long *size,
  1978. unsigned long *offset)
  1979. {
  1980. unsigned int i, best = 0;
  1981. unsigned long nextval;
  1982. /* At worse, next value is at end of module */
  1983. if (within(addr, mod->module_init, mod->init_size))
  1984. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  1985. else
  1986. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  1987. /* Scan for closest preceeding symbol, and next symbol. (ELF
  1988. starts real symbols at 1). */
  1989. for (i = 1; i < mod->num_symtab; i++) {
  1990. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  1991. continue;
  1992. /* We ignore unnamed symbols: they're uninformative
  1993. * and inserted at a whim. */
  1994. if (mod->symtab[i].st_value <= addr
  1995. && mod->symtab[i].st_value > mod->symtab[best].st_value
  1996. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1997. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1998. best = i;
  1999. if (mod->symtab[i].st_value > addr
  2000. && mod->symtab[i].st_value < nextval
  2001. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2002. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2003. nextval = mod->symtab[i].st_value;
  2004. }
  2005. if (!best)
  2006. return NULL;
  2007. if (size)
  2008. *size = nextval - mod->symtab[best].st_value;
  2009. if (offset)
  2010. *offset = addr - mod->symtab[best].st_value;
  2011. return mod->strtab + mod->symtab[best].st_name;
  2012. }
  2013. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  2014. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  2015. const char *module_address_lookup(unsigned long addr,
  2016. unsigned long *size,
  2017. unsigned long *offset,
  2018. char **modname,
  2019. char *namebuf)
  2020. {
  2021. struct module *mod;
  2022. const char *ret = NULL;
  2023. preempt_disable();
  2024. list_for_each_entry(mod, &modules, list) {
  2025. if (within(addr, mod->module_init, mod->init_size)
  2026. || within(addr, mod->module_core, mod->core_size)) {
  2027. if (modname)
  2028. *modname = mod->name;
  2029. ret = get_ksymbol(mod, addr, size, offset);
  2030. break;
  2031. }
  2032. }
  2033. /* Make a copy in here where it's safe */
  2034. if (ret) {
  2035. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2036. ret = namebuf;
  2037. }
  2038. preempt_enable();
  2039. return ret;
  2040. }
  2041. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2042. {
  2043. struct module *mod;
  2044. preempt_disable();
  2045. list_for_each_entry(mod, &modules, list) {
  2046. if (within(addr, mod->module_init, mod->init_size) ||
  2047. within(addr, mod->module_core, mod->core_size)) {
  2048. const char *sym;
  2049. sym = get_ksymbol(mod, addr, NULL, NULL);
  2050. if (!sym)
  2051. goto out;
  2052. strlcpy(symname, sym, KSYM_NAME_LEN);
  2053. preempt_enable();
  2054. return 0;
  2055. }
  2056. }
  2057. out:
  2058. preempt_enable();
  2059. return -ERANGE;
  2060. }
  2061. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2062. unsigned long *offset, char *modname, char *name)
  2063. {
  2064. struct module *mod;
  2065. preempt_disable();
  2066. list_for_each_entry(mod, &modules, list) {
  2067. if (within(addr, mod->module_init, mod->init_size) ||
  2068. within(addr, mod->module_core, mod->core_size)) {
  2069. const char *sym;
  2070. sym = get_ksymbol(mod, addr, size, offset);
  2071. if (!sym)
  2072. goto out;
  2073. if (modname)
  2074. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2075. if (name)
  2076. strlcpy(name, sym, KSYM_NAME_LEN);
  2077. preempt_enable();
  2078. return 0;
  2079. }
  2080. }
  2081. out:
  2082. preempt_enable();
  2083. return -ERANGE;
  2084. }
  2085. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2086. char *name, char *module_name, int *exported)
  2087. {
  2088. struct module *mod;
  2089. preempt_disable();
  2090. list_for_each_entry(mod, &modules, list) {
  2091. if (symnum < mod->num_symtab) {
  2092. *value = mod->symtab[symnum].st_value;
  2093. *type = mod->symtab[symnum].st_info;
  2094. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2095. KSYM_NAME_LEN);
  2096. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2097. *exported = is_exported(name, mod);
  2098. preempt_enable();
  2099. return 0;
  2100. }
  2101. symnum -= mod->num_symtab;
  2102. }
  2103. preempt_enable();
  2104. return -ERANGE;
  2105. }
  2106. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2107. {
  2108. unsigned int i;
  2109. for (i = 0; i < mod->num_symtab; i++)
  2110. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2111. mod->symtab[i].st_info != 'U')
  2112. return mod->symtab[i].st_value;
  2113. return 0;
  2114. }
  2115. /* Look for this name: can be of form module:name. */
  2116. unsigned long module_kallsyms_lookup_name(const char *name)
  2117. {
  2118. struct module *mod;
  2119. char *colon;
  2120. unsigned long ret = 0;
  2121. /* Don't lock: we're in enough trouble already. */
  2122. preempt_disable();
  2123. if ((colon = strchr(name, ':')) != NULL) {
  2124. *colon = '\0';
  2125. if ((mod = find_module(name)) != NULL)
  2126. ret = mod_find_symname(mod, colon+1);
  2127. *colon = ':';
  2128. } else {
  2129. list_for_each_entry(mod, &modules, list)
  2130. if ((ret = mod_find_symname(mod, name)) != 0)
  2131. break;
  2132. }
  2133. preempt_enable();
  2134. return ret;
  2135. }
  2136. #endif /* CONFIG_KALLSYMS */
  2137. /* Called by the /proc file system to return a list of modules. */
  2138. static void *m_start(struct seq_file *m, loff_t *pos)
  2139. {
  2140. mutex_lock(&module_mutex);
  2141. return seq_list_start(&modules, *pos);
  2142. }
  2143. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2144. {
  2145. return seq_list_next(p, &modules, pos);
  2146. }
  2147. static void m_stop(struct seq_file *m, void *p)
  2148. {
  2149. mutex_unlock(&module_mutex);
  2150. }
  2151. static char *module_flags(struct module *mod, char *buf)
  2152. {
  2153. int bx = 0;
  2154. if (mod->taints ||
  2155. mod->state == MODULE_STATE_GOING ||
  2156. mod->state == MODULE_STATE_COMING) {
  2157. buf[bx++] = '(';
  2158. if (mod->taints & TAINT_PROPRIETARY_MODULE)
  2159. buf[bx++] = 'P';
  2160. if (mod->taints & TAINT_FORCED_MODULE)
  2161. buf[bx++] = 'F';
  2162. /*
  2163. * TAINT_FORCED_RMMOD: could be added.
  2164. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2165. * apply to modules.
  2166. */
  2167. /* Show a - for module-is-being-unloaded */
  2168. if (mod->state == MODULE_STATE_GOING)
  2169. buf[bx++] = '-';
  2170. /* Show a + for module-is-being-loaded */
  2171. if (mod->state == MODULE_STATE_COMING)
  2172. buf[bx++] = '+';
  2173. buf[bx++] = ')';
  2174. }
  2175. buf[bx] = '\0';
  2176. return buf;
  2177. }
  2178. static int m_show(struct seq_file *m, void *p)
  2179. {
  2180. struct module *mod = list_entry(p, struct module, list);
  2181. char buf[8];
  2182. seq_printf(m, "%s %lu",
  2183. mod->name, mod->init_size + mod->core_size);
  2184. print_unload_info(m, mod);
  2185. /* Informative for users. */
  2186. seq_printf(m, " %s",
  2187. mod->state == MODULE_STATE_GOING ? "Unloading":
  2188. mod->state == MODULE_STATE_COMING ? "Loading":
  2189. "Live");
  2190. /* Used by oprofile and other similar tools. */
  2191. seq_printf(m, " 0x%p", mod->module_core);
  2192. /* Taints info */
  2193. if (mod->taints)
  2194. seq_printf(m, " %s", module_flags(mod, buf));
  2195. seq_printf(m, "\n");
  2196. return 0;
  2197. }
  2198. /* Format: modulename size refcount deps address
  2199. Where refcount is a number or -, and deps is a comma-separated list
  2200. of depends or -.
  2201. */
  2202. const struct seq_operations modules_op = {
  2203. .start = m_start,
  2204. .next = m_next,
  2205. .stop = m_stop,
  2206. .show = m_show
  2207. };
  2208. /* Given an address, look for it in the module exception tables. */
  2209. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2210. {
  2211. const struct exception_table_entry *e = NULL;
  2212. struct module *mod;
  2213. preempt_disable();
  2214. list_for_each_entry(mod, &modules, list) {
  2215. if (mod->num_exentries == 0)
  2216. continue;
  2217. e = search_extable(mod->extable,
  2218. mod->extable + mod->num_exentries - 1,
  2219. addr);
  2220. if (e)
  2221. break;
  2222. }
  2223. preempt_enable();
  2224. /* Now, if we found one, we are running inside it now, hence
  2225. we cannot unload the module, hence no refcnt needed. */
  2226. return e;
  2227. }
  2228. /*
  2229. * Is this a valid module address?
  2230. */
  2231. int is_module_address(unsigned long addr)
  2232. {
  2233. struct module *mod;
  2234. preempt_disable();
  2235. list_for_each_entry(mod, &modules, list) {
  2236. if (within(addr, mod->module_core, mod->core_size)) {
  2237. preempt_enable();
  2238. return 1;
  2239. }
  2240. }
  2241. preempt_enable();
  2242. return 0;
  2243. }
  2244. /* Is this a valid kernel address? */
  2245. struct module *__module_text_address(unsigned long addr)
  2246. {
  2247. struct module *mod;
  2248. list_for_each_entry(mod, &modules, list)
  2249. if (within(addr, mod->module_init, mod->init_text_size)
  2250. || within(addr, mod->module_core, mod->core_text_size))
  2251. return mod;
  2252. return NULL;
  2253. }
  2254. struct module *module_text_address(unsigned long addr)
  2255. {
  2256. struct module *mod;
  2257. preempt_disable();
  2258. mod = __module_text_address(addr);
  2259. preempt_enable();
  2260. return mod;
  2261. }
  2262. /* Don't grab lock, we're oopsing. */
  2263. void print_modules(void)
  2264. {
  2265. struct module *mod;
  2266. char buf[8];
  2267. printk("Modules linked in:");
  2268. list_for_each_entry(mod, &modules, list)
  2269. printk(" %s%s", mod->name, module_flags(mod, buf));
  2270. if (last_unloaded_module[0])
  2271. printk(" [last unloaded: %s]", last_unloaded_module);
  2272. printk("\n");
  2273. }
  2274. #ifdef CONFIG_MODVERSIONS
  2275. /* Generate the signature for struct module here, too, for modversions. */
  2276. void struct_module(struct module *mod) { return; }
  2277. EXPORT_SYMBOL(struct_module);
  2278. #endif
  2279. #ifdef CONFIG_MARKERS
  2280. void module_update_markers(void)
  2281. {
  2282. struct module *mod;
  2283. mutex_lock(&module_mutex);
  2284. list_for_each_entry(mod, &modules, list)
  2285. if (!mod->taints)
  2286. marker_update_probe_range(mod->markers,
  2287. mod->markers + mod->num_markers);
  2288. mutex_unlock(&module_mutex);
  2289. }
  2290. #endif