fnctl: f_modown should call write_lock_irqsave/restore
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / module.c
blobcda4d7667d70fceb62858a96d1f1e0eb42b6471f
1 /*
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
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.
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.
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
19 #include <linux/module.h>
20 #include <linux/moduleloader.h>
21 #include <linux/ftrace_event.h>
22 #include <linux/init.h>
23 #include <linux/kallsyms.h>
24 #include <linux/fs.h>
25 #include <linux/sysfs.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/elf.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/syscalls.h>
33 #include <linux/fcntl.h>
34 #include <linux/rcupdate.h>
35 #include <linux/capability.h>
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>
42 #include <linux/sched.h>
43 #include <linux/stop_machine.h>
44 #include <linux/device.h>
45 #include <linux/string.h>
46 #include <linux/mutex.h>
47 #include <linux/rculist.h>
48 #include <asm/uaccess.h>
49 #include <asm/cacheflush.h>
50 #include <linux/license.h>
51 #include <asm/sections.h>
52 #include <linux/tracepoint.h>
53 #include <linux/ftrace.h>
54 #include <linux/async.h>
55 #include <linux/percpu.h>
56 #include <linux/kmemleak.h>
58 #if 0
59 #define DEBUGP printk
60 #else
61 #define DEBUGP(fmt , a...)
62 #endif
64 #ifndef ARCH_SHF_SMALL
65 #define ARCH_SHF_SMALL 0
66 #endif
68 /* If this is set, the section belongs in the init part of the module */
69 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
71 /* List of modules, protected by module_mutex or preempt_disable
72 * (delete uses stop_machine/add uses RCU list operations). */
73 DEFINE_MUTEX(module_mutex);
74 EXPORT_SYMBOL_GPL(module_mutex);
75 static LIST_HEAD(modules);
77 /* Block module loading/unloading? */
78 int modules_disabled = 0;
80 /* Waiting for a module to finish initializing? */
81 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
83 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
85 /* Bounds of module allocation, for speeding __module_address */
86 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
88 int register_module_notifier(struct notifier_block * nb)
90 return blocking_notifier_chain_register(&module_notify_list, nb);
92 EXPORT_SYMBOL(register_module_notifier);
94 int unregister_module_notifier(struct notifier_block * nb)
96 return blocking_notifier_chain_unregister(&module_notify_list, nb);
98 EXPORT_SYMBOL(unregister_module_notifier);
100 /* We require a truly strong try_module_get(): 0 means failure due to
101 ongoing or failed initialization etc. */
102 static inline int strong_try_module_get(struct module *mod)
104 if (mod && mod->state == MODULE_STATE_COMING)
105 return -EBUSY;
106 if (try_module_get(mod))
107 return 0;
108 else
109 return -ENOENT;
112 static inline void add_taint_module(struct module *mod, unsigned flag)
114 add_taint(flag);
115 mod->taints |= (1U << flag);
119 * A thread that wants to hold a reference to a module only while it
120 * is running can call this to safely exit. nfsd and lockd use this.
122 void __module_put_and_exit(struct module *mod, long code)
124 module_put(mod);
125 do_exit(code);
127 EXPORT_SYMBOL(__module_put_and_exit);
129 /* Find a module section: 0 means not found. */
130 static unsigned int find_sec(Elf_Ehdr *hdr,
131 Elf_Shdr *sechdrs,
132 const char *secstrings,
133 const char *name)
135 unsigned int i;
137 for (i = 1; i < hdr->e_shnum; i++)
138 /* Alloc bit cleared means "ignore it." */
139 if ((sechdrs[i].sh_flags & SHF_ALLOC)
140 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
141 return i;
142 return 0;
145 /* Find a module section, or NULL. */
146 static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
147 const char *secstrings, const char *name)
149 /* Section 0 has sh_addr 0. */
150 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
153 /* Find a module section, or NULL. Fill in number of "objects" in section. */
154 static void *section_objs(Elf_Ehdr *hdr,
155 Elf_Shdr *sechdrs,
156 const char *secstrings,
157 const char *name,
158 size_t object_size,
159 unsigned int *num)
161 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
163 /* Section 0 has sh_addr 0 and sh_size 0. */
164 *num = sechdrs[sec].sh_size / object_size;
165 return (void *)sechdrs[sec].sh_addr;
168 /* Provided by the linker */
169 extern const struct kernel_symbol __start___ksymtab[];
170 extern const struct kernel_symbol __stop___ksymtab[];
171 extern const struct kernel_symbol __start___ksymtab_gpl[];
172 extern const struct kernel_symbol __stop___ksymtab_gpl[];
173 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
174 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
175 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
176 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
177 extern const unsigned long __start___kcrctab[];
178 extern const unsigned long __start___kcrctab_gpl[];
179 extern const unsigned long __start___kcrctab_gpl_future[];
180 #ifdef CONFIG_UNUSED_SYMBOLS
181 extern const struct kernel_symbol __start___ksymtab_unused[];
182 extern const struct kernel_symbol __stop___ksymtab_unused[];
183 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
184 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
185 extern const unsigned long __start___kcrctab_unused[];
186 extern const unsigned long __start___kcrctab_unused_gpl[];
187 #endif
189 #ifndef CONFIG_MODVERSIONS
190 #define symversion(base, idx) NULL
191 #else
192 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
193 #endif
195 static bool each_symbol_in_section(const struct symsearch *arr,
196 unsigned int arrsize,
197 struct module *owner,
198 bool (*fn)(const struct symsearch *syms,
199 struct module *owner,
200 unsigned int symnum, void *data),
201 void *data)
203 unsigned int i, j;
205 for (j = 0; j < arrsize; j++) {
206 for (i = 0; i < arr[j].stop - arr[j].start; i++)
207 if (fn(&arr[j], owner, i, data))
208 return true;
211 return false;
214 /* Returns true as soon as fn returns true, otherwise false. */
215 bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
216 unsigned int symnum, void *data), void *data)
218 struct module *mod;
219 const struct symsearch arr[] = {
220 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
221 NOT_GPL_ONLY, false },
222 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
223 __start___kcrctab_gpl,
224 GPL_ONLY, false },
225 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
226 __start___kcrctab_gpl_future,
227 WILL_BE_GPL_ONLY, false },
228 #ifdef CONFIG_UNUSED_SYMBOLS
229 { __start___ksymtab_unused, __stop___ksymtab_unused,
230 __start___kcrctab_unused,
231 NOT_GPL_ONLY, true },
232 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
233 __start___kcrctab_unused_gpl,
234 GPL_ONLY, true },
235 #endif
238 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
239 return true;
241 list_for_each_entry_rcu(mod, &modules, list) {
242 struct symsearch arr[] = {
243 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
244 NOT_GPL_ONLY, false },
245 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
246 mod->gpl_crcs,
247 GPL_ONLY, false },
248 { mod->gpl_future_syms,
249 mod->gpl_future_syms + mod->num_gpl_future_syms,
250 mod->gpl_future_crcs,
251 WILL_BE_GPL_ONLY, false },
252 #ifdef CONFIG_UNUSED_SYMBOLS
253 { mod->unused_syms,
254 mod->unused_syms + mod->num_unused_syms,
255 mod->unused_crcs,
256 NOT_GPL_ONLY, true },
257 { mod->unused_gpl_syms,
258 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
259 mod->unused_gpl_crcs,
260 GPL_ONLY, true },
261 #endif
264 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
265 return true;
267 return false;
269 EXPORT_SYMBOL_GPL(each_symbol);
271 struct find_symbol_arg {
272 /* Input */
273 const char *name;
274 bool gplok;
275 bool warn;
277 /* Output */
278 struct module *owner;
279 const unsigned long *crc;
280 const struct kernel_symbol *sym;
283 static bool find_symbol_in_section(const struct symsearch *syms,
284 struct module *owner,
285 unsigned int symnum, void *data)
287 struct find_symbol_arg *fsa = data;
289 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
290 return false;
292 if (!fsa->gplok) {
293 if (syms->licence == GPL_ONLY)
294 return false;
295 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
296 printk(KERN_WARNING "Symbol %s is being used "
297 "by a non-GPL module, which will not "
298 "be allowed in the future\n", fsa->name);
299 printk(KERN_WARNING "Please see the file "
300 "Documentation/feature-removal-schedule.txt "
301 "in the kernel source tree for more details.\n");
305 #ifdef CONFIG_UNUSED_SYMBOLS
306 if (syms->unused && fsa->warn) {
307 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
308 "however this module is using it.\n", fsa->name);
309 printk(KERN_WARNING
310 "This symbol will go away in the future.\n");
311 printk(KERN_WARNING
312 "Please evalute if this is the right api to use and if "
313 "it really is, submit a report the linux kernel "
314 "mailinglist together with submitting your code for "
315 "inclusion.\n");
317 #endif
319 fsa->owner = owner;
320 fsa->crc = symversion(syms->crcs, symnum);
321 fsa->sym = &syms->start[symnum];
322 return true;
325 /* Find a symbol and return it, along with, (optional) crc and
326 * (optional) module which owns it */
327 const struct kernel_symbol *find_symbol(const char *name,
328 struct module **owner,
329 const unsigned long **crc,
330 bool gplok,
331 bool warn)
333 struct find_symbol_arg fsa;
335 fsa.name = name;
336 fsa.gplok = gplok;
337 fsa.warn = warn;
339 if (each_symbol(find_symbol_in_section, &fsa)) {
340 if (owner)
341 *owner = fsa.owner;
342 if (crc)
343 *crc = fsa.crc;
344 return fsa.sym;
347 DEBUGP("Failed to find symbol %s\n", name);
348 return NULL;
350 EXPORT_SYMBOL_GPL(find_symbol);
352 /* Search for module by name: must hold module_mutex. */
353 struct module *find_module(const char *name)
355 struct module *mod;
357 list_for_each_entry(mod, &modules, list) {
358 if (strcmp(mod->name, name) == 0)
359 return mod;
361 return NULL;
363 EXPORT_SYMBOL_GPL(find_module);
365 #ifdef CONFIG_SMP
367 #ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
369 static void *percpu_modalloc(unsigned long size, unsigned long align,
370 const char *name)
372 void *ptr;
374 if (align > PAGE_SIZE) {
375 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
376 name, align, PAGE_SIZE);
377 align = PAGE_SIZE;
380 ptr = __alloc_reserved_percpu(size, align);
381 if (!ptr)
382 printk(KERN_WARNING
383 "Could not allocate %lu bytes percpu data\n", size);
384 return ptr;
387 static void percpu_modfree(void *freeme)
389 free_percpu(freeme);
392 #else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
394 /* Number of blocks used and allocated. */
395 static unsigned int pcpu_num_used, pcpu_num_allocated;
396 /* Size of each block. -ve means used. */
397 static int *pcpu_size;
399 static int split_block(unsigned int i, unsigned short size)
401 /* Reallocation required? */
402 if (pcpu_num_used + 1 > pcpu_num_allocated) {
403 int *new;
405 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
406 GFP_KERNEL);
407 if (!new)
408 return 0;
410 pcpu_num_allocated *= 2;
411 pcpu_size = new;
414 /* Insert a new subblock */
415 memmove(&pcpu_size[i+1], &pcpu_size[i],
416 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
417 pcpu_num_used++;
419 pcpu_size[i+1] -= size;
420 pcpu_size[i] = size;
421 return 1;
424 static inline unsigned int block_size(int val)
426 if (val < 0)
427 return -val;
428 return val;
431 static void *percpu_modalloc(unsigned long size, unsigned long align,
432 const char *name)
434 unsigned long extra;
435 unsigned int i;
436 void *ptr;
437 int cpu;
439 if (align > PAGE_SIZE) {
440 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
441 name, align, PAGE_SIZE);
442 align = PAGE_SIZE;
445 ptr = __per_cpu_start;
446 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
447 /* Extra for alignment requirement. */
448 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
449 BUG_ON(i == 0 && extra != 0);
451 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
452 continue;
454 /* Transfer extra to previous block. */
455 if (pcpu_size[i-1] < 0)
456 pcpu_size[i-1] -= extra;
457 else
458 pcpu_size[i-1] += extra;
459 pcpu_size[i] -= extra;
460 ptr += extra;
462 /* Split block if warranted */
463 if (pcpu_size[i] - size > sizeof(unsigned long))
464 if (!split_block(i, size))
465 return NULL;
467 /* add the per-cpu scanning areas */
468 for_each_possible_cpu(cpu)
469 kmemleak_alloc(ptr + per_cpu_offset(cpu), size, 0,
470 GFP_KERNEL);
472 /* Mark allocated */
473 pcpu_size[i] = -pcpu_size[i];
474 return ptr;
477 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
478 size);
479 return NULL;
482 static void percpu_modfree(void *freeme)
484 unsigned int i;
485 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
486 int cpu;
488 /* First entry is core kernel percpu data. */
489 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
490 if (ptr == freeme) {
491 pcpu_size[i] = -pcpu_size[i];
492 goto free;
495 BUG();
497 free:
498 /* remove the per-cpu scanning areas */
499 for_each_possible_cpu(cpu)
500 kmemleak_free(freeme + per_cpu_offset(cpu));
502 /* Merge with previous? */
503 if (pcpu_size[i-1] >= 0) {
504 pcpu_size[i-1] += pcpu_size[i];
505 pcpu_num_used--;
506 memmove(&pcpu_size[i], &pcpu_size[i+1],
507 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
508 i--;
510 /* Merge with next? */
511 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
512 pcpu_size[i] += pcpu_size[i+1];
513 pcpu_num_used--;
514 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
515 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
519 static int percpu_modinit(void)
521 pcpu_num_used = 2;
522 pcpu_num_allocated = 2;
523 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
524 GFP_KERNEL);
525 /* Static in-kernel percpu data (used). */
526 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
527 /* Free room. */
528 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
529 if (pcpu_size[1] < 0) {
530 printk(KERN_ERR "No per-cpu room for modules.\n");
531 pcpu_num_used = 1;
534 return 0;
536 __initcall(percpu_modinit);
538 #endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
540 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
541 Elf_Shdr *sechdrs,
542 const char *secstrings)
544 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
547 static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
549 int cpu;
551 for_each_possible_cpu(cpu)
552 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
555 #else /* ... !CONFIG_SMP */
557 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
558 const char *name)
560 return NULL;
562 static inline void percpu_modfree(void *pcpuptr)
564 BUG();
566 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
567 Elf_Shdr *sechdrs,
568 const char *secstrings)
570 return 0;
572 static inline void percpu_modcopy(void *pcpudst, const void *src,
573 unsigned long size)
575 /* pcpusec should be 0, and size of that section should be 0. */
576 BUG_ON(size != 0);
579 #endif /* CONFIG_SMP */
581 #define MODINFO_ATTR(field) \
582 static void setup_modinfo_##field(struct module *mod, const char *s) \
584 mod->field = kstrdup(s, GFP_KERNEL); \
586 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
587 struct module *mod, char *buffer) \
589 return sprintf(buffer, "%s\n", mod->field); \
591 static int modinfo_##field##_exists(struct module *mod) \
593 return mod->field != NULL; \
595 static void free_modinfo_##field(struct module *mod) \
597 kfree(mod->field); \
598 mod->field = NULL; \
600 static struct module_attribute modinfo_##field = { \
601 .attr = { .name = __stringify(field), .mode = 0444 }, \
602 .show = show_modinfo_##field, \
603 .setup = setup_modinfo_##field, \
604 .test = modinfo_##field##_exists, \
605 .free = free_modinfo_##field, \
608 MODINFO_ATTR(version);
609 MODINFO_ATTR(srcversion);
611 static char last_unloaded_module[MODULE_NAME_LEN+1];
613 #ifdef CONFIG_MODULE_UNLOAD
614 /* Init the unload section of the module. */
615 static void module_unload_init(struct module *mod)
617 int cpu;
619 INIT_LIST_HEAD(&mod->modules_which_use_me);
620 for_each_possible_cpu(cpu)
621 local_set(__module_ref_addr(mod, cpu), 0);
622 /* Hold reference count during initialization. */
623 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
624 /* Backwards compatibility macros put refcount during init. */
625 mod->waiter = current;
628 /* modules using other modules */
629 struct module_use
631 struct list_head list;
632 struct module *module_which_uses;
635 /* Does a already use b? */
636 static int already_uses(struct module *a, struct module *b)
638 struct module_use *use;
640 list_for_each_entry(use, &b->modules_which_use_me, list) {
641 if (use->module_which_uses == a) {
642 DEBUGP("%s uses %s!\n", a->name, b->name);
643 return 1;
646 DEBUGP("%s does not use %s!\n", a->name, b->name);
647 return 0;
650 /* Module a uses b */
651 int use_module(struct module *a, struct module *b)
653 struct module_use *use;
654 int no_warn, err;
656 if (b == NULL || already_uses(a, b)) return 1;
658 /* If we're interrupted or time out, we fail. */
659 if (wait_event_interruptible_timeout(
660 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
661 30 * HZ) <= 0) {
662 printk("%s: gave up waiting for init of module %s.\n",
663 a->name, b->name);
664 return 0;
667 /* If strong_try_module_get() returned a different error, we fail. */
668 if (err)
669 return 0;
671 DEBUGP("Allocating new usage for %s.\n", a->name);
672 use = kmalloc(sizeof(*use), GFP_ATOMIC);
673 if (!use) {
674 printk("%s: out of memory loading\n", a->name);
675 module_put(b);
676 return 0;
679 use->module_which_uses = a;
680 list_add(&use->list, &b->modules_which_use_me);
681 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
682 return 1;
684 EXPORT_SYMBOL_GPL(use_module);
686 /* Clear the unload stuff of the module. */
687 static void module_unload_free(struct module *mod)
689 struct module *i;
691 list_for_each_entry(i, &modules, list) {
692 struct module_use *use;
694 list_for_each_entry(use, &i->modules_which_use_me, list) {
695 if (use->module_which_uses == mod) {
696 DEBUGP("%s unusing %s\n", mod->name, i->name);
697 module_put(i);
698 list_del(&use->list);
699 kfree(use);
700 sysfs_remove_link(i->holders_dir, mod->name);
701 /* There can be at most one match. */
702 break;
708 #ifdef CONFIG_MODULE_FORCE_UNLOAD
709 static inline int try_force_unload(unsigned int flags)
711 int ret = (flags & O_TRUNC);
712 if (ret)
713 add_taint(TAINT_FORCED_RMMOD);
714 return ret;
716 #else
717 static inline int try_force_unload(unsigned int flags)
719 return 0;
721 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
723 struct stopref
725 struct module *mod;
726 int flags;
727 int *forced;
730 /* Whole machine is stopped with interrupts off when this runs. */
731 static int __try_stop_module(void *_sref)
733 struct stopref *sref = _sref;
735 /* If it's not unused, quit unless we're forcing. */
736 if (module_refcount(sref->mod) != 0) {
737 if (!(*sref->forced = try_force_unload(sref->flags)))
738 return -EWOULDBLOCK;
741 /* Mark it as dying. */
742 sref->mod->state = MODULE_STATE_GOING;
743 return 0;
746 static int try_stop_module(struct module *mod, int flags, int *forced)
748 if (flags & O_NONBLOCK) {
749 struct stopref sref = { mod, flags, forced };
751 return stop_machine(__try_stop_module, &sref, NULL);
752 } else {
753 /* We don't need to stop the machine for this. */
754 mod->state = MODULE_STATE_GOING;
755 synchronize_sched();
756 return 0;
760 unsigned int module_refcount(struct module *mod)
762 unsigned int total = 0;
763 int cpu;
765 for_each_possible_cpu(cpu)
766 total += local_read(__module_ref_addr(mod, cpu));
767 return total;
769 EXPORT_SYMBOL(module_refcount);
771 /* This exists whether we can unload or not */
772 static void free_module(struct module *mod);
774 static void wait_for_zero_refcount(struct module *mod)
776 /* Since we might sleep for some time, release the mutex first */
777 mutex_unlock(&module_mutex);
778 for (;;) {
779 DEBUGP("Looking at refcount...\n");
780 set_current_state(TASK_UNINTERRUPTIBLE);
781 if (module_refcount(mod) == 0)
782 break;
783 schedule();
785 current->state = TASK_RUNNING;
786 mutex_lock(&module_mutex);
789 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
790 unsigned int, flags)
792 struct module *mod;
793 char name[MODULE_NAME_LEN];
794 int ret, forced = 0;
796 if (!capable(CAP_SYS_MODULE) || modules_disabled)
797 return -EPERM;
799 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
800 return -EFAULT;
801 name[MODULE_NAME_LEN-1] = '\0';
803 /* Create stop_machine threads since free_module relies on
804 * a non-failing stop_machine call. */
805 ret = stop_machine_create();
806 if (ret)
807 return ret;
809 if (mutex_lock_interruptible(&module_mutex) != 0) {
810 ret = -EINTR;
811 goto out_stop;
814 mod = find_module(name);
815 if (!mod) {
816 ret = -ENOENT;
817 goto out;
820 if (!list_empty(&mod->modules_which_use_me)) {
821 /* Other modules depend on us: get rid of them first. */
822 ret = -EWOULDBLOCK;
823 goto out;
826 /* Doing init or already dying? */
827 if (mod->state != MODULE_STATE_LIVE) {
828 /* FIXME: if (force), slam module count and wake up
829 waiter --RR */
830 DEBUGP("%s already dying\n", mod->name);
831 ret = -EBUSY;
832 goto out;
835 /* If it has an init func, it must have an exit func to unload */
836 if (mod->init && !mod->exit) {
837 forced = try_force_unload(flags);
838 if (!forced) {
839 /* This module can't be removed */
840 ret = -EBUSY;
841 goto out;
845 /* Set this up before setting mod->state */
846 mod->waiter = current;
848 /* Stop the machine so refcounts can't move and disable module. */
849 ret = try_stop_module(mod, flags, &forced);
850 if (ret != 0)
851 goto out;
853 /* Never wait if forced. */
854 if (!forced && module_refcount(mod) != 0)
855 wait_for_zero_refcount(mod);
857 mutex_unlock(&module_mutex);
858 /* Final destruction now noone is using it. */
859 if (mod->exit != NULL)
860 mod->exit();
861 blocking_notifier_call_chain(&module_notify_list,
862 MODULE_STATE_GOING, mod);
863 async_synchronize_full();
864 mutex_lock(&module_mutex);
865 /* Store the name of the last unloaded module for diagnostic purposes */
866 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
867 ddebug_remove_module(mod->name);
868 free_module(mod);
870 out:
871 mutex_unlock(&module_mutex);
872 out_stop:
873 stop_machine_destroy();
874 return ret;
877 static inline void print_unload_info(struct seq_file *m, struct module *mod)
879 struct module_use *use;
880 int printed_something = 0;
882 seq_printf(m, " %u ", module_refcount(mod));
884 /* Always include a trailing , so userspace can differentiate
885 between this and the old multi-field proc format. */
886 list_for_each_entry(use, &mod->modules_which_use_me, list) {
887 printed_something = 1;
888 seq_printf(m, "%s,", use->module_which_uses->name);
891 if (mod->init != NULL && mod->exit == NULL) {
892 printed_something = 1;
893 seq_printf(m, "[permanent],");
896 if (!printed_something)
897 seq_printf(m, "-");
900 void __symbol_put(const char *symbol)
902 struct module *owner;
904 preempt_disable();
905 if (!find_symbol(symbol, &owner, NULL, true, false))
906 BUG();
907 module_put(owner);
908 preempt_enable();
910 EXPORT_SYMBOL(__symbol_put);
912 /* Note this assumes addr is a function, which it currently always is. */
913 void symbol_put_addr(void *addr)
915 struct module *modaddr;
916 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
918 if (core_kernel_text(a))
919 return;
921 /* module_text_address is safe here: we're supposed to have reference
922 * to module from symbol_get, so it can't go away. */
923 modaddr = __module_text_address(a);
924 BUG_ON(!modaddr);
925 module_put(modaddr);
927 EXPORT_SYMBOL_GPL(symbol_put_addr);
929 static ssize_t show_refcnt(struct module_attribute *mattr,
930 struct module *mod, char *buffer)
932 return sprintf(buffer, "%u\n", module_refcount(mod));
935 static struct module_attribute refcnt = {
936 .attr = { .name = "refcnt", .mode = 0444 },
937 .show = show_refcnt,
940 void module_put(struct module *module)
942 if (module) {
943 unsigned int cpu = get_cpu();
944 local_dec(__module_ref_addr(module, cpu));
945 /* Maybe they're waiting for us to drop reference? */
946 if (unlikely(!module_is_live(module)))
947 wake_up_process(module->waiter);
948 put_cpu();
951 EXPORT_SYMBOL(module_put);
953 #else /* !CONFIG_MODULE_UNLOAD */
954 static inline void print_unload_info(struct seq_file *m, struct module *mod)
956 /* We don't know the usage count, or what modules are using. */
957 seq_printf(m, " - -");
960 static inline void module_unload_free(struct module *mod)
964 int use_module(struct module *a, struct module *b)
966 return strong_try_module_get(b) == 0;
968 EXPORT_SYMBOL_GPL(use_module);
970 static inline void module_unload_init(struct module *mod)
973 #endif /* CONFIG_MODULE_UNLOAD */
975 static ssize_t show_initstate(struct module_attribute *mattr,
976 struct module *mod, char *buffer)
978 const char *state = "unknown";
980 switch (mod->state) {
981 case MODULE_STATE_LIVE:
982 state = "live";
983 break;
984 case MODULE_STATE_COMING:
985 state = "coming";
986 break;
987 case MODULE_STATE_GOING:
988 state = "going";
989 break;
991 return sprintf(buffer, "%s\n", state);
994 static struct module_attribute initstate = {
995 .attr = { .name = "initstate", .mode = 0444 },
996 .show = show_initstate,
999 static struct module_attribute *modinfo_attrs[] = {
1000 &modinfo_version,
1001 &modinfo_srcversion,
1002 &initstate,
1003 #ifdef CONFIG_MODULE_UNLOAD
1004 &refcnt,
1005 #endif
1006 NULL,
1009 static const char vermagic[] = VERMAGIC_STRING;
1011 static int try_to_force_load(struct module *mod, const char *reason)
1013 #ifdef CONFIG_MODULE_FORCE_LOAD
1014 if (!test_taint(TAINT_FORCED_MODULE))
1015 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1016 mod->name, reason);
1017 add_taint_module(mod, TAINT_FORCED_MODULE);
1018 return 0;
1019 #else
1020 return -ENOEXEC;
1021 #endif
1024 #ifdef CONFIG_MODVERSIONS
1025 static int check_version(Elf_Shdr *sechdrs,
1026 unsigned int versindex,
1027 const char *symname,
1028 struct module *mod,
1029 const unsigned long *crc)
1031 unsigned int i, num_versions;
1032 struct modversion_info *versions;
1034 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1035 if (!crc)
1036 return 1;
1038 /* No versions at all? modprobe --force does this. */
1039 if (versindex == 0)
1040 return try_to_force_load(mod, symname) == 0;
1042 versions = (void *) sechdrs[versindex].sh_addr;
1043 num_versions = sechdrs[versindex].sh_size
1044 / sizeof(struct modversion_info);
1046 for (i = 0; i < num_versions; i++) {
1047 if (strcmp(versions[i].name, symname) != 0)
1048 continue;
1050 if (versions[i].crc == *crc)
1051 return 1;
1052 DEBUGP("Found checksum %lX vs module %lX\n",
1053 *crc, versions[i].crc);
1054 goto bad_version;
1057 printk(KERN_WARNING "%s: no symbol version for %s\n",
1058 mod->name, symname);
1059 return 0;
1061 bad_version:
1062 printk("%s: disagrees about version of symbol %s\n",
1063 mod->name, symname);
1064 return 0;
1067 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1068 unsigned int versindex,
1069 struct module *mod)
1071 const unsigned long *crc;
1073 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1074 &crc, true, false))
1075 BUG();
1076 return check_version(sechdrs, versindex, "module_layout", mod, crc);
1079 /* First part is kernel version, which we ignore if module has crcs. */
1080 static inline int same_magic(const char *amagic, const char *bmagic,
1081 bool has_crcs)
1083 if (has_crcs) {
1084 amagic += strcspn(amagic, " ");
1085 bmagic += strcspn(bmagic, " ");
1087 return strcmp(amagic, bmagic) == 0;
1089 #else
1090 static inline int check_version(Elf_Shdr *sechdrs,
1091 unsigned int versindex,
1092 const char *symname,
1093 struct module *mod,
1094 const unsigned long *crc)
1096 return 1;
1099 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1100 unsigned int versindex,
1101 struct module *mod)
1103 return 1;
1106 static inline int same_magic(const char *amagic, const char *bmagic,
1107 bool has_crcs)
1109 return strcmp(amagic, bmagic) == 0;
1111 #endif /* CONFIG_MODVERSIONS */
1113 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1114 Must be holding module_mutex. */
1115 static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1116 unsigned int versindex,
1117 const char *name,
1118 struct module *mod)
1120 struct module *owner;
1121 const struct kernel_symbol *sym;
1122 const unsigned long *crc;
1124 sym = find_symbol(name, &owner, &crc,
1125 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1126 /* use_module can fail due to OOM,
1127 or module initialization or unloading */
1128 if (sym) {
1129 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1130 !use_module(mod, owner))
1131 sym = NULL;
1133 return sym;
1137 * /sys/module/foo/sections stuff
1138 * J. Corbet <corbet@lwn.net>
1140 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1141 struct module_sect_attr
1143 struct module_attribute mattr;
1144 char *name;
1145 unsigned long address;
1148 struct module_sect_attrs
1150 struct attribute_group grp;
1151 unsigned int nsections;
1152 struct module_sect_attr attrs[0];
1155 static ssize_t module_sect_show(struct module_attribute *mattr,
1156 struct module *mod, char *buf)
1158 struct module_sect_attr *sattr =
1159 container_of(mattr, struct module_sect_attr, mattr);
1160 return sprintf(buf, "0x%lx\n", sattr->address);
1163 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1165 unsigned int section;
1167 for (section = 0; section < sect_attrs->nsections; section++)
1168 kfree(sect_attrs->attrs[section].name);
1169 kfree(sect_attrs);
1172 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1173 char *secstrings, Elf_Shdr *sechdrs)
1175 unsigned int nloaded = 0, i, size[2];
1176 struct module_sect_attrs *sect_attrs;
1177 struct module_sect_attr *sattr;
1178 struct attribute **gattr;
1180 /* Count loaded sections and allocate structures */
1181 for (i = 0; i < nsect; i++)
1182 if (sechdrs[i].sh_flags & SHF_ALLOC
1183 && sechdrs[i].sh_size)
1184 nloaded++;
1185 size[0] = ALIGN(sizeof(*sect_attrs)
1186 + nloaded * sizeof(sect_attrs->attrs[0]),
1187 sizeof(sect_attrs->grp.attrs[0]));
1188 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1189 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1190 if (sect_attrs == NULL)
1191 return;
1193 /* Setup section attributes. */
1194 sect_attrs->grp.name = "sections";
1195 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1197 sect_attrs->nsections = 0;
1198 sattr = &sect_attrs->attrs[0];
1199 gattr = &sect_attrs->grp.attrs[0];
1200 for (i = 0; i < nsect; i++) {
1201 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1202 continue;
1203 if (!sechdrs[i].sh_size)
1204 continue;
1205 sattr->address = sechdrs[i].sh_addr;
1206 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1207 GFP_KERNEL);
1208 if (sattr->name == NULL)
1209 goto out;
1210 sect_attrs->nsections++;
1211 sattr->mattr.show = module_sect_show;
1212 sattr->mattr.store = NULL;
1213 sattr->mattr.attr.name = sattr->name;
1214 sattr->mattr.attr.mode = S_IRUGO;
1215 *(gattr++) = &(sattr++)->mattr.attr;
1217 *gattr = NULL;
1219 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1220 goto out;
1222 mod->sect_attrs = sect_attrs;
1223 return;
1224 out:
1225 free_sect_attrs(sect_attrs);
1228 static void remove_sect_attrs(struct module *mod)
1230 if (mod->sect_attrs) {
1231 sysfs_remove_group(&mod->mkobj.kobj,
1232 &mod->sect_attrs->grp);
1233 /* We are positive that no one is using any sect attrs
1234 * at this point. Deallocate immediately. */
1235 free_sect_attrs(mod->sect_attrs);
1236 mod->sect_attrs = NULL;
1241 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1244 struct module_notes_attrs {
1245 struct kobject *dir;
1246 unsigned int notes;
1247 struct bin_attribute attrs[0];
1250 static ssize_t module_notes_read(struct kobject *kobj,
1251 struct bin_attribute *bin_attr,
1252 char *buf, loff_t pos, size_t count)
1255 * The caller checked the pos and count against our size.
1257 memcpy(buf, bin_attr->private + pos, count);
1258 return count;
1261 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1262 unsigned int i)
1264 if (notes_attrs->dir) {
1265 while (i-- > 0)
1266 sysfs_remove_bin_file(notes_attrs->dir,
1267 &notes_attrs->attrs[i]);
1268 kobject_put(notes_attrs->dir);
1270 kfree(notes_attrs);
1273 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1274 char *secstrings, Elf_Shdr *sechdrs)
1276 unsigned int notes, loaded, i;
1277 struct module_notes_attrs *notes_attrs;
1278 struct bin_attribute *nattr;
1280 /* failed to create section attributes, so can't create notes */
1281 if (!mod->sect_attrs)
1282 return;
1284 /* Count notes sections and allocate structures. */
1285 notes = 0;
1286 for (i = 0; i < nsect; i++)
1287 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1288 (sechdrs[i].sh_type == SHT_NOTE))
1289 ++notes;
1291 if (notes == 0)
1292 return;
1294 notes_attrs = kzalloc(sizeof(*notes_attrs)
1295 + notes * sizeof(notes_attrs->attrs[0]),
1296 GFP_KERNEL);
1297 if (notes_attrs == NULL)
1298 return;
1300 notes_attrs->notes = notes;
1301 nattr = &notes_attrs->attrs[0];
1302 for (loaded = i = 0; i < nsect; ++i) {
1303 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1304 continue;
1305 if (sechdrs[i].sh_type == SHT_NOTE) {
1306 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1307 nattr->attr.mode = S_IRUGO;
1308 nattr->size = sechdrs[i].sh_size;
1309 nattr->private = (void *) sechdrs[i].sh_addr;
1310 nattr->read = module_notes_read;
1311 ++nattr;
1313 ++loaded;
1316 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1317 if (!notes_attrs->dir)
1318 goto out;
1320 for (i = 0; i < notes; ++i)
1321 if (sysfs_create_bin_file(notes_attrs->dir,
1322 &notes_attrs->attrs[i]))
1323 goto out;
1325 mod->notes_attrs = notes_attrs;
1326 return;
1328 out:
1329 free_notes_attrs(notes_attrs, i);
1332 static void remove_notes_attrs(struct module *mod)
1334 if (mod->notes_attrs)
1335 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1338 #else
1340 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1341 char *sectstrings, Elf_Shdr *sechdrs)
1345 static inline void remove_sect_attrs(struct module *mod)
1349 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1350 char *sectstrings, Elf_Shdr *sechdrs)
1354 static inline void remove_notes_attrs(struct module *mod)
1357 #endif
1359 #ifdef CONFIG_SYSFS
1360 int module_add_modinfo_attrs(struct module *mod)
1362 struct module_attribute *attr;
1363 struct module_attribute *temp_attr;
1364 int error = 0;
1365 int i;
1367 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1368 (ARRAY_SIZE(modinfo_attrs) + 1)),
1369 GFP_KERNEL);
1370 if (!mod->modinfo_attrs)
1371 return -ENOMEM;
1373 temp_attr = mod->modinfo_attrs;
1374 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1375 if (!attr->test ||
1376 (attr->test && attr->test(mod))) {
1377 memcpy(temp_attr, attr, sizeof(*temp_attr));
1378 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1379 ++temp_attr;
1382 return error;
1385 void module_remove_modinfo_attrs(struct module *mod)
1387 struct module_attribute *attr;
1388 int i;
1390 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1391 /* pick a field to test for end of list */
1392 if (!attr->attr.name)
1393 break;
1394 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1395 if (attr->free)
1396 attr->free(mod);
1398 kfree(mod->modinfo_attrs);
1401 int mod_sysfs_init(struct module *mod)
1403 int err;
1404 struct kobject *kobj;
1406 if (!module_sysfs_initialized) {
1407 printk(KERN_ERR "%s: module sysfs not initialized\n",
1408 mod->name);
1409 err = -EINVAL;
1410 goto out;
1413 kobj = kset_find_obj(module_kset, mod->name);
1414 if (kobj) {
1415 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1416 kobject_put(kobj);
1417 err = -EINVAL;
1418 goto out;
1421 mod->mkobj.mod = mod;
1423 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1424 mod->mkobj.kobj.kset = module_kset;
1425 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1426 "%s", mod->name);
1427 if (err)
1428 kobject_put(&mod->mkobj.kobj);
1430 /* delay uevent until full sysfs population */
1431 out:
1432 return err;
1435 int mod_sysfs_setup(struct module *mod,
1436 struct kernel_param *kparam,
1437 unsigned int num_params)
1439 int err;
1441 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1442 if (!mod->holders_dir) {
1443 err = -ENOMEM;
1444 goto out_unreg;
1447 err = module_param_sysfs_setup(mod, kparam, num_params);
1448 if (err)
1449 goto out_unreg_holders;
1451 err = module_add_modinfo_attrs(mod);
1452 if (err)
1453 goto out_unreg_param;
1455 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1456 return 0;
1458 out_unreg_param:
1459 module_param_sysfs_remove(mod);
1460 out_unreg_holders:
1461 kobject_put(mod->holders_dir);
1462 out_unreg:
1463 kobject_put(&mod->mkobj.kobj);
1464 return err;
1467 static void mod_sysfs_fini(struct module *mod)
1469 kobject_put(&mod->mkobj.kobj);
1472 #else /* CONFIG_SYSFS */
1474 static void mod_sysfs_fini(struct module *mod)
1478 #endif /* CONFIG_SYSFS */
1480 static void mod_kobject_remove(struct module *mod)
1482 module_remove_modinfo_attrs(mod);
1483 module_param_sysfs_remove(mod);
1484 kobject_put(mod->mkobj.drivers_dir);
1485 kobject_put(mod->holders_dir);
1486 mod_sysfs_fini(mod);
1490 * unlink the module with the whole machine is stopped with interrupts off
1491 * - this defends against kallsyms not taking locks
1493 static int __unlink_module(void *_mod)
1495 struct module *mod = _mod;
1496 list_del(&mod->list);
1497 return 0;
1500 /* Free a module, remove from lists, etc (must hold module_mutex). */
1501 static void free_module(struct module *mod)
1503 /* Delete from various lists */
1504 stop_machine(__unlink_module, mod, NULL);
1505 remove_notes_attrs(mod);
1506 remove_sect_attrs(mod);
1507 mod_kobject_remove(mod);
1509 /* Arch-specific cleanup. */
1510 module_arch_cleanup(mod);
1512 /* Module unload stuff */
1513 module_unload_free(mod);
1515 /* Free any allocated parameters. */
1516 destroy_params(mod->kp, mod->num_kp);
1518 /* This may be NULL, but that's OK */
1519 module_free(mod, mod->module_init);
1520 kfree(mod->args);
1521 if (mod->percpu)
1522 percpu_modfree(mod->percpu);
1523 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1524 if (mod->refptr)
1525 percpu_modfree(mod->refptr);
1526 #endif
1527 /* Free lock-classes: */
1528 lockdep_free_key_range(mod->module_core, mod->core_size);
1530 /* Finally, free the core (containing the module structure) */
1531 module_free(mod, mod->module_core);
1534 void *__symbol_get(const char *symbol)
1536 struct module *owner;
1537 const struct kernel_symbol *sym;
1539 preempt_disable();
1540 sym = find_symbol(symbol, &owner, NULL, true, true);
1541 if (sym && strong_try_module_get(owner))
1542 sym = NULL;
1543 preempt_enable();
1545 return sym ? (void *)sym->value : NULL;
1547 EXPORT_SYMBOL_GPL(__symbol_get);
1550 * Ensure that an exported symbol [global namespace] does not already exist
1551 * in the kernel or in some other module's exported symbol table.
1553 static int verify_export_symbols(struct module *mod)
1555 unsigned int i;
1556 struct module *owner;
1557 const struct kernel_symbol *s;
1558 struct {
1559 const struct kernel_symbol *sym;
1560 unsigned int num;
1561 } arr[] = {
1562 { mod->syms, mod->num_syms },
1563 { mod->gpl_syms, mod->num_gpl_syms },
1564 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1565 #ifdef CONFIG_UNUSED_SYMBOLS
1566 { mod->unused_syms, mod->num_unused_syms },
1567 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1568 #endif
1571 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1572 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1573 if (find_symbol(s->name, &owner, NULL, true, false)) {
1574 printk(KERN_ERR
1575 "%s: exports duplicate symbol %s"
1576 " (owned by %s)\n",
1577 mod->name, s->name, module_name(owner));
1578 return -ENOEXEC;
1582 return 0;
1585 /* Change all symbols so that st_value encodes the pointer directly. */
1586 static int simplify_symbols(Elf_Shdr *sechdrs,
1587 unsigned int symindex,
1588 const char *strtab,
1589 unsigned int versindex,
1590 unsigned int pcpuindex,
1591 struct module *mod)
1593 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1594 unsigned long secbase;
1595 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1596 int ret = 0;
1597 const struct kernel_symbol *ksym;
1599 for (i = 1; i < n; i++) {
1600 switch (sym[i].st_shndx) {
1601 case SHN_COMMON:
1602 /* We compiled with -fno-common. These are not
1603 supposed to happen. */
1604 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1605 printk("%s: please compile with -fno-common\n",
1606 mod->name);
1607 ret = -ENOEXEC;
1608 break;
1610 case SHN_ABS:
1611 /* Don't need to do anything */
1612 DEBUGP("Absolute symbol: 0x%08lx\n",
1613 (long)sym[i].st_value);
1614 break;
1616 case SHN_UNDEF:
1617 ksym = resolve_symbol(sechdrs, versindex,
1618 strtab + sym[i].st_name, mod);
1619 /* Ok if resolved. */
1620 if (ksym) {
1621 sym[i].st_value = ksym->value;
1622 break;
1625 /* Ok if weak. */
1626 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1627 break;
1629 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1630 mod->name, strtab + sym[i].st_name);
1631 ret = -ENOENT;
1632 break;
1634 default:
1635 /* Divert to percpu allocation if a percpu var. */
1636 if (sym[i].st_shndx == pcpuindex)
1637 secbase = (unsigned long)mod->percpu;
1638 else
1639 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1640 sym[i].st_value += secbase;
1641 break;
1645 return ret;
1648 /* Additional bytes needed by arch in front of individual sections */
1649 unsigned int __weak arch_mod_section_prepend(struct module *mod,
1650 unsigned int section)
1652 /* default implementation just returns zero */
1653 return 0;
1656 /* Update size with this section: return offset. */
1657 static long get_offset(struct module *mod, unsigned int *size,
1658 Elf_Shdr *sechdr, unsigned int section)
1660 long ret;
1662 *size += arch_mod_section_prepend(mod, section);
1663 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1664 *size = ret + sechdr->sh_size;
1665 return ret;
1668 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1669 might -- code, read-only data, read-write data, small data. Tally
1670 sizes, and place the offsets into sh_entsize fields: high bit means it
1671 belongs in init. */
1672 static void layout_sections(struct module *mod,
1673 const Elf_Ehdr *hdr,
1674 Elf_Shdr *sechdrs,
1675 const char *secstrings)
1677 static unsigned long const masks[][2] = {
1678 /* NOTE: all executable code must be the first section
1679 * in this array; otherwise modify the text_size
1680 * finder in the two loops below */
1681 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1682 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1683 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1684 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1686 unsigned int m, i;
1688 for (i = 0; i < hdr->e_shnum; i++)
1689 sechdrs[i].sh_entsize = ~0UL;
1691 DEBUGP("Core section allocation order:\n");
1692 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1693 for (i = 0; i < hdr->e_shnum; ++i) {
1694 Elf_Shdr *s = &sechdrs[i];
1696 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1697 || (s->sh_flags & masks[m][1])
1698 || s->sh_entsize != ~0UL
1699 || strstarts(secstrings + s->sh_name, ".init"))
1700 continue;
1701 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1702 DEBUGP("\t%s\n", secstrings + s->sh_name);
1704 if (m == 0)
1705 mod->core_text_size = mod->core_size;
1708 DEBUGP("Init section allocation order:\n");
1709 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1710 for (i = 0; i < hdr->e_shnum; ++i) {
1711 Elf_Shdr *s = &sechdrs[i];
1713 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1714 || (s->sh_flags & masks[m][1])
1715 || s->sh_entsize != ~0UL
1716 || !strstarts(secstrings + s->sh_name, ".init"))
1717 continue;
1718 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1719 | INIT_OFFSET_MASK);
1720 DEBUGP("\t%s\n", secstrings + s->sh_name);
1722 if (m == 0)
1723 mod->init_text_size = mod->init_size;
1727 static void set_license(struct module *mod, const char *license)
1729 if (!license)
1730 license = "unspecified";
1732 if (!license_is_gpl_compatible(license)) {
1733 if (!test_taint(TAINT_PROPRIETARY_MODULE))
1734 printk(KERN_WARNING "%s: module license '%s' taints "
1735 "kernel.\n", mod->name, license);
1736 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1740 /* Parse tag=value strings from .modinfo section */
1741 static char *next_string(char *string, unsigned long *secsize)
1743 /* Skip non-zero chars */
1744 while (string[0]) {
1745 string++;
1746 if ((*secsize)-- <= 1)
1747 return NULL;
1750 /* Skip any zero padding. */
1751 while (!string[0]) {
1752 string++;
1753 if ((*secsize)-- <= 1)
1754 return NULL;
1756 return string;
1759 static char *get_modinfo(Elf_Shdr *sechdrs,
1760 unsigned int info,
1761 const char *tag)
1763 char *p;
1764 unsigned int taglen = strlen(tag);
1765 unsigned long size = sechdrs[info].sh_size;
1767 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1768 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1769 return p + taglen + 1;
1771 return NULL;
1774 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1775 unsigned int infoindex)
1777 struct module_attribute *attr;
1778 int i;
1780 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1781 if (attr->setup)
1782 attr->setup(mod,
1783 get_modinfo(sechdrs,
1784 infoindex,
1785 attr->attr.name));
1789 #ifdef CONFIG_KALLSYMS
1791 /* lookup symbol in given range of kernel_symbols */
1792 static const struct kernel_symbol *lookup_symbol(const char *name,
1793 const struct kernel_symbol *start,
1794 const struct kernel_symbol *stop)
1796 const struct kernel_symbol *ks = start;
1797 for (; ks < stop; ks++)
1798 if (strcmp(ks->name, name) == 0)
1799 return ks;
1800 return NULL;
1803 static int is_exported(const char *name, unsigned long value,
1804 const struct module *mod)
1806 const struct kernel_symbol *ks;
1807 if (!mod)
1808 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
1809 else
1810 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1811 return ks != NULL && ks->value == value;
1814 /* As per nm */
1815 static char elf_type(const Elf_Sym *sym,
1816 Elf_Shdr *sechdrs,
1817 const char *secstrings,
1818 struct module *mod)
1820 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1821 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1822 return 'v';
1823 else
1824 return 'w';
1826 if (sym->st_shndx == SHN_UNDEF)
1827 return 'U';
1828 if (sym->st_shndx == SHN_ABS)
1829 return 'a';
1830 if (sym->st_shndx >= SHN_LORESERVE)
1831 return '?';
1832 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1833 return 't';
1834 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1835 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1836 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1837 return 'r';
1838 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1839 return 'g';
1840 else
1841 return 'd';
1843 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1844 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1845 return 's';
1846 else
1847 return 'b';
1849 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
1850 return 'n';
1851 return '?';
1854 static void add_kallsyms(struct module *mod,
1855 Elf_Shdr *sechdrs,
1856 unsigned int symindex,
1857 unsigned int strindex,
1858 const char *secstrings)
1860 unsigned int i;
1862 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1863 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1864 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1866 /* Set types up while we still have access to sections. */
1867 for (i = 0; i < mod->num_symtab; i++)
1868 mod->symtab[i].st_info
1869 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1871 #else
1872 static inline void add_kallsyms(struct module *mod,
1873 Elf_Shdr *sechdrs,
1874 unsigned int symindex,
1875 unsigned int strindex,
1876 const char *secstrings)
1879 #endif /* CONFIG_KALLSYMS */
1881 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1883 #ifdef CONFIG_DYNAMIC_DEBUG
1884 if (ddebug_add_module(debug, num, debug->modname))
1885 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1886 debug->modname);
1887 #endif
1890 static void *module_alloc_update_bounds(unsigned long size)
1892 void *ret = module_alloc(size);
1894 if (ret) {
1895 /* Update module bounds. */
1896 if ((unsigned long)ret < module_addr_min)
1897 module_addr_min = (unsigned long)ret;
1898 if ((unsigned long)ret + size > module_addr_max)
1899 module_addr_max = (unsigned long)ret + size;
1901 return ret;
1904 #ifdef CONFIG_DEBUG_KMEMLEAK
1905 static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1906 Elf_Shdr *sechdrs, char *secstrings)
1908 unsigned int i;
1910 /* only scan the sections containing data */
1911 kmemleak_scan_area(mod->module_core, (unsigned long)mod -
1912 (unsigned long)mod->module_core,
1913 sizeof(struct module), GFP_KERNEL);
1915 for (i = 1; i < hdr->e_shnum; i++) {
1916 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1917 continue;
1918 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1919 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1920 continue;
1922 kmemleak_scan_area(mod->module_core, sechdrs[i].sh_addr -
1923 (unsigned long)mod->module_core,
1924 sechdrs[i].sh_size, GFP_KERNEL);
1927 #else
1928 static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1929 Elf_Shdr *sechdrs, char *secstrings)
1932 #endif
1934 /* Allocate and load the module: note that size of section 0 is always
1935 zero, and we rely on this for optional sections. */
1936 static noinline struct module *load_module(void __user *umod,
1937 unsigned long len,
1938 const char __user *uargs)
1940 Elf_Ehdr *hdr;
1941 Elf_Shdr *sechdrs;
1942 char *secstrings, *args, *modmagic, *strtab = NULL;
1943 char *staging;
1944 unsigned int i;
1945 unsigned int symindex = 0;
1946 unsigned int strindex = 0;
1947 unsigned int modindex, versindex, infoindex, pcpuindex;
1948 struct module *mod;
1949 long err = 0;
1950 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1951 mm_segment_t old_fs;
1953 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1954 umod, len, uargs);
1955 if (len < sizeof(*hdr))
1956 return ERR_PTR(-ENOEXEC);
1958 /* Suck in entire file: we'll want most of it. */
1959 /* vmalloc barfs on "unusual" numbers. Check here */
1960 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1961 return ERR_PTR(-ENOMEM);
1963 if (copy_from_user(hdr, umod, len) != 0) {
1964 err = -EFAULT;
1965 goto free_hdr;
1968 /* Sanity checks against insmoding binaries or wrong arch,
1969 weird elf version */
1970 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
1971 || hdr->e_type != ET_REL
1972 || !elf_check_arch(hdr)
1973 || hdr->e_shentsize != sizeof(*sechdrs)) {
1974 err = -ENOEXEC;
1975 goto free_hdr;
1978 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1979 goto truncated;
1981 /* Convenience variables */
1982 sechdrs = (void *)hdr + hdr->e_shoff;
1983 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1984 sechdrs[0].sh_addr = 0;
1986 for (i = 1; i < hdr->e_shnum; i++) {
1987 if (sechdrs[i].sh_type != SHT_NOBITS
1988 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1989 goto truncated;
1991 /* Mark all sections sh_addr with their address in the
1992 temporary image. */
1993 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1995 /* Internal symbols and strings. */
1996 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1997 symindex = i;
1998 strindex = sechdrs[i].sh_link;
1999 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
2001 #ifndef CONFIG_MODULE_UNLOAD
2002 /* Don't load .exit sections */
2003 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
2004 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
2005 #endif
2008 modindex = find_sec(hdr, sechdrs, secstrings,
2009 ".gnu.linkonce.this_module");
2010 if (!modindex) {
2011 printk(KERN_WARNING "No module found in object\n");
2012 err = -ENOEXEC;
2013 goto free_hdr;
2015 /* This is temporary: point mod into copy of data. */
2016 mod = (void *)sechdrs[modindex].sh_addr;
2018 if (symindex == 0) {
2019 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2020 mod->name);
2021 err = -ENOEXEC;
2022 goto free_hdr;
2025 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
2026 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2027 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2029 /* Don't keep modinfo and version sections. */
2030 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2031 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2032 #ifdef CONFIG_KALLSYMS
2033 /* Keep symbol and string tables for decoding later. */
2034 sechdrs[symindex].sh_flags |= SHF_ALLOC;
2035 sechdrs[strindex].sh_flags |= SHF_ALLOC;
2036 #endif
2038 /* Check module struct version now, before we try to use module. */
2039 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2040 err = -ENOEXEC;
2041 goto free_hdr;
2044 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2045 /* This is allowed: modprobe --force will invalidate it. */
2046 if (!modmagic) {
2047 err = try_to_force_load(mod, "bad vermagic");
2048 if (err)
2049 goto free_hdr;
2050 } else if (!same_magic(modmagic, vermagic, versindex)) {
2051 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2052 mod->name, modmagic, vermagic);
2053 err = -ENOEXEC;
2054 goto free_hdr;
2057 staging = get_modinfo(sechdrs, infoindex, "staging");
2058 if (staging) {
2059 add_taint_module(mod, TAINT_CRAP);
2060 printk(KERN_WARNING "%s: module is from the staging directory,"
2061 " the quality is unknown, you have been warned.\n",
2062 mod->name);
2065 /* Now copy in args */
2066 args = strndup_user(uargs, ~0UL >> 1);
2067 if (IS_ERR(args)) {
2068 err = PTR_ERR(args);
2069 goto free_hdr;
2072 if (find_module(mod->name)) {
2073 err = -EEXIST;
2074 goto free_mod;
2077 mod->state = MODULE_STATE_COMING;
2079 /* Allow arches to frob section contents and sizes. */
2080 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2081 if (err < 0)
2082 goto free_mod;
2084 if (pcpuindex) {
2085 /* We have a special allocation for this section. */
2086 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
2087 sechdrs[pcpuindex].sh_addralign,
2088 mod->name);
2089 if (!percpu) {
2090 err = -ENOMEM;
2091 goto free_mod;
2093 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2094 mod->percpu = percpu;
2097 /* Determine total sizes, and put offsets in sh_entsize. For now
2098 this is done generically; there doesn't appear to be any
2099 special cases for the architectures. */
2100 layout_sections(mod, hdr, sechdrs, secstrings);
2102 /* Do the allocs. */
2103 ptr = module_alloc_update_bounds(mod->core_size);
2105 * The pointer to this block is stored in the module structure
2106 * which is inside the block. Just mark it as not being a
2107 * leak.
2109 kmemleak_not_leak(ptr);
2110 if (!ptr) {
2111 err = -ENOMEM;
2112 goto free_percpu;
2114 memset(ptr, 0, mod->core_size);
2115 mod->module_core = ptr;
2117 ptr = module_alloc_update_bounds(mod->init_size);
2119 * The pointer to this block is stored in the module structure
2120 * which is inside the block. This block doesn't need to be
2121 * scanned as it contains data and code that will be freed
2122 * after the module is initialized.
2124 kmemleak_ignore(ptr);
2125 if (!ptr && mod->init_size) {
2126 err = -ENOMEM;
2127 goto free_core;
2129 memset(ptr, 0, mod->init_size);
2130 mod->module_init = ptr;
2132 /* Transfer each section which specifies SHF_ALLOC */
2133 DEBUGP("final section addresses:\n");
2134 for (i = 0; i < hdr->e_shnum; i++) {
2135 void *dest;
2137 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2138 continue;
2140 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2141 dest = mod->module_init
2142 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2143 else
2144 dest = mod->module_core + sechdrs[i].sh_entsize;
2146 if (sechdrs[i].sh_type != SHT_NOBITS)
2147 memcpy(dest, (void *)sechdrs[i].sh_addr,
2148 sechdrs[i].sh_size);
2149 /* Update sh_addr to point to copy in image. */
2150 sechdrs[i].sh_addr = (unsigned long)dest;
2151 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2153 /* Module has been moved. */
2154 mod = (void *)sechdrs[modindex].sh_addr;
2155 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2157 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2158 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2159 mod->name);
2160 if (!mod->refptr) {
2161 err = -ENOMEM;
2162 goto free_init;
2164 #endif
2165 /* Now we've moved module, initialize linked lists, etc. */
2166 module_unload_init(mod);
2168 /* add kobject, so we can reference it. */
2169 err = mod_sysfs_init(mod);
2170 if (err)
2171 goto free_unload;
2173 /* Set up license info based on the info section */
2174 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2177 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2178 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2179 * using GPL-only symbols it needs.
2181 if (strcmp(mod->name, "ndiswrapper") == 0)
2182 add_taint(TAINT_PROPRIETARY_MODULE);
2184 /* driverloader was caught wrongly pretending to be under GPL */
2185 if (strcmp(mod->name, "driverloader") == 0)
2186 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2188 /* Set up MODINFO_ATTR fields */
2189 setup_modinfo(mod, sechdrs, infoindex);
2191 /* Fix up syms, so that st_value is a pointer to location. */
2192 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2193 mod);
2194 if (err < 0)
2195 goto cleanup;
2197 /* Now we've got everything in the final locations, we can
2198 * find optional sections. */
2199 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2200 sizeof(*mod->kp), &mod->num_kp);
2201 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2202 sizeof(*mod->syms), &mod->num_syms);
2203 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2204 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2205 sizeof(*mod->gpl_syms),
2206 &mod->num_gpl_syms);
2207 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2208 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2209 "__ksymtab_gpl_future",
2210 sizeof(*mod->gpl_future_syms),
2211 &mod->num_gpl_future_syms);
2212 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2213 "__kcrctab_gpl_future");
2215 #ifdef CONFIG_UNUSED_SYMBOLS
2216 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2217 "__ksymtab_unused",
2218 sizeof(*mod->unused_syms),
2219 &mod->num_unused_syms);
2220 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2221 "__kcrctab_unused");
2222 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2223 "__ksymtab_unused_gpl",
2224 sizeof(*mod->unused_gpl_syms),
2225 &mod->num_unused_gpl_syms);
2226 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2227 "__kcrctab_unused_gpl");
2228 #endif
2229 #ifdef CONFIG_CONSTRUCTORS
2230 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2231 sizeof(*mod->ctors), &mod->num_ctors);
2232 #endif
2234 #ifdef CONFIG_MARKERS
2235 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2236 sizeof(*mod->markers), &mod->num_markers);
2237 #endif
2238 #ifdef CONFIG_TRACEPOINTS
2239 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2240 "__tracepoints",
2241 sizeof(*mod->tracepoints),
2242 &mod->num_tracepoints);
2243 #endif
2244 #ifdef CONFIG_EVENT_TRACING
2245 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2246 "_ftrace_events",
2247 sizeof(*mod->trace_events),
2248 &mod->num_trace_events);
2249 #endif
2250 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2251 /* sechdrs[0].sh_size is always zero */
2252 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2253 "__mcount_loc",
2254 sizeof(*mod->ftrace_callsites),
2255 &mod->num_ftrace_callsites);
2256 #endif
2257 #ifdef CONFIG_MODVERSIONS
2258 if ((mod->num_syms && !mod->crcs)
2259 || (mod->num_gpl_syms && !mod->gpl_crcs)
2260 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2261 #ifdef CONFIG_UNUSED_SYMBOLS
2262 || (mod->num_unused_syms && !mod->unused_crcs)
2263 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2264 #endif
2266 err = try_to_force_load(mod,
2267 "no versions for exported symbols");
2268 if (err)
2269 goto cleanup;
2271 #endif
2273 /* Now do relocations. */
2274 for (i = 1; i < hdr->e_shnum; i++) {
2275 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2276 unsigned int info = sechdrs[i].sh_info;
2278 /* Not a valid relocation section? */
2279 if (info >= hdr->e_shnum)
2280 continue;
2282 /* Don't bother with non-allocated sections */
2283 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2284 continue;
2286 if (sechdrs[i].sh_type == SHT_REL)
2287 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2288 else if (sechdrs[i].sh_type == SHT_RELA)
2289 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2290 mod);
2291 if (err < 0)
2292 goto cleanup;
2295 /* Find duplicate symbols */
2296 err = verify_export_symbols(mod);
2297 if (err < 0)
2298 goto cleanup;
2300 /* Set up and sort exception table */
2301 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2302 sizeof(*mod->extable), &mod->num_exentries);
2303 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2305 /* Finally, copy percpu area over. */
2306 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2307 sechdrs[pcpuindex].sh_size);
2309 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2311 if (!mod->taints) {
2312 struct _ddebug *debug;
2313 unsigned int num_debug;
2315 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2316 sizeof(*debug), &num_debug);
2317 if (debug)
2318 dynamic_debug_setup(debug, num_debug);
2321 err = module_finalize(hdr, sechdrs, mod);
2322 if (err < 0)
2323 goto cleanup;
2325 /* flush the icache in correct context */
2326 old_fs = get_fs();
2327 set_fs(KERNEL_DS);
2330 * Flush the instruction cache, since we've played with text.
2331 * Do it before processing of module parameters, so the module
2332 * can provide parameter accessor functions of its own.
2334 if (mod->module_init)
2335 flush_icache_range((unsigned long)mod->module_init,
2336 (unsigned long)mod->module_init
2337 + mod->init_size);
2338 flush_icache_range((unsigned long)mod->module_core,
2339 (unsigned long)mod->module_core + mod->core_size);
2341 set_fs(old_fs);
2343 mod->args = args;
2344 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2345 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2346 mod->name);
2348 /* Now sew it into the lists so we can get lockdep and oops
2349 * info during argument parsing. Noone should access us, since
2350 * strong_try_module_get() will fail.
2351 * lockdep/oops can run asynchronous, so use the RCU list insertion
2352 * function to insert in a way safe to concurrent readers.
2353 * The mutex protects against concurrent writers.
2355 list_add_rcu(&mod->list, &modules);
2357 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2358 if (err < 0)
2359 goto unlink;
2361 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2362 if (err < 0)
2363 goto unlink;
2364 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2365 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2367 /* Get rid of temporary copy */
2368 vfree(hdr);
2370 /* Done! */
2371 return mod;
2373 unlink:
2374 /* Unlink carefully: kallsyms could be walking list. */
2375 list_del_rcu(&mod->list);
2376 synchronize_sched();
2377 module_arch_cleanup(mod);
2378 cleanup:
2379 kobject_del(&mod->mkobj.kobj);
2380 kobject_put(&mod->mkobj.kobj);
2381 free_unload:
2382 module_unload_free(mod);
2383 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2384 free_init:
2385 percpu_modfree(mod->refptr);
2386 #endif
2387 module_free(mod, mod->module_init);
2388 free_core:
2389 module_free(mod, mod->module_core);
2390 /* mod will be freed with core. Don't access it beyond this line! */
2391 free_percpu:
2392 if (percpu)
2393 percpu_modfree(percpu);
2394 free_mod:
2395 kfree(args);
2396 free_hdr:
2397 vfree(hdr);
2398 return ERR_PTR(err);
2400 truncated:
2401 printk(KERN_ERR "Module len %lu truncated\n", len);
2402 err = -ENOEXEC;
2403 goto free_hdr;
2406 /* Call module constructors. */
2407 static void do_mod_ctors(struct module *mod)
2409 #ifdef CONFIG_CONSTRUCTORS
2410 unsigned long i;
2412 for (i = 0; i < mod->num_ctors; i++)
2413 mod->ctors[i]();
2414 #endif
2417 /* This is where the real work happens */
2418 SYSCALL_DEFINE3(init_module, void __user *, umod,
2419 unsigned long, len, const char __user *, uargs)
2421 struct module *mod;
2422 int ret = 0;
2424 /* Must have permission */
2425 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2426 return -EPERM;
2428 /* Only one module load at a time, please */
2429 if (mutex_lock_interruptible(&module_mutex) != 0)
2430 return -EINTR;
2432 /* Do all the hard work */
2433 mod = load_module(umod, len, uargs);
2434 if (IS_ERR(mod)) {
2435 mutex_unlock(&module_mutex);
2436 return PTR_ERR(mod);
2439 /* Drop lock so they can recurse */
2440 mutex_unlock(&module_mutex);
2442 blocking_notifier_call_chain(&module_notify_list,
2443 MODULE_STATE_COMING, mod);
2445 do_mod_ctors(mod);
2446 /* Start the module */
2447 if (mod->init != NULL)
2448 ret = do_one_initcall(mod->init);
2449 if (ret < 0) {
2450 /* Init routine failed: abort. Try to protect us from
2451 buggy refcounters. */
2452 mod->state = MODULE_STATE_GOING;
2453 synchronize_sched();
2454 module_put(mod);
2455 blocking_notifier_call_chain(&module_notify_list,
2456 MODULE_STATE_GOING, mod);
2457 mutex_lock(&module_mutex);
2458 free_module(mod);
2459 mutex_unlock(&module_mutex);
2460 wake_up(&module_wq);
2461 return ret;
2463 if (ret > 0) {
2464 printk(KERN_WARNING
2465 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2466 "%s: loading module anyway...\n",
2467 __func__, mod->name, ret,
2468 __func__);
2469 dump_stack();
2472 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2473 mod->state = MODULE_STATE_LIVE;
2474 wake_up(&module_wq);
2475 blocking_notifier_call_chain(&module_notify_list,
2476 MODULE_STATE_LIVE, mod);
2478 /* We need to finish all async code before the module init sequence is done */
2479 async_synchronize_full();
2481 mutex_lock(&module_mutex);
2482 /* Drop initial reference. */
2483 module_put(mod);
2484 trim_init_extable(mod);
2485 module_free(mod, mod->module_init);
2486 mod->module_init = NULL;
2487 mod->init_size = 0;
2488 mod->init_text_size = 0;
2489 mutex_unlock(&module_mutex);
2491 return 0;
2494 static inline int within(unsigned long addr, void *start, unsigned long size)
2496 return ((void *)addr >= start && (void *)addr < start + size);
2499 #ifdef CONFIG_KALLSYMS
2501 * This ignores the intensely annoying "mapping symbols" found
2502 * in ARM ELF files: $a, $t and $d.
2504 static inline int is_arm_mapping_symbol(const char *str)
2506 return str[0] == '$' && strchr("atd", str[1])
2507 && (str[2] == '\0' || str[2] == '.');
2510 static const char *get_ksymbol(struct module *mod,
2511 unsigned long addr,
2512 unsigned long *size,
2513 unsigned long *offset)
2515 unsigned int i, best = 0;
2516 unsigned long nextval;
2518 /* At worse, next value is at end of module */
2519 if (within_module_init(addr, mod))
2520 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2521 else
2522 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2524 /* Scan for closest preceeding symbol, and next symbol. (ELF
2525 starts real symbols at 1). */
2526 for (i = 1; i < mod->num_symtab; i++) {
2527 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2528 continue;
2530 /* We ignore unnamed symbols: they're uninformative
2531 * and inserted at a whim. */
2532 if (mod->symtab[i].st_value <= addr
2533 && mod->symtab[i].st_value > mod->symtab[best].st_value
2534 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2535 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2536 best = i;
2537 if (mod->symtab[i].st_value > addr
2538 && mod->symtab[i].st_value < nextval
2539 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2540 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2541 nextval = mod->symtab[i].st_value;
2544 if (!best)
2545 return NULL;
2547 if (size)
2548 *size = nextval - mod->symtab[best].st_value;
2549 if (offset)
2550 *offset = addr - mod->symtab[best].st_value;
2551 return mod->strtab + mod->symtab[best].st_name;
2554 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2555 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2556 const char *module_address_lookup(unsigned long addr,
2557 unsigned long *size,
2558 unsigned long *offset,
2559 char **modname,
2560 char *namebuf)
2562 struct module *mod;
2563 const char *ret = NULL;
2565 preempt_disable();
2566 list_for_each_entry_rcu(mod, &modules, list) {
2567 if (within_module_init(addr, mod) ||
2568 within_module_core(addr, mod)) {
2569 if (modname)
2570 *modname = mod->name;
2571 ret = get_ksymbol(mod, addr, size, offset);
2572 break;
2575 /* Make a copy in here where it's safe */
2576 if (ret) {
2577 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2578 ret = namebuf;
2580 preempt_enable();
2581 return ret;
2584 int lookup_module_symbol_name(unsigned long addr, char *symname)
2586 struct module *mod;
2588 preempt_disable();
2589 list_for_each_entry_rcu(mod, &modules, list) {
2590 if (within_module_init(addr, mod) ||
2591 within_module_core(addr, mod)) {
2592 const char *sym;
2594 sym = get_ksymbol(mod, addr, NULL, NULL);
2595 if (!sym)
2596 goto out;
2597 strlcpy(symname, sym, KSYM_NAME_LEN);
2598 preempt_enable();
2599 return 0;
2602 out:
2603 preempt_enable();
2604 return -ERANGE;
2607 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2608 unsigned long *offset, char *modname, char *name)
2610 struct module *mod;
2612 preempt_disable();
2613 list_for_each_entry_rcu(mod, &modules, list) {
2614 if (within_module_init(addr, mod) ||
2615 within_module_core(addr, mod)) {
2616 const char *sym;
2618 sym = get_ksymbol(mod, addr, size, offset);
2619 if (!sym)
2620 goto out;
2621 if (modname)
2622 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2623 if (name)
2624 strlcpy(name, sym, KSYM_NAME_LEN);
2625 preempt_enable();
2626 return 0;
2629 out:
2630 preempt_enable();
2631 return -ERANGE;
2634 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2635 char *name, char *module_name, int *exported)
2637 struct module *mod;
2639 preempt_disable();
2640 list_for_each_entry_rcu(mod, &modules, list) {
2641 if (symnum < mod->num_symtab) {
2642 *value = mod->symtab[symnum].st_value;
2643 *type = mod->symtab[symnum].st_info;
2644 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2645 KSYM_NAME_LEN);
2646 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2647 *exported = is_exported(name, *value, mod);
2648 preempt_enable();
2649 return 0;
2651 symnum -= mod->num_symtab;
2653 preempt_enable();
2654 return -ERANGE;
2657 static unsigned long mod_find_symname(struct module *mod, const char *name)
2659 unsigned int i;
2661 for (i = 0; i < mod->num_symtab; i++)
2662 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2663 mod->symtab[i].st_info != 'U')
2664 return mod->symtab[i].st_value;
2665 return 0;
2668 /* Look for this name: can be of form module:name. */
2669 unsigned long module_kallsyms_lookup_name(const char *name)
2671 struct module *mod;
2672 char *colon;
2673 unsigned long ret = 0;
2675 /* Don't lock: we're in enough trouble already. */
2676 preempt_disable();
2677 if ((colon = strchr(name, ':')) != NULL) {
2678 *colon = '\0';
2679 if ((mod = find_module(name)) != NULL)
2680 ret = mod_find_symname(mod, colon+1);
2681 *colon = ':';
2682 } else {
2683 list_for_each_entry_rcu(mod, &modules, list)
2684 if ((ret = mod_find_symname(mod, name)) != 0)
2685 break;
2687 preempt_enable();
2688 return ret;
2691 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2692 struct module *, unsigned long),
2693 void *data)
2695 struct module *mod;
2696 unsigned int i;
2697 int ret;
2699 list_for_each_entry(mod, &modules, list) {
2700 for (i = 0; i < mod->num_symtab; i++) {
2701 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2702 mod, mod->symtab[i].st_value);
2703 if (ret != 0)
2704 return ret;
2707 return 0;
2709 #endif /* CONFIG_KALLSYMS */
2711 static char *module_flags(struct module *mod, char *buf)
2713 int bx = 0;
2715 if (mod->taints ||
2716 mod->state == MODULE_STATE_GOING ||
2717 mod->state == MODULE_STATE_COMING) {
2718 buf[bx++] = '(';
2719 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
2720 buf[bx++] = 'P';
2721 if (mod->taints & (1 << TAINT_FORCED_MODULE))
2722 buf[bx++] = 'F';
2723 if (mod->taints & (1 << TAINT_CRAP))
2724 buf[bx++] = 'C';
2726 * TAINT_FORCED_RMMOD: could be added.
2727 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2728 * apply to modules.
2731 /* Show a - for module-is-being-unloaded */
2732 if (mod->state == MODULE_STATE_GOING)
2733 buf[bx++] = '-';
2734 /* Show a + for module-is-being-loaded */
2735 if (mod->state == MODULE_STATE_COMING)
2736 buf[bx++] = '+';
2737 buf[bx++] = ')';
2739 buf[bx] = '\0';
2741 return buf;
2744 #ifdef CONFIG_PROC_FS
2745 /* Called by the /proc file system to return a list of modules. */
2746 static void *m_start(struct seq_file *m, loff_t *pos)
2748 mutex_lock(&module_mutex);
2749 return seq_list_start(&modules, *pos);
2752 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2754 return seq_list_next(p, &modules, pos);
2757 static void m_stop(struct seq_file *m, void *p)
2759 mutex_unlock(&module_mutex);
2762 static int m_show(struct seq_file *m, void *p)
2764 struct module *mod = list_entry(p, struct module, list);
2765 char buf[8];
2767 seq_printf(m, "%s %u",
2768 mod->name, mod->init_size + mod->core_size);
2769 print_unload_info(m, mod);
2771 /* Informative for users. */
2772 seq_printf(m, " %s",
2773 mod->state == MODULE_STATE_GOING ? "Unloading":
2774 mod->state == MODULE_STATE_COMING ? "Loading":
2775 "Live");
2776 /* Used by oprofile and other similar tools. */
2777 seq_printf(m, " 0x%p", mod->module_core);
2779 /* Taints info */
2780 if (mod->taints)
2781 seq_printf(m, " %s", module_flags(mod, buf));
2783 seq_printf(m, "\n");
2784 return 0;
2787 /* Format: modulename size refcount deps address
2789 Where refcount is a number or -, and deps is a comma-separated list
2790 of depends or -.
2792 static const struct seq_operations modules_op = {
2793 .start = m_start,
2794 .next = m_next,
2795 .stop = m_stop,
2796 .show = m_show
2799 static int modules_open(struct inode *inode, struct file *file)
2801 return seq_open(file, &modules_op);
2804 static const struct file_operations proc_modules_operations = {
2805 .open = modules_open,
2806 .read = seq_read,
2807 .llseek = seq_lseek,
2808 .release = seq_release,
2811 static int __init proc_modules_init(void)
2813 proc_create("modules", 0, NULL, &proc_modules_operations);
2814 return 0;
2816 module_init(proc_modules_init);
2817 #endif
2819 /* Given an address, look for it in the module exception tables. */
2820 const struct exception_table_entry *search_module_extables(unsigned long addr)
2822 const struct exception_table_entry *e = NULL;
2823 struct module *mod;
2825 preempt_disable();
2826 list_for_each_entry_rcu(mod, &modules, list) {
2827 if (mod->num_exentries == 0)
2828 continue;
2830 e = search_extable(mod->extable,
2831 mod->extable + mod->num_exentries - 1,
2832 addr);
2833 if (e)
2834 break;
2836 preempt_enable();
2838 /* Now, if we found one, we are running inside it now, hence
2839 we cannot unload the module, hence no refcnt needed. */
2840 return e;
2844 * is_module_address - is this address inside a module?
2845 * @addr: the address to check.
2847 * See is_module_text_address() if you simply want to see if the address
2848 * is code (not data).
2850 bool is_module_address(unsigned long addr)
2852 bool ret;
2854 preempt_disable();
2855 ret = __module_address(addr) != NULL;
2856 preempt_enable();
2858 return ret;
2862 * __module_address - get the module which contains an address.
2863 * @addr: the address.
2865 * Must be called with preempt disabled or module mutex held so that
2866 * module doesn't get freed during this.
2868 struct module *__module_address(unsigned long addr)
2870 struct module *mod;
2872 if (addr < module_addr_min || addr > module_addr_max)
2873 return NULL;
2875 list_for_each_entry_rcu(mod, &modules, list)
2876 if (within_module_core(addr, mod)
2877 || within_module_init(addr, mod))
2878 return mod;
2879 return NULL;
2881 EXPORT_SYMBOL_GPL(__module_address);
2884 * is_module_text_address - is this address inside module code?
2885 * @addr: the address to check.
2887 * See is_module_address() if you simply want to see if the address is
2888 * anywhere in a module. See kernel_text_address() for testing if an
2889 * address corresponds to kernel or module code.
2891 bool is_module_text_address(unsigned long addr)
2893 bool ret;
2895 preempt_disable();
2896 ret = __module_text_address(addr) != NULL;
2897 preempt_enable();
2899 return ret;
2903 * __module_text_address - get the module whose code contains an address.
2904 * @addr: the address.
2906 * Must be called with preempt disabled or module mutex held so that
2907 * module doesn't get freed during this.
2909 struct module *__module_text_address(unsigned long addr)
2911 struct module *mod = __module_address(addr);
2912 if (mod) {
2913 /* Make sure it's within the text section. */
2914 if (!within(addr, mod->module_init, mod->init_text_size)
2915 && !within(addr, mod->module_core, mod->core_text_size))
2916 mod = NULL;
2918 return mod;
2920 EXPORT_SYMBOL_GPL(__module_text_address);
2922 /* Don't grab lock, we're oopsing. */
2923 void print_modules(void)
2925 struct module *mod;
2926 char buf[8];
2928 printk(KERN_DEFAULT "Modules linked in:");
2929 /* Most callers should already have preempt disabled, but make sure */
2930 preempt_disable();
2931 list_for_each_entry_rcu(mod, &modules, list)
2932 printk(" %s%s", mod->name, module_flags(mod, buf));
2933 preempt_enable();
2934 if (last_unloaded_module[0])
2935 printk(" [last unloaded: %s]", last_unloaded_module);
2936 printk("\n");
2939 #ifdef CONFIG_MODVERSIONS
2940 /* Generate the signature for all relevant module structures here.
2941 * If these change, we don't want to try to parse the module. */
2942 void module_layout(struct module *mod,
2943 struct modversion_info *ver,
2944 struct kernel_param *kp,
2945 struct kernel_symbol *ks,
2946 struct marker *marker,
2947 struct tracepoint *tp)
2950 EXPORT_SYMBOL(module_layout);
2951 #endif
2953 #ifdef CONFIG_MARKERS
2954 void module_update_markers(void)
2956 struct module *mod;
2958 mutex_lock(&module_mutex);
2959 list_for_each_entry(mod, &modules, list)
2960 if (!mod->taints)
2961 marker_update_probe_range(mod->markers,
2962 mod->markers + mod->num_markers);
2963 mutex_unlock(&module_mutex);
2965 #endif
2967 #ifdef CONFIG_TRACEPOINTS
2968 void module_update_tracepoints(void)
2970 struct module *mod;
2972 mutex_lock(&module_mutex);
2973 list_for_each_entry(mod, &modules, list)
2974 if (!mod->taints)
2975 tracepoint_update_probe_range(mod->tracepoints,
2976 mod->tracepoints + mod->num_tracepoints);
2977 mutex_unlock(&module_mutex);
2981 * Returns 0 if current not found.
2982 * Returns 1 if current found.
2984 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2986 struct module *iter_mod;
2987 int found = 0;
2989 mutex_lock(&module_mutex);
2990 list_for_each_entry(iter_mod, &modules, list) {
2991 if (!iter_mod->taints) {
2993 * Sorted module list
2995 if (iter_mod < iter->module)
2996 continue;
2997 else if (iter_mod > iter->module)
2998 iter->tracepoint = NULL;
2999 found = tracepoint_get_iter_range(&iter->tracepoint,
3000 iter_mod->tracepoints,
3001 iter_mod->tracepoints
3002 + iter_mod->num_tracepoints);
3003 if (found) {
3004 iter->module = iter_mod;
3005 break;
3009 mutex_unlock(&module_mutex);
3010 return found;
3012 #endif