module: merge module_32.c and module_64.c
Subsystems (6)
v2 cover module: merge module_32.c and module_64.c
This patch set aims to merge module_32.c and module_64.c into module.c. And plus some other related fixes.
Please apply in order.
I have tested this patch set on x86_64, and uml over x86_64, and compile tested on i386. It looks fine.
V2:
Trimming exception tables correctly.
Unify module_alloc() for x86.
PATCH v2 1/5 x86 module: merge the same functions in module_32.c and module_64.c
Merge the same functions both in module_32.c and module_64.c into module.c.
This is the first step to merge both of them finally.
---
arch/x86/kernel/module.c new file +98 -0
@@ -0,0 +1,98 @@ +/* Kernel module help for x86. +Copyright (C) 2001 Rusty Russell. + +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 +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +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/moduleloader.h> +#include <linux/elf.h> +#include <linux/vmalloc.h> +#include <linux/fs.h> +#include <linux/string.h> +#include <linux/kernel.h> +#include <linux/bug.h> +#include <linux/mm.h> + +#include <asm/system.h> +#include <asm/page.h> +#include <asm/pgtable.h> + +#if 0 +#define DEBUGP printk +#else +#define DEBUGP(fmt...) +#endif + +/* Free memory returned from module_alloc */ +void module_free (struct module *mod,void *module_region) +{ +vfree (module_region); +/* FIXME: If module_region == mod->init_region, trim exception +table entries. */ +} + +/* We don't need anything special. */ +int module_frob_arch_sections (Elf_Ehdr *hdr, +Elf_Shdr *sechdrs, +char *secstrings, +struct module *mod) +{ +return 0 ; +} + +int module_finalize (const Elf_Ehdr *hdr, +const Elf_Shdr *sechdrs, +struct module *me) +{ +const Elf_Shdr *s, *text =NULL , *alt =NULL , *locks =NULL , + *para =NULL ; +char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; + +for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { +if (!strcmp (".text" , secstrings + s->sh_name)) + text = s; +if (!strcmp (".altinstructions" , secstrings + s->sh_name)) + alt = s; +if (!strcmp (".smp_locks" , secstrings + s->sh_name)) + locks = s; +if (!strcmp (".parainstructions" , secstrings + s->sh_name)) + para = s; + } + +if (alt) { +/* patch .altinstructions */ +void *aseg = (void *)alt->sh_addr; +apply_alternatives (aseg, aseg + alt->sh_size); + } +if (locks && text) { +void *lseg = (void *)locks->sh_addr; +void *tseg = (void *)text->sh_addr; +alternatives_smp_module_add (me, me->name, + lseg, lseg + locks->sh_size, + tseg, tseg + text->sh_size); + } + +if (para) { +void *pseg = (void *)para->sh_addr; +apply_paravirt (pseg, pseg + para->sh_size); + } + +return module_bug_finalize (hdr, sechdrs, me); +} + +void module_arch_cleanup (struct module *mod) +{ +alternatives_smp_module_del (mod); +module_bug_cleanup (mod); +}
arch/x86/kernel/Makefile +1 -1
@@ -72,7 +72,7 @@ obj-$(CONFIG_KEXEC) += machine_kexec_$( obj-$(CONFIG_KEXEC) += relocate_kernel_$(BITS).o crash.o obj-$(CONFIG_CRASH_DUMP) += crash_dump_$(BITS).o obj-$(CONFIG_KPROBES) += kprobes.o -obj-$(CONFIG_MODULES) += module_$(BITS).o +obj-$(CONFIG_MODULES) += module.o module_$(BITS).o obj-$(CONFIG_EFI) += efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_DOUBLEFAULT) += doublefault_32.o obj-$(CONFIG_KGDB) += kgdb.o
arch/x86/kernel/module_32.c +0 -62
71 lines not on this page · raw message
arch/x86/kernel/module_64.c +0 -61
76 lines not on this page · raw message
PATCH v2 2/5 x86 module: merge the rest functions with macros
In reply to Amerigo WangMerge the rest functions together, with proper preprocessing directives. Finally remove module_{32|64}.c.
---
arch/x86/kernel/Makefile +1 -1
@@ -72,7 +72,7 @@ obj-$(CONFIG_KEXEC) += machine_kexec_$( obj-$(CONFIG_KEXEC) += relocate_kernel_$(BITS).o crash.o obj-$(CONFIG_CRASH_DUMP) += crash_dump_$(BITS).o obj-$(CONFIG_KPROBES) += kprobes.o -obj-$(CONFIG_MODULES) += module.o module_$(BITS).o +obj-$(CONFIG_MODULES) += module.o obj-$(CONFIG_EFI) += efi.o efi_$(BITS).o efi_stub_$(BITS).o obj-$(CONFIG_DOUBLEFAULT) += doublefault_32.o obj-$(CONFIG_KGDB) += kgdb.o
arch/x86/kernel/module.c +160 -0
172 lines not on this page · raw message
arch/x86/kernel/module_32.c deleted +0 -90
90 lines not on this page · raw message
arch/x86/kernel/module_64.c deleted +0 -133
133 lines not on this page · raw message
PATCH v2 4/5 module: trim exception table in module_free()
In reply to Amerigo WangJust as the comment said, trim the exception table entries when module_free() mod->module_init.
Currently, this is only done for x86. Other platforms should also fix it like this (except sparc32).
---
arch/x86/kernel/module.c +15 -2
@@ -27,6 +27,7 @@#include <asm/system.h> #include <asm/page.h> #include <asm/pgtable.h> +#include <asm/uaccess.h> #if 0 #define DEBUGP printk
@@ -60,12 +61,24 @@ void *module_alloc(unsigned long size) }#endif +static void trim_init_extable (struct module *m) +{ +/*trim the beginning*/ +while (m->num_exentries &&within_module_init (m->extable[0 ].insn, m)) { + m->extable++; + m->num_exentries--; + } +/*trim the end*/ +while (m->num_exentries && +within_module_init (m->extable[m->num_exentries-1 ].insn, m)) + m->num_exentries--; +} +/* Free memory returned from module_alloc */ void module_free (struct module *mod,void *module_region) {vfree (module_region); -/* FIXME: If module_region == mod->init_region, trim exception -table entries. */ +trim_init_extable (mod); }/* We don't need anything special. */
PATCH v2 3/5 uml module: fix uml build process due to this merge
In reply to Amerigo WangDue to the previous merge, uml needs to be fixed.
---
arch/um/sys-i386/Makefile +1 -1
@@ -8,7 +8,7 @@ obj-y = bug.o bugs.o checksum.o delay.o subarch-obj-y = lib/semaphore_32.o lib/string_32.o subarch-obj-$(CONFIG_HIGHMEM) += mm/highmem_32.o -subarch-obj-$(CONFIG_MODULES) += kernel/module_32.o +subarch-obj-$(CONFIG_MODULES) += kernel/module.o USER_OBJS := bugs.o ptrace_user.o fault.o
arch/um/sys-x86_64/Makefile +1 -3
@@ -8,10 +8,8 @@ obj-y = bug.o bugs.o delay.o fault.o ldt setjmp.o signal.o stub.o stub_segv.o syscalls.o syscall_table.o \ sysrq.o ksyms.o tls.o -obj-$(CONFIG_MODULES) += um_module.o - subarch-obj-y = lib/csum-partial_64.o lib/memcpy_64.o lib/thunk_64.o -subarch-obj-$(CONFIG_MODULES) += kernel/module_64.o +subarch-obj-$(CONFIG_MODULES) += kernel/module.o ldt-y = ../sys-i386/ldt.o
arch/um/sys-x86_64/um_module.c deleted +0 -21
@@ -1,21 +0,0 @@ -#include <linux/vmalloc.h> -#include <linux/moduleloader.h> - -/* Copied from i386 arch/i386/kernel/module.c */ -void *module_alloc (unsigned long size) -{ -if (size ==0 ) -return NULL ; -return vmalloc_exec (size); -} - -/* Free memory returned from module_alloc */ -void module_free (struct module *mod,void *module_region) -{ -vfree (module_region); -/* -* FIXME: If module_region == mod->init_region, trim exception -* table entries. -*/ -} -
PATCH v2 5/5 module: merge module_alloc() finally
In reply to Amerigo WangAs Christoph Hellwig suggested, module_alloc() actually can be unified for i386 and x86_64 (of course, also UML).
---
arch/x86/include/asm/pgtable_32_types.h +4 -0
@@ -46,6 +46,10 @@ extern bool __vmalloc_start_set; /* set# define VMALLOC_END (FIXADDR_START - 2 * PAGE_SIZE)#endif +#define MODULES_VADDR VMALLOC_START +#define MODULES_END VMALLOC_END +#define MODULES_LEN (MODULES_VADDR - MODULES_END) +#define MAXMEM (VMALLOC_END - PAGE_OFFSET - __VMALLOC_RESERVE)#endif /* _ASM_X86_PGTABLE_32_DEFS_H */
arch/um/include/asm/pgtable.h +6 -1
@@ -53,16 +53,21 @@ extern unsigned long end_iomem;#else # define VMALLOC_END (FIXADDR_START-2*PAGE_SIZE)#endif +#define MODULES_VADDR VMALLOC_START +#define MODULES_END VMALLOC_END +#define MODULES_LEN (MODULES_VADDR - MODULES_END)#define _PAGE_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)#define _KERNPG_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)#define _PAGE_CHG_MASK (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY) - +#define __PAGE_KERNEL_EXEC \ + (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)#define PAGE_NONE __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)#define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED)#define PAGE_COPY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)#define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)#define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED) +#define PAGE_KERNEL_EXEC __pgprot(__PAGE_KERNEL_EXEC)/* * The i386 can't do page protection for execute, and considers that the same
arch/x86/kernel/module.c +2 -10
@@ -35,14 +35,6 @@ #define DEBUGP(fmt...)#endif -#if defined(CONFIG_UML ) || defined(CONFIG_X86_32 ) -void *module_alloc (unsigned long size) -{ -if (size ==0 ) -return NULL ; -return vmalloc_exec (size); -} -#else /*X86_64*/ void *module_alloc (unsigned long size) {struct vm_struct *area;
@@ -57,9 +49,9 @@ void *module_alloc(unsigned long size)if (!area)return NULL ; - return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC); +return __vmalloc_area (area,GFP_KERNEL | __GFP_HIGHMEM, +PAGE_KERNEL_EXEC ); } -#endif static void trim_init_extable (struct module *m) {
Re: [Patch 4/5] module: trim exception table in module_free()
In reply to Amerigo WangQuoted 5 lines · Amerigo Wang
Just as the comment said, trim the exception table entries when
module_free()mod->module_init.Currently, this is only done for x86. Other platforms should also fix it like this (except sparc32).
I've taken this one to make it more generic. I'll leave the x86 maintainers to apply the rest.
Thanks,
Rusty.
Re: [Patch 4/5] module: trim exception table in module_free()
Quoted 11 lines · Rusty Russell
Quoted 6 lines · Amerigo Wang
Just as the comment said, trim the exception table entries when
module_free()mod->module_init.Currently, this is only done for x86. Other platforms should also fix it like this (except sparc32).
I've taken this one to make it more generic. I'll leave the x86 maintainers to apply the rest.
Thank you. But this one depends on the previous, I don't think you can apply this without the previous ones.
It's better to let Ingo take all of these, of coures, if he thinks they are fine. :)
Re: [Patch 4/5] module: trim exception table in module_free()
Quoted 6 lines · Amerigo Wang
Quoted 2 lines · Rusty Russell
I've taken this one to make it more generic. I'll leave the x86 maintainers to apply the rest.
Thank you. But this one depends on the previous, I don't think you can apply this without the previous ones.
Here's what I ended up with: I need to rig up a test and make sure it goes to all the maintainers and linux-next.
Subject: module: trim exception table on init free.
It's theoretically possible that there are exception table entries which point into the (freed) init text of modules. These could cause future problems if other modules get loaded into that memory and cause an exception as we'd see the wrong fixup.
Amerigo fixed this long-standing FIXME in the x86 version, but this patch is more general.
This implements trim_init_extable(); most archs are simple since they use the standard lib/extable.c sort code. Alpha and IA64 use relative addresses in their fixups, so thier trimming is a slight variation.
Sparc32 is unique; it doesn't seem to define ARCH_HAS_SORT_EXTABLE, yet it defines its own sort_extable() which overrides the one in lib. It doesn't sort, so we have to mark deleted entries instead of actually trimming them.
--- arch/alpha/mm/extable.c | 19 +++++++++++++++++++ arch/ia64/mm/extable.c | 24 ++++++++++++++++++++++++ arch/sparc/include/asm/uaccess_32.h | 3 +++ arch/sparc/mm/extable.c | 27 +++++++++++++++++++++++++++ include/linux/module.h | 1 + kernel/module.c | 1 + lib/extable.c | 17 +++++++++++++++++ 7 files changed, 92 insertions(+)
arch/alpha/mm/extable.c +19 -0
@@ -48,6 +48,25 @@ void sort_extable(struct exception_table cmp_ex, swap_ex); } +/* +* Any entry referring to the module init will be at the beginning or +* the end. +*/ +void trim_init_extable (struct module *m) +{ +/*trim the beginning*/ +while (m->num_exentries && +within_module_init (ex_to_addr (&m->extable[0 ]), m)) { + m->extable++; + m->num_exentries--; + } +/*trim the end*/ +while (m->num_exentries && +within_module_init (ex_to_addr (&m->extable[m->num_exentries-1 ]), + m)) + m->num_exentries--; +} +const struct exception_table_entry * search_extable(const struct exception_table_entry *first, const struct exception_table_entry *last,
arch/ia64/mm/extable.c +24 -0
@@ -53,6 +53,30 @@ void sort_extable (struct exception_tabl cmp_ex, swap_ex); } +static inline unsigned long ex_to_addr (const struct exception_table_entry *x) +{ +return (unsigned long )&x->insn + x->insn; +} + +/* +* Any entry referring to the module init will be at the beginning or +* the end. +*/ +void trim_init_extable (struct module *m) +{ +/*trim the beginning*/ +while (m->num_exentries && +within_module_init (ex_to_addr (&m->extable[0 ]), m)) { + m->extable++; + m->num_exentries--; + } +/*trim the end*/ +while (m->num_exentries && +within_module_init (ex_to_addr (&m->extable[m->num_exentries-1 ]), + m)) + m->num_exentries--; +} +const struct exception_table_entry * search_extable (const struct exception_table_entry *first, const struct exception_table_entry *last,
arch/sparc/include/asm/uaccess_32.h +3 -0
@@ -17,6 +17,9 @@#ifndef __ASSEMBLY__ +#define ARCH_HAS_SORT_EXTABLE +#define ARCH_HAS_SEARCH_EXTABLE +/* Sparc is not segmented, however we need to be able to fool access_ok() * when doing system calls from kernel mode legitimately. *
arch/sparc/mm/extable.c +27 -0
@@ -28,6 +28,10 @@ search_extable(const struct exception_ta* word 3: last insn address + 4 bytes * word 4: fixup code address * +* Deleted entries are encoded as: +* word 1: unused +* word 2: -1 +* * See asm/uaccess.h for more details. */
@@ -39,6 +43,10 @@ search_extable(const struct exception_tacontinue ; } +/* A deleted entry; see trim_init_extable */ +if (walk->fixup ==-1 ) +continue ; +if (walk->insn == value)return walk; }
@@ -57,6 +65,25 @@ search_extable(const struct exception_tareturn NULL ; } +/* We could memmove them around; easier to mark the trimmed ones. */ +void trim_init_extable (struct module *m) +{ +unsigned int i; +bool range; + +for (i =0 ; i < m->num_exentries; i += range ?2 :1 ) { + range = m->extable[i].fixup ==0 ; + +if (within_module_init (m->extable[i].insn)) { + m->extable[i].fixup =-1 ; +if (range) + m->extable[i+1 ].fixup =-1 ; + } +if (range) + i++; + } +} +/* Special extable search, which handles ranges. Returns fixup */ unsigned long search_extables_range (unsigned long addr,unsigned long *g2) {
include/linux/module.h +1 -0
@@ -77,6 +77,7 @@ search_extable(const struct exception_tavoid sort_extable (struct exception_table_entry *start,struct exception_table_entry *finish);void sort_main_extable (void ); +void trim_init_extable (struct module *m);#ifdef MODULE #define MODULE_GENERIC_TABLE (gtype,name) \
kernel/module.c +1 -0
@@ -2394,6 +2394,7 @@ SYSCALL_DEFINE3(init_module, void __usermutex_lock (&module_mutex);/* Drop initial reference. */ module_put (mod); +trim_init_extable (mod);module_free (mod, mod->module_init); mod->module_init =NULL ; mod->init_size =0 ;
lib/extable.c +17 -0
@@ -39,6 +39,23 @@ void sort_extable(struct exception_tablesort (start, finish - start,sizeof (struct exception_table_entry ), cmp_ex, NULL); } + +/* +* If the exception table is sorted, any referring to the module init +* will be at the beginning or the end. +*/ +void trim_init_extable (struct module *m) +{ +/*trim the beginning*/ +while (m->num_exentries &&within_module_init (m->extable[0 ].insn, m)) { + m->extable++; + m->num_exentries--; + } +/*trim the end*/ +while (m->num_exentries && +within_module_init (m->extable[m->num_exentries-1 ].insn, m)) + m->num_exentries--; +}#endif #ifndef ARCH_HAS_SEARCH_EXTABLE
Re: [Patch 4/5] module: trim exception table in module_free()
In reply to Rusty RussellQuoted 9 lines · Rusty Russell
Quoted 5 lines · Amerigo Wang
Just as the comment said, trim the exception table entries when
module_free()mod->module_init.Currently, this is only done for x86. Other platforms should also fix it like this (except sparc32).
I've taken this one to make it more generic. I'll leave the x86 maintainers to apply the rest.
I think it makes most sense to handle them together. We have no interacting changes pending in the x86 tree, feel free to pick them up.
Ingo
Re: [Patch 4/5] module: trim exception table in module_free()
In reply to Rusty RussellQuoted 28 lines · Rusty Russell
Quoted 8 lines · Amerigo Wang
Quoted 3 lines · Rusty Russell
I've taken this one to make it more generic. I'll leave the x86 maintainers to apply the rest.
Thank you. But this one depends on the previous, I don't think you can apply this without the previous ones.
Here's what I ended up with: I need to rig up a test and make sure it goes to all the maintainers and linux-next.
Subject: module: trim exception table on init free.
It's theoretically possible that there are exception table entries which point into the (freed) init text of modules. These could cause future problems if other modules get loaded into that memory and cause an exception as we'd see the wrong fixup.
Amerigo fixed this long-standing FIXME in the x86 version, but this patch is more general.
This implements
trim_init_extable(); most archs are simple since they use the standardlib/extable.csort code. Alpha and IA64 use relative addresses in their fixups, so thier trimming is a slight variation.
Quoted 8 lines
Sparc32 is unique; it doesn't seem to define
ARCH_HAS_SORT_EXTABLE, yet it defines its ownsort_extable()which overrides the one in lib. It doesn't sort, so we have to mark deleted entries instead of actually trimming them.Inspired-by Amerigo Wang <[email protected]>Signed-off-by Rusty Russell <[email protected]>
Very nice!
The generic part of this patch looks good for me, of course. :)
Thank you, Rusty!
Re: [Patch 4/5] module: trim exception table in module_free()
In reply to Ingo MolnarQuoted 8 lines · Ingo Molnar
Quoted 5 lines · Rusty Russell
Quoted 3 lines · Amerigo Wang
Just as the comment said, trim the exception table entries when
module_free()mod->module_init.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Currently, this is only done for x86. Other platforms should also fix it like this (except sparc32).
I've taken this one to make it more generic. I'll leave the x86 maintainers to apply the rest.
I think it makes most sense to handle them together. We have no interacting changes pending in the x86 tree, feel free to pick them up.
Thanks for clarifying.
So, Rusty, could you please take all of these except patch 4/5? Since you have a much better one to replace that one. :)
Thank you.
Re: [Patch 4/5] module: trim exception table in module_free()
Quoted 9 lines · Amerigo Wang
Quoted 3 lines · Ingo Molnar
I think it makes most sense to handle them together. We have no interacting changes pending in the x86 tree, feel free to pick them up.
Thanks for clarifying.
So, Rusty, could you please take all of these except patch 4/5? Since you have a much better one to replace that one. :)
Yep, I've put them in my tree for testing; if all goes well, they'll hit linux-next in a couple of days.
Thanks!
Rusty.
Re: [Patch 4/5] module: trim exception table in module_free()
In reply to Rusty RussellFrom: Rusty Russell <[email protected]>
Date: Sat, 6 Jun 2009 00:38:02 +0930
Quoted 7 lines
Sparc32 is unique; it doesn't seem to define
ARCH_HAS_SORT_EXTABLE, yet it defines its ownsort_extable()which overrides the one in lib. It doesn't sort, so we have to mark deleted entries instead of actually trimming them.Inspired-by Amerigo Wang <[email protected]>Signed-off-by Rusty Russell <[email protected]>