From nobody Thu Dec 18 10:35:27 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1CDF8C4167B for ; Fri, 8 Dec 2023 10:27:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573511AbjLHK13 (ORCPT ); Fri, 8 Dec 2023 05:27:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573605AbjLHK07 (ORCPT ); Fri, 8 Dec 2023 05:26:59 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A6151BC3 for ; Fri, 8 Dec 2023 02:26:47 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D4C1CC433C8; Fri, 8 Dec 2023 10:26:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702031206; bh=cuVa4qIlG+/ZLno0EmFW6if4LFY8jZFXE8ZWIFfPmHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Igya/mQ+ywbmAkJR9GvJobt6tOa9toUGUERjH95OL6YYsdJgcBaMfdysnIcCIYJLq XK8eD3Hw7rQJzOlPstXQdl4z1eK1b0xXDSr66+hcbRxqTYxmMKQ6yr4z2W6Y8ZR3Fr vr+6lMN+f5Hh6yj1cMImOWvesjL2nXCpXdK8PvL5svmeGilyM9CdyBzgjl5e27cKg0 /F1g9Z8CAk3hd+VnIA7J6kHj9iIWOh85qUsjDNEwrAZjdk/QmfmUVRVurbmI9AGEKo f/gMiO/+L2R/ovvVGVAQjY6m7vcQ87Xv8sLYrh9AFENuMJmmKR5nk52SF1HcW9KiFS QtB+XUBud20og== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Sven Schnelle , Alexei Starovoitov , Jiri Olsa , Arnaldo Carvalho de Melo , Daniel Borkmann , Alan Maguire , Mark Rutland , Peter Zijlstra , Thomas Gleixner , Guo Ren Subject: [PATCH v4 12/33] function_graph: Use a simple LRU for fgraph_array index number Date: Fri, 8 Dec 2023 19:26:40 +0900 Message-Id: <170203119964.579004.13369344037111885313.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <170203105427.579004.8033550792660734570.stgit@devnote2> References: <170203105427.579004.8033550792660734570.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu (Google) Since the fgraph_array index is used for the bitmap on the shadow stack, it may leave some entries after a function_graph instance is removed. Thus if another instance reuses the fgraph_array index soon after releasing it, the fgraph may confuse to call the newer callback for the entries which are pushed by the older instance. To avoid reusing the fgraph_array index soon after releasing, introduce a simple LRU table for managing the index number. This will reduce the possibility of this confusion. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v4: - Newly added. --- kernel/trace/fgraph.c | 67 ++++++++++++++++++++++++++++++++++-----------= ---- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index 6f537ebd3ed7..d8b139959b62 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -99,10 +99,44 @@ enum { DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph); int ftrace_graph_active; =20 -static int fgraph_array_cnt; - static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE]; =20 +/* LRU index table for fgraph_array */ +static int fgraph_lru_table[FGRAPH_ARRAY_SIZE]; +static int fgraph_lru_next; +static int fgraph_lru_last; + +static void fgraph_lru_init(void) +{ + int i; + + for (i =3D 0; i < FGRAPH_ARRAY_SIZE; i++) + fgraph_lru_table[i] =3D i; +} + +static int fgraph_lru_release_index(int idx) +{ + if (idx < 0 || idx >=3D FGRAPH_ARRAY_SIZE || + fgraph_lru_table[fgraph_lru_last] !=3D -1) + return -1; + + fgraph_lru_table[fgraph_lru_last] =3D idx; + fgraph_lru_last =3D (fgraph_lru_last + 1) % FGRAPH_ARRAY_SIZE; + return fgraph_lru_last - 1; +} + +static int fgraph_lru_alloc_index(void) +{ + int idx =3D fgraph_lru_table[fgraph_lru_next]; + + if (idx =3D=3D -1) + return -1; + + fgraph_lru_table[fgraph_lru_next] =3D -1; + fgraph_lru_next =3D (fgraph_lru_next + 1) % FGRAPH_ARRAY_SIZE; + return idx; +} + static inline int get_ret_stack_index(struct task_struct *t, int offset) { return t->ret_stack[offset] & FGRAPH_RET_INDEX_MASK; @@ -367,7 +401,7 @@ int function_graph_enter(unsigned long ret, unsigned lo= ng func, if (index < 0) goto out; =20 - for (i =3D 0; i < fgraph_array_cnt; i++) { + for (i =3D 0; i < FGRAPH_ARRAY_SIZE; i++) { struct fgraph_ops *gops =3D fgraph_array[i]; =20 if (gops =3D=3D &fgraph_stub) @@ -932,21 +966,17 @@ int register_ftrace_graph(struct fgraph_ops *gops) /* The array must always have real data on it */ for (i =3D 0; i < FGRAPH_ARRAY_SIZE; i++) fgraph_array[i] =3D &fgraph_stub; + fgraph_lru_init(); } =20 - /* Look for an available spot */ - for (i =3D 0; i < FGRAPH_ARRAY_SIZE; i++) { - if (fgraph_array[i] =3D=3D &fgraph_stub) - break; - } - if (i >=3D FGRAPH_ARRAY_SIZE) { + i =3D fgraph_lru_alloc_index(); + if (i < 0 || + WARN_ON_ONCE(fgraph_array[i] !=3D &fgraph_stub)) { ret =3D -EBUSY; goto out; } =20 fgraph_array[i] =3D gops; - if (i + 1 > fgraph_array_cnt) - fgraph_array_cnt =3D i + 1; gops->idx =3D i; =20 ftrace_graph_active++; @@ -976,25 +1006,22 @@ int register_ftrace_graph(struct fgraph_ops *gops) void unregister_ftrace_graph(struct fgraph_ops *gops) { int command =3D 0; - int i; =20 mutex_lock(&ftrace_lock); =20 if (unlikely(!ftrace_graph_active)) goto out; =20 - if (unlikely(gops->idx < 0 || gops->idx >=3D fgraph_array_cnt)) + if (unlikely(gops->idx < 0 || gops->idx >=3D FGRAPH_ARRAY_SIZE)) + goto out; + + if (WARN_ON_ONCE(fgraph_array[gops->idx] !=3D gops)) goto out; =20 - WARN_ON_ONCE(fgraph_array[gops->idx] !=3D gops); + if (fgraph_lru_release_index(gops->idx) < 0) + goto out; =20 fgraph_array[gops->idx] =3D &fgraph_stub; - if (gops->idx + 1 =3D=3D fgraph_array_cnt) { - i =3D gops->idx; - while (i >=3D 0 && fgraph_array[i] =3D=3D &fgraph_stub) - i--; - fgraph_array_cnt =3D i + 1; - } =20 ftrace_graph_active--;