]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
ftrace: Fix BPF fexit with livepatch
authorSong Liu <song@kernel.org>
Mon, 27 Oct 2025 17:50:21 +0000 (10:50 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 4 Nov 2025 01:22:06 +0000 (17:22 -0800)
When livepatch is attached to the same function as bpf trampoline with
a fexit program, bpf trampoline code calls register_ftrace_direct()
twice. The first time will fail with -EAGAIN, and the second time it
will succeed. This requires register_ftrace_direct() to unregister
the address on the first attempt. Otherwise, the bpf trampoline cannot
attach. Here is an easy way to reproduce this issue:

  insmod samples/livepatch/livepatch-sample.ko
  bpftrace -e 'fexit:cmdline_proc_show {}'
  ERROR: Unable to attach probe: fexit:vmlinux:cmdline_proc_show...

Fix this by cleaning up the hash when register_ftrace_function_nolock hits
errors.

Also, move the code that resets ops->func and ops->trampoline to the error
path of register_ftrace_direct(); and add a helper function reset_direct()
in register_ftrace_direct() and unregister_ftrace_direct().

Fixes: d05cb470663a ("ftrace: Fix modification of direct_function hash while in use")
Cc: stable@vger.kernel.org # v6.6+
Reported-by: Andrey Grodzovsky <andrey.grodzovsky@crowdstrike.com>
Closes: https://lore.kernel.org/live-patching/c5058315a39d4615b333e485893345be@crowdstrike.com/
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Acked-and-tested-by: Andrey Grodzovsky <andrey.grodzovsky@crowdstrike.com>
Signed-off-by: Song Liu <song@kernel.org>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/r/20251027175023.1521602-2-song@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Steven Rostedt (Google) <rostedt@goodmis.org>
kernel/bpf/trampoline.c
kernel/trace/ftrace.c

index 5949095e51c3d072cb323bba464160c7012eb404..f2cb0b09709330a3528c3cd72c67922d3879354b 100644 (file)
@@ -479,11 +479,6 @@ again:
                 * BPF_TRAMP_F_SHARE_IPMODIFY is set, we can generate the
                 * trampoline again, and retry register.
                 */
-               /* reset fops->func and fops->trampoline for re-register */
-               tr->fops->func = NULL;
-               tr->fops->trampoline = 0;
-
-               /* free im memory and reallocate later */
                bpf_tramp_image_free(im);
                goto again;
        }
index 42bd2ba68a821935871f549889fe073ef7ed11ab..cbeb7e83313100c8abb515369acde4c8b9c95c99 100644 (file)
@@ -5953,6 +5953,17 @@ static void register_ftrace_direct_cb(struct rcu_head *rhp)
        free_ftrace_hash(fhp);
 }
 
+static void reset_direct(struct ftrace_ops *ops, unsigned long addr)
+{
+       struct ftrace_hash *hash = ops->func_hash->filter_hash;
+
+       remove_direct_functions_hash(hash, addr);
+
+       /* cleanup for possible another register call */
+       ops->func = NULL;
+       ops->trampoline = 0;
+}
+
 /**
  * register_ftrace_direct - Call a custom trampoline directly
  * for multiple functions registered in @ops
@@ -6048,6 +6059,8 @@ int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
        ops->direct_call = addr;
 
        err = register_ftrace_function_nolock(ops);
+       if (err)
+               reset_direct(ops, addr);
 
  out_unlock:
        mutex_unlock(&direct_mutex);
@@ -6080,7 +6093,6 @@ EXPORT_SYMBOL_GPL(register_ftrace_direct);
 int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
                             bool free_filters)
 {
-       struct ftrace_hash *hash = ops->func_hash->filter_hash;
        int err;
 
        if (check_direct_multi(ops))
@@ -6090,13 +6102,9 @@ int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
 
        mutex_lock(&direct_mutex);
        err = unregister_ftrace_function(ops);
-       remove_direct_functions_hash(hash, addr);
+       reset_direct(ops, addr);
        mutex_unlock(&direct_mutex);
 
-       /* cleanup for possible another register call */
-       ops->func = NULL;
-       ops->trampoline = 0;
-
        if (free_filters)
                ftrace_free_filter(ops);
        return err;