module.c 73 KB

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