From patchwork Tue Feb 6 15:10:04 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: 197519 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1624496dyb; Tue, 6 Feb 2024 07:38:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IH+yNb6gHk92RDAe2mEx3Uq6PlFY99XHFhaqUVn4vlzPFxABFBxk0FVfBZcbBHRAPIG/jti X-Received: by 2002:a17:903:120f:b0:1d9:4919:ed10 with SMTP id l15-20020a170903120f00b001d94919ed10mr2386101plh.22.1707233917913; Tue, 06 Feb 2024 07:38:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707233917; cv=pass; d=google.com; s=arc-20160816; b=WhmIqo7NPXMBx7fKYmbKRlpBs6m2BjMWTcqtabA59nczCl6r+DlBBNj/8Ug8Rdrr+4 6ZHlakgCeDOwhYUUc2shiCYDDj/+VVRAGHVuu4dRVQ9uYx3wZe9z75X4hGDVKhN9M9bn SKLlUrO+Ib1OaVSvdMfOxyYNAtgvVSAoaKB0nYCKHupMx/6mW+Q5kxtn/c00AbPGtOhr 3ED9avRfaOLiiWBAic+rR+MrUHiahRTyVlI+maYW1vxKRU/eic7LZmFtNFy20CE5SuVu Jp2zeiS0lxif6kODqMMZVrGE696EyeJmVaDp/EuEbeqTDkLLaJrkKy1js9iK+xPEbo1Y yT0w== 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=GFHomQBA5xBJD3fD0Fs8tP6D3i6iXmvE6Ql/kTKOIJA=; fh=w7O2MFcCEKnS/WmQLNi/J+AkfOUmrJ53q5rRW84OhLs=; b=oSjMWIQwI7FrpCEO4PYDk7jwubGwTd1j3YorvPoVAnY5srjKQlychbkUPWkFYgnXpf N1jLXZHuPCS49cFtocIDE+c0K923VlcGe0SLW6UZNY08iafwOq5E/hk3qFyYP63yMzT8 tMpOSJ324ZWSjm3DqBrKwQ7eWlSSXne9tzrJEj9LQJRP6VVKK60YBy7YhkkuJZfACfyL 5eRuPEMIpMW908VjclKKgfgI92QydAYYIGnANRboT0eEkPK5AfdmrIvl8HSm0KWVxMlh Ph59FMihVlDKvJ/bLanJ+niE2U2oAdwy5UYO6YHqW5c5DZmLIppNkPF9XNADuLOhugrI n28w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="g4P0vbF/"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-55192-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55192-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCUx48yRZnEDdkkBRbjamWaLry92PDSvuw2eOtX27CKQytBj80XQD/DVpGb/2t7Scl8o8J2HpcD8pjy5W73Reb6ksxDGCQ== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id h11-20020a170902f7cb00b001d7195d8a0fsi1776573plw.117.2024.02.06.07.38.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 07:38:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55192-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="g4P0vbF/"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-55192-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55192-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 9DD9DB27F9A for ; Tue, 6 Feb 2024 15:13:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9C0EC1339A8; Tue, 6 Feb 2024 15:10:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="g4P0vbF/" 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 D3AAC13328A; Tue, 6 Feb 2024 15:10:10 +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=1707232210; cv=none; b=obSeae4A+MGHVFZvmSeXLgkWTp60y3Vb+t3yGw8vYGpPoSquQ7qJGXEhscxKi3jD+Ka3ZOiDZZ5ERXY91FgHhU8ybW/uXlrwHLuRVvw2wK/YTWo3TRCKMysXyvsnPygIzVpxV48uoVHr1wzoC1+eH3pAXYZiNY4CFrHiwwsCuqc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707232210; c=relaxed/simple; bh=FOu7YqSOLWC5YAYXAHa1AHM2WwjkEtKXf4zI3+QaxKc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=k89Ps37mR8q2HTxjEpItpLAZPVmJcgQWRfymZugsdxvZ3H6yWBx9IjE6AERu/hdkzpAHs4P4Ztr7uCrIrCogh4CXLG/JORfakT8FMiJuJ4t9wgiI59FTz/ZnXU+TphPIVe35NndyoYgIY65GmH//d1Gmafbi6sWSqjmUzPgOCEg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=g4P0vbF/; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F927C433F1; Tue, 6 Feb 2024 15:10:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707232210; bh=FOu7YqSOLWC5YAYXAHa1AHM2WwjkEtKXf4zI3+QaxKc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g4P0vbF/d5giNFPKpulD6m9kxqKCKQri3erw44CiBzN6J391JkRXviVOn6w09UTRo xGwLuXW7XcMfUOq63hHh4B5aY/B+dgyhpuhh7ClsYaK5WZaq58XlwykPWsDmhT/Ps1 jwzwpsHCldO5//3HkOYYfRPCJ+TVS4RQSwOLJ/ru/VHN4wAAaYUTfF5q68V08kR+dJ Dc0FcBkRrcA8wSaZnv2DagjUPqu+dman7WfeFqzt0WaEA7lypKdQQ3s6yMFxroaOEC IsU6Vce8oqRueQe58lTAgEuKEgUFa89Or/liSj8a0OcAuKOoMJwlzI0HWnssjW97I4 /PZalWNMp1TXg== 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 v7 14/36] function_graph: Use a simple LRU for fgraph_array index number Date: Wed, 7 Feb 2024 00:10:04 +0900 Message-Id: <170723220474.502590.7646977373091779892.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <170723204881.502590.11906735097521170661.stgit@devnote2> References: <170723204881.502590.11906735097521170661.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: 1790164512566267889 X-GMAIL-MSGID: 1790164512566267889 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 ae42de909845..323a74623543 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) @@ -935,21 +969,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++; @@ -979,25 +1009,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--;