UBUNTU: Ubuntu-2.6.38-12.51
[linux-flexiantxendom0-natty.git] / kernel / module.c
index a64b26c..1f9f7bc 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
@@ -55,6 +55,8 @@
 #include <linux/async.h>
 #include <linux/percpu.h>
 #include <linux/kmemleak.h>
+#include <linux/jump_label.h>
+#include <linux/pfn.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/module.h>
 #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))
 
@@ -114,9 +136,11 @@ struct load_info {
        Elf_Ehdr *hdr;
        unsigned long len;
        Elf_Shdr *sechdrs;
-       char *secstrings, *args, *strtab;
+       char *secstrings, *strtab;
        unsigned long *strmap;
        unsigned long symoffs, stroffs;
+       struct _ddebug *debug;
+       unsigned int num_debug;
        struct {
                unsigned int sym, str, mod, vers, info, pcpu;
        } index;
@@ -152,42 +176,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 */
@@ -417,11 +437,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,
@@ -481,9 +499,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;
 }
@@ -1067,10 +1083,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;
@@ -1084,7 +1099,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;
        }
@@ -1103,21 +1119,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;
 }
@@ -1153,7 +1168,7 @@ static ssize_t module_sect_show(struct module_attribute *mattr,
 {
        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)
@@ -1544,9 +1559,119 @@ 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);
+       }
+}
+
+/* Setting memory back to RW+NX before releasing it */
+void unset_section_ro_nx(struct module *mod, void *module_region)
+{
+       unsigned long total_pages;
+
+       if (mod->module_core == module_region) {
+               /* Set core as NX+RW */
+               total_pages = MOD_NUMBER_OF_PAGES(mod->module_core, mod->core_size);
+               set_memory_nx((unsigned long)mod->module_core, total_pages);
+               set_memory_rw((unsigned long)mod->module_core, total_pages);
+
+       } else if (mod->module_init == module_region) {
+               /* Set init as NX+RW */
+               total_pages = MOD_NUMBER_OF_PAGES(mod->module_init, mod->init_size);
+               set_memory_nx((unsigned long)mod->module_init, total_pages);
+               set_memory_rw((unsigned long)mod->module_init, total_pages);
+       }
+}
+
+/* Iterate through all modules and set each module's text as RW */
+void set_all_modules_text_rw()
+{
+       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()
+{
+       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 inline void unset_section_ro_nx(struct module *mod, void *module_region) { }
+#endif
+
 /* Free a module, remove from lists, etc. */
 static void free_module(struct module *mod)
 {
@@ -1571,6 +1696,7 @@ static void free_module(struct module *mod)
        destroy_params(mod->kp, mod->num_kp);
 
        /* This may be NULL, but that's OK */
+       unset_section_ro_nx(mod, mod->module_init);
        module_free(mod, mod->module_init);
        kfree(mod->args);
        percpu_modfree(mod);
@@ -1579,6 +1705,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_section_ro_nx(mod, mod->module_core);
        module_free(mod, mod->module_core);
 
 #ifdef CONFIG_MPU
@@ -1640,25 +1767,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);
+                       DEBUGP("Common symbol: %s\n", name);
                        printk("%s: please compile with -fno-common\n",
                               mod->name);
                        ret = -ENOEXEC;
@@ -1671,9 +1796,7 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
                        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;
@@ -1685,17 +1808,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;
                }
@@ -1704,33 +1826,29 @@ 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)
+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;
        }
@@ -1761,10 +1879,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
@@ -1777,42 +1892,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");
        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);
+                       DEBUGP("\t%s\n", name);
                }
-               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");
        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);
+                       DEBUGP("\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;
+               }
        }
 }
 
@@ -1848,33 +1987,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));
        }
 }
 
@@ -1976,59 +2110,48 @@ 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)
+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;
 
        /* 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;
+       DEBUGP("\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)) {
+               if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
                        unsigned int j = src->st_name;
 
-                       while(!__test_and_set_bit(j, strmap) && strtab[j])
+                       while (!__test_and_set_bit(j, info->strmap)
+                              && info->strtab[j])
                                ++j;
                        ++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);
+       mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
 
        /* 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);
+                                        info->index.str) | INIT_OFFSET_MASK;
+       DEBUGP("\t%s\n", info->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->stroffs = mod->core_size;
+       __set_bit(0, info->strmap);
+       mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
 }
 
-static void add_kallsyms(struct module *mod, struct load_info *info)
+static void add_kallsyms(struct module *mod, const struct load_info *info)
 {
        unsigned int i, ndst;
        const Elf_Sym *src;
@@ -2064,25 +2187,19 @@ static void add_kallsyms(struct module *mod, struct load_info *info)
                        *++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 void add_kallsyms(struct module *mod, struct load_info *info)
+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",
@@ -2113,35 +2230,33 @@ 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
 
-/* Sets info->hdr, info->len and info->args. */
+/* 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)
@@ -2177,13 +2292,6 @@ static int copy_and_check(struct load_info *info,
                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;
-       }
-
        info->hdr = hdr;
        info->len = len;
        return 0;
@@ -2195,7 +2303,6 @@ free_hdr:
 
 static void free_copy(struct load_info *info)
 {
-       kfree(info->args);
        vfree(info->hdr);
 }
 
@@ -2227,8 +2334,8 @@ static int rewrite_section_headers(struct load_info *info)
        }
 
        /* Track but don't keep modinfo and version sections. */
-       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.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;
@@ -2268,8 +2375,7 @@ static struct module *setup_load_info(struct load_info *info)
                }
        }
 
-       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);
@@ -2283,7 +2389,7 @@ static struct module *setup_load_info(struct load_info *info)
                return ERR_PTR(-ENOEXEC);
        }
 
-       info->index.pcpu = find_pcpusec(info->hdr, info->sechdrs, info->secstrings);
+       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))
@@ -2292,11 +2398,9 @@ static struct module *setup_load_info(struct load_info *info)
        return mod;
 }
 
-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. */
@@ -2304,13 +2408,13 @@ 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, "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",
@@ -2318,58 +2422,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, ".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);
        /*
@@ -2379,22 +2480,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 int 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;
@@ -2430,32 +2546,31 @@ static int move_module(struct module *mod,
 
        /* Transfer each section which specifies SHF_ALLOC */
        DEBUGP("final section addresses:\n");
-       for (i = 0; i < hdr->e_shnum; i++) {
+       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;
+               shdr->sh_addr = (unsigned long)dest;
                DEBUGP("\t0x%lx %s\n",
-                      sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
+                      shdr->sh_addr, info->secstrings + shdr->sh_name);
        }
 
        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.
@@ -2512,34 +2627,37 @@ static struct module *layout_and_allocate(struct load_info *info)
 {
        /* Module within temporary copy. */
        struct module *mod;
+       Elf_Shdr *pcpusec;
        int err;
 
        mod = setup_load_info(info);
        if (IS_ERR(mod))
                return mod;
 
-       err = check_modinfo(mod, info->sechdrs, info->index.info, info->index.vers);
+       err = check_modinfo(mod, info);
        if (err)
                return ERR_PTR(err);
 
        /* 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_args;
+               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_args;
-               info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
+                       goto out;
+               pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
        }
 
        /* 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);
+       layout_sections(mod, info);
 
        info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
                         * sizeof(long), GFP_KERNEL);
@@ -2547,25 +2665,23 @@ static struct module *layout_and_allocate(struct load_info *info)
                err = -ENOMEM;
                goto free_percpu;
        }
-       info->symoffs = layout_symtab(mod, info->sechdrs, info->index.sym, info->index.str, info->hdr,
-                               info->secstrings, &info->stroffs, info->strmap);
+       layout_symtab(mod, info);
 
        /* Allocate and move to the final place */
-       err = move_module(mod, info->hdr, info->sechdrs, info->secstrings, info->index.mod);
+       err = move_module(mod, info);
        if (err)
                goto free_strmap;
 
        /* 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->hdr, info->sechdrs, info->secstrings);
+       kmemleak_load_module(mod, info);
        return mod;
 
 free_strmap:
        kfree(info->strmap);
 free_percpu:
        percpu_modfree(mod);
-free_args:
-       kfree(info->args);
+out:
        return ERR_PTR(err);
 }
 
@@ -2578,17 +2694,31 @@ static void module_deallocate(struct module *mod, struct load_info *info)
        module_free(mod, mod->module_core);
 }
 
+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 noinline struct module *load_module(void __user *umod,
+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;
-       struct _ddebug *debug = NULL;
-       unsigned int num_debug = 0;
 
        DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
               umod, len, uargs);
@@ -2605,55 +2735,45 @@ static noinline struct module *load_module(void __user *umod,
                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_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 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 free_modinfo;
 
-       /* 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);
-
-       /* 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);
-
-       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 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
@@ -2669,21 +2789,25 @@ 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. */
+       if (!mod->taints)
+               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);
 
+       /* Module is ready to execute: parsing args may do that. */
        err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
        if (err < 0)
                goto unlink;
 
+       /* Link in to syfs. */
        err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
        if (err < 0)
                goto unlink;
@@ -2692,20 +2816,24 @@ static noinline struct module *load_module(void __user *umod,
        kfree(info.strmap);
        free_copy(&info);
 
-       trace_module_load(mod);
-
        /* 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);
+       if (!mod->taints)
+               dynamic_debug_remove(info.debug);
  unlock:
        mutex_unlock(&module_mutex);
        synchronize_sched();
+       kfree(mod->args);
+ free_arch_cleanup:
        module_arch_cleanup(mod);
  free_modinfo:
        free_modinfo(mod);
@@ -2748,6 +2876,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)
@@ -2791,6 +2931,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
        mod->symtab = mod->core_symtab;
        mod->strtab = mod->core_strtab;
 #endif
+       unset_section_ro_nx(mod, mod->module_init);
        module_free(mod, mod->module_init);
        mod->module_init = NULL;
        mod->init_size = 0;
@@ -3083,7 +3224,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)
@@ -3252,7 +3393,7 @@ 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);
@@ -3266,8 +3407,8 @@ void module_update_tracepoints(void)
        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);
+                       tracepoint_update_probe_range(mod->tracepoints_ptrs,
+                               mod->tracepoints_ptrs + mod->num_tracepoints);
        mutex_unlock(&module_mutex);
 }
 
@@ -3291,8 +3432,8 @@ int module_get_iter_tracepoints(struct tracepoint_iter *iter)
                        else if (iter_mod > iter->module)
                                iter->tracepoint = NULL;
                        found = tracepoint_get_iter_range(&iter->tracepoint,
-                               iter_mod->tracepoints,
-                               iter_mod->tracepoints
+                               iter_mod->tracepoints_ptrs,
+                               iter_mod->tracepoints_ptrs
                                        + iter_mod->num_tracepoints);
                        if (found) {
                                iter->module = iter_mod;