From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796099138; b=0sNtoiP2LScD+iDcsCOr3884tKdt2+Fo2iN8fnS4onuB+FJ3mAtE1ZexV9sodDOiK43K gvbKHUW9i4ajAhKmkHGu4Bc6OYDoR6ZmynCpLXEE6R4MUwsquOk8lW8Un4O3NkjcsKQWM Qrewc5IFAb9jAr/B57f5n4PAxcUQ/TBEtMTQ3B51J955hY5ILc7YKwy8+Xi8cF+0lbWqU rFrK28nBTKkw4MIkdhcH9Q+/8P4ZG4TqZ2dQXn5hLBzKw3QtwC6u/W1Yb+2el/j6HmLOP WMMF/lgBvdDONJ/arEzJYHddpthbQ6/1bYB/BA+o8GPVFh69/cpV+0A7B7M9xyiFLeA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796099138; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=U0WlTTINLWp3K3I+Rxk/0BLAUSjhUPuJVgdhja3s7PM=; b=FZpNi1sQrD8HZ1c4sECeNIZl9gKnMsttbQ0gx3W+klThP9zO0KkLuHtFm2+3ZdPAu9FT 06/r74vkTwRvHJ9v532y2RwR+UWc0gZdaODFTlDYtk8wKxJ/w8aGSpU2NoJz2trev7LLD iLuyVEqt5hXgkJz3IZaJ+dtEqB5n71D4Gomf06UUAabjlYbuuNl884ES0ZATMrNf47AQX iMwZl2xKPco9sBlMglGAHZiTBU9HNBF5KuXsa0eOVTqeAnv2jF0Gkw5jtz3BeYNYCN1X6 KidJEvkDdmGoG73hQzFIB7GgajyznBEv1P7AdYDjcwjFzkEYa6JBcxzgakN7hPjxjbQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=BVEGOA4H; arc=none smtp.remote-ip=74.125.82.52 Received: from mail-dl1-f52.google.com (mail-dl1-f52.google.com [74.125.82.52]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E9CD606A327 for ; Tue, 1 Dec 2026 04:25:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9E9CD606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=BVEGOA4H Received: by mail-dl1-f52.google.com with SMTP id a92af1059eb24-170135e43c7so3997014c88.0 for ; Mon, 30 Nov 2026 20:25:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796099137; x=1796703937; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=U0WlTTINLWp3K3I+Rxk/0BLAUSjhUPuJVgdhja3s7PM=; b=BVEGOA4HegIcVJKer3aIan7O3GnlVRvR6o0QwpGWVE7kQ/KsHzISP+HtTymNVclOH+ bGbt/LmnyrVvdZaXK4VPkK/UeXA2KusPJma1/QxHlS4jrXPkPHx9AWwcCVT3mYoSmRvq COq580vch0IF0CdZ+lzXkbC4UDkjngOTIG6nlMEJuE9K+dIYppowUyO8gam81CpjLjf3 hqDExpRhMl0fLHyRuD78o9VfLN6S5Kuq1wlCZQL6/nXLoW6Fpv2lzQeZR7E5PUpNggvO d4OmqtBSvyXYiwV/jSfuwiyP+3P0sN5t7M0B3m5LMC4bhBVihhk+Gzs6nLcKEVVQv3ZK 8kYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796099137; x=1796703937; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=U0WlTTINLWp3K3I+Rxk/0BLAUSjhUPuJVgdhja3s7PM=; b=2r7+vTGIZPEEtbbADk4vzSvbxxWcWhvKwxWSMZSfW1FlnFfyrf3ByS76eAqF5A285e 7/bOubxAVgEVZHt9LOjniZhH+gB/kQQ4w/ZleG7IdXITg/FCQ6KRduvkmeQRgGsRWY0M f1WuY/8wLQxPgHZpimOePcsHx3QB2KuY2EjDHahWMISMXw8w2cMELp6CBv6Q1Sd8WUHL cGghe2Pi4dHibLH2pGEd73B+dA5+hHVPjNOcQIqce1A17wyJeNbmB5P021CPsbw9KqYT dz7K/RgVak5mSv5wMW/nB+Tmqe5CvnNIuKD4/++JgF4H4SZs3oRcwbGjUq590uRXr5z6 leGA== X-Forwarded-Encrypted: i=1; AL+hErFnYOtw9elPeRNwVuze8t7rdRHf2OZmCzyI5i1utJ6I5rw++Tq/SMdmxRVSpHucECDYRYsJcxH1iUMsF5/jFArTVYk=@vger.kernel.org X-Gm-Message-State: APuU4Z9cpNQXEPfBWcFlFN2PWaXkA8TTQ7RvtWxGUYNjMkij1diZ7f9X Wx5FWe/SiBMW2hoko7C4Kof18p78hgMuN3O1KV1NQXmZlM0uYYWnk9ph X-Gm-Gg: AZFdTZdqsr26y5FLN5cCqWwkm1wWHnI2g5J2K3DKa82TRZtEetldOQzcLqTY8ohUpsW KzpeNljCNRg0sBG9/pkXMx1uxO+yTNUkDDgm65XKnnCQ0VNwBmbWPvjoUQScmjoI4NQ0+/2qzVI /hPQeg28M/rshF+igY0jb63XlkWU4RWS2OZ7GWOidSueLeps0TPZPkA68xYo/Dfd71jkozpracE SCX4yDgPIhPjy97laKSv8zFk4rHKGd5vEyowNcZ7jhBdudiox9z6oTK4HCJPgw5mPVcH8H6LSp8 ISAn3CzuHlMdYZRciXvDvCksMBUxds7f2oekIbCJLe9QKuyUo1tzY1F34+ZlRIxlTaWZ6nXg+u+ 2Oaf5y5sFZerom8iy+9mprlroEgFSQHt7vRSn0oxyLlIYKQwR8WYUWXN6Tau4QiKeI2VsaflJ5L Zl3c8vbpg9VXRgf2Iaq2ZLJQtxPmjN1FG3mPeGCSTxV8dZivYumBNK4z4dR53aCemPqk7zEJ+Eb w6VmFLaNWd9EwEG4D1WlDAAKmGdXc+lmwVCDHdtS2lmU0rvuY6eG9DkFL7v2B3196e4fDLm2nHd 2ZhfZ8WW3aHf+hA17uhRuVlOKEw3noZDwoEwgEqDcs4L8Ese3YbWl9iys7pFMAonac1QMPYzcFy NlWs= X-Received: by 2002:a05:7022:b0c4:b0:1b3:b455:93db with SMTP id a92af1059eb24-1b5568eec9fmr32919210c88.24.1796099136435; Mon, 30 Nov 2026 20:25:36 -0800 (PST) Received: from Terra.tail0ac356.ts.net ([198.176.50.208]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b556847fdasm91221310c88.13.2026.11.30.20.25.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:25:35 -0800 (PST) From: Weiming Shi To: Steven Rostedt , Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, co+bba0c6b49150bef3@bugs.sh, Xiang Mei , Weiming Shi Subject: [PATCH v3] ftrace: Finish removing subops after a hash update failure Date: Mon, 30 Nov 2026 20:25:24 -0800 Message-ID: <20261201042523.444837-2-bestswngs@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When other graph operations remain, ftrace_shutdown_subops() removes a subop from the manager list before rebuilding the manager hashes. This non-final hash update only fails on an allocation failure (-ENOMEM) in rebuild_hashes() or ftrace_update_ops(). If rebuild_hashes() fails, the subop remains off the list with FTRACE_OPS_FL_ENABLED and FTRACE_OPS_FL_SUBOP still set. If ftrace_update_ops() fails, it adds the subop back to the list without clearing those flags. unregister_ftrace_graph() has already removed the operation from fgraph_array[] in both cases. In the fprobe path, ftrace_free_filter() then warns and returns because FTRACE_OPS_FL_ENABLED is still set, while __fprobe_graph_unregister() clears fprobe_graph_registered. A later registration follows the stale FTRACE_OPS_FL_SUBOP state into an empty manager list. rebuild_hashes() leaves its output pointers uninitialized for an empty list, so ops_equal() dereferences one of them and crashes. The reproducer hits this by enabling function_graph with a filter on tcp_v4_connect and a return fprobe on udp_sendmsg, then using failslab fault injection to force -ENOMEM in ftrace_shutdown_subops() while disabling the fprobe. Updating the remaining tracer filter to udp_sendmsg, trying to re-enable the fprobe (failing with -EBUSY), disabling function_graph, and enabling the fprobe again crashes in ops_equal(). Finish non-final subop removal on both -ENOMEM paths by clearing the ENABLED and SUBOP flags and managed pointer, and initialize the rebuild_hashes() outputs to EMPTY_HASH so an empty list is safe. Since the old manager hashes may still include functions needed only by the removed subop, keep direct graph calls disabled on an update failure so each remaining subop filter is checked; restore direct calls after a successful reduction to one graph operation or on a new first registration. Completing removal also lets callers free the subop filter even after a hash update failure, while graph callbacks that entered before removal may still use the operation or its filter with preemption disabled. Split unregister_ftrace_graph() into removal and synchronize_rcu() wait helpers, and clear saved_func only after that grace period. Keep the public API synchronous, and in fprobe remove the graph operation synchronously while deferring the wait and filter free to a worker that is flushed before reuse. Oops: general protection fault, probably for non-canonical address 0xdffffc0000000802: 0000 [#1] SMP KASAN NOPTI KASAN: probably user-memory-access in range [0x0000000000004010-0x0000000000004017] RIP: 0010:ops_equal (kernel/trace/trace.h:1011 kernel/trace/ftrace.c:3416) Call Trace: ftrace_update_ops (kernel/trace/ftrace.c:3464) ftrace_hash_move_and_update_subops.isra.0 (kernel/trace/ftrace.c:3795) ftrace_hash_move_and_update_ops (kernel/trace/ftrace.c:5096) ftrace_set_hash (kernel/trace/ftrace.c:6007) ftrace_set_filter_ips (kernel/trace/ftrace.c:6021 kernel/trace/ftrace.c:6770) register_fprobe_ips (kernel/trace/fprobe.c:290 kernel/trace/fprobe.c:1020) register_fprobe (kernel/trace/fprobe.c:979) __register_trace_fprobe.part.0 (kernel/trace/trace_fprobe.c:838) enable_trace_fprobe (kernel/trace/trace_fprobe.c:823 kernel/trace/trace_fprobe.c:1483) fprobe_register (kernel/trace/trace_fprobe.c:1553) __ftrace_event_enable_disable (kernel/trace/trace_events.c:913) event_enable_write (kernel/trace/trace_events.c:943 kernel/trace/trace_events.c:1995) vfs_write (fs/read_write.c:665) ksys_write (fs/read_write.c:719) do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) Kernel panic - not syncing: Fatal exception Fixes: 0ae6b8ce200d ("ftrace: Fix accounting of subop hashes") Reported-by: Assisted-by: LLM Signed-off-by: Weiming Shi --- Changes in v3: - Replace the Tasks RCU waits and "RCU not watching" comment in unregister_ftrace_graph_finish() with synchronize_rcu(). - Drop the fprobe_graph_registered check in fprobe_return() and the WRITE_ONCE()/READ_ONCE() wrappers on fprobe_graph_registered. - State in the changelog that the hash update failure only occurs on -ENOMEM (triggered with failslab) and drop Cc: stable. - Rebase onto 4dce97c6c265 and refresh the decoded trace. - v2: https://lore.kernel.org/r/20261122155620.319722-1-bestswngs@gmail.com - v2 reproducer: https://lore.kernel.org/r/20261122155620.319722-2-bestswngs@gmail.com Changes in v2: - Merge the dependent teardown and graph-reader fixes. - Defer the fprobe callback wait and filter release to a worker. - v1: https://lore.kernel.org/r/20261103175832.3374540-2-bestswngs@gmail.com kernel/trace/fgraph.c | 51 +++++++++++++++++++++++++++++----- kernel/trace/fprobe.c | 15 ++++++++-- kernel/trace/ftrace.c | 22 +++++++++------ kernel/trace/ftrace_internal.h | 2 ++ 4 files changed, 73 insertions(+), 17 deletions(-) diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index d1f79a85ed2e..8dea86a62ff4 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -1407,8 +1407,11 @@ int register_ftrace_graph(struct fgraph_ops *gops) gops->ops.flags |= FTRACE_OPS_FL_GRAPH; ret = ftrace_startup_subops(&graph_ops, &gops->ops, command); - if (!ret) + if (!ret) { fgraph_array[i] = gops; + if (ftrace_graph_active == 1) + ftrace_graph_enable_direct(true, gops); + } error: if (ret) { @@ -1421,13 +1424,27 @@ int register_ftrace_graph(struct fgraph_ops *gops) return ret; } -void unregister_ftrace_graph(struct fgraph_ops *gops) +/** + * unregister_ftrace_graph_start - remove a function graph operation + * @gops: graph operation to remove + * + * Remove @gops from graph dispatch, release its array slot, and update the + * graph and subop state. Existing graph callbacks may still be using @gops + * after this returns. + * + * Return: true if @gops was removed. The caller must then invoke + * unregister_ftrace_graph_finish() exactly once before releasing @gops or its + * filter, even if the subop hash update failed. + */ +bool unregister_ftrace_graph_start(struct fgraph_ops *gops) { int command = 0; + int ret = -EINVAL; + bool removed = false; if (WARN_ONCE(!(gops->ops.flags & FTRACE_OPS_FL_GRAPH), "function graph ops unregistered without registering")) - return; + return false; guard(mutex)(&ftrace_lock); @@ -1441,16 +1458,17 @@ void unregister_ftrace_graph(struct fgraph_ops *gops) if (fgraph_lru_release_index(gops->idx) < 0) goto out; - fgraph_array[gops->idx] = &fgraph_stub; + WRITE_ONCE(fgraph_array[gops->idx], &fgraph_stub); + removed = true; ftrace_graph_active--; if (!ftrace_graph_active) command = FTRACE_STOP_FUNC_RET; - ftrace_shutdown_subops(&graph_ops, &gops->ops, command); + ret = ftrace_shutdown_subops(&graph_ops, &gops->ops, command); - if (ftrace_graph_active == 1) + if (ftrace_graph_active == 1 && !ret) ftrace_graph_enable_direct(true, NULL); else if (!ftrace_graph_active) ftrace_graph_disable_direct(false); @@ -1461,7 +1479,26 @@ void unregister_ftrace_graph(struct fgraph_ops *gops) unregister_pm_notifier(&ftrace_suspend_notifier); unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); } - gops->saved_func = NULL; out: gops->ops.flags &= ~FTRACE_OPS_FL_GRAPH; + return removed; +} + +/** + * unregister_ftrace_graph_finish - wait for callbacks using a removed operation + * @gops: graph operation previously removed by unregister_ftrace_graph_start() + * + * Wait for existing graph readers, then clear the saved callback. This may + * sleep. The caller may release @gops and its filter after this returns. + */ +void unregister_ftrace_graph_finish(struct fgraph_ops *gops) +{ + synchronize_rcu(); + gops->saved_func = NULL; +} + +void unregister_ftrace_graph(struct fgraph_ops *gops) +{ + if (unregister_ftrace_graph_start(gops)) + unregister_ftrace_graph_finish(gops); } diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 47d565a1e2c5..5bd14c2e738f 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -14,9 +14,11 @@ #include #include #include +#include #include +#include "ftrace_internal.h" #include "trace.h" #define FPROBE_IP_HASH_BITS 8 @@ -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); ret = ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 0, 0); if (ret) @@ -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; } } diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 6654da9d1bbf..0d387760a6e8 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -3672,6 +3672,8 @@ static int rebuild_hashes(struct ftrace_hash **filter_hash, struct ftrace_hash * bool first = true; int ret; + *filter_hash = EMPTY_HASH; + *notrace_hash = EMPTY_HASH; temp_hash.filter_hash = EMPTY_HASH; temp_hash.notrace_hash = EMPTY_HASH; @@ -3713,7 +3715,13 @@ static int rebuild_hashes(struct ftrace_hash **filter_hash, struct ftrace_hash * * will not affect functions that are being traced by other subops that * still exist in @ops. * - * If the last subops is removed from @ops, then @ops is shutdown normally. + * If @subops is the last subop, @ops is shut down normally. Otherwise, + * @subops is detached even if the manager hash update fails. The manager + * hashes may be partially updated, but still cover all functions required by + * the remaining subops. + * + * Return: 0 on success, or a negative error code on shutdown or hash update + * failure. */ int ftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command) { @@ -3753,15 +3761,13 @@ int ftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, in /* Rebuild the hashes without subops */ ret = rebuild_hashes(&filter_hash, ¬race_hash, ops); if (ret < 0) - return ret; + goto out; ret = ftrace_update_ops(ops, filter_hash, notrace_hash); - if (ret < 0) { - list_add(&subops->list, &ops->subop_list); - } else { - subops->flags &= ~(FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP); - subops->managed = NULL; - } + +out: + subops->flags &= ~(FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP); + subops->managed = NULL; free_ftrace_hash(filter_hash); free_ftrace_hash(notrace_hash); return ret; diff --git a/kernel/trace/ftrace_internal.h b/kernel/trace/ftrace_internal.h index 3235470e61b3..abb7776a247f 100644 --- a/kernel/trace/ftrace_internal.h +++ b/kernel/trace/ftrace_internal.h @@ -52,6 +52,8 @@ static inline int ftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_o #ifdef CONFIG_FUNCTION_GRAPH_TRACER extern int ftrace_graph_active; +bool unregister_ftrace_graph_start(struct fgraph_ops *gops); +void unregister_ftrace_graph_finish(struct fgraph_ops *gops); # ifdef CONFIG_DYNAMIC_FTRACE extern void fgraph_update_pid_func(void); # else -- 2.56.0