From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Vrabel Subject: Re: [PATCHv2 5/6] xen: use ticket locks for spin locks Date: Fri, 10 Apr 2015 18:29:17 +0100 Message-ID: <5528086D.7090407@cantab.net> References: <1428675597-28465-1-git-send-email-david.vrabel@citrix.com> <1428675597-28465-6-git-send-email-david.vrabel@citrix.com> <5527FDF6.3030706@oracle.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 1Ygck8-0002d2-EP for xen-devel@lists.xenproject.org; Fri, 10 Apr 2015 17:29:24 +0000 In-Reply-To: <5527FDF6.3030706@oracle.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: Boris Ostrovsky , David Vrabel , xen-devel@lists.xenproject.org Cc: Keir Fraser , Tim Deegan , Ian Campbell , Jan Beulich , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org On 10/04/2015 17:44, Boris Ostrovsky wrote: > On 04/10/2015 10:19 AM, David Vrabel wrote: > >> @@ -138,14 +138,17 @@ void spin_debug_disable(void) >> >> void _spin_lock(spinlock_t *lock) >> { >> + spinlock_tickets_t tickets = { .tail = 1, }; >> LOCK_PROFILE_VAR; >> >> check_lock(&lock->debug); >> - while ( unlikely(!_raw_spin_trylock(&lock->raw)) ) >> + tickets.head_tail = xadd(&lock->tickets, tickets.head_tail); >> + if ( tickets.tail != read_atomic(&lock->tickets.head) ) >> { >> LOCK_PROFILE_BLOCK; >> - while ( likely(_raw_spin_is_locked(&lock->raw)) ) >> + do { >> cpu_relax(); >> + } while ( tickets.tail != read_atomic(&lock->tickets.head) ); >> } > > > Why do you use both 'if' and 'while"? I.e. why not just > > while ( tickets.tail != read_atomic(&lock->tickets.head) ) > { > LOCK_PROFILE_BLOCK; > cpu_relax(); > } We need to only call LOCK_PROFILE_BLOCK once when we start blocking. >> @@ -194,35 +174,44 @@ void _spin_unlock(spinlock_t *lock) >> { >> preempt_enable(); >> LOCK_PROFILE_REL; >> - _raw_spin_unlock(&lock->raw); >> + lock->tickets.head++; > > > Is it safe to do a plain increment here and a locked one? Yes. Only the lock holder writes to head. David