From: Tim Deegan Subject: Re: [PATCHv4 3/5] xen: use ticket locks for spin locks Date: Thu, 14 May 2015 21:55:01 +0100 Message-ID: <20150514205501.GA32424@deinos.phlegethon.org> References: <1431355025-20468-1-git-send-email-david.vrabel@citrix.com> <1431355025-20468-4-git-send-email-david.vrabel@citrix.com> <20150514103621.GE81528@deinos.phlegethon.org> <55550E1F02000078000CF476@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Yt09s-0002u6-Qd for xen-devel@lists.xenproject.org; Thu, 14 May 2015 20:55:08 +0000 Content-Disposition: inline In-Reply-To: <55550E1F02000078000CF476@mail.emea.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: keir@xen.org, ian.campbell@citrix.com, andrew.cooper3@citrix.com, jennifer.herbert@citrix.com, david.vrabel@citrix.com, xen-devel@lists.xenproject.org List-Id: xen-devel@lists.xenproject.org At 21:05 +0100 on 14 May (1431637535), Jan Beulich wrote: > >>> Tim Deegan 05/14/15 12:36 PM >>> > >At 15:37 +0100 on 11 May (1431358623), David Vrabel wrote: > >> void _spin_barrier(spinlock_t *lock) > >> { > >> + spinlock_tickets_t sample; > >> #ifdef LOCK_PROFILE > >> s_time_t block = NOW(); > >> - u64 loop = 0; > >> +#endif > >> > >> check_barrier(&lock->debug); > >> - do { smp_mb(); loop++;} while ( _raw_spin_is_locked(&lock->raw) ); > >> - if ((loop > 1) && lock->profile) > >> + smp_mb(); > >> + sample = observe_lock(&lock->tickets); > >> + if ( sample.head != sample.tail ) > >> { > >> - lock->profile->time_block += NOW() - block; > >> - lock->profile->block_cnt++; > >> - } > >> -#else > >> - check_barrier(&lock->debug); > >> - do { smp_mb(); } while ( _raw_spin_is_locked(&lock->raw) ); > >> + while ( observe_head(&lock->tickets) != sample.tail ) > > > >This test should be "observe_head(&lock->tickets) == sample.head", > >i.e. wait until the thread that held the lock has released it. > >Checking for it to reach the tail is unnecessary (other threads that > >were queueing for the lock at the sample time don't matter) and > >dangerous (on a contended lock head might pass sample.tail without us > >happening to observe it being == ). > > The observation of there being a problem is correct, but the suggested solution > doesn't seem to be. The new code being > > if ( sample.head != sample.tail ) > { > while ( observe_head(&lock->tickets) == sample.tail ) > cpu_relax(); > > means that if head didn't change between the full sample and the head sample > we'd end the loop right away, which can't be right. We really need to wait for > head to reach or pass the sampled tail. I think you misread what I asked for. We wait until the observed head doesn't match the sampled _head_, i.e. for whoever had the lock when we sampled it to realease it: if ( sample.head != sample.tail ) { while ( observe_head(&lock->tickets) == sample.head ) cpu_relax(); Cheers, Tim.