From nobody Tue Apr 7 18:48:30 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 3CCED301465; Fri, 27 Feb 2026 09:20:29 +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=1772184030; cv=none; b=YDGMlfY1cpwHYr6BQ9nlU4vt57L2NmCMzv1nXobIRmMLLw044yn+TL9tiFWpN6xPNGxcNUgpBUSCOyRuwFz+a8OuY15Ld8mSzCg4iL+PhkVX4RLt3z8xA4lvJAdI3NNVLjVixB9BsbhpHjOw/SPBbYuw5140S9NuHSDw9qkm4hw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772184030; c=relaxed/simple; bh=jBXGSAuaDrnmDy6hHh8LIOXIvYXZ5me6VCki2zdY5mk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Vl3WT4mCPn+Mvp90tYPU3YIndodAZ7ALAwOt5eOp5BC0uZadvjHb7uSc/XDoPXnQWbreMnmuLhywdRoGr3+4gOtlEdrT6aGe3k2FBxkU51CUc0oI4aoOrUdRewz+SfYIBwcs2XZRSyPXgfbQEdGQ41crHOVGSQ1oNVqAqOkaMlU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uCWej/k6; 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="uCWej/k6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 223B8C116C6; Fri, 27 Feb 2026 09:20:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772184029; bh=jBXGSAuaDrnmDy6hHh8LIOXIvYXZ5me6VCki2zdY5mk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uCWej/k6p4BDJuKOc1/XB6N09G4f6ZkcClZRdPr/bVamlqf00FxXKsc1vegWISNnT 2u799L1offqkQ1XqXsXovlLxb3AHEf84DoMLqLpMgVKhDwmbyPhzJvLVy9hhrkGF0b HCyeSDqdXCgs2gz1w1bssje8CICUxPWwG9OW0MR4Pon8Ml3ikwIe9eBiKCVo4x1zYu QIbHnjo9UIMafzi4BNvTg/EMFm8KSdJUN4ePMB9eHNgPfInXV/a7W8N2i2rrmJJOYm 2Fq3GQSteYdLBI9UEDgBRyNA/SNReSVTvoKBYPWM8rkjI0XiVM3xruKmxorlU1gtRP lLlvBF0BS+mLA== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v6 1/3] ring-buffer: Flush and stop persistent ring buffer on panic Date: Fri, 27 Feb 2026 18:20:27 +0900 Message-ID: <177218402749.1988514.15768451704953140678.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog In-Reply-To: <177218401821.1988514.5579163042147205021.stgit@mhiramat.tok.corp.google.com> References: <177218401821.1988514.5579163042147205021.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 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..dbe94f5785f9 --- /dev/null +++ b/include/asm-generic/ring_buffer.h @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Generiuc 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 */ +#define arch_ring_buffer_flush_range(start, end) flush_cache_vmap(start, e= nd) + +#endif /* __ASM_GENERIC_RING_BUFFER_H__ */ diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index f16f053ef77d..156ed19fb569 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -6,6 +6,7 @@ */ #include #include +#include #include #include #include @@ -30,6 +31,7 @@ #include #include =20 +#include #include #include #include @@ -589,6 +591,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 @@ -2471,6 +2474,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, @@ -2590,6 +2603,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: @@ -2677,6 +2696,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 Tue Apr 7 18:48:30 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 DDE3437AA90; Fri, 27 Feb 2026 09:20:37 +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=1772184037; cv=none; b=c6tSa/W09H8LV0P7gF+veyB0I4sfbE0b8Z8n7t1DJ0bplpYmqR97YMg6saG8nlRVdn7JiMwBzPe3x9pEhy9gmTqKn+eMxYzlKygVA+NyRTGtIzC2V3PxPyVESwKdnTnmG/Fm1PHhvzQ/hLpQT2kyYBBnRz1GMImWF9VW0sZFjt4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772184037; c=relaxed/simple; bh=7CTdl1fzOdmeH/Uzee6UCB/qqZjpNr2TNVQjKcODxB0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gQ3brEFRk6jxxACOK+zt6cYQoCuMCQacZ/TyC4aUdyXNUQw9B+UjXlJY9hZU9X+70M5EQrgADbkChmQ/oOq8nCYMgszTREqAVHKICk6ppa5XDsAgLP2AvviXyYnFL+dxTcsQZRW/uzwFUQU90rjqsPV0BUyxePzPUETw1FibCzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Prrvnng7; 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="Prrvnng7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0069C116C6; Fri, 27 Feb 2026 09:20:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772184037; bh=7CTdl1fzOdmeH/Uzee6UCB/qqZjpNr2TNVQjKcODxB0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Prrvnng7IY5fJMCI/mtNCcsgfs76ZnuobSKEdsHlUNK6ErpmQcwdkEY5aRJIeY02s HkRnev2aKF/OMhiEZDlPTeqiZGfOZohxrFjM2Il+26VigmcvsnMkTNBuQq+prvwkPs uxVujz2xOdR9LW8KT9qppI98Mi7BKrViRGQqfdb9YYIZpolidi300mCgGQ+HA3n6se JB/wiqvrHvU4Il6NQfmMj6/CSiWMo9N6jH0g4oLHjV/bxcHmwesWhnct7klZar7sfs bZnWrdYweUuOX1k/4jgzwsma6YzpREfNVx4WVnns+WGg1VdGYDjPP0R5BFefz39PU5 21D8za3XjGfNw== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v6 2/3] ring-buffer: Handle RB_MISSED_* flags on commit field correctly Date: Fri, 27 Feb 2026 18:20:35 +0900 Message-ID: <177218403512.1988514.11156837978785772102.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog In-Reply-To: <177218401821.1988514.5579163042147205021.stgit@mhiramat.tok.corp.google.com> References: <177218401821.1988514.5579163042147205021.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) Since the MSBs of rb_data_page::commit are used for storing RB_MISSED_EVENTS and RB_MISSED_STORED, we need to mask out those bits when it is used for finding the size of data pages. Fixes: 5f3b6e839f3c ("ring-buffer: Validate boot range memory events") Fixes: 5b7be9c709e1 ("ring-buffer: Add test to validate the time stamp delt= as") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- Changes in v5: - Do not move rb_commit_index(). - Fix verify_event() and rb_cpu_meta_valid() too. Changes in v4: - Fix to move rb_commit_index() after ring_buffer_per_cpu definition. --- kernel/trace/ring_buffer.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 156ed19fb569..87d6958e9656 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -396,6 +396,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 */ @@ -677,7 +683,7 @@ static void verify_event(struct ring_buffer_per_cpu *cp= u_buffer, do { if (page =3D=3D tail_page || WARN_ON_ONCE(stop++ > 100)) done =3D true; - commit =3D local_read(&page->page->commit); + commit =3D rb_page_size(page); write =3D local_read(&page->write); if (addr >=3D (unsigned long)&page->page->data[commit] && addr < (unsigned long)&page->page->data[write]) @@ -1821,13 +1827,16 @@ static bool rb_cpu_meta_valid(struct ring_buffer_cp= u_meta *meta, int cpu, =20 /* Is the meta buffers and the subbufs themselves have correct data? */ for (i =3D 0; i < meta->nr_subbufs; i++) { + unsigned long commit; + if (meta->buffers[i] < 0 || meta->buffers[i] >=3D meta->nr_subbufs) { pr_info("Ring buffer boot meta [%d] array out of range\n", cpu); return false; } =20 - if ((unsigned)local_read(&subbuf->commit) > subbuf_size) { + commit =3D local_read(&subbuf->commit) & ~RB_MISSED_MASK; + if (commit > subbuf_size) { pr_info("Ring buffer boot meta [%d] buffer invalid commit\n", cpu); return false; } @@ -1908,7 +1917,7 @@ static int rb_validate_buffer(struct buffer_data_page= *dpage, int cpu) u64 delta; int tail; =20 - tail =3D local_read(&dpage->commit); + tail =3D local_read(&dpage->commit) & ~RB_MISSED_MASK; return rb_read_data_buffer(dpage, tail, cpu, &ts, &delta); } =20 @@ -1935,7 +1944,7 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) goto invalid; } entries +=3D ret; - entry_bytes +=3D local_read(&cpu_buffer->reader_page->page->commit); + 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; @@ -2055,7 +2064,7 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) local_inc(&cpu_buffer->pages_touched); =20 entries +=3D ret; - entry_bytes +=3D local_read(&head_page->page->commit); + entry_bytes +=3D rb_page_size(head_page); local_set(&cpu_buffer->head_page->entries, ret); =20 if (head_page =3D=3D cpu_buffer->commit_page) @@ -3258,12 +3267,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) { @@ -4434,7 +4437,7 @@ static void check_buffer(struct ring_buffer_per_cpu *= cpu_buffer, =20 if (tail =3D=3D CHECK_FULL_PAGE) { full =3D true; - tail =3D local_read(&bpage->commit); + tail =3D local_read(&bpage->commit) & ~RB_MISSED_MASK; } else if (info->add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) { /* Ignore events with absolute time stamps */ From nobody Tue Apr 7 18:48:30 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 0EC49389E08; Fri, 27 Feb 2026 09:20:47 +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=1772184047; cv=none; b=FiCNjT5HBBMIBo/T5lydVgkTevLcGKEvee260J3mdRlSVL1GG8Ak/wP6mNW9UAeTDEuVSZgGMj3susbAhzj0XnMxlgjPtCiEXneMVPrUQysEpnx3TN7yL8zsFxH36EixlEZXiyymWOU8QQdPezgZnL3ysWJg0xiiCAr1VrLKBmY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772184047; c=relaxed/simple; bh=ZTL7VPKVQGVb3mxnY4WJUpcItV/URobGB8/uJRCEKlU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=B1TgMY+G6ySXnsn1e9ZqYdh5hTuJInOs/aOV4zUtNoPpdnSSPY29JKg6flcDBIxKNmE4HL7fitfcPI2MEGjJuoRszmqnwba6hVrWpMS0lbtp3cCHC4abNld7Ay6d4R+ZmhfdlVVTh1jSnmsKEY0Ei60hpXZsXvOHx54IV9XUVQw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E9pMerfK; 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="E9pMerfK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 339CDC116C6; Fri, 27 Feb 2026 09:20:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772184046; bh=ZTL7VPKVQGVb3mxnY4WJUpcItV/URobGB8/uJRCEKlU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E9pMerfKswr0hHKAn5o/v71ErzjF4QvBCgIJhGImB5WuJoyO/gVx4IRt+bWiM74+i nyXwpV9463Os7y5gdk2r2c2w3WG1zysfkPM04MINJzD+Imxj6D5IEY2Ir61hxzt83T KZP9/IPX3Y6aH0q3kpqV2NBBP+i9YQuIWzEzwH45XAyylrIUnBafvSG9zO4lc2vZY+ o4YyBcps1YmKcjWoRAwLhDaJVvCNKT2pNXWQihkcYwJLur3hl3ul7i0wyEu6llimf5 SucBWGUUvVAc2RmzUaCWG5JMYjNzaeSeQoREWu/APa5HOWf9LzHYhP9JPbDii46mma Od+ltdrT4UFzA== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v6 3/3] ring-buffer: Skip invalid sub-buffers when validating persistent ring buffer Date: Fri, 27 Feb 2026 18:20:43 +0900 Message-ID: <177218404280.1988514.11324224211296559513.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog In-Reply-To: <177218401821.1988514.5579163042147205021.stgit@mhiramat.tok.corp.google.com> References: <177218401821.1988514.5579163042147205021.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. Also, mark there are missed events on the discarded buffer. 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 subbuffersa that ar found to be corrupted. Signed-off-by: Masami Hiramatsu (Google) --- 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 | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 87d6958e9656..5838ee897aa3 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -1928,6 +1928,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; @@ -2054,19 +2055,22 @@ static void rb_meta_validate_events(struct ring_buf= fer_per_cpu *cpu_buffer) =20 ret =3D rb_validate_buffer(head_page->page, cpu_buffer->cpu); 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 rb_page_size(head_page); - local_set(&cpu_buffer->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, RB_MISSED_EVENTS); + } 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(&cpu_buffer->head_page->entries, ret); + } if (head_page =3D=3D cpu_buffer->commit_page) break; } @@ -2080,7 +2084,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: