sort exception tables
Subsystems (11)
PATCH 1/1 sort exception tables
This patch arranges for the exception tables to be sorted on most architectures. It sorts the main kernel exception table on startup and the module exception tables when they get loaded. The main table is sorted reasonably early - just after kmem_cache_init - but that could be moved even earlier if necessary.
There is now a lib/extable.c which includes the sort_extable() function from arch/ppc/mm/extable.c and the search_extable() function from arch/i386/mm/extable.c, which had been copied to many architectures. On many architectures, arch/$(ARCH)/mm/extable.c became empty and so I have removed it.
There are four architectures which do things differently from i386:
alpha, ia64, sparc and sparc64. Alpha and ia64 store the offset from the offset from the exception table entry to the instruction, and sparc and sparc64 have range entries in the table. For those architectures I have added empty sort_extable functions. The maintainers for those architectures can implement something better if they care to. As it is they are no worse off than before.
Although it is a moderately sizable patch, it ends up with a net reduction of 377 lines in the size of the kernel source. :)
I have tested this on x86 and ppc with a module that uses __get_user in an init function, deliberately laid out to get the exception table out of order, and it works (whereas it oopsed without this patch).
Please apply.
Thanks,
Paul.
arch/alpha/mm/extable.c +5 -0
@@ -6,6 +6,11 @@#include <linux/module.h> #include <asm/uaccess.h> +void sort_extable (struct exception_table_entry *start, +struct exception_table_entry *finish) +{ +} +const struct exception_table_entry * search_extable(const struct exception_table_entry *first, const struct exception_table_entry *last,
arch/arm/mm/extable.c +0 -21
@@ -4,27 +4,6 @@#include <linux/module.h> #include <asm/uaccess.h> -const struct exception_table_entry * -search_extable (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long value) -{ -while (first <= last) { -const struct exception_table_entry *mid; -long diff; - - mid = (last - first) /2 + first; - diff = mid->insn - value; -if (diff ==0 ) -return mid; -else if (diff <0 ) - first = mid+1 ; -else - last = mid-1 ; - } -return NULL ; -} -int fixup_exception (struct pt_regs *regs) {const struct exception_table_entry *fixup;
arch/arm26/mm/extable.c +0 -21
@@ -6,27 +6,6 @@#include <linux/module.h> #include <asm/uaccess.h> -const struct exception_table_entry * -search_extable (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long value) -{ -while (first <= last) { -const struct exception_table_entry *mid; -long diff; - - mid = (last - first) /2 + first; - diff = mid->insn - value; -if (diff ==0 ) -return mid; -else if (diff <0 ) - first = mid+1 ; -else - last = mid-1 ; - } -return NULL ; -} -int fixup_exception (struct pt_regs *regs) {const struct exception_table_entry *fixup;
arch/cris/mm/Makefile +1 -1
@@ -2,5 +2,5 @@ # Makefile for the linux cris-specific parts of the memory manager. # -obj-y := init.o fault.o tlb.o extable.o ioremap.o +obj-y := init.o fault.o tlb.o ioremap.o
arch/cris/mm/extable.c +0 -48
48 lines not on this page · raw message
arch/h8300/mm/Makefile +1 -1
@@ -7,4 +7,4 @@ # # Note 2! The CFLAGS definition is now in the main makefile... -obj-y := init.o fault.o memory.o kmap.o extable.o +obj-y := init.o fault.o memory.o kmap.o
arch/h8300/mm/extable.c +0 -30
@@ -1,30 +0,0 @@ -/* -* linux/arch/h8300/mm/extable.c -*/ - -#include <linux/config.h> -#include <linux/module.h> -#include <linux/spinlock.h> -#include <asm/uaccess.h> - -/* Simple binary search */ -const struct exception_table_entry * -search_extable (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long value) -{ -while (first <= last) { -const struct exception_table_entry *mid; -long diff; - - mid = (last - first) /2 + first; - diff = mid->insn - value; -if (diff ==0 ) -return mid; -else if (diff <0 ) - first = mid+1 ; -else - last = mid-1 ; - } -return NULL ; -}
arch/i386/mm/extable.c +0 -22
@@ -7,28 +7,6 @@#include <linux/spinlock.h> #include <asm/uaccess.h> -/* Simple binary search */ -const struct exception_table_entry * -search_extable (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long value) -{ -while (first <= last) { -const struct exception_table_entry *mid; -long diff; - - mid = (last - first) /2 + first; - diff = mid->insn - value; -if (diff ==0 ) -return mid; -else if (diff <0 ) - first = mid+1 ; -else - last = mid-1 ; - } -return NULL ; -} -int fixup_exception (struct pt_regs *regs) {const struct exception_table_entry *fixup;
arch/ia64/mm/extable.c +5 -0
@@ -10,6 +10,11 @@#include <asm/uaccess.h> #include <asm/module.h> +void sort_extable (struct exception_table_entry *start, +struct exception_table_entry *finish) +{ +} +const struct exception_table_entry * search_extable (const struct exception_table_entry *first, const struct exception_table_entry *last,
arch/m68k/mm/Makefile +1 -1
@@ -2,7 +2,7 @@ # Makefile for the linux m68k-specific parts of the memory manager. # -obj-y := init.o fault.o extable.o hwtest.o +obj-y := init.o fault.o hwtest.o ifndef CONFIG_SUN3 obj-y += kmap.o memory.o motorola.o
arch/m68k/mm/extable.c +0 -33
@@ -1,33 +0,0 @@ -/* -* linux/arch/m68k/mm/extable.c -*/ - -#include <linux/config.h> -#include <linux/module.h> -#include <asm/uaccess.h> - -extern const struct exception_table_entry __start___ex_table[]; -extern const struct exception_table_entry __stop___ex_table[]; - -/* Simple binary search */ -const struct exception_table_entry * -search_extable (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long value) -{ -while (first <= last) { -const struct exception_table_entry *mid; -long diff; - - mid = (last - first) /2 + first; - diff = value - mid->insn; -if (diff >=0 && diff <=2 ) -return mid; -else if (diff >0 ) - first = mid+1 ; -else - last = mid-1 ; - } -return NULL ; -} -
arch/m68knommu/mm/Makefile +1 -1
@@ -2,4 +2,4 @@ # Makefile for the linux m68knommu specific parts of the memory manager. # -obj-y += init.o fault.o memory.o kmap.o extable.o +obj-y += init.o fault.o memory.o kmap.o
arch/m68knommu/mm/extable.c +0 -30
@@ -1,30 +0,0 @@ -/* -* linux/arch/m68knommu/mm/extable.c -*/ - -#include <linux/config.h> -#include <linux/module.h> -#include <linux/spinlock.h> -#include <asm/uaccess.h> - -/* Simple binary search */ -const struct exception_table_entry * -search_extable (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long value) -{ -while (first <= last) { -const struct exception_table_entry *mid; -long diff; - - mid = (last - first) /2 + first; - diff = mid->insn - value; -if (diff ==0 ) -return mid; -else if (diff <0 ) - first = mid+1 ; -else - last = mid-1 ; - } -return NULL ; -}
arch/mips/mm/Makefile +1 -1
@@ -2,7 +2,7 @@ # Makefile for the Linux/MIPS-specific parts of the memory manager. # -obj-y += cache.o extable.o fault.o loadmmu.o pgtable.o +obj-y += cache.o fault.o loadmmu.o pgtable.o obj-$(CONFIG_MIPS32) += ioremap.o pgtable-32.o obj-$(CONFIG_MIPS64) += pgtable-64.o
arch/mips/mm/extable.c +0 -30
@@ -1,30 +0,0 @@ -/* -* linux/arch/i386/mm/extable.c -*/ - -#include <linux/config.h> -#include <linux/module.h> -#include <linux/spinlock.h> -#include <asm/uaccess.h> - -/* Simple binary search */ -const struct exception_table_entry * -search_extable (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long value) -{ -while (first <= last) { -const struct exception_table_entry *mid; -long diff; - - mid = (last - first) /2 + first; - diff = mid->insn - value; -if (diff ==0 ) -return mid; -else if (diff <0 ) - first = mid+1 ; -else - last = mid-1 ; - } -return NULL ; -}
arch/parisc/mm/Makefile +1 -1
@@ -2,4 +2,4 @@ # Makefile for arch/parisc/mm # -obj-y := init.o fault.o extable.o ioremap.o +obj-y := init.o fault.o ioremap.o
arch/parisc/mm/extable.c +0 -37
@@ -1,37 +0,0 @@ -/* -* Kernel exception handling table support. Derived from arch/i386/mm/extable.c. -* -* Copyright (C) 2000 Hewlett-Packard Co -* Copyright (C) 2000 John Marvin ([email protected]) -*/ - -#include <asm/uaccess.h> - -const struct exception_table_entry * -search_extable (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long addr) -{ -/* Abort early if the search value is out of range. */ - -if ((addr < first->addr) || (addr > last->addr)) -return 0 ; - -while (first <= last) { -const struct exception_table_entry *mid; -long diff; - - mid = first + ((last - first)/2 ); - diff = mid->addr - addr; - -if (diff ==0 ) -return mid; -else if (diff <0 ) - first = mid+1 ; -else - last = mid-1 ; - } - -return 0 ; -} -
arch/ppc/kernel/module.c +0 -1
@@ -265,7 +265,6 @@return 0 ; } -/* FIXME: Sort exception table --RR */ int module_finalize (const Elf_Ehdr *hdr,const Elf_Shdr *sechdrs,struct module *me)
arch/ppc/mm/Makefile +1 -1
@@ -6,7 +6,7 @@ EXTRA_AFLAGS := -Wa,-mppc64bridge endif -obj-y := fault.o init.o mem_pieces.o extable.o \ +obj-y := fault.o init.o mem_pieces.o \ mmu_context.o pgtable.o obj-$(CONFIG_PPC_STD_MMU) += hashtable.o ppc_mmu.o tlb.o
arch/ppc/mm/extable.c +0 -70
70 lines not on this page · raw message
arch/ppc64/kernel/module.c +0 -4
@@ -404,10 +404,6 @@*/ list_add (&me->arch.bug_list, &module_bug_list); -sort_ex_table ((struct exception_table_entry *)me->extable, - (struct exception_table_entry *)me->extable + - me->num_exentries); -return 0 ; }
arch/ppc64/kernel/setup.c +0 -2
@@ -486,7 +486,6 @@ }extern void (*calibrate_delay)(void ); -extern void sort_exception_table (void );/* * Called into from start_kernel, after lock_kernel has been called.
@@ -534,7 +533,6 @@ ppc_md.setup_arch ();paging_init (); -sort_exception_table ();ppc64_boot_msg (0x15 ,"Setup Done" ); }
arch/ppc64/mm/Makefile +1 -1
@@ -4,6 +4,6 @@ EXTRA_CFLAGS += -mno-minimal-toc -obj-y := fault.o init.o extable.o imalloc.o +obj-y := fault.o init.o imalloc.o obj-$(CONFIG_DISCONTIGMEM) += numa.o obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
arch/ppc64/mm/extable.c +0 -74
74 lines not on this page · raw message
arch/s390/mm/Makefile +1 -1
@@ -2,4 +2,4 @@ # Makefile for the linux s390-specific parts of the memory manager. # -obj-y := init.o fault.o ioremap.o extable.o +obj-y := init.o fault.o ioremap.o
arch/s390/mm/extable.c +0 -34
@@ -1,34 +0,0 @@ -/* -* arch/s390/mm/extable.c -* -* S390 version -* -* identical to arch/i386/mm/extable.c -*/ - -#include <linux/config.h> -#include <linux/module.h> -#include <linux/spinlock.h> -#include <asm/uaccess.h> - -/* Simple binary search */ -const struct exception_table_entry * -search_extable (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long value) -{ -while (first <= last) { -const struct exception_table_entry *mid; -long diff; - - mid = (last - first) /2 + first; - diff = mid->insn - value; -if (diff ==0 ) -return mid; -else if (diff <0 ) - first = mid+1 ; -else - last = mid-1 ; - } -return NULL ; -}
arch/sh/mm/extable.c +0 -23
@@ -9,29 +9,6 @@#include <linux/module.h> #include <asm/uaccess.h> -/* Simple binary search */ -const struct exception_table_entry * -search_extable (const struct exception_table_entry *first, -const struct exception_table_entry *last, -unsigned long value) -{ -while (first <= last) { -const struct exception_table_entry *mid; -long diff; - - mid = (last - first) /2 + first; - diff = mid->insn - value; -if (diff ==0 ) -return mid; -else if (diff <0 ) - first = mid+1 ; -else - last = mid-1 ; - } - -return NULL ; -} -int fixup_exception (struct pt_regs *regs) {const struct exception_table_entry *fixup;
arch/sparc/mm/extable.c +5 -0
@@ -6,6 +6,11 @@#include <linux/module.h> #include <asm/uaccess.h> +void sort_extable (struct exception_table_entry *start, +struct exception_table_entry *finish) +{ +} +/* Caller knows they are in a range if ret->fixup == 0 */ const struct exception_table_entry * search_extable(const struct exception_table_entry *start,
arch/sparc64/mm/extable.c +5 -0
@@ -9,6 +9,11 @@extern const struct exception_table_entry __start___ex_table[];extern const struct exception_table_entry __stop___ex_table[]; +void sort_extable (struct exception_table_entry *start, +struct exception_table_entry *finish) +{ +} +/* Caller knows they are in a range if ret->fixup == 0 */ const struct exception_table_entry * search_extable(const struct exception_table_entry *start,
arch/x86_64/mm/extable.c +3 -5
@@ -36,10 +36,9 @@/* When an exception handler is in an non standard section (like __init) the fixup table can end up unordered. Fix that here. */ -static __init int check_extable (void ) +void sort_extable (struct exception_table_entry *start, +struct exception_table_entry *finish) { -extern struct exception_table_entry __start___ex_table[]; -extern struct exception_table_entry __stop___ex_table[];struct exception_table_entry *e;int change;
@@ -47,7 +46,7 @@best (and simplest) sort algorithm. */ do { change = 0; - for (e = __start___ex_table+1; e < __stop___ex_table; e++) { + for (e = start+1; e < finish; e++) { if (e->insn < e[-1].insn) { struct exception_table_entry tmp = e[-1]; e[-1] = e[0];
@@ -58,4 +57,3 @@ }while (change !=0 );return 0 ; } -core_initcall (check_extable);
include/asm-ppc/uaccess.h +0 -2
@@ -60,8 +60,6 @@unsigned long insn, fixup; }; -extern void sort_exception_table (void ); -/* * These are the main single-value transfer routines. They automatically * use the right size if we just have the right pointer type.
include/asm-ppc64/uaccess.h +0 -1
@@ -82,7 +82,6 @@/* Returns 0 if exception not found and fixup otherwise. */ extern unsigned long search_exception_table (unsigned long ); -extern void sort_exception_table (void );/* * These are the main single-value transfer routines. They automatically
include/linux/module.h +3 -0
@@ -54,6 +54,9 @@ search_extable(const struct exception_table_entry *first, const struct exception_table_entry *last, unsigned long value); +void sort_extable (struct exception_table_entry *start, +struct exception_table_entry *finish); +void sort_main_extable (void );#ifdef MODULE #define ___module_cat (a,b) __mod_ ## a ## b
init/main.c +1 -0
@@ -435,6 +435,7 @@page_address_init ();mem_init ();kmem_cache_init (); +sort_main_extable ();if (late_time_init)late_time_init ();calibrate_delay ();
kernel/extable.c +9 -2
@@ -16,11 +16,18 @@Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <linux/module.h> +#include <linux/init.h> #include <asm/uaccess.h> #include <asm/sections.h> -extern const struct exception_table_entry __start___ex_table[]; -extern const struct exception_table_entry __stop___ex_table[]; +extern struct exception_table_entry __start___ex_table[]; +extern struct exception_table_entry __stop___ex_table[]; + +/* Sort the kernel's built-in exception table */ +void __init sort_main_extable (void ) +{ +sort_extable (__start___ex_table, __stop___ex_table); +}/* Given an address, look for it in the exception tables. */ const struct exception_table_entry *search_exception_tables (unsigned long addr)
kernel/module.c +6 -4
@@ -1395,6 +1395,7 @@struct module *mod;long err =0 ;void *percpu =NULL , *ptr =NULL ;/* Stops spurious gcc warning */ +struct exception_table_entry *extable;DEBUGP ("load_module: umod=%p, len=%lu, uargs=%p\n" , umod, len, uargs);
@@ -1611,10 +1612,6 @@ }#endif -/* Set up exception table */ - mod->num_exentries = sechdrs[exindex].sh_size /sizeof (*mod->extable); - mod->extable = (void *)sechdrs[exindex].sh_addr; -/* Now do relocations. */ for (i =1 ; i < hdr->e_shnum; i++) {const char *strtab = (char *)sechdrs[strindex].sh_addr;
@@ -1637,6 +1634,11 @@ goto cleanup; } +/* Set up and sort exception table */ + mod->num_exentries = sechdrs[exindex].sh_size /sizeof (*mod->extable); + mod->extable = extable = (void *)sechdrs[exindex].sh_addr; +sort_extable (extable, extable + mod->num_exentries); +/* Finally, copy percpu area over. */ percpu_modcopy (mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, sechdrs[pcpuindex].sh_size);
lib/Makefile +1 -1
@@ -5,7 +5,7 @@ lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \ bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \ - kobject.o idr.o div64.o parser.o int_sqrt.o mask.o + kobject.o idr.o div64.o parser.o int_sqrt.o mask.o extable.o lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
lib/extable.c +75 -0
75 lines not on this page · raw message
Quoted 3 lines · On Mon, 19 Jan 2004 22:07:17 +1100 Paul Mackerras <[email protected]> wrote:
I have tested this on x86 and ppc with a module that uses
__get_userin an init function, deliberately laid out to get the exception table out of order, and it works (whereas it oopsed without this patch).
I have no objections to this patch, however I'd like to be reminded why we desire to sort this stuff, performance?
Quoted 3 lines · David S. Miller
I have no objections to this patch, however I'd like to be reminded why we desire to sort this stuff, performance?
Yes, we've used a binary search for the last few years to look up the entry, because a linear search makes things like real SIGSEGV's really _really_ slow.
The linker sorts the thing normally, but gets confused by different code sections. And while we right now only have init/exit sections (in addition to the normal code one), that already has caused problems several times.
Linus