]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - kernel/module.c
module: remove module_text_address()
[karo-tx-linux.git] / kernel / module.c
index 77672233387ffc095b746435a90cb0689cabcbf6..8ddca629e07942631e3eaa6e7cf7c875c328792e 100644 (file)
@@ -51,6 +51,7 @@
 #include <linux/tracepoint.h>
 #include <linux/ftrace.h>
 #include <linux/async.h>
+#include <linux/percpu.h>
 
 #if 0
 #define DEBUGP printk
@@ -75,7 +76,7 @@ static DECLARE_WAIT_QUEUE_HEAD(module_wq);
 
 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
 
-/* Bounds of module allocation, for speeding __module_text_address */
+/* Bounds of module allocation, for speeding __module_address */
 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
 
 int register_module_notifier(struct notifier_block * nb)
@@ -282,7 +283,7 @@ struct find_symbol_arg {
        /* Output */
        struct module *owner;
        const unsigned long *crc;
-       unsigned long value;
+       const struct kernel_symbol *sym;
 };
 
 static bool find_symbol_in_section(const struct symsearch *syms,
@@ -323,17 +324,17 @@ static bool find_symbol_in_section(const struct symsearch *syms,
 
        fsa->owner = owner;
        fsa->crc = symversion(syms->crcs, symnum);
-       fsa->value = syms->start[symnum].value;
+       fsa->sym = &syms->start[symnum];
        return true;
 }
 
-/* Find a symbol, return value, (optional) crc and (optional) module
- * which owns it */
-static unsigned long find_symbol(const char *name,
-                                struct module **owner,
-                                const unsigned long **crc,
-                                bool gplok,
-                                bool warn)
+/* Find a symbol and return it, along with, (optional) crc and
+ * (optional) module which owns it */
+static const struct kernel_symbol *find_symbol(const char *name,
+                                              struct module **owner,
+                                              const unsigned long **crc,
+                                              bool gplok,
+                                              bool warn)
 {
        struct find_symbol_arg fsa;
 
@@ -346,11 +347,11 @@ static unsigned long find_symbol(const char *name,
                        *owner = fsa.owner;
                if (crc)
                        *crc = fsa.crc;
-               return fsa.value;
+               return fsa.sym;
        }
 
        DEBUGP("Failed to find symbol %s\n", name);
-       return -ENOENT;
+       return NULL;
 }
 
 /* Search for module by name: must hold module_mutex. */
@@ -366,6 +367,34 @@ static struct module *find_module(const char *name)
 }
 
 #ifdef CONFIG_SMP
+
+#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
+
+static void *percpu_modalloc(unsigned long size, unsigned long align,
+                            const char *name)
+{
+       void *ptr;
+
+       if (align > PAGE_SIZE) {
+               printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
+                      name, align, PAGE_SIZE);
+               align = PAGE_SIZE;
+       }
+
+       ptr = __alloc_reserved_percpu(size, align);
+       if (!ptr)
+               printk(KERN_WARNING
+                      "Could not allocate %lu bytes percpu data\n", size);
+       return ptr;
+}
+
+static void percpu_modfree(void *freeme)
+{
+       free_percpu(freeme);
+}
+
+#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
+
 /* Number of blocks used and allocated. */
 static unsigned int pcpu_num_used, pcpu_num_allocated;
 /* Size of each block.  -ve means used. */
@@ -480,21 +509,6 @@ static void percpu_modfree(void *freeme)
        }
 }
 
-static unsigned int find_pcpusec(Elf_Ehdr *hdr,
-                                Elf_Shdr *sechdrs,
-                                const char *secstrings)
-{
-       return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
-}
-
-static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
-{
-       int cpu;
-
-       for_each_possible_cpu(cpu)
-               memcpy(pcpudest + per_cpu_offset(cpu), from, size);
-}
-
 static int percpu_modinit(void)
 {
        pcpu_num_used = 2;
@@ -513,7 +527,26 @@ static int percpu_modinit(void)
        return 0;
 }
 __initcall(percpu_modinit);
+
+#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
+
+static unsigned int find_pcpusec(Elf_Ehdr *hdr,
+                                Elf_Shdr *sechdrs,
+                                const char *secstrings)
+{
+       return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
+}
+
+static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
+{
+       int cpu;
+
+       for_each_possible_cpu(cpu)
+               memcpy(pcpudest + per_cpu_offset(cpu), from, size);
+}
+
 #else /* ... !CONFIG_SMP */
+
 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
                                    const char *name)
 {
@@ -535,6 +568,7 @@ static inline void percpu_modcopy(void *pcpudst, const void *src,
        /* pcpusec should be 0, and size of that section should be 0. */
        BUG_ON(size != 0);
 }
+
 #endif /* CONFIG_SMP */
 
 #define MODINFO_ATTR(field)    \
@@ -860,7 +894,7 @@ void __symbol_put(const char *symbol)
        struct module *owner;
 
        preempt_disable();
-       if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
+       if (!find_symbol(symbol, &owner, NULL, true, false))
                BUG();
        module_put(owner);
        preempt_enable();
@@ -874,8 +908,10 @@ void symbol_put_addr(void *addr)
        if (core_kernel_text((unsigned long)addr))
                return;
 
-       if (!(modaddr = module_text_address((unsigned long)addr)))
-               BUG();
+       /* module_text_address is safe here: we're supposed to have reference
+        * to module from symbol_get, so it can't go away. */
+       modaddr = __module_text_address((unsigned long)addr);
+       BUG_ON(!modaddr);
        module_put(modaddr);
 }
 EXPORT_SYMBOL_GPL(symbol_put_addr);
@@ -1023,7 +1059,7 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
 {
        const unsigned long *crc;
 
-       if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
+       if (!find_symbol("struct_module", NULL, &crc, true, false))
                BUG();
        return check_version(sechdrs, versindex, "struct_module", mod, crc);
 }
@@ -1064,25 +1100,25 @@ static inline int same_magic(const char *amagic, const char *bmagic,
 
 /* Resolve a symbol for this module.  I.e. if we find one, record usage.
    Must be holding module_mutex. */
-static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
-                                   unsigned int versindex,
-                                   const char *name,
-                                   struct module *mod)
+static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
+                                                 unsigned int versindex,
+                                                 const char *name,
+                                                 struct module *mod)
 {
        struct module *owner;
-       unsigned long ret;
+       const struct kernel_symbol *sym;
        const unsigned long *crc;
 
-       ret = find_symbol(name, &owner, &crc,
+       sym = find_symbol(name, &owner, &crc,
                          !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
-       if (!IS_ERR_VALUE(ret)) {
-               /* use_module can fail due to OOM,
-                  or module initialization or unloading */
+       /* use_module can fail due to OOM,
+          or module initialization or unloading */
+       if (sym) {
                if (!check_version(sechdrs, versindex, name, mod, crc) ||
                    !use_module(mod, owner))
-                       ret = -EINVAL;
+                       sym = NULL;
        }
-       return ret;
+       return sym;
 }
 
 /*
@@ -1457,6 +1493,9 @@ static void free_module(struct module *mod)
        /* Module unload stuff */
        module_unload_free(mod);
 
+       /* Free any allocated parameters. */
+       destroy_params(mod->kp, mod->num_kp);
+
        /* release any pointers to mcount in this module */
        ftrace_release(mod->module_core, mod->core_size);
 
@@ -1479,17 +1518,15 @@ static void free_module(struct module *mod)
 void *__symbol_get(const char *symbol)
 {
        struct module *owner;
-       unsigned long value;
+       const struct kernel_symbol *sym;
 
        preempt_disable();
-       value = find_symbol(symbol, &owner, NULL, true, true);
-       if (IS_ERR_VALUE(value))
-               value = 0;
-       else if (strong_try_module_get(owner))
-               value = 0;
+       sym = find_symbol(symbol, &owner, NULL, true, true);
+       if (sym && strong_try_module_get(owner))
+               sym = NULL;
        preempt_enable();
 
-       return (void *)value;
+       return sym ? (void *)sym->value : NULL;
 }
 EXPORT_SYMBOL_GPL(__symbol_get);
 
@@ -1517,8 +1554,7 @@ static int verify_export_symbols(struct module *mod)
 
        for (i = 0; i < ARRAY_SIZE(arr); i++) {
                for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
-                       if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
-                                                     NULL, true, false))) {
+                       if (find_symbol(s->name, &owner, NULL, true, false)) {
                                printk(KERN_ERR
                                       "%s: exports duplicate symbol %s"
                                       " (owned by %s)\n",
@@ -1542,6 +1578,7 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
        unsigned long secbase;
        unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
        int ret = 0;
+       const struct kernel_symbol *ksym;
 
        for (i = 1; i < n; i++) {
                switch (sym[i].st_shndx) {
@@ -1561,13 +1598,14 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
                        break;
 
                case SHN_UNDEF:
-                       sym[i].st_value
-                         = resolve_symbol(sechdrs, versindex,
-                                          strtab + sym[i].st_name, mod);
-
+                       ksym = resolve_symbol(sechdrs, versindex,
+                                             strtab + sym[i].st_name, mod);
                        /* Ok if resolved.  */
-                       if (!IS_ERR_VALUE(sym[i].st_value))
+                       if (ksym) {
+                               sym[i].st_value = ksym->value;
                                break;
+                       }
+
                        /* Ok if weak.  */
                        if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
                                break;
@@ -1864,8 +1902,7 @@ static noinline struct module *load_module(void __user *umod,
        unsigned int symindex = 0;
        unsigned int strindex = 0;
        unsigned int modindex, versindex, infoindex, pcpuindex;
-       unsigned int num_kp, num_mcount;
-       struct kernel_param *kp;
+       unsigned int num_mcount;
        struct module *mod;
        long err = 0;
        void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
@@ -2110,8 +2147,8 @@ static noinline struct module *load_module(void __user *umod,
 
        /* Now we've got everything in the final locations, we can
         * find optional sections. */
-       kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
-                         &num_kp);
+       mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
+                              sizeof(*mod->kp), &mod->num_kp);
        mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
                                 sizeof(*mod->syms), &mod->num_syms);
        mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
@@ -2257,11 +2294,11 @@ static noinline struct module *load_module(void __user *umod,
         */
        list_add_rcu(&mod->list, &modules);
 
-       err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
+       err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
        if (err < 0)
                goto unlink;
 
-       err = mod_sysfs_setup(mod, kp, num_kp);
+       err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
        if (err < 0)
                goto unlink;
        add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
@@ -2283,8 +2320,8 @@ static noinline struct module *load_module(void __user *umod,
        ftrace_release(mod->module_core, mod->core_size);
  free_unload:
        module_unload_free(mod);
- free_init:
 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
+ free_init:
        percpu_modfree(mod->refptr);
 #endif
        module_free(mod, mod->module_init);
@@ -2710,29 +2747,31 @@ const struct exception_table_entry *search_module_extables(unsigned long addr)
 }
 
 /*
- * Is this a valid module address?
+ * is_module_address - is this address inside a module?
+ * @addr: the address to check.
+ *
+ * See is_module_text_address() if you simply want to see if the address
+ * is code (not data).
  */
-int is_module_address(unsigned long addr)
+bool is_module_address(unsigned long addr)
 {
-       struct module *mod;
+       bool ret;
 
        preempt_disable();
-
-       list_for_each_entry_rcu(mod, &modules, list) {
-               if (within_module_core(addr, mod)) {
-                       preempt_enable();
-                       return 1;
-               }
-       }
-
+       ret = __module_address(addr) != NULL;
        preempt_enable();
 
-       return 0;
+       return ret;
 }
 
-
-/* Is this a valid kernel address? */
-__notrace_funcgraph struct module *__module_text_address(unsigned long addr)
+/*
+ * __module_address - get the module which contains an address.
+ * @addr: the address.
+ *
+ * Must be called with preempt disabled or module mutex held so that
+ * module doesn't get freed during this.
+ */
+__notrace_funcgraph struct module *__module_address(unsigned long addr)
 {
        struct module *mod;
 
@@ -2740,20 +2779,47 @@ __notrace_funcgraph struct module *__module_text_address(unsigned long addr)
                return NULL;
 
        list_for_each_entry_rcu(mod, &modules, list)
-               if (within(addr, mod->module_init, mod->init_text_size)
-                   || within(addr, mod->module_core, mod->core_text_size))
+               if (within_module_core(addr, mod)
+                   || within_module_init(addr, mod))
                        return mod;
        return NULL;
 }
 
-struct module *module_text_address(unsigned long addr)
+/*
+ * is_module_text_address - is this address inside module code?
+ * @addr: the address to check.
+ *
+ * See is_module_address() if you simply want to see if the address is
+ * anywhere in a module.  See kernel_text_address() for testing if an
+ * address corresponds to kernel or module code.
+ */
+bool is_module_text_address(unsigned long addr)
 {
-       struct module *mod;
+       bool ret;
 
        preempt_disable();
-       mod = __module_text_address(addr);
+       ret = __module_text_address(addr) != NULL;
        preempt_enable();
 
+       return ret;
+}
+
+/*
+ * __module_text_address - get the module whose code contains an address.
+ * @addr: the address.
+ *
+ * Must be called with preempt disabled or module mutex held so that
+ * module doesn't get freed during this.
+ */
+struct module *__module_text_address(unsigned long addr)
+{
+       struct module *mod = __module_address(addr);
+       if (mod) {
+               /* Make sure it's within the text section. */
+               if (!within(addr, mod->module_init, mod->init_text_size)
+                   && !within(addr, mod->module_core, mod->core_text_size))
+                       mod = NULL;
+       }
        return mod;
 }