From nobody Fri Feb 13 03:15:19 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 EC4591494B5; Tue, 4 Jun 2024 14:42: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=1717512137; cv=none; b=Lw9cKaR/RJWksKRevbnsp9X6gRG6hqraOVLZG0cHp1YHy5++xgvG2pV1zz/ATKKA9IPXERm5vKFtr6Kpv2QhHiK9P3brFg93fSMYmgQH4ea5OO/2ZZDbb0Wye1hOuAQyW1PuI7MbJl7KWuUdpbMPcSeTsWk0t0FfKCMR69TYcUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717512137; c=relaxed/simple; bh=GhPHXHy8Ud+g8dqlhaVHKW3UEVIvMOVxP1EAWCQ8Nj0=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=YJkcdgymJzNoALnSE4wR0bBKC8Yk4lU0c+M18GXoXeC8HxuX3/ABsXDIUqIKNLzVME2077/mn2nAc2cVALSzwbikrDpYn+VpQ1d0EymjBK0KF8LYPHlPkZ3C9WrhwjovDtieBSaQZj5CNoSAS8BrOIxauIy8XurDXm//rgoZZkU= 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 D4A6AC4AF1B; Tue, 4 Jun 2024 14:42:16 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1sEVMe-00000000Z01-1Xsf; Tue, 04 Jun 2024 10:42:16 -0400 Message-ID: <20240604144216.229580890@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 04 Jun 2024 10:41:17 -0400 From: Steven Rostedt To: linux-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: [for-next][PATCH 14/27] function_graph: Use a simple LRU for fgraph_array index number References: <20240604144103.293353991@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 Link: https://lore.kernel.org/linux-trace-kernel/20240603190823.147421545@g= oodmis.org Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Alexei Starovoitov Cc: Florent Revest Cc: Martin KaFai Lau Cc: bpf Cc: Sven Schnelle Cc: Alexei Starovoitov Cc: Jiri Olsa Cc: Arnaldo Carvalho de Melo Cc: Daniel Borkmann Cc: Alan Maguire Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Guo Ren 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