module: split unset_section_ro_nx function.
[linux-2.6-block.git] / kernel / module.c
CommitLineData
f71d20e9 1/*
1da177e4 2 Copyright (C) 2002 Richard Henderson
51f3d0f4 3 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
1da177e4
LT
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*/
1da177e4
LT
19#include <linux/module.h>
20#include <linux/moduleloader.h>
6d723736 21#include <linux/ftrace_event.h>
1da177e4 22#include <linux/init.h>
ae84e324 23#include <linux/kallsyms.h>
3b5d5c6b 24#include <linux/fs.h>
6d760133 25#include <linux/sysfs.h>
9f158333 26#include <linux/kernel.h>
1da177e4
LT
27#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
3b5d5c6b 30#include <linux/proc_fs.h>
1da177e4
LT
31#include <linux/seq_file.h>
32#include <linux/syscalls.h>
33#include <linux/fcntl.h>
34#include <linux/rcupdate.h>
c59ede7b 35#include <linux/capability.h>
1da177e4
LT
36#include <linux/cpu.h>
37#include <linux/moduleparam.h>
38#include <linux/errno.h>
39#include <linux/err.h>
40#include <linux/vermagic.h>
41#include <linux/notifier.h>
f6a57033 42#include <linux/sched.h>
1da177e4
LT
43#include <linux/stop_machine.h>
44#include <linux/device.h>
c988d2b2 45#include <linux/string.h>
97d1f15b 46#include <linux/mutex.h>
d72b3751 47#include <linux/rculist.h>
1da177e4 48#include <asm/uaccess.h>
1da177e4 49#include <asm/cacheflush.h>
eb8cdec4 50#include <asm/mmu_context.h>
b817f6fe 51#include <linux/license.h>
6d762394 52#include <asm/sections.h>
97e1c18e 53#include <linux/tracepoint.h>
90d595fe 54#include <linux/ftrace.h>
22a9d645 55#include <linux/async.h>
fbf59bc9 56#include <linux/percpu.h>
4f2294b6 57#include <linux/kmemleak.h>
bf5438fc 58#include <linux/jump_label.h>
84e1c6bb 59#include <linux/pfn.h>
1da177e4 60
7ead8b83
LZ
61#define CREATE_TRACE_POINTS
62#include <trace/events/module.h>
63
1da177e4
LT
64#if 0
65#define DEBUGP printk
66#else
67#define DEBUGP(fmt , a...)
68#endif
69
70#ifndef ARCH_SHF_SMALL
71#define ARCH_SHF_SMALL 0
72#endif
73
84e1c6bb 74/*
75 * Modules' sections will be aligned on page boundaries
76 * to ensure complete separation of code and data, but
77 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
78 */
79#ifdef CONFIG_DEBUG_SET_MODULE_RONX
80# define debug_align(X) ALIGN(X, PAGE_SIZE)
81#else
82# define debug_align(X) (X)
83#endif
84
85/*
86 * Given BASE and SIZE this macro calculates the number of pages the
87 * memory regions occupies
88 */
89#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
90 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
91 PFN_DOWN((unsigned long)BASE) + 1) \
92 : (0UL))
93
1da177e4
LT
94/* If this is set, the section belongs in the init part of the module */
95#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
96
75676500
RR
97/*
98 * Mutex protects:
99 * 1) List of modules (also safely readable with preempt_disable),
100 * 2) module_use links,
101 * 3) module_addr_min/module_addr_max.
d72b3751 102 * (delete uses stop_machine/add uses RCU list operations). */
c6b37801
TA
103DEFINE_MUTEX(module_mutex);
104EXPORT_SYMBOL_GPL(module_mutex);
1da177e4 105static LIST_HEAD(modules);
67fc4e0c
JW
106#ifdef CONFIG_KGDB_KDB
107struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
108#endif /* CONFIG_KGDB_KDB */
109
1da177e4 110
19e4529e
SR
111/* Block module loading/unloading? */
112int modules_disabled = 0;
113
c9a3ba55
RR
114/* Waiting for a module to finish initializing? */
115static DECLARE_WAIT_QUEUE_HEAD(module_wq);
116
e041c683 117static BLOCKING_NOTIFIER_HEAD(module_notify_list);
1da177e4 118
75676500
RR
119/* Bounds of module allocation, for speeding __module_address.
120 * Protected by module_mutex. */
3a642e99
RR
121static unsigned long module_addr_min = -1UL, module_addr_max = 0;
122
1da177e4
LT
123int register_module_notifier(struct notifier_block * nb)
124{
e041c683 125 return blocking_notifier_chain_register(&module_notify_list, nb);
1da177e4
LT
126}
127EXPORT_SYMBOL(register_module_notifier);
128
129int unregister_module_notifier(struct notifier_block * nb)
130{
e041c683 131 return blocking_notifier_chain_unregister(&module_notify_list, nb);
1da177e4
LT
132}
133EXPORT_SYMBOL(unregister_module_notifier);
134
eded41c1
RR
135struct load_info {
136 Elf_Ehdr *hdr;
137 unsigned long len;
138 Elf_Shdr *sechdrs;
6526c534 139 char *secstrings, *strtab;
d913188c
RR
140 unsigned long *strmap;
141 unsigned long symoffs, stroffs;
811d66a0
RR
142 struct _ddebug *debug;
143 unsigned int num_debug;
eded41c1
RR
144 struct {
145 unsigned int sym, str, mod, vers, info, pcpu;
146 } index;
147};
148
9a4b9708
ML
149/* We require a truly strong try_module_get(): 0 means failure due to
150 ongoing or failed initialization etc. */
1da177e4
LT
151static inline int strong_try_module_get(struct module *mod)
152{
153 if (mod && mod->state == MODULE_STATE_COMING)
c9a3ba55
RR
154 return -EBUSY;
155 if (try_module_get(mod))
1da177e4 156 return 0;
c9a3ba55
RR
157 else
158 return -ENOENT;
1da177e4
LT
159}
160
fa3ba2e8
FM
161static inline void add_taint_module(struct module *mod, unsigned flag)
162{
163 add_taint(flag);
25ddbb18 164 mod->taints |= (1U << flag);
fa3ba2e8
FM
165}
166
02a3e59a
RD
167/*
168 * A thread that wants to hold a reference to a module only while it
169 * is running can call this to safely exit. nfsd and lockd use this.
1da177e4
LT
170 */
171void __module_put_and_exit(struct module *mod, long code)
172{
173 module_put(mod);
174 do_exit(code);
175}
176EXPORT_SYMBOL(__module_put_and_exit);
22a8bdeb 177
1da177e4 178/* Find a module section: 0 means not found. */
49668688 179static unsigned int find_sec(const struct load_info *info, const char *name)
1da177e4
LT
180{
181 unsigned int i;
182
49668688
RR
183 for (i = 1; i < info->hdr->e_shnum; i++) {
184 Elf_Shdr *shdr = &info->sechdrs[i];
1da177e4 185 /* Alloc bit cleared means "ignore it." */
49668688
RR
186 if ((shdr->sh_flags & SHF_ALLOC)
187 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
1da177e4 188 return i;
49668688 189 }
1da177e4
LT
190 return 0;
191}
192
5e458cc0 193/* Find a module section, or NULL. */
49668688 194static void *section_addr(const struct load_info *info, const char *name)
5e458cc0
RR
195{
196 /* Section 0 has sh_addr 0. */
49668688 197 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
5e458cc0
RR
198}
199
200/* Find a module section, or NULL. Fill in number of "objects" in section. */
49668688 201static void *section_objs(const struct load_info *info,
5e458cc0
RR
202 const char *name,
203 size_t object_size,
204 unsigned int *num)
205{
49668688 206 unsigned int sec = find_sec(info, name);
5e458cc0
RR
207
208 /* Section 0 has sh_addr 0 and sh_size 0. */
49668688
RR
209 *num = info->sechdrs[sec].sh_size / object_size;
210 return (void *)info->sechdrs[sec].sh_addr;
5e458cc0
RR
211}
212
1da177e4
LT
213/* Provided by the linker */
214extern const struct kernel_symbol __start___ksymtab[];
215extern const struct kernel_symbol __stop___ksymtab[];
216extern const struct kernel_symbol __start___ksymtab_gpl[];
217extern const struct kernel_symbol __stop___ksymtab_gpl[];
9f28bb7e
GKH
218extern const struct kernel_symbol __start___ksymtab_gpl_future[];
219extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
1da177e4
LT
220extern const unsigned long __start___kcrctab[];
221extern const unsigned long __start___kcrctab_gpl[];
9f28bb7e 222extern const unsigned long __start___kcrctab_gpl_future[];
f7f5b675
DV
223#ifdef CONFIG_UNUSED_SYMBOLS
224extern const struct kernel_symbol __start___ksymtab_unused[];
225extern const struct kernel_symbol __stop___ksymtab_unused[];
226extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
227extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
f71d20e9
AV
228extern const unsigned long __start___kcrctab_unused[];
229extern const unsigned long __start___kcrctab_unused_gpl[];
f7f5b675 230#endif
1da177e4
LT
231
232#ifndef CONFIG_MODVERSIONS
233#define symversion(base, idx) NULL
234#else
f83ca9fe 235#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
1da177e4
LT
236#endif
237
dafd0940
RR
238static bool each_symbol_in_section(const struct symsearch *arr,
239 unsigned int arrsize,
240 struct module *owner,
241 bool (*fn)(const struct symsearch *syms,
242 struct module *owner,
243 unsigned int symnum, void *data),
244 void *data)
ad9546c9 245{
dafd0940 246 unsigned int i, j;
ad9546c9 247
dafd0940
RR
248 for (j = 0; j < arrsize; j++) {
249 for (i = 0; i < arr[j].stop - arr[j].start; i++)
250 if (fn(&arr[j], owner, i, data))
251 return true;
f71d20e9 252 }
dafd0940
RR
253
254 return false;
ad9546c9
RR
255}
256
dafd0940 257/* Returns true as soon as fn returns true, otherwise false. */
c6b37801
TA
258bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
259 unsigned int symnum, void *data), void *data)
ad9546c9
RR
260{
261 struct module *mod;
44032e63 262 static const struct symsearch arr[] = {
ad9546c9 263 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
dafd0940 264 NOT_GPL_ONLY, false },
ad9546c9 265 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
dafd0940
RR
266 __start___kcrctab_gpl,
267 GPL_ONLY, false },
ad9546c9 268 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
dafd0940
RR
269 __start___kcrctab_gpl_future,
270 WILL_BE_GPL_ONLY, false },
f7f5b675 271#ifdef CONFIG_UNUSED_SYMBOLS
ad9546c9 272 { __start___ksymtab_unused, __stop___ksymtab_unused,
dafd0940
RR
273 __start___kcrctab_unused,
274 NOT_GPL_ONLY, true },
ad9546c9 275 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
dafd0940
RR
276 __start___kcrctab_unused_gpl,
277 GPL_ONLY, true },
f7f5b675 278#endif
ad9546c9 279 };
f71d20e9 280
dafd0940
RR
281 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
282 return true;
f71d20e9 283
d72b3751 284 list_for_each_entry_rcu(mod, &modules, list) {
ad9546c9
RR
285 struct symsearch arr[] = {
286 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
dafd0940 287 NOT_GPL_ONLY, false },
ad9546c9 288 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
dafd0940
RR
289 mod->gpl_crcs,
290 GPL_ONLY, false },
ad9546c9
RR
291 { mod->gpl_future_syms,
292 mod->gpl_future_syms + mod->num_gpl_future_syms,
dafd0940
RR
293 mod->gpl_future_crcs,
294 WILL_BE_GPL_ONLY, false },
f7f5b675 295#ifdef CONFIG_UNUSED_SYMBOLS
ad9546c9
RR
296 { mod->unused_syms,
297 mod->unused_syms + mod->num_unused_syms,
dafd0940
RR
298 mod->unused_crcs,
299 NOT_GPL_ONLY, true },
ad9546c9
RR
300 { mod->unused_gpl_syms,
301 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
dafd0940
RR
302 mod->unused_gpl_crcs,
303 GPL_ONLY, true },
f7f5b675 304#endif
ad9546c9
RR
305 };
306
dafd0940
RR
307 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
308 return true;
309 }
310 return false;
311}
c6b37801 312EXPORT_SYMBOL_GPL(each_symbol);
dafd0940
RR
313
314struct find_symbol_arg {
315 /* Input */
316 const char *name;
317 bool gplok;
318 bool warn;
319
320 /* Output */
321 struct module *owner;
322 const unsigned long *crc;
414fd31b 323 const struct kernel_symbol *sym;
dafd0940
RR
324};
325
326static bool find_symbol_in_section(const struct symsearch *syms,
327 struct module *owner,
328 unsigned int symnum, void *data)
329{
330 struct find_symbol_arg *fsa = data;
331
332 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
333 return false;
334
335 if (!fsa->gplok) {
336 if (syms->licence == GPL_ONLY)
337 return false;
338 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
339 printk(KERN_WARNING "Symbol %s is being used "
340 "by a non-GPL module, which will not "
341 "be allowed in the future\n", fsa->name);
342 printk(KERN_WARNING "Please see the file "
343 "Documentation/feature-removal-schedule.txt "
344 "in the kernel source tree for more details.\n");
9f28bb7e 345 }
1da177e4 346 }
ad9546c9 347
f7f5b675 348#ifdef CONFIG_UNUSED_SYMBOLS
dafd0940
RR
349 if (syms->unused && fsa->warn) {
350 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
351 "however this module is using it.\n", fsa->name);
352 printk(KERN_WARNING
353 "This symbol will go away in the future.\n");
354 printk(KERN_WARNING
355 "Please evalute if this is the right api to use and if "
356 "it really is, submit a report the linux kernel "
357 "mailinglist together with submitting your code for "
358 "inclusion.\n");
359 }
f7f5b675 360#endif
dafd0940
RR
361
362 fsa->owner = owner;
363 fsa->crc = symversion(syms->crcs, symnum);
414fd31b 364 fsa->sym = &syms->start[symnum];
dafd0940
RR
365 return true;
366}
367
414fd31b 368/* Find a symbol and return it, along with, (optional) crc and
75676500 369 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
c6b37801
TA
370const struct kernel_symbol *find_symbol(const char *name,
371 struct module **owner,
372 const unsigned long **crc,
373 bool gplok,
374 bool warn)
dafd0940
RR
375{
376 struct find_symbol_arg fsa;
377
378 fsa.name = name;
379 fsa.gplok = gplok;
380 fsa.warn = warn;
381
382 if (each_symbol(find_symbol_in_section, &fsa)) {
383 if (owner)
384 *owner = fsa.owner;
385 if (crc)
386 *crc = fsa.crc;
414fd31b 387 return fsa.sym;
dafd0940
RR
388 }
389
1da177e4 390 DEBUGP("Failed to find symbol %s\n", name);
414fd31b 391 return NULL;
1da177e4 392}
c6b37801 393EXPORT_SYMBOL_GPL(find_symbol);
1da177e4 394
1da177e4 395/* Search for module by name: must hold module_mutex. */
c6b37801 396struct module *find_module(const char *name)
1da177e4
LT
397{
398 struct module *mod;
399
400 list_for_each_entry(mod, &modules, list) {
401 if (strcmp(mod->name, name) == 0)
402 return mod;
403 }
404 return NULL;
405}
c6b37801 406EXPORT_SYMBOL_GPL(find_module);
1da177e4
LT
407
408#ifdef CONFIG_SMP
fbf59bc9 409
259354de 410static inline void __percpu *mod_percpu(struct module *mod)
fbf59bc9 411{
259354de
TH
412 return mod->percpu;
413}
fbf59bc9 414
259354de
TH
415static int percpu_modalloc(struct module *mod,
416 unsigned long size, unsigned long align)
417{
fbf59bc9
TH
418 if (align > PAGE_SIZE) {
419 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
259354de 420 mod->name, align, PAGE_SIZE);
fbf59bc9
TH
421 align = PAGE_SIZE;
422 }
423
259354de
TH
424 mod->percpu = __alloc_reserved_percpu(size, align);
425 if (!mod->percpu) {
fbf59bc9 426 printk(KERN_WARNING
d913188c
RR
427 "%s: Could not allocate %lu bytes percpu data\n",
428 mod->name, size);
259354de
TH
429 return -ENOMEM;
430 }
431 mod->percpu_size = size;
432 return 0;
fbf59bc9
TH
433}
434
259354de 435static void percpu_modfree(struct module *mod)
fbf59bc9 436{
259354de 437 free_percpu(mod->percpu);
fbf59bc9
TH
438}
439
49668688 440static unsigned int find_pcpusec(struct load_info *info)
6b588c18 441{
49668688 442 return find_sec(info, ".data..percpu");
6b588c18
TH
443}
444
259354de
TH
445static void percpu_modcopy(struct module *mod,
446 const void *from, unsigned long size)
6b588c18
TH
447{
448 int cpu;
449
450 for_each_possible_cpu(cpu)
259354de 451 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
6b588c18
TH
452}
453
10fad5e4
TH
454/**
455 * is_module_percpu_address - test whether address is from module static percpu
456 * @addr: address to test
457 *
458 * Test whether @addr belongs to module static percpu area.
459 *
460 * RETURNS:
461 * %true if @addr is from module static percpu area
462 */
463bool is_module_percpu_address(unsigned long addr)
464{
465 struct module *mod;
466 unsigned int cpu;
467
468 preempt_disable();
469
470 list_for_each_entry_rcu(mod, &modules, list) {
471 if (!mod->percpu_size)
472 continue;
473 for_each_possible_cpu(cpu) {
474 void *start = per_cpu_ptr(mod->percpu, cpu);
475
476 if ((void *)addr >= start &&
477 (void *)addr < start + mod->percpu_size) {
478 preempt_enable();
479 return true;
480 }
481 }
482 }
483
484 preempt_enable();
485 return false;
6b588c18
TH
486}
487
1da177e4 488#else /* ... !CONFIG_SMP */
6b588c18 489
259354de 490static inline void __percpu *mod_percpu(struct module *mod)
1da177e4
LT
491{
492 return NULL;
493}
259354de
TH
494static inline int percpu_modalloc(struct module *mod,
495 unsigned long size, unsigned long align)
496{
497 return -ENOMEM;
498}
499static inline void percpu_modfree(struct module *mod)
1da177e4 500{
1da177e4 501}
49668688 502static unsigned int find_pcpusec(struct load_info *info)
1da177e4
LT
503{
504 return 0;
505}
259354de
TH
506static inline void percpu_modcopy(struct module *mod,
507 const void *from, unsigned long size)
1da177e4
LT
508{
509 /* pcpusec should be 0, and size of that section should be 0. */
510 BUG_ON(size != 0);
511}
10fad5e4
TH
512bool is_module_percpu_address(unsigned long addr)
513{
514 return false;
515}
6b588c18 516
1da177e4
LT
517#endif /* CONFIG_SMP */
518
c988d2b2
MD
519#define MODINFO_ATTR(field) \
520static void setup_modinfo_##field(struct module *mod, const char *s) \
521{ \
522 mod->field = kstrdup(s, GFP_KERNEL); \
523} \
524static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
525 struct module *mod, char *buffer) \
526{ \
527 return sprintf(buffer, "%s\n", mod->field); \
528} \
529static int modinfo_##field##_exists(struct module *mod) \
530{ \
531 return mod->field != NULL; \
532} \
533static void free_modinfo_##field(struct module *mod) \
534{ \
22a8bdeb
DW
535 kfree(mod->field); \
536 mod->field = NULL; \
c988d2b2
MD
537} \
538static struct module_attribute modinfo_##field = { \
7b595756 539 .attr = { .name = __stringify(field), .mode = 0444 }, \
c988d2b2
MD
540 .show = show_modinfo_##field, \
541 .setup = setup_modinfo_##field, \
542 .test = modinfo_##field##_exists, \
543 .free = free_modinfo_##field, \
544};
545
546MODINFO_ATTR(version);
547MODINFO_ATTR(srcversion);
548
e14af7ee
AV
549static char last_unloaded_module[MODULE_NAME_LEN+1];
550
03e88ae1 551#ifdef CONFIG_MODULE_UNLOAD
eb0c5377
SR
552
553EXPORT_TRACEPOINT_SYMBOL(module_get);
554
1da177e4 555/* Init the unload section of the module. */
9f85a4bb 556static int module_unload_init(struct module *mod)
1da177e4 557{
9f85a4bb
RR
558 mod->refptr = alloc_percpu(struct module_ref);
559 if (!mod->refptr)
560 return -ENOMEM;
561
2c02dfe7
LT
562 INIT_LIST_HEAD(&mod->source_list);
563 INIT_LIST_HEAD(&mod->target_list);
e1783a24 564
1da177e4 565 /* Hold reference count during initialization. */
5fbfb18d 566 __this_cpu_write(mod->refptr->incs, 1);
1da177e4
LT
567 /* Backwards compatibility macros put refcount during init. */
568 mod->waiter = current;
9f85a4bb
RR
569
570 return 0;
1da177e4
LT
571}
572
1da177e4
LT
573/* Does a already use b? */
574static int already_uses(struct module *a, struct module *b)
575{
576 struct module_use *use;
577
2c02dfe7
LT
578 list_for_each_entry(use, &b->source_list, source_list) {
579 if (use->source == a) {
1da177e4
LT
580 DEBUGP("%s uses %s!\n", a->name, b->name);
581 return 1;
582 }
583 }
584 DEBUGP("%s does not use %s!\n", a->name, b->name);
585 return 0;
586}
587
2c02dfe7
LT
588/*
589 * Module a uses b
590 * - we add 'a' as a "source", 'b' as a "target" of module use
591 * - the module_use is added to the list of 'b' sources (so
592 * 'b' can walk the list to see who sourced them), and of 'a'
593 * targets (so 'a' can see what modules it targets).
594 */
595static int add_module_usage(struct module *a, struct module *b)
596{
2c02dfe7
LT
597 struct module_use *use;
598
599 DEBUGP("Allocating new usage for %s.\n", a->name);
600 use = kmalloc(sizeof(*use), GFP_ATOMIC);
601 if (!use) {
602 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
603 return -ENOMEM;
604 }
605
606 use->source = a;
607 use->target = b;
608 list_add(&use->source_list, &b->source_list);
609 list_add(&use->target_list, &a->target_list);
2c02dfe7
LT
610 return 0;
611}
612
75676500 613/* Module a uses b: caller needs module_mutex() */
9bea7f23 614int ref_module(struct module *a, struct module *b)
1da177e4 615{
c8e21ced 616 int err;
270a6c4c 617
9bea7f23 618 if (b == NULL || already_uses(a, b))
218ce735 619 return 0;
218ce735 620
9bea7f23
RR
621 /* If module isn't available, we fail. */
622 err = strong_try_module_get(b);
c9a3ba55 623 if (err)
9bea7f23 624 return err;
1da177e4 625
2c02dfe7
LT
626 err = add_module_usage(a, b);
627 if (err) {
1da177e4 628 module_put(b);
9bea7f23 629 return err;
1da177e4 630 }
9bea7f23 631 return 0;
1da177e4 632}
9bea7f23 633EXPORT_SYMBOL_GPL(ref_module);
1da177e4
LT
634
635/* Clear the unload stuff of the module. */
636static void module_unload_free(struct module *mod)
637{
2c02dfe7 638 struct module_use *use, *tmp;
1da177e4 639
75676500 640 mutex_lock(&module_mutex);
2c02dfe7
LT
641 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
642 struct module *i = use->target;
643 DEBUGP("%s unusing %s\n", mod->name, i->name);
644 module_put(i);
645 list_del(&use->source_list);
646 list_del(&use->target_list);
647 kfree(use);
1da177e4 648 }
75676500 649 mutex_unlock(&module_mutex);
9f85a4bb
RR
650
651 free_percpu(mod->refptr);
1da177e4
LT
652}
653
654#ifdef CONFIG_MODULE_FORCE_UNLOAD
fb169793 655static inline int try_force_unload(unsigned int flags)
1da177e4
LT
656{
657 int ret = (flags & O_TRUNC);
658 if (ret)
fb169793 659 add_taint(TAINT_FORCED_RMMOD);
1da177e4
LT
660 return ret;
661}
662#else
fb169793 663static inline int try_force_unload(unsigned int flags)
1da177e4
LT
664{
665 return 0;
666}
667#endif /* CONFIG_MODULE_FORCE_UNLOAD */
668
669struct stopref
670{
671 struct module *mod;
672 int flags;
673 int *forced;
674};
675
676/* Whole machine is stopped with interrupts off when this runs. */
677static int __try_stop_module(void *_sref)
678{
679 struct stopref *sref = _sref;
680
da39ba5e
RR
681 /* If it's not unused, quit unless we're forcing. */
682 if (module_refcount(sref->mod) != 0) {
fb169793 683 if (!(*sref->forced = try_force_unload(sref->flags)))
1da177e4
LT
684 return -EWOULDBLOCK;
685 }
686
687 /* Mark it as dying. */
688 sref->mod->state = MODULE_STATE_GOING;
689 return 0;
690}
691
692static int try_stop_module(struct module *mod, int flags, int *forced)
693{
da39ba5e
RR
694 if (flags & O_NONBLOCK) {
695 struct stopref sref = { mod, flags, forced };
1da177e4 696
9b1a4d38 697 return stop_machine(__try_stop_module, &sref, NULL);
da39ba5e
RR
698 } else {
699 /* We don't need to stop the machine for this. */
700 mod->state = MODULE_STATE_GOING;
701 synchronize_sched();
702 return 0;
703 }
1da177e4
LT
704}
705
706unsigned int module_refcount(struct module *mod)
707{
5fbfb18d 708 unsigned int incs = 0, decs = 0;
720eba31 709 int cpu;
1da177e4 710
720eba31 711 for_each_possible_cpu(cpu)
5fbfb18d
NP
712 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
713 /*
714 * ensure the incs are added up after the decs.
715 * module_put ensures incs are visible before decs with smp_wmb.
716 *
717 * This 2-count scheme avoids the situation where the refcount
718 * for CPU0 is read, then CPU0 increments the module refcount,
719 * then CPU1 drops that refcount, then the refcount for CPU1 is
720 * read. We would record a decrement but not its corresponding
721 * increment so we would see a low count (disaster).
722 *
723 * Rare situation? But module_refcount can be preempted, and we
724 * might be tallying up 4096+ CPUs. So it is not impossible.
725 */
726 smp_rmb();
727 for_each_possible_cpu(cpu)
728 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
729 return incs - decs;
1da177e4
LT
730}
731EXPORT_SYMBOL(module_refcount);
732
733/* This exists whether we can unload or not */
734static void free_module(struct module *mod);
735
736static void wait_for_zero_refcount(struct module *mod)
737{
a6550207 738 /* Since we might sleep for some time, release the mutex first */
6389a385 739 mutex_unlock(&module_mutex);
1da177e4
LT
740 for (;;) {
741 DEBUGP("Looking at refcount...\n");
742 set_current_state(TASK_UNINTERRUPTIBLE);
743 if (module_refcount(mod) == 0)
744 break;
745 schedule();
746 }
747 current->state = TASK_RUNNING;
6389a385 748 mutex_lock(&module_mutex);
1da177e4
LT
749}
750
17da2bd9
HC
751SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
752 unsigned int, flags)
1da177e4
LT
753{
754 struct module *mod;
dfff0a06 755 char name[MODULE_NAME_LEN];
1da177e4
LT
756 int ret, forced = 0;
757
3d43321b 758 if (!capable(CAP_SYS_MODULE) || modules_disabled)
dfff0a06
GKH
759 return -EPERM;
760
761 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
762 return -EFAULT;
763 name[MODULE_NAME_LEN-1] = '\0';
764
3fc1f1e2
TH
765 if (mutex_lock_interruptible(&module_mutex) != 0)
766 return -EINTR;
1da177e4
LT
767
768 mod = find_module(name);
769 if (!mod) {
770 ret = -ENOENT;
771 goto out;
772 }
773
2c02dfe7 774 if (!list_empty(&mod->source_list)) {
1da177e4
LT
775 /* Other modules depend on us: get rid of them first. */
776 ret = -EWOULDBLOCK;
777 goto out;
778 }
779
780 /* Doing init or already dying? */
781 if (mod->state != MODULE_STATE_LIVE) {
782 /* FIXME: if (force), slam module count and wake up
783 waiter --RR */
784 DEBUGP("%s already dying\n", mod->name);
785 ret = -EBUSY;
786 goto out;
787 }
788
789 /* If it has an init func, it must have an exit func to unload */
af49d924 790 if (mod->init && !mod->exit) {
fb169793 791 forced = try_force_unload(flags);
1da177e4
LT
792 if (!forced) {
793 /* This module can't be removed */
794 ret = -EBUSY;
795 goto out;
796 }
797 }
798
799 /* Set this up before setting mod->state */
800 mod->waiter = current;
801
802 /* Stop the machine so refcounts can't move and disable module. */
803 ret = try_stop_module(mod, flags, &forced);
804 if (ret != 0)
805 goto out;
806
807 /* Never wait if forced. */
808 if (!forced && module_refcount(mod) != 0)
809 wait_for_zero_refcount(mod);
810
df4b565e 811 mutex_unlock(&module_mutex);
25985edc 812 /* Final destruction now no one is using it. */
df4b565e 813 if (mod->exit != NULL)
1da177e4 814 mod->exit();
df4b565e
PO
815 blocking_notifier_call_chain(&module_notify_list,
816 MODULE_STATE_GOING, mod);
22a9d645 817 async_synchronize_full();
75676500 818
e14af7ee 819 /* Store the name of the last unloaded module for diagnostic purposes */
efa5345e 820 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
1da177e4 821
75676500
RR
822 free_module(mod);
823 return 0;
824out:
6389a385 825 mutex_unlock(&module_mutex);
1da177e4
LT
826 return ret;
827}
828
d1e99d7a 829static inline void print_unload_info(struct seq_file *m, struct module *mod)
1da177e4
LT
830{
831 struct module_use *use;
832 int printed_something = 0;
833
834 seq_printf(m, " %u ", module_refcount(mod));
835
836 /* Always include a trailing , so userspace can differentiate
837 between this and the old multi-field proc format. */
2c02dfe7 838 list_for_each_entry(use, &mod->source_list, source_list) {
1da177e4 839 printed_something = 1;
2c02dfe7 840 seq_printf(m, "%s,", use->source->name);
1da177e4
LT
841 }
842
1da177e4
LT
843 if (mod->init != NULL && mod->exit == NULL) {
844 printed_something = 1;
845 seq_printf(m, "[permanent],");
846 }
847
848 if (!printed_something)
849 seq_printf(m, "-");
850}
851
852void __symbol_put(const char *symbol)
853{
854 struct module *owner;
1da177e4 855
24da1cbf 856 preempt_disable();
414fd31b 857 if (!find_symbol(symbol, &owner, NULL, true, false))
1da177e4
LT
858 BUG();
859 module_put(owner);
24da1cbf 860 preempt_enable();
1da177e4
LT
861}
862EXPORT_SYMBOL(__symbol_put);
863
7d1d16e4 864/* Note this assumes addr is a function, which it currently always is. */
1da177e4
LT
865void symbol_put_addr(void *addr)
866{
5e376613 867 struct module *modaddr;
7d1d16e4 868 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
1da177e4 869
7d1d16e4 870 if (core_kernel_text(a))
5e376613 871 return;
1da177e4 872
a6e6abd5
RR
873 /* module_text_address is safe here: we're supposed to have reference
874 * to module from symbol_get, so it can't go away. */
7d1d16e4 875 modaddr = __module_text_address(a);
a6e6abd5 876 BUG_ON(!modaddr);
5e376613 877 module_put(modaddr);
1da177e4
LT
878}
879EXPORT_SYMBOL_GPL(symbol_put_addr);
880
881static ssize_t show_refcnt(struct module_attribute *mattr,
882 struct module *mod, char *buffer)
883{
256e2fdf 884 return sprintf(buffer, "%u\n", module_refcount(mod));
1da177e4
LT
885}
886
887static struct module_attribute refcnt = {
7b595756 888 .attr = { .name = "refcnt", .mode = 0444 },
1da177e4
LT
889 .show = show_refcnt,
890};
891
f6a57033
AV
892void module_put(struct module *module)
893{
894 if (module) {
e1783a24 895 preempt_disable();
5fbfb18d
NP
896 smp_wmb(); /* see comment in module_refcount */
897 __this_cpu_inc(module->refptr->decs);
e1783a24 898
ae832d1e 899 trace_module_put(module, _RET_IP_);
f6a57033
AV
900 /* Maybe they're waiting for us to drop reference? */
901 if (unlikely(!module_is_live(module)))
902 wake_up_process(module->waiter);
e1783a24 903 preempt_enable();
f6a57033
AV
904 }
905}
906EXPORT_SYMBOL(module_put);
907
1da177e4 908#else /* !CONFIG_MODULE_UNLOAD */
d1e99d7a 909static inline void print_unload_info(struct seq_file *m, struct module *mod)
1da177e4
LT
910{
911 /* We don't know the usage count, or what modules are using. */
912 seq_printf(m, " - -");
913}
914
915static inline void module_unload_free(struct module *mod)
916{
917}
918
9bea7f23 919int ref_module(struct module *a, struct module *b)
1da177e4 920{
9bea7f23 921 return strong_try_module_get(b);
1da177e4 922}
9bea7f23 923EXPORT_SYMBOL_GPL(ref_module);
1da177e4 924
9f85a4bb 925static inline int module_unload_init(struct module *mod)
1da177e4 926{
9f85a4bb 927 return 0;
1da177e4
LT
928}
929#endif /* CONFIG_MODULE_UNLOAD */
930
1f71740a
KS
931static ssize_t show_initstate(struct module_attribute *mattr,
932 struct module *mod, char *buffer)
933{
934 const char *state = "unknown";
935
936 switch (mod->state) {
937 case MODULE_STATE_LIVE:
938 state = "live";
939 break;
940 case MODULE_STATE_COMING:
941 state = "coming";
942 break;
943 case MODULE_STATE_GOING:
944 state = "going";
945 break;
946 }
947 return sprintf(buffer, "%s\n", state);
948}
949
950static struct module_attribute initstate = {
7b595756 951 .attr = { .name = "initstate", .mode = 0444 },
1f71740a
KS
952 .show = show_initstate,
953};
954
03e88ae1
GKH
955static struct module_attribute *modinfo_attrs[] = {
956 &modinfo_version,
957 &modinfo_srcversion,
1f71740a 958 &initstate,
03e88ae1
GKH
959#ifdef CONFIG_MODULE_UNLOAD
960 &refcnt,
961#endif
962 NULL,
963};
964
1da177e4
LT
965static const char vermagic[] = VERMAGIC_STRING;
966
c6e665c8 967static int try_to_force_load(struct module *mod, const char *reason)
826e4506
LT
968{
969#ifdef CONFIG_MODULE_FORCE_LOAD
25ddbb18 970 if (!test_taint(TAINT_FORCED_MODULE))
c6e665c8
RR
971 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
972 mod->name, reason);
826e4506
LT
973 add_taint_module(mod, TAINT_FORCED_MODULE);
974 return 0;
975#else
976 return -ENOEXEC;
977#endif
978}
979
1da177e4 980#ifdef CONFIG_MODVERSIONS
d4703aef
RR
981/* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
982static unsigned long maybe_relocated(unsigned long crc,
983 const struct module *crc_owner)
984{
985#ifdef ARCH_RELOCATES_KCRCTAB
986 if (crc_owner == NULL)
987 return crc - (unsigned long)reloc_start;
988#endif
989 return crc;
990}
991
1da177e4
LT
992static int check_version(Elf_Shdr *sechdrs,
993 unsigned int versindex,
994 const char *symname,
995 struct module *mod,
d4703aef
RR
996 const unsigned long *crc,
997 const struct module *crc_owner)
1da177e4
LT
998{
999 unsigned int i, num_versions;
1000 struct modversion_info *versions;
1001
1002 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1003 if (!crc)
1004 return 1;
1005
a5dd6970
RR
1006 /* No versions at all? modprobe --force does this. */
1007 if (versindex == 0)
1008 return try_to_force_load(mod, symname) == 0;
1009
1da177e4
LT
1010 versions = (void *) sechdrs[versindex].sh_addr;
1011 num_versions = sechdrs[versindex].sh_size
1012 / sizeof(struct modversion_info);
1013
1014 for (i = 0; i < num_versions; i++) {
1015 if (strcmp(versions[i].name, symname) != 0)
1016 continue;
1017
d4703aef 1018 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
1da177e4 1019 return 1;
1da177e4 1020 DEBUGP("Found checksum %lX vs module %lX\n",
d4703aef 1021 maybe_relocated(*crc, crc_owner), versions[i].crc);
826e4506 1022 goto bad_version;
1da177e4 1023 }
826e4506 1024
a5dd6970
RR
1025 printk(KERN_WARNING "%s: no symbol version for %s\n",
1026 mod->name, symname);
1027 return 0;
826e4506
LT
1028
1029bad_version:
1030 printk("%s: disagrees about version of symbol %s\n",
1031 mod->name, symname);
1032 return 0;
1da177e4
LT
1033}
1034
1035static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1036 unsigned int versindex,
1037 struct module *mod)
1038{
1039 const unsigned long *crc;
1da177e4 1040
75676500
RR
1041 /* Since this should be found in kernel (which can't be removed),
1042 * no locking is necessary. */
6560dc16
MF
1043 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1044 &crc, true, false))
1da177e4 1045 BUG();
d4703aef
RR
1046 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1047 NULL);
1da177e4
LT
1048}
1049
91e37a79
RR
1050/* First part is kernel version, which we ignore if module has crcs. */
1051static inline int same_magic(const char *amagic, const char *bmagic,
1052 bool has_crcs)
1da177e4 1053{
91e37a79
RR
1054 if (has_crcs) {
1055 amagic += strcspn(amagic, " ");
1056 bmagic += strcspn(bmagic, " ");
1057 }
1da177e4
LT
1058 return strcmp(amagic, bmagic) == 0;
1059}
1060#else
1061static inline int check_version(Elf_Shdr *sechdrs,
1062 unsigned int versindex,
1063 const char *symname,
1064 struct module *mod,
d4703aef
RR
1065 const unsigned long *crc,
1066 const struct module *crc_owner)
1da177e4
LT
1067{
1068 return 1;
1069}
1070
1071static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1072 unsigned int versindex,
1073 struct module *mod)
1074{
1075 return 1;
1076}
1077
91e37a79
RR
1078static inline int same_magic(const char *amagic, const char *bmagic,
1079 bool has_crcs)
1da177e4
LT
1080{
1081 return strcmp(amagic, bmagic) == 0;
1082}
1083#endif /* CONFIG_MODVERSIONS */
1084
75676500 1085/* Resolve a symbol for this module. I.e. if we find one, record usage. */
49668688
RR
1086static const struct kernel_symbol *resolve_symbol(struct module *mod,
1087 const struct load_info *info,
414fd31b 1088 const char *name,
9bea7f23 1089 char ownername[])
1da177e4
LT
1090{
1091 struct module *owner;
414fd31b 1092 const struct kernel_symbol *sym;
1da177e4 1093 const unsigned long *crc;
9bea7f23 1094 int err;
1da177e4 1095
75676500 1096 mutex_lock(&module_mutex);
414fd31b 1097 sym = find_symbol(name, &owner, &crc,
25ddbb18 1098 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
9bea7f23
RR
1099 if (!sym)
1100 goto unlock;
1101
49668688
RR
1102 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1103 owner)) {
9bea7f23
RR
1104 sym = ERR_PTR(-EINVAL);
1105 goto getname;
1da177e4 1106 }
9bea7f23
RR
1107
1108 err = ref_module(mod, owner);
1109 if (err) {
1110 sym = ERR_PTR(err);
1111 goto getname;
1112 }
1113
1114getname:
1115 /* We must make copy under the lock if we failed to get ref. */
1116 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1117unlock:
75676500 1118 mutex_unlock(&module_mutex);
218ce735 1119 return sym;
1da177e4
LT
1120}
1121
49668688
RR
1122static const struct kernel_symbol *
1123resolve_symbol_wait(struct module *mod,
1124 const struct load_info *info,
1125 const char *name)
9bea7f23
RR
1126{
1127 const struct kernel_symbol *ksym;
49668688 1128 char owner[MODULE_NAME_LEN];
9bea7f23
RR
1129
1130 if (wait_event_interruptible_timeout(module_wq,
49668688
RR
1131 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1132 || PTR_ERR(ksym) != -EBUSY,
9bea7f23
RR
1133 30 * HZ) <= 0) {
1134 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
49668688 1135 mod->name, owner);
9bea7f23
RR
1136 }
1137 return ksym;
1138}
1139
1da177e4
LT
1140/*
1141 * /sys/module/foo/sections stuff
1142 * J. Corbet <corbet@lwn.net>
1143 */
8f6d0378 1144#ifdef CONFIG_SYSFS
10b465aa 1145
8f6d0378 1146#ifdef CONFIG_KALLSYMS
10b465aa
BH
1147static inline bool sect_empty(const Elf_Shdr *sect)
1148{
1149 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1150}
1151
a58730c4
RR
1152struct module_sect_attr
1153{
1154 struct module_attribute mattr;
1155 char *name;
1156 unsigned long address;
1157};
1158
1159struct module_sect_attrs
1160{
1161 struct attribute_group grp;
1162 unsigned int nsections;
1163 struct module_sect_attr attrs[0];
1164};
1165
1da177e4
LT
1166static ssize_t module_sect_show(struct module_attribute *mattr,
1167 struct module *mod, char *buf)
1168{
1169 struct module_sect_attr *sattr =
1170 container_of(mattr, struct module_sect_attr, mattr);
9f36e2c4 1171 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
1da177e4
LT
1172}
1173
04b1db9f
IN
1174static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1175{
a58730c4 1176 unsigned int section;
04b1db9f
IN
1177
1178 for (section = 0; section < sect_attrs->nsections; section++)
1179 kfree(sect_attrs->attrs[section].name);
1180 kfree(sect_attrs);
1181}
1182
8f6d0378 1183static void add_sect_attrs(struct module *mod, const struct load_info *info)
1da177e4
LT
1184{
1185 unsigned int nloaded = 0, i, size[2];
1186 struct module_sect_attrs *sect_attrs;
1187 struct module_sect_attr *sattr;
1188 struct attribute **gattr;
22a8bdeb 1189
1da177e4 1190 /* Count loaded sections and allocate structures */
8f6d0378
RR
1191 for (i = 0; i < info->hdr->e_shnum; i++)
1192 if (!sect_empty(&info->sechdrs[i]))
1da177e4
LT
1193 nloaded++;
1194 size[0] = ALIGN(sizeof(*sect_attrs)
1195 + nloaded * sizeof(sect_attrs->attrs[0]),
1196 sizeof(sect_attrs->grp.attrs[0]));
1197 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
04b1db9f
IN
1198 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1199 if (sect_attrs == NULL)
1da177e4
LT
1200 return;
1201
1202 /* Setup section attributes. */
1203 sect_attrs->grp.name = "sections";
1204 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1205
04b1db9f 1206 sect_attrs->nsections = 0;
1da177e4
LT
1207 sattr = &sect_attrs->attrs[0];
1208 gattr = &sect_attrs->grp.attrs[0];
8f6d0378
RR
1209 for (i = 0; i < info->hdr->e_shnum; i++) {
1210 Elf_Shdr *sec = &info->sechdrs[i];
1211 if (sect_empty(sec))
35dead42 1212 continue;
8f6d0378
RR
1213 sattr->address = sec->sh_addr;
1214 sattr->name = kstrdup(info->secstrings + sec->sh_name,
04b1db9f
IN
1215 GFP_KERNEL);
1216 if (sattr->name == NULL)
1217 goto out;
1218 sect_attrs->nsections++;
361795b1 1219 sysfs_attr_init(&sattr->mattr.attr);
1da177e4
LT
1220 sattr->mattr.show = module_sect_show;
1221 sattr->mattr.store = NULL;
1222 sattr->mattr.attr.name = sattr->name;
1da177e4
LT
1223 sattr->mattr.attr.mode = S_IRUGO;
1224 *(gattr++) = &(sattr++)->mattr.attr;
1225 }
1226 *gattr = NULL;
1227
1228 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1229 goto out;
1230
1231 mod->sect_attrs = sect_attrs;
1232 return;
1233 out:
04b1db9f 1234 free_sect_attrs(sect_attrs);
1da177e4
LT
1235}
1236
1237static void remove_sect_attrs(struct module *mod)
1238{
1239 if (mod->sect_attrs) {
1240 sysfs_remove_group(&mod->mkobj.kobj,
1241 &mod->sect_attrs->grp);
1242 /* We are positive that no one is using any sect attrs
1243 * at this point. Deallocate immediately. */
04b1db9f 1244 free_sect_attrs(mod->sect_attrs);
1da177e4
LT
1245 mod->sect_attrs = NULL;
1246 }
1247}
1248
6d760133
RM
1249/*
1250 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1251 */
1252
1253struct module_notes_attrs {
1254 struct kobject *dir;
1255 unsigned int notes;
1256 struct bin_attribute attrs[0];
1257};
1258
2c3c8bea 1259static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
6d760133
RM
1260 struct bin_attribute *bin_attr,
1261 char *buf, loff_t pos, size_t count)
1262{
1263 /*
1264 * The caller checked the pos and count against our size.
1265 */
1266 memcpy(buf, bin_attr->private + pos, count);
1267 return count;
1268}
1269
1270static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1271 unsigned int i)
1272{
1273 if (notes_attrs->dir) {
1274 while (i-- > 0)
1275 sysfs_remove_bin_file(notes_attrs->dir,
1276 &notes_attrs->attrs[i]);
e9432093 1277 kobject_put(notes_attrs->dir);
6d760133
RM
1278 }
1279 kfree(notes_attrs);
1280}
1281
8f6d0378 1282static void add_notes_attrs(struct module *mod, const struct load_info *info)
6d760133
RM
1283{
1284 unsigned int notes, loaded, i;
1285 struct module_notes_attrs *notes_attrs;
1286 struct bin_attribute *nattr;
1287
ea6bff36
IM
1288 /* failed to create section attributes, so can't create notes */
1289 if (!mod->sect_attrs)
1290 return;
1291
6d760133
RM
1292 /* Count notes sections and allocate structures. */
1293 notes = 0;
8f6d0378
RR
1294 for (i = 0; i < info->hdr->e_shnum; i++)
1295 if (!sect_empty(&info->sechdrs[i]) &&
1296 (info->sechdrs[i].sh_type == SHT_NOTE))
6d760133
RM
1297 ++notes;
1298
1299 if (notes == 0)
1300 return;
1301
1302 notes_attrs = kzalloc(sizeof(*notes_attrs)
1303 + notes * sizeof(notes_attrs->attrs[0]),
1304 GFP_KERNEL);
1305 if (notes_attrs == NULL)
1306 return;
1307
1308 notes_attrs->notes = notes;
1309 nattr = &notes_attrs->attrs[0];
8f6d0378
RR
1310 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1311 if (sect_empty(&info->sechdrs[i]))
6d760133 1312 continue;
8f6d0378 1313 if (info->sechdrs[i].sh_type == SHT_NOTE) {
361795b1 1314 sysfs_bin_attr_init(nattr);
6d760133
RM
1315 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1316 nattr->attr.mode = S_IRUGO;
8f6d0378
RR
1317 nattr->size = info->sechdrs[i].sh_size;
1318 nattr->private = (void *) info->sechdrs[i].sh_addr;
6d760133
RM
1319 nattr->read = module_notes_read;
1320 ++nattr;
1321 }
1322 ++loaded;
1323 }
1324
4ff6abff 1325 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
6d760133
RM
1326 if (!notes_attrs->dir)
1327 goto out;
1328
1329 for (i = 0; i < notes; ++i)
1330 if (sysfs_create_bin_file(notes_attrs->dir,
1331 &notes_attrs->attrs[i]))
1332 goto out;
1333
1334 mod->notes_attrs = notes_attrs;
1335 return;
1336
1337 out:
1338 free_notes_attrs(notes_attrs, i);
1339}
1340
1341static void remove_notes_attrs(struct module *mod)
1342{
1343 if (mod->notes_attrs)
1344 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1345}
1346
1da177e4 1347#else
04b1db9f 1348
8f6d0378
RR
1349static inline void add_sect_attrs(struct module *mod,
1350 const struct load_info *info)
1da177e4
LT
1351{
1352}
1353
1354static inline void remove_sect_attrs(struct module *mod)
1355{
1356}
6d760133 1357
8f6d0378
RR
1358static inline void add_notes_attrs(struct module *mod,
1359 const struct load_info *info)
6d760133
RM
1360{
1361}
1362
1363static inline void remove_notes_attrs(struct module *mod)
1364{
1365}
8f6d0378 1366#endif /* CONFIG_KALLSYMS */
1da177e4 1367
80a3d1bb
RR
1368static void add_usage_links(struct module *mod)
1369{
1370#ifdef CONFIG_MODULE_UNLOAD
1371 struct module_use *use;
1372 int nowarn;
1373
75676500 1374 mutex_lock(&module_mutex);
80a3d1bb
RR
1375 list_for_each_entry(use, &mod->target_list, target_list) {
1376 nowarn = sysfs_create_link(use->target->holders_dir,
1377 &mod->mkobj.kobj, mod->name);
1378 }
75676500 1379 mutex_unlock(&module_mutex);
80a3d1bb
RR
1380#endif
1381}
1382
1383static void del_usage_links(struct module *mod)
1384{
1385#ifdef CONFIG_MODULE_UNLOAD
1386 struct module_use *use;
1387
75676500 1388 mutex_lock(&module_mutex);
80a3d1bb
RR
1389 list_for_each_entry(use, &mod->target_list, target_list)
1390 sysfs_remove_link(use->target->holders_dir, mod->name);
75676500 1391 mutex_unlock(&module_mutex);
80a3d1bb
RR
1392#endif
1393}
1394
6407ebb2 1395static int module_add_modinfo_attrs(struct module *mod)
c988d2b2
MD
1396{
1397 struct module_attribute *attr;
03e88ae1 1398 struct module_attribute *temp_attr;
c988d2b2
MD
1399 int error = 0;
1400 int i;
1401
03e88ae1
GKH
1402 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1403 (ARRAY_SIZE(modinfo_attrs) + 1)),
1404 GFP_KERNEL);
1405 if (!mod->modinfo_attrs)
1406 return -ENOMEM;
1407
1408 temp_attr = mod->modinfo_attrs;
c988d2b2
MD
1409 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1410 if (!attr->test ||
03e88ae1
GKH
1411 (attr->test && attr->test(mod))) {
1412 memcpy(temp_attr, attr, sizeof(*temp_attr));
361795b1 1413 sysfs_attr_init(&temp_attr->attr);
03e88ae1
GKH
1414 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1415 ++temp_attr;
1416 }
c988d2b2
MD
1417 }
1418 return error;
1419}
1420
6407ebb2 1421static void module_remove_modinfo_attrs(struct module *mod)
c988d2b2
MD
1422{
1423 struct module_attribute *attr;
1424 int i;
1425
03e88ae1
GKH
1426 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1427 /* pick a field to test for end of list */
1428 if (!attr->attr.name)
1429 break;
c988d2b2 1430 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
03e88ae1
GKH
1431 if (attr->free)
1432 attr->free(mod);
c988d2b2 1433 }
03e88ae1 1434 kfree(mod->modinfo_attrs);
c988d2b2 1435}
1da177e4 1436
6407ebb2 1437static int mod_sysfs_init(struct module *mod)
1da177e4
LT
1438{
1439 int err;
6494a93d 1440 struct kobject *kobj;
1da177e4 1441
823bccfc
GKH
1442 if (!module_sysfs_initialized) {
1443 printk(KERN_ERR "%s: module sysfs not initialized\n",
1cc5f714
ES
1444 mod->name);
1445 err = -EINVAL;
1446 goto out;
1447 }
6494a93d
GKH
1448
1449 kobj = kset_find_obj(module_kset, mod->name);
1450 if (kobj) {
1451 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1452 kobject_put(kobj);
1453 err = -EINVAL;
1454 goto out;
1455 }
1456
1da177e4 1457 mod->mkobj.mod = mod;
e17e0f51 1458
ac3c8141
GKH
1459 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1460 mod->mkobj.kobj.kset = module_kset;
1461 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1462 "%s", mod->name);
1463 if (err)
1464 kobject_put(&mod->mkobj.kobj);
270a6c4c 1465
97c146ef 1466 /* delay uevent until full sysfs population */
270a6c4c
KS
1467out:
1468 return err;
1469}
1470
6407ebb2 1471static int mod_sysfs_setup(struct module *mod,
8f6d0378 1472 const struct load_info *info,
270a6c4c
KS
1473 struct kernel_param *kparam,
1474 unsigned int num_params)
1475{
1476 int err;
1477
80a3d1bb
RR
1478 err = mod_sysfs_init(mod);
1479 if (err)
1480 goto out;
1481
4ff6abff 1482 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
240936e1
AM
1483 if (!mod->holders_dir) {
1484 err = -ENOMEM;
270a6c4c 1485 goto out_unreg;
240936e1 1486 }
270a6c4c 1487
1da177e4
LT
1488 err = module_param_sysfs_setup(mod, kparam, num_params);
1489 if (err)
270a6c4c 1490 goto out_unreg_holders;
1da177e4 1491
c988d2b2
MD
1492 err = module_add_modinfo_attrs(mod);
1493 if (err)
e17e0f51 1494 goto out_unreg_param;
c988d2b2 1495
80a3d1bb 1496 add_usage_links(mod);
8f6d0378
RR
1497 add_sect_attrs(mod, info);
1498 add_notes_attrs(mod, info);
80a3d1bb 1499
e17e0f51 1500 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1da177e4
LT
1501 return 0;
1502
e17e0f51
KS
1503out_unreg_param:
1504 module_param_sysfs_remove(mod);
270a6c4c 1505out_unreg_holders:
78a2d906 1506 kobject_put(mod->holders_dir);
270a6c4c 1507out_unreg:
e17e0f51 1508 kobject_put(&mod->mkobj.kobj);
80a3d1bb 1509out:
1da177e4
LT
1510 return err;
1511}
34e4e2fe
DL
1512
1513static void mod_sysfs_fini(struct module *mod)
1514{
8f6d0378
RR
1515 remove_notes_attrs(mod);
1516 remove_sect_attrs(mod);
34e4e2fe
DL
1517 kobject_put(&mod->mkobj.kobj);
1518}
1519
8f6d0378 1520#else /* !CONFIG_SYSFS */
34e4e2fe 1521
8f6d0378
RR
1522static int mod_sysfs_setup(struct module *mod,
1523 const struct load_info *info,
6407ebb2
RR
1524 struct kernel_param *kparam,
1525 unsigned int num_params)
1526{
1527 return 0;
1528}
1529
34e4e2fe
DL
1530static void mod_sysfs_fini(struct module *mod)
1531{
1532}
1533
36b0360d
RR
1534static void module_remove_modinfo_attrs(struct module *mod)
1535{
1536}
1537
80a3d1bb
RR
1538static void del_usage_links(struct module *mod)
1539{
1540}
1541
34e4e2fe 1542#endif /* CONFIG_SYSFS */
1da177e4 1543
36b0360d 1544static void mod_sysfs_teardown(struct module *mod)
1da177e4 1545{
80a3d1bb 1546 del_usage_links(mod);
c988d2b2 1547 module_remove_modinfo_attrs(mod);
1da177e4 1548 module_param_sysfs_remove(mod);
78a2d906
GKH
1549 kobject_put(mod->mkobj.drivers_dir);
1550 kobject_put(mod->holders_dir);
34e4e2fe 1551 mod_sysfs_fini(mod);
1da177e4
LT
1552}
1553
1554/*
1555 * unlink the module with the whole machine is stopped with interrupts off
1556 * - this defends against kallsyms not taking locks
1557 */
1558static int __unlink_module(void *_mod)
1559{
1560 struct module *mod = _mod;
1561 list_del(&mod->list);
5336377d 1562 module_bug_cleanup(mod);
1da177e4
LT
1563 return 0;
1564}
1565
84e1c6bb 1566#ifdef CONFIG_DEBUG_SET_MODULE_RONX
1567/*
1568 * LKM RO/NX protection: protect module's text/ro-data
1569 * from modification and any data from execution.
1570 */
1571void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1572{
1573 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1574 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1575
1576 if (end_pfn > begin_pfn)
1577 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1578}
1579
1580static void set_section_ro_nx(void *base,
1581 unsigned long text_size,
1582 unsigned long ro_size,
1583 unsigned long total_size)
1584{
1585 /* begin and end PFNs of the current subsection */
1586 unsigned long begin_pfn;
1587 unsigned long end_pfn;
1588
1589 /*
1590 * Set RO for module text and RO-data:
1591 * - Always protect first page.
1592 * - Do not protect last partial page.
1593 */
1594 if (ro_size > 0)
1595 set_page_attributes(base, base + ro_size, set_memory_ro);
1596
1597 /*
1598 * Set NX permissions for module data:
1599 * - Do not protect first partial page.
1600 * - Always protect last page.
1601 */
1602 if (total_size > text_size) {
1603 begin_pfn = PFN_UP((unsigned long)base + text_size);
1604 end_pfn = PFN_UP((unsigned long)base + total_size);
1605 if (end_pfn > begin_pfn)
1606 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1607 }
1608}
1609
01526ed0
JG
1610static void unset_module_core_ro_nx(struct module *mod)
1611{
1612 set_page_attributes(mod->module_core + mod->core_text_size,
1613 mod->module_core + mod->core_size,
1614 set_memory_x);
1615 set_page_attributes(mod->module_core,
1616 mod->module_core + mod->core_ro_size,
1617 set_memory_rw);
1618}
1619
1620static void unset_module_init_ro_nx(struct module *mod)
1621{
1622 set_page_attributes(mod->module_init + mod->init_text_size,
1623 mod->module_init + mod->init_size,
1624 set_memory_x);
1625 set_page_attributes(mod->module_init,
1626 mod->module_init + mod->init_ro_size,
1627 set_memory_rw);
84e1c6bb 1628}
1629
1630/* Iterate through all modules and set each module's text as RW */
5d05c708 1631void set_all_modules_text_rw(void)
84e1c6bb 1632{
1633 struct module *mod;
1634
1635 mutex_lock(&module_mutex);
1636 list_for_each_entry_rcu(mod, &modules, list) {
1637 if ((mod->module_core) && (mod->core_text_size)) {
1638 set_page_attributes(mod->module_core,
1639 mod->module_core + mod->core_text_size,
1640 set_memory_rw);
1641 }
1642 if ((mod->module_init) && (mod->init_text_size)) {
1643 set_page_attributes(mod->module_init,
1644 mod->module_init + mod->init_text_size,
1645 set_memory_rw);
1646 }
1647 }
1648 mutex_unlock(&module_mutex);
1649}
1650
1651/* Iterate through all modules and set each module's text as RO */
5d05c708 1652void set_all_modules_text_ro(void)
84e1c6bb 1653{
1654 struct module *mod;
1655
1656 mutex_lock(&module_mutex);
1657 list_for_each_entry_rcu(mod, &modules, list) {
1658 if ((mod->module_core) && (mod->core_text_size)) {
1659 set_page_attributes(mod->module_core,
1660 mod->module_core + mod->core_text_size,
1661 set_memory_ro);
1662 }
1663 if ((mod->module_init) && (mod->init_text_size)) {
1664 set_page_attributes(mod->module_init,
1665 mod->module_init + mod->init_text_size,
1666 set_memory_ro);
1667 }
1668 }
1669 mutex_unlock(&module_mutex);
1670}
1671#else
1672static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
01526ed0
JG
1673static void unset_module_core_ro_nx(struct module *mod) { }
1674static void unset_module_init_ro_nx(struct module *mod) { }
84e1c6bb 1675#endif
1676
75676500 1677/* Free a module, remove from lists, etc. */
1da177e4
LT
1678static void free_module(struct module *mod)
1679{
7ead8b83
LZ
1680 trace_module_free(mod);
1681
1da177e4 1682 /* Delete from various lists */
75676500 1683 mutex_lock(&module_mutex);
9b1a4d38 1684 stop_machine(__unlink_module, mod, NULL);
75676500 1685 mutex_unlock(&module_mutex);
36b0360d 1686 mod_sysfs_teardown(mod);
1da177e4 1687
b82bab4b
JB
1688 /* Remove dynamic debug info */
1689 ddebug_remove_module(mod->name);
1690
1da177e4
LT
1691 /* Arch-specific cleanup. */
1692 module_arch_cleanup(mod);
1693
1694 /* Module unload stuff */
1695 module_unload_free(mod);
1696
e180a6b7
RR
1697 /* Free any allocated parameters. */
1698 destroy_params(mod->kp, mod->num_kp);
1699
1da177e4 1700 /* This may be NULL, but that's OK */
01526ed0 1701 unset_module_init_ro_nx(mod);
1da177e4
LT
1702 module_free(mod, mod->module_init);
1703 kfree(mod->args);
259354de 1704 percpu_modfree(mod);
9f85a4bb 1705
fbb9ce95
IM
1706 /* Free lock-classes: */
1707 lockdep_free_key_range(mod->module_core, mod->core_size);
1708
1da177e4 1709 /* Finally, free the core (containing the module structure) */
01526ed0 1710 unset_module_core_ro_nx(mod);
1da177e4 1711 module_free(mod, mod->module_core);
eb8cdec4
BS
1712
1713#ifdef CONFIG_MPU
1714 update_protections(current->mm);
1715#endif
1da177e4
LT
1716}
1717
1718void *__symbol_get(const char *symbol)
1719{
1720 struct module *owner;
414fd31b 1721 const struct kernel_symbol *sym;
1da177e4 1722
24da1cbf 1723 preempt_disable();
414fd31b
TA
1724 sym = find_symbol(symbol, &owner, NULL, true, true);
1725 if (sym && strong_try_module_get(owner))
1726 sym = NULL;
24da1cbf 1727 preempt_enable();
1da177e4 1728
414fd31b 1729 return sym ? (void *)sym->value : NULL;
1da177e4
LT
1730}
1731EXPORT_SYMBOL_GPL(__symbol_get);
1732
eea8b54d
AN
1733/*
1734 * Ensure that an exported symbol [global namespace] does not already exist
02a3e59a 1735 * in the kernel or in some other module's exported symbol table.
be593f4c
RR
1736 *
1737 * You must hold the module_mutex.
eea8b54d
AN
1738 */
1739static int verify_export_symbols(struct module *mod)
1740{
b211104d 1741 unsigned int i;
eea8b54d 1742 struct module *owner;
b211104d
RR
1743 const struct kernel_symbol *s;
1744 struct {
1745 const struct kernel_symbol *sym;
1746 unsigned int num;
1747 } arr[] = {
1748 { mod->syms, mod->num_syms },
1749 { mod->gpl_syms, mod->num_gpl_syms },
1750 { mod->gpl_future_syms, mod->num_gpl_future_syms },
f7f5b675 1751#ifdef CONFIG_UNUSED_SYMBOLS
b211104d
RR
1752 { mod->unused_syms, mod->num_unused_syms },
1753 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
f7f5b675 1754#endif
b211104d 1755 };
eea8b54d 1756
b211104d
RR
1757 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1758 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
be593f4c 1759 if (find_symbol(s->name, &owner, NULL, true, false)) {
b211104d
RR
1760 printk(KERN_ERR
1761 "%s: exports duplicate symbol %s"
1762 " (owned by %s)\n",
1763 mod->name, s->name, module_name(owner));
1764 return -ENOEXEC;
1765 }
eea8b54d 1766 }
b211104d
RR
1767 }
1768 return 0;
eea8b54d
AN
1769}
1770
9a4b9708 1771/* Change all symbols so that st_value encodes the pointer directly. */
49668688
RR
1772static int simplify_symbols(struct module *mod, const struct load_info *info)
1773{
1774 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1775 Elf_Sym *sym = (void *)symsec->sh_addr;
1da177e4 1776 unsigned long secbase;
49668688 1777 unsigned int i;
1da177e4 1778 int ret = 0;
414fd31b 1779 const struct kernel_symbol *ksym;
1da177e4 1780
49668688
RR
1781 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1782 const char *name = info->strtab + sym[i].st_name;
1783
1da177e4
LT
1784 switch (sym[i].st_shndx) {
1785 case SHN_COMMON:
1786 /* We compiled with -fno-common. These are not
1787 supposed to happen. */
49668688 1788 DEBUGP("Common symbol: %s\n", name);
1da177e4
LT
1789 printk("%s: please compile with -fno-common\n",
1790 mod->name);
1791 ret = -ENOEXEC;
1792 break;
1793
1794 case SHN_ABS:
1795 /* Don't need to do anything */
1796 DEBUGP("Absolute symbol: 0x%08lx\n",
1797 (long)sym[i].st_value);
1798 break;
1799
1800 case SHN_UNDEF:
49668688 1801 ksym = resolve_symbol_wait(mod, info, name);
1da177e4 1802 /* Ok if resolved. */
9bea7f23 1803 if (ksym && !IS_ERR(ksym)) {
414fd31b 1804 sym[i].st_value = ksym->value;
1da177e4 1805 break;
414fd31b
TA
1806 }
1807
1da177e4 1808 /* Ok if weak. */
9bea7f23 1809 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1da177e4
LT
1810 break;
1811
9bea7f23 1812 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
49668688 1813 mod->name, name, PTR_ERR(ksym));
9bea7f23 1814 ret = PTR_ERR(ksym) ?: -ENOENT;
1da177e4
LT
1815 break;
1816
1817 default:
1818 /* Divert to percpu allocation if a percpu var. */
49668688 1819 if (sym[i].st_shndx == info->index.pcpu)
259354de 1820 secbase = (unsigned long)mod_percpu(mod);
1da177e4 1821 else
49668688 1822 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
1da177e4
LT
1823 sym[i].st_value += secbase;
1824 break;
1825 }
1826 }
1827
1828 return ret;
1829}
1830
49668688 1831static int apply_relocations(struct module *mod, const struct load_info *info)
22e268eb
RR
1832{
1833 unsigned int i;
1834 int err = 0;
1835
1836 /* Now do relocations. */
49668688
RR
1837 for (i = 1; i < info->hdr->e_shnum; i++) {
1838 unsigned int infosec = info->sechdrs[i].sh_info;
22e268eb
RR
1839
1840 /* Not a valid relocation section? */
49668688 1841 if (infosec >= info->hdr->e_shnum)
22e268eb
RR
1842 continue;
1843
1844 /* Don't bother with non-allocated sections */
49668688 1845 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
22e268eb
RR
1846 continue;
1847
49668688
RR
1848 if (info->sechdrs[i].sh_type == SHT_REL)
1849 err = apply_relocate(info->sechdrs, info->strtab,
1850 info->index.sym, i, mod);
1851 else if (info->sechdrs[i].sh_type == SHT_RELA)
1852 err = apply_relocate_add(info->sechdrs, info->strtab,
1853 info->index.sym, i, mod);
22e268eb
RR
1854 if (err < 0)
1855 break;
1856 }
1857 return err;
1858}
1859
088af9a6
HD
1860/* Additional bytes needed by arch in front of individual sections */
1861unsigned int __weak arch_mod_section_prepend(struct module *mod,
1862 unsigned int section)
1863{
1864 /* default implementation just returns zero */
1865 return 0;
1866}
1867
1da177e4 1868/* Update size with this section: return offset. */
088af9a6
HD
1869static long get_offset(struct module *mod, unsigned int *size,
1870 Elf_Shdr *sechdr, unsigned int section)
1da177e4
LT
1871{
1872 long ret;
1873
088af9a6 1874 *size += arch_mod_section_prepend(mod, section);
1da177e4
LT
1875 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1876 *size = ret + sechdr->sh_size;
1877 return ret;
1878}
1879
1880/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1881 might -- code, read-only data, read-write data, small data. Tally
1882 sizes, and place the offsets into sh_entsize fields: high bit means it
1883 belongs in init. */
49668688 1884static void layout_sections(struct module *mod, struct load_info *info)
1da177e4
LT
1885{
1886 static unsigned long const masks[][2] = {
1887 /* NOTE: all executable code must be the first section
1888 * in this array; otherwise modify the text_size
1889 * finder in the two loops below */
1890 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1891 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1892 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1893 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1894 };
1895 unsigned int m, i;
1896
49668688
RR
1897 for (i = 0; i < info->hdr->e_shnum; i++)
1898 info->sechdrs[i].sh_entsize = ~0UL;
1da177e4
LT
1899
1900 DEBUGP("Core section allocation order:\n");
1901 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
49668688
RR
1902 for (i = 0; i < info->hdr->e_shnum; ++i) {
1903 Elf_Shdr *s = &info->sechdrs[i];
1904 const char *sname = info->secstrings + s->sh_name;
1da177e4
LT
1905
1906 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1907 || (s->sh_flags & masks[m][1])
1908 || s->sh_entsize != ~0UL
49668688 1909 || strstarts(sname, ".init"))
1da177e4 1910 continue;
088af9a6 1911 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
49668688 1912 DEBUGP("\t%s\n", name);
1da177e4 1913 }
84e1c6bb 1914 switch (m) {
1915 case 0: /* executable */
1916 mod->core_size = debug_align(mod->core_size);
1da177e4 1917 mod->core_text_size = mod->core_size;
84e1c6bb 1918 break;
1919 case 1: /* RO: text and ro-data */
1920 mod->core_size = debug_align(mod->core_size);
1921 mod->core_ro_size = mod->core_size;
1922 break;
1923 case 3: /* whole core */
1924 mod->core_size = debug_align(mod->core_size);
1925 break;
1926 }
1da177e4
LT
1927 }
1928
1929 DEBUGP("Init section allocation order:\n");
1930 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
49668688
RR
1931 for (i = 0; i < info->hdr->e_shnum; ++i) {
1932 Elf_Shdr *s = &info->sechdrs[i];
1933 const char *sname = info->secstrings + s->sh_name;
1da177e4
LT
1934
1935 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1936 || (s->sh_flags & masks[m][1])
1937 || s->sh_entsize != ~0UL
49668688 1938 || !strstarts(sname, ".init"))
1da177e4 1939 continue;
088af9a6 1940 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1da177e4 1941 | INIT_OFFSET_MASK);
49668688 1942 DEBUGP("\t%s\n", sname);
1da177e4 1943 }
84e1c6bb 1944 switch (m) {
1945 case 0: /* executable */
1946 mod->init_size = debug_align(mod->init_size);
1da177e4 1947 mod->init_text_size = mod->init_size;
84e1c6bb 1948 break;
1949 case 1: /* RO: text and ro-data */
1950 mod->init_size = debug_align(mod->init_size);
1951 mod->init_ro_size = mod->init_size;
1952 break;
1953 case 3: /* whole init */
1954 mod->init_size = debug_align(mod->init_size);
1955 break;
1956 }
1da177e4
LT
1957 }
1958}
1959
1da177e4
LT
1960static void set_license(struct module *mod, const char *license)
1961{
1962 if (!license)
1963 license = "unspecified";
1964
fa3ba2e8 1965 if (!license_is_gpl_compatible(license)) {
25ddbb18 1966 if (!test_taint(TAINT_PROPRIETARY_MODULE))
1d4d2627 1967 printk(KERN_WARNING "%s: module license '%s' taints "
fa3ba2e8
FM
1968 "kernel.\n", mod->name, license);
1969 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1da177e4
LT
1970 }
1971}
1972
1973/* Parse tag=value strings from .modinfo section */
1974static char *next_string(char *string, unsigned long *secsize)
1975{
1976 /* Skip non-zero chars */
1977 while (string[0]) {
1978 string++;
1979 if ((*secsize)-- <= 1)
1980 return NULL;
1981 }
1982
1983 /* Skip any zero padding. */
1984 while (!string[0]) {
1985 string++;
1986 if ((*secsize)-- <= 1)
1987 return NULL;
1988 }
1989 return string;
1990}
1991
49668688 1992static char *get_modinfo(struct load_info *info, const char *tag)
1da177e4
LT
1993{
1994 char *p;
1995 unsigned int taglen = strlen(tag);
49668688
RR
1996 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
1997 unsigned long size = infosec->sh_size;
1da177e4 1998
49668688 1999 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
1da177e4
LT
2000 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2001 return p + taglen + 1;
2002 }
2003 return NULL;
2004}
2005
49668688 2006static void setup_modinfo(struct module *mod, struct load_info *info)
c988d2b2
MD
2007{
2008 struct module_attribute *attr;
2009 int i;
2010
2011 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2012 if (attr->setup)
49668688 2013 attr->setup(mod, get_modinfo(info, attr->attr.name));
c988d2b2
MD
2014 }
2015}
c988d2b2 2016
a263f776
RR
2017static void free_modinfo(struct module *mod)
2018{
2019 struct module_attribute *attr;
2020 int i;
2021
2022 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2023 if (attr->free)
2024 attr->free(mod);
2025 }
2026}
2027
1da177e4 2028#ifdef CONFIG_KALLSYMS
15bba37d
WC
2029
2030/* lookup symbol in given range of kernel_symbols */
2031static const struct kernel_symbol *lookup_symbol(const char *name,
2032 const struct kernel_symbol *start,
2033 const struct kernel_symbol *stop)
2034{
2035 const struct kernel_symbol *ks = start;
2036 for (; ks < stop; ks++)
2037 if (strcmp(ks->name, name) == 0)
2038 return ks;
2039 return NULL;
2040}
2041
ca4787b7
TA
2042static int is_exported(const char *name, unsigned long value,
2043 const struct module *mod)
1da177e4 2044{
ca4787b7
TA
2045 const struct kernel_symbol *ks;
2046 if (!mod)
2047 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
3fd6805f 2048 else
ca4787b7
TA
2049 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2050 return ks != NULL && ks->value == value;
1da177e4
LT
2051}
2052
2053/* As per nm */
eded41c1 2054static char elf_type(const Elf_Sym *sym, const struct load_info *info)
1da177e4 2055{
eded41c1
RR
2056 const Elf_Shdr *sechdrs = info->sechdrs;
2057
1da177e4
LT
2058 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2059 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2060 return 'v';
2061 else
2062 return 'w';
2063 }
2064 if (sym->st_shndx == SHN_UNDEF)
2065 return 'U';
2066 if (sym->st_shndx == SHN_ABS)
2067 return 'a';
2068 if (sym->st_shndx >= SHN_LORESERVE)
2069 return '?';
2070 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2071 return 't';
2072 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2073 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2074 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2075 return 'r';
2076 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2077 return 'g';
2078 else
2079 return 'd';
2080 }
2081 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2082 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2083 return 's';
2084 else
2085 return 'b';
2086 }
eded41c1
RR
2087 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2088 ".debug")) {
1da177e4 2089 return 'n';
eded41c1 2090 }
1da177e4
LT
2091 return '?';
2092}
2093
4a496226
JB
2094static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2095 unsigned int shnum)
2096{
2097 const Elf_Shdr *sec;
2098
2099 if (src->st_shndx == SHN_UNDEF
2100 || src->st_shndx >= shnum
2101 || !src->st_name)
2102 return false;
2103
2104 sec = sechdrs + src->st_shndx;
2105 if (!(sec->sh_flags & SHF_ALLOC)
2106#ifndef CONFIG_KALLSYMS_ALL
2107 || !(sec->sh_flags & SHF_EXECINSTR)
2108#endif
2109 || (sec->sh_entsize & INIT_OFFSET_MASK))
2110 return false;
2111
2112 return true;
2113}
2114
49668688 2115static void layout_symtab(struct module *mod, struct load_info *info)
4a496226 2116{
49668688
RR
2117 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2118 Elf_Shdr *strsect = info->sechdrs + info->index.str;
4a496226
JB
2119 const Elf_Sym *src;
2120 unsigned int i, nsrc, ndst;
2121
2122 /* Put symbol section at end of init part of module. */
2123 symsect->sh_flags |= SHF_ALLOC;
2124 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
49668688
RR
2125 info->index.sym) | INIT_OFFSET_MASK;
2126 DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
4a496226 2127
49668688 2128 src = (void *)info->hdr + symsect->sh_offset;
4a496226
JB
2129 nsrc = symsect->sh_size / sizeof(*src);
2130 for (ndst = i = 1; i < nsrc; ++i, ++src)
49668688 2131 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
554bdfe5
JB
2132 unsigned int j = src->st_name;
2133
49668688
RR
2134 while (!__test_and_set_bit(j, info->strmap)
2135 && info->strtab[j])
554bdfe5 2136 ++j;
4a496226 2137 ++ndst;
554bdfe5 2138 }
4a496226
JB
2139
2140 /* Append room for core symbols at end of core part. */
49668688
RR
2141 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2142 mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
4a496226 2143
554bdfe5
JB
2144 /* Put string table section at end of init part of module. */
2145 strsect->sh_flags |= SHF_ALLOC;
2146 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
49668688
RR
2147 info->index.str) | INIT_OFFSET_MASK;
2148 DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
554bdfe5
JB
2149
2150 /* Append room for core symbols' strings at end of core part. */
49668688
RR
2151 info->stroffs = mod->core_size;
2152 __set_bit(0, info->strmap);
2153 mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
4a496226
JB
2154}
2155
811d66a0 2156static void add_kallsyms(struct module *mod, const struct load_info *info)
1da177e4 2157{
4a496226
JB
2158 unsigned int i, ndst;
2159 const Elf_Sym *src;
2160 Elf_Sym *dst;
554bdfe5 2161 char *s;
eded41c1 2162 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1da177e4 2163
eded41c1
RR
2164 mod->symtab = (void *)symsec->sh_addr;
2165 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
511ca6ae
RR
2166 /* Make sure we get permanent strtab: don't use info->strtab. */
2167 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
1da177e4
LT
2168
2169 /* Set types up while we still have access to sections. */
2170 for (i = 0; i < mod->num_symtab; i++)
eded41c1 2171 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
4a496226 2172
d913188c 2173 mod->core_symtab = dst = mod->module_core + info->symoffs;
4a496226
JB
2174 src = mod->symtab;
2175 *dst = *src;
2176 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
eded41c1 2177 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
4a496226
JB
2178 continue;
2179 dst[ndst] = *src;
d913188c
RR
2180 dst[ndst].st_name = bitmap_weight(info->strmap,
2181 dst[ndst].st_name);
4a496226
JB
2182 ++ndst;
2183 }
2184 mod->core_num_syms = ndst;
554bdfe5 2185
d913188c 2186 mod->core_strtab = s = mod->module_core + info->stroffs;
eded41c1 2187 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
d913188c 2188 if (test_bit(i, info->strmap))
554bdfe5 2189 *++s = mod->strtab[i];
1da177e4
LT
2190}
2191#else
49668688 2192static inline void layout_symtab(struct module *mod, struct load_info *info)
4a496226
JB
2193{
2194}
3ae91c21 2195
abbce906 2196static void add_kallsyms(struct module *mod, const struct load_info *info)
1da177e4
LT
2197{
2198}
2199#endif /* CONFIG_KALLSYMS */
2200
e9d376f0 2201static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
346e15be 2202{
811d66a0
RR
2203 if (!debug)
2204 return;
e9d376f0
JB
2205#ifdef CONFIG_DYNAMIC_DEBUG
2206 if (ddebug_add_module(debug, num, debug->modname))
2207 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2208 debug->modname);
2209#endif
5e458cc0 2210}
346e15be 2211
ff49d74a
YS
2212static void dynamic_debug_remove(struct _ddebug *debug)
2213{
2214 if (debug)
2215 ddebug_remove_module(debug->modname);
2216}
2217
3a642e99
RR
2218static void *module_alloc_update_bounds(unsigned long size)
2219{
2220 void *ret = module_alloc(size);
2221
2222 if (ret) {
75676500 2223 mutex_lock(&module_mutex);
3a642e99
RR
2224 /* Update module bounds. */
2225 if ((unsigned long)ret < module_addr_min)
2226 module_addr_min = (unsigned long)ret;
2227 if ((unsigned long)ret + size > module_addr_max)
2228 module_addr_max = (unsigned long)ret + size;
75676500 2229 mutex_unlock(&module_mutex);
3a642e99
RR
2230 }
2231 return ret;
2232}
2233
4f2294b6 2234#ifdef CONFIG_DEBUG_KMEMLEAK
49668688
RR
2235static void kmemleak_load_module(const struct module *mod,
2236 const struct load_info *info)
4f2294b6
CM
2237{
2238 unsigned int i;
2239
2240 /* only scan the sections containing data */
c017b4be 2241 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
4f2294b6 2242
49668688
RR
2243 for (i = 1; i < info->hdr->e_shnum; i++) {
2244 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2245 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
4f2294b6 2246 continue;
49668688 2247 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
4f2294b6
CM
2248 continue;
2249
49668688
RR
2250 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2251 info->sechdrs[i].sh_size, GFP_KERNEL);
4f2294b6
CM
2252 }
2253}
2254#else
49668688
RR
2255static inline void kmemleak_load_module(const struct module *mod,
2256 const struct load_info *info)
4f2294b6
CM
2257{
2258}
2259#endif
2260
6526c534 2261/* Sets info->hdr and info->len. */
d913188c
RR
2262static int copy_and_check(struct load_info *info,
2263 const void __user *umod, unsigned long len,
2264 const char __user *uargs)
40dd2560
RR
2265{
2266 int err;
2267 Elf_Ehdr *hdr;
2268
2269 if (len < sizeof(*hdr))
2270 return -ENOEXEC;
2271
2272 /* Suck in entire file: we'll want most of it. */
2273 /* vmalloc barfs on "unusual" numbers. Check here */
3264d3f9 2274 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
40dd2560
RR
2275 return -ENOMEM;
2276
2277 if (copy_from_user(hdr, umod, len) != 0) {
2278 err = -EFAULT;
2279 goto free_hdr;
2280 }
2281
2282 /* Sanity checks against insmoding binaries or wrong arch,
2283 weird elf version */
2284 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2285 || hdr->e_type != ET_REL
2286 || !elf_check_arch(hdr)
2287 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2288 err = -ENOEXEC;
2289 goto free_hdr;
2290 }
2291
2292 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2293 err = -ENOEXEC;
2294 goto free_hdr;
2295 }
d913188c 2296
3264d3f9
LT
2297 info->hdr = hdr;
2298 info->len = len;
40dd2560
RR
2299 return 0;
2300
2301free_hdr:
2302 vfree(hdr);
2303 return err;
2304}
2305
d913188c
RR
2306static void free_copy(struct load_info *info)
2307{
d913188c
RR
2308 vfree(info->hdr);
2309}
2310
8b5f61a7
RR
2311static int rewrite_section_headers(struct load_info *info)
2312{
2313 unsigned int i;
2314
2315 /* This should always be true, but let's be sure. */
2316 info->sechdrs[0].sh_addr = 0;
2317
2318 for (i = 1; i < info->hdr->e_shnum; i++) {
2319 Elf_Shdr *shdr = &info->sechdrs[i];
2320 if (shdr->sh_type != SHT_NOBITS
2321 && info->len < shdr->sh_offset + shdr->sh_size) {
2322 printk(KERN_ERR "Module len %lu truncated\n",
2323 info->len);
2324 return -ENOEXEC;
2325 }
2326
2327 /* Mark all sections sh_addr with their address in the
2328 temporary image. */
2329 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2330
2331#ifndef CONFIG_MODULE_UNLOAD
2332 /* Don't load .exit sections */
2333 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2334 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2335#endif
8b5f61a7 2336 }
d6df72a0
RR
2337
2338 /* Track but don't keep modinfo and version sections. */
49668688
RR
2339 info->index.vers = find_sec(info, "__versions");
2340 info->index.info = find_sec(info, ".modinfo");
d6df72a0
RR
2341 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2342 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
8b5f61a7
RR
2343 return 0;
2344}
2345
3264d3f9
LT
2346/*
2347 * Set up our basic convenience variables (pointers to section headers,
2348 * search for module section index etc), and do some basic section
2349 * verification.
2350 *
2351 * Return the temporary module pointer (we'll replace it with the final
2352 * one when we move the module sections around).
2353 */
2354static struct module *setup_load_info(struct load_info *info)
2355{
2356 unsigned int i;
8b5f61a7 2357 int err;
3264d3f9
LT
2358 struct module *mod;
2359
2360 /* Set up the convenience variables */
2361 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
8b5f61a7
RR
2362 info->secstrings = (void *)info->hdr
2363 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
3264d3f9 2364
8b5f61a7
RR
2365 err = rewrite_section_headers(info);
2366 if (err)
2367 return ERR_PTR(err);
3264d3f9 2368
8b5f61a7
RR
2369 /* Find internal symbols and strings. */
2370 for (i = 1; i < info->hdr->e_shnum; i++) {
3264d3f9
LT
2371 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2372 info->index.sym = i;
2373 info->index.str = info->sechdrs[i].sh_link;
8b5f61a7
RR
2374 info->strtab = (char *)info->hdr
2375 + info->sechdrs[info->index.str].sh_offset;
2376 break;
3264d3f9 2377 }
3264d3f9
LT
2378 }
2379
49668688 2380 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
3264d3f9
LT
2381 if (!info->index.mod) {
2382 printk(KERN_WARNING "No module found in object\n");
2383 return ERR_PTR(-ENOEXEC);
2384 }
2385 /* This is temporary: point mod into copy of data. */
2386 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2387
2388 if (info->index.sym == 0) {
2389 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2390 mod->name);
2391 return ERR_PTR(-ENOEXEC);
2392 }
2393
49668688 2394 info->index.pcpu = find_pcpusec(info);
3264d3f9 2395
3264d3f9
LT
2396 /* Check module struct version now, before we try to use module. */
2397 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2398 return ERR_PTR(-ENOEXEC);
2399
2400 return mod;
3264d3f9
LT
2401}
2402
49668688 2403static int check_modinfo(struct module *mod, struct load_info *info)
40dd2560 2404{
49668688 2405 const char *modmagic = get_modinfo(info, "vermagic");
40dd2560
RR
2406 int err;
2407
2408 /* This is allowed: modprobe --force will invalidate it. */
2409 if (!modmagic) {
2410 err = try_to_force_load(mod, "bad vermagic");
2411 if (err)
2412 return err;
49668688 2413 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
40dd2560
RR
2414 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2415 mod->name, modmagic, vermagic);
2416 return -ENOEXEC;
2417 }
2418
49668688 2419 if (get_modinfo(info, "staging")) {
40dd2560
RR
2420 add_taint_module(mod, TAINT_CRAP);
2421 printk(KERN_WARNING "%s: module is from the staging directory,"
2422 " the quality is unknown, you have been warned.\n",
2423 mod->name);
2424 }
22e268eb
RR
2425
2426 /* Set up license info based on the info section */
49668688 2427 set_license(mod, get_modinfo(info, "license"));
22e268eb 2428
40dd2560
RR
2429 return 0;
2430}
2431
811d66a0 2432static void find_module_sections(struct module *mod, struct load_info *info)
f91a13bb 2433{
49668688 2434 mod->kp = section_objs(info, "__param",
f91a13bb 2435 sizeof(*mod->kp), &mod->num_kp);
49668688 2436 mod->syms = section_objs(info, "__ksymtab",
f91a13bb 2437 sizeof(*mod->syms), &mod->num_syms);
49668688
RR
2438 mod->crcs = section_addr(info, "__kcrctab");
2439 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
f91a13bb
LT
2440 sizeof(*mod->gpl_syms),
2441 &mod->num_gpl_syms);
49668688
RR
2442 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2443 mod->gpl_future_syms = section_objs(info,
f91a13bb
LT
2444 "__ksymtab_gpl_future",
2445 sizeof(*mod->gpl_future_syms),
2446 &mod->num_gpl_future_syms);
49668688 2447 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
f91a13bb
LT
2448
2449#ifdef CONFIG_UNUSED_SYMBOLS
49668688 2450 mod->unused_syms = section_objs(info, "__ksymtab_unused",
f91a13bb
LT
2451 sizeof(*mod->unused_syms),
2452 &mod->num_unused_syms);
49668688
RR
2453 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2454 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
f91a13bb
LT
2455 sizeof(*mod->unused_gpl_syms),
2456 &mod->num_unused_gpl_syms);
49668688 2457 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
f91a13bb
LT
2458#endif
2459#ifdef CONFIG_CONSTRUCTORS
49668688 2460 mod->ctors = section_objs(info, ".ctors",
f91a13bb
LT
2461 sizeof(*mod->ctors), &mod->num_ctors);
2462#endif
2463
2464#ifdef CONFIG_TRACEPOINTS
65498646
MD
2465 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2466 sizeof(*mod->tracepoints_ptrs),
2467 &mod->num_tracepoints);
f91a13bb 2468#endif
bf5438fc
JB
2469#ifdef HAVE_JUMP_LABEL
2470 mod->jump_entries = section_objs(info, "__jump_table",
2471 sizeof(*mod->jump_entries),
2472 &mod->num_jump_entries);
2473#endif
f91a13bb 2474#ifdef CONFIG_EVENT_TRACING
49668688 2475 mod->trace_events = section_objs(info, "_ftrace_events",
f91a13bb
LT
2476 sizeof(*mod->trace_events),
2477 &mod->num_trace_events);
2478 /*
2479 * This section contains pointers to allocated objects in the trace
2480 * code and not scanning it leads to false positives.
2481 */
2482 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2483 mod->num_trace_events, GFP_KERNEL);
2484#endif
13b9b6e7
SR
2485#ifdef CONFIG_TRACING
2486 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2487 sizeof(*mod->trace_bprintk_fmt_start),
2488 &mod->num_trace_bprintk_fmt);
2489 /*
2490 * This section contains pointers to allocated objects in the trace
2491 * code and not scanning it leads to false positives.
2492 */
2493 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2494 sizeof(*mod->trace_bprintk_fmt_start) *
2495 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2496#endif
f91a13bb
LT
2497#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2498 /* sechdrs[0].sh_size is always zero */
49668688 2499 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
f91a13bb
LT
2500 sizeof(*mod->ftrace_callsites),
2501 &mod->num_ftrace_callsites);
2502#endif
22e268eb 2503
811d66a0
RR
2504 mod->extable = section_objs(info, "__ex_table",
2505 sizeof(*mod->extable), &mod->num_exentries);
2506
49668688 2507 if (section_addr(info, "__obsparm"))
22e268eb
RR
2508 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2509 mod->name);
811d66a0
RR
2510
2511 info->debug = section_objs(info, "__verbose",
2512 sizeof(*info->debug), &info->num_debug);
f91a13bb
LT
2513}
2514
49668688 2515static int move_module(struct module *mod, struct load_info *info)
65b8a9b4
LT
2516{
2517 int i;
2518 void *ptr;
2519
2520 /* Do the allocs. */
2521 ptr = module_alloc_update_bounds(mod->core_size);
2522 /*
2523 * The pointer to this block is stored in the module structure
2524 * which is inside the block. Just mark it as not being a
2525 * leak.
2526 */
2527 kmemleak_not_leak(ptr);
2528 if (!ptr)
d913188c 2529 return -ENOMEM;
65b8a9b4
LT
2530
2531 memset(ptr, 0, mod->core_size);
2532 mod->module_core = ptr;
2533
2534 ptr = module_alloc_update_bounds(mod->init_size);
2535 /*
2536 * The pointer to this block is stored in the module structure
2537 * which is inside the block. This block doesn't need to be
2538 * scanned as it contains data and code that will be freed
2539 * after the module is initialized.
2540 */
2541 kmemleak_ignore(ptr);
2542 if (!ptr && mod->init_size) {
2543 module_free(mod, mod->module_core);
d913188c 2544 return -ENOMEM;
65b8a9b4
LT
2545 }
2546 memset(ptr, 0, mod->init_size);
2547 mod->module_init = ptr;
2548
2549 /* Transfer each section which specifies SHF_ALLOC */
2550 DEBUGP("final section addresses:\n");
49668688 2551 for (i = 0; i < info->hdr->e_shnum; i++) {
65b8a9b4 2552 void *dest;
49668688 2553 Elf_Shdr *shdr = &info->sechdrs[i];
65b8a9b4 2554
49668688 2555 if (!(shdr->sh_flags & SHF_ALLOC))
65b8a9b4
LT
2556 continue;
2557
49668688 2558 if (shdr->sh_entsize & INIT_OFFSET_MASK)
65b8a9b4 2559 dest = mod->module_init
49668688 2560 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
65b8a9b4 2561 else
49668688 2562 dest = mod->module_core + shdr->sh_entsize;
65b8a9b4 2563
49668688
RR
2564 if (shdr->sh_type != SHT_NOBITS)
2565 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
65b8a9b4 2566 /* Update sh_addr to point to copy in image. */
49668688 2567 shdr->sh_addr = (unsigned long)dest;
65b8a9b4 2568 DEBUGP("\t0x%lx %s\n",
49668688 2569 shdr->sh_addr, info->secstrings + shdr->sh_name);
65b8a9b4 2570 }
d913188c
RR
2571
2572 return 0;
65b8a9b4
LT
2573}
2574
49668688 2575static int check_module_license_and_versions(struct module *mod)
22e268eb
RR
2576{
2577 /*
2578 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2579 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2580 * using GPL-only symbols it needs.
2581 */
2582 if (strcmp(mod->name, "ndiswrapper") == 0)
2583 add_taint(TAINT_PROPRIETARY_MODULE);
2584
2585 /* driverloader was caught wrongly pretending to be under GPL */
2586 if (strcmp(mod->name, "driverloader") == 0)
2587 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2588
2589#ifdef CONFIG_MODVERSIONS
2590 if ((mod->num_syms && !mod->crcs)
2591 || (mod->num_gpl_syms && !mod->gpl_crcs)
2592 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2593#ifdef CONFIG_UNUSED_SYMBOLS
2594 || (mod->num_unused_syms && !mod->unused_crcs)
2595 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2596#endif
2597 ) {
2598 return try_to_force_load(mod,
2599 "no versions for exported symbols");
2600 }
2601#endif
2602 return 0;
2603}
2604
2605static void flush_module_icache(const struct module *mod)
2606{
2607 mm_segment_t old_fs;
2608
2609 /* flush the icache in correct context */
2610 old_fs = get_fs();
2611 set_fs(KERNEL_DS);
2612
2613 /*
2614 * Flush the instruction cache, since we've played with text.
2615 * Do it before processing of module parameters, so the module
2616 * can provide parameter accessor functions of its own.
2617 */
2618 if (mod->module_init)
2619 flush_icache_range((unsigned long)mod->module_init,
2620 (unsigned long)mod->module_init
2621 + mod->init_size);
2622 flush_icache_range((unsigned long)mod->module_core,
2623 (unsigned long)mod->module_core + mod->core_size);
2624
2625 set_fs(old_fs);
2626}
2627
d913188c 2628static struct module *layout_and_allocate(struct load_info *info)
1da177e4 2629{
d913188c 2630 /* Module within temporary copy. */
1da177e4 2631 struct module *mod;
49668688 2632 Elf_Shdr *pcpusec;
d913188c 2633 int err;
3ae91c21 2634
d913188c
RR
2635 mod = setup_load_info(info);
2636 if (IS_ERR(mod))
2637 return mod;
1da177e4 2638
49668688 2639 err = check_modinfo(mod, info);
40dd2560
RR
2640 if (err)
2641 return ERR_PTR(err);
1da177e4 2642
1da177e4 2643 /* Allow arches to frob section contents and sizes. */
49668688
RR
2644 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2645 info->secstrings, mod);
1da177e4 2646 if (err < 0)
6526c534 2647 goto out;
1da177e4 2648
49668688
RR
2649 pcpusec = &info->sechdrs[info->index.pcpu];
2650 if (pcpusec->sh_size) {
1da177e4 2651 /* We have a special allocation for this section. */
49668688
RR
2652 err = percpu_modalloc(mod,
2653 pcpusec->sh_size, pcpusec->sh_addralign);
259354de 2654 if (err)
6526c534 2655 goto out;
49668688 2656 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
1da177e4
LT
2657 }
2658
2659 /* Determine total sizes, and put offsets in sh_entsize. For now
2660 this is done generically; there doesn't appear to be any
2661 special cases for the architectures. */
49668688 2662 layout_sections(mod, info);
d913188c
RR
2663
2664 info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
2665 * sizeof(long), GFP_KERNEL);
2666 if (!info->strmap) {
2667 err = -ENOMEM;
2668 goto free_percpu;
2669 }
49668688 2670 layout_symtab(mod, info);
1da177e4 2671
65b8a9b4 2672 /* Allocate and move to the final place */
49668688 2673 err = move_module(mod, info);
d913188c
RR
2674 if (err)
2675 goto free_strmap;
2676
2677 /* Module has been copied to its final place now: return it. */
2678 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
49668688 2679 kmemleak_load_module(mod, info);
d913188c
RR
2680 return mod;
2681
2682free_strmap:
2683 kfree(info->strmap);
2684free_percpu:
2685 percpu_modfree(mod);
6526c534 2686out:
d913188c
RR
2687 return ERR_PTR(err);
2688}
2689
2690/* mod is no longer valid after this! */
2691static void module_deallocate(struct module *mod, struct load_info *info)
2692{
2693 kfree(info->strmap);
2694 percpu_modfree(mod);
2695 module_free(mod, mod->module_init);
2696 module_free(mod, mod->module_core);
2697}
2698
811d66a0
RR
2699static int post_relocation(struct module *mod, const struct load_info *info)
2700{
51f3d0f4 2701 /* Sort exception table now relocations are done. */
811d66a0
RR
2702 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2703
2704 /* Copy relocated percpu area over. */
2705 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2706 info->sechdrs[info->index.pcpu].sh_size);
2707
51f3d0f4 2708 /* Setup kallsyms-specific fields. */
811d66a0
RR
2709 add_kallsyms(mod, info);
2710
2711 /* Arch-specific module finalizing. */
2712 return module_finalize(info->hdr, info->sechdrs, mod);
2713}
2714
d913188c
RR
2715/* Allocate and load the module: note that size of section 0 is always
2716 zero, and we rely on this for optional sections. */
51f3d0f4 2717static struct module *load_module(void __user *umod,
d913188c
RR
2718 unsigned long len,
2719 const char __user *uargs)
2720{
2721 struct load_info info = { NULL, };
2722 struct module *mod;
2723 long err;
d913188c
RR
2724
2725 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2726 umod, len, uargs);
2727
2728 /* Copy in the blobs from userspace, check they are vaguely sane. */
2729 err = copy_and_check(&info, umod, len, uargs);
2730 if (err)
2731 return ERR_PTR(err);
2732
2733 /* Figure out module layout, and allocate all the memory. */
2734 mod = layout_and_allocate(&info);
65b8a9b4
LT
2735 if (IS_ERR(mod)) {
2736 err = PTR_ERR(mod);
d913188c 2737 goto free_copy;
1da177e4 2738 }
1da177e4 2739
49668688 2740 /* Now module is in final location, initialize linked lists, etc. */
9f85a4bb
RR
2741 err = module_unload_init(mod);
2742 if (err)
d913188c 2743 goto free_module;
1da177e4 2744
22e268eb
RR
2745 /* Now we've got everything in the final locations, we can
2746 * find optional sections. */
49668688 2747 find_module_sections(mod, &info);
9b37ccfc 2748
49668688 2749 err = check_module_license_and_versions(mod);
22e268eb
RR
2750 if (err)
2751 goto free_unload;
9841d61d 2752
c988d2b2 2753 /* Set up MODINFO_ATTR fields */
49668688 2754 setup_modinfo(mod, &info);
c988d2b2 2755
1da177e4 2756 /* Fix up syms, so that st_value is a pointer to location. */
49668688 2757 err = simplify_symbols(mod, &info);
1da177e4 2758 if (err < 0)
d913188c 2759 goto free_modinfo;
1da177e4 2760
49668688 2761 err = apply_relocations(mod, &info);
22e268eb 2762 if (err < 0)
d913188c 2763 goto free_modinfo;
1da177e4 2764
811d66a0 2765 err = post_relocation(mod, &info);
1da177e4 2766 if (err < 0)
d913188c 2767 goto free_modinfo;
1da177e4 2768
22e268eb 2769 flush_module_icache(mod);
378bac82 2770
6526c534
RR
2771 /* Now copy in args */
2772 mod->args = strndup_user(uargs, ~0UL >> 1);
2773 if (IS_ERR(mod->args)) {
2774 err = PTR_ERR(mod->args);
2775 goto free_arch_cleanup;
2776 }
8d3b33f6 2777
51f3d0f4 2778 /* Mark state as coming so strong_try_module_get() ignores us. */
d913188c
RR
2779 mod->state = MODULE_STATE_COMING;
2780
bb9d3d56 2781 /* Now sew it into the lists so we can get lockdep and oops
25985edc 2782 * info during argument parsing. No one should access us, since
d72b3751
AK
2783 * strong_try_module_get() will fail.
2784 * lockdep/oops can run asynchronous, so use the RCU list insertion
2785 * function to insert in a way safe to concurrent readers.
2786 * The mutex protects against concurrent writers.
2787 */
75676500 2788 mutex_lock(&module_mutex);
3bafeb62
LT
2789 if (find_module(mod->name)) {
2790 err = -EEXIST;
be593f4c 2791 goto unlock;
3bafeb62
LT
2792 }
2793
811d66a0
RR
2794 /* This has to be done once we're sure module name is unique. */
2795 if (!mod->taints)
2796 dynamic_debug_setup(info.debug, info.num_debug);
ff49d74a 2797
be593f4c
RR
2798 /* Find duplicate symbols */
2799 err = verify_export_symbols(mod);
2800 if (err < 0)
ff49d74a 2801 goto ddebug;
be593f4c 2802
5336377d 2803 module_bug_finalize(info.hdr, info.sechdrs, mod);
d72b3751 2804 list_add_rcu(&mod->list, &modules);
75676500 2805 mutex_unlock(&module_mutex);
bb9d3d56 2806
51f3d0f4 2807 /* Module is ready to execute: parsing args may do that. */
e180a6b7 2808 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
1da177e4 2809 if (err < 0)
bb9d3d56 2810 goto unlink;
1da177e4 2811
51f3d0f4 2812 /* Link in to syfs. */
8f6d0378 2813 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
1da177e4 2814 if (err < 0)
bb9d3d56 2815 goto unlink;
80a3d1bb 2816
d913188c
RR
2817 /* Get rid of temporary copy and strmap. */
2818 kfree(info.strmap);
2819 free_copy(&info);
1da177e4
LT
2820
2821 /* Done! */
51f3d0f4 2822 trace_module_load(mod);
1da177e4
LT
2823 return mod;
2824
bb9d3d56 2825 unlink:
75676500 2826 mutex_lock(&module_mutex);
e91defa2
RR
2827 /* Unlink carefully: kallsyms could be walking list. */
2828 list_del_rcu(&mod->list);
5336377d
LT
2829 module_bug_cleanup(mod);
2830
ff49d74a 2831 ddebug:
811d66a0
RR
2832 if (!mod->taints)
2833 dynamic_debug_remove(info.debug);
be593f4c 2834 unlock:
75676500 2835 mutex_unlock(&module_mutex);
e91defa2 2836 synchronize_sched();
6526c534
RR
2837 kfree(mod->args);
2838 free_arch_cleanup:
1da177e4 2839 module_arch_cleanup(mod);
d913188c 2840 free_modinfo:
a263f776 2841 free_modinfo(mod);
22e268eb 2842 free_unload:
1da177e4 2843 module_unload_free(mod);
d913188c
RR
2844 free_module:
2845 module_deallocate(mod, &info);
2846 free_copy:
2847 free_copy(&info);
6fe2e70b 2848 return ERR_PTR(err);
1da177e4
LT
2849}
2850
b99b87f7
PO
2851/* Call module constructors. */
2852static void do_mod_ctors(struct module *mod)
2853{
2854#ifdef CONFIG_CONSTRUCTORS
2855 unsigned long i;
2856
2857 for (i = 0; i < mod->num_ctors; i++)
2858 mod->ctors[i]();
2859#endif
2860}
2861
1da177e4 2862/* This is where the real work happens */
17da2bd9
HC
2863SYSCALL_DEFINE3(init_module, void __user *, umod,
2864 unsigned long, len, const char __user *, uargs)
1da177e4
LT
2865{
2866 struct module *mod;
2867 int ret = 0;
2868
2869 /* Must have permission */
3d43321b 2870 if (!capable(CAP_SYS_MODULE) || modules_disabled)
1da177e4
LT
2871 return -EPERM;
2872
1da177e4
LT
2873 /* Do all the hard work */
2874 mod = load_module(umod, len, uargs);
75676500 2875 if (IS_ERR(mod))
1da177e4 2876 return PTR_ERR(mod);
1da177e4 2877
e041c683
AS
2878 blocking_notifier_call_chain(&module_notify_list,
2879 MODULE_STATE_COMING, mod);
1da177e4 2880
94462ad3
SR
2881 /* Set RO and NX regions for core */
2882 set_section_ro_nx(mod->module_core,
2883 mod->core_text_size,
2884 mod->core_ro_size,
2885 mod->core_size);
2886
2887 /* Set RO and NX regions for init */
2888 set_section_ro_nx(mod->module_init,
2889 mod->init_text_size,
2890 mod->init_ro_size,
2891 mod->init_size);
2892
b99b87f7 2893 do_mod_ctors(mod);
1da177e4
LT
2894 /* Start the module */
2895 if (mod->init != NULL)
59f9415f 2896 ret = do_one_initcall(mod->init);
1da177e4
LT
2897 if (ret < 0) {
2898 /* Init routine failed: abort. Try to protect us from
2899 buggy refcounters. */
2900 mod->state = MODULE_STATE_GOING;
fbd568a3 2901 synchronize_sched();
af49d924 2902 module_put(mod);
df4b565e
PO
2903 blocking_notifier_call_chain(&module_notify_list,
2904 MODULE_STATE_GOING, mod);
af49d924 2905 free_module(mod);
c9a3ba55 2906 wake_up(&module_wq);
1da177e4
LT
2907 return ret;
2908 }
e24e2e64 2909 if (ret > 0) {
ad361c98
JP
2910 printk(KERN_WARNING
2911"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2912"%s: loading module anyway...\n",
e24e2e64
AD
2913 __func__, mod->name, ret,
2914 __func__);
2915 dump_stack();
2916 }
1da177e4 2917
6c5db22d 2918 /* Now it's a first class citizen! Wake up anyone waiting for it. */
1da177e4 2919 mod->state = MODULE_STATE_LIVE;
6c5db22d 2920 wake_up(&module_wq);
0deddf43
MH
2921 blocking_notifier_call_chain(&module_notify_list,
2922 MODULE_STATE_LIVE, mod);
6c5db22d 2923
d6de2c80
LT
2924 /* We need to finish all async code before the module init sequence is done */
2925 async_synchronize_full();
2926
6c5db22d 2927 mutex_lock(&module_mutex);
1da177e4
LT
2928 /* Drop initial reference. */
2929 module_put(mod);
ad6561df 2930 trim_init_extable(mod);
4a496226
JB
2931#ifdef CONFIG_KALLSYMS
2932 mod->num_symtab = mod->core_num_syms;
2933 mod->symtab = mod->core_symtab;
554bdfe5 2934 mod->strtab = mod->core_strtab;
4a496226 2935#endif
01526ed0 2936 unset_module_init_ro_nx(mod);
1da177e4
LT
2937 module_free(mod, mod->module_init);
2938 mod->module_init = NULL;
2939 mod->init_size = 0;
4d10380e 2940 mod->init_ro_size = 0;
1da177e4 2941 mod->init_text_size = 0;
6389a385 2942 mutex_unlock(&module_mutex);
1da177e4
LT
2943
2944 return 0;
2945}
2946
2947static inline int within(unsigned long addr, void *start, unsigned long size)
2948{
2949 return ((void *)addr >= start && (void *)addr < start + size);
2950}
2951
2952#ifdef CONFIG_KALLSYMS
2953/*
2954 * This ignores the intensely annoying "mapping symbols" found
2955 * in ARM ELF files: $a, $t and $d.
2956 */
2957static inline int is_arm_mapping_symbol(const char *str)
2958{
22a8bdeb 2959 return str[0] == '$' && strchr("atd", str[1])
1da177e4
LT
2960 && (str[2] == '\0' || str[2] == '.');
2961}
2962
2963static const char *get_ksymbol(struct module *mod,
2964 unsigned long addr,
2965 unsigned long *size,
2966 unsigned long *offset)
2967{
2968 unsigned int i, best = 0;
2969 unsigned long nextval;
2970
2971 /* At worse, next value is at end of module */
a06f6211 2972 if (within_module_init(addr, mod))
1da177e4 2973 nextval = (unsigned long)mod->module_init+mod->init_text_size;
22a8bdeb 2974 else
1da177e4
LT
2975 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2976
25985edc 2977 /* Scan for closest preceding symbol, and next symbol. (ELF
22a8bdeb 2978 starts real symbols at 1). */
1da177e4
LT
2979 for (i = 1; i < mod->num_symtab; i++) {
2980 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2981 continue;
2982
2983 /* We ignore unnamed symbols: they're uninformative
2984 * and inserted at a whim. */
2985 if (mod->symtab[i].st_value <= addr
2986 && mod->symtab[i].st_value > mod->symtab[best].st_value
2987 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2988 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2989 best = i;
2990 if (mod->symtab[i].st_value > addr
2991 && mod->symtab[i].st_value < nextval
2992 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2993 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2994 nextval = mod->symtab[i].st_value;
2995 }
2996
2997 if (!best)
2998 return NULL;
2999
ffb45122
AD
3000 if (size)
3001 *size = nextval - mod->symtab[best].st_value;
3002 if (offset)
3003 *offset = addr - mod->symtab[best].st_value;
1da177e4
LT
3004 return mod->strtab + mod->symtab[best].st_name;
3005}
3006
6dd06c9f
RR
3007/* For kallsyms to ask for address resolution. NULL means not found. Careful
3008 * not to lock to avoid deadlock on oopses, simply disable preemption. */
92dfc9dc 3009const char *module_address_lookup(unsigned long addr,
6dd06c9f
RR
3010 unsigned long *size,
3011 unsigned long *offset,
3012 char **modname,
3013 char *namebuf)
1da177e4
LT
3014{
3015 struct module *mod;
cb2a5205 3016 const char *ret = NULL;
1da177e4 3017
cb2a5205 3018 preempt_disable();
d72b3751 3019 list_for_each_entry_rcu(mod, &modules, list) {
a06f6211
MH
3020 if (within_module_init(addr, mod) ||
3021 within_module_core(addr, mod)) {
ffc50891
FBH
3022 if (modname)
3023 *modname = mod->name;
cb2a5205
RR
3024 ret = get_ksymbol(mod, addr, size, offset);
3025 break;
1da177e4
LT
3026 }
3027 }
6dd06c9f
RR
3028 /* Make a copy in here where it's safe */
3029 if (ret) {
3030 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3031 ret = namebuf;
3032 }
cb2a5205 3033 preempt_enable();
92dfc9dc 3034 return ret;
1da177e4
LT
3035}
3036
9d65cb4a
AD
3037int lookup_module_symbol_name(unsigned long addr, char *symname)
3038{
3039 struct module *mod;
3040
cb2a5205 3041 preempt_disable();
d72b3751 3042 list_for_each_entry_rcu(mod, &modules, list) {
a06f6211
MH
3043 if (within_module_init(addr, mod) ||
3044 within_module_core(addr, mod)) {
9d65cb4a
AD
3045 const char *sym;
3046
3047 sym = get_ksymbol(mod, addr, NULL, NULL);
3048 if (!sym)
3049 goto out;
9281acea 3050 strlcpy(symname, sym, KSYM_NAME_LEN);
cb2a5205 3051 preempt_enable();
9d65cb4a
AD
3052 return 0;
3053 }
3054 }
3055out:
cb2a5205 3056 preempt_enable();
9d65cb4a
AD
3057 return -ERANGE;
3058}
3059
a5c43dae
AD
3060int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3061 unsigned long *offset, char *modname, char *name)
3062{
3063 struct module *mod;
3064
cb2a5205 3065 preempt_disable();
d72b3751 3066 list_for_each_entry_rcu(mod, &modules, list) {
a06f6211
MH
3067 if (within_module_init(addr, mod) ||
3068 within_module_core(addr, mod)) {
a5c43dae
AD
3069 const char *sym;
3070
3071 sym = get_ksymbol(mod, addr, size, offset);
3072 if (!sym)
3073 goto out;
3074 if (modname)
9281acea 3075 strlcpy(modname, mod->name, MODULE_NAME_LEN);
a5c43dae 3076 if (name)
9281acea 3077 strlcpy(name, sym, KSYM_NAME_LEN);
cb2a5205 3078 preempt_enable();
a5c43dae
AD
3079 return 0;
3080 }
3081 }
3082out:
cb2a5205 3083 preempt_enable();
a5c43dae
AD
3084 return -ERANGE;
3085}
3086
ea07890a
AD
3087int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3088 char *name, char *module_name, int *exported)
1da177e4
LT
3089{
3090 struct module *mod;
3091
cb2a5205 3092 preempt_disable();
d72b3751 3093 list_for_each_entry_rcu(mod, &modules, list) {
1da177e4
LT
3094 if (symnum < mod->num_symtab) {
3095 *value = mod->symtab[symnum].st_value;
3096 *type = mod->symtab[symnum].st_info;
098c5eea 3097 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
9281acea
TH
3098 KSYM_NAME_LEN);
3099 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
ca4787b7 3100 *exported = is_exported(name, *value, mod);
cb2a5205 3101 preempt_enable();
ea07890a 3102 return 0;
1da177e4
LT
3103 }
3104 symnum -= mod->num_symtab;
3105 }
cb2a5205 3106 preempt_enable();
ea07890a 3107 return -ERANGE;
1da177e4
LT
3108}
3109
3110static unsigned long mod_find_symname(struct module *mod, const char *name)
3111{
3112 unsigned int i;
3113
3114 for (i = 0; i < mod->num_symtab; i++)
54e8ce46
KO
3115 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3116 mod->symtab[i].st_info != 'U')
1da177e4
LT
3117 return mod->symtab[i].st_value;
3118 return 0;
3119}
3120
3121/* Look for this name: can be of form module:name. */
3122unsigned long module_kallsyms_lookup_name(const char *name)
3123{
3124 struct module *mod;
3125 char *colon;
3126 unsigned long ret = 0;
3127
3128 /* Don't lock: we're in enough trouble already. */
cb2a5205 3129 preempt_disable();
1da177e4
LT
3130 if ((colon = strchr(name, ':')) != NULL) {
3131 *colon = '\0';
3132 if ((mod = find_module(name)) != NULL)
3133 ret = mod_find_symname(mod, colon+1);
3134 *colon = ':';
3135 } else {
d72b3751 3136 list_for_each_entry_rcu(mod, &modules, list)
1da177e4
LT
3137 if ((ret = mod_find_symname(mod, name)) != 0)
3138 break;
3139 }
cb2a5205 3140 preempt_enable();
1da177e4
LT
3141 return ret;
3142}
75a66614
AK
3143
3144int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3145 struct module *, unsigned long),
3146 void *data)
3147{
3148 struct module *mod;
3149 unsigned int i;
3150 int ret;
3151
3152 list_for_each_entry(mod, &modules, list) {
3153 for (i = 0; i < mod->num_symtab; i++) {
3154 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3155 mod, mod->symtab[i].st_value);
3156 if (ret != 0)
3157 return ret;
3158 }
3159 }
3160 return 0;
3161}
1da177e4
LT
3162#endif /* CONFIG_KALLSYMS */
3163
21aa9280 3164static char *module_flags(struct module *mod, char *buf)
fa3ba2e8
FM
3165{
3166 int bx = 0;
3167
21aa9280
AV
3168 if (mod->taints ||
3169 mod->state == MODULE_STATE_GOING ||
3170 mod->state == MODULE_STATE_COMING) {
fa3ba2e8 3171 buf[bx++] = '(';
25ddbb18 3172 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
fa3ba2e8 3173 buf[bx++] = 'P';
25ddbb18 3174 if (mod->taints & (1 << TAINT_FORCED_MODULE))
fa3ba2e8 3175 buf[bx++] = 'F';
26e9a397 3176 if (mod->taints & (1 << TAINT_CRAP))
061b1bd3 3177 buf[bx++] = 'C';
fa3ba2e8
FM
3178 /*
3179 * TAINT_FORCED_RMMOD: could be added.
3180 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3181 * apply to modules.
3182 */
21aa9280
AV
3183
3184 /* Show a - for module-is-being-unloaded */
3185 if (mod->state == MODULE_STATE_GOING)
3186 buf[bx++] = '-';
3187 /* Show a + for module-is-being-loaded */
3188 if (mod->state == MODULE_STATE_COMING)
3189 buf[bx++] = '+';
fa3ba2e8
FM
3190 buf[bx++] = ')';
3191 }
3192 buf[bx] = '\0';
3193
3194 return buf;
3195}
3196
3b5d5c6b
AD
3197#ifdef CONFIG_PROC_FS
3198/* Called by the /proc file system to return a list of modules. */
3199static void *m_start(struct seq_file *m, loff_t *pos)
3200{
3201 mutex_lock(&module_mutex);
3202 return seq_list_start(&modules, *pos);
3203}
3204
3205static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3206{
3207 return seq_list_next(p, &modules, pos);
3208}
3209
3210static void m_stop(struct seq_file *m, void *p)
3211{
3212 mutex_unlock(&module_mutex);
3213}
3214
1da177e4
LT
3215static int m_show(struct seq_file *m, void *p)
3216{
3217 struct module *mod = list_entry(p, struct module, list);
fa3ba2e8
FM
3218 char buf[8];
3219
2f0f2a33 3220 seq_printf(m, "%s %u",
1da177e4
LT
3221 mod->name, mod->init_size + mod->core_size);
3222 print_unload_info(m, mod);
3223
3224 /* Informative for users. */
3225 seq_printf(m, " %s",
3226 mod->state == MODULE_STATE_GOING ? "Unloading":
3227 mod->state == MODULE_STATE_COMING ? "Loading":
3228 "Live");
3229 /* Used by oprofile and other similar tools. */
9f36e2c4 3230 seq_printf(m, " 0x%pK", mod->module_core);
1da177e4 3231
fa3ba2e8
FM
3232 /* Taints info */
3233 if (mod->taints)
21aa9280 3234 seq_printf(m, " %s", module_flags(mod, buf));
fa3ba2e8 3235
1da177e4
LT
3236 seq_printf(m, "\n");
3237 return 0;
3238}
3239
3240/* Format: modulename size refcount deps address
3241
3242 Where refcount is a number or -, and deps is a comma-separated list
3243 of depends or -.
3244*/
3b5d5c6b 3245static const struct seq_operations modules_op = {
1da177e4
LT
3246 .start = m_start,
3247 .next = m_next,
3248 .stop = m_stop,
3249 .show = m_show
3250};
3251
3b5d5c6b
AD
3252static int modules_open(struct inode *inode, struct file *file)
3253{
3254 return seq_open(file, &modules_op);
3255}
3256
3257static const struct file_operations proc_modules_operations = {
3258 .open = modules_open,
3259 .read = seq_read,
3260 .llseek = seq_lseek,
3261 .release = seq_release,
3262};
3263
3264static int __init proc_modules_init(void)
3265{
3266 proc_create("modules", 0, NULL, &proc_modules_operations);
3267 return 0;
3268}
3269module_init(proc_modules_init);
3270#endif
3271
1da177e4
LT
3272/* Given an address, look for it in the module exception tables. */
3273const struct exception_table_entry *search_module_extables(unsigned long addr)
3274{
1da177e4
LT
3275 const struct exception_table_entry *e = NULL;
3276 struct module *mod;
3277
24da1cbf 3278 preempt_disable();
d72b3751 3279 list_for_each_entry_rcu(mod, &modules, list) {
1da177e4
LT
3280 if (mod->num_exentries == 0)
3281 continue;
22a8bdeb 3282
1da177e4
LT
3283 e = search_extable(mod->extable,
3284 mod->extable + mod->num_exentries - 1,
3285 addr);
3286 if (e)
3287 break;
3288 }
24da1cbf 3289 preempt_enable();
1da177e4
LT
3290
3291 /* Now, if we found one, we are running inside it now, hence
22a8bdeb 3292 we cannot unload the module, hence no refcnt needed. */
1da177e4
LT
3293 return e;
3294}
3295
4d435f9d 3296/*
e610499e
RR
3297 * is_module_address - is this address inside a module?
3298 * @addr: the address to check.
3299 *
3300 * See is_module_text_address() if you simply want to see if the address
3301 * is code (not data).
4d435f9d 3302 */
e610499e 3303bool is_module_address(unsigned long addr)
4d435f9d 3304{
e610499e 3305 bool ret;
4d435f9d 3306
24da1cbf 3307 preempt_disable();
e610499e 3308 ret = __module_address(addr) != NULL;
24da1cbf 3309 preempt_enable();
4d435f9d 3310
e610499e 3311 return ret;
4d435f9d
IM
3312}
3313
e610499e
RR
3314/*
3315 * __module_address - get the module which contains an address.
3316 * @addr: the address.
3317 *
3318 * Must be called with preempt disabled or module mutex held so that
3319 * module doesn't get freed during this.
3320 */
714f83d5 3321struct module *__module_address(unsigned long addr)
1da177e4
LT
3322{
3323 struct module *mod;
3324
3a642e99
RR
3325 if (addr < module_addr_min || addr > module_addr_max)
3326 return NULL;
3327
d72b3751 3328 list_for_each_entry_rcu(mod, &modules, list)
e610499e
RR
3329 if (within_module_core(addr, mod)
3330 || within_module_init(addr, mod))
1da177e4
LT
3331 return mod;
3332 return NULL;
3333}
c6b37801 3334EXPORT_SYMBOL_GPL(__module_address);
1da177e4 3335
e610499e
RR
3336/*
3337 * is_module_text_address - is this address inside module code?
3338 * @addr: the address to check.
3339 *
3340 * See is_module_address() if you simply want to see if the address is
3341 * anywhere in a module. See kernel_text_address() for testing if an
3342 * address corresponds to kernel or module code.
3343 */
3344bool is_module_text_address(unsigned long addr)
3345{
3346 bool ret;
3347
3348 preempt_disable();
3349 ret = __module_text_address(addr) != NULL;
3350 preempt_enable();
3351
3352 return ret;
3353}
3354
3355/*
3356 * __module_text_address - get the module whose code contains an address.
3357 * @addr: the address.
3358 *
3359 * Must be called with preempt disabled or module mutex held so that
3360 * module doesn't get freed during this.
3361 */
3362struct module *__module_text_address(unsigned long addr)
3363{
3364 struct module *mod = __module_address(addr);
3365 if (mod) {
3366 /* Make sure it's within the text section. */
3367 if (!within(addr, mod->module_init, mod->init_text_size)
3368 && !within(addr, mod->module_core, mod->core_text_size))
3369 mod = NULL;
3370 }
3371 return mod;
3372}
c6b37801 3373EXPORT_SYMBOL_GPL(__module_text_address);
e610499e 3374
1da177e4
LT
3375/* Don't grab lock, we're oopsing. */
3376void print_modules(void)
3377{
3378 struct module *mod;
2bc2d61a 3379 char buf[8];
1da177e4 3380
b231125a 3381 printk(KERN_DEFAULT "Modules linked in:");
d72b3751
AK
3382 /* Most callers should already have preempt disabled, but make sure */
3383 preempt_disable();
3384 list_for_each_entry_rcu(mod, &modules, list)
21aa9280 3385 printk(" %s%s", mod->name, module_flags(mod, buf));
d72b3751 3386 preempt_enable();
e14af7ee
AV
3387 if (last_unloaded_module[0])
3388 printk(" [last unloaded: %s]", last_unloaded_module);
1da177e4
LT
3389 printk("\n");
3390}
3391
1da177e4 3392#ifdef CONFIG_MODVERSIONS
8c8ef42a
RR
3393/* Generate the signature for all relevant module structures here.
3394 * If these change, we don't want to try to parse the module. */
3395void module_layout(struct module *mod,
3396 struct modversion_info *ver,
3397 struct kernel_param *kp,
3398 struct kernel_symbol *ks,
65498646 3399 struct tracepoint * const *tp)
8c8ef42a
RR
3400{
3401}
3402EXPORT_SYMBOL(module_layout);
1da177e4 3403#endif
8256e47c 3404
97e1c18e
MD
3405#ifdef CONFIG_TRACEPOINTS
3406void module_update_tracepoints(void)
3407{
3408 struct module *mod;
3409
3410 mutex_lock(&module_mutex);
3411 list_for_each_entry(mod, &modules, list)
3412 if (!mod->taints)
65498646
MD
3413 tracepoint_update_probe_range(mod->tracepoints_ptrs,
3414 mod->tracepoints_ptrs + mod->num_tracepoints);
97e1c18e
MD
3415 mutex_unlock(&module_mutex);
3416}
3417
3418/*
3419 * Returns 0 if current not found.
3420 * Returns 1 if current found.
3421 */
3422int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3423{
3424 struct module *iter_mod;
3425 int found = 0;
3426
3427 mutex_lock(&module_mutex);
3428 list_for_each_entry(iter_mod, &modules, list) {
3429 if (!iter_mod->taints) {
3430 /*
3431 * Sorted module list
3432 */
3433 if (iter_mod < iter->module)
3434 continue;
3435 else if (iter_mod > iter->module)
3436 iter->tracepoint = NULL;
3437 found = tracepoint_get_iter_range(&iter->tracepoint,
65498646
MD
3438 iter_mod->tracepoints_ptrs,
3439 iter_mod->tracepoints_ptrs
97e1c18e
MD
3440 + iter_mod->num_tracepoints);
3441 if (found) {
3442 iter->module = iter_mod;
3443 break;
3444 }
3445 }
3446 }
3447 mutex_unlock(&module_mutex);
3448 return found;
3449}
3450#endif