module.c 72 KB

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