ring-buffer: Handle a reader taking the new head in rb_handle_head_page()
Subsystems (3)
PATCH v2 1/1 ring-buffer: Handle a reader taking the new head in rb_handle_head_page()
The following warning occasionally fires on a KASAN-enabled debug kernel in Meta's fleet:
WARNING: kernel/trace/ring_buffer.c:3793 at rb_move_tail+0xf78/0x1050 Call trace: rb_move_tail+0xf78/0x1050 (P) __rb_reserve_next+0x648/0xac0 ring_buffer_lock_reserve+0xd00/0xf00 trace_buffer_lock_reserve+0x28/0x188 trace_graph_return+0x220/0x3e8 ftrace_return_to_handler+0x2bc/0x5a8 return_to_handler+0x20/0x48 __arm64_sys_sched_yield+0x18/0x30 (F)
rb_handle_head_page() moves the head page forward in two steps: it marks the pointer to the old head page UPDATE, then marks the pointer to the page after it (new_head) HEAD. The second cmpxchg is assumed to return only HEAD or NORMAL, but it can also return RB_PAGE_MOVED:
CPU 0 (writer) CPU 1 (reader)
------------------------------------ -------------------------------- rb_handle_head_page()
old head ptr: HEAD -> UPDATE
new_head = page after old head
<interrupt: nested write>
sees UPDATE, marks new_head
HEAD and moves the tail page
</interrupt>
rb_get_reader_page()
swaps new_head out for the reader page
cmpxchg(new_head|NORMAL ->
new_head|HEAD) -> MOVED
RB_WARN_ON()
A nested writer that interrupts the outer one after the second cmpxchg still sees the old head pointer UPDATE and repeats the second step. If the reader swaps new_head out just before that cmpxchg, it returns RB_PAGE_MOVED too.
RB_WARN_ON() permanently disables recording on the whole ring buffer, and the outer writer returns without clearing its UPDATE flag.
In both cases another writer has already moved the head off the old head page, as when the cmpxchg returns HEAD. Treat RB_PAGE_MOVED the same way and fall through: the outer writer still clears its UPDATE flag, and the caller updates the tail page as usual.
---
Changes in v2:
- Fall through on RB_PAGE_MOVED instead of returning 1, so a nested
writer still moves the tail page (Vincent)
- Drop the duplicated UPDATE clear (Vincent)
- Document RB_PAGE_MOVED in the "Valid returns" comment (Vincent)
- Commit message improvements
- Link to v1: https://patch.msgid.link/[email protected]
--- kernel/trace/ring_buffer.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/trace/ring_buffer.c +3 -0
@@ -3783,10 +3783,13 @@ rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,* 1) We really set it. * 2) A bunch of interrupts came in and moved * the page forward again. +* MOVED - another writer set it, then a reader on +* another CPU swapped it for its reader page. */ switch (ret) {case RB_PAGE_HEAD :case RB_PAGE_NORMAL : +case RB_PAGE_MOVED :/* OK */ break ;default :
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261102-ring-buffer-head-moved-f9caf5da9444
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] ring-buffer: duplicate RB_PAGE_HEAD race in rb_handle_head_page() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ring-buffer: duplicate RB_PAGE_HEAD race in rb_handle_head_page() --
commit 0a0249d6b3f6922c5b67d590d4639ec1b62531b2
Author: Breno Leitao <[email protected]>
ring-buffer: Handle a reader taking the new head in rb_handle_head_page()
This commit updates rb_handle_head_page() to treat RB_PAGE_MOVED returned by rb_head_page_set_head() the same as RB_PAGE_HEAD and RB_PAGE_NORMAL when a concurrent reader swaps out new_head after a nested writer already marked it as the head page.
Quoted 8 lines
kernel/trace/ring_buffer.c +0
-0@@ -3783,10 +3783,13 @@ rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,* 1) We really set it. * 2) A bunch of interrupts came in and moved * the page forward again.
kernel/trace/ring_buffer.c rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer, · line 3795
Quoted 10 lines
kernel/trace/ring_buffer.c +3
-0+* MOVED - another writer set it, then a reader on +* another CPU swapped it for its reader page. */ switch (ret) {case RB_PAGE_HEAD :case RB_PAGE_NORMAL : +case RB_PAGE_MOVED :/* OK */ break ;default :
[Severity: High]
As reported by Sashiko on v1 [1], does handling RB_PAGE_MOVED here cover the case where the reader swaps out the new head page before the writer loads new_head via rb_inc_page(&new_head)?
In rb_handle_head_page(), new_head is loaded right before calling rb_head_page_set_head():
new_head = next_page; rb_inc_page(&new_head); ret = rb_head_page_set_head(cpu_buffer, new_head, next_page, RB_PAGE_NORMAL);
rb_head_page_set_head() only returns RB_PAGE_MOVED if the reader swaps new_head out in the narrow window between rb_inc_page(&new_head) and the cmpxchg in rb_head_page_set_head().
If the reader swaps the page out in __rb_get_reader_page() before rb_inc_page(&new_head) executes (either while the outer writer is interrupted after rb_head_page_set_update(), or before a nested interrupt writer reaches rb_inc_page(&new_head)), __rb_get_reader_page() sets RB_PAGE_HEAD on reader_page->list.next and replaces next_page->list.next with &reader_page->list | RB_PAGE_NORMAL:
rb_set_list_to_head(&cpu_buffer->reader_page->list); ... ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
When the writer then executes rb_inc_page(&new_head), it loads the newly spliced reader_page as new_head. rb_head_page_set_head() then matches &reader_page->list | RB_PAGE_NORMAL, sets next_page->list.next to RB_PAGE_HEAD, and returns RB_PAGE_NORMAL instead of RB_PAGE_MOVED.
In the recovery check that follows:
if (ret == RB_PAGE_NORMAL) {
struct buffer_page *buffer_tail_page;
buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
/*
* If the tail had moved passed next, then we need
* to reset the pointer.
*/
if (buffer_tail_page != tail_page &&
buffer_tail_page != next_page)
rb_head_page_set_normal(cpu_buffer, new_head,
next_page,
RB_PAGE_HEAD);
}cpu_buffer->tail_page is still either next_page (if the nested writer moved the tail) or tail_page (if the nested writer interrupted after the outer writer's second cmpxchg), so rb_head_page_set_normal() is not called. Wouldn't that leave RB_PAGE_HEAD set on both next_page->list.next and reader_page->list.next simultaneously?
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Steven Rostedt
maintainerQuoted 19 lines · On Thu, 05 Nov 2026 03:12:42 -0800 Breno Leitao <[email protected]> wrote:
The following warning occasionally fires on a KASAN-enabled debug kernel in Meta's fleet:
WARNING: kernel/trace/ring_buffer.c:3793 at rb_move_tail+0xf78/0x1050 Call trace: rb_move_tail+0xf78/0x1050 (P) __rb_reserve_next+0x648/0xac0 ring_buffer_lock_reserve+0xd00/0xf00 trace_buffer_lock_reserve+0x28/0x188 trace_graph_return+0x220/0x3e8 ftrace_return_to_handler+0x2bc/0x5a8 return_to_handler+0x20/0x48 __arm64_sys_sched_yield+0x18/0x30 (F)
rb_handle_head_page()moves the head page forward in two steps: it marks the pointer to the old head page UPDATE, then marks the pointer to the page after it (new_head) HEAD. The second cmpxchg is assumed to return only HEAD or NORMAL, but it can also returnRB_PAGE_MOVED:
Quoted 8 lines
CPU 0 (writer) CPU 1 (reader)
------------------------------------ --------------------------------rb_handle_head_page()
old head ptr: HEAD -> UPDATE
new_head= page after old head
<interrupt: nested write>
sees UPDATE, marksnew_head
HEAD and moves the tail page
Wait? The interrupt should never change UPDATE to HEAD.
Something else is broken here. Because only the one that set HEAD to update is allowed to set it back to NORMAL.
Quoted 4 lines
</interrupt>
rb_get_reader_page()
swapsnew_headout for the reader page
This should not happen. The entire point of setting the pointer to UPDATE is to prevent swaps from happening when any commit is in progress.
The rb_handle_head_page() even states this:
/*
* If this was the outer most commit (the one that
* changed the original pointer from HEAD to UPDATE),
* then it is up to us to reset it to NORMAL.
*/
if (type == RB_PAGE_HEAD) {
ret = rb_head_page_set_normal(cpu_buffer, next_page,
tail_page,
RB_PAGE_UPDATE);
if (RB_WARN_ON(cpu_buffer,
ret != RB_PAGE_UPDATE))
return -1;
}Quoted 20 lines
cmpxchg(new_head|NORMAL -> new_head|HEAD) -> MOVED RB_WARN_ON()A nested writer that interrupts the outer one after the second cmpxchg still sees the old head pointer UPDATE and repeats the second step. If the reader swaps
new_headout just before that cmpxchg, it returnsRB_PAGE_MOVEDtoo.
RB_WARN_ON()permanently disables recording on the whole ring buffer, and the outer writer returns without clearing its UPDATE flag.In both cases another writer has already moved the head off the old head page, as when the cmpxchg returns HEAD. Treat
RB_PAGE_MOVEDthe same way and fall through: the outer writer still clears its UPDATE flag, and the caller updates the tail page as usual.Fixes 77ae365eca89 ("ring-buffer: make lockless")Signed-off-by Breno Leitao <[email protected]>
Quoted 8 lines
---
Changes in v2:
- Fall through onRB_PAGE_MOVEDinstead of returning 1, so a nested
writer still moves the tail page (Vincent)
- Drop the duplicated UPDATE clear (Vincent)
- DocumentRB_PAGE_MOVEDin the "Valid returns" comment (Vincent)
- Commit message improvements
- Link to v1: https://patch.msgid.link/[email protected]
From your v1 comment:
Quoted 7 lines
A nested writer and a consuming reader can race so that the second cmpxchg of a head page move in
rb_handle_head_page()returnsRB_PAGE_MOVED. That tripsRB_WARN_ON(), which disables the ring buffer and leaves the old head pointer flagged UPDATE. The patch handles the return like theRB_PAGE_MOVEDcase of the first cmpxchg: clear the UPDATE flag if this writer set it, and retry.
Quoted 5 lines
Testing, on arm64 KVM guests under virtme-ng (32 vCPUs, KASAN config, next-20261030;
kernel/trace/ring_buffer.cis identical in v7.4-rc1):
- Setup:function_graphtracer filtered to "*sched*", buffer_size_kb=7 (two pages per CPU), overwrite on, atrace_pipe_rawreader on every CPU plus atrace_pipereader, and stress-ng --yield/--timer/--futex.
OK, I think I see the issue here. I believe the reader is finding the wrong head page. Making the buffer really small can make this scenario happen more often.
The problem is where the reader thinks the "head_page" is. When the reader swaps out the page, it first looks for the "head_page". The head page always has flags set in the pointer to it, so before the swap, the reader will search for the head page.
It starts with the cpu_buffer->head_page pointer which is where it found it last. It then searches for the head page and when it finds it, it tries the swap. The swap will only succeed if the pointer is HEAD.
The issue I think you hit was that we have two HEAD pages, when interrupts come in. That is, we can get a state of:
--> subbuf --UPDATE--> subbuf --> subbuf --HEAD--> subbuf -->
With the expectation that the reader will never be between those two pointers and it will always find the UPDATE before it sees the new HEAD. But since only the reader moves the cpu_buffer->head_page around and the writer does not, we can have a stale pointer of this:
--> subbuf --UPDATE--> subbuf --> subbuf --HEAD--> subbuf -->
^
|
cpu_buffer->head_pageAnd when the reader looks for the new head, it will find this:
--> subbuf --UPDATE--> subbuf --> subbuf --HEAD--> subbuf -->
^
|
cpu_buffer->head_pageAnd possibly swap out the head page before it is allowed to.
To prevent this, I believe having the writer move this pointer will keep it from being stale and ending up between the two.
Can you try this?
kernel/trace/ring_buffer.c +8 -0
@@ -3721,6 +3721,14 @@ rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,local_sub (rb_page_commit (next_page), &cpu_buffer->entries_bytes);local_inc (&cpu_buffer->pages_lost); +/* +* To prevent a stale head_page from being between +* the UPDATE and new HEAD pages, set it here to have +* the reader check always find the update before +* finding the HEAD page. +*/ + cpu_buffer->head_page = next_page; +if (cpu_buffer->ring_meta)rb_update_meta_head (cpu_buffer, next_page);/*
We may need to add memory barriers here, but for now, I think this could fix it. As it should prevent the search from ever starting in between the two heads.
-- Steve
Steven Rostedt
maintainerQuoted 3 lines · On Thu, 5 Nov 2026 15:57:29 -0500 Steven Rostedt <[email protected]> wrote:
We may need to add memory barriers here, but for now, I think this could fix it. As it should prevent the search from ever starting in between the two heads.
Yeah, I think this will do it. Please test, I'll go write up a formal patch:
-- Steve
kernel/trace/ring_buffer.c +16 -1
@@ -1494,7 +1494,13 @@ rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)if (RB_WARN_ON (cpu_buffer,rb_list_head (list->prev->next) != list))return NULL ; - page = head = cpu_buffer->head_page; +/* +* Make sure that the start of the search of the head_page does not +* start between the writer adding the UPDATE and the new HEAD that it +* moves forward. +*/ +/* Sync with the writer moving the HEAD in rb_handle_head_page() */ + page = head =smp_load_acquire (&cpu_buffer->head_page);/* * It is possible that the writer moves the header behind * where we started, and we miss in one loop.
@@ -3721,6 +3727,15 @@ rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,local_sub (rb_page_commit (next_page), &cpu_buffer->entries_bytes);local_inc (&cpu_buffer->pages_lost); +/* +* To prevent a stale head_page from being between +* the UPDATE and new HEAD pages, set it here to have +* the reader check always find the update before +* finding the HEAD page. +*/ +/* Sync with the reader setting the head page */ +smp_store_release (&cpu_buffer->head_page, next_page); +if (cpu_buffer->ring_meta)rb_update_meta_head (cpu_buffer, next_page);/*
Signature · 1 line
2.53.0
Vincent Donnefort
maintainerQuoted 117 lines · Steven Rostedt
Quoted 19 lines · On Thu, 05 Nov 2026 03:12:42 -0800 Breno Leitao <[email protected]> wrote:
The following warning occasionally fires on a KASAN-enabled debug kernel in Meta's fleet:
WARNING: kernel/trace/ring_buffer.c:3793 at rb_move_tail+0xf78/0x1050 Call trace: rb_move_tail+0xf78/0x1050 (P) __rb_reserve_next+0x648/0xac0 ring_buffer_lock_reserve+0xd00/0xf00 trace_buffer_lock_reserve+0x28/0x188 trace_graph_return+0x220/0x3e8 ftrace_return_to_handler+0x2bc/0x5a8 return_to_handler+0x20/0x48 __arm64_sys_sched_yield+0x18/0x30 (F)
rb_handle_head_page()moves the head page forward in two steps: it marks the pointer to the old head page UPDATE, then marks the pointer to the page after it (new_head) HEAD. The second cmpxchg is assumed to return only HEAD or NORMAL, but it can also returnRB_PAGE_MOVED:Quoted 8 lines
CPU 0 (writer) CPU 1 (reader)
------------------------------------ --------------------------------rb_handle_head_page()
old head ptr: HEAD -> UPDATE
new_head= page after old head
<interrupt: nested write>
sees UPDATE, marksnew_head
HEAD and moves the tail pageWait? The interrupt should never change UPDATE to HEAD.
Something else is broken here. Because only the one that set HEAD to update is allowed to set it back to NORMAL.
Quoted 4 lines
</interrupt>
rb_get_reader_page()
swapsnew_headout for the reader pageThis should not happen. The entire point of setting the pointer to UPDATE is to prevent swaps from happening when any commit is in progress.
The
rb_handle_head_page()even states this:/* * If this was the outer most commit (the one that * changed the original pointer from HEAD to UPDATE), * then it is up to us to reset it to NORMAL. */ if (type == RB_PAGE_HEAD) { ret = rb_head_page_set_normal(cpu_buffer, next_page, tail_page, RB_PAGE_UPDATE); if (RB_WARN_ON(cpu_buffer, ret != RB_PAGE_UPDATE)) return -1; }Quoted 20 lines
cmpxchg(new_head|NORMAL -> new_head|HEAD) -> MOVED RB_WARN_ON()A nested writer that interrupts the outer one after the second cmpxchg still sees the old head pointer UPDATE and repeats the second step. If the reader swaps
new_headout just before that cmpxchg, it returnsRB_PAGE_MOVEDtoo.
RB_WARN_ON()permanently disables recording on the whole ring buffer, and the outer writer returns without clearing its UPDATE flag.In both cases another writer has already moved the head off the old head page, as when the cmpxchg returns HEAD. Treat
RB_PAGE_MOVEDthe same way and fall through: the outer writer still clears its UPDATE flag, and the caller updates the tail page as usual.Fixes 77ae365eca89 ("ring-buffer: make lockless")Signed-off-by Breno Leitao <[email protected]>Quoted 8 lines
---
Changes in v2:
- Fall through onRB_PAGE_MOVEDinstead of returning 1, so a nested
writer still moves the tail page (Vincent)
- Drop the duplicated UPDATE clear (Vincent)
- DocumentRB_PAGE_MOVEDin the "Valid returns" comment (Vincent)
- Commit message improvements
- Link to v1: https://patch.msgid.link/[email protected]From your v1 comment:
Quoted 7 lines
A nested writer and a consuming reader can race so that the second cmpxchg of a head page move in
rb_handle_head_page()returnsRB_PAGE_MOVED. That tripsRB_WARN_ON(), which disables the ring buffer and leaves the old head pointer flagged UPDATE. The patch handles the return like theRB_PAGE_MOVEDcase of the first cmpxchg: clear the UPDATE flag if this writer set it, and retry.Quoted 5 lines
Testing, on arm64 KVM guests under virtme-ng (32 vCPUs, KASAN config, next-20261030;
kernel/trace/ring_buffer.cis identical in v7.4-rc1):
- Setup:function_graphtracer filtered to "*sched*", buffer_size_kb=7 (two pages per CPU), overwrite on, atrace_pipe_rawreader on every CPU plus atrace_pipereader, and stress-ng --yield/--timer/--futex.OK, I think I see the issue here. I believe the reader is finding the wrong head page. Making the buffer really small can make this scenario happen more often.
The problem is where the reader thinks the "
head_page" is. When the reader swaps out the page, it first looks for the "head_page". The head page always has flags set in the pointer to it, so before the swap, the reader will search for the head page.
Quoted 8 lines
It starts with the
cpu_buffer->head_pagepointer which is where it found it last. It then searches for the head page and when it finds it, it tries the swap. The swap will only succeed if the pointer is HEAD.The issue I think you hit was that we have two HEAD pages, when interrupts come in. That is, we can get a state of:
--> subbuf --UPDATE--> subbuf --> subbuf --HEAD--> subbuf -->
How we can end-up in this situation? The cmpxch will only swap HEAD for UPDATE.
Quoted 53 lines
With the expectation that the reader will never be between those two pointers and it will always find the UPDATE before it sees the new HEAD. But since only the reader moves the
cpu_buffer->head_pagearound and the writer does not, we can have a stale pointer of this:--> subbuf --UPDATE--> subbuf --> subbuf --HEAD--> subbuf --> ^ | cpu_buffer->head_pageAnd when the reader looks for the new head, it will find this:
--> subbuf --UPDATE--> subbuf --> subbuf --HEAD--> subbuf --> ^ | cpu_buffer->head_pageAnd possibly swap out the head page before it is allowed to.
To prevent this, I believe having the writer move this pointer will keep it from being stale and ending up between the two.
Can you try this?
kernel/trace/ring_buffer.c +8
-0@@ -3721,6 +3721,14 @@ rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,local_sub (rb_page_commit (next_page), &cpu_buffer->entries_bytes);local_inc (&cpu_buffer->pages_lost); +/* +* To prevent a stale head_page from being between +* the UPDATE and new HEAD pages, set it here to have +* the reader check always find the update before +* finding the HEAD page. +*/ + cpu_buffer->head_page = next_page; +if (cpu_buffer->ring_meta)rb_update_meta_head (cpu_buffer, next_page);/* We may need to add memory barriers here, but for now, I think this could fix it. As it should prevent the search from ever starting in between the two heads.
-- Steve
Signature · 1 line
Vincent
Hello Steven,
Quoted 22 lines · Steven Rostedt
Can you try this?
kernel/trace/ring_buffer.c +8
-0@@ -3721,6 +3721,14 @@ rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,local_sub (rb_page_commit (next_page), &cpu_buffer->entries_bytes);local_inc (&cpu_buffer->pages_lost); +/* +* To prevent a stale head_page from being between +* the UPDATE and new HEAD pages, set it here to have +* the reader check always find the update before +* finding the HEAD page. +*/ + cpu_buffer->head_page = next_page; +if (cpu_buffer->ring_meta)rb_update_meta_head (cpu_buffer, next_page);/*
Quoted 3 lines
We may need to add memory barriers here, but for now, I think this could fix it. As it should prevent the search from ever starting in between the two heads.
This seems to be racy as well. Sashiko found the race, and it can be reproduced by adding some delays in the code.
Thanks for looking at it,
--breno
Steven Rostedt
maintainerQuoted 6 lines · On Mon, 9 Nov 2026 09:21:32 +0000 Vincent Donnefort <[email protected]>…
Quoted 4 lines
The issue I think you hit was that we have two HEAD pages, when interrupts come in. That is, we can get a state of:
--> subbuf --UPDATE--> subbuf --> subbuf --HEAD--> subbuf -->How we can end-up in this situation? The cmpxch will only swap HEAD for UPDATE.
The cpu_buffer->head_page is only updated by the reader, when it did its last read.
Let's look at it one at a time:
cpu_buffer->head_page
tail |
v v
+--> subbuf ----> subbuf --HEAD--> subbuf ----> subbuf ----> subbuf --+
| |
+---------------------------------------------------------------------+Then the writer moves the HEAD page around the buffer while there are no reads happening
cpu_buffer->head_page
tail |
v v
+--> subbuf ----> subbuf ----> subbuf --HEAD--> subbuf ----> subbuf --+
| |
+---------------------------------------------------------------------+ cpu_buffer->head_page
| tail
v v
+--> subbuf ----> subbuf ----> subbuf ----> subbuf --HEAD--> subbuf --+
| |
+---------------------------------------------------------------------+ cpu_buffer->head_page
| tail
v v
+-HEAD-> subbuf ----> subbuf ----> subbuf ----> subbuf ----> subbuf --+
| |
+---------------------------------------------------------------------+ cpu_buffer->head_page
tail |
v v
+--> subbuf --HEAD--> subbuf ----> subbuf ----> subbuf ----> subbuf --+
| |
+---------------------------------------------------------------------+Now, I'll break up the update part:
cpu_buffer->head_page
tail |
v v
+--> subbuf --UPDATE--> subbuf ----> subbuf ----> subbuf ----> subbuf --+
| |
+-----------------------------------------------------------------------+ cpu_buffer->head_page
tail |
v v
+--> subbuf --UPDATE--> subbuf --HEAD--> subbuf ----> subbuf ----> subbuf --+
| |
+---------------------------------------------------------------------------+An interrupt comes in and moves the head even more.
cpu_buffer->head_page
tail |
v v
+--> subbuf --UPDATE--> subbuf ----> subbuf --HEAD--> subbuf ----> subbuf --+
| |
+---------------------------------------------------------------------------+Now the reader decides to read again and starts between the UPDATE and the HEAD
cpu_buffer->head_page
tail |
v v
+--> subbuf --UPDATE--> subbuf ----> subbuf --HEAD--> subbuf ----> subbuf --+
| |
+---------------------------------------------------------------------------+It finds a page with the HEAD pointer set but not the UPDATE pointer and does a swap!
reader_page -----+
|
cpu_buffer->head_page |
tail | |
v v v
+--> subbuf --UPDATE--> subbuf ----> subbuf ----> subbuf --HEAD--> subbuf --+
| |
+---------------------------------------------------------------------------+Now the commit finishes and moves forward and will even hit a blank page but the last write is now on the reader page. This breaks the entire integrity of the ring buffer.
Does this make sense?
-- Steve
Steven Rostedt
maintainerQuoted 25 lines · On Mon, 9 Nov 2026 05:10:37 -0800 Breno Leitao <[email protected]> wrote:
Hello Steven,
Quoted 22 lines · Steven Rostedt
Can you try this?
kernel/trace/ring_buffer.c +8
-0@@ -3721,6 +3721,14 @@ rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,local_sub (rb_page_commit (next_page), &cpu_buffer->entries_bytes);local_inc (&cpu_buffer->pages_lost); +/* +* To prevent a stale head_page from being between +* the UPDATE and new HEAD pages, set it here to have +* the reader check always find the update before +* finding the HEAD page. +*/ + cpu_buffer->head_page = next_page; +if (cpu_buffer->ring_meta)rb_update_meta_head (cpu_buffer, next_page);/*
Quoted 6 lines
Quoted 3 lines
We may need to add memory barriers here, but for now, I think this could fix it. As it should prevent the search from ever starting in between the two heads.
This seems to be racy as well. Sashiko found the race, and it can be reproduced by adding some delays in the code.
Yeah, I'm currently traveling and didn't have time to do a deep dive into a real fix and decided to be lazy and post the patch and see what sashiko found ;-)
Thanks for verifying the issue with this "fix" that Sashiko pointed out.
I need to spend a bit of time looking deeper into it to come up with a real solution.
-- Steve
Vincent Donnefort
maintainerQuoted 100 lines · Steven Rostedt
Quoted 6 lines · On Mon, 9 Nov 2026 09:21:32 +0000 Vincent Donnefort <[email protected]>…
Quoted 4 lines
The issue I think you hit was that we have two HEAD pages, when interrupts come in. That is, we can get a state of:
--> subbuf --UPDATE--> subbuf --> subbuf --HEAD--> subbuf -->How we can end-up in this situation? The cmpxch will only swap HEAD for UPDATE.
The
cpu_buffer->head_pageis only updated by the reader, when it did its last read.Let's look at it one at a time:
cpu_buffer->head_page tail | v v +--> subbuf ----> subbuf --HEAD--> subbuf ----> subbuf ----> subbuf --+ | | +---------------------------------------------------------------------+Then the writer moves the HEAD page around the buffer while there are no reads happening
cpu_buffer->head_page tail | v v +--> subbuf ----> subbuf ----> subbuf --HEAD--> subbuf ----> subbuf --+ | | +---------------------------------------------------------------------+cpu_buffer->head_page | tail v v +--> subbuf ----> subbuf ----> subbuf ----> subbuf --HEAD--> subbuf --+ | | +---------------------------------------------------------------------+cpu_buffer->head_page | tail v v +-HEAD-> subbuf ----> subbuf ----> subbuf ----> subbuf ----> subbuf --+ | | +---------------------------------------------------------------------+cpu_buffer->head_page tail | v v +--> subbuf --HEAD--> subbuf ----> subbuf ----> subbuf ----> subbuf --+ | | +---------------------------------------------------------------------+Now, I'll break up the update part:
cpu_buffer->head_page tail | v v +--> subbuf --UPDATE--> subbuf ----> subbuf ----> subbuf ----> subbuf --+ | | +-----------------------------------------------------------------------+cpu_buffer->head_page tail | v v +--> subbuf --UPDATE--> subbuf --HEAD--> subbuf ----> subbuf ----> subbuf --+ | | +---------------------------------------------------------------------------+An interrupt comes in and moves the head even more.
cpu_buffer->head_page tail | v v +--> subbuf --UPDATE--> subbuf ----> subbuf --HEAD--> subbuf ----> subbuf --+ | | +---------------------------------------------------------------------------+Now the reader decides to read again and starts between the UPDATE and the HEAD
cpu_buffer->head_page tail | v v +--> subbuf --UPDATE--> subbuf ----> subbuf --HEAD--> subbuf ----> subbuf --+ | | +---------------------------------------------------------------------------+It finds a page with the HEAD pointer set but not the UPDATE pointer and does a swap!
reader_page -----+ | cpu_buffer->head_page | tail | | v v v +--> subbuf --UPDATE--> subbuf ----> subbuf ----> subbuf --HEAD--> subbuf --+ | | +---------------------------------------------------------------------------+
Quoted 7 lines
Now the commit finishes and moves forward and will even hit a blank page but the last write is now on the reader page. This breaks the entire integrity of the ring buffer.
Does this make sense?
-- Steve
Yes, now I spent more time on it, I believe it makes sense. I looked at what we could do and I'm trying to use a temporary RB_PAGE_HEAD | RB_PAGE_UPDATE. This link flag is then only converted to RB_PAGE_HEAD by the outer writer. I believe this would fix the race entirely... However that means the reader spins as long as the buffer writing is interrupted, which I admit is far from ideal.
Signature · 1 line
Vincent
Steven Rostedt
maintainerQuoted 5 lines · On Wed, 11 Nov 2026 13:32:13 +0000 Vincent Donnefort <[email protected]>…
Yes, now I spent more time on it, I believe it makes sense. I looked at what we could do and I'm trying to use a temporary
RB_PAGE_HEAD|RB_PAGE_UPDATE. This link flag is then only converted toRB_PAGE_HEADby the outer writer. I believe this would fix the race entirely... However that means the reader spins as long as the buffer writing is interrupted, which I admit is far from ideal.
But it is only interrupted by interrupts ;-) which should be fast.
No different than waiting for a spin lock whose owner got interrupted by an interrupt.
-- Steve
Vincent Donnefort
maintainerQuoted 9 lines · Steven Rostedt
Quoted 5 lines · On Wed, 11 Nov 2026 13:32:13 +0000 Vincent Donnefort <[email protected]>…
Yes, now I spent more time on it, I believe it makes sense. I looked at what we could do and I'm trying to use a temporary
RB_PAGE_HEAD|RB_PAGE_UPDATE. This link flag is then only converted toRB_PAGE_HEADby the outer writer. I believe this would fix the race entirely... However that means the reader spins as long as the buffer writing is interrupted, which I admit is far from ideal.
Quoted 6 lines
But it is only interrupted by interrupts ;-) which should be fast.
No different than waiting for a spin lock whose owner got interrupted by an interrupt.
-- Steve
Yes hopefully it shouldn't be too terrible. But, as we disable interrupts on the CPU doing the read, it's sort of propagating the latency from one CPU to the other, which I believe wasn't the case so far, since an interrupt could "release" the reader by setting HEAD.
Perhaps I could propagate a -EAGAIN back to ring_buffer_consume to temporarily release rb_reader_lock() and try again there? But that would be a bit intrusive as most of the functions are returning a ring_buffer_event * and not an int.
The attempt is here [1]. Sashiko had something to say but I haven't processed yet how bad it is.
[1] https://lore.kernel.org/all/[email protected]/
Signature · 1 line
Vincent