Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp719043lqj; Sun, 2 Jun 2024 20:11:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUbB6LuHtPc/w5eDS9Rs2PCj12mkyK1ndG/It3VDtLY2/pcabp0iH3kAYA5DX0M1F34nYUjIlhEyxgMepuHlsXvdOAfe307SLIyrumprw== X-Google-Smtp-Source: AGHT+IHGRGsQKURMPV9qwUaWNgiPdt51vNbcakiHhObFKaOtomhy0eGBdNp2ybKcb7uDoENGcmOT X-Received: by 2002:a67:ffd1:0:b0:48b:a4f1:c4c5 with SMTP id ada2fe7eead31-48bc21f3717mr7742707137.19.1717384283645; Sun, 02 Jun 2024 20:11:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717384283; cv=pass; d=google.com; s=arc-20160816; b=accqeHkErg2PTDUhZvOpD6JajeFQQw+tLW+5eF8/k/PYoYBG8jp1UskuxhR+2vhVcK xT9gr3dwcXPzNj/Ug0Rs0XELRhdYAUfKuu5woYx07cluTGFoA0agtxr17NkdDrcqrxyu +3RlFCH91jOS5+gJsJ5GRNgQAOfJXnqlqOXdnFxTbxAa1tItTKxxiVhpDk+Rks8iVoZU TKueUCNgrKpI31Sa17YhFI3rtHlyimq/hSTaKVVU1IuTna01mqTY4ZY0bTJlX/uIYcUY z17fkZkg5YfqMnIQMjn91m8/CnEbE3HW6hOaklfpchX3Ld2EacRmV0yCVt2iK/D7VIv+ IdTA== 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:dkim-signature; bh=fZF/nkBmmNEyb4pKE/BfWu0sKNodQ6Eif11QmPo2gUY=; fh=jPbTOLaFRquCWSHePkx5JyHgrJfqc0pfK3tHoljjLqk=; b=r1wgrnO74+4TMF3M8WMirSUSdNYR60vDQvBjvxw5d+bOvN3ymBg3gefVvK8uku5GbR oa0BnCfYK8v4laj5CnTqvUF1wNf3cxhch5a3E0Zyc4uslZ6KEyuDTFKscFviBgb+jLhP +7I1M4JJY/5gXK8gdEMT4qB6u5kc6TbuAqldMZ6ccrL7701QEZVlvOuzgIjYPXGqWt2A N9hewHd17UBKtfatgOIr7zEetmBQ/2Jd/jpwBDAxe1LNbTErsykcCYHCYVeqy4xCdZpS 9MAJNIFFNIIpTgKfnoDDsPxj4eShXMKAAb6Xt0+Tmx8RLT2Wt0EPeyOf8du4GNC12w2U ZBew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q87XzTeI; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-198565-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-198565-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 af79cd13be357-794f2f050casi784662585a.72.2024.06.02.20.11.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Jun 2024 20:11:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-198565-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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q87XzTeI; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-198565-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-198565-linux.lists.archive=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 5090A1C20886 for ; Mon, 3 Jun 2024 03:11:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 53084F514; Mon, 3 Jun 2024 03:11:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Q87XzTeI" 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 4603CD29B; Mon, 3 Jun 2024 03:11:13 +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=1717384274; cv=none; b=CCI3RwVqDu/sMOx7TjqQgQYHCRCbAdWjY+vaGk4vI46BNODPRcs56F1VLtpfBmcDuN9yiyTtUDhmT4PW66NLoV7K9Hqct5DKlshWwA5v7BsebDQAsU1yMl5pFzxBuCZ6oGDR9lDJHsoUI0+ir3SCUVeA3KKIxSi3vnSv2RsnVhE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717384274; c=relaxed/simple; bh=3Iq570y1O+dBo8MJ2sPHzPkoGj+5BwKLE3Cc+XpquIM=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=hANIdZTNkcIRzbvCChL/R+/OUgNSn6rKBAcKfn9Mqdc7Uhvjt2wVfqgeGOm2Dg1CIqtnxeGkaHlE0LaG7UUJ4PA3AKXrxlylXo/b1mqLloBE3jcKxp0uPzM64trMihSLZeO6NRPn2os6YFSZ5nR4NIfCCQgyj8iYm7KaPEi1EBY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Q87XzTeI; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91E3BC2BBFC; Mon, 3 Jun 2024 03:11:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717384273; bh=3Iq570y1O+dBo8MJ2sPHzPkoGj+5BwKLE3Cc+XpquIM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Q87XzTeIn36G6VKkzd3klnjoRwuJx1+4sDBMRy151kFh4/0iJTfypOkcYOK8SK2yF /w6OjaDqtGEsSPrKXe3wgYorMOyqCMvpFX6nilqxa7tRiH8RwmYA8n8ud9HXgcLIlX X7wubovvf+4BhflpOTO4NdPE9SBgGc/654TY6X+X/e0XwvJTE8wiQRq2ekqRmcoMq/ xHlYuBfI/HST9boq6S07YDuAW3YMh/gsLf/1YIB0/jze3lkf6yLfMN+vz7m6WvFtZB XjXScuTcP/HKOhDBDfyXtXuVxPKLgWBWY0/wnXbBtziyRisTF5KkzpMz9JGtXExL8/ jLCnyrETOJR3A== Date: Mon, 3 Jun 2024 12:11:07 +0900 From: Masami Hiramatsu (Google) To: Steven Rostedt Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, 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: Re: [PATCH v2 24/27] function_graph: Use static_call and branch to optimize entry function Message-Id: <20240603121107.42f98858ebb790805f75c9b1@kernel.org> In-Reply-To: <20240602033834.997761817@goodmis.org> References: <20240602033744.563858532@goodmis.org> <20240602033834.997761817@goodmis.org> X-Mailer: Sylpheed 3.7.0 (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 Sat, 01 Jun 2024 23:38:08 -0400 Steven Rostedt wrote: > From: "Steven Rostedt (Google)" > > In most cases function graph is used by a single user. Instead of calling > a loop to call function graph callbacks in this case, call the function > entry callback directly. > > Add a static_key that will be used to set the function graph logic to > either do the loop (when more than one callback is registered) or to call > the callback directly if there is only one registered callback. I understand this works, but my concern is that, if we use fprobe and function_graph at the same time, does it always loop on both gops? I mean if those are the subops of one ftrace_ops, ftrace_trampoline will always call the same function_graph_enter() for both gops, and loop on the gops list. For example, if there are 2 fgraph_ops, one has "vfs_*" filter and another has "sched_*" filter, those does not cover each other. Are there any way to solve this issue? I think my previous series calls function_graph_enter_ops() directly from trampoline (If it works correctly...) Thank you, > > Signed-off-by: Steven Rostedt (Google) > --- > kernel/trace/fgraph.c | 77 ++++++++++++++++++++++++++++++++++++------- > 1 file changed, 66 insertions(+), 11 deletions(-) > > diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c > index 4d566a0a741d..7c3b0261b1bb 100644 > --- a/kernel/trace/fgraph.c > +++ b/kernel/trace/fgraph.c > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > #include > > #include > @@ -511,6 +512,10 @@ static struct fgraph_ops fgraph_stub = { > .retfunc = ftrace_graph_ret_stub, > }; > > +static struct fgraph_ops *fgraph_direct_gops = &fgraph_stub; > +DEFINE_STATIC_CALL(fgraph_func, ftrace_graph_entry_stub); > +DEFINE_STATIC_KEY_TRUE(fgraph_do_direct); > + > /** > * ftrace_graph_stop - set to permanently disable function graph tracing > * > @@ -636,21 +641,34 @@ int function_graph_enter(unsigned long ret, unsigned long func, > if (offset < 0) > goto out; > > - for_each_set_bit(i, &fgraph_array_bitmask, > - sizeof(fgraph_array_bitmask) * BITS_PER_BYTE) { > - struct fgraph_ops *gops = fgraph_array[i]; > - int save_curr_ret_stack; > - > - if (gops == &fgraph_stub) > - continue; > +#ifdef CONFIG_HAVE_STATIC_CALL > + if (static_branch_likely(&fgraph_do_direct)) { > + int save_curr_ret_stack = current->curr_ret_stack; > > - save_curr_ret_stack = current->curr_ret_stack; > - if (ftrace_ops_test(&gops->ops, func, NULL) && > - gops->entryfunc(&trace, gops)) > - bitmap |= BIT(i); > + if (static_call(fgraph_func)(&trace, fgraph_direct_gops)) > + bitmap |= BIT(fgraph_direct_gops->idx); > else > /* Clear out any saved storage */ > current->curr_ret_stack = save_curr_ret_stack; > + } else > +#endif > + { > + for_each_set_bit(i, &fgraph_array_bitmask, > + sizeof(fgraph_array_bitmask) * BITS_PER_BYTE) { > + struct fgraph_ops *gops = fgraph_array[i]; > + int save_curr_ret_stack; > + > + if (gops == &fgraph_stub) > + continue; > + > + save_curr_ret_stack = current->curr_ret_stack; > + if (ftrace_ops_test(&gops->ops, func, NULL) && > + gops->entryfunc(&trace, gops)) > + bitmap |= BIT(i); > + else > + /* Clear out any saved storage */ > + current->curr_ret_stack = save_curr_ret_stack; > + } > } > > if (!bitmap) > @@ -1155,6 +1173,8 @@ void fgraph_update_pid_func(void) > gops = container_of(op, struct fgraph_ops, ops); > gops->entryfunc = ftrace_pids_enabled(op) ? > fgraph_pid_func : gops->saved_func; > + if (ftrace_graph_active == 1) > + static_call_update(fgraph_func, gops->entryfunc); > } > } > } > @@ -1209,6 +1229,32 @@ static void init_task_vars(int idx) > read_unlock(&tasklist_lock); > } > > +static void ftrace_graph_enable_direct(bool enable_branch) > +{ > + trace_func_graph_ent_t func = NULL; > + int i; > + > + for_each_set_bit(i, &fgraph_array_bitmask, > + sizeof(fgraph_array_bitmask) * BITS_PER_BYTE) { > + func = fgraph_array[i]->entryfunc; > + fgraph_direct_gops = fgraph_array[i]; > + } > + if (WARN_ON_ONCE(!func)) > + return; > + > + static_call_update(fgraph_func, func); > + if (enable_branch) > + static_branch_disable(&fgraph_do_direct); > +} > + > +static void ftrace_graph_disable_direct(bool disable_branch) > +{ > + if (disable_branch) > + static_branch_disable(&fgraph_do_direct); > + static_call_update(fgraph_func, ftrace_graph_entry_stub); > + fgraph_direct_gops = &fgraph_stub; > +} > + > int register_ftrace_graph(struct fgraph_ops *gops) > { > int command = 0; > @@ -1235,7 +1281,11 @@ int register_ftrace_graph(struct fgraph_ops *gops) > > ftrace_graph_active++; > > + if (ftrace_graph_active == 2) > + ftrace_graph_disable_direct(true); > + > if (ftrace_graph_active == 1) { > + ftrace_graph_enable_direct(false); > register_pm_notifier(&ftrace_suspend_notifier); > ret = start_graph_tracing(); > if (ret) > @@ -1292,6 +1342,11 @@ void unregister_ftrace_graph(struct fgraph_ops *gops) > > ftrace_shutdown_subops(&graph_ops, &gops->ops, command); > > + if (ftrace_graph_active == 1) > + ftrace_graph_enable_direct(true); > + else if (!ftrace_graph_active) > + ftrace_graph_disable_direct(false); > + > if (!ftrace_graph_active) { > ftrace_graph_return = ftrace_stub_graph; > ftrace_graph_entry = ftrace_graph_entry_stub; > -- > 2.43.0 > > -- Masami Hiramatsu (Google)