Merge git://git.kernel.org/pub/scm/linux/kernel/git/mingo/linux-2.6-sched
[cascardo/linux.git] / kernel / module.c
index 015d60c..7734595 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/moduleloader.h>
 #include <linux/init.h>
 #include <linux/kallsyms.h>
+#include <linux/sysfs.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
@@ -61,10 +62,8 @@ extern int module_sysfs_initialized;
 /* If this is set, the section belongs in the init part of the module */
 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
-/* Protects module list */
-static DEFINE_SPINLOCK(modlist_lock);
-
-/* List of modules, protected by module_mutex AND modlist_lock */
+/* List of modules, protected by module_mutex or preempt_disable
+ * (add/delete uses stop_machine). */
 static DEFINE_MUTEX(module_mutex);
 static LIST_HEAD(modules);
 
@@ -106,7 +105,7 @@ void __module_put_and_exit(struct module *mod, long code)
        do_exit(code);
 }
 EXPORT_SYMBOL(__module_put_and_exit);
-       
+
 /* Find a module section: 0 means not found. */
 static unsigned int find_sec(Elf_Ehdr *hdr,
                             Elf_Shdr *sechdrs,
@@ -180,7 +179,7 @@ static unsigned long __find_symbol(const char *name,
        struct module *mod;
        const struct kernel_symbol *ks;
 
-       /* Core kernel first. */ 
+       /* Core kernel first. */
        *owner = NULL;
        ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
        if (ks) {
@@ -232,7 +231,7 @@ static unsigned long __find_symbol(const char *name,
                return ks->value;
        }
 
-       /* Now try modules. */ 
+       /* Now try modules. */
        list_for_each_entry(mod, &modules, list) {
                *owner = mod;
                ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
@@ -286,7 +285,7 @@ static unsigned long __find_symbol(const char *name,
                }
        }
        DEBUGP("Failed to find symbol %s\n", name);
-       return 0;
+       return 0;
 }
 
 /* Search for module by name: must hold module_mutex. */
@@ -442,7 +441,7 @@ static int percpu_modinit(void)
        }
 
        return 0;
-}      
+}
 __initcall(percpu_modinit);
 #else /* ... !CONFIG_SMP */
 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
@@ -484,8 +483,8 @@ static int modinfo_##field##_exists(struct module *mod)               \
 }                                                                     \
 static void free_modinfo_##field(struct module *mod)                  \
 {                                                                     \
-        kfree(mod->field);                                            \
-        mod->field = NULL;                                            \
+       kfree(mod->field);                                            \
+       mod->field = NULL;                                            \
 }                                                                     \
 static struct module_attribute modinfo_##field = {                    \
        .attr = { .name = __stringify(field), .mode = 0444 },         \
@@ -694,8 +693,7 @@ sys_delete_module(const char __user *name_user, unsigned int flags)
        }
 
        /* If it has an init func, it must have an exit func to unload */
-       if ((mod->init != NULL && mod->exit == NULL)
-           || mod->unsafe) {
+       if (mod->init && !mod->exit) {
                forced = try_force_unload(flags);
                if (!forced) {
                        /* This module can't be removed */
@@ -743,11 +741,6 @@ static void print_unload_info(struct seq_file *m, struct module *mod)
                seq_printf(m, "%s,", use->module_which_uses->name);
        }
 
-       if (mod->unsafe) {
-               printed_something = 1;
-               seq_printf(m, "[unsafe],");
-       }
-
        if (mod->init != NULL && mod->exit == NULL) {
                printed_something = 1;
                seq_printf(m, "[permanent],");
@@ -760,14 +753,13 @@ static void print_unload_info(struct seq_file *m, struct module *mod)
 void __symbol_put(const char *symbol)
 {
        struct module *owner;
-       unsigned long flags;
        const unsigned long *crc;
 
-       spin_lock_irqsave(&modlist_lock, flags);
+       preempt_disable();
        if (!__find_symbol(symbol, &owner, &crc, 1))
                BUG();
        module_put(owner);
-       spin_unlock_irqrestore(&modlist_lock, flags);
+       preempt_enable();
 }
 EXPORT_SYMBOL(__symbol_put);
 
@@ -787,8 +779,7 @@ EXPORT_SYMBOL_GPL(symbol_put_addr);
 static ssize_t show_refcnt(struct module_attribute *mattr,
                           struct module *mod, char *buffer)
 {
-       /* sysfs holds a reference */
-       return sprintf(buffer, "%u\n", module_refcount(mod)-1);
+       return sprintf(buffer, "%u\n", module_refcount(mod));
 }
 
 static struct module_attribute refcnt = {
@@ -999,7 +990,7 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
        struct module_sect_attrs *sect_attrs;
        struct module_sect_attr *sattr;
        struct attribute **gattr;
-       
+
        /* Count loaded sections and allocate structures */
        for (i = 0; i < nsect; i++)
                if (sechdrs[i].sh_flags & SHF_ALLOC)
@@ -1057,6 +1048,100 @@ static void remove_sect_attrs(struct module *mod)
        }
 }
 
+/*
+ * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
+ */
+
+struct module_notes_attrs {
+       struct kobject *dir;
+       unsigned int notes;
+       struct bin_attribute attrs[0];
+};
+
+static ssize_t module_notes_read(struct kobject *kobj,
+                                struct bin_attribute *bin_attr,
+                                char *buf, loff_t pos, size_t count)
+{
+       /*
+        * The caller checked the pos and count against our size.
+        */
+       memcpy(buf, bin_attr->private + pos, count);
+       return count;
+}
+
+static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
+                            unsigned int i)
+{
+       if (notes_attrs->dir) {
+               while (i-- > 0)
+                       sysfs_remove_bin_file(notes_attrs->dir,
+                                             &notes_attrs->attrs[i]);
+               kobject_del(notes_attrs->dir);
+       }
+       kfree(notes_attrs);
+}
+
+static void add_notes_attrs(struct module *mod, unsigned int nsect,
+                           char *secstrings, Elf_Shdr *sechdrs)
+{
+       unsigned int notes, loaded, i;
+       struct module_notes_attrs *notes_attrs;
+       struct bin_attribute *nattr;
+
+       /* Count notes sections and allocate structures.  */
+       notes = 0;
+       for (i = 0; i < nsect; i++)
+               if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
+                   (sechdrs[i].sh_type == SHT_NOTE))
+                       ++notes;
+
+       if (notes == 0)
+               return;
+
+       notes_attrs = kzalloc(sizeof(*notes_attrs)
+                             + notes * sizeof(notes_attrs->attrs[0]),
+                             GFP_KERNEL);
+       if (notes_attrs == NULL)
+               return;
+
+       notes_attrs->notes = notes;
+       nattr = &notes_attrs->attrs[0];
+       for (loaded = i = 0; i < nsect; ++i) {
+               if (!(sechdrs[i].sh_flags & SHF_ALLOC))
+                       continue;
+               if (sechdrs[i].sh_type == SHT_NOTE) {
+                       nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
+                       nattr->attr.mode = S_IRUGO;
+                       nattr->size = sechdrs[i].sh_size;
+                       nattr->private = (void *) sechdrs[i].sh_addr;
+                       nattr->read = module_notes_read;
+                       ++nattr;
+               }
+               ++loaded;
+       }
+
+       notes_attrs->dir = kobject_add_dir(&mod->mkobj.kobj, "notes");
+       if (!notes_attrs->dir)
+               goto out;
+
+       for (i = 0; i < notes; ++i)
+               if (sysfs_create_bin_file(notes_attrs->dir,
+                                         &notes_attrs->attrs[i]))
+                       goto out;
+
+       mod->notes_attrs = notes_attrs;
+       return;
+
+  out:
+       free_notes_attrs(notes_attrs, i);
+}
+
+static void remove_notes_attrs(struct module *mod)
+{
+       if (mod->notes_attrs)
+               free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
+}
+
 #else
 
 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
@@ -1067,6 +1152,15 @@ static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
 static inline void remove_sect_attrs(struct module *mod)
 {
 }
+
+static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
+                                  char *sectstrings, Elf_Shdr *sechdrs)
+{
+}
+
+static inline void remove_notes_attrs(struct module *mod)
+{
+}
 #endif /* CONFIG_KALLSYMS */
 
 #ifdef CONFIG_SYSFS
@@ -1201,6 +1295,7 @@ static void free_module(struct module *mod)
 {
        /* Delete from various lists */
        stop_machine_run(__unlink_module, mod, NR_CPUS);
+       remove_notes_attrs(mod);
        remove_sect_attrs(mod);
        mod_kobject_remove(mod);
 
@@ -1228,14 +1323,14 @@ static void free_module(struct module *mod)
 void *__symbol_get(const char *symbol)
 {
        struct module *owner;
-       unsigned long value, flags;
+       unsigned long value;
        const unsigned long *crc;
 
-       spin_lock_irqsave(&modlist_lock, flags);
+       preempt_disable();
        value = __find_symbol(symbol, &owner, &crc, 1);
        if (value && !strong_try_module_get(owner))
                value = 0;
-       spin_unlock_irqrestore(&modlist_lock, flags);
+       preempt_enable();
 
        return (void *)value;
 }
@@ -1253,14 +1348,14 @@ static int verify_export_symbols(struct module *mod)
        const unsigned long *crc;
 
        for (i = 0; i < mod->num_syms; i++)
-               if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
+               if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
                        name = mod->syms[i].name;
                        ret = -ENOEXEC;
                        goto dup;
                }
 
        for (i = 0; i < mod->num_gpl_syms; i++)
-               if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
+               if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
                        name = mod->gpl_syms[i].name;
                        ret = -ENOEXEC;
                        goto dup;
@@ -1786,7 +1881,8 @@ static struct module *load_module(void __user *umod,
        module_unload_init(mod);
 
        /* Initialize kobject, so we can reference it. */
-       if (mod_sysfs_init(mod) != 0)
+       err = mod_sysfs_init(mod);
+       if (err)
                goto cleanup;
 
        /* Set up license info based on the info section */
@@ -1833,7 +1929,7 @@ static struct module *load_module(void __user *umod,
                mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
 
 #ifdef CONFIG_MODVERSIONS
-       if ((mod->num_syms && !crcindex) || 
+       if ((mod->num_syms && !crcindex) ||
            (mod->num_gpl_syms && !gplcrcindex) ||
            (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
            (mod->num_unused_syms && !unusedcrcindex) ||
@@ -1920,7 +2016,7 @@ static struct module *load_module(void __user *umod,
        if (err < 0)
                goto arch_cleanup;
 
-       err = mod_sysfs_setup(mod, 
+       err = mod_sysfs_setup(mod,
                              (struct kernel_param *)
                              sechdrs[setupindex].sh_addr,
                              sechdrs[setupindex].sh_size
@@ -1928,11 +2024,12 @@ static struct module *load_module(void __user *umod,
        if (err < 0)
                goto arch_cleanup;
        add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
+       add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
 
        /* Size of section 0 is 0, so this works well if no unwind info. */
        mod->unwind_info = unwind_add_table(mod,
-                                           (void *)sechdrs[unwindex].sh_addr,
-                                           sechdrs[unwindex].sh_size);
+                                           (void *)sechdrs[unwindex].sh_addr,
+                                           sechdrs[unwindex].sh_size);
 
        /* Get rid of temporary copy */
        vfree(hdr);
@@ -2015,15 +2112,10 @@ sys_init_module(void __user *umod,
                    buggy refcounters. */
                mod->state = MODULE_STATE_GOING;
                synchronize_sched();
-               if (mod->unsafe)
-                       printk(KERN_ERR "%s: module is now stuck!\n",
-                              mod->name);
-               else {
-                       module_put(mod);
-                       mutex_lock(&module_mutex);
-                       free_module(mod);
-                       mutex_unlock(&module_mutex);
-               }
+               module_put(mod);
+               mutex_lock(&module_mutex);
+               free_module(mod);
+               mutex_unlock(&module_mutex);
                return ret;
        }
 
@@ -2054,7 +2146,7 @@ static inline int within(unsigned long addr, void *start, unsigned long size)
  */
 static inline int is_arm_mapping_symbol(const char *str)
 {
-       return str[0] == '$' && strchr("atd", str[1]) 
+       return str[0] == '$' && strchr("atd", str[1])
               && (str[2] == '\0' || str[2] == '.');
 }
 
@@ -2069,11 +2161,11 @@ static const char *get_ksymbol(struct module *mod,
        /* At worse, next value is at end of module */
        if (within(addr, mod->module_init, mod->init_size))
                nextval = (unsigned long)mod->module_init+mod->init_text_size;
-       else 
+       else
                nextval = (unsigned long)mod->module_core+mod->core_text_size;
 
        /* Scan for closest preceeding symbol, and next symbol. (ELF
-           starts real symbols at 1). */
+          starts real symbols at 1). */
        for (i = 1; i < mod->num_symtab; i++) {
                if (mod->symtab[i].st_shndx == SHN_UNDEF)
                        continue;
@@ -2136,7 +2228,7 @@ int lookup_module_symbol_name(unsigned long addr, char *symname)
                        sym = get_ksymbol(mod, addr, NULL, NULL);
                        if (!sym)
                                goto out;
-                       strlcpy(symname, sym, KSYM_NAME_LEN + 1);
+                       strlcpy(symname, sym, KSYM_NAME_LEN);
                        mutex_unlock(&module_mutex);
                        return 0;
                }
@@ -2161,9 +2253,9 @@ int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
                        if (!sym)
                                goto out;
                        if (modname)
-                               strlcpy(modname, mod->name, MODULE_NAME_LEN + 1);
+                               strlcpy(modname, mod->name, MODULE_NAME_LEN);
                        if (name)
-                               strlcpy(name, sym, KSYM_NAME_LEN + 1);
+                               strlcpy(name, sym, KSYM_NAME_LEN);
                        mutex_unlock(&module_mutex);
                        return 0;
                }
@@ -2184,8 +2276,8 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
                        *value = mod->symtab[symnum].st_value;
                        *type = mod->symtab[symnum].st_info;
                        strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
-                               KSYM_NAME_LEN + 1);
-                       strlcpy(module_name, mod->name, MODULE_NAME_LEN + 1);
+                               KSYM_NAME_LEN);
+                       strlcpy(module_name, mod->name, MODULE_NAME_LEN);
                        *exported = is_exported(name, mod);
                        mutex_unlock(&module_mutex);
                        return 0;
@@ -2232,26 +2324,13 @@ unsigned long module_kallsyms_lookup_name(const char *name)
 /* Called by the /proc file system to return a list of modules. */
 static void *m_start(struct seq_file *m, loff_t *pos)
 {
-       struct list_head *i;
-       loff_t n = 0;
-
        mutex_lock(&module_mutex);
-       list_for_each(i, &modules) {
-               if (n++ == *pos)
-                       break;
-       }
-       if (i == &modules)
-               return NULL;
-       return i;
+       return seq_list_start(&modules, *pos);
 }
 
 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
 {
-       struct list_head *i = p;
-       (*pos)++;
-       if (i->next == &modules)
-               return NULL;
-       return i->next;
+       return seq_list_next(p, &modules, pos);
 }
 
 static void m_stop(struct seq_file *m, void *p)
@@ -2321,25 +2400,24 @@ const struct seq_operations modules_op = {
 /* Given an address, look for it in the module exception tables. */
 const struct exception_table_entry *search_module_extables(unsigned long addr)
 {
-       unsigned long flags;
        const struct exception_table_entry *e = NULL;
        struct module *mod;
 
-       spin_lock_irqsave(&modlist_lock, flags);
+       preempt_disable();
        list_for_each_entry(mod, &modules, list) {
                if (mod->num_exentries == 0)
                        continue;
-                               
+
                e = search_extable(mod->extable,
                                   mod->extable + mod->num_exentries - 1,
                                   addr);
                if (e)
                        break;
        }
-       spin_unlock_irqrestore(&modlist_lock, flags);
+       preempt_enable();
 
        /* Now, if we found one, we are running inside it now, hence
-           we cannot unload the module, hence no refcnt needed. */
+          we cannot unload the module, hence no refcnt needed. */
        return e;
 }
 
@@ -2348,25 +2426,24 @@ const struct exception_table_entry *search_module_extables(unsigned long addr)
  */
 int is_module_address(unsigned long addr)
 {
-       unsigned long flags;
        struct module *mod;
 
-       spin_lock_irqsave(&modlist_lock, flags);
+       preempt_disable();
 
        list_for_each_entry(mod, &modules, list) {
                if (within(addr, mod->module_core, mod->core_size)) {
-                       spin_unlock_irqrestore(&modlist_lock, flags);
+                       preempt_enable();
                        return 1;
                }
        }
 
-       spin_unlock_irqrestore(&modlist_lock, flags);
+       preempt_enable();
 
        return 0;
 }
 
 
-/* Is this a valid kernel address?  We don't grab the lock: we are oopsing. */
+/* Is this a valid kernel address? */
 struct module *__module_text_address(unsigned long addr)
 {
        struct module *mod;
@@ -2381,11 +2458,10 @@ struct module *__module_text_address(unsigned long addr)
 struct module *module_text_address(unsigned long addr)
 {
        struct module *mod;
-       unsigned long flags;
 
-       spin_lock_irqsave(&modlist_lock, flags);
+       preempt_disable();
        mod = __module_text_address(addr);
-       spin_unlock_irqrestore(&modlist_lock, flags);
+       preempt_enable();
 
        return mod;
 }