Skip to content

Commit 3d9622c

Browse files
committed
tracing: Add barrier to trace_printk() buffer nesting modification
trace_printk() uses 4 buffers, one for each context (normal, softirq, irq and NMI), such that it does not need to worry about one context preempting the other. There's a nesting counter that gets incremented to figure out which buffer to use. If the context gets preempted by another context which calls trace_printk() it will increment the counter and use the next buffer, and restore the counter when it is finished. The problem is that gcc may optimize the modification of the buffer nesting counter and it may not be incremented in memory before the buffer is used. If this happens, and the context gets interrupted by another context, it could pick the same buffer and corrupt the one that is being used. Compiler barriers need to be added after the nesting variable is incremented and before it is decremented to prevent usage of the context buffers by more than one context at the same time. Cc: Andy Lutomirski <[email protected]> Cc: [email protected] Fixes: e2ace00 ("tracing: Choose static tp_printk buffer by explicit nesting count") Hat-tip-to: Peter Zijlstra <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent edb096e commit 3d9622c

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

kernel/trace/trace.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2802,11 +2802,17 @@ static char *get_trace_buf(void)
28022802
if (!buffer || buffer->nesting >= 4)
28032803
return NULL;
28042804

2805-
return &buffer->buffer[buffer->nesting++][0];
2805+
buffer->nesting++;
2806+
2807+
/* Interrupts must see nesting incremented before we use the buffer */
2808+
barrier();
2809+
return &buffer->buffer[buffer->nesting][0];
28062810
}
28072811

28082812
static void put_trace_buf(void)
28092813
{
2814+
/* Don't let the decrement of nesting leak before this */
2815+
barrier();
28102816
this_cpu_dec(trace_percpu_buffer->nesting);
28112817
}
28122818

0 commit comments

Comments
 (0)