Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp913358lqb; Fri, 24 May 2024 19:40:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWyl3VRJis8PVpk31g9Js6U3WxXtcXe0A9EXu0idpLIhV8qBAqm+V7S5uO8rl3OPbV9hrv3C5J4TseqlwrxwJvb1yWpaO6GtCCHIlAjQw== X-Google-Smtp-Source: AGHT+IFz7fgGBo6TZdjpp9wdxcnvQG2AqGgmx2PxwNyVxJlJO2PAvDsjEbkE1uc6Ah2OW3T7qJLo X-Received: by 2002:ac8:7d84:0:b0:43a:f1f0:12f with SMTP id d75a77b69052e-43fb0f0ce26mr42795661cf.59.1716604832469; Fri, 24 May 2024 19:40:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716604832; cv=pass; d=google.com; s=arc-20160816; b=bViKVTmIzNL1zFcAwmBC4kuq24SnpegUsqeexgtTbicHPTjoyt8aq10sNb8HPySyK5 5wkNEFEMz4BX5+oopF6vSpq1rZZDNv55f7RU7rmRDFO1A5Nf5albIbBy/6qrFP76YVRo 93TW9RNr5K4+nCJNoeJcXq2IGJUd2YWa/E2fIHkXGTbFle/UyaD+37+aEYwZ3tZiphBq XRXPaWyN4yhJKblNWJ0FiFoZROx/FKe5mu8QBjwM1OOzus5dJJV6oDNuCmE30eZIdDse efxicanL0H2PkFy4bGNh4TVv30CcBzPCs63ITUauLOOOeJBY8E5bvQSZrvPSRiMCBzR5 ui/g== 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=jdO1Kj8EV/SXSgUqSnAo13hyxJC8EilahgRx5Eizpw4=; fh=YfcaVrAzBpSwi8dOlqHV4K1os6qdULs4cyXFG/DyLfE=; b=HQMBFkQJHaM8H3lLkqdVF0xaq+ESm2f0I8OiIF/sx1CXguZCA8OWPVodl/zW2uBeov WGhMhR3shdtiq0RlQaCApeEqVt3SeardojvwmNOtVEFh9Ji8rnhjgsrVf3CtLzhRdZ4U TzNCHq4fy1bv2X/MJfwtwWUwEmdGEGNOhIrN61zJbH9hF1DnQriRMd+dQYW8/3xAIfSa g7IRM+WCZpYv4xF+t3HqiXoC10F/nF/C51CYxNXuujjJR6XGLEryws9UUSyJLA8/4GmX FB1+hZOgD0xpoMVhr40GCKXeXWS6tlTjVqlsL42oChDVt0S+Dq++LpBDfuCQQVZiy5m0 ECbw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-189253-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189253-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-43fb1d05f68si31130971cf.190.2024.05.24.19.40.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 19:40:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-189253-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-189253-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189253-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 ABB771C21BEC for ; Sat, 25 May 2024 02:40:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7D741128393; 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 669C428DD1; 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=CxPFTYibP3tpJszEmj8XcXvFCCtk09ImzZ+vL/vAAcGDyQB0HMbrJCe312AJBo/r6CHnbHoRyo9+VtfBjDI1v9u7YPpiyvFw07ot71k0PlizwOrTEPzCNJW4TD1D0T8C7T0V2rv1hYX66Tg6Or62BQg/MwVADz56t1WoIksdrQA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716604615; c=relaxed/simple; bh=pT1Qo6CHsMs4Cw+fcD0C6pCyluUKTWozMiIBQ3lQvfU=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=phQu3/mNb41EwvF94CISFYoi2Y3FA/jtm8xb+DEfI1N9oAW+oA2rt6vVz9B3TPtrA72adPebjqhslfVUENhnr/djg6w2Ex1rScu5fSR/SJ93taqgmCCw1vRNolt276Gzo3LGXsDhQs6Xqv3nkJQ5vNVwyDQids54b/sIUFyO8+U= 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 9137DC4DE0F; Sat, 25 May 2024 02:36:54 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1sAhHz-00000007DNZ-10vA; Fri, 24 May 2024 22:37:43 -0400 Message-ID: <20240525023743.101188314@goodmis.org> User-Agent: quilt/0.68 Date: Fri, 24 May 2024 22:37:04 -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 12/20] function_graph: Add "task variables" per task for fgraph_ops 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: "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. Co-developed with Masami Hiramatsu: Link: https://lore.kernel.org/linux-trace-kernel/171509104383.162236.12239656156685718550.stgit@devnote2 Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- include/linux/ftrace.h | 1 + kernel/trace/fgraph.c | 74 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index e5b41683ffb9..80eb1ab3cae3 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -1087,6 +1087,7 @@ ftrace_graph_get_ret_stack(struct task_struct *task, int skip); 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 8e029d5e94f6..0d536a48f696 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -55,6 +55,10 @@ * on the return of the function being traced, this is what will be on the * task's shadow ret_stack: (the stack grows upward) * + * ret_stack[SHADOW_STACK_OFFSET] + * | SHADOW_STACK_TASK_VARS(ret_stack)[15] | + * ... + * | SHADOW_STACK_TASK_VARS(ret_stack)[0] | * ret_stack[SHADOW_STACK_MAX_OFFSET] * ... * | | <- task->curr_ret_stack @@ -117,11 +121,19 @@ enum { #define SHADOW_STACK_SIZE (PAGE_SIZE) #define SHADOW_STACK_OFFSET (SHADOW_STACK_SIZE / sizeof(long)) /* Leave on a buffer at the end */ -#define SHADOW_STACK_MAX_OFFSET (SHADOW_STACK_OFFSET - (FGRAPH_FRAME_OFFSET + 1)) +#define SHADOW_STACK_MAX_OFFSET \ + (SHADOW_STACK_OFFSET - (FGRAPH_FRAME_OFFSET + 1 + FGRAPH_ARRAY_SIZE)) /* RET_STACK(): Return the frame from a given @offset from task @t */ #define RET_STACK(t, offset) ((struct ftrace_ret_stack *)(&(t)->ret_stack[offset])) +/* + * 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_OFFSET - FGRAPH_ARRAY_SIZE])) + DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph); int ftrace_graph_active; @@ -212,6 +224,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 offset into @t->ret_stack to find the ret_stack entry * @frame_offset: Where to place the offset into @t->ret_stack of that entry @@ -803,6 +853,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: */ @@ -863,6 +914,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; @@ -961,6 +1013,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; @@ -1007,6 +1077,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); -- 2.43.0