[PATCH] riscv: ftrace: sorttable: fix mcount sorting

Martin Kaiser posted 1 patch 2 weeks, 2 days ago
scripts/sorttable.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
[PATCH] riscv: ftrace: sorttable: fix mcount sorting
Posted by Martin Kaiser 2 weeks, 2 days ago
Commit de04fca5d2f8 ("riscv: ftrace: select HAVE_BUILDTIME_MCOUNT_SORT")
enabled mcount sorting for risc-v. On my system, the sorting changes all
entries to address 0. The startup test reports that the mcount section is
properly sorted, but no traceable functions are registered at boot time.

[    0.000000] ftrace section at ffffffff8101bba0 sorted properly
[    0.000000] ftrace: allocating 0 entries in 128 pages
[    0.000000] ftrace: allocated 128 pages with 1 groups

The sorttable helper program does not take into account that the
-fpatchable-function-entry compiler flag reserves an additional 8 bytes
before each traceable function. (The size in bytes is the same for
RV32/64 or compressed/uncompressed instructions).

Define before_func = 8 to fix the mcount sorting for risc-v, share this
setting with arm64.

Fixes: de04fca5d2f8 ("riscv: ftrace: select HAVE_BUILDTIME_MCOUNT_SORT")
Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
 scripts/sorttable.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/scripts/sorttable.c b/scripts/sorttable.c
index e8ed11c680c6..da70f0938d04 100644
--- a/scripts/sorttable.c
+++ b/scripts/sorttable.c
@@ -895,13 +895,19 @@ static int do_file(char const *const fname, void *addr)
 #ifdef MCOUNT_SORT_ENABLED
 		sort_reloc = true;
 		rela_type = 0x403;
-		/* arm64 uses patchable function entry placing before function */
+#endif
+		/* fallthrough */
+	case EM_RISCV:
+#ifdef MCOUNT_SORT_ENABLED
+		/*
+		 * risc-v and arm64 use patchable function entry that stores
+		 * 8 bytes of nops before the function
+		 */
 		before_func = 8;
 #endif
 		/* fallthrough */
 	case EM_386:
 	case EM_LOONGARCH:
-	case EM_RISCV:
 	case EM_S390:
 	case EM_X86_64:
 		custom_sort = sort_relative_table_with_data;
-- 
2.43.7
Re: [PATCH] riscv: ftrace: sorttable: fix mcount sorting
Posted by Chen Pei 2 weeks, 1 day ago
On Mon,  8 Jun 2026 22:18:27 +0200, martin@kaiser.cx wrote:

> The sorttable helper program does not take into account that the
> -fpatchable-function-entry compiler flag reserves an additional 8 bytes
> before each traceable function. (The size in bytes is the same for
> RV32/64 or compressed/uncompressed instructions).
> 
> Define before_func = 8 to fix the mcount sorting for risc-v, share this
> setting with arm64.
> 
> Fixes: de04fca5d2f8 ("riscv: ftrace: select HAVE_BUILDTIME_MCOUNT_SORT")

Hi Martin,

Thanks for the patch.

This has already been addressed by Wang Han here:
https://lore.kernel.org/linux-riscv/20260527123530.2593918-2-wanghan@linux.alibaba.com/

Thanks,
Pei