From patchwork Sun Feb 25 15:17:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 206054 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1620895dyb; Sun, 25 Feb 2024 07:18:47 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXYAu6/JgN5p0Qe/YA1WgQkdvXTb4gSmvHkWKLtdtVEc8o9Ayc+5PR5HjRehVeUE0l5HQz4Pvk4QYJZC1DWaHVaKku1jA== X-Google-Smtp-Source: AGHT+IHX1oEpKT7TRQ9zexc1uGdOMB1o7Eg55Rf7+hp4dU6RoeuJISgCIiTAGR/s7ONt9sMG6Z4M X-Received: by 2002:a0c:e113:0:b0:68f:44eb:b944 with SMTP id w19-20020a0ce113000000b0068f44ebb944mr5044248qvk.15.1708874326826; Sun, 25 Feb 2024 07:18:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708874326; cv=pass; d=google.com; s=arc-20160816; b=QVEZZ7KGSZjoldTRw0rMsoh1nr/hMgt6Cv7kc579rPjOkOKHOLLQZR/tEHZGcdaEdQ 0u8kS/GvdQQpB47xWc+RogUsGHEwrBNfT++SNkU5CSfUtRsLyxcQZOmWp5RNFYYGNNKI i8zEXqypiv0ocDOEXjXAUy35sD4+HxhYSISd222s07OJdliv8RjSLLZfJrHNkGYxgu00 qJZtzz6CCkBbIajtIddwv+WWcYgaO/MJilgBRlz3KTw1jtD6uFDJrLhDNQGp8qg8rv+0 XlzaiT9BNxAT4kV7SxPyen9qrXtQ8jkqulzJLRCO9C8OfHNICvqyxdkv4NdfOI9yxPen 3YVw== ARC-Message-Signature: i=2; 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=hGFd+nhs/1MKjmeqpTudxnr0klIsyX1xMfNsf18q8xg=; fh=SIgps5XdV0XNwjZfT2uAI7g3mrspDldK9Qs8qQAfoa4=; b=0AdwNUGhU1ktD/XYpDW8MEP3MAap25xasPk/9Lsa6eygyjGXIj0ej7z2IetQUQGzCi TkSHqv7ZbZEYJjP9gQmQRFptAlIqdj74F5kITBXXHA7tYzj2cZKeAXvDpljQkAeYbUxd PkYhbuywA1r6ZCViiTg6f9ZSSYlvvhT32pXB8OhXXfIfhId/ZuHTZMb1GQmXdLv6gQrL jF4fUi2taJPSdfky2DoNRtmmK0S6wPRj+hSAr2CMWYPQL1hYwncxza39oJjYox2PH0ND +ERZmCU9JPaoVs8w4Fuq4aLSgiM68wL+Ub8QWfrVZ1X3mEGS44Jy2Rbz/o8lAo/uGRPb 3MoQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bwkIzM46; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-80141-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80141-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 p15-20020ad45f4f000000b0068fc86e7879si3220721qvg.612.2024.02.25.07.18.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 07:18:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80141-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=bwkIzM46; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-80141-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80141-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 8DE751C20850 for ; Sun, 25 Feb 2024 15:18:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 16BE8199AB; Sun, 25 Feb 2024 15:17:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bwkIzM46" 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 06ABE18EA2; Sun, 25 Feb 2024 15:17:31 +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=1708874251; cv=none; b=tGKOMcpEOQ57Vg8tn4RPcBmh0tNFMAH5dm+jN+8wa/6RzBIBirr894SW9tWC7m7e622zpKqAeAMns3FDKUDGXwzIYCmghzCIehybhaFXY3eQR+aT83RnnJSJDy07XpUyZ/fvxXtK81Pc/P5mgCYOz31vYCQcuRVLh3avCiTC45s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708874251; c=relaxed/simple; bh=zY1lZ8MZ8U9F3DpQIB4h84/gioR/3PWPTMDRQesiD6A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=dWhtF4ClUwELnc+MzOW+vhP+DCslBpAt8xV5xkhpjkoOEO2vK8ZknMq6tDh0/3Q5jXgf4qR5xficpK7ii9+aiwfmpro2nJl5jdtgc0rIWpw6IEXqMFX4zhkq7iO93HEG4WahX43NIgtQ4xhLL5B/gWXD46Do7vC2imvHwOuHdWw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bwkIzM46; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83FB0C433C7; Sun, 25 Feb 2024 15:17:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708874250; bh=zY1lZ8MZ8U9F3DpQIB4h84/gioR/3PWPTMDRQesiD6A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bwkIzM46UObQsqWkG5HS5vti3Txt9yvE1Fk3VorgTiJd3aDbXosni9JTZajLjwuup JtN1wBLiOnxXL6vcf2tDJpkaMqrtDoYsPeHuISlNEo1QRrbIu5mwHBBoYV2OtQLCLQ 1XetQbQE4jxbXlB+qRYcSUsh0rlSg76sOoXV0omz1g7LFIj9Ew6We2XuP5FdbeyWsl +ku3zLpDcsEqbbcjMXsbJq1K/UalFsfEHqmkkMtuevTMaTCFc7SjDhon+RuKcT/o/M juee10q90x4kdIVYZwIdyaA0PhMuSuu00VE/CPjU9aSa0gRf8I1Y4iiFbWlVkwAlik R7Fvw5lG1i73Q== 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 v8 13/35] function_graph: Use a simple LRU for fgraph_array index number Date: Mon, 26 Feb 2024 00:17:25 +0900 Message-Id: <170887424513.564249.7565785232831569172.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <170887410337.564249.6360118840946697039.stgit@devnote2> References: <170887410337.564249.6360118840946697039.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: 1791884606331336381 X-GMAIL-MSGID: 1791884606331336381 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 v8: - Add a WARN_ON_ONCE() if fgraph_lru_table[] is broken when releasing index, and remove WARN_ON_ONCE() from unregister_ftrace_graph() - Fix to release allocated index if register_ftrace_graph() fails. - Add comments and code cleanup. 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 | 72 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index ae42de909845..da5f269ce718 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -99,10 +99,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; +} + static inline int get_ret_stack_index(struct task_struct *t, int offset) { return t->ret_stack[offset] & FGRAPH_RET_INDEX_MASK; @@ -367,7 +405,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) @@ -919,7 +957,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) { int command = 0; int ret = 0; - int i; + int i = -1; mutex_lock(&ftrace_lock); @@ -935,21 +973,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 = -EBUSY; goto out; } fgraph_array[i] = gops; - if (i + 1 > fgraph_array_cnt) - fgraph_array_cnt = i + 1; gops->idx = i; ftrace_graph_active++; @@ -972,6 +1005,8 @@ int register_ftrace_graph(struct fgraph_ops *gops) ret = ftrace_startup(&gops->ops, command); out: + if (ret && i >= 0) + fgraph_lru_release_index(i); mutex_unlock(&ftrace_lock); return ret; } @@ -979,25 +1014,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--;