From nobody Mon Apr 6 23:37:14 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 8F4FF371072; Tue, 17 Mar 2026 09:36:28 +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=1773740188; cv=none; b=LXA0IUU69ZhwI7a3srFFQUCpoItdxVamUewgghNoyEH6jBJ4XysFltbfWSGa1fORxprmZuyB5H18I7tQseQg8MvFNxATcPmBcHyg/4yx49oVgg91ZElPq642KrcHCt65Lw2BZqAYdESsQaWmxF0emqwgSheMy8PDnYRkNuV5UVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773740188; c=relaxed/simple; bh=dnkb6p/eQabzlKwOsghBnbUtdqt485jhvOonh5ntruY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ia4KSUYmpTFY4J2XuPmOoGGxas0sARX0XWLDoMsONxv/pulqI5zZJQrNM2CdgCKyjyBISVUOgwQss0glnEDOTVuxjmsRNE0n+wrLxKs6MGl05bbFs8fkZDZIwh/2C7kntp2KUn74Hq74LKQChrbSIFQ7NxzDKHQ2YhT2yhJU9kU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kkNCI6KC; 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="kkNCI6KC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65DBFC4CEF7; Tue, 17 Mar 2026 09:36:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773740188; bh=dnkb6p/eQabzlKwOsghBnbUtdqt485jhvOonh5ntruY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kkNCI6KCcoSbIGE35yWrT+la6Q49tptr7+dkvUP6rJZY+XkXdKqhFK5Czo6s37+kI pBZQ+YL3c7YKwMi9SARLtsnDMysC8poX/CpruZTxBRpCDTl2lT6aUXxNyZsJn6Y/HH fP8FmxS9bCMT7vep7SCDKfFgHlOIP7j0zfiAWUxxbafhNmQ6crC1xH4/dG5aD8VlKr zqFIOgojExK68PXtT9kmLIFfmpeeaJuM8uHTVbtvdvcIFFJzBpvQEE6Si3J0aikVN2 YzagfKw1nlQcrWmPPYMVe72aEsmlF/7hFlqAk5ViTibvl555eD96TR6vfrDim62Ang GZYjh9cxeOFog== 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 v10 1/5] ring-buffer: Fix to update per-subbuf entries of persistent ring buffer Date: Tue, 17 Mar 2026 18:36:25 +0900 Message-ID: <177374018573.2358053.10088271720776923412.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog In-Reply-To: <177374017536.2358053.12341235939816794384.stgit@mhiramat.tok.corp.google.com> References: <177374017536.2358053.12341235939816794384.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 validation loop in rb_meta_validate_events() updates the same cpu_buffer->head_page->entries, the other subbuf entries are not updated. Fix to use head_page to update the entries field, since it is the cursor in this loop. Fixes: 5f3b6e839f3c ("ring-buffer: Validate boot range memory events") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- kernel/trace/ring_buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 96e0d80d492b..d6bebb782efc 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -2024,7 +2024,7 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) =20 entries +=3D ret; entry_bytes +=3D local_read(&head_page->page->commit); - local_set(&cpu_buffer->head_page->entries, ret); + local_set(&head_page->entries, ret); =20 if (head_page =3D=3D cpu_buffer->commit_page) break; From nobody Mon Apr 6 23:37:14 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 65755315D58; Tue, 17 Mar 2026 09:36:36 +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=1773740196; cv=none; b=eE5FNccYlmGR0B4OGsIQyKJDWEI6tWTfqaKa1CB13Foz9XMs2cdvxFs7yVc3mkuBCezu1ScDIIpTOwHc05gvowzMtZJYfa3aL3hRg2qStR161jAGQKXUjhGwm25STXuo0OxLgASkUBpC01Q68qVe+mynCv5jWDEVW7A0kw3AFDY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773740196; c=relaxed/simple; bh=e02wVcwtNvt8d9EHC1YeVfwX/7Bn7ErPK/kzUP+UmeI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lmkdjGuVrREM/d2qcikspOh7WpqgSbpK1NSQWy6JqrKDhV+WXc1l1dWJWK3MI6bHdDK2TT8RkM2F3QIz3YV9wzkydyNv3SFrW/ywPT1+ju3YfQ/n7GN4nnDiIoEaEbTrhK4ozmM0lNP+KsCWtg9+uk7SumdkPmaR2hKPZ3i/3bs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UlN3bsh7; 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="UlN3bsh7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3A79BC19425; Tue, 17 Mar 2026 09:36:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773740196; bh=e02wVcwtNvt8d9EHC1YeVfwX/7Bn7ErPK/kzUP+UmeI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UlN3bsh7sSST6Ta3gx09+cY0xbJaKQoULiC/swS8G6UEP5EJC8XNiySKiTKyYNip4 P965OxLwg+Xy6XyqE/lCTxy+oOclbu6tbFoCyvmSfOGz461IgdcqGwt+DGCTwHVydA ICYQ8KDvGBxgiAYE9TRIXizwxafDkfjrop4Ss6f8clZ+HmnOAQplugJ6064Y+4UR9/ MCeDPXnIHAIuHGjpLWfYzfXxFXBL9rXvUcREkvfX+Kmjx6djIWXskzRwrhEHpur6y5 sB3Je9XLiqeYZVFfRuWd1hquL+QsqT1BJp530tgmi6xvKRvY/nZMEdi42ITCqHNQH1 PxIlosri/22dQ== 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 v10 2/5] ring-buffer: Flush and stop persistent ring buffer on panic Date: Tue, 17 Mar 2026 18:36:33 +0900 Message-ID: <177374019350.2358053.9073773987793678481.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog In-Reply-To: <177374017536.2358053.12341235939816794384.stgit@mhiramat.tok.corp.google.com> References: <177374017536.2358053.12341235939816794384.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 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..930d96571f23 --- /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 */ +#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 d6bebb782efc..3d2acaf75e79 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 Mon Apr 6 23:37:14 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 25F3D345CC0; Tue, 17 Mar 2026 09:36:43 +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=1773740204; cv=none; b=Jw33Rqpx+UCJu8fPQYgH+dtf5AZmA5hSkEgeTG9PTXiidCj0BNEWnp7MfJ5DYXpPSAM41KkU5xDDbpmNYruskwYHqRzr69CBE3XHaZTYF95UNztL2jGCa63NfX99PqlKq4I3exYwVi+UlSewjtkUrFF7B8iHqT2GM4QJk+/Pb24= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773740204; c=relaxed/simple; bh=6oymFQtx3PvpZAiC0svp0n5m+NWVV/2ZQ8R7+Aq3JJk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fW9CXrMzy1PRt6+8Dr2OXwljcmyANYXi7qoUa8ZRUx57VHTrXeOe3p/PvZc817Nv5XnyFIvkC+nydVPeu/HlVxwGd/uJlUPQ9if4nZ/twYxsxqf6LDVF24CNiNxR5YUPPrVa+Ft5UI52PnAgNWsCod/XO6SaysxxD4YZ915Q5SU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ddECJOGf; 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="ddECJOGf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F0FA1C4CEF7; Tue, 17 Mar 2026 09:36:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773740203; bh=6oymFQtx3PvpZAiC0svp0n5m+NWVV/2ZQ8R7+Aq3JJk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ddECJOGftuvePOPxrzEwoSy+8C+9NnE+GYSuUAUcLcvsEF+Et4WUR6BQceGQQCPml WspfCNgVY3ppx9I0k6SkkTuJYNTGrGluLOeZPNyqY10f8iWU/VWnhMukXN96FEercF z8fmDdYqdV3HWVWHh0DEfPaHkIhUQCmV38gD0fe1BbrdeFBpm1yKix4pH2IC3w9en4 1tf3V3xisII6dl1r1N3h5xgg/hYq5LQAj/Wz/n6LPb1xt0q7DQWetASJkRD3SkI7xq hULIizO6cihAjIGk8rLPItjT2NxH+HPwD1t1kttWxSu1B01YOln0SirG3+BUThl2ic 13nIVTVTebC4A== 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 v10 3/5] ring-buffer: Skip invalid sub-buffers when validating persistent ring buffer Date: Tue, 17 Mar 2026 18:36:41 +0900 Message-ID: <177374020132.2358053.15988802077150889305.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog In-Reply-To: <177374017536.2358053.12341235939816794384.stgit@mhiramat.tok.corp.google.com> References: <177374017536.2358053.12341235939816794384.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 subbuffersa that ar found to be corrupted. Signed-off-by: Masami Hiramatsu (Google) --- 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 3d2acaf75e79..67826021867b 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 Mon Apr 6 23:37:14 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 C629035CB80; Tue, 17 Mar 2026 09:36:51 +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=1773740211; cv=none; b=OVZnBdO79oGY0eeSiEwgWMk4i67EMTZEEL25j6PvR7d09B10vmardBcS716+CeVWPmYvEoAxHolHIrV2QlnZ4sIWsT4YtOTVq0rqw4H62w9ejKb6grsZLErUOa3zQEov9G0hwpmwYmOSfzCK1BOUlaD5crYi6WCUMNnaWWIbStM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773740211; c=relaxed/simple; bh=cnEeIaIEyw7n5zEntR6NgfDfQhbUUyhzCWjcD7WdC2k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cXudUW05OX3EbcNBGpshoE2sYUXCjBvVSSrcOomZULWU3likDnZRA4oJ6uHA72PuYqs0s3xEt2iEM/6EvpX5r8J3VojDeFaOlk195eFs6BaZQF0fLGgJ9O9JQ0h1lieX5pKa9TwrwAoLJNusJXRO5Z2HxG9XSaQwH54PvoSiDNA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ecQlasBH; 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="ecQlasBH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4386C19425; Tue, 17 Mar 2026 09:36:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773740211; bh=cnEeIaIEyw7n5zEntR6NgfDfQhbUUyhzCWjcD7WdC2k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ecQlasBHi0L1bn20jjt3ZSjqioSIF+35azW6JnVxyAdM7COBKePAZJBtwqGI60ahD fWaSHi68t8y4lHFzusbhLpD7Kpu0P6noGEmOeoxyeOrPeswCogDEWiS1FJbzuxyHd9 X4+pGcYexdRWuzVTlb8/pTb4OqHoe3WnNgRWYxKTenVa8ZpFe6hWkN3vBGsuNO47p3 2lKPEKNwoEXjh1Q3aBXiVwmBX4Qj4G9ASEmAUIw0m64jd/W7rnoJIz48uHYbsP623B JccNouHbG1JJgIQJ70M9gHBMgJEaO2GVM7Ow2PcePdVLAB5G1JqOfQkiJq570KLSzS soOciwCTmHKOg== 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 v10 4/5] ring-buffer: Skip invalid sub-buffers when rewinding persistent ring buffer Date: Tue, 17 Mar 2026 18:36:49 +0900 Message-ID: <177374020905.2358053.8035885338481549558.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog In-Reply-To: <177374017536.2358053.12341235939816794384.stgit@mhiramat.tok.corp.google.com> References: <177374017536.2358053.12341235939816794384.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 v10: - Newly added. --- kernel/trace/ring_buffer.c | 65 ++++++++++++++++++++++++----------------= ---- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 67826021867b..c33ff60dea6f 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,28 @@ 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) + /* Ensure the page has older data than the previous verified page. */ + if (ts < head_page->page->time_stamp) 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. */ + 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++; + } 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 +2043,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 +2056,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; From nobody Mon Apr 6 23:37:14 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 C513034DB66; Tue, 17 Mar 2026 09:36: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=1773740219; cv=none; b=ZEn36BLF1WuiTf5M07eEyUN/2bZflfBsLQHd2ELalqgVK+poHRtnIkN05jyh57zV+EccpXzrt4mEtpVblRM4ziEai+xoBeUWQOrO4ecvxhPaKMS+nrJzUyZNyNBIKFhuXKQg1fnkJOMmbHgq1l2elcjxEPiOoqtu3qlDvGup0hw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773740219; c=relaxed/simple; bh=E0W39Bc7/1GzXzOo6n1qMki6qjbCLRtEtH7Mex9dUbw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lIn1lZ9uzOQT6JsNEB0eYt+FXzDjhQX3Z2LjHYcbHSRrc0j3BHBtKizpS6y5x2YgATkwUz4eRSk+tXWVdkBkbkniG89oPZ46ryiSNV83B3JDZUDLYUhvqgAVruxYT8rjYh4I7TdNJQO5qVAkRixUYtyEvBYIq0BOFVI9otdlLy0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rseujuVN; 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="rseujuVN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90220C4CEF7; Tue, 17 Mar 2026 09:36:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773740219; bh=E0W39Bc7/1GzXzOo6n1qMki6qjbCLRtEtH7Mex9dUbw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rseujuVNE1AP4Seys+vUWml/P87gBuwGseUxOhPqU9ka9EAlT1vtWaoaEPeRBViW3 hZogV6mnZFraQdQOMU7PbRT+gPIuOMHwTgodM0owuvQp2hDZvjnY8FpACpn7D3o9Lk zC6OhcxF7iCyAv5sMWilI/qJjcJ5ZV+IqhK9aOLs0lqamJ1dcchM3YblaOwasL42Bl I2JQCDMtWHhWDYnwxZh81Q2ZDqLNZ4Vlh4nO0QbGFEXg6/eM1hngTAyu6pAlUj+Twy WybTpLLnsz9Tr7LdvWWuQB2TX+YcVRJ4k/kYpa4zxTr5xautMgTlsVQaozuFws0FsR 5bF74O1DTH+Qw== 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 v10 5/5] ring-buffer: Add persistent ring buffer selftest Date: Tue, 17 Mar 2026 18:36:57 +0900 Message-ID: <177374021689.2358053.1239832388084112468.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog In-Reply-To: <177374017536.2358053.12341235939816794384.stgit@mhiramat.tok.corp.google.com> References: <177374017536.2358053.12341235939816794384.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_SELFTEST 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 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 | 15 +++++++++ kernel/trace/ring_buffer.c | 69 +++++++++++++++++++++++++++++++++++++++= ++++ kernel/trace/trace.c | 4 ++ 4 files changed, 89 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..094d5511bb17 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -1202,6 +1202,21 @@ 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_SELFTEST + bool "Enable persistent ring buffer selftest" + 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. + 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 c33ff60dea6f..82be6c3b9feb 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_SELFTEST + __u32 nr_invalid; + __u32 entry_bytes; +#endif int buffers[]; }; =20 @@ -2072,6 +2076,19 @@ 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_SELFTEST + 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); +#endif return; =20 invalid: @@ -2552,12 +2569,64 @@ static void rb_free_cpu_buffer(struct ring_buffer_p= er_cpu *cpu_buffer) kfree(cpu_buffer); } =20 +#ifdef CONFIG_RING_BUFFER_PERSISTENT_SELFTEST +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_SELFTEST */ +#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 5e1129b011cb..dc23fa63c789 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -9349,6 +9349,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) { @@ -9361,6 +9363,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,