From patchwork Mon Dec 18 13:13:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 180388 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1232859dyi; Mon, 18 Dec 2023 05:19:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHLDc2db5EZoZfjtY2c2QzpMOC7IcJhXscXtDYEyLHn5zlHkw4UN1yp+WI7o/1F3MXzi2jp X-Received: by 2002:a05:6122:2a45:b0:4b6:daf4:4cf7 with SMTP id fx5-20020a0561222a4500b004b6daf44cf7mr364300vkb.18.1702905551591; Mon, 18 Dec 2023 05:19:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702905551; cv=none; d=google.com; s=arc-20160816; b=tBQZvetK6KtMRWt0YkKfHGto4Iv9mqeMpnsybjFY91u16bqFL2rWvZZR4nibarYBEP AQfFFtKXamceBaFrn+KIqs7zAt5yvcWrfU09A4QeXNUQg9lIzsPtcIp5uheHxxHdEohl UmTzXpokCtQYtHw8PvdxB9FBo2evJ8cnKPWoTGstgdRCMR0rh3vI/w7xkif4cT3RHyWA FGS7c66G0UsyN0Xed8D3CCuxYt+JVT3EUUUwoBuk/tvgCYkCqCSBKCsqtJZ3Tw9+d7tj xi3RO7vbMpCEGPKlsFT1ZJH/urSEh+Z4rm6yJp0EWKTaU2YWoKO35RSva40/6sSNIcKP CZKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=Vs7KTQCLbsA/6vvy+t/qm4l9ZKzTZj2wyPwJpKfMbkE=; fh=SIgps5XdV0XNwjZfT2uAI7g3mrspDldK9Qs8qQAfoa4=; b=GWx2H2L1GVz/hMO5UvnlTxJZBJV1HIinIB9sm6ZGQe9cP/jvsLzsBjXCgiAVP3PPY6 fumOUZUnyPVzaBHwe+oE4aOruelTut8WHB694lssRit4QosLS7Bbq47j0VB/T6VbK+se JlvfO8oMtiyVJvkGytD8VAHOHCOEPqBfvJocfIXIN4BXiWftTeT/n3Q+BQZ9JnGyGuea dvjQoMLdV0P/9clgfy+q5IKglRVMxbFq5yNzqYVM3D9FMw6q7J52ia/r58k4siZ6MEnn PexjrQal/gud/UxjC0NqEOHgPg9FdBTlJdJnL80PQG/KuVy94JIzx0CP8IAck4vQBlrq M1QA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eeujou5T; spf=pass (google.com: domain of linux-kernel+bounces-3716-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3716-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id j18-20020ab02512000000b007cbe9ad3cabsi89948uan.62.2023.12.18.05.19.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 05:19:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3716-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eeujou5T; spf=pass (google.com: domain of linux-kernel+bounces-3716-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3716-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 503731C24590 for ; Mon, 18 Dec 2023 13:19:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E5BE171445; Mon, 18 Dec 2023 13:14:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="eeujou5T" X-Original-To: linux-kernel@vger.kernel.org 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 118A31D13E; Mon, 18 Dec 2023 13:14:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9C75C433C9; Mon, 18 Dec 2023 13:14:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702905244; bh=f7QMtwuppQrO3+oBbRegUPlFoOPH8Ztq+kJKsy45dPk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eeujou5TO4AdUpY3OsgpiVB2uhkOWtBBQ3yjoNza57xrK9Q5yYMGJtjl8LgiscAMj OvVj+oOgWEMNdJ6L60T7/u2OAKFYVK3fCNbBYl6Qa/R7vftNgyJSXh0sotsbkCd9Lt eVqDFswqMfYCpF0m3tR3GmvGeD5S/yDpR1xaLY6h9ieTpUpNvVmkZW6MQtSlTmcDk0 +QLyBBMtKiSI0Gygb7lnwgkHltoJbEEHRI7X0Oh9YCT39F+YtqyYqw9RmPgQ4FYo/w Y0CsCYYQbz2CM4bwHwF7QY5koPcwbLUeVJd1UHX3NsPfooLh7MgMPAc7WEIEHCNE8x BpMIIsaQd79ng== 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 v5 12/34] function_graph: Use a simple LRU for fgraph_array index number Date: Mon, 18 Dec 2023 22:13:58 +0900 Message-Id: <170290523774.220107.15908166798566272758.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <170290509018.220107.1347127510564358608.stgit@devnote2> References: <170290509018.220107.1347127510564358608.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785625891579677084 X-GMAIL-MSGID: 1785625891579677084 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 v5: - Fix the underflow bug in fgraph_lru_release_index() and return 0 if the release is succeded. 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..aa5e4ec9fbb2 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; -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; + +static void fgraph_lru_init(void) +{ + int i; + + for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) + fgraph_lru_table[i] = i; +} + +static int fgraph_lru_release_index(int idx) +{ + if (idx < 0 || idx >= FGRAPH_ARRAY_SIZE || + 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; +} + +static int fgraph_lru_alloc_index(void) +{ + int idx = fgraph_lru_table[fgraph_lru_next]; + + if (idx == -1) + return -1; + + fgraph_lru_table[fgraph_lru_next] = -1; + fgraph_lru_next = (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 long func, if (index < 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) @@ -932,21 +966,17 @@ 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 = -EBUSY; goto out; } fgraph_array[i] = gops; - if (i + 1 > fgraph_array_cnt) - fgraph_array_cnt = i + 1; gops->idx = i; 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 = 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)) + goto out; + + if (WARN_ON_ONCE(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--;