From nobody Thu Apr 2 04:20:15 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6DCE13D171C; Mon, 30 Mar 2026 12:49:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774874999; cv=none; b=HoM3UZKKsi2FxTcK+8Nvd/ApUFN/Hc/eohUiNWGP+9ULfPd/yaO9sPZErO7EA63nZotJ5B6HpZzb9gTTySAHQluFYebdMJzTqvQNzjcuLs/c9BkWe54S7Wh1EW12VicgpFh7Zn+Oq0kfKId+7EdQ07TyYHMfVrk+u4QAMUAGb9I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774874999; c=relaxed/simple; bh=oxY5AjCtxEJ0v8z5z/jGzQ5+cr18tgOH/tiblbJmkzM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VqSY9XdM+cmbOeeW7pqA6KE9YmuwB/1Rlu06mC9e3NGlmUr98mxOhaYeb8G3jAHqdGFn1AiiXik+prOGlHfn1+hci6b96GtwH1TMu3dQQL/BHgDswqSIoNSZBfJ2NakG5/z71ZGt/dCGbsyLyqEJKtN/JPK1e7yt75N8FWrOkEI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=B9wKp2MN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="B9wKp2MN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D9B3C2BCB3; Mon, 30 Mar 2026 12:49:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774874999; bh=oxY5AjCtxEJ0v8z5z/jGzQ5+cr18tgOH/tiblbJmkzM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B9wKp2MNPSxASk9EulqrF/8YXCZRrbB0/cRILw3oaneLYln16v/P2JNSoV1q89qDF vcoWpz+4H9OiirP3N4uWF9WniAsCaKbkRNm+d50rW9BnNhEMqhXCu3bKJNPhtE7lHF mnK6x52xIxeSJ0594XwMKmGmSfM7oaWeXFM28vKxZjvtUiSkU0YEdscbyqZK7MpSnX hVjWWw36EDTv1VBkVm1T0PvNnlj0A0dKs3w/QaCskfn5oE8Z9lcJyrrZ7f0a8uOxf6 yYHZC/n/cqEyMjyNQymW2Cj/b5PmDFoW0xxx0XIyzv5bupOPECcJIovMZGiEuitCA8 CDu0X3ACcAZyw== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Ian Rogers Subject: [PATCH v14 1/5] ring-buffer: Flush and stop persistent ring buffer on panic Date: Mon, 30 Mar 2026 21:49:56 +0900 Message-ID: <177487499643.3463592.15413057950716995168.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.1018.g2bb0e51243-goog In-Reply-To: <177487498530.3463592.12715592581212799257.stgit@mhiramat.tok.corp.google.com> References: <177487498530.3463592.12715592581212799257.stgit@mhiramat.tok.corp.google.com> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) On real hardware, panic and machine reboot may not flush hardware cache to memory. This means the persistent ring buffer, which relies on a coherent state of memory, may not have its events written to the buffer and they may be lost. Moreover, there may be inconsistency with the counters which are used for validation of the integrity of the persistent ring buffer which may cause all data to be discarded. To avoid this issue, stop recording of the ring buffer on panic and flush the cache of the ring buffer's memory. Fixes: e645535a954a ("tracing: Add option to use memmapped memory for trace= boot instance") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- Changes in v13: - Fix a rebase conflict. Changes in v11: - Do nothing by default since flush_cache_vmap() does nothing on x86 but it can cause deadlock on some architectures via on_each_cpu() because other CPUs will be stoppped when panic notifier is called. Changes in v9: - Fix typo of & to &&. - Fix typo of "Generic" Changes in v6: - Introduce asm/ring_buffer.h for arch_ring_buffer_flush_range(). - Use flush_cache_vmap() instead of flush_cache_all(). Changes in v5: - Use ring_buffer_record_off() instead of ring_buffer_record_disable(). - Use flush_cache_all() to ensure flush all cache. Changes in v3: - update patch description. --- arch/alpha/include/asm/Kbuild | 1 + arch/arc/include/asm/Kbuild | 1 + arch/arm/include/asm/Kbuild | 1 + arch/arm64/include/asm/ring_buffer.h | 10 ++++++++++ arch/csky/include/asm/Kbuild | 1 + arch/hexagon/include/asm/Kbuild | 1 + arch/loongarch/include/asm/Kbuild | 1 + arch/m68k/include/asm/Kbuild | 1 + arch/microblaze/include/asm/Kbuild | 1 + arch/mips/include/asm/Kbuild | 1 + arch/nios2/include/asm/Kbuild | 1 + arch/openrisc/include/asm/Kbuild | 1 + arch/parisc/include/asm/Kbuild | 1 + arch/powerpc/include/asm/Kbuild | 1 + arch/riscv/include/asm/Kbuild | 1 + arch/s390/include/asm/Kbuild | 1 + arch/sh/include/asm/Kbuild | 1 + arch/sparc/include/asm/Kbuild | 1 + arch/um/include/asm/Kbuild | 1 + arch/x86/include/asm/Kbuild | 1 + arch/xtensa/include/asm/Kbuild | 1 + include/asm-generic/ring_buffer.h | 13 +++++++++++++ kernel/trace/ring_buffer.c | 22 ++++++++++++++++++++++ 23 files changed, 65 insertions(+) create mode 100644 arch/arm64/include/asm/ring_buffer.h create mode 100644 include/asm-generic/ring_buffer.h diff --git a/arch/alpha/include/asm/Kbuild b/arch/alpha/include/asm/Kbuild index 483965c5a4de..b154b4e3dfa8 100644 --- a/arch/alpha/include/asm/Kbuild +++ b/arch/alpha/include/asm/Kbuild @@ -5,4 +5,5 @@ generic-y +=3D agp.h generic-y +=3D asm-offsets.h generic-y +=3D kvm_para.h generic-y +=3D mcs_spinlock.h +generic-y +=3D ring_buffer.h generic-y +=3D text-patching.h diff --git a/arch/arc/include/asm/Kbuild b/arch/arc/include/asm/Kbuild index 4c69522e0328..483caacc6988 100644 --- a/arch/arc/include/asm/Kbuild +++ b/arch/arc/include/asm/Kbuild @@ -5,5 +5,6 @@ generic-y +=3D extable.h generic-y +=3D kvm_para.h generic-y +=3D mcs_spinlock.h generic-y +=3D parport.h +generic-y +=3D ring_buffer.h generic-y +=3D user.h generic-y +=3D text-patching.h diff --git a/arch/arm/include/asm/Kbuild b/arch/arm/include/asm/Kbuild index 03657ff8fbe3..decad5f2c826 100644 --- a/arch/arm/include/asm/Kbuild +++ b/arch/arm/include/asm/Kbuild @@ -3,6 +3,7 @@ generic-y +=3D early_ioremap.h generic-y +=3D extable.h generic-y +=3D flat.h generic-y +=3D parport.h +generic-y +=3D ring_buffer.h =20 generated-y +=3D mach-types.h generated-y +=3D unistd-nr.h diff --git a/arch/arm64/include/asm/ring_buffer.h b/arch/arm64/include/asm/= ring_buffer.h new file mode 100644 index 000000000000..62316c406888 --- /dev/null +++ b/arch/arm64/include/asm/ring_buffer.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASM_ARM64_RING_BUFFER_H +#define _ASM_ARM64_RING_BUFFER_H + +#include + +/* Flush D-cache on persistent ring buffer */ +#define arch_ring_buffer_flush_range(start, end) dcache_clean_pop(start, e= nd) + +#endif /* _ASM_ARM64_RING_BUFFER_H */ diff --git a/arch/csky/include/asm/Kbuild b/arch/csky/include/asm/Kbuild index 3a5c7f6e5aac..7dca0c6cdc84 100644 --- a/arch/csky/include/asm/Kbuild +++ b/arch/csky/include/asm/Kbuild @@ -9,6 +9,7 @@ generic-y +=3D qrwlock.h generic-y +=3D qrwlock_types.h generic-y +=3D qspinlock.h generic-y +=3D parport.h +generic-y +=3D ring_buffer.h generic-y +=3D user.h generic-y +=3D vmlinux.lds.h generic-y +=3D text-patching.h diff --git a/arch/hexagon/include/asm/Kbuild b/arch/hexagon/include/asm/Kbu= ild index 1efa1e993d4b..0f887d4238ed 100644 --- a/arch/hexagon/include/asm/Kbuild +++ b/arch/hexagon/include/asm/Kbuild @@ -5,4 +5,5 @@ generic-y +=3D extable.h generic-y +=3D iomap.h generic-y +=3D kvm_para.h generic-y +=3D mcs_spinlock.h +generic-y +=3D ring_buffer.h generic-y +=3D text-patching.h diff --git a/arch/loongarch/include/asm/Kbuild b/arch/loongarch/include/asm= /Kbuild index 9034b583a88a..7e92957baf6a 100644 --- a/arch/loongarch/include/asm/Kbuild +++ b/arch/loongarch/include/asm/Kbuild @@ -10,5 +10,6 @@ generic-y +=3D qrwlock.h generic-y +=3D user.h generic-y +=3D ioctl.h generic-y +=3D mmzone.h +generic-y +=3D ring_buffer.h generic-y +=3D statfs.h generic-y +=3D text-patching.h diff --git a/arch/m68k/include/asm/Kbuild b/arch/m68k/include/asm/Kbuild index b282e0dd8dc1..62543bf305ff 100644 --- a/arch/m68k/include/asm/Kbuild +++ b/arch/m68k/include/asm/Kbuild @@ -3,5 +3,6 @@ generated-y +=3D syscall_table.h generic-y +=3D extable.h generic-y +=3D kvm_para.h generic-y +=3D mcs_spinlock.h +generic-y +=3D ring_buffer.h generic-y +=3D spinlock.h generic-y +=3D text-patching.h diff --git a/arch/microblaze/include/asm/Kbuild b/arch/microblaze/include/a= sm/Kbuild index 7178f990e8b3..0030309b47ad 100644 --- a/arch/microblaze/include/asm/Kbuild +++ b/arch/microblaze/include/asm/Kbuild @@ -5,6 +5,7 @@ generic-y +=3D extable.h generic-y +=3D kvm_para.h generic-y +=3D mcs_spinlock.h generic-y +=3D parport.h +generic-y +=3D ring_buffer.h generic-y +=3D syscalls.h generic-y +=3D tlb.h generic-y +=3D user.h diff --git a/arch/mips/include/asm/Kbuild b/arch/mips/include/asm/Kbuild index 684569b2ecd6..9771c3d85074 100644 --- a/arch/mips/include/asm/Kbuild +++ b/arch/mips/include/asm/Kbuild @@ -12,5 +12,6 @@ generic-y +=3D mcs_spinlock.h generic-y +=3D parport.h generic-y +=3D qrwlock.h generic-y +=3D qspinlock.h +generic-y +=3D ring_buffer.h generic-y +=3D user.h generic-y +=3D text-patching.h diff --git a/arch/nios2/include/asm/Kbuild b/arch/nios2/include/asm/Kbuild index 28004301c236..0a2530964413 100644 --- a/arch/nios2/include/asm/Kbuild +++ b/arch/nios2/include/asm/Kbuild @@ -5,6 +5,7 @@ generic-y +=3D cmpxchg.h generic-y +=3D extable.h generic-y +=3D kvm_para.h generic-y +=3D mcs_spinlock.h +generic-y +=3D ring_buffer.h generic-y +=3D spinlock.h generic-y +=3D user.h generic-y +=3D text-patching.h diff --git a/arch/openrisc/include/asm/Kbuild b/arch/openrisc/include/asm/K= build index cef49d60d74c..8aa34621702d 100644 --- a/arch/openrisc/include/asm/Kbuild +++ b/arch/openrisc/include/asm/Kbuild @@ -8,4 +8,5 @@ generic-y +=3D spinlock_types.h generic-y +=3D spinlock.h generic-y +=3D qrwlock_types.h generic-y +=3D qrwlock.h +generic-y +=3D ring_buffer.h generic-y +=3D user.h diff --git a/arch/parisc/include/asm/Kbuild b/arch/parisc/include/asm/Kbuild index 4fb596d94c89..d48d158f7241 100644 --- a/arch/parisc/include/asm/Kbuild +++ b/arch/parisc/include/asm/Kbuild @@ -4,4 +4,5 @@ generated-y +=3D syscall_table_64.h generic-y +=3D agp.h generic-y +=3D kvm_para.h generic-y +=3D mcs_spinlock.h +generic-y +=3D ring_buffer.h generic-y +=3D user.h diff --git a/arch/powerpc/include/asm/Kbuild b/arch/powerpc/include/asm/Kbu= ild index 2e23533b67e3..805b5aeebb6f 100644 --- a/arch/powerpc/include/asm/Kbuild +++ b/arch/powerpc/include/asm/Kbuild @@ -5,4 +5,5 @@ generated-y +=3D syscall_table_spu.h generic-y +=3D agp.h generic-y +=3D mcs_spinlock.h generic-y +=3D qrwlock.h +generic-y +=3D ring_buffer.h generic-y +=3D early_ioremap.h diff --git a/arch/riscv/include/asm/Kbuild b/arch/riscv/include/asm/Kbuild index bd5fc9403295..7721b63642f4 100644 --- a/arch/riscv/include/asm/Kbuild +++ b/arch/riscv/include/asm/Kbuild @@ -14,5 +14,6 @@ generic-y +=3D ticket_spinlock.h generic-y +=3D qrwlock.h generic-y +=3D qrwlock_types.h generic-y +=3D qspinlock.h +generic-y +=3D ring_buffer.h generic-y +=3D user.h generic-y +=3D vmlinux.lds.h diff --git a/arch/s390/include/asm/Kbuild b/arch/s390/include/asm/Kbuild index 80bad7de7a04..0c1fc47c3ba0 100644 --- a/arch/s390/include/asm/Kbuild +++ b/arch/s390/include/asm/Kbuild @@ -7,3 +7,4 @@ generated-y +=3D unistd_nr.h generic-y +=3D asm-offsets.h generic-y +=3D mcs_spinlock.h generic-y +=3D mmzone.h +generic-y +=3D ring_buffer.h diff --git a/arch/sh/include/asm/Kbuild b/arch/sh/include/asm/Kbuild index 4d3f10ed8275..f0403d3ee8ab 100644 --- a/arch/sh/include/asm/Kbuild +++ b/arch/sh/include/asm/Kbuild @@ -3,4 +3,5 @@ generated-y +=3D syscall_table.h generic-y +=3D kvm_para.h generic-y +=3D mcs_spinlock.h generic-y +=3D parport.h +generic-y +=3D ring_buffer.h generic-y +=3D text-patching.h diff --git a/arch/sparc/include/asm/Kbuild b/arch/sparc/include/asm/Kbuild index 17ee8a273aa6..49c6bb326b75 100644 --- a/arch/sparc/include/asm/Kbuild +++ b/arch/sparc/include/asm/Kbuild @@ -4,4 +4,5 @@ generated-y +=3D syscall_table_64.h generic-y +=3D agp.h generic-y +=3D kvm_para.h generic-y +=3D mcs_spinlock.h +generic-y +=3D ring_buffer.h generic-y +=3D text-patching.h diff --git a/arch/um/include/asm/Kbuild b/arch/um/include/asm/Kbuild index 1b9b82bbe322..2a1629ba8140 100644 --- a/arch/um/include/asm/Kbuild +++ b/arch/um/include/asm/Kbuild @@ -17,6 +17,7 @@ generic-y +=3D module.lds.h generic-y +=3D parport.h generic-y +=3D percpu.h generic-y +=3D preempt.h +generic-y +=3D ring_buffer.h generic-y +=3D runtime-const.h generic-y +=3D softirq_stack.h generic-y +=3D switch_to.h diff --git a/arch/x86/include/asm/Kbuild b/arch/x86/include/asm/Kbuild index 4566000e15c4..078fd2c0d69d 100644 --- a/arch/x86/include/asm/Kbuild +++ b/arch/x86/include/asm/Kbuild @@ -14,3 +14,4 @@ generic-y +=3D early_ioremap.h generic-y +=3D fprobe.h generic-y +=3D mcs_spinlock.h generic-y +=3D mmzone.h +generic-y +=3D ring_buffer.h diff --git a/arch/xtensa/include/asm/Kbuild b/arch/xtensa/include/asm/Kbuild index 13fe45dea296..e57af619263a 100644 --- a/arch/xtensa/include/asm/Kbuild +++ b/arch/xtensa/include/asm/Kbuild @@ -6,5 +6,6 @@ generic-y +=3D mcs_spinlock.h generic-y +=3D parport.h generic-y +=3D qrwlock.h generic-y +=3D qspinlock.h +generic-y +=3D ring_buffer.h generic-y +=3D user.h generic-y +=3D text-patching.h diff --git a/include/asm-generic/ring_buffer.h b/include/asm-generic/ring_b= uffer.h new file mode 100644 index 000000000000..201d2aee1005 --- /dev/null +++ b/include/asm-generic/ring_buffer.h @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Generic arch dependent ring_buffer macros. + */ +#ifndef __ASM_GENERIC_RING_BUFFER_H__ +#define __ASM_GENERIC_RING_BUFFER_H__ + +#include + +/* Flush cache on ring buffer range if needed. Do nothing by default. */ +#define arch_ring_buffer_flush_range(start, end) do { } while (0) + +#endif /* __ASM_GENERIC_RING_BUFFER_H__ */ diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 8b6c39bba56d..3e793bd1c134 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include #include =20 +#include #include #include #include @@ -559,6 +561,7 @@ struct trace_buffer { =20 unsigned long range_addr_start; unsigned long range_addr_end; + struct notifier_block flush_nb; =20 struct ring_buffer_meta *meta; =20 @@ -2520,6 +2523,16 @@ static void rb_free_cpu_buffer(struct ring_buffer_pe= r_cpu *cpu_buffer) kfree(cpu_buffer); } =20 +/* Stop recording on a persistent buffer and flush cache if needed. */ +static int rb_flush_buffer_cb(struct notifier_block *nb, unsigned long eve= nt, void *data) +{ + struct trace_buffer *buffer =3D container_of(nb, struct trace_buffer, flu= sh_nb); + + ring_buffer_record_off(buffer); + arch_ring_buffer_flush_range(buffer->range_addr_start, buffer->range_addr= _end); + return NOTIFY_DONE; +} + static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flag= s, int order, unsigned long start, unsigned long end, @@ -2650,6 +2663,12 @@ static struct trace_buffer *alloc_buffer(unsigned lo= ng size, unsigned flags, =20 mutex_init(&buffer->mutex); =20 + /* Persistent ring buffer needs to flush cache before reboot. */ + if (start && end) { + buffer->flush_nb.notifier_call =3D rb_flush_buffer_cb; + atomic_notifier_chain_register(&panic_notifier_list, &buffer->flush_nb); + } + return_ptr(buffer); =20 fail_free_buffers: @@ -2748,6 +2767,9 @@ ring_buffer_free(struct trace_buffer *buffer) { int cpu; =20 + if (buffer->range_addr_start && buffer->range_addr_end) + atomic_notifier_chain_unregister(&panic_notifier_list, &buffer->flush_nb= ); + cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node); =20 irq_work_sync(&buffer->irq_work.work); From nobody Thu Apr 2 04:20:15 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D0BE3B6363; Mon, 30 Mar 2026 12:50:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875007; cv=none; b=LZf0OFlagmE5CjP9orNCF8LXrx3MHfA/vsthvkQAmvzvF4iK4o6qdDfFX7S/26nxBFzFMF8Vm6+Zsi7wzY8f6VnDfZEZxyGQ6+ugRqrEkeb2Sk47KKO3t2viN1pUIswQK9r3WJcJBZiisGRv5sIMKIQ1yoODeTLOF5jT+OQiytU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875007; c=relaxed/simple; bh=Bwijqm6mp2Mh++9T85hwk5ktALNNxioqXnq/4OcvvBw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JsCqK+688hEc4mbfRXTYMCr1rPcXKUOpwJE8zsikoOrvKxIG1b1oYPm6M/DZKJs1iliLr4/Ciz0wvK4caVRrmjIFhyoK8LPbNT1wtqUz92eWG7dfkNfdWacMoFLdRGjBSvyB4xilR3gsG6mpzaMUxAUWiMJIFwj8n4sNM/SR7QE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JxTZFY44; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JxTZFY44" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0362FC4CEF7; Mon, 30 Mar 2026 12:50:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774875006; bh=Bwijqm6mp2Mh++9T85hwk5ktALNNxioqXnq/4OcvvBw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JxTZFY4458t+SIjwCVPItvJXhdWLgfYdC6pe54AS7bIsQjBwh7EZkOnUoycWPIMFs BrQjWN+sKFGS3JJ9UTAv4ulnxcDbnw8tZI3qTWeNRJ+ybvP3b0YAT47hvs0Itt3nUH 657EJFDqE6C7KNb2hJgq+WeU0FhizkxQiC6YAzTiunNiZnw/GVBSeC38M7fHJB+3Gf FhBQ0A7YtVUwQlWXNHGYcGMHb43Y/CjUSwCEKQk1KgzI9frX58/oE48AJBuSOT+Czj ok9ruYldf/JqcFaOFyFQRtunUTOQ0O0UVe+9LQFCNh1nvHXZkGP2I3T2AgBUf3yryI GfhGOWPlM3uLA== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Ian Rogers Subject: [PATCH v14 2/5] ring-buffer: Skip invalid sub-buffers when validating persistent ring buffer Date: Mon, 30 Mar 2026 21:50:04 +0900 Message-ID: <177487500432.3463592.13753720277119177967.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.1018.g2bb0e51243-goog In-Reply-To: <177487498530.3463592.12715592581212799257.stgit@mhiramat.tok.corp.google.com> References: <177487498530.3463592.12715592581212799257.stgit@mhiramat.tok.corp.google.com> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Skip invalid sub-buffers when validating the persistent ring buffer instead of discarding the entire ring buffer. Only skipped buffers are invalidated (cleared). If the cache data in memory fails to be synchronized during a reboot, the persistent ring buffer may become partially corrupted, but other sub-buffers may still contain readable event data. Only discard the subbuffers that are found to be corrupted. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v11: - Fix a typo. Changes in v9: - Add meta->subbuf_size check. - Fix a typo. - Handle invalid reader_page case. Changes in v8: - Add comment in rb_valudate_buffer() - Clear the RB_MISSED_* flags in rb_valudate_buffer() instead of skipping subbuf. - Remove unused subbuf local variable from rb_cpu_meta_valid(). Changes in v7: - Combined with Handling RB_MISSED_* flags patch, focus on validation at = boot. - Remove checking subbuffer data when validating metadata, because it sho= uld be done later. - Do not mark the discarded sub buffer page but just reset it. Changes in v6: - Show invalid page detection message once per CPU. Changes in v5: - Instead of showing errors for each page, just show the number of discarded pages at last. Changes in v3: - Record missed data event on commit. --- kernel/trace/ring_buffer.c | 98 ++++++++++++++++++++++++++--------------= ---- 1 file changed, 58 insertions(+), 40 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 3e793bd1c134..31cad8edd488 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -370,6 +370,12 @@ static __always_inline unsigned int rb_page_commit(str= uct buffer_page *bpage) return local_read(&bpage->page->commit); } =20 +/* Size is determined by what has been committed */ +static __always_inline unsigned int rb_page_size(struct buffer_page *bpage) +{ + return rb_page_commit(bpage) & ~RB_MISSED_MASK; +} + static void free_buffer_page(struct buffer_page *bpage) { /* Range pages are not to be freed */ @@ -1762,7 +1768,6 @@ static bool rb_cpu_meta_valid(struct ring_buffer_cpu_= meta *meta, int cpu, unsigned long *subbuf_mask) { int subbuf_size =3D PAGE_SIZE; - struct buffer_data_page *subbuf; unsigned long buffers_start; unsigned long buffers_end; int i; @@ -1770,6 +1775,11 @@ static bool rb_cpu_meta_valid(struct ring_buffer_cpu= _meta *meta, int cpu, if (!subbuf_mask) return false; =20 + if (meta->subbuf_size !=3D PAGE_SIZE) { + pr_info("Ring buffer boot meta [%d] invalid subbuf_size\n", cpu); + return false; + } + buffers_start =3D meta->first_buffer; buffers_end =3D meta->first_buffer + (subbuf_size * meta->nr_subbufs); =20 @@ -1786,11 +1796,12 @@ static bool rb_cpu_meta_valid(struct ring_buffer_cp= u_meta *meta, int cpu, return false; } =20 - subbuf =3D rb_subbufs_from_meta(meta); - bitmap_clear(subbuf_mask, 0, meta->nr_subbufs); =20 - /* Is the meta buffers and the subbufs themselves have correct data? */ + /* + * Ensure the meta::buffers array has correct data. The data in each subb= ufs + * are checked later in rb_meta_validate_events(). + */ for (i =3D 0; i < meta->nr_subbufs; i++) { if (meta->buffers[i] < 0 || meta->buffers[i] >=3D meta->nr_subbufs) { @@ -1798,18 +1809,12 @@ static bool rb_cpu_meta_valid(struct ring_buffer_cp= u_meta *meta, int cpu, return false; } =20 - if ((unsigned)local_read(&subbuf->commit) > subbuf_size) { - pr_info("Ring buffer boot meta [%d] buffer invalid commit\n", cpu); - return false; - } - if (test_bit(meta->buffers[i], subbuf_mask)) { pr_info("Ring buffer boot meta [%d] array has duplicates\n", cpu); return false; } =20 set_bit(meta->buffers[i], subbuf_mask); - subbuf =3D (void *)subbuf + subbuf_size; } =20 return true; @@ -1873,13 +1878,22 @@ static int rb_read_data_buffer(struct buffer_data_p= age *dpage, int tail, int cpu return events; } =20 -static int rb_validate_buffer(struct buffer_data_page *dpage, int cpu) +static int rb_validate_buffer(struct buffer_data_page *dpage, int cpu, + struct ring_buffer_cpu_meta *meta) { unsigned long long ts; + unsigned long tail; u64 delta; - int tail; =20 - tail =3D local_read(&dpage->commit); + /* + * When a sub-buffer is recovered from a read, the commit value may + * have RB_MISSED_* bits set, as these bits are reset on reuse. + * Even after clearing these bits, a commit value greater than the + * subbuf_size is considered invalid. + */ + tail =3D local_read(&dpage->commit) & ~RB_MISSED_MASK; + if (tail > meta->subbuf_size) + return -1; return rb_read_data_buffer(dpage, tail, cpu, &ts, &delta); } =20 @@ -1890,6 +1904,7 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) struct buffer_page *head_page, *orig_head; unsigned long entry_bytes =3D 0; unsigned long entries =3D 0; + int discarded =3D 0; int ret; u64 ts; int i; @@ -1900,14 +1915,19 @@ static void rb_meta_validate_events(struct ring_buf= fer_per_cpu *cpu_buffer) orig_head =3D head_page =3D cpu_buffer->head_page; =20 /* Do the reader page first */ - ret =3D rb_validate_buffer(cpu_buffer->reader_page->page, cpu_buffer->cpu= ); + ret =3D rb_validate_buffer(cpu_buffer->reader_page->page, cpu_buffer->cpu= , meta); if (ret < 0) { - pr_info("Ring buffer reader page is invalid\n"); - goto invalid; + pr_info("Ring buffer meta [%d] invalid reader page detected\n", + cpu_buffer->cpu); + discarded++; + /* Instead of discard whole ring buffer, discard only this sub-buffer. */ + local_set(&cpu_buffer->reader_page->entries, 0); + local_set(&cpu_buffer->reader_page->page->commit, 0); + } else { + entries +=3D ret; + entry_bytes +=3D rb_page_size(cpu_buffer->reader_page); + local_set(&cpu_buffer->reader_page->entries, ret); } - entries +=3D ret; - entry_bytes +=3D local_read(&cpu_buffer->reader_page->page->commit); - local_set(&cpu_buffer->reader_page->entries, ret); =20 ts =3D head_page->page->time_stamp; =20 @@ -1935,7 +1955,7 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) break; =20 /* Stop rewind if the page is invalid. */ - ret =3D rb_validate_buffer(head_page->page, cpu_buffer->cpu); + ret =3D rb_validate_buffer(head_page->page, cpu_buffer->cpu, meta); if (ret < 0) break; =20 @@ -2014,21 +2034,24 @@ static void rb_meta_validate_events(struct ring_buf= fer_per_cpu *cpu_buffer) if (head_page =3D=3D cpu_buffer->reader_page) continue; =20 - ret =3D rb_validate_buffer(head_page->page, cpu_buffer->cpu); + ret =3D rb_validate_buffer(head_page->page, cpu_buffer->cpu, meta); if (ret < 0) { - pr_info("Ring buffer meta [%d] invalid buffer page\n", - cpu_buffer->cpu); - goto invalid; - } - - /* If the buffer has content, update pages_touched */ - if (ret) - local_inc(&cpu_buffer->pages_touched); - - entries +=3D ret; - entry_bytes +=3D local_read(&head_page->page->commit); - local_set(&head_page->entries, ret); + if (!discarded) + pr_info("Ring buffer meta [%d] invalid buffer page detected\n", + cpu_buffer->cpu); + discarded++; + /* Instead of discard whole ring buffer, discard only this sub-buffer. = */ + local_set(&head_page->entries, 0); + local_set(&head_page->page->commit, 0); + } else { + /* If the buffer has content, update pages_touched */ + if (ret) + local_inc(&cpu_buffer->pages_touched); =20 + entries +=3D ret; + entry_bytes +=3D rb_page_size(head_page); + local_set(&head_page->entries, ret); + } if (head_page =3D=3D cpu_buffer->commit_page) break; } @@ -2042,7 +2065,8 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) local_set(&cpu_buffer->entries, entries); local_set(&cpu_buffer->entries_bytes, entry_bytes); =20 - pr_info("Ring buffer meta [%d] is from previous boot!\n", cpu_buffer->cpu= ); + pr_info("Ring buffer meta [%d] is from previous boot! (%d pages discarded= )\n", + cpu_buffer->cpu, discarded); return; =20 invalid: @@ -3329,12 +3353,6 @@ rb_iter_head_event(struct ring_buffer_iter *iter) return NULL; } =20 -/* Size is determined by what has been committed */ -static __always_inline unsigned rb_page_size(struct buffer_page *bpage) -{ - return rb_page_commit(bpage) & ~RB_MISSED_MASK; -} - static __always_inline unsigned rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer) { From nobody Thu Apr 2 04:20:15 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E2EC53CAE89; Mon, 30 Mar 2026 12:50:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875015; cv=none; b=JGmeFCtphf7zDLun/JPs3WyX2FQ4TEeDp5hHaYbWIrAQWd1WG8ZfoxSZJmxC5TRpC7RIuivKEb/OeCyUoRSimik4ELp/wvzDABzqLLjOyChcAuWSpyeYa7zaHD5LkAKVchtd3ew0OphX7Loeg7oumOp4XI2s1Qm5Q1mY/wObNOo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875015; c=relaxed/simple; bh=IRjdI8UdKKsi6dG8Or3gV8oUCFYxFsta5yCcLV1LQn0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=UsJHLv2N5+XHyqsfhzWlRWVxvw0KWaK7MZshVaNy52TcRSeHL5jmS0A5sagrWJ1GcwOaSvQjdlOPalodawMp4mssGD+sEuNxbaSnEZnKctFW7tms1E29HKOhXwjIVnuogTBFrEoaK4rqndhzLk8h8G/r3CvRYElS1gPj3wy7b44= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TMrbPaCF; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TMrbPaCF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8004C4CEF7; Mon, 30 Mar 2026 12:50:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774875014; bh=IRjdI8UdKKsi6dG8Or3gV8oUCFYxFsta5yCcLV1LQn0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TMrbPaCF7Z72+oj8JADLFzcZCZ3ZhqOrTcjeAsPjn+gBbQJQFt7IZecmI4SaawJZm 2QwY1BKxjyCBXXy7WTgZJ0zQqm/yE+wJ5Yff2bK13246vIT+yTG3C8wBoJ1jgmq/Vp A0tNq0xRZhk83DrWb5JNMV34qILan5mP+P+vQSU7sR1FOoQNClSLdUrTOsao28sJHz tX+ODIsx1XE3pj+BBA7LmK9/rz5azaZ3j3ejXbNDh/wFwPKhnZRGhCFSyfvUBkS387 8iSrmG5A+NqQIPPNK6cAuUX3ToTn343kTFMQYym5STlhhDAYxv1nalqkT8rCoobzen xBnUwWroO5n+Q== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Ian Rogers Subject: [PATCH v14 3/5] ring-buffer: Skip invalid sub-buffers when rewinding persistent ring buffer Date: Mon, 30 Mar 2026 21:50:12 +0900 Message-ID: <177487501208.3463592.7006376990661947220.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.1018.g2bb0e51243-goog In-Reply-To: <177487498530.3463592.12715592581212799257.stgit@mhiramat.tok.corp.google.com> References: <177487498530.3463592.12715592581212799257.stgit@mhiramat.tok.corp.google.com> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Skip invalid sub-buffers when rewinding the persistent ring buffer instead of stopping the rewinding the ring buffer. The skipped buffers are cleared. To ensure the rewinding stops at the unused page, this also clears buffer_data_page::time_stamp when tracing resets the buffer. This allows us to identify unused pages and empty pages. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v12: - Fix build error. Changes in v11: - Reset timestamp when the buffer is invalid. - When rewinding, skip subbuf page if timestamp is wrong and check timestamp after validating buffer data page. Changes in v10: - Newly added. --- kernel/trace/ring_buffer.c | 76 +++++++++++++++++++++++++---------------= ---- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 31cad8edd488..e5178239f2f9 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -363,6 +363,7 @@ struct buffer_page { static void rb_init_page(struct buffer_data_page *bpage) { local_set(&bpage->commit, 0); + bpage->time_stamp =3D 0; } =20 static __always_inline unsigned int rb_page_commit(struct buffer_page *bpa= ge) @@ -1878,12 +1879,14 @@ static int rb_read_data_buffer(struct buffer_data_p= age *dpage, int tail, int cpu return events; } =20 -static int rb_validate_buffer(struct buffer_data_page *dpage, int cpu, +static int rb_validate_buffer(struct buffer_page *bpage, int cpu, struct ring_buffer_cpu_meta *meta) { + struct buffer_data_page *dpage =3D bpage->page; unsigned long long ts; unsigned long tail; u64 delta; + int ret =3D -1; =20 /* * When a sub-buffer is recovered from a read, the commit value may @@ -1892,9 +1895,17 @@ static int rb_validate_buffer(struct buffer_data_pag= e *dpage, int cpu, * subbuf_size is considered invalid. */ tail =3D local_read(&dpage->commit) & ~RB_MISSED_MASK; - if (tail > meta->subbuf_size) - return -1; - return rb_read_data_buffer(dpage, tail, cpu, &ts, &delta); + if (tail <=3D meta->subbuf_size) + ret =3D rb_read_data_buffer(dpage, tail, cpu, &ts, &delta); + + if (ret < 0) { + local_set(&bpage->entries, 0); + local_set(&bpage->page->commit, 0); + } else { + local_set(&bpage->entries, ret); + } + + return ret; } =20 /* If the meta data has been validated, now validate the events */ @@ -1915,18 +1926,14 @@ static void rb_meta_validate_events(struct ring_buf= fer_per_cpu *cpu_buffer) orig_head =3D head_page =3D cpu_buffer->head_page; =20 /* Do the reader page first */ - ret =3D rb_validate_buffer(cpu_buffer->reader_page->page, cpu_buffer->cpu= , meta); + ret =3D rb_validate_buffer(cpu_buffer->reader_page, cpu_buffer->cpu, meta= ); if (ret < 0) { pr_info("Ring buffer meta [%d] invalid reader page detected\n", cpu_buffer->cpu); discarded++; - /* Instead of discard whole ring buffer, discard only this sub-buffer. */ - local_set(&cpu_buffer->reader_page->entries, 0); - local_set(&cpu_buffer->reader_page->page->commit, 0); } else { entries +=3D ret; entry_bytes +=3D rb_page_size(cpu_buffer->reader_page); - local_set(&cpu_buffer->reader_page->entries, ret); } =20 ts =3D head_page->page->time_stamp; @@ -1945,26 +1952,33 @@ static void rb_meta_validate_events(struct ring_buf= fer_per_cpu *cpu_buffer) if (head_page =3D=3D cpu_buffer->tail_page) break; =20 - /* Ensure the page has older data than head. */ - if (ts < head_page->page->time_stamp) - break; - - ts =3D head_page->page->time_stamp; - /* Ensure the page has correct timestamp and some data. */ - if (!ts || rb_page_commit(head_page) =3D=3D 0) - break; - - /* Stop rewind if the page is invalid. */ - ret =3D rb_validate_buffer(head_page->page, cpu_buffer->cpu, meta); - if (ret < 0) + /* Rewind until unused page (no timestamp, no commit). */ + if (!head_page->page->time_stamp && rb_page_commit(head_page) =3D=3D 0) break; =20 - /* Recover the number of entries and update stats. */ - local_set(&head_page->entries, ret); - if (ret) - local_inc(&cpu_buffer->pages_touched); - entries +=3D ret; - entry_bytes +=3D rb_page_commit(head_page); + /* + * Skip if the page is invalid, or its timestamp is newer than the + * previous valid page. + */ + ret =3D rb_validate_buffer(head_page, cpu_buffer->cpu, meta); + if (ret >=3D 0 && ts < head_page->page->time_stamp) { + local_set(&head_page->entries, 0); + local_set(&head_page->page->commit, 0); + head_page->page->time_stamp =3D ts; + ret =3D -1; + } + if (ret < 0) { + if (!discarded) + pr_info("Ring buffer meta [%d] invalid buffer page detected\n", + cpu_buffer->cpu); + discarded++; + } else { + entries +=3D ret; + entry_bytes +=3D rb_page_size(head_page); + if (ret > 0) + local_inc(&cpu_buffer->pages_touched); + ts =3D head_page->page->time_stamp; + } } if (i) pr_info("Ring buffer [%d] rewound %d pages\n", cpu_buffer->cpu, i); @@ -2034,15 +2048,12 @@ static void rb_meta_validate_events(struct ring_buf= fer_per_cpu *cpu_buffer) if (head_page =3D=3D cpu_buffer->reader_page) continue; =20 - ret =3D rb_validate_buffer(head_page->page, cpu_buffer->cpu, meta); + ret =3D rb_validate_buffer(head_page, cpu_buffer->cpu, meta); if (ret < 0) { if (!discarded) pr_info("Ring buffer meta [%d] invalid buffer page detected\n", cpu_buffer->cpu); discarded++; - /* Instead of discard whole ring buffer, discard only this sub-buffer. = */ - local_set(&head_page->entries, 0); - local_set(&head_page->page->commit, 0); } else { /* If the buffer has content, update pages_touched */ if (ret) @@ -2050,7 +2061,6 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) =20 entries +=3D ret; entry_bytes +=3D rb_page_size(head_page); - local_set(&head_page->entries, ret); } if (head_page =3D=3D cpu_buffer->commit_page) break; @@ -2081,7 +2091,7 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) /* Reset all the subbuffers */ for (i =3D 0; i < meta->nr_subbufs - 1; i++, rb_inc_page(&head_page)) { local_set(&head_page->entries, 0); - local_set(&head_page->page->commit, 0); + rb_init_page(head_page->page); } } From nobody Thu Apr 2 04:20:15 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AACF93D16EE; Mon, 30 Mar 2026 12:50:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875022; cv=none; b=doL5/IrcnKebxVaccn4W0N/RFN32wyu1fsN7ZyQSvoqOvXU3fiB12mZQMZ4qAHA09cllOQiCIYbvh7Xyj1gUGpMcVl13Y93n0JoogS5DdSOmC1b5AzzVupNPz6Zz53ILGMHFSDI8w7pc/HmwG9K33coFhsxr7SmbRkjxSKxBADc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875022; c=relaxed/simple; bh=KiXl6D4P1qHHuBke+upWrE3oVkO09pvTUZyEhv/Ilqc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AJaXv6g77Y5hcujga2YznRqH7hPmrCc862539Ch5w/2GK5dYNqPlLEAtSrjmbYNUFbm0iNiR9I53mdlfawnIaeRv9ZoVlVoiJNIrOMnkfaHHbXvRTaJxAerMCo3qgjgzRdn5SySh7f/7roGGBG17TbI5nQd/qg++c+onuBjMPSI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Vx0EAp1O; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Vx0EAp1O" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82E85C4CEF7; Mon, 30 Mar 2026 12:50:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774875022; bh=KiXl6D4P1qHHuBke+upWrE3oVkO09pvTUZyEhv/Ilqc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Vx0EAp1OeUUAZQGlogE7revZaYEgSvB6uimIC9RAhZqHtZLZad4k/mxcc7gO/irOE 0muZUh/W6ymuzQLavPF9uU1iqMNsrtsFGXesFDuuqsZev+NlhRkkHxsDjVXPEnaucL WEFprh8m8lzwli72l4JcS9RAqCe5kYr/Hf0elKCmSzyJb0KIeLtu3hm/4ttBDiCTrY lMxEh7kNf6seopX4a9FUOFpM18PvgZrPFKGvjWzkVN9LX6bTvgamWxRHeZbnqK4bSa 8LOMbRHGzMS3B4u8oYGn3VXQUKGb/rhsGLxhpbrghgkxiMh8rQzc5cwYydEwAhY+AA qgWWCm8PRyekQ== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Ian Rogers Subject: [PATCH v14 4/5] ring-buffer: Reset RB_MISSED_* flags on persistent ring buffer Date: Mon, 30 Mar 2026 21:50:20 +0900 Message-ID: <177487501981.3463592.2886576368556755178.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.1018.g2bb0e51243-goog In-Reply-To: <177487498530.3463592.12715592581212799257.stgit@mhiramat.tok.corp.google.com> References: <177487498530.3463592.12715592581212799257.stgit@mhiramat.tok.corp.google.com> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Reset RB_MISSED_* flags when the persistent ring buffer is validated at boot. Since these flags are used only in reading process, such process should be stopped when reboot and never be restarted. Thus, these flags are meaningless in the next boot. Moreover, it can confuse the read process after reboot. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v14: - Newly added. --- kernel/trace/ring_buffer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index e5178239f2f9..5049cf13021e 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -1903,6 +1903,7 @@ static int rb_validate_buffer(struct buffer_page *bpa= ge, int cpu, local_set(&bpage->page->commit, 0); } else { local_set(&bpage->entries, ret); + local_set(&bpage->page->commit, tail); } =20 return ret; From nobody Thu Apr 2 04:20:15 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63F543B6363; Mon, 30 Mar 2026 12:50:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875030; cv=none; b=q2VDXF1kXLKX83NxY45o7/HphMhEdsamdgChADaOts1f75dy+v4ahLvN2mwDFEyPmQOXL3JVaYpPNe7DMvp47c/8FokeKxo3cAF757crsnYhYbyGctp8Uh78E3fhPPAJDsE7fWMd0KFKGOiUlDixSlPBEKVU+4ZDsaWqMefUP5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875030; c=relaxed/simple; bh=EXYO15ATJn+QqnvNAAHVALJL+AFONrSWh1pbmJzee9o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iD6KP1D3RIdoPEF5VOmhy6AH6fXxSMKVUn16xd0GFjOBgCxohvkGwCrlCb6y1PlRfa4EKG2rPM0IysnKl0+lsvSaQmjRlUjIa1f2RiNHoHrYvBGHEOj4TCQsnarl90AwGwuC/Qdi6H6wAShsGXIgvSV3Z+RNbq/WDRMHCbogdic= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oWNiD1YG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oWNiD1YG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 440A5C4CEF7; Mon, 30 Mar 2026 12:50:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774875030; bh=EXYO15ATJn+QqnvNAAHVALJL+AFONrSWh1pbmJzee9o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oWNiD1YGMRMaVNKZnSv6Rnw8An31861K2yca6enH8C0C7F8MwXqsfjCk0V3qWnZzi vVtfd0Egu4RHzPz6bzY61WDj5iejYiBNmWVrbcInufMx4tc3abYI5Uxp3N+faVJBWI MHGlI/MOteQSLkRau0JfOGwE2re/ACTY+XSDXDm5CrCHwftSosh+wD6BspVGSxhxBN aHz/gW2lgkBanax+1DUffvWKU5gp7p/C7gPsDiCAUhanR92mcccBLW32w9/eTQjnVk tWhf14RdAnntUBYqvhzm2ZLgdYF509C55ri/LnZzYcLmbLh1RQC/l0xoAODEnI70pu 8VB2wm5nZdp1A== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Ian Rogers Subject: [PATCH v14 5/5] ring-buffer: Add persistent ring buffer selftest Date: Mon, 30 Mar 2026 21:50:27 +0900 Message-ID: <177487502763.3463592.7901517545360137050.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.1018.g2bb0e51243-goog In-Reply-To: <177487498530.3463592.12715592581212799257.stgit@mhiramat.tok.corp.google.com> References: <177487498530.3463592.12715592581212799257.stgit@mhiramat.tok.corp.google.com> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Add a self-destractive test for the persistent ring buffer. This will invalidate some sub-buffer pages in the persistent ring buffer when kernel gets panic, and check whether the number of detected invalid pages and the total entry_bytes are the same as record after reboot. This can ensure the kernel correctly recover partially corrupted persistent ring buffer when boot. The test only runs on the persistent ring buffer whose name is "ptracingtest". And user has to fill it up with events before kernel panics. To run the test, enable CONFIG_RING_BUFFER_PERSISTENT_INJECT and you have to setup the kernel cmdline; reserve_mem=3D20M:2M:trace trace_instance=3Dptracingtest^traceoff@trace panic=3D1 And run following commands after the 1st boot; cd /sys/kernel/tracing/instances/ptracingtest echo 1 > tracing_on echo 1 > events/enable sleep 3 echo c > /proc/sysrq-trigger After panic message, the kernel will reboot and run the verification on the persistent ring buffer, e.g. Ring buffer meta [2] invalid buffer page detected Ring buffer meta [2] is from previous boot! (318 pages discarded) Ring buffer testing [2] invalid pages: PASSED (318/318) Ring buffer testing [2] entry_bytes: PASSED (1300476/1300476) Signed-off-by: Masami Hiramatsu (Google) --- Changes in v14: - Rename config to CONFIG_RING_BUFFER_PERSISTENT_INJECT. - Clear meta->nr_invalid/entry_bytes after testing. - Add test commands in config comment. Changes in v10: - Add entry_bytes test. - Do not compile test code if CONFIG_RING_BUFFER_PERSISTENT_SELFTEST=3Dn. Changes in v9: - Test also reader pages. --- include/linux/ring_buffer.h | 1 + kernel/trace/Kconfig | 31 +++++++++++++++++++ kernel/trace/ring_buffer.c | 71 +++++++++++++++++++++++++++++++++++++++= ++++ kernel/trace/trace.c | 4 ++ 4 files changed, 107 insertions(+) diff --git a/include/linux/ring_buffer.h b/include/linux/ring_buffer.h index 994f52b34344..0670742b2d60 100644 --- a/include/linux/ring_buffer.h +++ b/include/linux/ring_buffer.h @@ -238,6 +238,7 @@ int ring_buffer_subbuf_size_get(struct trace_buffer *bu= ffer); =20 enum ring_buffer_flags { RB_FL_OVERWRITE =3D 1 << 0, + RB_FL_TESTING =3D 1 << 1, }; =20 #ifdef CONFIG_RING_BUFFER diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index e130da35808f..07305ed6d745 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -1202,6 +1202,37 @@ config RING_BUFFER_VALIDATE_TIME_DELTAS Only say Y if you understand what this does, and you still want it enabled. Otherwise say N =20 +config RING_BUFFER_PERSISTENT_INJECT + bool "Enable persistent ring buffer error injection test" + depends on RING_BUFFER + help + Run a selftest on the persistent ring buffer which names + "ptracingtest" (and its backup) when panic_on_reboot by + invalidating ring buffer pages. + To use this, boot kernel with "ptracingtest" persistent + ring buffer, e.g. + + reserve_mem=3D20M:2M:trace trace_instance=3Dptracingtest@trace panic= =3D1 + + And after the 1st boot, run test command, like; + + cd /sys/kernel/tracing/instances/ptracingtest + echo 1 > events/enable + echo 1 > tracing_on + sleep 3 + echo c > /proc/sysrq-trigger + + After panic message, the kernel reboots and show test results + on the boot log. + + Note that user has to enable events on the persistent ring + buffer manually to fill up ring buffers before rebooting. + Since this invalidates the data on test target ring buffer, + "ptracingtest" persistent ring buffer must not be used for + actual tracing, but only for testing. + + If unsure, say N + config MMIOTRACE_TEST tristate "Test module for mmiotrace" depends on MMIOTRACE && m diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 5049cf13021e..7f8140c54fce 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -64,6 +64,10 @@ struct ring_buffer_cpu_meta { unsigned long commit_buffer; __u32 subbuf_size; __u32 nr_subbufs; +#ifdef CONFIG_RING_BUFFER_PERSISTENT_INJECT + __u32 nr_invalid; + __u32 entry_bytes; +#endif int buffers[]; }; =20 @@ -2078,6 +2082,21 @@ static void rb_meta_validate_events(struct ring_buff= er_per_cpu *cpu_buffer) =20 pr_info("Ring buffer meta [%d] is from previous boot! (%d pages discarded= )\n", cpu_buffer->cpu, discarded); + +#ifdef CONFIG_RING_BUFFER_PERSISTENT_INJECT + if (meta->nr_invalid) + pr_info("Ring buffer testing [%d] invalid pages: %s (%d/%d)\n", + cpu_buffer->cpu, + (discarded =3D=3D meta->nr_invalid) ? "PASSED" : "FAILED", + discarded, meta->nr_invalid); + if (meta->entry_bytes) + pr_info("Ring buffer testing [%d] entry_bytes: %s (%ld/%ld)\n", + cpu_buffer->cpu, + (entry_bytes =3D=3D meta->entry_bytes) ? "PASSED" : "FAILED", + (long)entry_bytes, (long)meta->entry_bytes); + meta->nr_invalid =3D 0; + meta->entry_bytes =3D 0; +#endif return; =20 invalid: @@ -2558,12 +2577,64 @@ static void rb_free_cpu_buffer(struct ring_buffer_p= er_cpu *cpu_buffer) kfree(cpu_buffer); } =20 +#ifdef CONFIG_RING_BUFFER_PERSISTENT_INJECT +static void rb_test_inject_invalid_pages(struct trace_buffer *buffer) +{ + struct ring_buffer_per_cpu *cpu_buffer; + struct ring_buffer_cpu_meta *meta; + struct buffer_data_page *dpage; + u32 entry_bytes =3D 0; + unsigned long ptr; + int subbuf_size; + int invalid =3D 0; + int cpu; + int i; + + if (!(buffer->flags & RB_FL_TESTING)) + return; + + guard(preempt)(); + cpu =3D smp_processor_id(); + + cpu_buffer =3D buffer->buffers[cpu]; + meta =3D cpu_buffer->ring_meta; + ptr =3D (unsigned long)rb_subbufs_from_meta(meta); + subbuf_size =3D meta->subbuf_size; + + for (i =3D 0; i < meta->nr_subbufs; i++) { + int idx =3D meta->buffers[i]; + + dpage =3D (void *)(ptr + idx * subbuf_size); + /* Skip unused pages */ + if (!local_read(&dpage->commit)) + continue; + + /* Invalidate even pages. */ + if (!(i & 0x1)) { + local_add(subbuf_size + 1, &dpage->commit); + invalid++; + } else { + /* Count total commit bytes. */ + entry_bytes +=3D local_read(&dpage->commit); + } + } + + pr_info("Inject invalidated %d pages on CPU%d, total size: %ld\n", + invalid, cpu, (long)entry_bytes); + meta->nr_invalid =3D invalid; + meta->entry_bytes =3D entry_bytes; +} +#else /* !CONFIG_RING_BUFFER_PERSISTENT_INJECT */ +#define rb_test_inject_invalid_pages(buffer) do { } while (0) +#endif + /* Stop recording on a persistent buffer and flush cache if needed. */ static int rb_flush_buffer_cb(struct notifier_block *nb, unsigned long eve= nt, void *data) { struct trace_buffer *buffer =3D container_of(nb, struct trace_buffer, flu= sh_nb); =20 ring_buffer_record_off(buffer); + rb_test_inject_invalid_pages(buffer); arch_ring_buffer_flush_range(buffer->range_addr_start, buffer->range_addr= _end); return NOTIFY_DONE; } diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 4189ec9df6a5..108b0d16badf 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -9366,6 +9366,8 @@ static void setup_trace_scratch(struct trace_array *t= r, memset(tscratch, 0, size); } =20 +#define TRACE_TEST_PTRACING_NAME "ptracingtest" + static int allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, un= signed long size) { @@ -9378,6 +9380,8 @@ allocate_trace_buffer(struct trace_array *tr, struct = array_buffer *buf, unsigned buf->tr =3D tr; =20 if (tr->range_addr_start && tr->range_addr_size) { + if (!strcmp(tr->name, TRACE_TEST_PTRACING_NAME)) + rb_flags |=3D RB_FL_TESTING; /* Add scratch buffer to handle 128 modules */ buf->buffer =3D ring_buffer_alloc_range(size, rb_flags, 0, tr->range_addr_start,