mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] x86/tracing: Flush persistent ring buffer cache on panic
@ 2026-12-02  5:34 Masami Hiramatsu (Google)
  2026-12-02 13:50 ` Steven Rostedt
  0 siblings, 1 reply; 4+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-02  5:34 UTC (permalink / raw)
  To: Steven Rostedt, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86
  Cc: H . Peter Anvin, Vincent Donnefort, Mathieu Desnoyers,
	Catalin Marinas, Geert Uytterhoeven, linux-trace-kernel,
	linux-kernel

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/tracing: Flush persistent ring buffer cache on panic
  2026-12-02  5:34 [PATCH] x86/tracing: Flush persistent ring buffer cache on panic Masami Hiramatsu (Google)
@ 2026-12-02 13:50 ` Steven Rostedt
  2026-12-03  2:07   ` Masami Hiramatsu (Google)
  0 siblings, 1 reply; 4+ messages in thread
From: Steven Rostedt @ 2026-12-02 13:50 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H . Peter Anvin, Vincent Donnefort, Mathieu Desnoyers,
	Catalin Marinas, Geert Uytterhoeven, linux-trace-kernel,
	linux-kernel

On Wed,  2 Dec 2026 14:34:40 +0900
"Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:

> 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>

Reviewed-by: Steven Rostedt <rostedt@goodmis.org>

-- Steve

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/tracing: Flush persistent ring buffer cache on panic
  2026-12-02 13:50 ` Steven Rostedt
@ 2026-12-03  2:07   ` Masami Hiramatsu (Google)
  2026-12-03  2:10     ` Steven Rostedt
  0 siblings, 1 reply; 4+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-03  2:07 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H . Peter Anvin, Vincent Donnefort, Mathieu Desnoyers,
	Catalin Marinas, Geert Uytterhoeven, linux-trace-kernel,
	linux-kernel

On Wed, 2 Dec 2026 08:50:57 -0500, Steven Rostedt <rostedt@goodmis.org> wrote:
> On Wed,  2 Dec 2026 14:34:40 +0900
> "Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:
> 
> > 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>
> 
> Reviewed-by: Steven Rostedt <rostedt@goodmis.org>

Thanks! BTW, should we ask x86 maintainers to pick this via -tip tree?

Thank you,

> 
> -- Steve
> 
>

-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/tracing: Flush persistent ring buffer cache on panic
  2026-12-03  2:07   ` Masami Hiramatsu (Google)
@ 2026-12-03  2:10     ` Steven Rostedt
  0 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2026-12-03  2:10 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H . Peter Anvin, Vincent Donnefort, Mathieu Desnoyers,
	Catalin Marinas, Geert Uytterhoeven, linux-trace-kernel,
	linux-kernel

On Thu, 03 Dec 2026 11:07:38 +0900
"Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:

> Thanks! BTW, should we ask x86 maintainers to pick this via -tip tree?

Yeah, I think this should go through the x86 tree.

-- Steve

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-03  2:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02  5:34 [PATCH] x86/tracing: Flush persistent ring buffer cache on panic Masami Hiramatsu (Google)
2026-12-02 13:50 ` Steven Rostedt
2026-12-03  2:07   ` Masami Hiramatsu (Google)
2026-12-03  2:10     ` Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®