LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] perf/ring_buffer: Fix exposing a temporarily decreased data_head.
@ 2019-05-15  0:30 Yabin Cui
  2019-05-15  6:51 ` Alexander Shishkin
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Yabin Cui @ 2019-05-15  0:30 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Alexander Shishkin, Jiri Olsa, Namhyung Kim
  Cc: linux-kernel, Yabin Cui

In perf_output_put_handle(), an IRQ/NMI can happen in below location and
write records to the same ring buffer:
	...
	local_dec_and_test(&rb->nest)
	...                          <-- an IRQ/NMI can happen here
	rb->user_page->data_head = head;
	...

In this case, a value A is written to data_head in the IRQ, then a value
B is written to data_head after the IRQ. And A > B. As a result,
data_head is temporarily decreased from A to B. And a reader may see
data_head < data_tail if it read the buffer frequently enough, which
creates unexpected behaviors.

This can be fixed by moving dec(&rb->nest) to after updating data_head,
which prevents the IRQ/NMI above from updating data_head.

Signed-off-by: Yabin Cui <yabinc@google.com>
---
 kernel/events/ring_buffer.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
index 674b35383491..0b9aefe13b04 100644
--- a/kernel/events/ring_buffer.c
+++ b/kernel/events/ring_buffer.c
@@ -54,8 +54,10 @@ static void perf_output_put_handle(struct perf_output_handle *handle)
 	 * IRQ/NMI can happen here, which means we can miss a head update.
 	 */
 
-	if (!local_dec_and_test(&rb->nest))
+	if (local_read(&rb->nest) > 1) {
+		local_dec(&rb->nest);
 		goto out;
+	}
 
 	/*
 	 * Since the mmap() consumer (userspace) can run on a different CPU:
@@ -86,6 +88,13 @@ static void perf_output_put_handle(struct perf_output_handle *handle)
 	smp_wmb(); /* B, matches C */
 	rb->user_page->data_head = head;
 
+	/*
+	 * Clear rb->nest after updating data_head. This prevents IRQ/NMI from
+	 * updating data_head before us. If that happens, we will expose a
+	 * temporarily decreased data_head.
+	 */
+	local_set(&rb->nest, 0);
+
 	/*
 	 * Now check if we missed an update -- rely on previous implied
 	 * compiler barriers to force a re-read.
-- 
2.21.0.1020.gf2820cf01a-goog


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

end of thread, other threads:[~2019-05-17 17:59 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-15  0:30 [PATCH] perf/ring_buffer: Fix exposing a temporarily decreased data_head Yabin Cui
2019-05-15  6:51 ` Alexander Shishkin
2019-05-15  9:43   ` Peter Zijlstra
2019-05-15 10:04     ` Alexander Shishkin
2019-05-15 10:51       ` Peter Zijlstra
2019-05-15 10:48 ` Peter Zijlstra
2019-05-16 18:37   ` Yabin Cui
2019-05-16 18:40 ` [PATCH v2] " Yabin Cui
2019-05-17  8:47   ` Peter Zijlstra
2019-05-17 17:59     ` [PATCH] " Yabin Cui

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).