]> 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 f99558e1945a9b9450dee8f31d41a5b5ce5561fd..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);
 
@@ -1638,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:
@@ -2035,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);
@@ -2097,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;
 
@@ -2449,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)
@@ -2499,10 +2530,13 @@ static noinline struct module *load_module(void __user *umod,
                goto unlock;
        }
 
+       if (debug)
+               dynamic_debug_setup(debug, num_debug);
+
        /* Find duplicate symbols */
        err = verify_export_symbols(mod);
        if (err < 0)
-               goto unlock;
+               goto ddebug;
 
        list_add_rcu(&mod->list, &modules);
        mutex_unlock(&module_mutex);
@@ -2530,6 +2564,8 @@ 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();