Skip to contentSkip to conversation

Speed booting by sorting ORC unwind tables at build time

RFC PATCH Superseded [email protected] 3 participants 12 messages Nov 7, 2019 – Nov 11, 2019

Subsystems (3)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover Speed booting by sorting ORC unwind tables at build time

From: Shile Zhang <[email protected]>

Hi,

I found the unwind_init taken long time (more than 90ms) in kernel booting, mainly spent on sorting the two ORC unwind tables, orc_unwind and orc_unwind_ip.

I also noticed that this issued has reported and discussed last year:
https://lkml.org/lkml/2018/10/8/342
But seems no final solution until now, I tried to sort the ORC tables at build time, followed the helpful hints from Josh and Ingo in that thread. And mainly referred the implementation of 'sortextable' tool:
https://lore.kernel.org/linux-mips/[email protected]/

What I did:

- Add a Kconfig to control build-time sorting or runtime sorting;
- Referred 'sortextable', create a similar helper tool 'sortorctable', help to sort the ORC unwind tables at vmlinux link process.

One potential improvement is to sort the module ORC tables in future.

Thanks!

Shile Zhang (4):
scripts: Add sortorctable to sort ORC unwind tables
kbuild: Sort ORC unwind tables in vmlinux link process x86/unwind/orc: Skip sorting if BUILDTIME_ORCTABLE_SORT is configured x86/Kconfig: Add a Kconfig option to sort ORC tables at build time

 arch/x86/Kconfig.debug       |   9 ++
 arch/x86/kernel/unwind_orc.c |   2 +
 scripts/.gitignore           |   1 +
 scripts/Makefile             |   2 +
 scripts/link-vmlinux.sh      |  10 ++
 scripts/sortorctable.c       | 246 +++++++++++++++++++++++++++++++++++
 scripts/sortorctable.h       |  25 ++++
 7 files changed, 295 insertions(+)
 create mode 100644 scripts/sortorctable.c
 create mode 100644 scripts/sortorctable.h
Signature · 1 line
2.24.0.rc2
#1 lore raw

RFC PATCH 1/4 scripts: Add sortorctable to sort ORC unwind tables

From: Shile Zhang <[email protected]>

Sort orc_unwind and orc_unwind_ip tables at build time instead of runtime in boot pharse can save more boot time.

---
 scripts/.gitignore     |   1 +
 scripts/Makefile       |   2 +
 scripts/sortorctable.c | 246 +++++++++++++++++++++++++++++++++++++++++
 scripts/sortorctable.h |  25 +++++
 4 files changed, 274 insertions(+)
 create mode 100644 scripts/sortorctable.c
 create mode 100644 scripts/sortorctable.h
scripts/.gitignore +1 -0
index 17f8cef88fa8..52976f32f974 100644
@@ -12,3 +12,4 @@ asn1_compiler
 extract-cert
 sign-file
 insert-sys-cert
+sortorctable
scripts/Makefile +2 -0
index 3e86b300f5a1..51b2d465f042 100644
@@ -16,12 +16,14 @@ hostprogs-$(CONFIG_LOGO)         += pnmtologo
 hostprogs-$(CONFIG_VT)           += conmakehash
 hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount
 hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable
+hostprogs-$(CONFIG_BUILDTIME_ORCTABLE_SORT) += sortorctable
 hostprogs-$(CONFIG_ASN1)	 += asn1_compiler
 hostprogs-$(CONFIG_MODULE_SIG_FORMAT) += sign-file
 hostprogs-$(CONFIG_SYSTEM_TRUSTED_KEYRING) += extract-cert
 hostprogs-$(CONFIG_SYSTEM_EXTRA_CERTIFICATE) += insert-sys-cert
 
 HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include
+HOSTCFLAGS_sortorctable.o = -I$(srctree)/tools/include
 HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
 HOSTLDLIBS_sign-file = -lcrypto
 HOSTLDLIBS_extract-cert = -lcrypto
scripts/sortorctable.c new file +246 -0

246 lines not on this page · raw message

scripts/sortorctable.h new file +25 -0
new file mode 100644
index 000000000000..8a7d3bd8b01b
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2019 Shile Zhang <[email protected]>
+ *
+ * This code was taken out of arch/x86/include/asm/orc_types.h written by:
+ * Copyright (C) 2017 Josh Poimboeuf <[email protected]>
+ */
+
+#ifndef _SORTORCTABLE_H_
+#define _SORTORCTABLE_H_
+
+#include <linux/types.h>
+
+#define ORC_REG_UNDEFINED	0
+
+struct orc_entry {
+	s16		sp_offset;
+	s16		bp_offset;
+	unsigned	sp_reg:4;
+	unsigned	bp_reg:4;
+	unsigned	type:2;
+	unsigned	end:1;
+} __attribute__((packed));
+
+#endif//_SORTORCTABLE_H_
Signature · 1 line
2.24.0.rc2
#2 lore raw

RFC PATCH 2/4 kbuild: Sort ORC unwind tables in vmlinux link process

In reply to [email protected]

From: Shile Zhang <[email protected]>

To sort the ORC unwind tables in vmlinux link process, controlled by configure BUILDTIME_ORCTABLE_SORT.

---
 scripts/link-vmlinux.sh | 10 ++++++++++
 1 file changed, 10 insertions(+)
scripts/link-vmlinux.sh +10 -0
index 06495379fcd8..43fe8c151c8d 100755
@@ -183,6 +183,11 @@ sortextable()
 	${objtree}/scripts/sortextable ${1}
 }
 
+sortorctable()
+{
+	${objtree}/scripts/sortorctable ${1}
+}
+
 # Delete output files in case of error
 cleanup()
 {
@@ -303,6 +308,11 @@ if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
 	sortextable vmlinux
 fi
 
+if [ -n "${CONFIG_BUILDTIME_ORCTABLE_SORT}" ]; then
+	info SORTORC vmlinux
+	sortorctable vmlinux
+fi
+
 info SYSMAP System.map
 mksysmap vmlinux System.map
 
Signature · 1 line
2.24.0.rc2
#3 lore raw

RFC PATCH 3/4 x86/unwind/orc: Skip sorting if BUILDTIME_ORCTABLE_SORT is configured

In reply to [email protected]

From: Shile Zhang <[email protected]>

The orc_unwind and orc_unwind_ip tables are sorted at build time if BUILDTIME_ORCTABLE_SORT is configured, skip sort at boot time.

---
 arch/x86/kernel/unwind_orc.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kernel/unwind_orc.c +2 -0
index 332ae6530fa8..2c2fa9ef116e 100644
@@ -273,9 +273,11 @@ void __init unwind_init(void)
 		return;
 	}
 
+#ifndef CONFIG_BUILDTIME_ORCTABLE_SORT
 	/* Sort the .orc_unwind and .orc_unwind_ip tables: */
 	sort(__start_orc_unwind_ip, num_entries, sizeof(int), orc_sort_cmp,
 	     orc_sort_swap);
+#endif
 
 	/* Initialize the fast lookup table: */
 	lookup_num_blocks = orc_lookup_end - orc_lookup;
Signature · 1 line
2.24.0.rc2
#4 lore raw

RFC PATCH 4/4 x86/Kconfig: Add a Kconfig option to sort ORC tables at build time

In reply to [email protected]

From: Shile Zhang <[email protected]>

Add a new Kconfig BUILDTIME_ORCTABLE_SORT to control the ORC unwind tables at build time. Select for ORC unwinder on x86_64 by default.

---
 arch/x86/Kconfig.debug | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/x86/Kconfig.debug +9 -0
index bf9cd83de777..320ff2af4837 100644
@@ -335,6 +335,15 @@ config UNWINDER_GUESS
 
 endchoice
 
+config BUILDTIME_ORCTABLE_SORT
+	bool "Sort ORC unwind tables at build time"
+	depends on X86_64
+	depends on UNWINDER_ORC
+	default y
+	help
+	This option enables the build-time sorting for ORC unwind tables. It
+	can help to speed up kernel boot by skip the runtime sorting.
+
 config FRAME_POINTER
 	depends on !UNWINDER_ORC && !UNWINDER_GUESS
 	bool
Signature · 1 line
2.24.0.rc2
maintainer
#5 lore raw
In reply to [email protected]
Quoted 15 lines · On Thu, Nov 07, 2019 at 10:32:01PM +0800, [email protected] wrote:

From: Shile Zhang <[email protected]>

Hi,

I found the unwind_init taken long time (more than 90ms) in kernel booting, mainly spent on sorting the two ORC unwind tables, orc_unwind and orc_unwind_ip.

I also noticed that this issued has reported and discussed last year:
https://lkml.org/lkml/2018/10/8/342
But seems no final solution until now, I tried to sort the ORC tables at build time, followed the helpful hints from Josh and Ingo in that thread. And mainly referred the implementation of 'sortextable' tool:
https://lore.kernel.org/linux-mips/[email protected]/

Quoted 5 lines

What I did:

- Add a Kconfig to control build-time sorting or runtime sorting;
- Referred 'sortextable', create a similar helper tool 'sortorctable', help to sort the ORC unwind tables at vmlinux link process.

What is the build-time cost for doing this? The link phase is already a fairly big bottleneck for building a kernel.

Can sort{ex,orc}table() be ran concurrently? Do they want to be the same (threaded) tool?

#6 lore raw
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · On Thu, Nov 07, 2019 at 10:32:01PM +0800, [email protected] wrote:

From: Shile Zhang <[email protected]>

Hi,

I found the unwind_init taken long time (more than 90ms) in kernel booting, mainly spent on sorting the two ORC unwind tables, orc_unwind and orc_unwind_ip.

I also noticed that this issued has reported and discussed last year:
https://lkml.org/lkml/2018/10/8/342
But seems no final solution until now, I tried to sort the ORC tables at build time, followed the helpful hints from Josh and Ingo in that thread. And mainly referred the implementation of 'sortextable' tool:
https://lore.kernel.org/linux-mips/[email protected]/

Quoted 7 lines
Quoted 5 lines

What I did:

- Add a Kconfig to control build-time sorting or runtime sorting;
- Referred 'sortextable', create a similar helper tool 'sortorctable', help to sort the ORC unwind tables at vmlinux link process.

What is the build-time cost for doing this? The link phase is already a fairly big bottleneck for building a kernel.

Hi, Peter, Thanks for your kindly reply!
On my test env, the build-time sort spend about 100ms, which is similar to runtime sorting due to the same sorting code. Of course there are few compiling cost in sortorctable tool itself. I think the overall cost of this build-time sorting is not so much.

I agree with you that the link time of vmlinux shoud be optimized.
But IMHO, for one kernel product release, the kernel building is once for all. So put the sorting in build time can save boot time for customer, for each booting. I think this is significant for boot time sensitive products, such as embedded devices in IoT, or VM in Cloud.

Quoted 2 lines

Can sort{ex,orc}table() be ran concurrently? Do they want to be the same (threaded) tool?

I think it is possible to do those sort work concurrently, likes deferred memory init which is big boot time speed up.
But I don't know if the exception table and ORC unwind tables can be deferred, due to those tables might be used in early boot time, for early exception handling and early debugging. I'm not familiar with that.

IMO, the init works in kernel boot time should do the necessary runtime-depends initialization, which cannot done out-of-bands. For exception, ORC unwind like tables, which does not depends on the runtime ENV. It can/should be ready in building time. IOW, this kind of "setup cost" should not put on customer.

Thanks again!

maintainer
#7 lore raw
Quoted 8 lines · Shile Zhang
Quoted 2 lines

Can sort{ex,orc}table() be ran concurrently? Do they want to be the same (threaded) tool?

I think it is possible to do those sort work concurrently, likes deferred memory init which is big boot time speed up.
But I don't know if the exception table and ORC unwind tables can be deferred, due to those tables might be used in early boot time, for early exception handling and early debugging. I'm not familiar with that.

I meant at link time, run both sorts concurrently such that we only have to wait for the longest, instead of the sum of them.

They're not changing the same part of the ELF file, so it should be possible to have one tool have multiple threads, each sorting a different table.

Aside from the .ex_table and ORC there's also .jump_table that wants sorting (see jump_label_sort_entries()).

I agree that doing it at link time makes sense, I just hate to do all this sorting in sequence and blowing up the link time. I don't build for customers, I build for single use boot and linking _SUCKS_.

maintainer
#8 lore raw
Quoted 14 lines · Peter Zijlstra
Quoted 8 lines · Shile Zhang
Quoted 2 lines

Can sort{ex,orc}table() be ran concurrently? Do they want to be the same (threaded) tool?

I think it is possible to do those sort work concurrently, likes deferred memory init which is big boot time speed up.
But I don't know if the exception table and ORC unwind tables can be deferred, due to those tables might be used in early boot time, for early exception handling and early debugging. I'm not familiar with that.

I meant at link time, run both sorts concurrently such that we only have to wait for the longest, instead of the sum of them.

Quoted 6 lines

They're not changing the same part of the ELF file, so it should be possible to have one tool have multiple threads, each sorting a different table.

Aside from the .ex_table and ORC there's also .jump_table that wants sorting (see jump_label_sort_entries()).

Oh, and I'll be adding .static_call_sites soon, see:

https://lkml.kernel.org/r/[email protected]

(I should repost that)

That gives us 4 tables to sort which we can do concurrently in 4 threads.

Quoted 3 lines

I agree that doing it at link time makes sense, I just hate to do all this sorting in sequence and blowing up the link time. I don't build for customers, I build for single use boot and linking _SUCKS_.

#9 lore raw
Quoted 19 lines · Peter Zijlstra
Quoted 12 lines · Peter Zijlstra
Quoted 7 lines · Shile Zhang
Quoted 2 lines

Can sort{ex,orc}table() be ran concurrently? Do they want to be the same (threaded) tool?

I think it is possible to do those sort work concurrently, likes deferred memory init which is big boot time speed up.
But I don't know if the exception table and ORC unwind tables can be deferred, due to those tables might be used in early boot time, for early exception handling and early debugging. I'm not familiar with that.

I meant at link time, run both sorts concurrently such that we only have to wait for the longest, instead of the sum of them.

Quoted 6 lines

They're not changing the same part of the ELF file, so it should be possible to have one tool have multiple threads, each sorting a different table.

Aside from the .ex_table and ORC there's also .jump_table that wants sorting (see jump_label_sort_entries()).

Quoted 8 lines

Oh, and I'll be adding .static_call_sites soon, see:

   https://lkml.kernel.org/r/[email protected]

(I should repost that)

That gives us 4 tables to sort which we can do concurrently in 4 threads.

I got your point now.
I'll try to rework the sort tool to sort all tables concurrently in one tool with multiple-threads. Thanks for your advice!

Quoted 3 lines
Quoted 3 lines

I agree that doing it at link time makes sense, I just hate to do all this sorting in sequence and blowing up the link time. I don't build for customers, I build for single use boot and linking _SUCKS_.

#10 lore raw
In reply to [email protected]
Quoted 17 lines · On Thu, Nov 07, 2019 at 10:32:01PM +0800, [email protected] wrote:

From: Shile Zhang <[email protected]>

Hi,

I found the unwind_init taken long time (more than 90ms) in kernel booting, mainly spent on sorting the two ORC unwind tables, orc_unwind and orc_unwind_ip.

I also noticed that this issued has reported and discussed last year:
https://lkml.org/lkml/2018/10/8/342
But seems no final solution until now, I tried to sort the ORC tables at build time, followed the helpful hints from Josh and Ingo in that thread. And mainly referred the implementation of 'sortextable' tool:
https://lore.kernel.org/linux-mips/[email protected]/

What I did:

Quoted 7 lines

- Add a Kconfig to control build-time sorting or runtime sorting;
- Referred 'sortextable', create a similar helper tool 'sortorctable', help to sort the ORC unwind tables at vmlinux link process.

One potential improvement is to sort the module ORC tables in future.

Thanks!

Thanks a lot for working on this!

I'd say the new config option isn't needed. The runtime ORC sorting logic is unconditionally bad and the code should just be removed. I saw recently that it's one of the main offenders for boot time latency.

I also agree with Peter that we should try to reduce the link-time penalty as much as possible. But it's a necessary evil to a certain extent.

Signature · 1 line
Josh
#11 lore raw
Quoted 22 lines · Josh Poimboeuf
Quoted 21 lines · On Thu, Nov 07, 2019 at 10:32:01PM +0800, [email protected] wrote:

From: Shile Zhang <[email protected]>

Hi,

I found the unwind_init taken long time (more than 90ms) in kernel booting, mainly spent on sorting the two ORC unwind tables, orc_unwind and orc_unwind_ip.

I also noticed that this issued has reported and discussed last year:
https://lkml.org/lkml/2018/10/8/342
But seems no final solution until now, I tried to sort the ORC tables at build time, followed the helpful hints from Josh and Ingo in that thread. And mainly referred the implementation of 'sortextable' tool:
https://lore.kernel.org/linux-mips/[email protected]/

What I did:

- Add a Kconfig to control build-time sorting or runtime sorting;
- Referred 'sortextable', create a similar helper tool 'sortorctable', help to sort the ORC unwind tables at vmlinux link process.

Quoted 8 lines
Quoted 3 lines

One potential improvement is to sort the module ORC tables in future.

Thanks!

Thanks a lot for working on this!

I'd say the new config option isn't needed. The runtime ORC sorting logic is unconditionally bad and the code should just be removed. I saw recently that it's one of the main offenders for boot time latency.

Hi, Josh, Thanks very much for your quickly response!

I'll refactor the code as your advice.
Yes, the run-time sorting cost is bigger for currently Cloud products, such as serverless products, which needs boot up ASAP.

Quoted 5 lines

I also agree with Peter that we should try to reduce the link-time penalty as much as possible. But it's a necessary evil to a certain extent.

agree!

Thanks again and looking forwards more advice from you!