Re: [PATCH 1/4] perf/ring_buffer: Fix exposing a temporarily decreased data_head.

From: Peter Zijlstra
Date: Fri May 17 2019 - 10:29:23 EST


On Fri, May 17, 2019 at 03:05:09PM +0200, Ingo Molnar wrote:
>
> * Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> > 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.
> >
> > Cc: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
> > Cc: Ingo Molnar <mingo@xxxxxxxxxx>
> > Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
> > Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
> > Cc: Alexander Shishkin <alexander.shishkin@xxxxxxxxxxxxxxx>
> > Fixes: ef60777c9abd ("perf: Optimize the perf_output() path by removing IRQ-disables")
> > Signed-off-by: Yabin Cui <yabinc@xxxxxxxxxx>
> > Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
> > Link: https://lkml.kernel.org/r/20190516184010.167903-1-yabinc@xxxxxxxxxx
>
> So these are missing a bunch of:
>
> From: Yabin Cui <yabinc@xxxxxxxxxx>
>
> lines, right?
>

The first. certainly, the rest, while inspired by his patch, is more
complete than what he did.