Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp1166602lqj; Mon, 3 Jun 2024 12:11:11 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWF4sXDPd5PN5/PRbqxISISZBT8MpnE+EqpWC5VO6Vb8uW/PyCaVLYQccabruGvv7RFoob1b3MLsb3Vo/qNakAxh4RloqO9opHydcjY+A== X-Google-Smtp-Source: AGHT+IHcpnJYYZpnThkVt+B0W8WZsMKRZMtgu2AvROf6M1G3Wn6CkLdIO9yLPC382+meko3hXL66 X-Received: by 2002:ac2:5927:0:b0:523:b7ec:a222 with SMTP id 2adb3069b0e04-52b896c4937mr6038726e87.51.1717441871285; Mon, 03 Jun 2024 12:11:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717441871; cv=pass; d=google.com; s=arc-20160816; b=SlbOjh304vE+lIbGZ2ByfqrbyqzxHC9vmlRwWqFYUN+HRSfdmFrIbPalI+2rwgt0+P 7tY6IgIshjDCUjEwKX+YMLIvRnYX8AX+XRnPBwST7A5m1lMw12Dd4Wiwu9VU9QSpYN3e HYQVsUEC2TApqZtfa2VSqV4Sx8LA8HSCRVhvaOEQJvGjl9UsLf99T+qO10OPFm70j7SJ bhw24rcDK6DHIZ4011RyWb8aMKFhT8m9TqUjokBCEmNXhndq2d1DDZdXW34m1so7xXPC HCwgWf65nXV7h7wbKwMkPq72JqeKSXbs8mwtzNXyFC55720gWDrgpACStWdRb5o+i20U HdLw== 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=0Httan0qDMkS6Z4iGPkwniaPWOoUXdBsFlxxsu8ktMo=; fh=YfcaVrAzBpSwi8dOlqHV4K1os6qdULs4cyXFG/DyLfE=; b=GdqSVSnj+kd2fnxcEpxdGayRKlFEgVp8aC3Hdj5dMj/6FX3Co5/X3c5XOVpowuymG3 2kMNzcBuSe0t2DCqxnJd1iNLQKAYKoPqAs4AK/bchUBf0RoYlQO0a3r1PlansMCbb11K fqGYb7GUaeBDxAGaUTmNoJ1C4iZm0bQx5d/WO3MZ9wLzmU/TYi02PLDslcB7AjTuSPm/ NAv2uMsApipTNNwxAOCBJS6ypIlp2Qxs+/ssEU/GlmKoTMcP6LFyPLHkxtpbSxsFdAeA UETO7QBkx5EiXcbMHWV1y8R3KfX2u9MWbGdggJsA+qjtIDqybb9u+VtofRZ0VGVGGNyz 7Lkg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-199612-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199612-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a68bcf710b4si288333266b.304.2024.06.03.12.11.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 12:11:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-199612-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-199612-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199612-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 8C9E51F25F1D for ; Mon, 3 Jun 2024 19:11:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9662113E03E; Mon, 3 Jun 2024 19:07:16 +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 55C6F13C9D0; Mon, 3 Jun 2024 19:07:13 +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=1717441633; cv=none; b=eZaojl70b/HkKBT0MUzSS2viS5vrTmRPux5uQ0Ee4RY07OZ3RG7YLS+DPQFrfgHC9ZeamkkqeMTKj1uJBLQ/4hNy/OXhm6tiRlZQF9MUCfUPCCgNxKMKIXKKFdUiO2Z+0GPhYr+oiDiE7oua2T4VMlEMO7DWGMADZPnQxFMNhuc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717441633; c=relaxed/simple; bh=9LpGl/CYTHJuPvy0Cg8mag2iEBwvUFaJNUn1Vi1Mfk8=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=U2z9fpcybj7gDCiwj1xcG/A0xprKnh/7RsUW2Fo5xy23bqVSSfTw/BQII0WEbcBdg9xgW3PZZ1Cu7HrTb6FhOZ0VU6EB/maNNBvU/mx4nmz/Yf+mO0hzIM6lqybdJ6bEhxXMcfgKt8APLLkzQdD7dP9hMY5W2Qxpfib3zHsR2Xk= 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 5AD7EC4AF55; Mon, 3 Jun 2024 19:07:12 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1sED2d-00000009Tsw-1DHN; Mon, 03 Jun 2024 15:08:23 -0400 Message-ID: <20240603190823.147421545@goodmis.org> User-Agent: quilt/0.68 Date: Mon, 03 Jun 2024 15:07:18 -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 v3 14/27] function_graph: Use a simple LRU for fgraph_array index number References: <20240603190704.663840775@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 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