Skip to contentSkip to conversation

modules: few of alignment fixes

PATCH In review Luis Chamberlain 3 participants 12 messages Jan 29, 2024 – Oct 23, 2024

Subsystems (4)
lore
Share & tools
lore

Luis Chamberlain

author maintainer
#0 lore raw

v2 cover modules: few of alignment fixes

Helge Deller had written a series of patches to fix a few misalignemnt annotations in the kernel [0]. Three of these patches were tagged as being stable candidates. Because of these annotation I suggested proof of the imapact, however we did not easily have a way to verify the value / impact of fixing a few misaligment changes in the kernel for modules.

For the more hotpath alignment fix Helge had I suggested we could easily test this by stress testing find_symbol() with a few set of modules. This adds such tests to allow us to both allow us to test the impact of such possible fix, and also while at it, let's us test future improvements on the find_symbol() path.

Changes on this v2:

- Adds new selftest for kallsyms
- Drops patch #1 as Masahiro Yamada already applied it to linux-kbuild/fixes
- Removes stable tags
- Drops patch #3 as it was not needed
- Adds a new patch with the issues noted by Helge as a fix
to commit f3304ecd7f06 ("linux/export: use inline assembler to populate symbol CRCs") as noted by Masahiro Yamada
- Adds selftest impact on x86_64 for patch #2, this really should be tested on parisc though as that's an example architecture where we could see perhaps more improvement

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

Masahiro, if there no issues feel free to take this or I can take them in too via the modules-next tree. Lemme know!

Helge Deller (2):
modules: Ensure 64-bit alignment on __ksymtab_* sections
modules: Add missing entry for __ex_table

Luis Chamberlain (2):
selftests: add new kallsyms selftests vmlinux.lds.h: add missing alignment for symbol CRCs

 include/linux/export-internal.h               |   1 +
 lib/Kconfig.debug                             | 103 ++++++++++++++
 lib/Makefile                                  |   1 +
 lib/tests/Makefile                            |   1 +
 lib/tests/module/.gitignore                   |   4 +
 lib/tests/module/Makefile                     |  15 ++
 lib/tests/module/gen_test_kallsyms.sh         | 128 ++++++++++++++++++
 scripts/module.lds.S                          |   9 +-
 tools/testing/selftests/module/Makefile       |  12 ++
 tools/testing/selftests/module/config         |   3 +
 tools/testing/selftests/module/find_symbol.sh |  81 +++++++++++
 11 files changed, 354 insertions(+), 4 deletions(-)
 create mode 100644 lib/tests/Makefile
 create mode 100644 lib/tests/module/.gitignore
 create mode 100644 lib/tests/module/Makefile
 create mode 100755 lib/tests/module/gen_test_kallsyms.sh
 create mode 100644 tools/testing/selftests/module/Makefile
 create mode 100644 tools/testing/selftests/module/config
 create mode 100755 tools/testing/selftests/module/find_symbol.sh
Signature · 1 line
2.42.0
author maintainer
#1 lore raw

PATCH v2 1/4 selftests: add new kallsyms selftests

We lack find_symbol() selftests, so add one. This let's us stress test improvements easily on find_symbol() or optimizations. It also inherently allows us to test the limits of kallsyms on Linux today.

We test a pathalogical use case for kallsyms by introducing modules which are automatically written for us with a larger number of symbols. We have 4 kallsyms test modules:

A: has KALLSYSMS_NUMSYMS exported symbols
B: uses one of A's symbols
C: adds KALLSYMS_SCALE_FACTOR * KALLSYSMS_NUMSYMS exported
D: adds 2 * the symbols than C

By using anything much larger than KALLSYSMS_NUMSYMS as 10,000 and KALLSYMS_SCALE_FACTOR of 8 we segfault today. So we're capped at around 160000 symbols somehow today. We can inpsect that issue at our leasure later, but for now the real value to this test is that this will easily allow us to test improvements on find_symbol().

On x86_64 we can use perf, for other architectures we just use 'time' and allow for customizations. For example a future enhancements could be done for parisc to check for unaligned accesses which triggers a special special exception handler assembler code inside the kernel. The negative impact on performance is so large on parisc that it keeps track of its accesses on /proc/cpuinfo as UAH:

IRQ: CPU0 CPU1

3:       1332          0         SuperIO  ttyS0
7:    1270013          0         SuperIO  pata_ns87415
64:  320023012  320021431             CPU  timer
65:   17080507   20624423             CPU  IPI

UAH: 10948640 58104 Unaligned access handler traps

While at it, this tidies up lib/ test modules to allow us to have a new directory for them. The amount of test modules under lib/ is insane.

This should also hopefully showcase how to start doing basic self module writing code, which may be more useful for more complex cases later in the future.

---
 lib/Kconfig.debug                             | 103 ++++++++++++++
 lib/Makefile                                  |   1 +
 lib/tests/Makefile                            |   1 +
 lib/tests/module/.gitignore                   |   4 +
 lib/tests/module/Makefile                     |  15 ++
 lib/tests/module/gen_test_kallsyms.sh         | 128 ++++++++++++++++++
 tools/testing/selftests/module/Makefile       |  12 ++
 tools/testing/selftests/module/config         |   3 +
 tools/testing/selftests/module/find_symbol.sh |  81 +++++++++++
 9 files changed, 348 insertions(+)
 create mode 100644 lib/tests/Makefile
 create mode 100644 lib/tests/module/.gitignore
 create mode 100644 lib/tests/module/Makefile
 create mode 100755 lib/tests/module/gen_test_kallsyms.sh
 create mode 100644 tools/testing/selftests/module/Makefile
 create mode 100644 tools/testing/selftests/module/config
 create mode 100755 tools/testing/selftests/module/find_symbol.sh
lib/Kconfig.debug +103 -0
index 97ce28f4d154..29db47ca251f 100644
@@ -2835,6 +2835,109 @@ config TEST_KMOD
 
 	  If unsure, say N.
 
+config TEST_RUNTIME
+	bool
+
+config TEST_RUNTIME_MODULE
+	bool
+
+config TEST_KALLSYMS
+	tristate "module kallsyms find_symbol() test"
+	depends on m
+	select TEST_RUNTIME
+	select TEST_RUNTIME_MODULE
+	select TEST_KALLSYMS_A
+	select TEST_KALLSYMS_B
+	select TEST_KALLSYMS_C
+	select TEST_KALLSYMS_D
+	help
+	  This allows us to stress test find_symbol() through the kallsyms
+	  used to place symbols on the kernel ELF kallsyms and modules kallsyms
+	  where we place kernel symbols such as exported symbols.
+
+	  We have four test modules:
+
+	  A: has KALLSYSMS_NUMSYMS exported symbols
+	  B: uses one of A's symbols
+	  C: adds KALLSYMS_SCALE_FACTOR * KALLSYSMS_NUMSYMS exported
+	  D: adds 2 * the symbols than C
+
+	  We stress test find_symbol() through two means:
+
+	  1) Upon load of B it will trigger simplify_symbols() to look for the
+	  one symbol it uses from the module A with tons of symbols. This is an
+	  indirect way for us to have B call resolve_symbol_wait() upon module
+	  load. This will eventually call find_symbol() which will eventually
+	  try to find the symbols used with find_exported_symbol_in_section().
+	  find_exported_symbol_in_section() uses bsearch() so a binary search
+	  for each symbol. Binary search will at worst be O(log(n)) so the
+	  larger TEST_MODULE_KALLSYSMS the worse the search.
+
+	  2) The selftests should load C first, before B. Upon B's load towards
+	  the end right before we call module B's init routine we get
+	  complete_formation() called on the module. That will first check
+	  for duplicate symbols with the call to verify_exported_symbols().
+	  That is when we'll force iteration on module C's insane symbol list.
+	  Since it has 10 * KALLSYMS_NUMSYMS it means we can first test
+	  just loading B without C. The amount of time it takes to load C Vs
+	  B can give us an idea of the impact growth of the symbol space and
+	  give us projection. Module A only uses one symbol from B so to allow
+	  this scaling in module C to be proportional, if it used more symbols
+	  then the first test would be doing more and increasing just the
+	  search space would be slightly different. The last module, module D
+	  will just increase the search space by twice the number of symbols in
+	  C so to allow for full projects.
+
+	  tools/testing/selftests/module/find_symbol.sh
+
+	  The current defaults will incur a build delay of about 7 minutes
+	  on an x86_64 with only 8 cores. Enable this only if you want to
+	  stress test find_symbol() with thousands of symbols. At the same
+	  time this is also useful to test building modules with thousands of
+	  symbols, and if BTF is enabled this also stress tests adding BTF
+	  information for each module. Currently enabling many more symbols
+	  will segfault the build system.
+
+	  If unsure, say N.
+
+if TEST_KALLSYMS
+
+config TEST_KALLSYMS_A
+	tristate
+	depends on m
+
+config TEST_KALLSYMS_B
+	tristate
+	depends on m
+
+config TEST_KALLSYMS_C
+	tristate
+	depends on m
+
+config TEST_KALLSYMS_D
+	tristate
+	depends on m
+
+config TEST_KALLSYMS_NUMSYMS
+	int "test kallsyms number of symbols"
+	default 10000
+	help
+	  The number of symbols to create on TEST_KALLSYMS_A, only one of which
+	  module TEST_KALLSYMS_B will use. This also will be used
+	  for how many symbols TEST_KALLSYMS_C will have, scaled up by
+	  TEST_KALLSYMS_SCALE_FACTOR.
+
+config TEST_KALLSYMS_SCALE_FACTOR
+	int "test kallsyms scale factor"
+	default 8
+	help
+	  How many more unusued symbols will TEST_KALLSYSMS_C have than
+	  TEST_KALLSYMS_A. If 8, then module C will have 8 * syms
+	  than module A. Then TEST_KALLSYMS_D will have double the amount
+	  of symbols than C so to allow projections.
+
+endif # TEST_KALLSYMS
+
 config TEST_DEBUG_VIRTUAL
 	tristate "Test CONFIG_DEBUG_VIRTUAL feature"
 	depends on DEBUG_VIRTUAL
lib/Makefile +1 -0
index 6b09731d8e61..48e69a61cd85 100644
@@ -95,6 +95,7 @@ obj-$(CONFIG_TEST_XARRAY) += test_xarray.o
 obj-$(CONFIG_TEST_MAPLE_TREE) += test_maple_tree.o
 obj-$(CONFIG_TEST_PARMAN) += test_parman.o
 obj-$(CONFIG_TEST_KMOD) += test_kmod.o
+obj-$(CONFIG_TEST_RUNTIME) += tests/
 obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
 obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o
 obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o
lib/tests/Makefile new file +1 -0
new file mode 100644
index 000000000000..8e4f42cb9c54
@@ -0,0 +1 @@
+obj-$(CONFIG_TEST_RUNTIME_MODULE)		+= module/
lib/tests/module/.gitignore new file +4 -0
new file mode 100644
index 000000000000..8be7891b250f
@@ -0,0 +1,4 @@
+test_kallsyms_a.c
+test_kallsyms_b.c
+test_kallsyms_c.c
+test_kallsyms_d.c
lib/tests/module/Makefile new file +15 -0
new file mode 100644
index 000000000000..af5c27b996cb
@@ -0,0 +1,15 @@
+obj-$(CONFIG_TEST_KALLSYMS_A) += test_kallsyms_a.o
+obj-$(CONFIG_TEST_KALLSYMS_B) += test_kallsyms_b.o
+obj-$(CONFIG_TEST_KALLSYMS_C) += test_kallsyms_c.o
+obj-$(CONFIG_TEST_KALLSYMS_D) += test_kallsyms_d.o
+
+$(obj)/%.c: FORCE
+	@$(kecho) "  GEN     $@"
+	$(Q)$(srctree)/lib/tests/module/gen_test_kallsyms.sh $@\
+		$(CONFIG_TEST_KALLSYMS_NUMSYMS) \
+		$(CONFIG_TEST_KALLSYMS_SCALE_FACTOR)
+
+clean-files += test_kallsyms_a.c
+clean-files += test_kallsyms_b.c
+clean-files += test_kallsyms_c.c
+clean-files += test_kallsyms_d.c
lib/tests/module/gen_test_kallsyms.sh new file +128 -0

128 lines not on this page · raw message

tools/testing/selftests/module/Makefile new file +12 -0
new file mode 100644
index 000000000000..6132d7ddb08b
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Makefile for module loading selftests
+
+# No binaries, but make sure arg-less "make" doesn't trigger "run_tests"
+all:
+
+TEST_PROGS := find_symbol.sh
+
+include ../lib.mk
+
+# Nothing to clean up.
+clean:
tools/testing/selftests/module/config new file +3 -0
new file mode 100644
index 000000000000..b0c206b1ad47
@@ -0,0 +1,3 @@
+CONFIG_TEST_RUNTIME=y
+CONFIG_TEST_RUNTIME_MODULE=y
+CONFIG_TEST_KALLSYMS=m
tools/testing/selftests/module/find_symbol.sh new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.42.0
author maintainer
#2 lore raw

PATCH v2 2/4 modules: Ensure 64-bit alignment on __ksymtab_* sections

In reply to Luis Chamberlain

From: Helge Deller <[email protected]>

On 64-bit architectures without CONFIG_HAVE_ARCH_PREL32_RELOCATIONS (e.g. ppc64, ppc64le, parisc, s390x,...) the __KSYM_REF() macro stores 64-bit pointers into the __ksymtab* sections.
Make sure that those sections will be correctly aligned at module link time, otherwise unaligned memory accesses may happen at runtime.

The __kcrctab* sections store 32-bit entities, so use ALIGN(4) for those.

Testing with the kallsyms selftest on x86_64 we see a savings of about 1,958,153 ns in the worst case which may or not be within noise. Testing on parisc would be useful and welcomed.

On x86_64 before:

Performance counter stats for '/sbin/modprobe test_kallsyms_b':

        86,430,119 ns   duration_time
        84,407,000 ns   system_time
               213      page-faults
       0.086430119 seconds time elapsed

       0.000000000 seconds user
       0.084407000 seconds sys

Performance counter stats for '/sbin/modprobe test_kallsyms_b':

        85,777,474 ns   duration_time
        82,581,000 ns   system_time
               212      page-faults
       0.085777474 seconds time elapsed

       0.000000000 seconds user
       0.082581000 seconds sys

Performance counter stats for '/sbin/modprobe test_kallsyms_b':

        87,906,053 ns   duration_time
        87,939,000 ns   system_time
               212      page-faults
       0.087906053 seconds time elapsed

       0.000000000 seconds user
       0.087939000 seconds sys

After:

Performance counter stats for '/sbin/modprobe test_kallsyms_b':

        82,925,631 ns   duration_time
        83,000,000 ns   system_time
               212      page-faults
       0.082925631 seconds time elapsed

       0.000000000 seconds user
       0.083000000 seconds sys

Performance counter stats for '/sbin/modprobe test_kallsyms_b':

        87,776,380 ns   duration_time
        86,678,000 ns   system_time
               213      page-faults
       0.087776380 seconds time elapsed

       0.000000000 seconds user
       0.086678000 seconds sys

Performance counter stats for '/sbin/modprobe test_kallsyms_b':

        85,947,900 ns   duration_time
        82,006,000 ns   system_time
               212      page-faults
       0.085947900 seconds time elapsed

       0.000000000 seconds user
       0.082006000 seconds sys
[mcgrof: ran kallsyms selftests on x86_64]
---
 scripts/module.lds.S | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
scripts/module.lds.S +4 -4
index bf5bcf2836d8..b00415a9ff27 100644
@@ -15,10 +15,10 @@ SECTIONS {
 		*(.discard.*)
 	}
 
-	__ksymtab		0 : { *(SORT(___ksymtab+*)) }
-	__ksymtab_gpl		0 : { *(SORT(___ksymtab_gpl+*)) }
-	__kcrctab		0 : { *(SORT(___kcrctab+*)) }
-	__kcrctab_gpl		0 : { *(SORT(___kcrctab_gpl+*)) }
+	__ksymtab		0 : ALIGN(8) { *(SORT(___ksymtab+*)) }
+	__ksymtab_gpl		0 : ALIGN(8) { *(SORT(___ksymtab_gpl+*)) }
+	__kcrctab		0 : ALIGN(4) { *(SORT(___kcrctab+*)) }
+	__kcrctab_gpl		0 : ALIGN(4) { *(SORT(___kcrctab_gpl+*)) }
 
 	.ctors			0 : ALIGN(8) { *(SORT(.ctors.*)) *(.ctors) }
 	.init_array		0 : ALIGN(8) { *(SORT(.init_array.*)) *(.init_array) }
Signature · 1 line
2.42.0
author maintainer
#3 lore raw

PATCH v2 3/4 vmlinux.lds.h: add missing alignment for symbol CRCs

In reply to Luis Chamberlain

Commit f3304ecd7f06 (linux/export: use inline assembler to populate symbol CRCs") fixed an issue with unexpected padding by adding asm inline assembly to directly specify the desired data layout. It however forgot to add the alignment, fix that.

Fixes f3304ecd7f06 ("linux/export: use inline assembler to populate symbol CRCs")
---
 include/linux/export-internal.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/export-internal.h +1 -0
index 69501e0ec239..51b8cf3f60ef 100644
@@ -61,6 +61,7 @@
 
 #define SYMBOL_CRC(sym, crc, sec)   \
 	asm(".section \"___kcrctab" sec "+" #sym "\",\"a\""	"\n" \
+	    ".balign 4"						"\n" \
 	    "__crc_" #sym ":"					"\n" \
 	    ".long " #crc					"\n" \
 	    ".previous"						"\n")
Signature · 1 line
2.42.0
author maintainer
#4 lore raw

PATCH v2 4/4 modules: Add missing entry for __ex_table

In reply to Luis Chamberlain

From: Helge Deller <[email protected]>

The entry for __ex_table was missing, which may make __ex_table become 1- or 2-byte aligned in modules.
Add the entry to ensure it gets 32-bit aligned.

---
 scripts/module.lds.S | 1 +
 1 file changed, 1 insertion(+)
scripts/module.lds.S +1 -0
index b00415a9ff27..488f61b156b2 100644
@@ -26,6 +26,7 @@ SECTIONS {
 	.altinstructions	0 : ALIGN(8) { KEEP(*(.altinstructions)) }
 	__bug_table		0 : ALIGN(8) { KEEP(*(__bug_table)) }
 	__jump_table		0 : ALIGN(8) { KEEP(*(__jump_table)) }
+	__ex_table		0 : ALIGN(4) { KEEP(*(__ex_table)) }
 
 	__patchable_function_entries : { *(__patchable_function_entries) }
 
Signature · 1 line
2.42.0
author maintainer
#6 lore raw
Quoted 5 lines · Luis Chamberlain
Quoted 2 lines · Luis Chamberlain

Masahiro, if there no issues feel free to take this or I can take them in too via the modules-next tree. Lemme know!

I've queued this onto modules-testing to get winder testing [0]

I've moved it to modules-next as I've found no issues.

Luis

#7 lore raw
Quoted 11 lines · Luis Chamberlain
Quoted 5 lines · Luis Chamberlain
Quoted 2 lines · Luis Chamberlain

Masahiro, if there no issues feel free to take this or I can take them in too via the modules-next tree. Lemme know!

I've queued this onto modules-testing to get winder testing [0]

I've moved it to modules-next as I've found no issues.

Luis

I believe this patch series is wrong.

I thought we agreed that the alignment must be added to individual asm code, not to the linker script.

I am surprised that you came back to this.

--
Best Regards
Masahiro Yamada

author maintainer
#8 lore raw
Quoted 14 lines · Masahiro Yamada
Quoted 11 lines · Luis Chamberlain
Quoted 5 lines · Luis Chamberlain
Quoted 2 lines · Luis Chamberlain

Masahiro, if there no issues feel free to take this or I can take them in too via the modules-next tree. Lemme know!

I've queued this onto modules-testing to get winder testing [0]

I've moved it to modules-next as I've found no issues.

Luis

Quoted 6 lines

I believe this patch series is wrong.

I thought we agreed that the alignment must be added to individual asm code, not to the linker script.

I am surprised that you came back to this.

I misseed the dialog on the old cover letter, sorry. I've yanked these patches out. I'd expect a respin from Helge.

Luis

author maintainer
#9 lore raw
Quoted 17 lines · Luis Chamberlain
Quoted 16 lines · Masahiro Yamada
Quoted 11 lines · Luis Chamberlain
Quoted 5 lines · Luis Chamberlain
Quoted 2 lines · Luis Chamberlain

Masahiro, if there no issues feel free to take this or I can take them in too via the modules-next tree. Lemme know!

I've queued this onto modules-testing to get winder testing [0]

I've moved it to modules-next as I've found no issues.

Luis

I believe this patch series is wrong.

Quoted 7 lines
Quoted 4 lines

I thought we agreed that the alignment must be added to individual asm code, not to the linker script.

I am surprised that you came back to this.

I misseed the dialog on the old cover letter, sorry. I've yanked these patches out. I'd expect a respin from Helge.

Just goind down memory lane -- Helge, the work here pending was to move this to the linker script. Were you going to follow up on this?

I'll split out my test patch as that's useful for other reasons now.

Luis

#10 lore raw
Quoted 21 lines · Luis Chamberlain
Quoted 20 lines · Luis Chamberlain
Quoted 19 lines · Masahiro Yamada
Quoted 11 lines · Luis Chamberlain
Quoted 5 lines · Luis Chamberlain
Quoted 2 lines · Luis Chamberlain

Masahiro, if there no issues feel free to take this or I can take them in too via the modules-next tree. Lemme know!

I've queued this onto modules-testing to get winder testing [0]

I've moved it to modules-next as I've found no issues.

Luis

I believe this patch series is wrong.

I thought we agreed that the alignment must be added to individual asm code, not to the linker script.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I am surprised that you came back to this.

I misseed the dialog on the old cover letter, sorry. I've yanked these patches out. I'd expect a respin from Helge.

Just goind down memory lane -- Helge, the work here pending was to move this to the linker script. Were you going to follow up on this?

Masahiro mentions above, that the alignment should be added to the individual asm code. This happened in the meantime for parisc, but I'm not sure if all platforms get this right.
So in addition, I still believe that adding the alignment to the linker script too is another right thing to do.

Helge

#11 lore raw
Quoted 31 lines · Helge Deller
Quoted 21 lines · Luis Chamberlain
Quoted 20 lines · Luis Chamberlain
Quoted 19 lines · Masahiro Yamada
Quoted 11 lines · Luis Chamberlain
Quoted 5 lines · Luis Chamberlain
Quoted 2 lines · Luis Chamberlain

Masahiro, if there no issues feel free to take this or I can take them in too via the modules-next tree. Lemme know!

I've queued this onto modules-testing to get winder testing [0]

I've moved it to modules-next as I've found no issues.

Luis

I believe this patch series is wrong.

I thought we agreed that the alignment must be added to individual asm code, not to the linker script.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I am surprised that you came back to this.

I misseed the dialog on the old cover letter, sorry. I've yanked these patches out. I'd expect a respin from Helge.

Just goind down memory lane -- Helge, the work here pending was to move this to the linker script. Were you going to follow up on this?

Quoted 7 lines

Masahiro mentions above, that the alignment should be added to the individual asm code. This happened in the meantime for parisc, but I'm not sure if all platforms get this right.
So in addition, I still believe that adding the alignment to the linker script too is another right thing to do.

Helge

Yes, I believe the proper alignment should be specified in asm code.

Signature · 2 lines
Best Regards
Masahiro Yamada