]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/4.9.52/tracing-add-barrier-to-trace_printk-buffer-nesting-modification.patch
4.9-stable patches
[thirdparty/kernel/stable-queue.git] / releases / 4.9.52 / tracing-add-barrier-to-trace_printk-buffer-nesting-modification.patch
1 From 3d9622c12c8873911f4cc0ccdabd0362c2fca06b Mon Sep 17 00:00:00 2001
2 From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
3 Date: Tue, 5 Sep 2017 11:32:01 -0400
4 Subject: tracing: Add barrier to trace_printk() buffer nesting modification
5
6 From: Steven Rostedt (VMware) <rostedt@goodmis.org>
7
8 commit 3d9622c12c8873911f4cc0ccdabd0362c2fca06b upstream.
9
10 trace_printk() uses 4 buffers, one for each context (normal, softirq, irq
11 and NMI), such that it does not need to worry about one context preempting
12 the other. There's a nesting counter that gets incremented to figure out
13 which buffer to use. If the context gets preempted by another context which
14 calls trace_printk() it will increment the counter and use the next buffer,
15 and restore the counter when it is finished.
16
17 The problem is that gcc may optimize the modification of the buffer nesting
18 counter and it may not be incremented in memory before the buffer is used.
19 If this happens, and the context gets interrupted by another context, it
20 could pick the same buffer and corrupt the one that is being used.
21
22 Compiler barriers need to be added after the nesting variable is incremented
23 and before it is decremented to prevent usage of the context buffers by more
24 than one context at the same time.
25
26 Cc: Andy Lutomirski <luto@kernel.org>
27 Fixes: e2ace00117 ("tracing: Choose static tp_printk buffer by explicit nesting count")
28 Hat-tip-to: Peter Zijlstra <peterz@infradead.org>
29 Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
30 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
31
32 ---
33 kernel/trace/trace.c | 8 +++++++-
34 1 file changed, 7 insertions(+), 1 deletion(-)
35
36 --- a/kernel/trace/trace.c
37 +++ b/kernel/trace/trace.c
38 @@ -2369,11 +2369,17 @@ static char *get_trace_buf(void)
39 if (!buffer || buffer->nesting >= 4)
40 return NULL;
41
42 - return &buffer->buffer[buffer->nesting++][0];
43 + buffer->nesting++;
44 +
45 + /* Interrupts must see nesting incremented before we use the buffer */
46 + barrier();
47 + return &buffer->buffer[buffer->nesting][0];
48 }
49
50 static void put_trace_buf(void)
51 {
52 + /* Don't let the decrement of nesting leak before this */
53 + barrier();
54 this_cpu_dec(trace_percpu_buffer->nesting);
55 }
56