module: Make the 'usage' lists be two-way
[safe/jmp/linux-2.6] / kernel / module.c
index 625985e..be18c3e 100644 (file)
@@ -403,7 +403,7 @@ static unsigned int find_pcpusec(Elf_Ehdr *hdr,
                                 Elf_Shdr *sechdrs,
                                 const char *secstrings)
 {
-       return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
+       return find_sec(hdr, sechdrs, secstrings, ".data..percpu");
 }
 
 static void percpu_modcopy(struct module *mod,
@@ -523,7 +523,8 @@ static void module_unload_init(struct module *mod)
 {
        int cpu;
 
-       INIT_LIST_HEAD(&mod->modules_which_use_me);
+       INIT_LIST_HEAD(&mod->source_list);
+       INIT_LIST_HEAD(&mod->target_list);
        for_each_possible_cpu(cpu) {
                per_cpu_ptr(mod->refptr, cpu)->incs = 0;
                per_cpu_ptr(mod->refptr, cpu)->decs = 0;
@@ -538,8 +539,9 @@ static void module_unload_init(struct module *mod)
 /* modules using other modules */
 struct module_use
 {
-       struct list_head list;
-       struct module *module_which_uses;
+       struct list_head source_list;
+       struct list_head target_list;
+       struct module *source, *target;
 };
 
 /* Does a already use b? */
@@ -547,8 +549,8 @@ static int already_uses(struct module *a, struct module *b)
 {
        struct module_use *use;
 
-       list_for_each_entry(use, &b->modules_which_use_me, list) {
-               if (use->module_which_uses == a) {
+       list_for_each_entry(use, &b->source_list, source_list) {
+               if (use->source == a) {
                        DEBUGP("%s uses %s!\n", a->name, b->name);
                        return 1;
                }
@@ -557,54 +559,76 @@ static int already_uses(struct module *a, struct module *b)
        return 0;
 }
 
+/*
+ * Module a uses b
+ *  - we add 'a' as a "source", 'b' as a "target" of module use
+ *  - the module_use is added to the list of 'b' sources (so
+ *    'b' can walk the list to see who sourced them), and of 'a'
+ *    targets (so 'a' can see what modules it targets).
+ */
+static int add_module_usage(struct module *a, struct module *b)
+{
+       int no_warn;
+       struct module_use *use;
+
+       DEBUGP("Allocating new usage for %s.\n", a->name);
+       use = kmalloc(sizeof(*use), GFP_ATOMIC);
+       if (!use) {
+               printk(KERN_WARNING "%s: out of memory loading\n", a->name);
+               return -ENOMEM;
+       }
+
+       use->source = a;
+       use->target = b;
+       list_add(&use->source_list, &b->source_list);
+       list_add(&use->target_list, &a->target_list);
+       no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
+       return 0;
+}
+
 /* Module a uses b */
 int use_module(struct module *a, struct module *b)
 {
        struct module_use *use;
        int no_warn, err;
 
-       if (b == NULL || already_uses(a, b))
-               return 0;
+       if (b == NULL || already_uses(a, b)) return 1;
 
        /* If we're interrupted or time out, we fail. */
-       err = strong_try_module_get(b);
+       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);
+               return 0;
+       }
+
+       /* If strong_try_module_get() returned a different error, we fail. */
        if (err)
-               return err;
+               return 0;
 
-       DEBUGP("Allocating new usage for %s.\n", a->name);
-       use = kmalloc(sizeof(*use), GFP_ATOMIC);
-       if (!use) {
-               printk("%s: out of memory loading\n", a->name);
+       err = add_module_usage(a, b);
+       if (err) {
                module_put(b);
-               return -ENOMEM;
+               return 0;
        }
-
-       use->module_which_uses = a;
-       list_add(&use->list, &b->modules_which_use_me);
-       no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
-       return 0;
+       return 1;
 }
 EXPORT_SYMBOL_GPL(use_module);
 
 /* Clear the unload stuff of the module. */
 static void module_unload_free(struct module *mod)
 {
-       struct module *i;
+       struct module_use *use, *tmp;
 
-       list_for_each_entry(i, &modules, list) {
-               struct module_use *use;
-
-               list_for_each_entry(use, &i->modules_which_use_me, list) {
-                       if (use->module_which_uses == mod) {
-                               DEBUGP("%s unusing %s\n", mod->name, i->name);
-                               module_put(i);
-                               list_del(&use->list);
-                               kfree(use);
-                               sysfs_remove_link(i->holders_dir, mod->name);
-                               /* There can be at most one match. */
-                               break;
-                       }
-               }
+       list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
+               struct module *i = use->target;
+               DEBUGP("%s unusing %s\n", mod->name, i->name);
+               module_put(i);
+               list_del(&use->source_list);
+               list_del(&use->target_list);
+               kfree(use);
+               sysfs_remove_link(i->holders_dir, mod->name);
        }
 }
 
@@ -728,7 +752,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
                goto out;
        }
 
-       if (!list_empty(&mod->modules_which_use_me)) {
+       if (!list_empty(&mod->source_list)) {
                /* Other modules depend on us: get rid of them first. */
                ret = -EWOULDBLOCK;
                goto out;
@@ -792,9 +816,9 @@ static inline void print_unload_info(struct seq_file *m, struct module *mod)
 
        /* Always include a trailing , so userspace can differentiate
            between this and the old multi-field proc format. */
-       list_for_each_entry(use, &mod->modules_which_use_me, list) {
+       list_for_each_entry(use, &mod->source_list, source_list) {
                printed_something = 1;
-               seq_printf(m, "%s,", use->module_which_uses->name);
+               seq_printf(m, "%s,", use->source->name);
        }
 
        if (mod->init != NULL && mod->exit == NULL) {
@@ -875,7 +899,7 @@ static inline void module_unload_free(struct module *mod)
 
 int use_module(struct module *a, struct module *b)
 {
-       return strong_try_module_get(b);
+       return strong_try_module_get(b) == 0;
 }
 EXPORT_SYMBOL_GPL(use_module);
 
@@ -1046,39 +1070,17 @@ static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
        struct module *owner;
        const struct kernel_symbol *sym;
        const unsigned long *crc;
-       DEFINE_WAIT(wait);
-       int err;
-       long timeleft = 30 * HZ;
 
-again:
        sym = find_symbol(name, &owner, &crc,
                          !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
-       if (!sym)
-               return NULL;
-
-       if (!check_version(sechdrs, versindex, name, mod, crc, owner))
-               return NULL;
-
-       prepare_to_wait(&module_wq, &wait, TASK_INTERRUPTIBLE);
-       err = use_module(mod, owner);
-       if (likely(!err) || err != -EBUSY || signal_pending(current)) {
-               finish_wait(&module_wq, &wait);
-               return err ? NULL : sym;
+       /* 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;
        }
-
-       /* Module is still loading.  Drop lock and wait. */
-       mutex_unlock(&module_mutex);
-       timeleft = schedule_timeout(timeleft);
-       mutex_lock(&module_mutex);
-       finish_wait(&module_wq, &wait);
-
-       /* Module might be gone entirely, or replaced.  Re-lookup. */
-       if (timeleft)
-               goto again;
-
-       printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
-              mod->name, owner->name);
-       return NULL;
+       return sym;
 }
 
 /*
@@ -2029,6 +2031,7 @@ static noinline struct module *load_module(void __user *umod,
        long err = 0;
        void *ptr = NULL; /* Stops spurious gcc warning */
        unsigned long symoffs, stroffs, *strmap;
+       void __percpu *percpu;
 
        mm_segment_t old_fs;
 
@@ -2173,6 +2176,8 @@ static noinline struct module *load_module(void __user *umod,
                        goto free_mod;
                sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
        }
+       /* Keep this around for failure path. */
+       percpu = mod_percpu(mod);
 
        /* Determine total sizes, and put offsets in sh_entsize.  For now
           this is done generically; there doesn't appear to be any
@@ -2478,7 +2483,7 @@ static noinline struct module *load_module(void __user *umod,
        module_free(mod, mod->module_core);
        /* mod will be freed with core. Don't access it beyond this line! */
  free_percpu:
-       percpu_modfree(mod);
+       free_percpu(percpu);
  free_mod:
        kfree(args);
        kfree(strmap);