Skip to contentSkip to conversation

x86, vmlinux.lds: Add debug option to force all data sections aligned

PATCH Superseded Feng Tang 2 participants 3 messages Apr 24, 2022 – Apr 25, 2022 x86

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Feng Tang

author
#0 lore raw

PATCH v2 1/1 x86, vmlinux.lds: Add debug option to force all data sections aligned

0day has reported many strange performance changes (regression or improvement), in which there was no obvious relation between the culprit commit and the benchmark at the first look, and it causes people to doubt the test itself is wrong.

Upon further check, many of these cases are caused by the change to the alignment of kernel text or data, as whole text/data of kernel are linked together, change in one domain can affect alignments of other domains.

To help quickly identifying if the strange performance change is caused by _data_ alignment, add a debug option to force the data sections from all .o files aligned on THREAD_SIZE, so that change in one domain won't affect other modules' data alignment.

We have used this option to check some strange kernel changes [1][2][3], and those performance changes were gone after enabling it, which proved they are data alignment related. Besides these publicly reported cases, recently there are other similar cases found by 0day, and this option has been actively used by 0Day for analyzing strange performance changes, and filter some from reporting out.

With the debug option on, the vmlinux is around 0.73% larger:

#ls -l vmlinux*
805891208 Apr 24 19:31 vmlinux.data-4k-align
799599752 Apr 24 19:28 vmlinux.raw

$size vmlinux*
text data bss dec hex filename

  17849671        22654886        4702208 45206765        2b1cced vmlinux.data-4k-align
  17849671        14784294        6275072 38909037        251b46d vmlinux.raw

Similarly, there is another kernel debug option to check text alignment related performance changes: CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B, which forces all function's start address to be 64 bytes alinged.

This option depends on CONFIG_DYNAMIC_DEBUG==n, as '__dyndbg' subsection of .data has a hard requirement of ALIGN(8), shown in the 'vmlinux.lds':

"
. = ALIGN(8); __start___dyndbg = .; KEEP(*(__dyndbg)) __stop___dyndbg = .; "

It contains all pointers to 'struct _ddebug', and dynamic_debug_init() will "pointer++" to loop accessing these pointers, which will be broken with this option enabled.

[1]. https://lore.kernel.org/lkml/20200205123216.GO12867@shao2-debian/
[2]. https://lore.kernel.org/lkml/20200305062138.GI5972@shao2-debian/
[3]. https://lore.kernel.org/lkml/20201112140625.GA21612@xsang-OptiPlex-9020/

---
Changelog:

since v1
* reduce the alignment from THREAD_SIZE to PAGE_SIZE
* refine the commit log with size change data, and code comments

since RFC (https://lore.kernel.org/lkml/[email protected]/)
* rebase against 5.17-rc1
* modify the changelog adding more recent info

 arch/x86/Kconfig.debug        | 13 +++++++++++++
 arch/x86/kernel/vmlinux.lds.S | 12 +++++++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)
arch/x86/Kconfig.debug +13 -0
index d3a6f74a94bd..7b8cfe9717f2 100644
@@ -225,6 +225,19 @@ config PUNIT_ATOM_DEBUG
 	  The current power state can be read from
 	  /sys/kernel/debug/punit_atom/dev_power_state
 
+config DEBUG_FORCE_DATA_SECTION_ALIGNED
+	bool "Force all data sections to be THREAD_SIZE aligned"
+	depends on EXPERT && !DYNAMIC_DEBUG
+	help
+	  There are cases that a commit from one kernel domain changes
+	  data sections' alignment of other domains, as they are all
+	  linked together compactly, and cause magic performance bump
+	  (regression or improvement), which is hard to debug. Enable
+	  this option will help to verify if the bump is caused by
+	  data alignment changes.
+
+	  It is mainly for debug and performance tuning use.
+
 choice
 	prompt "Choose kernel unwinder"
 	default UNWINDER_ORC if X86_64
arch/x86/kernel/vmlinux.lds.S +11 -1
index 7fda7f27e762..6add703de45f 100644
@@ -155,7 +155,17 @@ SECTIONS
 	X86_ALIGN_RODATA_END
 
 	/* Data */
-	.data : AT(ADDR(.data) - LOAD_OFFSET) {
+	.data : AT(ADDR(.data) - LOAD_OFFSET)
+#ifdef CONFIG_DEBUG_FORCE_DATA_SECTION_ALIGNED
+	/*
+	 * In theroy, THREAD_SIZE as the biggest alignment of below sections
+	 * should be picked, but since upper 'X86_ALIGN_RODATA_END' can
+	 * ganrantees the alignment of 'INIT_TASK_DATA', PAGE_SIZE is picked
+	 * instead to reduce size of kernel binary
+	 */
+	SUBALIGN(PAGE_SIZE)
+#endif
+	{
 		/* Start of data section */
 		_sdata = .;
 

--
2.27.0

#1 lore raw

arch/x86/Kconfig.debug config PUNIT_ATOM_DEBUG · line 229

Quoted 10 lines · Feng Tang
arch/x86/Kconfig.debug +2 -0
index d3a6f74a94bd..7b8cfe9717f2 100644
@@ -225,6 +225,19 @@ config PUNIT_ATOM_DEBUG
 	  The current power state can be read from
 	  /sys/kernel/debug/punit_atom/dev_power_state
 
+config DEBUG_FORCE_DATA_SECTION_ALIGNED
+	bool "Force all data sections to be THREAD_SIZE aligned"

s/THREAD_SIZE/PAGE_SIZE/ as that seems to be what the actual patch does.

Quoted 19 lines
arch/x86/Kconfig.debug +11 -0
+	depends on EXPERT && !DYNAMIC_DEBUG
+	help
+	  There are cases that a commit from one kernel domain changes
+	  data sections' alignment of other domains, as they are all
+	  linked together compactly, and cause magic performance bump
+	  (regression or improvement), which is hard to debug. Enable
+	  this option will help to verify if the bump is caused by
+	  data alignment changes.
+
+	  It is mainly for debug and performance tuning use.
+
 choice
 	prompt "Choose kernel unwinder"
 	default UNWINDER_ORC if X86_64
arch/x86/kernel/vmlinux.lds.S +0 -0
index 7fda7f27e762..6add703de45f 100644

arch/x86/kernel/vmlinux.lds.S SECTIONS · line 161

Quoted 8 lines
arch/x86/kernel/vmlinux.lds.S +4 -1
@@ -155,7 +155,17 @@ SECTIONS
 	X86_ALIGN_RODATA_END
 
 	/* Data */
-	.data : AT(ADDR(.data) - LOAD_OFFSET) {
+	.data : AT(ADDR(.data) - LOAD_OFFSET)
+#ifdef CONFIG_DEBUG_FORCE_DATA_SECTION_ALIGNED
+	/*
+	 * In theroy, THREAD_SIZE as the biggest alignment of below sections

I think the more common spelling is: 'theory' :-)

Quoted 13 lines
arch/x86/kernel/vmlinux.lds.S +7 -1
+	 * should be picked, but since upper 'X86_ALIGN_RODATA_END' can
+	 * ganrantees the alignment of 'INIT_TASK_DATA', PAGE_SIZE is picked
+	 * instead to reduce size of kernel binary
+	 */
+	SUBALIGN(PAGE_SIZE)
+#endif
+	{
 		/* Start of data section */
 		_sdata = .;
 
--

2.27.0

author
#2 lore raw

Hi Peter,

Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Feng Tang
arch/x86/Kconfig.debug +0 -0
index d3a6f74a94bd..7b8cfe9717f2 100644
@@ -225,6 +225,19 @@ config PUNIT_ATOM_DEBUG
 	  The current power state can be read from
 	  /sys/kernel/debug/punit_atom/dev_power_state
 
Quoted 4 lines

arch/x86/Kconfig.debug config PUNIT_ATOM_DEBUG · line 229

Quoted 2 lines
arch/x86/Kconfig.debug +2 -0
+config DEBUG_FORCE_DATA_SECTION_ALIGNED
+	bool "Force all data sections to be THREAD_SIZE aligned"

s/THREAD_SIZE/PAGE_SIZE/ as that seems to be what the actual patch does.

Thanks for the catch! It was changed from v1 to v2, I should reviewed these texts more carefully, and there is another similar typo in the commit log.

Quoted 21 lines
Quoted 21 lines
arch/x86/Kconfig.debug +11 -0
+	depends on EXPERT && !DYNAMIC_DEBUG
+	help
+	  There are cases that a commit from one kernel domain changes
+	  data sections' alignment of other domains, as they are all
+	  linked together compactly, and cause magic performance bump
+	  (regression or improvement), which is hard to debug. Enable
+	  this option will help to verify if the bump is caused by
+	  data alignment changes.
+
+	  It is mainly for debug and performance tuning use.
+
 choice
 	prompt "Choose kernel unwinder"
 	default UNWINDER_ORC if X86_64
arch/x86/kernel/vmlinux.lds.S +0 -0
index 7fda7f27e762..6add703de45f 100644
@@ -155,7 +155,17 @@ SECTIONS
 	X86_ALIGN_RODATA_END
 
Quoted 8 lines

arch/x86/kernel/vmlinux.lds.S SECTIONS · line 161

Quoted 6 lines
arch/x86/kernel/vmlinux.lds.S +4 -1
 	/* Data */
-	.data : AT(ADDR(.data) - LOAD_OFFSET) {
+	.data : AT(ADDR(.data) - LOAD_OFFSET)
+#ifdef CONFIG_DEBUG_FORCE_DATA_SECTION_ALIGNED
+	/*
+	 * In theroy, THREAD_SIZE as the biggest alignment of below sections

I think the more common spelling is: 'theory' :-)

Yes, will change in the next verson.

Thanks,
Feng

Quoted 13 lines
Quoted 13 lines
arch/x86/kernel/vmlinux.lds.S +7 -1
+	 * should be picked, but since upper 'X86_ALIGN_RODATA_END' can
+	 * ganrantees the alignment of 'INIT_TASK_DATA', PAGE_SIZE is picked
+	 * instead to reduce size of kernel binary
+	 */
+	SUBALIGN(PAGE_SIZE)
+#endif
+	{
 		/* Start of data section */
 		_sdata = .;
 
--

2.27.0