Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp913347lqb; Fri, 24 May 2024 19:40:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUMbP5AgMArjx6ru4vSjChDyTAklvLOUUmKkO8tT6ybi4ykKbwzL4xr4ZWJ3YZNDtzeCbNggCOgkI0i2jm5DXfoYA14scKc3WQfui1+hQ== X-Google-Smtp-Source: AGHT+IHCyliYywU/D+FWrKTD2oXYPezdjypG1rav688nXhEMZLWCCiJWZoC377RTsuMgK1/PwCvU X-Received: by 2002:a25:f626:0:b0:de8:bbc4:2b7 with SMTP id 3f1490d57ef6-df7721555a9mr4083325276.8.1716604828907; Fri, 24 May 2024 19:40:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716604828; cv=pass; d=google.com; s=arc-20160816; b=wdnZhhlrEhNTqSQF84m86M5Cf20XaR6yRCK4b7g0LJQS/BSUMkBlWSRZ6L6PpkDDOd PzmTrVV8OnbKXniGd9Tq++4pExKIGKkngkh5ZE9XZ5nWuEpk17w4XV1Enk5uqLYM7Oc0 eBmTJZM/O8QwDi84ceJo8E9LOoxT80R2yVjihwlMqlcxcJJaQ+Pt+kYVAwwHuYjnEqP+ 6HDSrHlz29ImqEKbuWvKxZk7qju4R21ILj/bn9jyaPyyKQ5wiwj7FSfVGTb2z9zaT7dy 3XUJ1talyghagPT8S9xIRpPFm1tkQzqCyPyqEw/oOwLR6IA+c6Q7mcUHvbhU8LqQnfn0 ImGw== 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=aydJQUmHwt1TaywMTteYutZ3ce8UomrH9KgGwcs1WvI=; fh=YfcaVrAzBpSwi8dOlqHV4K1os6qdULs4cyXFG/DyLfE=; b=szTM3SKaaehx+yChMOItmlZiXnOLiwXaQlDTpKlpXs9KmEw2XtAxBsJADmxiutTm2w Xwn3TrfzHr26XOr8KtjXQeB5lZIspCM0IlzovsgNOy+fVkAan0lOCVCK++4/RhbZyHo0 +AMFkGrBpleMsoTFbVhJFNOARP8KfmPMNZFQOw1ZW8iYex6qkwtmnmMZ9VKN8L5QmUte jBzhixRql6Mt2hWCx0MMTtVe07SwS9iSc16Ylg7fO9wpqG1JBsqEBz3LK4P40cqiJ88z nR7MpkXcUfqzLMNcKL7lu+jaRtXku4edaqDkoh1kiuQu0aAoKXktXSEvIV+Nn35kcVDO tmRg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-189252-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189252-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-43fb18b2b2asi31554591cf.455.2024.05.24.19.40.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 19:40:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-189252-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-189252-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189252-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 8D8721C21B04 for ; Sat, 25 May 2024 02:40:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6987212838A; Sat, 25 May 2024 02:36:59 +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 4900D2555B; Sat, 25 May 2024 02:36:55 +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=1716604615; cv=none; b=M6uJ4q65peQbc0g3SdToJgGKCga8/76TYgOnVv8X+RFzWyGXqRdwMBD5UFtoXMJfe8wPRlpTKpLZfeEySJKWyZV13brk/iL0GlgCobI2HCDRjr0DKcmgin1xYMxMZfV/VfP/RwRZt7DEzKQEpFq89msmPSDbbBEU9fmA40+E0zQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716604615; c=relaxed/simple; bh=thcMow/SAZdTsefHHRMVlJB1R1mY9N9R73UMp4wZwxc=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=JquFaJjQz7CJXyyBxUBtYMHnsOvhZUOmQYyvHYlPUzDX7HMEmOkyv3rdGHm9h+Fr/lIaO60M+irR/j40buE43OEOsd9Y7+mqY4Zw6N5kB5fWHFMIaA6LHXITMyZ5mvF0cLiSrbiucl5LuMkSmLd2SjnnlP8tQiNmrCoIzbdfiLU= 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 8C852C4AF17; Sat, 25 May 2024 02:36:54 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1sAhHz-00000007DN4-0LNu; Fri, 24 May 2024 22:37:43 -0400 Message-ID: <20240525023742.944236388@goodmis.org> User-Agent: quilt/0.68 Date: Fri, 24 May 2024 22:37:03 -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 11/20] function_graph: Use a simple LRU for fgraph_array index number References: <20240525023652.903909489@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 67fa7fbf6aac..8e029d5e94f6 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -125,10 +125,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) { @@ -375,7 +413,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) @@ -927,7 +965,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) { int command = 0; int ret = 0; - int i; + int i = -1; mutex_lock(&ftrace_lock); @@ -943,21 +981,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++; @@ -981,6 +1014,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) if (ret) { fgraph_array[i] = &fgraph_stub; ftrace_graph_active--; + fgraph_lru_release_index(i); } out: mutex_unlock(&ftrace_lock); @@ -990,25 +1024,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