module.c 81 KB

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