From patchwork Fri Dec 8 10:26:40 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: 175741 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp5363180vqy; Fri, 8 Dec 2023 02:28:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IHP6B+S825A+ufVfcUIZd/g74AP3aZ9riaF1qe13nqzsb0R3TmpZwKGxYz5iH3Tp0NfxmyQ X-Received: by 2002:a17:903:2310:b0:1d0:ca40:15a2 with SMTP id d16-20020a170903231000b001d0ca4015a2mr3809810plh.17.1702031286225; Fri, 08 Dec 2023 02:28:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702031286; cv=none; d=google.com; s=arc-20160816; b=AOwB/7wky09Hwsq2yWnIoShXORI3pyMOyFDG2qLmYAZ5VVX77Dm2DjOjqdSUwr5ANl +xuTG7DEMT8VMQeFisRNU7x/ziEffeblsOlrl2AMaQbixoyhCVb8ushBGWWbfkNvZk5W ooITpi/cVHjKvfwnImKlTjRqSvbR1BhF+uLwfopW7+Old/yLveNflontXO9CfDhkRTDZ Rr1COQHvUG1wBSecL9vn5Xg+OyQ3ZdrcPuGALZlOwxChbOiiasHoRhlKlbdTqHuf4b9O Fok+7OFgACLfO3L7u6LchEnpRX6CRKwYSTWaLtU6kuFSEKFMo0/npXClmIGpPBOKybC0 LQog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=YNA1VX2mDjfQXIz74erT/WzxRw7KR9PXLakkeVYi8E8=; fh=SIgps5XdV0XNwjZfT2uAI7g3mrspDldK9Qs8qQAfoa4=; b=b2SuoDk3Bzip6SAz1IxUeN7P4LFklO80iWNIgBz2eadzU9QVncfiG3ckD4WpA7D7HD naxnixrsqZ2MhQ7Ur5l3eQqPg+k/f7fsq4UCukOGkjuY0QeJvfzYlybk4PIDWW3FavzL KRYRJPZ+TOepfJSTb983a5r1KwwkWCD0F7Ofg1BgxT0YETgLpqrlAY35jcgtkKnQmZBN 2FO3xgkRkFDFrJcEoF5tSV6dOk8hrVlmX1wPQbwjSO+1TAuL21Vi4O0uesnCUeZwtRE/ pqmXO9VLBjtY0+YYhNHDDeermaQGO45R+1niy3Z5evRlLyD7n2yF1lNFVuA62Nm3z+3p auKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Igya/mQ+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id c10-20020a170902c1ca00b001cffcc110c4si1419115plc.54.2023.12.08.02.28.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 02:28:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Igya/mQ+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 7B9038236260; Fri, 8 Dec 2023 02:27:54 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231947AbjLHK12 (ORCPT + 99 others); Fri, 8 Dec 2023 05:27:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573605AbjLHK07 (ORCPT ); Fri, 8 Dec 2023 05:26:59 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A6151BC3 for ; Fri, 8 Dec 2023 02:26:47 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D4C1CC433C8; Fri, 8 Dec 2023 10:26:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702031206; bh=cuVa4qIlG+/ZLno0EmFW6if4LFY8jZFXE8ZWIFfPmHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Igya/mQ+ywbmAkJR9GvJobt6tOa9toUGUERjH95OL6YYsdJgcBaMfdysnIcCIYJLq XK8eD3Hw7rQJzOlPstXQdl4z1eK1b0xXDSr66+hcbRxqTYxmMKQ6yr4z2W6Y8ZR3Fr vr+6lMN+f5Hh6yj1cMImOWvesjL2nXCpXdK8PvL5svmeGilyM9CdyBzgjl5e27cKg0 /F1g9Z8CAk3hd+VnIA7J6kHj9iIWOh85qUsjDNEwrAZjdk/QmfmUVRVurbmI9AGEKo f/gMiO/+L2R/ovvVGVAQjY6m7vcQ87Xv8sLYrh9AFENuMJmmKR5nk52SF1HcW9KiFS QtB+XUBud20og== 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 v4 12/33] function_graph: Use a simple LRU for fgraph_array index number Date: Fri, 8 Dec 2023 19:26:40 +0900 Message-Id: <170203119964.579004.13369344037111885313.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <170203105427.579004.8033550792660734570.stgit@devnote2> References: <170203105427.579004.8033550792660734570.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Fri, 08 Dec 2023 02:27:54 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784709157555642660 X-GMAIL-MSGID: 1784709157555642660 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 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..d8b139959b62 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 fgraph_lru_last - 1; +} + +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--;