From nobody Mon Jun 15 19:03:11 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BEDF38237B; Mon, 13 Apr 2026 08:39:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776069580; cv=none; b=KmvHzYlDRQ+V8cH+nGueaVdMiJO26Dj1sGues31oZH3pozMQOxQ0+Lwrk7lxvX42kr550qBMZBV84AOqW4vgvWnJA1l0gHANkydVO/EAOLdKWKGz2omyV5nBBGpN8cnBMbZ9u3Ui8kLzOzctF36EJuD0aqke4fyFMxX7DYfzSWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776069580; c=relaxed/simple; bh=jXA6yadbpUkxcpwhhq5hK3fAHHFh5qDZGWqxwlhONQo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jEwJiFr4D5emjaaS7He7rAzm0kucTuSs40XG0NspT4Mf1iXhLDqmiRDPEbf3h38qCkve+u+9lJkmvz2069LM8kpE0eCVxD+Xql0xduBH/qCSMdRfG0mblop8F/ahXdYIjvMOdImpXYMKWFxq7Ea5T53gH5PiwQqfl3yknhePMNI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bmAbHFq/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bmAbHFq/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3343C116C6; Mon, 13 Apr 2026 08:39:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776069580; bh=jXA6yadbpUkxcpwhhq5hK3fAHHFh5qDZGWqxwlhONQo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bmAbHFq/mg8fK7oPBGx7hI//NpbxS/TD5GPWtUBzprn467HU3Ybje7helA3DlW0y0 tu34Y8an2gQ8/7B+huRSYlCiuanEk40jcDbZqmhL7LqgcP/arR3iMniexHRsEIbEz3 u0r+bWM5HtFLkNbWT65piuCX4qKZFDTwJBCrpHYvW/Re0xKuCh1+cTbqZSR9TuSOMh TGCLUqmehhDvajVxevTTRExb0UxmUYUglXqXtYRZTbC8HxdiriKI8IC930+xcawYtJ BiIrDp1yyD1XZasc5hegw/zmAqc2qU82EyKNClIxVWSVKHdsCXSRWBsSrX4uBq2Wq9 r6FYpsCBt/SKg== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Masami Hiramatsu Cc: Menglong Dong , Mathieu Desnoyers , jiang.biao@linux.dev, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v5 1/3] tracing/fprobe: Remove fprobe from hash in failure path Date: Mon, 13 Apr 2026 17:39:35 +0900 Message-ID: <177606957579.929411.7546403883012683849.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <177606956628.929411.17392736689322577701.stgit@devnote2> References: <177606956628.929411.17392736689322577701.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) When register_fprobe_ips() fails, it tries to remove a list of fprobe_hash_node from fprobe_ip_table, but it missed to remove fprobe itself from fprobe_table. Moreover, when removing the fprobe_hash_node which is added to rhltable once, it must use kfree_rcu() after removing from rhltable. To fix these issues, this reuses unregister_fprobe() internal code to rollback the half-way registered fprobe. Fixes: 4346ba160409 ("fprobe: Rewrite fprobe on function-graph tracer") Signed-off-by: Masami Hiramatsu (Google) --- Changes in v5: - When rolling back an fprobe that failed to register, the fprobe_hash_node are forcibly removed and warn if failure. Changes in v4: - Remove short-cut case because we always need to upadte ftrace_ops. - Use guard(mutex) in register_fprobe_ips() to unlock it correctly. - Remove redundant !ret check in register_fprobe_ips(). - Do not set hlist_array->size in failure case, instead, hlist_array->array[i].fp is set only when insertion is succeeded. Changes in v3: - Newly added. --- kernel/trace/fprobe.c | 101 ++++++++++++++++++++++++++-------------------= ---- 1 file changed, 53 insertions(+), 48 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index dcadf1d23b8a..1d9a3d2276cd 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -4,6 +4,7 @@ */ #define pr_fmt(fmt) "fprobe: " fmt =20 +#include #include #include #include @@ -78,20 +79,27 @@ static const struct rhashtable_params fprobe_rht_params= =3D { }; =20 /* Node insertion and deletion requires the fprobe_mutex */ -static int insert_fprobe_node(struct fprobe_hlist_node *node) +static int insert_fprobe_node(struct fprobe_hlist_node *node, struct fprob= e *fp) { + int ret; + lockdep_assert_held(&fprobe_mutex); =20 - return rhltable_insert(&fprobe_ip_table, &node->hlist, fprobe_rht_params); + ret =3D rhltable_insert(&fprobe_ip_table, &node->hlist, fprobe_rht_params= ); + /* Set the fprobe pointer if insertion was successful. */ + if (!ret) + WRITE_ONCE(node->fp, fp); + return ret; } =20 /* Return true if there are synonims */ static bool delete_fprobe_node(struct fprobe_hlist_node *node) { - lockdep_assert_held(&fprobe_mutex); bool ret; =20 - /* Avoid double deleting */ + lockdep_assert_held(&fprobe_mutex); + + /* Avoid double deleting and non-inserted nodes */ if (READ_ONCE(node->fp) !=3D NULL) { WRITE_ONCE(node->fp, NULL); rhltable_remove(&fprobe_ip_table, &node->hlist, @@ -759,7 +767,6 @@ static int fprobe_init(struct fprobe *fp, unsigned long= *addrs, int num) fp->hlist_array =3D hlist_array; hlist_array->fp =3D fp; for (i =3D 0; i < num; i++) { - hlist_array->array[i].fp =3D fp; addr =3D ftrace_location(addrs[i]); if (!addr) { fprobe_fail_cleanup(fp); @@ -823,6 +830,8 @@ int register_fprobe(struct fprobe *fp, const char *filt= er, const char *notfilter } EXPORT_SYMBOL_GPL(register_fprobe); =20 +static int unregister_fprobe_nolock(struct fprobe *fp, bool force); + /** * register_fprobe_ips() - Register fprobe to ftrace by address. * @fp: A fprobe data structure to be registered. @@ -845,31 +854,27 @@ int register_fprobe_ips(struct fprobe *fp, unsigned l= ong *addrs, int num) if (ret) return ret; =20 - mutex_lock(&fprobe_mutex); + guard(mutex)(&fprobe_mutex); =20 - hlist_array =3D fp->hlist_array; if (fprobe_is_ftrace(fp)) ret =3D fprobe_ftrace_add_ips(addrs, num); else ret =3D fprobe_graph_add_ips(addrs, num); + if (ret) { + fprobe_fail_cleanup(fp); + return ret; + } =20 - if (!ret) { - add_fprobe_hash(fp); - for (i =3D 0; i < hlist_array->size; i++) { - ret =3D insert_fprobe_node(&hlist_array->array[i]); - if (ret) - break; - } - /* fallback on insert error */ + hlist_array =3D fp->hlist_array; + add_fprobe_hash(fp); + for (i =3D 0; i < hlist_array->size; i++) { + ret =3D insert_fprobe_node(&hlist_array->array[i], fp); if (ret) { - for (i--; i >=3D 0; i--) - delete_fprobe_node(&hlist_array->array[i]); + if (unregister_fprobe_nolock(fp, true)) + pr_warn("Failed to cleanup fprobe after insertion failure.\n"); + break; } } - mutex_unlock(&fprobe_mutex); - - if (ret) - fprobe_fail_cleanup(fp); =20 return ret; } @@ -913,37 +918,23 @@ bool fprobe_is_registered(struct fprobe *fp) return true; } =20 -/** - * unregister_fprobe() - Unregister fprobe. - * @fp: A fprobe data structure to be unregistered. - * - * Unregister fprobe (and remove ftrace hooks from the function entries). - * - * Return 0 if @fp is unregistered successfully, -errno if not. - */ -int unregister_fprobe(struct fprobe *fp) +static int unregister_fprobe_nolock(struct fprobe *fp, bool force) { - struct fprobe_hlist *hlist_array; + struct fprobe_hlist *hlist_array =3D fp->hlist_array; unsigned long *addrs =3D NULL; - int ret =3D 0, i, count; + int i, count; =20 - mutex_lock(&fprobe_mutex); - if (!fp || !is_fprobe_still_exist(fp)) { - ret =3D -EINVAL; - goto out; - } - - hlist_array =3D fp->hlist_array; addrs =3D kcalloc(hlist_array->size, sizeof(unsigned long), GFP_KERNEL); - if (!addrs) { - ret =3D -ENOMEM; /* TODO: Fallback to one-by-one loop */ - goto out; - } + if (!addrs && !force) + return -ENOMEM; =20 /* Remove non-synonim ips from table and hash */ count =3D 0; for (i =3D 0; i < hlist_array->size; i++) { - if (!delete_fprobe_node(&hlist_array->array[i])) + if (delete_fprobe_node(&hlist_array->array[i])) + continue; + + if (addrs) addrs[count++] =3D hlist_array->array[i].addr; } del_fprobe_hash(fp); @@ -955,12 +946,26 @@ int unregister_fprobe(struct fprobe *fp) =20 kfree_rcu(hlist_array, rcu); fp->hlist_array =3D NULL; + kfree(addrs); =20 -out: - mutex_unlock(&fprobe_mutex); + return !addrs ? -ENOMEM : 0; +} =20 - kfree(addrs); - return ret; +/** + * unregister_fprobe() - Unregister fprobe. + * @fp: A fprobe data structure to be unregistered. + * + * Unregister fprobe (and remove ftrace hooks from the function entries). + * + * Return 0 if @fp is unregistered successfully, -errno if not. + */ +int unregister_fprobe(struct fprobe *fp) +{ + guard(mutex)(&fprobe_mutex); + if (!fp || !is_fprobe_still_exist(fp)) + return -EINVAL; + + return unregister_fprobe_nolock(fp, false); } EXPORT_SYMBOL_GPL(unregister_fprobe); From nobody Mon Jun 15 19:03:11 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D13D43AB271; Mon, 13 Apr 2026 08:39:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776069589; cv=none; b=J8fwl+bj0JwffQiC1ntltPSQCxsNCyM1D+z9JwOnT8E9E3CMj+qDnxf2dcCeQEOBFsMgfm7vMkdvHnRoNo+kpw+9/XF04zMfo3sfT2v0QiQRL9IoBh8w0i477WgItLVhl3QTHZIOVd9kjsS0LWlvSdyudU28ichBRoWurOdbkIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776069589; c=relaxed/simple; bh=KBivbDUsqlBG6oFLfI0wxGuCqKupalhKNiE4IyQiSSM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cZES6f687fShnQPc8E+HWfu3yMftZKJSO2v6kBSkF1RdCB/1/Ovjn2jFPzzGp5hZ+/2c4JH66eI6ttrT866SBBIprsFCq6zc62LpW3QoFzprCYXdQsApV/3OHr8MHVMkb8GJd8WriQrmX4uDbapolpChYq19EDz83PlpLa7br5I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lUeHoMvd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lUeHoMvd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5CEA2C116C6; Mon, 13 Apr 2026 08:39:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776069589; bh=KBivbDUsqlBG6oFLfI0wxGuCqKupalhKNiE4IyQiSSM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lUeHoMvdF73SmXPSvhPf8Le7YMlfoGwERU5K2CouXWSOC4E/r40rlmYKNAGW0V4FT Klic3tia/RZrxA53LEAlBGI1zKJmrOGpKpoEO78w/k3JBVNsrgH4btrTcwCg+3JjoM dkOxc/mnPXgS8iApGNJF5cEU64GrsHpj/RctUozQsA+dZod3/HxN/MGxZgzlu02Q3j KHzxMBFlzAnG5VP5Vx7uDdHlxg7KzGeLfbgsIZu6CuN1zHSEutmEOoBVsy9OQ9yFwF uv/slVvlQwT2Yvxwli2O+ZWfPQ2xVHjARCWc+YFL8zmxra3lUsdsGqfgSgt++5ah+K +kveZhmgjPRNQ== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Masami Hiramatsu Cc: Menglong Dong , Mathieu Desnoyers , jiang.biao@linux.dev, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v5 2/3] tracing/fprobe: Avoid kcalloc() in rcu_read_lock section Date: Mon, 13 Apr 2026 17:39:45 +0900 Message-ID: <177606958559.929411.17735687274744351885.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <177606956628.929411.17392736689322577701.stgit@devnote2> References: <177606956628.929411.17392736689322577701.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) fprobe_remove_node_in_module() is called under RCU read locked, but this invokes kcalloc() if there are more than 8 fprobes installed on the module. Sashiko warns it because kcalloc() can sleep [1]. [1] https://sashiko.dev/#/patchset/177552432201.853249.5125045538812833325= .stgit%40mhiramat.tok.corp.google.com To fix this issue, expand the batch size to 128 and do not expand the fprobe_addr_list, but just cancel walking on fprobe_ip_table, update fgraph/ftrace_ops and retry the loop again. Fixes: 0de4c70d04a4 ("tracing: fprobe: use rhltable for fprobe_ip_table") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- Changes in v5: - Skip updating ftrace_ops when fails to allocate memory in module unloading. Changes in v4: - fix a build error typo in case of CONFIG_DYNAMIC_FTRACE=3Dn. Changes in v3: - Retry inside rhltable_walk_enter/exit(). - Rename fprobe_set_ips() to fprobe_remove_ips(). - Rename 'retry' label to 'again'. --- kernel/trace/fprobe.c | 89 +++++++++++++++++++++++----------------------= ---- 1 file changed, 41 insertions(+), 48 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 1d9a3d2276cd..b5ce98d2ea96 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -346,11 +346,10 @@ static bool fprobe_is_ftrace(struct fprobe *fp) } =20 #ifdef CONFIG_MODULES -static void fprobe_set_ips(unsigned long *ips, unsigned int cnt, int remov= e, - int reset) +static void fprobe_remove_ips(unsigned long *ips, unsigned int cnt) { - ftrace_set_filter_ips(&fprobe_graph_ops.ops, ips, cnt, remove, reset); - ftrace_set_filter_ips(&fprobe_ftrace_ops, ips, cnt, remove, reset); + ftrace_set_filter_ips(&fprobe_graph_ops.ops, ips, cnt, 1, 0); + ftrace_set_filter_ips(&fprobe_ftrace_ops, ips, cnt, 1, 0); } #endif #else @@ -369,10 +368,9 @@ static bool fprobe_is_ftrace(struct fprobe *fp) } =20 #ifdef CONFIG_MODULES -static void fprobe_set_ips(unsigned long *ips, unsigned int cnt, int remov= e, - int reset) +static void fprobe_remove_ips(unsigned long *ips, unsigned int cnt) { - ftrace_set_filter_ips(&fprobe_graph_ops.ops, ips, cnt, remove, reset); + ftrace_set_filter_ips(&fprobe_graph_ops.ops, ips, cnt, 1, 0); } #endif #endif /* !CONFIG_DYNAMIC_FTRACE_WITH_ARGS && !CONFIG_DYNAMIC_FTRACE_WITH_= REGS */ @@ -546,7 +544,7 @@ static void fprobe_graph_remove_ips(unsigned long *addr= s, int num) =20 #ifdef CONFIG_MODULES =20 -#define FPROBE_IPS_BATCH_INIT 8 +#define FPROBE_IPS_BATCH_INIT 128 /* instruction pointer address list */ struct fprobe_addr_list { int index; @@ -554,45 +552,24 @@ struct fprobe_addr_list { unsigned long *addrs; }; =20 -static int fprobe_addr_list_add(struct fprobe_addr_list *alist, unsigned l= ong addr) +static int fprobe_remove_node_in_module(struct module *mod, struct fprobe_= hlist_node *node, + struct fprobe_addr_list *alist) { - unsigned long *addrs; - - /* Previously we failed to expand the list. */ - if (alist->index =3D=3D alist->size) - return -ENOSPC; - - alist->addrs[alist->index++] =3D addr; - if (alist->index < alist->size) + if (!within_module(node->addr, mod)) return 0; =20 - /* Expand the address list */ - addrs =3D kcalloc(alist->size * 2, sizeof(*addrs), GFP_KERNEL); - if (!addrs) - return -ENOMEM; - - memcpy(addrs, alist->addrs, alist->size * sizeof(*addrs)); - alist->size *=3D 2; - kfree(alist->addrs); - alist->addrs =3D addrs; + if (delete_fprobe_node(node)) + return 0; + /* If no address list is available, we can't track this address. */ + if (!alist->addrs) + return 0; =20 + alist->addrs[alist->index++] =3D node->addr; + if (alist->index =3D=3D alist->size) + return -ENOSPC; return 0; } =20 -static void fprobe_remove_node_in_module(struct module *mod, struct fprobe= _hlist_node *node, - struct fprobe_addr_list *alist) -{ - if (!within_module(node->addr, mod)) - return; - if (delete_fprobe_node(node)) - return; - /* - * If failed to update alist, just continue to update hlist. - * Therefore, at list user handler will not hit anymore. - */ - fprobe_addr_list_add(alist, node->addr); -} - /* Handle module unloading to manage fprobe_ip_table. */ static int fprobe_module_callback(struct notifier_block *nb, unsigned long val, void *data) @@ -601,29 +578,45 @@ static int fprobe_module_callback(struct notifier_blo= ck *nb, struct fprobe_hlist_node *node; struct rhashtable_iter iter; struct module *mod =3D data; + bool retry; =20 if (val !=3D MODULE_STATE_GOING) return NOTIFY_DONE; =20 alist.addrs =3D kcalloc(alist.size, sizeof(*alist.addrs), GFP_KERNEL); - /* If failed to alloc memory, we can not remove ips from hash. */ - if (!alist.addrs) - return NOTIFY_DONE; + /* + * If failed to alloc memory, ftrace_ops will not be able to remove ips f= rom + * hash, but we can still remove nodes from fprobe_ip_table, so we can av= oid + * the potential wrong callback. So just print a warning here and try to + * continue without address list. + */ + WARN_ONCE(!alist.addrs, + "Failed to allocate memory for fprobe_addr_list, ftrace_ops will not be = updated"); =20 mutex_lock(&fprobe_mutex); rhltable_walk_enter(&fprobe_ip_table, &iter); +again: + retry =3D false; + alist.index =3D 0; do { rhashtable_walk_start(&iter); =20 while ((node =3D rhashtable_walk_next(&iter)) && !IS_ERR(node)) - fprobe_remove_node_in_module(mod, node, &alist); + if (fprobe_remove_node_in_module(mod, node, &alist) < 0) { + retry =3D true; + break; + } =20 rhashtable_walk_stop(&iter); - } while (node =3D=3D ERR_PTR(-EAGAIN)); - rhashtable_walk_exit(&iter); + } while (node =3D=3D ERR_PTR(-EAGAIN) && !retry); + /* Remove any ips from hash table(s) */ + if (alist.index > 0) { + fprobe_remove_ips(alist.addrs, alist.index); + if (retry) + goto again; + } =20 - if (alist.index > 0) - fprobe_set_ips(alist.addrs, alist.index, 1, 0); + rhashtable_walk_exit(&iter); mutex_unlock(&fprobe_mutex); =20 kfree(alist.addrs); From nobody Mon Jun 15 19:03:11 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46F443AB271; Mon, 13 Apr 2026 08:39:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776069599; cv=none; b=JIdgLB/m3qfq0RWE2jEvCiwvxGWraAf1D7dNswVvxw1W4oqZFC/CVdMR6xFS+2pvHbR5XpfjBfCWAwnrJJ6dRIHrhXXq+LfDFyBomAdEzAWdS2KQAqzVy5OVDQYTqmHhi71VCSvZoqxTs0lfZEiLG6BR422vp1Ax+jnGVmz9yqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776069599; c=relaxed/simple; bh=q2p6Urce9GMFfNXtX2/9C/Xdoad61fnHAfOix50SLQ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QcPYaoU4CcPugfkcLTUeGDUGOqxF6EdEEojAlDKGb82ejBg7eaKdYSJDKJbX8uC4HPNxQCBGBWHecu57FUEd1uY0N+IKVphu9C1MPuD1V9gFtH+wmQ976v3nPxHmKLIAv5J0M9E1/zKQQjr0s/sbqa8zBdh705OWn6qiTGxEKhc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mcaP7zPZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mcaP7zPZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41C89C116C6; Mon, 13 Apr 2026 08:39:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776069599; bh=q2p6Urce9GMFfNXtX2/9C/Xdoad61fnHAfOix50SLQ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mcaP7zPZKmpouNYetBeCue0JZPohrMMqftblW2SX0NQjM9+gB9jGM2YXV95vtwSBF LwsTgJ1tUl7ic+o5g6GbSS/SD2A4lB1hIN1GaJcFyeJheWJCRCRdOEyRTmajvXF9tl QZ60wy56kqAGX8e23NKBKgRLZwhBWOOyrpZrI4M9tUp7eC34ABtRtSY/Wi3kHbno13 eZ7G1NCOaJZanEArJXtxP+DKvTejJm3xBd6MPoHv4uh0AkakY2uU3EdHvrUO9k6Vwi ZJalpA1WH4a/31Sz/eQVqyPJsGLEIiNzmDJSnm0M6+rY7mAHGUocKIcMGQOGbEYbGt 7YDo5Lo+jObtg== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Masami Hiramatsu Cc: Menglong Dong , Mathieu Desnoyers , jiang.biao@linux.dev, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v5 3/3] tracing/fprobe: Check the same type fprobe on table as the unregistered one Date: Mon, 13 Apr 2026 17:39:54 +0900 Message-ID: <177606959485.929411.16337975722137102756.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <177606956628.929411.17392736689322577701.stgit@devnote2> References: <177606956628.929411.17392736689322577701.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Commit 2c67dc457bc6 ("tracing: fprobe: optimization for entry only case") introduced a different ftrace_ops for entry-only fprobes. However, when unregistering an fprobe, the kernel only checks if another fprobe exists at the same address, without checking which type of fprobe it is. If different fprobes are registered at the same address, the same address will be registered in both fgraph_ops and ftrace_ops, but only one of them will be deleted when unregistering. (the one removed first will not be deleted from the ops). This results in junk entries remaining in either fgraph_ops or ftrace_ops. For example: =3D=3D=3D=3D=3D=3D=3D cd /sys/kernel/tracing # 'Add entry and exit events on the same place' echo 'f:event1 vfs_read' >> dynamic_events echo 'f:event2 vfs_read%return' >> dynamic_events # 'Enable both of them' echo 1 > events/fprobes/enable cat enabled_functions vfs_read (2) ->arch_ftrace_ops_list_func+0x0/0x210 # 'Disable and remove exit event' echo 0 > events/fprobes/event2/enable echo -:event2 >> dynamic_events # 'Disable and remove all events' echo 0 > events/fprobes/enable echo > dynamic_events # 'Add another event' echo 'f:event3 vfs_open%return' > dynamic_events cat dynamic_events f:fprobes/event3 vfs_open%return echo 1 > events/fprobes/enable cat enabled_functions vfs_open (1) tramp: 0xffffffffa0001000 (ftrace_graph_func+0x0/0x= 60) ->ftrace_graph_func+0x0/0x60 subops: {ent:fprobe_fgraph_entry+0x0/0x= 620 ret:fprobe_return+0x0/0x150} vfs_read (1) tramp: 0xffffffffa0001000 (ftrace_graph_func+0x0/0x= 60) ->ftrace_graph_func+0x0/0x60 subops: {ent:fprobe_fgraph_entry+0x0/0x= 620 ret:fprobe_return+0x0/0x150} =3D=3D=3D=3D=3D=3D=3D As you can see, an entry for the vfs_read remains. To fix this issue, when unregistering, the kernel should also check if there is the same type of fprobes still exist at the same address, and if not, delete its entry from either fgraph_ops or ftrace_ops. Fixes: 2c67dc457bc6 ("tracing: fprobe: optimization for entry only case") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- kernel/trace/fprobe.c | 85 +++++++++++++++++++++++++++++++++++++--------= ---- 1 file changed, 65 insertions(+), 20 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index b5ce98d2ea96..19c5b65ed5fb 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -92,11 +92,8 @@ static int insert_fprobe_node(struct fprobe_hlist_node *= node, struct fprobe *fp) return ret; } =20 -/* Return true if there are synonims */ -static bool delete_fprobe_node(struct fprobe_hlist_node *node) +static void delete_fprobe_node(struct fprobe_hlist_node *node) { - bool ret; - lockdep_assert_held(&fprobe_mutex); =20 /* Avoid double deleting and non-inserted nodes */ @@ -105,13 +102,6 @@ static bool delete_fprobe_node(struct fprobe_hlist_nod= e *node) rhltable_remove(&fprobe_ip_table, &node->hlist, fprobe_rht_params); } - - rcu_read_lock(); - ret =3D !!rhltable_lookup(&fprobe_ip_table, &node->addr, - fprobe_rht_params); - rcu_read_unlock(); - - return ret; } =20 /* Check existence of the fprobe */ @@ -345,6 +335,32 @@ static bool fprobe_is_ftrace(struct fprobe *fp) return !fp->exit_handler; } =20 +static bool fprobe_exists_on_hash(unsigned long ip, bool ftrace) +{ + struct rhlist_head *head, *pos; + struct fprobe_hlist_node *node; + struct fprobe *fp; + + guard(rcu)(); + head =3D rhltable_lookup(&fprobe_ip_table, &ip, + fprobe_rht_params); + if (!head) + return false; + /* We have to check the same type on the list. */ + rhl_for_each_entry_rcu(node, pos, head, hlist) { + if (node->addr !=3D ip) + break; + fp =3D READ_ONCE(node->fp); + if (likely(fp)) { + if ((!ftrace && fp->exit_handler) || + (ftrace && !fp->exit_handler)) + return true; + } + } + + return false; +} + #ifdef CONFIG_MODULES static void fprobe_remove_ips(unsigned long *ips, unsigned int cnt) { @@ -367,6 +383,29 @@ static bool fprobe_is_ftrace(struct fprobe *fp) return false; } =20 +static bool fprobe_exists_on_hash(unsigned long ip, bool ftrace __maybe_un= used) +{ + struct rhlist_head *head, *pos; + struct fprobe_hlist_node *node; + struct fprobe *fp; + + guard(rcu)(); + head =3D rhltable_lookup(&fprobe_ip_table, &ip, + fprobe_rht_params); + if (!head) + return false; + /* We only need to check fp is there. */ + rhl_for_each_entry_rcu(node, pos, head, hlist) { + if (node->addr !=3D ip) + break; + fp =3D READ_ONCE(node->fp); + if (likely(fp)) + return true; + } + + return false; +} + #ifdef CONFIG_MODULES static void fprobe_remove_ips(unsigned long *ips, unsigned int cnt) { @@ -555,18 +594,25 @@ struct fprobe_addr_list { static int fprobe_remove_node_in_module(struct module *mod, struct fprobe_= hlist_node *node, struct fprobe_addr_list *alist) { + lockdep_assert_in_rcu_read_lock(); + if (!within_module(node->addr, mod)) return 0; =20 - if (delete_fprobe_node(node)) - return 0; + delete_fprobe_node(node); /* If no address list is available, we can't track this address. */ if (!alist->addrs) return 0; + /* + * Don't care the type here, because all fprobes on the same + * address must be removed eventually. + */ + if (!rhltable_lookup(&fprobe_ip_table, &node->addr, fprobe_rht_params)) { + alist->addrs[alist->index++] =3D node->addr; + if (alist->index =3D=3D alist->size) + return -ENOSPC; + } =20 - alist->addrs[alist->index++] =3D node->addr; - if (alist->index =3D=3D alist->size) - return -ENOSPC; return 0; } =20 @@ -924,10 +970,9 @@ static int unregister_fprobe_nolock(struct fprobe *fp,= bool force) /* Remove non-synonim ips from table and hash */ count =3D 0; for (i =3D 0; i < hlist_array->size; i++) { - if (delete_fprobe_node(&hlist_array->array[i])) - continue; - - if (addrs) + delete_fprobe_node(&hlist_array->array[i]); + if (addrs && !fprobe_exists_on_hash(hlist_array->array[i].addr, + fprobe_is_ftrace(fp))) addrs[count++] =3D hlist_array->array[i].addr; } del_fprobe_hash(fp);