powerpc/ftrace: Fix ftrace bug with KASAN=y
authorMichael Ellerman <mpe@ellerman.id.au>
Thu, 7 Nov 2024 11:16:30 +0000 (22:16 +1100)
committerMichael Ellerman <mpe@ellerman.id.au>
Sun, 10 Nov 2024 11:33:51 +0000 (22:33 +1100)
commitcfec8463d9a19ec043845525fe5fd675e59a8aab
tree4300b107a44da24fc602109982e9711eb8c56104
parentf3ef7dbda9b589cdad833001e4366eb80977b7f1
powerpc/ftrace: Fix ftrace bug with KASAN=y

Booting a KASAN=y kernel with the recently added ftrace out-of-line
support causes a warning at boot:

  ------------[ cut here ]------------
  Stub index overflow (1729 > 1728)
  WARNING: CPU: 0 PID: 0 at arch/powerpc/kernel/trace/ftrace.c:209 ftrace_init_nop+0x408/0x444
  ...
  NIP ftrace_init_nop+0x408/0x444
  LR  ftrace_init_nop+0x404/0x444
  Call Trace:
    ftrace_init_nop+0x404/0x444 (unreliable)
    ftrace_process_locs+0x544/0x8a0
    ftrace_init+0xb4/0x22c
    start_kernel+0x1dc/0x4d4
    start_here_common+0x1c/0x20
  ...
  ftrace failed to modify
  [<c0000000030beddc>] _sub_I_65535_1+0x8/0x3c
   actual:   00:00:00:60
  Initializing ftrace call sites
  ftrace record flags: 0
   (0)
   expected tramp: c00000000008b418
  ------------[ cut here ]------------

The function in question, _sub_I_65535_1 is some sort of trampoline
generated for KASAN, and is in the .text.startup section. That section
is part of INIT_TEXT, meaning is_kernel_inittext() returns true for it.

But the script that determines how many out-of-line ftrace stubs are
needed isn't doesn't consider .text.startup as inittext, leading to
there not being enough space for the init stubs.

Conversely the logic to calculate how many stubs are needed for the text
section isn't filtering out the symbols in .text.startup and so ends up
over counting.

Fix both problems by calculating the total number of stubs first, then
the number that count as inittext, and then subtract the latter from the
former to get the count for the text section.

Fixes: eec37961a56a ("powerpc64/ftrace: Move ftrace sequence out of line")
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://patch.msgid.link/20241107111630.31068-1-mpe@ellerman.id.au
arch/powerpc/tools/ftrace-gen-ool-stubs.sh