: [PATCH] ftrace/x86: Add FTRACE_MCOUNT_MAX_OFFSET to avoid adding weak functions
Subsystems (6)
Steven Rostedt
maintainerFrom: "Steven Rostedt (Google)" <[email protected]>
If an unused weak function was traced, it's call to fentry will still exist, which gets added into the __mcount_loc table. Ftrace will use kallsyms to retrieve the name for each location in __mcount_loc to display it in the available_filter_functions and used to enable functions via the name matching in set_ftrace_filter/notrace. Enabling these functions do nothing but enable an unused call to ftrace_caller. If a traced weak function is overridden, the symbol of the function would be used for it, which will either created duplicate names, or if the previous function was not traced, it would be incorrectly listed in available_filter_functions as a function that can be traced.
This became an issue with BPF[1] as there are tooling that enables the direct callers via ftrace but then checks to see if the functions were actually enabled. The case of one function that was marked notrace, but was followed by an unused weak function that was traced. The unused function's call to fentry was added to the __mcount_loc section, and kallsyms retrieved the untraced function's symbol as the weak function was overridden. Since the untraced function would not get traced, the BPF check would detect this and fail.
The real fix would be to fix kallsyms to not show address of weak functions as the function before it. But that would require adding code in the build to add function size to kallsyms so that it can know when the function ends instead of just using the start of the next known symbol.
In the mean time, this is a work around. Add a FTRACE_MCOUNT_MAX_OFFSET macro that if defined, ftrace will ignore any function that has its call to fentry/mcount that has an offset from the symbol that is greater than FTRACE_MCOUNT_MAX_OFFSET.
If CONFIG_HAVE_FENTRY is defined for x86, define FTRACE_MCOUNT_MAX_OFFSET to zero, which will have ftrace ignore all locations that are not at the start of the function.
[1] https://lore.kernel.org/all/[email protected]/
--- arch/x86/include/asm/ftrace.h | 5 ++++ kernel/trace/ftrace.c | 50 +++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-)
arch/x86/include/asm/ftrace.h +5 -0
@@ -9,6 +9,11 @@# define MCOUNT_ADDR ((unsigned long)(__fentry__))#define MCOUNT_INSN_SIZE 5/* sizeof mcount call */ +/* Ignore unused weak functions which will have non zero offsets */ +#ifdef CONFIG_HAVE_FENTRY +# define FTRACE_MCOUNT_MAX_OFFSET 0 +#endif +#ifdef CONFIG_DYNAMIC_FTRACE #define ARCH_SUPPORTS_FTRACE_OPS 1#endif
kernel/trace/ftrace.c +48 -2
74 lines not on this page · raw message
Signature · 1 line
2.35.1
Quoted 27 lines · Steven Rostedt
From: "Steven Rostedt (Google)" <[email protected]>
If an unused weak function was traced, it's call to fentry will still exist, which gets added into the
__mcount_loctable. Ftrace will use kallsyms to retrieve the name for each location in__mcount_locto display it in theavailable_filter_functionsand used to enable functions via the name matching in set_ftrace_filter/notrace. Enabling these functions do nothing but enable an unused call toftrace_caller. If a traced weak function is overridden, the symbol of the function would be used for it, which will either created duplicate names, or if the previous function was not traced, it would be incorrectly listed inavailable_filter_functionsas a function that can be traced.This became an issue with BPF[1] as there are tooling that enables the direct callers via ftrace but then checks to see if the functions were actually enabled. The case of one function that was marked notrace, but was followed by an unused weak function that was traced. The unused function's call to fentry was added to the
__mcount_locsection, and kallsyms retrieved the untraced function's symbol as the weak function was overridden. Since the untraced function would not get traced, the BPF check would detect this and fail.The real fix would be to fix kallsyms to not show address of weak functions as the function before it. But that would require adding code in the build to add function size to kallsyms so that it can know when the function ends instead of just using the start of the next known symbol.
Quoted 4 lines
In the mean time, this is a work around. Add a
FTRACE_MCOUNT_MAX_OFFSETmacro that if defined, ftrace will ignore any function that has its call to fentry/mcount that has an offset from the symbol that is greater thanFTRACE_MCOUNT_MAX_OFFSET.
So for x86-64... objtool knows about these holes and *could* squash these entries if you want (at the cost of requiring link time objtool run).
Also see commit:
4adb23686795 ("objtool: Ignore extra-symbol code")
But yeah, ensuring fentry is close ought to work.
Quoted 3 lines
If
CONFIG_HAVE_FENTRYis defined for x86, defineFTRACE_MCOUNT_MAX_OFFSETto zero, which will have ftrace ignore all locations that are not at the start of the function.
You forgot about IBT again? __fentry__ no longer lives at +0 on x86 anymore.
Steven Rostedt
maintainerQuoted 2 lines · On Fri, 6 May 2022 21:38:29 +0200 Peter Zijlstra <[email protected]> wrote:
You forgot about IBT again?
__fentry__no longer lives at +0 on x86 anymore.
I didn't forget. It was that the kernel I wrote this for didn't have it ;-)
I wasn't sure if it was accepted into mainline yet.
-- Steve
Quoted 37 lines · Steven Rostedt
From: "Steven Rostedt (Google)" <[email protected]>
If an unused weak function was traced, it's call to fentry will still exist, which gets added into the
__mcount_loctable. Ftrace will use kallsyms to retrieve the name for each location in__mcount_locto display it in theavailable_filter_functionsand used to enable functions via the name matching in set_ftrace_filter/notrace. Enabling these functions do nothing but enable an unused call toftrace_caller. If a traced weak function is overridden, the symbol of the function would be used for it, which will either created duplicate names, or if the previous function was not traced, it would be incorrectly listed inavailable_filter_functionsas a function that can be traced.This became an issue with BPF[1] as there are tooling that enables the direct callers via ftrace but then checks to see if the functions were actually enabled. The case of one function that was marked notrace, but was followed by an unused weak function that was traced. The unused function's call to fentry was added to the
__mcount_locsection, and kallsyms retrieved the untraced function's symbol as the weak function was overridden. Since the untraced function would not get traced, the BPF check would detect this and fail.The real fix would be to fix kallsyms to not show address of weak functions as the function before it. But that would require adding code in the build to add function size to kallsyms so that it can know when the function ends instead of just using the start of the next known symbol.
In the mean time, this is a work around. Add a
FTRACE_MCOUNT_MAX_OFFSETmacro that if defined, ftrace will ignore any function that has its call to fentry/mcount that has an offset from the symbol that is greater thanFTRACE_MCOUNT_MAX_OFFSET.If
CONFIG_HAVE_FENTRYis defined for x86, defineFTRACE_MCOUNT_MAX_OFFSETto zero, which will have ftrace ignore all locations that are not at the start of the function.
Quoted 8 lines
[1] https://lore.kernel.org/all/[email protected]/
Signed-off-by Steven Rostedt (Google) <[email protected]>--- arch/x86/include/asm/ftrace.h | 5 ++++ kernel/trace/ftrace.c | 50 +++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-)
Thanks for investigating and fixing this! I guess we'll need ENDBR handling, but otherwise it looks good to me!