ring-buffer: Take cpus_read_lock() when changing the subbuf order
Subsystems (3)
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.
--- kernel/trace/ring_buffer.c | 6 ++++++ 1 file changed, 6 insertions(+)
kernel/trace/ring_buffer.c +6 -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. +*/ +guard (cpus_read_lock)(); +/* prevent another thread from changing buffer sizes */ guard (mutex)(&buffer->mutex);
Signature · 1 line
2.55.0
Vincent Donnefort
maintainerQuoted 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->cpumaskafter the allocation loop, and the install loop then uses its emptynew_pageslist, corrupting the buffer.Take
cpus_read_lock()asring_buffer_resize()does.Fixes f9b94daa542a ("ring-buffer: Set new size of the ring buffer sub page")Signed-off-by David Carlier <[email protected]>--- kernel/trace/ring_buffer.c | 6 ++++++ 1 file changed, 6 insertions(+)kernel/trace/ring_buffer.c +0
-0
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
PATCH v2 1/1 ring-buffer: Take cpus_read_lock() when changing the subbuf order
In reply to David CarlierA 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.
---
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
@@ -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
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 tobuffer->cpumask. If that happens whilering_buffer_subbuf_order_set()is running, the CPU shows up after the allocation loop and the install loop walks it with an emptynew_pageslist, corrupting the buffer.Take
cpus_read_lock()asring_buffer_resize()does.Fixes f9b94daa542a ("ring-buffer: Set new size of the ring buffer sub page")Signed-off-by David Carlier <[email protected]>
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@@ -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