module: merge module_32.c and module_64.c
Subsystems (3)
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.
arch/um/sys-i386/Makefile | 2 arch/um/sys-x86_64/Makefile | 4 arch/um/sys-x86_64/um_module.c | 21 --- arch/x86/kernel/Makefile | 4 arch/x86/kernel/module.c | 262 ++++++++++++++++++++++++++++++++++++++++- arch/x86/kernel/module_32.c | 152 ----------------------- arch/x86/kernel/module_64.c | 194 ------------------------------ 7 files changed, 264 insertions(+), 375 deletions(-)
PATCH 1/4 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.
---
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 3/4 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 4/4 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.
---
arch/x86/kernel/module.c +2 -2
@@ -64,8 +64,8 @@ void *module_alloc(unsigned long size)void module_free (struct module *mod,void *module_region) {vfree (module_region); -/* FIXME: If module_region == mod->init_region, trim exception -table entries. */ +if (module_region == mod->module_init) + mod->num_exentries =0 ; }/* We don't need anything special. */
PATCH 2/4 x86 module: merge the rest functions with macros
In reply to Amerigo WangMerge the rest functions together, with 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
Re: [Patch 2/4] x86 module: merge the rest functions with macros
Quoted 15 lines · Amerigo Wang
arch/x86/kernel/module.c +15
-0+#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; + +if (!size) +return NULL ; + size =PAGE_ALIGN (size);
arch/x86/kernel/module.c line 61
Quoted 10 lines
arch/x86/kernel/module.c +10
-0+if (size >MODULES_LEN ) +return NULL ; + + area =__get_vm_area (size,VM_ALLOC ,MODULES_VADDR ,MODULES_END ); +if (!area) +return NULL ; + + return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC); +} +#endif
vmalloc_exec basically expands to the x86-64 version of that code, just using VMALLOC_START/VMALLOC_END instead of MODULES_VADDR/MODULES_END.
So instead of having two variants it would be better to use the x86-64 unconditionally and define MODULES_VADDR/MODULES_END to VMALLOC_START/VMALLOC_END to 32bit and uml.
And that part should be a patch of it's own, not mixed with others.
Re: [Patch 2/4] x86 module: merge the rest functions with macros
Quoted 32 lines · Christoph Hellwig
Quoted 16 lines · Amerigo Wang
arch/x86/kernel/module.c +15
-0+#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; + +if (!size) +return NULL ; + size =PAGE_ALIGN (size);arch/x86/kernel/module.c line 61
Quoted 10 lines
arch/x86/kernel/module.c +10
-0+if (size >MODULES_LEN ) +return NULL ; + + area =__get_vm_area (size,VM_ALLOC ,MODULES_VADDR ,MODULES_END ); +if (!area) +return NULL ; + + return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC); +} +#endif
vmalloc_execbasically expands to the x86-64 version of that code, just using VMALLOC_START/VMALLOC_END instead of MODULES_VADDR/MODULES_END.
Quoted 6 lines
So instead of having two variants it would be better to use the x86-64 unconditionally and define MODULES_VADDR/MODULES_END to VMALLOC_START/VMALLOC_END to 32bit and uml.
And that part should be a patch of it's own, not mixed with others.
Thanks, it is a good idea!
But... vmalloc_exec() also sets __GFP_HIGHMEM, this is different from x86_64. No? Or __GFP_HIGHMEM is meaningless on x86_64? :-)
Re: [Patch 4/4] module: trim exception table in module_free()
In reply to Amerigo Wangarch/x86/kernel/module.c void *module_alloc(unsigned long size) · line 69
Quoted 8 lines · Amerigo Wang
void
module_free(struct module *mod, void *module_region){arch/x86/kernel/module.c +2
-2vfree (module_region); -/* FIXME: If module_region == mod->init_region, trim exception -table entries. */ +if (module_region == mod->module_init) + mod->num_exentries =0 ; }
Hi Amerigo,
This looks wrong. The extable covers both init and core exception entries.
We want to remove the ones in the module_init section. The good news is that it's sorted, so they're either at the start or the end (except sparc 32).
The bad news is that this is really a generic problem, and deserves a
generic fix. That's easy for archs which use the generic sort_extable(), but alpha, ia64 and sparc(32) will need to define their own trim_extable().
Thanks!
Rusty.
Re: [Patch 4/4] module: trim exception table in module_free()
Quoted 14 lines · Rusty Russell
arch/x86/kernel/module.c void *module_alloc(unsigned long size) · line 69
Quoted 9 lines · Amerigo Wang
void
module_free(struct module *mod, void *module_region){arch/x86/kernel/module.c +2
-2vfree (module_region); -/* FIXME: If module_region == mod->init_region, trim exception -table entries. */ +if (module_region == mod->module_init) + mod->num_exentries =0 ; }Hi Amerigo,
Quoted 3 lines
This looks wrong. The extable covers both init and core exception entries.
We want to remove the ones in themodule_initsection. The good news is that it's sorted, so they're either at the start or the end (except sparc 32).
Hi, Rusty.
Yes? The extable of a module is in '__ex_table' section, and during the section transfer, one section will be either in module_init or module_core, so its entries are only in one of them, not both, right?
Thank you.
Re: [Patch 4/4] module: trim exception table in module_free()
Quoted 18 lines · Amerigo Wang
Quoted 12 lines · Rusty Russell
arch/x86/kernel/module.c void *module_alloc(unsigned long size) · line 69
Quoted 8 lines · Amerigo Wang
void
module_free(struct module *mod, void *module_region){arch/x86/kernel/module.c +2
-2vfree (module_region); -/* FIXME: If module_region == mod->init_region, trim exception -table entries. */ +if (module_region == mod->module_init) + mod->num_exentries =0 ; }Hi Amerigo,
Quoted 4 lines
This looks wrong. The extable covers both init and core exception
entries. We want to remove the ones in themodule_initsection. The good news is that it's sorted, so they're either at the start or the end (except sparc 32).
Quoted 7 lines
Hi, Rusty.
Yes? The extable of a module is in '
__ex_table' section, and during the section transfer, one
section will be either inmodule_initormodule_core, so its entries are only in one of them,
not both, right?
32-bit example:
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/moduleparam.h>
static unsigned long uaddr;module_param(uaddr, ulong, 0600);
void extable_not_init(u64 val)
{
__put_user(val, (u64 *)uaddr);
}static int __init init(void)
{
__put_user(0, (u64 *)uaddr);
return 0;
}module_init(init);
__ex_table ends up with two entries:
Contents of section __ex_table:
0000 0c000000 00000000 0e000000 00000000 ................ 0010 10000000 0a000000 12000000 0a000000 ................
The first is for the __put_user in .text (extable_not_init()) and the second is for the one in .init.text (init()).
Depending on how the module gets allocated, the one referring to .init.text may be first or last.
(You can see here why we haven't fixed this: exceptions in __init in modules are rare, perhaps non-existent).
Hope that clarifies?
Rusty.
Re: [Patch 4/4] module: trim exception table in module_free()
Quoted 10 lines · Rusty Russell
__ex_tableends up with two entries:Contents of section
__ex_table:
00000c000000000000000e00000000000000 ................ 0010 100000000a000000120000000a000000................The first is for the
__put_userin .text (extable_not_init()) and the second is for the one in .init.text (init()).Depending on how the module gets allocated, the one referring to .init.text may be first or last.
Hmm, I understand now. The problem is that we don't know which entries are for __init, and which are not...
Quoted 2 lines
(You can see here why we haven't fixed this: exceptions in
__initin modules are rare, perhaps non-existent).
Agreed.
Is it possible to put extable for __init in a separate section?
Thanks for your explanation!
Re: [Patch 4/4] module: trim exception table in module_free()
In reply to Rusty RussellQuoted 10 lines · Rusty Russell
__ex_tableends up with two entries:Contents of section
__ex_table:
00000c000000000000000e00000000000000 ................ 0010 100000000a000000120000000a000000................The first is for the
__put_userin .text (extable_not_init()) and the second is for the one in .init.text (init()).Depending on how the module gets allocated, the one referring to .init.text may be first or last.
Hmm, how about the following? :-)
struct exception_table_entry *p = mod->extable;
for (;p <= mod->extable+mod->num_exentries; p++ )
if (with_in_module_init(p->insn, mod))
trim_it(p);Re: [Patch 4/4] module: trim exception table in module_free()
Quoted 13 lines · Amerigo Wang
Quoted 11 lines · Rusty Russell
__ex_tableends up with two entries:Contents of section
__ex_table:
00000c000000000000000e00000000000000 ................ 0010 100000000a000000120000000a000000................The first is for the
__put_userin .text (extable_not_init()) and the second is for the one in .init.text (init()).Depending on how the module gets allocated, the one referring to .init.text may be first or last.
Quoted 7 lines
Hmm, how about the following? :-)
struct
exception_table_entry*p =mod->extable;for (;p <= mod->extable+mod->num_exentries; p++ )
if (with_in_module_init(p->insn, mod)) trim_it(p);
More like this:
void trim_init_extable(struct module *m)
{
/* Since entries are sorted, init entries are at the start... */
while (m->num_exentries && within_module_init(m->extable[0].insn)) {
m->extable++;
m->num_exentries--;
}
/* ... or the end. */
while (m->num_exentries && within_module_init(m->extable[m->num_exentries-1].insn))
m->num_exentries--;
}Cheers,
Rusty.
Re: [Patch 4/4] module: trim exception table in module_free()
Quoted 28 lines · Rusty Russell
Quoted 14 lines · Amerigo Wang
Quoted 6 lines · Rusty Russell
__ex_tableends up with two entries:Contents of section
__ex_table:
00000c000000000000000e00000000000000 ................ 0010 100000000a000000120000000a000000................Quoted 6 lines
The first is for the
__put_userin .text (extable_not_init()) and the second is for the one in .init.text (init()).Depending on how the module gets allocated, the one referring to .init.text may be first or last.
Quoted 8 lines
Hmm, how about the following? :-)
struct
exception_table_entry*p =mod->extable;for (;p <= mod->extable+mod->num_exentries; p++ )
if (with_in_module_init(p->insn, mod)) trim_it(p);More like this:
void
trim_init_extable(struct module *m)
Quoted 12 lines
{
/* Since entries are sorted, init entries are at the start... */
while (m->num_exentries && within_module_init(m->extable[0].insn)) {
m->extable++;
m->num_exentries--;
}
/* ... or the end. */
while (m->num_exentries && within_module_init(m->extable[m->num_exentries-1].insn))
m->num_exentries--;
}Great! Thank you!
But does this works on all arch? Or only except sparc32?
Re: [Patch 4/4] module: trim exception table in module_free()
Quoted 33 lines · Amerigo Wang
Quoted 32 lines · Rusty Russell
Quoted 13 lines · Amerigo Wang
Quoted 11 lines · Rusty Russell
__ex_tableends up with two entries:Contents of section
__ex_table:
00000c000000000000000e00000000000000 ................ 0010 100000000a000000120000000a000000................The first is for the
__put_userin .text (extable_not_init()) and the second is for the one in .init.text (init()).Depending on how the module gets allocated, the one referring to .init.text may be first or last.
Quoted 7 lines
Hmm, how about the following? :-)
struct
exception_table_entry*p =mod->extable;for (;p <= mod->extable+mod->num_exentries; p++ )
if (with_in_module_init(p->insn, mod)) trim_it(p);More like this:
void
trim_init_extable(struct module *m){ /* Since entries are sorted, init entries are at the start... */ while (m->num_exentries && within_module_init(m->extable[0].insn)) { m->extable++; m->num_exentries--; }
Quoted 8 lines
Quoted 5 lines
/* ... or the end. */ while (m->num_exentries &&
within_module_init(m->extable[m->num_exentries-1].insn)) m->num_exentries--;
}Great! Thank you!
But does this works on all arch? Or only except sparc32?
Variations will work on all archs. sparc32 doesn't sort its exception table AFAICT, so it will need something more sophisticated (ISTR they have exception areas, so perhaps setting length to 0 would 'delete' them).
Hope that helps,
Rusty.