module.c 82 KB

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