From: Frediano Ziglio Subject: Re: [PATCH] mce: Fix race condition in mctelem_xchg_head Date: Thu, 16 Jan 2014 14:07:43 +0000 Message-ID: <1389881263.1061.7.camel@hamster.uk.xensource.com> References: <1389878814.1061.1.camel@hamster.uk.xensource.com> <52D7F31E0200007800114419@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <52D7F31E0200007800114419@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Jan Beulich Cc: Liu Jinsong , Christoph Egger , xen-devel@lists.xen.org List-Id: xen-devel@lists.xenproject.org On Thu, 2014-01-16 at 13:56 +0000, Jan Beulich wrote: > >>> On 16.01.14 at 14:26, Frediano Ziglio wrote: > > The function that exchange the head is racy. > > The cmpxchg and the compare are not atomic. > > Assume two thread one (T1) inserting on committed list and another > > trying to comsume it (T2). > > T1 start inserting the element (A), set prev pointer (commit list use > > mcte_prev) then is stop after the cmpxchg succeeded. > > T2 get the list and change elements (A) and update the commit list > > head. > > T1 resume, read pointer to prev again and compare with result from > > cmpxchg which succeeded but in the meantime prev changed in memory. > > Not T1 assume the element was not inserted on the list and try to > > insert again. Now A however is in another state and should not be > > modified by T1. > > To solve the race use temporary variable for prev pointer. > > Note that compiler should not optimize the memory fetch as cmpxhg > > do a full barrier. > > This last sentence is pretty pointless, since there's an explicit > wmb() between setting old and doing the cmpxchg. (Question is > whether that wmb() actually is necessary; without a comment I > can't easily see what it is supposed to fence - surely it's not the > write to *oldp. And that's regardless of it being redundant with > the barrier embedded with cmpxchgptr().) > > But overall, while I think your analysis is correct, the description > could do with some cleanup, also spelling-wise. > Yes, sorry, I'm not native English. Suggestion accepted. The race is here if (cmpxchgptr(headp, *old, new) == *old) You do the cmpxchg (which is atomic), then you read old pointer (*old) then you compare cmpxchg result with the content of the pointer. Yes, wmb is quite useless, cmpxchg should be enough. > > static void mctelem_xchg_head(struct mctelem_ent **headp, > > - struct mctelem_ent **old, > > + struct mctelem_ent **oldp, > > struct mctelem_ent *new) > > { > > + struct mctelem_ent *old; > > + > > for (;;) { > > - *old = *headp; > > + *oldp = old = *headp; > > wmb(); > > - if (cmpxchgptr(headp, *old, new) == *old) > > + if (cmpxchgptr(headp, old, new) == old) > > break; > > } > > } > > Now that you use a temporary, it would make sense (and make > the code easier to read) if you set *oldp to old just once, after > the loop. > > Jan > No, this will create another race. After cmpxchg you must assume that the list element can be own by somebody else. Frediano