Re: [PATCH] [RFC] perf: Fix a race between ring_buffer_detach() and ring_buffer_wakeup()

Peter Zijlstra
2014-05-08T16:09:54Z

[-- Attachment #1: Type: text/plain, Size: 1864 bytes --]

On Thu, May 08, 2014 at 08:37:27AM -0700, Paul E. McKenney wrote:
> On Wed, May 07, 2014 at 08:06:21PM +0200, Peter Zijlstra wrote:
> > On Wed, May 07, 2014 at 02:35:26PM +0200, Peter Zijlstra wrote:
> > > static void ring_buffer_attach(struct perf_event *event,
> > > struct ring_buffer *rb)
> > > {
> > > + struct ring_buffer *old_rb = NULL;
> > > unsigned long flags;
> > >
> > > + if (event->rb) {
> > > + /*
> > > + * Should be impossible, we set this when removing
> > > + * event->rb_entry and wait/clear when adding event->rb_entry.
> > > + */
> > > + WARN_ON_ONCE(event->rcu_pending);
> > >
> > > + old_rb = event->rb;
> > > + event->rcu_batches = get_state_synchronize_rcu();
> > > + event->rcu_pending = 1;
> > >
> > > + spin_lock_irqsave(&rb->event_lock, flags);
> > > + list_del_rcu(&event->rb_entry);
> > > + spin_unlock_irqrestore(&rb->event_lock, flags);
> >
> > This all works a whole lot better if you make that old_rb->event_lock.
> >
> > > + }
> > >
> > > + if (event->rcu_pending && rb) {
> > > + cond_synchronize_rcu(event->rcu_batches);
>
> There is not a whole lot of code between the get_state_synchronize_rcu()
> and the cond_synchronize_rcu(), so I would expect this to do a
> synchronize_rcu() almost all the time. Or am I missing something here?

From the Changelog:

2) an event that has a buffer attached, the buffer is destroyed
(munmap) and then the event is attached to a new/different buffer
using PERF_EVENT_IOC_SET_OUTPUT.

This case is more complex because the buffer destruction does:
ring_buffer_attach(.rb = NULL)
followed by the ioctl() doing:
ring_buffer_attach(.rb = foo);

and we still need to observe the grace period between these two
calls due to us reusing the event->rb_entry list_head.

[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]