Skip to contentSkip to conversation

rwlock: allow recursive read locking when already locked in write mode

PATCH Superseded Roger Pau Monne 4 participants 37 messages Feb 20, 2020 – Feb 21, 2020

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rwlock: allow recursive read locking when already locked in write mode

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.

Fixes 5872c83b42c608 ('smp: convert the cpu maps lock into a rw lock')

---
Changes since v1:
- Move the processor ID field to start at bit 0 and hence avoid the shift when checking it.
- Enlarge write related structures to use 16bit, and hence allow them to be cleared using an atomic write.

---
 xen/common/rwlock.c      |  4 +--
 xen/include/xen/rwlock.h | 53 +++++++++++++++++++++++++---------------
 2 files changed, 35 insertions(+), 22 deletions(-)
xen/common/rwlock.c +2 -2
index d568bbf6de..dadab372b5 100644
@@ -69,7 +69,7 @@ void queue_write_lock_slowpath(rwlock_t *lock)
 
     /* Try to acquire the lock directly if no reader is present. */
     if ( !atomic_read(&lock->cnts) &&
-         (atomic_cmpxchg(&lock->cnts, 0, _QW_LOCKED) == 0) )
+         (atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) == 0) )
         goto unlock;
 
     /*
@@ -93,7 +93,7 @@ void queue_write_lock_slowpath(rwlock_t *lock)
         cnts = atomic_read(&lock->cnts);
         if ( (cnts == _QW_WAITING) &&
              (atomic_cmpxchg(&lock->cnts, _QW_WAITING,
-                             _QW_LOCKED) == _QW_WAITING) )
+                             _write_lock_val()) == _QW_WAITING) )
             break;
 
         cpu_relax();
xen/include/xen/rwlock.h +33 -20

100 lines not on this page · raw message

Signature · 7 lines
2.25.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

xen/include/xen/rwlock.h static inline int _write_trylock(rwlock_t *lock) · line 182

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
xen/include/xen/rwlock.h +4 -5
-    /*
-     * 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.

The rest of the code looks good to me.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

xen/include/xen/rwlock.h static inline int _write_trylock(rwlock_t *lock) · line 182

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
xen/include/xen/rwlock.h +4 -5
-    /*
-     * 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);
Quoted 2 lines

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.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by
#3 lore raw

Hi,

Quoted 35 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 11 lines
   static inline void _write_unlock(rwlock_t *lock)
   {
xen/include/xen/rwlock.h +4 -5
-    /*
-     * 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);
Quoted 7 lines
Quoted 2 lines

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 is trivial enough to be fixed while committing. Assuming this is going to be fixed:

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw
In reply to Roger Pau Monné
Quoted 35 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
xen/include/xen/rwlock.h +4 -5
-    /*
-     * 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);
Quoted 7 lines
Quoted 2 lines

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.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

xen/include/xen/rwlock.h static inline int _write_trylock(rwlock_t *lock) · line 182

Quoted 11 lines
   static inline void _write_unlock(rwlock_t *lock)
   {
xen/include/xen/rwlock.h +4 -5
-    /*
-     * 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);
Quoted 2 lines

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.

Quoted 8 lines
Quoted 2 lines

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?

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 46 lines · Jürgen Groß
Quoted 45 lines · Jan Beulich
Quoted 43 lines · Roger Pau Monné
Quoted 35 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 4 lines

   static inline void _write_unlock(rwlock_t *lock)
   {
-    /*
-     * If the writer field is atomic, it can be cleared directly.

Quoted 8 lines

-     * 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);

Quoted 2 lines

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.

Quoted 7 lines
Quoted 5 lines

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.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw
Quoted 55 lines · Julien Grall
Quoted 52 lines · Jürgen Groß
Quoted 50 lines · Jan Beulich
Quoted 36 lines · Roger Pau Monné
Quoted 35 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 12 lines

   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);

Quoted 7 lines
Quoted 2 lines

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.

Quoted 3 lines
Quoted 1 line

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
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#24 lore raw
In reply to Jürgen Groß
Quoted 45 lines · Jürgen Groß
Quoted 44 lines · Jan Beulich
Quoted 42 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

xen/include/xen/rwlock.h static inline int _write_trylock(rwlock_t *lock) · line 182

Quoted 11 lines
   static inline void _write_unlock(rwlock_t *lock)
   {
xen/include/xen/rwlock.h +4 -5
-    /*
-     * 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);
Quoted 2 lines

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.

Quoted 7 lines
Quoted 5 lines

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?

Well, a union (with the second half being an array of uint16_t) could be the mechanism to implement this in an endian safe way, but the union alone won't help - you'd still have to decide which of the array elements you actually need to write to. And of course you'd then also make assumptions on sizeof(int) which are more strict than our current ones (which I'd like to avoid if possible).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw
In reply to Jürgen Groß

Hi Juergen,

Quoted 75 lines · Jürgen Groß
Quoted 55 lines · Julien Grall
Quoted 52 lines · Jürgen Groß
Quoted 50 lines · Jan Beulich
Quoted 36 lines · Roger Pau Monné
Quoted 35 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 12 lines

   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);

Quoted 7 lines
Quoted 2 lines

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.

Quoted 3 lines
Quoted 1 line

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;

Quoted 8 lines

    return x.val;
}

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

I guess this should do the trick, no?

You are assuming the atomic_t layout which I would rather no want to happen.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#21 lore raw
In reply to Jürgen Groß
Quoted 82 lines · Jürgen Groß
Quoted 58 lines · Julien Grall
Quoted 55 lines · Jürgen Groß
Quoted 53 lines · Jan Beulich
Quoted 38 lines · Roger Pau Monné
Quoted 37 lines · Julien Grall

Hi,

Quoted 13 lines · Roger Pau Monne

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.

Quoted 5 lines

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.

Quoted 12 lines

   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);

Quoted 8 lines
Quoted 3 lines

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.

Quoted 4 lines
Quoted 1 line

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;
}
Quoted 5 lines

...

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

...

I guess this should do the trick, no?

I'm afraid it won't, and not just because of the sizeof() aspect already pointed out. Your x variable would end up like this in memory:

little: 00 00 01 00
big: 00 00 00 01 => 00000001

which, read as 32-bit value, then ends up being

little: 00010000
big: 00000001

The add therefore would be able to spill into the high 16 bits.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw
In reply to Julien Grall
Quoted 81 lines · Julien Grall

Hi Juergen,

Quoted 78 lines · Jürgen Groß
Quoted 55 lines · Julien Grall
Quoted 52 lines · Jürgen Groß
Quoted 50 lines · Jan Beulich
Quoted 36 lines · Roger Pau Monné
Quoted 35 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 12 lines

   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);

Quoted 7 lines
Quoted 2 lines

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.

Quoted 3 lines
Quoted 1 line

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;
}

Quoted 7 lines
Quoted 5 lines

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

I guess this should do the trick, no?

You are assuming the atomic_t layout which I would rather no want to happen.

That already happened. rwlock.h already assumes atomic_t to be 32 bits wide. I agree it would be better to have an atomic32_t type for this use case.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw
Quoted 93 lines · Jürgen Groß
Quoted 92 lines · Julien Grall

Hi Juergen,

Quoted 89 lines · Jürgen Groß
Quoted 61 lines · Julien Grall
Quoted 58 lines · Jürgen Groß
Quoted 56 lines · Jan Beulich
Quoted 41 lines · Roger Pau Monné
Quoted 40 lines · Julien Grall

Hi,

Quoted 15 lines · Roger Pau Monne

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.

Quoted 6 lines

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.

Quoted 12 lines

   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);

Quoted 8 lines
Quoted 3 lines

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.

Quoted 4 lines
Quoted 1 line

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);
...

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I guess this should do the trick, no?

You are assuming the atomic_t layout which I would rather no want to happen.

That already happened. rwlock.h already assumes atomic_t to be 32 bits wide.

Exactly 32 bits, or at least as many?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#22 lore raw
In reply to Jan Beulich
Quoted 96 lines · Jan Beulich
Quoted 82 lines · Jürgen Groß
Quoted 58 lines · Julien Grall
Quoted 55 lines · Jürgen Groß
Quoted 53 lines · Jan Beulich
Quoted 38 lines · Roger Pau Monné
Quoted 37 lines · Julien Grall

Hi,

Quoted 13 lines · Roger Pau Monne

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.

Quoted 5 lines

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.

Quoted 12 lines

   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);

Quoted 8 lines
Quoted 3 lines

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.

Quoted 4 lines
Quoted 1 line

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;
}
Quoted 5 lines

...

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

...

I guess this should do the trick, no?

I'm afraid it won't, and not just because of the sizeof() aspect already pointed out. Your x variable would end up like this in memory:

little: 00 00 01 00
big: 00 00 00 01 => 00000001

Quoted 6 lines

which, read as 32-bit value, then ends up being

little: 00010000
big: 00000001

The add therefore would be able to spill into the high 16 bits.

And why exactly is this worse than just dropping spilled bits? Both cases will explode rather soon. Both cases can be avoided by introduction of e.g. ASSERTing that reader isn't ~0 before incrementing it (or to be zero afterwards).

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw
In reply to Jan Beulich
Quoted 96 lines · Jan Beulich
Quoted 95 lines · Jürgen Groß
Quoted 94 lines · Julien Grall

Hi Juergen,

Quoted 90 lines · Jürgen Groß
Quoted 61 lines · Julien Grall
Quoted 58 lines · Jürgen Groß
Quoted 56 lines · Jan Beulich
Quoted 41 lines · Roger Pau Monné
Quoted 40 lines · Julien Grall

Hi,

Quoted 15 lines · Roger Pau Monne

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.

Quoted 6 lines

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.

Quoted 12 lines

   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);

Quoted 8 lines
Quoted 3 lines

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.

Quoted 4 lines
Quoted 1 line

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?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

You are assuming the atomic_t layout which I would rather no want to happen.

That already happened. rwlock.h already assumes atomic_t to be 32 bits wide.

Exactly 32 bits, or at least as many?

Value is read into a u32 variable and then an update via cmpxchg() is done using that value for comparison. So any additional bits will be lost and update will never succeed (see queue_write_lock_slowpath()).

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#25 lore raw
In reply to Jan Beulich
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

xen/include/xen/rwlock.h static inline int _write_trylock(rwlock_t *lock) · line 182

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
xen/include/xen/rwlock.h +4 -5
-    /*
-     * 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);
Quoted 2 lines

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.

Quoted 8 lines
Quoted 2 lines

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.

Why don't we leave the atomic_dec then?

The ASSERT I've added can be turned into a BUG_ON, and that's likely even safer than what we currently have.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#26 lore raw
Quoted 45 lines · Roger Pau Monné
Quoted 44 lines · Jan Beulich
Quoted 42 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

xen/include/xen/rwlock.h static inline int _write_trylock(rwlock_t *lock) · line 182

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
xen/include/xen/rwlock.h +4 -5
-    /*
-     * 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);
Quoted 2 lines

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.

Quoted 7 lines
Quoted 5 lines

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.

Why don't we leave the atomic_dec then?

Sorry, that should be atomic_sub, not atomic_dec.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw
In reply to Jürgen Groß
Quoted 86 lines · Jürgen Groß
Quoted 85 lines · Julien Grall

Hi Juergen,

Quoted 82 lines · Jürgen Groß
Quoted 55 lines · Julien Grall
Quoted 52 lines · Jürgen Groß
Quoted 50 lines · Jan Beulich
Quoted 36 lines · Roger Pau Monné
Quoted 35 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 12 lines

   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);

Quoted 7 lines
Quoted 2 lines

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.

Quoted 3 lines
Quoted 1 line

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);
...

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I guess this should do the trick, no?

You are assuming the atomic_t layout which I would rather no want to happen.

That already happened. rwlock.h already assumes atomic_t to be 32 bits wide. I agree it would be better to have an atomic32_t type for this use case.

I don't think you understood my point here. An atomic32_t will not be better as you still assume the layout of the structure. I.e the structure has only one field.

I don't know if this is going to bite us in the future, but I don't want to make attempt to use such union. At least without any safeguard in Xen (i.e BUILD_BUG_ON(...)).

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#14 lore raw
Quoted 93 lines · Julien Grall
Quoted 90 lines · Jürgen Groß
Quoted 89 lines · Julien Grall

Hi Juergen,

Quoted 85 lines · Jürgen Groß
Quoted 57 lines · Julien Grall
Quoted 54 lines · Jürgen Groß
Quoted 52 lines · Jan Beulich
Quoted 38 lines · Roger Pau Monné
Quoted 37 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 14 lines

   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);

Quoted 7 lines
Quoted 2 lines

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.

Quoted 3 lines
Quoted 1 line

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?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

You are assuming the atomic_t layout which I would rather no want to happen.

That already happened. rwlock.h already assumes atomic_t to be 32 bits wide. I agree it would be better to have an atomic32_t type for this use case.

I don't think you understood my point here. An atomic32_t will not be better as you still assume the layout of the structure. I.e the structure has only one field.

I don't understand your reasoning here, sorry.

Are you saying that a structure of two uint16_t isn't known to be 32 bits long?

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw
In reply to Jürgen Groß
Quoted 100 lines · Jürgen Groß
Quoted 99 lines · Jan Beulich
Quoted 98 lines · Jürgen Groß
Quoted 96 lines · Julien Grall

Hi Juergen,

Quoted 82 lines · Jürgen Groß
Quoted 61 lines · Julien Grall
Quoted 58 lines · Jürgen Groß
Quoted 56 lines · Jan Beulich
Quoted 41 lines · Roger Pau Monné
Quoted 40 lines · Julien Grall

Hi,

Quoted 15 lines · Roger Pau Monne

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.

Quoted 6 lines

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.

Quoted 12 lines

   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);

Quoted 8 lines
Quoted 3 lines

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.

Quoted 4 lines
Quoted 1 line

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;

Quoted 8 lines

     return x.val;
}

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

I guess this should do the trick, no?

You are assuming the atomic_t layout which I would rather no want to happen.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

That already happened. rwlock.h already assumes atomic_t to be 32 bits wide.

Exactly 32 bits, or at least as many?

Value is read into a u32 variable and then an update via cmpxchg() is done using that value for comparison. So any additional bits will be lost and update will never succeed (see queue_write_lock_slowpath()).

Well, the local variables should be of type int (or possibly unsigned int), but the logic there isn't tied to anything else being exactly 32 bits wide. These local variable are merely an example of misguided use of fixed width types.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#27 lore raw
In reply to Roger Pau Monné
Quoted 45 lines · Roger Pau Monné
Quoted 44 lines · Jan Beulich
Quoted 42 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

xen/include/xen/rwlock.h static inline int _write_trylock(rwlock_t *lock) · line 182

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
xen/include/xen/rwlock.h +4 -5
-    /*
-     * 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);
Quoted 2 lines

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.

Quoted 7 lines
Quoted 5 lines

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.

Why don't we leave the atomic_dec then?

Because you need to invoke smp_processor_id() to calculate the value to use in the subtraction. I'm not meaning to say I'm entirely opposed (seeing how much of a discussion we're having), but the "simple write of zero" approach is certainly appealing.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#28 lore raw
Quoted 52 lines · Jan Beulich
Quoted 51 lines · Roger Pau Monné
Quoted 49 lines · Jan Beulich
Quoted 35 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
xen/include/xen/rwlock.h +4 -5
-    /*
-     * 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);
Quoted 7 lines
Quoted 2 lines

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.

Quoted 6 lines
Quoted 1 line

Why don't we leave the atomic_dec then?

Because you need to invoke smp_processor_id() to calculate the value to use in the subtraction. I'm not meaning to say I'm entirely opposed (seeing how much of a discussion we're having), but the "simple write of zero" approach is certainly appealing.

Well, we could avoid the smp_processor_id() call and instead use:

atomic_sub(atomic_read(&lock->cnts) & 0xffff, &lock->cnts);

Note mask is using the low 16bits now, but if we go the atomic_sub route we could change the CPU ID fields to be 12 bits again and thus have some more room for the readers count.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#23 lore raw
In reply to Jürgen Groß
Quoted 102 lines · Jürgen Groß
Quoted 101 lines · Jan Beulich
Quoted 82 lines · Jürgen Groß
Quoted 58 lines · Julien Grall
Quoted 55 lines · Jürgen Groß
Quoted 53 lines · Jan Beulich
Quoted 38 lines · Roger Pau Monné
Quoted 37 lines · Julien Grall

Hi,

Quoted 13 lines · Roger Pau Monne

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.

Quoted 5 lines

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.

Quoted 12 lines

   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);

Quoted 8 lines
Quoted 3 lines

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.

Quoted 4 lines
Quoted 1 line

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;
}
Quoted 5 lines

...

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

...

I guess this should do the trick, no?

I'm afraid it won't, and not just because of the sizeof() aspect already pointed out. Your x variable would end up like this in memory:

little: 00 00 01 00
big: 00 00 00 01 => 00000001

which, read as 32-bit value, then ends up being

little: 00010000
big: 00000001

Quoted 6 lines
Quoted 1 line

The add therefore would be able to spill into the high 16 bits.

And why exactly is this worse than just dropping spilled bits? Both cases will explode rather soon. Both cases can be avoided by introduction of e.g. ASSERTing that reader isn't ~0 before incrementing it (or to be zero afterwards).

Hmm, yes. I can't reconstruct the thoughts of the people having laid out the bit assignments the way they are right now. I can only assume there was a reason to put the reader count in the high bits. But it looks like my previous remark wasn't even pointing at the worst effect. The resulting big endian 32-bit layout also is not in line with the _QW_* constants, due to mis-ordering of the two halves.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#15 lore raw
In reply to Jürgen Groß
Quoted 100 lines · Jürgen Groß
Quoted 99 lines · Julien Grall
Quoted 95 lines · Jürgen Groß
Quoted 83 lines · Julien Grall

Hi Juergen,

Quoted 80 lines · Jürgen Groß
Quoted 57 lines · Julien Grall
Quoted 54 lines · Jürgen Groß
Quoted 52 lines · Jan Beulich
Quoted 38 lines · Roger Pau Monné
Quoted 37 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 14 lines

   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);

Quoted 7 lines
Quoted 2 lines

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.

Quoted 3 lines
Quoted 1 line

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;
}

Quoted 7 lines
Quoted 5 lines

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

I guess this should do the trick, no?

You are assuming the atomic_t layout which I would rather no want to happen.

That already happened. rwlock.h already assumes atomic_t to be 32 bits wide. I agree it would be better to have an atomic32_t type for this use case.

Quoted 6 lines
Quoted 1 line

I don't think you understood my point here. An atomic32_t will not be better as you still assume the layout of the structure. I.e the structure has only one field.

I don't understand your reasoning here, sorry.

Are you saying that a structure of two uint16_t isn't known to be 32 bits long?

You are assuming that atomic_t will always be:

struct
{
int counter;
}

What if we decide to turn into

struct
{
bool a;
int counter;
}

You would at least want a BUILD_BUG_ON() as at the compiler will throw you an error rather than happily countinuing.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#29 lore raw
In reply to Roger Pau Monné
Quoted 55 lines · Roger Pau Monné
Quoted 54 lines · Jan Beulich
Quoted 52 lines · Roger Pau Monné
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Review

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
(quoted diff) +4 -5
-    /*
-     * 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);
Quoted 2 lines

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.

Quoted 8 lines
Quoted 2 lines

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.

Why don't we leave the atomic_dec then?

Quoted 8 lines
Quoted 4 lines

Because you need to invoke smp_processor_id() to calculate the value to use in the subtraction. I'm not meaning to say I'm entirely opposed (seeing how much of a discussion we're having), but the "simple write of zero" approach is certainly appealing.

Well, we could avoid the smp_processor_id() call and instead use:

atomic_sub(atomic_read(&lock->cnts) & 0xffff, &lock->cnts);

Which would make me suggest atomic_and() again.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#33 lore raw
In reply to Roger Pau Monné
Quoted 9 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich

Because you need to invoke smp_processor_id() to calculate the value to use in the subtraction. I'm not meaning to say I'm entirely opposed (seeing how much of a discussion we're having), but the "simple write of zero" approach is certainly appealing.

Well, we could avoid the smp_processor_id() call and instead use:

atomic_sub(atomic_read(&lock->cnts) & 0xffff, &lock->cnts);

AFAICT, this would not be safe because the top 16-bit may change behind your back (via a read_lock).

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#30 lore raw
In reply to Jan Beulich
Quoted 61 lines · Jan Beulich
Quoted 60 lines · Roger Pau Monné
Quoted 58 lines · Jan Beulich
Quoted 45 lines · Roger Pau Monné
Quoted 44 lines · Jan Beulich
Quoted 42 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Review

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
(quoted diff) +4 -5
-    /*
-     * 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);
Quoted 2 lines

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.

Quoted 7 lines
Quoted 5 lines

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.

Why don't we leave the atomic_dec then?

Because you need to invoke smp_processor_id() to calculate the value to use in the subtraction. I'm not meaning to say I'm entirely opposed (seeing how much of a discussion we're having), but the "simple write of zero" approach is certainly appealing.

Quoted 5 lines
Quoted 3 lines

Well, we could avoid the smp_processor_id() call and instead use:

atomic_sub(atomic_read(&lock->cnts) & 0xffff, &lock->cnts);

Which would make me suggest atomic_and() again.

I'm certainly not opposed to that, but in order to get this regression fixed I would argue that such atomic_sub is no worse than what's currently done.

I can look into adding an atomic_and operation to all arches, but this is likely to take time and I would like to get this sorted sooner rather than later.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#34 lore raw
In reply to Julien Grall
Quoted 9 lines · Julien Grall
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich

Because you need to invoke smp_processor_id() to calculate the value to use in the subtraction. I'm not meaning to say I'm entirely opposed (seeing how much of a discussion we're having), but the "simple write of zero" approach is certainly appealing.

Quoted 6 lines
Quoted 3 lines

Well, we could avoid the smp_processor_id() call and instead use:

atomic_sub(atomic_read(&lock->cnts) & 0xffff, &lock->cnts);

AFAICT, this would not be safe because the top 16-bit may change behind your back (via a read_lock).

Why not? We don't touch the top 16bits in any way, the subtraction only affects the low 16bits and is done in an atomic manner.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#35 lore raw
Quoted 12 lines · Roger Pau Monné
Quoted 11 lines · Julien Grall
Quoted 8 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich

Because you need to invoke smp_processor_id() to calculate the value to use in the subtraction. I'm not meaning to say I'm entirely opposed (seeing how much of a discussion we're having), but the "simple write of zero" approach is certainly appealing.

Well, we could avoid the smp_processor_id() call and instead use:

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

atomic_sub(atomic_read(&lock->cnts) & 0xffff, &lock->cnts);

AFAICT, this would not be safe because the top 16-bit may change behind your back (via a read_lock).

Why not? We don't touch the top 16bits in any way, the subtraction only affects the low 16bits and is done in an atomic manner.

Hmmm, I did misread the code :(. Sorry for the that.

Quoted 3 lines

Thanks, Roger.

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#36 lore raw
In reply to Julien Grall
Quoted 7 lines · Julien Grall
Quoted 6 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich

Because you need to invoke smp_processor_id() to calculate the value to use in the subtraction. I'm not meaning to say I'm entirely opposed (seeing how much of a discussion we're having), but the "simple write of zero" approach is certainly appealing.

Quoted 6 lines
Quoted 3 lines

Well, we could avoid the smp_processor_id() call and instead use:

atomic_sub(atomic_read(&lock->cnts) & 0xffff, &lock->cnts);

AFAICT, this would not be safe because the top 16-bit may change behind your back (via a read_lock).

But them changing between the atomic_read() and the atomic_sub() is fine. The sub then will still only affect the low bits, leaving the high ones as they were (potentially as updated after the read).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#16 lore raw
In reply to Julien Grall
Quoted 117 lines · Julien Grall
Quoted 100 lines · Jürgen Groß
Quoted 99 lines · Julien Grall
Quoted 95 lines · Jürgen Groß
Quoted 83 lines · Julien Grall

Hi Juergen,

Quoted 80 lines · Jürgen Groß
Quoted 57 lines · Julien Grall
Quoted 54 lines · Jürgen Groß
Quoted 52 lines · Jan Beulich
Quoted 38 lines · Roger Pau Monné
Quoted 37 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 14 lines

   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);

Quoted 7 lines
Quoted 2 lines

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.

Quoted 3 lines
Quoted 1 line

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;
}

Quoted 7 lines
Quoted 5 lines

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

I guess this should do the trick, no?

You are assuming the atomic_t layout which I would rather no want to happen.

That already happened. rwlock.h already assumes atomic_t to be 32 bits wide. I agree it would be better to have an atomic32_t type for this use case.

Quoted 6 lines
Quoted 1 line

I don't think you understood my point here. An atomic32_t will not be better as you still assume the layout of the structure. I.e the structure has only one field.

I don't understand your reasoning here, sorry.

Are you saying that a structure of two uint16_t isn't known to be 32 bits long?

You are assuming that atomic_t will always be:

struct
{
  int counter;
}

Quoted 7 lines

What if we decide to turn into

struct
{
  bool a;
  int counter;
}

As said before: then queue_write_lock_slowpath() is already broken.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#31 lore raw
In reply to Roger Pau Monné
Quoted 64 lines · Roger Pau Monné
Quoted 63 lines · Jan Beulich
Quoted 62 lines · Roger Pau Monné
Quoted 52 lines · Jan Beulich
Quoted 51 lines · Roger Pau Monné
Quoted 49 lines · Jan Beulich
Quoted 35 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
(quoted diff) +4 -5
-    /*
-     * 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);
Quoted 7 lines
Quoted 2 lines

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.

Quoted 6 lines
Quoted 1 line

Why don't we leave the atomic_dec then?

Because you need to invoke smp_processor_id() to calculate the value to use in the subtraction. I'm not meaning to say I'm entirely opposed (seeing how much of a discussion we're having), but the "simple write of zero" approach is certainly appealing.

Well, we could avoid the smp_processor_id() call and instead use:

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

atomic_sub(atomic_read(&lock->cnts) & 0xffff, &lock->cnts);

Which would make me suggest atomic_and() again.

I'm certainly not opposed to that, but in order to get this regression fixed I would argue that such atomic_sub is no worse than what's currently done.

It's one more read of a potentially heavily contended memory location.

Quoted 3 lines

I can look into adding an atomic_and operation to all arches, but this is likely to take time and I would like to get this sorted sooner rather than later.

Because of it presumably taking time it was that I also brought up to consider reverting. Then again, the Arm atomic_and() can, afaict, be trivially cloned from their "add" counterparts, by replacing the middle 'd' both in the function names and in the insn mnemonics. It's just that there shouldn't be atomic_and_return() (for not being useful, as it doesn't allow reconstructing the original value).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#17 lore raw
In reply to Jürgen Groß
Quoted 141 lines · Jürgen Groß
Quoted 140 lines · Julien Grall
Quoted 119 lines · Jürgen Groß
Quoted 118 lines · Julien Grall
Quoted 114 lines · Jürgen Groß
Quoted 101 lines · Julien Grall

Hi Juergen,

Quoted 98 lines · Jürgen Groß
Quoted 74 lines · Julien Grall
Quoted 71 lines · Jürgen Groß
Quoted 69 lines · Jan Beulich
Quoted 54 lines · Roger Pau Monné
Quoted 49 lines · Julien Grall

Hi,

Quoted 27 lines · Roger Pau Monne

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.

Quoted 14 lines

   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);

Quoted 3 lines

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.

Quoted 5 lines

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.

Quoted 4 lines
Quoted 1 line

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;
}

Quoted 8 lines
Quoted 5 lines

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

I guess this should do the trick, no?

You are assuming the atomic_t layout which I would rather no want to happen.

That already happened. rwlock.h already assumes atomic_t to be 32 bits wide. I agree it would be better to have an atomic32_t type for this use case.

Quoted 8 lines
Quoted 3 lines

I don't think you understood my point here. An atomic32_t will not be better as you still assume the layout of the structure. I.e the structure has only one field.

I don't understand your reasoning here, sorry.

Are you saying that a structure of two uint16_t isn't known to be 32 bits long?

You are assuming that atomic_t will always be:

struct
{
  int counter;
}

What if we decide to turn into

Quoted 7 lines
Quoted 5 lines

struct
{
  bool a;
  int counter;
}

As said before: then queue_write_lock_slowpath() is already broken.

Why? The atomic_*() used would still only act on the counter field (for their actual operation, i.e. what matters to callers; the other field(s) could be statistics or whatever).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#18 lore raw
Quoted 148 lines · Jan Beulich
Quoted 147 lines · Jürgen Groß
Quoted 145 lines · Julien Grall
Quoted 119 lines · Jürgen Groß
Quoted 118 lines · Julien Grall
Quoted 114 lines · Jürgen Groß
Quoted 101 lines · Julien Grall

Hi Juergen,

Quoted 98 lines · Jürgen Groß
Quoted 74 lines · Julien Grall
Quoted 71 lines · Jürgen Groß
Quoted 69 lines · Jan Beulich
Quoted 54 lines · Roger Pau Monné
Quoted 52 lines · Julien Grall

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.

Quoted 5 lines

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.

Quoted 4 lines
Quoted 1 line

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;
}

Quoted 8 lines
Quoted 5 lines

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

I guess this should do the trick, no?

You are assuming the atomic_t layout which I would rather no want to happen.

That already happened. rwlock.h already assumes atomic_t to be 32 bits wide. I agree it would be better to have an atomic32_t type for this use case.

Quoted 8 lines
Quoted 3 lines

I don't think you understood my point here. An atomic32_t will not be better as you still assume the layout of the structure. I.e the structure has only one field.

I don't understand your reasoning here, sorry.

Are you saying that a structure of two uint16_t isn't known to be 32 bits long?

You are assuming that atomic_t will always be:

struct
{
  int counter;
}

What if we decide to turn into

struct
{
  bool a;
  int counter;
}

Quoted 5 lines
Quoted 1 line

As said before: then queue_write_lock_slowpath() is already broken.

Why? The atomic_*() used would still only act on the counter field (for their actual operation, i.e. what matters to callers; the other field(s) could be statistics or whatever).

No:

u32 cnts;
...
if ( !(cnts & _QW_WMASK) &&

     (atomic_cmpxchg(&lock->cnts, cnts,
                     cnts | _QW_WAITING) == cnts) )

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#19 lore raw
Quoted 24 lines · Jürgen Groß
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Jürgen Groß
Quoted 14 lines · Julien Grall

You are assuming that atomic_t will always be:

struct
{
  int counter;
}

What if we decide to turn into

struct
{
  bool a;
  int counter;
}

Quoted 5 lines
Quoted 1 line

As said before: then queue_write_lock_slowpath() is already broken.

Why? The atomic_*() used would still only act on the counter field (for their actual operation, i.e. what matters to callers; the other field(s) could be statistics or whatever).

Quoted 7 lines

No:

u32 cnts;
...
if ( !(cnts & _QW_WMASK) &&

      (atomic_cmpxchg(&lock->cnts, cnts,
                      cnts | _QW_WAITING) == cnts) )

I must be blind then. As said, atomic_cmpxchg() would still (for the purpose of consuming "cnts") act on only the "counter" field. It may additionally e.g. increment a stats counter.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#20 lore raw
Quoted 33 lines · Jan Beulich
Quoted 24 lines · Jürgen Groß
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Jürgen Groß
Quoted 14 lines · Julien Grall

You are assuming that atomic_t will always be:

struct

{
     int counter;
}

What if we decide to turn into

struct

{
     bool a;
     int counter;
}
Quoted 5 lines
Quoted 1 line

As said before: then queue_write_lock_slowpath() is already broken.

Why? The atomic_*() used would still only act on the counter field (for their actual operation, i.e. what matters to callers; the other field(s) could be statistics or whatever).

Quoted 7 lines

No:

u32 cnts;
...
if ( !(cnts & _QW_WMASK) &&

       (atomic_cmpxchg(&lock->cnts, cnts,
                       cnts | _QW_WAITING) == cnts) )
Quoted 3 lines

I must be blind then. As said, atomic_cmpxchg() would still (for the purpose of consuming "cnts") act on only the "counter" field. It may additionally e.g. increment a stats counter.

Oh, sorry, I misunderstood you.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#32 lore raw
In reply to Jan Beulich
Quoted 79 lines · Jan Beulich
Quoted 64 lines · Roger Pau Monné
Quoted 63 lines · Jan Beulich
Quoted 62 lines · Roger Pau Monné
Quoted 52 lines · Jan Beulich
Quoted 51 lines · Roger Pau Monné
Quoted 49 lines · Jan Beulich
Quoted 35 lines · Roger Pau Monné
Quoted 34 lines · Julien Grall

Hi,

Quoted 12 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Quoted 11 lines
  static inline void _write_unlock(rwlock_t *lock)
  {
(quoted diff) +4 -5
-    /*
-     * 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);
Quoted 7 lines
Quoted 2 lines

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.

Quoted 6 lines
Quoted 1 line

Why don't we leave the atomic_dec then?

Because you need to invoke smp_processor_id() to calculate the value to use in the subtraction. I'm not meaning to say I'm entirely opposed (seeing how much of a discussion we're having), but the "simple write of zero" approach is certainly appealing.

Well, we could avoid the smp_processor_id() call and instead use:

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

atomic_sub(atomic_read(&lock->cnts) & 0xffff, &lock->cnts);

Which would make me suggest atomic_and() again.

I'm certainly not opposed to that, but in order to get this regression fixed I would argue that such atomic_sub is no worse than what's currently done.

It's one more read of a potentially heavily contended memory location.

Quoted 3 lines

I can look into adding an atomic_and operation to all arches, but this is likely to take time and I would like to get this sorted sooner rather than later.

Quoted 6 lines

Because of it presumably taking time it was that I also brought up to consider reverting. Then again, the Arm atomic_and() can, afaict, be trivially cloned from their "add" counterparts, by replacing the middle 'd' both in the function names and in the insn mnemonics. It's just that there shouldn't be atomic_and_return() (for not being useful, as it doesn't allow reconstructing the original value).

I've added atomic_and in a pre-patch and I'm currently waiting for gitlab to finish testing to assert that I'm not breaking the build. Will post once that's finished if successful.

FWIW, the branch is at:

http://xenbits.xen.org/gitweb/?p=people/royger/xen.git;a=shortlog;h=refs/heads/rwlock3

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel