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=1796382086; b=NSL9ZOI5/6UhY9aZHccPBIbfqaSLVKcO1UyBUfAFL4L4vDMTVcmZyB4j+PuTOCGrcQJG k2EyjrC5+t6GD2p57HTCbAxFHUKYDOCJ15BbH2ODiVFdSPq/pyZ9LPNGXqTIkRdxxgNPl eLqbeyJoC3dEQFTABpk3wJ4lMYwiMQAEtFpv6S5am8cvWqbTi/0lkRPFrDD5l81YiNfjz TLeXubmTMpJBqAPsB5t0C7crmq2w1hw5dpIkhLNhx6ZIQAh7PpdrnQQxG1nQhLsakBmTI trHY5bmlO7YrJP4zzxosZBOcRAhoRZy841V3aT6/hBfbuSqceZWBMwLF6z7N/EXIq/g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382086; 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=ARTGnNrSO4tFHHU9XCgcuV7kZBDf0Rybpra6iHcocfw=; b=FcMOZSdjStkqoTd2uNYMJlXssgGHJWPYajY5VvodXYXiNAjrr7NiXAK0/a2dM2q/xgTQ g+oVYMAbD4O1Q4JGJWCsRGJrvSwkAJVRAGjRZAy9RQ/vW0TV8aJBxQ1IuUKuiJRAt8gVc otwmpaB6uA68ZovbpFzU3ufQDzg/aMNceymt55lmGD+CmQ1grlAEIYIiMoTqhLgFIBnLb u/HFdRIcEaayHxIj9a/8WRcsGEjxHFprrsl0+AMqJQEO4gB7FilyIsRbGelouOKJ0Ta/W xv8CcAE4R+q1HwBxcsDQOGaOsOX1LHCvMmobXVYRi3zKrA3IhFmfbU7wVnijMUF7zOw== 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=z28UyWxJ; 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 6EC2E606A32C; Fri, 4 Dec 2026 11:01:26 +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=z28UyWxJ Received: by smtp.kernel.org (Postfix) with ESMTPSA id A09491F000CE; Fri, 4 Dec 2026 11:01:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382086; bh=ARTGnNrSO4tFHHU9XCgcuV7kZBDf0Rybpra6iHcocfw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=z28UyWxJkd2dIbCPDaT4yOVWzSj1q0vJHTMfruUjse4WaR0RXzaeEJOYtE6zsAkj2 iJn2eqfNua4SNYvorIMm2v8NS1kDpAU4ORVEnThrldPNOoMdZJ82s2JiiHOJGhq1xI fyGXoMjcm0B6JACq+YblHUy+R3LxYgb2n9u0ttMc= 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.18 275/703] ftrace: Fix lost recursion records in ftrace_record_recursion() Date: Fri, 4 Dec 2026 19:02:54 +0900 Message-ID: <20261204100635.092479715@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-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