module.c 72 KB

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