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=1796386410; b=R5Mglugm+5KC8P4dxCzIRpnr/3exJHlNtbjV/7pEnF4pgUOgtEnH3QOwomQXV6UrdJMT RuuK206Z3ZCOI7Z/aG5g9zNYpYwPhxjRgMbvm6ZStXPBe11/pSoXSZLTN0R3JGRKcdsi+ DjwO2WfZjNSvf9sCSH4xGRbulERTqjwad+aaYLES3Ao+MIUcYgP7g53gbA5mRZpEjOqOT tajN0RgenRRd4tVK+wxF8LV5LXjdKVZ/pmZCfIcQvGaPl6KzULIDOpVMHV6pU9o1jxgS1 fLv6r9sU+qgzSZKHGqDUqAt9R9wPdi8rj6wRMvpI3br1G/9mITuONU1e9dPRhHsED/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796386410; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=vY8RhQ4X/SOs1N7jMs7AI/G+jX523Cex0vDPuO7Ejkk=; b=g7SxXLoQ5lz8id68ot5NGO4zD4gHwMWEtRF+Yn1J7zW3iMZr2ouDuG/GptXpTeNI6W2P bzdPhi0PMrIomXL/GgwRG9ngqRpszoNvD7Z8rEyguVxZGLIhdj/7WAWKgrOR+fZxjGYIk Y2xMO9r/SPWMzp1yYah8FKb7DkROkQSWfTt0+CSaL8nrbAiorEzDMuGZ0OfCc/TOyTsC1 XRv337YRgJKIyZJCdAOkMDXOKI/as6EKqqZ6n7cwjFSrkOpjm5Gvjr4PVByp2rQ8gTnnu EoaBMiOitN6RYS7kcUDx7+EmUS+RhFkV1jUBOLxa4wov0QDP9HFJHu3efkOOQmvWUCg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=x0e1iGva; 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 8F0E5606A32C; Fri, 4 Dec 2026 12:13:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=x0e1iGva Received: by smtp.kernel.org (Postfix) with ESMTPSA id ABB6D1F000C2; Fri, 4 Dec 2026 12:13:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796386410; bh=vY8RhQ4X/SOs1N7jMs7AI/G+jX523Cex0vDPuO7Ejkk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=x0e1iGvaNQ0guTqdKrbo3vXN0v6QH9egjKeyn/rFPdhSetNNW3b4OEoz2J4hcHoR8 7lNmg56goYJexjOYpLKBel+Be1wjG2PQ74cay49JoWPFWk20T5tQHS9SYgqTWTToaT JFb1uj9GdRvBNn0jdMU1rAVPI11RO6hoy1quEspA= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Andrea Parri , Steven Rostedt , Sasha Levin Subject: [PATCH 6.6 0951/1167] ftrace: Fix lost recursion records in ftrace_record_recursion() Date: Fri, 4 Dec 2026 19:06:18 +0900 Message-ID: <20261204100726.958357560@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore 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 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Andrea Parri [ Upstream commit 7792e21d0a6e2fa819209949e8ba957504bedcd4 ] cached_function is set to the current ip before the cmpxchg() that claims a slot in recursed_functions[]. When that cmpxchg() loses a race for the same slot, the code bumps index and retries via "goto again", but the retry immediately matches its own cached_function write and returns without ever reaching the bumped-index cmpxchg(). Concretely, for two writers racing on the same index: CPU 0 (ip = A) CPU 1 (ip = B) -------------- -------------- cmpxchg(&recursed_functions[index].ip, 0, B); // succeeds cached_function = A; cmpxchg(&recursed_functions[index].ip, 0, A); // fails, old == B index++; goto again; if (A == cached_function) // true: matches A's own store return; // A is dropped, not retried Once dropped this way, cached_function stays set to A, so every later recursion of A also hits the fast path and returns before reaching the retry, until some unrelated ip overwrites the cache. Set cached_function only after the cmpxchg() has claimed the slot. The retry path then leaves the cache untouched and no longer matches its own store. When the cmpxchg() finds ip already present, the writer that installed it sets the cache. The bug was found with the help of an LLM. Link: https://patch.msgid.link/20261026182553.221141-1-parri.andrea@gmail.com Fixes: 773c16705058e ("ftrace: Add recording of functions that caused recursion") Signed-off-by: Andrea Parri Signed-off-by: Steven Rostedt Signed-off-by: Sasha Levin --- kernel/trace/trace_recursion_record.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/kernel/trace/trace_recursion_record.c b/kernel/trace/trace_recursion_record.c index a520b11afb0dd..800569b647178 100644 --- a/kernel/trace/trace_recursion_record.c +++ b/kernel/trace/trace_recursion_record.c @@ -17,8 +17,8 @@ static struct recursed_functions recursed_functions[CONFIG_FTRACE_RECORD_RECURSI static atomic_t nr_records; /* - * Cache the last found function. Yes, updates to this is racey, but - * so is memory cache ;-) + * Cache the last function found in or added to recursed_functions[]. + * Updates to this are racy, but this is only a best-effort cache. */ static unsigned long cached_function; @@ -67,24 +67,25 @@ void ftrace_record_recursion(unsigned long ip, unsigned long parent_ip) } } - cached_function = ip; - /* * We only want to add a function if it hasn't been added before. - * Add to the current location before incrementing the count. - * If it fails to add, then increment the index (save in i) - * and try again. + * Claim the current slot before incrementing the count. If the slot + * is occupied by another function, advance to the next slot and retry. + * + * Do not update cached_function until ip is known to be present; + * otherwise the retry would match its own cache update. */ old = cmpxchg(&recursed_functions[index].ip, 0, ip); if (old != 0) { /* Did something else already added this for us? */ if (old == ip) return; - /* Try the next location (use i for the next index) */ + /* Try the next location */ index++; goto again; } + cached_function = ip; recursed_functions[index].parent_ip = parent_ip; /* -- 2.53.0