public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: "Radim Krčmář" <rkrcmar@redhat.com>
Cc: kvm@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
	Thomas Huth <thuth@redhat.com>,
	Laurent Vivier <lvivier@redhat.com>,
	kvm-ppc@vger.kernel.org
Subject: Re: [kvm-unit-tests PATCH v1 1/3] lib: provide generic spinlock
Date: Mon, 15 May 2017 09:48:22 +0200	[thread overview]
Message-ID: <2daabf2e-b472-8da9-8e41-17cda8518ee6@redhat.com> (raw)
In-Reply-To: <20170512164313.GA12549@potion>

On 12.05.2017 18:43, Radim Krčmář wrote:
> 2017-05-12 12:20+0200, David Hildenbrand:
>> Let's provide a basic lock implementation that should work on most
>> architectures.
>>
>> Signed-off-by: David Hildenbrand <david@redhat.com>
>> ---
>>  lib/asm-generic/spinlock.h | 16 +++++++++++++++-
>>  1 file changed, 15 insertions(+), 1 deletion(-)
>>
>> diff --git a/lib/asm-generic/spinlock.h b/lib/asm-generic/spinlock.h
>> index 3141744..e8c3a58 100644
>> --- a/lib/asm-generic/spinlock.h
>> +++ b/lib/asm-generic/spinlock.h
>> @@ -1,4 +1,18 @@
>>  #ifndef _ASM_GENERIC_SPINLOCK_H_
>>  #define _ASM_GENERIC_SPINLOCK_H_
>> -#error need architecture specific asm/spinlock.h
>> +
>> +struct spinlock {
>> +    unsigned int v;
>> +};
>> +
>> +static inline void spin_lock(struct spinlock *lock)
>> +{
>> +	while (!__sync_bool_compare_and_swap(&lock->v, 0, 1));
>> +}
>> +
>> +static inline void spin_unlock(struct spinlock *lock)
>> +{
>> +	__sync_bool_compare_and_swap(&lock->v, 1, 0);
>> +}
> 
> x86 would be better with __sync_lock_test_and_set() and
> __sync_lock_release() as they generate the same code we have now,
> instead of two locked cmpxchgs.

Both should work, however using your pair looks nicer and also seems to
work for x86, powerpc and s390x (at least GCC doesn't spit fire).

So I'll resend, thanks!

> 
> GCC mentions that some targets might have problems with that, but they
> seem to fall back to boolean value and compare-and-swap.
> 
> Any reason to avoid "while(__sync_lock_test_and_set(&lock->v, 1));"?

I haven't found anything speaking against it. There are even multiple
articles suggesting to either use __sync_lock_test_and_set or
__sync_bool_compare_and_swap in a loop for exactly that purpose.

> 
> Thanks.
> 


-- 

Thanks,

David

  parent reply	other threads:[~2017-05-15  7:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-12 10:20 [kvm-unit-tests PATCH v1 0/3] provide asm-generic spinlock David Hildenbrand
2017-05-12 10:20 ` [kvm-unit-tests PATCH v1 1/3] lib: provide generic spinlock David Hildenbrand
2017-05-12 10:58   ` Thomas Huth
2017-05-12 16:43   ` Radim Krčmář
2017-05-12 16:49     ` Paolo Bonzini
2017-05-15  7:48     ` David Hildenbrand [this message]
2017-05-12 10:20 ` [kvm-unit-tests PATCH v1 2/3] ppc64: use asm-generic spinlock David Hildenbrand
2017-05-12 10:55   ` Thomas Huth
2017-05-12 10:20 ` [kvm-unit-tests PATCH v1 3/3] x86: " David Hildenbrand

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=2daabf2e-b472-8da9-8e41-17cda8518ee6@redhat.com \
    --to=david@redhat.com \
    --cc=kvm-ppc@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@redhat.com \
    --cc=thuth@redhat.com \
    /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