From nobody Thu Apr 23 11:58:20 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 0FB3034029E; Wed, 22 Apr 2026 16:16: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=1776874597; cv=none; b=mEBXu2Lsn7NG9mjPTt7Vwz85vnmLz7V+vX+RDyIqad2DaiUxaK5GGXOTPAQSPkEpWV5u7XY/hJx3h/D1aoE6XxlTKvHA7nsHCVDj/GzHFfLopJPjMrrIsUlf9nvW+zQkAo6yq0XVjZ0eWlOhzF/hmdhXmaJ4H5zWjloPPMudQI8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776874597; c=relaxed/simple; bh=3q/bUG/9SP2sW+Fss6rdfX98x9tFohnui+b4zFbAdjg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LQM36VbaV8540VkoZjhNdCVojLt2wiAXvXe1zD/73s9G78MBv8EPgoOXeGT5RjW0hfJtfvqz3LXv3viPrMh555Ad65+1J/Y5ouVAcBlo1WEGrN2jgdRWPnj8cG3fvzhsi6mklYf5TKe3AKIHLJA1pK4PE9PGuwnrjSodlXvZqyQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=br/NnyHN; 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="br/NnyHN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3B5FC19425; Wed, 22 Apr 2026 16:16:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776874596; bh=3q/bUG/9SP2sW+Fss6rdfX98x9tFohnui+b4zFbAdjg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=br/NnyHN+QOpLlk6wWzKHk5lFiPO8Hn1HKgKyTUbtt37XScZ6+kt5/O08iG+ZyIPY qhM/o+nc2yZTG81OVI5aPIAp3ZvCN+CUUrcctwwySYkCpg0zJhNoJjT7/uZ7Uyg45/ JOXuGn82TwVcWVasmAFk6njdO0BJZcMMzqkT3kYNQ0rcTi/kicxtuMGQASfqWAGS1J lQB3xx7c3BXAdNBqpTEDtwGXlZ4naHhbhSy55AIIiVm7rAeIOxmGcyRt+CWlu90Nh8 F5R4/pXf8Fck5VYhlW/RfW2j9VnilWMCHyyccrZGSEtfWZIRrRCVhIWlBW6/cy1krm NnVDcUsFOGJow== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Catalin Marinas , Will Deacon Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Ian Rogers , linux-arm-kernel@lists.infradead.org Subject: [PATCH v17 1/5] ring-buffer: Flush and stop persistent ring buffer on panic Date: Thu, 23 Apr 2026 01:16:34 +0900 Message-ID: <177687459412.932171.8121855108122534476.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.555.g9c883467ad-goog In-Reply-To: <177687458572.932171.10907864814735342737.stgit@mhiramat.tok.corp.google.com> References: <177687458572.932171.10907864814735342737.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) Acked-by: Catalin Marinas Acked-by: Geert Uytterhoeven # m68k --- 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 cef49f8871d2..b5ed4c72643e 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 23 11:58:20 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 607AF36494A; Wed, 22 Apr 2026 16:16:45 +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=1776874605; cv=none; b=AE4upta3QYMQP7ZrqtKWv70yeWSzo9gFyp71lMMdZPsdKUiUbzLMfzCHall2VgIywhx+HHn4rlfeZ4/ykcGaCb7M1qAanjTvr4CVdmxJGdgyYC9NlU5BRRwFDRb3L0a+ZFKGf9NyIjXSWDK5RO0IYq8rrGj0iOiNec8SUO/NOc0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776874605; c=relaxed/simple; bh=RUSpqGR0bABGHEnjLkuaF2h20ycIVneoIYq/9uKOaOk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PhXck7cjboGMYk5NFXq7aLUUR9+xbPAczUknOaSgAE4QikbLY3oPYOurytXvFt6uFXZv2rtwmLAS3PZXkrMU4hLgWmqpyv5uCVH3WmLqeXAyCCutBQjyHnkrzv7M8PsiqCBjMiZ5DJ176lNfZ8uZVaUToVBmmRD0VStN7qO+yuA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ewZh08U2; 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="ewZh08U2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CC536C2BCAF; Wed, 22 Apr 2026 16:16:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776874605; bh=RUSpqGR0bABGHEnjLkuaF2h20ycIVneoIYq/9uKOaOk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ewZh08U28G8MGRbb0r8L1kYrWbyBt2j+s/yX3EvXkOKm3WEfmnJt90CvRrxo46vsQ p04GHAjPzQDb48sLtRKIQfEUj2KeQ9UYRdh/lqWxvAoD9AuLGH56w3ZRZPk8wI+edS 2+IzAMEkQjUTCbvy3GevRj7IWG/pIwNYeFtKUkQkl8cpTsRRDtmGI5ZbE+uH1wBDAx pfQraNgzAUdRIoDW6nxNaryJpTrV9LmRYWbLG1XmbPJz42fGEq5UgY9fBp37XdwrPM sIF8uvjzGd8J5iXd9adZduv0zV/MiLaS+CU8D+9MFylGV7UyuPkKfPb1b6Ozq1R4NR n0btve+WJVa4w== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Catalin Marinas , Will Deacon Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Ian Rogers , linux-arm-kernel@lists.infradead.org Subject: [PATCH v17 2/5] ring-buffer: Skip invalid sub-buffers when validating persistent ring buffer Date: Thu, 23 Apr 2026 01:16:42 +0900 Message-ID: <177687460220.932171.10793442799442549570.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.555.g9c883467ad-goog In-Reply-To: <177687458572.932171.10907864814735342737.stgit@mhiramat.tok.corp.google.com> References: <177687458572.932171.10907864814735342737.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 v17: - Fix to use rb_page_size() of rewound pages for entry_bytes. Changes in v15: - Skip reader_page loop check on persistent ring buffer because there can be contiguous empty(invalidated) pages. - Do not show discarded page number information if it is 0. 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 | 111 ++++++++++++++++++++++++++--------------= ---- 1 file changed, 66 insertions(+), 45 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index b5ed4c72643e..26507b93cf40 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 @@ -1944,7 +1964,7 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) if (ret) local_inc(&cpu_buffer->pages_touched); entries +=3D ret; - entry_bytes +=3D rb_page_commit(head_page); + entry_bytes +=3D rb_page_size(head_page); } if (i) pr_info("Ring buffer [%d] rewound %d pages\n", cpu_buffer->cpu, i); @@ -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,10 @@ static void rb_meta_validate_events(struct ring_buff= er_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!", cpu_buffer->cpu); + if (discarded) + pr_cont(" (%d pages discarded)", discarded); + pr_cont("\n"); return; =20 invalid: @@ -3329,12 +3355,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) { @@ -5647,11 +5667,12 @@ __rb_get_reader_page(struct ring_buffer_per_cpu *cp= u_buffer) again: /* * This should normally only loop twice. But because the - * start of the reader inserts an empty page, it causes - * a case where we will loop three times. There should be no - * reason to loop four times (that I know of). + * start of the reader inserts an empty page, it causes a + * case where we will loop three times. There should be no + * reason to loop four times unless the ring buffer is a + * recovered persistent ring buffer. */ - if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) { + if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3 && !cpu_buffer->ring_meta)) { reader =3D NULL; goto out; } From nobody Thu Apr 23 11:58:20 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 20FEC35DA5B; Wed, 22 Apr 2026 16:16:53 +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=1776874613; cv=none; b=bk2BOQsoZzWowOC/mzqqMMnDe++jrf2E3n3VBkFwYUbjLkoXWChyRpoLhwFGm7oZJvBmgfwbZxJOd4eZTza6gGjEnCkdFjf9fvEhyX8ZZTOslNBm09wfCx9UR+jPxmH2YLhuyU9yrIP1lrvwAvVQTny/MlKebM1geRq/zzU2w34= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776874613; c=relaxed/simple; bh=JjKwuICybJ4Ua61/3YaBXShdj+/IxEAVisloMY2M9EE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LVB7Xqgt7NYrtOIf6+zajVzrR+UMsDmty9SPZtJu4GosC3/+dvUxsOLLpjJkDPA5vylyj/VH8f8uIVXbcgBfYMXxIg4UayYiyRLu8OdgizbRT4wyX7VooPzbfnF8B7NbiiN+/yBPn0QspfUSzfNWcLfFZNkjZ2dJTthzdViSwcc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Xin6s4f9; 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="Xin6s4f9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D36B1C2BCAF; Wed, 22 Apr 2026 16:16:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776874613; bh=JjKwuICybJ4Ua61/3YaBXShdj+/IxEAVisloMY2M9EE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xin6s4f9DrEjdvVbIBXiGjw2y9W0UuKIhhQDhGfO/Mr4jCigh1s4ONXR5QC+jW1LO 1ZJ97rEqJV6xb3iR2SwZOsWzDvslNhp2JCtcBCowrb3spv+Tlkd/SwjUFptErKn2zM viIFI/yepEm6zW1y1JZoGOsjwLDzKG17u4sudryTZj79WmS+j65bRLPoPuwLQOa81Y GGu0OlYduYK22bOle9fGpIuiSsd986Xvah787CVtaAUx0IbEhECsIMdIQNKGj8yCvC 14ndxENA3LCm0Glpo62aHbXTpNqyoCeNhR2B4228Xr4KIYwpByOMDi1CeYTbrmqBHG dG3eifn0AuuRQ== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Catalin Marinas , Will Deacon Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Ian Rogers , linux-arm-kernel@lists.infradead.org Subject: [PATCH v17 3/5] ring-buffer: Skip invalid sub-buffers when rewinding persistent ring buffer Date: Thu, 23 Apr 2026 01:16:50 +0900 Message-ID: <177687461022.932171.13406318513559556090.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.555.g9c883467ad-goog In-Reply-To: <177687458572.932171.10907864814735342737.stgit@mhiramat.tok.corp.google.com> References: <177687458572.932171.10907864814735342737.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 v17: - Fix to verify head_page at first before using its timestamp. - Reset timestamp if the page is invalid. 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 | 92 ++++++++++++++++++++++++++--------------= ---- 1 file changed, 54 insertions(+), 38 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 26507b93cf40..0e3d2d037d4d 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, - struct ring_buffer_cpu_meta *meta) +static int rb_validate_buffer(struct buffer_page *bpage, int cpu, + struct ring_buffer_cpu_meta *meta, u64 prev_ts, u64 next_ts) { + 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,19 @@ 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 || (prev_ts && prev_ts > ts) || (next_ts && ts > next_ts)) { + local_set(&bpage->entries, 0); + local_set(&bpage->page->commit, 0); + bpage->page->time_stamp =3D prev_ts ? prev_ts : next_ts; + ret =3D -1; + } else { + local_set(&bpage->entries, ret); + } + + return ret; } =20 /* If the meta data has been validated, now validate the events */ @@ -1914,25 +1927,29 @@ static void rb_meta_validate_events(struct ring_buf= fer_per_cpu *cpu_buffer) =20 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); + /* Do the head page first */ + ret =3D rb_validate_buffer(head_page, cpu_buffer->cpu, meta, 0, 0); + if (ret < 0) { + pr_info("Ring buffer meta [%d] invalid head page detected\n", + cpu_buffer->cpu); + goto skip_rewind; + } + ts =3D head_page->page->time_stamp; + + /* Do the reader page - reader must be previous to head. */ + ret =3D rb_validate_buffer(cpu_buffer->reader_page, cpu_buffer->cpu, meta= , 0, ts); 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); + ts =3D cpu_buffer->reader_page->page->time_stamp; } =20 - ts =3D head_page->page->time_stamp; - /* - * Try to rewind the head so that we can read the pages which already + * Try to rewind the head so that we can read the pages which are already * read in the previous boot. */ if (head_page =3D=3D cpu_buffer->tail_page) @@ -1945,26 +1962,27 @@ 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) + /* Rewind until unused page (no timestamp, no commit). */ + if (!head_page->page->time_stamp && rb_page_commit(head_page) =3D=3D 0) break; =20 - /* Stop rewind if the page is invalid. */ - ret =3D rb_validate_buffer(head_page->page, cpu_buffer->cpu, meta); - if (ret < 0) - break; - - /* 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_size(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, 0, ts); + 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); @@ -2026,6 +2044,7 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) /* Nothing more to do, the only page is the reader page */ goto done; } + ts =3D head_page->page->time_stamp; =20 /* Iterate until finding the commit page */ for (i =3D 0; i < meta->nr_subbufs + 1; i++, rb_inc_page(&head_page)) { @@ -2034,15 +2053,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, ts, 0); 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 +2066,7 @@ 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); + ts =3D head_page->page->time_stamp; } if (head_page =3D=3D cpu_buffer->commit_page) break; @@ -2083,7 +2099,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 23 11:58:20 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 45ED029DB6E; Wed, 22 Apr 2026 16:17:01 +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=1776874621; cv=none; b=OnvevdC2gDsh3xL+gnBUFz5XW4Zj2R695e2Fi2b8JgjIaBEOihyd8pg6CEwmfOxIyLjN7B0IEyYT0ziC1tqzTV+3djx//WFzW7cPncLSuNQk7bY8lB9H46oaygOH0/n3HFKHCIWsWwaNF8I566QmHPiE5WFsYgkAJEsP3JOV8sQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776874621; c=relaxed/simple; bh=KbeOG0LK3SVmIT/zSGbmoZfMCuW9WB15K8bNET/iu+M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=n4r33im6sGGGDHekXwa5Gv2upgRzncfCTHDbcIEME8hVXMrCbY9H0ifqKKcpcrcm7/b26fkIu818/cZkl2s2xm/nGSqC5YIm0d5VK4cW5HhzS+8CaS8hGaHLb7Pgcl/X3bRC3O72cRYG8h/ETCT0rQJz5XA4e4DL2cHcD8OM3eQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=A6uFEN6X; 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="A6uFEN6X" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3825C19425; Wed, 22 Apr 2026 16:16:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776874621; bh=KbeOG0LK3SVmIT/zSGbmoZfMCuW9WB15K8bNET/iu+M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A6uFEN6Xcjd2EfzwGlyuJvAGcV/abhywqhqSja68k8QENdzpYNeSw75f6BkmY++2J pfw0v3JlKDc/r3Jj8uWbvEeYKcqwiPLr7XQNGydL1VjwfRAMSXKv7cJS25fg/QsJLw w3Pp8lmQzBJpIpvTpc4fLYqVpKjsCjV5NhqqCS1lazAWBflFxB/fa2X5Mfgy3hWpru JofL2tXZ/v7TacaxnmQG7fcdGvtjGXMZlTSUL9sKxFik/K2FZ3v+xDv5GGnp+akCI4 fXifwqSvh89FLSiytd3iPHhTKoTlw++c18PvJgo0ZZxfD4wri7AGlYy9mQA0e6SEF3 ZcOh3ieN3xpqw== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Catalin Marinas , Will Deacon Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Ian Rogers , linux-arm-kernel@lists.infradead.org Subject: [PATCH v17 4/5] ring-buffer: Add persistent ring buffer invalid-page inject test Date: Thu, 23 Apr 2026 01:16:58 +0900 Message-ID: <177687461825.932171.5322598724018671023.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.555.g9c883467ad-goog In-Reply-To: <177687458572.932171.10907864814735342737.stgit@mhiramat.tok.corp.google.com> References: <177687458572.932171.10907864814735342737.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-corrupting test for the persistent ring buffer. This will inject an erroneous value to some sub-buffer pages (where the index is even or multiples of 5) in the persistent ring buffer when the kernel panics, and checks whether the number of detected invalid pages and the total entry_bytes are the same as the recorded values after reboot. This ensures that the kernel can correctly recover a partially corrupted persistent ring buffer after a reboot or panic. The test only runs on the persistent ring buffer whose name is "ptracingtest". The user has to fill it with events before a kernel panic. To run the test, enable CONFIG_RING_BUFFER_PERSISTENT_INJECT and add the following kernel cmdline: reserve_mem=3D20M:2M:trace trace_instance=3Dptracingtest^traceoff@trace panic=3D1 Run the 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 v17: - In rb_test_inject_invalid_pages(), changed entry_bytes and idx to unsigned long - Added NULL checks for cpu_buffer and meta. - In allocate_trace_buffer(), added a NULL check for tr->name before comparing it with strcmp. Changes in v16: - Update description and comments according to review comments. Changes in v15: - Use pr_warn() for test result. - Inject errors on the page index is multiples of 5 so that this can reproduce contiguous empty pages. 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 | 34 +++++++++++++++++++ kernel/trace/ring_buffer.c | 79 +++++++++++++++++++++++++++++++++++++++= ++++ kernel/trace/trace.c | 4 ++ 4 files changed, 118 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..084f34dc6c9f 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -1202,6 +1202,40 @@ 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 + This option will have the kernel check if the persistent ring + buffer is named "ptracingtest". and if so, it will corrupt some + of its pages on a kernel panic. This is used to test if the + persistent ring buffer can recover from some of its sub-buffers + being corrupted. + To use this, boot a kernel with a "ptracingtest" persistent + ring buffer, e.g. + + reserve_mem=3D20M:2M:trace trace_instance=3Dptracingtest@trace panic= =3D1 + + And after the 1st boot, run the following commands: + + cd /sys/kernel/tracing/instances/ptracingtest + echo 1 > events/enable + echo 1 > tracing_on + sleep 3 + echo c > /proc/sysrq-trigger + + After the panic message, the kernel will reboot and will show + the test results in the console output. + + Note that events for the test ring buffer needs to be enabled + prior to crashing the kernel so that the ring buffer has content + that the test will corrupt. + As the test will corrupt events in the "ptracingtest" persistent + ring buffer, it should not be used for any other purpose other + than this test. + + 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 0e3d2d037d4d..31448c5ea791 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 @@ -2085,6 +2089,21 @@ static void rb_meta_validate_events(struct ring_buff= er_per_cpu *cpu_buffer) if (discarded) pr_cont(" (%d pages discarded)", discarded); pr_cont("\n"); + +#ifdef CONFIG_RING_BUFFER_PERSISTENT_INJECT + if (meta->nr_invalid) + pr_warn("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_warn("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: @@ -2565,12 +2584,72 @@ 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; + unsigned long 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]; + if (!cpu_buffer) + return; + meta =3D cpu_buffer->ring_meta; + if (!meta) + return; + + ptr =3D (unsigned long)rb_subbufs_from_meta(meta); + subbuf_size =3D meta->subbuf_size; + + for (i =3D 0; i < meta->nr_subbufs; i++) { + unsigned long idx =3D meta->buffers[i]; + + dpage =3D (void *)(ptr + idx * subbuf_size); + /* Skip unused pages */ + if (!local_read(&dpage->commit)) + continue; + + /* + * Invalidate even pages or multiples of 5. This will cause 3 + * contiguous invalidated(empty) pages. + */ + if (!(i & 0x1) || !(i % 5)) { + 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 e9455d46ec16..d972b24cd73b 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -9436,6 +9436,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) { @@ -9448,6 +9450,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 (tr->name && !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, From nobody Thu Apr 23 11:58:20 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 1477137FF4E; Wed, 22 Apr 2026 16:17:11 +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=1776874631; cv=none; b=Fjhgo7LzKiuvtxZ3uKssv6c1ZixAHLM9rJK13cnnYV7tQh2COt96bHFBbQX2zMTHlgWdrJWGk4JyRfEVJmCBLcUhRiN12LjJy83VSubvLOwbEE1j5UGzFOGBX4s0MaOEbGZzstmSFE55glD4qRzNL8lmbRr4ocwOB17dpAuQ/jY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776874631; c=relaxed/simple; bh=c7iBYZRpOtWPvLcFtw1dd9PzPthaeOLO/ie+Zfx/ByQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=C442JBzt/YwR46ABDBlfINkgSPZGxK5kHfHb6spErTABeSTnHQRasn5ajlzMADeQDYNLvTrrt+6EjnJhOXOBspQ6hmQmiTi6ekloB3Vwmsc9OupSisKhNe3MZSmA3JoJ3dGqMuZmCVcdnXOfUrV9Pbj54xFEaMvGWrTchLIDW20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EtN5BZ6F; 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="EtN5BZ6F" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE3D6C2BCAF; Wed, 22 Apr 2026 16:17:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776874631; bh=c7iBYZRpOtWPvLcFtw1dd9PzPthaeOLO/ie+Zfx/ByQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EtN5BZ6FQTsY6ccim/pco0ZmKP9wp8NZSXFDAFHf5PqLX8+EFcWD92MAhe+/O4fPW GiDYi5I6C1l206Q4fGFFgxoiyfdZls/04p2tAU+KgDLEPeNqg00EhOkmeHPBUKSMtA FsKw8/Xe0DsppJZ2cB6W4ESUNrAySe+RjROXZnT/zJNlLuANX/AaVKW4IM05Ycqx+F Vn7ACFEVeZ5LxpdxOxBesF2BgnnW/l/o1TfBh4xn++sW7XVxl324J65Czhwqvpwyx3 dfSlFESonGFjoFY0G/LxIvzc8M6dZEg8CzHbCGZVxdp/JSTcO0MlN6tkqVeJEXBe0X TTStuH0SJjalg== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Catalin Marinas , Will Deacon Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Ian Rogers , linux-arm-kernel@lists.infradead.org Subject: [PATCH v17 5/5] ring-buffer: Show commit numbers in buffer_meta file Date: Thu, 23 Apr 2026 01:17:06 +0900 Message-ID: <177687462643.932171.4169785470864985964.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.54.0.rc1.555.g9c883467ad-goog In-Reply-To: <177687458572.932171.10907864814735342737.stgit@mhiramat.tok.corp.google.com> References: <177687458572.932171.10907864814735342737.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) In addition to the index number, show the commit numbers of each data page in the per_cpu buffer_meta file. This is useful for understanding the current status of the persistent ring buffer. (Note that this file is shown only for persistent ring buffer and its backup instance) Signed-off-by: Masami Hiramatsu (Google) --- Changes in v17: - Added NULL check for dpage in rbm_show in ring_buffer.c. Changes in v16: - update description. --- kernel/trace/ring_buffer.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 31448c5ea791..15dcbf554d49 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -2215,6 +2215,7 @@ static int rbm_show(struct seq_file *m, void *v) struct ring_buffer_per_cpu *cpu_buffer =3D m->private; struct ring_buffer_cpu_meta *meta =3D cpu_buffer->ring_meta; unsigned long val =3D (unsigned long)v; + struct buffer_data_page *dpage; =20 if (val =3D=3D 1) { seq_printf(m, "head_buffer: %d\n", @@ -2227,7 +2228,9 @@ static int rbm_show(struct seq_file *m, void *v) } =20 val -=3D 2; - seq_printf(m, "buffer[%ld]: %d\n", val, meta->buffers[val]); + dpage =3D rb_range_buffer(cpu_buffer, val); + seq_printf(m, "buffer[%ld]: %d (commit: %ld)\n", + val, meta->buffers[val], dpage ? local_read(&dpage->commit) : -1); =20 return 0; }