Skip to contentSkip to conversation

ring-buffer: Take cpus_read_lock() when changing the subbuf order

PATCH Superseded David Carlier 2 participants 4 messages Sep 14 – Sep 17

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

PATCH 1/1 ring-buffer: Take cpus_read_lock() when changing the subbuf order

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")
---
 kernel/trace/ring_buffer.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/trace/ring_buffer.c +6 -0
index 04bb94c29f58..445931bc9ae0 100644
@@ -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);
 
Signature · 1 line
2.55.0
#1 lore raw
Quoted 1 line · David Carlier

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.

Quoted 17 lines

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")
---
 kernel/trace/ring_buffer.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/trace/ring_buffer.c +0 -0
index 04bb94c29f58..445931bc9ae0 100644

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

Quoted 7 lines
kernel/trace/ring_buffer.c +4 -0
@@ -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.

Quoted 8 lines
kernel/trace/ring_buffer.c +2 -0
+	guard(cpus_read_lock)();
+
 	/* prevent another thread from changing buffer sizes */
 	guard(mutex)(&buffer->mutex);
 
Signature · 1 line
2.55.0
Signature · 1 line
Vincent
#2 lore raw

PATCH v2 1/1 ring-buffer: Take cpus_read_lock() when changing the subbuf order

In reply to 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")

---

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/[email protected]/

 kernel/trace/ring_buffer.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/trace/ring_buffer.c +2 -0
index 04bb94c29f58..e78fb4bb2195 100644
@@ -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);
 
Signature · 1 line
2.55.0
maintainer Reviewed-by on v2 1/1
#3 lore raw
Quoted 11 lines · 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")

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

Beside,

Quoted 29 lines

---

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/[email protected]/

 kernel/trace/ring_buffer.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/trace/ring_buffer.c +2 -0
index 04bb94c29f58..e78fb4bb2195 100644
@@ -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);
 
Signature · 1 line
2.55.0
Signature · 1 line
Vincent