mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ring-buffer: Take cpus_read_lock() when changing the subbuf order
@ 2026-09-14 18:28 David Carlier
  2026-09-15  7:16 ` Vincent Donnefort
  2026-09-16 19:26 ` [PATCH v2] " David Carlier
  0 siblings, 2 replies; 4+ messages in thread
From: David Carlier @ 2026-09-14 18:28 UTC (permalink / raw)
  To: rostedt, mhiramat
  Cc: mathieu.desnoyers, vdonnefort, linux-trace-kernel, linux-kernel,
	David Carlier, stable

A CPU coming online during ring_buffer_subbuf_order_set() can be added
to buffer->cpumask after the allocation loop, and the install loop then
uses its empty new_pages list, corrupting the buffer.

Take cpus_read_lock() as ring_buffer_resize() does.

Fixes: f9b94daa542a ("ring-buffer: Set new size of the ring buffer sub page")
Cc: stable@vger.kernel.org
Signed-off-by: David Carlier <devnexen@gmail.com>
---
 kernel/trace/ring_buffer.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 04bb94c29f58..445931bc9ae0 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -7464,6 +7464,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);
 
-- 
2.55.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ring-buffer: Take cpus_read_lock() when changing the subbuf order
  2026-09-14 18:28 [PATCH] ring-buffer: Take cpus_read_lock() when changing the subbuf order David Carlier
@ 2026-09-15  7:16 ` Vincent Donnefort
  2026-09-16 19:26 ` [PATCH v2] " David Carlier
  1 sibling, 0 replies; 4+ messages in thread
From: Vincent Donnefort @ 2026-09-15  7:16 UTC (permalink / raw)
  To: David Carlier
  Cc: rostedt, mhiramat, mathieu.desnoyers, linux-trace-kernel,
	linux-kernel, stable

On Mon, Sep 14, 2026 at 07:28:43PM +0100, David Carlier wrote:
> A CPU coming online during ring_buffer_subbuf_order_set() can be added

nit: a "new" CPU or "A CPU we've never seen before"?

Offlined CPUs aren't removed from ->cpumask.

> to buffer->cpumask after the allocation loop, and the install loop then
> uses its empty new_pages list, corrupting the buffer.
> 
> Take cpus_read_lock() as ring_buffer_resize() does.
> 
> Fixes: f9b94daa542a ("ring-buffer: Set new size of the ring buffer sub page")
> Cc: stable@vger.kernel.org
> Signed-off-by: David Carlier <devnexen@gmail.com>
> ---
>  kernel/trace/ring_buffer.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
> index 04bb94c29f58..445931bc9ae0 100644
> --- a/kernel/trace/ring_buffer.c
> +++ b/kernel/trace/ring_buffer.c
> @@ -7464,6 +7464,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.
> +	 */

Can we drop this comment? cpus_read_lock is self-explanatory.

> +	guard(cpus_read_lock)();
> +
>  	/* prevent another thread from changing buffer sizes */
>  	guard(mutex)(&buffer->mutex);
>  
> -- 
> 2.55.0
> 

-- 
Vincent

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] ring-buffer: Take cpus_read_lock() when changing the subbuf order
  2026-09-14 18:28 [PATCH] ring-buffer: Take cpus_read_lock() when changing the subbuf order David Carlier
  2026-09-15  7:16 ` Vincent Donnefort
@ 2026-09-16 19:26 ` David Carlier
  2026-09-17  6:55   ` Vincent Donnefort
  1 sibling, 1 reply; 4+ messages in thread
From: David Carlier @ 2026-09-16 19:26 UTC (permalink / raw)
  To: rostedt, mhiramat
  Cc: vdonnefort, mathieu.desnoyers, linux-trace-kernel, linux-kernel,
	stable, David Carlier

A CPU coming online for the first time gets its per CPU buffer allocated
by trace_rb_cpu_prepare(), which then adds it to buffer->cpumask. If that
happens while ring_buffer_subbuf_order_set() is running, the CPU shows up
after the allocation loop and the install loop walks it with an empty
new_pages list, corrupting the buffer.

Take cpus_read_lock() as ring_buffer_resize() does.

Fixes: f9b94daa542a ("ring-buffer: Set new size of the ring buffer sub page")
Cc: stable@vger.kernel.org
Signed-off-by: David Carlier <devnexen@gmail.com>
---

Notes:
    v2:
     - Reword the changelog: offlined CPUs stay in ->cpumask, so only a CPU
       coming online for the first time can appear mid-flight (Vincent)
     - Drop the comment above guard(cpus_read_lock)() (Vincent)
    
    v1: https://lore.kernel.org/linux-trace-kernel/20260914182850.21449-1-devnexen@gmail.com/

 kernel/trace/ring_buffer.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 04bb94c29f58..e78fb4bb2195 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -7464,6 +7464,8 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
 	if (psize > RB_WRITE_MASK + 1)
 		return -EINVAL;
 
+	guard(cpus_read_lock)();
+
 	/* prevent another thread from changing buffer sizes */
 	guard(mutex)(&buffer->mutex);
 
-- 
2.55.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] ring-buffer: Take cpus_read_lock() when changing the subbuf order
  2026-09-16 19:26 ` [PATCH v2] " David Carlier
@ 2026-09-17  6:55   ` Vincent Donnefort
  0 siblings, 0 replies; 4+ messages in thread
From: Vincent Donnefort @ 2026-09-17  6:55 UTC (permalink / raw)
  To: David Carlier
  Cc: rostedt, mhiramat, mathieu.desnoyers, linux-trace-kernel,
	linux-kernel, stable

On Wed, Sep 16, 2026 at 08:26:28PM +0100, David Carlier wrote:
> A CPU coming online for the first time gets its per CPU buffer allocated
> by trace_rb_cpu_prepare(), which then adds it to buffer->cpumask. If that
> happens while ring_buffer_subbuf_order_set() is running, the CPU shows up
> after the allocation loop and the install loop walks it with an empty
> new_pages list, corrupting the buffer.
> 
> Take cpus_read_lock() as ring_buffer_resize() does.
> 
> Fixes: f9b94daa542a ("ring-buffer: Set new size of the ring buffer sub page")
> Cc: stable@vger.kernel.org
> Signed-off-by: David Carlier <devnexen@gmail.com>

It is usually good practice to not link the different versions of patches
together.

Beside,

Reviewed-by: Vincent Donnefort <vdonnefort@google.com>

> ---
> 
> Notes:
>     v2:
>      - Reword the changelog: offlined CPUs stay in ->cpumask, so only a CPU
>        coming online for the first time can appear mid-flight (Vincent)
>      - Drop the comment above guard(cpus_read_lock)() (Vincent)
>     
>     v1: https://lore.kernel.org/linux-trace-kernel/20260914182850.21449-1-devnexen@gmail.com/
> 
>  kernel/trace/ring_buffer.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
> index 04bb94c29f58..e78fb4bb2195 100644
> --- a/kernel/trace/ring_buffer.c
> +++ b/kernel/trace/ring_buffer.c
> @@ -7464,6 +7464,8 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order)
>  	if (psize > RB_WRITE_MASK + 1)
>  		return -EINVAL;
>  
> +	guard(cpus_read_lock)();
> +
>  	/* prevent another thread from changing buffer sizes */
>  	guard(mutex)(&buffer->mutex);
>  
> -- 
> 2.55.0
> 

-- 
Vincent

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-17  6:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-14 18:28 [PATCH] ring-buffer: Take cpus_read_lock() when changing the subbuf order David Carlier
2026-09-15  7:16 ` Vincent Donnefort
2026-09-16 19:26 ` [PATCH v2] " David Carlier
2026-09-17  6:55   ` Vincent Donnefort

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®