Skip to contentSkip to conversation

Implement per-cpu reader-writer locks

PATCH Superseded Malcolm Crossley 5 participants 14 messages Dec 18, 2015 – Jan 21, 2016

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Implement per-cpu reader-writer locks

This patch series adds per-cpu reader-writer locks as a generic lock implementation and then converts the grant table and p2m rwlocks to use the percpu rwlocks, in order to improve multi-socket host performance.

CPU profiling has revealed the rwlocks themselves suffer from severe cache line bouncing due to the cmpxchg operation used even when taking a read lock. Multiqueue paravirtualised I/O results in heavy contention of the grant table and p2m read locks of a specific domain and so I/O throughput is bottlenecked by the overhead of the cache line bouncing itself.

Per-cpu read locks avoid lock cache line bouncing by using a per-cpu data area to record a CPU has taken the read lock. Correctness is enforced for the write lock by using a per lock barrier which forces the per-cpu read lock to revert to using a standard read lock. The write lock then polls all the percpu data area until active readers for the lock have exited.

Removing the cache line bouncing on a multi-socket Haswell-EP system dramatically improves performance, with 16 vCPU network IO performance going from 15 gb/s to 64 gb/s! The host under test was fully utilising all 40 logical CPU's at 64 gb/s, so a bigger logical CPU host may see an even better IO improvement.

Note: Benchmarking of the these performance improvements should be done with the non debug version of the hypervisor otherwise the map_domain_page spinlock is the main bottleneck.

Changes in V4:
- Move percpu_owner ASSERTS to be inline function
- Rename grant table rwlock wrappers

Changes in V4:
- Fix the ASSERTS for the percpu_owner check

Changes in V3:
- Add percpu rwlock owner for debug Xen builds
- Validate percpu rwlock owner at runtime for debug Xen builds
- Fix hard tab issues
- Use percpu rwlock wrappers for grant table rwlock users
- Add comments why rw_is_locked ASSERTS have been removed in grant table code

Changes in V2:
- Add Cover letter
- Convert p2m rwlock to percpu rwlock
- Improve percpu rwlock to safely handle simultaneously holding 2 or more locks
- Move percpu rwlock barrier from global to per lock
- Move write lock cpumask variable to a percpu variable
- Add macros to help initialise and use percpu rwlocks
- Updated IO benchmark results to cover revised locking implementation

Malcolm Crossley (3):
rwlock: Add per-cpu reader-writer lock infrastructure
grant_table: convert grant table rwlock to percpu rwlock
p2m: convert p2m rwlock to percpu rwlock

 xen/arch/arm/mm.c             |   4 +-
 xen/arch/x86/mm.c             |   4 +-
 xen/arch/x86/mm/mm-locks.h    |  12 ++--
 xen/arch/x86/mm/p2m.c         |   1 +
 xen/common/grant_table.c      | 126 +++++++++++++++++++++++-------------------
 xen/common/spinlock.c         |  46 +++++++++++++++
 xen/include/asm-arm/percpu.h  |   5 ++
 xen/include/asm-x86/mm.h      |   2 +-
 xen/include/asm-x86/percpu.h  |   6 ++
 xen/include/xen/grant_table.h |  24 +++++++-
 xen/include/xen/percpu.h      |   4 ++
 xen/include/xen/spinlock.h    | 115 ++++++++++++++++++++++++++++++++++++++
 12 files changed, 282 insertions(+), 67 deletions(-)
Signature · 1 line
1.7.12.4
#1 lore raw

PATCH v5 1/3 rwlock: Add per-cpu reader-writer lock infrastructure

Per-cpu read-write locks allow for the fast path read case to have low overhead by only setting/clearing a per-cpu variable for using the read lock. The per-cpu read fast path also avoids locked compare swap operations which can be particularly slow on coherent multi-socket systems, particularly if there is heavy usage of the read lock itself.

The per-cpu reader-writer lock uses a local variable to control the read lock fast path. This allows a writer to disable the fast path and ensures the readers switch to using the underlying read-write lock implementation instead of the per-cpu variable.

Once the writer has taken the write lock and disabled the fast path, it must poll the per-cpu variable for all CPU's which have entered the critical section for the specific read-write lock the writer is attempting to take. This design allows for a single per-cpu variable to be used for read/write locks belonging to seperate data structures. If a two or more different per-cpu read lock(s) are taken simultaneously then the per-cpu data structure is not used and the implementation takes the read lock of the underlying read-write lock, this behaviour is equivalent to the slow path in terms of performance. The per-cpu rwlock is not recursion safe for taking the per-cpu read lock because there is no recursion count variable, this is functionally equivalent to standard spin locks.

Slow path readers which are unblocked, set the per-cpu variable and drop the read lock. This simplifies the implementation and allows for fairness in the underlying read-write lock to be taken advantage of.

There is more overhead on the per-cpu write lock path due to checking each CPUs fast path per-cpu variable but this overhead is likely be hidden by the required delay of waiting for readers to exit the critical section. The loop is optimised to only iterate over the per-cpu data of active readers of the rwlock. The cpumask_t for tracking the active readers is stored in a single per-cpu data location and thus the write lock is not pre-emption safe. Therefore the per-cpu write lock can only be used with interrupts disabled.

--
Changes since v4:
- Use static inlines for the percpu_owner check

Changes since v3:
- Fix the ASSERTS for the percpu_owner check

Changes since v2:
- Remove stray hard tabs
- Added owner field to percpu_rwlock_t for debug builds. This allows for ASSERTs to be added which verify the correct global percpu structure is used for the local initialised percpu_rwlock lock

Changes since v1:
- Removed restriction on taking two or more different percpu rw locks simultaneously
- Moved fast-path/slow-path barrier to be per lock instead of global
- Created seperate percpu_rwlock_t type and added macros to initialise new type
- Added helper macros for using the percpu rwlock itself
- Moved active_readers cpumask off the stack and into a percpu variable

---
 xen/common/spinlock.c        |  45 +++++++++++++++++
 xen/include/asm-arm/percpu.h |   5 ++
 xen/include/asm-x86/percpu.h |   6 +++
 xen/include/xen/percpu.h     |   4 ++
 xen/include/xen/spinlock.h   | 115 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 175 insertions(+)
xen/common/spinlock.c +45 -0
index 7f89694..901c2b2 100644
@@ -10,6 +10,8 @@
 #include <asm/processor.h>
 #include <asm/atomic.h>
 
+static DEFINE_PER_CPU(cpumask_t, percpu_rwlock_readers);
+
 #ifndef NDEBUG
 
 static atomic_t spin_debug __read_mostly = ATOMIC_INIT(0);
@@ -492,6 +494,49 @@ int _rw_is_write_locked(rwlock_t *lock)
     return (lock->lock == RW_WRITE_FLAG); /* writer in critical section? */
 }
 
+void _percpu_write_lock(percpu_rwlock_t **per_cpudata,
+                percpu_rwlock_t *percpu_rwlock)
+{
+    unsigned int cpu;
+    cpumask_t *rwlock_readers = &this_cpu(percpu_rwlock_readers);
+
+    /* Validate the correct per_cpudata variable has been provided. */
+    _percpu_rwlock_owner_check(per_cpudata, percpu_rwlock);
+
+    /* 
+     * First take the write lock to protect against other writers or slow 
+     * path readers.
+     */
+    write_lock(&percpu_rwlock->rwlock);
+
+    /* Now set the global variable so that readers start using read_lock. */
+    percpu_rwlock->writer_activating = 1;
+    smp_mb();
+
+    /* Using a per cpu cpumask is only safe if there is no nesting. */
+    ASSERT(!in_irq());
+    cpumask_copy(rwlock_readers, &cpu_online_map);
+
+    /* Check if there are any percpu readers in progress on this rwlock. */
+    for ( ; ; )
+    {
+        for_each_cpu(cpu, rwlock_readers)
+        {
+            /* 
+             * Remove any percpu readers not contending on this rwlock
+             * from our check mask.
+             */
+            if ( per_cpu_ptr(per_cpudata, cpu) != percpu_rwlock )
+                __cpumask_clear_cpu(cpu, rwlock_readers);
+        }
+        /* Check if we've cleared all percpu readers from check mask. */
+        if ( cpumask_empty(rwlock_readers) )
+            break;
+        /* Give the coherency fabric a break. */
+        cpu_relax();
+    };
+}
+
 #ifdef LOCK_PROFILE
 
 struct lock_profile_anc {
xen/include/asm-arm/percpu.h +5 -0
index 71e7649..c308a56 100644
@@ -27,6 +27,11 @@ void percpu_init_areas(void);
 #define __get_cpu_var(var) \
     (*RELOC_HIDE(&per_cpu__##var, READ_SYSREG(TPIDR_EL2)))
 
+#define per_cpu_ptr(var, cpu)  \
+    (*RELOC_HIDE(&var, __per_cpu_offset[cpu]))
+#define __get_cpu_ptr(var) \
+    (*RELOC_HIDE(&var, READ_SYSREG(TPIDR_EL2)))
+
 #define DECLARE_PER_CPU(type, name) extern __typeof__(type) per_cpu__##name
 
 DECLARE_PER_CPU(unsigned int, cpu_id);
xen/include/asm-x86/percpu.h +6 -0
index 604ff0d..51562b9 100644
@@ -20,4 +20,10 @@ void percpu_init_areas(void);
 
 #define DECLARE_PER_CPU(type, name) extern __typeof__(type) per_cpu__##name
 
+#define __get_cpu_ptr(var) \
+    (*RELOC_HIDE(var, get_cpu_info()->per_cpu_offset))
+
+#define per_cpu_ptr(var, cpu)  \
+    (*RELOC_HIDE(var, __per_cpu_offset[cpu]))
+
 #endif /* __X86_PERCPU_H__ */
xen/include/xen/percpu.h +4 -0
index abe0b11..c896863 100644
@@ -16,6 +16,10 @@
 /* Preferred on Xen. Also see arch-defined per_cpu(). */
 #define this_cpu(var)    __get_cpu_var(var)
 
+#define this_cpu_ptr(ptr)    __get_cpu_ptr(ptr)
+
+#define get_per_cpu_var(var)  (per_cpu__##var)
+
 /* Linux compatibility. */
 #define get_cpu_var(var) this_cpu(var)
 #define put_cpu_var(var)
xen/include/xen/spinlock.h +115 -0

125 lines not on this page · raw message

Signature · 1 line
1.7.12.4
Reviewed-by on v5 1/3
#2 lore raw

Re: [PATCHv5 1/3] rwlock: Add per-cpu reader-writer lock infrastructure

Quoted 40 lines
Quoted 1 line
Quoted 1 line · On 18.12.15 at 17:08, <[email protected]> wrote:

Per-cpu read-write locks allow for the fast path read case to have low overhead by only setting/clearing a per-cpu variable for using the read lock. The per-cpu read fast path also avoids locked compare swap operations which can be particularly slow on coherent multi-socket systems, particularly if there is heavy usage of the read lock itself.

The per-cpu reader-writer lock uses a local variable to control the read lock fast path. This allows a writer to disable the fast path and ensures the readers switch to using the underlying read-write lock implementation instead of the per-cpu variable.

Once the writer has taken the write lock and disabled the fast path, it must poll the per-cpu variable for all CPU's which have entered the critical section for the specific read-write lock the writer is attempting to take. This design allows for a single per-cpu variable to be used for read/write locks belonging to seperate data structures. If a two or more different per-cpu read lock(s) are taken simultaneously then the per-cpu data structure is not used and the implementation takes the read lock of the underlying read-write lock, this behaviour is equivalent to the slow path in terms of performance. The per-cpu rwlock is not recursion safe for taking the per-cpu read lock because there is no recursion count variable, this is functionally equivalent to standard spin locks.

Slow path readers which are unblocked, set the per-cpu variable and drop the read lock. This simplifies the implementation and allows for fairness in the underlying read-write lock to be taken advantage of.

There is more overhead on the per-cpu write lock path due to checking each CPUs fast path per-cpu variable but this overhead is likely be hidden by the required delay of waiting for readers to exit the critical section. The loop is optimised to only iterate over the per-cpu data of active readers of the rwlock. The cpumask_t for tracking the active readers is stored in a single per-cpu data location and thus the write lock is not pre-emption safe. Therefore the per-cpu write lock can only be used with interrupts disabled.

Quoted 1 line
#3 lore raw

Re: [PATCHv5 1/3] rwlock: Add per-cpu reader-writer lock infrastructure

In reply to Malcolm Crossley
Quoted 214 lines · Malcolm Crossley

Per-cpu read-write locks allow for the fast path read case to have low overhead by only setting/clearing a per-cpu variable for using the read lock. The per-cpu read fast path also avoids locked compare swap operations which can be particularly slow on coherent multi-socket systems, particularly if there is heavy usage of the read lock itself.

The per-cpu reader-writer lock uses a local variable to control the read lock fast path. This allows a writer to disable the fast path and ensures the readers switch to using the underlying read-write lock implementation instead of the per-cpu variable.

Once the writer has taken the write lock and disabled the fast path, it must poll the per-cpu variable for all CPU's which have entered the critical section for the specific read-write lock the writer is attempting to take. This design allows for a single per-cpu variable to be used for read/write locks belonging to seperate data structures. If a two or more different per-cpu read lock(s) are taken simultaneously then the per-cpu data structure is not used and the implementation takes the read lock of the underlying read-write lock, this behaviour is equivalent to the slow path in terms of performance. The per-cpu rwlock is not recursion safe for taking the per-cpu read lock because there is no recursion count variable, this is functionally equivalent to standard spin locks.

Slow path readers which are unblocked, set the per-cpu variable and drop the read lock. This simplifies the implementation and allows for fairness in the underlying read-write lock to be taken advantage of.

There is more overhead on the per-cpu write lock path due to checking each CPUs fast path per-cpu variable but this overhead is likely be hidden by the required delay of waiting for readers to exit the critical section. The loop is optimised to only iterate over the per-cpu data of active readers of the rwlock. The cpumask_t for tracking the active readers is stored in a single per-cpu data location and thus the write lock is not pre-emption safe. Therefore the per-cpu write lock can only be used with interrupts disabled.

--
Changes since v4:
- Use static inlines for the percpu_owner check

Changes since v3:
- Fix the ASSERTS for the percpu_owner check

Changes since v2:
- Remove stray hard tabs
- Added owner field to percpu_rwlock_t for debug builds. This allows for ASSERTs to be added which verify the correct global percpu structure is used for the local initialised percpu_rwlock lock

Changes since v1:
- Removed restriction on taking two or more different percpu rw locks simultaneously
- Moved fast-path/slow-path barrier to be per lock instead of global
- Created seperate percpu_rwlock_t type and added macros to initialise new type
- Added helper macros for using the percpu rwlock itself
- Moved active_readers cpumask off the stack and into a percpu variable

---
 xen/common/spinlock.c        |  45 +++++++++++++++++
 xen/include/asm-arm/percpu.h |   5 ++
 xen/include/asm-x86/percpu.h |   6 +++
 xen/include/xen/percpu.h     |   4 ++
 xen/include/xen/spinlock.h   | 115 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 175 insertions(+)
xen/common/spinlock.c +45 -0
index 7f89694..901c2b2 100644
@@ -10,6 +10,8 @@
 #include <asm/processor.h>
 #include <asm/atomic.h>
 
+static DEFINE_PER_CPU(cpumask_t, percpu_rwlock_readers);
+
 #ifndef NDEBUG
 
 static atomic_t spin_debug __read_mostly = ATOMIC_INIT(0);
@@ -492,6 +494,49 @@ int _rw_is_write_locked(rwlock_t *lock)
     return (lock->lock == RW_WRITE_FLAG); /* writer in critical section? */
 }
 
+void _percpu_write_lock(percpu_rwlock_t **per_cpudata,
+                percpu_rwlock_t *percpu_rwlock)
+{
+    unsigned int cpu;
+    cpumask_t *rwlock_readers = &this_cpu(percpu_rwlock_readers);
+
+    /* Validate the correct per_cpudata variable has been provided. */
+    _percpu_rwlock_owner_check(per_cpudata, percpu_rwlock);
+
+    /* 
+     * First take the write lock to protect against other writers or slow 
+     * path readers.
+     */
+    write_lock(&percpu_rwlock->rwlock);
+
+    /* Now set the global variable so that readers start using read_lock. */
+    percpu_rwlock->writer_activating = 1;
+    smp_mb();
+
+    /* Using a per cpu cpumask is only safe if there is no nesting. */
+    ASSERT(!in_irq());
+    cpumask_copy(rwlock_readers, &cpu_online_map);
+
+    /* Check if there are any percpu readers in progress on this rwlock. */
+    for ( ; ; )
+    {
+        for_each_cpu(cpu, rwlock_readers)
+        {
+            /* 
+             * Remove any percpu readers not contending on this rwlock
+             * from our check mask.
+             */
+            if ( per_cpu_ptr(per_cpudata, cpu) != percpu_rwlock )
+                __cpumask_clear_cpu(cpu, rwlock_readers);
+        }
+        /* Check if we've cleared all percpu readers from check mask. */
+        if ( cpumask_empty(rwlock_readers) )
+            break;
+        /* Give the coherency fabric a break. */
+        cpu_relax();
+    };
+}
+
 #ifdef LOCK_PROFILE
 
 struct lock_profile_anc {
xen/include/asm-arm/percpu.h +5 -0
index 71e7649..c308a56 100644
@@ -27,6 +27,11 @@ void percpu_init_areas(void);
 #define __get_cpu_var(var) \
     (*RELOC_HIDE(&per_cpu__##var, READ_SYSREG(TPIDR_EL2)))
 
+#define per_cpu_ptr(var, cpu)  \
+    (*RELOC_HIDE(&var, __per_cpu_offset[cpu]))
+#define __get_cpu_ptr(var) \
+    (*RELOC_HIDE(&var, READ_SYSREG(TPIDR_EL2)))
+
 #define DECLARE_PER_CPU(type, name) extern __typeof__(type) per_cpu__##name
 
 DECLARE_PER_CPU(unsigned int, cpu_id);
xen/include/asm-x86/percpu.h +6 -0
index 604ff0d..51562b9 100644
@@ -20,4 +20,10 @@ void percpu_init_areas(void);
 
 #define DECLARE_PER_CPU(type, name) extern __typeof__(type) per_cpu__##name
 
+#define __get_cpu_ptr(var) \
+    (*RELOC_HIDE(var, get_cpu_info()->per_cpu_offset))
+
+#define per_cpu_ptr(var, cpu)  \
+    (*RELOC_HIDE(var, __per_cpu_offset[cpu]))
+
 #endif /* __X86_PERCPU_H__ */
xen/include/xen/percpu.h +4 -0
index abe0b11..c896863 100644
@@ -16,6 +16,10 @@
 /* Preferred on Xen. Also see arch-defined per_cpu(). */
 #define this_cpu(var)    __get_cpu_var(var)
 
+#define this_cpu_ptr(ptr)    __get_cpu_ptr(ptr)
+
+#define get_per_cpu_var(var)  (per_cpu__##var)
+
 /* Linux compatibility. */
 #define get_cpu_var(var) this_cpu(var)
 #define put_cpu_var(var)
xen/include/xen/spinlock.h +20 -0
index fb0438e..7338328 100644
@@ -3,6 +3,7 @@
 
 #include <asm/system.h>
 #include <asm/spinlock.h>
+#include <asm/types.h>
 
 #ifndef NDEBUG
 struct lock_debug {
@@ -261,4 +262,118 @@ int _rw_is_write_locked(rwlock_t *lock);
 #define rw_is_locked(l)               _rw_is_locked(l)
 #define rw_is_write_locked(l)         _rw_is_write_locked(l)
 
+typedef struct percpu_rwlock percpu_rwlock_t;
+
+struct percpu_rwlock {
+    rwlock_t            rwlock;
+    bool_t              writer_activating;
+#ifndef NDEBUG
+    percpu_rwlock_t     **percpu_owner;
+#endif
+};
+
+#ifndef NDEBUG
+#define PERCPU_RW_LOCK_UNLOCKED(owner) { RW_LOCK_UNLOCKED, 0, owner }
+static inline void _percpu_rwlock_owner_check(percpu_rwlock_t **per_cpudata,
+                                         percpu_rwlock_t *percpu_rwlock)
+{
+    ASSERT(per_cpudata == percpu_rwlock->percpu_owner);
+}
+#else
+#define PERCPU_RW_LOCK_UNLOCKED(owner) { RW_LOCK_UNLOCKED, 0 }

xen/include/xen/spinlock.h int _rw_is_write_locked(rwlock_t *lock); · line 293

Quoted 10 lines
xen/include/xen/spinlock.h +10 -0
+#define _percpu_rwlock_owner_check(data, lock) ((void)0)
+#endif
+
+#define DEFINE_PERCPU_RWLOCK_RESOURCE(l, owner) \
+    percpu_rwlock_t l = PERCPU_RW_LOCK_UNLOCKED(&get_per_cpu_var(owner))
+#define percpu_rwlock_resource_init(l, owner) \
+    (*(l) = (percpu_rwlock_t)PERCPU_RW_LOCK_UNLOCKED(&get_per_cpu_var(owner)))
+
+static inline void _percpu_read_lock(percpu_rwlock_t **per_cpudata,
+                                         percpu_rwlock_t *percpu_rwlock)

Is there a particular reason you chose to only use the "owner" value in the struct to verify that the "per_cpudata" argument passed matched the one you expected, rather than just getting rid of the "per_cpudata" argument altogether and always using the pointer in the struct?

(i.e., _percpu_read_lock(percpu_rwlock_t *percpu_rwlock) { ... per_cpudata = percpu_rwlock->percpu_owner; ... })

I'm not an expert in this sort of micro-optimization, but it seems like you're trading off storing a pointer in your rwlock struct for storing a pointer at every call site. Since you have to read writer_activating for every lock or unlock anyway, it doesn't seem like you'd actually be saving that many memory fetches; but having only one copy in the cache, rather than one copy per call site, would on the whole reduce both the cache footprint and the total memory used (if only by a few bytes).

It also makes the code cleaner to have only one argument, rather than two which must match; but since in all the places you use it you end up using a wrapper to give you a single argument anyway, I don't think that matters in this case. (i.e., if there's a good reason for having it at the call site instead if in the struct, I'm fine with this approach).

Everything else looks good, thanks.

-George

#4 lore raw

Re: [PATCHv5 1/3] rwlock: Add per-cpu reader-writer lock infrastructure

Quoted 19 lines · George Dunlap
Quoted 18 lines · Malcolm Crossley

<snip>

xen/include/xen/spinlock.h +17 -0
+
+#ifndef NDEBUG
+#define PERCPU_RW_LOCK_UNLOCKED(owner) { RW_LOCK_UNLOCKED, 0, owner }
+static inline void _percpu_rwlock_owner_check(percpu_rwlock_t **per_cpudata,
+                                         percpu_rwlock_t *percpu_rwlock)
+{
+    ASSERT(per_cpudata == percpu_rwlock->percpu_owner);
+}
+#else
+#define PERCPU_RW_LOCK_UNLOCKED(owner) { RW_LOCK_UNLOCKED, 0 }
+#define _percpu_rwlock_owner_check(data, lock) ((void)0)
+#endif
+
+#define DEFINE_PERCPU_RWLOCK_RESOURCE(l, owner) \
+    percpu_rwlock_t l = PERCPU_RW_LOCK_UNLOCKED(&get_per_cpu_var(owner))
+#define percpu_rwlock_resource_init(l, owner) \
+    (*(l) = (percpu_rwlock_t)PERCPU_RW_LOCK_UNLOCKED(&get_per_cpu_var(owner)))
Quoted 8 lines

xen/include/xen/spinlock.h int _rw_is_write_locked(rwlock_t *lock); · line 293

Quoted 3 lines
xen/include/xen/spinlock.h +3 -0
+
+static inline void _percpu_read_lock(percpu_rwlock_t **per_cpudata,
+                                         percpu_rwlock_t *percpu_rwlock)

Is there a particular reason you chose to only use the "owner" value in the struct to verify that the "per_cpudata" argument passed matched the one you expected, rather than just getting rid of the "per_cpudata" argument altogether and always using the pointer in the struct?

Initially I was aiming to add percpu aspects to the rwlock without increasing the size of the rwlock structure itself, this was to keep data cache usage and memory allocations the same.
It became clear that having a global writer_activating barrier would cause the read_lock to enter the slow path far too often. So I put the writer_activating variable in the percpu_rwlock_t, as writer_activating is just a bool then the additional data overhead should be small. Always having a 8 byte pointer may add a lot of overhead to data structures contain multiple rwlocks and thus cause additional allocation overhead.

Quoted 8 lines

(i.e., _percpu_read_lock(percpu_rwlock_t *percpu_rwlock) { ... per_cpudata = percpu_rwlock->percpu_owner; ... })

I'm not an expert in this sort of micro-optimization, but it seems like you're trading off storing a pointer in your rwlock struct for storing a pointer at every call site. Since you have to read writer_activating for every lock or unlock anyway,

writer_activating is not read on the read_unlock path. As these are rwlocks then I'm assuming the read lock/unlock paths are more critical for performance. So I'd prefer to not do a read of the percpu_rwlock structure if it's not required (i.e. on the read unlock path)
Furthermore, the single byte for the writer_activating variable is likely to have been read into cache by accesses to other parts of the data structure near the percpu_rwlock_t. If we add additional 8 bytes to the percpu_rwlock_t then this may not happen and it may also adjust the cache line alignment aswell.

Quoted 4 lines

it doesn't seem like you'd actually be
saving that many memory fetches; but having only one copy in the cache, rather than one copy per call site, would on the whole reduce both the cache footprint and the total memory used (if only by a few bytes).

If you put the owner pointer in the percpu_rwlock_t then wouldn't you have a copy per instance of percpu_rwlock_t? Surely this would use more cache than the handful of call site references to a global variable.

Quoted 6 lines

It also makes the code cleaner to have only one argument, rather than two which must match; but since in all the places you use it you end up using a wrapper to give you a single argument anyway, I don't think that matters in this case. (i.e., if there's a good reason for having it at the call site instead if in the struct, I'm fine with this approach).

If you agree with my reasoning for the cache overhead and performance of the read unlock path being better with passing the percpu_data as an argument then I propose we keep the patches as is.

Quoted 5 lines

Everything else looks good, thanks.

-George

Thanks for the review and sorry about the delayed reply.

Malcolm

#5 lore raw

Re: [PATCHv5 1/3] rwlock: Add per-cpu reader-writer lock infrastructure

Quoted 72 lines · Malcolm Crossley
Quoted 19 lines · George Dunlap
Quoted 18 lines · Malcolm Crossley

<snip>

xen/include/xen/spinlock.h +17 -0
+
+#ifndef NDEBUG
+#define PERCPU_RW_LOCK_UNLOCKED(owner) { RW_LOCK_UNLOCKED, 0, owner }
+static inline void _percpu_rwlock_owner_check(percpu_rwlock_t **per_cpudata,
+                                         percpu_rwlock_t *percpu_rwlock)
+{
+    ASSERT(per_cpudata == percpu_rwlock->percpu_owner);
+}
+#else
+#define PERCPU_RW_LOCK_UNLOCKED(owner) { RW_LOCK_UNLOCKED, 0 }
+#define _percpu_rwlock_owner_check(data, lock) ((void)0)
+#endif
+
+#define DEFINE_PERCPU_RWLOCK_RESOURCE(l, owner) \
+    percpu_rwlock_t l = PERCPU_RW_LOCK_UNLOCKED(&get_per_cpu_var(owner))
+#define percpu_rwlock_resource_init(l, owner) \
+    (*(l) = (percpu_rwlock_t)PERCPU_RW_LOCK_UNLOCKED(&get_per_cpu_var(owner)))
Quoted 8 lines

xen/include/xen/spinlock.h int _rw_is_write_locked(rwlock_t *lock); · line 293

Quoted 3 lines
xen/include/xen/spinlock.h +3 -0
+
+static inline void _percpu_read_lock(percpu_rwlock_t **per_cpudata,
+                                         percpu_rwlock_t *percpu_rwlock)

Is there a particular reason you chose to only use the "owner" value in the struct to verify that the "per_cpudata" argument passed matched the one you expected, rather than just getting rid of the "per_cpudata" argument altogether and always using the pointer in the struct?

Initially I was aiming to add percpu aspects to the rwlock without increasing the size of the rwlock structure itself, this was to keep data cache usage and memory allocations the same.
It became clear that having a global writer_activating barrier would cause the read_lock to enter the slow path far too often. So I put the writer_activating variable in the percpu_rwlock_t, as writer_activating is just a bool then the additional data overhead should be small. Always having a 8 byte pointer may add a lot of overhead to data structures contain multiple rwlocks and thus cause additional allocation overhead.

Quoted 8 lines

(i.e., _percpu_read_lock(percpu_rwlock_t *percpu_rwlock) { ... per_cpudata = percpu_rwlock->percpu_owner; ... })

I'm not an expert in this sort of micro-optimization, but it seems like you're trading off storing a pointer in your rwlock struct for storing a pointer at every call site. Since you have to read writer_activating for every lock or unlock anyway,

writer_activating is not read on the read_unlock path. As these are rwlocks then I'm assuming the read lock/unlock paths are more critical for performance. So I'd prefer to not do a read of the percpu_rwlock structure if it's not required (i.e. on the read unlock path)
Furthermore, the single byte for the writer_activating variable is likely to have been read into cache by accesses to other parts of the data structure near the percpu_rwlock_t. If we add additional 8 bytes to the percpu_rwlock_t then this may not happen and it may also adjust the cache line alignment aswell.

Quoted 4 lines

it doesn't seem like you'd actually be
saving that many memory fetches; but having only one copy in the cache, rather than one copy per call site, would on the whole reduce both the cache footprint and the total memory used (if only by a few bytes).

If you put the owner pointer in the percpu_rwlock_t then wouldn't you have a copy per instance of percpu_rwlock_t? Surely this would use more cache than the handful of call site references to a global variable.

Quoted 6 lines

It also makes the code cleaner to have only one argument, rather than two which must match; but since in all the places you use it you end up using a wrapper to give you a single argument anyway, I don't think that matters in this case. (i.e., if there's a good reason for having it at the call site instead if in the struct, I'm fine with this approach).

Quoted 4 lines

If you agree with my reasoning for the cache overhead and performance of the read unlock path being better with passing the percpu_data as an argument then I propose we keep the patches as is.

Ping? I believe this is the last point of discussion before the patches can go in.

Malcolm

Quoted 7 lines
Quoted 5 lines

Everything else looks good, thanks.

-George

#6 lore raw

Re: [PATCHv5 1/3] rwlock: Add per-cpu reader-writer lock infrastructure

Quoted 73 lines · Malcolm Crossley
Quoted 72 lines · Malcolm Crossley
Quoted 19 lines · George Dunlap
Quoted 18 lines · Malcolm Crossley

<snip>

xen/include/xen/spinlock.h +17 -0
+
+#ifndef NDEBUG
+#define PERCPU_RW_LOCK_UNLOCKED(owner) { RW_LOCK_UNLOCKED, 0, owner }
+static inline void _percpu_rwlock_owner_check(percpu_rwlock_t **per_cpudata,
+                                         percpu_rwlock_t *percpu_rwlock)
+{
+    ASSERT(per_cpudata == percpu_rwlock->percpu_owner);
+}
+#else
+#define PERCPU_RW_LOCK_UNLOCKED(owner) { RW_LOCK_UNLOCKED, 0 }
+#define _percpu_rwlock_owner_check(data, lock) ((void)0)
+#endif
+
+#define DEFINE_PERCPU_RWLOCK_RESOURCE(l, owner) \
+    percpu_rwlock_t l = PERCPU_RW_LOCK_UNLOCKED(&get_per_cpu_var(owner))
+#define percpu_rwlock_resource_init(l, owner) \
+    (*(l) = (percpu_rwlock_t)PERCPU_RW_LOCK_UNLOCKED(&get_per_cpu_var(owner)))
Quoted 8 lines

xen/include/xen/spinlock.h int _rw_is_write_locked(rwlock_t *lock); · line 293

Quoted 3 lines
xen/include/xen/spinlock.h +3 -0
+
+static inline void _percpu_read_lock(percpu_rwlock_t **per_cpudata,
+                                         percpu_rwlock_t *percpu_rwlock)

Is there a particular reason you chose to only use the "owner" value in the struct to verify that the "per_cpudata" argument passed matched the one you expected, rather than just getting rid of the "per_cpudata" argument altogether and always using the pointer in the struct?

Initially I was aiming to add percpu aspects to the rwlock without increasing the size of the rwlock structure itself, this was to keep data cache usage and memory allocations the same.
It became clear that having a global writer_activating barrier would cause the read_lock to enter the slow path far too often. So I put the writer_activating variable in the percpu_rwlock_t, as writer_activating is just a bool then the additional data overhead should be small. Always having a 8 byte pointer may add a lot of overhead to data structures contain multiple rwlocks and thus cause additional allocation overhead.

Quoted 8 lines

(i.e., _percpu_read_lock(percpu_rwlock_t *percpu_rwlock) { ... per_cpudata = percpu_rwlock->percpu_owner; ... })

I'm not an expert in this sort of micro-optimization, but it seems like you're trading off storing a pointer in your rwlock struct for storing a pointer at every call site. Since you have to read writer_activating for every lock or unlock anyway,

writer_activating is not read on the read_unlock path. As these are rwlocks then I'm assuming the read lock/unlock paths are more critical for performance. So I'd prefer to not do a read of the percpu_rwlock structure if it's not required (i.e. on the read unlock path)
Furthermore, the single byte for the writer_activating variable is likely to have been read into cache by accesses to other parts of the data structure near the percpu_rwlock_t. If we add additional 8 bytes to the percpu_rwlock_t then this may not happen and it may also adjust the cache line alignment aswell.

Quoted 4 lines

it doesn't seem like you'd actually be
saving that many memory fetches; but having only one copy in the cache, rather than one copy per call site, would on the whole reduce both the cache footprint and the total memory used (if only by a few bytes).

If you put the owner pointer in the percpu_rwlock_t then wouldn't you have a copy per instance of percpu_rwlock_t? Surely this would use more cache than the handful of call site references to a global variable.

Quoted 6 lines

It also makes the code cleaner to have only one argument, rather than two which must match; but since in all the places you use it you end up using a wrapper to give you a single argument anyway, I don't think that matters in this case. (i.e., if there's a good reason for having it at the call site instead if in the struct, I'm fine with this approach).

Quoted 5 lines
Quoted 4 lines

If you agree with my reasoning for the cache overhead and performance of the read unlock path being better with passing the percpu_data as an argument then I propose we keep the patches as is.

Ping? I believe this is the last point of discussion before the patches can go in.

Sorry -- I did skim this, and intended to give it another once-over last week, but some other stuff came up. I should get a chance to take a look at it sometime this week.

-George

Reviewed-by
#7 lore raw

Re: [PATCHv5 1/3] rwlock: Add per-cpu reader-writer lock infrastructure

In reply to Malcolm Crossley
Quoted 72 lines · Malcolm Crossley
Quoted 19 lines · George Dunlap
Quoted 18 lines · Malcolm Crossley

<snip>

xen/include/xen/spinlock.h +17 -0
+
+#ifndef NDEBUG
+#define PERCPU_RW_LOCK_UNLOCKED(owner) { RW_LOCK_UNLOCKED, 0, owner }
+static inline void _percpu_rwlock_owner_check(percpu_rwlock_t **per_cpudata,
+                                         percpu_rwlock_t *percpu_rwlock)
+{
+    ASSERT(per_cpudata == percpu_rwlock->percpu_owner);
+}
+#else
+#define PERCPU_RW_LOCK_UNLOCKED(owner) { RW_LOCK_UNLOCKED, 0 }
+#define _percpu_rwlock_owner_check(data, lock) ((void)0)
+#endif
+
+#define DEFINE_PERCPU_RWLOCK_RESOURCE(l, owner) \
+    percpu_rwlock_t l = PERCPU_RW_LOCK_UNLOCKED(&get_per_cpu_var(owner))
+#define percpu_rwlock_resource_init(l, owner) \
+    (*(l) = (percpu_rwlock_t)PERCPU_RW_LOCK_UNLOCKED(&get_per_cpu_var(owner)))
Quoted 8 lines

xen/include/xen/spinlock.h int _rw_is_write_locked(rwlock_t *lock); · line 293

Quoted 3 lines
xen/include/xen/spinlock.h +3 -0
+
+static inline void _percpu_read_lock(percpu_rwlock_t **per_cpudata,
+                                         percpu_rwlock_t *percpu_rwlock)

Is there a particular reason you chose to only use the "owner" value in the struct to verify that the "per_cpudata" argument passed matched the one you expected, rather than just getting rid of the "per_cpudata" argument altogether and always using the pointer in the struct?

Initially I was aiming to add percpu aspects to the rwlock without increasing the size of the rwlock structure itself, this was to keep data cache usage and memory allocations the same.
It became clear that having a global writer_activating barrier would cause the read_lock to enter the slow path far too often. So I put the writer_activating variable in the percpu_rwlock_t, as writer_activating is just a bool then the additional data overhead should be small. Always having a 8 byte pointer may add a lot of overhead to data structures contain multiple rwlocks and thus cause additional allocation overhead.

Quoted 8 lines

(i.e., _percpu_read_lock(percpu_rwlock_t *percpu_rwlock) { ... per_cpudata = percpu_rwlock->percpu_owner; ... })

I'm not an expert in this sort of micro-optimization, but it seems like you're trading off storing a pointer in your rwlock struct for storing a pointer at every call site. Since you have to read writer_activating for every lock or unlock anyway,

writer_activating is not read on the read_unlock path. As these are rwlocks then I'm assuming the read lock/unlock paths are more critical for performance. So I'd prefer to not do a read of the percpu_rwlock structure if it's not required (i.e. on the read unlock path)
Furthermore, the single byte for the writer_activating variable is likely to have been read into cache by accesses to other parts of the data structure near the percpu_rwlock_t. If we add additional 8 bytes to the percpu_rwlock_t then this may not happen and it may also adjust the cache line alignment aswell.

Quoted 4 lines

it doesn't seem like you'd actually be
saving that many memory fetches; but having only one copy in the cache, rather than one copy per call site, would on the whole reduce both the cache footprint and the total memory used (if only by a few bytes).

If you put the owner pointer in the percpu_rwlock_t then wouldn't you have a copy per instance of percpu_rwlock_t? Surely this would use more cache than the handful of call site references to a global variable.

Quoted 6 lines

It also makes the code cleaner to have only one argument, rather than two which must match; but since in all the places you use it you end up using a wrapper to give you a single argument anyway, I don't think that matters in this case. (i.e., if there's a good reason for having it at the call site instead if in the struct, I'm fine with this approach).

Quoted 3 lines

If you agree with my reasoning for the cache overhead and performance of the read unlock path being better with passing the percpu_data as an argument then I propose we keep the patches as is.

Yeah, I think you convinced me:

Thanks!
-George

#8 lore raw

Re: [PATCHv5 1/3] rwlock: Add per-cpu reader-writer lock infrastructure

In reply to Malcolm Crossley
Quoted 13 lines · Malcolm Crossley
xen/include/asm-arm/percpu.h +0 -0
index 71e7649..c308a56 100644
@@ -27,6 +27,11 @@ void percpu_init_areas(void);
  #define __get_cpu_var(var) \
      (*RELOC_HIDE(&per_cpu__##var, READ_SYSREG(TPIDR_EL2)))

xen/include/asm-arm/percpu.h +5 -0
+#define per_cpu_ptr(var, cpu)  \
+    (*RELOC_HIDE(&var, __per_cpu_offset[cpu]))
+#define __get_cpu_ptr(var) \
+    (*RELOC_HIDE(&var, READ_SYSREG(TPIDR_EL2)))
+
Quoted 3 lines

 #define DECLARE_PER_CPU(type, name) extern __typeof__(type) per_cpu__##name

For ARM: Acked-by :Ian Campbell <[email protected]>

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw

PATCH v5 2/3 grant_table: convert grant table rwlock to percpu rwlock

In reply to Malcolm Crossley

The per domain grant table read lock suffers from significant contention when performance multi-queue block or network IO due to the parallel grant map/unmaps/copies occurring on the DomU's grant table.

On multi-socket systems, the contention results in the locked compare swap operation failing frequently which results in a tight loop of retries of the compare swap operation. As the coherency fabric can only support a specific rate of compare swap operations for a particular data location then taking the read lock itself becomes a bottleneck for grant operations.

Standard rwlock performance of a single VIF VM-VM transfer with 16 queues configured was limited to approximately 15 gbit/s on a 2 socket Haswell-EP host.

Percpu rwlock performance with the same configuration is approximately 48 gbit/s.

Oprofile was used to determine the initial overhead of the read-write locks and to confirm the overhead was dramatically reduced by the percpu rwlocks.

--
Changes since v4:
- Rename grant table rwlock wrappers and use grant table pointer as argument
Changes since v3:
- None
Changes since v2:
- Switched to using wrappers for taking percpu rwlock
- Added percpu structure pointer to percpu rwlock initialisation
- Added comment on removal of ASSERTS for grant table rw_is_locked()
Changes since v1:
- Used new macros provided in updated percpu rwlock v2 patch
- Converted grant table rwlock_t to percpu_rwlock_t
- Patched a missed grant table rwlock_t usage site

---
 xen/arch/arm/mm.c             |   4 +-
 xen/arch/x86/mm.c             |   4 +-
 xen/common/grant_table.c      | 126 +++++++++++++++++++++++-------------------
 xen/include/xen/grant_table.h |  24 +++++++-
 4 files changed, 97 insertions(+), 61 deletions(-)
xen/arch/arm/mm.c +2 -2
index 47bfb27..81f9e2e 100644
@@ -1055,7 +1055,7 @@ int xenmem_add_to_physmap_one(
     switch ( space )
     {
     case XENMAPSPACE_grant_table:
-        write_lock(&d->grant_table->lock);
+        grant_write_lock(d->grant_table);
 
         if ( d->grant_table->gt_version == 0 )
             d->grant_table->gt_version = 1;
@@ -1085,7 +1085,7 @@ int xenmem_add_to_physmap_one(
 
         t = p2m_ram_rw;
 
-        write_unlock(&d->grant_table->lock);
+        grant_write_unlock(d->grant_table);
         break;
     case XENMAPSPACE_shared_info:
         if ( idx != 0 )
xen/arch/x86/mm.c +2 -2
index 92df36f..4faeab2 100644
@@ -4654,7 +4654,7 @@ int xenmem_add_to_physmap_one(
                 mfn = virt_to_mfn(d->shared_info);
             break;
         case XENMAPSPACE_grant_table:
-            write_lock(&d->grant_table->lock);
+            grant_write_lock(d->grant_table);
 
             if ( d->grant_table->gt_version == 0 )
                 d->grant_table->gt_version = 1;
@@ -4676,7 +4676,7 @@ int xenmem_add_to_physmap_one(
                     mfn = virt_to_mfn(d->grant_table->shared_raw[idx]);
             }
 
-            write_unlock(&d->grant_table->lock);
+            grant_write_unlock(d->grant_table);
             break;
         case XENMAPSPACE_gmfn_range:
         case XENMAPSPACE_gmfn:
xen/common/grant_table.c +70 -56

431 lines not on this page · raw message

xen/include/xen/grant_table.h +23 -1
index 1c29cee..b4f064e 100644
@@ -51,13 +51,15 @@
 /* The maximum size of a grant table. */
 extern unsigned int max_grant_frames;
 
+DECLARE_PERCPU_RWLOCK_GLOBAL(grant_rwlock);
+
 /* Per-domain grant information. */
 struct grant_table {
     /*
      * Lock protecting updates to grant table state (version, active
      * entry list, etc.)
      */
-    rwlock_t              lock;
+    percpu_rwlock_t       lock;
     /* Table size. Number of frames shared with guest */
     unsigned int          nr_grant_frames;
     /* Shared grant table (see include/public/grant_table.h). */
@@ -82,6 +84,26 @@ struct grant_table {
     unsigned              gt_version;
 };
 
+static inline void grant_read_lock(struct grant_table *gt)
+{
+    percpu_read_lock(grant_rwlock, &gt->lock);
+}
+
+static inline void grant_read_unlock(struct grant_table *gt)
+{
+    percpu_read_unlock(grant_rwlock, &gt->lock);
+}
+
+static inline void grant_write_lock(struct grant_table *gt)
+{
+    percpu_write_lock(grant_rwlock, &gt->lock);
+}
+
+static inline void grant_write_unlock(struct grant_table *gt)
+{
+    percpu_write_unlock(grant_rwlock, &gt->lock);
+}
+
 /* Create/destroy per-domain grant table context. */
 int grant_table_create(
     struct domain *d);
Signature · 1 line
1.7.12.4
Reviewed-by on v5 2/3
#10 lore raw

Re: [PATCHv5 2/3] grant_table: convert grant table rwlock to percpu rwlock

Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 18.12.15 at 17:08, <[email protected]> wrote:

The per domain grant table read lock suffers from significant contention when performance multi-queue block or network IO due to the parallel grant map/unmaps/copies occurring on the DomU's grant table.

On multi-socket systems, the contention results in the locked compare swap operation failing frequently which results in a tight loop of retries of the compare swap operation. As the coherency fabric can only support a specific rate of compare swap operations for a particular data location then taking the read lock itself becomes a bottleneck for grant operations.

Standard rwlock performance of a single VIF VM-VM transfer with 16 queues configured was limited to approximately 15 gbit/s on a 2 socket Haswell-EP host.

Quoted 7 lines

Percpu rwlock performance with the same configuration is approximately 48 gbit/s.

Oprofile was used to determine the initial overhead of the read-write locks and to confirm the overhead was dramatically reduced by the percpu rwlocks.

#11 lore raw

Re: [PATCHv5 2/3] grant_table: convert grant table rwlock to percpu rwlock

In reply to Malcolm Crossley
Quoted 2 lines · Malcolm Crossley


 xen/arch/arm/mm.c             |   4 +-

[...]

Quoted 14 lines
xen/arch/arm/mm.c +0 -0
index 47bfb27..81f9e2e 100644
@@ -1055,7 +1055,7 @@ int xenmem_add_to_physmap_one(
      switch ( space )
      {

     case XENMAPSPACE_grant_table:
-        write_lock(&d->grant_table->lock);
+        grant_write_lock(d->grant_table);

         if ( d->grant_table->gt_version == 0 )
             d->grant_table->gt_version = 1;

xen/arch/arm/mm.c +0 -0
@@ -1085,7 +1085,7 @@ int xenmem_add_to_physmap_one(
Quoted 5 lines


         t = p2m_ram_rw;

-        write_unlock(&d->grant_table->lock);
+        grant_write_unlock(d->grant_table);

ARML Acked-by: Ian Campbell <[email protected]>
_______________________________________________
Xen-devel mailing list [email protected]
http://lists.xen.org/xen-devel

#12 lore raw

PATCH v5 3/3 p2m: convert p2m rwlock to percpu rwlock

In reply to Malcolm Crossley

The per domain p2m read lock suffers from significant contention when performance multi-queue block or network IO due to the parallel grant map/unmaps/copies occuring on the DomU's p2m.

On multi-socket systems, the contention results in the locked compare swap operation failing frequently which results in a tight loop of retries of the compare swap operation. As the coherency fabric can only support a specific rate of compare swap operations for a particular data location then taking the read lock itself becomes a bottleneck for p2m operations.

Percpu rwlock p2m performance with the same configuration is approximately 64 gbit/s vs the 48 gbit/s with grant table percpu rwlocks only.

Oprofile was used to determine the initial overhead of the read-write locks and to confirm the overhead was dramatically reduced by the percpu rwlocks.

Note: altp2m users will not achieve a gain if they take an altp2m read lock simultaneously with the main p2m lock.

--
Changes since v4:
- None
Changes since v3:
- None
Changes since v2
- Updated local percpu rwlock initialisation

---
 xen/arch/x86/mm/mm-locks.h | 12 +++++++-----
 xen/arch/x86/mm/p2m.c      |  1 +
 xen/include/asm-x86/mm.h   |  2 +-
 3 files changed, 9 insertions(+), 6 deletions(-)
xen/arch/x86/mm/mm-locks.h +7 -5
index 76c7217..8a40986 100644
@@ -31,6 +31,8 @@
 DECLARE_PER_CPU(int, mm_lock_level);
 #define __get_lock_level()  (this_cpu(mm_lock_level))
 
+DECLARE_PERCPU_RWLOCK_GLOBAL(p2m_percpu_rwlock);
+
 static inline void mm_lock_init(mm_lock_t *l)
 {
     spin_lock_init(&l->lock);
@@ -99,7 +101,7 @@ static inline void _mm_enforce_order_lock_post(int level, int *unlock_level,
 
 static inline void mm_rwlock_init(mm_rwlock_t *l)
 {
-    rwlock_init(&l->lock);
+    percpu_rwlock_resource_init(&l->lock, p2m_percpu_rwlock);
     l->locker = -1;
     l->locker_function = "nobody";
     l->unlock_level = 0;
@@ -115,7 +117,7 @@ static inline void _mm_write_lock(mm_rwlock_t *l, const char *func, int level)
     if ( !mm_write_locked_by_me(l) )
     {
         __check_lock_level(level);
-        write_lock(&l->lock);
+        percpu_write_lock(p2m_percpu_rwlock, &l->lock);
         l->locker = get_processor_id();
         l->locker_function = func;
         l->unlock_level = __get_lock_level();
@@ -131,20 +133,20 @@ static inline void mm_write_unlock(mm_rwlock_t *l)
     l->locker = -1;
     l->locker_function = "nobody";
     __set_lock_level(l->unlock_level);
-    write_unlock(&l->lock);
+    percpu_write_unlock(p2m_percpu_rwlock, &l->lock);
 }
 
 static inline void _mm_read_lock(mm_rwlock_t *l, int level)
 {
     __check_lock_level(level);
-    read_lock(&l->lock);
+    percpu_read_lock(p2m_percpu_rwlock, &l->lock);
     /* There's nowhere to store the per-CPU unlock level so we can't
      * set the lock level. */
 }
 
 static inline void mm_read_unlock(mm_rwlock_t *l)
 {
-    read_unlock(&l->lock);
+    percpu_read_unlock(p2m_percpu_rwlock, &l->lock);
 }
 
 /* This wrapper uses the line number to express the locking order below */
xen/arch/x86/mm/p2m.c +1 -0
index ed0bbd7..a45ee35 100644
@@ -54,6 +54,7 @@ boolean_param("hap_2mb", opt_hap_2mb);
 #undef page_to_mfn
 #define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
 
+DEFINE_PERCPU_RWLOCK_GLOBAL(p2m_percpu_rwlock);
 
 /* Init the datastructures for later use by the p2m code */
 static int p2m_initialise(struct domain *d, struct p2m_domain *p2m)
xen/include/asm-x86/mm.h +1 -1
index 67b34c6..78511dd 100644
@@ -566,7 +566,7 @@ typedef struct mm_lock {
 } mm_lock_t;
 
 typedef struct mm_rwlock {
-    rwlock_t           lock;
+    percpu_rwlock_t    lock;
     int                unlock_level;
     int                recurse_count;
     int                locker; /* CPU that holds the write lock */
Signature · 1 line
1.7.12.4
Reviewed-by on v5 3/3
#13 lore raw

Re: [PATCHv5 3/3] p2m: convert p2m rwlock to percpu rwlock

Quoted 13 lines · Malcolm Crossley

The per domain p2m read lock suffers from significant contention when performance multi-queue block or network IO due to the parallel grant map/unmaps/copies occuring on the DomU's p2m.

On multi-socket systems, the contention results in the locked compare swap operation failing frequently which results in a tight loop of retries of the compare swap operation. As the coherency fabric can only support a specific rate of compare swap operations for a particular data location then taking the read lock itself becomes a bottleneck for p2m operations.

Percpu rwlock p2m performance with the same configuration is approximately 64 gbit/s vs the 48 gbit/s with grant table percpu rwlocks only.

Quoted 7 lines

Oprofile was used to determine the initial overhead of the read-write locks and to confirm the overhead was dramatically reduced by the percpu rwlocks.

Note: altp2m users will not achieve a gain if they take an altp2m read lock simultaneously with the main p2m lock.

Looks good, thanks:

If you end up switching to always using the per-cpu pointer stored in the percpu_rwlock struct, you can retain the Reviewed-by for those changes.

-George