Skip to contentSkip to conversation

ring-buffer: Check resize_disabled before publishing the new subbuf order

PATCH In review David Carlier 4 participants 6 messages Sep 12 – Sep 14

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ring-buffer: Check resize_disabled before publishing the new subbuf order

ring_buffer_subbuf_order_set() stores the new order and only then walks the CPUs, returning -EBUSY if any of them has resizing disabled. A user mapped buffer has resizing disabled, and __rb_map_vma() reads buffer->subbuf_order without buffer->mutex, so an mmap of an already mapped CPU racing the failing order change sizes the mapping with the new order and inserts pages past the sub-buffer into the VMA.

Check the CPUs before storing the new order.

Fixes 117c39200d9d ("ring-buffer: Introducing ring-buffer mapping functions")
---
 kernel/trace/ring_buffer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/trace/ring_buffer.c +8 -0
index 9c03a555a6ba..d7e5e4620d09 100644
@@ -7474,6 +7474,14 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
 
 	old_capacity = rb_subbuf_capacity(buffer);
 
+	/* The mmap fast path reads subbuf_order without buffer->mutex. */
+	for_each_buffer_cpu(buffer, cpu) {
+		if (!cpumask_test_cpu(cpu, buffer->cpumask))
+			continue;
+		if (atomic_read(&buffer->buffers[cpu]->resize_disabled))
+			return -EBUSY;
+	}
+
 	atomic_inc(&buffer->record_disabled);
 
 	/* Make sure all commits have finished */
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Missing cpus_read_lock() allows concurrent CPU hotplug events to crash…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing cpus_read_lock() allows concurrent CPU hotplug events to crash ring_buffer_subbuf_order_set() via uninitialized list dereference or NULL pointer dereference. --

commit d554518f86e022de0069fc76033b9d71e9046148
Author: David Carlier <[email protected]>

ring-buffer: Check resize_disabled before publishing the new subbuf order

This commit checks if resizing is disabled on any CPU before applying the new sub-buffer order. This aims to prevent a race condition with mmap fast paths mapping already-mapped CPUs with the new order.

Quoted 6 lines
kernel/trace/ring_buffer.c +0 -0
index 9c03a555a6ba2..d7e5e4620d096 100644
@@ -7474,6 +7474,14 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
 

kernel/trace/ring_buffer.c int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order) · line 7484

Quoted 10 lines
kernel/trace/ring_buffer.c +8 -0
 	old_capacity = rb_subbuf_capacity(buffer);
 
+	/* The mmap fast path reads subbuf_order without buffer->mutex. */
+	for_each_buffer_cpu(buffer, cpu) {
+		if (!cpumask_test_cpu(cpu, buffer->cpumask))
+			continue;
+		if (atomic_read(&buffer->buffers[cpu]->resize_disabled))
+			return -EBUSY;
+	}
+

[Severity: High]
Can this new loop race with concurrent CPU hotplug events and trigger a NULL pointer dereference?

Unlike ring_buffer_resize(), ring_buffer_subbuf_order_set() doesn't appear to take cpus_read_lock(). Meanwhile, trace_rb_cpu_prepare() can update buffer->cpumask without holding buffer->mutex.

On weakly ordered architectures, could the lack of an smp_rmb() here cause the reader to observe a newly added CPU in buffer->cpumask but load a NULL buffer->buffers[cpu]?

Additionally, without cpus_read_lock(), if a CPU is added to the cpumask between the allocation loop and the final page update loop later in this function, new_pages will be an empty list head:

ring_buffer_subbuf_order_set() {
    ...
    /* One page was allocated for the reader page */
    cpu_buffer->reader_page = list_entry(cpu_buffer->new_pages.next,
                                         struct buffer_page, list);
    list_del_init(&cpu_buffer->reader_page->list);
    ...
}

Could this calculate a fake pointer overlapping the cpu_buffer struct and corrupt memory when modified?

Quoted 3 lines
kernel/trace/ring_buffer.c +0 -0
 	atomic_inc(&buffer->record_disabled);
 
 	/* Make sure all commits have finished */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

The first point does not hold. trace_rb_cpu_prepare() allocates buffer->buffers[cpu] and IPIs via rb_cpu_sync() before setting the cpumask bit, so observing the bit implies observing the pointer.

owever, The second is real but predates this patch:
ring_buffer_subbuf_order_set()
never took cpus_read_lock(), we can fix as follow :

--- a/kernel/trace/ring_buffer.c (/home/dcarlier/Contribs/linux-trace/kernel/trace/ring_buffer.c) +++ b/kernel/trace/ring_buffer.c
@@ -7465,6 +7465,12 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
if (psize > RB_WRITE_MASK + 1)
return -EINVAL;

+ /*

+      * Keep CPUs from coming online while changing the order to
+      * synchronize with new per CPU buffers being created.
+      */
+     guard(cpus_read_lock)();
+
      /* prevent another thread from changing buffer sizes */
      guard(mutex)(&buffer->mutex);

Cheers.

Quoted 67 lines · On Sat, 12 Sept 2026 at 11:56, <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing cpus_read_lock() allows concurrent CPU hotplug events to crash ring_buffer_subbuf_order_set() via uninitialized list dereference or NULL pointer dereference. --

commit d554518f86e022de0069fc76033b9d71e9046148
Author: David Carlier <[email protected]>

ring-buffer: Check resize_disabled before publishing the new subbuf order

This commit checks if resizing is disabled on any CPU before applying the new sub-buffer order. This aims to prevent a race condition with mmap fast paths mapping already-mapped CPUs with the new order.

Quoted 6 lines
kernel/trace/ring_buffer.c +0 -0
index 9c03a555a6ba2..d7e5e4620d096 100644
@@ -7474,6 +7474,14 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
 

kernel/trace/ring_buffer.c int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order) · line 7484

Quoted 10 lines
kernel/trace/ring_buffer.c +8 -0
      old_capacity = rb_subbuf_capacity(buffer);
 
+     /* The mmap fast path reads subbuf_order without buffer->mutex. */
+     for_each_buffer_cpu(buffer, cpu) {
+             if (!cpumask_test_cpu(cpu, buffer->cpumask))
+                     continue;
+             if (atomic_read(&buffer->buffers[cpu]->resize_disabled))
+                     return -EBUSY;
+     }
+

[Severity: High]
Can this new loop race with concurrent CPU hotplug events and trigger a NULL pointer dereference?

Unlike ring_buffer_resize(), ring_buffer_subbuf_order_set() doesn't appear to take cpus_read_lock(). Meanwhile, trace_rb_cpu_prepare() can update buffer->cpumask without holding buffer->mutex.

On weakly ordered architectures, could the lack of an smp_rmb() here cause the reader to observe a newly added CPU in buffer->cpumask but load a NULL buffer->buffers[cpu]?

Additionally, without cpus_read_lock(), if a CPU is added to the cpumask between the allocation loop and the final page update loop later in this function, new_pages will be an empty list head:

ring_buffer_subbuf_order_set() {
    ...
    /* One page was allocated for the reader page */
    cpu_buffer->reader_page = list_entry(cpu_buffer->new_pages.next,
                                         struct buffer_page, list);
    list_del_init(&cpu_buffer->reader_page->list);
    ...
}

Could this calculate a fake pointer overlapping the cpu_buffer struct and corrupt memory when modified?

kernel/trace/ring_buffer.c int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order) · line 7487

Quoted 3 lines
kernel/trace/ring_buffer.c +0 -0
      atomic_inc(&buffer->record_disabled);
 
      /* Make sure all commits have finished */

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

#3 lore raw
In reply to David Carlier
Quoted 38 lines · David Carlier

ring_buffer_subbuf_order_set() stores the new order and only then walks the CPUs, returning -EBUSY if any of them has resizing disabled. A user mapped buffer has resizing disabled, and __rb_map_vma() reads buffer->subbuf_order without buffer->mutex, so an mmap of an already mapped CPU racing the failing order change sizes the mapping with the new order and inserts pages past the sub-buffer into the VMA.

Check the CPUs before storing the new order.

Fixes 117c39200d9d ("ring-buffer: Introducing ring-buffer mapping functions")
---
 kernel/trace/ring_buffer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/trace/ring_buffer.c +8 -0
index 9c03a555a6ba..d7e5e4620d09 100644
@@ -7474,6 +7474,14 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
 
 	old_capacity = rb_subbuf_capacity(buffer);
 
+	/* The mmap fast path reads subbuf_order without buffer->mutex. */
+	for_each_buffer_cpu(buffer, cpu) {
+		if (!cpumask_test_cpu(cpu, buffer->cpumask))
+			continue;
+		if (atomic_read(&buffer->buffers[cpu]->resize_disabled))
+			return -EBUSY;
+	}
+
 	atomic_inc(&buffer->record_disabled);
 
 	/* Make sure all commits have finished */
Signature · 1 line
2.55.0

I do not think this is correct.

__rb_map_vma() reads subbuf_order without the the buffer lock __only__ if it is already ->user_mapped, which means it has already the resized disabled.

During the first setup, it takes buffer->mutex.

Signature · 1 line
Vincent
#4 lore raw
Quoted 42 lines · Vincent Donnefort
Quoted 38 lines · David Carlier

ring_buffer_subbuf_order_set() stores the new order and only then walks the CPUs, returning -EBUSY if any of them has resizing disabled. A user mapped buffer has resizing disabled, and __rb_map_vma() reads buffer->subbuf_order without buffer->mutex, so an mmap of an already mapped CPU racing the failing order change sizes the mapping with the new order and inserts pages past the sub-buffer into the VMA.

Check the CPUs before storing the new order.

Fixes 117c39200d9d ("ring-buffer: Introducing ring-buffer mapping functions")
---
 kernel/trace/ring_buffer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/trace/ring_buffer.c +8 -0
index 9c03a555a6ba..d7e5e4620d09 100644
@@ -7474,6 +7474,14 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
 
 	old_capacity = rb_subbuf_capacity(buffer);
 
+	/* The mmap fast path reads subbuf_order without buffer->mutex. */
+	for_each_buffer_cpu(buffer, cpu) {
+		if (!cpumask_test_cpu(cpu, buffer->cpumask))
+			continue;
+		if (atomic_read(&buffer->buffers[cpu]->resize_disabled))
+			return -EBUSY;
+	}
+
 	atomic_inc(&buffer->record_disabled);
 
 	/* Make sure all commits have finished */
Signature · 1 line
2.55.0

I do not think this is correct.

Quoted 7 lines

__rb_map_vma() reads subbuf_order without the the buffer lock __only__ if it is already ->user_mapped, which means it has already the resized disabled.

During the first setup, it takes buffer->mutex.

Signature · 1 line
Vincent

Ha my bad, that function also modifies the subbuf_order __before__ checking the resize_disabled.

Could we also clean the later resize_disabled check in the following loop?

Signature · 1 line
Vincent
maintainer
#5 lore raw
Quoted 4 lines · On Sun, 13 Sep 2026 21:41:14 +0100 Vincent Donnefort <[email protected]>…

Ha my bad, that function also modifies the subbuf_order __before__ checking the resize_disabled.

Could we also clean the later resize_disabled check in the following loop?

FYI, I already pulled this patch and it's in 7.3-rc3. It looked fine to me as I was just kicking off one of my long test sessions, so I threw it in. Perhaps I should have waited to this week. :-/

Any update needs to be done on top of this patch.

-- Steve