From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796336065; b=z86bwJwuJrNaKrwAJmmEDNhL2gV9isDodGqQN14m/4tPP3U2FB9EWJkA0IXAezfWadni a2uKqMptkZ3qubdI1BeBEWXCSPpHcZ4JXhABl4tTKfM1MISFHMZxSffZyfPy++CB1265B 8BfhUXvE6UX5+uPgREDvL6quG1Um7bXV0oZa2VfvkJcb0MXpeLYDZOsK2dA7MOYs+Ml6m /ebV1hQbCbu3Y2EBxyrpi+5JamOC5wnpKWoP+f0xYsD+dKTzrFRecusKDkgWB1Igg9Yys +JtKKkJCcndy9zx48eBXjeUUKfaav903+MsTLzy5TLBbKhGprz2X1XttesILfeVE9xA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796336065; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=6XXbmzgMbiFYAmvqgzogMe4plH1ElU6SBOPUBCvB0Pw=; b=a476LSC3LU9ysj89kX6q+y87J1/01js0F+ALIagRPm5aqecqxBuMOHo/I9J075BxpZ2f F8D6m893MPywkjn0h3W/C1WKuoyrb09YDO+RUrW0TIlM8cmkZBb2RIAJROlFvtY+N9nCH 4wvBvGnZO2LPxgtTo8FmRiSBa0kIPJPB7gUXvj2fU5Wxm0Uu35tyhwgPoidhqn7OCp9eK 6EHbA4CcuPSNktWYI5Vuvt/t7VZyFqxonaeld0v6SpRl0h3+diAKONulS2Qc2fY1pJVXd 1vgn8JnPA1HzTuK6uZRwFoiAHxsTwy3t6H/InCRfemn0LNVes7YUwnAVa3NZGMwH/AQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mzIxYUxC; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89743606A335 for ; Thu, 3 Dec 2026 22:14:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mzIxYUxC Received: by smtp.kernel.org (Postfix) with ESMTPSA id 377071F0092B; Thu, 3 Dec 2026 22:14:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796336065; bh=6XXbmzgMbiFYAmvqgzogMe4plH1ElU6SBOPUBCvB0Pw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=mzIxYUxCBwZAHQCn2K9sjlU9S0naKM/Qn9z3GFEux2g2fN3qhEzZAsTsmtv7G4PGs rl3qcsi1Z5TTCfNCjl65g/XmZTkfxU3VYk+1MsKp40VHKqtvfs2p2cbEFDftP25vGL Eckl6u3QL6KDusWlhJviSeqCv/fzwl4JOL9cTi1u9i+WMPDbUaZbmhD9EaZFnuW37m ODoehQUe7Md83puaPho6IbKPxzTxJuEMrIO1iVAQ+THhHKRR0tC3+ZHeuV7DOG81qZ IP2uN3v/LyHiNB+ksjzi40MdPZUuAIdD8mHY2kJNbPoM+Io/l2BamMUHGllpSJKlWO CeCjbWpSue+gQ== From: Sasha Levin To: stable@vger.kernel.org Cc: Vineet Gupta , Peter Zijlstra , Andrii Nakryiko , "Masami Hiramatsu (Google)" , Steven Rostedt , Sasha Levin Subject: [PATCH 6.1.y] fgraph: Allocate shadow stacks inline with GFP_NOWAIT Date: Thu, 3 Dec 2026 17:14:22 -0500 Message-ID: <20261203221348.801142-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120221-obstruct-preaching-8ff9@gregkh> References: <2026120221-obstruct-preaching-8ff9@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Vineet Gupta [ Upstream commit 700e5c063fd765fd09c1007ec8a927bddd82798b ] When ftrace graphing is turned on, all tasks in the system missing return stack page are assigned one. This is done in a simplistic multi-sweep loop of FTRACE_RETSTACK_ALLOC_SIZE (currently 32) tasks at a time as follows: start_graph_tracing() do { alloc_retstack_tasklist } while (-EAGAIN); alloc_retstack_tasklist() alloc x32 # GFP_KERNEL, may sleep rcu_read_lock() # preempt off for_each_process_thread walk N_total, no cond_resched t->ret_stack = new_page rcu_read_unlock() # preempt enable but no explicit yield Each successive iteration of loop invokes for_each_process_thread() which doesn't support cursor based resume and always restarts from the init_task. Thus each successive loop needs to skip the tasks assigned ret_stack in prior sweeps and thus take longer and longer to find the candidate 32 tasks. And while the loop end calls rcu unlock, and re-enables preemption briefly, there is no explicit yield. The total number of iterations turn out to be N_total * N_null / (2 * FTRACE_RETSTACK_ALLOC_SIZE) where N_total is the number of threads on the system and N_null the number of those whose ret_stack is still NULL. After boot with no fgraph user that is every thread. This is not a tracing-only path. Since commit 4346ba160409 ("fprobe: Rewrite fprobe on function-graph tracer") fprobe is built on fgraph, so an ordinary kprobe_multi BPF attach that flips ftrace_graph_active from 0 to 1 pays the whole cost inside one bpf() syscall. Commit 2c67dc457bc6 ("tracing: fprobe: optimization for entry only case") narrows that to return and session probes but does not remove it. A host running hundreds of thousands of threads, with the current batch size of 32, turns the ftrace_graph_active 0 -> 1 transition into a multi-second, and eventually multi-minute, operation which showed up as RCU stall and softlockup_panic on heavily loaded Meta Fleet machines with preemption disabled. rcu: INFO: rcu_sched self-detected stall on CPU rcu: 0-....: (20718 ticks this GP) (t=21000 jiffies g=913 q=8 ncpus=8) CPU: 0 UID: 0 PID: 160141 Comm: bpftrace ___slab_alloc+0x549/0xa20 kmem_cache_alloc_noprof+0x16c/0x340 register_ftrace_graph+0x3d7/0x6c0 register_fprobe_ips+0x2d5/0x310 bpf_kprobe_multi_link_attach+0x218/0x7e0 __sys_bpf+0x267a/0x27a0 Fix this by allocating inline with GFP_NOWAIT instead of pre-allocating a fixed batch. GFP_NOWAIT does not sleep, so it is legal inside the RCU read-side critical section. A single sweep now serves every task, so the walk becomes O(N) rather than O(N_total * N_null), and the retry loop is no longer the normal path. The pre-allocated array remains as the fallback when GFP_NOWAIT fails. The runs below never needed it, so it was exercised with failslab injection: at a 100% failure rate every task falls back and -EAGAIN retries ~1560 times for 50000 threads, with no stall or splat. smp_store_release() is a drop-in replacement for the smp_wmb() and plain store it replaces. Measured on a 60-core Sapphire Rapids machine running a PREEMPT_LAZY kernel (CONFIG_PREEMPT_DYNAMIC=y, mode lazy), timing the write that drives the ftrace_graph_active 0 -> 1 transition. Threads are spawned while fgraph is inactive so every one of them has ret_stack == NULL, and the loop was instrumented to count sweeps: threads before after sweeps time sweeps time 100000 3126 15.3 s 1 0.019 s 200000 6253 59.1 s 1 0.047 s 400000 12503 227.8 s 1 0.092 s The sweep count is 1 throughout. Time now scales linearly with the thread count at roughly 230 ns per task, which is the allocation and initialisation of one shadow stack. Fixes: f201ae2356c7 ("tracing/function-return-tracer: store return stack into task_struct and allocate it dynamically") Suggested-by: Peter Zijlstra Signed-off-by: Vineet Gupta Tested-by: Vineet Gupta Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20261021174406.2891598-2-vineet.gupta@linux.dev Acked-by: Andrii Nakryiko Acked-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt [ sashal: Replaced the ret_stack_init_task_vars() and kmem_cache_alloc() calls with kmalloc_array() of ftrace_ret_stack entries, and kept curr_ret_stack = -1 with its comment instead of 0. ] Signed-off-by: Sasha Levin --- kernel/trace/fgraph.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index 383c8c7f11311..6df902b8d1e84 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -376,13 +376,16 @@ trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph; trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub; -/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ +/* + * Loop thru all the tasks in the system and assign ret_stack. + * The @ret_stack_list is a small pre-allocated fallback, in case + * the GFP_NOWAIT allocations here fail. + */ static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) { - int i; - int ret = 0; int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; struct task_struct *g, *t; + int i, ret = 0; for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { ret_stack_list[i] = @@ -399,19 +402,27 @@ static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) rcu_read_lock(); for_each_process_thread(g, t) { - if (start == end) { - ret = -EAGAIN; - goto unlock; + struct ftrace_ret_stack *rs; + + if (t->ret_stack) + continue; + + rs = kmalloc_array(FTRACE_RETFUNC_DEPTH, + sizeof(struct ftrace_ret_stack), + GFP_NOWAIT); + if (!rs) { + if (start == end) { + ret = -EAGAIN; + goto unlock; + } + rs = ret_stack_list[start++]; } - if (t->ret_stack == NULL) { - atomic_set(&t->trace_overrun, 0); - t->curr_ret_stack = -1; - t->curr_ret_depth = -1; - /* Make sure the tasks see the -1 first: */ - smp_wmb(); - t->ret_stack = ret_stack_list[start++]; - } + atomic_set(&t->trace_overrun, 0); + t->curr_ret_stack = -1; + t->curr_ret_depth = -1; + /* Make sure the tasks see the -1 first: */ + smp_store_release(&t->ret_stack, rs); } unlock: -- 2.53.0