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=1796189690; b=GmompPwCV+OKHvXT6dx2weo/Wk4LklmftT61igW7zji7ztmxhtegqHWLWc7erj0/t+S/ KgpSr5ubNm3DhFgb+nFRNjSLe8JdoheEG9wN1wYpHLuB2fdUnGjM5Fj7qiPATBZo+pvm2 o5odDcpMsb6FHUONwGDMt7eU8UbJ8lSKGql8d6SYNoYQie46bsLE3OSBbuyEOoJTXL2jB cXUdciVT8u5ztRHH2/N1XsX2yigV8pFqW5/7cqI45wlqwR+zk4HVWJ3fkzfkDJAjDOfVt W71Zhemu5lgkQuReNTVNfoLNyndQE9n+9CjSPdnlQ2B6dL1XH5qjjocKsI+NmuPCbIw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796189690; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=3Efr5pZdhmlo863GSgpvBerJD9tyFRK0K135fbnXOuY=; b=OVz3Qs4WY+MN5w026Tt+3AiqNDhAzULobSYUewomneWkuevujpTYFXyfixI6uWhF/4sl GUOdskIBGUmUX4ipXXT6ZEY8KuE9hObMyBZqv8/RTl2hiMaagAHFE8g+Sx2jX3LjzGu3a WpZbH2TrxdAmYrdNOS5euViyPbDt6g+sy3VnS/j+yqreiB/pFIL/L95XyblFvtB8Y3v0n 0BiARfJ4+gez5yAUSLyuShhnB8yy3ookqVfQm5NRrTJUx/nSvUht4mYYxD1D5zvNVXXhp thGHdITOjFH6ujyWCju5Iu+5BmkQlg8XzezjUKFv5URrAMpone/K8Ztp838d2BeEB/w== 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=YCyhIj5v; 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 3274A606A31A; Wed, 2 Dec 2026 05:34:50 +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=YCyhIj5v Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2E2841F000C1; Wed, 2 Dec 2026 05:34:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796189690; bh=3Efr5pZdhmlo863GSgpvBerJD9tyFRK0K135fbnXOuY=; h=From:To:Cc:Subject:Date; b=YCyhIj5vFhAj58X0WzuIBd5sPf44CH3NWj2c60D1iV8bR5RsDOuF+YtqXWU3m5tJe kPmMfLPSYyTxQCFgjGF5CPPvj4U2Ce3LWonz71Xhkjv/eIEkZBkFPAICt+HCyi1vvt 0XCc7eEBK2CoA8sAyfArNOegio+rk5EuKFFHVZFoCra+mMXHBkDLy60O6tGHiAU/JJ Tv9+ReycByFK2g2W0o7nn/5IY5iQ5TQrOQUKV6S+B9lhbrsQ7N7MHOyfCItLPAeLnJ RLthLSzJXy9nxpaxajYmiuz7jatg3w0UTDlQ2FwxKo7+8iLD9LWPwJUyBcCGzbNLSS JPpE2xvhbMapw== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org Cc: "H . Peter Anvin" , Vincent Donnefort , Mathieu Desnoyers , Catalin Marinas , Geert Uytterhoeven , linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] x86/tracing: Flush persistent ring buffer cache on panic Date: Wed, 2 Dec 2026 14:34:40 +0900 Message-ID: <20261202053440.2609843-1-mhiramat@kernel.org> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog X-Mailing-List: linux-kernel@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 Persistent ring buffers (configured via reserve_mem= and trace_instance=...@...) are mapped as write-back (WB) cached memory. While x86-TSO keeps caches coherent across CPUs during normal operation, it does not guarantee that dirty cache lines are written back to DRAM before a hard reset triggered on panic (for example, emergency_restart() via acpi_reboot() writing to port 0xcf9, where neither stop_this_cpu() on normal WB memory nor acpi_reboot() flushes CPU caches). Currently arch/x86/include/asm/Kbuild falls back to asm-generic/ring_buffer.h, leaving arch_ring_buffer_flush_range() as a no-op on x86. On real x86 hardware, a panic-triggered reboot therefore loses dirty cache lines in L1/L2/LLC for the persistent ring buffer: on the next boot, rb_validate_buffer() either detects inconsistent pages ("Ring buffer meta [N] invalid buffer page") and discards the affected CPU buffer, or validates up to a stale commit pointer in DRAM and silently drops the last several milliseconds of trace events immediately preceding the panic. Implement arch_ring_buffer_flush_range() for x86 using clflush_cache_range() (chunked by SZ_2G to accommodate unsigned long ranges against clflush_cache_range()'s unsigned int size parameter), falling back to wbinvd() if X86_FEATURE_CLFLUSH is not available. Because CLFLUSH/CLFLUSHOPT broadcast across the cache coherence domain and require no IPIs, locks, or sleeping, the panicking CPU can safely write back dirty lines from all halted CPUs in panic/NMI context. Tested on an x86_64 Intel system with repeated sysrq-c panic reboots: without this change, 0/5 runs preserved a trace_marker written immediately before panic and 1-4 CPUs per reboot reported invalid buffer pages; with this change, 5/5 runs preserved the final pre-panic trace_marker with 0 invalid buffer pages across all 8 CPUs. Fixes: a494d3c8d539 ("ring-buffer: Flush and stop persistent ring buffer on panic") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- arch/x86/include/asm/Kbuild | 1 - arch/x86/include/asm/ring_buffer.h | 31 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/ring_buffer.h diff --git a/arch/x86/include/asm/Kbuild b/arch/x86/include/asm/Kbuild index 47ef8cb482e3..b26bdb0f3699 100644 --- a/arch/x86/include/asm/Kbuild +++ b/arch/x86/include/asm/Kbuild @@ -14,5 +14,4 @@ generic-y += early_ioremap.h generic-y += fprobe.h generic-y += mcs_spinlock.h generic-y += mmzone.h -generic-y += ring_buffer.h generic-y += kexec_handover.h diff --git a/arch/x86/include/asm/ring_buffer.h b/arch/x86/include/asm/ring_buffer.h new file mode 100644 index 000000000000..2f0c03b009d0 --- /dev/null +++ b/arch/x86/include/asm/ring_buffer.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASM_X86_RING_BUFFER_H +#define _ASM_X86_RING_BUFFER_H + +#include +#include +#include +#include +#include + +/* Flush cache on persistent ring buffer */ +static inline void arch_ring_buffer_flush_range(unsigned long start, + unsigned long end) +{ + if (start >= end) + return; + + if (!cpu_feature_enabled(X86_FEATURE_CLFLUSH)) { + wbinvd(); + return; + } + + while (start < end) { + unsigned int size = min_t(unsigned long, end - start, SZ_2G); + + clflush_cache_range((void *)start, size); + start += size; + } +} + +#endif /* _ASM_X86_RING_BUFFER_H */ -- 2.56.1.911.g883d8bcb20-goog