module.c 80 KB

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