blob: d753fd9d83ece9803880e6ad4d60184a51d90645 [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleloader.h>
21#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070022#include <linux/kallsyms.h>
Roland McGrath6d760132007-10-16 23:26:40 -070023#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070024#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <linux/elf.h>
28#include <linux/seq_file.h>
29#include <linux/syscalls.h>
30#include <linux/fcntl.h>
31#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/cpu.h>
34#include <linux/moduleparam.h>
35#include <linux/errno.h>
36#include <linux/err.h>
37#include <linux/vermagic.h>
38#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040039#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/stop_machine.h>
41#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070042#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080043#include <linux/mutex.h>
Jan Beulich4552d5d2006-06-26 13:57:28 +020044#include <linux/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020047#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080048#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040049#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040050#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#if 0
53#define DEBUGP printk
54#else
55#define DEBUGP(fmt , a...)
56#endif
57
58#ifndef ARCH_SHF_SMALL
59#define ARCH_SHF_SMALL 0
60#endif
61
62/* If this is set, the section belongs in the init part of the module */
63#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
64
Rusty Russell24da1cb2007-07-15 23:41:46 -070065/* List of modules, protected by module_mutex or preempt_disable
66 * (add/delete uses stop_machine). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080067static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static LIST_HEAD(modules);
69
Rusty Russellc9a3ba52008-01-29 17:13:18 -050070/* Waiting for a module to finish initializing? */
71static DECLARE_WAIT_QUEUE_HEAD(module_wq);
72
Alan Sterne041c682006-03-27 01:16:30 -080073static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Rusty Russell3a642e92008-07-22 19:24:28 -050075/* Bounds of module allocation, for speeding __module_text_address */
76static unsigned long module_addr_min = -1UL, module_addr_max = 0;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078int register_module_notifier(struct notifier_block * nb)
79{
Alan Sterne041c682006-03-27 01:16:30 -080080 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82EXPORT_SYMBOL(register_module_notifier);
83
84int unregister_module_notifier(struct notifier_block * nb)
85{
Alan Sterne041c682006-03-27 01:16:30 -080086 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88EXPORT_SYMBOL(unregister_module_notifier);
89
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080090/* We require a truly strong try_module_get(): 0 means failure due to
91 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static inline int strong_try_module_get(struct module *mod)
93{
94 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -050095 return -EBUSY;
96 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -050098 else
99 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700102static inline void add_taint_module(struct module *mod, unsigned flag)
103{
104 add_taint(flag);
105 mod->taints |= flag;
106}
107
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200108/*
109 * A thread that wants to hold a reference to a module only while it
110 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
112void __module_put_and_exit(struct module *mod, long code)
113{
114 module_put(mod);
115 do_exit(code);
116}
117EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Find a module section: 0 means not found. */
120static unsigned int find_sec(Elf_Ehdr *hdr,
121 Elf_Shdr *sechdrs,
122 const char *secstrings,
123 const char *name)
124{
125 unsigned int i;
126
127 for (i = 1; i < hdr->e_shnum; i++)
128 /* Alloc bit cleared means "ignore it." */
129 if ((sechdrs[i].sh_flags & SHF_ALLOC)
130 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
131 return i;
132 return 0;
133}
134
135/* Provided by the linker */
136extern const struct kernel_symbol __start___ksymtab[];
137extern const struct kernel_symbol __stop___ksymtab[];
138extern const struct kernel_symbol __start___ksymtab_gpl[];
139extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800140extern const struct kernel_symbol __start___ksymtab_gpl_future[];
141extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700142extern const struct kernel_symbol __start___ksymtab_gpl_future[];
143extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144extern const unsigned long __start___kcrctab[];
145extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800146extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500147#ifdef CONFIG_UNUSED_SYMBOLS
148extern const struct kernel_symbol __start___ksymtab_unused[];
149extern const struct kernel_symbol __stop___ksymtab_unused[];
150extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
151extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700152extern const unsigned long __start___kcrctab_unused[];
153extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156#ifndef CONFIG_MODVERSIONS
157#define symversion(base, idx) NULL
158#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800159#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#endif
161
Rusty Russelldafd0942008-07-22 19:24:25 -0500162struct symsearch {
163 const struct kernel_symbol *start, *stop;
164 const unsigned long *crcs;
165 enum {
166 NOT_GPL_ONLY,
167 GPL_ONLY,
168 WILL_BE_GPL_ONLY,
169 } licence;
170 bool unused;
171};
172
173static bool each_symbol_in_section(const struct symsearch *arr,
174 unsigned int arrsize,
175 struct module *owner,
176 bool (*fn)(const struct symsearch *syms,
177 struct module *owner,
178 unsigned int symnum, void *data),
179 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100180{
Rusty Russelldafd0942008-07-22 19:24:25 -0500181 unsigned int i, j;
182
183 for (j = 0; j < arrsize; j++) {
184 for (i = 0; i < arr[j].stop - arr[j].start; i++)
185 if (fn(&arr[j], owner, i, data))
186 return true;
187 }
188
189 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100190}
191
Rusty Russelldafd0942008-07-22 19:24:25 -0500192/* Returns true as soon as fn returns true, otherwise false. */
193static bool each_symbol(bool (*fn)(const struct symsearch *arr,
194 struct module *owner,
195 unsigned int symnum, void *data),
196 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700197{
Rusty Russelldafd0942008-07-22 19:24:25 -0500198 struct module *mod;
199 const struct symsearch arr[] = {
200 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
201 NOT_GPL_ONLY, false },
202 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
203 __start___kcrctab_gpl,
204 GPL_ONLY, false },
205 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
206 __start___kcrctab_gpl_future,
207 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500208#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500209 { __start___ksymtab_unused, __stop___ksymtab_unused,
210 __start___kcrctab_unused,
211 NOT_GPL_ONLY, true },
212 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
213 __start___kcrctab_unused_gpl,
214 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500215#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500216 };
217
218 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
219 return true;
220
221 list_for_each_entry(mod, &modules, list) {
222 struct symsearch arr[] = {
223 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
224 NOT_GPL_ONLY, false },
225 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
226 mod->gpl_crcs,
227 GPL_ONLY, false },
228 { mod->gpl_future_syms,
229 mod->gpl_future_syms + mod->num_gpl_future_syms,
230 mod->gpl_future_crcs,
231 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500232#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500233 { mod->unused_syms,
234 mod->unused_syms + mod->num_unused_syms,
235 mod->unused_crcs,
236 NOT_GPL_ONLY, true },
237 { mod->unused_gpl_syms,
238 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
239 mod->unused_gpl_crcs,
240 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500241#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500242 };
243
244 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
245 return true;
246 }
247 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700248}
249
Rusty Russelldafd0942008-07-22 19:24:25 -0500250struct find_symbol_arg {
251 /* Input */
252 const char *name;
253 bool gplok;
254 bool warn;
255
256 /* Output */
257 struct module *owner;
258 const unsigned long *crc;
259 unsigned long value;
260};
261
262static bool find_symbol_in_section(const struct symsearch *syms,
263 struct module *owner,
264 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500265{
Rusty Russelldafd0942008-07-22 19:24:25 -0500266 struct find_symbol_arg *fsa = data;
267
268 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
269 return false;
270
271 if (!fsa->gplok) {
272 if (syms->licence == GPL_ONLY)
273 return false;
274 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
275 printk(KERN_WARNING "Symbol %s is being used "
276 "by a non-GPL module, which will not "
277 "be allowed in the future\n", fsa->name);
278 printk(KERN_WARNING "Please see the file "
279 "Documentation/feature-removal-schedule.txt "
280 "in the kernel source tree for more details.\n");
281 }
282 }
283
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500284#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500285 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500286 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500287 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500288 printk(KERN_WARNING
289 "This symbol will go away in the future.\n");
290 printk(KERN_WARNING
291 "Please evalute if this is the right api to use and if "
292 "it really is, submit a report the linux kernel "
293 "mailinglist together with submitting your code for "
294 "inclusion.\n");
295 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500296#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500297
298 fsa->owner = owner;
299 fsa->crc = symversion(syms->crcs, symnum);
300 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500301 return true;
302}
303
Rusty Russellad9546c2008-05-01 21:14:59 -0500304/* Find a symbol, return value, (optional) crc and (optional) module
305 * which owns it */
306static unsigned long find_symbol(const char *name,
307 struct module **owner,
308 const unsigned long **crc,
309 bool gplok,
310 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Rusty Russelldafd0942008-07-22 19:24:25 -0500312 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Rusty Russelldafd0942008-07-22 19:24:25 -0500314 fsa.name = name;
315 fsa.gplok = gplok;
316 fsa.warn = warn;
317
318 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500319 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500320 *owner = fsa.owner;
321 if (crc)
322 *crc = fsa.crc;
323 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800327 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/* Search for module by name: must hold module_mutex. */
331static struct module *find_module(const char *name)
332{
333 struct module *mod;
334
335 list_for_each_entry(mod, &modules, list) {
336 if (strcmp(mod->name, name) == 0)
337 return mod;
338 }
339 return NULL;
340}
341
342#ifdef CONFIG_SMP
343/* Number of blocks used and allocated. */
344static unsigned int pcpu_num_used, pcpu_num_allocated;
345/* Size of each block. -ve means used. */
346static int *pcpu_size;
347
348static int split_block(unsigned int i, unsigned short size)
349{
350 /* Reallocation required? */
351 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700352 int *new;
353
354 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
355 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!new)
357 return 0;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 pcpu_size = new;
361 }
362
363 /* Insert a new subblock */
364 memmove(&pcpu_size[i+1], &pcpu_size[i],
365 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
366 pcpu_num_used++;
367
368 pcpu_size[i+1] -= size;
369 pcpu_size[i] = size;
370 return 1;
371}
372
373static inline unsigned int block_size(int val)
374{
375 if (val < 0)
376 return -val;
377 return val;
378}
379
Rusty Russell842bbaa2005-08-01 21:11:47 -0700380static void *percpu_modalloc(unsigned long size, unsigned long align,
381 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 unsigned long extra;
384 unsigned int i;
385 void *ptr;
386
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200387 if (align > PAGE_SIZE) {
388 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
389 name, align, PAGE_SIZE);
390 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 ptr = __per_cpu_start;
394 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
395 /* Extra for alignment requirement. */
396 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
397 BUG_ON(i == 0 && extra != 0);
398
399 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
400 continue;
401
402 /* Transfer extra to previous block. */
403 if (pcpu_size[i-1] < 0)
404 pcpu_size[i-1] -= extra;
405 else
406 pcpu_size[i-1] += extra;
407 pcpu_size[i] -= extra;
408 ptr += extra;
409
410 /* Split block if warranted */
411 if (pcpu_size[i] - size > sizeof(unsigned long))
412 if (!split_block(i, size))
413 return NULL;
414
415 /* Mark allocated */
416 pcpu_size[i] = -pcpu_size[i];
417 return ptr;
418 }
419
420 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
421 size);
422 return NULL;
423}
424
425static void percpu_modfree(void *freeme)
426{
427 unsigned int i;
428 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
429
430 /* First entry is core kernel percpu data. */
431 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
432 if (ptr == freeme) {
433 pcpu_size[i] = -pcpu_size[i];
434 goto free;
435 }
436 }
437 BUG();
438
439 free:
440 /* Merge with previous? */
441 if (pcpu_size[i-1] >= 0) {
442 pcpu_size[i-1] += pcpu_size[i];
443 pcpu_num_used--;
444 memmove(&pcpu_size[i], &pcpu_size[i+1],
445 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
446 i--;
447 }
448 /* Merge with next? */
449 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
450 pcpu_size[i] += pcpu_size[i+1];
451 pcpu_num_used--;
452 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
453 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
454 }
455}
456
457static unsigned int find_pcpusec(Elf_Ehdr *hdr,
458 Elf_Shdr *sechdrs,
459 const char *secstrings)
460{
461 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
462}
463
Mike Travisdd5af902008-01-30 13:33:32 +0100464static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
465{
466 int cpu;
467
468 for_each_possible_cpu(cpu)
469 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static int percpu_modinit(void)
473{
474 pcpu_num_used = 2;
475 pcpu_num_allocated = 2;
476 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
477 GFP_KERNEL);
478 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200479 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* Free room. */
481 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
482 if (pcpu_size[1] < 0) {
483 printk(KERN_ERR "No per-cpu room for modules.\n");
484 pcpu_num_used = 1;
485 }
486
487 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700488}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489__initcall(percpu_modinit);
490#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700491static inline void *percpu_modalloc(unsigned long size, unsigned long align,
492 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 return NULL;
495}
496static inline void percpu_modfree(void *pcpuptr)
497{
498 BUG();
499}
500static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
501 Elf_Shdr *sechdrs,
502 const char *secstrings)
503{
504 return 0;
505}
506static inline void percpu_modcopy(void *pcpudst, const void *src,
507 unsigned long size)
508{
509 /* pcpusec should be 0, and size of that section should be 0. */
510 BUG_ON(size != 0);
511}
512#endif /* CONFIG_SMP */
513
Matt Domschc988d2b2005-06-23 22:05:15 -0700514#define MODINFO_ATTR(field) \
515static void setup_modinfo_##field(struct module *mod, const char *s) \
516{ \
517 mod->field = kstrdup(s, GFP_KERNEL); \
518} \
519static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
520 struct module *mod, char *buffer) \
521{ \
522 return sprintf(buffer, "%s\n", mod->field); \
523} \
524static int modinfo_##field##_exists(struct module *mod) \
525{ \
526 return mod->field != NULL; \
527} \
528static void free_modinfo_##field(struct module *mod) \
529{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700530 kfree(mod->field); \
531 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700532} \
533static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900534 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700535 .show = show_modinfo_##field, \
536 .setup = setup_modinfo_##field, \
537 .test = modinfo_##field##_exists, \
538 .free = free_modinfo_##field, \
539};
540
541MODINFO_ATTR(version);
542MODINFO_ATTR(srcversion);
543
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100544static char last_unloaded_module[MODULE_NAME_LEN+1];
545
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800546#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/* Init the unload section of the module. */
548static void module_unload_init(struct module *mod)
549{
550 unsigned int i;
551
552 INIT_LIST_HEAD(&mod->modules_which_use_me);
553 for (i = 0; i < NR_CPUS; i++)
554 local_set(&mod->ref[i].count, 0);
555 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700556 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* Backwards compatibility macros put refcount during init. */
558 mod->waiter = current;
559}
560
561/* modules using other modules */
562struct module_use
563{
564 struct list_head list;
565 struct module *module_which_uses;
566};
567
568/* Does a already use b? */
569static int already_uses(struct module *a, struct module *b)
570{
571 struct module_use *use;
572
573 list_for_each_entry(use, &b->modules_which_use_me, list) {
574 if (use->module_which_uses == a) {
575 DEBUGP("%s uses %s!\n", a->name, b->name);
576 return 1;
577 }
578 }
579 DEBUGP("%s does not use %s!\n", a->name, b->name);
580 return 0;
581}
582
583/* Module a uses b */
584static int use_module(struct module *a, struct module *b)
585{
586 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500587 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (b == NULL || already_uses(a, b)) return 1;
590
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500591 /* If we're interrupted or time out, we fail. */
592 if (wait_event_interruptible_timeout(
593 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
594 30 * HZ) <= 0) {
595 printk("%s: gave up waiting for init of module %s.\n",
596 a->name, b->name);
597 return 0;
598 }
599
600 /* If strong_try_module_get() returned a different error, we fail. */
601 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603
604 DEBUGP("Allocating new usage for %s.\n", a->name);
605 use = kmalloc(sizeof(*use), GFP_ATOMIC);
606 if (!use) {
607 printk("%s: out of memory loading\n", a->name);
608 module_put(b);
609 return 0;
610 }
611
612 use->module_which_uses = a;
613 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100614 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return 1;
616}
617
618/* Clear the unload stuff of the module. */
619static void module_unload_free(struct module *mod)
620{
621 struct module *i;
622
623 list_for_each_entry(i, &modules, list) {
624 struct module_use *use;
625
626 list_for_each_entry(use, &i->modules_which_use_me, list) {
627 if (use->module_which_uses == mod) {
628 DEBUGP("%s unusing %s\n", mod->name, i->name);
629 module_put(i);
630 list_del(&use->list);
631 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100632 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* There can be at most one match. */
634 break;
635 }
636 }
637 }
638}
639
640#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800641static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 int ret = (flags & O_TRUNC);
644 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800645 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return ret;
647}
648#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800649static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 return 0;
652}
653#endif /* CONFIG_MODULE_FORCE_UNLOAD */
654
655struct stopref
656{
657 struct module *mod;
658 int flags;
659 int *forced;
660};
661
662/* Whole machine is stopped with interrupts off when this runs. */
663static int __try_stop_module(void *_sref)
664{
665 struct stopref *sref = _sref;
666
Rusty Russellda39ba52008-07-22 19:24:25 -0500667 /* If it's not unused, quit unless we're forcing. */
668 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800669 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EWOULDBLOCK;
671 }
672
673 /* Mark it as dying. */
674 sref->mod->state = MODULE_STATE_GOING;
675 return 0;
676}
677
678static int try_stop_module(struct module *mod, int flags, int *forced)
679{
Rusty Russellda39ba52008-07-22 19:24:25 -0500680 if (flags & O_NONBLOCK) {
681 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500683 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500684 } else {
685 /* We don't need to stop the machine for this. */
686 mod->state = MODULE_STATE_GOING;
687 synchronize_sched();
688 return 0;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692unsigned int module_refcount(struct module *mod)
693{
694 unsigned int i, total = 0;
695
696 for (i = 0; i < NR_CPUS; i++)
697 total += local_read(&mod->ref[i].count);
698 return total;
699}
700EXPORT_SYMBOL(module_refcount);
701
702/* This exists whether we can unload or not */
703static void free_module(struct module *mod);
704
705static void wait_for_zero_refcount(struct module *mod)
706{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500707 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800708 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 for (;;) {
710 DEBUGP("Looking at refcount...\n");
711 set_current_state(TASK_UNINTERRUPTIBLE);
712 if (module_refcount(mod) == 0)
713 break;
714 schedule();
715 }
716 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800717 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800720asmlinkage long
721sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800724 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int ret, forced = 0;
726
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800727 if (!capable(CAP_SYS_MODULE))
728 return -EPERM;
729
730 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
731 return -EFAULT;
732 name[MODULE_NAME_LEN-1] = '\0';
733
Ashutosh Naik6389a382006-03-23 03:00:46 -0800734 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EINTR;
736
737 mod = find_module(name);
738 if (!mod) {
739 ret = -ENOENT;
740 goto out;
741 }
742
743 if (!list_empty(&mod->modules_which_use_me)) {
744 /* Other modules depend on us: get rid of them first. */
745 ret = -EWOULDBLOCK;
746 goto out;
747 }
748
749 /* Doing init or already dying? */
750 if (mod->state != MODULE_STATE_LIVE) {
751 /* FIXME: if (force), slam module count and wake up
752 waiter --RR */
753 DEBUGP("%s already dying\n", mod->name);
754 ret = -EBUSY;
755 goto out;
756 }
757
758 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700759 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800760 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!forced) {
762 /* This module can't be removed */
763 ret = -EBUSY;
764 goto out;
765 }
766 }
767
768 /* Set this up before setting mod->state */
769 mod->waiter = current;
770
771 /* Stop the machine so refcounts can't move and disable module. */
772 ret = try_stop_module(mod, flags, &forced);
773 if (ret != 0)
774 goto out;
775
776 /* Never wait if forced. */
777 if (!forced && module_refcount(mod) != 0)
778 wait_for_zero_refcount(mod);
779
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200780 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200782 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200784 blocking_notifier_call_chain(&module_notify_list,
785 MODULE_STATE_GOING, mod);
786 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100787 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500788 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 free_module(mod);
790
791 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800792 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return ret;
794}
795
796static void print_unload_info(struct seq_file *m, struct module *mod)
797{
798 struct module_use *use;
799 int printed_something = 0;
800
801 seq_printf(m, " %u ", module_refcount(mod));
802
803 /* Always include a trailing , so userspace can differentiate
804 between this and the old multi-field proc format. */
805 list_for_each_entry(use, &mod->modules_which_use_me, list) {
806 printed_something = 1;
807 seq_printf(m, "%s,", use->module_which_uses->name);
808 }
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (mod->init != NULL && mod->exit == NULL) {
811 printed_something = 1;
812 seq_printf(m, "[permanent],");
813 }
814
815 if (!printed_something)
816 seq_printf(m, "-");
817}
818
819void __symbol_put(const char *symbol)
820{
821 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Rusty Russell24da1cb2007-07-15 23:41:46 -0700823 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500824 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 BUG();
826 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700827 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829EXPORT_SYMBOL(__symbol_put);
830
831void symbol_put_addr(void *addr)
832{
Trent Piepho5e376612006-05-15 09:44:06 -0700833 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Trent Piepho5e376612006-05-15 09:44:06 -0700835 if (core_kernel_text((unsigned long)addr))
836 return;
837
838 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700840 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842EXPORT_SYMBOL_GPL(symbol_put_addr);
843
844static ssize_t show_refcnt(struct module_attribute *mattr,
845 struct module *mod, char *buffer)
846{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400847 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900851 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 .show = show_refcnt,
853};
854
Al Virof6a57032006-10-18 01:47:25 -0400855void module_put(struct module *module)
856{
857 if (module) {
858 unsigned int cpu = get_cpu();
859 local_dec(&module->ref[cpu].count);
860 /* Maybe they're waiting for us to drop reference? */
861 if (unlikely(!module_is_live(module)))
862 wake_up_process(module->waiter);
863 put_cpu();
864 }
865}
866EXPORT_SYMBOL(module_put);
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868#else /* !CONFIG_MODULE_UNLOAD */
869static void print_unload_info(struct seq_file *m, struct module *mod)
870{
871 /* We don't know the usage count, or what modules are using. */
872 seq_printf(m, " - -");
873}
874
875static inline void module_unload_free(struct module *mod)
876{
877}
878
879static inline int use_module(struct module *a, struct module *b)
880{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500881 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884static inline void module_unload_init(struct module *mod)
885{
886}
887#endif /* CONFIG_MODULE_UNLOAD */
888
Kay Sievers1f717402006-11-24 12:15:25 +0100889static ssize_t show_initstate(struct module_attribute *mattr,
890 struct module *mod, char *buffer)
891{
892 const char *state = "unknown";
893
894 switch (mod->state) {
895 case MODULE_STATE_LIVE:
896 state = "live";
897 break;
898 case MODULE_STATE_COMING:
899 state = "coming";
900 break;
901 case MODULE_STATE_GOING:
902 state = "going";
903 break;
904 }
905 return sprintf(buffer, "%s\n", state);
906}
907
908static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900909 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100910 .show = show_initstate,
911};
912
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800913static struct module_attribute *modinfo_attrs[] = {
914 &modinfo_version,
915 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100916 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800917#ifdef CONFIG_MODULE_UNLOAD
918 &refcnt,
919#endif
920 NULL,
921};
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static const char vermagic[] = VERMAGIC_STRING;
924
Linus Torvalds826e4502008-05-04 17:04:16 -0700925static int try_to_force_load(struct module *mod, const char *symname)
926{
927#ifdef CONFIG_MODULE_FORCE_LOAD
928 if (!(tainted & TAINT_FORCED_MODULE))
929 printk("%s: no version for \"%s\" found: kernel tainted.\n",
930 mod->name, symname);
931 add_taint_module(mod, TAINT_FORCED_MODULE);
932 return 0;
933#else
934 return -ENOEXEC;
935#endif
936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938#ifdef CONFIG_MODVERSIONS
939static int check_version(Elf_Shdr *sechdrs,
940 unsigned int versindex,
941 const char *symname,
942 struct module *mod,
943 const unsigned long *crc)
944{
945 unsigned int i, num_versions;
946 struct modversion_info *versions;
947
948 /* Exporting module didn't supply crcs? OK, we're already tainted. */
949 if (!crc)
950 return 1;
951
Rusty Russella5dd6972008-05-09 16:24:21 +1000952 /* No versions at all? modprobe --force does this. */
953 if (versindex == 0)
954 return try_to_force_load(mod, symname) == 0;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 versions = (void *) sechdrs[versindex].sh_addr;
957 num_versions = sechdrs[versindex].sh_size
958 / sizeof(struct modversion_info);
959
960 for (i = 0; i < num_versions; i++) {
961 if (strcmp(versions[i].name, symname) != 0)
962 continue;
963
964 if (versions[i].crc == *crc)
965 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 DEBUGP("Found checksum %lX vs module %lX\n",
967 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700968 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Linus Torvalds826e4502008-05-04 17:04:16 -0700970
Rusty Russella5dd6972008-05-09 16:24:21 +1000971 printk(KERN_WARNING "%s: no symbol version for %s\n",
972 mod->name, symname);
973 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -0700974
975bad_version:
976 printk("%s: disagrees about version of symbol %s\n",
977 mod->name, symname);
978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981static inline int check_modstruct_version(Elf_Shdr *sechdrs,
982 unsigned int versindex,
983 struct module *mod)
984{
985 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Rusty Russellad9546c2008-05-01 21:14:59 -0500987 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -0500989 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Rusty Russell91e37a72008-05-09 16:25:28 +1000992/* First part is kernel version, which we ignore if module has crcs. */
993static inline int same_magic(const char *amagic, const char *bmagic,
994 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Rusty Russell91e37a72008-05-09 16:25:28 +1000996 if (has_crcs) {
997 amagic += strcspn(amagic, " ");
998 bmagic += strcspn(bmagic, " ");
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return strcmp(amagic, bmagic) == 0;
1001}
1002#else
1003static inline int check_version(Elf_Shdr *sechdrs,
1004 unsigned int versindex,
1005 const char *symname,
1006 struct module *mod,
1007 const unsigned long *crc)
1008{
1009 return 1;
1010}
1011
1012static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1013 unsigned int versindex,
1014 struct module *mod)
1015{
1016 return 1;
1017}
1018
Rusty Russell91e37a72008-05-09 16:25:28 +10001019static inline int same_magic(const char *amagic, const char *bmagic,
1020 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 return strcmp(amagic, bmagic) == 0;
1023}
1024#endif /* CONFIG_MODVERSIONS */
1025
1026/* Resolve a symbol for this module. I.e. if we find one, record usage.
1027 Must be holding module_mutex. */
1028static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1029 unsigned int versindex,
1030 const char *name,
1031 struct module *mod)
1032{
1033 struct module *owner;
1034 unsigned long ret;
1035 const unsigned long *crc;
1036
Rusty Russellad9546c2008-05-01 21:14:59 -05001037 ret = find_symbol(name, &owner, &crc,
1038 !(mod->taints & TAINT_PROPRIETARY_MODULE), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001039 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001040 /* use_module can fail due to OOM,
1041 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1043 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001044 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return ret;
1047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049/*
1050 * /sys/module/foo/sections stuff
1051 * J. Corbet <corbet@lwn.net>
1052 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001053#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001054struct module_sect_attr
1055{
1056 struct module_attribute mattr;
1057 char *name;
1058 unsigned long address;
1059};
1060
1061struct module_sect_attrs
1062{
1063 struct attribute_group grp;
1064 unsigned int nsections;
1065 struct module_sect_attr attrs[0];
1066};
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068static ssize_t module_sect_show(struct module_attribute *mattr,
1069 struct module *mod, char *buf)
1070{
1071 struct module_sect_attr *sattr =
1072 container_of(mattr, struct module_sect_attr, mattr);
1073 return sprintf(buf, "0x%lx\n", sattr->address);
1074}
1075
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001076static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1077{
Rusty Russella58730c2008-03-13 09:03:44 +00001078 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001079
1080 for (section = 0; section < sect_attrs->nsections; section++)
1081 kfree(sect_attrs->attrs[section].name);
1082 kfree(sect_attrs);
1083}
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085static void add_sect_attrs(struct module *mod, unsigned int nsect,
1086 char *secstrings, Elf_Shdr *sechdrs)
1087{
1088 unsigned int nloaded = 0, i, size[2];
1089 struct module_sect_attrs *sect_attrs;
1090 struct module_sect_attr *sattr;
1091 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /* Count loaded sections and allocate structures */
1094 for (i = 0; i < nsect; i++)
1095 if (sechdrs[i].sh_flags & SHF_ALLOC)
1096 nloaded++;
1097 size[0] = ALIGN(sizeof(*sect_attrs)
1098 + nloaded * sizeof(sect_attrs->attrs[0]),
1099 sizeof(sect_attrs->grp.attrs[0]));
1100 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001101 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1102 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return;
1104
1105 /* Setup section attributes. */
1106 sect_attrs->grp.name = "sections";
1107 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1108
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001109 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 sattr = &sect_attrs->attrs[0];
1111 gattr = &sect_attrs->grp.attrs[0];
1112 for (i = 0; i < nsect; i++) {
1113 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1114 continue;
1115 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001116 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1117 GFP_KERNEL);
1118 if (sattr->name == NULL)
1119 goto out;
1120 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 sattr->mattr.show = module_sect_show;
1122 sattr->mattr.store = NULL;
1123 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 sattr->mattr.attr.mode = S_IRUGO;
1125 *(gattr++) = &(sattr++)->mattr.attr;
1126 }
1127 *gattr = NULL;
1128
1129 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1130 goto out;
1131
1132 mod->sect_attrs = sect_attrs;
1133 return;
1134 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001135 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138static void remove_sect_attrs(struct module *mod)
1139{
1140 if (mod->sect_attrs) {
1141 sysfs_remove_group(&mod->mkobj.kobj,
1142 &mod->sect_attrs->grp);
1143 /* We are positive that no one is using any sect attrs
1144 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001145 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 mod->sect_attrs = NULL;
1147 }
1148}
1149
Roland McGrath6d760132007-10-16 23:26:40 -07001150/*
1151 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1152 */
1153
1154struct module_notes_attrs {
1155 struct kobject *dir;
1156 unsigned int notes;
1157 struct bin_attribute attrs[0];
1158};
1159
1160static ssize_t module_notes_read(struct kobject *kobj,
1161 struct bin_attribute *bin_attr,
1162 char *buf, loff_t pos, size_t count)
1163{
1164 /*
1165 * The caller checked the pos and count against our size.
1166 */
1167 memcpy(buf, bin_attr->private + pos, count);
1168 return count;
1169}
1170
1171static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1172 unsigned int i)
1173{
1174 if (notes_attrs->dir) {
1175 while (i-- > 0)
1176 sysfs_remove_bin_file(notes_attrs->dir,
1177 &notes_attrs->attrs[i]);
1178 kobject_del(notes_attrs->dir);
1179 }
1180 kfree(notes_attrs);
1181}
1182
1183static void add_notes_attrs(struct module *mod, unsigned int nsect,
1184 char *secstrings, Elf_Shdr *sechdrs)
1185{
1186 unsigned int notes, loaded, i;
1187 struct module_notes_attrs *notes_attrs;
1188 struct bin_attribute *nattr;
1189
1190 /* Count notes sections and allocate structures. */
1191 notes = 0;
1192 for (i = 0; i < nsect; i++)
1193 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1194 (sechdrs[i].sh_type == SHT_NOTE))
1195 ++notes;
1196
1197 if (notes == 0)
1198 return;
1199
1200 notes_attrs = kzalloc(sizeof(*notes_attrs)
1201 + notes * sizeof(notes_attrs->attrs[0]),
1202 GFP_KERNEL);
1203 if (notes_attrs == NULL)
1204 return;
1205
1206 notes_attrs->notes = notes;
1207 nattr = &notes_attrs->attrs[0];
1208 for (loaded = i = 0; i < nsect; ++i) {
1209 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1210 continue;
1211 if (sechdrs[i].sh_type == SHT_NOTE) {
1212 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1213 nattr->attr.mode = S_IRUGO;
1214 nattr->size = sechdrs[i].sh_size;
1215 nattr->private = (void *) sechdrs[i].sh_addr;
1216 nattr->read = module_notes_read;
1217 ++nattr;
1218 }
1219 ++loaded;
1220 }
1221
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001222 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001223 if (!notes_attrs->dir)
1224 goto out;
1225
1226 for (i = 0; i < notes; ++i)
1227 if (sysfs_create_bin_file(notes_attrs->dir,
1228 &notes_attrs->attrs[i]))
1229 goto out;
1230
1231 mod->notes_attrs = notes_attrs;
1232 return;
1233
1234 out:
1235 free_notes_attrs(notes_attrs, i);
1236}
1237
1238static void remove_notes_attrs(struct module *mod)
1239{
1240 if (mod->notes_attrs)
1241 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1242}
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1247 char *sectstrings, Elf_Shdr *sechdrs)
1248{
1249}
1250
1251static inline void remove_sect_attrs(struct module *mod)
1252{
1253}
Roland McGrath6d760132007-10-16 23:26:40 -07001254
1255static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1256 char *sectstrings, Elf_Shdr *sechdrs)
1257{
1258}
1259
1260static inline void remove_notes_attrs(struct module *mod)
1261{
1262}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001263#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Randy Dunlapef665c12007-02-13 15:19:06 -08001265#ifdef CONFIG_SYSFS
1266int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001267{
1268 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001269 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001270 int error = 0;
1271 int i;
1272
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001273 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1274 (ARRAY_SIZE(modinfo_attrs) + 1)),
1275 GFP_KERNEL);
1276 if (!mod->modinfo_attrs)
1277 return -ENOMEM;
1278
1279 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001280 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1281 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001282 (attr->test && attr->test(mod))) {
1283 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001284 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1285 ++temp_attr;
1286 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001287 }
1288 return error;
1289}
1290
Randy Dunlapef665c12007-02-13 15:19:06 -08001291void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001292{
1293 struct module_attribute *attr;
1294 int i;
1295
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001296 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1297 /* pick a field to test for end of list */
1298 if (!attr->attr.name)
1299 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001300 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001301 if (attr->free)
1302 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001303 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001304 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Randy Dunlapef665c12007-02-13 15:19:06 -08001307int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001310 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001312 if (!module_sysfs_initialized) {
1313 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001314 mod->name);
1315 err = -EINVAL;
1316 goto out;
1317 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001318
1319 kobj = kset_find_obj(module_kset, mod->name);
1320 if (kobj) {
1321 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1322 kobject_put(kobj);
1323 err = -EINVAL;
1324 goto out;
1325 }
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001328
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001329 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1330 mod->mkobj.kobj.kset = module_kset;
1331 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1332 "%s", mod->name);
1333 if (err)
1334 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001335
Kay Sievers97c146e2007-11-29 23:46:11 +01001336 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001337out:
1338 return err;
1339}
1340
Randy Dunlapef665c12007-02-13 15:19:06 -08001341int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001342 struct kernel_param *kparam,
1343 unsigned int num_params)
1344{
1345 int err;
1346
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001347 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001348 if (!mod->holders_dir) {
1349 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001350 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001351 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 err = module_param_sysfs_setup(mod, kparam, num_params);
1354 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001355 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Matt Domschc988d2b2005-06-23 22:05:15 -07001357 err = module_add_modinfo_attrs(mod);
1358 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001359 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001360
Kay Sieverse17e0f52006-11-24 12:15:25 +01001361 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return 0;
1363
Kay Sieverse17e0f52006-11-24 12:15:25 +01001364out_unreg_param:
1365 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001366out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001367 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001368out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001369 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return err;
1371}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001372
1373static void mod_sysfs_fini(struct module *mod)
1374{
1375 kobject_put(&mod->mkobj.kobj);
1376}
1377
1378#else /* CONFIG_SYSFS */
1379
1380static void mod_sysfs_fini(struct module *mod)
1381{
1382}
1383
1384#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386static void mod_kobject_remove(struct module *mod)
1387{
Matt Domschc988d2b2005-06-23 22:05:15 -07001388 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001390 kobject_put(mod->mkobj.drivers_dir);
1391 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001392 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395/*
Rusty Russellbb9d3d52008-01-29 17:13:21 -05001396 * link the module with the whole machine is stopped with interrupts off
1397 * - this defends against kallsyms not taking locks
1398 */
1399static int __link_module(void *_mod)
1400{
1401 struct module *mod = _mod;
1402 list_add(&mod->list, &modules);
1403 return 0;
1404}
1405
1406/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * unlink the module with the whole machine is stopped with interrupts off
1408 * - this defends against kallsyms not taking locks
1409 */
1410static int __unlink_module(void *_mod)
1411{
1412 struct module *mod = _mod;
1413 list_del(&mod->list);
1414 return 0;
1415}
1416
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001417/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418static void free_module(struct module *mod)
1419{
1420 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001421 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001422 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 remove_sect_attrs(mod);
1424 mod_kobject_remove(mod);
1425
Jan Beulich4552d5d2006-06-26 13:57:28 +02001426 unwind_remove_table(mod->unwind_info, 0);
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /* Arch-specific cleanup. */
1429 module_arch_cleanup(mod);
1430
1431 /* Module unload stuff */
1432 module_unload_free(mod);
1433
1434 /* This may be NULL, but that's OK */
1435 module_free(mod, mod->module_init);
1436 kfree(mod->args);
1437 if (mod->percpu)
1438 percpu_modfree(mod->percpu);
1439
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001440 /* Free lock-classes: */
1441 lockdep_free_key_range(mod->module_core, mod->core_size);
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Finally, free the core (containing the module structure) */
1444 module_free(mod, mod->module_core);
1445}
1446
1447void *__symbol_get(const char *symbol)
1448{
1449 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001450 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Rusty Russell24da1cb2007-07-15 23:41:46 -07001452 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001453 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001454 if (IS_ERR_VALUE(value))
1455 value = 0;
1456 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001458 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 return (void *)value;
1461}
1462EXPORT_SYMBOL_GPL(__symbol_get);
1463
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001464/*
1465 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001466 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001467 */
1468static int verify_export_symbols(struct module *mod)
1469{
Rusty Russellb2111042008-05-01 21:15:00 -05001470 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001471 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001472 const struct kernel_symbol *s;
1473 struct {
1474 const struct kernel_symbol *sym;
1475 unsigned int num;
1476 } arr[] = {
1477 { mod->syms, mod->num_syms },
1478 { mod->gpl_syms, mod->num_gpl_syms },
1479 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001480#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001481 { mod->unused_syms, mod->num_unused_syms },
1482 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001483#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001484 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001485
Rusty Russellb2111042008-05-01 21:15:00 -05001486 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1487 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1488 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1489 NULL, true, false))) {
1490 printk(KERN_ERR
1491 "%s: exports duplicate symbol %s"
1492 " (owned by %s)\n",
1493 mod->name, s->name, module_name(owner));
1494 return -ENOEXEC;
1495 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001496 }
Rusty Russellb2111042008-05-01 21:15:00 -05001497 }
1498 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001499}
1500
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001501/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502static int simplify_symbols(Elf_Shdr *sechdrs,
1503 unsigned int symindex,
1504 const char *strtab,
1505 unsigned int versindex,
1506 unsigned int pcpuindex,
1507 struct module *mod)
1508{
1509 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1510 unsigned long secbase;
1511 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1512 int ret = 0;
1513
1514 for (i = 1; i < n; i++) {
1515 switch (sym[i].st_shndx) {
1516 case SHN_COMMON:
1517 /* We compiled with -fno-common. These are not
1518 supposed to happen. */
1519 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1520 printk("%s: please compile with -fno-common\n",
1521 mod->name);
1522 ret = -ENOEXEC;
1523 break;
1524
1525 case SHN_ABS:
1526 /* Don't need to do anything */
1527 DEBUGP("Absolute symbol: 0x%08lx\n",
1528 (long)sym[i].st_value);
1529 break;
1530
1531 case SHN_UNDEF:
1532 sym[i].st_value
1533 = resolve_symbol(sechdrs, versindex,
1534 strtab + sym[i].st_name, mod);
1535
1536 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001537 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 break;
1539 /* Ok if weak. */
1540 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1541 break;
1542
1543 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1544 mod->name, strtab + sym[i].st_name);
1545 ret = -ENOENT;
1546 break;
1547
1548 default:
1549 /* Divert to percpu allocation if a percpu var. */
1550 if (sym[i].st_shndx == pcpuindex)
1551 secbase = (unsigned long)mod->percpu;
1552 else
1553 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1554 sym[i].st_value += secbase;
1555 break;
1556 }
1557 }
1558
1559 return ret;
1560}
1561
1562/* Update size with this section: return offset. */
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05001563static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 long ret;
1566
1567 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1568 *size = ret + sechdr->sh_size;
1569 return ret;
1570}
1571
1572/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1573 might -- code, read-only data, read-write data, small data. Tally
1574 sizes, and place the offsets into sh_entsize fields: high bit means it
1575 belongs in init. */
1576static void layout_sections(struct module *mod,
1577 const Elf_Ehdr *hdr,
1578 Elf_Shdr *sechdrs,
1579 const char *secstrings)
1580{
1581 static unsigned long const masks[][2] = {
1582 /* NOTE: all executable code must be the first section
1583 * in this array; otherwise modify the text_size
1584 * finder in the two loops below */
1585 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1586 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1587 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1588 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1589 };
1590 unsigned int m, i;
1591
1592 for (i = 0; i < hdr->e_shnum; i++)
1593 sechdrs[i].sh_entsize = ~0UL;
1594
1595 DEBUGP("Core section allocation order:\n");
1596 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1597 for (i = 0; i < hdr->e_shnum; ++i) {
1598 Elf_Shdr *s = &sechdrs[i];
1599
1600 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1601 || (s->sh_flags & masks[m][1])
1602 || s->sh_entsize != ~0UL
1603 || strncmp(secstrings + s->sh_name,
1604 ".init", 5) == 0)
1605 continue;
1606 s->sh_entsize = get_offset(&mod->core_size, s);
1607 DEBUGP("\t%s\n", secstrings + s->sh_name);
1608 }
1609 if (m == 0)
1610 mod->core_text_size = mod->core_size;
1611 }
1612
1613 DEBUGP("Init section allocation order:\n");
1614 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1615 for (i = 0; i < hdr->e_shnum; ++i) {
1616 Elf_Shdr *s = &sechdrs[i];
1617
1618 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1619 || (s->sh_flags & masks[m][1])
1620 || s->sh_entsize != ~0UL
1621 || strncmp(secstrings + s->sh_name,
1622 ".init", 5) != 0)
1623 continue;
1624 s->sh_entsize = (get_offset(&mod->init_size, s)
1625 | INIT_OFFSET_MASK);
1626 DEBUGP("\t%s\n", secstrings + s->sh_name);
1627 }
1628 if (m == 0)
1629 mod->init_text_size = mod->init_size;
1630 }
1631}
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633static void set_license(struct module *mod, const char *license)
1634{
1635 if (!license)
1636 license = "unspecified";
1637
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001638 if (!license_is_gpl_compatible(license)) {
1639 if (!(tainted & TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001640 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001641 "kernel.\n", mod->name, license);
1642 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644}
1645
1646/* Parse tag=value strings from .modinfo section */
1647static char *next_string(char *string, unsigned long *secsize)
1648{
1649 /* Skip non-zero chars */
1650 while (string[0]) {
1651 string++;
1652 if ((*secsize)-- <= 1)
1653 return NULL;
1654 }
1655
1656 /* Skip any zero padding. */
1657 while (!string[0]) {
1658 string++;
1659 if ((*secsize)-- <= 1)
1660 return NULL;
1661 }
1662 return string;
1663}
1664
1665static char *get_modinfo(Elf_Shdr *sechdrs,
1666 unsigned int info,
1667 const char *tag)
1668{
1669 char *p;
1670 unsigned int taglen = strlen(tag);
1671 unsigned long size = sechdrs[info].sh_size;
1672
1673 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1674 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1675 return p + taglen + 1;
1676 }
1677 return NULL;
1678}
1679
Matt Domschc988d2b2005-06-23 22:05:15 -07001680static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1681 unsigned int infoindex)
1682{
1683 struct module_attribute *attr;
1684 int i;
1685
1686 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1687 if (attr->setup)
1688 attr->setup(mod,
1689 get_modinfo(sechdrs,
1690 infoindex,
1691 attr->attr.name));
1692 }
1693}
Matt Domschc988d2b2005-06-23 22:05:15 -07001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001696
1697/* lookup symbol in given range of kernel_symbols */
1698static const struct kernel_symbol *lookup_symbol(const char *name,
1699 const struct kernel_symbol *start,
1700 const struct kernel_symbol *stop)
1701{
1702 const struct kernel_symbol *ks = start;
1703 for (; ks < stop; ks++)
1704 if (strcmp(ks->name, name) == 0)
1705 return ks;
1706 return NULL;
1707}
1708
Alexey Dobriyanea078902007-05-08 00:28:39 -07001709static int is_exported(const char *name, const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001711 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1712 return 1;
1713 else
Jesper Juhlf867d2a2006-06-25 05:47:09 -07001714 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001716 else
1717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720/* As per nm */
1721static char elf_type(const Elf_Sym *sym,
1722 Elf_Shdr *sechdrs,
1723 const char *secstrings,
1724 struct module *mod)
1725{
1726 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1727 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1728 return 'v';
1729 else
1730 return 'w';
1731 }
1732 if (sym->st_shndx == SHN_UNDEF)
1733 return 'U';
1734 if (sym->st_shndx == SHN_ABS)
1735 return 'a';
1736 if (sym->st_shndx >= SHN_LORESERVE)
1737 return '?';
1738 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1739 return 't';
1740 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1741 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1742 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1743 return 'r';
1744 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1745 return 'g';
1746 else
1747 return 'd';
1748 }
1749 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1750 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1751 return 's';
1752 else
1753 return 'b';
1754 }
1755 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1756 ".debug", strlen(".debug")) == 0)
1757 return 'n';
1758 return '?';
1759}
1760
1761static void add_kallsyms(struct module *mod,
1762 Elf_Shdr *sechdrs,
1763 unsigned int symindex,
1764 unsigned int strindex,
1765 const char *secstrings)
1766{
1767 unsigned int i;
1768
1769 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1770 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1771 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1772
1773 /* Set types up while we still have access to sections. */
1774 for (i = 0; i < mod->num_symtab; i++)
1775 mod->symtab[i].st_info
1776 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1777}
1778#else
1779static inline void add_kallsyms(struct module *mod,
1780 Elf_Shdr *sechdrs,
1781 unsigned int symindex,
1782 unsigned int strindex,
1783 const char *secstrings)
1784{
1785}
1786#endif /* CONFIG_KALLSYMS */
1787
Rusty Russell3a642e92008-07-22 19:24:28 -05001788static void *module_alloc_update_bounds(unsigned long size)
1789{
1790 void *ret = module_alloc(size);
1791
1792 if (ret) {
1793 /* Update module bounds. */
1794 if ((unsigned long)ret < module_addr_min)
1795 module_addr_min = (unsigned long)ret;
1796 if ((unsigned long)ret + size > module_addr_max)
1797 module_addr_max = (unsigned long)ret + size;
1798 }
1799 return ret;
1800}
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802/* Allocate and load the module: note that size of section 0 is always
1803 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001804static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 unsigned long len,
1806 const char __user *uargs)
1807{
1808 Elf_Ehdr *hdr;
1809 Elf_Shdr *sechdrs;
1810 char *secstrings, *args, *modmagic, *strtab = NULL;
Andrew Morton84860f92006-06-28 04:26:46 -07001811 unsigned int i;
1812 unsigned int symindex = 0;
1813 unsigned int strindex = 0;
1814 unsigned int setupindex;
1815 unsigned int exindex;
1816 unsigned int exportindex;
1817 unsigned int modindex;
1818 unsigned int obsparmindex;
1819 unsigned int infoindex;
1820 unsigned int gplindex;
1821 unsigned int crcindex;
1822 unsigned int gplcrcindex;
1823 unsigned int versindex;
1824 unsigned int pcpuindex;
1825 unsigned int gplfutureindex;
1826 unsigned int gplfuturecrcindex;
1827 unsigned int unwindex = 0;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001828#ifdef CONFIG_UNUSED_SYMBOLS
Andrew Morton84860f92006-06-28 04:26:46 -07001829 unsigned int unusedindex;
1830 unsigned int unusedcrcindex;
1831 unsigned int unusedgplindex;
1832 unsigned int unusedgplcrcindex;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001833#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001834 unsigned int markersindex;
1835 unsigned int markersstringsindex;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04001836 unsigned int tracepointsindex;
1837 unsigned int tracepointsstringsindex;
Steven Rostedt90d595f2008-08-14 15:45:09 -04001838 unsigned int mcountindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 struct module *mod;
1840 long err = 0;
1841 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1842 struct exception_table_entry *extable;
Thomas Koeller378bac82005-09-06 15:17:11 -07001843 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1846 umod, len, uargs);
1847 if (len < sizeof(*hdr))
1848 return ERR_PTR(-ENOEXEC);
1849
1850 /* Suck in entire file: we'll want most of it. */
1851 /* vmalloc barfs on "unusual" numbers. Check here */
1852 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1853 return ERR_PTR(-ENOMEM);
1854 if (copy_from_user(hdr, umod, len) != 0) {
1855 err = -EFAULT;
1856 goto free_hdr;
1857 }
1858
1859 /* Sanity checks against insmoding binaries or wrong arch,
1860 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001861 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 || hdr->e_type != ET_REL
1863 || !elf_check_arch(hdr)
1864 || hdr->e_shentsize != sizeof(*sechdrs)) {
1865 err = -ENOEXEC;
1866 goto free_hdr;
1867 }
1868
1869 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1870 goto truncated;
1871
1872 /* Convenience variables */
1873 sechdrs = (void *)hdr + hdr->e_shoff;
1874 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1875 sechdrs[0].sh_addr = 0;
1876
1877 for (i = 1; i < hdr->e_shnum; i++) {
1878 if (sechdrs[i].sh_type != SHT_NOBITS
1879 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1880 goto truncated;
1881
1882 /* Mark all sections sh_addr with their address in the
1883 temporary image. */
1884 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1885
1886 /* Internal symbols and strings. */
1887 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1888 symindex = i;
1889 strindex = sechdrs[i].sh_link;
1890 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1891 }
1892#ifndef CONFIG_MODULE_UNLOAD
1893 /* Don't load .exit sections */
1894 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1895 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1896#endif
1897 }
1898
1899 modindex = find_sec(hdr, sechdrs, secstrings,
1900 ".gnu.linkonce.this_module");
1901 if (!modindex) {
1902 printk(KERN_WARNING "No module found in object\n");
1903 err = -ENOEXEC;
1904 goto free_hdr;
1905 }
1906 mod = (void *)sechdrs[modindex].sh_addr;
1907
1908 if (symindex == 0) {
1909 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1910 mod->name);
1911 err = -ENOEXEC;
1912 goto free_hdr;
1913 }
1914
1915 /* Optional sections */
1916 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1917 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001918 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1920 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001921 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001922#ifdef CONFIG_UNUSED_SYMBOLS
1923 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1924 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001925 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1926 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001927#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1929 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1930 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1931 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1932 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1933 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001934#ifdef ARCH_UNWIND_SECTION_NAME
1935 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1936#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Rusty Russellea01e792008-03-13 09:02:17 +00001938 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001940 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941#ifdef CONFIG_KALLSYMS
1942 /* Keep symbol and string tables for decoding later. */
1943 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1944 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1945#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001946 if (unwindex)
1947 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 /* Check module struct version now, before we try to use module. */
1950 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1951 err = -ENOEXEC;
1952 goto free_hdr;
1953 }
1954
1955 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1956 /* This is allowed: modprobe --force will invalidate it. */
1957 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001958 err = try_to_force_load(mod, "magic");
1959 if (err)
1960 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001961 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1963 mod->name, modmagic, vermagic);
1964 err = -ENOEXEC;
1965 goto free_hdr;
1966 }
1967
1968 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001969 args = strndup_user(uargs, ~0UL >> 1);
1970 if (IS_ERR(args)) {
1971 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 goto free_hdr;
1973 }
Andrew Morton8e08b752006-02-07 12:58:45 -08001974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (find_module(mod->name)) {
1976 err = -EEXIST;
1977 goto free_mod;
1978 }
1979
1980 mod->state = MODULE_STATE_COMING;
1981
1982 /* Allow arches to frob section contents and sizes. */
1983 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1984 if (err < 0)
1985 goto free_mod;
1986
1987 if (pcpuindex) {
1988 /* We have a special allocation for this section. */
1989 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07001990 sechdrs[pcpuindex].sh_addralign,
1991 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (!percpu) {
1993 err = -ENOMEM;
1994 goto free_mod;
1995 }
1996 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1997 mod->percpu = percpu;
1998 }
1999
2000 /* Determine total sizes, and put offsets in sh_entsize. For now
2001 this is done generically; there doesn't appear to be any
2002 special cases for the architectures. */
2003 layout_sections(mod, hdr, sechdrs, secstrings);
2004
2005 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002006 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (!ptr) {
2008 err = -ENOMEM;
2009 goto free_percpu;
2010 }
2011 memset(ptr, 0, mod->core_size);
2012 mod->module_core = ptr;
2013
Rusty Russell3a642e92008-07-22 19:24:28 -05002014 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (!ptr && mod->init_size) {
2016 err = -ENOMEM;
2017 goto free_core;
2018 }
2019 memset(ptr, 0, mod->init_size);
2020 mod->module_init = ptr;
2021
2022 /* Transfer each section which specifies SHF_ALLOC */
2023 DEBUGP("final section addresses:\n");
2024 for (i = 0; i < hdr->e_shnum; i++) {
2025 void *dest;
2026
2027 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2028 continue;
2029
2030 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2031 dest = mod->module_init
2032 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2033 else
2034 dest = mod->module_core + sechdrs[i].sh_entsize;
2035
2036 if (sechdrs[i].sh_type != SHT_NOBITS)
2037 memcpy(dest, (void *)sechdrs[i].sh_addr,
2038 sechdrs[i].sh_size);
2039 /* Update sh_addr to point to copy in image. */
2040 sechdrs[i].sh_addr = (unsigned long)dest;
2041 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2042 }
2043 /* Module has been moved. */
2044 mod = (void *)sechdrs[modindex].sh_addr;
2045
2046 /* Now we've moved module, initialize linked lists, etc. */
2047 module_unload_init(mod);
2048
Kay Sievers97c146e2007-11-29 23:46:11 +01002049 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002050 err = mod_sysfs_init(mod);
2051 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002052 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /* Set up license info based on the info section */
2055 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2056
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002057 /*
2058 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2059 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2060 * using GPL-only symbols it needs.
2061 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002062 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002063 add_taint(TAINT_PROPRIETARY_MODULE);
2064
2065 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002066 if (strcmp(mod->name, "driverloader") == 0)
2067 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002068
Matt Domschc988d2b2005-06-23 22:05:15 -07002069 /* Set up MODINFO_ATTR fields */
2070 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 /* Fix up syms, so that st_value is a pointer to location. */
2073 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2074 mod);
2075 if (err < 0)
2076 goto cleanup;
2077
2078 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
2079 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
2080 mod->syms = (void *)sechdrs[exportindex].sh_addr;
2081 if (crcindex)
2082 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
2083 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
2084 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
2085 if (gplcrcindex)
2086 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08002087 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
2088 sizeof(*mod->gpl_future_syms);
2089 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
2090 if (gplfuturecrcindex)
2091 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002093#ifdef CONFIG_UNUSED_SYMBOLS
2094 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
2095 sizeof(*mod->unused_syms);
2096 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
2097 sizeof(*mod->unused_gpl_syms);
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002098 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
2099 if (unusedcrcindex)
2100 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
2101 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
2102 if (unusedgplcrcindex)
Rusty Russell4e2d9242008-05-01 21:15:00 -05002103 mod->unused_gpl_crcs
2104 = (void *)sechdrs[unusedgplcrcindex].sh_addr;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002105#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107#ifdef CONFIG_MODVERSIONS
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002108 if ((mod->num_syms && !crcindex)
2109 || (mod->num_gpl_syms && !gplcrcindex)
2110 || (mod->num_gpl_future_syms && !gplfuturecrcindex)
2111#ifdef CONFIG_UNUSED_SYMBOLS
2112 || (mod->num_unused_syms && !unusedcrcindex)
2113 || (mod->num_unused_gpl_syms && !unusedgplcrcindex)
2114#endif
2115 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002116 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2117 err = try_to_force_load(mod, "nocrc");
2118 if (err)
2119 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
2121#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002122 markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
2123 markersstringsindex = find_sec(hdr, sechdrs, secstrings,
2124 "__markers_strings");
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002125 tracepointsindex = find_sec(hdr, sechdrs, secstrings, "__tracepoints");
2126 tracepointsstringsindex = find_sec(hdr, sechdrs, secstrings,
2127 "__tracepoints_strings");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Steven Rostedt90d595f2008-08-14 15:45:09 -04002129 mcountindex = find_sec(hdr, sechdrs, secstrings,
2130 "__mcount_loc");
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /* Now do relocations. */
2133 for (i = 1; i < hdr->e_shnum; i++) {
2134 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2135 unsigned int info = sechdrs[i].sh_info;
2136
2137 /* Not a valid relocation section? */
2138 if (info >= hdr->e_shnum)
2139 continue;
2140
2141 /* Don't bother with non-allocated sections */
2142 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2143 continue;
2144
2145 if (sechdrs[i].sh_type == SHT_REL)
2146 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2147 else if (sechdrs[i].sh_type == SHT_RELA)
2148 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2149 mod);
2150 if (err < 0)
2151 goto cleanup;
2152 }
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002153#ifdef CONFIG_MARKERS
2154 mod->markers = (void *)sechdrs[markersindex].sh_addr;
2155 mod->num_markers =
2156 sechdrs[markersindex].sh_size / sizeof(*mod->markers);
2157#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002158#ifdef CONFIG_TRACEPOINTS
2159 mod->tracepoints = (void *)sechdrs[tracepointsindex].sh_addr;
2160 mod->num_tracepoints =
2161 sechdrs[tracepointsindex].sh_size / sizeof(*mod->tracepoints);
2162#endif
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002165 /* Find duplicate symbols */
2166 err = verify_export_symbols(mod);
2167
2168 if (err < 0)
2169 goto cleanup;
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /* Set up and sort exception table */
2172 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
2173 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
2174 sort_extable(extable, extable + mod->num_exentries);
2175
2176 /* Finally, copy percpu area over. */
2177 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2178 sechdrs[pcpuindex].sh_size);
2179
2180 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2181
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002182 if (!mod->taints) {
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002183#ifdef CONFIG_MARKERS
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002184 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002185 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002186#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002187#ifdef CONFIG_TRACEPOINTS
2188 tracepoint_update_probe_range(mod->tracepoints,
2189 mod->tracepoints + mod->num_tracepoints);
2190#endif
2191 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002192
2193 if (mcountindex) {
2194 void *mseg = (void *)sechdrs[mcountindex].sh_addr;
2195 ftrace_init_module(mseg, mseg + sechdrs[mcountindex].sh_size);
2196 }
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 err = module_finalize(hdr, sechdrs, mod);
2199 if (err < 0)
2200 goto cleanup;
2201
Thomas Koeller378bac82005-09-06 15:17:11 -07002202 /* flush the icache in correct context */
2203 old_fs = get_fs();
2204 set_fs(KERNEL_DS);
2205
2206 /*
2207 * Flush the instruction cache, since we've played with text.
2208 * Do it before processing of module parameters, so the module
2209 * can provide parameter accessor functions of its own.
2210 */
2211 if (mod->module_init)
2212 flush_icache_range((unsigned long)mod->module_init,
2213 (unsigned long)mod->module_init
2214 + mod->init_size);
2215 flush_icache_range((unsigned long)mod->module_core,
2216 (unsigned long)mod->module_core + mod->core_size);
2217
2218 set_fs(old_fs);
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 mod->args = args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002221 if (obsparmindex)
2222 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2223 mod->name);
2224
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002225 /* Now sew it into the lists so we can get lockdep and oops
2226 * info during argument parsing. Noone should access us, since
2227 * strong_try_module_get() will fail. */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002228 stop_machine(__link_module, mod, NULL);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002229
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002230 /* Size of section 0 is 0, so this works well if no params */
2231 err = parse_args(mod->name, mod->args,
2232 (struct kernel_param *)
2233 sechdrs[setupindex].sh_addr,
2234 sechdrs[setupindex].sh_size
2235 / sizeof(struct kernel_param),
2236 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002238 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Daniel Walker22a8bde2007-10-18 03:06:07 -07002240 err = mod_sysfs_setup(mod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 (struct kernel_param *)
2242 sechdrs[setupindex].sh_addr,
2243 sechdrs[setupindex].sh_size
2244 / sizeof(struct kernel_param));
2245 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002246 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002248 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Jan Beulich4552d5d2006-06-26 13:57:28 +02002250 /* Size of section 0 is 0, so this works well if no unwind info. */
2251 mod->unwind_info = unwind_add_table(mod,
Daniel Walker22a8bde2007-10-18 03:06:07 -07002252 (void *)sechdrs[unwindex].sh_addr,
2253 sechdrs[unwindex].sh_size);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /* Get rid of temporary copy */
2256 vfree(hdr);
2257
2258 /* Done! */
2259 return mod;
2260
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002261 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002262 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 module_arch_cleanup(mod);
2264 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002265 kobject_del(&mod->mkobj.kobj);
2266 kobject_put(&mod->mkobj.kobj);
2267 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 module_unload_free(mod);
2269 module_free(mod, mod->module_init);
2270 free_core:
2271 module_free(mod, mod->module_core);
2272 free_percpu:
2273 if (percpu)
2274 percpu_modfree(percpu);
2275 free_mod:
2276 kfree(args);
2277 free_hdr:
2278 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002279 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 truncated:
2282 printk(KERN_ERR "Module len %lu truncated\n", len);
2283 err = -ENOEXEC;
2284 goto free_hdr;
2285}
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287/* This is where the real work happens */
2288asmlinkage long
2289sys_init_module(void __user *umod,
2290 unsigned long len,
2291 const char __user *uargs)
2292{
2293 struct module *mod;
2294 int ret = 0;
2295
2296 /* Must have permission */
2297 if (!capable(CAP_SYS_MODULE))
2298 return -EPERM;
2299
2300 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002301 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return -EINTR;
2303
2304 /* Do all the hard work */
2305 mod = load_module(umod, len, uargs);
2306 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002307 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return PTR_ERR(mod);
2309 }
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002312 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Alan Sterne041c682006-03-27 01:16:30 -08002314 blocking_notifier_call_chain(&module_notify_list,
2315 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 /* Start the module */
2318 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002319 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (ret < 0) {
2321 /* Init routine failed: abort. Try to protect us from
2322 buggy refcounters. */
2323 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002324 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002325 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002326 blocking_notifier_call_chain(&module_notify_list,
2327 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002328 mutex_lock(&module_mutex);
2329 free_module(mod);
2330 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002331 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return ret;
2333 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002334 if (ret > 0) {
2335 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2336 "it should follow 0/-E convention\n"
2337 KERN_WARNING "%s: loading module anyway...\n",
2338 __func__, mod->name, ret,
2339 __func__);
2340 dump_stack();
2341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Rusty Russell6c5db222008-03-10 11:43:52 -07002343 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002345 wake_up(&module_wq);
2346
2347 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 /* Drop initial reference. */
2349 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002350 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 module_free(mod, mod->module_init);
2352 mod->module_init = NULL;
2353 mod->init_size = 0;
2354 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002355 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 return 0;
2358}
2359
2360static inline int within(unsigned long addr, void *start, unsigned long size)
2361{
2362 return ((void *)addr >= start && (void *)addr < start + size);
2363}
2364
2365#ifdef CONFIG_KALLSYMS
2366/*
2367 * This ignores the intensely annoying "mapping symbols" found
2368 * in ARM ELF files: $a, $t and $d.
2369 */
2370static inline int is_arm_mapping_symbol(const char *str)
2371{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002372 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 && (str[2] == '\0' || str[2] == '.');
2374}
2375
2376static const char *get_ksymbol(struct module *mod,
2377 unsigned long addr,
2378 unsigned long *size,
2379 unsigned long *offset)
2380{
2381 unsigned int i, best = 0;
2382 unsigned long nextval;
2383
2384 /* At worse, next value is at end of module */
2385 if (within(addr, mod->module_init, mod->init_size))
2386 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002387 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2389
2390 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002391 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 for (i = 1; i < mod->num_symtab; i++) {
2393 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2394 continue;
2395
2396 /* We ignore unnamed symbols: they're uninformative
2397 * and inserted at a whim. */
2398 if (mod->symtab[i].st_value <= addr
2399 && mod->symtab[i].st_value > mod->symtab[best].st_value
2400 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2401 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2402 best = i;
2403 if (mod->symtab[i].st_value > addr
2404 && mod->symtab[i].st_value < nextval
2405 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2406 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2407 nextval = mod->symtab[i].st_value;
2408 }
2409
2410 if (!best)
2411 return NULL;
2412
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002413 if (size)
2414 *size = nextval - mod->symtab[best].st_value;
2415 if (offset)
2416 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return mod->strtab + mod->symtab[best].st_name;
2418}
2419
Rusty Russell6dd06c92008-01-29 17:13:22 -05002420/* For kallsyms to ask for address resolution. NULL means not found. Careful
2421 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002422const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002423 unsigned long *size,
2424 unsigned long *offset,
2425 char **modname,
2426 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
2428 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002429 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Rusty Russellcb2a5202008-01-14 00:55:03 -08002431 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 list_for_each_entry(mod, &modules, list) {
2433 if (within(addr, mod->module_init, mod->init_size)
2434 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002435 if (modname)
2436 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002437 ret = get_ksymbol(mod, addr, size, offset);
2438 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002441 /* Make a copy in here where it's safe */
2442 if (ret) {
2443 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2444 ret = namebuf;
2445 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002446 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002447 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002450int lookup_module_symbol_name(unsigned long addr, char *symname)
2451{
2452 struct module *mod;
2453
Rusty Russellcb2a5202008-01-14 00:55:03 -08002454 preempt_disable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002455 list_for_each_entry(mod, &modules, list) {
2456 if (within(addr, mod->module_init, mod->init_size) ||
2457 within(addr, mod->module_core, mod->core_size)) {
2458 const char *sym;
2459
2460 sym = get_ksymbol(mod, addr, NULL, NULL);
2461 if (!sym)
2462 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002463 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002464 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002465 return 0;
2466 }
2467 }
2468out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002469 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002470 return -ERANGE;
2471}
2472
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002473int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2474 unsigned long *offset, char *modname, char *name)
2475{
2476 struct module *mod;
2477
Rusty Russellcb2a5202008-01-14 00:55:03 -08002478 preempt_disable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002479 list_for_each_entry(mod, &modules, list) {
2480 if (within(addr, mod->module_init, mod->init_size) ||
2481 within(addr, mod->module_core, mod->core_size)) {
2482 const char *sym;
2483
2484 sym = get_ksymbol(mod, addr, size, offset);
2485 if (!sym)
2486 goto out;
2487 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002488 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002489 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002490 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002491 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002492 return 0;
2493 }
2494 }
2495out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002496 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002497 return -ERANGE;
2498}
2499
Alexey Dobriyanea078902007-05-08 00:28:39 -07002500int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2501 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
2503 struct module *mod;
2504
Rusty Russellcb2a5202008-01-14 00:55:03 -08002505 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 list_for_each_entry(mod, &modules, list) {
2507 if (symnum < mod->num_symtab) {
2508 *value = mod->symtab[symnum].st_value;
2509 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002510 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002511 KSYM_NAME_LEN);
2512 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002513 *exported = is_exported(name, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002514 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
2517 symnum -= mod->num_symtab;
2518 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002519 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002520 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
2523static unsigned long mod_find_symname(struct module *mod, const char *name)
2524{
2525 unsigned int i;
2526
2527 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002528 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2529 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return mod->symtab[i].st_value;
2531 return 0;
2532}
2533
2534/* Look for this name: can be of form module:name. */
2535unsigned long module_kallsyms_lookup_name(const char *name)
2536{
2537 struct module *mod;
2538 char *colon;
2539 unsigned long ret = 0;
2540
2541 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002542 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if ((colon = strchr(name, ':')) != NULL) {
2544 *colon = '\0';
2545 if ((mod = find_module(name)) != NULL)
2546 ret = mod_find_symname(mod, colon+1);
2547 *colon = ':';
2548 } else {
2549 list_for_each_entry(mod, &modules, list)
2550 if ((ret = mod_find_symname(mod, name)) != 0)
2551 break;
2552 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002553 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return ret;
2555}
2556#endif /* CONFIG_KALLSYMS */
2557
2558/* Called by the /proc file system to return a list of modules. */
2559static void *m_start(struct seq_file *m, loff_t *pos)
2560{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002561 mutex_lock(&module_mutex);
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002562 return seq_list_start(&modules, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
2565static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2566{
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002567 return seq_list_next(p, &modules, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570static void m_stop(struct seq_file *m, void *p)
2571{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002572 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002575static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002576{
2577 int bx = 0;
2578
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002579 if (mod->taints ||
2580 mod->state == MODULE_STATE_GOING ||
2581 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002582 buf[bx++] = '(';
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002583 if (mod->taints & TAINT_PROPRIETARY_MODULE)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002584 buf[bx++] = 'P';
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002585 if (mod->taints & TAINT_FORCED_MODULE)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002586 buf[bx++] = 'F';
2587 /*
2588 * TAINT_FORCED_RMMOD: could be added.
2589 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2590 * apply to modules.
2591 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002592
2593 /* Show a - for module-is-being-unloaded */
2594 if (mod->state == MODULE_STATE_GOING)
2595 buf[bx++] = '-';
2596 /* Show a + for module-is-being-loaded */
2597 if (mod->state == MODULE_STATE_COMING)
2598 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002599 buf[bx++] = ')';
2600 }
2601 buf[bx] = '\0';
2602
2603 return buf;
2604}
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606static int m_show(struct seq_file *m, void *p)
2607{
2608 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002609 char buf[8];
2610
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002611 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 mod->name, mod->init_size + mod->core_size);
2613 print_unload_info(m, mod);
2614
2615 /* Informative for users. */
2616 seq_printf(m, " %s",
2617 mod->state == MODULE_STATE_GOING ? "Unloading":
2618 mod->state == MODULE_STATE_COMING ? "Loading":
2619 "Live");
2620 /* Used by oprofile and other similar tools. */
2621 seq_printf(m, " 0x%p", mod->module_core);
2622
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002623 /* Taints info */
2624 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002625 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 seq_printf(m, "\n");
2628 return 0;
2629}
2630
2631/* Format: modulename size refcount deps address
2632
2633 Where refcount is a number or -, and deps is a comma-separated list
2634 of depends or -.
2635*/
Helge Deller15ad7cd2006-12-06 20:40:36 -08002636const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 .start = m_start,
2638 .next = m_next,
2639 .stop = m_stop,
2640 .show = m_show
2641};
2642
2643/* Given an address, look for it in the module exception tables. */
2644const struct exception_table_entry *search_module_extables(unsigned long addr)
2645{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 const struct exception_table_entry *e = NULL;
2647 struct module *mod;
2648
Rusty Russell24da1cb2007-07-15 23:41:46 -07002649 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 list_for_each_entry(mod, &modules, list) {
2651 if (mod->num_exentries == 0)
2652 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 e = search_extable(mod->extable,
2655 mod->extable + mod->num_exentries - 1,
2656 addr);
2657 if (e)
2658 break;
2659 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002660 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002663 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return e;
2665}
2666
Ingo Molnar4d435f92006-07-03 00:24:24 -07002667/*
2668 * Is this a valid module address?
2669 */
2670int is_module_address(unsigned long addr)
2671{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002672 struct module *mod;
2673
Rusty Russell24da1cb2007-07-15 23:41:46 -07002674 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002675
2676 list_for_each_entry(mod, &modules, list) {
2677 if (within(addr, mod->module_core, mod->core_size)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002678 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002679 return 1;
2680 }
2681 }
2682
Rusty Russell24da1cb2007-07-15 23:41:46 -07002683 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002684
2685 return 0;
2686}
2687
2688
Rusty Russell24da1cb2007-07-15 23:41:46 -07002689/* Is this a valid kernel address? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690struct module *__module_text_address(unsigned long addr)
2691{
2692 struct module *mod;
2693
Rusty Russell3a642e92008-07-22 19:24:28 -05002694 if (addr < module_addr_min || addr > module_addr_max)
2695 return NULL;
2696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 list_for_each_entry(mod, &modules, list)
2698 if (within(addr, mod->module_init, mod->init_text_size)
2699 || within(addr, mod->module_core, mod->core_text_size))
2700 return mod;
2701 return NULL;
2702}
2703
2704struct module *module_text_address(unsigned long addr)
2705{
2706 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Rusty Russell24da1cb2007-07-15 23:41:46 -07002708 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002710 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712 return mod;
2713}
2714
2715/* Don't grab lock, we're oopsing. */
2716void print_modules(void)
2717{
2718 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002719 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 printk("Modules linked in:");
2722 list_for_each_entry(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002723 printk(" %s%s", mod->name, module_flags(mod, buf));
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002724 if (last_unloaded_module[0])
2725 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 printk("\n");
2727}
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729#ifdef CONFIG_MODVERSIONS
2730/* Generate the signature for struct module here, too, for modversions. */
2731void struct_module(struct module *mod) { return; }
2732EXPORT_SYMBOL(struct_module);
2733#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002734
2735#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002736void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002737{
2738 struct module *mod;
2739
2740 mutex_lock(&module_mutex);
2741 list_for_each_entry(mod, &modules, list)
2742 if (!mod->taints)
2743 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002744 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002745 mutex_unlock(&module_mutex);
2746}
2747#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002748
2749#ifdef CONFIG_TRACEPOINTS
2750void module_update_tracepoints(void)
2751{
2752 struct module *mod;
2753
2754 mutex_lock(&module_mutex);
2755 list_for_each_entry(mod, &modules, list)
2756 if (!mod->taints)
2757 tracepoint_update_probe_range(mod->tracepoints,
2758 mod->tracepoints + mod->num_tracepoints);
2759 mutex_unlock(&module_mutex);
2760}
2761
2762/*
2763 * Returns 0 if current not found.
2764 * Returns 1 if current found.
2765 */
2766int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2767{
2768 struct module *iter_mod;
2769 int found = 0;
2770
2771 mutex_lock(&module_mutex);
2772 list_for_each_entry(iter_mod, &modules, list) {
2773 if (!iter_mod->taints) {
2774 /*
2775 * Sorted module list
2776 */
2777 if (iter_mod < iter->module)
2778 continue;
2779 else if (iter_mod > iter->module)
2780 iter->tracepoint = NULL;
2781 found = tracepoint_get_iter_range(&iter->tracepoint,
2782 iter_mod->tracepoints,
2783 iter_mod->tracepoints
2784 + iter_mod->num_tracepoints);
2785 if (found) {
2786 iter->module = iter_mod;
2787 break;
2788 }
2789 }
2790 }
2791 mutex_unlock(&module_mutex);
2792 return found;
2793}
2794#endif