Skip to contentSkip to conversation

arm64: scripts/sorttable: Implement sorting mcount_loc at boot for arm64

PATCH In review Steven Rostedt 3 participants 6 messages Feb 10, 2025 – Feb 13, 2025

Subsystems (3)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

PATCH 1/1 arm64: scripts/sorttable: Implement sorting mcount_loc at boot for arm64

From: Steven Rostedt <[email protected]>

The mcount_loc section holds the addresses of the functions that get patched by ftrace when enabling function callbacks. It can contain tens of thousands of entries. These addresses must be sorted. If they are not sorted at compile time, they are sorted at boot. Sorting at boot does take some time and does have a small impact on boot performance.

x86 and arm32 have the addresses in the mcount_loc section of the ELF file. But for arm64, the section just contains zeros. The .rela.dyn Elf_Rela section holds the addresses and they get patched at boot during the relocation phase.

In order to sort these addresses, the Elf_Rela needs to be updated instead of the location in the binary that holds the mcount_loc section. Have the sorttable code, allocate an array to hold the functions, load the addresses from the Elf_Rela entries, sort them, then put them back in order into the Elf_rela entries so that they will be sorted at boot up without having to sort them during boot up.

---

Can I get a "Tested-by" and a Reviewed-by or Acked-by from one of the ARM64 maintainers? I have some code that I want to add on top of this that will remove the weak functions from the mcount_loc section.

For the s390 folks. I cross compiled a s390 and looked at the mcount_loc section, and I have no idea how to implement this for that. I wrote a elf parser to dump sections based symbols:

https://rostedt.org/code/dump_elf_sym.c

And ran it on the s390 vmlinux:

 $ ./dump_elf_sym vmlinux __start_mcount_loc __stop_mcount_loc
 1801620: .. .. .. .. .. .. .. ..   00 00 00 00 00 11 70 20  ......... .....p 
 1801630: 00 00 00 00 00 11 70 90   00 00 00 00 00 11 70 a0  ......p.. .....p.
 1801640: 00 00 00 00 00 11 71 10   00 00 00 00 00 11 71 20  ......q.. .....q 
 1801650: 00 00 00 00 00 11 71 90   00 00 00 00 01 7c 70 00  ......q.. ....|p.
 1801660: 00 00 00 00 01 7c 70 20   00 00 00 00 01 7c 70 40  .....|p . ....|p@
 1801670: 00 00 00 00 01 7c 70 60   00 00 00 00 01 7c 70 70  .....|p`. ....|pp
 1801680: 00 00 00 00 01 7c 70 98   00 00 00 00 01 7c 70 c0  .....|p.. ....|p.
 1801690: 00 00 00 00 01 7c 70 d0   00 00 00 00 01 7c 71 68  .....|p.. ....|qh
[..]

It looks like addresses in that section where ARM64 only has zeros. I couldn't find a .rel or .rela section either.

I guess there's some other table that these addresses are dependent upon?

 arch/arm64/Kconfig  |   1 +
 scripts/sorttable.c | 181 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 179 insertions(+), 3 deletions(-)
arch/arm64/Kconfig +1 -0
index fcdd0ed3eca8..3c6c9dcd96aa 100644
@@ -217,6 +217,7 @@ config ARM64
 		if DYNAMIC_FTRACE_WITH_ARGS
 	select HAVE_SAMPLE_FTRACE_DIRECT
 	select HAVE_SAMPLE_FTRACE_DIRECT_MULTI
+	select HAVE_BUILDTIME_MCOUNT_SORT
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS
 	select HAVE_GUP_FAST
 	select HAVE_FTRACE_GRAPH_FUNC
scripts/sorttable.c +178 -3

275 lines not on this page · raw message

Signature · 1 line
2.47.2
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Steven,

kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on v6.14-rc2 next-20250210]
[cannot apply to arm64/for-next/core arm-perf/for-next/perf kvmarm/next soc/for-next arm/for-next arm/fixes] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Steven-Rostedt/arm64-scripts-sorttable-Implement-sorting-mcount_loc-at-boot-for-arm64/20250211-032956
base:   linus/master
patch link:    https://lore.kernel.org/r/20250210142647.083ff456%40gandalf.local.home
patch subject: [PATCH] arm64: scripts/sorttable: Implement sorting mcount_loc at boot for arm64
config: riscv-randconfig-001-20250211 (https://download.01.org/0day-ci/archive/20250211/[email protected]/config)
compiler: riscv64-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250211/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> scripts/sorttable.c:306:13: warning: 'rela_write_addend' defined but not used [-Wunused-function]
     306 | static void rela_write_addend(Elf_Rela *rela, uint64_t val)
         |             ^~~~~~~~~~~~~~~~~
>> scripts/sorttable.c:287:17: warning: 'rela_addend' defined but not used [-Wunused-function]
     287 | static uint64_t rela_##fn_name(Elf_Rela *rela)          \
         |                 ^~~~~
   scripts/sorttable.c:294:1: note: in expansion of macro 'RELA_ADDR'
     294 | RELA_ADDR(addend)
         | ^~~~~~~~~
>> scripts/sorttable.c:287:17: warning: 'rela_info' defined but not used [-Wunused-function]
     287 | static uint64_t rela_##fn_name(Elf_Rela *rela)          \
         |                 ^~~~~
   scripts/sorttable.c:293:1: note: in expansion of macro 'RELA_ADDR'
     293 | RELA_ADDR(info)
         | ^~~~~~~~~
>> scripts/sorttable.c:287:17: warning: 'rela_offset' defined but not used [-Wunused-function]
     287 | static uint64_t rela_##fn_name(Elf_Rela *rela)          \
         |                 ^~~~~
   scripts/sorttable.c:292:1: note: in expansion of macro 'RELA_ADDR'
     292 | RELA_ADDR(offset)
         | ^~~~~~~~~
   In file included from arch/riscv/kernel/asm-offsets.c:10:
   include/linux/ftrace.h: In function 'ftrace_get_regs':
   include/linux/ftrace.h:190:16: error: implicit declaration of function 'arch_ftrace_get_regs'; did you mean 'arch_ftrace_regs'? [-Wimplicit-function-declaration]
     190 |         return arch_ftrace_get_regs(fregs);
         |                ^~~~~~~~~~~~~~~~~~~~
         |                arch_ftrace_regs
   include/linux/ftrace.h:190:16: error: returning 'int' from a function with return type 'struct pt_regs *' makes pointer from integer without a cast [-Wint-conversion]
     190 |         return arch_ftrace_get_regs(fregs);
         |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~

make[3]: *** [scripts/Makefile.build:102: arch/riscv/kernel/asm-offsets.s] Error 1 shuffle=1925395641 make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1264: prepare0] Error 2 shuffle=1925395641
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:251: __sub-make] Error 2 shuffle=1925395641
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:251: __sub-make] Error 2 shuffle=1925395641
make: Target 'prepare' not remade because of errors.

vim +/rela_write_addend +306 scripts/sorttable.c

275
276 #define RELA_ADDR(fn_name) \
277 static uint64_t rela64_##fn_name(Elf_Rela *rela) \
278 { \
279 return r8((uint64_t *)&rela->e64.r_##fn_name); \
280 } \
281 \
282 static uint64_t rela32_##fn_name(Elf_Rela *rela) \
283 { \
284 return r((uint32_t *)&rela->e32.r_##fn_name); \
285 } \
286 \

 > 287	static uint64_t rela_##fn_name(Elf_Rela *rela)		\
   288	{							\
   289		return e.rela_##fn_name(rela);			\
   290	}
   291	
   292	RELA_ADDR(offset)
   293	RELA_ADDR(info)
   294	RELA_ADDR(addend)
   295	
   296	static void rela64_write_addend(Elf_Rela *rela, uint64_t val)
   297	{
   298		w8(val, (uint64_t *)&rela->e64.r_addend);
   299	}
   300	
   301	static void rela32_write_addend(Elf_Rela *rela, uint64_t val)
   302	{
   303		w(val, (uint32_t *)&rela->e32.r_addend);
   304	}
   305	
 > 306	static void rela_write_addend(Elf_Rela *rela, uint64_t val)

307 {
308 e.rela_write_addend(rela, val);
309 }
310

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#2 lore raw
Quoted 11 lines · On Tue, 11 Feb 2025 17:07:13 +0800 kernel test robot <[email protected]> wrote:

All warnings (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

scripts/sorttable.c:306:13: warning: 'rela_write_addend' defined but not used [-Wunused-function]

306 | static void rela_write_addend(Elf_Rela *rela, uint64_t val)
| ^~~~~~~~~~~~~~~~~

Quoted 1 line
Quoted 1 line

scripts/sorttable.c:287:17: warning: 'rela_addend' defined but not used [-Wunused-function]

     287 | static uint64_t rela_##fn_name(Elf_Rela *rela)          \
         |                 ^~~~~
   scripts/sorttable.c:294:1: note: in expansion of macro 'RELA_ADDR'
     294 | RELA_ADDR(addend)
         | ^~~~~~~~~

The above isn't used if sorting isn't enabled.

Looks like I'll have to add some "__maybe_unused" attributes.

-- Steve

maintainer
#3 lore raw
In reply to Steven Rostedt
Quoted 19 lines · Steven Rostedt

For the s390 folks. I cross compiled a s390 and looked at the mcount_loc section, and I have no idea how to implement this for that. I wrote a elf parser to dump sections based symbols:

https://rostedt.org/code/dump_elf_sym.c

And ran it on the s390 vmlinux:

 $ ./dump_elf_sym vmlinux __start_mcount_loc __stop_mcount_loc
 1801620: .. .. .. .. .. .. .. ..   00 00 00 00 00 11 70 20  ......... .....p 
 1801630: 00 00 00 00 00 11 70 90   00 00 00 00 00 11 70 a0  ......p.. .....p.
 1801640: 00 00 00 00 00 11 71 10   00 00 00 00 00 11 71 20  ......q.. .....q 
 1801650: 00 00 00 00 00 11 71 90   00 00 00 00 01 7c 70 00  ......q.. ....|p.
 1801660: 00 00 00 00 01 7c 70 20   00 00 00 00 01 7c 70 40  .....|p . ....|p@
 1801670: 00 00 00 00 01 7c 70 60   00 00 00 00 01 7c 70 70  .....|p`. ....|pp
 1801680: 00 00 00 00 01 7c 70 98   00 00 00 00 01 7c 70 c0  .....|p.. ....|p.
 1801690: 00 00 00 00 01 7c 70 d0   00 00 00 00 01 7c 71 68  .....|p.. ....|qh
[..]
Quoted 1 line

It looks like addresses in that section...

Those are the addresses of the mcount locations. After looking at sorttable.c it really looks like that for s390 we can simply select HAVE_BUILDTIME_MCOUNT_SORT without any further changes.

I just tested it with different compiler options (fentry vs hotpatch), including selecting FTRACE_SORT_STARTUP_TEST, and as expected everything works.

I'm going to give it some more testing in our CI - but if nothing breaks a patch which selects HAVE_BUILDTIME_MCOUNT_SORT for s390 will go upstream with the next merge window.

maintainer
#4 lore raw
Quoted 26 lines · Heiko Carstens
Quoted 19 lines · Steven Rostedt

For the s390 folks. I cross compiled a s390 and looked at the mcount_loc section, and I have no idea how to implement this for that. I wrote a elf parser to dump sections based symbols:

https://rostedt.org/code/dump_elf_sym.c

And ran it on the s390 vmlinux:

 $ ./dump_elf_sym vmlinux __start_mcount_loc __stop_mcount_loc
 1801620: .. .. .. .. .. .. .. ..   00 00 00 00 00 11 70 20  ......... .....p 
 1801630: 00 00 00 00 00 11 70 90   00 00 00 00 00 11 70 a0  ......p.. .....p.
 1801640: 00 00 00 00 00 11 71 10   00 00 00 00 00 11 71 20  ......q.. .....q 
 1801650: 00 00 00 00 00 11 71 90   00 00 00 00 01 7c 70 00  ......q.. ....|p.
 1801660: 00 00 00 00 01 7c 70 20   00 00 00 00 01 7c 70 40  .....|p . ....|p@
 1801670: 00 00 00 00 01 7c 70 60   00 00 00 00 01 7c 70 70  .....|p`. ....|pp
 1801680: 00 00 00 00 01 7c 70 98   00 00 00 00 01 7c 70 c0  .....|p.. ....|p.
 1801690: 00 00 00 00 01 7c 70 d0   00 00 00 00 01 7c 71 68  .....|p.. ....|qh
[..]
Quoted 1 line

It looks like addresses in that section...

Those are the addresses of the mcount locations. After looking at sorttable.c it really looks like that for s390 we can simply select HAVE_BUILDTIME_MCOUNT_SORT without any further changes.

Quoted 7 lines

I just tested it with different compiler options (fentry vs hotpatch), including selecting FTRACE_SORT_STARTUP_TEST, and as expected everything works.

I'm going to give it some more testing in our CI - but if nothing breaks a patch which selects HAVE_BUILDTIME_MCOUNT_SORT for s390 will go upstream with the next merge window.

Something like this:

From 0759d6b961946b7e5cfb19971d56f5493204301c Mon Sep 17 00:00:00 2001
From: Heiko Carstens <[email protected]>
Date: Thu, 13 Feb 2025 13:57:33 +0100
Subject: [PATCH] s390: Sort mcount locations at build time

For s390 the mcount_loc section of the kernel image contains the addresses of the mcount locations. All addresses will be adjusted with the same offset by the decompressor before the kernel is started.

Therefore select HAVE_BUILDTIME_MCOUNT_SORT so that the entries of this section are sorted at build time. Given that the same offset is applied to all entries the section will be sorted in any case.

Note that this was not possible before commit 778666df60f0 ("s390: compile relocatable kernel without -fPIE"). Since this commit all R_390_64 absolute relocations are handled in a special way: only the address of the to be changed location is put into a special section. For all those locations the same offset is applied as described above.

Without that change it would have been necessary to also adjust the addend of all relocations which correspond to the mcount_loc section, when sorting the mcount_loc section.

---
 arch/s390/Kconfig                 | 1 +
 arch/s390/configs/debug_defconfig | 1 +
 2 files changed, 2 insertions(+)
arch/s390/Kconfig +1 -0
index 9c9ec08d78c7..acaa1d1c12b2 100644
@@ -185,6 +185,7 @@ config S390
 	select HAVE_ARCH_TRANSPARENT_HUGEPAGE
 	select HAVE_ARCH_VMAP_STACK
 	select HAVE_ASM_MODVERSIONS
+	select HAVE_BUILDTIME_MCOUNT_SORT
 	select HAVE_CMPXCHG_DOUBLE
 	select HAVE_CMPXCHG_LOCAL
 	select HAVE_DEBUG_KMEMLEAK
arch/s390/configs/debug_defconfig +1 -0
index d6beec5292a0..a2b0444b7d6b 100644
@@ -887,6 +887,7 @@ CONFIG_USER_EVENTS=y
 CONFIG_HIST_TRIGGERS=y
 CONFIG_FTRACE_STARTUP_TEST=y
 # CONFIG_EVENT_TRACE_STARTUP_TEST is not set
+CONFIG_FTRACE_SORT_STARTUP_TEST=y
 CONFIG_SAMPLES=y
 CONFIG_SAMPLE_TRACE_PRINTK=m
 CONFIG_SAMPLE_FTRACE_DIRECT=m
Signature · 1 line
2.45.2
author maintainer
#5 lore raw
Quoted 1 line · On Thu, 13 Feb 2025 16:09:34 +0100 Heiko Carstens <[email protected]> wrote:

Something like this:

Thanks,

I'm about to send a bunch of patches to this code that removes weak functions. Since s390 is big-endian, it would be good for you to test them too. I'll Cc you on them. I think I handled the endianess correctly. Although I found a bug where if you compile le arm64 on s390 it will break because it assumed that the endianess of the build machine was the same as the target.

I'll be sending them shortly. Just doing some more smoke tests.

-- Steve