module.c 71 KB

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