FAILED: patch "[PATCH] ring-buffer: Fix subbuf resize race with" failed to apply to 6.12-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x e743527c5bfdceda1095bc0a9e596e2aebb6a9c3
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026090352-announcer-magnify-98b4@gregkh' --subject-prefix 'PATCH 6.12.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From e743527c5bfdceda1095bc0a9e596e2aebb6a9c3 Mon Sep 17 00:00:00 2001
From: Vincent Donnefort <[email protected]>
Date: Thu, 13 Aug 2026 14:11:47 +0100
Subject: [PATCH] ring-buffer: Fix subbuf resize race with
ring_buffer_alloc_read_page()
ring_buffer_alloc_read_page() is racy with ring_buffer_subbuf_order_set, it can allocate a reader page with an outdated order. This isn't a big issue, the user can still re-allocate a new reader page and try again.
However, what is more problematic is if the value of subbuf_order changes in the middle of ring_buffer_alloc_read_page(). In that case, bpage->order might not match the actual allocated memory.
Use bpage->order for the allocation to prevent this race.
kernel/trace/ring_buffer.c +1 -1
@@ -7018,7 +7018,7 @@ ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)if (bpage->data) {rb_init_data_page (bpage->data); }else { - bpage->data =alloc_cpu_data (cpu, cpu_buffer->buffer->subbuf_order); + bpage->data =alloc_cpu_data (cpu, bpage->order);if (!bpage->data) {kfree (bpage);return ERR_PTR (-ENOMEM );
Sasha Levin
maintainerPATCH 1/2 ring-buffer: Add helper functions for allocations
From: Steven Rostedt <[email protected]>
[ Upstream commit b1e7a590a0133606d3efd41aee38cdeac630b52f ]
The allocation of the per CPU buffer descriptor, the buffer page descriptors and the buffer page data itself can be pretty ugly:
kzalloc_node(ALIGN(sizeof(struct buffer_page), cache_line_size()),
GFP_KERNEL, cpu_to_node(cpu));And the data pages:
page = alloc_pages_node(cpu_to_node(cpu),
GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO, order); if (!page) return NULL;
bpage->page = page_address(page);
rb_init_page(bpage->page);
Add helper functions to make the code easier to read.
This does make all allocations of the data page (bpage->page) allocated with the __GFP_RETRY_MAYFAIL flag (and not just the bulk allocator). Which is actually better, as allocating the data page for the ring buffer tracing should try hard but not trigger the OOM killer.
Stable backport adjustment for e743527c5bfd:
Keep only the read-page allocation refactor needed by the target. Express alloc_cpu_data as a statement-expression macro instead of adding a C function, retaining NUMA allocation, __GFP_RETRY_MAYFAIL, compound/zeroed pages, NULL failure handling, and data-page initialization.
Rename the existing rb_init_page initializer to rb_init_data_page, without changing its implementation, and update its callers. This supplies the context expected by the target without importing the unrelated cleanup from 7cf02d0aa6bd or introducing additional functions.
Drop the CPU-buffer and buffer-page descriptor refactors and the bulk and internal reader-page allocator conversions. These are not needed by the target; leaving them intact preserves this tree's ring_buffer_meta type, __GFP_RETRY_MAYFAIL descriptor allocations, and the existing bpage->order and resize-disabled fixes. Only newly allocated external read pages switch from __GFP_NORETRY to __GFP_RETRY_MAYFAIL in this backport.
The target's change to allocate using the saved bpage->order is deliberately left for e743527c5bfd itself.
Stable-dep-of: e743527c5bfd ("ring-buffer: Fix subbuf resize race with ring_buffer_alloc_read_page()")
--- kernel/trace/ring_buffer.c | 50 ++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 21 deletions(-)
kernel/trace/ring_buffer.c +29 -21
@@ -382,7 +382,7 @@ struct buffer_page {#define RB_WRITE_MASK 0xfffff#define RB_WRITE_INTCNT (1 << 20) -static void rb_init_page (struct buffer_data_page *bpage) +static void rb_init_data_page (struct buffer_data_page *bpage) {local_set (&bpage->commit,0 ); }
@@ -411,6 +411,21 @@ static void free_buffer_page(struct buffer_page *bpage)kfree (bpage); } +/* Try hard to allocate data pages without invoking the OOM killer. */ +#define alloc_cpu_data (cpu, order) ({ \ + struct buffer_data_page *__rb_data = NULL; \ + struct page *__rb_page; \ + \ + __rb_page = alloc_pages_node(cpu_to_node(cpu), \ + GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO, \ + (order)); \ + if (__rb_page) { \ + __rb_data = page_address(__rb_page); \ + rb_init_data_page(__rb_data); \ + } \ + __rb_data; \ +}) +/* * We need to fit the time_stamp delta into 27 bits. */
@@ -2013,7 +2028,7 @@ static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages)*/ for (i =0 ; i < meta->nr_subbufs; i++) { meta->buffers[i] = i; -rb_init_page (subbuf); +rb_init_data_page (subbuf); subbuf += meta->subbuf_size; } }
@@ -2184,7 +2199,7 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,if (!page) goto free_pages; bpage->page =page_address (page); -rb_init_page (bpage->page); +rb_init_data_page (bpage->page); } bpage->order = cpu_buffer->buffer->subbuf_order;
@@ -2290,7 +2305,7 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)if (!page) goto fail_free_reader; bpage->page =page_address (page); -rb_init_page (bpage->page); +rb_init_data_page (bpage->page); }INIT_LIST_HEAD (&cpu_buffer->reader_page->list);
@@ -6006,7 +6021,7 @@ static void rb_clear_buffer_page(struct buffer_page *page) {local_set (&page->write,0 );local_set (&page->entries,0 ); -rb_init_page (page->page); +rb_init_data_page (page->page); page->read =0 ; }
@@ -6392,7 +6407,6 @@ ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)struct ring_buffer_per_cpu *cpu_buffer;struct buffer_data_read_page *bpage =NULL ;unsigned long flags; -struct page *page;if (!cpumask_test_cpu (cpu, buffer->cpumask))return ERR_PTR (-ENODEV );
@@ -6414,22 +6428,16 @@ ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)arch_spin_unlock (&cpu_buffer->lock);local_irq_restore (flags); -if (bpage->data) - goto out; - - page =alloc_pages_node (cpu_to_node (cpu), -GFP_KERNEL | __GFP_NORETRY | __GFP_COMP | __GFP_ZERO, - cpu_buffer->buffer->subbuf_order); -if (!page) { -kfree (bpage); -return ERR_PTR (-ENOMEM ); +if (bpage->data) { +rb_init_data_page (bpage->data); + }else { + bpage->data =alloc_cpu_data (cpu, cpu_buffer->buffer->subbuf_order); +if (!bpage->data) { +kfree (bpage); +return ERR_PTR (-ENOMEM ); + } } - bpage->data =page_address (page); - - out: -rb_init_page (bpage->data); -return bpage; }EXPORT_SYMBOL_GPL (ring_buffer_alloc_read_page);
@@ -6638,7 +6646,7 @@ int ring_buffer_read_page(struct trace_buffer *buffer, cpu_buffer->read_bytes +=rb_page_size (reader);/* swap the pages */ -rb_init_page (bpage); +rb_init_data_page (bpage); bpage = reader->page; reader->page = data_page->data;local_set (&reader->write,0 );
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/2 ring-buffer: Fix subbuf resize race with ring_buffer_alloc_read_page()
From: Vincent Donnefort <[email protected]>
[ Upstream commit e743527c5bfdceda1095bc0a9e596e2aebb6a9c3 ]
ring_buffer_alloc_read_page() is racy with ring_buffer_subbuf_order_set, it can allocate a reader page with an outdated order. This isn't a big issue, the user can still re-allocate a new reader page and try again.
However, what is more problematic is if the value of subbuf_order changes in the middle of ring_buffer_alloc_read_page(). In that case, bpage->order might not match the actual allocated memory.
Use bpage->order for the allocation to prevent this race.
--- kernel/trace/ring_buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/ring_buffer.c +1 -1
@@ -6431,7 +6431,7 @@ ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)if (bpage->data) {rb_init_data_page (bpage->data); }else { - bpage->data =alloc_cpu_data (cpu, cpu_buffer->buffer->subbuf_order); + bpage->data =alloc_cpu_data (cpu, bpage->order);if (!bpage->data) {kfree (bpage);return ERR_PTR (-ENOMEM );
Signature · 1 line
2.53.0