From nobody Tue Jun 16 02:39:30 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 EEB95314A60; Wed, 15 Apr 2026 10:08:44 +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=1776247725; cv=none; b=ojJ/qblKxkb3VhijlPMFawq9pzHQq5CTEDtIcGmsmcyzXsPfCX/dizLMVdfmPp9Q8xHAJyWrHtSIzXUx34eu+tdADTzMRzwMa9eqAckCOBPr+4pgawbxI9p6YGVf7/PUX4Nzqn5G/tHGbZM9SgH9ThldDKN3ePleR/PKFySojpY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776247725; c=relaxed/simple; bh=rqs+M1PyAlmYt6BIh4c8u/Ci2zxPhYMYAfWmhLkdOIg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cNMcQ0YdORciIsblnxPYg0UWsmToX0tKJZSStrxSX7wYT16TGfCilttsGy9G1ALe0IUowMUe7phlob825ZqOIB3QpOzq33Yezi1rYbnSKHUMuIuuNQUmI8huvx6PDmVyeZW5+r8hJ27dTSKJRuTRScblvQBKj1w0JTkf3vg1ogA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iKNjs56O; 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="iKNjs56O" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0F80C19424; Wed, 15 Apr 2026 10:08:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776247724; bh=rqs+M1PyAlmYt6BIh4c8u/Ci2zxPhYMYAfWmhLkdOIg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iKNjs56OdDhf14GyqgobValCarJnw8OJ2FwxfnnOPoTyNHLiWO2Og4ZQAZnohPqqP YEHONAh3YQLd22c3G49s9vHlcZ+QDRJsVVqBvto0HeBwIOGPIFhld5NnBTwgC8Nejn Qj5Gngh72VEFMFm6ctOtI7mMiann8MfN7vUhGvkuskMLfWooyCYE13ZZ/Dx6I7/gDp FBqGB5nS2hoPWjG2K/3ATOTgUcw8UCs3q6PbYgAHM/MDCXKAHUIF2Ofz+BTegen5AU WPp95ukB+Bc9bcPOmbkY7I+KgnHL/zZF2yS6xIeBnwiUxqynpiaptHQld9GSvq3NQ4 kEpbhvd7G3fbw== 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 v7 1/6] tracing/fprobe: Reject registration of a registered fprobe before init Date: Wed, 15 Apr 2026 19:08:42 +0900 Message-ID: <177624772205.2407691.8530954581518787436.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.513.gad8abe7a5a-goog In-Reply-To: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> References: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> 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) Reject registration of a registered fprobe which is on the fprobe hash table before initializing fprobe. The add_fprobe_hash() checks this re-register fprobe, but since fprobe_init() clears hlist_array field, it is too late to check it. It has to check the re-registration before touncing fprobe. Fixes: 4346ba160409 ("fprobe: Rewrite fprobe on function-graph tracer") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- Changes in v6: - Newly added. --- kernel/trace/fprobe.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index dcadf1d23b8a..fc7018b28fdd 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 @@ -107,7 +108,7 @@ static bool delete_fprobe_node(struct fprobe_hlist_node= *node) } =20 /* Check existence of the fprobe */ -static bool is_fprobe_still_exist(struct fprobe *fp) +static bool fprobe_registered(struct fprobe *fp) { struct hlist_head *head; struct fprobe_hlist *fph; @@ -120,7 +121,7 @@ static bool is_fprobe_still_exist(struct fprobe *fp) } return false; } -NOKPROBE_SYMBOL(is_fprobe_still_exist); +NOKPROBE_SYMBOL(fprobe_registered); =20 static int add_fprobe_hash(struct fprobe *fp) { @@ -132,9 +133,6 @@ static int add_fprobe_hash(struct fprobe *fp) if (WARN_ON_ONCE(!fph)) return -EINVAL; =20 - if (is_fprobe_still_exist(fp)) - return -EEXIST; - head =3D &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)]; hlist_add_head_rcu(&fp->hlist_array->hlist, head); return 0; @@ -149,7 +147,7 @@ static int del_fprobe_hash(struct fprobe *fp) if (WARN_ON_ONCE(!fph)) return -EINVAL; =20 - if (!is_fprobe_still_exist(fp)) + if (!fprobe_registered(fp)) return -ENOENT; =20 fph->fp =3D NULL; @@ -482,7 +480,7 @@ static void fprobe_return(struct ftrace_graph_ret *trac= e, if (!fp) break; curr +=3D FPROBE_HEADER_SIZE_IN_LONG; - if (is_fprobe_still_exist(fp) && !fprobe_disabled(fp)) { + if (fprobe_registered(fp) && !fprobe_disabled(fp)) { if (WARN_ON_ONCE(curr + size > size_words)) break; fp->exit_handler(fp, trace->func, ret_ip, fregs, @@ -841,12 +839,14 @@ int register_fprobe_ips(struct fprobe *fp, unsigned l= ong *addrs, int num) struct fprobe_hlist *hlist_array; int ret, i; =20 + guard(mutex)(&fprobe_mutex); + if (fprobe_registered(fp)) + return -EEXIST; + ret =3D fprobe_init(fp, addrs, num); if (ret) return ret; =20 - mutex_lock(&fprobe_mutex); - hlist_array =3D fp->hlist_array; if (fprobe_is_ftrace(fp)) ret =3D fprobe_ftrace_add_ips(addrs, num); @@ -866,7 +866,6 @@ int register_fprobe_ips(struct fprobe *fp, unsigned lon= g *addrs, int num) delete_fprobe_node(&hlist_array->array[i]); } } - mutex_unlock(&fprobe_mutex); =20 if (ret) fprobe_fail_cleanup(fp); @@ -928,7 +927,7 @@ int unregister_fprobe(struct fprobe *fp) int ret =3D 0, i, count; =20 mutex_lock(&fprobe_mutex); - if (!fp || !is_fprobe_still_exist(fp)) { + if (!fp || !fprobe_registered(fp)) { ret =3D -EINVAL; goto out; } From nobody Tue Jun 16 02:39:30 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 E54B52288CB; Wed, 15 Apr 2026 10:08:52 +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=1776247733; cv=none; b=dIJI3e+acLxYa/WEv3iPVG/TSGf5Qix+mdbhB81V3Q9k9YxXKTr7/9m4GYv+wqVUCfxijDzr+O0yz9XsMUS6tDrSZ4gtDn8T8zFWJnWEcqURTPoUQVRp+3pppZh83HvF2CJdCM8sH8ZtexhN46fhCyYASWzZeW4+2cbjRWzYrVA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776247733; c=relaxed/simple; bh=gN1BRrqXMRbChB0x61xF2kukdXPqZcRjnyQOsPmr+Ig=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OghIwFtaQKik+XRC9ZpFf7sOUc97+8ntJULaxKcUhEDwHsi8MpB0Bq4RzejfeRlw7kNvBIHwpGnvfydRPuYZ9r0Vgc5l9re+4ZQpl7rt3Dx+U1Z+Qz+c2dOQRbjybNK8e5ZznHFztAFrpoHWphUEJBntcARce8PzkV2ahxLbnaQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oPql3u9U; 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="oPql3u9U" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FAF8C19424; Wed, 15 Apr 2026 10:08:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776247732; bh=gN1BRrqXMRbChB0x61xF2kukdXPqZcRjnyQOsPmr+Ig=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oPql3u9UYGsOEhgsQcyl3pPSgsnSw4qLHQ3kp94Fq7YI2djUg6fVLMcDQQDHtH3wZ qMYvJvyvFJLdIZeTDsLzEfvY8hMkGSKrBUutD/Ips3tbCpxognxYQPfQgvqDZUtUw9 +LpPw80kMaTUkSVnUmzL5JyDJTQy5p3OVz2h8i+2nEdSNKVQRvKGWbNP0CVcDEAkFK rgUpNtHoHegetfwGj6TLDuHJ0iEdOMIYtEx4FYP1WTkI3Mndtos/8+SD7aIv1eCnwh x4aLZsTEuo3x7GHa5Wa/D0g+zFlpheufj2AzNvcBiOQRjwFgEdw8y4EMn+DAuokIZq OsEWXTNKMpKuw== 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 v7 2/6] tracing/fprobe: Unregister fprobe even if memory allocation fails Date: Wed, 15 Apr 2026 19:08:50 +0900 Message-ID: <177624772989.2407691.17449580584634294999.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.513.gad8abe7a5a-goog In-Reply-To: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> References: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> 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) unregister_fprobe() can fail under memory pressure because of memory allocation failure, but this maybe called from module unloading, and usually there is no way to retry it. Moreover. trace_fprobe does not check the return value. To fix this problem, unregister fprobe and fprobe_hash_node even if working memory allocation fails. Anyway, if the last fprobe is removed, the filter will be freed. Fixes: 4346ba160409 ("fprobe: Rewrite fprobe on function-graph tracer") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- Changes in v7: - Newly added. --- kernel/trace/fprobe.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index fc7018b28fdd..d634eb8e8b9e 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -934,15 +934,19 @@ int unregister_fprobe(struct fprobe *fp) =20 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; - } + /* + * This will remove fprobe_hash_node from the hash table even if + * memory allocation fails. However, ftrace_ops will not be updated. + * Anyway, when the last fprobe is unregistered, ftrace_ops is also + * unregistered. + */ + if (!addrs) + pr_warn("Failed to allocate working array. ftrace_ops may not sync.\n"); =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]) && addrs) addrs[count++] =3D hlist_array->array[i].addr; } del_fprobe_hash(fp); From nobody Tue Jun 16 02:39:30 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 B77D3340A6A; Wed, 15 Apr 2026 10:09:00 +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=1776247740; cv=none; b=glzzuBsGwk50cztS3gpDCiTxZmy242przgsf0XCwAA9QsFdl8HsEDCL1xEIgR44nkB0VeCZXYn2h7UDGwaKna/GmwamkqsiHEofaDz2LPEnhfR7zzqlWzEpDbkZf1bFSktgP8MRVBn+aZsaY2JQiS8MRq8aWO/af5kIxTxzm8lY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776247740; c=relaxed/simple; bh=2cnKJdk5yVgYoMxjltisR4SF9rg4l0e+Rj4E0adsHkc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DRscUccVvhwXq1IsJEhtaPQfuymrLAiEgcbM3HlPedhViC5tTYinJkH9zEsH+2e49QTUwVn9gygd3MK0sGIEe4LiJCXH3fDElslnuflA1bOUYM78URXFWQnlNtA9bayRkpeyZPrUEtGXhpumWwX+ZZ9jICYIIVEWMY4E2puKOiM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=STOSt7/c; 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="STOSt7/c" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 772EDC2BCB4; Wed, 15 Apr 2026 10:08:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776247740; bh=2cnKJdk5yVgYoMxjltisR4SF9rg4l0e+Rj4E0adsHkc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=STOSt7/cu3WrPiYs7nnfioLEgsdUvgC6Vw7uuxEsoz0d7pdOptYFOQ5sqIGWUuV7g xFr3VGRSmI1swbWefqlnmyK8GksiO3GfLoNU4TIKV5nEhU5NSyGddxYxxitVYO+uYN 5xC1MbiDLVHVaXa1eoIdZ+ZveMibngDwmgV0Uv+vlpKhdp+M1RzpTDsgR8ZzUzMNeW jHXG9rwNpxvnkK9TW+8iSmxheUIJWpf/w31oOVk606I8p3Exx0yhUJsH6oDSspb4X0 /dgKQ4FQpqyIZUMfLj/gAivMCwBkyJLbZo0HKEaglYIA7BlWQr2FEhoDbSRlL5wq24 zJ2aGEZwbgxow== 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 v7 3/6] tracing/fprobe: Remove fprobe from hash in failure path Date: Wed, 15 Apr 2026 19:08:58 +0900 Message-ID: <177624773781.2407691.17563168322102270682.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.513.gad8abe7a5a-goog In-Reply-To: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> References: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> 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") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- Changes in v7: - Remove RCU grace period wait, since fprobe itself is not that is not needed. Changes in v6: - Wait for an RCU grace period before returning error in unregister_fprobe_nolock(). 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 | 87 +++++++++++++++++++++++++--------------------= ---- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index d634eb8e8b9e..c58e41b2da8e 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -79,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, @@ -757,7 +764,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); @@ -821,6 +827,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); + /** * register_fprobe_ips() - Register fprobe to ftrace by address. * @fp: A fprobe data structure to be registered. @@ -847,28 +855,22 @@ int register_fprobe_ips(struct fprobe *fp, unsigned l= ong *addrs, int num) if (ret) return ret; =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) { - 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 */ - if (ret) { - for (i--; i >=3D 0; i--) - delete_fprobe_node(&hlist_array->array[i]); - } + if (ret) { + fprobe_fail_cleanup(fp); + return ret; } =20 - if (ret) - fprobe_fail_cleanup(fp); + 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) + unregister_fprobe_nolock(fp); + } =20 return ret; } @@ -912,27 +914,12 @@ 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) { - 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 || !fprobe_registered(fp)) { - ret =3D -EINVAL; - goto out; - } - - hlist_array =3D fp->hlist_array; addrs =3D kcalloc(hlist_array->size, sizeof(unsigned long), GFP_KERNEL); /* * This will remove fprobe_hash_node from the hash table even if @@ -958,12 +945,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 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 || !fprobe_registered(fp)) + return -EINVAL; + + return unregister_fprobe_nolock(fp); } EXPORT_SYMBOL_GPL(unregister_fprobe); From nobody Tue Jun 16 02:39:30 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 B08202288CB; Wed, 15 Apr 2026 10:09:08 +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=1776247748; cv=none; b=SuKl+YNxi02AF0cz8xzncnBXd0Dk1sAArZF7i0MPhy0tEmvhFMLIJ60PT4hGy1i1C1dtnEpd3xouFcmcNQ/llGDumQjAfa1NVbe9gPEKcF3JTm7DsCPQKixOjz+7l6wD3ggwFqKsBPbWPB/QpD+MEC5vhc3pnFvNp3CRo67BY5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776247748; c=relaxed/simple; bh=mnYd09qAmaj7dYWhLmi8/ZAR5RiHiWstovDXsHiPbkI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=asFTZGYAvQfpqT1Mjf03KGZy68JyXa1ZTIg12Sw4G7u1VjgO3SVjdlJciiOBxQrfpxDByK3HKwKkxOOiXgTRAm44M3hN3u2F5EMA7rIatsZMIuzAaXIa3ieUsqpnnVkmWb2dERBFzNCm8TrE9XFLqYx11j0uG+Rnawqj8eTF8Gg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f3x5/Zcn; 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="f3x5/Zcn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 589E3C19424; Wed, 15 Apr 2026 10:09:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776247748; bh=mnYd09qAmaj7dYWhLmi8/ZAR5RiHiWstovDXsHiPbkI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f3x5/ZcnlbL78K3Y1jw9RlyZOLdOBHC2IxvPD1dA97NPP9e9ynZ0gd9PAvq9D+WIY rlWNnawKRX9MuQKXtOwpeBlWR5jJuplQ17G2RfGuJESVicVPSMAHLzHQG06JHdBEzY DX1DA98xALpwO7qcF2DMKKbBqpTpnLI+S2ieLaGaRoQBbfFo9Qpi8UxMcXITZNCqT/ fUAsgSg05NFLpegP354jnNFSvcolMuYlvopmEXCdIFskow4I/ZpJiCIEoohCMcySJ2 OyadKG7T3JIF6iqrE/Y15MAkKFYBM+yh/L9qwFIK/wdIAZ+mfoUxb2Rolof4ngNg2g 8XjBBMHU4tLyg== 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 v7 4/6] tracing/fprobe: Avoid kcalloc() in rcu_read_lock section Date: Wed, 15 Apr 2026 19:09:05 +0900 Message-ID: <177624774567.2407691.4607367874855149551.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.513.gad8abe7a5a-goog In-Reply-To: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> References: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> 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 v6: - Retry outside rhltable_walk_enter/exit() again. 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 | 92 ++++++++++++++++++++++++---------------------= ---- 1 file changed, 45 insertions(+), 47 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index c58e41b2da8e..0df8e003e88e 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -343,11 +343,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 @@ -366,10 +365,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 */ @@ -543,7 +541,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; @@ -551,45 +549,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) @@ -598,29 +575,50 @@ 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); +again: + retry =3D false; + alist.index =3D 0; rhltable_walk_enter(&fprobe_ip_table, &iter); 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)); + } while (node =3D=3D ERR_PTR(-EAGAIN) && !retry); rhashtable_walk_exit(&iter); + /* Remove any ips from hash table(s) */ + if (alist.index > 0) { + fprobe_remove_ips(alist.addrs, alist.index); + /* + * If we break rhashtable walk loop except for -EAGAIN, we need + * to restart looping from start for safety. Anyway, this is + * not a hotpath. + */ + if (retry) + goto again; + } =20 - if (alist.index > 0) - fprobe_set_ips(alist.addrs, alist.index, 1, 0); mutex_unlock(&fprobe_mutex); =20 kfree(alist.addrs); From nobody Tue Jun 16 02:39:30 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 8196233FE0A; Wed, 15 Apr 2026 10:09:16 +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=1776247756; cv=none; b=lHIVdukgxluFm2XTOW/E0k8M9RTXBpfPLuyK3YpnQkqqBXSIl1lT71+I/XuMxPPmEkvkdaccI7KbXh74T/YtggsnasgbfP3oC1fW+AGMM9CX+5xzBOh9ULeXBzHilG46QSqZLRUgX7kqCXg7M+0gsKwmAvOXg6kV+sng5CRXlwE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776247756; c=relaxed/simple; bh=VT2viwFLFCcmv2oXd/4RUZgiiKcOVKNA6CB6ObYWgcw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=H9/KSNTWEgJtXqSx8bBBKXIS87Ii0JGVTatKppQrnhrpmjv0xmw91ivT7KtA6/a3wGUpItPZu+wgVZQrbOztUO9NEDZXuUChU0lDZrvEUyiar7p4+fih4ooH+H92A4VXmIkt6YgGVKQEB8klJq/nhB6dNsAIs0n2Q8X/iz5Y4RA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eCZr9krS; 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="eCZr9krS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B8BBC19424; Wed, 15 Apr 2026 10:09:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776247756; bh=VT2viwFLFCcmv2oXd/4RUZgiiKcOVKNA6CB6ObYWgcw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eCZr9krSbNY//7jeY/uy2Fm9DmpMY1lNBnSSWwC5hZ01j/J7VWZYL54DymGk3poN0 74c9taE7kL00J5Y69CA0LEQlCWfu33ZmHykG/fyTdjK2Vhrdl3jXqrtQJT8N9x/yOm Okhg+097+JgVnBi8cLUEn9FqaompzYhXdSZedT41Q1VyK+dsUXAy1U+G62K+5TszMw bIt7Dvsuwe/dh3OAyyVeyhkPmJXwXLEqQjYoOM/9ANzIWtQFmKTCW7ncuKp+VzK9d+ HVbvwqGDsBbYCzf8mxXnx418Fqedl8gTaR6VXSJC9fCf84Cc3Y6zPc2LU7oJ00pme0 x5hWDBKO4eRIw== 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 v7 5/6] tracing/fprobe: Check the same type fprobe on table as the unregistered one Date: Wed, 15 Apr 2026 19:09:13 +0900 Message-ID: <177624775355.2407691.15569738415712795834.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.513.gad8abe7a5a-goog In-Reply-To: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> References: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> 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 | 82 +++++++++++++++++++++++++++++++++++++++------= ---- 1 file changed, 65 insertions(+), 17 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 0df8e003e88e..8a5601bf2330 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 */ @@ -342,6 +332,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) { @@ -364,6 +380,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) { @@ -552,18 +591,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 @@ -931,7 +977,9 @@ static int unregister_fprobe_nolock(struct fprobe *fp) /* 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]) && 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); From nobody Tue Jun 16 02:39:30 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 9D90A345CA5; Wed, 15 Apr 2026 10:09:24 +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=1776247764; cv=none; b=KlX0bkkEQnzhYZxBA0hD//ZpBX1Rdo7BraqZJmqb88Npfgz+yNuSnuHmuTA1wx0cRqe6hPn6fAHtdXJzukaWFsPtv4qRc7zDEqtOXZZpbFL0Sizo4NFJMuSl3kE8txxZToImnMmiMQV7suCrgG2ZhKlMvVuaROuzS6kyBBfeS38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776247764; c=relaxed/simple; bh=A6kqK+GlVusk8buhMSCmQFw5rBo1+ztzcU/d1/28lbA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Yyk9uoiSQgxlQahImah/2NCWGGSn02F/Nv2hZp/88ezzju6qqCaBwk6hhiziy9ecyRt9+v8/9Sh7sa5auV6AAoW9QbdB9j63fDNTaTBi2MBaW+llnNY0mGecQojeqzquIklaVwSbm1c0lTSCHHYnfwlO9ig92UgYi4/+qJKaczI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ojcwFdo/; 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="ojcwFdo/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B2F0C19424; Wed, 15 Apr 2026 10:09:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776247764; bh=A6kqK+GlVusk8buhMSCmQFw5rBo1+ztzcU/d1/28lbA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ojcwFdo/K8TB+KhX4hZf1OoEswQFlp8vU1y9Nd+L5EWL+5y6cbWGKPTUxWQGyMzle 4Uqi+U64/KKIwGC7ixoDn4vUvIJl8kcJO9FsetTvmvqSoGuSm4QgsRlXtuPuxyeWzY D2I0Do3aPGExVtGC7go9FrYAMOxDTTOZROOxg4HLewGcYP1kOaQHyYu9dtXmvysx/P QLSSbamVladAillmSC5SpAAw68Ecc57TNJ2Bi+bLe2xa717b5PvN4AATHRgIRFAfSd ovpkkHdFC0dOUUfp2dSzI8N41x6ilJFmeSlwfiTMpSG7dVjvANpCwsjFP0jeM6pm1B zGk+4zBtWQehw== 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 v7 6/6] tracing/fprobe: Fix to unregister ftrace_ops if it is empty on module unloading Date: Wed, 15 Apr 2026 19:09:21 +0900 Message-ID: <177624776142.2407691.5687670949567786266.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.513.gad8abe7a5a-goog In-Reply-To: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> References: <177624771150.2407691.15764846647014540969.stgit@mhiramat.tok.corp.google.com> 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) Fix fprobe to unregister ftrace_ops if corresponding type of fprobe does not exist on the fprobe_ip_table and it is expected to be empty when unloading modules. Since ftrace thinks that the empty hash means everything to be traced, if we set fprobes only on the unloaded module, all functions are traced unexpectedly after unloading module. e.g. # modprobe xt_LOG.ko # echo 'f:test log_tg*' > dynamic_events # echo 1 > events/fprobes/test/enable # cat enabled_functions log_tg [xt_LOG] (1) tramp: 0xffffffffa0004000 (fprobe_ftrace_en= try+0x0/0x490) ->fprobe_ftrace_entry+0x0/0x490 log_tg_check [xt_LOG] (1) tramp: 0xffffffffa0004000 (fprobe_f= trace_entry+0x0/0x490) ->fprobe_ftrace_entry+0x0/0x490 log_tg_destroy [xt_LOG] (1) tramp: 0xffffffffa0004000 (fprobe_f= trace_entry+0x0/0x490) ->fprobe_ftrace_entry+0x0/0x490 # rmmod xt_LOG # wc -l enabled_functions 34085 enabled_functions Signed-off-by: Masami Hiramatsu (Google) --- Changes in v7: - Fix to split checking whether ftrace_ops is registered from the number of registered fprobes, because ftrace_ops can be unregistered in module unloading. Changes in v6: - Newly added. --- kernel/trace/fprobe.c | 214 ++++++++++++++++++++++++++++++++++++++-------= ---- 1 file changed, 165 insertions(+), 49 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 8a5601bf2330..3e3df2e13c06 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -79,7 +79,7 @@ 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, struct fprob= e *fp) +static int __insert_fprobe_node(struct fprobe_hlist_node *node, struct fpr= obe *fp) { int ret; =20 @@ -92,7 +92,7 @@ static int insert_fprobe_node(struct fprobe_hlist_node *n= ode, struct fprobe *fp) return ret; } =20 -static void delete_fprobe_node(struct fprobe_hlist_node *node) +static void __delete_fprobe_node(struct fprobe_hlist_node *node) { lockdep_assert_held(&fprobe_mutex); =20 @@ -250,7 +250,74 @@ static inline int __fprobe_kprobe_handler(unsigned lon= g ip, unsigned long parent return ret; } =20 +static int fprobe_fgraph_entry(struct ftrace_graph_ent *trace, struct fgra= ph_ops *gops, + struct ftrace_regs *fregs); +static void fprobe_return(struct ftrace_graph_ret *trace, + struct fgraph_ops *gops, + struct ftrace_regs *fregs); + +static struct fgraph_ops fprobe_graph_ops =3D { + .entryfunc =3D fprobe_fgraph_entry, + .retfunc =3D fprobe_return, +}; +/* Number of fgraph fprobes */ +static int fprobe_graph_active; +/* Number of fgraph fprobe nodes */ +static int nr_fgraph_fprobes; +/* Is fprobe_graph_ops registered? */ +static bool fprobe_graph_registered; + +/* Add @addrs to the ftrace filter and register fgraph if needed. */ +static int fprobe_graph_add_ips(unsigned long *addrs, int num) +{ + int ret; + + lockdep_assert_held(&fprobe_mutex); + + ret =3D ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 0, 0); + if (ret) + return ret; + + if (!fprobe_graph_active) { + if (WARN_ON_ONCE(fprobe_graph_registered)) + return -EINVAL; + ret =3D register_ftrace_graph(&fprobe_graph_ops); + if (WARN_ON_ONCE(ret)) { + ftrace_free_filter(&fprobe_graph_ops.ops); + return ret; + } + fprobe_graph_registered =3D true; + } + fprobe_graph_active++; + return 0; +} + +static void __fprobe_graph_unregister(void) +{ + if (fprobe_graph_registered) { + unregister_ftrace_graph(&fprobe_graph_ops); + ftrace_free_filter(&fprobe_graph_ops.ops); + fprobe_graph_registered =3D false; + } +} + +/* Remove @addrs from the ftrace filter and unregister fgraph if possible.= */ +static void fprobe_graph_remove_ips(unsigned long *addrs, int num) +{ + lockdep_assert_held(&fprobe_mutex); + + if (!fprobe_graph_active) + return; + + fprobe_graph_active--; + if (!fprobe_graph_active) + __fprobe_graph_unregister(); + else if (num) + ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0); +} + #if defined(CONFIG_DYNAMIC_FTRACE_WITH_ARGS) || defined(CONFIG_DYNAMIC_FTR= ACE_WITH_REGS) + /* ftrace_ops callback, this processes fprobes which have only entry_handl= er. */ static void fprobe_ftrace_entry(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *ops, struct ftrace_regs *fregs) @@ -293,7 +360,12 @@ static struct ftrace_ops fprobe_ftrace_ops =3D { .func =3D fprobe_ftrace_entry, .flags =3D FTRACE_OPS_FL_SAVE_ARGS, }; +/* Number of fgraph fprobes */ static int fprobe_ftrace_active; +/* Number of ftrace fprobe nodes */ +static int nr_ftrace_fprobes; +/* Is fprobe_ftrace_ops registered? */ +static bool fprobe_ftrace_registered; =20 static int fprobe_ftrace_add_ips(unsigned long *addrs, int num) { @@ -306,24 +378,40 @@ static int fprobe_ftrace_add_ips(unsigned long *addrs= , int num) return ret; =20 if (!fprobe_ftrace_active) { + if (WARN_ON_ONCE(fprobe_ftrace_registered)) + return -EINVAL; + ret =3D register_ftrace_function(&fprobe_ftrace_ops); if (ret) { ftrace_free_filter(&fprobe_ftrace_ops); return ret; } + fprobe_ftrace_registered =3D true; } fprobe_ftrace_active++; return 0; } =20 +static void __fprobe_ftrace_unregister(void) +{ + if (fprobe_ftrace_registered) { + unregister_ftrace_function(&fprobe_ftrace_ops); + ftrace_free_filter(&fprobe_ftrace_ops); + fprobe_ftrace_registered =3D false; + } +} + static void fprobe_ftrace_remove_ips(unsigned long *addrs, int num) { lockdep_assert_held(&fprobe_mutex); =20 + if (!fprobe_ftrace_active) + return; + fprobe_ftrace_active--; if (!fprobe_ftrace_active) - unregister_ftrace_function(&fprobe_ftrace_ops); - if (num) + __fprobe_ftrace_unregister(); + else if (num) ftrace_set_filter_ips(&fprobe_ftrace_ops, addrs, num, 1, 0); } =20 @@ -332,6 +420,40 @@ static bool fprobe_is_ftrace(struct fprobe *fp) return !fp->exit_handler; } =20 +/* Node insertion and deletion requires the fprobe_mutex */ +static int insert_fprobe_node(struct fprobe_hlist_node *node, struct fprob= e *fp) +{ + int ret; + + lockdep_assert_held(&fprobe_mutex); + + ret =3D __insert_fprobe_node(node, fp); + if (!ret) { + if (fprobe_is_ftrace(fp)) + nr_ftrace_fprobes++; + else + nr_fgraph_fprobes++; + } + + return ret; +} + +static void delete_fprobe_node(struct fprobe_hlist_node *node) +{ + struct fprobe *fp; + + lockdep_assert_held(&fprobe_mutex); + + fp =3D READ_ONCE(node->fp); + if (fp) { + if (fprobe_is_ftrace(fp)) + nr_ftrace_fprobes--; + else + nr_fgraph_fprobes--; + } + __delete_fprobe_node(node); +} + static bool fprobe_exists_on_hash(unsigned long ip, bool ftrace) { struct rhlist_head *head, *pos; @@ -361,8 +483,15 @@ static bool fprobe_exists_on_hash(unsigned long ip, bo= ol ftrace) #ifdef CONFIG_MODULES static void fprobe_remove_ips(unsigned long *ips, unsigned int cnt) { - ftrace_set_filter_ips(&fprobe_graph_ops.ops, ips, cnt, 1, 0); - ftrace_set_filter_ips(&fprobe_ftrace_ops, ips, cnt, 1, 0); + if (!nr_fgraph_fprobes) + __fprobe_graph_unregister(); + else + ftrace_set_filter_ips(&fprobe_graph_ops.ops, ips, cnt, 1, 0); + + if (!nr_ftrace_fprobes) + __fprobe_ftrace_unregister(); + else + ftrace_set_filter_ips(&fprobe_ftrace_ops, ips, cnt, 1, 0); } #endif #else @@ -380,6 +509,32 @@ static bool fprobe_is_ftrace(struct fprobe *fp) return false; } =20 +/* Node insertion and deletion requires the fprobe_mutex */ +static int insert_fprobe_node(struct fprobe_hlist_node *node, struct fprob= e *fp) +{ + int ret; + + lockdep_assert_held(&fprobe_mutex); + + ret =3D __insert_fprobe_node(node, fp); + if (!ret) + nr_fgraph_fprobes++; + + return ret; +} + +static void delete_fprobe_node(struct fprobe_hlist_node *node) +{ + struct fprobe *fp; + + lockdep_assert_held(&fprobe_mutex); + + fp =3D READ_ONCE(node->fp); + if (fp) + nr_fgraph_fprobes--; + __delete_fprobe_node(node); +} + static bool fprobe_exists_on_hash(unsigned long ip, bool ftrace __maybe_un= used) { struct rhlist_head *head, *pos; @@ -406,7 +561,10 @@ static bool fprobe_exists_on_hash(unsigned long ip, bo= ol ftrace __maybe_unused) #ifdef CONFIG_MODULES static void fprobe_remove_ips(unsigned long *ips, unsigned int cnt) { - ftrace_set_filter_ips(&fprobe_graph_ops.ops, ips, cnt, 1, 0); + if (!nr_fgraph_fprobes) + __fprobe_graph_unregister(); + else + ftrace_set_filter_ips(&fprobe_graph_ops.ops, ips, cnt, 1, 0); } #endif #endif /* !CONFIG_DYNAMIC_FTRACE_WITH_ARGS && !CONFIG_DYNAMIC_FTRACE_WITH_= REGS */ @@ -536,48 +694,6 @@ static void fprobe_return(struct ftrace_graph_ret *tra= ce, } NOKPROBE_SYMBOL(fprobe_return); =20 -static struct fgraph_ops fprobe_graph_ops =3D { - .entryfunc =3D fprobe_fgraph_entry, - .retfunc =3D fprobe_return, -}; -static int fprobe_graph_active; - -/* Add @addrs to the ftrace filter and register fgraph if needed. */ -static int fprobe_graph_add_ips(unsigned long *addrs, int num) -{ - int ret; - - lockdep_assert_held(&fprobe_mutex); - - ret =3D ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 0, 0); - if (ret) - return ret; - - if (!fprobe_graph_active) { - ret =3D register_ftrace_graph(&fprobe_graph_ops); - if (WARN_ON_ONCE(ret)) { - ftrace_free_filter(&fprobe_graph_ops.ops); - return ret; - } - } - fprobe_graph_active++; - return 0; -} - -/* Remove @addrs from the ftrace filter and unregister fgraph if possible.= */ -static void fprobe_graph_remove_ips(unsigned long *addrs, int num) -{ - lockdep_assert_held(&fprobe_mutex); - - fprobe_graph_active--; - /* Q: should we unregister it ? */ - if (!fprobe_graph_active) - unregister_ftrace_graph(&fprobe_graph_ops); - - if (num) - ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0); -} - #ifdef CONFIG_MODULES =20 #define FPROBE_IPS_BATCH_INIT 128