module.c 73 KB

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