Received: by 2002:a05:6500:1b41:b0:1fb:d597:ff75 with SMTP id cz1csp161984lqb; Tue, 4 Jun 2024 07:59:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXBEPGK0vOfLPn/y2O1pV4M+cqWjAYFEIC+xdtORe9kQqGAcSh3vz7Yu3et6sA3GnP7IK7N8dmZgyaVd2si4et+q73PDEcTfVjwP4wozg== X-Google-Smtp-Source: AGHT+IG43KcpnkCkLHIr7+zJh+6pKN31tsH/O1l4a5Tq1rTiqxAWc/uXNYpOP/OjLQjI9BEDD4AQ X-Received: by 2002:a17:902:f34b:b0:1f4:5278:5c19 with SMTP id d9443c01a7336-1f6370bcd43mr91505235ad.49.1717513142583; Tue, 04 Jun 2024 07:59:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717513142; cv=pass; d=google.com; s=arc-20160816; b=ZvOx4/EtGcWcv91542IVOzq34nnRcFiZic88MA4uTEsuBMxMaWhgUE0JgyN2FzDD1Y nr9JzFjJVse/6G3KVSCEqqs4hCfta82aTJ4gPszYX+YWUWUaSSQfysEfOJattTnUodle XtDAbBBX97jAfhTxmUtAgkNISBMC3ErEqmPNvSmGaOu7nwJRmTf6QrwgAb2m3vNBS0nG OWW/GxCxa6qB1y0C+2yU2+P6li8yqTPgVozJRk+cDvcfKrsH4PjSFXNsx/r2RcAZy3F/ +Za+YWT7jeJmfYpu/esfGs3KxxQW98oy2rZg7wK9HuR4jaEki//CZhDW0BvUBrTX7ldk UFPQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=aRA3ux8a67oUgQ5e8JQFkGTLq671oMcXym6FHUjNORc=; fh=/wy5e/q+z2qlOMkv0QH/u80KV4ZXPtz68q6c1OyjqGY=; b=kEGGR8P1WDiiAkKxbrwQ8z2tS1311D/aceItX1E89RRBWzt8xAiBPyc8MOQWXIrcF9 64n8whEKcVEQyT7WepEXQCPONg+B22+yKantSs0Jgq+7OiyxJFj9mEM20mDgV7mS8pr5 MgraKI5BeelPHHlIlHqmLjNShzdtQWDer7VngSTvi75b7P1BUEWHK7Wxs5jaCOm8KP5S l0bFxdO/+6HVjTT7rfvJTburmIZzY50y5X7Ty+ggr8Vhps2WAB/XVMYGuvGMsFZax1Bl OprGzRZGNL29NylC7Y6gcmLGGI3tI1hMi5dFS8qkzXubBbLTaJJmwgbuFwWjXaPRzQ5C p5mg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-200875-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200875-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f651f55650si61744555ad.445.2024.06.04.07.59.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 07:59:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-200875-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-200875-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200875-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 9F2DD2865D5 for ; Tue, 4 Jun 2024 14:46:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 480B914A0A7; Tue, 4 Jun 2024 14:42:21 +0000 (UTC) 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-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.6885097397289135378.stgit@devnote2 Link: https://lore.kernel.org/linux-trace-kernel/20240603190823.147421545@goodmis.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; -static int fgraph_array_cnt; - static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE]; +/* 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 = 0; i < FGRAPH_ARRAY_SIZE; i++) + fgraph_lru_table[i] = i; +} + +/* Release the used index to the LRU table */ +static int fgraph_lru_release_index(int idx) +{ + if (idx < 0 || idx >= FGRAPH_ARRAY_SIZE || + WARN_ON_ONCE(fgraph_lru_table[fgraph_lru_last] != -1)) + return -1; + + fgraph_lru_table[fgraph_lru_last] = idx; + fgraph_lru_last = (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 = fgraph_lru_table[fgraph_lru_next]; + + /* No id is available */ + if (idx == -1) + return -1; + + fgraph_lru_table[fgraph_lru_next] = -1; + fgraph_lru_next = (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 long func, if (offset < 0) goto out; - for (i = 0; i < fgraph_array_cnt; i++) { + for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) { struct fgraph_ops *gops = fgraph_array[i]; if (gops == &fgraph_stub) @@ -925,7 +963,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) { int command = 0; int ret = 0; - int i; + int i = -1; mutex_lock(&ftrace_lock); @@ -933,21 +971,16 @@ int register_ftrace_graph(struct fgraph_ops *gops) /* The array must always have real data on it */ for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) fgraph_array[i] = &fgraph_stub; + fgraph_lru_init(); } - /* Look for an available spot */ - for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) { - if (fgraph_array[i] == &fgraph_stub) - break; - } - if (i >= FGRAPH_ARRAY_SIZE) { + i = fgraph_lru_alloc_index(); + if (i < 0 || WARN_ON_ONCE(fgraph_array[i] != &fgraph_stub)) { ret = -ENOSPC; goto out; } fgraph_array[i] = gops; - if (i + 1 > fgraph_array_cnt) - fgraph_array_cnt = i + 1; gops->idx = i; ftrace_graph_active++; @@ -975,6 +1008,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) fgraph_array[i] = &fgraph_stub; ftrace_graph_active--; gops->saved_func = 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 = 0; - int i; mutex_lock(&ftrace_lock); if (unlikely(!ftrace_graph_active)) goto out; - if (unlikely(gops->idx < 0 || gops->idx >= fgraph_array_cnt)) + if (unlikely(gops->idx < 0 || gops->idx >= FGRAPH_ARRAY_SIZE || + fgraph_array[gops->idx] != gops)) goto out; - WARN_ON_ONCE(fgraph_array[gops->idx] != gops); + if (fgraph_lru_release_index(gops->idx) < 0) + goto out; fgraph_array[gops->idx] = &fgraph_stub; - if (gops->idx + 1 == fgraph_array_cnt) { - i = gops->idx; - while (i >= 0 && fgraph_array[i] == &fgraph_stub) - i--; - fgraph_array_cnt = i + 1; - } ftrace_graph_active--; -- 2.43.0