module.c 82 KB

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