From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796557689; b=2S/E5sSPQ/LDEaebWdpCzotkw91v3AEDIYy76LoZz+JZKVYzdgwmqCcuW3ynIBfQJxCW eNI9OXTFPz5ak5JY3QZ8vI8up0ewglBbCPTzAgBDAzww3cfMwF6n9NHcqV2mbOswri9Qw KM3v1/lmueiUpL2f/Xx8vigrLy5XhzbeReHdlcYABBXmY6bREmoiPx2dIuDVt/iLWN6Sp UWdOU5o3t0w420AFIxHnRmdnGrRQeL6e26giyyypvpwVsdRuoPjgU7gy85yWKjBdDMclA cV7Mu9hy4uOTzDIwfWa8VliSHl3SnWwulcKe7YsIzPM+4pVNJnHrSea/WuAgJolUvog== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796557689; 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=O20zFpamzZid4taRt88yjUhEwuEklJDuOM+z5Wmanag=; b=KQlwoIo9D08W32XtHLHVms06z57LLP6NQEkjdkZSp0KQyqTIFKkXhU3ypIq9yvIulg/7 qUfoCdt2a/iHHF0vNMunw4bXRU6tdEDb0yLngUmYs8ktHkUCvaQ9VcC+B/JpO7ASseFmp n5AcsQ6ugstT+5hL+shwjbWNr55ECCGhfyYOAawfDiJSA73xF1Lp0dpyM6EZy5UGS+yDZ TZ2PslKh6TMhupp+Jr1QEtGMIzQJEkHqLUyrMtVp0z3dEXkZswCJIufe0q77dyoaQcNsg psC77+VGroBjLjDc1UWKx7ltvI7Z4cYCrsuHwaRVh1BKY9JAUZraj6vC8+IoiDqg/DQ== 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=sBvdXiL7; arc=none smtp.remote-ip=74.125.82.172 Received: from mail-dy1-f172.google.com (mail-dy1-f172.google.com [74.125.82.172]) (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 DA3D1606A31F for ; Sun, 6 Dec 2026 11:48:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DA3D1606A31F 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=sBvdXiL7 Received: by mail-dy1-f172.google.com with SMTP id 5a478bee46e88-387504e1934so2552476eec.1 for ; Sun, 06 Dec 2026 03:48:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796557688; x=1797162488; 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=O20zFpamzZid4taRt88yjUhEwuEklJDuOM+z5Wmanag=; b=sBvdXiL7tA+XkfOxsDMA/mY2pu+Vjac/zhy7MSqIt6oPhUuZtYnBqJfGSMCmtDIeFr brZhqb+RtErt2TpHWJhphQZIOsGSb54TnKo/Eenv203PiGh3/brk0ASh6Is8hH1sveFX y2s85yUC5TPOFCAQJ4gWuDnhMf8FwS4DvmYa62AlcCI/8OdvLOBSS7IqZEkH2cvyHJ7b INXCDNoz2HkrliZTgQvszyz6yRFpfkA3KWNFZxVcO3zkMlLdPz/aXhDry9YTdbZzRF3V jp6gXu6SKw+kRHmX1JUakw6P0bDOOfqsl51JPXu8mkV/b92jJDcHqbmwNmTCO0LOXE1n nVwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796557688; x=1797162488; 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=O20zFpamzZid4taRt88yjUhEwuEklJDuOM+z5Wmanag=; b=GFtV2bvPENQzuvflLYPSQu+3HnqNH/IaVzHU9CuCHQyeg5xYGEzybULTZAnj058RbA Lo4dS4huc6JNyfFcXx+uGsZlVy5FglaLAJsJQ3r4dnj3FuUGo9hRJs6O3J/n0jaYAcCj SsIov9CYvJ3t3x1NXrQRptSB0id3UKSjO3ltayCzJocX10b3Po/gbJewK2kgf7RvutND UuV+apekWp39klYxeVVReAri8zuv7oAmdiPMNWxIiBE9RJVSyVZw2iinB0Qv90R/ECn4 tam4oK0eCwwtU0C7I7hZIPy9B1HhEpYcwzJiY5OjTf/Nl7D+/bS07U2BgKolloiZdJN1 ZwnA== X-Forwarded-Encrypted: i=1; AL+hErGk8Vv/sQvtGo1P7f0YtS9oW0w9Wk/uPKrORmWVqczbGmBOOC7ddMcgxYPg9wMFoHIQt60rzRLzA9uaFKcLh5MxF0g=@vger.kernel.org X-Gm-Message-State: AB4q+uFMm7ssQm5oMhJQySsKadXbF+joqHVCitJhqIWpGHCidq5sqPXm +ebkWNgvyJ9I0QRllyQeeWQRdp/VuHcso7moNyUnGNLKoYzvepbiU28G X-Gm-Gg: AZFdTZemb09os9tR3+tB2vgLK9OJKKkNs07Va6Gk0tIgjMmhhbMDCHo9d1B/0bM8Q67 g883bCyQs/rjNpKCGtHe14EDdAKwRS/QDGhUrJLT6w/94APO0zRzkvs3keIAL5+7A0HXNRlsy2w 5lWx88x6uLcUXuPBmP+DR0ENZMbNUq/pR8DRQ4gFezM44yQQ+tT/mp0XbbpIUi2C3IKsBMCAv/W 8BDVJtDye1ReKU2TJjT1k+CntUquzld84eqNoIxiwAHyHRvvGml5pw3wzoKzYNJ62jNctKTveDx Gl6P2byksv2kiuVvilOKVOjLY7j3aQogsUUnirMMlk5xasdOiVY+UYWXeeItO1d8HO+lAoFMLzW lUOnNkxYMdNNRnWMKd3JWJ0HWdsqW5nYSB8sLC0CZY4MLAxO/Q4WOONeYqA3bC0oFxl2TeNf6Gi 8ncdieTWMTDzpgeyICXyzAlgDpH99X2PEHalLHR2LAyy5Msr7sVEayrGtbY75g6Ykc0V/pbY4je Jz1+kaGraXswaNJO9gupl/wF9MUHv14ohFn2YS4zRXoa4bCZ4zzlqgnoDXCYLqrDroXHjga/NXc SauoyI3ySM+OsBJBvQyqt6r/0JsAIp1iAdRfNQWq4JBBHtLziyHpnUlLrS6Lg1Afa8q36YRxezX EGg== X-Received: by 2002:a05:7301:d80e:10b0:386:240c:19d6 with SMTP id 5a478bee46e88-3875212bcb9mr7279633eec.6.1796557687732; Sun, 06 Dec 2026 03:48:07 -0800 (PST) Received: from Terra.tail0ac356.ts.net ([198.176.50.208]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521bb368sm46454040eec.10.2026.12.06.03.48.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 03:48:07 -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 v5] ftrace: Finish removing subops after a hash update failure Date: Sun, 6 Dec 2026 03:37:42 -0800 Message-ID: <20261206113738.2307040-5-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 An allocation failure during graph unregistration can leave a removed subop marked FTRACE_OPS_FL_ENABLED. For fprobe, this blocks filter cleanup and can crash a later registration. When another graph user remains, unregister_ftrace_graph() removes the subop from fgraph_array[] and calls ftrace_shutdown_subops() to rebuild the manager hashes. If rebuild_hashes() fails, the subop stays off the manager list. If ftrace_update_ops() fails, it is added back. Either way, FTRACE_OPS_FL_ENABLED, FTRACE_OPS_FL_SUBOP, and the managed pointer stay set. ftrace_free_filter() then warns and keeps the filter allocated. After the last graph user is removed, enabling the fprobe again follows the stale FTRACE_OPS_FL_SUBOP state and rebuilds the manager hashes for an empty list. rebuild_hashes() leaves both output pointers uninitialized for an empty list, so ops_equal() dereferences one of them and crashes. Using failslab to fail an allocation in ftrace_shutdown_subops(): cd /sys/kernel/tracing echo tcp_v4_connect > set_ftrace_filter echo function_graph > current_tracer echo 'f:poc udp_sendmsg%return' >> dynamic_events echo 1 > events/fprobes/poc/enable echo 0 > events/fprobes/poc/enable # failslab injects -ENOMEM echo udp_sendmsg > set_ftrace_filter echo 1 > events/fprobes/poc/enable # fails with -EBUSY echo nop > current_tracer echo 1 > events/fprobes/poc/enable # crashes in ops_equal() 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) ... Kernel panic - not syncing: Fatal exception ftrace_shutdown_subops() now detaches the subop on both failures and clears FTRACE_OPS_FL_ENABLED, FTRACE_OPS_FL_SUBOP, and the managed pointer. rebuild_hashes() initializes both output pointers to EMPTY_HASH, so an empty list is safe. A failed manager update may leave old or partially updated hashes, but they still cover the remaining subops. Direct graph calls stay disabled until a removal succeeds and leaves one user or a new first user registers. Clearing FTRACE_OPS_FL_ENABLED lets callers free the filter. Graph callbacks access the operation and filter with preemption disabled, so callers must wait with synchronize_rcu() before clearing saved_func or freeing the filter. unregister_ftrace_graph() is split into start and finish helpers and still calls both synchronously. unregister_fprobe_async() cannot wait for the grace period, so fprobe detaches under fprobe_mutex and queues the finish step and filter cleanup on system_dfl_wq. Graph-backed registration flushes that work outside fprobe_mutex before reusing the shared operation. Fixes: 0ae6b8ce200d ("ftrace: Fix accounting of subop hashes") Reported-by: Assisted-by: LLM Signed-off-by: Weiming Shi --- Changes in v5: - Flush fprobe_graph_cleanup_work outside fprobe_mutex in register_fprobe_ips() and queue it on system_dfl_wq instead of system_percpu_wq. - v4: https://lore.kernel.org/r/20261203103722.3018885-3-bestswngs@gmail.com Changes in v4: - Rewrite the commit message for clarity and show the reproducer commands directly. - Rebase onto 0df89587a56e. - v3: https://lore.kernel.org/r/20261201042523.444837-2-bestswngs@gmail.com 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 | 42 +++++++++++++++++++++++----- kernel/trace/ftrace.c | 22 +++++++++------ kernel/trace/ftrace_internal.h | 2 ++ 4 files changed, 95 insertions(+), 22 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..9437dcd61b98 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,25 @@ 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); +} + +static void fprobe_graph_wait_cleanup(void) +{ + lockdep_assert_held(&fprobe_mutex); + + while (work_busy(&fprobe_graph_cleanup_work)) { + mutex_unlock(&fprobe_mutex); + flush_work(&fprobe_graph_cleanup_work); + mutex_lock(&fprobe_mutex); + } +} /* Add @addrs to the ftrace filter and register fgraph if needed. */ static int fprobe_graph_add_ips(unsigned long *addrs, int num) @@ -305,8 +326,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)) + queue_work(system_dfl_wq, &fprobe_graph_cleanup_work); fprobe_graph_registered = false; } } @@ -1006,13 +1027,18 @@ int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num) struct fprobe_hlist *hlist_array; int ret, i; - guard(mutex)(&fprobe_mutex); - if (fprobe_registered(fp)) - return -EEXIST; + mutex_lock(&fprobe_mutex); + if (fp && !fprobe_is_ftrace(fp) && !fprobe_registered(fp)) + fprobe_graph_wait_cleanup(); + + if (fprobe_registered(fp)) { + ret = -EEXIST; + goto out_unlock; + } ret = fprobe_init(fp, addrs, num); if (ret) - return ret; + goto out_unlock; if (fprobe_is_ftrace(fp)) ret = fprobe_ftrace_add_ips(addrs, num); @@ -1020,7 +1046,7 @@ int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num) ret = fprobe_graph_add_ips(addrs, num); if (ret) { fprobe_fail_cleanup(fp); - return ret; + goto out_unlock; } hlist_array = fp->hlist_array; @@ -1034,6 +1060,8 @@ int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num) synchronize_rcu(); } +out_unlock: + mutex_unlock(&fprobe_mutex); return ret; } EXPORT_SYMBOL_GPL(register_fprobe_ips); diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 5b161959e701..0fda24844d01 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.1