Speed booting by sorting exception tables at build time.
Subsystems (4)
cover Speed booting by sorting exception tables at build time.
From: David Daney <[email protected]>
I noticed when booting MIPS64 kernels that sorting of the main __ex_table was taking a long time (2,692,220 cycles or 3.3 mS at 800MHz to be exact). That is not too bad for real silicon implementations, but when running on a slow simulator, it can be significant.
I can get this down to about 1,000,000 cycles by supplying a custom swap function for the sort, but even better is to eliminate it entirely by doing the sort at build time. That is the idea behind this patch set.
Here is more or less what I did:
o A flag word is added to the kernel to indicate that the __ex_table is already sorted. sort_main_extable() checks this and if it is clear, returns without doing the sort.
o I shamelessly stole code from recordmcount and created a new build time helper program 'sortextable'. This is run on the final vmlinux image, it sorts the table, and then clears the flag word.
Potential areas for improvement:
o Sort module exception tables too.
o Get rit of the flag word, and assume that if an architecture supports build time sorting, that it must have been done.
o Add support for architectures other than MIPS and x86
Any comments most welcome,
David Daney (5):
scripts: Add sortextable to sort the kernel's exception table.
extable: Skip sorting if sorted at build time. kbuild/extable: Hook up sortextable into the build system.
MIPS: Select BUILDTIME_EXTABLE_SORT
x86: Select BUILDTIME_EXTABLE_SORT
Makefile | 10 ++ arch/mips/Kconfig | 1 + arch/x86/Kconfig | 1 + init/Kconfig | 3 + kernel/extable.c | 8 ++- scripts/.gitignore | 1 + scripts/Makefile | 1 + scripts/sortextable.c | 273 +++++++++++++++++++++++++++++++++++++++++++++++++ scripts/sortextable.h | 168 ++++++++++++++++++++++++++++++ 9 files changed, 465 insertions(+), 1 deletions(-) create mode 100644 scripts/sortextable.c create mode 100644 scripts/sortextable.h
Signature · 1 line
1.7.2.3
RFC PATCH 1/5 scripts: Add sortextable to sort the kernel's exception table.
From: David Daney <[email protected]>
Using this build-time sort saves time booting as we don't have to burn cycles sorting the exception table.
--- scripts/.gitignore | 1 + scripts/Makefile | 1 + scripts/sortextable.c | 273 +++++++++++++++++++++++++++++++++++++++++++++++++ scripts/sortextable.h | 168 ++++++++++++++++++++++++++++++ 4 files changed, 443 insertions(+), 0 deletions(-) create mode 100644 scripts/sortextable.c create mode 100644 scripts/sortextable.h
scripts/.gitignore +1 -0
@@ -9,3 +9,4 @@ unifdef ihex2fw recordmcount docproc +sortextable
scripts/Makefile +1 -0
@@ -13,6 +13,7 @@ hostprogs-$(CONFIG_LOGO) += pnmtologo hostprogs-$(CONFIG_VT) += conmakehash hostprogs-$(CONFIG_IKCONFIG) += bin2c hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount +hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable always := $(hostprogs-y) $(hostprogs-m)
scripts/sortextable.c new file +273 -0
273 lines not on this page · raw message
scripts/sortextable.h new file +168 -0
168 lines not on this page · raw message
Signature · 1 line
1.7.2.3
RFC PATCH 2/5 extable: Skip sorting if sorted at build time.
In reply to David DaneyFrom: David Daney <[email protected]>
If the build program sortextable has already sorted the exception table, don't sort it again.
--- kernel/extable.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)
kernel/extable.c +7 -1
@@ -35,10 +35,16 @@ DEFINE_MUTEX(text_mutex);extern struct exception_table_entry __start___ex_table[];extern struct exception_table_entry __stop___ex_table[]; +/* Cleared by build time tools if the table is already sorted. */ +u32 __initdata main_extable_sort_needed =1 ; +/* Sort the kernel's built-in exception table */ void __init sort_main_extable (void ) { -sort_extable (__start___ex_table, __stop___ex_table); +if (main_extable_sort_needed) +sort_extable (__start___ex_table, __stop___ex_table); +else +pr_notice ("__ex_table already sorted, skipping sort\n" ); }/* Given an address, look for it in the exception tables. */
Signature · 1 line
1.7.2.3
RFC PATCH 3/5 kbuild/extable: Hook up sortextable into the build system.
In reply to David DaneyFrom: David Daney <[email protected]>
Define a config variable BUILDTIME_EXTABLE_SORT to control build time sorting of the kernel's exception table.
Patch Makefile to do the sorting when BUILDTIME_EXTABLE_SORT is selected.
--- Makefile | 10 ++++++++++ init/Kconfig | 3 +++ 2 files changed, 13 insertions(+), 0 deletions(-)
Makefile +10 -0
@@ -784,6 +784,10 @@ quiet_cmd_vmlinux_version = GEN .version quiet_cmd_sysmap = SYSMAP cmd_sysmap = $(CONFIG_SHELL) $(srctree)/scripts/mksysmap +# Sort exception table at build time +quiet_cmd_sortextable = SORTEX + cmd_sortextable = $(objtree)/scripts/sortextable + # Link of vmlinux # If CONFIG_KALLSYMS is set .version is already updated # Generate System.map and verify that the content is consistent
@@ -796,6 +800,12 @@ define rule_vmlinux__ $(call cmd,vmlinux__) $(Q)echo 'cmd_$@ := $(cmd_vmlinux__)' > $(@D)/.$(@F).cmd + $(if $(CONFIG_BUILDTIME_EXTABLE_SORT), \ + $(Q)$(if $($(quiet)cmd_sortextable), \ + echo ' $($(quiet)cmd_sortextable) vmlinux' &&) \ + $(cmd_sortextable) vmlinux) + + $(Q)$(if $($(quiet)cmd_sysmap), \ echo ' $($(quiet)cmd_sysmap) System.map' &&) \ $(cmd_sysmap) $@ System.map; \
init/Kconfig +3 -0
@@ -27,6 +27,9 @@ config IRQ_WORK bool depends on HAVE_IRQ_WORK +config BUILDTIME_EXTABLE_SORT + bool + menu "General setup" config EXPERIMENTAL
Signature · 1 line
1.7.2.3
RFC PATCH 4/5 MIPS: Select BUILDTIME_EXTABLE_SORT
In reply to David DaneyFrom: David Daney <[email protected]>
We can sort the exeception table at build time for MIPS, so let's do it.
--- arch/mips/Kconfig | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
arch/mips/Kconfig +1 -0
@@ -25,6 +25,7 @@ config MIPS select GENERIC_IRQ_SHOW select HAVE_ARCH_JUMP_LABEL select IRQ_FORCED_THREADING + select BUILDTIME_EXTABLE_SORT menu "Machine selection"
Signature · 1 line
1.7.2.3
RFC PATCH 5/5 x86: Select BUILDTIME_EXTABLE_SORT
In reply to David DaneyFrom: David Daney <[email protected]>
We can sort the exeception table at build time for x86, so let's do it.
--- arch/x86/Kconfig | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/Kconfig +1 -0
@@ -75,6 +75,7 @@ config X86 select HAVE_BPF_JIT if (X86_64 && NET) select CLKEVT_I8253 select ARCH_HAVE_NMI_SAFE_CMPXCHG + select BUILDTIME_EXTABLE_SORT config INSTRUCTION_DECODER def_bool (KPROBES || PERF_EVENTS)
Signature · 1 line
1.7.2.3
Re: [PATCH RFC 3/5] kbuild/extable: Hook up sortextable into the build system.
In reply to David DaneyMakefile define rule_vmlinux__ · line 806
Quoted 4 lines · David Daney
Makefile +4
-0+ $(if $(CONFIG_BUILDTIME_EXTABLE_SORT), \ + $(Q)$(if $($(quiet)cmd_sortextable), \ + echo ' $($(quiet)cmd_sortextable) vmlinux' &&) \ + $(cmd_sortextable) vmlinux)
Why do you opencode $(call cmd,sortextable) here?
Michal
Quoted 5 lines · On Friday 18 November 2011 14:37:43 David Daney wrote:
I noticed when booting MIPS64 kernels that sorting of the main
__ex_tablewas taking a long time (2,692,220 cycles or 3.3 mS at 800MHz to be exact). That is not too bad for real silicon implementations, but when running on a slow simulator, it can be significant.
i've seen this perf hit in my simulation runs too
Quoted 10 lines
Here is more or less what I did:
o A flag word is added to the kernel to indicate that the
__ex_tableis already sorted.sort_main_extable()checks this and if it is clear, returns without doing the sort.o I shamelessly stole code from recordmcount and created a new build time helper program 'sortextable'. This is run on the final vmlinux image, it sorts the table, and then clears the flag word.
Quoted 8 lines
Potential areas for improvement:
o Sort module exception tables too.
o Get rit of the flag word, and assume that if an architecture supports build time sorting, that it must have been done.
o Add support for architectures other than MIPS and x86
i don't see much here that is arch-specific. why have a knob at all ? let's just jump in with both feet and do this for everyone :).
-mike
Re: [PATCH RFC 1/5] scripts: Add sortextable to sort the kernel's exception table.
In reply to David Daneyscripts/sortextable.c line 6
Quoted 9 lines · On Friday 18 November 2011 14:37:44 David Daney wrote:
scripts/sortextable.c new file +6
-0+/* +* sortextable.c: Sort the kernel's exception table +* +* Copyright 2011 Cavium, Inc. +* +* Based on code taken from recortmcount.c which is:
seems like it'd be nice if the duplicate helper funcs were placed in a common header file rather than copying & pasting between them.
scripts/sortextable.c line 201
Quoted 11 lines
scripts/sortextable.c new file +11
-0+switch (w2 (ehdr->e_machine)) { +default : +fprintf (stderr,"unrecognized e_machine %d %s\n" , +w2 (ehdr->e_machine), fname); +fail_file (); +break ; +case EM_386 : +case EM_MIPS : +case EM_X86_64 : +break ; + }/* end switch */
unlike recordmcount, this file doesn't do anything arch specific. so let's just delete this and be done.
-mike
Re: [PATCH RFC 1/5] scripts: Add sortextable to sort the kernel's exception table.
In reply to David DaneyQuoted 5 lines · David Daney
From: David Daney <[email protected]>
Using this build-time sort saves time booting as we don't have to burn cycles sorting the exception table.
If we're going to do this at build time, I would suggest using a collisionless hash instead. The lookup time for those are O(1), but they definitely need to be done at build time.
-hpa
Re: [PATCH RFC 1/5] scripts: Add sortextable to sort the kernel's exception table.
Quoted 11 lines · H. Peter Anvin
Quoted 5 lines · David Daney
From: David Daney <[email protected]>
Using this build-time sort saves time booting as we don't have to burn cycles sorting the exception table.
If we're going to do this at build time, I would suggest using a collisionless hash instead. The lookup time for those are O(1), but they definitely need to be done at build time.
I have some code for generating these kinds of tables, they just need to be hooked up. I will dig it up later today or tomorrow.
-hpa
Re: [PATCH RFC 1/5] scripts: Add sortextable to sort the kernel's exception table.
In reply to H. Peter AnvinQuoted 3 lines · H. Peter Anvin
If we're going to do this at build time, I would suggest using a collisionless hash instead. The lookup time for those are O(1), but they definitely need to be done at build time.
Is the lookup time really an issue?
Signature · 1 line
dwmw2
Re: [PATCH RFC 1/5] scripts: Add sortextable to sort the kernel's exception table.
Quoted 7 lines · David Woodhouse
Quoted 3 lines · H. Peter Anvin
If we're going to do this at build time, I would suggest using a collisionless hash instead. The lookup time for those are O(1), but they definitely need to be done at build time.
Is the lookup time really an issue?
Probably not a big one (in most scenarios), but with better exception handling it might stretch the usability of exceptions. The bigger thing is that once you're doing a build-time special handler for this *anyway*, you might as well drive the cost of the lookup to functionally zero.
-hpa
Re: [PATCH RFC 1/5] scripts: Add sortextable to sort the kernel's exception table.
In reply to Mike FrysingerQuoted 4 lines · Mike Frysinger
Quoted 3 lines · On Friday 18 November 2011 14:37:44 David Daney wrote:
scripts/sortextable.c new file +0
-0
Quoted 10 lines
Quoted 6 lines
+/*
+ *sortextable.c: Sort the kernel's exception table
+ *
+ * Copyright 2011 Cavium, Inc.
+ *
+ * Based on code taken fromrecortmcount.cwhich is:seems like it'd be nice if the duplicate helper funcs were placed in a common header file rather than copying& pasting between them.
Yes, I may try to factor out some common code if we decide to move forward with the patch set.
Quoted 6 lines
Quoted 6 lines
scripts/sortextable.c new file +6
-0+switch (w2 (ehdr->e_machine)) { +default : +fprintf (stderr,"unrecognized e_machine %d %s\n" , +w2 (ehdr->e_machine), fname); +fail_file (); +break ;
Quoted 8 lines
scripts/sortextable.c line 201
Quoted 5 lines
scripts/sortextable.c new file +5
-0+case EM_386 : +case EM_MIPS : +case EM_X86_64 : +break ; + }/* end switch */ unlike recordmcount, this file doesn't do anything arch specific. so let's just delete this and be done.
Not really true at this point. We don't know the size or layout of the architecture specific exception table entries, likewise for CONFIG_ARCH_HAS_SORT_EXTABLE, we don't even know how to do the comparison.
I was trying to be a little conservative and only apply the build time sort to configurations that I could test.
David Daney
Re: [PATCH RFC 1/5] scripts: Add sortextable to sort the kernel's exception table.
Quoted 17 lines · On Monday 21 November 2011 13:25:36 David Daney wrote:
Quoted 15 lines · Mike Frysinger
scripts/sortextable.c line 201
Quoted 11 lines · On Friday 18 November 2011 14:37:44 David Daney wrote:
scripts/sortextable.c new file +11
-0+switch (w2 (ehdr->e_machine)) { +default : +fprintf (stderr,"unrecognized e_machine %d %s\n" , +w2 (ehdr->e_machine), fname); +fail_file (); +break ; +case EM_386 : +case EM_MIPS : +case EM_X86_64 : +break ; + }/* end switch */ unlike recordmcount, this file doesn't do anything arch specific. so let's just delete this and be done.
Quoted 3 lines
Not really true at this point. We don't know the size or layout of the architecture specific exception table entries, likewise for
CONFIG_ARCH_HAS_SORT_EXTABLE, we don't even know how to do the comparison.
all of your code that i could see is based on "is it 32bit or is it 64bit". there is no code that says "if it's x86, we need to do XXX".
when i look in the kernel, we have common code behind ARCH_HAS_SORT_EXTABLE. so you could easily do the same thing:
scripts/sortextable.c:
#ifdef ARCH_HAS_SORT_EXTABLE
switch (w2(ehdr->e_machine)) {
default:
fprintf(stderr, "unrecognized e_machine %d %s\n",
w2(ehdr->e_machine), fname);
... return a unique exit code like 77 ...
break;
/* add arch sorting info here */
} /* end switch */
#endifkernel/extable.c:
#if defined(ARCH_HAS_SORT_EXTABLE) && !defined(ARCH_HAS_SORTED_EXTABLE)
void __init sort_main_extable(void)
{
sort_extable(__start___ex_table, __stop___ex_table);
}
#endifthis way all the people not doing unique stuff work out of the box. only the people who are doing funky stuff need to extend things.
-mike
Re: [PATCH RFC 1/5] scripts: Add sortextable to sort the kernel's exception table.
In reply to H. Peter AnvinQuoted 11 lines · H. Peter Anvin
Quoted 5 lines · David Daney
From: David Daney<[email protected]>
Using this build-time sort saves time booting as we don't have to burn cycles sorting the exception table.
If we're going to do this at build time, I would suggest using a collisionless hash instead. The lookup time for those are O(1), but they definitely need to be done at build time.
It is my understanding that such a hash table would be sparsely populated, so space would have to be reserved for the empty buckets. The current patch, which works in-place on the fully linked vmlinux, doesn't have to worry about finding enough space for the table.
If we were to do the collisionless hash, we would somehow have to reserve space for the empty buckets.
On my test kernel, there were only 1453 entries in the exception table, So doing the binary search takes a maximum of 11 loads.
So, I guess I am not strongly opposed to using a collisionless hash, but I think it may not be worth the extra effort.
David Daney
Re: [PATCH RFC 1/5] scripts: Add sortextable to sort the kernel's exception table.
In reply to Mike FrysingerQuoted 18 lines · Mike Frysinger
Quoted 17 lines · On Monday 21 November 2011 13:25:36 David Daney wrote:
Quoted 15 lines · Mike Frysinger
scripts/sortextable.c line 201
Quoted 11 lines · On Friday 18 November 2011 14:37:44 David Daney wrote:
scripts/sortextable.c new file +11
-0+switch (w2 (ehdr->e_machine)) { +default : +fprintf (stderr,"unrecognized e_machine %d %s\n" , +w2 (ehdr->e_machine), fname); +fail_file (); +break ; +case EM_386 : +case EM_MIPS : +case EM_X86_64 : +break ; + }/* end switch */ unlike recordmcount, this file doesn't do anything arch specific. so let's just delete this and be done.
Quoted 6 lines
Quoted 3 lines
Not really true at this point. We don't know the size or layout of the architecture specific exception table entries, likewise for
CONFIG_ARCH_HAS_SORT_EXTABLE, we don't even know how to do the comparison.all of your code that i could see is based on "is it 32bit or is it 64bit". there is no code that says "if it's x86, we need to do XXX".
At this point there is no need. MIPS, i386 and x86_64 all store the key in the first word of a two word structure.
If there were some architecture that didn't fit this model, we would have to create a special sorting function and select it (and perhaps other parameters as well) in that switch statement.
Quoted 16 lines
when i look in the kernel, we have common code behind
ARCH_HAS_SORT_EXTABLE. so you could easily do the same thing:
scripts/sortextable.c:#ifdef ARCH_HAS_SORT_EXTABLE switch (w2(ehdr->e_machine)) { default: fprintf(stderr, "unrecognized e_machine %d %s\n", w2(ehdr->e_machine), fname); ... return a unique exit code like 77 ... break; /* add arch sorting info here */ } /* end switch */ #endif
Quoted 8 lines
kernel/extable.c:#if defined(ARCH_HAS_SORT_EXTABLE)&& !defined(ARCH_HAS_SORTED_EXTABLE) void __init sort_main_extable(void) { sort_extable(__start___ex_table, __stop___ex_table); } #endif
Yes, I am familiar with that code. One thing to keep in mind is that the compiler has access to struct exception_table_entry, and can easily figure out both how big the structure is *and* where the insn field is within the structure.
This is not the case for the author of sortextable. Except for MIPS, MIPS64, i386 and x86_64, I know neither the size of struct exception_table_entry, nor the offset of its insn field.
For those with knowledge of the inner working of other architectures, it may be as simple as a two line patch to add support, but it isn't something that I want to take responsibility for at this point
Quoted 2 lines
this way all the people not doing unique stuff work out of the box. only the people who are doing funky stuff need to extend things.
I didn't want to include something like this that I cannot test. An unsorted (or improperly sorted) exception table is not necessarily something that will be noticeable by simply booting the kernel. Your only indication may be a panic or OOPS under rarely encountered conditions.
David Daney
Re: [PATCH RFC 1/5] scripts: Add sortextable to sort the kernel's exception table.
Quoted 27 lines · On Monday 21 November 2011 14:16:04 David Daney wrote:
Quoted 18 lines · Mike Frysinger
Quoted 17 lines · On Monday 21 November 2011 13:25:36 David Daney wrote:
Quoted 15 lines · Mike Frysinger
scripts/sortextable.c line 201
Quoted 11 lines · On Friday 18 November 2011 14:37:44 David Daney wrote:
scripts/sortextable.c new file +11
-0+switch (w2 (ehdr->e_machine)) { +default : +fprintf (stderr,"unrecognized e_machine %d %s\n" , +w2 (ehdr->e_machine), fname); +fail_file (); +break ; +case EM_386 : +case EM_MIPS : +case EM_X86_64 : +break ; + }/* end switch */ unlike recordmcount, this file doesn't do anything arch specific. so let's just delete this and be done.
Quoted 7 lines
Quoted 4 lines
Not really true at this point. We don't know the size or layout of the architecture specific exception table entries, likewise for
CONFIG_ARCH_HAS_SORT_EXTABLE, we don't even know how to do the comparison.all of your code that i could see is based on "is it 32bit or is it 64bit". there is no code that says "if it's x86, we need to do XXX".
Quoted 6 lines
At this point there is no need. MIPS, i386 and
x86_64all store the key in the first word of a two word structure.If there were some architecture that didn't fit this model, we would have to create a special sorting function and select it (and perhaps other parameters as well) in that switch statement.
that's trivial to check:
sed -n '/^struct exception_table_entry/,/};/p'\ arch/*/include/asm/uaccess* include/asm-generic/uaccess.h
and indeed, the only arches that don't follow this model are the ones that define ARCH_HAS_SORT_EXTABLE.
Quoted 31 lines
Quoted 22 lines
when i look in the kernel, we have common code behind
ARCH_HAS_SORT_EXTABLE. so you could easily do the same thing:
scripts/sortextable.c:#ifdef ARCH_HAS_SORT_EXTABLE switch (w2(ehdr->e_machine)) { default: fprintf(stderr, "unrecognized e_machine %d %s\n", w2(ehdr->e_machine), fname); ... return a unique exit code like 77 ... break; /* add arch sorting info here */ } /* end switch */ #endif
kernel/extable.c:Quoted 8 lines
#if defined(ARCH_HAS_SORT_EXTABLE)&& !defined(ARCH_HAS_SORTED_EXTABLE) void __init sort_main_extable(void) { sort_extable(__start___ex_table, __stop___ex_table); } #endif
Quoted 8 lines
Yes, I am familiar with that code. One thing to keep in mind is that the compiler has access to struct
exception_table_entry, and can easily figure out both how big the structure is *and* where the insn field is within the structure.This is not the case for the author of sortextable. Except for MIPS, MIPS64, i386 and
x86_64, I know neither the size of structexception_table_entry, nor the offset of its insn field.
a trivial sed/grep gets you the answer: they're all the same
Quoted 7 lines
Quoted 2 lines
this way all the people not doing unique stuff work out of the box. only the people who are doing funky stuff need to extend things.
I didn't want to include something like this that I cannot test. An unsorted (or improperly sorted) exception table is not necessarily something that will be noticeable by simply booting the kernel. Your only indication may be a panic or OOPS under rarely encountered conditions.
this is what linux-next is for :)
-mike
Re: [PATCH RFC 3/5] kbuild/extable: Hook up sortextable into the build system.
In reply to Michal MarekQuoted 8 lines · Michal Marek
Makefile define rule_vmlinux__ · line 806
Quoted 4 lines · David Daney
Makefile +4
-0+ $(if $(CONFIG_BUILDTIME_EXTABLE_SORT), \ + $(Q)$(if $($(quiet)cmd_sortextable), \ + echo ' $($(quiet)cmd_sortextable) vmlinux'&&) \ + $(cmd_sortextable) vmlinux)Why do you opencode $(call cmd,sortextable) here?
Mostly just mimicking the other code in the vicinity.
I will try to improve it in the next version of the patch.
Thanks,
David Daney