[RFC PATCH 14/27] x86/ftrace,livepatch: Use CET helpers for ftrace regs->ip and trampoline redirection

From: K Prateek Nayak

Date: Sun Nov 29 2026 - 17:34:36 EST


Ftrace core uses ftrace_regs_set_instruction_pointer() to redirect
regs->ip both from ftrace_caller and from ftrace_return_to_handler
return trampoline. Use regs_set_return_ip() to redirect function
execution while correcting the shadow stack.

Livepatch too uses ftrace_regs_set_instruction_pointer() to redirect the
execution to the livepatched function. As a consequence, using CET
helper for the regs->ip redirection also enables livepatching.

Use regs_set_ret_trampoline() to insert the return_to_handler
trampoline.

Signed-off-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
---
arch/x86/include/asm/ftrace.h | 7 +++++--
arch/x86/kernel/ftrace.c | 20 ++++++++++++++++++--
kernel/trace/fgraph.c | 1 +
3 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/ftrace.h b/arch/x86/include/asm/ftrace.h
index c56e1e63b893..de4f71566a36 100644
--- a/arch/x86/include/asm/ftrace.h
+++ b/arch/x86/include/asm/ftrace.h
@@ -68,9 +68,12 @@ arch_ftrace_get_regs(struct ftrace_regs *fregs)
(_regs)->flags = 0; \
} while (0)

-#define ftrace_regs_set_instruction_pointer(fregs, _ip) \
- do { arch_ftrace_regs(fregs)->regs.ip = (_ip); } while (0)

+void ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs,
+ unsigned long ip);
+
+#define ftrace_regs_set_instruction_pointer \
+ ftrace_regs_set_instruction_pointer

static __always_inline unsigned long
ftrace_regs_get_return_address(struct ftrace_regs *fregs)
diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c
index 17d6edfcb7e0..245deb44075f 100644
--- a/arch/x86/kernel/ftrace.c
+++ b/arch/x86/kernel/ftrace.c
@@ -651,11 +651,16 @@ void prepare_ftrace_return(unsigned long ip, unsigned long *parent,
if (unlikely(skip_ftrace_return()))
return;

+ /*
+ * This is only used for !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
+ * (implying !X86_64) and does not need shadow stack handling.
+ */
if (!function_graph_enter(*parent, ip, frame_pointer, parent))
*parent = return_hooker;
}

#ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
+
void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
struct ftrace_ops *op, struct ftrace_regs *fregs)
{
@@ -668,9 +673,20 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
return;


- if (!function_graph_enter_regs(*parent, ip, 0, parent, fregs))
+ if (!function_graph_enter_regs(*parent, ip, 0, parent, fregs)) {
+ regs_set_ret_trampoline(regs, return_hooker);
*parent = return_hooker;
+ }
}
-#endif
+
+void ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs,
+ unsigned long ip)
+{
+ struct pt_regs *regs = &arch_ftrace_regs(fregs)->regs;
+
+ regs_set_return_ip(regs, ip);
+}
+
+#endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */

#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c
index ed455b53513b..2ec8140e5c33 100644
--- a/kernel/trace/fgraph.c
+++ b/kernel/trace/fgraph.c
@@ -822,6 +822,7 @@ __ftrace_return_to_handler(struct ftrace_regs *fregs, unsigned long frame_pointe
ftrace_graph_stop();
WARN_ON(1);
/* Might as well panic. What else to do? */
+ ftrace_regs_set_instruction_pointer(fregs, (unsigned long)panic);
return (unsigned long)panic;
}

--
2.43.0