module.c 75 KB

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