From: "Jan Beulich" Subject: Re: [PATCHv4 5/8] xen: use ticket locks for spin locks Date: Fri, 08 May 2015 10:36:22 +0100 Message-ID: <554C9FB60200007800078181@mail.emea.novell.com> References: <1430408002-30666-1-git-send-email-david.vrabel@citrix.com> <1430408002-30666-6-git-send-email-david.vrabel@citrix.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 1Yqehj-0007cp-Kg for xen-devel@lists.xenproject.org; Fri, 08 May 2015 09:36:23 +0000 In-Reply-To: <1430408002-30666-6-git-send-email-david.vrabel@citrix.com> Content-Disposition: inline List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: David Vrabel Cc: Keir Fraser , Ian Campbell , Andrew Cooper , Tim Deegan , Jennifer Herbert , xen-devel@lists.xenproject.org List-Id: xen-devel@lists.xenproject.org >>> On 30.04.15 at 17:33, wrote: > int _spin_trylock(spinlock_t *lock) > { > + spinlock_tickets_t old, new; > + > check_lock(&lock->debug); > - if ( !_raw_spin_trylock(&lock->raw) ) > + old = observe_lock(&lock->tickets); > + if ( old.head != old.tail ) > + return 0; > + new = old; > + new.tail++; > + if ( cmpxchg(&lock->tickets.head_tail, old.head_tail, new.head_tail) > + != old.head_tail ) > return 0; Maybe worth a comment here that arch_lock_acquire_barrier() is not needed (implicitly done by cmpxchg())? > @@ -213,27 +213,32 @@ int _spin_trylock(spinlock_t *lock) > > 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) > + 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 ) > + { > +#ifdef LOCK_PROFILE > + if ( lock->profile ) > + { > + lock->profile->time_block += NOW() - block; > + lock->profile->block_cnt++; > + } If you want to keep this inside the loop (which seems a bad idea) you'd need to update "block" in each iteration and increment ->block_cnt only the first time through. > #endif Wouldn't there be a cpu_relax() on order here? > + } > + } > smp_mb(); > } The old code had smp_mb() before _and_ after the check - is it really correct to drop the one before (or effectively replace it by smp_rmb() in observe_{lock,head}())? > @@ -256,8 +261,17 @@ int _spin_trylock_recursive(spinlock_t *lock) > > void _spin_lock_recursive(spinlock_t *lock) > { > - while ( !spin_trylock_recursive(lock) ) > - cpu_relax(); > + unsigned int cpu = smp_processor_id(); > + > + if ( likely(lock->recurse_cpu != cpu) ) > + { > + spin_lock(lock); While benign with what xen/spinlock.h currently has, it would seem to me that in a function named _spin_lock_recursive() this ought to be _spin_lock(). Jan