Linux Trace Kernel
 help / color / mirror / Atom feed
From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org
Cc: "H . Peter Anvin" <hpa@zytor.com>,
	Vincent Donnefort <vdonnefort@google.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	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	[thread overview]
Message-ID: <20261202053440.2609843-1-mhiramat@kernel.org> (raw)

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) <mhiramat@kernel.org>
---
 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 <linux/cacheflush.h>
+#include <linux/minmax.h>
+#include <linux/sizes.h>
+#include <asm/cpufeature.h>
+#include <asm/special_insns.h>
+
+/* 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


                 reply	other threads:[~2026-12-02  5:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202053440.2609843-1-mhiramat@kernel.org \
    --to=mhiramat@kernel.org \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=geert@linux-m68k.org \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=tglx@kernel.org \
    --cc=vdonnefort@google.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox