]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - kernel/module.c
rcu: Permit call_rcu() from CPU_DYING notifiers
[linux-2.6.git] / kernel / module.c
index cb40a4e64a0eaeda7c4f8b09b7e00851bef74d81..b084bf116fc4ca62f9ea9faa259abf7677ed2898 100644 (file)
@@ -1,6 +1,6 @@
 /*
    Copyright (C) 2002 Richard Henderson
-   Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
+   Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -16,7 +16,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
-#include <linux/module.h>
+#include <linux/export.h>
 #include <linux/moduleloader.h>
 #include <linux/ftrace_event.h>
 #include <linux/init.h>
 #include <linux/async.h>
 #include <linux/percpu.h>
 #include <linux/kmemleak.h>
+#include <linux/jump_label.h>
+#include <linux/pfn.h>
+#include <linux/bsearch.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/module.h>
 
-#if 0
-#define DEBUGP printk
-#else
-#define DEBUGP(fmt , a...)
-#endif
-
 #ifndef ARCH_SHF_SMALL
 #define ARCH_SHF_SMALL 0
 #endif
 
+/*
+ * Modules' sections will be aligned on page boundaries
+ * to ensure complete separation of code and data, but
+ * only when CONFIG_DEBUG_SET_MODULE_RONX=y
+ */
+#ifdef CONFIG_DEBUG_SET_MODULE_RONX
+# define debug_align(X) ALIGN(X, PAGE_SIZE)
+#else
+# define debug_align(X) (X)
+#endif
+
+/*
+ * Given BASE and SIZE this macro calculates the number of pages the
+ * memory regions occupies
+ */
+#define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ?                \
+               (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
+                        PFN_DOWN((unsigned long)BASE) + 1)     \
+               : (0UL))
+
 /* If this is set, the section belongs in the init part of the module */
 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
@@ -88,6 +105,7 @@ struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
 
 /* Block module loading/unloading? */
 int modules_disabled = 0;
+core_param(nomodule, modules_disabled, bint, 0);
 
 /* Waiting for a module to finish initializing? */
 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
@@ -110,6 +128,19 @@ int unregister_module_notifier(struct notifier_block * nb)
 }
 EXPORT_SYMBOL(unregister_module_notifier);
 
+struct load_info {
+       Elf_Ehdr *hdr;
+       unsigned long len;
+       Elf_Shdr *sechdrs;
+       char *secstrings, *strtab;
+       unsigned long symoffs, stroffs;
+       struct _ddebug *debug;
+       unsigned int num_debug;
+       struct {
+               unsigned int sym, str, mod, vers, info, pcpu;
+       } index;
+};
+
 /* We require a truly strong try_module_get(): 0 means failure due to
    ongoing or failed initialization etc. */
 static inline int strong_try_module_get(struct module *mod)
@@ -140,42 +171,38 @@ void __module_put_and_exit(struct module *mod, long 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,
-                            const char *secstrings,
-                            const char *name)
+static unsigned int find_sec(const struct load_info *info, const char *name)
 {
        unsigned int i;
 
-       for (i = 1; i < hdr->e_shnum; i++)
+       for (i = 1; i < info->hdr->e_shnum; i++) {
+               Elf_Shdr *shdr = &info->sechdrs[i];
                /* Alloc bit cleared means "ignore it." */
-               if ((sechdrs[i].sh_flags & SHF_ALLOC)
-                   && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
+               if ((shdr->sh_flags & SHF_ALLOC)
+                   && strcmp(info->secstrings + shdr->sh_name, name) == 0)
                        return i;
+       }
        return 0;
 }
 
 /* Find a module section, or NULL. */
-static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
-                         const char *secstrings, const char *name)
+static void *section_addr(const struct load_info *info, const char *name)
 {
        /* Section 0 has sh_addr 0. */
-       return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
+       return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
 }
 
 /* Find a module section, or NULL.  Fill in number of "objects" in section. */
-static void *section_objs(Elf_Ehdr *hdr,
-                         Elf_Shdr *sechdrs,
-                         const char *secstrings,
+static void *section_objs(const struct load_info *info,
                          const char *name,
                          size_t object_size,
                          unsigned int *num)
 {
-       unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
+       unsigned int sec = find_sec(info, name);
 
        /* Section 0 has sh_addr 0 and sh_size 0. */
-       *num = sechdrs[sec].sh_size / object_size;
-       return (void *)sechdrs[sec].sh_addr;
+       *num = info->sechdrs[sec].sh_size / object_size;
+       return (void *)info->sechdrs[sec].sh_addr;
 }
 
 /* Provided by the linker */
@@ -208,23 +235,24 @@ static bool each_symbol_in_section(const struct symsearch *arr,
                                   struct module *owner,
                                   bool (*fn)(const struct symsearch *syms,
                                              struct module *owner,
-                                             unsigned int symnum, void *data),
+                                             void *data),
                                   void *data)
 {
-       unsigned int i, j;
+       unsigned int j;
 
        for (j = 0; j < arrsize; j++) {
-               for (i = 0; i < arr[j].stop - arr[j].start; i++)
-                       if (fn(&arr[j], owner, i, data))
-                               return true;
+               if (fn(&arr[j], owner, data))
+                       return true;
        }
 
        return false;
 }
 
 /* Returns true as soon as fn returns true, otherwise false. */
-bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
-                           unsigned int symnum, void *data), void *data)
+bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
+                                   struct module *owner,
+                                   void *data),
+                        void *data)
 {
        struct module *mod;
        static const struct symsearch arr[] = {
@@ -277,7 +305,7 @@ bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
        }
        return false;
 }
-EXPORT_SYMBOL_GPL(each_symbol);
+EXPORT_SYMBOL_GPL(each_symbol_section);
 
 struct find_symbol_arg {
        /* Input */
@@ -291,15 +319,12 @@ struct find_symbol_arg {
        const struct kernel_symbol *sym;
 };
 
-static bool find_symbol_in_section(const struct symsearch *syms,
-                                  struct module *owner,
-                                  unsigned int symnum, void *data)
+static bool check_symbol(const struct symsearch *syms,
+                                struct module *owner,
+                                unsigned int symnum, void *data)
 {
        struct find_symbol_arg *fsa = data;
 
-       if (strcmp(syms->start[symnum].name, fsa->name) != 0)
-               return false;
-
        if (!fsa->gplok) {
                if (syms->licence == GPL_ONLY)
                        return false;
@@ -333,6 +358,30 @@ static bool find_symbol_in_section(const struct symsearch *syms,
        return true;
 }
 
+static int cmp_name(const void *va, const void *vb)
+{
+       const char *a;
+       const struct kernel_symbol *b;
+       a = va; b = vb;
+       return strcmp(a, b->name);
+}
+
+static bool find_symbol_in_section(const struct symsearch *syms,
+                                  struct module *owner,
+                                  void *data)
+{
+       struct find_symbol_arg *fsa = data;
+       struct kernel_symbol *sym;
+
+       sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
+                       sizeof(struct kernel_symbol), cmp_name);
+
+       if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
+               return true;
+
+       return false;
+}
+
 /* Find a symbol and return it, along with, (optional) crc and
  * (optional) module which owns it.  Needs preempt disabled or module_mutex. */
 const struct kernel_symbol *find_symbol(const char *name,
@@ -347,7 +396,7 @@ const struct kernel_symbol *find_symbol(const char *name,
        fsa.gplok = gplok;
        fsa.warn = warn;
 
-       if (each_symbol(find_symbol_in_section, &fsa)) {
+       if (each_symbol_section(find_symbol_in_section, &fsa)) {
                if (owner)
                        *owner = fsa.owner;
                if (crc)
@@ -355,7 +404,7 @@ const struct kernel_symbol *find_symbol(const char *name,
                return fsa.sym;
        }
 
-       DEBUGP("Failed to find symbol %s\n", name);
+       pr_debug("Failed to find symbol %s\n", name);
        return NULL;
 }
 EXPORT_SYMBOL_GPL(find_symbol);
@@ -392,7 +441,8 @@ static int percpu_modalloc(struct module *mod,
        mod->percpu = __alloc_reserved_percpu(size, align);
        if (!mod->percpu) {
                printk(KERN_WARNING
-                      "Could not allocate %lu bytes percpu data\n", size);
+                      "%s: Could not allocate %lu bytes percpu data\n",
+                      mod->name, size);
                return -ENOMEM;
        }
        mod->percpu_size = size;
@@ -404,11 +454,9 @@ static void percpu_modfree(struct module *mod)
        free_percpu(mod->percpu);
 }
 
-static unsigned int find_pcpusec(Elf_Ehdr *hdr,
-                                Elf_Shdr *sechdrs,
-                                const char *secstrings)
+static unsigned int find_pcpusec(struct load_info *info)
 {
-       return find_sec(hdr, sechdrs, secstrings, ".data..percpu");
+       return find_sec(info, ".data..percpu");
 }
 
 static void percpu_modcopy(struct module *mod,
@@ -468,9 +516,7 @@ static inline int percpu_modalloc(struct module *mod,
 static inline void percpu_modfree(struct module *mod)
 {
 }
-static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
-                                       Elf_Shdr *sechdrs,
-                                       const char *secstrings)
+static unsigned int find_pcpusec(struct load_info *info)
 {
        return 0;
 }
@@ -493,9 +539,9 @@ static void setup_modinfo_##field(struct module *mod, const char *s)  \
        mod->field = kstrdup(s, GFP_KERNEL);                          \
 }                                                                     \
 static ssize_t show_modinfo_##field(struct module_attribute *mattr,   \
-                       struct module *mod, char *buffer)             \
+                       struct module_kobject *mk, char *buffer)      \
 {                                                                     \
-       return sprintf(buffer, "%s\n", mod->field);                   \
+       return sprintf(buffer, "%s\n", mk->mod->field);               \
 }                                                                     \
 static int modinfo_##field##_exists(struct module *mod)               \
 {                                                                     \
@@ -548,11 +594,11 @@ static int already_uses(struct module *a, struct module *b)
 
        list_for_each_entry(use, &b->source_list, source_list) {
                if (use->source == a) {
-                       DEBUGP("%s uses %s!\n", a->name, b->name);
+                       pr_debug("%s uses %s!\n", a->name, b->name);
                        return 1;
                }
        }
-       DEBUGP("%s does not use %s!\n", a->name, b->name);
+       pr_debug("%s does not use %s!\n", a->name, b->name);
        return 0;
 }
 
@@ -567,7 +613,7 @@ static int add_module_usage(struct module *a, struct module *b)
 {
        struct module_use *use;
 
-       DEBUGP("Allocating new usage for %s.\n", a->name);
+       pr_debug("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);
@@ -611,7 +657,7 @@ static void module_unload_free(struct module *mod)
        mutex_lock(&module_mutex);
        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);
+               pr_debug("%s unusing %s\n", mod->name, i->name);
                module_put(i);
                list_del(&use->source_list);
                list_del(&use->target_list);
@@ -674,9 +720,9 @@ static int try_stop_module(struct module *mod, int flags, int *forced)
        }
 }
 
-unsigned int module_refcount(struct module *mod)
+unsigned long module_refcount(struct module *mod)
 {
-       unsigned int incs = 0, decs = 0;
+       unsigned long incs = 0, decs = 0;
        int cpu;
 
        for_each_possible_cpu(cpu)
@@ -709,7 +755,7 @@ static void wait_for_zero_refcount(struct module *mod)
        /* Since we might sleep for some time, release the mutex first */
        mutex_unlock(&module_mutex);
        for (;;) {
-               DEBUGP("Looking at refcount...\n");
+               pr_debug("Looking at refcount...\n");
                set_current_state(TASK_UNINTERRUPTIBLE);
                if (module_refcount(mod) == 0)
                        break;
@@ -752,7 +798,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
        if (mod->state != MODULE_STATE_LIVE) {
                /* FIXME: if (force), slam module count and wake up
                    waiter --RR */
-               DEBUGP("%s already dying\n", mod->name);
+               pr_debug("%s already dying\n", mod->name);
                ret = -EBUSY;
                goto out;
        }
@@ -780,7 +826,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
                wait_for_zero_refcount(mod);
 
        mutex_unlock(&module_mutex);
-       /* Final destruction now noone is using it. */
+       /* Final destruction now no one is using it. */
        if (mod->exit != NULL)
                mod->exit();
        blocking_notifier_call_chain(&module_notify_list,
@@ -802,7 +848,7 @@ static inline void print_unload_info(struct seq_file *m, struct module *mod)
        struct module_use *use;
        int printed_something = 0;
 
-       seq_printf(m, " %u ", module_refcount(mod));
+       seq_printf(m, " %lu ", module_refcount(mod));
 
        /* Always include a trailing , so userspace can differentiate
            between this and the old multi-field proc format. */
@@ -850,15 +896,43 @@ void symbol_put_addr(void *addr)
 EXPORT_SYMBOL_GPL(symbol_put_addr);
 
 static ssize_t show_refcnt(struct module_attribute *mattr,
-                          struct module *mod, char *buffer)
+                          struct module_kobject *mk, char *buffer)
 {
-       return sprintf(buffer, "%u\n", module_refcount(mod));
+       return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
 }
 
-static struct module_attribute refcnt = {
-       .attr = { .name = "refcnt", .mode = 0444 },
-       .show = show_refcnt,
-};
+static struct module_attribute modinfo_refcnt =
+       __ATTR(refcnt, 0444, show_refcnt, NULL);
+
+void __module_get(struct module *module)
+{
+       if (module) {
+               preempt_disable();
+               __this_cpu_inc(module->refptr->incs);
+               trace_module_get(module, _RET_IP_);
+               preempt_enable();
+       }
+}
+EXPORT_SYMBOL(__module_get);
+
+bool try_module_get(struct module *module)
+{
+       bool ret = true;
+
+       if (module) {
+               preempt_disable();
+
+               if (likely(module_is_live(module))) {
+                       __this_cpu_inc(module->refptr->incs);
+                       trace_module_get(module, _RET_IP_);
+               } else
+                       ret = false;
+
+               preempt_enable();
+       }
+       return ret;
+}
+EXPORT_SYMBOL(try_module_get);
 
 void module_put(struct module *module)
 {
@@ -899,12 +973,32 @@ static inline int module_unload_init(struct module *mod)
 }
 #endif /* CONFIG_MODULE_UNLOAD */
 
+static size_t module_flags_taint(struct module *mod, char *buf)
+{
+       size_t l = 0;
+
+       if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
+               buf[l++] = 'P';
+       if (mod->taints & (1 << TAINT_OOT_MODULE))
+               buf[l++] = 'O';
+       if (mod->taints & (1 << TAINT_FORCED_MODULE))
+               buf[l++] = 'F';
+       if (mod->taints & (1 << TAINT_CRAP))
+               buf[l++] = 'C';
+       /*
+        * TAINT_FORCED_RMMOD: could be added.
+        * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
+        * apply to modules.
+        */
+       return l;
+}
+
 static ssize_t show_initstate(struct module_attribute *mattr,
-                          struct module *mod, char *buffer)
+                             struct module_kobject *mk, char *buffer)
 {
        const char *state = "unknown";
 
-       switch (mod->state) {
+       switch (mk->mod->state) {
        case MODULE_STATE_LIVE:
                state = "live";
                break;
@@ -918,17 +1012,64 @@ static ssize_t show_initstate(struct module_attribute *mattr,
        return sprintf(buffer, "%s\n", state);
 }
 
-static struct module_attribute initstate = {
-       .attr = { .name = "initstate", .mode = 0444 },
-       .show = show_initstate,
-};
+static struct module_attribute modinfo_initstate =
+       __ATTR(initstate, 0444, show_initstate, NULL);
+
+static ssize_t store_uevent(struct module_attribute *mattr,
+                           struct module_kobject *mk,
+                           const char *buffer, size_t count)
+{
+       enum kobject_action action;
+
+       if (kobject_action_type(buffer, count, &action) == 0)
+               kobject_uevent(&mk->kobj, action);
+       return count;
+}
+
+struct module_attribute module_uevent =
+       __ATTR(uevent, 0200, NULL, store_uevent);
+
+static ssize_t show_coresize(struct module_attribute *mattr,
+                            struct module_kobject *mk, char *buffer)
+{
+       return sprintf(buffer, "%u\n", mk->mod->core_size);
+}
+
+static struct module_attribute modinfo_coresize =
+       __ATTR(coresize, 0444, show_coresize, NULL);
+
+static ssize_t show_initsize(struct module_attribute *mattr,
+                            struct module_kobject *mk, char *buffer)
+{
+       return sprintf(buffer, "%u\n", mk->mod->init_size);
+}
+
+static struct module_attribute modinfo_initsize =
+       __ATTR(initsize, 0444, show_initsize, NULL);
+
+static ssize_t show_taint(struct module_attribute *mattr,
+                         struct module_kobject *mk, char *buffer)
+{
+       size_t l;
+
+       l = module_flags_taint(mk->mod, buffer);
+       buffer[l++] = '\n';
+       return l;
+}
+
+static struct module_attribute modinfo_taint =
+       __ATTR(taint, 0444, show_taint, NULL);
 
 static struct module_attribute *modinfo_attrs[] = {
+       &module_uevent,
        &modinfo_version,
        &modinfo_srcversion,
-       &initstate,
+       &modinfo_initstate,
+       &modinfo_coresize,
+       &modinfo_initsize,
+       &modinfo_taint,
 #ifdef CONFIG_MODULE_UNLOAD
-       &refcnt,
+       &modinfo_refcnt,
 #endif
        NULL,
 };
@@ -988,7 +1129,7 @@ static int check_version(Elf_Shdr *sechdrs,
 
                if (versions[i].crc == maybe_relocated(*crc, crc_owner))
                        return 1;
-               DEBUGP("Found checksum %lX vs module %lX\n",
+               pr_debug("Found checksum %lX vs module %lX\n",
                       maybe_relocated(*crc, crc_owner), versions[i].crc);
                goto bad_version;
        }
@@ -1054,10 +1195,9 @@ static inline int same_magic(const char *amagic, const char *bmagic,
 #endif /* CONFIG_MODVERSIONS */
 
 /* Resolve a symbol for this module.  I.e. if we find one, record usage. */
-static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
-                                                 unsigned int versindex,
+static const struct kernel_symbol *resolve_symbol(struct module *mod,
+                                                 const struct load_info *info,
                                                  const char *name,
-                                                 struct module *mod,
                                                  char ownername[])
 {
        struct module *owner;
@@ -1071,7 +1211,8 @@ static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
        if (!sym)
                goto unlock;
 
-       if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
+       if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
+                          owner)) {
                sym = ERR_PTR(-EINVAL);
                goto getname;
        }
@@ -1090,21 +1231,20 @@ unlock:
        return sym;
 }
 
-static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs,
-                                                      unsigned int versindex,
-                                                      const char *name,
-                                                      struct module *mod)
+static const struct kernel_symbol *
+resolve_symbol_wait(struct module *mod,
+                   const struct load_info *info,
+                   const char *name)
 {
        const struct kernel_symbol *ksym;
-       char ownername[MODULE_NAME_LEN];
+       char owner[MODULE_NAME_LEN];
 
        if (wait_event_interruptible_timeout(module_wq,
-                       !IS_ERR(ksym = resolve_symbol(sechdrs, versindex, name,
-                                                     mod, ownername)) ||
-                       PTR_ERR(ksym) != -EBUSY,
+                       !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
+                       || PTR_ERR(ksym) != -EBUSY,
                                             30 * HZ) <= 0) {
                printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
-                      mod->name, ownername);
+                      mod->name, owner);
        }
        return ksym;
 }
@@ -1113,8 +1253,9 @@ static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs,
  * /sys/module/foo/sections stuff
  * J. Corbet <corbet@lwn.net>
  */
-#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
+#ifdef CONFIG_SYSFS
 
+#ifdef CONFIG_KALLSYMS
 static inline bool sect_empty(const Elf_Shdr *sect)
 {
        return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
@@ -1135,11 +1276,11 @@ struct module_sect_attrs
 };
 
 static ssize_t module_sect_show(struct module_attribute *mattr,
-                               struct module *mod, char *buf)
+                               struct module_kobject *mk, char *buf)
 {
        struct module_sect_attr *sattr =
                container_of(mattr, struct module_sect_attr, mattr);
-       return sprintf(buf, "0x%lx\n", sattr->address);
+       return sprintf(buf, "0x%pK\n", (void *)sattr->address);
 }
 
 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
@@ -1151,8 +1292,7 @@ static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
        kfree(sect_attrs);
 }
 
-static void add_sect_attrs(struct module *mod, unsigned int nsect,
-               char *secstrings, Elf_Shdr *sechdrs)
+static void add_sect_attrs(struct module *mod, const struct load_info *info)
 {
        unsigned int nloaded = 0, i, size[2];
        struct module_sect_attrs *sect_attrs;
@@ -1160,8 +1300,8 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
        struct attribute **gattr;
 
        /* Count loaded sections and allocate structures */
-       for (i = 0; i < nsect; i++)
-               if (!sect_empty(&sechdrs[i]))
+       for (i = 0; i < info->hdr->e_shnum; i++)
+               if (!sect_empty(&info->sechdrs[i]))
                        nloaded++;
        size[0] = ALIGN(sizeof(*sect_attrs)
                        + nloaded * sizeof(sect_attrs->attrs[0]),
@@ -1178,11 +1318,12 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
        sect_attrs->nsections = 0;
        sattr = &sect_attrs->attrs[0];
        gattr = &sect_attrs->grp.attrs[0];
-       for (i = 0; i < nsect; i++) {
-               if (sect_empty(&sechdrs[i]))
+       for (i = 0; i < info->hdr->e_shnum; i++) {
+               Elf_Shdr *sec = &info->sechdrs[i];
+               if (sect_empty(sec))
                        continue;
-               sattr->address = sechdrs[i].sh_addr;
-               sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
+               sattr->address = sec->sh_addr;
+               sattr->name = kstrdup(info->secstrings + sec->sh_name,
                                        GFP_KERNEL);
                if (sattr->name == NULL)
                        goto out;
@@ -1250,8 +1391,7 @@ static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
        kfree(notes_attrs);
 }
 
-static void add_notes_attrs(struct module *mod, unsigned int nsect,
-                           char *secstrings, Elf_Shdr *sechdrs)
+static void add_notes_attrs(struct module *mod, const struct load_info *info)
 {
        unsigned int notes, loaded, i;
        struct module_notes_attrs *notes_attrs;
@@ -1263,9 +1403,9 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
 
        /* Count notes sections and allocate structures.  */
        notes = 0;
-       for (i = 0; i < nsect; i++)
-               if (!sect_empty(&sechdrs[i]) &&
-                   (sechdrs[i].sh_type == SHT_NOTE))
+       for (i = 0; i < info->hdr->e_shnum; i++)
+               if (!sect_empty(&info->sechdrs[i]) &&
+                   (info->sechdrs[i].sh_type == SHT_NOTE))
                        ++notes;
 
        if (notes == 0)
@@ -1279,15 +1419,15 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
 
        notes_attrs->notes = notes;
        nattr = &notes_attrs->attrs[0];
-       for (loaded = i = 0; i < nsect; ++i) {
-               if (sect_empty(&sechdrs[i]))
+       for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
+               if (sect_empty(&info->sechdrs[i]))
                        continue;
-               if (sechdrs[i].sh_type == SHT_NOTE) {
+               if (info->sechdrs[i].sh_type == SHT_NOTE) {
                        sysfs_bin_attr_init(nattr);
                        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->size = info->sechdrs[i].sh_size;
+                       nattr->private = (void *) info->sechdrs[i].sh_addr;
                        nattr->read = module_notes_read;
                        ++nattr;
                }
@@ -1318,8 +1458,8 @@ static void remove_notes_attrs(struct module *mod)
 
 #else
 
-static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
-               char *sectstrings, Elf_Shdr *sechdrs)
+static inline void add_sect_attrs(struct module *mod,
+                                 const struct load_info *info)
 {
 }
 
@@ -1327,17 +1467,16 @@ 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 add_notes_attrs(struct module *mod,
+                                  const struct load_info *info)
 {
 }
 
 static inline void remove_notes_attrs(struct module *mod)
 {
 }
-#endif
+#endif /* CONFIG_KALLSYMS */
 
-#ifdef CONFIG_SYSFS
 static void add_usage_links(struct module *mod)
 {
 #ifdef CONFIG_MODULE_UNLOAD
@@ -1442,6 +1581,7 @@ out:
 }
 
 static int mod_sysfs_setup(struct module *mod,
+                          const struct load_info *info,
                           struct kernel_param *kparam,
                           unsigned int num_params)
 {
@@ -1466,6 +1606,8 @@ static int mod_sysfs_setup(struct module *mod,
                goto out_unreg_param;
 
        add_usage_links(mod);
+       add_sect_attrs(mod, info);
+       add_notes_attrs(mod, info);
 
        kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
        return 0;
@@ -1482,33 +1624,26 @@ out:
 
 static void mod_sysfs_fini(struct module *mod)
 {
+       remove_notes_attrs(mod);
+       remove_sect_attrs(mod);
        kobject_put(&mod->mkobj.kobj);
 }
 
-#else /* CONFIG_SYSFS */
+#else /* !CONFIG_SYSFS */
 
-static inline int mod_sysfs_init(struct module *mod)
-{
-       return 0;
-}
-
-static inline int mod_sysfs_setup(struct module *mod,
+static int mod_sysfs_setup(struct module *mod,
+                          const struct load_info *info,
                           struct kernel_param *kparam,
                           unsigned int num_params)
 {
        return 0;
 }
 
-static inline int module_add_modinfo_attrs(struct module *mod)
-{
-       return 0;
-}
-
-static inline void module_remove_modinfo_attrs(struct module *mod)
+static void mod_sysfs_fini(struct module *mod)
 {
 }
 
-static void mod_sysfs_fini(struct module *mod)
+static void module_remove_modinfo_attrs(struct module *mod)
 {
 }
 
@@ -1518,7 +1653,7 @@ static void del_usage_links(struct module *mod)
 
 #endif /* CONFIG_SYSFS */
 
-static void mod_kobject_remove(struct module *mod)
+static void mod_sysfs_teardown(struct module *mod)
 {
        del_usage_links(mod);
        module_remove_modinfo_attrs(mod);
@@ -1536,9 +1671,130 @@ static int __unlink_module(void *_mod)
 {
        struct module *mod = _mod;
        list_del(&mod->list);
+       module_bug_cleanup(mod);
        return 0;
 }
 
+#ifdef CONFIG_DEBUG_SET_MODULE_RONX
+/*
+ * LKM RO/NX protection: protect module's text/ro-data
+ * from modification and any data from execution.
+ */
+void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
+{
+       unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
+       unsigned long end_pfn = PFN_DOWN((unsigned long)end);
+
+       if (end_pfn > begin_pfn)
+               set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
+}
+
+static void set_section_ro_nx(void *base,
+                       unsigned long text_size,
+                       unsigned long ro_size,
+                       unsigned long total_size)
+{
+       /* begin and end PFNs of the current subsection */
+       unsigned long begin_pfn;
+       unsigned long end_pfn;
+
+       /*
+        * Set RO for module text and RO-data:
+        * - Always protect first page.
+        * - Do not protect last partial page.
+        */
+       if (ro_size > 0)
+               set_page_attributes(base, base + ro_size, set_memory_ro);
+
+       /*
+        * Set NX permissions for module data:
+        * - Do not protect first partial page.
+        * - Always protect last page.
+        */
+       if (total_size > text_size) {
+               begin_pfn = PFN_UP((unsigned long)base + text_size);
+               end_pfn = PFN_UP((unsigned long)base + total_size);
+               if (end_pfn > begin_pfn)
+                       set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
+       }
+}
+
+static void unset_module_core_ro_nx(struct module *mod)
+{
+       set_page_attributes(mod->module_core + mod->core_text_size,
+               mod->module_core + mod->core_size,
+               set_memory_x);
+       set_page_attributes(mod->module_core,
+               mod->module_core + mod->core_ro_size,
+               set_memory_rw);
+}
+
+static void unset_module_init_ro_nx(struct module *mod)
+{
+       set_page_attributes(mod->module_init + mod->init_text_size,
+               mod->module_init + mod->init_size,
+               set_memory_x);
+       set_page_attributes(mod->module_init,
+               mod->module_init + mod->init_ro_size,
+               set_memory_rw);
+}
+
+/* Iterate through all modules and set each module's text as RW */
+void set_all_modules_text_rw(void)
+{
+       struct module *mod;
+
+       mutex_lock(&module_mutex);
+       list_for_each_entry_rcu(mod, &modules, list) {
+               if ((mod->module_core) && (mod->core_text_size)) {
+                       set_page_attributes(mod->module_core,
+                                               mod->module_core + mod->core_text_size,
+                                               set_memory_rw);
+               }
+               if ((mod->module_init) && (mod->init_text_size)) {
+                       set_page_attributes(mod->module_init,
+                                               mod->module_init + mod->init_text_size,
+                                               set_memory_rw);
+               }
+       }
+       mutex_unlock(&module_mutex);
+}
+
+/* Iterate through all modules and set each module's text as RO */
+void set_all_modules_text_ro(void)
+{
+       struct module *mod;
+
+       mutex_lock(&module_mutex);
+       list_for_each_entry_rcu(mod, &modules, list) {
+               if ((mod->module_core) && (mod->core_text_size)) {
+                       set_page_attributes(mod->module_core,
+                                               mod->module_core + mod->core_text_size,
+                                               set_memory_ro);
+               }
+               if ((mod->module_init) && (mod->init_text_size)) {
+                       set_page_attributes(mod->module_init,
+                                               mod->module_init + mod->init_text_size,
+                                               set_memory_ro);
+               }
+       }
+       mutex_unlock(&module_mutex);
+}
+#else
+static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
+static void unset_module_core_ro_nx(struct module *mod) { }
+static void unset_module_init_ro_nx(struct module *mod) { }
+#endif
+
+void __weak module_free(struct module *mod, void *module_region)
+{
+       vfree(module_region);
+}
+
+void __weak module_arch_cleanup(struct module *mod)
+{
+}
+
 /* Free a module, remove from lists, etc. */
 static void free_module(struct module *mod)
 {
@@ -1548,9 +1804,7 @@ static void free_module(struct module *mod)
        mutex_lock(&module_mutex);
        stop_machine(__unlink_module, mod, NULL);
        mutex_unlock(&module_mutex);
-       remove_notes_attrs(mod);
-       remove_sect_attrs(mod);
-       mod_kobject_remove(mod);
+       mod_sysfs_teardown(mod);
 
        /* Remove dynamic debug info */
        ddebug_remove_module(mod->name);
@@ -1565,6 +1819,7 @@ static void free_module(struct module *mod)
        destroy_params(mod->kp, mod->num_kp);
 
        /* This may be NULL, but that's OK */
+       unset_module_init_ro_nx(mod);
        module_free(mod, mod->module_init);
        kfree(mod->args);
        percpu_modfree(mod);
@@ -1573,6 +1828,7 @@ static void free_module(struct module *mod)
        lockdep_free_key_range(mod->module_core, mod->core_size);
 
        /* Finally, free the core (containing the module structure) */
+       unset_module_core_ro_nx(mod);
        module_free(mod, mod->module_core);
 
 #ifdef CONFIG_MPU
@@ -1634,25 +1890,23 @@ static int verify_export_symbols(struct module *mod)
 }
 
 /* Change all symbols so that st_value encodes the pointer directly. */
-static int simplify_symbols(Elf_Shdr *sechdrs,
-                           unsigned int symindex,
-                           const char *strtab,
-                           unsigned int versindex,
-                           unsigned int pcpuindex,
-                           struct module *mod)
-{
-       Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
+static int simplify_symbols(struct module *mod, const struct load_info *info)
+{
+       Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
+       Elf_Sym *sym = (void *)symsec->sh_addr;
        unsigned long secbase;
-       unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
+       unsigned int i;
        int ret = 0;
        const struct kernel_symbol *ksym;
 
-       for (i = 1; i < n; i++) {
+       for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
+               const char *name = info->strtab + sym[i].st_name;
+
                switch (sym[i].st_shndx) {
                case SHN_COMMON:
                        /* We compiled with -fno-common.  These are not
                           supposed to happen.  */
-                       DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
+                       pr_debug("Common symbol: %s\n", name);
                        printk("%s: please compile with -fno-common\n",
                               mod->name);
                        ret = -ENOEXEC;
@@ -1660,14 +1914,12 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
 
                case SHN_ABS:
                        /* Don't need to do anything */
-                       DEBUGP("Absolute symbol: 0x%08lx\n",
+                       pr_debug("Absolute symbol: 0x%08lx\n",
                               (long)sym[i].st_value);
                        break;
 
                case SHN_UNDEF:
-                       ksym = resolve_symbol_wait(sechdrs, versindex,
-                                                  strtab + sym[i].st_name,
-                                                  mod);
+                       ksym = resolve_symbol_wait(mod, info, name);
                        /* Ok if resolved.  */
                        if (ksym && !IS_ERR(ksym)) {
                                sym[i].st_value = ksym->value;
@@ -1679,17 +1931,16 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
                                break;
 
                        printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
-                              mod->name, strtab + sym[i].st_name,
-                              PTR_ERR(ksym));
+                              mod->name, name, PTR_ERR(ksym));
                        ret = PTR_ERR(ksym) ?: -ENOENT;
                        break;
 
                default:
                        /* Divert to percpu allocation if a percpu var. */
-                       if (sym[i].st_shndx == pcpuindex)
+                       if (sym[i].st_shndx == info->index.pcpu)
                                secbase = (unsigned long)mod_percpu(mod);
                        else
-                               secbase = sechdrs[sym[i].st_shndx].sh_addr;
+                               secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
                        sym[i].st_value += secbase;
                        break;
                }
@@ -1698,33 +1949,49 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
        return ret;
 }
 
-static int apply_relocations(struct module *mod,
-                            Elf_Ehdr *hdr,
-                            Elf_Shdr *sechdrs,
-                            unsigned int symindex,
-                            unsigned int strindex)
+int __weak apply_relocate(Elf_Shdr *sechdrs,
+                         const char *strtab,
+                         unsigned int symindex,
+                         unsigned int relsec,
+                         struct module *me)
+{
+       pr_err("module %s: REL relocation unsupported\n", me->name);
+       return -ENOEXEC;
+}
+
+int __weak apply_relocate_add(Elf_Shdr *sechdrs,
+                             const char *strtab,
+                             unsigned int symindex,
+                             unsigned int relsec,
+                             struct module *me)
+{
+       pr_err("module %s: RELA relocation unsupported\n", me->name);
+       return -ENOEXEC;
+}
+
+static int apply_relocations(struct module *mod, const struct load_info *info)
 {
        unsigned int i;
        int err = 0;
 
        /* Now do relocations. */
-       for (i = 1; i < hdr->e_shnum; i++) {
-               const char *strtab = (char *)sechdrs[strindex].sh_addr;
-               unsigned int info = sechdrs[i].sh_info;
+       for (i = 1; i < info->hdr->e_shnum; i++) {
+               unsigned int infosec = info->sechdrs[i].sh_info;
 
                /* Not a valid relocation section? */
-               if (info >= hdr->e_shnum)
+               if (infosec >= info->hdr->e_shnum)
                        continue;
 
                /* Don't bother with non-allocated sections */
-               if (!(sechdrs[info].sh_flags & SHF_ALLOC))
+               if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
                        continue;
 
-               if (sechdrs[i].sh_type == SHT_REL)
-                       err = apply_relocate(sechdrs, strtab, symindex, i, mod);
-               else if (sechdrs[i].sh_type == SHT_RELA)
-                       err = apply_relocate_add(sechdrs, strtab, symindex, i,
-                                                mod);
+               if (info->sechdrs[i].sh_type == SHT_REL)
+                       err = apply_relocate(info->sechdrs, info->strtab,
+                                            info->index.sym, i, mod);
+               else if (info->sechdrs[i].sh_type == SHT_RELA)
+                       err = apply_relocate_add(info->sechdrs, info->strtab,
+                                                info->index.sym, i, mod);
                if (err < 0)
                        break;
        }
@@ -1755,10 +2022,7 @@ static long get_offset(struct module *mod, unsigned int *size,
    might -- code, read-only data, read-write data, small data.  Tally
    sizes, and place the offsets into sh_entsize fields: high bit means it
    belongs in init. */
-static void layout_sections(struct module *mod,
-                           const Elf_Ehdr *hdr,
-                           Elf_Shdr *sechdrs,
-                           const char *secstrings)
+static void layout_sections(struct module *mod, struct load_info *info)
 {
        static unsigned long const masks[][2] = {
                /* NOTE: all executable code must be the first section
@@ -1771,42 +2035,66 @@ static void layout_sections(struct module *mod,
        };
        unsigned int m, i;
 
-       for (i = 0; i < hdr->e_shnum; i++)
-               sechdrs[i].sh_entsize = ~0UL;
+       for (i = 0; i < info->hdr->e_shnum; i++)
+               info->sechdrs[i].sh_entsize = ~0UL;
 
-       DEBUGP("Core section allocation order:\n");
+       pr_debug("Core section allocation order:\n");
        for (m = 0; m < ARRAY_SIZE(masks); ++m) {
-               for (i = 0; i < hdr->e_shnum; ++i) {
-                       Elf_Shdr *s = &sechdrs[i];
+               for (i = 0; i < info->hdr->e_shnum; ++i) {
+                       Elf_Shdr *s = &info->sechdrs[i];
+                       const char *sname = info->secstrings + s->sh_name;
 
                        if ((s->sh_flags & masks[m][0]) != masks[m][0]
                            || (s->sh_flags & masks[m][1])
                            || s->sh_entsize != ~0UL
-                           || strstarts(secstrings + s->sh_name, ".init"))
+                           || strstarts(sname, ".init"))
                                continue;
                        s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
-                       DEBUGP("\t%s\n", secstrings + s->sh_name);
+                       pr_debug("\t%s\n", sname);
                }
-               if (m == 0)
+               switch (m) {
+               case 0: /* executable */
+                       mod->core_size = debug_align(mod->core_size);
                        mod->core_text_size = mod->core_size;
+                       break;
+               case 1: /* RO: text and ro-data */
+                       mod->core_size = debug_align(mod->core_size);
+                       mod->core_ro_size = mod->core_size;
+                       break;
+               case 3: /* whole core */
+                       mod->core_size = debug_align(mod->core_size);
+                       break;
+               }
        }
 
-       DEBUGP("Init section allocation order:\n");
+       pr_debug("Init section allocation order:\n");
        for (m = 0; m < ARRAY_SIZE(masks); ++m) {
-               for (i = 0; i < hdr->e_shnum; ++i) {
-                       Elf_Shdr *s = &sechdrs[i];
+               for (i = 0; i < info->hdr->e_shnum; ++i) {
+                       Elf_Shdr *s = &info->sechdrs[i];
+                       const char *sname = info->secstrings + s->sh_name;
 
                        if ((s->sh_flags & masks[m][0]) != masks[m][0]
                            || (s->sh_flags & masks[m][1])
                            || s->sh_entsize != ~0UL
-                           || !strstarts(secstrings + s->sh_name, ".init"))
+                           || !strstarts(sname, ".init"))
                                continue;
                        s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
                                         | INIT_OFFSET_MASK);
-                       DEBUGP("\t%s\n", secstrings + s->sh_name);
+                       pr_debug("\t%s\n", sname);
                }
-               if (m == 0)
+               switch (m) {
+               case 0: /* executable */
+                       mod->init_size = debug_align(mod->init_size);
                        mod->init_text_size = mod->init_size;
+                       break;
+               case 1: /* RO: text and ro-data */
+                       mod->init_size = debug_align(mod->init_size);
+                       mod->init_ro_size = mod->init_size;
+                       break;
+               case 3: /* whole init */
+                       mod->init_size = debug_align(mod->init_size);
+                       break;
+               }
        }
 }
 
@@ -1842,33 +2130,28 @@ static char *next_string(char *string, unsigned long *secsize)
        return string;
 }
 
-static char *get_modinfo(const Elf_Shdr *sechdrs,
-                        unsigned int info,
-                        const char *tag)
+static char *get_modinfo(struct load_info *info, const char *tag)
 {
        char *p;
        unsigned int taglen = strlen(tag);
-       unsigned long size = sechdrs[info].sh_size;
+       Elf_Shdr *infosec = &info->sechdrs[info->index.info];
+       unsigned long size = infosec->sh_size;
 
-       for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
+       for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
                if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
                        return p + taglen + 1;
        }
        return NULL;
 }
 
-static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
-                         unsigned int infoindex)
+static void setup_modinfo(struct module *mod, struct load_info *info)
 {
        struct module_attribute *attr;
        int i;
 
        for (i = 0; (attr = modinfo_attrs[i]); i++) {
                if (attr->setup)
-                       attr->setup(mod,
-                                   get_modinfo(sechdrs,
-                                               infoindex,
-                                               attr->attr.name));
+                       attr->setup(mod, get_modinfo(info, attr->attr.name));
        }
 }
 
@@ -1890,11 +2173,8 @@ static const struct kernel_symbol *lookup_symbol(const char *name,
        const struct kernel_symbol *start,
        const struct kernel_symbol *stop)
 {
-       const struct kernel_symbol *ks = start;
-       for (; ks < stop; ks++)
-               if (strcmp(ks->name, name) == 0)
-                       return ks;
-       return NULL;
+       return bsearch(name, start, stop - start,
+                       sizeof(struct kernel_symbol), cmp_name);
 }
 
 static int is_exported(const char *name, unsigned long value,
@@ -1909,11 +2189,10 @@ static int is_exported(const char *name, unsigned long value,
 }
 
 /* As per nm */
-static char elf_type(const Elf_Sym *sym,
-                    Elf_Shdr *sechdrs,
-                    const char *secstrings,
-                    struct module *mod)
+static char elf_type(const Elf_Sym *sym, const struct load_info *info)
 {
+       const Elf_Shdr *sechdrs = info->sechdrs;
+
        if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
                if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
                        return 'v';
@@ -1943,8 +2222,10 @@ static char elf_type(const Elf_Sym *sym,
                else
                        return 'b';
        }
-       if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
+       if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
+                     ".debug")) {
                return 'n';
+       }
        return '?';
 }
 
@@ -1969,127 +2250,94 @@ static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
        return true;
 }
 
-static unsigned long layout_symtab(struct module *mod,
-                                  Elf_Shdr *sechdrs,
-                                  unsigned int symindex,
-                                  unsigned int strindex,
-                                  const Elf_Ehdr *hdr,
-                                  const char *secstrings,
-                                  unsigned long *pstroffs,
-                                  unsigned long *strmap)
+/*
+ * We only allocate and copy the strings needed by the parts of symtab
+ * we keep.  This is simple, but has the effect of making multiple
+ * copies of duplicates.  We could be more sophisticated, see
+ * linux-kernel thread starting with
+ * <73defb5e4bca04a6431392cc341112b1@localhost>.
+ */
+static void layout_symtab(struct module *mod, struct load_info *info)
 {
-       unsigned long symoffs;
-       Elf_Shdr *symsect = sechdrs + symindex;
-       Elf_Shdr *strsect = sechdrs + strindex;
+       Elf_Shdr *symsect = info->sechdrs + info->index.sym;
+       Elf_Shdr *strsect = info->sechdrs + info->index.str;
        const Elf_Sym *src;
-       const char *strtab;
-       unsigned int i, nsrc, ndst;
+       unsigned int i, nsrc, ndst, strtab_size;
 
        /* Put symbol section at end of init part of module. */
        symsect->sh_flags |= SHF_ALLOC;
        symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
-                                        symindex) | INIT_OFFSET_MASK;
-       DEBUGP("\t%s\n", secstrings + symsect->sh_name);
+                                        info->index.sym) | INIT_OFFSET_MASK;
+       pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
 
-       src = (void *)hdr + symsect->sh_offset;
+       src = (void *)info->hdr + symsect->sh_offset;
        nsrc = symsect->sh_size / sizeof(*src);
-       strtab = (void *)hdr + strsect->sh_offset;
-       for (ndst = i = 1; i < nsrc; ++i, ++src)
-               if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
-                       unsigned int j = src->st_name;
-
-                       while(!__test_and_set_bit(j, strmap) && strtab[j])
-                               ++j;
-                       ++ndst;
+
+       /* Compute total space required for the core symbols' strtab. */
+       for (ndst = i = strtab_size = 1; i < nsrc; ++i, ++src)
+               if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
+                       strtab_size += strlen(&info->strtab[src->st_name]) + 1;
+                       ndst++;
                }
 
        /* Append room for core symbols at end of core part. */
-       symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
-       mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
+       info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
+       info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
+       mod->core_size += strtab_size;
 
        /* Put string table section at end of init part of module. */
        strsect->sh_flags |= SHF_ALLOC;
        strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
-                                        strindex) | INIT_OFFSET_MASK;
-       DEBUGP("\t%s\n", secstrings + strsect->sh_name);
-
-       /* Append room for core symbols' strings at end of core part. */
-       *pstroffs = mod->core_size;
-       __set_bit(0, strmap);
-       mod->core_size += bitmap_weight(strmap, strsect->sh_size);
-
-       return symoffs;
+                                        info->index.str) | INIT_OFFSET_MASK;
+       pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
 }
 
-static void add_kallsyms(struct module *mod,
-                        Elf_Shdr *sechdrs,
-                        unsigned int shnum,
-                        unsigned int symindex,
-                        unsigned int strindex,
-                        unsigned long symoffs,
-                        unsigned long stroffs,
-                        const char *secstrings,
-                        unsigned long *strmap)
+static void add_kallsyms(struct module *mod, const struct load_info *info)
 {
        unsigned int i, ndst;
        const Elf_Sym *src;
        Elf_Sym *dst;
        char *s;
+       Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
 
-       mod->symtab = (void *)sechdrs[symindex].sh_addr;
-       mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
-       mod->strtab = (void *)sechdrs[strindex].sh_addr;
+       mod->symtab = (void *)symsec->sh_addr;
+       mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
+       /* Make sure we get permanent strtab: don't use info->strtab. */
+       mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
 
        /* Set types up while we still have access to sections. */
        for (i = 0; i < mod->num_symtab; i++)
-               mod->symtab[i].st_info
-                       = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
+               mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
 
-       mod->core_symtab = dst = mod->module_core + symoffs;
+       mod->core_symtab = dst = mod->module_core + info->symoffs;
+       mod->core_strtab = s = mod->module_core + info->stroffs;
        src = mod->symtab;
        *dst = *src;
+       *s++ = 0;
        for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
-               if (!is_core_symbol(src, sechdrs, shnum))
+               if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
                        continue;
+
                dst[ndst] = *src;
-               dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
-               ++ndst;
+               dst[ndst++].st_name = s - mod->core_strtab;
+               s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1;
        }
        mod->core_num_syms = ndst;
-
-       mod->core_strtab = s = mod->module_core + stroffs;
-       for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
-               if (test_bit(i, strmap))
-                       *++s = mod->strtab[i];
 }
 #else
-static inline unsigned long layout_symtab(struct module *mod,
-                                         Elf_Shdr *sechdrs,
-                                         unsigned int symindex,
-                                         unsigned int strindex,
-                                         const Elf_Ehdr *hdr,
-                                         const char *secstrings,
-                                         unsigned long *pstroffs,
-                                         unsigned long *strmap)
+static inline void layout_symtab(struct module *mod, struct load_info *info)
 {
-       return 0;
 }
 
-static inline void add_kallsyms(struct module *mod,
-                               Elf_Shdr *sechdrs,
-                               unsigned int shnum,
-                               unsigned int symindex,
-                               unsigned int strindex,
-                               unsigned long symoffs,
-                               unsigned long stroffs,
-                               const char *secstrings,
-                               const unsigned long *strmap)
+static void add_kallsyms(struct module *mod, const struct load_info *info)
 {
 }
 #endif /* CONFIG_KALLSYMS */
 
 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
 {
+       if (!debug)
+               return;
 #ifdef CONFIG_DYNAMIC_DEBUG
        if (ddebug_add_module(debug, num, debug->modname))
                printk(KERN_ERR "dynamic debug error adding module: %s\n",
@@ -2103,6 +2351,11 @@ static void dynamic_debug_remove(struct _ddebug *debug)
                ddebug_remove_module(debug->modname);
 }
 
+void * __weak module_alloc(unsigned long size)
+{
+       return size == 0 ? NULL : vmalloc_exec(size);
+}
+
 static void *module_alloc_update_bounds(unsigned long size)
 {
        void *ret = module_alloc(size);
@@ -2120,45 +2373,36 @@ static void *module_alloc_update_bounds(unsigned long size)
 }
 
 #ifdef CONFIG_DEBUG_KMEMLEAK
-static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
-                                const Elf_Shdr *sechdrs,
-                                const char *secstrings)
+static void kmemleak_load_module(const struct module *mod,
+                                const struct load_info *info)
 {
        unsigned int i;
 
        /* only scan the sections containing data */
        kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
 
-       for (i = 1; i < hdr->e_shnum; i++) {
-               if (!(sechdrs[i].sh_flags & SHF_ALLOC))
+       for (i = 1; i < info->hdr->e_shnum; i++) {
+               const char *name = info->secstrings + info->sechdrs[i].sh_name;
+               if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
                        continue;
-               if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
-                   && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
+               if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
                        continue;
 
-               kmemleak_scan_area((void *)sechdrs[i].sh_addr,
-                                  sechdrs[i].sh_size, GFP_KERNEL);
+               kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
+                                  info->sechdrs[i].sh_size, GFP_KERNEL);
        }
 }
 #else
-static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
-                                       Elf_Shdr *sechdrs,
-                                       const char *secstrings)
+static inline void kmemleak_load_module(const struct module *mod,
+                                       const struct load_info *info)
 {
 }
 #endif
 
-struct load_info {
-       Elf_Ehdr *hdr;
-       unsigned long len;
-       Elf_Shdr *sechdrs;
-       char *secstrings, *args, *strtab;
-       struct {
-               unsigned int sym, str, mod, vers, info, pcpu;
-       } index;
-};
-
-static int copy_and_check(struct load_info *info, const void __user *umod, unsigned long len)
+/* Sets info->hdr and info->len. */
+static int copy_and_check(struct load_info *info,
+                         const void __user *umod, unsigned long len,
+                         const char __user *uargs)
 {
        int err;
        Elf_Ehdr *hdr;
@@ -2167,8 +2411,7 @@ static int copy_and_check(struct load_info *info, const void __user *umod, unsig
                return -ENOEXEC;
 
        /* Suck in entire file: we'll want most of it. */
-       /* vmalloc barfs on "unusual" numbers.  Check here */
-       if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
+       if ((hdr = vmalloc(len)) == NULL)
                return -ENOMEM;
 
        if (copy_from_user(hdr, umod, len) != 0) {
@@ -2190,6 +2433,7 @@ static int copy_and_check(struct load_info *info, const void __user *umod, unsig
                err = -ENOEXEC;
                goto free_hdr;
        }
+
        info->hdr = hdr;
        info->len = len;
        return 0;
@@ -2199,6 +2443,46 @@ free_hdr:
        return err;
 }
 
+static void free_copy(struct load_info *info)
+{
+       vfree(info->hdr);
+}
+
+static int rewrite_section_headers(struct load_info *info)
+{
+       unsigned int i;
+
+       /* This should always be true, but let's be sure. */
+       info->sechdrs[0].sh_addr = 0;
+
+       for (i = 1; i < info->hdr->e_shnum; i++) {
+               Elf_Shdr *shdr = &info->sechdrs[i];
+               if (shdr->sh_type != SHT_NOBITS
+                   && info->len < shdr->sh_offset + shdr->sh_size) {
+                       printk(KERN_ERR "Module len %lu truncated\n",
+                              info->len);
+                       return -ENOEXEC;
+               }
+
+               /* Mark all sections sh_addr with their address in the
+                  temporary image. */
+               shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
+
+#ifndef CONFIG_MODULE_UNLOAD
+               /* Don't load .exit sections */
+               if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
+                       shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
+#endif
+       }
+
+       /* Track but don't keep modinfo and version sections. */
+       info->index.vers = find_sec(info, "__versions");
+       info->index.info = find_sec(info, ".modinfo");
+       info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
+       info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
+       return 0;
+}
+
 /*
  * Set up our basic convenience variables (pointers to section headers,
  * search for module section index etc), and do some basic section
@@ -2210,37 +2494,30 @@ free_hdr:
 static struct module *setup_load_info(struct load_info *info)
 {
        unsigned int i;
+       int err;
        struct module *mod;
 
        /* Set up the convenience variables */
        info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
-       info->secstrings = (void *)info->hdr + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
-       info->sechdrs[0].sh_addr = 0;
+       info->secstrings = (void *)info->hdr
+               + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
 
-       for (i = 1; i < info->hdr->e_shnum; i++) {
-               if (info->sechdrs[i].sh_type != SHT_NOBITS
-                   && info->len < info->sechdrs[i].sh_offset + info->sechdrs[i].sh_size)
-                       goto truncated;
-
-               /* Mark all sections sh_addr with their address in the
-                  temporary image. */
-               info->sechdrs[i].sh_addr = (size_t)info->hdr + info->sechdrs[i].sh_offset;
+       err = rewrite_section_headers(info);
+       if (err)
+               return ERR_PTR(err);
 
-               /* Internal symbols and strings. */
+       /* Find internal symbols and strings. */
+       for (i = 1; i < info->hdr->e_shnum; i++) {
                if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
                        info->index.sym = i;
                        info->index.str = info->sechdrs[i].sh_link;
-                       info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset;
+                       info->strtab = (char *)info->hdr
+                               + info->sechdrs[info->index.str].sh_offset;
+                       break;
                }
-#ifndef CONFIG_MODULE_UNLOAD
-               /* Don't load .exit sections */
-               if (strstarts(info->secstrings+info->sechdrs[i].sh_name, ".exit"))
-                       info->sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
-#endif
        }
 
-       info->index.mod = find_sec(info->hdr, info->sechdrs, info->secstrings,
-                           ".gnu.linkonce.this_module");
+       info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
        if (!info->index.mod) {
                printk(KERN_WARNING "No module found in object\n");
                return ERR_PTR(-ENOEXEC);
@@ -2254,30 +2531,18 @@ static struct module *setup_load_info(struct load_info *info)
                return ERR_PTR(-ENOEXEC);
        }
 
-       info->index.vers = find_sec(info->hdr, info->sechdrs, info->secstrings, "__versions");
-       info->index.info = find_sec(info->hdr, info->sechdrs, info->secstrings, ".modinfo");
-       info->index.pcpu = find_pcpusec(info->hdr, info->sechdrs, info->secstrings);
-
-       /* Don't keep modinfo and version sections. */
-       info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
-       info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
+       info->index.pcpu = find_pcpusec(info);
 
        /* Check module struct version now, before we try to use module. */
        if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
                return ERR_PTR(-ENOEXEC);
 
        return mod;
-
- truncated:
-       printk(KERN_ERR "Module len %lu truncated\n", info->len);
-       return ERR_PTR(-ENOEXEC);
 }
 
-static int check_modinfo(struct module *mod,
-                        const Elf_Shdr *sechdrs,
-                        unsigned int infoindex, unsigned int versindex)
+static int check_modinfo(struct module *mod, struct load_info *info)
 {
-       const char *modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
+       const char *modmagic = get_modinfo(info, "vermagic");
        int err;
 
        /* This is allowed: modprobe --force will invalidate it. */
@@ -2285,13 +2550,16 @@ static int check_modinfo(struct module *mod,
                err = try_to_force_load(mod, "bad vermagic");
                if (err)
                        return err;
-       } else if (!same_magic(modmagic, vermagic, versindex)) {
+       } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
                printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
                       mod->name, modmagic, vermagic);
                return -ENOEXEC;
        }
 
-       if (get_modinfo(sechdrs, infoindex, "staging")) {
+       if (!get_modinfo(info, "intree"))
+               add_taint_module(mod, TAINT_OOT_MODULE);
+
+       if (get_modinfo(info, "staging")) {
                add_taint_module(mod, TAINT_CRAP);
                printk(KERN_WARNING "%s: module is from the staging directory,"
                       " the quality is unknown, you have been warned.\n",
@@ -2299,58 +2567,55 @@ static int check_modinfo(struct module *mod,
        }
 
        /* Set up license info based on the info section */
-       set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
+       set_license(mod, get_modinfo(info, "license"));
 
        return 0;
 }
 
-static void find_module_sections(struct module *mod, Elf_Ehdr *hdr,
-                                Elf_Shdr *sechdrs, const char *secstrings)
+static void find_module_sections(struct module *mod, struct load_info *info)
 {
-       mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
+       mod->kp = section_objs(info, "__param",
                               sizeof(*mod->kp), &mod->num_kp);
-       mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
+       mod->syms = section_objs(info, "__ksymtab",
                                 sizeof(*mod->syms), &mod->num_syms);
-       mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
-       mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
+       mod->crcs = section_addr(info, "__kcrctab");
+       mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
                                     sizeof(*mod->gpl_syms),
                                     &mod->num_gpl_syms);
-       mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
-       mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
+       mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
+       mod->gpl_future_syms = section_objs(info,
                                            "__ksymtab_gpl_future",
                                            sizeof(*mod->gpl_future_syms),
                                            &mod->num_gpl_future_syms);
-       mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
-                                           "__kcrctab_gpl_future");
+       mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
 
 #ifdef CONFIG_UNUSED_SYMBOLS
-       mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
-                                       "__ksymtab_unused",
+       mod->unused_syms = section_objs(info, "__ksymtab_unused",
                                        sizeof(*mod->unused_syms),
                                        &mod->num_unused_syms);
-       mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
-                                       "__kcrctab_unused");
-       mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
-                                           "__ksymtab_unused_gpl",
+       mod->unused_crcs = section_addr(info, "__kcrctab_unused");
+       mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
                                            sizeof(*mod->unused_gpl_syms),
                                            &mod->num_unused_gpl_syms);
-       mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
-                                           "__kcrctab_unused_gpl");
+       mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
 #endif
 #ifdef CONFIG_CONSTRUCTORS
-       mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
+       mod->ctors = section_objs(info, CONFIG_GCOV_CTORS,
                                  sizeof(*mod->ctors), &mod->num_ctors);
 #endif
 
 #ifdef CONFIG_TRACEPOINTS
-       mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
-                                       "__tracepoints",
-                                       sizeof(*mod->tracepoints),
-                                       &mod->num_tracepoints);
+       mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
+                                            sizeof(*mod->tracepoints_ptrs),
+                                            &mod->num_tracepoints);
+#endif
+#ifdef HAVE_JUMP_LABEL
+       mod->jump_entries = section_objs(info, "__jump_table",
+                                       sizeof(*mod->jump_entries),
+                                       &mod->num_jump_entries);
 #endif
 #ifdef CONFIG_EVENT_TRACING
-       mod->trace_events = section_objs(hdr, sechdrs, secstrings,
-                                        "_ftrace_events",
+       mod->trace_events = section_objs(info, "_ftrace_events",
                                         sizeof(*mod->trace_events),
                                         &mod->num_trace_events);
        /*
@@ -2360,22 +2625,37 @@ static void find_module_sections(struct module *mod, Elf_Ehdr *hdr,
        kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
                           mod->num_trace_events, GFP_KERNEL);
 #endif
+#ifdef CONFIG_TRACING
+       mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
+                                        sizeof(*mod->trace_bprintk_fmt_start),
+                                        &mod->num_trace_bprintk_fmt);
+       /*
+        * This section contains pointers to allocated objects in the trace
+        * code and not scanning it leads to false positives.
+        */
+       kmemleak_scan_area(mod->trace_bprintk_fmt_start,
+                          sizeof(*mod->trace_bprintk_fmt_start) *
+                          mod->num_trace_bprintk_fmt, GFP_KERNEL);
+#endif
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
        /* sechdrs[0].sh_size is always zero */
-       mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
-                                            "__mcount_loc",
+       mod->ftrace_callsites = section_objs(info, "__mcount_loc",
                                             sizeof(*mod->ftrace_callsites),
                                             &mod->num_ftrace_callsites);
 #endif
 
-       if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
+       mod->extable = section_objs(info, "__ex_table",
+                                   sizeof(*mod->extable), &mod->num_exentries);
+
+       if (section_addr(info, "__obsparm"))
                printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
                       mod->name);
+
+       info->debug = section_objs(info, "__verbose",
+                                  sizeof(*info->debug), &info->num_debug);
 }
 
-static struct module *move_module(struct module *mod,
-                                 Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
-                                 const char *secstrings, unsigned modindex)
+static int move_module(struct module *mod, struct load_info *info)
 {
        int i;
        void *ptr;
@@ -2389,7 +2669,7 @@ static struct module *move_module(struct module *mod,
         */
        kmemleak_not_leak(ptr);
        if (!ptr)
-               return ERR_PTR(-ENOMEM);
+               return -ENOMEM;
 
        memset(ptr, 0, mod->core_size);
        mod->module_core = ptr;
@@ -2404,41 +2684,38 @@ static struct module *move_module(struct module *mod,
        kmemleak_ignore(ptr);
        if (!ptr && mod->init_size) {
                module_free(mod, mod->module_core);
-               return ERR_PTR(-ENOMEM);
+               return -ENOMEM;
        }
        memset(ptr, 0, mod->init_size);
        mod->module_init = ptr;
 
        /* Transfer each section which specifies SHF_ALLOC */
-       DEBUGP("final section addresses:\n");
-       for (i = 0; i < hdr->e_shnum; i++) {
+       pr_debug("final section addresses:\n");
+       for (i = 0; i < info->hdr->e_shnum; i++) {
                void *dest;
+               Elf_Shdr *shdr = &info->sechdrs[i];
 
-               if (!(sechdrs[i].sh_flags & SHF_ALLOC))
+               if (!(shdr->sh_flags & SHF_ALLOC))
                        continue;
 
-               if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
+               if (shdr->sh_entsize & INIT_OFFSET_MASK)
                        dest = mod->module_init
-                               + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
+                               + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
                else
-                       dest = mod->module_core + sechdrs[i].sh_entsize;
+                       dest = mod->module_core + shdr->sh_entsize;
 
-               if (sechdrs[i].sh_type != SHT_NOBITS)
-                       memcpy(dest, (void *)sechdrs[i].sh_addr,
-                              sechdrs[i].sh_size);
+               if (shdr->sh_type != SHT_NOBITS)
+                       memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
                /* Update sh_addr to point to copy in image. */
-               sechdrs[i].sh_addr = (unsigned long)dest;
-               DEBUGP("\t0x%lx %s\n",
-                      sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
+               shdr->sh_addr = (unsigned long)dest;
+               pr_debug("\t0x%lx %s\n",
+                        (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
        }
-       /* Module has been moved. */
-       mod = (void *)sechdrs[modindex].sh_addr;
-       kmemleak_load_module(mod, hdr, sechdrs, secstrings);
-       return mod;
+
+       return 0;
 }
 
-static int check_module_license_and_versions(struct module *mod,
-                                            Elf_Shdr *sechdrs)
+static int check_module_license_and_versions(struct module *mod)
 {
        /*
         * ndiswrapper is under GPL by itself, but loads proprietary modules.
@@ -2491,137 +2768,166 @@ static void flush_module_icache(const struct module *mod)
        set_fs(old_fs);
 }
 
-/* Allocate and load the module: note that size of section 0 is always
-   zero, and we rely on this for optional sections. */
-static noinline struct module *load_module(void __user *umod,
-                                 unsigned long len,
-                                 const char __user *uargs)
+int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
+                                    Elf_Shdr *sechdrs,
+                                    char *secstrings,
+                                    struct module *mod)
 {
-       struct load_info info = { NULL, };
+       return 0;
+}
+
+static struct module *layout_and_allocate(struct load_info *info)
+{
+       /* Module within temporary copy. */
        struct module *mod;
-       long err;
-       unsigned long symoffs, stroffs, *strmap;
-       void __percpu *percpu;
-       struct _ddebug *debug = NULL;
-       unsigned int num_debug = 0;
+       Elf_Shdr *pcpusec;
+       int err;
 
-       DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
-              umod, len, uargs);
+       mod = setup_load_info(info);
+       if (IS_ERR(mod))
+               return mod;
 
-       err = copy_and_check(&info, umod, len);
+       err = check_modinfo(mod, info);
        if (err)
                return ERR_PTR(err);
 
-       mod = setup_load_info(&info);
-       if (IS_ERR(mod)) {
-               err = PTR_ERR(mod);
-               goto free_hdr;
-       }
-
-       err = check_modinfo(mod, info.sechdrs, info.index.info, info.index.vers);
-       if (err)
-               goto free_hdr;
-
-       /* Now copy in args */
-       info.args = strndup_user(uargs, ~0UL >> 1);
-       if (IS_ERR(info.args)) {
-               err = PTR_ERR(info.args);
-               goto free_hdr;
-       }
-
-       strmap = kzalloc(BITS_TO_LONGS(info.sechdrs[info.index.str].sh_size)
-                        * sizeof(long), GFP_KERNEL);
-       if (!strmap) {
-               err = -ENOMEM;
-               goto free_mod;
-       }
-
-       mod->state = MODULE_STATE_COMING;
-
        /* Allow arches to frob section contents and sizes.  */
-       err = module_frob_arch_sections(info.hdr, info.sechdrs, info.secstrings, mod);
+       err = module_frob_arch_sections(info->hdr, info->sechdrs,
+                                       info->secstrings, mod);
        if (err < 0)
-               goto free_mod;
+               goto out;
 
-       if (info.index.pcpu) {
+       pcpusec = &info->sechdrs[info->index.pcpu];
+       if (pcpusec->sh_size) {
                /* We have a special allocation for this section. */
-               err = percpu_modalloc(mod, info.sechdrs[info.index.pcpu].sh_size,
-                                     info.sechdrs[info.index.pcpu].sh_addralign);
+               err = percpu_modalloc(mod,
+                                     pcpusec->sh_size, pcpusec->sh_addralign);
                if (err)
-                       goto free_mod;
-               info.sechdrs[info.index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
+                       goto out;
+               pcpusec->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
           special cases for the architectures. */
-       layout_sections(mod, info.hdr, info.sechdrs, info.secstrings);
-       symoffs = layout_symtab(mod, info.sechdrs, info.index.sym, info.index.str, info.hdr,
-                               info.secstrings, &stroffs, strmap);
+       layout_sections(mod, info);
+       layout_symtab(mod, info);
 
        /* Allocate and move to the final place */
-       mod = move_module(mod, info.hdr, info.sechdrs, info.secstrings, info.index.mod);
+       err = move_module(mod, info);
+       if (err)
+               goto free_percpu;
+
+       /* Module has been copied to its final place now: return it. */
+       mod = (void *)info->sechdrs[info->index.mod].sh_addr;
+       kmemleak_load_module(mod, info);
+       return mod;
+
+free_percpu:
+       percpu_modfree(mod);
+out:
+       return ERR_PTR(err);
+}
+
+/* mod is no longer valid after this! */
+static void module_deallocate(struct module *mod, struct load_info *info)
+{
+       percpu_modfree(mod);
+       module_free(mod, mod->module_init);
+       module_free(mod, mod->module_core);
+}
+
+int __weak module_finalize(const Elf_Ehdr *hdr,
+                          const Elf_Shdr *sechdrs,
+                          struct module *me)
+{
+       return 0;
+}
+
+static int post_relocation(struct module *mod, const struct load_info *info)
+{
+       /* Sort exception table now relocations are done. */
+       sort_extable(mod->extable, mod->extable + mod->num_exentries);
+
+       /* Copy relocated percpu area over. */
+       percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
+                      info->sechdrs[info->index.pcpu].sh_size);
+
+       /* Setup kallsyms-specific fields. */
+       add_kallsyms(mod, info);
+
+       /* Arch-specific module finalizing. */
+       return module_finalize(info->hdr, info->sechdrs, mod);
+}
+
+/* Allocate and load the module: note that size of section 0 is always
+   zero, and we rely on this for optional sections. */
+static struct module *load_module(void __user *umod,
+                                 unsigned long len,
+                                 const char __user *uargs)
+{
+       struct load_info info = { NULL, };
+       struct module *mod;
+       long err;
+
+       pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n",
+              umod, len, uargs);
+
+       /* Copy in the blobs from userspace, check they are vaguely sane. */
+       err = copy_and_check(&info, umod, len, uargs);
+       if (err)
+               return ERR_PTR(err);
+
+       /* Figure out module layout, and allocate all the memory. */
+       mod = layout_and_allocate(&info);
        if (IS_ERR(mod)) {
                err = PTR_ERR(mod);
-               goto free_percpu;
+               goto free_copy;
        }
 
-       /* Now we've moved module, initialize linked lists, etc. */
+       /* Now module is in final location, initialize linked lists, etc. */
        err = module_unload_init(mod);
        if (err)
-               goto free_init;
+               goto free_module;
 
        /* Now we've got everything in the final locations, we can
         * find optional sections. */
-       find_module_sections(mod, info.hdr, info.sechdrs, info.secstrings);
+       find_module_sections(mod, &info);
 
-       err = check_module_license_and_versions(mod, info.sechdrs);
+       err = check_module_license_and_versions(mod);
        if (err)
                goto free_unload;
 
        /* Set up MODINFO_ATTR fields */
-       setup_modinfo(mod, info.sechdrs, info.index.info);
+       setup_modinfo(mod, &info);
 
        /* Fix up syms, so that st_value is a pointer to location. */
-       err = simplify_symbols(info.sechdrs, info.index.sym, info.strtab, info.index.vers, info.index.pcpu,
-                              mod);
+       err = simplify_symbols(mod, &info);
        if (err < 0)
-               goto cleanup;
+               goto free_modinfo;
 
-       err = apply_relocations(mod, info.hdr, info.sechdrs, info.index.sym, info.index.str);
+       err = apply_relocations(mod, &info);
        if (err < 0)
-               goto cleanup;
-
-       /* Set up and sort exception table */
-       mod->extable = section_objs(info.hdr, info.sechdrs, info.secstrings, "__ex_table",
-                                   sizeof(*mod->extable), &mod->num_exentries);
-       sort_extable(mod->extable, mod->extable + mod->num_exentries);
+               goto free_modinfo;
 
-       /* Finally, copy percpu area over. */
-       percpu_modcopy(mod, (void *)info.sechdrs[info.index.pcpu].sh_addr,
-                      info.sechdrs[info.index.pcpu].sh_size);
-
-       add_kallsyms(mod, info.sechdrs, info.hdr->e_shnum, info.index.sym, info.index.str,
-                    symoffs, stroffs, info.secstrings, strmap);
-       kfree(strmap);
-       strmap = NULL;
-
-       if (!mod->taints)
-               debug = section_objs(info.hdr, info.sechdrs, info.secstrings, "__verbose",
-                                    sizeof(*debug), &num_debug);
-
-       err = module_finalize(info.hdr, info.sechdrs, mod);
+       err = post_relocation(mod, &info);
        if (err < 0)
-               goto cleanup;
+               goto free_modinfo;
 
        flush_module_icache(mod);
 
-       mod->args = info.args;
+       /* Now copy in args */
+       mod->args = strndup_user(uargs, ~0UL >> 1);
+       if (IS_ERR(mod->args)) {
+               err = PTR_ERR(mod->args);
+               goto free_arch_cleanup;
+       }
+
+       /* Mark state as coming so strong_try_module_get() ignores us. */
+       mod->state = MODULE_STATE_COMING;
 
        /* Now sew it into the lists so we can get lockdep and oops
-        * info during argument parsing.  Noone should access us, since
+        * info during argument parsing.  No one should access us, since
         * strong_try_module_get() will fail.
         * lockdep/oops can run asynchronous, so use the RCU list insertion
         * function to insert in a way safe to concurrent readers.
@@ -2633,61 +2939,58 @@ static noinline struct module *load_module(void __user *umod,
                goto unlock;
        }
 
-       if (debug)
-               dynamic_debug_setup(debug, num_debug);
+       /* This has to be done once we're sure module name is unique. */
+       dynamic_debug_setup(info.debug, info.num_debug);
 
        /* Find duplicate symbols */
        err = verify_export_symbols(mod);
        if (err < 0)
                goto ddebug;
 
+       module_bug_finalize(info.hdr, info.sechdrs, mod);
        list_add_rcu(&mod->list, &modules);
        mutex_unlock(&module_mutex);
 
-       err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
+       /* Module is ready to execute: parsing args may do that. */
+       err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
+                        -32768, 32767, NULL);
        if (err < 0)
                goto unlink;
 
-       err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
+       /* Link in to syfs. */
+       err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
        if (err < 0)
                goto unlink;
 
-       add_sect_attrs(mod, info.hdr->e_shnum, info.secstrings, info.sechdrs);
-       add_notes_attrs(mod, info.hdr->e_shnum, info.secstrings, info.sechdrs);
-
-       /* Get rid of temporary copy */
-       vfree(info.hdr);
-
-       trace_module_load(mod);
+       /* Get rid of temporary copy. */
+       free_copy(&info);
 
        /* Done! */
+       trace_module_load(mod);
        return mod;
 
  unlink:
        mutex_lock(&module_mutex);
        /* Unlink carefully: kallsyms could be walking list. */
        list_del_rcu(&mod->list);
+       module_bug_cleanup(mod);
+
  ddebug:
-       dynamic_debug_remove(debug);
+       dynamic_debug_remove(info.debug);
  unlock:
        mutex_unlock(&module_mutex);
        synchronize_sched();
+       kfree(mod->args);
+ free_arch_cleanup:
        module_arch_cleanup(mod);
cleanup:
free_modinfo:
        free_modinfo(mod);
  free_unload:
        module_unload_free(mod);
- free_init:
-       module_free(mod, mod->module_init);
-       module_free(mod, mod->module_core);
-       /* mod will be freed with core. Don't access it beyond this line! */
- free_percpu:
-       free_percpu(percpu);
- free_mod:
-       kfree(info.args);
-       kfree(strmap);
- free_hdr:
-       vfree(info.hdr);
+ free_module:
+       module_deallocate(mod, &info);
+ free_copy:
+       free_copy(&info);
        return ERR_PTR(err);
 }
 
@@ -2721,6 +3024,18 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
        blocking_notifier_call_chain(&module_notify_list,
                        MODULE_STATE_COMING, mod);
 
+       /* Set RO and NX regions for core */
+       set_section_ro_nx(mod->module_core,
+                               mod->core_text_size,
+                               mod->core_ro_size,
+                               mod->core_size);
+
+       /* Set RO and NX regions for init */
+       set_section_ro_nx(mod->module_init,
+                               mod->init_text_size,
+                               mod->init_ro_size,
+                               mod->init_size);
+
        do_mod_ctors(mod);
        /* Start the module */
        if (mod->init != NULL)
@@ -2764,9 +3079,11 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
        mod->symtab = mod->core_symtab;
        mod->strtab = mod->core_strtab;
 #endif
+       unset_module_init_ro_nx(mod);
        module_free(mod, mod->module_init);
        mod->module_init = NULL;
        mod->init_size = 0;
+       mod->init_ro_size = 0;
        mod->init_text_size = 0;
        mutex_unlock(&module_mutex);
 
@@ -2803,7 +3120,7 @@ static const char *get_ksymbol(struct module *mod,
        else
                nextval = (unsigned long)mod->module_core+mod->core_text_size;
 
-       /* Scan for closest preceeding symbol, and next symbol. (ELF
+       /* Scan for closest preceding symbol, and next symbol. (ELF
           starts real symbols at 1). */
        for (i = 1; i < mod->num_symtab; i++) {
                if (mod->symtab[i].st_shndx == SHN_UNDEF)
@@ -2998,18 +3315,7 @@ static char *module_flags(struct module *mod, char *buf)
            mod->state == MODULE_STATE_GOING ||
            mod->state == MODULE_STATE_COMING) {
                buf[bx++] = '(';
-               if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
-                       buf[bx++] = 'P';
-               if (mod->taints & (1 << TAINT_FORCED_MODULE))
-                       buf[bx++] = 'F';
-               if (mod->taints & (1 << TAINT_CRAP))
-                       buf[bx++] = 'C';
-               /*
-                * TAINT_FORCED_RMMOD: could be added.
-                * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
-                * apply to modules.
-                */
-
+               bx += module_flags_taint(mod, buf + bx);
                /* Show a - for module-is-being-unloaded */
                if (mod->state == MODULE_STATE_GOING)
                        buf[bx++] = '-';
@@ -3056,7 +3362,7 @@ static int m_show(struct seq_file *m, void *p)
                   mod->state == MODULE_STATE_COMING ? "Loading":
                   "Live");
        /* Used by oprofile and other similar tools. */
-       seq_printf(m, " 0x%p", mod->module_core);
+       seq_printf(m, " 0x%pK", mod->module_core);
 
        /* Taints info */
        if (mod->taints)
@@ -3225,55 +3531,8 @@ void module_layout(struct module *mod,
                   struct modversion_info *ver,
                   struct kernel_param *kp,
                   struct kernel_symbol *ks,
-                  struct tracepoint *tp)
+                  struct tracepoint * const *tp)
 {
 }
 EXPORT_SYMBOL(module_layout);
 #endif
-
-#ifdef CONFIG_TRACEPOINTS
-void module_update_tracepoints(void)
-{
-       struct module *mod;
-
-       mutex_lock(&module_mutex);
-       list_for_each_entry(mod, &modules, list)
-               if (!mod->taints)
-                       tracepoint_update_probe_range(mod->tracepoints,
-                               mod->tracepoints + mod->num_tracepoints);
-       mutex_unlock(&module_mutex);
-}
-
-/*
- * Returns 0 if current not found.
- * Returns 1 if current found.
- */
-int module_get_iter_tracepoints(struct tracepoint_iter *iter)
-{
-       struct module *iter_mod;
-       int found = 0;
-
-       mutex_lock(&module_mutex);
-       list_for_each_entry(iter_mod, &modules, list) {
-               if (!iter_mod->taints) {
-                       /*
-                        * Sorted module list
-                        */
-                       if (iter_mod < iter->module)
-                               continue;
-                       else if (iter_mod > iter->module)
-                               iter->tracepoint = NULL;
-                       found = tracepoint_get_iter_range(&iter->tracepoint,
-                               iter_mod->tracepoints,
-                               iter_mod->tracepoints
-                                       + iter_mod->num_tracepoints);
-                       if (found) {
-                               iter->module = iter_mod;
-                               break;
-                       }
-               }
-       }
-       mutex_unlock(&module_mutex);
-       return found;
-}
-#endif