From nobody Fri Feb 13 06:06:05 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 714D42C69B; Sun, 2 Jun 2024 03:37:26 +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=1717299446; cv=none; b=f3WI6sgIoB5j7mT2WlRio2paBMVWe1ay+cadFYuygMwvTbMqLs5GBGKJ2PV0Kh0N32LNDwe5iKtaHCZl4DAF06lh9wjN83fd/9AzlBAP/YGAqJhd8pHQBOUe3rCQJq+AADXmfalZftbmQM4fdsZwHTin2NsHsWZQ4vl7h6PLJ00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717299446; c=relaxed/simple; bh=9LpGl/CYTHJuPvy0Cg8mag2iEBwvUFaJNUn1Vi1Mfk8=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=I09u4dmkBS2Rc4++0ml9tqewREsM8o5w2iMEt+/YwzYIAk7/EcCNTU1zmmn5xUlL/DLyBJP0xtA7uktLs6MWkwqlah2v539Odz1DILI8D58ITrxdanUtBrExlX0zjYwtcMNSPzFlFvgmUy1RKCuHIRb3xR9z/cGEhhB2s/RpQ3Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 357C9C3277B; Sun, 2 Jun 2024 03:37:26 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1sDc3F-000000094Pw-278P; Sat, 01 Jun 2024 23:38:33 -0400 Message-ID: <20240602033833.363962538@goodmis.org> User-Agent: quilt/0.68 Date: Sat, 01 Jun 2024 23:37:58 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Alexei Starovoitov , Florent Revest , Martin KaFai Lau , bpf , Sven Schnelle , Alexei Starovoitov , Jiri Olsa , Arnaldo Carvalho de Melo , Daniel Borkmann , Alan Maguire , Peter Zijlstra , Thomas Gleixner , Guo Ren Subject: [PATCH v2 14/27] function_graph: Use a simple LRU for fgraph_array index number References: <20240602033744.563858532@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Link: https://lore.kernel.org/linux-trace-kernel/171509103267.162236.688509= 7397289135378.stgit@devnote2 Signed-off-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- kernel/trace/fgraph.c | 71 ++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index 30bed20c655f..7fd9b03bd170 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -124,10 +124,48 @@ 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; + +/* Initialize fgraph_lru_table with unused index */ +static void fgraph_lru_init(void) +{ + int i; + + for (i =3D 0; i < FGRAPH_ARRAY_SIZE; i++) + fgraph_lru_table[i] =3D i; +} + +/* Release the used index to the LRU table */ +static int fgraph_lru_release_index(int idx) +{ + if (idx < 0 || idx >=3D FGRAPH_ARRAY_SIZE || + WARN_ON_ONCE(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 0; +} + +/* Allocate a new index from LRU table */ +static int fgraph_lru_alloc_index(void) +{ + int idx =3D fgraph_lru_table[fgraph_lru_next]; + + /* No id is available */ + 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; +} + /* Get the FRAME_OFFSET from the word from the @offset on ret_stack */ static inline int get_frame_offset(struct task_struct *t, int offset) { @@ -374,7 +412,7 @@ int function_graph_enter(unsigned long ret, unsigned lo= ng func, if (offset < 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) @@ -925,7 +963,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) { int command =3D 0; int ret =3D 0; - int i; + int i =3D -1; =20 mutex_lock(&ftrace_lock); =20 @@ -933,21 +971,16 @@ 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 -ENOSPC; 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++; @@ -975,6 +1008,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) fgraph_array[i] =3D &fgraph_stub; ftrace_graph_active--; gops->saved_func =3D NULL; + fgraph_lru_release_index(i); } out: mutex_unlock(&ftrace_lock); @@ -984,25 +1018,20 @@ 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 || + 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--; =20 --=20 2.43.0