From nobody Tue Apr 7 15:29:12 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 8DB12363C4A; Thu, 26 Feb 2026 13:38:38 +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=1772113118; cv=none; b=KACXM6p9rvg2l7aJ6mP7K4A0ZXxoxvjdZxS/YCipxiGwkzUtZqCp7vuHz4Drz3EvuioxM/Mayw1zPD3Wj87Qvg3FPf2Zsf1EJJdx12qCILpiBK99iUhWugLN9IrMuqFeDdS2/LEkIAooQwkyAYRwAqNQa3Ue+rbdcl/JbcALXZc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772113118; c=relaxed/simple; bh=XSg6yRkkFKBKMmH1rJ97eXmWwnKKidYEa2psHsJwUe4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dtHVoJW6IU23ek4JXCilRYUMs4HiFV/vRJvnbpVf2VOfk/VEB24QMpfzygsoCYQcZ1UKix+H41VFPLKOwnbtqjE5QHB7DDtyZc/NKFhYcX56Ft9B+57lere3MJ3nQkmZAEIKPtH7LnPa3YaYfgr21TEegpAbupThFDK6I6wD5c8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Wlw6ixzc; 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="Wlw6ixzc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9DCF8C116C6; Thu, 26 Feb 2026 13:38:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772113118; bh=XSg6yRkkFKBKMmH1rJ97eXmWwnKKidYEa2psHsJwUe4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wlw6ixzc5rlaqrrKfRbC/iDBaIZhzwcUD4ydsBQ7ZoaelObGe+bzQdu08bdxybd6X wbT5wnEvXnBaiQ/MxZSrtiBiYS8stAw9YfKSWwZneosuRZW6sKFOxT77RLVSAYZmGm kNUCx2UVT0ZZ/dKNAVqoSgJA1EXopNN2rJDfRVrkQSefJQoANued8e5uCsYwrHW4fb GknBrQGbwVv6Uu8OEffBxx1tYC31ZhyYv+/TomR44s/NtQPoMNfO2KOIDvqkrXyEX5 ZT6iAVf2eoBTlU4ZFWXyN6aleZcusRBVRtZdl3OLwKx5vWXahPZmkBqS36C1FM4F56 XIw5smRHvnZjQ== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v5 1/3] ring-buffer: Flush and stop persistent ring buffer on panic Date: Thu, 26 Feb 2026 22:38:36 +0900 Message-ID: <177211311593.419230.2212568977306190482.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <177211310553.419230.7846100548994399256.stgit@mhiramat.tok.corp.google.com> References: <177211310553.419230.7846100548994399256.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 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. --- kernel/trace/ring_buffer.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index f16f053ef77d..0eb6e6595f37 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -6,6 +6,7 @@ */ #include #include +#include #include #include #include @@ -589,6 +590,7 @@ struct trace_buffer { =20 unsigned long range_addr_start; unsigned long range_addr_end; + struct notifier_block flush_nb; =20 struct ring_buffer_meta *meta; =20 @@ -2471,6 +2473,15 @@ static void rb_free_cpu_buffer(struct ring_buffer_pe= r_cpu *cpu_buffer) kfree(cpu_buffer); } =20 +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); + flush_cache_all(); + return NOTIFY_DONE; +} + static struct trace_buffer *alloc_buffer(unsigned long size, unsigned flag= s, int order, unsigned long start, unsigned long end, @@ -2590,6 +2601,12 @@ static struct trace_buffer *alloc_buffer(unsigned lo= ng size, unsigned flags, =20 mutex_init(&buffer->mutex); =20 + /* Persistent ring buffer needs to flush cache before reboot. */ + if (start & end) { + buffer->flush_nb.notifier_call =3D rb_flush_buffer_cb; + atomic_notifier_chain_register(&panic_notifier_list, &buffer->flush_nb); + } + return_ptr(buffer); =20 fail_free_buffers: @@ -2677,6 +2694,9 @@ ring_buffer_free(struct trace_buffer *buffer) { int cpu; =20 + if (buffer->range_addr_start && buffer->range_addr_end) + atomic_notifier_chain_unregister(&panic_notifier_list, &buffer->flush_nb= ); + cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node); =20 irq_work_sync(&buffer->irq_work.work); From nobody Tue Apr 7 15:29:12 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 9F12636165F; Thu, 26 Feb 2026 13:38:46 +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=1772113126; cv=none; b=itXfz6C/gk2AvrjQjj7I9/1mjTeWPvJR8vMxLEhRO3Z30pAKfqUgRaKJGp5YX6ZJI2M11gGaR73GxAvqyxeqPj7i1f55TTVyz4tQyfsJyvXajq6LEU2aNukSU0b/0iTM8dJEdzNDqqscRa9mwqmQr39r5iQ6c23op4eQwnt5D80= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772113126; c=relaxed/simple; bh=tPEl7RiyBxci9RH9bwWwgOWrKzS/dFTt+z3BuPaz/Wg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qcm/sXv9GwjLWRprSOdUoR1EAyBkqwEg7alyfIt2G3yNG6tSHYe519tjMb6fKOnOl2pjs2TI0U8wh3pr2g4H0ZWB8ieRNY/yUquzQbKG4eM44fANM8lRne3D4wfdTwtzVTc75liHfrRZwXXUJ0Q323IWBWu/YyuKkHZNn+PS/cE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JomJTjj6; 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="JomJTjj6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50D7DC116C6; Thu, 26 Feb 2026 13:38:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772113126; bh=tPEl7RiyBxci9RH9bwWwgOWrKzS/dFTt+z3BuPaz/Wg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JomJTjj6WSMF1UUGV193DqwVV6JIcDUp4kQAUBjI65xsJDM44Cbg/p+UUIZbxNmHx PfiIN/jIlWGPxw/JYowWr2ekEBKDwf1IeGZSSzAFI3x5Nxwe70nCxkpA74JvrFGRCM MziupwTSi/7C5LPUEfsuxkk8kKCnpNm+EQBjrik8w8cDULcKGqXooKIW3GE/xeaAVo BDpA1OZTOHniOzDzsosD1zvbZ+m0iT9r03ucNEFN50NxkW9JTxbc3w/H4RL0dlDpfn ucfTd1OZcv3lhv8tHKC6qqtsIuZWyQP0DyNZF8l1/XhOnW6Gnq5ZCMkQzClixtTwc9 TK3M62HycRRwg== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v5 2/3] ring-buffer: Handle RB_MISSED_* flags on commit field correctly Date: Thu, 26 Feb 2026 22:38:43 +0900 Message-ID: <177211312362.419230.15156461178245984273.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <177211310553.419230.7846100548994399256.stgit@mhiramat.tok.corp.google.com> References: <177211310553.419230.7846100548994399256.stgit@mhiramat.tok.corp.google.com> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Since the MSBs of rb_data_page::commit are used for storing RB_MISSED_EVENTS and RB_MISSED_STORED, we need to mask out those bits when it is used for finding the size of data pages. Fixes: 5f3b6e839f3c ("ring-buffer: Validate boot range memory events") Fixes: 5b7be9c709e1 ("ring-buffer: Add test to validate the time stamp delt= as") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) --- Changes in v5: - Do not move rb_commit_index(). - Fix verify_event() and rb_cpu_meta_valid() too. Changes in v4: - Fix to move rb_commit_index() after ring_buffer_per_cpu definition. --- kernel/trace/ring_buffer.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 0eb6e6595f37..2e9b8ce6b4dc 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -395,6 +395,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 */ @@ -676,7 +682,7 @@ static void verify_event(struct ring_buffer_per_cpu *cp= u_buffer, do { if (page =3D=3D tail_page || WARN_ON_ONCE(stop++ > 100)) done =3D true; - commit =3D local_read(&page->page->commit); + commit =3D rb_page_size(page); write =3D local_read(&page->write); if (addr >=3D (unsigned long)&page->page->data[commit] && addr < (unsigned long)&page->page->data[write]) @@ -1820,13 +1826,16 @@ static bool rb_cpu_meta_valid(struct ring_buffer_cp= u_meta *meta, int cpu, =20 /* Is the meta buffers and the subbufs themselves have correct data? */ for (i =3D 0; i < meta->nr_subbufs; i++) { + unsigned long commit; + if (meta->buffers[i] < 0 || meta->buffers[i] >=3D meta->nr_subbufs) { pr_info("Ring buffer boot meta [%d] array out of range\n", cpu); return false; } =20 - if ((unsigned)local_read(&subbuf->commit) > subbuf_size) { + commit =3D local_read(&subbuf->commit) & ~RB_MISSED_MASK; + if (commit > subbuf_size) { pr_info("Ring buffer boot meta [%d] buffer invalid commit\n", cpu); return false; } @@ -1907,7 +1916,7 @@ static int rb_validate_buffer(struct buffer_data_page= *dpage, int cpu) u64 delta; int tail; =20 - tail =3D local_read(&dpage->commit); + tail =3D local_read(&dpage->commit) & ~RB_MISSED_MASK; return rb_read_data_buffer(dpage, tail, cpu, &ts, &delta); } =20 @@ -1934,7 +1943,7 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) goto invalid; } entries +=3D ret; - entry_bytes +=3D local_read(&cpu_buffer->reader_page->page->commit); + entry_bytes +=3D rb_page_size(cpu_buffer->reader_page); local_set(&cpu_buffer->reader_page->entries, ret); =20 ts =3D head_page->page->time_stamp; @@ -2054,7 +2063,7 @@ static void rb_meta_validate_events(struct ring_buffe= r_per_cpu *cpu_buffer) local_inc(&cpu_buffer->pages_touched); =20 entries +=3D ret; - entry_bytes +=3D local_read(&head_page->page->commit); + entry_bytes +=3D rb_page_size(head_page); local_set(&cpu_buffer->head_page->entries, ret); =20 if (head_page =3D=3D cpu_buffer->commit_page) @@ -3256,12 +3265,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) { @@ -4432,7 +4435,7 @@ static void check_buffer(struct ring_buffer_per_cpu *= cpu_buffer, =20 if (tail =3D=3D CHECK_FULL_PAGE) { full =3D true; - tail =3D local_read(&bpage->commit); + tail =3D local_read(&bpage->commit) & ~RB_MISSED_MASK; } else if (info->add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) { /* Ignore events with absolute time stamps */ From nobody Tue Apr 7 15:29:12 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 5281636C0BE; Thu, 26 Feb 2026 13:38: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=1772113134; cv=none; b=fIfKGO8D/LmdEStoThPX6ozN0YoaWMCaAz2OJcMuU1r27pHsgdPHT3PJUfsaM79khKbb41KkKe7UMfYRHetXxuopzaGxqXEVwDUstB70CflZ12gVpaioS5C0ihrTUrHUSiy47yE9dRnQYXKhYEomkejKWUnj36C3Doo6J7ITVj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772113134; c=relaxed/simple; bh=Grz+va2dx3OEHfYda/aWVdwrIxRxbJLjBcQHQoMMrQc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QCS29RtQI4mqhiEEw4WQn4rQi2TDp5ol5k7NsTEKg1FHBSaIMqVDHakzWyAPvDC8BEAoNfNgIp1L8e+iei8p+oASBhEQR1BSuGuTsvxnRJGUnEpEotwV3DXLNgn3O0VDVJe28jm41M8fhqEmAE30c52FnLsDg2bHFOP9sZFql9A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Cy0pOlgp; 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="Cy0pOlgp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED819C19422; Thu, 26 Feb 2026 13:38:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772113133; bh=Grz+va2dx3OEHfYda/aWVdwrIxRxbJLjBcQHQoMMrQc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cy0pOlgp3PEauRGzL04yIcRTcVDUN02G5mExTp8EMeCmVOOX+Se08tzGMrzgDfOTZ frw3Wtraba76TkFd0mHKHHZF9P6rnKQOX+a09oewlooQThG87Djn1mYG1prom31Fku fETgtfkO1JPgAUcRnqmLZ6N5S+btF6LuwDURkalzmWh9P8jJrAm7b03Z2c/6IsXuzE NdKMI+m4kmvdVKPub0Q7JZpn6v1+k753wxxtZC1c8c4gb2fL7Sqtt1+NV1hw5LVkaS duAFvhgq+wD64oL3btKK+UmOy+eYVA8crQVDsE64RraF/L0XBvzp3cTT9Zqa8MdMT3 FdFPA0QiAIMjw== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v5 3/3] ring-buffer: Skip invalid sub-buffers when validating persistent ring buffer Date: Thu, 26 Feb 2026 22:38:51 +0900 Message-ID: <177211313130.419230.16617033664882679312.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog In-Reply-To: <177211310553.419230.7846100548994399256.stgit@mhiramat.tok.corp.google.com> References: <177211310553.419230.7846100548994399256.stgit@mhiramat.tok.corp.google.com> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Skip invalid sub-buffers when validating the persistent ring buffer instead of discarding the entire ring buffer. Also, mark there are missed events on the discarded buffer. If the cache data in memory fails to be synchronized during a reboot, the persistent ring buffer may become partially corrupted, but other sub-buffers may still contain readable event data. Only discard the subbuffersa that ar found to be corrupted. Signed-off-by: Masami Hiramatsu (Google) --- Changes in 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 | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 2e9b8ce6b4dc..cfe1e15f1a93 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -1927,6 +1927,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; @@ -2053,19 +2054,19 @@ static void rb_meta_validate_events(struct ring_buf= fer_per_cpu *cpu_buffer) =20 ret =3D rb_validate_buffer(head_page->page, cpu_buffer->cpu); if (ret < 0) { - pr_info("Ring buffer meta [%d] invalid buffer page\n", - cpu_buffer->cpu); - goto invalid; - } - - /* If the buffer has content, update pages_touched */ - if (ret) - local_inc(&cpu_buffer->pages_touched); - - entries +=3D ret; - entry_bytes +=3D rb_page_size(head_page); - local_set(&cpu_buffer->head_page->entries, ret); + discarded++; + /* Instead of discard whole ring buffer, discard only this sub-buffer. = */ + local_set(&head_page->entries, 0); + local_set(&head_page->page->commit, RB_MISSED_EVENTS); + } else { + /* If the buffer has content, update pages_touched */ + if (ret) + local_inc(&cpu_buffer->pages_touched); =20 + entries +=3D ret; + entry_bytes +=3D rb_page_size(head_page); + local_set(&cpu_buffer->head_page->entries, ret); + } if (head_page =3D=3D cpu_buffer->commit_page) break; } @@ -2079,7 +2080,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: