module.c 70 KB

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