public inbox for linux-rdma@vger.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
To: "Hefty, Sean" <sean.hefty-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Cc: "linux-rdma
	(linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org)"
	<linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: Re: identify the race condition in this code and win the respect of linux-rdma developers!
Date: Fri, 16 Sep 2011 09:59:55 -0600	[thread overview]
Message-ID: <20110916155955.GA18548@obsidianresearch.com> (raw)
In-Reply-To: <1828884A29C6694DAF28B7E6B8A8237316E5B320-P5GAC/sN6hmkrb+BlOpmy7fspsVTdybXVpNB7YpNyf8@public.gmane.org>

On Fri, Sep 16, 2011 at 02:51:04PM +0000, Hefty, Sean wrote:
> > > Case 1:
> > > 	ret = ibv_poll_cq(id->recv_cq, 1, wc);
> > > 	ret = ibv_req_notify_cq(id->recv_cq, 0);
> > >
> > > 	while (!(ret = ibv_poll_cq(id->recv_cq, 1, wc))) {
> > > 		ret = ibv_get_cq_event(id->recv_cq_channel, &cq, &context);
> > > 		ibv_ack_cq_events(id->recv_cq, 1);
> > > 	}
> > 
> > If ibv_get_cq_event returns here, because the CQ notify triggers but
> > poll_cq goes not return a WC then you go back into get_cq_event
> > without requesting notification.
> 
> I understand this, but I still don't quite see how the situation fully occurs.
> 
> > I'm pretty sure ibv_req_notify_cq does nothing if there is already a
> > notification pending.
> 
> This last statement might be it, but I couldn't see that in the
> code.  I was under the assumption that once an event occurred, the
> CQ would be 'un-armed'.  ibv_req_notify_cq() would rearm it.  In the
> above code then, ibv_get_cq_event() would pull the old event, but
> then a new one should eventually show up with a corresponding
> completion.  The flow would be:

As I see it the problem flow would be this:

poll
ibv_req_notify_cq
// HCA Write CQ Entry
// Trigger EVENT
poll -> return CQ

poll
ibv_req_notify_cq // does nothing, event is already triggered
poll
ibv_get_cq_event // returns immediately due to prior Trigger event
poll
ibv_get_cq_event // blocks because the CQ was never rearmed.

Which would happen easily and frequently without having to invoke any
spooky infrequent racing.

poll and cq_events are totally independent, I belive the implementation
of CQ events is like:

enum {DISABLED,ARMED,TRIGGERED} flag;

And the only sane, race free implementation of req_notify must thus be

if (flag == DISABLED) flag = ARMED

And for CQ entry add:
if (flag == ARMED) flag = TRIGGERED

And for get_cq_event:

if (flag == TRIGGERED) {flag = DISABLED; return;}

What you are imagining is that ibv_req_notify_cq does this:

if (flag == ARMED || flag == TRIGGERED) flag = ARMED

which is not a good choice because it will destroy a triggered state
and that creates all sorts of bad race conditions, again because of
the independence of poll and cq_event.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2011-09-16 15:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-15 23:58 identify the race condition in this code and win the respect of linux-rdma developers! Hefty, Sean
     [not found] ` <1828884A29C6694DAF28B7E6B8A8237316E5B2B1-P5GAC/sN6hmkrb+BlOpmy7fspsVTdybXVpNB7YpNyf8@public.gmane.org>
2011-09-16  0:50   ` Jason Gunthorpe
     [not found]     ` <20110916005008.GC6020-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2011-09-16 14:51       ` Hefty, Sean
     [not found]         ` <1828884A29C6694DAF28B7E6B8A8237316E5B320-P5GAC/sN6hmkrb+BlOpmy7fspsVTdybXVpNB7YpNyf8@public.gmane.org>
2011-09-16 15:59           ` Jason Gunthorpe [this message]
     [not found]             ` <20110916155955.GA18548-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2011-09-16 16:57               ` Hefty, Sean
     [not found]                 ` <1828884A29C6694DAF28B7E6B8A8237316E5B3B8-P5GAC/sN6hmkrb+BlOpmy7fspsVTdybXVpNB7YpNyf8@public.gmane.org>
2011-09-16 17:21                   ` Hefty, Sean
2011-09-16 19:36               ` Hefty, Sean
     [not found]                 ` <1828884A29C6694DAF28B7E6B8A8237316E5B55E-P5GAC/sN6hmkrb+BlOpmy7fspsVTdybXVpNB7YpNyf8@public.gmane.org>
2011-09-16 19:57                   ` Jason Gunthorpe

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=20110916155955.GA18548@obsidianresearch.com \
    --to=jgunthorpe-epgobjl8dl3ta4ec/59zmfatqe2ktcn/@public.gmane.org \
    --cc=linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=sean.hefty-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox