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=1796383562; b=x/JvIhQdZZ/eN3LCH+1qt4eeFyxiL86Oi7qlWIvYukqe40+Bp6j5hCMp72SGTSPUJiLc AIzgBd3JHV6A5CyDRhJUKdT0RRaHheCJut7Q3QxvtmiDg5zDtSTm3wBd9+IDJ8JD1QYm1 aey9I1cKjbgjGTiO/Gwnl8LsimlNHFeogCvZIWGPsz7lqU4Q8Gc8rIil1AFz9g2iyxm+3 GaFMEsv3cJdbIIvOgSlAMyUVzlbH2GQ4LGi7XnEpkxVkV9nw2EuJqpLKP5GsqxmPmKo02 OMNXJ9/QSG3cNTpIIi1HR99n0VE+pf7MCU8D45UYx2go27CwDMxhMaNEHK4mO/INr4Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383562; 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=fCNYz2Ckjg5z1puvU+m8N4LeOlKErw7zmcUZul7Rvxc=; b=CIElm5/z8cBxkvsHWqXo8UlCnMSWtsVhgAAK90m2chjfTzV+qyU8XFrFDpzJnN3aFGG+ A4vDfhOb21YFhD25BkqjikldW6PKiAIXmk3BpmeQZIuWmo3/Fu+Vu5xllECN6gBrm8FGB OplG1xAG/QyMK6GD8zNkVuLEc5h+DjdA+bd9NTRJ0jYL5Rf60+VHZFSNcTRJHmKVb15+d +ZpKKfTABS64DFPl6Mq1BBfTB2NGjkONoWNhqO8I3Rej5ApeceUi1lSjTnNubokHqCZzt wH9qDJJ1sGy6fJnpFvVr6ctU+M63+JrCS/OkUekOfcw1PgcCt3Aj7+I9T1CJp8fvf8g== 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=FJ5vmRIZ; 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 51498606A322; Fri, 4 Dec 2026 11:26:02 +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=FJ5vmRIZ Received: by smtp.kernel.org (Postfix) with ESMTPSA id C09F31F000CE; Fri, 4 Dec 2026 11:26:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383562; bh=fCNYz2Ckjg5z1puvU+m8N4LeOlKErw7zmcUZul7Rvxc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FJ5vmRIZhK4DLDn69T9U73lXRJ925UAYmMxMd506cz5UnMp1uLM9OCBYl9wpgGQKp 9PFTNLhTeCE7KROGS6eTBRjWWt7h0wjy5ivoTgj/sGPdZ61YTYiqCxAX1bwdaFW/NB AQsXsDXK7yotBZDK7o7jMBitBk95A01xTKZ+bGiw= 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.12 272/555] ftrace: Fix lost recursion records in ftrace_record_recursion() Date: Fri, 4 Dec 2026 19:05:15 +0900 Message-ID: <20261204100619.517906598@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@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.12-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