All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jürgen Groß" <jgross@suse.com>
To: "Julien Grall" <julien@xen.org>,
	"Jan Beulich" <jbeulich@suse.com>,
	"Roger Pau Monné" <roger.pau@citrix.com>
Cc: Stefano Stabellini <sstabellini@kernel.org>, Wei Liu <wl@xen.org>,
	Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>,
	George Dunlap <George.Dunlap@eu.citrix.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>,
	xen-devel@lists.xenproject.org
Subject: Re: [Xen-devel] [PATCH v2] rwlock: allow recursive read locking when already locked in write mode
Date: Fri, 21 Feb 2020 15:06:00 +0100	[thread overview]
Message-ID: <21c59ebb-e396-447f-5ac1-d7e2efd76bb5@suse.com> (raw)
In-Reply-To: <e5be3888-6252-f03c-675d-8d7dfd9330b8@xen.org>

On 21.02.20 14:49, Julien Grall wrote:
> 
> 
> On 21/02/2020 13:46, Jürgen Groß wrote:
>> On 21.02.20 14:36, Jan Beulich wrote:
>>> On 21.02.2020 10:10, Roger Pau Monné wrote:
>>>> On Thu, Feb 20, 2020 at 07:20:06PM +0000, Julien Grall wrote:
>>>>> Hi,
>>>>>
>>>>> On 20/02/2020 17:31, Roger Pau Monne wrote:
>>>>>> Allow a CPU already holding the lock in write mode to also lock it in
>>>>>> read mode. There's no harm in allowing read locking a rwlock that's
>>>>>> already owned by the caller (ie: CPU) in write mode. Allowing such
>>>>>> accesses is required at least for the CPU maps use-case.
>>>>>>
>>>>>> In order to do this reserve 14bits of the lock, this allows to 
>>>>>> support
>>>>>> up to 16384 CPUs. Also reduce the write lock mask to 2 bits: one to
>>>>>> signal there are pending writers waiting on the lock and the other to
>>>>>> signal the lock is owned in write mode. Note the write related data
>>>>>> is using 16bits, this is done in order to be able to clear it (and
>>>>>> thus release the lock) using a 16bit atomic write.
>>>>>>
>>>>>> This reduces the maximum number of concurrent readers from 
>>>>>> 16777216 to
>>>>>> 65536, I think this should still be enough, or else the lock field
>>>>>> can be expanded from 32 to 64bits if all architectures support atomic
>>>>>> operations on 64bit integers.
>>>>>
>>>>> FWIW, arm32 is able to support atomic operations on 64-bit integers.
>>>>>
>>>>>>    static inline void _write_unlock(rwlock_t *lock)
>>>>>>    {
>>>>>> -    /*
>>>>>> -     * If the writer field is atomic, it can be cleared directly.
>>>>>> -     * Otherwise, an atomic subtraction will be used to clear it.
>>>>>> -     */
>>>>>> -    atomic_sub(_QW_LOCKED, &lock->cnts);
>>>>>> +    /* Since the writer field is atomic, it can be cleared 
>>>>>> directly. */
>>>>>> +    ASSERT(_is_write_locked_by_me(atomic_read(&lock->cnts)));
>>>>>> +    BUILD_BUG_ON(_QR_SHIFT != 16);
>>>>>> +    write_atomic((uint16_t *)&lock->cnts, 0);
>>>>>
>>>>> I think this is an abuse to cast an atomic_t() directly into a 
>>>>> uint16_t. You
>>>>> would at least want to use &lock->cnts.counter here.
>>>>
>>>> Sure, I was wondering about this myself.
>>>>
>>>> Will wait for more comments, not sure whether this can be fixed upon
>>>> commit if there are no other issues.
>>>
>>> It's more than just adding another field specifier here. A cast like
>>> this one is endianness-unsafe, and hence a trap waiting for a big
>>> endian port attempt to fall into. At the very least this should cause
>>> a build failure on big endian systems, even better would be if it was
>>> endianness-safe.
>>
>> Wouldn't a union be the better choice?
> 
> You would not be able to use atomic_t in that case as you can't assume 
> the layout of the structure.

union rwlockword {
     atomic_t cnts;
     uint32_t val;
     struct {
         uint16_t write;
         uint16_t readers;
     };
};

static inline const uint32_t _qr_bias(
     const union rwlockword {
         .write = 0,
         .readers = 1
     } x;

     return x.val;
}

...
     cnts = atomic_add_return(_qr_bias(), &lock->cnts);
...

I guess this should do the trick, no?


Juergen

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  reply	other threads:[~2020-02-21 14:06 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-20 17:31 [Xen-devel] [PATCH v2] rwlock: allow recursive read locking when already locked in write mode Roger Pau Monne
2020-02-20 19:20 ` Julien Grall
2020-02-21  9:10   ` Roger Pau Monné
2020-02-21 12:42     ` Julien Grall
2020-02-21 13:36     ` Jan Beulich
2020-02-21 13:46       ` Jürgen Groß
2020-02-21 13:49         ` Julien Grall
2020-02-21 14:06           ` Jürgen Groß [this message]
2020-02-21 14:11             ` Julien Grall
2020-02-21 14:16               ` Jürgen Groß
2020-02-21 14:17                 ` Jan Beulich
2020-02-21 14:24                   ` Jürgen Groß
2020-02-21 14:37                     ` Jan Beulich
2020-02-21 14:32                 ` Julien Grall
2020-02-21 14:35                   ` Jürgen Groß
2020-02-21 14:51                     ` Julien Grall
2020-02-21 15:13                       ` Jürgen Groß
2020-02-21 15:17                         ` Jan Beulich
2020-02-21 15:23                           ` Jürgen Groß
2020-02-21 15:27                             ` Jan Beulich
2020-02-21 15:33                               ` Jürgen Groß
2020-02-21 14:16             ` Jan Beulich
2020-02-21 14:19               ` Jürgen Groß
2020-02-21 14:50                 ` Jan Beulich
2020-02-21 14:06         ` Jan Beulich
2020-02-21 14:26       ` Roger Pau Monné
2020-02-21 14:32         ` Roger Pau Monné
2020-02-21 14:41         ` Jan Beulich
2020-02-21 14:49           ` Roger Pau Monné
2020-02-21 14:52             ` Jan Beulich
2020-02-21 14:58               ` Roger Pau Monné
2020-02-21 15:15                 ` Jan Beulich
2020-02-21 16:22                   ` Roger Pau Monné
2020-02-21 14:56             ` Julien Grall
2020-02-21 14:59               ` Roger Pau Monné
2020-02-21 15:02                 ` Julien Grall
2020-02-21 15:11               ` Jan Beulich

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=21c59ebb-e396-447f-5ac1-d7e2efd76bb5@suse.com \
    --to=jgross@suse.com \
    --cc=George.Dunlap@eu.citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=jbeulich@suse.com \
    --cc=julien@xen.org \
    --cc=konrad.wilk@oracle.com \
    --cc=roger.pau@citrix.com \
    --cc=sstabellini@kernel.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.