Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp1166588lqj; Mon, 3 Jun 2024 12:11:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXRX1QTnCjkN6DjCvUHg1l/wWYw0gMoXZWMIlNHvxP34sb4/DN7JW5IurtmHr73x+vlJFgSKI3A/1lYPz+nwVcpG3Ii4mFdenWn/5f/nQ== X-Google-Smtp-Source: AGHT+IHZbm/Y+MF98bGzbg9f/spa9wv4EiM0kBmWSoSWbqoAHpdz0qUOhJPlhHRd2dBQ/v8QU+SW X-Received: by 2002:a50:c8cd:0:b0:574:eba7:4741 with SMTP id 4fb4d7f45d1cf-57a365afe40mr5628987a12.42.1717441869729; Mon, 03 Jun 2024 12:11:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717441869; cv=pass; d=google.com; s=arc-20160816; b=uNl25MvQGo2Q6O5xCz1pWBQxwyG5kxOnPBHyV6BUcJKkLYo1C8K/VHTVqx0ifngIhh CtvoreW2RusofnsUXUbdeFSXcRxHnWoIB+NDdjYjD59yqluGpvtTpb/4D8Uylwtm2myX FDzGmbjQdb0FcSVez4ExBA0kDfOuZtrKGzwpXb4Xe2rv/C+AHBzGFqXH14mB7fpAXonj cPTsJJQSOioXticaSmr88y0tXDiQGEwzSKxl5+IS4BSd5USv/I8OOlPTy3lijvJjv1ou S5gy4/eG95utwbxFl7ObNy0wdc2d+0J5W9NbWJuhT5QTMbYcSSWZJT1Cy3YaKVEmqbSK qApw== 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=n1AP2RFZFAIqQfCodZMag8vODlB2tw9882f8hyL1ZkA=; fh=YfcaVrAzBpSwi8dOlqHV4K1os6qdULs4cyXFG/DyLfE=; b=tZLo6OFNc3NLBlOczVUcmjAwRLfZyoKw7zImvjv8TFKlK/L6EJpxTXRxnp9NSFHkpm gHPypBJ40LZiZDZn01z9gCDrkpQXe9UJwQHghAQ1Q0faceXkCbJVgWDRbExGugJtBmey NHZB1hVXFoSJisEWl8CouWceuKtF8vBL9DyaasVZ/nvzSqX5qqVo7vOeUdPh/XW7+UZ1 ic0JF+tfQddMfRROmy8YFBCJ6/7PZ/w1ha9QgBDyh2otzvnSoSicdt9tSxByQ/6dyn2E IXXzBBM7QX8KkFcHA55kGrOSE5XjnTswAy4bH8KCxc7Ro53JhKVX/ekqgP+K2n5m9ONT Ym1w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-199609-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199609-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57a730aacd6si390006a12.26.2024.06.03.12.11.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 12:11:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-199609-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-199609-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199609-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 4F9421F25FEB for ; Mon, 3 Jun 2024 19:11:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8C99913E031; Mon, 3 Jun 2024 19:07:16 +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 3A34F13C9A4; Mon, 3 Jun 2024 19:07: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=1717441633; cv=none; b=G/c4astVoaN8ozwTnnArXXPTwDy577I+++zO2Bp99ozo6P3vhpQs9n22xuS7jvplHZzImN3gBFMa+srYeUGxpZJ9JvwE7H/cfuAMUDcoIUvj5PB/3jH+MPN5pceTzjZzsDUISwj7U6Y1VXMD1NscNtQpgGVvCmoqeA05BRmvIvk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717441633; c=relaxed/simple; bh=RBw53ycHF+DElS6M+SUgA8ingNid6OrS+4Pj7y1fIh8=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=MssF+qHmpLUfFZN+qoDxCqpJt0El1dNFvI9HW7wFwhic/gNNoU1JwYP6IXH7V4ebYcQfdRYEGvIOOh2H5S4e+t6T3wwqYnr7P/J9lg/Qzp7Ut74stzK293d3ChgxKWtiSqtaeCDFdkZE3cyec8mt4ekA+2yKl2wN5xGuV/d2EQk= 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 6D17BC4DDE7; Mon, 3 Jun 2024 19:07:12 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1sED2d-00000009TtR-1tkU; Mon, 03 Jun 2024 15:08:23 -0400 Message-ID: <20240603190823.308806126@goodmis.org> User-Agent: quilt/0.68 Date: Mon, 03 Jun 2024 15:07:19 -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 v3 15/27] function_graph: Add "task variables" per task for fgraph_ops References: <20240603190704.663840775@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 e31ec8516de1..82cfc8e09cfd 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -1089,6 +1089,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 7fd9b03bd170..baa08249ffd5 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -54,6 +54,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 @@ -116,11 +120,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; @@ -211,6 +223,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 @@ -766,6 +816,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: */ @@ -826,6 +877,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; @@ -959,6 +1011,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; @@ -997,6 +1067,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); } /* Always save the function, and reset at unregistering */ -- 2.43.0