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