Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp749594rdh; Wed, 14 Feb 2024 10:08:21 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWlbGaq3ZKwf2Glt6E1nWfr5+/ONAwrDS/yWoZAW+QiA+WH7QwX11Qh+qGzLrA6BBhGtSndhSD7g8o5xLMc+NmgRfE3Vcbp6Cw+TZZIFQ== X-Google-Smtp-Source: AGHT+IHGQy4dRBhqCuDbVvXqNxPEmrVdn5FFDMdlmHBHy5MVmk1dKj/uAA9n+ovnCgYLvxjJ6BRb X-Received: by 2002:a17:906:6d54:b0:a3d:70ba:5988 with SMTP id a20-20020a1709066d5400b00a3d70ba5988mr636062ejt.40.1707934101578; Wed, 14 Feb 2024 10:08:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707934101; cv=pass; d=google.com; s=arc-20160816; b=oAfrUBV8uCOiPG6qv6OJdfjoHZgyKH/FhA97LZFaKwqTgYxu4w8JbyXfDUj4CqGtMW I+rqa60HzBJzv+XzVkkdUjOyc5Vb3+jgW2rIw55tVNgM37ij9Ran8KU+y6fhUAw2sF7x eS+Rkdoy+9UtoCdu6IJRccr1i1QpE9U/P5eGs+bw9u4cUfqezoTfjXNcBy8rtY0qnETm TiOFrW2DAwQvEHb1qBg4eEJyLtyBlIt9VExd8kaTx6OQ9D1MiJpw4LAhrd46APyfSiao kR5N1VJu9JK+miiKoESf6r7iEN7IwxdR+wtpQRJzxzPDW61U7urRXNBAlAiGg/BouBVg gKiw== 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:references:in-reply-to:message-id :subject:cc:to:from:date; bh=QxPzLJJtFBGuBBltrU9TwvER6ZOJu/Tn//eBmEYXuV8=; fh=BhyCZS3e+ikexVtNY1CCnxx6ATpMT5DIp+Fbp+zK+AE=; b=ZYd2xpXRzgFKtqLaFyFZme2vWvjtf4yYqULj+WeQ3OG+jNCr7XZCP1+fsXFZTmD3BM gw0V662JANarFs3Jqv7794+NqiWluZeCkIKOAwun6g7ENE4BeZaJ9KKwMFR+CQIQhcae UHCqfbdmvBwAScPfkBg0JAh3OoPqTyKxcddfCZYDEloybnYEmSvXRut8f6Sitqk6YKgB oDARIhNTKls03o/h9OYGZZfbKufRe3G8SvgfVew6v3V8IBE2Fcpa9wYKXUKmIV1ZLVKl Lz+8M/QE6q9f+QqmJLjXgZiJD/GzcY+7N6LM/nC5F2Gfj31EiPV0u9JcHiICZxKLtn0Q PAGg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-65768-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65768-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCWCk7eA+gbwSPyocz/YPh4DP8POadxwECeKLjyvbC7s/AAZz75VnPYD6Z6DlNs5oeSLSNxiWFcfbM1YNBLjxz2aeidH0h/CQB7JJzyBQg== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id fi12-20020a170906da0c00b00a3d125c82ebsi1543089ejb.703.2024.02.14.10.08.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 10:08:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65768-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-65768-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65768-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 576481F21220 for ; Wed, 14 Feb 2024 18:08:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F0CDB12BEA7; Wed, 14 Feb 2024 18:02:40 +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 6D1F3126F32; Wed, 14 Feb 2024 18:02:40 +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=1707933760; cv=none; b=jTZjPzO6WibUNW/vedvMur3p9v11KrVPqX2anYS+wluoIckU+MypWAgLaUnMYYFbzyOsaXbrndSY5BGo1badyD09VNlw6sY13aTCIyCKuOAM7FuIM3E/bfUCwWUStEoNxhn+xFZQXRpQ1v995LNF3j2s48Hcm9ioOKNs+QsQbfY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933760; c=relaxed/simple; bh=mi9PPom/j9dEgEqpLeXjgQGLWutzp/v4ktOkOCYFZx0=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hs/Hm3IvXhFVJtEjJpHAwNyUQU5Td6Ej2ja69z1sKsRTqCMTIkbP1oubro2aNkuVflTCq1gCvwh37evGlG5J8EMHq97FX9OBPdcSqPtxIFoQ+5bgVCqn9aVLvNkoiQ7WVlD4ZLZ19lVB+OOnbeZwhYr6UmSypKzkL9EbhRYvJLs= 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 245FFC433C7; Wed, 14 Feb 2024 18:02:38 +0000 (UTC) Date: Wed, 14 Feb 2024 13:04:09 -0500 From: Steven Rostedt To: "Masami Hiramatsu (Google)" Cc: Alexei Starovoitov , Florent Revest , 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: Re: [PATCH v7 14/36] function_graph: Use a simple LRU for fgraph_array index number Message-ID: <20240214130409.463ae408@gandalf.local.home> In-Reply-To: <170723220474.502590.7646977373091779892.stgit@devnote2> References: <170723204881.502590.11906735097521170661.stgit@devnote2> <170723220474.502590.7646977373091779892.stgit@devnote2> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 7 Feb 2024 00:10:04 +0900 "Masami Hiramatsu (Google)" wrote: > 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) Can fgraph_lru_table[fgraph_lru_last] != -1 ever happen? If not, we should probably add a: WARN_ON_ONCE(fgraph_lru_table[fgraph_lru_last] != -1)) As the size of fgraph_lru_table is the same size as the available indexes, if we hit this I would think we had a fgraph_lru_relaese_index() without a fgraph_lru_alloc_index() associated with it. > + 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)) { The above can nicely fit on one column. No need to break it up: 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; Removing the above WARN_ON_ONCE() is more reason to add it to the release function. -- Steve > > 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--; >