LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Yabin Cui <yabinc@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@redhat.com>, Namhyung Kim <namhyung@kernel.org>
Cc: linux-kernel@vger.kernel.org, Yabin Cui <yabinc@google.com>
Subject: [PATCH v2] perf/ring_buffer: Fix exposing a temporarily decreased data_head.
Date: Thu, 16 May 2019 11:40:10 -0700 [thread overview]
Message-ID: <20190516184010.167903-1-yabinc@google.com> (raw)
In-Reply-To: <20190515003059.23920-1-yabinc@google.com>
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>
---
v1 -> v2: change rb->nest from local_t to unsigned int, and add barriers.
---
kernel/events/internal.h | 2 +-
kernel/events/ring_buffer.c | 24 ++++++++++++++++++------
2 files changed, 19 insertions(+), 7 deletions(-)
diff --git a/kernel/events/internal.h b/kernel/events/internal.h
index 79c47076700a..0a8c003b9bcf 100644
--- a/kernel/events/internal.h
+++ b/kernel/events/internal.h
@@ -24,7 +24,7 @@ struct ring_buffer {
atomic_t poll; /* POLL_ for wakeups */
local_t head; /* write position */
- local_t nest; /* nested writers */
+ unsigned int nest; /* nested writers */
local_t events; /* event limit */
local_t wakeup; /* wakeup stamp */
local_t lost; /* nr records lost */
diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
index 674b35383491..c677beb01fb1 100644
--- a/kernel/events/ring_buffer.c
+++ b/kernel/events/ring_buffer.c
@@ -38,7 +38,8 @@ static void perf_output_get_handle(struct perf_output_handle *handle)
struct ring_buffer *rb = handle->rb;
preempt_disable();
- local_inc(&rb->nest);
+ rb->nest++;
+ barrier();
handle->wakeup = local_read(&rb->wakeup);
}
@@ -54,8 +55,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 (rb->nest > 1) {
+ rb->nest--;
goto out;
+ }
/*
* Since the mmap() consumer (userspace) can run on a different CPU:
@@ -84,14 +87,23 @@ static void perf_output_put_handle(struct perf_output_handle *handle)
* See perf_output_begin().
*/
smp_wmb(); /* B, matches C */
- rb->user_page->data_head = head;
+ WRITE_ONCE(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.
+ */
+ WRITE_ONCE(rb->nest, 0);
/*
- * Now check if we missed an update -- rely on previous implied
- * compiler barriers to force a re-read.
+ * Now check if we missed an update -- use barrier() to force a
+ * re-read.
*/
+ barrier();
if (unlikely(head != local_read(&rb->head))) {
- local_inc(&rb->nest);
+ rb->nest++;
+ barrier();
goto again;
}
--
2.21.0.1020.gf2820cf01a-goog
next prev parent reply other threads:[~2019-05-16 18:40 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-15 0:30 [PATCH] " 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 ` Yabin Cui [this message]
2019-05-17 8:47 ` [PATCH v2] " Peter Zijlstra
2019-05-17 17:59 ` [PATCH] " Yabin Cui
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190516184010.167903-1-yabinc@google.com \
--to=yabinc@google.com \
--cc=acme@kernel.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=jolsa@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--subject='Re: [PATCH v2] perf/ring_buffer: Fix exposing a temporarily decreased data_head.' \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
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).