From nobody Mon Nov 25 07:29:56 2024 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 D33371CF289 for ; Wed, 30 Oct 2024 05:36:11 +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=1730266571; cv=none; b=Tj1zMjm0BLm/427S7GCironuexLtGAG7LI3VaY0BVCw7MNSjt8iOMrsi0BBJvLJmqXY/HguUIS+mYYRYFTWRe/NZXPG02B3mU9228vw7M2YadUktx+RWCKPZTNHE9ib3+Shz9VwArwInniANlxdG04FFMOp9MtExH1EvP5Jn2H4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730266571; c=relaxed/simple; bh=XNUK0TECBAjRZzXRzwjfDmT9lMeI7Lr7DwwAmU30QsM=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=hE+lTc7OFzcI0VwvDWYbecuOIiUHkOOMijG2vCFTDoPt4OplX+Tsl9j+hB0ua+Oo1s9pjQGh3ZNa2pRt53vo0xY21+KPZ0/F4xnJRBPviB2iyG22lBl1Cd9GrDzo/7FW0NpjOO2mAHkGTKnr+7s7kSfD90VBYgJ89Kt60BRlZvg= 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 8A8AAC4CEE4; Wed, 30 Oct 2024 05:36:11 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1t61OC-00000005BD8-3qGF; Wed, 30 Oct 2024 01:37:04 -0400 Message-ID: <20241030053704.781457290@goodmis.org> User-Agent: quilt/0.68 Date: Wed, 30 Oct 2024 01:36:52 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Ryan Roberts Subject: [for-next][PATCH 2/7] fgraph: Give ret_stack its own kmem cache References: <20241030053650.956861738@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The ret_stack (shadow stack used by function graph infrastructure) is created for every task on the system when function graph is enabled. Give it its own kmem_cache. This will make it easier to see how much memory is being used specifically for function graph shadow stacks. In the future, this size may change and may not be a power of two. Having its own cache can also keep it from fragmenting memory. Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Ryan Roberts Link: https://lore.kernel.org/20241026063210.7d4910a7@rorschach.local.home Acked-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- kernel/trace/fgraph.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index 4ce87982966a..001abf376c0c 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -172,6 +172,8 @@ enum { DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph); int ftrace_graph_active; =20 +static struct kmem_cache *fgraph_stack_cachep; + static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE]; static unsigned long fgraph_array_bitmask; =20 @@ -1022,8 +1024,11 @@ static int alloc_retstack_tasklist(unsigned long **r= et_stack_list) int start =3D 0, end =3D FTRACE_RETSTACK_ALLOC_SIZE; struct task_struct *g, *t; =20 + if (WARN_ON_ONCE(!fgraph_stack_cachep)) + return -ENOMEM; + for (i =3D 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { - ret_stack_list[i] =3D kmalloc(SHADOW_STACK_SIZE, GFP_KERNEL); + ret_stack_list[i] =3D kmem_cache_alloc(fgraph_stack_cachep, GFP_KERNEL); if (!ret_stack_list[i]) { start =3D 0; end =3D i; @@ -1054,7 +1059,7 @@ static int alloc_retstack_tasklist(unsigned long **re= t_stack_list) rcu_read_unlock(); free: for (i =3D start; i < end; i++) - kfree(ret_stack_list[i]); + kmem_cache_free(fgraph_stack_cachep, ret_stack_list[i]); return ret; } =20 @@ -1117,9 +1122,12 @@ void ftrace_graph_init_idle_task(struct task_struct = *t, int cpu) if (ftrace_graph_active) { unsigned long *ret_stack; =20 + if (WARN_ON_ONCE(!fgraph_stack_cachep)) + return; + ret_stack =3D per_cpu(idle_ret_stack, cpu); if (!ret_stack) { - ret_stack =3D kmalloc(SHADOW_STACK_SIZE, GFP_KERNEL); + ret_stack =3D kmem_cache_alloc(fgraph_stack_cachep, GFP_KERNEL); if (!ret_stack) return; per_cpu(idle_ret_stack, cpu) =3D ret_stack; @@ -1139,7 +1147,10 @@ void ftrace_graph_init_task(struct task_struct *t) if (ftrace_graph_active) { unsigned long *ret_stack; =20 - ret_stack =3D kmalloc(SHADOW_STACK_SIZE, GFP_KERNEL); + if (WARN_ON_ONCE(!fgraph_stack_cachep)) + return; + + ret_stack =3D kmem_cache_alloc(fgraph_stack_cachep, GFP_KERNEL); if (!ret_stack) return; graph_init_task(t, ret_stack); @@ -1154,7 +1165,11 @@ void ftrace_graph_exit_task(struct task_struct *t) /* NULL must become visible to IRQs before we free it: */ barrier(); =20 - kfree(ret_stack); + if (ret_stack) { + if (WARN_ON_ONCE(!fgraph_stack_cachep)) + return; + kmem_cache_free(fgraph_stack_cachep, ret_stack); + } } =20 #ifdef CONFIG_DYNAMIC_FTRACE @@ -1294,6 +1309,14 @@ int register_ftrace_graph(struct fgraph_ops *gops) =20 guard(mutex)(&ftrace_lock); =20 + if (!fgraph_stack_cachep) { + fgraph_stack_cachep =3D kmem_cache_create("fgraph_stack", + SHADOW_STACK_SIZE, + SHADOW_STACK_SIZE, 0, NULL); + if (!fgraph_stack_cachep) + return -ENOMEM; + } + if (!fgraph_initialized) { ret =3D cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "fgraph:online", fgraph_cpu_init, NULL); --=20 2.45.2