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