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=1796380365; b=yBhBvRsL8xdxi58jVO9kkxE+ZoUfPVxv0Wv0mGyFBN+Qhp0Rs7ODwVxSS9sf8HEte2NN pMcUiAsXsqE4cokGkWESHIl5ML1kgrwYEfIdYEBCeQ3m2er9By1AFs7fAcnXBQy0PaAFR KDJe3scCYcek3tu+KWBb0GgwrXG5wFP3XUlqlNaXrpn9/uJ3JOkXeJqe9xaI7ROyizUxv EXRfbMjP0N4WW7k/jhKr9h8bDTTOLVvH5QT4TwkfagKPRZx9yNuacLUIav7djtq3e8f7F LD+UEAmaWdDPISAD6QFvR5Irbe7SHUxjqGolIf/GAa+picsuNhkaluBbc2EseKYSjqw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380365; 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=ieLgwITcTPkm9/IdQoVPSEAIAPuNBpOYWMWA8c3Ia84=; b=AbAwNUIJyBnhR6FVBPPNGtcgnmLnSW3m0JNoT6eStDqxXjpITq9/NiNPOqhycWv3hmwy R2Z/sNNqg/Y1XeSycGnlqmN+U4zTPVU88W6R+XD9/4qyIRNhPfpVYvJkc6Tt9UslYLLyA iJRBPOYXKSuC6Y0DtWT0W2llzZl5Qvtg2d9+SDxiKojV4XNvaI34FfuqtJo+PWQbaDa69 p+85tJ9kSogpz3xOn+VOVqty56nlWC9T12SgIAug3HGFdiaaTtF2jMh+XoiaIbhcSwSQV OEoCcdlQUP4PvHfnGVTmwEy4fGgLihtHQ5d52ZxxTwuwNMfYSBAgD0DGWQbJNWKWmgg== 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=svpaiOZY; 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 65524606A336; Fri, 4 Dec 2026 10:32:45 +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=svpaiOZY Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D5371F00897; Fri, 4 Dec 2026 10:32:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380365; bh=ieLgwITcTPkm9/IdQoVPSEAIAPuNBpOYWMWA8c3Ia84=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=svpaiOZYMJAn109lmXWZIyLeVaDKxTzQCEQQHv6woL+//aB3mUksjVbkE53azTyWP byFI26yiGI8spCMi/Vt0a2Vee/Uiszk9atg/bxTSo1OeHjwjB6PGmHL3PkkyCL95oO MFeXpOBwWQqIU6xXqE9wz+j1NZDFjT2T2oLYFAfs= 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 7.3 242/742] ftrace: Fix lost recursion records in ftrace_record_recursion() Date: Fri, 4 Dec 2026 19:01:46 +0900 Message-ID: <20261204100641.497223362@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@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 7.3-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 bac4bc844ccd8..4f63bec052fca 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