From patchwork Wed Nov 8 14:26:53 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: 163044 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp949488vqo; Wed, 8 Nov 2023 06:27:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IGD0F/H0aOX5G4PEuVWEhE+4Tu+fA0595ny2BRo5H5bI1t9GMQ+fETvM1uxShVDI53lhSwR X-Received: by 2002:a05:6a21:187:b0:17e:45d0:7036 with SMTP id le7-20020a056a21018700b0017e45d07036mr2388859pzb.11.1699453674120; Wed, 08 Nov 2023 06:27:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699453674; cv=none; d=google.com; s=arc-20160816; b=vOnqYsFzmxYP3vkbRiuLNKEPx4q30n0W4jsBTNkY5r9I6E+hc5Q1Wx2BS3+6xFwmWi A0M5pooPEqbEQ7UhkGY6BJl0F4frqEgLRS6ZKONQ2KuWY1mCg6IisHs9jnBBABpT0+Ax 61QDgtgja/RbMBDOrUx3pOwSgi/HPCUu5MRw7tnhhzfpniNJmtVPs3uA/6cq8c5MD12n 1fpmJkfX+EBZ6VX+nW176EV61V/zksffXP2hSKZ99QO9XTBXMvyiS4DO5z9Oon8VFX4i WSM+Y3mBkcXrc0axmOfrCauvAaTx06K35kOK7CyIqtJBJ/0vZ7krntvbOrPYA9LSq8LZ It7g== 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=XNHAmrWo92dxIKdcOy8MmyNDTTrhfBtxiUrhP1zrhKk=; fh=SIgps5XdV0XNwjZfT2uAI7g3mrspDldK9Qs8qQAfoa4=; b=lBzkWLlqvr7svPtHCedUO2snn/ClGJ24iFLPSXUO4rcONRFTu2Ihxja/iTTzdapaaP ERc35DgxMw7ylwP8lVt3X7/WGqBWmEa/YgIWvcILemvG7S0S0zF6hK63Xh+kjcdnjeYK iebTHdpu+NDVPOKFMhb8Lk2CzyOOX1lmjkmrhvN2qQVv5JxNRmbTiytOFNhcMzpYuXl6 hV7MoNXJuWXQlJ+A0SESIlSYufJ/uCVQLI1B79obVtxCqeg4NzRqqYmjAB5IWFNuXSNW NYSNUeb4JwuBsBLZBtioeDNPy1qWOMQm//kTliXQgVc3ul5NDhQp6Sr77ct7V8lCW2FK 0Y+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JgQnHPH3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id j3-20020a056a00174300b00690fe0f6e0dsi14027211pfc.68.2023.11.08.06.27.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 06:27:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JgQnHPH3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id 88ECB829F1CC; Wed, 8 Nov 2023 06:27:47 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232262AbjKHO1Y (ORCPT + 32 others); Wed, 8 Nov 2023 09:27:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235615AbjKHO1B (ORCPT ); Wed, 8 Nov 2023 09:27:01 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F60F1FE6 for ; Wed, 8 Nov 2023 06:26:59 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82C1EC433C8; Wed, 8 Nov 2023 14:26:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699453618; bh=g+niYb9NeHMllJMJ9gCEQ6bAowsEfv6df0ZgqQIH2uY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JgQnHPH3x0GHhljwQ2Si8wyhLTq1GotX78pAeAsJ8NMcjya5kHWVS2RZkGPJs6xCD OIHe9rpFpVaSfVj1vuUz71ZzW223I8orij0I2eCxhXeqMlPENxsR8T5+n1upKO3fdI XATsplRs4kwuO7BWShcK1wXmlzMmxRb892EYC8EUmEx9TDnKxlKze/44CU8CsgiAoS E/MQPCkBAN+m+FS2Ha6/37iKdBWvcWUN6BJ5iNHdbZ8dbRETXDn+Ssc2zIb+bnm+iV KvfdgL4zOL9RisPXXXZYkuCwLIbrzRky7SOX7e7X+RxKc4Hw5NWVgu55Z/CYibmsau r0ocptQuI4EDg== 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: [RFC PATCH v2 13/31] function_graph: Add "task variables" per task for fgraph_ops Date: Wed, 8 Nov 2023 23:26:53 +0900 Message-Id: <169945361301.55307.14643353930499117634.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <169945345785.55307.5003201137843449313.stgit@devnote2> References: <169945345785.55307.5003201137843449313.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 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 (fry.vger.email [0.0.0.0]); Wed, 08 Nov 2023 06:27:47 -0800 (PST) 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 fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782006336031138618 X-GMAIL-MSGID: 1782006336031138618 From: Steven Rostedt (VMware) Add a "task variables" array on the tasks shadow ret_stack that is the size of longs for each possible registered fgraph_ops. That's a total of 16, taking up 8 * 16 = 128 bytes (out of a page size 4k). This will allow for fgraph_ops to do specific features on a per task basis having a way to maintain state for each task. Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Masami Hiramatsu (Google) --- Changes in v2: - Make description lines shorter than 76 chars. --- include/linux/ftrace.h | 2 + kernel/trace/fgraph.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index d30eb8a97a50..3f9f1f48e8fd 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -1071,6 +1071,7 @@ struct fgraph_ops { trace_func_graph_ret_t retfunc; struct ftrace_ops ops; /* for the hash lists */ void *private; + int idx; }; /* @@ -1109,6 +1110,7 @@ ftrace_graph_get_ret_stack(struct task_struct *task, int idx); unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret, unsigned long *retp); +unsigned long *fgraph_get_task_var(struct fgraph_ops *gops); /* * Sometimes we don't want to trace a function with the function diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index 97cf320d20a8..79bdd3c775dd 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -92,10 +92,18 @@ enum { #define SHADOW_STACK_SIZE (PAGE_SIZE) #define SHADOW_STACK_INDEX (SHADOW_STACK_SIZE / sizeof(long)) /* Leave on a buffer at the end */ -#define SHADOW_STACK_MAX_INDEX (SHADOW_STACK_INDEX - (FGRAPH_RET_INDEX + 1)) +#define SHADOW_STACK_MAX_INDEX \ + (SHADOW_STACK_INDEX - (FGRAPH_RET_INDEX + 1 + FGRAPH_ARRAY_SIZE)) #define RET_STACK(t, index) ((struct ftrace_ret_stack *)(&(t)->ret_stack[index])) +/* + * Each fgraph_ops has a reservered unsigned long at the end (top) of the + * ret_stack to store task specific state. + */ +#define SHADOW_STACK_TASK_VARS(ret_stack) \ + ((unsigned long *)(&(ret_stack)[SHADOW_STACK_INDEX - FGRAPH_ARRAY_SIZE])) + DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph); int ftrace_graph_active; @@ -131,6 +139,44 @@ static void return_run(struct ftrace_graph_ret *trace, struct fgraph_ops *ops) { } +static void ret_stack_set_task_var(struct task_struct *t, int idx, long val) +{ + unsigned long *gvals = SHADOW_STACK_TASK_VARS(t->ret_stack); + + gvals[idx] = val; +} + +static unsigned long * +ret_stack_get_task_var(struct task_struct *t, int idx) +{ + unsigned long *gvals = SHADOW_STACK_TASK_VARS(t->ret_stack); + + return &gvals[idx]; +} + +static void ret_stack_init_task_vars(unsigned long *ret_stack) +{ + unsigned long *gvals = SHADOW_STACK_TASK_VARS(ret_stack); + + memset(gvals, 0, sizeof(*gvals) * FGRAPH_ARRAY_SIZE); +} + +/** + * fgraph_get_task_var - retrieve a task specific state variable + * @gops: The ftrace_ops that owns the task specific variable + * + * Every registered fgraph_ops has a task state variable + * reserved on the task's ret_stack. This function returns the + * address to that variable. + * + * Returns the address to the fgraph_ops @gops tasks specific + * unsigned long variable. + */ +unsigned long *fgraph_get_task_var(struct fgraph_ops *gops) +{ + return ret_stack_get_task_var(current, gops->idx); +} + /* * @offset: The index into @t->ret_stack to find the ret_stack entry * @index: Where to place the index into @t->ret_stack of that entry @@ -708,6 +754,7 @@ static int alloc_retstack_tasklist(unsigned long **ret_stack_list) if (t->ret_stack == NULL) { atomic_set(&t->trace_overrun, 0); + ret_stack_init_task_vars(ret_stack_list[start]); t->curr_ret_stack = 0; t->curr_ret_depth = -1; /* Make sure the tasks see the 0 first: */ @@ -768,6 +815,7 @@ static void graph_init_task(struct task_struct *t, unsigned long *ret_stack) { atomic_set(&t->trace_overrun, 0); + ret_stack_init_task_vars(ret_stack); t->ftrace_timestamp = 0; t->curr_ret_stack = 0; t->curr_ret_depth = -1; @@ -866,6 +914,24 @@ static int start_graph_tracing(void) return ret; } +static void init_task_vars(int idx) +{ + struct task_struct *g, *t; + int cpu; + + for_each_online_cpu(cpu) { + if (idle_task(cpu)->ret_stack) + ret_stack_set_task_var(idle_task(cpu), idx, 0); + } + + read_lock(&tasklist_lock); + for_each_process_thread(g, t) { + if (t->ret_stack) + ret_stack_set_task_var(t, idx, 0); + } + read_unlock(&tasklist_lock); +} + int register_ftrace_graph(struct fgraph_ops *gops) { int command = 0; @@ -901,6 +967,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) fgraph_array[i] = gops; if (i + 1 > fgraph_array_cnt) fgraph_array_cnt = i + 1; + gops->idx = i; ftrace_graph_active++; @@ -918,6 +985,8 @@ int register_ftrace_graph(struct fgraph_ops *gops) ftrace_graph_return = return_run; ftrace_graph_entry = entry_run; command = FTRACE_START_FUNC_RET; + } else { + init_task_vars(gops->idx); } ret = ftrace_startup(&gops->ops, command); @@ -942,6 +1011,8 @@ void unregister_ftrace_graph(struct fgraph_ops *gops) if (i >= fgraph_array_cnt) goto out; + WARN_ON_ONCE(gops->idx != i); + fgraph_array[i] = &fgraph_stub; if (i + 1 == fgraph_array_cnt) { for (; i >= 0; i--)