]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - kernel/module.c
dynamic debug: move ddebug_remove_module() down into free_module()
[net-next-2.6.git] / kernel / module.c
index d293c213c22c9d91540c12237b09114ab0e83a53..6c562828c85c2d3d952c46be2de0b2cb0b3748a9 100644 (file)
@@ -582,33 +582,26 @@ static int add_module_usage(struct module *a, struct module *b)
 }
 
 /* Module a uses b: caller needs module_mutex() */
-int use_module(struct module *a, struct module *b)
+int ref_module(struct module *a, struct module *b)
 {
        int err;
 
-       if (b == NULL || already_uses(a, b)) return 1;
-
-       /* If we're interrupted or time out, we fail. */
-       if (wait_event_interruptible_timeout(
-                   module_wq, (err = strong_try_module_get(b)) != -EBUSY,
-                   30 * HZ) <= 0) {
-               printk("%s: gave up waiting for init of module %s.\n",
-                      a->name, b->name);
+       if (b == NULL || already_uses(a, b))
                return 0;
-       }
 
-       /* If strong_try_module_get() returned a different error, we fail. */
+       /* If module isn't available, we fail. */
+       err = strong_try_module_get(b);
        if (err)
-               return 0;
+               return err;
 
        err = add_module_usage(a, b);
        if (err) {
                module_put(b);
-               return 0;
+               return err;
        }
-       return 1;
+       return 0;
 }
-EXPORT_SYMBOL_GPL(use_module);
+EXPORT_SYMBOL_GPL(ref_module);
 
 /* Clear the unload stuff of the module. */
 static void module_unload_free(struct module *mod)
@@ -794,7 +787,6 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
 
        /* Store the name of the last unloaded module for diagnostic purposes */
        strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
-       ddebug_remove_module(mod->name);
 
        free_module(mod);
        return 0;
@@ -893,11 +885,11 @@ static inline void module_unload_free(struct module *mod)
 {
 }
 
-int use_module(struct module *a, struct module *b)
+int ref_module(struct module *a, struct module *b)
 {
-       return strong_try_module_get(b) == 0;
+       return strong_try_module_get(b);
 }
-EXPORT_SYMBOL_GPL(use_module);
+EXPORT_SYMBOL_GPL(ref_module);
 
 static inline void module_unload_init(struct module *mod)
 {
@@ -1062,26 +1054,58 @@ static inline int same_magic(const char *amagic, const char *bmagic,
 static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
                                                  unsigned int versindex,
                                                  const char *name,
-                                                 struct module *mod)
+                                                 struct module *mod,
+                                                 char ownername[])
 {
        struct module *owner;
        const struct kernel_symbol *sym;
        const unsigned long *crc;
+       int err;
 
        mutex_lock(&module_mutex);
        sym = find_symbol(name, &owner, &crc,
                          !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
-       /* use_module can fail due to OOM,
-          or module initialization or unloading */
-       if (sym) {
-               if (!check_version(sechdrs, versindex, name, mod, crc, owner)
-                   || !use_module(mod, owner))
-                       sym = NULL;
+       if (!sym)
+               goto unlock;
+
+       if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
+               sym = ERR_PTR(-EINVAL);
+               goto getname;
+       }
+
+       err = ref_module(mod, owner);
+       if (err) {
+               sym = ERR_PTR(err);
+               goto getname;
        }
+
+getname:
+       /* We must make copy under the lock if we failed to get ref. */
+       strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
+unlock:
        mutex_unlock(&module_mutex);
        return sym;
 }
 
+static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs,
+                                                      unsigned int versindex,
+                                                      const char *name,
+                                                      struct module *mod)
+{
+       const struct kernel_symbol *ksym;
+       char ownername[MODULE_NAME_LEN];
+
+       if (wait_event_interruptible_timeout(module_wq,
+                       !IS_ERR(ksym = resolve_symbol(sechdrs, versindex, name,
+                                                     mod, ownername)) ||
+                       PTR_ERR(ksym) != -EBUSY,
+                                            30 * HZ) <= 0) {
+               printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
+                      mod->name, ownername);
+       }
+       return ksym;
+}
+
 /*
  * /sys/module/foo/sections stuff
  * J. Corbet <corbet@lwn.net>
@@ -1525,6 +1549,9 @@ static void free_module(struct module *mod)
        remove_sect_attrs(mod);
        mod_kobject_remove(mod);
 
+       /* Remove dynamic debug info */
+       ddebug_remove_module(mod->name);
+
        /* Arch-specific cleanup. */
        module_arch_cleanup(mod);
 
@@ -1571,6 +1598,8 @@ EXPORT_SYMBOL_GPL(__symbol_get);
 /*
  * Ensure that an exported symbol [global namespace] does not already exist
  * in the kernel or in some other module's exported symbol table.
+ *
+ * You must hold the module_mutex.
  */
 static int verify_export_symbols(struct module *mod)
 {
@@ -1592,14 +1621,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++) {
-                       const struct kernel_symbol *sym;
-
-                       /* Stopping preemption makes find_symbol safe. */
-                       preempt_disable();
-                       sym = find_symbol(s->name, &owner, NULL, true, false);
-                       preempt_enable();
-
-                       if (sym) {
+                       if (find_symbol(s->name, &owner, NULL, true, false)) {
                                printk(KERN_ERR
                                       "%s: exports duplicate symbol %s"
                                       " (owned by %s)\n",
@@ -1643,21 +1665,23 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
                        break;
 
                case SHN_UNDEF:
-                       ksym = resolve_symbol(sechdrs, versindex,
-                                             strtab + sym[i].st_name, mod);
+                       ksym = resolve_symbol_wait(sechdrs, versindex,
+                                                  strtab + sym[i].st_name,
+                                                  mod);
                        /* Ok if resolved.  */
-                       if (ksym) {
+                       if (ksym && !IS_ERR(ksym)) {
                                sym[i].st_value = ksym->value;
                                break;
                        }
 
                        /* Ok if weak.  */
-                       if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
+                       if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
                                break;
 
-                       printk(KERN_WARNING "%s: Unknown symbol %s\n",
-                              mod->name, strtab + sym[i].st_name);
-                       ret = -ENOENT;
+                       printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
+                              mod->name, strtab + sym[i].st_name,
+                              PTR_ERR(ksym));
+                       ret = PTR_ERR(ksym) ?: -ENOENT;
                        break;
 
                default:
@@ -2040,6 +2064,12 @@ static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
 #endif
 }
 
+static void dynamic_debug_remove(struct _ddebug *debug)
+{
+       if (debug)
+               ddebug_remove_module(debug->modname);
+}
+
 static void *module_alloc_update_bounds(unsigned long size)
 {
        void *ret = module_alloc(size);
@@ -2102,6 +2132,8 @@ static noinline struct module *load_module(void __user *umod,
        void *ptr = NULL; /* Stops spurious gcc warning */
        unsigned long symoffs, stroffs, *strmap;
        void __percpu *percpu;
+       struct _ddebug *debug = NULL;
+       unsigned int num_debug = 0;
 
        mm_segment_t old_fs;
 
@@ -2226,11 +2258,6 @@ static noinline struct module *load_module(void __user *umod,
                goto free_mod;
        }
 
-       if (find_module(mod->name)) {
-               err = -EEXIST;
-               goto free_mod;
-       }
-
        mod->state = MODULE_STATE_COMING;
 
        /* Allow arches to frob section contents and sizes.  */
@@ -2445,11 +2472,6 @@ static noinline struct module *load_module(void __user *umod,
                        goto cleanup;
        }
 
-        /* Find duplicate symbols */
-       err = verify_export_symbols(mod);
-       if (err < 0)
-               goto cleanup;
-
        /* Set up and sort exception table */
        mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
                                    sizeof(*mod->extable), &mod->num_exentries);
@@ -2464,15 +2486,9 @@ static noinline struct module *load_module(void __user *umod,
        kfree(strmap);
        strmap = NULL;
 
-       if (!mod->taints) {
-               struct _ddebug *debug;
-               unsigned int num_debug;
-
+       if (!mod->taints)
                debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
                                     sizeof(*debug), &num_debug);
-               if (debug)
-                       dynamic_debug_setup(debug, num_debug);
-       }
 
        err = module_finalize(hdr, sechdrs, mod);
        if (err < 0)
@@ -2509,6 +2525,19 @@ static noinline struct module *load_module(void __user *umod,
         * The mutex protects against concurrent writers.
         */
        mutex_lock(&module_mutex);
+       if (find_module(mod->name)) {
+               err = -EEXIST;
+               goto unlock;
+       }
+
+       if (debug)
+               dynamic_debug_setup(debug, num_debug);
+
+       /* Find duplicate symbols */
+       err = verify_export_symbols(mod);
+       if (err < 0)
+               goto ddebug;
+
        list_add_rcu(&mod->list, &modules);
        mutex_unlock(&module_mutex);
 
@@ -2535,6 +2564,9 @@ static noinline struct module *load_module(void __user *umod,
        mutex_lock(&module_mutex);
        /* Unlink carefully: kallsyms could be walking list. */
        list_del_rcu(&mod->list);
+ ddebug:
+       dynamic_debug_remove(debug);
+ unlock:
        mutex_unlock(&module_mutex);
        synchronize_sched();
        module_arch_cleanup(mod);