All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Weiming Shi" <bestswngs@gmail.com>
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH v4] ftrace: Finish removing subops after a hash update failure
Date: Thu, 03 Dec 2026 11:04:58 +0000	[thread overview]
Message-ID: <sashiko-outbox-247643@kernel.org> (raw)
In-Reply-To: <20261203103722.3018885-3-bestswngs@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] ftrace: unregister_ftrace_graph_finish() omits
  synchronize_rcu_tasks_rude()
- [Medium] fprobe: flush_work() on system_percpu_wq under fprobe_mutex
  can deadlock
- [Medium] fprobe: fprobe_graph_add_ips() flushes RCU cleanup work under
  fprobe_mutex
--

commit 56709e03db270d3dcfbc9de74d45ec089fa5e472
Author: Weiming Shi <bestswngs@gmail.com>

ftrace: Finish removing subops after a hash update failure

This commit detaches subops in ftrace_shutdown_subops() even when the
manager hash update fails, splits unregister_ftrace_graph() into start
and finish helpers with synchronize_rcu(), and defers fprobe graph
cleanup to a work item flushed on re-registration.

> diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c
> index d1f79a85ed2e9..8dea86a62ff44 100644
> --- a/kernel/trace/fgraph.c
> +++ b/kernel/trace/fgraph.c

[ ... ]

> +void unregister_ftrace_graph_finish(struct fgraph_ops *gops)
> +{
> +	synchronize_rcu();
> +	gops->saved_func = NULL;
> +}

[Severity: Medium]
Is synchronize_rcu() alone sufficient to wait for in-flight graph
callbacks before clearing gops->saved_func and freeing the filter?

On architectures without CONFIG_ARCH_WANTS_NO_INSTR (such as PowerPC),
traceable functions like arch_cpu_idle() run after ct_cpuidle_enter()
while !rcu_is_watching().  function_graph_enter_regs() only holds
preempt_disable_notrace() and does not check rcu_is_watching().

CPU0 (!rcu_is_watching())         CPU1
arch_cpu_idle()
  function_graph_enter_regs()
    ftrace_test_recursion_trylock()
    gops = READ_ONCE(...)
                                  unregister_ftrace_graph_start(gops)
                                  unregister_ftrace_graph_finish(gops)
                                    synchronize_rcu()
                                    gops->saved_func = NULL
                                  ftrace_free_filter(&gops->ops)
    ftrace_ops_test(...)  <-- use-after-free

Because CPU0 is in an RCU extended quiescent state, synchronize_rcu()
returns without waiting for it.  CPU0 can then dereference a freed
filter hash in ftrace_ops_test() or a NULL gops->saved_func in
fgraph_pid_func().

Calling synchronize_rcu_tasks_rude() alongside synchronize_rcu(), as
ftrace_shutdown() and ftrace_graph_release() do, waits for these readers
on non-noinstr architectures while remaining a no-op when
CONFIG_ARCH_WANTS_NO_INSTR is enabled.

> diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c
> index 47d565a1e2c56..5bd14c2e738f6 100644
> --- a/kernel/trace/fprobe.c
> +++ b/kernel/trace/fprobe.c

[ ... ]

> @@ -279,6 +281,14 @@ static struct fgraph_ops fprobe_graph_ops = {
>  static int nr_fgraph_fprobes;
>  /* Is fprobe_graph_ops registered? */
>  static bool fprobe_graph_registered;
> +static void fprobe_graph_cleanup_workfn(struct work_struct *work);
> +static DECLARE_WORK(fprobe_graph_cleanup_work, fprobe_graph_cleanup_workfn);
> +
> +static void fprobe_graph_cleanup_workfn(struct work_struct *work)
> +{
> +	unregister_ftrace_graph_finish(&fprobe_graph_ops);
> +	ftrace_free_filter(&fprobe_graph_ops.ops);
> +}
>  
>  /* Add @addrs to the ftrace filter and register fgraph if needed. */
>  static int fprobe_graph_add_ips(unsigned long *addrs, int num)
> @@ -286,6 +296,7 @@ static int fprobe_graph_add_ips(unsigned long *addrs, int num)
>  	int ret;
>  
>  	lockdep_assert_held(&fprobe_mutex);
> +	flush_work(&fprobe_graph_cleanup_work);

[Severity: Medium]
Does flushing fprobe_graph_cleanup_work here hold fprobe_mutex across a
full RCU grace period?

When the last graph-backed fprobe unregisters via
unregister_fprobe_async() and a new one registers before
fprobe_graph_cleanup_workfn() finishes synchronize_rcu(),
register_fprobe_ips() sleeps in flush_work() while holding fprobe_mutex.

This stalls concurrent unregister_fprobe_async() calls (such as
bpf_kprobe_multi_link_release(), which avoids waiting on an RCU grace
period), unrelated fprobe registrations, and module unloads in
fprobe_module_callback() for the entire grace period.

Since fprobe_graph_cleanup_workfn() does not acquire fprobe_mutex,
flushing pending cleanup outside fprobe_mutex (or dropping and
re-acquiring it when cleanup is pending) would avoid the stall.

[ ... ]

> @@ -305,8 +316,8 @@ static int fprobe_graph_add_ips(unsigned long *addrs, int num)
>  static void __fprobe_graph_unregister(void)
>  {
>  	if (fprobe_graph_registered) {
> -		unregister_ftrace_graph(&fprobe_graph_ops);
> -		ftrace_free_filter(&fprobe_graph_ops.ops);
> +		if (unregister_ftrace_graph_start(&fprobe_graph_ops))
> +			schedule_work(&fprobe_graph_cleanup_work);
>  		fprobe_graph_registered = false;
>  	}
>  }

[Severity: Medium]
Can scheduling fprobe_graph_cleanup_work on system_percpu_wq deadlock
with fprobe_graph_add_ips() calling flush_work() under fprobe_mutex?

bpf_link_put() also queues bpf_link_put_deferred() on system_percpu_wq,
and for kprobe_multi links that worker acquires fprobe_mutex via
unregister_fprobe_async().  If 1024 such works fill a CPU's
percpu_max_active limit, fprobe_graph_cleanup_work stays inactive:

CPU1                              CPU0 (system_percpu_wq)
----                              ----
                                  1024x bpf_link_put_deferred() active
                                  fprobe_graph_cleanup_work inactive
register_fprobe_ips()
  mutex_lock(&fprobe_mutex)
  fprobe_graph_add_ips()
    flush_work(...)  /* waits for inactive work */
                                  bpf_link_put_deferred()
                                    bpf_kprobe_multi_link_release()
                                      unregister_fprobe_async()
                                        mutex_lock(&fprobe_mutex)
                                          <-- deadlock

None of the active bpf_link_put_deferred() workers can finish until
fprobe_mutex is released, so fprobe_graph_cleanup_work is never
activated and fprobe_mutex deadlocks permanently.

Using a dedicated workqueue for fprobe_graph_cleanup_work (or flushing
outside fprobe_mutex) would break the shared-workqueue dependency.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203103722.3018885-3-bestswngs@gmail.com?part=1

  reply	other threads:[~2026-12-03 11:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 10:37 [PATCH v4] ftrace: Finish removing subops after a hash update failure Weiming Shi
2026-12-03 11:04 ` sashiko-bot [this message]
2026-12-03 13:46   ` Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-247643@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bestswngs@gmail.com \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.