On Fri, Feb 27, 2026 at 09:37:52PM +0100, Jiri Olsa wrote:
> On Fri, Feb 27, 2026 at 09:40:12AM -0800, Ihor Solodrai wrote:
> > On 12/30/25 6:50 AM, Jiri Olsa wrote:
> > > Using single ftrace_ops for direct calls update instead of allocating
> > > ftrace_ops object for each trampoline.
> > >
> > > With single ftrace_ops object we can use update_ftrace_direct_* api
> > > that allows multiple ip sites updates on single ftrace_ops object.
> > >
> > > Adding HAVE_SINGLE_FTRACE_DIRECT_OPS config option to be enabled on
> > > each arch that supports this.
> > >
> > > At the moment we can enable this only on x86 arch, because arm relies
> > > on ftrace_ops object representing just single trampoline image (stored
> > > in ftrace_ops::direct_call). Archs that do not support this will continue
> > > to use *_ftrace_direct api.
> > >
> > > Signed-off-by: Jiri Olsa <[email protected]>
> >
> > Hi Jiri,
> >
> > Me and Kumar stumbled on kernel splats with "ftrace failed to modify",
> > and if running with KASAN:
> >
> > BUG: KASAN: slab-use-after-free in __get_valid_kprobe+0x224/0x2a0
> >
> > Pasting a full splat example at the bottom.
> >
> > I was able to create a reproducer with AI, and then used it to bisect
> > to this patch. You can run it with ./test_progs -t ftrace_direct_race
> >
> > Below is my (human-generated, haha) summary of AI's analysis of what's
> > happening. It makes sense to me conceptually, but I don't know enough
> > details here to call bullshit. Please take a look:
>
> hi, nice :)
>
> >
> > With CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS ftrace_replace_code()
> > operates on all call sites in the shared ops. Then if a concurrent
> > ftrace user (like kprobe) modifies a call site in between
> > ftrace_replace_code's verify pass and its patch pass, then ftrace_bug
> > fires and sets ftrace_disabled to 1.
>
> hum, I'd think that's all under ftrace_lock/direct_mutex,
> but we might be missing some paths
>
could you please try with change below? I can no longer trigger the bug with it
thanks,
jirka
---
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 827fb9a0bf0d..e333749a5896 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -6404,7 +6404,9 @@ int update_ftrace_direct_add(struct ftrace_ops *ops,
struct ftrace_hash *hash)
new_filter_hash = old_filter_hash;
}
} else {
+ mutex_lock(&ftrace_lock);
err = ftrace_update_ops(ops, new_filter_hash, EMPTY_HASH);
+ mutex_unlock(&ftrace_lock);
/*
* new_filter_hash is dup-ed, so we need to release it anyway,
* old_filter_hash either stays on error or is already released
@@ -6530,7 +6532,9 @@ int update_ftrace_direct_del(struct ftrace_ops *ops,
struct ftrace_hash *hash)
ops->func_hash->filter_hash = NULL;
}
} else {
+ mutex_lock(&ftrace_lock);
err = ftrace_update_ops(ops, new_filter_hash, EMPTY_HASH);
+ mutex_unlock(&ftrace_lock);
/*
* new_filter_hash is dup-ed, so we need to release it anyway,
* old_filter_hash either stays on error or is already released