module: Make the 'usage' lists be two-way
[safe/jmp/linux-2.6] / kernel / module.c
index b751f19..be18c3e 100644 (file)
@@ -180,8 +180,6 @@ extern const struct kernel_symbol __start___ksymtab_gpl[];
 extern const struct kernel_symbol __stop___ksymtab_gpl[];
 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
-extern const struct kernel_symbol __start___ksymtab_gpl_future[];
-extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
 extern const unsigned long __start___kcrctab[];
 extern const unsigned long __start___kcrctab_gpl[];
 extern const unsigned long __start___kcrctab_gpl_future[];
@@ -405,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,
@@ -525,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;
@@ -540,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? */
@@ -549,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;
                }
@@ -559,6 +559,33 @@ 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)
 {
@@ -580,17 +607,11 @@ int use_module(struct module *a, struct module *b)
        if (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 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 1;
 }
 EXPORT_SYMBOL_GPL(use_module);
@@ -598,22 +619,16 @@ EXPORT_SYMBOL_GPL(use_module);
 /* Clear the unload stuff of the module. */
 static void module_unload_free(struct module *mod)
 {
-       struct module *i;
-
-       list_for_each_entry(i, &modules, list) {
-               struct module_use *use;
+       struct module_use *use, *tmp;
 
-               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);
        }
 }
 
@@ -737,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;
@@ -801,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) {
@@ -1186,7 +1201,7 @@ struct module_notes_attrs {
        struct bin_attribute attrs[0];
 };
 
-static ssize_t module_notes_read(struct kobject *kobj,
+static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
                                 struct bin_attribute *bin_attr,
                                 char *buf, loff_t pos, size_t count)
 {
@@ -2016,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;
 
@@ -2160,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
@@ -2465,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);