Skip to contentSkip to conversation

powerpc: alternate queued spinlock implementation

PATCH Superseded Nicholas Piggin 4 participants 78 messages Jul 28, 2022 – Nov 10, 2022

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

cover powerpc: alternate queued spinlock implementation

This replaces the generic queued spinlock code (like s390 does) with our own implementation. There is an extra shim patch 1a here to get the series to apply.

So far the microbenchmarks look okay, haven't really had time to write up a good set of results. I hope to get some significant bigger workloads some testing time in the next week or so so if those turn out positive I may resubmit with any tweaks and some more details of numbers.

Thanks,
Nick

Nicholas Piggin (17):
powerpc/qspinlock: powerpc qspinlock implementation powerpc/qspinlock: add mcs queueing for contended waiters powerpc/qspinlock: use a half-word store to unlock to avoid larx/stcx. powerpc/qspinlock: convert atomic operations to assembly powerpc/qspinlock: allow new waiters to steal the lock before queueing powerpc/qspinlock: theft prevention to control latency powerpc/qspinlock: store owner CPU in lock word powerpc/qspinlock: paravirt yield to lock owner powerpc/qspinlock: implement option to yield to previous node powerpc/qspinlock: allow stealing when head of queue yields powerpc/qspinlock: allow propagation of yield CPU down the queue powerpc/qspinlock: add ability to prod new queue head CPU powerpc/qspinlock: trylock and initial lock attempt may steal powerpc/qspinlock: use spin_begin/end API powerpc/qspinlock: reduce remote node steal spins powerpc/qspinlock: allow indefinite spinning on a preempted owner powerpc/qspinlock: provide accounting and options for sleepy locks

 arch/powerpc/Kconfig                       |    1 -
 arch/powerpc/include/asm/qspinlock.h       |  130 ++-
 arch/powerpc/include/asm/qspinlock_types.h |   70 ++
 arch/powerpc/include/asm/spinlock_types.h  |    2 +-
 arch/powerpc/lib/Makefile                  |    4 +-
 arch/powerpc/lib/qspinlock.c               | 1009 ++++++++++++++++++++
 6 files changed, 1172 insertions(+), 44 deletions(-)
 create mode 100644 arch/powerpc/include/asm/qspinlock_types.h
 create mode 100644 arch/powerpc/lib/qspinlock.c
Signature · 1 line
2.35.1
author reviewer
#1 lore raw

PATCH 1/17 powerpc/qspinlock: powerpc qspinlock implementation

Add a powerpc specific implementation of queued spinlocks. This is the build framework with a very simple (non-queued) spinlock implementation to begin with. Later changes add queueing, and other features and optimisations one-at-a-time. It is done this way to more easily see how the queued spinlocks are built, and to make performance and correctness bisects more useful.

Generic PV qspinlock code is causing latency / starvation regressions on large systems that are resulting in hard lockups reported (mostly in pathoogical cases). The generic qspinlock code has a number of issues important for powerpc hardware and hypervisors that aren't easily solved without changing code that would impact other architectures. Follow s390's lead and implement our own for now.

Issues for powerpc using generic qspinlocks:
- The previous lock value should not be loaded with simple loads, and need not be passed around from previous loads or cmpxchg results, because powerpc uses ll/sc-style atomics which can perform more complex operations that do not require this. powerpc implementations tend to prefer loads use larx for improved coherency performance.
- The queueing process should absolutely minimise the number of stores to the lock word to reduce exclusive coherency probes, important for large system scalability. The pending logic is counter productive here.
- Non-atomic unlock for paravirt locks is important (atomic instructions tend to still be more expensive than x86 CPUs).
- Yielding to the lock owner is important in the oversubscribed paravirt case, which requires storing the owner CPU in the lock word.
- More control of lock stealing for the paravirt case is important to keep latency down on large systems.
- The lock acquisition operation should always be made with a special variant of atomic instructions with the lock hint bit set, including (especially) in the queueing paths. This is more a matter of adding more arch lock helpers so not an insurmountable problem for generic code.

Since the RFC series, I tested this on a 16-socket 1920 thread POWER10 system with some microbenchmarks, and that showed up significant problems with the previous series. High amount of spinning on the lock up-front (lock stealing) for SPLPAR mode (paravirt) really hurts scalability when the guest is not overcommitted. However on smaller KVM systems with significant overcommit (e.g., 5-10%), this spinning is very important to avoid performance tanking due to the queueing problem. So rather than set STEAL_SPINS and HEAD_SPINS based on SPLPAR at boot-time, I lowered them and do more to dynamically deal with vCPU preemption. So behaviour of dedicated and shared LPAR mode is now the same until there is vCPU preemption detected. This seems to be leading to better results overall, but some worst-case latencies are significantly up with the lockstorm test (latency is still better than generic queued spinlocks, but not as good as it previously was or as good as simple). Statistical fairness is still significantly better.

Thanks,
Nick

---
 arch/powerpc/Kconfig                       |  1 -
 arch/powerpc/include/asm/qspinlock.h       | 78 ++++++++++------------
 arch/powerpc/include/asm/qspinlock_types.h | 13 ++++
 arch/powerpc/include/asm/spinlock_types.h  |  2 +-
 arch/powerpc/lib/Makefile                  |  4 +-
 arch/powerpc/lib/qspinlock.c               | 18 +++++
 6 files changed, 69 insertions(+), 47 deletions(-)
 create mode 100644 arch/powerpc/include/asm/qspinlock_types.h
 create mode 100644 arch/powerpc/lib/qspinlock.c
arch/powerpc/Kconfig +0 -1
index 7aa12e88c580..4838e6c96b20 100644
@@ -154,7 +154,6 @@ config PPC
 	select ARCH_USE_CMPXCHG_LOCKREF		if PPC64
 	select ARCH_USE_MEMTEST
 	select ARCH_USE_QUEUED_RWLOCKS		if PPC_QUEUED_SPINLOCKS
-	select ARCH_USE_QUEUED_SPINLOCKS	if PPC_QUEUED_SPINLOCKS
 	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
 	select ARCH_WANT_IPC_PARSE_VERSION
 	select ARCH_WANT_IRQS_OFF_ACTIVATE_MM
arch/powerpc/include/asm/qspinlock.h +34 -44

100 lines not on this page · raw message

arch/powerpc/include/asm/qspinlock_types.h new file +13 -0
new file mode 100644
index 000000000000..59606bc0c774
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _ASM_POWERPC_QSPINLOCK_TYPES_H
+#define _ASM_POWERPC_QSPINLOCK_TYPES_H
+
+#include <linux/types.h>
+
+typedef struct qspinlock {
+	atomic_t val;
+} arch_spinlock_t;
+
+#define	__ARCH_SPIN_LOCK_UNLOCKED	{ .val = ATOMIC_INIT(0) }
+
+#endif /* _ASM_POWERPC_QSPINLOCK_TYPES_H */
arch/powerpc/include/asm/spinlock_types.h +1 -1
index d5f8a74ed2e8..40b01446cf75 100644
@@ -7,7 +7,7 @@
 #endif
 
 #ifdef CONFIG_PPC_QUEUED_SPINLOCKS
-#include <asm-generic/qspinlock_types.h>
+#include <asm/qspinlock_types.h>
 #include <asm-generic/qrwlock_types.h>
 #else
 #include <asm/simple_spinlock_types.h>
arch/powerpc/lib/Makefile +3 -1
index 8560c912186d..b895cbf6a709 100644
@@ -52,7 +52,9 @@ obj-$(CONFIG_PPC_BOOK3S_64) += copyuser_power7.o copypage_power7.o \
 obj64-y	+= copypage_64.o copyuser_64.o mem_64.o hweight_64.o \
 	   memcpy_64.o copy_mc_64.o
 
-ifndef CONFIG_PPC_QUEUED_SPINLOCKS
+ifdef CONFIG_PPC_QUEUED_SPINLOCKS
+obj64-$(CONFIG_SMP)	+= qspinlock.o
+else
 obj64-$(CONFIG_SMP)	+= locks.o
 endif
 
arch/powerpc/lib/qspinlock.c new file +18 -0
new file mode 100644
index 000000000000..8dbce99a373c
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+#include <linux/export.h>
+#include <linux/processor.h>
+#include <asm/qspinlock.h>
+
+void queued_spin_lock_slowpath(struct qspinlock *lock)
+{
+	while (!queued_spin_trylock(lock))
+		cpu_relax();
+}
+EXPORT_SYMBOL(queued_spin_lock_slowpath);
+
+#ifdef CONFIG_PARAVIRT_SPINLOCKS
+void pv_spinlocks_init(void)
+{
+}
+#endif
+
Signature · 1 line
2.35.1
author reviewer
#8 lore raw

PATCH 1/1 powerpc/qspinlock: Prepare qspinlock code

In reply to Nicholas Piggin

I have a bunch of parallel patches that clean up the generic queued spinlock code, but the powerpc implementation does not use or depend on any of that except patch collision. I've based the powerpc series on top of that work, but it's annoying to post or carry around all those patches as well. This shim patch takes the powerpc changes and should be applied first. If powerpc series is to go ahead of the generic series, then this patch would just be merged into patch 1 of this series. This patch won't compile or do anything useful alone.

---
 arch/powerpc/include/asm/qspinlock.h          | 45 ++++++-------------
 arch/powerpc/include/asm/qspinlock_paravirt.h |  7 ---
 arch/powerpc/include/asm/spinlock.h           |  2 +-
 3 files changed, 15 insertions(+), 39 deletions(-)
 delete mode 100644 arch/powerpc/include/asm/qspinlock_paravirt.h
arch/powerpc/include/asm/qspinlock.h +14 -31
index b676c4fb90fd..39c1c7f80579 100644
@@ -7,42 +7,32 @@
 
 #define _Q_PENDING_LOOPS	(1 << 9) /* not tuned */
 
-#ifdef CONFIG_PARAVIRT_SPINLOCKS
-extern void native_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
-extern void __pv_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
-extern void __pv_queued_spin_unlock(struct qspinlock *lock);
+void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
+void __pv_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
+void __pv_queued_spin_unlock(struct qspinlock *lock);
 
-static __always_inline void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
+static __always_inline void queued_spin_lock(struct qspinlock *lock)
 {
-	if (!is_shared_processor())
-		native_queued_spin_lock_slowpath(lock, val);
+	u32 val = 0;
+
+	if (likely(arch_atomic_try_cmpxchg_lock(&lock->val, &val, _Q_LOCKED_VAL)))
+		return;
+
+	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) || !is_shared_processor())
+		queued_spin_lock_slowpath(lock, val);
 	else
 		__pv_queued_spin_lock_slowpath(lock, val);
 }
+#define queued_spin_lock queued_spin_lock
 
-#define queued_spin_unlock queued_spin_unlock
 static inline void queued_spin_unlock(struct qspinlock *lock)
 {
-	if (!is_shared_processor())
+	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) || !is_shared_processor())
 		smp_store_release(&lock->locked, 0);
 	else
 		__pv_queued_spin_unlock(lock);
 }
-
-#else
-extern void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
-#endif
-
-static __always_inline void queued_spin_lock(struct qspinlock *lock)
-{
-	u32 val = 0;
-
-	if (likely(arch_atomic_try_cmpxchg_lock(&lock->val, &val, _Q_LOCKED_VAL)))
-		return;
-
-	queued_spin_lock_slowpath(lock, val);
-}
-#define queued_spin_lock queued_spin_lock
+#define queued_spin_unlock queued_spin_unlock
 
 #ifdef CONFIG_PARAVIRT_SPINLOCKS
 #define SPIN_THRESHOLD (1<<15) /* not tuned */
@@ -63,13 +53,6 @@ static __always_inline void pv_kick(int cpu)
 	prod_cpu(cpu);
 }
 
-extern void __pv_init_lock_hash(void);
-
-static inline void pv_spinlocks_init(void)
-{
-	__pv_init_lock_hash();
-}
-
 #endif
 
 /*
arch/powerpc/include/asm/qspinlock_paravirt.h deleted +0 -7
deleted file mode 100644
index 6b60e7736a47..000000000000
@@ -1,7 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-or-later */
-#ifndef _ASM_POWERPC_QSPINLOCK_PARAVIRT_H
-#define _ASM_POWERPC_QSPINLOCK_PARAVIRT_H
-
-EXPORT_SYMBOL(__pv_queued_spin_unlock);
-
-#endif /* _ASM_POWERPC_QSPINLOCK_PARAVIRT_H */
arch/powerpc/include/asm/spinlock.h +1 -1
index bd75872a6334..7dafca8e3f02 100644
@@ -13,7 +13,7 @@
 /* See include/linux/spinlock.h */
 #define smp_mb__after_spinlock()	smp_mb()
 
-#ifndef CONFIG_PARAVIRT_SPINLOCKS
+#ifndef CONFIG_PPC_QUEUED_SPINLOCKS
 static inline void pv_spinlocks_init(void) { }
 #endif
 
Signature · 1 line
2.35.1
author reviewer
#9 lore raw

PATCH 2/17 powerpc/qspinlock: add mcs queueing for contended waiters

In reply to Nicholas Piggin

This forms the basis of the qspinlock slow path.

Like generic qspinlocks and unlike the vanilla MCS algorithm, the lock owner does not participate in the queue, only waiters. The first waiter spins on the lock word, then when the lock is released it takes ownership and unqueues the next waiter. This is how qspinlocks can be implemented with the spinlock API -- lock owners don't need a node, only waiters do.

---
 arch/powerpc/include/asm/qspinlock.h       |  10 +-
 arch/powerpc/include/asm/qspinlock_types.h |  21 +++
 arch/powerpc/lib/qspinlock.c               | 166 ++++++++++++++++++++-
 3 files changed, 191 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/qspinlock.h +7 -3
index cb2b4f91e976..f06117aa60e1 100644
@@ -18,12 +18,12 @@ static __always_inline int queued_spin_value_unlocked(struct qspinlock lock)
 
 static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
 {
-	return 0;
+	return !!(atomic_read(&lock->val) & _Q_TAIL_CPU_MASK);
 }
 
 static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 {
-	if (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0)
+	if (atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL) == 0)
 		return 1;
 	return 0;
 }
@@ -38,7 +38,11 @@ static __always_inline void queued_spin_lock(struct qspinlock *lock)
 
 static inline void queued_spin_unlock(struct qspinlock *lock)
 {
-	atomic_set_release(&lock->val, 0);
+	for (;;) {
+		int val = atomic_read(&lock->val);
+		if (atomic_cmpxchg_release(&lock->val, val, val & ~_Q_LOCKED_VAL) == val)
+			return;
+	}
 }
 
 #define arch_spin_is_locked(l)		queued_spin_is_locked(l)
arch/powerpc/include/asm/qspinlock_types.h +21 -0
index 59606bc0c774..9630e714c70d 100644
@@ -10,4 +10,25 @@ typedef struct qspinlock {
 
 #define	__ARCH_SPIN_LOCK_UNLOCKED	{ .val = ATOMIC_INIT(0) }
 
+/*
+ * Bitfields in the atomic value:
+ *
+ *     0: locked bit
+ * 16-31: tail cpu (+1)
+ */
+#define	_Q_SET_MASK(type)	(((1U << _Q_ ## type ## _BITS) - 1)\
+				      << _Q_ ## type ## _OFFSET)
+#define _Q_LOCKED_OFFSET	0
+#define _Q_LOCKED_BITS		1
+#define _Q_LOCKED_MASK		_Q_SET_MASK(LOCKED)
+#define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
+
+#define _Q_TAIL_CPU_OFFSET	16
+#define _Q_TAIL_CPU_BITS	(32 - _Q_TAIL_CPU_OFFSET)
+#define _Q_TAIL_CPU_MASK	_Q_SET_MASK(TAIL_CPU)
+
+#if CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS)
+#error "qspinlock does not support such large CONFIG_NR_CPUS"
+#endif
+
 #endif /* _ASM_POWERPC_QSPINLOCK_TYPES_H */
arch/powerpc/lib/qspinlock.c +163 -3

175 lines not on this page · raw message

Signature · 1 line
2.35.1
author reviewer
#13 lore raw

PATCH 3/17 powerpc/qspinlock: use a half-word store to unlock to avoid larx/stcx.

In reply to Nicholas Piggin

The first 16 bits of the lock are only modified by the owner, and other modifications always use atomic operations on the entire 32 bits, so unlocks can use plain stores on the 16 bits. This is the same kind of optimisation done by core qspinlock code.

---
 arch/powerpc/include/asm/qspinlock.h       |  6 +-----
 arch/powerpc/include/asm/qspinlock_types.h | 19 +++++++++++++++++--
 2 files changed, 18 insertions(+), 7 deletions(-)
arch/powerpc/include/asm/qspinlock.h +1 -5
index f06117aa60e1..79a1936fb68d 100644
@@ -38,11 +38,7 @@ static __always_inline void queued_spin_lock(struct qspinlock *lock)
 
 static inline void queued_spin_unlock(struct qspinlock *lock)
 {
-	for (;;) {
-		int val = atomic_read(&lock->val);
-		if (atomic_cmpxchg_release(&lock->val, val, val & ~_Q_LOCKED_VAL) == val)
-			return;
-	}
+	smp_store_release(&lock->locked, 0);
 }
 
 #define arch_spin_is_locked(l)		queued_spin_is_locked(l)
arch/powerpc/include/asm/qspinlock_types.h +17 -2
index 9630e714c70d..3425dab42576 100644
@@ -3,12 +3,27 @@
 #define _ASM_POWERPC_QSPINLOCK_TYPES_H
 
 #include <linux/types.h>
+#include <asm/byteorder.h>
 
 typedef struct qspinlock {
-	atomic_t val;
+	union {
+		atomic_t val;
+
+#ifdef __LITTLE_ENDIAN
+		struct {
+			u16	locked;
+			u8	reserved[2];
+		};
+#else
+		struct {
+			u8	reserved[2];
+			u16	locked;
+		};
+#endif
+	};
 } arch_spinlock_t;
 
-#define	__ARCH_SPIN_LOCK_UNLOCKED	{ .val = ATOMIC_INIT(0) }
+#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { .val = ATOMIC_INIT(0) } }
 
 /*
  * Bitfields in the atomic value:
Signature · 1 line
2.35.1
author reviewer
#17 lore raw

PATCH 4/17 powerpc/qspinlock: convert atomic operations to assembly

In reply to Nicholas Piggin

This uses more optimal ll/sc style access patterns (rather than cmpxchg), and also sets the EH=1 lock hint on those operations which acquire ownership of the lock.

---
 arch/powerpc/include/asm/qspinlock.h       | 25 +++++--
 arch/powerpc/include/asm/qspinlock_types.h |  6 +-
 arch/powerpc/lib/qspinlock.c               | 81 +++++++++++++++-------
 3 files changed, 79 insertions(+), 33 deletions(-)
arch/powerpc/include/asm/qspinlock.h +20 -5
index 79a1936fb68d..3ab354159e5e 100644
@@ -2,28 +2,43 @@
 #ifndef _ASM_POWERPC_QSPINLOCK_H
 #define _ASM_POWERPC_QSPINLOCK_H
 
-#include <linux/atomic.h>
 #include <linux/compiler.h>
 #include <asm/qspinlock_types.h>
 
 static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
 {
-	return atomic_read(&lock->val);
+	return READ_ONCE(lock->val);
 }
 
 static __always_inline int queued_spin_value_unlocked(struct qspinlock lock)
 {
-	return !atomic_read(&lock.val);
+	return !lock.val;
 }
 
 static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
 {
-	return !!(atomic_read(&lock->val) & _Q_TAIL_CPU_MASK);
+	return !!(READ_ONCE(lock->val) & _Q_TAIL_CPU_MASK);
 }
 
 static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 {
-	if (atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL) == 0)
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	asm volatile(
+"1:	lwarx	%0,0,%1,%3	# queued_spin_trylock			\n"
+"	cmpwi	0,%0,0							\n"
+"	bne-	2f							\n"
+"	stwcx.	%2,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");
+
+	if (likely(prev == 0))
 		return 1;
 	return 0;
 }
arch/powerpc/include/asm/qspinlock_types.h +3 -3
index 3425dab42576..210adf05b235 100644
@@ -7,7 +7,7 @@
 
 typedef struct qspinlock {
 	union {
-		atomic_t val;
+		u32 val;
 
 #ifdef __LITTLE_ENDIAN
 		struct {
@@ -23,10 +23,10 @@ typedef struct qspinlock {
 	};
 } arch_spinlock_t;
 
-#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { .val = ATOMIC_INIT(0) } }
+#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { .val = 0 } }
 
 /*
- * Bitfields in the atomic value:
+ * Bitfields in the lock word:
  *
  *     0: locked bit
  * 16-31: tail cpu (+1)
arch/powerpc/lib/qspinlock.c +56 -25

127 lines not on this page · raw message

Signature · 1 line
2.35.1
author reviewer
#23 lore raw

PATCH 5/17 powerpc/qspinlock: allow new waiters to steal the lock before queueing

In reply to Nicholas Piggin

Allow new waiters a number of spins on the lock word before queueing, which particularly helps paravirt performance when physical CPUs are oversubscribed.

---
 arch/powerpc/lib/qspinlock.c | 152 ++++++++++++++++++++++++++++++++---
 1 file changed, 141 insertions(+), 11 deletions(-)
arch/powerpc/lib/qspinlock.c +141 -11
index 7c71e5e287df..1625cce714b2 100644
@@ -19,8 +19,17 @@ struct qnodes {
 	struct qnode nodes[MAX_NODES];
 };
 
+/* Tuning parameters */
+static int STEAL_SPINS __read_mostly = (1<<5);
+static bool MAYBE_STEALERS __read_mostly = true;
+
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
+static __always_inline int get_steal_spins(void)
+{
+	return STEAL_SPINS;
+}
+
 static inline u32 encode_tail_cpu(void)
 {
 	return (smp_processor_id() + 1) << _Q_TAIL_CPU_OFFSET;
@@ -76,6 +85,39 @@ static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 ol
 	return 0;
 }
 
+static __always_inline u32 __trylock_cmpxchg(struct qspinlock *lock, u32 old, u32 new)
+{
+	u32 prev;
+
+	BUG_ON(old & _Q_LOCKED_VAL);
+
+	asm volatile(
+"1:	lwarx	%0,0,%1,%4	# queued_spin_trylock_cmpxchg		\n"
+"	cmpw	0,%0,%2							\n"
+"	bne-	2f							\n"
+"	stwcx.	%3,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r"(old), "r" (new),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");
+
+	return prev;
+}
+
+/* Take lock, preserving tail, cmpxchg with val (which must not be locked) */
+static __always_inline int trylock_with_tail_cpu(struct qspinlock *lock, u32 val)
+{
+	u32 newval = _Q_LOCKED_VAL | (val & _Q_TAIL_CPU_MASK);
+
+	if (__trylock_cmpxchg(lock, val, newval) == val)
+		return 1;
+	else
+		return 0;
+}
+
 /*
  * Publish our tail, replacing previous tail. Return previous value.
  *
@@ -115,6 +157,31 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
+static inline bool try_to_steal_lock(struct qspinlock *lock)
+{
+	int iters;
+
+	/* Attempt to steal the lock */
+	for (;;) {
+		u32 val = READ_ONCE(lock->val);
+
+		if (unlikely(!(val & _Q_LOCKED_VAL))) {
+			if (trylock_with_tail_cpu(lock, val))
+				return true;
+			continue;
+		}
+
+		cpu_relax();
+
+		iters++;
+
+		if (iters >= get_steal_spins())
+			break;
+	}
+
+	return false;
+}
+
 static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 {
 	struct qnodes *qnodesp;
@@ -164,20 +231,39 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 		smp_rmb(); /* acquire barrier for the mcs lock */
 	}
 
-	/* We're at the head of the waitqueue, wait for the lock. */
-	while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
-		cpu_relax();
+	if (!MAYBE_STEALERS) {
+		/* We're at the head of the waitqueue, wait for the lock. */
+		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
+			cpu_relax();
 
-	/* If we're the last queued, must clean up the tail. */
-	if ((val & _Q_TAIL_CPU_MASK) == tail) {
-		if (trylock_clear_tail_cpu(lock, val))
-			goto release;
-		/* Another waiter must have enqueued */
-	}
+		/* If we're the last queued, must clean up the tail. */
+		if ((val & _Q_TAIL_CPU_MASK) == tail) {
+			if (trylock_clear_tail_cpu(lock, val))
+				goto release;
+			/* Another waiter must have enqueued. */
+		}
+
+		/* We must be the owner, just set the lock bit and acquire */
+		lock_set_locked(lock);
+	} else {
+again:
+		/* We're at the head of the waitqueue, wait for the lock. */
+		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
+			cpu_relax();
 
-	/* We must be the owner, just set the lock bit and acquire */
-	lock_set_locked(lock);
+		/* If we're the last queued, must clean up the tail. */
+		if ((val & _Q_TAIL_CPU_MASK) == tail) {
+			if (trylock_clear_tail_cpu(lock, val))
+				goto release;
+			/* Another waiter must have enqueued, or lock stolen. */
+		} else {
+			if (trylock_with_tail_cpu(lock, val))
+				goto unlock_next;
+		}
+		goto again;
+	}
 
+unlock_next:
 	/* contended path; must wait for next != NULL (MCS protocol) */
 	while (!(next = READ_ONCE(node->next)))
 		cpu_relax();
@@ -197,6 +283,9 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 
 void queued_spin_lock_slowpath(struct qspinlock *lock)
 {
+	if (try_to_steal_lock(lock))
+		return;
+
 	queued_spin_lock_mcs_queue(lock);
 }
 EXPORT_SYMBOL(queued_spin_lock_slowpath);
@@ -207,3 +296,44 @@ void pv_spinlocks_init(void)
 }
 #endif
 
+#include <linux/debugfs.h>
+static int steal_spins_set(void *data, u64 val)
+{
+	static DEFINE_MUTEX(lock);
+
+	mutex_lock(&lock);
+	if (val && !STEAL_SPINS) {
+		MAYBE_STEALERS = true;
+		/* wait for waiter to go away */
+		synchronize_rcu();
+		STEAL_SPINS = val;
+	} else if (!val && STEAL_SPINS) {
+		STEAL_SPINS = val;
+		/* wait for all possible stealers to go away */
+		synchronize_rcu();
+		MAYBE_STEALERS = false;
+	} else {
+		STEAL_SPINS = val;
+	}
+	mutex_unlock(&lock);
+
+	return 0;
+}
+
+static int steal_spins_get(void *data, u64 *val)
+{
+	*val = STEAL_SPINS;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_steal_spins, steal_spins_get, steal_spins_set, "%llu\n");
+
+static __init int spinlock_debugfs_init(void)
+{
+	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
+
+	return 0;
+}
+device_initcall(spinlock_debugfs_init);
+
Signature · 1 line
2.35.1
author reviewer
#27 lore raw

PATCH 6/17 powerpc/qspinlock: theft prevention to control latency

In reply to Nicholas Piggin

Give the queue head the ability to stop stealers. After a number of spins without sucessfully acquiring the lock, the queue head employs this, which will assure it is the next owner.

---
 arch/powerpc/include/asm/qspinlock_types.h | 10 +++-
 arch/powerpc/lib/qspinlock.c               | 56 +++++++++++++++++++++-
 2 files changed, 63 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/qspinlock_types.h +8 -2
index 210adf05b235..8b20f5e22bba 100644
@@ -29,7 +29,8 @@ typedef struct qspinlock {
  * Bitfields in the lock word:
  *
  *     0: locked bit
- * 16-31: tail cpu (+1)
+ *    16: must queue bit
+ * 17-31: tail cpu (+1)
  */
 #define	_Q_SET_MASK(type)	(((1U << _Q_ ## type ## _BITS) - 1)\
 				      << _Q_ ## type ## _OFFSET)
@@ -38,7 +39,12 @@ typedef struct qspinlock {
 #define _Q_LOCKED_MASK		_Q_SET_MASK(LOCKED)
 #define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
 
-#define _Q_TAIL_CPU_OFFSET	16
+#define _Q_MUST_Q_OFFSET	16
+#define _Q_MUST_Q_BITS		1
+#define _Q_MUST_Q_MASK		_Q_SET_MASK(MUST_Q)
+#define _Q_MUST_Q_VAL		(1U << _Q_MUST_Q_OFFSET)
+
+#define _Q_TAIL_CPU_OFFSET	17
 #define _Q_TAIL_CPU_BITS	(32 - _Q_TAIL_CPU_OFFSET)
 #define _Q_TAIL_CPU_MASK	_Q_SET_MASK(TAIL_CPU)
 
arch/powerpc/lib/qspinlock.c +55 -1

99 lines not on this page · raw message

Signature · 1 line
2.35.1
author reviewer
#31 lore raw

PATCH 7/17 powerpc/qspinlock: store owner CPU in lock word

In reply to Nicholas Piggin

Store the owner CPU number in the lock word so it may be yielded to, as powerpc's paravirtualised simple spinlocks do.

---
 arch/powerpc/include/asm/qspinlock.h       |  8 +++++++-
 arch/powerpc/include/asm/qspinlock_types.h | 10 ++++++++++
 arch/powerpc/lib/qspinlock.c               |  6 +++---
 3 files changed, 20 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/qspinlock.h +7 -1
index 3ab354159e5e..44601b261e08 100644
@@ -20,9 +20,15 @@ static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
 	return !!(READ_ONCE(lock->val) & _Q_TAIL_CPU_MASK);
 }
 
+static __always_inline u32 queued_spin_get_locked_val(void)
+{
+	/* XXX: make this use lock value in paca like simple spinlocks? */
+	return _Q_LOCKED_VAL | (smp_processor_id() << _Q_OWNER_CPU_OFFSET);
+}
+
 static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 {
-	u32 new = _Q_LOCKED_VAL;
+	u32 new = queued_spin_get_locked_val();
 	u32 prev;
 
 	asm volatile(
arch/powerpc/include/asm/qspinlock_types.h +10 -0
index 8b20f5e22bba..35f9525381e6 100644
@@ -29,6 +29,8 @@ typedef struct qspinlock {
  * Bitfields in the lock word:
  *
  *     0: locked bit
+ *  1-14: lock holder cpu
+ *    15: unused bit
  *    16: must queue bit
  * 17-31: tail cpu (+1)
  */
@@ -39,6 +41,14 @@ typedef struct qspinlock {
 #define _Q_LOCKED_MASK		_Q_SET_MASK(LOCKED)
 #define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
 
+#define _Q_OWNER_CPU_OFFSET	1
+#define _Q_OWNER_CPU_BITS	14
+#define _Q_OWNER_CPU_MASK	_Q_SET_MASK(OWNER_CPU)
+
+#if CONFIG_NR_CPUS > (1U << _Q_OWNER_CPU_BITS)
+#error "qspinlock does not support such large CONFIG_NR_CPUS"
+#endif
+
 #define _Q_MUST_Q_OFFSET	16
 #define _Q_MUST_Q_BITS		1
 #define _Q_MUST_Q_MASK		_Q_SET_MASK(MUST_Q)
arch/powerpc/lib/qspinlock.c +3 -3
index a906cc8f15fa..aa26cfe21f18 100644
@@ -50,7 +50,7 @@ static inline int get_tail_cpu(u32 val)
 /* Take the lock by setting the lock bit, no other CPUs will touch it. */
 static __always_inline void lock_set_locked(struct qspinlock *lock)
 {
-	u32 new = _Q_LOCKED_VAL;
+	u32 new = queued_spin_get_locked_val();
 	u32 prev;
 
 	asm volatile(
@@ -68,7 +68,7 @@ static __always_inline void lock_set_locked(struct qspinlock *lock)
 /* Take lock, clearing tail, cmpxchg with old (which must not be locked) */
 static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 old)
 {
-	u32 new = _Q_LOCKED_VAL;
+	u32 new = queued_spin_get_locked_val();
 	u32 prev;
 
 	BUG_ON(old & _Q_LOCKED_VAL);
@@ -116,7 +116,7 @@ static __always_inline u32 __trylock_cmpxchg(struct qspinlock *lock, u32 old, u3
 /* Take lock, preserving tail, cmpxchg with val (which must not be locked) */
 static __always_inline int trylock_with_tail_cpu(struct qspinlock *lock, u32 val)
 {
-	u32 newval = _Q_LOCKED_VAL | (val & _Q_TAIL_CPU_MASK);
+	u32 newval = queued_spin_get_locked_val() | (val & _Q_TAIL_CPU_MASK);
 
 	if (__trylock_cmpxchg(lock, val, newval) == val)
 		return 1;
Signature · 1 line
2.35.1
author reviewer
#35 lore raw

PATCH 8/17 powerpc/qspinlock: paravirt yield to lock owner

In reply to Nicholas Piggin

Waiters spinning on the lock word should yield to the lock owner if the vCPU is preempted. This improves performance when the hypervisor has oversubscribed physical CPUs.

---
 arch/powerpc/lib/qspinlock.c | 97 ++++++++++++++++++++++++++++++------
 1 file changed, 83 insertions(+), 14 deletions(-)
arch/powerpc/lib/qspinlock.c +83 -14
index aa26cfe21f18..55286ac91da5 100644
@@ -5,6 +5,7 @@
 #include <linux/percpu.h>
 #include <linux/smp.h>
 #include <asm/qspinlock.h>
+#include <asm/paravirt.h>
 
 #define MAX_NODES	4
 
@@ -24,14 +25,16 @@ static int STEAL_SPINS __read_mostly = (1<<5);
 static bool MAYBE_STEALERS __read_mostly = true;
 static int HEAD_SPINS __read_mostly = (1<<8);
 
+static bool pv_yield_owner __read_mostly = true;
+
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
-static __always_inline int get_steal_spins(void)
+static __always_inline int get_steal_spins(bool paravirt)
 {
 	return STEAL_SPINS;
 }
 
-static __always_inline int get_head_spins(void)
+static __always_inline int get_head_spins(bool paravirt)
 {
 	return HEAD_SPINS;
 }
@@ -46,7 +49,11 @@ static inline int get_tail_cpu(u32 val)
 	return (val >> _Q_TAIL_CPU_OFFSET) - 1;
 }
 
-/* Take the lock by setting the bit, no other CPUs may concurrently lock it. */
+static inline int get_owner_cpu(u32 val)
+{
+	return (val & _Q_OWNER_CPU_MASK) >> _Q_OWNER_CPU_OFFSET;
+}
+
 /* Take the lock by setting the lock bit, no other CPUs will touch it. */
 static __always_inline void lock_set_locked(struct qspinlock *lock)
 {
@@ -180,7 +187,45 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
-static inline bool try_to_steal_lock(struct qspinlock *lock)
+static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+{
+	int owner;
+	u32 yield_count;
+
+	BUG_ON(!(val & _Q_LOCKED_VAL));
+
+	if (!paravirt)
+		goto relax;
+
+	if (!pv_yield_owner)
+		goto relax;
+
+	owner = get_owner_cpu(val);
+	yield_count = yield_count_of(owner);
+
+	if ((yield_count & 1) == 0)
+		goto relax; /* owner vcpu is running */
+
+	/*
+	 * Read the lock word after sampling the yield count. On the other side
+	 * there may a wmb because the yield count update is done by the
+	 * hypervisor preemption and the value update by the OS, however this
+	 * ordering might reduce the chance of out of order accesses and
+	 * improve the heuristic.
+	 */
+	smp_rmb();
+
+	if (READ_ONCE(lock->val) == val) {
+		yield_to_preempted(owner, yield_count);
+		/* Don't relax if we yielded. Maybe we should? */
+		return;
+	}
+relax:
+	cpu_relax();
+}
+
+
+static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {
 	int iters;
 
@@ -197,18 +242,18 @@ static inline bool try_to_steal_lock(struct qspinlock *lock)
 			continue;
 		}
 
-		cpu_relax();
+		yield_to_locked_owner(lock, val, paravirt);
 
 		iters++;
 
-		if (iters >= get_steal_spins())
+		if (iters >= get_steal_spins(paravirt))
 			break;
 	}
 
 	return false;
 }
 
-static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
+static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, bool paravirt)
 {
 	struct qnodes *qnodesp;
 	struct qnode *next, *node;
@@ -260,7 +305,7 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 	if (!MAYBE_STEALERS) {
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
-			cpu_relax();
+			yield_to_locked_owner(lock, val, paravirt);
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -278,10 +323,10 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
-			cpu_relax();
+			yield_to_locked_owner(lock, val, paravirt);
 
 			iters++;
-			if (!set_mustq && iters >= get_head_spins()) {
+			if (!set_mustq && iters >= get_head_spins(paravirt)) {
 				set_mustq = true;
 				lock_set_mustq(lock);
 				val |= _Q_MUST_Q_VAL;
@@ -320,10 +365,15 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 
 void queued_spin_lock_slowpath(struct qspinlock *lock)
 {
-	if (try_to_steal_lock(lock))
-		return;
-
-	queued_spin_lock_mcs_queue(lock);
+	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) && is_shared_processor()) {
+		if (try_to_steal_lock(lock, true))
+			return;
+		queued_spin_lock_mcs_queue(lock, true);
+	} else {
+		if (try_to_steal_lock(lock, false))
+			return;
+		queued_spin_lock_mcs_queue(lock, false);
+	}
 }
 EXPORT_SYMBOL(queued_spin_lock_slowpath);
 
@@ -382,10 +432,29 @@ static int head_spins_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_head_spins, head_spins_get, head_spins_set, "%llu\n");
 
+static int pv_yield_owner_set(void *data, u64 val)
+{
+	pv_yield_owner = !!val;
+
+	return 0;
+}
+
+static int pv_yield_owner_get(void *data, u64 *val)
+{
+	*val = pv_yield_owner;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_owner, pv_yield_owner_get, pv_yield_owner_set, "%llu\n");
+
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
 	debugfs_create_file("qspl_head_spins", 0600, arch_debugfs_dir, NULL, &fops_head_spins);
+	if (is_shared_processor()) {
+		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
+	}
 
 	return 0;
 }
Signature · 1 line
2.35.1
author reviewer
#39 lore raw

PATCH 9/17 powerpc/qspinlock: implement option to yield to previous node

In reply to Nicholas Piggin

Queued waiters which are not at the head of the queue don't spin on the lock word but their qnode lock word, waiting for the previous queued CPU to release them. Add an option which allows these waiters to yield to the previous CPU if its vCPU is preempted.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 46 +++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +45 -1
index 55286ac91da5..b39f8c5b329c 100644
@@ -26,6 +26,7 @@ static bool MAYBE_STEALERS __read_mostly = true;
 static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
+static bool pv_yield_prev __read_mostly = true;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
@@ -224,6 +225,31 @@ static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va
 	cpu_relax();
 }
 
+static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
+{
+	u32 yield_count;
+
+	if (!paravirt)
+		goto relax;
+
+	if (!pv_yield_prev)
+		goto relax;
+
+	yield_count = yield_count_of(prev_cpu);
+	if ((yield_count & 1) == 0)
+		goto relax; /* owner vcpu is running */
+
+	smp_rmb(); /* See yield_to_locked_owner comment */
+
+	if (!node->locked) {
+		yield_to_preempted(prev_cpu, yield_count);
+		return;
+	}
+
+relax:
+	cpu_relax();
+}
+
 
 static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {
@@ -291,13 +317,14 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	 */
 	if (old & _Q_TAIL_CPU_MASK) {
 		struct qnode *prev = get_tail_qnode(lock, old);
+		int prev_cpu = get_tail_cpu(old);
 
 		/* Link @node into the waitqueue. */
 		WRITE_ONCE(prev->next, node);
 
 		/* Wait for mcs node lock to be released */
 		while (!node->locked)
-			cpu_relax();
+			yield_to_prev(lock, node, prev_cpu, paravirt);
 
 		smp_rmb(); /* acquire barrier for the mcs lock */
 	}
@@ -448,12 +475,29 @@ static int pv_yield_owner_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_owner, pv_yield_owner_get, pv_yield_owner_set, "%llu\n");
 
+static int pv_yield_prev_set(void *data, u64 val)
+{
+	pv_yield_prev = !!val;
+
+	return 0;
+}
+
+static int pv_yield_prev_get(void *data, u64 *val)
+{
+	*val = pv_yield_prev;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_prev, pv_yield_prev_get, pv_yield_prev_set, "%llu\n");
+
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
 	debugfs_create_file("qspl_head_spins", 0600, arch_debugfs_dir, NULL, &fops_head_spins);
 	if (is_shared_processor()) {
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
+		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
 	}
 
 	return 0;
Signature · 1 line
2.35.1
author reviewer
#43 lore raw

PATCH 10/17 powerpc/qspinlock: allow stealing when head of queue yields

In reply to Nicholas Piggin

If the head of queue is preventing stealing but it finds the owner vCPU is preempted, it will yield its cycles to the owner which could cause it to become preempted. Add an option to re-allow stealers before yielding, and disallow them again after returning from the yield.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 56 ++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 3 deletions(-)
arch/powerpc/lib/qspinlock.c +53 -3
index b39f8c5b329c..94f007f66942 100644
@@ -26,6 +26,7 @@ static bool MAYBE_STEALERS __read_mostly = true;
 static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
+static bool pv_yield_allow_steal __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
@@ -173,6 +174,23 @@ static __always_inline u32 lock_set_mustq(struct qspinlock *lock)
 	return prev;
 }
 
+static __always_inline u32 lock_clear_mustq(struct qspinlock *lock)
+{
+	u32 new = _Q_MUST_Q_VAL;
+	u32 prev;
+
+	asm volatile(
+"1:	lwarx	%0,0,%1		# lock_clear_mustq			\n"
+"	andc	%0,%0,%2						\n"
+"	stwcx.	%0,0,%1							\n"
+"	bne-	1b							\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new)
+	: "cr0", "memory");
+
+	return prev;
+}
+
 static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 {
 	int cpu = get_tail_cpu(val);
@@ -188,7 +206,7 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
 {
 	int owner;
 	u32 yield_count;
@@ -217,7 +235,11 @@ static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va
 	smp_rmb();
 
 	if (READ_ONCE(lock->val) == val) {
+		if (clear_mustq)
+			lock_clear_mustq(lock);
 		yield_to_preempted(owner, yield_count);
+		if (clear_mustq)
+			lock_set_mustq(lock);
 		/* Don't relax if we yielded. Maybe we should? */
 		return;
 	}
@@ -225,6 +247,16 @@ static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va
 	cpu_relax();
 }
 
+static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+{
+	__yield_to_locked_owner(lock, val, paravirt, false);
+}
+
+static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+{
+	__yield_to_locked_owner(lock, val, paravirt, clear_mustq);
+}
+
 static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
 {
 	u32 yield_count;
@@ -332,7 +364,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	if (!MAYBE_STEALERS) {
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
-			yield_to_locked_owner(lock, val, paravirt);
+			yield_head_to_locked_owner(lock, val, paravirt, false);
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -350,7 +382,8 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
-			yield_to_locked_owner(lock, val, paravirt);
+			yield_head_to_locked_owner(lock, val, paravirt,
+					pv_yield_allow_steal && set_mustq);
 
 			iters++;
 			if (!set_mustq && iters >= get_head_spins(paravirt)) {
@@ -475,6 +508,22 @@ static int pv_yield_owner_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_owner, pv_yield_owner_get, pv_yield_owner_set, "%llu\n");
 
+static int pv_yield_allow_steal_set(void *data, u64 val)
+{
+	pv_yield_allow_steal = !!val;
+
+	return 0;
+}
+
+static int pv_yield_allow_steal_get(void *data, u64 *val)
+{
+	*val = pv_yield_allow_steal;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_allow_steal, pv_yield_allow_steal_get, pv_yield_allow_steal_set, "%llu\n");
+
 static int pv_yield_prev_set(void *data, u64 val)
 {
 	pv_yield_prev = !!val;
@@ -497,6 +546,7 @@ static __init int spinlock_debugfs_init(void)
 	debugfs_create_file("qspl_head_spins", 0600, arch_debugfs_dir, NULL, &fops_head_spins);
 	if (is_shared_processor()) {
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
+		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
 	}
 
Signature · 1 line
2.35.1
author reviewer
#47 lore raw

PATCH 11/17 powerpc/qspinlock: allow propagation of yield CPU down the queue

In reply to Nicholas Piggin

Having all CPUs poll the lock word for the owner CPU that should be yielded to defeats most of the purpose of using MCS queueing for scalability. Yet it may be desirable for queued waiters to to yield to a preempted owner.

s390 addreses this problem by having queued waiters sample the lock word to find the owner much less frequently. In this approach, the waiters never sample it directly, but the queue head propagates the owner CPU back to the next waiter if it ever finds the owner has been preempted. Queued waiters then subsequently propagate the owner CPU back to the next waiter, and so on.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 85 +++++++++++++++++++++++++++++++++++-
 1 file changed, 84 insertions(+), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +84 -1
index 94f007f66942..28c85a2d5635 100644
@@ -12,6 +12,7 @@
 struct qnode {
 	struct qnode	*next;
 	struct qspinlock *lock;
+	int		yield_cpu;
 	u8		locked; /* 1 if lock acquired */
 };
 
@@ -28,6 +29,7 @@ static int HEAD_SPINS __read_mostly = (1<<8);
 static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
+static bool pv_yield_propagate_owner __read_mostly = true;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
@@ -257,13 +259,66 @@ static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u
 	__yield_to_locked_owner(lock, val, paravirt, clear_mustq);
 }
 
+static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int *set_yield_cpu, bool paravirt)
+{
+	struct qnode *next;
+	int owner;
+
+	if (!paravirt)
+		return;
+	if (!pv_yield_propagate_owner)
+		return;
+
+	owner = get_owner_cpu(val);
+	if (*set_yield_cpu == owner)
+		return;
+
+	next = READ_ONCE(node->next);
+	if (!next)
+		return;
+
+	if (vcpu_is_preempted(owner)) {
+		next->yield_cpu = owner;
+		*set_yield_cpu = owner;
+	} else if (*set_yield_cpu != -1) {
+		next->yield_cpu = owner;
+		*set_yield_cpu = owner;
+	}
+}
+
 static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
 {
 	u32 yield_count;
+	int yield_cpu;
 
 	if (!paravirt)
 		goto relax;
 
+	if (!pv_yield_propagate_owner)
+		goto yield_prev;
+
+	yield_cpu = READ_ONCE(node->yield_cpu);
+	if (yield_cpu == -1) {
+		/* Propagate back the -1 CPU */
+		if (node->next && node->next->yield_cpu != -1)
+			node->next->yield_cpu = yield_cpu;
+		goto yield_prev;
+	}
+
+	yield_count = yield_count_of(yield_cpu);
+	if ((yield_count & 1) == 0)
+		goto yield_prev; /* owner vcpu is running */
+
+	smp_rmb();
+
+	if (yield_cpu == node->yield_cpu) {
+		if (node->next && node->next->yield_cpu != yield_cpu)
+			node->next->yield_cpu = yield_cpu;
+		yield_to_preempted(yield_cpu, yield_count);
+		return;
+	}
+
+yield_prev:
 	if (!pv_yield_prev)
 		goto relax;
 
@@ -337,6 +392,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	node = &qnodesp->nodes[idx];
 	node->next = NULL;
 	node->lock = lock;
+	node->yield_cpu = -1;
 	node->locked = 0;
 
 	tail = encode_tail_cpu();
@@ -358,13 +414,21 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		while (!node->locked)
 			yield_to_prev(lock, node, prev_cpu, paravirt);
 
+		/* Clear out stale propagated yield_cpu */
+		if (paravirt && pv_yield_propagate_owner && node->yield_cpu != -1)
+			node->yield_cpu = -1;
+
 		smp_rmb(); /* acquire barrier for the mcs lock */
 	}
 
 	if (!MAYBE_STEALERS) {
+		int set_yield_cpu = -1;
+
 		/* We're at the head of the waitqueue, wait for the lock. */
-		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
+		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt, false);
+		}
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -376,12 +440,14 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		/* We must be the owner, just set the lock bit and acquire */
 		lock_set_locked(lock);
 	} else {
+		int set_yield_cpu = -1;
 		int iters = 0;
 		bool set_mustq = false;
 
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt,
 					pv_yield_allow_steal && set_mustq);
 
@@ -540,6 +606,22 @@ static int pv_yield_prev_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_prev, pv_yield_prev_get, pv_yield_prev_set, "%llu\n");
 
+static int pv_yield_propagate_owner_set(void *data, u64 val)
+{
+	pv_yield_propagate_owner = !!val;
+
+	return 0;
+}
+
+static int pv_yield_propagate_owner_get(void *data, u64 *val)
+{
+	*val = pv_yield_propagate_owner;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_propagate_owner, pv_yield_propagate_owner_get, pv_yield_propagate_owner_set, "%llu\n");
+
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
@@ -548,6 +630,7 @@ static __init int spinlock_debugfs_init(void)
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
 		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
+		debugfs_create_file("qspl_pv_yield_propagate_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);
 	}
 
 	return 0;
Signature · 1 line
2.35.1
author reviewer
#52 lore raw

PATCH 12/17 powerpc/qspinlock: add ability to prod new queue head CPU

In reply to Nicholas Piggin

After the head of the queue acquires the lock, it releases the next waiter in the queue to become the new head. Add an option to prod the new head if its vCPU was preempted. This may only have an effect if queue waiters are yielding.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +28 -1
index 28c85a2d5635..3b10e31bcf0a 100644
@@ -12,6 +12,7 @@
 struct qnode {
 	struct qnode	*next;
 	struct qspinlock *lock;
+	int		cpu;
 	int		yield_cpu;
 	u8		locked; /* 1 if lock acquired */
 };
@@ -30,6 +31,7 @@ static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
 static bool pv_yield_propagate_owner __read_mostly = true;
+static bool pv_prod_head __read_mostly = false;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
@@ -392,6 +394,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	node = &qnodesp->nodes[idx];
 	node->next = NULL;
 	node->lock = lock;
+	node->cpu = smp_processor_id();
 	node->yield_cpu = -1;
 	node->locked = 0;
 
@@ -483,7 +486,14 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	 * this store to locked. The corresponding barrier is the smp_rmb()
 	 * acquire barrier for mcs lock, above.
 	 */
-	WRITE_ONCE(next->locked, 1);
+	if (paravirt && pv_prod_head) {
+		int next_cpu = next->cpu;
+		WRITE_ONCE(next->locked, 1);
+		if (vcpu_is_preempted(next_cpu))
+			prod_cpu(next_cpu);
+	} else {
+		WRITE_ONCE(next->locked, 1);
+	}
 
 release:
 	qnodesp->count--; /* release the node */
@@ -622,6 +632,22 @@ static int pv_yield_propagate_owner_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_propagate_owner, pv_yield_propagate_owner_get, pv_yield_propagate_owner_set, "%llu\n");
 
+static int pv_prod_head_set(void *data, u64 val)
+{
+	pv_prod_head = !!val;
+
+	return 0;
+}
+
+static int pv_prod_head_get(void *data, u64 *val)
+{
+	*val = pv_prod_head;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_prod_head, pv_prod_head_get, pv_prod_head_set, "%llu\n");
+
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
@@ -631,6 +657,7 @@ static __init int spinlock_debugfs_init(void)
 		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
 		debugfs_create_file("qspl_pv_yield_propagate_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);
+		debugfs_create_file("qspl_pv_prod_head", 0600, arch_debugfs_dir, NULL, &fops_pv_prod_head);
 	}
 
 	return 0;
Signature · 1 line
2.35.1
author reviewer
#56 lore raw

PATCH 13/17 powerpc/qspinlock: trylock and initial lock attempt may steal

In reply to Nicholas Piggin

This gives trylock slightly more strength, and it also gives most of the benefit of passing 'val' back through the slowpath without the complexity.

---
 arch/powerpc/include/asm/qspinlock.h | 39 +++++++++++++++++++++++++++-
 arch/powerpc/lib/qspinlock.c         |  9 +++++++
 2 files changed, 47 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/qspinlock.h +38 -1
index 44601b261e08..d3d2039237b2 100644
@@ -5,6 +5,8 @@
 #include <linux/compiler.h>
 #include <asm/qspinlock_types.h>
 
+#define _Q_SPIN_TRY_LOCK_STEAL 1
+
 static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
 {
 	return READ_ONCE(lock->val);
@@ -26,11 +28,12 @@ static __always_inline u32 queued_spin_get_locked_val(void)
 	return _Q_LOCKED_VAL | (smp_processor_id() << _Q_OWNER_CPU_OFFSET);
 }
 
-static __always_inline int queued_spin_trylock(struct qspinlock *lock)
+static __always_inline int __queued_spin_trylock_nosteal(struct qspinlock *lock)
 {
 	u32 new = queued_spin_get_locked_val();
 	u32 prev;
 
+	/* Trylock succeeds only when unlocked and no queued nodes */
 	asm volatile(
 "1:	lwarx	%0,0,%1,%3	# queued_spin_trylock			\n"
 "	cmpwi	0,%0,0							\n"
@@ -49,6 +52,40 @@ static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 	return 0;
 }
 
+static __always_inline int __queued_spin_trylock_steal(struct qspinlock *lock)
+{
+	u32 new = queued_spin_get_locked_val();
+	u32 prev, tmp;
+
+	/* Trylock may get ahead of queued nodes if it finds unlocked */
+	asm volatile(
+"1:	lwarx	%0,0,%2,%5	# queued_spin_trylock			\n"
+"	andc.	%1,%0,%4						\n"
+"	bne-	2f							\n"
+"	and	%1,%0,%4						\n"
+"	or	%1,%1,%3						\n"
+"	stwcx.	%1,0,%2							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"
+	: "=&r" (prev), "=&r" (tmp)
+	: "r" (&lock->val), "r" (new), "r" (_Q_TAIL_CPU_MASK),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");
+
+	if (likely(!(prev & ~_Q_TAIL_CPU_MASK)))
+		return 1;
+	return 0;
+}
+
+static __always_inline int queued_spin_trylock(struct qspinlock *lock)
+{
+	if (!_Q_SPIN_TRY_LOCK_STEAL)
+		return __queued_spin_trylock_nosteal(lock);
+	else
+		return __queued_spin_trylock_steal(lock);
+}
+
 void queued_spin_lock_slowpath(struct qspinlock *lock);
 
 static __always_inline void queued_spin_lock(struct qspinlock *lock)
arch/powerpc/lib/qspinlock.c +9 -0
index 3b10e31bcf0a..277aef1fab0a 100644
@@ -24,7 +24,11 @@ struct qnodes {
 
 /* Tuning parameters */
 static int STEAL_SPINS __read_mostly = (1<<5);
+#if _Q_SPIN_TRY_LOCK_STEAL == 1
+static const bool MAYBE_STEALERS = true;
+#else
 static bool MAYBE_STEALERS __read_mostly = true;
+#endif
 static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
@@ -522,6 +526,10 @@ void pv_spinlocks_init(void)
 #include <linux/debugfs.h>
 static int steal_spins_set(void *data, u64 val)
 {
+#if _Q_SPIN_TRY_LOCK_STEAL == 1
+	/* MAYBE_STEAL remains true */
+	STEAL_SPINS = val;
+#else
 	static DEFINE_MUTEX(lock);
 
 	mutex_lock(&lock);
@@ -539,6 +547,7 @@ static int steal_spins_set(void *data, u64 val)
 		STEAL_SPINS = val;
 	}
 	mutex_unlock(&lock);
+#endif
 
 	return 0;
 }
Signature · 1 line
2.35.1
author reviewer
#60 lore raw

PATCH 14/17 powerpc/qspinlock: use spin_begin/end API

In reply to Nicholas Piggin

Use the spin_begin/spin_cpu_relax/spin_end APIs in qspinlock, which helps to prevent threads issuing a lot of expensive priority nops which may not have much effect due to immediately executing low then medium priority.

---
 arch/powerpc/lib/qspinlock.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
arch/powerpc/lib/qspinlock.c +31 -4
index 277aef1fab0a..d4594c701f7d 100644
@@ -233,6 +233,8 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 	if ((yield_count & 1) == 0)
 		goto relax; /* owner vcpu is running */
 
+	spin_end();
+
 	/*
 	 * Read the lock word after sampling the yield count. On the other side
 	 * there may a wmb because the yield count update is done by the
@@ -248,11 +250,13 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 		yield_to_preempted(owner, yield_count);
 		if (clear_mustq)
 			lock_set_mustq(lock);
+		spin_begin();
 		/* Don't relax if we yielded. Maybe we should? */
 		return;
 	}
+	spin_begin();
 relax:
-	cpu_relax();
+	spin_cpu_relax();
 }
 
 static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
@@ -315,14 +319,18 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 	if ((yield_count & 1) == 0)
 		goto yield_prev; /* owner vcpu is running */
 
+	spin_end();
+
 	smp_rmb();
 
 	if (yield_cpu == node->yield_cpu) {
 		if (node->next && node->next->yield_cpu != yield_cpu)
 			node->next->yield_cpu = yield_cpu;
 		yield_to_preempted(yield_cpu, yield_count);
+		spin_begin();
 		return;
 	}
+	spin_begin();
 
 yield_prev:
 	if (!pv_yield_prev)
@@ -332,15 +340,19 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 	if ((yield_count & 1) == 0)
 		goto relax; /* owner vcpu is running */
 
+	spin_end();
+
 	smp_rmb(); /* See yield_to_locked_owner comment */
 
 	if (!node->locked) {
 		yield_to_preempted(prev_cpu, yield_count);
+		spin_begin();
 		return;
 	}
+	spin_begin();
 
 relax:
-	cpu_relax();
+	spin_cpu_relax();
 }
 
 
@@ -349,6 +361,7 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 	int iters;
 
 	/* Attempt to steal the lock */
+	spin_begin();
 	for (;;) {
 		u32 val = READ_ONCE(lock->val);
 
@@ -356,8 +369,10 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 			break;
 
 		if (unlikely(!(val & _Q_LOCKED_VAL))) {
+			spin_end();
 			if (trylock_with_tail_cpu(lock, val))
 				return true;
+			spin_begin();
 			continue;
 		}
 
@@ -368,6 +383,7 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 		if (iters >= get_steal_spins(paravirt))
 			break;
 	}
+	spin_end();
 
 	return false;
 }
@@ -418,8 +434,10 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		WRITE_ONCE(prev->next, node);
 
 		/* Wait for mcs node lock to be released */
+		spin_begin();
 		while (!node->locked)
 			yield_to_prev(lock, node, prev_cpu, paravirt);
+		spin_end();
 
 		/* Clear out stale propagated yield_cpu */
 		if (paravirt && pv_yield_propagate_owner && node->yield_cpu != -1)
@@ -432,10 +450,12 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		int set_yield_cpu = -1;
 
 		/* We're at the head of the waitqueue, wait for the lock. */
+		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt, false);
 		}
+		spin_end();
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -453,6 +473,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
+		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt,
@@ -465,6 +486,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 				val |= _Q_MUST_Q_VAL;
 			}
 		}
+		spin_end();
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -480,8 +502,13 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 
 unlock_next:
 	/* contended path; must wait for next != NULL (MCS protocol) */
-	while (!(next = READ_ONCE(node->next)))
-		cpu_relax();
+	next = READ_ONCE(node->next);
+	if (!next) {
+		spin_begin();
+		while (!(next = READ_ONCE(node->next)))
+			cpu_relax();
+		spin_end();
+	}
 
 	/*
 	 * Unlock the next mcs waiter node. Release barrier is not required
Signature · 1 line
2.35.1
author reviewer
#64 lore raw

PATCH 15/17 powerpc/qspinlock: reduce remote node steal spins

In reply to Nicholas Piggin

Allow for a reduction in the number of times a CPU from a different node than the owner can attempt to steal the lock before queueing. This could bias the transfer behaviour of the lock across the machine and reduce NUMA crossings.

---
 arch/powerpc/lib/qspinlock.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)
arch/powerpc/lib/qspinlock.c +31 -3
index d4594c701f7d..24f68bd71e2b 100644
@@ -4,6 +4,7 @@
 #include <linux/export.h>
 #include <linux/percpu.h>
 #include <linux/smp.h>
+#include <linux/topology.h>
 #include <asm/qspinlock.h>
 #include <asm/paravirt.h>
 
@@ -24,6 +25,7 @@ struct qnodes {
 
 /* Tuning parameters */
 static int STEAL_SPINS __read_mostly = (1<<5);
+static int REMOTE_STEAL_SPINS __read_mostly = (1<<2);
 #if _Q_SPIN_TRY_LOCK_STEAL == 1
 static const bool MAYBE_STEALERS = true;
 #else
@@ -39,9 +41,13 @@ static bool pv_prod_head __read_mostly = false;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
-static __always_inline int get_steal_spins(bool paravirt)
+static __always_inline int get_steal_spins(bool paravirt, bool remote)
 {
-	return STEAL_SPINS;
+	if (remote) {
+		return REMOTE_STEAL_SPINS;
+	} else {
+		return STEAL_SPINS;
+	}
 }
 
 static __always_inline int get_head_spins(bool paravirt)
@@ -380,8 +386,13 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 
 		iters++;
 
-		if (iters >= get_steal_spins(paravirt))
+		if (iters >= get_steal_spins(paravirt, false))
 			break;
+		if (iters >= get_steal_spins(paravirt, true)) {
+			int cpu = get_owner_cpu(val);
+			if (numa_node_id() != cpu_to_node(cpu))
+				break;
+		}
 	}
 	spin_end();
 
@@ -588,6 +599,22 @@ static int steal_spins_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_steal_spins, steal_spins_get, steal_spins_set, "%llu\n");
 
+static int remote_steal_spins_set(void *data, u64 val)
+{
+	REMOTE_STEAL_SPINS = val;
+
+	return 0;
+}
+
+static int remote_steal_spins_get(void *data, u64 *val)
+{
+	*val = REMOTE_STEAL_SPINS;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_remote_steal_spins, remote_steal_spins_get, remote_steal_spins_set, "%llu\n");
+
 static int head_spins_set(void *data, u64 val)
 {
 	HEAD_SPINS = val;
@@ -687,6 +714,7 @@ DEFINE_SIMPLE_ATTRIBUTE(fops_pv_prod_head, pv_prod_head_get, pv_prod_head_set, "
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
+	debugfs_create_file("qspl_remote_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_remote_steal_spins);
 	debugfs_create_file("qspl_head_spins", 0600, arch_debugfs_dir, NULL, &fops_head_spins);
 	if (is_shared_processor()) {
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
Signature · 1 line
2.35.1
author reviewer
#68 lore raw

PATCH 16/17 powerpc/qspinlock: allow indefinite spinning on a preempted owner

In reply to Nicholas Piggin

Provide an option that holds off queueing indefinitely while the lock owner is preempted. This could reduce queueing latencies for very overcommitted vcpu situations.

This is disabled by default.

---
 arch/powerpc/lib/qspinlock.c | 91 +++++++++++++++++++++++++++++++-----
 1 file changed, 79 insertions(+), 12 deletions(-)
arch/powerpc/lib/qspinlock.c +79 -12
index 24f68bd71e2b..5cfd69931e31 100644
@@ -35,6 +35,7 @@ static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
+static bool pv_spin_on_preempted_owner __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
 static bool pv_yield_propagate_owner __read_mostly = true;
 static bool pv_prod_head __read_mostly = false;
@@ -220,13 +221,15 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
-static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq, bool *preempted)
 {
 	int owner;
 	u32 yield_count;
 
 	BUG_ON(!(val & _Q_LOCKED_VAL));
 
+	*preempted = false;
+
 	if (!paravirt)
 		goto relax;
 
@@ -241,6 +244,8 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 
 	spin_end();
 
+	*preempted = true;
+
 	/*
 	 * Read the lock word after sampling the yield count. On the other side
 	 * there may a wmb because the yield count update is done by the
@@ -265,14 +270,14 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 	spin_cpu_relax();
 }
 
-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool *preempted)
 {
-	__yield_to_locked_owner(lock, val, paravirt, false);
+	__yield_to_locked_owner(lock, val, paravirt, false, preempted);
 }
 
-static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq, bool *preempted)
 {
-	__yield_to_locked_owner(lock, val, paravirt, clear_mustq);
+	__yield_to_locked_owner(lock, val, paravirt, clear_mustq, preempted);
 }
 
 static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int *set_yield_cpu, bool paravirt)
@@ -364,12 +369,33 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 
 static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {
-	int iters;
+	int iters = 0;
+
+	if (!STEAL_SPINS) {
+		if (paravirt && pv_spin_on_preempted_owner) {
+			spin_begin();
+			for (;;) {
+				u32 val = READ_ONCE(lock->val);
+				bool preempted;
+
+				if (val & _Q_MUST_Q_VAL)
+					break;
+				if (!(val & _Q_LOCKED_VAL))
+					break;
+				if (!vcpu_is_preempted(get_owner_cpu(val)))
+					break;
+				yield_to_locked_owner(lock, val, paravirt, &preempted);
+			}
+			spin_end();
+		}
+		return false;
+	}
 
 	/* Attempt to steal the lock */
 	spin_begin();
 	for (;;) {
 		u32 val = READ_ONCE(lock->val);
+		bool preempted;
 
 		if (val & _Q_MUST_Q_VAL)
 			break;
@@ -382,9 +408,22 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 			continue;
 		}
 
-		yield_to_locked_owner(lock, val, paravirt);
-
-		iters++;
+		yield_to_locked_owner(lock, val, paravirt, &preempted);
+
+		if (paravirt && preempted) {
+			if (!pv_spin_on_preempted_owner)
+				iters++;
+			/*
+			 * pv_spin_on_preempted_owner don't increase iters
+			 * while the owner is preempted -- we won't interfere
+			 * with it by definition. This could introduce some
+			 * latency issue if we continually observe preempted
+			 * owners, but hopefully that's a rare corner case of
+			 * a badly oversubscribed system.
+			 */
+		} else {
+			iters++;
+		}
 
 		if (iters >= get_steal_spins(paravirt, false))
 			break;
@@ -463,8 +502,10 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		/* We're at the head of the waitqueue, wait for the lock. */
 		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			bool preempted;
+
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
-			yield_head_to_locked_owner(lock, val, paravirt, false);
+			yield_head_to_locked_owner(lock, val, paravirt, false, &preempted);
 		}
 		spin_end();
 
@@ -486,11 +527,20 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		/* We're at the head of the waitqueue, wait for the lock. */
 		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			bool preempted;
+
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt,
-					pv_yield_allow_steal && set_mustq);
+					pv_yield_allow_steal && set_mustq,
+					&preempted);
+
+			if (paravirt && preempted) {
+				if (!pv_spin_on_preempted_owner)
+					iters++;
+			} else {
+				iters++;
+			}
 
-			iters++;
 			if (!set_mustq && iters >= get_head_spins(paravirt)) {
 				set_mustq = true;
 				lock_set_mustq(lock);
@@ -663,6 +713,22 @@ static int pv_yield_allow_steal_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_allow_steal, pv_yield_allow_steal_get, pv_yield_allow_steal_set, "%llu\n");
 
+static int pv_spin_on_preempted_owner_set(void *data, u64 val)
+{
+	pv_spin_on_preempted_owner = !!val;
+
+	return 0;
+}
+
+static int pv_spin_on_preempted_owner_get(void *data, u64 *val)
+{
+	*val = pv_spin_on_preempted_owner;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_spin_on_preempted_owner, pv_spin_on_preempted_owner_get, pv_spin_on_preempted_owner_set, "%llu\n");
+
 static int pv_yield_prev_set(void *data, u64 val)
 {
 	pv_yield_prev = !!val;
@@ -719,6 +785,7 @@ static __init int spinlock_debugfs_init(void)
 	if (is_shared_processor()) {
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
 		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
+		debugfs_create_file("qspl_pv_spin_on_preempted_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_spin_on_preempted_owner);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
 		debugfs_create_file("qspl_pv_yield_propagate_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);
 		debugfs_create_file("qspl_pv_prod_head", 0600, arch_debugfs_dir, NULL, &fops_pv_prod_head);
Signature · 1 line
2.35.1
author reviewer
#74 lore raw

PATCH 17/17 powerpc/qspinlock: provide accounting and options for sleepy locks

In reply to Nicholas Piggin

Finding the owner or a queued waiter on a lock with a preempted vcpu is indicative of an oversubscribed guest causing the lock to get into trouble. Provide some options to detect this situation and have new CPUs avoid queueing for a longer time (more steal iterations) to minimise the problems caused by vcpu preemption on the queue.

---
 arch/powerpc/include/asm/qspinlock_types.h |   7 +-
 arch/powerpc/lib/qspinlock.c               | 240 +++++++++++++++++++--
 2 files changed, 232 insertions(+), 15 deletions(-)
arch/powerpc/include/asm/qspinlock_types.h +6 -1
index 35f9525381e6..4fbcc8a4230b 100644
@@ -30,7 +30,7 @@ typedef struct qspinlock {
  *
  *     0: locked bit
  *  1-14: lock holder cpu
- *    15: unused bit
+ *    15: lock owner or queuer vcpus observed to be preempted bit
  *    16: must queue bit
  * 17-31: tail cpu (+1)
  */
@@ -49,6 +49,11 @@ typedef struct qspinlock {
 #error "qspinlock does not support such large CONFIG_NR_CPUS"
 #endif
 
+#define _Q_SLEEPY_OFFSET	15
+#define _Q_SLEEPY_BITS		1
+#define _Q_SLEEPY_MASK		_Q_SET_MASK(SLEEPY_OWNER)
+#define _Q_SLEEPY_VAL		(1U << _Q_SLEEPY_OFFSET)
+
 #define _Q_MUST_Q_OFFSET	16
 #define _Q_MUST_Q_BITS		1
 #define _Q_MUST_Q_MASK		_Q_SET_MASK(MUST_Q)
arch/powerpc/lib/qspinlock.c +226 -14

393 lines not on this page · raw message

Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH 01/17] powerpc/qspinlock: powerpc qspinlock implementation

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +-1000, Nicholas Piggin wrote:
<snip>>-#define queued_spin_lock queued_spin_lock

Review

Quoted 7 lines

-static inline void queued_spin_unlock(struct qspinlock *lock)

+-static __always_inline int queued_spin_trylock(struct qspinlock *lock)
{>-	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) ||!is_shared_processor())
(quoted diff) +1 -2
-		smp_store_release(&lock->locked, 0);>-	else
-		__pv_queued_spin_unlock(lock);>+-	if (atomic_cmpxchg_acquire(&lock->val, 0, 1) ==0)
+-		return 1;>+-	return 0;

optional style nit: return (atomic_cmpxchg_acquire(&lock->val, 0, 1) ==0);

#10 lore raw

Re: [PATCH 02/17] powerpc/qspinlock: add mcs queueing for contended waiters

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +-1000, Nicholas Piggin wrote:
<snip>

Quoted 15 lines
+-/*>+- *Bitfields in the atomic value:
+- *>+- *    0: locked bit
+- *16-31: tail cpu (+-1)
+- */
+-#define	_Q_SET_MASK(type)	(((1U <<_Q_##type ##_BITS) - 1)\>+-				      <<_Q_##type ##_OFFSET)
+-#define _Q_LOCKED_OFFSET	0
+-#define _Q_LOCKED_BITS		1
+-#define _Q_LOCKED_MASK		_Q_SET_MASK(LOCKED)
+-#define _Q_LOCKED_VAL		(1U <<_Q_LOCKED_OFFSET)
+-
+-#define _Q_TAIL_CPU_OFFSET	16
+-#define _Q_TAIL_CPU_BITS	(32 - _Q_TAIL_CPU_OFFSET)
+-#define _Q_TAIL_CPU_MASK	_Q_SET_MASK(TAIL_CPU)
+-

Just to state the obvious this is:

#define _Q_LOCKED_OFFSET 0
#define _Q_LOCKED_BITS 1
#define _Q_LOCKED_MASK 0x00000001
#define _Q_LOCKED_VAL 1

#define _Q_TAIL_CPU_OFFSET 16
#define _Q_TAIL_CPU_BITS 16
#define _Q_TAIL_CPU_MASK 0xffff0000

Quoted 11 lines
+-#if CONFIG_NR_CPUS >=(1U <<_Q_TAIL_CPU_BITS)
+-#error "qspinlock does not support such large CONFIG_NR_CPUS">+-#endif
+-

#endif /*_ASM_POWERPC_QSPINLOCK_TYPES_H */

arch/powerpc/lib/qspinlock.c +1 -1
index 8dbce99a373c..5ebb88d95636 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-1,12 +-1,172 @@> // SPDX-License-Identifier: GPL-2.0-or-later
+-#include <linux/atomic.h>>+-#include <linux/bug.h>>+-#include <linux/compiler.h>> #include <linux/export.h>>-#include <linux/processor.h>>+-#include <linux/percpu.h>>+-#include <linux/smp.h>> #include <asm/qspinlock.h>> -void queued_spin_lock_slowpath(struct qspinlock *lock)

Quoted 5 lines
+-#define MAX_NODES	4
+-
+-struct qnode {>+-	struct qnode	*next;>+-	struct qspinlock *lock;>+-	u8		locked;/*1 if lock acquired */
+-};>+-
+-struct qnodes {>+-	int		count;>+-	struct qnode nodes[MAX_NODES];>+-};

I think it could be worth commenting why qnodes::count instead _Q_TAIL_IDX_OFFSET.

Quoted 3 lines
+-
+-static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);>+-
+-static inline int encode_tail_cpu(void)

I think the generic version that takes smp_processor_id() as a parameter is clearer - at least with this function name.

Quoted 2 lines
+-{>+-	return (smp_processor_id() +- 1) <<_Q_TAIL_CPU_OFFSET;>+-}>+-
+-static inline int get_tail_cpu(int val)

It seems like there should be a "decode"function to pair up with the "encode"function.

Quoted 2 lines
+-{>+-	return (val >>_Q_TAIL_CPU_OFFSET) - 1;>+-}>+-
+-/*Take the lock by setting the bit, no other CPUs may concurrently lock it. */

Does that comment mean it is not necessary to use an atomic_or here?

Quoted 1 line

+-static __always_inline void lock_set_locked(struct qspinlock *lock)

nit: could just be called set_locked()

Quoted 7 lines
+-{>+-	atomic_or(_Q_LOCKED_VAL, &lock->val);>+-	__atomic_acquire_fence();>+-}>+-
+-/*Take lock, clearing tail, cmpxchg with val (which must not be locked) */
+-static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int val)
+-{>+-	int newval =_Q_LOCKED_VAL;>+-
+-	if (atomic_cmpxchg_acquire(&lock->val, val, newval) ==val)
+-		return 1;>+-	else
+-		return 0;

same optional style nit: return (atomic_cmpxchg_acquire(&lock->val, val, newval) ==val);

Quoted 6 lines
+-}>+-
+-/*>+- *Publish our tail, replacing previous tail. Return previous value.
+- *>+- *This provides a release barrier for publishing node, and an acquire barrier
+- *for getting the old node.
+- */
+-static __always_inline int publish_tail_cpu(struct qspinlock *lock, int tail)

Did you change from the xchg_tail() name in the generic version because of the release and acquire barriers this provides? Does "publish"generally imply the old value will be returned?

Quoted 8 lines

{>- while (!queued_spin_trylock(lock))

+-	for (;; {>+-		int val =atomic_read(&lock->val);>+-		int newval =(val &~_Q_TAIL_CPU_MASK) |tail;>+-		int old;>+-
+-		old =atomic_cmpxchg(&lock->val, val, newval);>+-		if (old ==val)
+-			return old;>+-	}>+-}>+-
+-static struct qnode *get_tail_qnode(struct qspinlock *lock, int val)
+-{>+-	int cpu =get_tail_cpu(val);>+-	struct qnodes *qnodesp =per_cpu_ptr(&qnodes, cpu);>+-	int idx;>+-
+-	for (idx =0;idx <MAX_NODES;idx+-+-) {>+-		struct qnode *qnode =&qnodesp->nodes[idx];>+-		if (qnode->lock ==lock)
+-			return qnode;>+-	}

In case anyone else is confused by this, Nick explained each cpu can only queue on a unique spinlock once regardless of "idx"level.

Quoted 6 lines
+-
+-	BUG();>+-}>+-
+-static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
+-{>+-	struct qnodes *qnodesp;>+-	struct qnode *next, *node;>+-	int val, old, tail;>+-	int idx;>+-
+-	BUILD_BUG_ON(CONFIG_NR_CPUS >=(1U <<_Q_TAIL_CPU_BITS));>+-
+-	qnodesp =this_cpu_ptr(&qnodes);>+-	if (unlikely(qnodesp->count ==MAX_NODES)) {

The comparison is >=in the generic, I guess we've no nested NMI so this is safe?

Quoted 44 lines
+-		while (!queued_spin_trylock(lock))
+-			cpu_relax();>+-		return;>+-	}>+-
+-	idx =qnodesp->count+-+-;>+-	/*>+-	 *Ensure that we increment the head node->count before initialising
+-	 *the actual node. If the compiler is kind enough to reorder these
+-	 *stores, then an IRQ could overwrite our assignments.
+-	 */
+-	barrier();>+-	node =&qnodesp->nodes[idx];>+-	node->next =NULL;>+-	node->lock =lock;>+-	node->locked =0;>+-
+-	tail =encode_tail_cpu();>+-
+-	old =publish_tail_cpu(lock, tail);>+-
+-	/*>+-	 *If there was a previous node;link it and wait until reaching the
+-	 *head of the waitqueue.
+-	 */
+-	if (old &_Q_TAIL_CPU_MASK) {>+-		struct qnode *prev =get_tail_qnode(lock, old);>+-
+-		/*Link @node into the waitqueue. */
+-		WRITE_ONCE(prev->next, node);>+-
+-		/*Wait for mcs node lock to be released */
+-		while (!node->locked)
+-			cpu_relax();>+-
+-		smp_rmb();/*acquire barrier for the mcs lock */
+-	}>+-
+-	/*We're at the head of the waitqueue, wait for the lock. */
+-	while ((val =atomic_read(&lock->val)) &_Q_LOCKED_VAL)
+-		cpu_relax();>+-
+-	/*If we're the last queued, must clean up the tail. */
+-	if ((val &_Q_TAIL_CPU_MASK) ==tail) {>+-		if (trylock_clear_tail_cpu(lock, val))
+-			goto release;>+-		/*Another waiter must have enqueued */
+-	}>+-
+-	/*We must be the owner, just set the lock bit and acquire */
+-	lock_set_locked(lock);>+-
+-	/*contended path;must wait for next !=NULL (MCS protocol) */
+-	while (!next =READ_ONCE(node->next)))
		cpu_relax();>+-
+-	/*>+-	 *Unlock the next mcs waiter node. Release barrier is not required
+-	 *here because the acquirer is only accessing the lock word, and
+-	 *the acquire barrier we took the lock with orders that update vs
+-	 *this store to locked. The corresponding barrier is the smp_rmb()
+-	 *acquire barrier for mcs lock, above.
+-	 */
+-	WRITE_ONCE(next->locked, 1);>+-
+-release:
+-	qnodesp->count--;/*release the node */
+-}>+-
+-void queued_spin_lock_slowpath(struct qspinlock *lock)
+-{>+-	queued_spin_lock_mcs_queue(lock);> }> EXPORT_SYMBOL(queued_spin_lock_slowpath);> 
#14 lore raw

Re: [PATCH 03/17] powerpc/qspinlock: use a half-word store to unlock to avoid larx/stcx.

In reply to Nicholas Piggin
Quoted 17 lines · Nicholas Piggin

The first 16 bits of the lock are only modified by the owner, and other modifications always use atomic operations on the entire 32 bits, so unlocks can use plain stores on the 16 bits. This is the same kind of optimisation done by core qspinlock code.
---
arch/powerpc/include/asm/qspinlock.h | 6 +------
arch/powerpc/include/asm/qspinlock_types.h |19 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+---

2 files changed, 18 insertions(+-), 7 deletions(-)
arch/powerpc/include/asm/qspinlock.h +1 -1
index f06117aa60e1..79a1936fb68d 100644
--- a/arch/powerpc/include/asm/qspinlock.h
+-+-+- b/arch/powerpc/include/asm/qspinlock.h

@@-38,11 +-38,7 @@static __always_inline void queued_spin_lock(struct qspinlock *lock)

static inline void queued_spin_unlock(struct qspinlock *lock)
{>- for (;; {>- int val =atomic_read(&lock->val);>- if (atomic_cmpxchg_release(&lock->val, val, val &~_Q_LOCKED_VAL) ==val)

arch/powerpc/include/asm/qspinlock.h

Quoted 1 line
arch/powerpc/include/asm/qspinlock.h +0 -1
-			return;>-	}>+-	smp_store_release(&lock->locked, 0);

Is it also possible for lock_set_locked() to use a non-atomic acquire operation?

Quoted 8 lines

}>
#define arch_spin_is_locked(l) queued_spin_is_locked(l)

arch/powerpc/include/asm/qspinlock_types.h +1 -1
index 9630e714c70d..3425dab42576 100644
--- a/arch/powerpc/include/asm/qspinlock_types.h
+-+-+- b/arch/powerpc/include/asm/qspinlock_types.h

@@-3,12 +-3,27 @@> #define _ASM_POWERPC_QSPINLOCK_TYPES_H

Quoted 6 lines

#include <linux/types.h>>+-#include <asm/byteorder.h>>
typedef struct qspinlock {>- atomic_t val;>+- union {>+- atomic_t val;>+-

+-#ifdef __LITTLE_ENDIAN
+-		struct {>+-			u16	locked;>+-			u8	reserved[2];>+-		};>+-#else
+-		struct {>+-			u8	reserved[2];>+-			u16	locked;>+-		};>+-#endif
+-	};> }arch_spinlock_t;

Just to double check we have:

#define _Q_LOCKED_OFFSET 0
#define _Q_LOCKED_BITS 1
#define _Q_LOCKED_MASK 0x00000001
#define _Q_LOCKED_VAL 1

#define _Q_TAIL_CPU_OFFSET 16
#define _Q_TAIL_CPU_BITS 16
#define _Q_TAIL_CPU_MASK 0xffff0000

so the ordering here looks correct.

Quoted 3 lines

-#define __ARCH_SPIN_LOCK_UNLOCKED {.val =ATOMIC_INIT(0) }>+-#define __ARCH_SPIN_LOCK_UNLOCKED {{.val =ATOMIC_INIT(0) }}>
/*> *Bitfields in the atomic value:

#18 lore raw

Re: [PATCH 04/17] powerpc/qspinlock: convert atomic operations to assembly

In reply to Nicholas Piggin
Quoted 25 lines · Nicholas Piggin

This uses more optimal ll/sc style access patterns (rather than cmpxchg), and also sets the EH=1 lock hint on those operations which acquire ownership of the lock.
---

arch/powerpc/include/asm/qspinlock.h       |25 +-+-+-+-+---
arch/powerpc/include/asm/qspinlock_types.h | 6 +--
arch/powerpc/lib/qspinlock.c               |81 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+--------
3 files changed, 79 insertions(+-), 33 deletions(-)
arch/powerpc/include/asm/qspinlock.h +1 -1
index 79a1936fb68d..3ab354159e5e 100644
--- a/arch/powerpc/include/asm/qspinlock.h
+-+-+- b/arch/powerpc/include/asm/qspinlock.h

@@-2,28 +-2,43 @@> #ifndef _ASM_POWERPC_QSPINLOCK_H #define _ASM_POWERPC_QSPINLOCK_H

-#include <linux/atomic.h>> #include <linux/compiler.h>> #include <asm/qspinlock_types.h>> static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
{>- return atomic_read(&lock->val);>+- return READ_ONCE(lock->val);> }>
static __always_inline int queued_spin_value_unlocked(struct qspinlock lock)
{>- return !atomic_read(&lock.val);>+- return !lock.val;> }>
static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
{>- return !!atomic_read(&lock->val) &_Q_TAIL_CPU_MASK);>+- return !!READ_ONCE(lock->val) &_Q_TAIL_CPU_MASK);> }>
static __always_inline int queued_spin_trylock(struct qspinlock *lock)
{>- if (atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL) ==0)

Quoted 5 lines
+-	u32 new =_Q_LOCKED_VAL;>+-	u32 prev;>+-
+-	asm volatile(
+-"1:	lwarx	%0,0,%1,%3	#queued_spin_trylock			\n">+-"cmpwi	0,%0,0							\n">+-"bne-	2f							\n">+-"stwcx.	%2,0,%1							\n">+-"bne-	1b							\n">+-"\t"PPC_ACQUIRE_BARRIER "					\n">+-"2:									\n">+-	: "=&r"(prev)
+-	: "r"(&lock->val), "r"(new),
+-	  "i"(IS_ENABLED(CONFIG_PPC64) ? 1 : 0)

btw IS_ENABLED() already returns 1 or 0

Quoted 1 line

+- : "cr0" "memory";

This is the ISA's "test and set"atomic primitive. Do you think it would be worth seperating it as a helper?

Quoted 3 lines
+-
+-	if (likely(prev ==0))
		return 1;> 	return 0;

same optional style nit: return likely(prev ==0);

Quoted 19 lines

}>diff --git a/arch/powerpc/include/asm/qspinlock_types.h b/arch/powerpc/include/asm/qspinlock_types.h index 3425dab42576..210adf05b235 100644
--- a/arch/powerpc/include/asm/qspinlock_types.h
+-+-+- b/arch/powerpc/include/asm/qspinlock_types.h
@@-7,7 +-7,7 @@>
typedef struct qspinlock {> union {>- atomic_t val;>+- u32 val;>

#ifdef __LITTLE_ENDIAN
		struct {>@@-23,10 +-23,10 @@typedef struct qspinlock {> 	};> }arch_spinlock_t;> 
-#define	__ARCH_SPIN_LOCK_UNLOCKED	{{.val =ATOMIC_INIT(0) }}>+-#define	__ARCH_SPIN_LOCK_UNLOCKED	{{.val =0 }}> 
/*>- *Bitfields in the atomic value:
+- *Bitfields in the lock word:
 *>  *    0: locked bit

*16-31: tail cpu (+-1)

arch/powerpc/lib/qspinlock.c +1 -1
index 5ebb88d95636..7c71e5e287df 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-1,5 +-1,4 @@> // SPDX-License-Identifier: GPL-2.0-or-later
-#include <linux/atomic.h>> #include <linux/bug.h>> #include <linux/compiler.h>> #include <linux/export.h>>@@-22,32 +-21,59 @@struct qnodes {>

Quoted 8 lines

static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);>
-static inline int encode_tail_cpu(void)
+-static inline u32 encode_tail_cpu(void)
{> return (smp_processor_id() +- 1) <<_Q_TAIL_CPU_OFFSET;> }>
-static inline int get_tail_cpu(int val)
+-static inline int get_tail_cpu(u32 val)
{> return (val >>_Q_TAIL_CPU_OFFSET) - 1;> }>
/*Take the lock by setting the bit, no other CPUs may concurrently lock it. */

I think you missed deleting the above line.

Quoted 8 lines

+-/*Take the lock by setting the lock bit, no other CPUs will touch it. */ static __always_inline void lock_set_locked(struct qspinlock *lock)
{>- atomic_or(_Q_LOCKED_VAL, &lock->val);>- __atomic_acquire_fence();>+- u32 new =_Q_LOCKED_VAL;>+- u32 prev;>+-

+-	asm volatile(
+-"1:	lwarx	%0,0,%1,%3	#lock_set_locked			\n">+-"or	%0,%0,%2						\n">+-"stwcx.	%0,0,%1							\n">+-"bne-	1b							\n">+-"\t"PPC_ACQUIRE_BARRIER "					\n">+-	: "=&r"(prev)
+-	: "r"(&lock->val), "r"(new),
+-	  "i"(IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+-	: "cr0" "memory";> }

This is pretty similar with the DEFINE_TESTOP() pattern from arch/powerpc/include/asm/bitops.h (such as test_and_set_bits_lock()) except for word instead of double word. Do you think it's possible / beneficial to make use of those macros?

arch/powerpc/lib/qspinlock.c

Quoted 9 lines
arch/powerpc/lib/qspinlock.c +2 -2
-/*Take lock, clearing tail, cmpxchg with val (which must not be locked) */
-static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int val)
+-/*Take lock, clearing tail, cmpxchg with old (which must not be locked) */
+-static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 old)

{>- int newval =_Q_LOCKED_VAL;>-

arch/powerpc/lib/qspinlock.c +2 -1
-	if (atomic_cmpxchg_acquire(&lock->val, val, newval) ==val)
+-	u32 new =_Q_LOCKED_VAL;>+-	u32 prev;>+-
+-	BUG_ON(old &_Q_LOCKED_VAL);

The BUG_ON() could have been introduced in an earlier patch I think.

Quoted 4 lines
+-
+-	asm volatile(
+-"1:	lwarx	%0,0,%1,%4	#trylock_clear_tail_cpu		\n">+-"cmpw	0,%0,%2							\n">+-"bne-	2f							\n">+-"stwcx.	%3,0,%1							\n">+-"bne-	1b							\n">+-"\t"PPC_ACQUIRE_BARRIER "					\n">+-"2:									\n">+-	: "=&r"(prev)
+-	: "r"(&lock->val), "r"old), "r"(new),

Could this be like "r"_Q_TAIL_CPU_MASK) below? i.e. "r"(_Q_LOCKED_VAL)? Makes it clear new doesn't change.

Quoted 7 lines
+-	  "i"(IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+-	: "cr0" "memory";>+-
+-	if (likely(prev ==old))
		return 1;>-	else
arch/powerpc/lib/qspinlock.c +0 -1
-		return 0;>+-	return 0;> }> 

/*>@@-56,20 +-82,25 @@static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int va
*This provides a release barrier for publishing node, and an acquire barrier

Does the comment mean there needs to be an acquire barrier in this assembly?

Quoted 23 lines

*for getting the old node.
*/
-static __always_inline int publish_tail_cpu(struct qspinlock *lock, int tail)

+-static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail)
{>-	for (;; {>-		int val =atomic_read(&lock->val);>-		int newval =(val &~_Q_TAIL_CPU_MASK) |tail;>-		int old;>-
arch/powerpc/lib/qspinlock.c +6 -3
-		old =atomic_cmpxchg(&lock->val, val, newval);>-		if (old ==val)
-			return old;>-	}>+-	u32 prev, tmp;>+-
+-	asm volatile(
+-"\t"PPC_RELEASE_BARRIER "					\n">+-"1:	lwarx	%0,0,%2		#publish_tail_cpu			\n">+-"andc	%1,%0,%4						\n">+-"or	%1,%1,%3						\n">+-"stwcx.	%1,0,%2							\n">+-"bne-	1b							\n">+-	: "=&r"(prev), "=&r"tmp)
+-	: "r"(&lock->val), "r"(tail), "r"_Q_TAIL_CPU_MASK)
+-	: "cr0" "memory";>+-
+-	return prev;> }> 
-static struct qnode *get_tail_qnode(struct qspinlock *lock, int val)
+-static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)

{> int cpu =get_tail_cpu(val);> struct qnodes *qnodesp =per_cpu_ptr(&qnodes, cpu);>@@-88,7 +-119,7 @@static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
{> struct qnodes *qnodesp;> struct qnode *next, *node;>- int val, old, tail;>+- u32 val, old, tail;> int idx;>

	BUILD_BUG_ON(CONFIG_NR_CPUS >=(1U <<_Q_TAIL_CPU_BITS));>@@-134,7 +-165,7 @@static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
	}> 
	/*We're at the head of the waitqueue, wait for the lock. */
arch/powerpc/lib/qspinlock.c +1 -1
-	while ((val =atomic_read(&lock->val)) &_Q_LOCKED_VAL)
+-	while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL)
		cpu_relax();> 
	/*If we're the last queued, must clean up the tail. */
#24 lore raw

Re: [PATCH 05/17] powerpc/qspinlock: allow new waiters to steal the lock before queueing

In reply to Nicholas Piggin
Quoted 12 lines · Nicholas Piggin

Allow new waiters a number of spins on the lock word before queueing,
which particularly helps paravirt performance when physical CPUs are
oversubscribed.
---
arch/powerpc/lib/qspinlock.c |152 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+----

1 file changed, 141 insertions(+-), 11 deletions(-)
arch/powerpc/lib/qspinlock.c +1 -1
index 7c71e5e287df..1625cce714b2 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-19,8 +-19,17 @@struct qnodes {> struct qnode nodes[MAX_NODES];> };>

Quoted 2 lines
+-/*Tuning parameters */
+-static int STEAL_SPINS __read_mostly =(1<<5);>+-static bool MAYBE_STEALERS __read_mostly =true;

I can understand why, but macro case variables can be a bit confusing.

Quoted 12 lines

+-
static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);>

+-static __always_inline int get_steal_spins(void)
+-{>+-	return STEAL_SPINS;>+-}>+-
static inline u32 encode_tail_cpu(void)
{> 	return (smp_processor_id() +- 1) <<_Q_TAIL_CPU_OFFSET;>@@-76,6 +-85,39 @@static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 ol
	return 0;> }> 
+-static __always_inline u32 __trylock_cmpxchg(struct qspinlock *lock, u32 old, u32 new)
+-{>+-	u32 prev;>+-
+-	BUG_ON(old &_Q_LOCKED_VAL);>+-
+-	asm volatile(
+-"1:	lwarx	%0,0,%1,%4	#queued_spin_trylock_cmpxchg		\n"

s/queued_spin_trylock_cmpxchg/__trylock_cmpxchg/

btw what is the format you using for the '\n's in the inline asm?

Quoted 4 lines
+-"cmpw	0,%0,%2							\n">+-"bne-	2f							\n">+-"stwcx.	%3,0,%1							\n">+-"bne-	1b							\n">+-"\t"PPC_ACQUIRE_BARRIER "					\n">+-"2:									\n">+-	: "=&r"(prev)
+-	: "r"(&lock->val), "r"old), "r"(new),
+-	  "i"(IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+-	: "cr0" "memory";

This is very similar to trylock_clear_tail_cpu(). So maybe it is worth having some form of "test and set"primitive helper.

Quoted 8 lines
+-
+-	return prev;>+-}>+-
+-/*Take lock, preserving tail, cmpxchg with val (which must not be locked) */
+-static __always_inline int trylock_with_tail_cpu(struct qspinlock *lock, u32 val)
+-{>+-	u32 newval =_Q_LOCKED_VAL |(val &_Q_TAIL_CPU_MASK);>+-
+-	if (__trylock_cmpxchg(lock, val, newval) ==val)
+-		return 1;>+-	else
+-		return 0;

same optional style nit: return __trylock_cmpxchg(lock, val, newval) ==val

Quoted 10 lines
+-}>+-
/*>  *Publish our tail, replacing previous tail. Return previous value.
 *>@@-115,6 +-157,31 @@static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
	BUG();> }> 
+-static inline bool try_to_steal_lock(struct qspinlock *lock)
+-{>+-	int iters;>+-
+-	/*Attempt to steal the lock */
+-	for (;; {>+-		u32 val =READ_ONCE(lock->val);>+-
+-		if (unlikely(!val &_Q_LOCKED_VAL))) {>+-			if (trylock_with_tail_cpu(lock, val))
+-				return true;>+-			continue;>+-		}

The continue would bypass iters+-+-/cpu_relax but the next time around
if (unlikely(!val &_Q_LOCKED_VAL))) {should fail so everything should be fine?

Quoted 38 lines
+-
+-		cpu_relax();>+-
+-		iters+-+-;>+-
+-		if (iters >=get_steal_spins())
+-			break;>+-	}>+-
+-	return false;>+-}>+-
static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
{> 	struct qnodes *qnodesp;>@@-164,20 +-231,39 @@static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
		smp_rmb();/*acquire barrier for the mcs lock */
	}> 
arch/powerpc/lib/qspinlock.c +15 -9
-	/*We're at the head of the waitqueue, wait for the lock. */
-	while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL)
-		cpu_relax();>+-	if (!MAYBE_STEALERS) {>+-		/*We're at the head of the waitqueue, wait for the lock. */
+-		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL)
+-			cpu_relax();> 
-	/*If we're the last queued, must clean up the tail. */
-	if ((val &_Q_TAIL_CPU_MASK) ==tail) {>-		if (trylock_clear_tail_cpu(lock, val))
-			goto release;>-		/*Another waiter must have enqueued */
-	}>+-		/*If we're the last queued, must clean up the tail. */
+-		if ((val &_Q_TAIL_CPU_MASK) ==tail) {>+-			if (trylock_clear_tail_cpu(lock, val))
+-				goto release;>+-			/*Another waiter must have enqueued. */
+-		}>+-
+-		/*We must be the owner, just set the lock bit and acquire */
+-		lock_set_locked(lock);>+-	}else {>+-again:
+-		/*We're at the head of the waitqueue, wait for the lock. */
+-		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL)
+-			cpu_relax();> 
-	/*We must be the owner, just set the lock bit and acquire */
-	lock_set_locked(lock);>+-		/*If we're the last queued, must clean up the tail. */
+-		if ((val &_Q_TAIL_CPU_MASK) ==tail) {>+-			if (trylock_clear_tail_cpu(lock, val))
+-				goto release;>+-			/*Another waiter must have enqueued, or lock stolen. */
+-		}else {>+-			if (trylock_with_tail_cpu(lock, val))
+-				goto unlock_next;>+-		}>+-		goto again;>+-	}> 
+-unlock_next:
	/*contended path;must wait for next !=NULL (MCS protocol) */
	while (!next =READ_ONCE(node->next)))
		cpu_relax();>@@-197,6 +-283,9 @@static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
Quoted 8 lines

void queued_spin_lock_slowpath(struct qspinlock *lock)
{>+- if (try_to_steal_lock(lock))
+- return;>+-

	queued_spin_lock_mcs_queue(lock);> }> EXPORT_SYMBOL(queued_spin_lock_slowpath);>@@-207,3 +-296,44 @@void pv_spinlocks_init(void)
}> #endif
+-#include <linux/debugfs.h>>+-static int steal_spins_set(void *data, u64 val)
+-{>+-	static DEFINE_MUTEX(lock);

I just want to check if it would be possible to get rid of the MAYBE_STEALERS variable completely and do something like:

bool maybe_stealers() {return STEAL_SPINS >0;}
I guess based on the below code it wouldn't work, but I'm still not quite sure why that is.

Quoted 4 lines
+-
+-	mutex_lock(&lock);>+-	if (val &&!STEAL_SPINS) {>+-		MAYBE_STEALERS =true;>+-		/*wait for waiter to go away */
+-		synchronize_rcu();>+-		STEAL_SPINS =val;>+-	}else if (!val &&STEAL_SPINS) {>+-		STEAL_SPINS =val;>+-		/*wait for all possible stealers to go away */
+-		synchronize_rcu();>+-		MAYBE_STEALERS =false;>+-	}else {>+-		STEAL_SPINS =val;>+-	}>+-	mutex_unlock(&lock);

STEAL_SPINS is an int not a u64.

Quoted 9 lines
+-
+-	return 0;>+-}>+-
+-static int steal_spins_get(void *data, u64 *val)
+-{>+-	*val =STEAL_SPINS;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_steal_spins, steal_spins_get, steal_spins_set, "%llu\n";>+-
+-static __init int spinlock_debugfs_init(void)
+-{>+-	debugfs_create_file("qspl_steal_spins" 0600, arch_debugfs_dir, NULL, &fops_steal_spins);>+-
+-	return 0;>+-}>+-device_initcall(spinlock_debugfs_init);>+-
#28 lore raw

Re: [PATCH 06/17] powerpc/qspinlock: theft prevention to control latency

In reply to Nicholas Piggin
Quoted 23 lines · Nicholas Piggin

Give the queue head the ability to stop stealers. After a number of spins without sucessfully acquiring the lock, the queue head employs this, which will assure it is the next owner.
---
arch/powerpc/include/asm/qspinlock_types.h |10 +-+-+--
arch/powerpc/lib/qspinlock.c |56 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--

2 files changed, 63 insertions(+-), 3 deletions(-)
arch/powerpc/include/asm/qspinlock_types.h +1 -1
index 210adf05b235..8b20f5e22bba 100644
--- a/arch/powerpc/include/asm/qspinlock_types.h
+-+-+- b/arch/powerpc/include/asm/qspinlock_types.h

@@-29,7 +-29,8 @@typedef struct qspinlock {> *Bitfields in the lock word:
*> * 0: locked bit
- *16-31: tail cpu (+-1)

+- *   16: must queue bit
+- *17-31: tail cpu (+-1)

*/
#define _Q_SET_MASK(type) (((1U <<_Q_##type ##_BITS) - 1)\> <<_Q_##type ##_OFFSET)
@@-38,7 +-39,12 @@typedef struct qspinlock {> #define _Q_LOCKED_MASK _Q_SET_MASK(LOCKED)
#define _Q_LOCKED_VAL (1U <<_Q_LOCKED_OFFSET)

-#define _Q_TAIL_CPU_OFFSET 16

Quoted 8 lines
+-#define _Q_MUST_Q_OFFSET	16
+-#define _Q_MUST_Q_BITS		1
+-#define _Q_MUST_Q_MASK		_Q_SET_MASK(MUST_Q)
+-#define _Q_MUST_Q_VAL		(1U <<_Q_MUST_Q_OFFSET)
+-
+-#define _Q_TAIL_CPU_OFFSET	17

#define _Q_TAIL_CPU_BITS (32 - _Q_TAIL_CPU_OFFSET)
#define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU)

Not a big deal but some of these values could be calculated like in the generic version. e.g.

	#define _Q_PENDING_OFFSET	(_Q_LOCKED_OFFSET +-_Q_LOCKED_BITS)
Quoted 10 lines
arch/powerpc/lib/qspinlock.c +1 -1
index 1625cce714b2..a906cc8f15fa 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-22,6 +-22,7 @@struct qnodes {> /*Tuning parameters */
static int STEAL_SPINS __read_mostly =(1<<5);> static bool MAYBE_STEALERS __read_mostly =true;>+-static int HEAD_SPINS __read_mostly =(1<<8);> static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);>
@@-30,6 +-31,11 @@static __always_inline int get_steal_spins(void)
return STEAL_SPINS;> }>

Quoted 9 lines
+-static __always_inline int get_head_spins(void)
+-{>+-	return HEAD_SPINS;>+-}>+-
static inline u32 encode_tail_cpu(void)
{> 	return (smp_processor_id() +- 1) <<_Q_TAIL_CPU_OFFSET;>@@-142,6 +-148,23 @@static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail)
	return prev;> }> 
+-static __always_inline u32 lock_set_mustq(struct qspinlock *lock)
+-{>+-	u32 new =_Q_MUST_Q_VAL;>+-	u32 prev;>+-
+-	asm volatile(
+-"1:	lwarx	%0,0,%1		#lock_set_mustq			\n"

Is the EH bit not set because we don't hold the lock here?

Quoted 3 lines
+-"or	%0,%0,%2						\n">+-"stwcx.	%0,0,%1							\n">+-"bne-	1b							\n">+-	: "=&r"(prev)
+-	: "r"(&lock->val), "r"(new)
+-	: "cr0" "memory";

This is another usage close to the DEFINE_TESTOP() pattern.

Quoted 13 lines
+-
+-	return prev;>+-}>+-
static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
{> 	int cpu =get_tail_cpu(val);>@@-165,6 +-188,9 @@static inline bool try_to_steal_lock(struct qspinlock *lock)
	for (;; {> 		u32 val =READ_ONCE(lock->val);> 
+-		if (val &_Q_MUST_Q_VAL)
+-			break;>+-
		if (unlikely(!val &_Q_LOCKED_VAL))) {> 			if (trylock_with_tail_cpu(lock, val))
				return true;>@@-246,11 +-272,22 @@static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
		/*We must be the owner, just set the lock bit and acquire */
		lock_set_locked(lock);> 	}else {>+-		int iters =0;>+-		bool set_mustq =false;>+-
again:
		/*We're at the head of the waitqueue, wait for the lock. */

arch/powerpc/lib/qspinlock.c

Quoted 3 lines
arch/powerpc/lib/qspinlock.c +2 -1
-		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL)
+-		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL) {> 			cpu_relax();> 
+-			iters+-+-;

It seems instead of using set_mustq, (val &_Q_MUST_Q_VAL) could be checked?

Quoted 16 lines

+- if (!set_mustq &&iters >=get_head_spins()) {>+- set_mustq =true;>+- lock_set_mustq(lock);>+- val |=_Q_MUST_Q_VAL;>+- }>+- }>+-

		/*If we're the last queued, must clean up the tail. */
		if ((val &_Q_TAIL_CPU_MASK) ==tail) {> 			if (trylock_clear_tail_cpu(lock, val))

@@-329,9 +-366,26 @@static int steal_spins_get(void *data, u64 *val)

DEFINE_SIMPLE_ATTRIBUTE(fops_steal_spins, steal_spins_get, steal_spins_set, "%llu\n";>

+-static int head_spins_set(void *data, u64 val)
+-{>+-	HEAD_SPINS =val;>+-
+-	return 0;>+-}>+-
+-static int head_spins_get(void *data, u64 *val)
+-{>+-	*val =HEAD_SPINS;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_head_spins, head_spins_get, head_spins_set, "%llu\n";>+-
static __init int spinlock_debugfs_init(void)
{> 	debugfs_create_file("qspl_steal_spins" 0600, arch_debugfs_dir, NULL, &fops_steal_spins);>+-	debugfs_create_file("qspl_head_spins" 0600, arch_debugfs_dir, NULL, &fops_head_spins);> 
	return 0;> }
#3 lore raw

Re: [PATCH 01/17] powerpc/qspinlock: powerpc qspinlock implementation

In reply to Jordan NIethe

Le 10/08/2022 à 03:52, Jordan NIethe a écrit :

Quoted 17 lines · Nicholas Piggin

<snip>

arch/powerpc/include/asm/qspinlock.h line 28

Quoted 12 lines
arch/powerpc/include/asm/qspinlock.h +4 -6
-#define queued_spin_lock queued_spin_lock
 
-static inline void queued_spin_unlock(struct qspinlock *lock)
+static __always_inline int queued_spin_trylock(struct qspinlock *lock)
  {
-	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) || !is_shared_processor())
-		smp_store_release(&lock->locked, 0);
-	else
-		__pv_queued_spin_unlock(lock);
+	if (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0)
+		return 1;
+	return 0;

optional style nit: return (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0);

The parenthesis are pointless, and ! is usually prefered to == 0, something like that:

	return !atomic_cmpxchg_acquire(&lock->val, 0, 1);
#32 lore raw

Re: [PATCH 07/17] powerpc/qspinlock: store owner CPU in lock word

In reply to Nicholas Piggin
Quoted 13 lines · Nicholas Piggin

Store the owner CPU number in the lock word so it may be yielded to, as powerpc's paravirtualised simple spinlocks do.
---

arch/powerpc/include/asm/qspinlock.h       | 8 +-+-+-+-+-+-+--
arch/powerpc/include/asm/qspinlock_types.h |10 +-+-+-+-+-+-+-+-+-+-
arch/powerpc/lib/qspinlock.c               | 6 +-+-+----
3 files changed, 20 insertions(+-), 4 deletions(-)
arch/powerpc/include/asm/qspinlock.h +1 -1
index 3ab354159e5e..44601b261e08 100644
--- a/arch/powerpc/include/asm/qspinlock.h
+-+-+- b/arch/powerpc/include/asm/qspinlock.h

@@-20,9 +-20,15 @@static __always_inline int queued_spin_is_contended(struct qspinlock *lock)

Quoted 2 lines
	return !!READ_ONCE(lock->val) &_Q_TAIL_CPU_MASK);> }> 

+-static __always_inline u32 queued_spin_get_locked_val(void)

Maybe this function should have "encode"in the name to match with encode_tail_cpu().

Quoted 1 line

+-{>+- /*XXX: make this use lock value in paca like simple spinlocks? */

Is that the paca's lock_token which is 0x8000?

Quoted 10 lines

+- return _Q_LOCKED_VAL |(smp_processor_id() <<_Q_OWNER_CPU_OFFSET);>+-}>+-
static __always_inline int queued_spin_trylock(struct qspinlock *lock)
{>- u32 new =_Q_LOCKED_VAL;>+- u32 new =queued_spin_get_locked_val();> u32 prev;>

	asm volatile(
arch/powerpc/include/asm/qspinlock_types.h +1 -1
index 8b20f5e22bba..35f9525381e6 100644
--- a/arch/powerpc/include/asm/qspinlock_types.h
+-+-+- b/arch/powerpc/include/asm/qspinlock_types.h

@@-29,6 +-29,8 @@typedef struct qspinlock {> *Bitfields in the lock word:
*> * 0: locked bit

Quoted 4 lines
+- * 1-14: lock holder cpu
+- *   15: unused bit
 *   16: must queue bit

*17-31: tail cpu (+-1)

So there is one more bit to store the tail cpu vs the lock holder cpu?

Quoted 33 lines

*/
@@-39,6 +-41,14 @@typedef struct qspinlock {> #define _Q_LOCKED_MASK _Q_SET_MASK(LOCKED)
#define _Q_LOCKED_VAL (1U <<_Q_LOCKED_OFFSET)

+-#define _Q_OWNER_CPU_OFFSET	1
+-#define _Q_OWNER_CPU_BITS	14
+-#define _Q_OWNER_CPU_MASK	_Q_SET_MASK(OWNER_CPU)
+-
+-#if CONFIG_NR_CPUS >(1U <<_Q_OWNER_CPU_BITS)
+-#error "qspinlock does not support such large CONFIG_NR_CPUS">+-#endif
+-

#define _Q_MUST_Q_OFFSET 16
#define _Q_MUST_Q_BITS 1
#define _Q_MUST_Q_MASK _Q_SET_MASK(MUST_Q)

arch/powerpc/lib/qspinlock.c +1 -1
index a906cc8f15fa..aa26cfe21f18 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-50,7 +-50,7 @@static inline int get_tail_cpu(u32 val) /*Take the lock by setting the lock bit, no other CPUs will touch it. */ static __always_inline void lock_set_locked(struct qspinlock *lock)
{>- u32 new =_Q_LOCKED_VAL;>+- u32 new =queued_spin_get_locked_val();> u32 prev;>

	asm volatile(

@@-68,7 +-68,7 @@static __always_inline void lock_set_locked(struct qspinlock *lock) /*Take lock, clearing tail, cmpxchg with old (which must not be locked) */ static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 old)
{>- u32 new =_Q_LOCKED_VAL;>+- u32 new =queued_spin_get_locked_val();> u32 prev;>

	BUG_ON(old &_Q_LOCKED_VAL);>@@-116,7 +-116,7 @@static __always_inline u32 __trylock_cmpxchg(struct qspinlock *lock, u32 old, u3
/*Take lock, preserving tail, cmpxchg with val (which must not be locked) */
static __always_inline int trylock_with_tail_cpu(struct qspinlock *lock, u32 val)
{>-	u32 newval =_Q_LOCKED_VAL |(val &_Q_TAIL_CPU_MASK);>+-	u32 newval =queued_spin_get_locked_val() |(val &_Q_TAIL_CPU_MASK);> 
	if (__trylock_cmpxchg(lock, val, newval) ==val)
		return 1;
#36 lore raw

Re: [PATCH 08/17] powerpc/qspinlock: paravirt yield to lock owner

In reply to Nicholas Piggin
Quoted 14 lines · Nicholas Piggin

Waiters spinning on the lock word should yield to the lock owner if the
vCPU is preempted. This improves performance when the hypervisor has
oversubscribed physical CPUs.
---
arch/powerpc/lib/qspinlock.c |97 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-------

1 file changed, 83 insertions(+-), 14 deletions(-)
arch/powerpc/lib/qspinlock.c +1 -1
index aa26cfe21f18..55286ac91da5 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-5,6 +-5,7 @@> #include <linux/percpu.h>> #include <linux/smp.h>> #include <asm/qspinlock.h>>+-#include <asm/paravirt.h>>
#define MAX_NODES 4

Quoted 2 lines

@@-24,14 +-25,16 @@static int STEAL_SPINS __read_mostly =(1<<5);> static bool MAYBE_STEALERS __read_mostly =true;> static int HEAD_SPINS __read_mostly =(1<<8);> +-static bool pv_yield_owner __read_mostly =true;

Not macro case for these globals? To me name does not make it super clear this is a boolean. What about pv_yield_owner_enabled?

Quoted 16 lines

+-
static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);> -static __always_inline int get_steal_spins(void) +-static __always_inline int get_steal_spins(bool paravirt)
{> return STEAL_SPINS;> }>
-static __always_inline int get_head_spins(void) +-static __always_inline int get_head_spins(bool paravirt)
{> return HEAD_SPINS;> }>@@-46,7 +-49,11 @@static inline int get_tail_cpu(u32 val)

	return (val >>_Q_TAIL_CPU_OFFSET) - 1;> }> 
-/*Take the lock by setting the bit, no other CPUs may concurrently lock it. */
+-static inline int get_owner_cpu(u32 val)
+-{>+-	return (val &_Q_OWNER_CPU_MASK) >>_Q_OWNER_CPU_OFFSET;>+-}>+-
/*Take the lock by setting the lock bit, no other CPUs will touch it. */
static __always_inline void lock_set_locked(struct qspinlock *lock)
{>@@-180,7 +-187,45 @@static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
	BUG();> }> 
Quoted 2 lines

-static inline bool try_to_steal_lock(struct qspinlock *lock)
+-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)

This name doesn't seem correct for the non paravirt case.

Quoted 9 lines
+-{>+-	int owner;>+-	u32 yield_count;>+-
+-	BUG_ON(!val &_Q_LOCKED_VAL));>+-
+-	if (!paravirt)
+-		goto relax;>+-
+-	if (!pv_yield_owner)
+-		goto relax;>+-
+-	owner =get_owner_cpu(val);>+-	yield_count =yield_count_of(owner);>+-
+-	if ((yield_count &1) ==0)
+-		goto relax;/*owner vcpu is running */

I wonder why not use vcpu_is_preempted()?

Quoted 37 lines
+-
+-	/*>+-	 *Read the lock word after sampling the yield count. On the other side
+-	 *there may a wmb because the yield count update is done by the
+-	 *hypervisor preemption and the value update by the OS, however this
+-	 *ordering might reduce the chance of out of order accesses and
+-	 *improve the heuristic.
+-	 */
+-	smp_rmb();>+-
+-	if (READ_ONCE(lock->val) ==val) {>+-		yield_to_preempted(owner, yield_count);>+-		/*Don't relax if we yielded. Maybe we should? */
+-		return;>+-	}>+-relax:
+-	cpu_relax();>+-}>+-
+-
+-static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
{> 	int iters;> 
@@-197,18 +-242,18 @@static inline bool try_to_steal_lock(struct qspinlock *lock)
			continue;> 		}> 
arch/powerpc/lib/qspinlock.c +0 -1
-		cpu_relax();>+-		yield_to_locked_owner(lock, val, paravirt);> 
		iters+-+-;> 
arch/powerpc/lib/qspinlock.c +1 -1
-		if (iters >=get_steal_spins())
+-		if (iters >=get_steal_spins(paravirt))
			break;> 	}> 
	return false;> }> 
-static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
+-static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, bool paravirt)
{> 	struct qnodes *qnodesp;> 	struct qnode *next, *node;>@@-260,7 +-305,7 @@static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
	if (!MAYBE_STEALERS) {> 		/*We're at the head of the waitqueue, wait for the lock. */
		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL)
arch/powerpc/lib/qspinlock.c +0 -1
-			cpu_relax();>+-			yield_to_locked_owner(lock, val, paravirt);> 
		/*If we're the last queued, must clean up the tail. */
		if ((val &_Q_TAIL_CPU_MASK) ==tail) {>@@-278,10 +-323,10 @@static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
again:
		/*We're at the head of the waitqueue, wait for the lock. */
		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL) {>-			cpu_relax();>+-			yield_to_locked_owner(lock, val, paravirt);> 
			iters+-+-;>-			if (!set_mustq &&iters >=get_head_spins()) {>+-			if (!set_mustq &&iters >=get_head_spins(paravirt)) {> 				set_mustq =true;> 				lock_set_mustq(lock);> 				val |=_Q_MUST_Q_VAL;>@@-320,10 +-365,15 @@static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)

void queued_spin_lock_slowpath(struct qspinlock *lock)
{>- if (try_to_steal_lock(lock))

arch/powerpc/lib/qspinlock.c

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +2 -2
-		return;>-
-	queued_spin_lock_mcs_queue(lock);>+-	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&is_shared_processor()) {>+-		if (try_to_steal_lock(lock, true))
+-			return;>+-		queued_spin_lock_mcs_queue(lock, true);>+-	}else {>+-		if (try_to_steal_lock(lock, false))
+-			return;>+-		queued_spin_lock_mcs_queue(lock, false);>+-	}> }

There is not really a need for a conditional:

bool paravirt =IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&is_shared_processor(); if (try_to_steal_lock(lock, paravirt))

	return;

queued_spin_lock_mcs_queue(lock, paravirt);

The paravirt parameter used by the various functions seems always to be
equivalent to (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&is_shared_processor()).
I wonder if it would be simpler testing (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&is_shared_processor()) (using a helper function) in those functions instead passing it as a parameter?

Quoted 14 lines

EXPORT_SYMBOL(queued_spin_lock_slowpath);>
@@-382,10 +-432,29 @@static int head_spins_get(void *data, u64 *val)

DEFINE_SIMPLE_ATTRIBUTE(fops_head_spins, head_spins_get, head_spins_set, "%llu\n";>

+-static int pv_yield_owner_set(void *data, u64 val)
+-{>+-	pv_yield_owner =!!val;>+-
+-	return 0;>+-}>+-
+-static int pv_yield_owner_get(void *data, u64 *val)
+-{>+-	*val =pv_yield_owner;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_owner, pv_yield_owner_get, pv_yield_owner_set, "%llu\n";>+-
static __init int spinlock_debugfs_init(void)
{> 	debugfs_create_file("qspl_steal_spins" 0600, arch_debugfs_dir, NULL, &fops_steal_spins);> 	debugfs_create_file("qspl_head_spins" 0600, arch_debugfs_dir, NULL, &fops_head_spins);>+-	if (is_shared_processor()) {>+-		debugfs_create_file("qspl_pv_yield_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);>+-	}> 
	return 0;> }
#40 lore raw

Re: [PATCH 09/17] powerpc/qspinlock: implement option to yield to previous node

In reply to Nicholas Piggin
Quoted 14 lines · Nicholas Piggin

Queued waiters which are not at the head of the queue don't spin on the lock word but their qnode lock word, waiting for the previous queued CPU to release them. Add an option which allows these waiters to yield to the previous CPU if its vCPU is preempted.

Disable this option by default for now, i.e., no logical change.
---
arch/powerpc/lib/qspinlock.c |46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--

1 file changed, 45 insertions(+-), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +1 -1
index 55286ac91da5..b39f8c5b329c 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c
Quoted 2 lines

@@-26,6 +-26,7 @@static bool MAYBE_STEALERS __read_mostly =true;> static int HEAD_SPINS __read_mostly =(1<<8);> static bool pv_yield_owner __read_mostly =true;>+-static bool pv_yield_prev __read_mostly =true;

Similiar suggestion, maybe pv_yield_prev_enabled would read better.

Isn't this enabled by default contrary to the commit message?

Quoted 5 lines

static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);>
@@-224,6 +-225,31 @@static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va

	cpu_relax();> }> 

+-static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)

yield_to_locked_owner() takes a raw val and works out the cpu to yield to. I think for consistency have yield_to_prev() take the raw val and work it out too.

Quoted 14 lines
+-{>+-	u32 yield_count;>+-
+-	if (!paravirt)
+-		goto relax;>+-
+-	if (!pv_yield_prev)
+-		goto relax;>+-
+-	yield_count =yield_count_of(prev_cpu);>+-	if ((yield_count &1) ==0)
+-		goto relax;/*owner vcpu is running */
+-
+-	smp_rmb();/*See yield_to_locked_owner comment */
+-
+-	if (!node->locked) {>+-		yield_to_preempted(prev_cpu, yield_count);>+-		return;>+-	}>+-
+-relax:
+-	cpu_relax();>+-}>+-
Quoted 4 lines

static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt) {>@@-291,13 +-317,14 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b

	 */
	if (old &_Q_TAIL_CPU_MASK) {> 		struct qnode *prev =get_tail_qnode(lock, old);>+-		int prev_cpu =get_tail_cpu(old);

This could then be removed.

arch/powerpc/lib/qspinlock.c

Quoted 6 lines
		/*Link @node into the waitqueue. */
		WRITE_ONCE(prev->next, node);> 
		/*Wait for mcs node lock to be released */
		while (!node->locked)
arch/powerpc/lib/qspinlock.c +0 -1
-			cpu_relax();>+-			yield_to_prev(lock, node, prev_cpu, paravirt);

And would have this as:

			yield_to_prev(lock, node, old, paravirt);
Quoted 15 lines
		smp_rmb();/*acquire barrier for the mcs lock */
	}>@@-448,12 +-475,29 @@static int pv_yield_owner_get(void *data, u64 *val)

DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_owner, pv_yield_owner_get, pv_yield_owner_set, "%llu\n";>

+-static int pv_yield_prev_set(void *data, u64 val)
+-{>+-	pv_yield_prev =!!val;>+-
+-	return 0;>+-}>+-
+-static int pv_yield_prev_get(void *data, u64 *val)
+-{>+-	*val =pv_yield_prev;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_prev, pv_yield_prev_get, pv_yield_prev_set, "%llu\n";>+-
static __init int spinlock_debugfs_init(void)
{> 	debugfs_create_file("qspl_steal_spins" 0600, arch_debugfs_dir, NULL, &fops_steal_spins);> 	debugfs_create_file("qspl_head_spins" 0600, arch_debugfs_dir, NULL, &fops_head_spins);> 	if (is_shared_processor()) {> 		debugfs_create_file("qspl_pv_yield_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);>+-		debugfs_create_file("qspl_pv_yield_prev" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);> 	}> 
	return 0;
#44 lore raw

Re: [PATCH 10/17] powerpc/qspinlock: allow stealing when head of queue yields

In reply to Nicholas Piggin
Quoted 14 lines · Nicholas Piggin

If the head of queue is preventing stealing but it finds the owner vCPU is preempted, it will yield its cycles to the owner which could cause it to become preempted. Add an option to re-allow stealers before yielding, and disallow them again after returning from the yield.

Disable this option by default for now, i.e., no logical change.
---
arch/powerpc/lib/qspinlock.c |56 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+---

1 file changed, 53 insertions(+-), 3 deletions(-)
arch/powerpc/lib/qspinlock.c +1 -1
index b39f8c5b329c..94f007f66942 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c
Quoted 2 lines

@@-26,6 +-26,7 @@static bool MAYBE_STEALERS __read_mostly =true;> static int HEAD_SPINS __read_mostly =(1<<8);> static bool pv_yield_owner __read_mostly =true;>+-static bool pv_yield_allow_steal __read_mostly =false;

To me this one does read as a boolean, but if you go with those other changes I'd make it pv_yield_steal_enable to be consistent.

Quoted 9 lines

static bool pv_yield_prev __read_mostly =true;>
static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);>@@-173,6 +-174,23 @@static __always_inline u32 lock_set_mustq(struct qspinlock *lock)
return prev;> }>

+-static __always_inline u32 lock_clear_mustq(struct qspinlock *lock)
+-{>+-	u32 new =_Q_MUST_Q_VAL;>+-	u32 prev;>+-
+-	asm volatile(
+-"1:	lwarx	%0,0,%1		#lock_clear_mustq			\n">+-"andc	%0,%0,%2						\n">+-"stwcx.	%0,0,%1							\n">+-"bne-	1b							\n">+-	: "=&r"(prev)
+-	: "r"(&lock->val), "r"(new)
+-	: "cr0" "memory";>+-

This is pretty similar to the DEFINE_TESTOP() pattern again with the same llong caveat.

Quoted 6 lines

+- return prev;>+-}>+-
static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
{> int cpu =get_tail_cpu(val);>@@-188,7 +-206,7 @@static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)

	BUG();> }> 

-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt) +-static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)

/*See yield_to_locked_owner comment */ comment needs to be updated now.

Quoted 11 lines

{> int owner;> u32 yield_count;>@@-217,7 +-235,11 @@static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va

	smp_rmb();> 
	if (READ_ONCE(lock->val) ==val) {>+-		if (clear_mustq)
+-			lock_clear_mustq(lock);> 		yield_to_preempted(owner, yield_count);>+-		if (clear_mustq)
+-			lock_set_mustq(lock);> 		/*Don't relax if we yielded. Maybe we should? */
		return;> 	}>@@-225,6 +-247,16 @@static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va
	cpu_relax();> }> 
+-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+-{>+-	__yield_to_locked_owner(lock, val, paravirt, false);>+-}>+-
+-static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+-{

The check for pv_yield_allow_steal seems like it could go here instead of being done by the caller.
__yield_to_locked_owner() checks for pv_yield_owner so it seems more
consistent.

Quoted 25 lines

+- __yield_to_locked_owner(lock, val, paravirt, clear_mustq);>+-}>+-
static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
{> u32 yield_count;>@@-332,7 +-364,7 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b

	if (!MAYBE_STEALERS) {> 		/*We're at the head of the waitqueue, wait for the lock. */
		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL)
arch/powerpc/lib/qspinlock.c +0 -1
-			yield_to_locked_owner(lock, val, paravirt);>+-			yield_head_to_locked_owner(lock, val, paravirt, false);> 
		/*If we're the last queued, must clean up the tail. */
		if ((val &_Q_TAIL_CPU_MASK) ==tail) {>@@-350,7 +-382,8 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
again:
		/*We're at the head of the waitqueue, wait for the lock. */
		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL) {>-			yield_to_locked_owner(lock, val, paravirt);>+-			yield_head_to_locked_owner(lock, val, paravirt,
+-					pv_yield_allow_steal &&set_mustq);> 
			iters+-+-;> 			if (!set_mustq &&iters >=get_head_spins(paravirt)) {>@@-475,6 +-508,22 @@static int pv_yield_owner_get(void *data, u64 *val)

DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_owner, pv_yield_owner_get, pv_yield_owner_set, "%llu\n";>

+-static int pv_yield_allow_steal_set(void *data, u64 val)
+-{>+-	pv_yield_allow_steal =!!val;>+-
+-	return 0;>+-}>+-
+-static int pv_yield_allow_steal_get(void *data, u64 *val)
+-{>+-	*val =pv_yield_allow_steal;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_allow_steal, pv_yield_allow_steal_get, pv_yield_allow_steal_set, "%llu\n";>+-
static int pv_yield_prev_set(void *data, u64 val)
{> 	pv_yield_prev =!!val;>@@-497,6 +-546,7 @@static __init int spinlock_debugfs_init(void)
	debugfs_create_file("qspl_head_spins" 0600, arch_debugfs_dir, NULL, &fops_head_spins);> 	if (is_shared_processor()) {> 		debugfs_create_file("qspl_pv_yield_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);>+-		debugfs_create_file("qspl_pv_yield_allow_steal" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);> 		debugfs_create_file("qspl_pv_yield_prev" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);> 	}> 
#48 lore raw

Re: [PATCH 11/17] powerpc/qspinlock: allow propagation of yield CPU down the queue

In reply to Nicholas Piggin
Quoted 21 lines · Nicholas Piggin

Having all CPUs poll the lock word for the owner CPU that should be yielded to defeats most of the purpose of using MCS queueing for scalability. Yet it may be desirable for queued waiters to to yield to a preempted owner.

s390 addreses this problem by having queued waiters sample the lock word to find the owner much less frequently. In this approach, the waiters never sample it directly, but the queue head propagates the owner CPU back to the next waiter if it ever finds the owner has been preempted. Queued waiters then subsequently propagate the owner CPU back to the next waiter, and so on.

Disable this option by default for now, i.e., no logical change.
---
arch/powerpc/lib/qspinlock.c |85 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--

1 file changed, 84 insertions(+-), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +1 -1
index 94f007f66942..28c85a2d5635 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c
Quoted 3 lines

@@-12,6 +-12,7 @@> struct qnode {> struct qnode *next;> struct qspinlock *lock;>+- int yield_cpu;> u8 locked;/*1 if lock acquired */
};>
@@-28,6 +-29,7 @@static int HEAD_SPINS __read_mostly =(1<<8);> static bool pv_yield_owner __read_mostly =true;> static bool pv_yield_allow_steal __read_mostly =false;> static bool pv_yield_prev __read_mostly =true;>+-static bool pv_yield_propagate_owner __read_mostly =true;

This also seems to be enabled by default.

Quoted 4 lines

static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);>
@@-257,13 +-259,66 @@static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u
__yield_to_locked_owner(lock, val, paravirt, clear_mustq);> }>

Quoted 10 lines
+-static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int *set_yield_cpu, bool paravirt)
+-{>+-	struct qnode *next;>+-	int owner;>+-
+-	if (!paravirt)
+-		return;>+-	if (!pv_yield_propagate_owner)
+-		return;>+-
+-	owner =get_owner_cpu(val);>+-	if (*set_yield_cpu ==owner)
+-		return;>+-
+-	next =READ_ONCE(node->next);>+-	if (!next)
+-		return;>+-
+-	if (vcpu_is_preempted(owner)) {

Is there a difference about using vcpu_is_preempted() here vs checking bit 0 in other places?

Quoted 1 line

+- next->yield_cpu =owner;>+- *set_yield_cpu =owner;>+- }else if (*set_yield_cpu !=-1) {

It might be worth giving the -1 CPU a #define.

Quoted 1 line

+- next->yield_cpu =owner;>+- *set_yield_cpu =owner;>+- }>+-}

Does this need to pass set_yield_cpu by reference? Couldn't it's new value be returned? To me it makes it more clear the function is used to change set_yield_cpu. I think this would work:

int set_yield_cpu =-1;
static __always_inline int propagate_yield_cpu(struct qnode *node, u32 val, int set_yield_cpu, bool paravirt)

{	struct qnode *next;	int owner;
	if (!paravirt)
		goto out;	if (!pv_yield_propagate_owner)
		goto out;
	owner =get_owner_cpu(val);	if (set_yield_cpu ==owner)
		goto out;
	next =READ_ONCE(node->next);	if (!next)
		goto out;
	if (vcpu_is_preempted(owner)) {		next->yield_cpu =owner;		return owner;	}else if (set_yield_cpu !=-1) {		next->yield_cpu =owner;		return owner;	}
out:
	return set_yield_cpu;}

set_yield_cpu =propagate_yield_cpu(... set_yield_cpu ...);

Quoted 55 lines

+-
static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
{> u32 yield_count;>+- int yield_cpu;>
if (!paravirt)
goto relax;>

+-	if (!pv_yield_propagate_owner)
+-		goto yield_prev;>+-
+-	yield_cpu =READ_ONCE(node->yield_cpu);>+-	if (yield_cpu ==-1) {>+-		/*Propagate back the -1 CPU */
+-		if (node->next &&node->next->yield_cpu !=-1)
+-			node->next->yield_cpu =yield_cpu;>+-		goto yield_prev;>+-	}>+-
+-	yield_count =yield_count_of(yield_cpu);>+-	if ((yield_count &1) ==0)
+-		goto yield_prev;/*owner vcpu is running */
+-
+-	smp_rmb();>+-
+-	if (yield_cpu ==node->yield_cpu) {>+-		if (node->next &&node->next->yield_cpu !=yield_cpu)
+-			node->next->yield_cpu =yield_cpu;>+-		yield_to_preempted(yield_cpu, yield_count);>+-		return;>+-	}>+-
+-yield_prev:
	if (!pv_yield_prev)
		goto relax;> 
@@-337,6 +-392,7 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
	node =&qnodesp->nodes[idx];> 	node->next =NULL;> 	node->lock =lock;>+-	node->yield_cpu =-1;> 	node->locked =0;> 
	tail =encode_tail_cpu();>@@-358,13 +-414,21 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
		while (!node->locked)
			yield_to_prev(lock, node, prev_cpu, paravirt);> 
+-		/*Clear out stale propagated yield_cpu */
+-		if (paravirt &&pv_yield_propagate_owner &&node->yield_cpu !=-1)
+-			node->yield_cpu =-1;>+-
		smp_rmb();/*acquire barrier for the mcs lock */
	}> 
	if (!MAYBE_STEALERS) {>+-		int set_yield_cpu =-1;>+-
		/*We're at the head of the waitqueue, wait for the lock. */
arch/powerpc/lib/qspinlock.c +1 -1
-		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL)
+-		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL) {>+-			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);> 			yield_head_to_locked_owner(lock, val, paravirt, false);>+-		}> 
		/*If we're the last queued, must clean up the tail. */
		if ((val &_Q_TAIL_CPU_MASK) ==tail) {>@@-376,12 +-440,14 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
		/*We must be the owner, just set the lock bit and acquire */
		lock_set_locked(lock);> 	}else {>+-		int set_yield_cpu =-1;> 		int iters =0;> 		bool set_mustq =false;> 
again:
		/*We're at the head of the waitqueue, wait for the lock. */
		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL) {>+-			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);> 			yield_head_to_locked_owner(lock, val, paravirt,
					pv_yield_allow_steal &&set_mustq);> 

@@-540,6 +-606,22 @@static int pv_yield_prev_get(void *data, u64 *val)

DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_prev, pv_yield_prev_get, pv_yield_prev_set, "%llu\n";>

+-static int pv_yield_propagate_owner_set(void *data, u64 val)
+-{>+-	pv_yield_propagate_owner =!!val;>+-
+-	return 0;>+-}>+-
+-static int pv_yield_propagate_owner_get(void *data, u64 *val)
+-{>+-	*val =pv_yield_propagate_owner;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_propagate_owner, pv_yield_propagate_owner_get, pv_yield_propagate_owner_set, "%llu\n";>+-
static __init int spinlock_debugfs_init(void)
{> 	debugfs_create_file("qspl_steal_spins" 0600, arch_debugfs_dir, NULL, &fops_steal_spins);>@@-548,6 +-630,7 @@static __init int spinlock_debugfs_init(void)
		debugfs_create_file("qspl_pv_yield_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);> 		debugfs_create_file("qspl_pv_yield_allow_steal" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);> 		debugfs_create_file("qspl_pv_yield_prev" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);>+-		debugfs_create_file("qspl_pv_yield_propagate_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);> 	}> 
	return 0;
#53 lore raw

Re: [PATCH 12/17] powerpc/qspinlock: add ability to prod new queue head CPU

In reply to Nicholas Piggin
Quoted 18 lines · Nicholas Piggin

After the head of the queue acquires the lock, it releases the next waiter in the queue to become the new head. Add an option to prod the new head if its vCPU was preempted. This may only have an effect if queue waiters are yielding.

Disable this option by default for now, i.e., no logical change.
---
arch/powerpc/lib/qspinlock.c |29 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--

1 file changed, 28 insertions(+-), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +1 -1
index 28c85a2d5635..3b10e31bcf0a 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-12,6 +-12,7 @@> struct qnode {> struct qnode *next;> struct qspinlock *lock;>+- int cpu;> int yield_cpu;> u8 locked;/*1 if lock acquired */
};>@@-30,6 +-31,7 @@static bool pv_yield_owner __read_mostly =true;> static bool pv_yield_allow_steal __read_mostly =false;> static bool pv_yield_prev __read_mostly =true;> static bool pv_yield_propagate_owner __read_mostly =true;>+-static bool pv_prod_head __read_mostly =false;> static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);>
@@-392,6 +-394,7 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b

Quoted 1 line
	node =&qnodesp->nodes[idx];> 	node->next =NULL;> 	node->lock =lock;>+-	node->cpu =smp_processor_id();

I suppose this could be used in some other places too.

For example change:

	yield_to_prev(lock, node, prev, paravirt);

In yield_to_prev() it could then access the prev->cpu.

Quoted 23 lines
	node->yield_cpu =-1;> 	node->locked =0;> 

@@-483,7 +-486,14 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
*this store to locked. The corresponding barrier is the smp_rmb()
*acquire barrier for mcs lock, above.
*/

arch/powerpc/lib/qspinlock.c +1 -1
-	WRITE_ONCE(next->locked, 1);>+-	if (paravirt &&pv_prod_head) {>+-		int next_cpu =next->cpu;>+-		WRITE_ONCE(next->locked, 1);>+-		if (vcpu_is_preempted(next_cpu))
+-			prod_cpu(next_cpu);>+-	}else {>+-		WRITE_ONCE(next->locked, 1);>+-	}> 

release:

	qnodesp->count--;/*release the node */

@@-622,6 +-632,22 @@static int pv_yield_propagate_owner_get(void *data, u64 *val)

DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_propagate_owner, pv_yield_propagate_owner_get, pv_yield_propagate_owner_set, "%llu\n";>

+-static int pv_prod_head_set(void *data, u64 val)
+-{>+-	pv_prod_head =!!val;>+-
+-	return 0;>+-}>+-
+-static int pv_prod_head_get(void *data, u64 *val)
+-{>+-	*val =pv_prod_head;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_pv_prod_head, pv_prod_head_get, pv_prod_head_set, "%llu\n";>+-
static __init int spinlock_debugfs_init(void)
{> 	debugfs_create_file("qspl_steal_spins" 0600, arch_debugfs_dir, NULL, &fops_steal_spins);>@@-631,6 +-657,7 @@static __init int spinlock_debugfs_init(void)
		debugfs_create_file("qspl_pv_yield_allow_steal" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);> 		debugfs_create_file("qspl_pv_yield_prev" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);> 		debugfs_create_file("qspl_pv_yield_propagate_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);>+-		debugfs_create_file("qspl_pv_prod_head" 0600, arch_debugfs_dir, NULL, &fops_pv_prod_head);> 	}> 
	return 0;
#57 lore raw

Re: [PATCH 13/17] powerpc/qspinlock: trylock and initial lock attempt may steal

In reply to Nicholas Piggin
Quoted 12 lines · Nicholas Piggin

This gives trylock slightly more strength, and it also gives most
of the benefit of passing 'val' back through the slowpath without
the complexity.
---
arch/powerpc/include/asm/qspinlock.h |39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--
arch/powerpc/lib/qspinlock.c | 9 +-+-+-+-+-+-+-

2 files changed, 47 insertions(+-), 1 deletion(-)
arch/powerpc/include/asm/qspinlock.h +1 -1
index 44601b261e08..d3d2039237b2 100644
--- a/arch/powerpc/include/asm/qspinlock.h
+-+-+- b/arch/powerpc/include/asm/qspinlock.h
Quoted 2 lines

@@-5,6 +-5,8 @@> #include <linux/compiler.h>> #include <asm/qspinlock_types.h>> +-#define _Q_SPIN_TRY_LOCK_STEAL 1

Would this be a config option?

Quoted 10 lines

+-
static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
{> return READ_ONCE(lock->val);>@@-26,11 +-28,12 @@static __always_inline u32 queued_spin_get_locked_val(void)

	return _Q_LOCKED_VAL |(smp_processor_id() <<_Q_OWNER_CPU_OFFSET);> }> 

-static __always_inline int queued_spin_trylock(struct qspinlock *lock) +-static __always_inline int __queued_spin_trylock_nosteal(struct qspinlock *lock)
{> u32 new =queued_spin_get_locked_val();> u32 prev;>
+- /*Trylock succeeds only when unlocked and no queued nodes */

	asm volatile(

"1: lwarx %0,0,%1,%3 #queued_spin_trylock \n"

s/queued_spin_trylock/__queued_spin_trylock_nosteal

Quoted 7 lines

"cmpwi 0,%0,0 \n">@@-49,6 +-52,40 @@static __always_inline int queued_spin_trylock(struct qspinlock *lock)

	return 0;> }> 
+-static __always_inline int __queued_spin_trylock_steal(struct qspinlock *lock)
+-{>+-	u32 new =queued_spin_get_locked_val();>+-	u32 prev, tmp;>+-
+-	/*Trylock may get ahead of queued nodes if it finds unlocked */
+-	asm volatile(
+-"1:	lwarx	%0,0,%2,%5	#queued_spin_trylock			\n"

s/queued_spin_trylock/__queued_spin_trylock_steal

Quoted 1 line

+-"andc. %1,%0,%4 \n">+-"bne- 2f \n">+-"and %1,%0,%4 \n">+-"or %1,%1,%3 \n">+-"stwcx. %1,0,%2 \n">+-"bne- 1b \n">+-"\t"PPC_ACQUIRE_BARRIER " \n">+-"2: \n"

Just because there's a little bit more going on here...

Q_TAIL_CPU_MASK =0xFFFE0000
~Q_TAIL_CPU_MASK =0x1FFFF

1: lwarx prev, 0, &lock->val, IS_ENABLED_PPC64

	andc.	tmp, prev, _Q_TAIL_CPU_MASK 	(tmp =prev &~_Q_TAIL_CPU_MASK)
	bne-	2f 				(exit if locked)
	and	tmp, prev, _Q_TAIL_CPU_MASK 	(tmp =prev &_Q_TAIL_CPU_MASK)
	or	tmp, tmp, new			(tmp |=new)					
	stwcx.	tmp, 0, &lock->val					
		
	bne-	1b							
	PPC_ACQUIRE_BARRIER		

2:

... which seems correct.

Quoted 32 lines
+-	: "=&r"(prev), "=&r"(tmp)
+-	: "r"(&lock->val), "r"(new), "r"(_Q_TAIL_CPU_MASK),
+-	  "i"(IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+-	: "cr0" "memory";>+-
+-	if (likely(!prev &~_Q_TAIL_CPU_MASK)))
+-		return 1;>+-	return 0;>+-}>+-
+-static __always_inline int queued_spin_trylock(struct qspinlock *lock)
+-{>+-	if (!_Q_SPIN_TRY_LOCK_STEAL)
+-		return __queued_spin_trylock_nosteal(lock);>+-	else
+-		return __queued_spin_trylock_steal(lock);>+-}>+-

void queued_spin_lock_slowpath(struct qspinlock *lock);> static __always_inline void queued_spin_lock(struct qspinlock *lock)

arch/powerpc/lib/qspinlock.c +1 -1
index 3b10e31bcf0a..277aef1fab0a 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-24,7 +-24,11 @@struct qnodes {>
/*Tuning parameters */
static int STEAL_SPINS __read_mostly =(1<<5);>+-#if _Q_SPIN_TRY_LOCK_STEAL ==1 +-static const bool MAYBE_STEALERS =true;>+-#else
static bool MAYBE_STEALERS __read_mostly =true;>+-#endif
static int HEAD_SPINS __read_mostly =(1<<8);>
static bool pv_yield_owner __read_mostly =true;>@@-522,6 +-526,10 @@void pv_spinlocks_init(void) #include <linux/debugfs.h>> static int steal_spins_set(void *data, u64 val) {>+-#if _Q_SPIN_TRY_LOCK_STEAL ==1

+-	/*MAYBE_STEAL remains true */
+-	STEAL_SPINS =val;>+-#else
	static DEFINE_MUTEX(lock);> 
	mutex_lock(&lock);>@@-539,6 +-547,7 @@static int steal_spins_set(void *data, u64 val)
		STEAL_SPINS =val;> 	}> 	mutex_unlock(&lock);>+-#endif

	return 0;> }
#61 lore raw

Re: [PATCH 14/17] powerpc/qspinlock: use spin_begin/end API

In reply to Nicholas Piggin
Quoted 3 lines · Nicholas Piggin

Use the spin_begin/spin_cpu_relax/spin_end APIs in qspinlock, which helps to prevent threads issuing a lot of expensive priority nops which may not have much effect due to immediately executing low then medium priority.

Just a general comment regarding the spin_{begin,end}API, more complicated than something like

	spin_begin()
	for(;;
		spin_cpu_relax()
	spin_end()

it becomes difficult to keep track of. Unfortunately, I don't have any good suggestions how to improve it. Hopefully with P10s wait instruction we can maybe try and move away from this.

It might be useful to comment the functions pre and post conditions regarding expectations about spin_begin() and spin_end().

Quoted 79 lines

---
arch/powerpc/lib/qspinlock.c |35 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-----

1 file changed, 31 insertions(+-), 4 deletions(-)
arch/powerpc/lib/qspinlock.c +1 -1
index 277aef1fab0a..d4594c701f7d 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-233,6 +-233,8 @@static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32

	if ((yield_count &1) ==0)
		goto relax;/*owner vcpu is running */

+- spin_end();>+-

	/*> 	 *Read the lock word after sampling the yield count. On the other side
	 *there may a wmb because the yield count update is done by the
@@-248,11 +-250,13 @@static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
		yield_to_preempted(owner, yield_count);> 		if (clear_mustq)
			lock_set_mustq(lock);>+-		spin_begin();> 		/*Don't relax if we yielded. Maybe we should? */
		return;> 	}>+-	spin_begin();> relax:
arch/powerpc/lib/qspinlock.c +0 -1
-	cpu_relax();>+-	spin_cpu_relax();> }> 

static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
@@-315,14 +-319,18 @@static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *> if ((yield_count &1) ==0)
goto yield_prev;/*owner vcpu is running */

+- spin_end();>+-

	smp_rmb();> 
	if (yield_cpu ==node->yield_cpu) {> 		if (node->next &&node->next->yield_cpu !=yield_cpu)
			node->next->yield_cpu =yield_cpu;> 		yield_to_preempted(yield_cpu, yield_count);>+-		spin_begin();> 		return;> 	}>+-	spin_begin();> 
yield_prev:
	if (!pv_yield_prev)

@@-332,15 +-340,19 @@static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *> if ((yield_count &1) ==0)
goto relax;/*owner vcpu is running */

+- spin_end();>+-

	smp_rmb();/*See yield_to_locked_owner comment */

	if (!node->locked) {> 		yield_to_preempted(prev_cpu, yield_count);>+-		spin_begin();> 		return;> 	}>+-	spin_begin();> 

relax:

arch/powerpc/lib/qspinlock.c +0 -1
-	cpu_relax();>+-	spin_cpu_relax();> }> 

@@-349,6 +-361,7 @@static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav

	int iters;> 
	/*Attempt to steal the lock */
+-	spin_begin();> 	for (;; {> 		u32 val =READ_ONCE(lock->val);> 
@@-356,8 +-369,10 @@static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
			break;> 
		if (unlikely(!val &_Q_LOCKED_VAL))) {>+-			spin_end();> 			if (trylock_with_tail_cpu(lock, val))
				return true;>+-			spin_begin();> 			continue;> 		}> 
@@-368,6 +-383,7 @@static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
		if (iters >=get_steal_spins(paravirt))
			break;> 	}>+-	spin_end();> 
	return false;> }>@@-418,8 +-434,10 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
		WRITE_ONCE(prev->next, node);> 
		/*Wait for mcs node lock to be released */
+-		spin_begin();> 		while (!node->locked)
			yield_to_prev(lock, node, prev_cpu, paravirt);>+-		spin_end();> 
		/*Clear out stale propagated yield_cpu */
		if (paravirt &&pv_yield_propagate_owner &&node->yield_cpu !=-1)
@@-432,10 +-450,12 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
		int set_yield_cpu =-1;> 
		/*We're at the head of the waitqueue, wait for the lock. */
+-		spin_begin();> 		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL) {> 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);> 			yield_head_to_locked_owner(lock, val, paravirt, false);> 		}>+-		spin_end();> 
		/*If we're the last queued, must clean up the tail. */
		if ((val &_Q_TAIL_CPU_MASK) ==tail) {>@@-453,6 +-473,7 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b

again:

		/*We're at the head of the waitqueue, wait for the lock. */
+-		spin_begin();> 		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL) {> 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);> 			yield_head_to_locked_owner(lock, val, paravirt,
@@-465,6 +-486,7 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
				val |=_Q_MUST_Q_VAL;> 			}> 		}>+-		spin_end();> 
		/*If we're the last queued, must clean up the tail. */
		if ((val &_Q_TAIL_CPU_MASK) ==tail) {>@@-480,8 +-502,13 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b

unlock_next:

	/*contended path;must wait for next !=NULL (MCS protocol) */
arch/powerpc/lib/qspinlock.c +1 -2
-	while (!next =READ_ONCE(node->next)))
-		cpu_relax();>+-	next =READ_ONCE(node->next);>+-	if (!next) {>+-		spin_begin();>+-		while (!next =READ_ONCE(node->next)))
+-			cpu_relax();>+-		spin_end();>+-	}> 
	/*> 	 *Unlock the next mcs waiter node. Release barrier is not required
#65 lore raw

Re: [PATCH 15/17] powerpc/qspinlock: reduce remote node steal spins

In reply to Nicholas Piggin
Quoted 28 lines · Nicholas Piggin

Allow for a reduction in the number of times a CPU from a different
node than the owner can attempt to steal the lock before queueing.
This could bias the transfer behaviour of the lock across the
machine and reduce NUMA crossings.
---
arch/powerpc/lib/qspinlock.c |34 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+----

1 file changed, 31 insertions(+-), 3 deletions(-)
arch/powerpc/lib/qspinlock.c +1 -1
index d4594c701f7d..24f68bd71e2b 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-4,6 +-4,7 @@> #include <linux/export.h>> #include <linux/percpu.h>> #include <linux/smp.h>>+-#include <linux/topology.h>> #include <asm/qspinlock.h>> #include <asm/paravirt.h>> @@-24,6 +-25,7 @@struct qnodes {>
/*Tuning parameters */
static int STEAL_SPINS __read_mostly =(1<<5);>+-static int REMOTE_STEAL_SPINS __read_mostly =(1<<2);> #if _Q_SPIN_TRY_LOCK_STEAL ==1
static const bool MAYBE_STEALERS =true;> #else
@@-39,9 +-41,13 @@static bool pv_prod_head __read_mostly =false;>
static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);>
-static __always_inline int get_steal_spins(bool paravirt)
+-static __always_inline int get_steal_spins(bool paravirt, bool remote)
{>- return STEAL_SPINS;>+- if (remote) {>+- return REMOTE_STEAL_SPINS;>+- }else {>+- return STEAL_SPINS;>+- }> }>
static __always_inline int get_head_spins(bool paravirt)
@@-380,8 +-386,13 @@static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav

		iters+-+-;> 
arch/powerpc/lib/qspinlock.c +1 -1
-		if (iters >=get_steal_spins(paravirt))
+-		if (iters >=get_steal_spins(paravirt, false))
Quoted 1 line
			break;>+-		if (iters >=get_steal_spins(paravirt, true)) {

There's no indication of what true and false mean here which is hard to read. To me it feels like two separate functions would be more clear.

Quoted 1 line

+- int cpu =get_owner_cpu(val);>+- if (numa_node_id() !=cpu_to_node(cpu))

What about using node_distance() instead?

Quoted 6 lines

+- break;>+- }> }> spin_end();>
@@-588,6 +-599,22 @@static int steal_spins_get(void *data, u64 *val)

DEFINE_SIMPLE_ATTRIBUTE(fops_steal_spins, steal_spins_get, steal_spins_set, "%llu\n";>

+-static int remote_steal_spins_set(void *data, u64 val)
+-{>+-	REMOTE_STEAL_SPINS =val;

REMOTE_STEAL_SPINS is int not u64.

Quoted 9 lines
+-
+-	return 0;>+-}>+-
+-static int remote_steal_spins_get(void *data, u64 *val)
+-{>+-	*val =REMOTE_STEAL_SPINS;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_remote_steal_spins, remote_steal_spins_get, remote_steal_spins_set, "%llu\n";>+-

static int head_spins_set(void *data, u64 val)
{> HEAD_SPINS =val;>@@-687,6 +-714,7 @@DEFINE_SIMPLE_ATTRIBUTE(fops_pv_prod_head, pv_prod_head_get, pv_prod_head_set, "> static __init int spinlock_debugfs_init(void)
{> debugfs_create_file("qspl_steal_spins" 0600, arch_debugfs_dir, NULL, &fops_steal_spins);>+- debugfs_create_file("qspl_remote_steal_spins" 0600, arch_debugfs_dir, NULL, &fops_remote_steal_spins);> debugfs_create_file("qspl_head_spins" 0600, arch_debugfs_dir, NULL, &fops_head_spins);> if (is_shared_processor()) {> debugfs_create_file("qspl_pv_yield_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);

#69 lore raw

Re: [PATCH 16/17] powerpc/qspinlock: allow indefinite spinning on a preempted owner

In reply to Nicholas Piggin
Quoted 25 lines · Nicholas Piggin

Provide an option that holds off queueing indefinitely while the lock owner is preempted. This could reduce queueing latencies for very overcommitted vcpu situations.

This is disabled by default.
---
arch/powerpc/lib/qspinlock.c |91 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+------

1 file changed, 79 insertions(+-), 12 deletions(-)
arch/powerpc/lib/qspinlock.c +1 -1
index 24f68bd71e2b..5cfd69931e31 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c

@@-35,6 +-35,7 @@static int HEAD_SPINS __read_mostly =(1<<8);>
static bool pv_yield_owner __read_mostly =true;> static bool pv_yield_allow_steal __read_mostly =false;>+-static bool pv_spin_on_preempted_owner __read_mostly =false;> static bool pv_yield_prev __read_mostly =true;> static bool pv_yield_propagate_owner __read_mostly =true;> static bool pv_prod_head __read_mostly =false;>@@-220,13 +-221,15 @@static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)

	BUG();> }> 
-static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+-static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq, bool *preempted)
{> 	int owner;> 	u32 yield_count;> 
	BUG_ON(!val &_Q_LOCKED_VAL));> 

+- *preempted =false;>+-
if (!paravirt)
goto relax;>
@@-241,6 +-244,8 @@static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32

Quoted 8 lines
	spin_end();> 
+-	*preempted =true;>+-
	/*> 	 *Read the lock word after sampling the yield count. On the other side
	 *there may a wmb because the yield count update is done by the
@@-265,14 +-270,14 @@static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
	spin_cpu_relax();> }> 

-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt) +-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool *preempted)

It seems like preempted parameter could be the return value of yield_to_locked_owner(). Then callers that don't use the value returned in preempted don't need to create an unnecessary variable to pass in.

Quoted 55 lines

{>- __yield_to_locked_owner(lock, val, paravirt, false);>+- __yield_to_locked_owner(lock, val, paravirt, false, preempted);> }>
-static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq) +-static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq, bool *preempted)
{>- __yield_to_locked_owner(lock, val, paravirt, clear_mustq);>+- __yield_to_locked_owner(lock, val, paravirt, clear_mustq, preempted);> }>
static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int *set_yield_cpu, bool paravirt) @@-364,12 +-369,33 @@static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *>
static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
{>- int iters;>+- int iters =0;>+-

+-	if (!STEAL_SPINS) {>+-		if (paravirt &&pv_spin_on_preempted_owner) {>+-			spin_begin();>+-			for (;; {>+-				u32 val =READ_ONCE(lock->val);>+-				bool preempted;>+-
+-				if (val &_Q_MUST_Q_VAL)
+-					break;>+-				if (!val &_Q_LOCKED_VAL))
+-					break;>+-				if (!vcpu_is_preempted(get_owner_cpu(val)))
+-					break;>+-				yield_to_locked_owner(lock, val, paravirt, &preempted);>+-			}>+-			spin_end();>+-		}>+-		return false;>+-	}> 
	/*Attempt to steal the lock */
	spin_begin();> 	for (;; {> 		u32 val =READ_ONCE(lock->val);>+-		bool preempted;> 
		if (val &_Q_MUST_Q_VAL)
			break;>@@-382,9 +-408,22 @@static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
			continue;> 		}> 
arch/powerpc/lib/qspinlock.c +9 -2
-		yield_to_locked_owner(lock, val, paravirt);>-
-		iters+-+-;>+-		yield_to_locked_owner(lock, val, paravirt, &preempted);>+-
+-		if (paravirt &&preempted) {>+-			if (!pv_spin_on_preempted_owner)
+-				iters+-+-;>+-			/*>+-			 *pv_spin_on_preempted_owner don't increase iters
+-			 *while the owner is preempted -- we won't interfere
+-			 *with it by definition. This could introduce some
+-			 *latency issue if we continually observe preempted
+-			 *owners, but hopefully that's a rare corner case of
+-			 *a badly oversubscribed system.
+-			 */
+-		}else {>+-			iters+-+-;>+-		}> 
		if (iters >=get_steal_spins(paravirt, false))
			break;>@@-463,8 +-502,10 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
		/*We're at the head of the waitqueue, wait for the lock. */
		spin_begin();> 		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL) {>+-			bool preempted;>+-
			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);>-			yield_head_to_locked_owner(lock, val, paravirt, false);>+-			yield_head_to_locked_owner(lock, val, paravirt, false, &preempted);> 		}> 		spin_end();> 
@@-486,11 +-527,20 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
		/*We're at the head of the waitqueue, wait for the lock. */
		spin_begin();> 		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL) {>+-			bool preempted;>+-
			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);> 			yield_head_to_locked_owner(lock, val, paravirt,
arch/powerpc/lib/qspinlock.c +3 -2
-					pv_yield_allow_steal &&set_mustq);>+-					pv_yield_allow_steal &&set_mustq,
+-					&preempted);>+-
+-			if (paravirt &&preempted) {>+-				if (!pv_spin_on_preempted_owner)
+-					iters+-+-;>+-			}else {>+-				iters+-+-;>+-			}> 
-			iters+-+-;> 			if (!set_mustq &&iters >=get_head_spins(paravirt)) {> 				set_mustq =true;> 				lock_set_mustq(lock);>@@-663,6 +-713,22 @@static int pv_yield_allow_steal_get(void *data, u64 *val)

DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_allow_steal, pv_yield_allow_steal_get, pv_yield_allow_steal_set, "%llu\n";>

+-static int pv_spin_on_preempted_owner_set(void *data, u64 val)
+-{>+-	pv_spin_on_preempted_owner =!!val;>+-
+-	return 0;>+-}>+-
+-static int pv_spin_on_preempted_owner_get(void *data, u64 *val)
+-{>+-	*val =pv_spin_on_preempted_owner;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_pv_spin_on_preempted_owner, pv_spin_on_preempted_owner_get, pv_spin_on_preempted_owner_set, "%llu\n";>+-
static int pv_yield_prev_set(void *data, u64 val)
{> 	pv_yield_prev =!!val;>@@-719,6 +-785,7 @@static __init int spinlock_debugfs_init(void)
	if (is_shared_processor()) {> 		debugfs_create_file("qspl_pv_yield_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);> 		debugfs_create_file("qspl_pv_yield_allow_steal" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);>+-		debugfs_create_file("qspl_pv_spin_on_preempted_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_spin_on_preempted_owner);> 		debugfs_create_file("qspl_pv_yield_prev" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);> 		debugfs_create_file("qspl_pv_yield_propagate_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);> 		debugfs_create_file("qspl_pv_prod_head" 0600, arch_debugfs_dir, NULL, &fops_pv_prod_head);
#75 lore raw

Re: [PATCH 17/17] powerpc/qspinlock: provide accounting and options for sleepy locks

In reply to Nicholas Piggin
Quoted 35 lines · Nicholas Piggin

Finding the owner or a queued waiter on a lock with a preempted vcpu is indicative of an oversubscribed guest causing the lock to get into trouble. Provide some options to detect this situation and have new CPUs avoid queueing for a longer time (more steal iterations) to minimise the problems caused by vcpu preemption on the queue.
---
arch/powerpc/include/asm/qspinlock_types.h | 7 +--
arch/powerpc/lib/qspinlock.c |240 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+---

2 files changed, 232 insertions(+-), 15 deletions(-)
arch/powerpc/include/asm/qspinlock_types.h +1 -1
index 35f9525381e6..4fbcc8a4230b 100644
--- a/arch/powerpc/include/asm/qspinlock_types.h
+-+-+- b/arch/powerpc/include/asm/qspinlock_types.h

@@-30,7 +-30,7 @@typedef struct qspinlock {> *> * 0: locked bit
* 1-14: lock holder cpu
- * 15: unused bit

+- *   15: lock owner or queuer vcpus observed to be preempted bit
 *   16: must queue bit

*17-31: tail cpu (+-1)
*/
@@-49,6 +-49,11 @@typedef struct qspinlock {> #error "qspinlock does not support such large CONFIG_NR_CPUS"> #endif

+-#define _Q_SLEEPY_OFFSET	15
+-#define _Q_SLEEPY_BITS		1
+-#define _Q_SLEEPY_MASK		_Q_SET_MASK(SLEEPY_OWNER)
+-#define _Q_SLEEPY_VAL		(1U <<_Q_SLEEPY_OFFSET)
+-

#define _Q_MUST_Q_OFFSET 16
#define _Q_MUST_Q_BITS 1
#define _Q_MUST_Q_MASK _Q_SET_MASK(MUST_Q)

arch/powerpc/lib/qspinlock.c +1 -1
index 5cfd69931e31..c18133c01450 100644
--- a/arch/powerpc/lib/qspinlock.c
+-+-+- b/arch/powerpc/lib/qspinlock.c
Quoted 2 lines

@@-5,6 +-5,7 @@> #include <linux/percpu.h>> #include <linux/smp.h>> #include <linux/topology.h>>+-#include <linux/sched/clock.h>> #include <asm/qspinlock.h>> #include <asm/paravirt.h>>
@@-36,24 +-37,54 @@static int HEAD_SPINS __read_mostly =(1<<8);> static bool pv_yield_owner __read_mostly =true;> static bool pv_yield_allow_steal __read_mostly =false;> static bool pv_spin_on_preempted_owner __read_mostly =false;>+-static bool pv_sleepy_lock __read_mostly =true;>+-static bool pv_sleepy_lock_sticky __read_mostly =false;

The sticky part could potentially be its own patch.

Quoted 5 lines

+-static u64 pv_sleepy_lock_interval_ns __read_mostly =0;>+-static int pv_sleepy_lock_factor __read_mostly =256;> static bool pv_yield_prev __read_mostly =true;> static bool pv_yield_propagate_owner __read_mostly =true;> static bool pv_prod_head __read_mostly =false;> static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);>+-static DEFINE_PER_CPU_ALIGNED(u64, sleepy_lock_seen_clock);>
-static __always_inline int get_steal_spins(bool paravirt, bool remote)

+-static __always_inline bool recently_sleepy(void)
+-{

Other users of pv_sleepy_lock_interval_ns first check pv_sleepy_lock.

Quoted 5 lines
+-	if (pv_sleepy_lock_interval_ns) {>+-		u64 seen =this_cpu_read(sleepy_lock_seen_clock);>+-
+-		if (seen) {>+-			u64 delta =sched_clock() - seen;>+-			if (delta <pv_sleepy_lock_interval_ns)
+-				return true;>+-			this_cpu_write(sleepy_lock_seen_clock, 0);>+-		}>+-	}>+-
+-	return false;>+-}>+-
+-static __always_inline int get_steal_spins(bool paravirt, bool remote, bool sleepy)

It seems like paravirt is implied by sleepy.

Quoted 5 lines

{> if (remote) {>- return REMOTE_STEAL_SPINS;>+- if (paravirt &&sleepy)

+-			return REMOTE_STEAL_SPINS *pv_sleepy_lock_factor;>+-		else
+-			return REMOTE_STEAL_SPINS;> 	}else {>-		return STEAL_SPINS;>+-		if (paravirt &&sleepy)
+-			return STEAL_SPINS *pv_sleepy_lock_factor;>+-		else
+-			return STEAL_SPINS;> 	}> }

I think that separate functions would still be nicer but this could get rid of the nesting conditionals like

	int spins;	if (remote)
		spins =REMOTE_STEAL_SPINS;	else
		spins =STEAL_SPINS;
	if (sleepy)
		return spins *pv_sleepy_lock_factor;	return spins;
Quoted 19 lines

-static __always_inline int get_head_spins(bool paravirt) +-static __always_inline int get_head_spins(bool paravirt, bool sleepy)
{>- return HEAD_SPINS;>+- if (paravirt &&sleepy)

+-		return HEAD_SPINS *pv_sleepy_lock_factor;>+-	else
+-		return HEAD_SPINS;> }> 
static inline u32 encode_tail_cpu(void)
@@-206,6 +-237,60 @@static __always_inline u32 lock_clear_mustq(struct qspinlock *lock)
	return prev;> }> 
+-static __always_inline bool lock_try_set_sleepy(struct qspinlock *lock, u32 old)
+-{>+-	u32 prev;>+-	u32 new =old |_Q_SLEEPY_VAL;>+-
+-	BUG_ON(!old &_Q_LOCKED_VAL));>+-	BUG_ON(old &_Q_SLEEPY_VAL);>+-
+-	asm volatile(
+-"1:	lwarx	%0,0,%1		#lock_try_set_sleepy			\n">+-"cmpw	0,%0,%2							\n">+-"bne-	2f							\n">+-"stwcx.	%3,0,%1							\n">+-"bne-	1b							\n">+-"2:									\n">+-	: "=&r"(prev)
+-	: "r"(&lock->val), "r"old), "r"(new)
+-	: "cr0" "memory";>+-
+-	if (prev ==old)
+-		return true;>+-	return false;>+-}>+-
+-static __always_inline void seen_sleepy_owner(struct qspinlock *lock, u32 val)
Quoted 8 lines
+-{>+-	if (pv_sleepy_lock) {>+-		if (pv_sleepy_lock_interval_ns)
+-			this_cpu_write(sleepy_lock_seen_clock, sched_clock());>+-		if (!val &_Q_SLEEPY_VAL))
+-			lock_try_set_sleepy(lock, val);>+-	}>+-}>+-
+-static __always_inline void seen_sleepy_lock(void)
+-{>+-	if (pv_sleepy_lock &&pv_sleepy_lock_interval_ns)
+-		this_cpu_write(sleepy_lock_seen_clock, sched_clock());>+-}>+-
+-static __always_inline void seen_sleepy_node(struct qspinlock *lock)
+-{

If yield_to_prev() was made to take a raw val, that val could be passed to seen_sleepy_node() and it would not need to get it by itself.

Quoted 37 lines
+-	if (pv_sleepy_lock) {>+-		u32 val =READ_ONCE(lock->val);>+-
+-		if (pv_sleepy_lock_interval_ns)
+-			this_cpu_write(sleepy_lock_seen_clock, sched_clock());>+-		if (val &_Q_LOCKED_VAL) {>+-			if (!val &_Q_SLEEPY_VAL))
+-				lock_try_set_sleepy(lock, val);>+-		}>+-	}>+-}>+-

static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
{> int cpu =get_tail_cpu(val);>@@-244,6 +-329,7 @@static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32

	spin_end();> 
+-	seen_sleepy_owner(lock, val);> 	*preempted =true;> 
	/*>@@-307,11 +-393,13 @@static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int
	}> }> 

-static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt) +-static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt, bool *preempted)
{> u32 yield_count;> int yield_cpu;>
+- *preempted =false;>+-
if (!paravirt)
goto relax;>
@@-332,6 +-420,9 @@static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *>

	spin_end();> 
+-	*preempted =true;>+-	seen_sleepy_node(lock);>+-
	smp_rmb();> 
	if (yield_cpu ==node->yield_cpu) {>@@-353,6 +-444,9 @@static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *> 
	spin_end();> 
+-	*preempted =true;>+-	seen_sleepy_node(lock);>+-
	smp_rmb();/*See yield_to_locked_owner comment */

	if (!node->locked) {>@@-369,6 +-463,9 @@static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *> 
static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
{>+-	bool preempted;>+-	bool seen_preempted =false;>+-	bool sleepy =false;> 	int iters =0;> 
	if (!STEAL_SPINS) {>@@-376,7 +-473,6 @@static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
			spin_begin();> 			for (;; {> 				u32 val =READ_ONCE(lock->val);>-				bool preempted;> 
				if (val &_Q_MUST_Q_VAL)
					break;>@@-395,7 +-491,6 @@static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
	spin_begin();> 	for (;; {> 		u32 val =READ_ONCE(lock->val);>-		bool preempted;> 
		if (val &_Q_MUST_Q_VAL)
			break;>@@-408,9 +-503,29 @@static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
			continue;> 		}> 
Quoted 1 line

+- if (paravirt &&pv_sleepy_lock &&!sleepy) {>+- if (!sleepy) {

The enclosing conditional means this would always be true. I think the out conditional should be if (paravirt &&pv_sleepy_lock)
otherwise the pv_sleepy_lock_sticky part wouldn't work properly.

Quoted 6 lines
+-				if (val &_Q_SLEEPY_VAL) {>+-					seen_sleepy_lock();>+-					sleepy =true;>+-				}else if (recently_sleepy()) {>+-					sleepy =true;>+-				}>+-
+-			if (pv_sleepy_lock_sticky &&seen_preempted &&>+-					!val &_Q_SLEEPY_VAL)) {>+-				if (lock_try_set_sleepy(lock, val))
+-					val |=_Q_SLEEPY_VAL;>+-			}>+-
+-

yield_to_locked_owner(lock, val, paravirt, &preempted);>+- if (preempted)
+- seen_preempted =true;

This could belong to the next if statement, there can not be !paravirt &&preempted ?

Quoted 71 lines
		if (paravirt &&preempted) {>+-			sleepy =true;>+-
			if (!pv_spin_on_preempted_owner)
				iters+-+-;> 			/*>@@-425,14 +-540,15 @@static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
			iters+-+-;> 		}> 
arch/powerpc/lib/qspinlock.c +1 -1
-		if (iters >=get_steal_spins(paravirt, false))
+-		if (iters >=get_steal_spins(paravirt, false, sleepy))
			break;>-		if (iters >=get_steal_spins(paravirt, true)) {>+-		if (iters >=get_steal_spins(paravirt, true, sleepy)) {> 			int cpu =get_owner_cpu(val);> 			if (numa_node_id() !=cpu_to_node(cpu))
				break;> 		}> 	}>+-
	spin_end();> 
	return false;>@@-443,6 +-559,7 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
	struct qnodes *qnodesp;> 	struct qnode *next, *node;> 	u32 val, old, tail;>+-	bool seen_preempted =false;> 	int idx;> 
	BUILD_BUG_ON(CONFIG_NR_CPUS >=(1U <<_Q_TAIL_CPU_BITS));>@@-485,8 +-602,13 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b

		/*Wait for mcs node lock to be released */
		spin_begin();>-		while (!node->locked)
arch/powerpc/lib/qspinlock.c +2 -1
-			yield_to_prev(lock, node, prev_cpu, paravirt);>+-		while (!node->locked) {>+-			bool preempted;>+-
+-			yield_to_prev(lock, node, prev_cpu, paravirt, &preempted);>+-			if (preempted)
+-				seen_preempted =true;>+-		}> 		spin_end();> 
		/*Clear out stale propagated yield_cpu */

@@-506,6 +-628,8 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b

propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);> yield_head_to_locked_owner(lock, val, paravirt, false, &preempted);>+- if (preempted)
+- seen_preempted =true;> }> spin_end();>
@@-521,27 +-645,47 @@static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b

	}else {> 		int set_yield_cpu =-1;> 		int iters =0;>+-		bool sleepy =false;> 		bool set_mustq =false;>+-		bool preempted;> 
again:
		/*We're at the head of the waitqueue, wait for the lock. */
		spin_begin();> 		while ((val =READ_ONCE(lock->val)) &_Q_LOCKED_VAL) {>-			bool preempted;>+-			if (paravirt &&pv_sleepy_lock) {>+-				if (!sleepy) {>+-					if (val &_Q_SLEEPY_VAL) {>+-						seen_sleepy_lock();>+-						sleepy =true;>+-					}else if (recently_sleepy()) {>+-						sleepy =true;>+-					}>+-				}>+-				if (pv_sleepy_lock_sticky &&seen_preempted &&>+-						!val &_Q_SLEEPY_VAL)) {>+-					if (lock_try_set_sleepy(lock, val))
+-						val |=_Q_SLEEPY_VAL;>+-				}>+-			}> 
			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);> 			yield_head_to_locked_owner(lock, val, paravirt,
					pv_yield_allow_steal &&set_mustq,
					&preempted);>+-			if (preempted)
+-				seen_preempted =true;> 
			if (paravirt &&preempted) {>+-				sleepy =true;>+-
				if (!pv_spin_on_preempted_owner)
					iters+-+-;> 			}else {> 				iters+-+-;> 			}> 
arch/powerpc/lib/qspinlock.c +0 -1
-			if (!set_mustq &&iters >=get_head_spins(paravirt)) {>+-			if (!set_mustq &&iters >=get_head_spins(paravirt, sleepy)) {> 				set_mustq =true;> 				lock_set_mustq(lock);> 				val |=_Q_MUST_Q_VAL;>@@-729,6 +-873,70 @@static int pv_spin_on_preempted_owner_get(void *data, u64 *val)

DEFINE_SIMPLE_ATTRIBUTE(fops_pv_spin_on_preempted_owner, pv_spin_on_preempted_owner_get, pv_spin_on_preempted_owner_set, "%llu\n";>

+-static int pv_sleepy_lock_set(void *data, u64 val)
+-{>+-	pv_sleepy_lock =!!val;>+-
+-	return 0;>+-}>+-
+-static int pv_sleepy_lock_get(void *data, u64 *val)
+-{>+-	*val =pv_sleepy_lock;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock, pv_sleepy_lock_get, pv_sleepy_lock_set, "%llu\n";>+-
+-static int pv_sleepy_lock_sticky_set(void *data, u64 val)
+-{>+-	pv_sleepy_lock_sticky =!!val;>+-
+-	return 0;>+-}>+-
+-static int pv_sleepy_lock_sticky_get(void *data, u64 *val)
+-{>+-	*val =pv_sleepy_lock_sticky;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock_sticky, pv_sleepy_lock_sticky_get, pv_sleepy_lock_sticky_set, "%llu\n";>+-
+-static int pv_sleepy_lock_interval_ns_set(void *data, u64 val)
+-{>+-	pv_sleepy_lock_interval_ns =val;>+-
+-	return 0;>+-}>+-
+-static int pv_sleepy_lock_interval_ns_get(void *data, u64 *val)
+-{>+-	*val =pv_sleepy_lock_interval_ns;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock_interval_ns, pv_sleepy_lock_interval_ns_get, pv_sleepy_lock_interval_ns_set, "%llu\n";>+-
+-static int pv_sleepy_lock_factor_set(void *data, u64 val)
+-{>+-	pv_sleepy_lock_factor =val;>+-
+-	return 0;>+-}>+-
+-static int pv_sleepy_lock_factor_get(void *data, u64 *val)
+-{>+-	*val =pv_sleepy_lock_factor;>+-
+-	return 0;>+-}>+-
+-DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock_factor, pv_sleepy_lock_factor_get, pv_sleepy_lock_factor_set, "%llu\n";>+-
static int pv_yield_prev_set(void *data, u64 val)
{> 	pv_yield_prev =!!val;>@@-786,6 +-994,10 @@static __init int spinlock_debugfs_init(void)
		debugfs_create_file("qspl_pv_yield_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);> 		debugfs_create_file("qspl_pv_yield_allow_steal" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);> 		debugfs_create_file("qspl_pv_spin_on_preempted_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_spin_on_preempted_owner);>+-		debugfs_create_file("qspl_pv_sleepy_lock" 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock);>+-		debugfs_create_file("qspl_pv_sleepy_lock_sticky" 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock_sticky);>+-		debugfs_create_file("qspl_pv_sleepy_lock_interval_ns" 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock_interval_ns);>+-		debugfs_create_file("qspl_pv_sleepy_lock_factor" 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock_factor);> 		debugfs_create_file("qspl_pv_yield_prev" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);> 		debugfs_create_file("qspl_pv_yield_propagate_owner" 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);> 		debugfs_create_file("qspl_pv_prod_head" 0600, arch_debugfs_dir, NULL, &fops_pv_prod_head);
#70 lore raw

Re: [PATCH 16/17] powerpc/qspinlock: allow indefinite spinning on a preempted owner

In reply to Nicholas Piggin
Quoted 5 lines · Nicholas Piggin

Provide an option that holds off queueing indefinitely while the lock owner is preempted. This could reduce queueing latencies for very overcommitted vcpu situations.

This is disabled by default.

Hi Nick,

I should have missed something here.

If this option is turned on, CPU trying to lock when there is a preempted owner will spin checking the lock->val and yielding the lock owner CPU. Am I right?

If yes, why not being queued and spin checking its own value, yielding against the lock owner CPU? This will generate less cache bouncing, which is what the queued spinlock is trying to address, isn't it?

Thanks,
Laurent.

Quoted 188 lines
---
 arch/powerpc/lib/qspinlock.c | 91 +++++++++++++++++++++++++++++++-----
 1 file changed, 79 insertions(+), 12 deletions(-)
arch/powerpc/lib/qspinlock.c +79 -12
index 24f68bd71e2b..5cfd69931e31 100644
@@ -35,6 +35,7 @@ static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
+static bool pv_spin_on_preempted_owner __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
 static bool pv_yield_propagate_owner __read_mostly = true;
 static bool pv_prod_head __read_mostly = false;
@@ -220,13 +221,15 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
-static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq, bool *preempted)
 {
 	int owner;
 	u32 yield_count;
 
 	BUG_ON(!(val & _Q_LOCKED_VAL));
 
+	*preempted = false;
+
 	if (!paravirt)
 		goto relax;
 
@@ -241,6 +244,8 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 
 	spin_end();
 
+	*preempted = true;
+
 	/*
 	 * Read the lock word after sampling the yield count. On the other side
 	 * there may a wmb because the yield count update is done by the
@@ -265,14 +270,14 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 	spin_cpu_relax();
 }
 
-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool *preempted)
 {
-	__yield_to_locked_owner(lock, val, paravirt, false);
+	__yield_to_locked_owner(lock, val, paravirt, false, preempted);
 }
 
-static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq, bool *preempted)
 {
-	__yield_to_locked_owner(lock, val, paravirt, clear_mustq);
+	__yield_to_locked_owner(lock, val, paravirt, clear_mustq, preempted);
 }
 
 static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int *set_yield_cpu, bool paravirt)
@@ -364,12 +369,33 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 
 static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {
-	int iters;
+	int iters = 0;
+
+	if (!STEAL_SPINS) {
+		if (paravirt && pv_spin_on_preempted_owner) {
+			spin_begin();
+			for (;;) {
+				u32 val = READ_ONCE(lock->val);
+				bool preempted;
+
+				if (val & _Q_MUST_Q_VAL)
+					break;
+				if (!(val & _Q_LOCKED_VAL))
+					break;
+				if (!vcpu_is_preempted(get_owner_cpu(val)))
+					break;
+				yield_to_locked_owner(lock, val, paravirt, &preempted);
+			}
+			spin_end();
+		}
+		return false;
+	}
 
 	/* Attempt to steal the lock */
 	spin_begin();
 	for (;;) {
 		u32 val = READ_ONCE(lock->val);
+		bool preempted;
 
 		if (val & _Q_MUST_Q_VAL)
 			break;
@@ -382,9 +408,22 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 			continue;
 		}
 
-		yield_to_locked_owner(lock, val, paravirt);
-
-		iters++;
+		yield_to_locked_owner(lock, val, paravirt, &preempted);
+
+		if (paravirt && preempted) {
+			if (!pv_spin_on_preempted_owner)
+				iters++;
+			/*
+			 * pv_spin_on_preempted_owner don't increase iters
+			 * while the owner is preempted -- we won't interfere
+			 * with it by definition. This could introduce some
+			 * latency issue if we continually observe preempted
+			 * owners, but hopefully that's a rare corner case of
+			 * a badly oversubscribed system.
+			 */
+		} else {
+			iters++;
+		}
 
 		if (iters >= get_steal_spins(paravirt, false))
 			break;
@@ -463,8 +502,10 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		/* We're at the head of the waitqueue, wait for the lock. */
 		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			bool preempted;
+
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
-			yield_head_to_locked_owner(lock, val, paravirt, false);
+			yield_head_to_locked_owner(lock, val, paravirt, false, &preempted);
 		}
 		spin_end();
 
@@ -486,11 +527,20 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		/* We're at the head of the waitqueue, wait for the lock. */
 		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			bool preempted;
+
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt,
-					pv_yield_allow_steal && set_mustq);
+					pv_yield_allow_steal && set_mustq,
+					&preempted);
+
+			if (paravirt && preempted) {
+				if (!pv_spin_on_preempted_owner)
+					iters++;
+			} else {
+				iters++;
+			}
 
-			iters++;
 			if (!set_mustq && iters >= get_head_spins(paravirt)) {
 				set_mustq = true;
 				lock_set_mustq(lock);
@@ -663,6 +713,22 @@ static int pv_yield_allow_steal_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_allow_steal, pv_yield_allow_steal_get, pv_yield_allow_steal_set, "%llu\n");
 
+static int pv_spin_on_preempted_owner_set(void *data, u64 val)
+{
+	pv_spin_on_preempted_owner = !!val;
+
+	return 0;
+}
+
+static int pv_spin_on_preempted_owner_get(void *data, u64 *val)
+{
+	*val = pv_spin_on_preempted_owner;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_spin_on_preempted_owner, pv_spin_on_preempted_owner_get, pv_spin_on_preempted_owner_set, "%llu\n");
+
 static int pv_yield_prev_set(void *data, u64 val)
 {
 	pv_yield_prev = !!val;
@@ -719,6 +785,7 @@ static __init int spinlock_debugfs_init(void)
 	if (is_shared_processor()) {
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
 		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
+		debugfs_create_file("qspl_pv_spin_on_preempted_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_spin_on_preempted_owner);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
 		debugfs_create_file("qspl_pv_yield_propagate_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);
 		debugfs_create_file("qspl_pv_prod_head", 0600, arch_debugfs_dir, NULL, &fops_pv_prod_head);
author reviewer
#71 lore raw

Re: [PATCH 16/17] powerpc/qspinlock: allow indefinite spinning on a preempted owner

Quoted 7 lines · Laurent Dufour
Quoted 5 lines · Nicholas Piggin

Provide an option that holds off queueing indefinitely while the lock owner is preempted. This could reduce queueing latencies for very overcommitted vcpu situations.

This is disabled by default.

Quoted 7 lines

Hi Nick,

I should have missed something here.

If this option is turned on, CPU trying to lock when there is a preempted owner will spin checking the lock->val and yielding the lock owner CPU. Am I right?

Yes.

Quoted 2 lines

If yes, why not being queued and spin checking its own value, yielding against the lock owner CPU?

I guess the idea is that when we start getting vCPU preemption, queueing behaviour causes this "train wreck" behaviour where lock waiters being preempted can halt lock transfers to other waiters (whereas with simple spinlocks only owner vCPU preemption matters). So the heuristics for paravirt qspinlock basically come down to avoiding queueing and making waiters behave more like a simple spinlock when it matters. That's the case for upstream and this rewrite.

Quoted 2 lines

This will generate less cache bouncing, which
is what the queued spinlock is trying to address, isn't it?

It could. When the owner is preempted it's not going to be modifying the lock word and probably not surrounding data in the same cache line, and there won't be a lot of other try-lock operations come in (because they'll mostly queue up here as well). So cacheline bouncing shouldn't be the worst problem we face here. But it possibly is a concern.

I didn't yet meausre any real improvement from this option, and it possibly has some starvation potential, so it's disabled by default for now.

Thanks,
Nick

#49 lore raw

Re: [PATCH 11/17] powerpc/qspinlock: allow propagation of yield CPU down the queue

In reply to Nicholas Piggin
Quoted 56 lines · Nicholas Piggin

Having all CPUs poll the lock word for the owner CPU that should be yielded to defeats most of the purpose of using MCS queueing for scalability. Yet it may be desirable for queued waiters to to yield to a preempted owner.

s390 addreses this problem by having queued waiters sample the lock word to find the owner much less frequently. In this approach, the waiters never sample it directly, but the queue head propagates the owner CPU back to the next waiter if it ever finds the owner has been preempted. Queued waiters then subsequently propagate the owner CPU back to the next waiter, and so on.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 85 +++++++++++++++++++++++++++++++++++-
 1 file changed, 84 insertions(+), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +17 -0
index 94f007f66942..28c85a2d5635 100644
@@ -12,6 +12,7 @@
 struct qnode {
 	struct qnode	*next;
 	struct qspinlock *lock;
+	int		yield_cpu;
 	u8		locked; /* 1 if lock acquired */
 };
 
@@ -28,6 +29,7 @@ static int HEAD_SPINS __read_mostly = (1<<8);
 static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
+static bool pv_yield_propagate_owner __read_mostly = true;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
@@ -257,13 +259,66 @@ static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u
 	__yield_to_locked_owner(lock, val, paravirt, clear_mustq);
 }
 
+static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int *set_yield_cpu, bool paravirt)
+{
+	struct qnode *next;
+	int owner;
+
+	if (!paravirt)
+		return;
+	if (!pv_yield_propagate_owner)
+		return;
+
+	owner = get_owner_cpu(val);
+	if (*set_yield_cpu == owner)
+		return;
+
+	next = READ_ONCE(node->next);

arch/powerpc/lib/qspinlock.c static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u · line 286

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+	if (!next)
+		return;
+
+	if (vcpu_is_preempted(owner)) {
+		next->yield_cpu = owner;
+		*set_yield_cpu = owner;
+	} else if (*set_yield_cpu != -1) {
+		next->yield_cpu = owner;
+		*set_yield_cpu = owner;
+	}

This is bit confusing, the else branch is the same as the true one. This might be written like this:

	if (vcpu_is_preempted(owner) || *set_yield_cpu != -1) {
		next->yield_cpu = owner;
		*set_yield_cpu = owner;
	}
Quoted 24 lines
arch/powerpc/lib/qspinlock.c +17 -0
+}
+
 static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
 {
 	u32 yield_count;
+	int yield_cpu;
 
 	if (!paravirt)
 		goto relax;
 
+	if (!pv_yield_propagate_owner)
+		goto yield_prev;
+
+	yield_cpu = READ_ONCE(node->yield_cpu);
+	if (yield_cpu == -1) {
+		/* Propagate back the -1 CPU */
+		if (node->next && node->next->yield_cpu != -1)
+			node->next->yield_cpu = yield_cpu;
+		goto yield_prev;
+	}
+
+	yield_count = yield_count_of(yield_cpu);
+	if ((yield_count & 1) == 0)
+		goto yield_prev; /* owner vcpu is running */

arch/powerpc/lib/qspinlock.c static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u · line 320

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+
+	smp_rmb();
+
+	if (yield_cpu == node->yield_cpu) {
+		if (node->next && node->next->yield_cpu != yield_cpu)
+			node->next->yield_cpu = yield_cpu;
+		yield_to_preempted(yield_cpu, yield_count);
+		return;
+	}
+

In the case that test is false, this means that the lock owner has probably changed, why are we yeilding to the previous node instead of reading again the node->yield_cpu, checking against -1 value etc..?
Yielding to the previous node is valid, but it might be better to yield to the owner, isn't it?

Quoted 13 lines
arch/powerpc/lib/qspinlock.c +2 -0
+yield_prev:
 	if (!pv_yield_prev)
 		goto relax;
 
@@ -337,6 +392,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	node = &qnodesp->nodes[idx];
 	node->next = NULL;
 	node->lock = lock;
+	node->yield_cpu = -1;
 	node->locked = 0;
 
 	tail = encode_tail_cpu();

arch/powerpc/lib/qspinlock.c static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b · line 419

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +3 -0
@@ -358,13 +414,21 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		while (!node->locked)
 			yield_to_prev(lock, node, prev_cpu, paravirt);
 
+		/* Clear out stale propagated yield_cpu */
+		if (paravirt && pv_yield_propagate_owner && node->yield_cpu != -1)
+			node->yield_cpu = -1;

Why doing tests and not directly setting node->yield_cpu to -1? Is the write operation more costly than the 3 tests?

Quoted 62 lines
arch/powerpc/lib/qspinlock.c +25 -1
+
 		smp_rmb(); /* acquire barrier for the mcs lock */
 	}
 
 	if (!MAYBE_STEALERS) {
+		int set_yield_cpu = -1;
+
 		/* We're at the head of the waitqueue, wait for the lock. */
-		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
+		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt, false);
+		}
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -376,12 +440,14 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		/* We must be the owner, just set the lock bit and acquire */
 		lock_set_locked(lock);
 	} else {
+		int set_yield_cpu = -1;
 		int iters = 0;
 		bool set_mustq = false;
 
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt,
 					pv_yield_allow_steal && set_mustq);
 
@@ -540,6 +606,22 @@ static int pv_yield_prev_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_prev, pv_yield_prev_get, pv_yield_prev_set, "%llu\n");
 
+static int pv_yield_propagate_owner_set(void *data, u64 val)
+{
+	pv_yield_propagate_owner = !!val;
+
+	return 0;
+}
+
+static int pv_yield_propagate_owner_get(void *data, u64 *val)
+{
+	*val = pv_yield_propagate_owner;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_propagate_owner, pv_yield_propagate_owner_get, pv_yield_propagate_owner_set, "%llu\n");
+
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
@@ -548,6 +630,7 @@ static __init int spinlock_debugfs_init(void)
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
 		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
+		debugfs_create_file("qspl_pv_yield_propagate_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);
 	}
 
 	return 0;
#4 lore raw

Re: [PATCH 01/17] powerpc/qspinlock: powerpc qspinlock implementation

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
<snip>

arch/powerpc/include/asm/qspinlock.h line 28

Quoted 12 lines
arch/powerpc/include/asm/qspinlock.h +4 -6
-#define queued_spin_lock queued_spin_lock
 
-static inline void queued_spin_unlock(struct qspinlock *lock)
+static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 {
-	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) || !is_shared_processor())
-		smp_store_release(&lock->locked, 0);
-	else
-		__pv_queued_spin_unlock(lock);
+	if (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0)
+		return 1;
+	return 0;

optional style nit: return (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0);

[resend as utf-8, not utf-7]

#11 lore raw

Re: [PATCH 02/17] powerpc/qspinlock: add mcs queueing for contended waiters

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
<snip>
[resend as utf-8, not utf-7]

Quoted 8 lines
arch/powerpc/include/asm/qspinlock_types.h +7 -0
+/*
+ * Bitfields in the atomic value:
+ *
+ *     0: locked bit
+ * 16-31: tail cpu (+1)
+ */
+#define	_Q_SET_MASK(type)	(((1U << _Q_ ## type ## _BITS) - 1)\

arch/powerpc/include/asm/qspinlock_types.h typedef struct qspinlock { · line 29

Quoted 10 lines
arch/powerpc/include/asm/qspinlock_types.h +10 -0
+				      << _Q_ ## type ## _OFFSET)
+#define _Q_LOCKED_OFFSET	0
+#define _Q_LOCKED_BITS		1
+#define _Q_LOCKED_MASK		_Q_SET_MASK(LOCKED)
+#define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
+
+#define _Q_TAIL_CPU_OFFSET	16
+#define _Q_TAIL_CPU_BITS	(32 - _Q_TAIL_CPU_OFFSET)
+#define _Q_TAIL_CPU_MASK	_Q_SET_MASK(TAIL_CPU)
+

Just to state the obvious this is:

#define _Q_LOCKED_OFFSET 0
#define _Q_LOCKED_BITS 1
#define _Q_LOCKED_MASK 0x00000001
#define _Q_LOCKED_VAL 1

#define _Q_TAIL_CPU_OFFSET 16
#define _Q_TAIL_CPU_BITS 16
#define _Q_TAIL_CPU_MASK 0xffff0000

Quoted 23 lines
arch/powerpc/include/asm/qspinlock_types.h +4 -0
+#if CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS)
+#error "qspinlock does not support such large CONFIG_NR_CPUS"
+#endif
+
 #endif /* _ASM_POWERPC_QSPINLOCK_TYPES_H */
arch/powerpc/lib/qspinlock.c +7 -2
index 8dbce99a373c..5ebb88d95636 100644
@@ -1,12 +1,172 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
+#include <linux/atomic.h>
+#include <linux/bug.h>
+#include <linux/compiler.h>
 #include <linux/export.h>
-#include <linux/processor.h>
+#include <linux/percpu.h>
+#include <linux/smp.h>
 #include <asm/qspinlock.h>
 
-void queued_spin_lock_slowpath(struct qspinlock *lock)
+#define MAX_NODES	4
+

arch/powerpc/lib/qspinlock.c line 21

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+struct qnode {
+	struct qnode	*next;
+	struct qspinlock *lock;
+	u8		locked; /* 1 if lock acquired */
+};
+
+struct qnodes {
+	int		count;
+	struct qnode nodes[MAX_NODES];
+};

I think it could be worth commenting why qnodes::count instead _Q_TAIL_IDX_OFFSET.

arch/powerpc/lib/qspinlock.c line 25

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +4 -0
+
+static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
+
+static inline int encode_tail_cpu(void)

I think the generic version that takes smp_processor_id() as a parameter is clearer - at least with this function name.

arch/powerpc/lib/qspinlock.c line 30

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+{
+	return (smp_processor_id() + 1) << _Q_TAIL_CPU_OFFSET;
+}
+
+static inline int get_tail_cpu(int val)

It seems like there should be a "decode" function to pair up with the "encode" function.

arch/powerpc/lib/qspinlock.c line 35

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+{
+	return (val >> _Q_TAIL_CPU_OFFSET) - 1;
+}
+
+/* Take the lock by setting the bit, no other CPUs may concurrently lock it. */

Does that comment mean it is not necessary to use an atomic_or here?

arch/powerpc/lib/qspinlock.c line 36

Quoted 1 line
arch/powerpc/lib/qspinlock.c +1 -0
+static __always_inline void lock_set_locked(struct qspinlock *lock)

nit: could just be called set_locked()

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +4 -0
+{
+	atomic_or(_Q_LOCKED_VAL, &lock->val);
+	__atomic_acquire_fence();
+}

arch/powerpc/lib/qspinlock.c line 50

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+
+/* Take lock, clearing tail, cmpxchg with val (which must not be locked) */
+static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int val)
+{
+	int newval = _Q_LOCKED_VAL;
+
+	if (atomic_cmpxchg_acquire(&lock->val, val, newval) == val)
+		return 1;
+	else
+		return 0;

same optional style nit: return (atomic_cmpxchg_acquire(&lock->val, val, newval) == val);

arch/powerpc/lib/qspinlock.c line 59

Quoted 9 lines
arch/powerpc/lib/qspinlock.c +9 -0
+}
+
+/*
+ * Publish our tail, replacing previous tail. Return previous value.
+ *
+ * This provides a release barrier for publishing node, and an acquire barrier
+ * for getting the old node.
+ */
+static __always_inline int publish_tail_cpu(struct qspinlock *lock, int tail)

Did you change from the xchg_tail() name in the generic version because of the release and acquire barriers this provides? Does "publish" generally imply the old value will be returned?

Quoted 14 lines
arch/powerpc/lib/qspinlock.c +12 -1
 {
-	while (!queued_spin_trylock(lock))
+	for (;;) {
+		int val = atomic_read(&lock->val);
+		int newval = (val & ~_Q_TAIL_CPU_MASK) | tail;
+		int old;
+
+		old = atomic_cmpxchg(&lock->val, val, newval);
+		if (old == val)
+			return old;
+	}
+}
+
+static struct qnode *get_tail_qnode(struct qspinlock *lock, int val)

arch/powerpc/lib/qspinlock.c line 82

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+{
+	int cpu = get_tail_cpu(val);
+	struct qnodes *qnodesp = per_cpu_ptr(&qnodes, cpu);
+	int idx;
+
+	for (idx = 0; idx < MAX_NODES; idx++) {
+		struct qnode *qnode = &qnodesp->nodes[idx];
+		if (qnode->lock == lock)
+			return qnode;
+	}

In case anyone else is confused by this, Nick explained each cpu can only queue on a unique spinlock once regardless of "idx" level.

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+
+	BUG();
+}
+
+static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)

arch/powerpc/lib/qspinlock.c line 97

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+{
+	struct qnodes *qnodesp;
+	struct qnode *next, *node;
+	int val, old, tail;
+	int idx;
+
+	BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
+
+	qnodesp = this_cpu_ptr(&qnodes);
+	if (unlikely(qnodesp->count == MAX_NODES)) {

The comparison is >= in the generic, I guess we've no nested NMI so this is safe?

Quoted 75 lines
arch/powerpc/lib/qspinlock.c +71 -0
+		while (!queued_spin_trylock(lock))
+			cpu_relax();
+		return;
+	}
+
+	idx = qnodesp->count++;
+	/*
+	 * Ensure that we increment the head node->count before initialising
+	 * the actual node. If the compiler is kind enough to reorder these
+	 * stores, then an IRQ could overwrite our assignments.
+	 */
+	barrier();
+	node = &qnodesp->nodes[idx];
+	node->next = NULL;
+	node->lock = lock;
+	node->locked = 0;
+
+	tail = encode_tail_cpu();
+
+	old = publish_tail_cpu(lock, tail);
+
+	/*
+	 * If there was a previous node; link it and wait until reaching the
+	 * head of the waitqueue.
+	 */
+	if (old & _Q_TAIL_CPU_MASK) {
+		struct qnode *prev = get_tail_qnode(lock, old);
+
+		/* Link @node into the waitqueue. */
+		WRITE_ONCE(prev->next, node);
+
+		/* Wait for mcs node lock to be released */
+		while (!node->locked)
+			cpu_relax();
+
+		smp_rmb(); /* acquire barrier for the mcs lock */
+	}
+
+	/* We're at the head of the waitqueue, wait for the lock. */
+	while ((val = atomic_read(&lock->val)) & _Q_LOCKED_VAL)
+		cpu_relax();
+
+	/* If we're the last queued, must clean up the tail. */
+	if ((val & _Q_TAIL_CPU_MASK) == tail) {
+		if (trylock_clear_tail_cpu(lock, val))
+			goto release;
+		/* Another waiter must have enqueued */
+	}
+
+	/* We must be the owner, just set the lock bit and acquire */
+	lock_set_locked(lock);
+
+	/* contended path; must wait for next != NULL (MCS protocol) */
+	while (!(next = READ_ONCE(node->next)))
 		cpu_relax();
+
+	/*
+	 * Unlock the next mcs waiter node. Release barrier is not required
+	 * here because the acquirer is only accessing the lock word, and
+	 * the acquire barrier we took the lock with orders that update vs
+	 * this store to locked. The corresponding barrier is the smp_rmb()
+	 * acquire barrier for mcs lock, above.
+	 */
+	WRITE_ONCE(next->locked, 1);
+
+release:
+	qnodesp->count--; /* release the node */
+}
+
+void queued_spin_lock_slowpath(struct qspinlock *lock)
+{
+	queued_spin_lock_mcs_queue(lock);
 }
 EXPORT_SYMBOL(queued_spin_lock_slowpath);
 
#15 lore raw

Re: [PATCH 03/17] powerpc/qspinlock: use a half-word store to unlock to avoid larx/stcx.

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 14 lines

The first 16 bits of the lock are only modified by the owner, and other modifications always use atomic operations on the entire 32 bits, so unlocks can use plain stores on the 16 bits. This is the same kind of optimisation done by core qspinlock code.

---
 arch/powerpc/include/asm/qspinlock.h       |  6 +-----
 arch/powerpc/include/asm/qspinlock_types.h | 19 +++++++++++++++++--
 2 files changed, 18 insertions(+), 7 deletions(-)
arch/powerpc/include/asm/qspinlock.h +0 -0
index f06117aa60e1..79a1936fb68d 100644

arch/powerpc/include/asm/qspinlock.h static __always_inline void queued_spin_lock(struct qspinlock *lock) · line 41

Quoted 9 lines
arch/powerpc/include/asm/qspinlock.h +1 -5
@@ -38,11 +38,7 @@ static __always_inline void queued_spin_lock(struct qspinlock *lock)
 
 static inline void queued_spin_unlock(struct qspinlock *lock)
 {
-	for (;;) {
-		int val = atomic_read(&lock->val);
-		if (atomic_cmpxchg_release(&lock->val, val, val & ~_Q_LOCKED_VAL) == val)
-			return;
-	}
+	smp_store_release(&lock->locked, 0);

Is it also possible for lock_set_locked() to use a non-atomic acquire operation?

Quoted 21 lines
arch/powerpc/include/asm/qspinlock.h +0 -0
 }
 
 #define arch_spin_is_locked(l)		queued_spin_is_locked(l)
arch/powerpc/include/asm/qspinlock_types.h +7 -1
index 9630e714c70d..3425dab42576 100644
@@ -3,12 +3,27 @@
 #define _ASM_POWERPC_QSPINLOCK_TYPES_H
 
 #include <linux/types.h>
+#include <asm/byteorder.h>
 
 typedef struct qspinlock {
-	atomic_t val;
+	union {
+		atomic_t val;
+
+#ifdef __LITTLE_ENDIAN
+		struct {
+			u16	locked;

arch/powerpc/include/asm/qspinlock_types.h line 24

Quoted 10 lines
arch/powerpc/include/asm/qspinlock_types.h +9 -0
+			u8	reserved[2];
+		};
+#else
+		struct {
+			u8	reserved[2];
+			u16	locked;
+		};
+#endif
+	};
 } arch_spinlock_t;

Just to double check we have:

#define _Q_LOCKED_OFFSET 0
#define _Q_LOCKED_BITS 1
#define _Q_LOCKED_MASK 0x00000001
#define _Q_LOCKED_VAL 1

#define _Q_TAIL_CPU_OFFSET 16
#define _Q_TAIL_CPU_BITS 16
#define _Q_TAIL_CPU_MASK 0xffff0000

so the ordering here looks correct.

Quoted 6 lines
arch/powerpc/include/asm/qspinlock_types.h +1 -1
-#define	__ARCH_SPIN_LOCK_UNLOCKED	{ .val = ATOMIC_INIT(0) }
+#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { .val = ATOMIC_INIT(0) } }
 
 /*
  * Bitfields in the atomic value:
#19 lore raw

Re: [PATCH 04/17] powerpc/qspinlock: convert atomic operations to assembly

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 46 lines

This uses more optimal ll/sc style access patterns (rather than cmpxchg), and also sets the EH=1 lock hint on those operations which acquire ownership of the lock.

---
 arch/powerpc/include/asm/qspinlock.h       | 25 +++++--
 arch/powerpc/include/asm/qspinlock_types.h |  6 +-
 arch/powerpc/lib/qspinlock.c               | 81 +++++++++++++++-------
 3 files changed, 79 insertions(+), 33 deletions(-)
arch/powerpc/include/asm/qspinlock.h +7 -5
index 79a1936fb68d..3ab354159e5e 100644
@@ -2,28 +2,43 @@
 #ifndef _ASM_POWERPC_QSPINLOCK_H
 #define _ASM_POWERPC_QSPINLOCK_H
 
-#include <linux/atomic.h>
 #include <linux/compiler.h>
 #include <asm/qspinlock_types.h>
 
 static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
 {
-	return atomic_read(&lock->val);
+	return READ_ONCE(lock->val);
 }
 
 static __always_inline int queued_spin_value_unlocked(struct qspinlock lock)
 {
-	return !atomic_read(&lock.val);
+	return !lock.val;
 }
 
 static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
 {
-	return !!(atomic_read(&lock->val) & _Q_TAIL_CPU_MASK);
+	return !!(READ_ONCE(lock->val) & _Q_TAIL_CPU_MASK);
 }
 
 static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 {
-	if (atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL) == 0)
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	asm volatile(

arch/powerpc/include/asm/qspinlock.h line 38

Quoted 10 lines
arch/powerpc/include/asm/qspinlock.h +10 -0
+"1:	lwarx	%0,0,%1,%3	# queued_spin_trylock			\n"
+"	cmpwi	0,%0,0							\n"
+"	bne-	2f							\n"
+"	stwcx.	%2,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)

btw IS_ENABLED() already returns 1 or 0

arch/powerpc/include/asm/qspinlock.h line 39

Quoted 1 line
arch/powerpc/include/asm/qspinlock.h +1 -0
+	: "cr0", "memory");

This is the ISA's "test and set" atomic primitive. Do you think it would be worth seperating it as a helper?

arch/powerpc/include/asm/qspinlock.h line 43

Quoted 4 lines
arch/powerpc/include/asm/qspinlock.h +2 -0
+
+	if (likely(prev == 0))
 		return 1;
 	return 0;

same optional style nit: return likely(prev == 0);

Quoted 44 lines
arch/powerpc/include/asm/qspinlock.h +0 -0
 }
arch/powerpc/include/asm/qspinlock_types.h +3 -3
index 3425dab42576..210adf05b235 100644
@@ -7,7 +7,7 @@
 
 typedef struct qspinlock {
 	union {
-		atomic_t val;
+		u32 val;
 
 #ifdef __LITTLE_ENDIAN
 		struct {
@@ -23,10 +23,10 @@ typedef struct qspinlock {
 	};
 } arch_spinlock_t;
 
-#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { .val = ATOMIC_INIT(0) } }
+#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { .val = 0 } }
 
 /*
- * Bitfields in the atomic value:
+ * Bitfields in the lock word:
  *
  *     0: locked bit
  * 16-31: tail cpu (+1)
arch/powerpc/lib/qspinlock.c +1 -2
index 5ebb88d95636..7c71e5e287df 100644
@@ -1,5 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
-#include <linux/atomic.h>
 #include <linux/bug.h>
 #include <linux/compiler.h>
 #include <linux/export.h>
@@ -22,32 +21,59 @@ struct qnodes {
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
-static inline int encode_tail_cpu(void)
+static inline u32 encode_tail_cpu(void)
 {

arch/powerpc/lib/qspinlock.c struct qnodes { · line 34

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +1 -1
 	return (smp_processor_id() + 1) << _Q_TAIL_CPU_OFFSET;
 }
 
-static inline int get_tail_cpu(int val)
+static inline int get_tail_cpu(u32 val)
 {
 	return (val >> _Q_TAIL_CPU_OFFSET) - 1;
 }
 
 /* Take the lock by setting the bit, no other CPUs may concurrently lock it. */

I think you missed deleting the above line.

Quoted 9 lines
arch/powerpc/lib/qspinlock.c +5 -2
+/* Take the lock by setting the lock bit, no other CPUs will touch it. */
 static __always_inline void lock_set_locked(struct qspinlock *lock)
 {
-	atomic_or(_Q_LOCKED_VAL, &lock->val);
-	__atomic_acquire_fence();
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	asm volatile(

arch/powerpc/lib/qspinlock.c struct qnodes { · line 51

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +9 -0
+"1:	lwarx	%0,0,%1,%3	# lock_set_locked			\n"
+"	or	%0,%0,%2						\n"
+"	stwcx.	%0,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");
 }

This is pretty similar with the DEFINE_TESTOP() pattern from arch/powerpc/include/asm/bitops.h (such as test_and_set_bits_lock()) except for word instead of double word. Do you think it's possible / beneficial to make use of those macros?

arch/powerpc/lib/qspinlock.c struct qnodes { · line 59

Quoted 13 lines
arch/powerpc/lib/qspinlock.c +6 -5
-/* Take lock, clearing tail, cmpxchg with val (which must not be locked) */
-static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int val)
+/* Take lock, clearing tail, cmpxchg with old (which must not be locked) */
+static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 old)
 {
-	int newval = _Q_LOCKED_VAL;
-
-	if (atomic_cmpxchg_acquire(&lock->val, val, newval) == val)
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	BUG_ON(old & _Q_LOCKED_VAL);

The BUG_ON() could have been introduced in an earlier patch I think.

arch/powerpc/lib/qspinlock.c struct qnodes { · line 70

Quoted 11 lines
arch/powerpc/lib/qspinlock.c +11 -0
+
+	asm volatile(
+"1:	lwarx	%0,0,%1,%4	# trylock_clear_tail_cpu		\n"
+"	cmpw	0,%0,%2							\n"
+"	bne-	2f							\n"
+"	stwcx.	%3,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r"(old), "r" (new),

Could this be like "r"(_Q_TAIL_CPU_MASK) below? i.e. "r" (_Q_LOCKED_VAL)? Makes it clear new doesn't change.

arch/powerpc/lib/qspinlock.c static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int va · line 82

Quoted 13 lines
arch/powerpc/lib/qspinlock.c +5 -2
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");
+
+	if (likely(prev == old))
 		return 1;
-	else
-		return 0;
+	return 0;
 }
 
 /*
@@ -56,20 +82,25 @@ static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int va
  * This provides a release barrier for publishing node, and an acquire barrier

Does the comment mean there needs to be an acquire barrier in this assembly?

Quoted 53 lines
arch/powerpc/lib/qspinlock.c +18 -13
  * for getting the old node.
  */
-static __always_inline int publish_tail_cpu(struct qspinlock *lock, int tail)
+static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail)
 {
-	for (;;) {
-		int val = atomic_read(&lock->val);
-		int newval = (val & ~_Q_TAIL_CPU_MASK) | tail;
-		int old;
-
-		old = atomic_cmpxchg(&lock->val, val, newval);
-		if (old == val)
-			return old;
-	}
+	u32 prev, tmp;
+
+	asm volatile(
+"\t"	PPC_RELEASE_BARRIER "						\n"
+"1:	lwarx	%0,0,%2		# publish_tail_cpu			\n"
+"	andc	%1,%0,%4						\n"
+"	or	%1,%1,%3						\n"
+"	stwcx.	%1,0,%2							\n"
+"	bne-	1b							\n"
+	: "=&r" (prev), "=&r"(tmp)
+	: "r" (&lock->val), "r" (tail), "r"(_Q_TAIL_CPU_MASK)
+	: "cr0", "memory");
+
+	return prev;
 }
 
-static struct qnode *get_tail_qnode(struct qspinlock *lock, int val)
+static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 {
 	int cpu = get_tail_cpu(val);
 	struct qnodes *qnodesp = per_cpu_ptr(&qnodes, cpu);
@@ -88,7 +119,7 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 {
 	struct qnodes *qnodesp;
 	struct qnode *next, *node;
-	int val, old, tail;
+	u32 val, old, tail;
 	int idx;
 
 	BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
@@ -134,7 +165,7 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 	}
 
 	/* We're at the head of the waitqueue, wait for the lock. */
-	while ((val = atomic_read(&lock->val)) & _Q_LOCKED_VAL)
+	while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
 		cpu_relax();
 
 	/* If we're the last queued, must clean up the tail. */
#25 lore raw

Re: [PATCH 05/17] powerpc/qspinlock: allow new waiters to steal the lock before queueing

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 12 lines

Allow new waiters a number of spins on the lock word before queueing, which particularly helps paravirt performance when physical CPUs are oversubscribed.

---
 arch/powerpc/lib/qspinlock.c | 152 ++++++++++++++++++++++++++++++++---
 1 file changed, 141 insertions(+), 11 deletions(-)
arch/powerpc/lib/qspinlock.c +0 -0
index 7c71e5e287df..1625cce714b2 100644

arch/powerpc/lib/qspinlock.c struct qnodes { · line 24

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +3 -0
@@ -19,8 +19,17 @@ struct qnodes {
 	struct qnode nodes[MAX_NODES];
 };
 
+/* Tuning parameters */
+static int STEAL_SPINS __read_mostly = (1<<5);
+static bool MAYBE_STEALERS __read_mostly = true;

I can understand why, but macro case variables can be a bit confusing.

Quoted 13 lines
arch/powerpc/lib/qspinlock.c +6 -0
+
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
+static __always_inline int get_steal_spins(void)
+{
+	return STEAL_SPINS;
+}
+
 static inline u32 encode_tail_cpu(void)
 {
 	return (smp_processor_id() + 1) << _Q_TAIL_CPU_OFFSET;
@@ -76,6 +85,39 @@ static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 ol
 	return 0;

arch/powerpc/lib/qspinlock.c static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 ol · line 95

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +8 -0
 }
 
+static __always_inline u32 __trylock_cmpxchg(struct qspinlock *lock, u32 old, u32 new)
+{
+	u32 prev;
+
+	BUG_ON(old & _Q_LOCKED_VAL);
+
+	asm volatile(
+"1:	lwarx	%0,0,%1,%4	# queued_spin_trylock_cmpxchg		\n"

s/queued_spin_trylock_cmpxchg/__trylock_cmpxchg/

btw what is the format you using for the '\n's in the inline asm?

arch/powerpc/lib/qspinlock.c static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 ol · line 105

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+"	cmpw	0,%0,%2							\n"
+"	bne-	2f							\n"
+"	stwcx.	%3,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r"(old), "r" (new),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");

This is very similar to trylock_clear_tail_cpu(). So maybe it is worth having some form of "test and set" primitive helper.

arch/powerpc/lib/qspinlock.c static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 ol · line 118

Quoted 13 lines
arch/powerpc/lib/qspinlock.c +13 -0
+
+	return prev;
+}
+
+/* Take lock, preserving tail, cmpxchg with val (which must not be locked) */
+static __always_inline int trylock_with_tail_cpu(struct qspinlock *lock, u32 val)
+{
+	u32 newval = _Q_LOCKED_VAL | (val & _Q_TAIL_CPU_MASK);
+
+	if (__trylock_cmpxchg(lock, val, newval) == val)
+		return 1;
+	else
+		return 0;

same optional style nit: return __trylock_cmpxchg(lock, val, newval) == val

Quoted 12 lines
arch/powerpc/lib/qspinlock.c +5 -0
+}
+
 /*
  * Publish our tail, replacing previous tail. Return previous value.
  *
@@ -115,6 +157,31 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
+static inline bool try_to_steal_lock(struct qspinlock *lock)
+{
+	int iters;

arch/powerpc/lib/qspinlock.c static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val) · line 172

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+
+	/* Attempt to steal the lock */
+	for (;;) {
+		u32 val = READ_ONCE(lock->val);
+
+		if (unlikely(!(val & _Q_LOCKED_VAL))) {
+			if (trylock_with_tail_cpu(lock, val))
+				return true;
+			continue;
+		}

The continue would bypass iters++/cpu_relax but the next time around
if (unlikely(!(val & _Q_LOCKED_VAL))) {
should fail so everything should be fine?

Quoted 77 lines
arch/powerpc/lib/qspinlock.c +45 -11
+
+		cpu_relax();
+
+		iters++;
+
+		if (iters >= get_steal_spins())
+			break;
+	}
+
+	return false;
+}
+
 static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 {
 	struct qnodes *qnodesp;
@@ -164,20 +231,39 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 		smp_rmb(); /* acquire barrier for the mcs lock */
 	}
 
-	/* We're at the head of the waitqueue, wait for the lock. */
-	while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
-		cpu_relax();
+	if (!MAYBE_STEALERS) {
+		/* We're at the head of the waitqueue, wait for the lock. */
+		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
+			cpu_relax();
 
-	/* If we're the last queued, must clean up the tail. */
-	if ((val & _Q_TAIL_CPU_MASK) == tail) {
-		if (trylock_clear_tail_cpu(lock, val))
-			goto release;
-		/* Another waiter must have enqueued */
-	}
+		/* If we're the last queued, must clean up the tail. */
+		if ((val & _Q_TAIL_CPU_MASK) == tail) {
+			if (trylock_clear_tail_cpu(lock, val))
+				goto release;
+			/* Another waiter must have enqueued. */
+		}
+
+		/* We must be the owner, just set the lock bit and acquire */
+		lock_set_locked(lock);
+	} else {
+again:
+		/* We're at the head of the waitqueue, wait for the lock. */
+		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
+			cpu_relax();
 
-	/* We must be the owner, just set the lock bit and acquire */
-	lock_set_locked(lock);
+		/* If we're the last queued, must clean up the tail. */
+		if ((val & _Q_TAIL_CPU_MASK) == tail) {
+			if (trylock_clear_tail_cpu(lock, val))
+				goto release;
+			/* Another waiter must have enqueued, or lock stolen. */
+		} else {
+			if (trylock_with_tail_cpu(lock, val))
+				goto unlock_next;
+		}
+		goto again;
+	}
 
+unlock_next:
 	/* contended path; must wait for next != NULL (MCS protocol) */
 	while (!(next = READ_ONCE(node->next)))
 		cpu_relax();
@@ -197,6 +283,9 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 
 void queued_spin_lock_slowpath(struct qspinlock *lock)
 {
+	if (try_to_steal_lock(lock))
+		return;
+
 	queued_spin_lock_mcs_queue(lock);
 }
 EXPORT_SYMBOL(queued_spin_lock_slowpath);

arch/powerpc/lib/qspinlock.c void pv_spinlocks_init(void) · line 302

Quoted 7 lines
arch/powerpc/lib/qspinlock.c +4 -0
@@ -207,3 +296,44 @@ void pv_spinlocks_init(void)
 }
 #endif
 
+#include <linux/debugfs.h>
+static int steal_spins_set(void *data, u64 val)
+{
+	static DEFINE_MUTEX(lock);

I just want to check if it would be possible to get rid of the MAYBE_STEALERS variable completely and do something like:

bool maybe_stealers() { return STEAL_SPINS > 0; }

I guess based on the below code it wouldn't work, but I'm still not quite sure why that is.

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +6 -0
+
+	mutex_lock(&lock);
+	if (val && !STEAL_SPINS) {
+		MAYBE_STEALERS = true;
+		/* wait for waiter to go away */
+		synchronize_rcu();

arch/powerpc/lib/qspinlock.c void pv_spinlocks_init(void) · line 318

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+		STEAL_SPINS = val;
+	} else if (!val && STEAL_SPINS) {
+		STEAL_SPINS = val;
+		/* wait for all possible stealers to go away */
+		synchronize_rcu();
+		MAYBE_STEALERS = false;
+	} else {
+		STEAL_SPINS = val;
+	}
+	mutex_unlock(&lock);

STEAL_SPINS is an int not a u64.

Quoted 21 lines
arch/powerpc/lib/qspinlock.c +21 -0
+
+	return 0;
+}
+
+static int steal_spins_get(void *data, u64 *val)
+{
+	*val = STEAL_SPINS;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_steal_spins, steal_spins_get, steal_spins_set, "%llu\n");
+
+static __init int spinlock_debugfs_init(void)
+{
+	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
+
+	return 0;
+}
+device_initcall(spinlock_debugfs_init);
+
#29 lore raw

Re: [PATCH 06/17] powerpc/qspinlock: theft prevention to control latency

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 25 lines

Give the queue head the ability to stop stealers. After a number of spins without sucessfully acquiring the lock, the queue head employs this, which will assure it is the next owner.

---
 arch/powerpc/include/asm/qspinlock_types.h | 10 +++-
 arch/powerpc/lib/qspinlock.c               | 56 +++++++++++++++++++++-
 2 files changed, 63 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/qspinlock_types.h +2 -1
index 210adf05b235..8b20f5e22bba 100644
@@ -29,7 +29,8 @@ typedef struct qspinlock {
  * Bitfields in the lock word:
  *
  *     0: locked bit
- * 16-31: tail cpu (+1)
+ *    16: must queue bit
+ * 17-31: tail cpu (+1)
  */
 #define	_Q_SET_MASK(type)	(((1U << _Q_ ## type ## _BITS) - 1)\
 				      << _Q_ ## type ## _OFFSET)
@@ -38,7 +39,12 @@ typedef struct qspinlock {
 #define _Q_LOCKED_MASK		_Q_SET_MASK(LOCKED)
 #define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)

arch/powerpc/include/asm/qspinlock_types.h typedef struct qspinlock { · line 49

Quoted 10 lines
arch/powerpc/include/asm/qspinlock_types.h +6 -1
 
-#define _Q_TAIL_CPU_OFFSET	16
+#define _Q_MUST_Q_OFFSET	16
+#define _Q_MUST_Q_BITS		1
+#define _Q_MUST_Q_MASK		_Q_SET_MASK(MUST_Q)
+#define _Q_MUST_Q_VAL		(1U << _Q_MUST_Q_OFFSET)
+
+#define _Q_TAIL_CPU_OFFSET	17
 #define _Q_TAIL_CPU_BITS	(32 - _Q_TAIL_CPU_OFFSET)
 #define _Q_TAIL_CPU_MASK	_Q_SET_MASK(TAIL_CPU)

Not a big deal but some of these values could be calculated like in the generic version. e.g.

	#define _Q_PENDING_OFFSET	(_Q_LOCKED_OFFSET +_Q_LOCKED_BITS)
Quoted 26 lines
arch/powerpc/lib/qspinlock.c +6 -0
index 1625cce714b2..a906cc8f15fa 100644
@@ -22,6 +22,7 @@ struct qnodes {
 /* Tuning parameters */
 static int STEAL_SPINS __read_mostly = (1<<5);
 static bool MAYBE_STEALERS __read_mostly = true;
+static int HEAD_SPINS __read_mostly = (1<<8);
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
@@ -30,6 +31,11 @@ static __always_inline int get_steal_spins(void)
 	return STEAL_SPINS;
 }
 
+static __always_inline int get_head_spins(void)
+{
+	return HEAD_SPINS;
+}
+
 static inline u32 encode_tail_cpu(void)
 {
 	return (smp_processor_id() + 1) << _Q_TAIL_CPU_OFFSET;

arch/powerpc/lib/qspinlock.c static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail) · line 157

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +7 -0
@@ -142,6 +148,23 @@ static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail)
 	return prev;
 }
 
+static __always_inline u32 lock_set_mustq(struct qspinlock *lock)
+{
+	u32 new = _Q_MUST_Q_VAL;
+	u32 prev;
+
+	asm volatile(
+"1:	lwarx	%0,0,%1		# lock_set_mustq			\n"

Is the EH bit not set because we don't hold the lock here?

arch/powerpc/lib/qspinlock.c static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail) · line 163

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +6 -0
+"	or	%0,%0,%2						\n"
+"	stwcx.	%0,0,%1							\n"
+"	bne-	1b							\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new)
+	: "cr0", "memory");

This is another usage close to the DEFINE_TESTOP() pattern.

Quoted 21 lines
arch/powerpc/lib/qspinlock.c +7 -0
+
+	return prev;
+}
+
 static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 {
 	int cpu = get_tail_cpu(val);
@@ -165,6 +188,9 @@ static inline bool try_to_steal_lock(struct qspinlock *lock)
 	for (;;) {
 		u32 val = READ_ONCE(lock->val);
 
+		if (val & _Q_MUST_Q_VAL)
+			break;
+
 		if (unlikely(!(val & _Q_LOCKED_VAL))) {
 			if (trylock_with_tail_cpu(lock, val))
 				return true;
@@ -246,11 +272,22 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 		/* We must be the owner, just set the lock bit and acquire */
 		lock_set_locked(lock);
 	} else {

arch/powerpc/lib/qspinlock.c static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock) · line 283

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +5 -1
+		int iters = 0;
+		bool set_mustq = false;
+
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
-		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
+		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
 			cpu_relax();
 
+			iters++;

It seems instead of using set_mustq, (val & _Q_MUST_Q_VAL) could be checked?

Quoted 37 lines
arch/powerpc/lib/qspinlock.c +24 -0
+			if (!set_mustq && iters >= get_head_spins()) {
+				set_mustq = true;
+				lock_set_mustq(lock);
+				val |= _Q_MUST_Q_VAL;
+			}
+		}
+
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
 			if (trylock_clear_tail_cpu(lock, val))
@@ -329,9 +366,26 @@ static int steal_spins_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_steal_spins, steal_spins_get, steal_spins_set, "%llu\n");
 
+static int head_spins_set(void *data, u64 val)
+{
+	HEAD_SPINS = val;
+
+	return 0;
+}
+
+static int head_spins_get(void *data, u64 *val)
+{
+	*val = HEAD_SPINS;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_head_spins, head_spins_get, head_spins_set, "%llu\n");
+
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
+	debugfs_create_file("qspl_head_spins", 0600, arch_debugfs_dir, NULL, &fops_head_spins);
 
 	return 0;
 }
#33 lore raw

Re: [PATCH 07/17] powerpc/qspinlock: store owner CPU in lock word

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 13 lines

Store the owner CPU number in the lock word so it may be yielded to, as powerpc's paravirtualised simple spinlocks do.

---
 arch/powerpc/include/asm/qspinlock.h       |  8 +++++++-
 arch/powerpc/include/asm/qspinlock_types.h | 10 ++++++++++
 arch/powerpc/lib/qspinlock.c               |  6 +++---
 3 files changed, 20 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/qspinlock.h +0 -0
index 3ab354159e5e..44601b261e08 100644

arch/powerpc/include/asm/qspinlock.h static __always_inline int queued_spin_is_contended(struct qspinlock *lock) · line 23

Quoted 4 lines
arch/powerpc/include/asm/qspinlock.h +1 -0
@@ -20,9 +20,15 @@ static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
 	return !!(READ_ONCE(lock->val) & _Q_TAIL_CPU_MASK);
 }
 
+static __always_inline u32 queued_spin_get_locked_val(void)

Maybe this function should have "encode" in the name to match with encode_tail_cpu().

arch/powerpc/include/asm/qspinlock.h static __always_inline int queued_spin_is_contended(struct qspinlock *lock) · line 25

Quoted 2 lines
arch/powerpc/include/asm/qspinlock.h +2 -0
+{
+	/* XXX: make this use lock value in paca like simple spinlocks? */

Is that the paca's lock_token which is 0x8000?

Quoted 15 lines
arch/powerpc/include/asm/qspinlock.h +4 -1
+	return _Q_LOCKED_VAL | (smp_processor_id() << _Q_OWNER_CPU_OFFSET);
+}
+
 static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 {
-	u32 new = _Q_LOCKED_VAL;
+	u32 new = queued_spin_get_locked_val();
 	u32 prev;
 
 	asm volatile(
arch/powerpc/include/asm/qspinlock_types.h +0 -0
index 8b20f5e22bba..35f9525381e6 100644

arch/powerpc/include/asm/qspinlock_types.h typedef struct qspinlock { · line 35

Quoted 7 lines
arch/powerpc/include/asm/qspinlock_types.h +2 -0
@@ -29,6 +29,8 @@ typedef struct qspinlock {
  * Bitfields in the lock word:
  *
  *     0: locked bit
+ *  1-14: lock holder cpu
+ *    15: unused bit
  *    16: must queue bit
  * 17-31: tail cpu (+1)

So there is one more bit to store the tail cpu vs the lock holder cpu?

Quoted 47 lines
arch/powerpc/include/asm/qspinlock_types.h +8 -0
  */
@@ -39,6 +41,14 @@ typedef struct qspinlock {
 #define _Q_LOCKED_MASK		_Q_SET_MASK(LOCKED)
 #define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
 
+#define _Q_OWNER_CPU_OFFSET	1
+#define _Q_OWNER_CPU_BITS	14
+#define _Q_OWNER_CPU_MASK	_Q_SET_MASK(OWNER_CPU)
+
+#if CONFIG_NR_CPUS > (1U << _Q_OWNER_CPU_BITS)
+#error "qspinlock does not support such large CONFIG_NR_CPUS"
+#endif
+
 #define _Q_MUST_Q_OFFSET	16
 #define _Q_MUST_Q_BITS		1
 #define _Q_MUST_Q_MASK		_Q_SET_MASK(MUST_Q)
arch/powerpc/lib/qspinlock.c +3 -3
index a906cc8f15fa..aa26cfe21f18 100644
@@ -50,7 +50,7 @@ static inline int get_tail_cpu(u32 val)
 /* Take the lock by setting the lock bit, no other CPUs will touch it. */
 static __always_inline void lock_set_locked(struct qspinlock *lock)
 {
-	u32 new = _Q_LOCKED_VAL;
+	u32 new = queued_spin_get_locked_val();
 	u32 prev;
 
 	asm volatile(
@@ -68,7 +68,7 @@ static __always_inline void lock_set_locked(struct qspinlock *lock)
 /* Take lock, clearing tail, cmpxchg with old (which must not be locked) */
 static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 old)
 {
-	u32 new = _Q_LOCKED_VAL;
+	u32 new = queued_spin_get_locked_val();
 	u32 prev;
 
 	BUG_ON(old & _Q_LOCKED_VAL);
@@ -116,7 +116,7 @@ static __always_inline u32 __trylock_cmpxchg(struct qspinlock *lock, u32 old, u3
 /* Take lock, preserving tail, cmpxchg with val (which must not be locked) */
 static __always_inline int trylock_with_tail_cpu(struct qspinlock *lock, u32 val)
 {
-	u32 newval = _Q_LOCKED_VAL | (val & _Q_TAIL_CPU_MASK);
+	u32 newval = queued_spin_get_locked_val() | (val & _Q_TAIL_CPU_MASK);
 
 	if (__trylock_cmpxchg(lock, val, newval) == val)
 		return 1;
#37 lore raw

Re: [PATCH 08/17] powerpc/qspinlock: paravirt yield to lock owner

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 20 lines

Waiters spinning on the lock word should yield to the lock owner if the vCPU is preempted. This improves performance when the hypervisor has oversubscribed physical CPUs.

---
 arch/powerpc/lib/qspinlock.c | 97 ++++++++++++++++++++++++++++++------
 1 file changed, 83 insertions(+), 14 deletions(-)
arch/powerpc/lib/qspinlock.c +1 -0
index aa26cfe21f18..55286ac91da5 100644
@@ -5,6 +5,7 @@
 #include <linux/percpu.h>
 #include <linux/smp.h>
 #include <asm/qspinlock.h>
+#include <asm/paravirt.h>
 
 #define MAX_NODES	4
 

arch/powerpc/lib/qspinlock.c static int STEAL_SPINS __read_mostly = (1<<5); · line 28

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -24,14 +25,16 @@ static int STEAL_SPINS __read_mostly = (1<<5);
 static bool MAYBE_STEALERS __read_mostly = true;
 static int HEAD_SPINS __read_mostly = (1<<8);
 
+static bool pv_yield_owner __read_mostly = true;

Not macro case for these globals? To me name does not make it super clear this is a boolean. What about pv_yield_owner_enabled?

Quoted 28 lines
arch/powerpc/lib/qspinlock.c +8 -3
+
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
-static __always_inline int get_steal_spins(void)
+static __always_inline int get_steal_spins(bool paravirt)
 {
 	return STEAL_SPINS;
 }
 
-static __always_inline int get_head_spins(void)
+static __always_inline int get_head_spins(bool paravirt)
 {
 	return HEAD_SPINS;
 }
@@ -46,7 +49,11 @@ static inline int get_tail_cpu(u32 val)
 	return (val >> _Q_TAIL_CPU_OFFSET) - 1;
 }
 
-/* Take the lock by setting the bit, no other CPUs may concurrently lock it. */
+static inline int get_owner_cpu(u32 val)
+{
+	return (val & _Q_OWNER_CPU_MASK) >> _Q_OWNER_CPU_OFFSET;
+}
+
 /* Take the lock by setting the lock bit, no other CPUs will touch it. */
 static __always_inline void lock_set_locked(struct qspinlock *lock)
 {

arch/powerpc/lib/qspinlock.c static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val) · line 190

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +1 -1
@@ -180,7 +187,45 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
-static inline bool try_to_steal_lock(struct qspinlock *lock)
+static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)

This name doesn't seem correct for the non paravirt case.

Quoted 7 lines
arch/powerpc/lib/qspinlock.c +7 -0
+{
+	int owner;
+	u32 yield_count;
+
+	BUG_ON(!(val & _Q_LOCKED_VAL));
+
+	if (!paravirt)

arch/powerpc/lib/qspinlock.c static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val) · line 207

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+		goto relax;
+
+	if (!pv_yield_owner)
+		goto relax;
+
+	owner = get_owner_cpu(val);
+	yield_count = yield_count_of(owner);
+
+	if ((yield_count & 1) == 0)
+		goto relax; /* owner vcpu is running */

I wonder why not use vcpu_is_preempted()?

Quoted 76 lines
arch/powerpc/lib/qspinlock.c +27 -10
+
+	/*
+	 * Read the lock word after sampling the yield count. On the other side
+	 * there may a wmb because the yield count update is done by the
+	 * hypervisor preemption and the value update by the OS, however this
+	 * ordering might reduce the chance of out of order accesses and
+	 * improve the heuristic.
+	 */
+	smp_rmb();
+
+	if (READ_ONCE(lock->val) == val) {
+		yield_to_preempted(owner, yield_count);
+		/* Don't relax if we yielded. Maybe we should? */
+		return;
+	}
+relax:
+	cpu_relax();
+}
+
+
+static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {
 	int iters;
 
@@ -197,18 +242,18 @@ static inline bool try_to_steal_lock(struct qspinlock *lock)
 			continue;
 		}
 
-		cpu_relax();
+		yield_to_locked_owner(lock, val, paravirt);
 
 		iters++;
 
-		if (iters >= get_steal_spins())
+		if (iters >= get_steal_spins(paravirt))
 			break;
 	}
 
 	return false;
 }
 
-static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
+static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, bool paravirt)
 {
 	struct qnodes *qnodesp;
 	struct qnode *next, *node;
@@ -260,7 +305,7 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 	if (!MAYBE_STEALERS) {
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
-			cpu_relax();
+			yield_to_locked_owner(lock, val, paravirt);
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -278,10 +323,10 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
-			cpu_relax();
+			yield_to_locked_owner(lock, val, paravirt);
 
 			iters++;
-			if (!set_mustq && iters >= get_head_spins()) {
+			if (!set_mustq && iters >= get_head_spins(paravirt)) {
 				set_mustq = true;
 				lock_set_mustq(lock);
 				val |= _Q_MUST_Q_VAL;
@@ -320,10 +365,15 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 
 void queued_spin_lock_slowpath(struct qspinlock *lock)
 {
-	if (try_to_steal_lock(lock))
-		return;
-
-	queued_spin_lock_mcs_queue(lock);

arch/powerpc/lib/qspinlock.c static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock) · line 377

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +9 -0
+	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) && is_shared_processor()) {
+		if (try_to_steal_lock(lock, true))
+			return;
+		queued_spin_lock_mcs_queue(lock, true);
+	} else {
+		if (try_to_steal_lock(lock, false))
+			return;
+		queued_spin_lock_mcs_queue(lock, false);
+	}
 }

There is not really a need for a conditional:

bool paravirt = IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) && is_shared_processor();

if (try_to_steal_lock(lock, paravirt))

	return;

queued_spin_lock_mcs_queue(lock, paravirt);

The paravirt parameter used by the various functions seems always to be
equivalent to (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) && is_shared_processor()).
I wonder if it would be simpler testing (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) && is_shared_processor()) (using a helper function) in those functions instead passing it as a parameter?

Quoted 32 lines
arch/powerpc/lib/qspinlock.c +19 -0
 EXPORT_SYMBOL(queued_spin_lock_slowpath);
 
@@ -382,10 +432,29 @@ static int head_spins_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_head_spins, head_spins_get, head_spins_set, "%llu\n");
 
+static int pv_yield_owner_set(void *data, u64 val)
+{
+	pv_yield_owner = !!val;
+
+	return 0;
+}
+
+static int pv_yield_owner_get(void *data, u64 *val)
+{
+	*val = pv_yield_owner;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_owner, pv_yield_owner_get, pv_yield_owner_set, "%llu\n");
+
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
 	debugfs_create_file("qspl_head_spins", 0600, arch_debugfs_dir, NULL, &fops_head_spins);
+	if (is_shared_processor()) {
+		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
+	}
 
 	return 0;
 }
#41 lore raw

Re: [PATCH 09/17] powerpc/qspinlock: implement option to yield to previous node

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 15 lines

Queued waiters which are not at the head of the queue don't spin on the lock word but their qnode lock word, waiting for the previous queued CPU to release them. Add an option which allows these waiters to yield to the previous CPU if its vCPU is preempted.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 46 +++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +0 -0
index 55286ac91da5..b39f8c5b329c 100644

arch/powerpc/lib/qspinlock.c static bool MAYBE_STEALERS __read_mostly = true; · line 29

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -26,6 +26,7 @@ static bool MAYBE_STEALERS __read_mostly = true;
 static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
+static bool pv_yield_prev __read_mostly = true;

Similiar suggestion, maybe pv_yield_prev_enabled would read better.

Isn't this enabled by default contrary to the commit message?

arch/powerpc/lib/qspinlock.c static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va · line 228

Quoted 8 lines
arch/powerpc/lib/qspinlock.c +1 -0
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
@@ -224,6 +225,31 @@ static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va
 	cpu_relax();
 }
 
+static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)

yield_to_locked_owner() takes a raw val and works out the cpu to yield to. I think for consistency have yield_to_prev() take the raw val and work it out too.

Quoted 28 lines
arch/powerpc/lib/qspinlock.c +24 -0
+{
+	u32 yield_count;
+
+	if (!paravirt)
+		goto relax;
+
+	if (!pv_yield_prev)
+		goto relax;
+
+	yield_count = yield_count_of(prev_cpu);
+	if ((yield_count & 1) == 0)
+		goto relax; /* owner vcpu is running */
+
+	smp_rmb(); /* See yield_to_locked_owner comment */
+
+	if (!node->locked) {
+		yield_to_preempted(prev_cpu, yield_count);
+		return;
+	}
+
+relax:
+	cpu_relax();
+}
+
 
 static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {

arch/powerpc/lib/qspinlock.c static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b · line 320

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -291,13 +317,14 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	 */
 	if (old & _Q_TAIL_CPU_MASK) {
 		struct qnode *prev = get_tail_qnode(lock, old);
+		int prev_cpu = get_tail_cpu(old);

This could then be removed.

arch/powerpc/lib/qspinlock.c static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b · line 327

Quoted 8 lines
arch/powerpc/lib/qspinlock.c +1 -1
 		/* Link @node into the waitqueue. */
 		WRITE_ONCE(prev->next, node);
 
 		/* Wait for mcs node lock to be released */
 		while (!node->locked)
-			cpu_relax();
+			yield_to_prev(lock, node, prev_cpu, paravirt);

And would have this as:

			yield_to_prev(lock, node, old, paravirt);
Quoted 33 lines
arch/powerpc/lib/qspinlock.c +17 -0
 		smp_rmb(); /* acquire barrier for the mcs lock */
 	}
@@ -448,12 +475,29 @@ static int pv_yield_owner_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_owner, pv_yield_owner_get, pv_yield_owner_set, "%llu\n");
 
+static int pv_yield_prev_set(void *data, u64 val)
+{
+	pv_yield_prev = !!val;
+
+	return 0;
+}
+
+static int pv_yield_prev_get(void *data, u64 *val)
+{
+	*val = pv_yield_prev;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_prev, pv_yield_prev_get, pv_yield_prev_set, "%llu\n");
+
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
 	debugfs_create_file("qspl_head_spins", 0600, arch_debugfs_dir, NULL, &fops_head_spins);
 	if (is_shared_processor()) {
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
+		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
 	}
 
 	return 0;
#45 lore raw

Re: [PATCH 10/17] powerpc/qspinlock: allow stealing when head of queue yields

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 15 lines

If the head of queue is preventing stealing but it finds the owner vCPU is preempted, it will yield its cycles to the owner which could cause it to become preempted. Add an option to re-allow stealers before yielding, and disallow them again after returning from the yield.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 56 ++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 3 deletions(-)
arch/powerpc/lib/qspinlock.c +0 -0
index b39f8c5b329c..94f007f66942 100644

arch/powerpc/lib/qspinlock.c static bool MAYBE_STEALERS __read_mostly = true; · line 29

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -26,6 +26,7 @@ static bool MAYBE_STEALERS __read_mostly = true;
 static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
+static bool pv_yield_allow_steal __read_mostly = false;

To me this one does read as a boolean, but if you go with those other changes I'd make it pv_yield_steal_enable to be consistent.

Quoted 11 lines
arch/powerpc/lib/qspinlock.c +4 -0
 static bool pv_yield_prev __read_mostly = true;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
@@ -173,6 +174,23 @@ static __always_inline u32 lock_set_mustq(struct qspinlock *lock)
 	return prev;
 }
 
+static __always_inline u32 lock_clear_mustq(struct qspinlock *lock)
+{
+	u32 new = _Q_MUST_Q_VAL;
+	u32 prev;

arch/powerpc/lib/qspinlock.c static __always_inline u32 lock_set_mustq(struct qspinlock *lock) · line 190

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+
+	asm volatile(
+"1:	lwarx	%0,0,%1		# lock_clear_mustq			\n"
+"	andc	%0,%0,%2						\n"
+"	stwcx.	%0,0,%1							\n"
+"	bne-	1b							\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new)
+	: "cr0", "memory");
+

This is pretty similar to the DEFINE_TESTOP() pattern again with the same llong caveat.

arch/powerpc/lib/qspinlock.c static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val) · line 209

Quoted 12 lines
arch/powerpc/lib/qspinlock.c +4 -1
+	return prev;
+}
+
 static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 {
 	int cpu = get_tail_cpu(val);
@@ -188,7 +206,7 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)

/* See yield_to_locked_owner comment */ comment needs to be updated now.

Quoted 16 lines
arch/powerpc/lib/qspinlock.c +4 -0
 {
 	int owner;
 	u32 yield_count;
@@ -217,7 +235,11 @@ static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va
 	smp_rmb();
 
 	if (READ_ONCE(lock->val) == val) {
+		if (clear_mustq)
+			lock_clear_mustq(lock);
 		yield_to_preempted(owner, yield_count);
+		if (clear_mustq)
+			lock_set_mustq(lock);
 		/* Don't relax if we yielded. Maybe we should? */
 		return;
 	}

arch/powerpc/lib/qspinlock.c static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va · line 256

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +7 -0
@@ -225,6 +247,16 @@ static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va
 	cpu_relax();
 }
 
+static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+{
+	__yield_to_locked_owner(lock, val, paravirt, false);
+}
+
+static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+{

The check for pv_yield_allow_steal seems like it could go here instead of being done by the caller.
__yield_to_locked_owner() checks for pv_yield_owner so it seems more
consistent.

Quoted 56 lines
arch/powerpc/lib/qspinlock.c +23 -2
+	__yield_to_locked_owner(lock, val, paravirt, clear_mustq);
+}
+
 static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
 {
 	u32 yield_count;
@@ -332,7 +364,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	if (!MAYBE_STEALERS) {
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
-			yield_to_locked_owner(lock, val, paravirt);
+			yield_head_to_locked_owner(lock, val, paravirt, false);
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -350,7 +382,8 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
-			yield_to_locked_owner(lock, val, paravirt);
+			yield_head_to_locked_owner(lock, val, paravirt,
+					pv_yield_allow_steal && set_mustq);
 
 			iters++;
 			if (!set_mustq && iters >= get_head_spins(paravirt)) {
@@ -475,6 +508,22 @@ static int pv_yield_owner_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_owner, pv_yield_owner_get, pv_yield_owner_set, "%llu\n");
 
+static int pv_yield_allow_steal_set(void *data, u64 val)
+{
+	pv_yield_allow_steal = !!val;
+
+	return 0;
+}
+
+static int pv_yield_allow_steal_get(void *data, u64 *val)
+{
+	*val = pv_yield_allow_steal;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_allow_steal, pv_yield_allow_steal_get, pv_yield_allow_steal_set, "%llu\n");
+
 static int pv_yield_prev_set(void *data, u64 val)
 {
 	pv_yield_prev = !!val;
@@ -497,6 +546,7 @@ static __init int spinlock_debugfs_init(void)
 	debugfs_create_file("qspl_head_spins", 0600, arch_debugfs_dir, NULL, &fops_head_spins);
 	if (is_shared_processor()) {
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
+		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
 	}
 
#50 lore raw

Re: [PATCH 11/17] powerpc/qspinlock: allow propagation of yield CPU down the queue

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 30 lines

Having all CPUs poll the lock word for the owner CPU that should be yielded to defeats most of the purpose of using MCS queueing for scalability. Yet it may be desirable for queued waiters to to yield to a preempted owner.

s390 addreses this problem by having queued waiters sample the lock word to find the owner much less frequently. In this approach, the waiters never sample it directly, but the queue head propagates the owner CPU back to the next waiter if it ever finds the owner has been preempted. Queued waiters then subsequently propagate the owner CPU back to the next waiter, and so on.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 85 +++++++++++++++++++++++++++++++++++-
 1 file changed, 84 insertions(+), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +1 -0
index 94f007f66942..28c85a2d5635 100644
@@ -12,6 +12,7 @@
 struct qnode {
 	struct qnode	*next;
 	struct qspinlock *lock;
+	int		yield_cpu;
 	u8		locked; /* 1 if lock acquired */
 };
 

arch/powerpc/lib/qspinlock.c static int HEAD_SPINS __read_mostly = (1<<8); · line 32

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -28,6 +29,7 @@ static int HEAD_SPINS __read_mostly = (1<<8);
 static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
+static bool pv_yield_propagate_owner __read_mostly = true;

This also seems to be enabled by default.

Quoted 16 lines
arch/powerpc/lib/qspinlock.c +9 -0
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
@@ -257,13 +259,66 @@ static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u
 	__yield_to_locked_owner(lock, val, paravirt, clear_mustq);
 }
 
+static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int *set_yield_cpu, bool paravirt)
+{
+	struct qnode *next;
+	int owner;
+
+	if (!paravirt)
+		return;
+	if (!pv_yield_propagate_owner)
+		return;

arch/powerpc/lib/qspinlock.c static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u · line 280

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+
+	owner = get_owner_cpu(val);
+	if (*set_yield_cpu == owner)
+		return;
+
+	next = READ_ONCE(node->next);
+	if (!next)
+		return;
+
+	if (vcpu_is_preempted(owner)) {

Is there a difference about using vcpu_is_preempted() here vs checking bit 0 in other places?

arch/powerpc/lib/qspinlock.c static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u · line 283

Quoted 3 lines
arch/powerpc/lib/qspinlock.c +3 -0
+		next->yield_cpu = owner;
+		*set_yield_cpu = owner;
+	} else if (*set_yield_cpu != -1) {

It might be worth giving the -1 CPU a #define.

arch/powerpc/lib/qspinlock.c static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u · line 287

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +4 -0
+		next->yield_cpu = owner;
+		*set_yield_cpu = owner;
+	}
+}

Does this need to pass set_yield_cpu by reference? Couldn't it's new value be returned? To me it makes it more clear the function is used to change set_yield_cpu. I think this would work:

int set_yield_cpu = -1;

static __always_inline int propagate_yield_cpu(struct qnode *node, u32 val, int set_yield_cpu, bool paravirt)

{
	struct qnode *next;
	int owner;

	if (!paravirt)
		goto out;
	if (!pv_yield_propagate_owner)
		goto out;

	owner = get_owner_cpu(val);
	if (set_yield_cpu == owner)
		goto out;

	next = READ_ONCE(node->next);
	if (!next)
		goto out;

	if (vcpu_is_preempted(owner)) {
		next->yield_cpu = owner;
		return owner;
	} else if (set_yield_cpu != -1) {
		next->yield_cpu = owner;
		return owner;
	}

out:

	return set_yield_cpu;
}

set_yield_cpu = propagate_yield_cpu(... set_yield_cpu ...);

Quoted 114 lines
arch/powerpc/lib/qspinlock.c +56 -1
+
 static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
 {
 	u32 yield_count;
+	int yield_cpu;
 
 	if (!paravirt)
 		goto relax;
 
+	if (!pv_yield_propagate_owner)
+		goto yield_prev;
+
+	yield_cpu = READ_ONCE(node->yield_cpu);
+	if (yield_cpu == -1) {
+		/* Propagate back the -1 CPU */
+		if (node->next && node->next->yield_cpu != -1)
+			node->next->yield_cpu = yield_cpu;
+		goto yield_prev;
+	}
+
+	yield_count = yield_count_of(yield_cpu);
+	if ((yield_count & 1) == 0)
+		goto yield_prev; /* owner vcpu is running */
+
+	smp_rmb();
+
+	if (yield_cpu == node->yield_cpu) {
+		if (node->next && node->next->yield_cpu != yield_cpu)
+			node->next->yield_cpu = yield_cpu;
+		yield_to_preempted(yield_cpu, yield_count);
+		return;
+	}
+
+yield_prev:
 	if (!pv_yield_prev)
 		goto relax;
 
@@ -337,6 +392,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	node = &qnodesp->nodes[idx];
 	node->next = NULL;
 	node->lock = lock;
+	node->yield_cpu = -1;
 	node->locked = 0;
 
 	tail = encode_tail_cpu();
@@ -358,13 +414,21 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		while (!node->locked)
 			yield_to_prev(lock, node, prev_cpu, paravirt);
 
+		/* Clear out stale propagated yield_cpu */
+		if (paravirt && pv_yield_propagate_owner && node->yield_cpu != -1)
+			node->yield_cpu = -1;
+
 		smp_rmb(); /* acquire barrier for the mcs lock */
 	}
 
 	if (!MAYBE_STEALERS) {
+		int set_yield_cpu = -1;
+
 		/* We're at the head of the waitqueue, wait for the lock. */
-		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
+		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt, false);
+		}
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -376,12 +440,14 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		/* We must be the owner, just set the lock bit and acquire */
 		lock_set_locked(lock);
 	} else {
+		int set_yield_cpu = -1;
 		int iters = 0;
 		bool set_mustq = false;
 
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt,
 					pv_yield_allow_steal && set_mustq);
 
@@ -540,6 +606,22 @@ static int pv_yield_prev_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_prev, pv_yield_prev_get, pv_yield_prev_set, "%llu\n");
 
+static int pv_yield_propagate_owner_set(void *data, u64 val)
+{
+	pv_yield_propagate_owner = !!val;
+
+	return 0;
+}
+
+static int pv_yield_propagate_owner_get(void *data, u64 *val)
+{
+	*val = pv_yield_propagate_owner;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_propagate_owner, pv_yield_propagate_owner_get, pv_yield_propagate_owner_set, "%llu\n");
+
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
@@ -548,6 +630,7 @@ static __init int spinlock_debugfs_init(void)
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
 		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
+		debugfs_create_file("qspl_pv_yield_propagate_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);
 	}
 
 	return 0;
#54 lore raw

Re: [PATCH 12/17] powerpc/qspinlock: add ability to prod new queue head CPU

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 31 lines

After the head of the queue acquires the lock, it releases the next waiter in the queue to become the new head. Add an option to prod the new head if its vCPU was preempted. This may only have an effect if queue waiters are yielding.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +2 -0
index 28c85a2d5635..3b10e31bcf0a 100644
@@ -12,6 +12,7 @@
 struct qnode {
 	struct qnode	*next;
 	struct qspinlock *lock;
+	int		cpu;
 	int		yield_cpu;
 	u8		locked; /* 1 if lock acquired */
 };
@@ -30,6 +31,7 @@ static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
 static bool pv_yield_propagate_owner __read_mostly = true;
+static bool pv_prod_head __read_mostly = false;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 

arch/powerpc/lib/qspinlock.c static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b · line 397

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -392,6 +394,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	node = &qnodesp->nodes[idx];
 	node->next = NULL;
 	node->lock = lock;
+	node->cpu = smp_processor_id();

I suppose this could be used in some other places too.

For example change:

	yield_to_prev(lock, node, prev, paravirt);

In yield_to_prev() it could then access the prev->cpu.

Quoted 50 lines
arch/powerpc/lib/qspinlock.c +25 -1
 	node->yield_cpu = -1;
 	node->locked = 0;
 
@@ -483,7 +486,14 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	 * this store to locked. The corresponding barrier is the smp_rmb()
 	 * acquire barrier for mcs lock, above.
 	 */
-	WRITE_ONCE(next->locked, 1);
+	if (paravirt && pv_prod_head) {
+		int next_cpu = next->cpu;
+		WRITE_ONCE(next->locked, 1);
+		if (vcpu_is_preempted(next_cpu))
+			prod_cpu(next_cpu);
+	} else {
+		WRITE_ONCE(next->locked, 1);
+	}
 
 release:
 	qnodesp->count--; /* release the node */
@@ -622,6 +632,22 @@ static int pv_yield_propagate_owner_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_propagate_owner, pv_yield_propagate_owner_get, pv_yield_propagate_owner_set, "%llu\n");
 
+static int pv_prod_head_set(void *data, u64 val)
+{
+	pv_prod_head = !!val;
+
+	return 0;
+}
+
+static int pv_prod_head_get(void *data, u64 *val)
+{
+	*val = pv_prod_head;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_prod_head, pv_prod_head_get, pv_prod_head_set, "%llu\n");
+
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
@@ -631,6 +657,7 @@ static __init int spinlock_debugfs_init(void)
 		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
 		debugfs_create_file("qspl_pv_yield_propagate_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);
+		debugfs_create_file("qspl_pv_prod_head", 0600, arch_debugfs_dir, NULL, &fops_pv_prod_head);
 	}
 
 	return 0;
#58 lore raw

Re: [PATCH 13/17] powerpc/qspinlock: trylock and initial lock attempt may steal

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 13 lines

This gives trylock slightly more strength, and it also gives most of the benefit of passing 'val' back through the slowpath without the complexity.

---
 arch/powerpc/include/asm/qspinlock.h | 39 +++++++++++++++++++++++++++-
 arch/powerpc/lib/qspinlock.c         |  9 +++++++
 2 files changed, 47 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/qspinlock.h +0 -0
index 44601b261e08..d3d2039237b2 100644

arch/powerpc/include/asm/qspinlock.h line 8

Quoted 4 lines
arch/powerpc/include/asm/qspinlock.h +1 -0
@@ -5,6 +5,8 @@
 #include <linux/compiler.h>
 #include <asm/qspinlock_types.h>
 
+#define _Q_SPIN_TRY_LOCK_STEAL 1

Would this be a config option?

Quoted 7 lines
arch/powerpc/include/asm/qspinlock.h +1 -0
+
 static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
 {
 	return READ_ONCE(lock->val);
@@ -26,11 +28,12 @@ static __always_inline u32 queued_spin_get_locked_val(void)
 	return _Q_LOCKED_VAL | (smp_processor_id() << _Q_OWNER_CPU_OFFSET);
 }

arch/powerpc/include/asm/qspinlock.h static __always_inline u32 queued_spin_get_locked_val(void) · line 38

Quoted 10 lines
arch/powerpc/include/asm/qspinlock.h +2 -1
 
-static __always_inline int queued_spin_trylock(struct qspinlock *lock)
+static __always_inline int __queued_spin_trylock_nosteal(struct qspinlock *lock)
 {
 	u32 new = queued_spin_get_locked_val();
 	u32 prev;
 
+	/* Trylock succeeds only when unlocked and no queued nodes */
 	asm volatile(
 "1:	lwarx	%0,0,%1,%3	# queued_spin_trylock			\n"

s/queued_spin_trylock/__queued_spin_trylock_nosteal

arch/powerpc/include/asm/qspinlock.h static __always_inline int queued_spin_trylock(struct qspinlock *lock) · line 62

Quoted 13 lines
arch/powerpc/include/asm/qspinlock.h +8 -0
 "	cmpwi	0,%0,0							\n"
@@ -49,6 +52,40 @@ static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 	return 0;
 }
 
+static __always_inline int __queued_spin_trylock_steal(struct qspinlock *lock)
+{
+	u32 new = queued_spin_get_locked_val();
+	u32 prev, tmp;
+
+	/* Trylock may get ahead of queued nodes if it finds unlocked */
+	asm volatile(
+"1:	lwarx	%0,0,%2,%5	# queued_spin_trylock			\n"

s/queued_spin_trylock/__queued_spin_trylock_steal

arch/powerpc/include/asm/qspinlock.h static __always_inline int queued_spin_trylock(struct qspinlock *lock) · line 70

Quoted 8 lines
arch/powerpc/include/asm/qspinlock.h +8 -0
+"	andc.	%1,%0,%4						\n"
+"	bne-	2f							\n"
+"	and	%1,%0,%4						\n"
+"	or	%1,%1,%3						\n"
+"	stwcx.	%1,0,%2							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"

Just because there's a little bit more going on here...

Q_TAIL_CPU_MASK = 0xFFFE0000
~Q_TAIL_CPU_MASK = 0x1FFFF

1: lwarx prev, 0, &lock->val, IS_ENABLED_PPC64

	andc.	tmp, prev, _Q_TAIL_CPU_MASK 	(tmp = prev & ~_Q_TAIL_CPU_MASK)
	bne-	2f 				(exit if locked)
	and	tmp, prev, _Q_TAIL_CPU_MASK 	(tmp = prev & _Q_TAIL_CPU_MASK)
	or	tmp, tmp, new			(tmp |= new)					
	stwcx.	tmp, 0, &lock->val					
		
	bne-	1b							
	PPC_ACQUIRE_BARRIER		

2:

... which seems correct.

Quoted 56 lines
arch/powerpc/include/asm/qspinlock.h +18 -0
+	: "=&r" (prev), "=&r" (tmp)
+	: "r" (&lock->val), "r" (new), "r" (_Q_TAIL_CPU_MASK),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");
+
+	if (likely(!(prev & ~_Q_TAIL_CPU_MASK)))
+		return 1;
+	return 0;
+}
+
+static __always_inline int queued_spin_trylock(struct qspinlock *lock)
+{
+	if (!_Q_SPIN_TRY_LOCK_STEAL)
+		return __queued_spin_trylock_nosteal(lock);
+	else
+		return __queued_spin_trylock_steal(lock);
+}
+
 void queued_spin_lock_slowpath(struct qspinlock *lock);
 
 static __always_inline void queued_spin_lock(struct qspinlock *lock)
arch/powerpc/lib/qspinlock.c +9 -0
index 3b10e31bcf0a..277aef1fab0a 100644
@@ -24,7 +24,11 @@ struct qnodes {
 
 /* Tuning parameters */
 static int STEAL_SPINS __read_mostly = (1<<5);
+#if _Q_SPIN_TRY_LOCK_STEAL == 1
+static const bool MAYBE_STEALERS = true;
+#else
 static bool MAYBE_STEALERS __read_mostly = true;
+#endif
 static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
@@ -522,6 +526,10 @@ void pv_spinlocks_init(void)
 #include <linux/debugfs.h>
 static int steal_spins_set(void *data, u64 val)
 {
+#if _Q_SPIN_TRY_LOCK_STEAL == 1
+	/* MAYBE_STEAL remains true */
+	STEAL_SPINS = val;
+#else
 	static DEFINE_MUTEX(lock);
 
 	mutex_lock(&lock);
@@ -539,6 +547,7 @@ static int steal_spins_set(void *data, u64 val)
 		STEAL_SPINS = val;
 	}
 	mutex_unlock(&lock);
+#endif
 
 	return 0;
 }
#62 lore raw

Re: [PATCH 14/17] powerpc/qspinlock: use spin_begin/end API

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 3 lines

Use the spin_begin/spin_cpu_relax/spin_end APIs in qspinlock, which helps to prevent threads issuing a lot of expensive priority nops which may not have much effect due to immediately executing low then medium priority.

Just a general comment regarding the spin_{begin,end} API, more complicated than something like

	spin_begin()
	for(;;)
		spin_cpu_relax()
	spin_end()

it becomes difficult to keep track of. Unfortunately, I don't have any good suggestions how to improve it. Hopefully with P10s wait instruction we can maybe try and move away from this.

It might be useful to comment the functions pre and post conditions regarding expectations about spin_begin() and spin_end().

Quoted 155 lines
---
 arch/powerpc/lib/qspinlock.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
arch/powerpc/lib/qspinlock.c +31 -4
index 277aef1fab0a..d4594c701f7d 100644
@@ -233,6 +233,8 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 	if ((yield_count & 1) == 0)
 		goto relax; /* owner vcpu is running */
 
+	spin_end();
+
 	/*
 	 * Read the lock word after sampling the yield count. On the other side
 	 * there may a wmb because the yield count update is done by the
@@ -248,11 +250,13 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 		yield_to_preempted(owner, yield_count);
 		if (clear_mustq)
 			lock_set_mustq(lock);
+		spin_begin();
 		/* Don't relax if we yielded. Maybe we should? */
 		return;
 	}
+	spin_begin();
 relax:
-	cpu_relax();
+	spin_cpu_relax();
 }
 
 static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
@@ -315,14 +319,18 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 	if ((yield_count & 1) == 0)
 		goto yield_prev; /* owner vcpu is running */
 
+	spin_end();
+
 	smp_rmb();
 
 	if (yield_cpu == node->yield_cpu) {
 		if (node->next && node->next->yield_cpu != yield_cpu)
 			node->next->yield_cpu = yield_cpu;
 		yield_to_preempted(yield_cpu, yield_count);
+		spin_begin();
 		return;
 	}
+	spin_begin();
 
 yield_prev:
 	if (!pv_yield_prev)
@@ -332,15 +340,19 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 	if ((yield_count & 1) == 0)
 		goto relax; /* owner vcpu is running */
 
+	spin_end();
+
 	smp_rmb(); /* See yield_to_locked_owner comment */
 
 	if (!node->locked) {
 		yield_to_preempted(prev_cpu, yield_count);
+		spin_begin();
 		return;
 	}
+	spin_begin();
 
 relax:
-	cpu_relax();
+	spin_cpu_relax();
 }
 
 
@@ -349,6 +361,7 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 	int iters;
 
 	/* Attempt to steal the lock */
+	spin_begin();
 	for (;;) {
 		u32 val = READ_ONCE(lock->val);
 
@@ -356,8 +369,10 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 			break;
 
 		if (unlikely(!(val & _Q_LOCKED_VAL))) {
+			spin_end();
 			if (trylock_with_tail_cpu(lock, val))
 				return true;
+			spin_begin();
 			continue;
 		}
 
@@ -368,6 +383,7 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 		if (iters >= get_steal_spins(paravirt))
 			break;
 	}
+	spin_end();
 
 	return false;
 }
@@ -418,8 +434,10 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		WRITE_ONCE(prev->next, node);
 
 		/* Wait for mcs node lock to be released */
+		spin_begin();
 		while (!node->locked)
 			yield_to_prev(lock, node, prev_cpu, paravirt);
+		spin_end();
 
 		/* Clear out stale propagated yield_cpu */
 		if (paravirt && pv_yield_propagate_owner && node->yield_cpu != -1)
@@ -432,10 +450,12 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		int set_yield_cpu = -1;
 
 		/* We're at the head of the waitqueue, wait for the lock. */
+		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt, false);
 		}
+		spin_end();
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -453,6 +473,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
+		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt,
@@ -465,6 +486,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 				val |= _Q_MUST_Q_VAL;
 			}
 		}
+		spin_end();
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -480,8 +502,13 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 
 unlock_next:
 	/* contended path; must wait for next != NULL (MCS protocol) */
-	while (!(next = READ_ONCE(node->next)))
-		cpu_relax();
+	next = READ_ONCE(node->next);
+	if (!next) {
+		spin_begin();
+		while (!(next = READ_ONCE(node->next)))
+			cpu_relax();
+		spin_end();
+	}
 
 	/*
 	 * Unlock the next mcs waiter node. Release barrier is not required
#66 lore raw

Re: [PATCH 15/17] powerpc/qspinlock: reduce remote node steal spins

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 45 lines

Allow for a reduction in the number of times a CPU from a different node than the owner can attempt to steal the lock before queueing. This could bias the transfer behaviour of the lock across the machine and reduce NUMA crossings.

---
 arch/powerpc/lib/qspinlock.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)
arch/powerpc/lib/qspinlock.c +8 -2
index d4594c701f7d..24f68bd71e2b 100644
@@ -4,6 +4,7 @@
 #include <linux/export.h>
 #include <linux/percpu.h>
 #include <linux/smp.h>
+#include <linux/topology.h>
 #include <asm/qspinlock.h>
 #include <asm/paravirt.h>
 
@@ -24,6 +25,7 @@ struct qnodes {
 
 /* Tuning parameters */
 static int STEAL_SPINS __read_mostly = (1<<5);
+static int REMOTE_STEAL_SPINS __read_mostly = (1<<2);
 #if _Q_SPIN_TRY_LOCK_STEAL == 1
 static const bool MAYBE_STEALERS = true;
 #else
@@ -39,9 +41,13 @@ static bool pv_prod_head __read_mostly = false;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
-static __always_inline int get_steal_spins(bool paravirt)
+static __always_inline int get_steal_spins(bool paravirt, bool remote)
 {
-	return STEAL_SPINS;
+	if (remote) {
+		return REMOTE_STEAL_SPINS;
+	} else {
+		return STEAL_SPINS;
+	}
 }
 
 static __always_inline int get_head_spins(bool paravirt)

arch/powerpc/lib/qspinlock.c static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav · line 391

Quoted 7 lines
arch/powerpc/lib/qspinlock.c +2 -1
@@ -380,8 +386,13 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 
 		iters++;
 
-		if (iters >= get_steal_spins(paravirt))
+		if (iters >= get_steal_spins(paravirt, false))
 			break;
+		if (iters >= get_steal_spins(paravirt, true)) {

There's no indication of what true and false mean here which is hard to read. To me it feels like two separate functions would be more clear.

arch/powerpc/lib/qspinlock.c static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav · line 393

Quoted 2 lines
arch/powerpc/lib/qspinlock.c +2 -0
+			int cpu = get_owner_cpu(val);
+			if (numa_node_id() != cpu_to_node(cpu))

What about using node_distance() instead?

arch/powerpc/lib/qspinlock.c static int steal_spins_get(void *data, u64 *val) · line 604

Quoted 12 lines
arch/powerpc/lib/qspinlock.c +5 -0
+				break;
+		}
 	}
 	spin_end();
 
@@ -588,6 +599,22 @@ static int steal_spins_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_steal_spins, steal_spins_get, steal_spins_set, "%llu\n");
 
+static int remote_steal_spins_set(void *data, u64 val)
+{
+	REMOTE_STEAL_SPINS = val;

REMOTE_STEAL_SPINS is int not u64.

Quoted 24 lines
arch/powerpc/lib/qspinlock.c +14 -0
+
+	return 0;
+}
+
+static int remote_steal_spins_get(void *data, u64 *val)
+{
+	*val = REMOTE_STEAL_SPINS;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_remote_steal_spins, remote_steal_spins_get, remote_steal_spins_set, "%llu\n");
+
 static int head_spins_set(void *data, u64 val)
 {
 	HEAD_SPINS = val;
@@ -687,6 +714,7 @@ DEFINE_SIMPLE_ATTRIBUTE(fops_pv_prod_head, pv_prod_head_get, pv_prod_head_set, "
 static __init int spinlock_debugfs_init(void)
 {
 	debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
+	debugfs_create_file("qspl_remote_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_remote_steal_spins);
 	debugfs_create_file("qspl_head_spins", 0600, arch_debugfs_dir, NULL, &fops_head_spins);
 	if (is_shared_processor()) {
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
#72 lore raw

Re: [PATCH 16/17] powerpc/qspinlock: allow indefinite spinning on a preempted owner

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 48 lines

Provide an option that holds off queueing indefinitely while the lock owner is preempted. This could reduce queueing latencies for very overcommitted vcpu situations.

This is disabled by default.

---
 arch/powerpc/lib/qspinlock.c | 91 +++++++++++++++++++++++++++++++-----
 1 file changed, 79 insertions(+), 12 deletions(-)
arch/powerpc/lib/qspinlock.c +6 -1
index 24f68bd71e2b..5cfd69931e31 100644
@@ -35,6 +35,7 @@ static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
+static bool pv_spin_on_preempted_owner __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
 static bool pv_yield_propagate_owner __read_mostly = true;
 static bool pv_prod_head __read_mostly = false;
@@ -220,13 +221,15 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
-static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq, bool *preempted)
 {
 	int owner;
 	u32 yield_count;
 
 	BUG_ON(!(val & _Q_LOCKED_VAL));
 
+	*preempted = false;
+
 	if (!paravirt)
 		goto relax;
 
@@ -241,6 +244,8 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 
 	spin_end();
 
+	*preempted = true;
+
 	/*
 	 * Read the lock word after sampling the yield count. On the other side
 	 * there may a wmb because the yield count update is done by the

arch/powerpc/lib/qspinlock.c static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 · line 273

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +1 -1
@@ -265,14 +270,14 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 	spin_cpu_relax();
 }
 
-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool *preempted)

It seems like preempted parameter could be the return value of yield_to_locked_owner(). Then callers that don't use the value returned in preempted don't need to create an unnecessary variable to pass in.

Quoted 140 lines
arch/powerpc/lib/qspinlock.c +72 -10
 {
-	__yield_to_locked_owner(lock, val, paravirt, false);
+	__yield_to_locked_owner(lock, val, paravirt, false, preempted);
 }
 
-static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq, bool *preempted)
 {
-	__yield_to_locked_owner(lock, val, paravirt, clear_mustq);
+	__yield_to_locked_owner(lock, val, paravirt, clear_mustq, preempted);
 }
 
 static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int *set_yield_cpu, bool paravirt)
@@ -364,12 +369,33 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 
 static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {
-	int iters;
+	int iters = 0;
+
+	if (!STEAL_SPINS) {
+		if (paravirt && pv_spin_on_preempted_owner) {
+			spin_begin();
+			for (;;) {
+				u32 val = READ_ONCE(lock->val);
+				bool preempted;
+
+				if (val & _Q_MUST_Q_VAL)
+					break;
+				if (!(val & _Q_LOCKED_VAL))
+					break;
+				if (!vcpu_is_preempted(get_owner_cpu(val)))
+					break;
+				yield_to_locked_owner(lock, val, paravirt, &preempted);
+			}
+			spin_end();
+		}
+		return false;
+	}
 
 	/* Attempt to steal the lock */
 	spin_begin();
 	for (;;) {
 		u32 val = READ_ONCE(lock->val);
+		bool preempted;
 
 		if (val & _Q_MUST_Q_VAL)
 			break;
@@ -382,9 +408,22 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 			continue;
 		}
 
-		yield_to_locked_owner(lock, val, paravirt);
-
-		iters++;
+		yield_to_locked_owner(lock, val, paravirt, &preempted);
+
+		if (paravirt && preempted) {
+			if (!pv_spin_on_preempted_owner)
+				iters++;
+			/*
+			 * pv_spin_on_preempted_owner don't increase iters
+			 * while the owner is preempted -- we won't interfere
+			 * with it by definition. This could introduce some
+			 * latency issue if we continually observe preempted
+			 * owners, but hopefully that's a rare corner case of
+			 * a badly oversubscribed system.
+			 */
+		} else {
+			iters++;
+		}
 
 		if (iters >= get_steal_spins(paravirt, false))
 			break;
@@ -463,8 +502,10 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		/* We're at the head of the waitqueue, wait for the lock. */
 		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			bool preempted;
+
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
-			yield_head_to_locked_owner(lock, val, paravirt, false);
+			yield_head_to_locked_owner(lock, val, paravirt, false, &preempted);
 		}
 		spin_end();
 
@@ -486,11 +527,20 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 		/* We're at the head of the waitqueue, wait for the lock. */
 		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
+			bool preempted;
+
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt,
-					pv_yield_allow_steal && set_mustq);
+					pv_yield_allow_steal && set_mustq,
+					&preempted);
+
+			if (paravirt && preempted) {
+				if (!pv_spin_on_preempted_owner)
+					iters++;
+			} else {
+				iters++;
+			}
 
-			iters++;
 			if (!set_mustq && iters >= get_head_spins(paravirt)) {
 				set_mustq = true;
 				lock_set_mustq(lock);
@@ -663,6 +713,22 @@ static int pv_yield_allow_steal_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_allow_steal, pv_yield_allow_steal_get, pv_yield_allow_steal_set, "%llu\n");
 
+static int pv_spin_on_preempted_owner_set(void *data, u64 val)
+{
+	pv_spin_on_preempted_owner = !!val;
+
+	return 0;
+}
+
+static int pv_spin_on_preempted_owner_get(void *data, u64 *val)
+{
+	*val = pv_spin_on_preempted_owner;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_spin_on_preempted_owner, pv_spin_on_preempted_owner_get, pv_spin_on_preempted_owner_set, "%llu\n");
+
 static int pv_yield_prev_set(void *data, u64 val)
 {
 	pv_yield_prev = !!val;
@@ -719,6 +785,7 @@ static __init int spinlock_debugfs_init(void)
 	if (is_shared_processor()) {
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
 		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
+		debugfs_create_file("qspl_pv_spin_on_preempted_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_spin_on_preempted_owner);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
 		debugfs_create_file("qspl_pv_yield_propagate_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);
 		debugfs_create_file("qspl_pv_prod_head", 0600, arch_debugfs_dir, NULL, &fops_pv_prod_head);
#76 lore raw

Re: [PATCH 17/17] powerpc/qspinlock: provide accounting and options for sleepy locks

In reply to Nicholas Piggin

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 48 lines

Finding the owner or a queued waiter on a lock with a preempted vcpu is indicative of an oversubscribed guest causing the lock to get into trouble. Provide some options to detect this situation and have new CPUs avoid queueing for a longer time (more steal iterations) to minimise the problems caused by vcpu preemption on the queue.

---
 arch/powerpc/include/asm/qspinlock_types.h |   7 +-
 arch/powerpc/lib/qspinlock.c               | 240 +++++++++++++++++++--
 2 files changed, 232 insertions(+), 15 deletions(-)
arch/powerpc/include/asm/qspinlock_types.h +6 -1
index 35f9525381e6..4fbcc8a4230b 100644
@@ -30,7 +30,7 @@ typedef struct qspinlock {
  *
  *     0: locked bit
  *  1-14: lock holder cpu
- *    15: unused bit
+ *    15: lock owner or queuer vcpus observed to be preempted bit
  *    16: must queue bit
  * 17-31: tail cpu (+1)
  */
@@ -49,6 +49,11 @@ typedef struct qspinlock {
 #error "qspinlock does not support such large CONFIG_NR_CPUS"
 #endif
 
+#define _Q_SLEEPY_OFFSET	15
+#define _Q_SLEEPY_BITS		1
+#define _Q_SLEEPY_MASK		_Q_SET_MASK(SLEEPY_OWNER)
+#define _Q_SLEEPY_VAL		(1U << _Q_SLEEPY_OFFSET)
+
 #define _Q_MUST_Q_OFFSET	16
 #define _Q_MUST_Q_BITS		1
 #define _Q_MUST_Q_MASK		_Q_SET_MASK(MUST_Q)
arch/powerpc/lib/qspinlock.c +1 -0
index 5cfd69931e31..c18133c01450 100644
@@ -5,6 +5,7 @@
 #include <linux/percpu.h>
 #include <linux/smp.h>
 #include <linux/topology.h>
+#include <linux/sched/clock.h>
 #include <asm/qspinlock.h>
 #include <asm/paravirt.h>
 

arch/powerpc/lib/qspinlock.c static int HEAD_SPINS __read_mostly = (1<<8); · line 41

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +2 -0
@@ -36,24 +37,54 @@ static int HEAD_SPINS __read_mostly = (1<<8);
 static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
 static bool pv_spin_on_preempted_owner __read_mostly = false;
+static bool pv_sleepy_lock __read_mostly = true;
+static bool pv_sleepy_lock_sticky __read_mostly = false;

The sticky part could potentially be its own patch.

arch/powerpc/lib/qspinlock.c static int HEAD_SPINS __read_mostly = (1<<8); · line 52

Quoted 12 lines
arch/powerpc/lib/qspinlock.c +5 -1
+static u64 pv_sleepy_lock_interval_ns __read_mostly = 0;
+static int pv_sleepy_lock_factor __read_mostly = 256;
 static bool pv_yield_prev __read_mostly = true;
 static bool pv_yield_propagate_owner __read_mostly = true;
 static bool pv_prod_head __read_mostly = false;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
+static DEFINE_PER_CPU_ALIGNED(u64, sleepy_lock_seen_clock);
 
-static __always_inline int get_steal_spins(bool paravirt, bool remote)
+static __always_inline bool recently_sleepy(void)
+{

Other users of pv_sleepy_lock_interval_ns first check pv_sleepy_lock.

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+	if (pv_sleepy_lock_interval_ns) {
+		u64 seen = this_cpu_read(sleepy_lock_seen_clock);
+
+		if (seen) {
+			u64 delta = sched_clock() - seen;

arch/powerpc/lib/qspinlock.c static int HEAD_SPINS __read_mostly = (1<<8); · line 67

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+			if (delta < pv_sleepy_lock_interval_ns)
+				return true;
+			this_cpu_write(sleepy_lock_seen_clock, 0);
+		}
+	}
+
+	return false;
+}
+
+static __always_inline int get_steal_spins(bool paravirt, bool remote, bool sleepy)

It seems like paravirt is implied by sleepy.

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +2 -1
 {
 	if (remote) {
-		return REMOTE_STEAL_SPINS;
+		if (paravirt && sleepy)
+			return REMOTE_STEAL_SPINS * pv_sleepy_lock_factor;

arch/powerpc/lib/qspinlock.c static int HEAD_SPINS __read_mostly = (1<<8); · line 80

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +6 -1
+		else
+			return REMOTE_STEAL_SPINS;
 	} else {
-		return STEAL_SPINS;
+		if (paravirt && sleepy)
+			return STEAL_SPINS * pv_sleepy_lock_factor;
+		else
+			return STEAL_SPINS;
 	}
 }

I think that separate functions would still be nicer but this could get rid of the nesting conditionals like

	int spins;
	if (remote)
		spins = REMOTE_STEAL_SPINS;
	else
		spins = STEAL_SPINS;

	if (sleepy)
		return spins * pv_sleepy_lock_factor;
	return spins;
Quoted 48 lines
arch/powerpc/lib/qspinlock.c +37 -2
-static __always_inline int get_head_spins(bool paravirt)
+static __always_inline int get_head_spins(bool paravirt, bool sleepy)
 {
-	return HEAD_SPINS;
+	if (paravirt && sleepy)
+		return HEAD_SPINS * pv_sleepy_lock_factor;
+	else
+		return HEAD_SPINS;
 }
 
 static inline u32 encode_tail_cpu(void)
@@ -206,6 +237,60 @@ static __always_inline u32 lock_clear_mustq(struct qspinlock *lock)
 	return prev;
 }
 
+static __always_inline bool lock_try_set_sleepy(struct qspinlock *lock, u32 old)
+{
+	u32 prev;
+	u32 new = old | _Q_SLEEPY_VAL;
+
+	BUG_ON(!(old & _Q_LOCKED_VAL));
+	BUG_ON(old & _Q_SLEEPY_VAL);
+
+	asm volatile(
+"1:	lwarx	%0,0,%1		# lock_try_set_sleepy			\n"
+"	cmpw	0,%0,%2							\n"
+"	bne-	2f							\n"
+"	stwcx.	%3,0,%1							\n"
+"	bne-	1b							\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r"(old), "r" (new)
+	: "cr0", "memory");
+
+	if (prev == old)
+		return true;
+	return false;
+}
+
+static __always_inline void seen_sleepy_owner(struct qspinlock *lock, u32 val)
+{
+	if (pv_sleepy_lock) {
+		if (pv_sleepy_lock_interval_ns)
+			this_cpu_write(sleepy_lock_seen_clock, sched_clock());
+		if (!(val & _Q_SLEEPY_VAL))
+			lock_try_set_sleepy(lock, val);
+	}

arch/powerpc/lib/qspinlock.c static __always_inline u32 lock_clear_mustq(struct qspinlock *lock) · line 281

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+}
+
+static __always_inline void seen_sleepy_lock(void)
+{
+	if (pv_sleepy_lock && pv_sleepy_lock_interval_ns)
+		this_cpu_write(sleepy_lock_seen_clock, sched_clock());
+}
+
+static __always_inline void seen_sleepy_node(struct qspinlock *lock)
+{

If yield_to_prev() was made to take a raw val, that val could be passed to seen_sleepy_node() and it would not need to get it by itself.

Quoted 85 lines
arch/powerpc/lib/qspinlock.c +25 -3
+	if (pv_sleepy_lock) {
+		u32 val = READ_ONCE(lock->val);
+
+		if (pv_sleepy_lock_interval_ns)
+			this_cpu_write(sleepy_lock_seen_clock, sched_clock());
+		if (val & _Q_LOCKED_VAL) {
+			if (!(val & _Q_SLEEPY_VAL))
+				lock_try_set_sleepy(lock, val);
+		}
+	}
+}
+
 static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 {
 	int cpu = get_tail_cpu(val);
@@ -244,6 +329,7 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 
 	spin_end();
 
+	seen_sleepy_owner(lock, val);
 	*preempted = true;
 
 	/*
@@ -307,11 +393,13 @@ static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int
 	}
 }
 
-static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
+static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt, bool *preempted)
 {
 	u32 yield_count;
 	int yield_cpu;
 
+	*preempted = false;
+
 	if (!paravirt)
 		goto relax;
 
@@ -332,6 +420,9 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 
 	spin_end();
 
+	*preempted = true;
+	seen_sleepy_node(lock);
+
 	smp_rmb();
 
 	if (yield_cpu == node->yield_cpu) {
@@ -353,6 +444,9 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 
 	spin_end();
 
+	*preempted = true;
+	seen_sleepy_node(lock);
+
 	smp_rmb(); /* See yield_to_locked_owner comment */
 
 	if (!node->locked) {
@@ -369,6 +463,9 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 
 static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {
+	bool preempted;
+	bool seen_preempted = false;
+	bool sleepy = false;
 	int iters = 0;
 
 	if (!STEAL_SPINS) {
@@ -376,7 +473,6 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 			spin_begin();
 			for (;;) {
 				u32 val = READ_ONCE(lock->val);
-				bool preempted;
 
 				if (val & _Q_MUST_Q_VAL)
 					break;
@@ -395,7 +491,6 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 	spin_begin();
 	for (;;) {
 		u32 val = READ_ONCE(lock->val);
-		bool preempted;
 
 		if (val & _Q_MUST_Q_VAL)
 			break;

arch/powerpc/lib/qspinlock.c static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav · line 507

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +2 -0
@@ -408,9 +503,29 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 			continue;
 		}
 
+		if (paravirt && pv_sleepy_lock && !sleepy) {
+			if (!sleepy) {

The enclosing conditional means this would always be true. I think the out conditional should be if (paravirt && pv_sleepy_lock)
otherwise the pv_sleepy_lock_sticky part wouldn't work properly.

Quoted 7 lines
arch/powerpc/lib/qspinlock.c +7 -0
+				if (val & _Q_SLEEPY_VAL) {
+					seen_sleepy_lock();
+					sleepy = true;
+				} else if (recently_sleepy()) {
+					sleepy = true;
+				}
+

arch/powerpc/lib/qspinlock.c static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav · line 524

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +9 -0
+			if (pv_sleepy_lock_sticky && seen_preempted &&
+					!(val & _Q_SLEEPY_VAL)) {
+				if (lock_try_set_sleepy(lock, val))
+					val |= _Q_SLEEPY_VAL;
+			}
+
+
 		yield_to_locked_owner(lock, val, paravirt, &preempted);
+		if (preempted)
+			seen_preempted = true;

This could belong to the next if statement, there can not be !paravirt && preempted ?

Quoted 190 lines
arch/powerpc/lib/qspinlock.c +105 -6
 		if (paravirt && preempted) {
+			sleepy = true;
+
 			if (!pv_spin_on_preempted_owner)
 				iters++;
 			/*
@@ -425,14 +540,15 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 			iters++;
 		}
 
-		if (iters >= get_steal_spins(paravirt, false))
+		if (iters >= get_steal_spins(paravirt, false, sleepy))
 			break;
-		if (iters >= get_steal_spins(paravirt, true)) {
+		if (iters >= get_steal_spins(paravirt, true, sleepy)) {
 			int cpu = get_owner_cpu(val);
 			if (numa_node_id() != cpu_to_node(cpu))
 				break;
 		}
 	}
+
 	spin_end();
 
 	return false;
@@ -443,6 +559,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	struct qnodes *qnodesp;
 	struct qnode *next, *node;
 	u32 val, old, tail;
+	bool seen_preempted = false;
 	int idx;
 
 	BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
@@ -485,8 +602,13 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 
 		/* Wait for mcs node lock to be released */
 		spin_begin();
-		while (!node->locked)
-			yield_to_prev(lock, node, prev_cpu, paravirt);
+		while (!node->locked) {
+			bool preempted;
+
+			yield_to_prev(lock, node, prev_cpu, paravirt, &preempted);
+			if (preempted)
+				seen_preempted = true;
+		}
 		spin_end();
 
 		/* Clear out stale propagated yield_cpu */
@@ -506,6 +628,8 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt, false, &preempted);
+			if (preempted)
+				seen_preempted = true;
 		}
 		spin_end();
 
@@ -521,27 +645,47 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	} else {
 		int set_yield_cpu = -1;
 		int iters = 0;
+		bool sleepy = false;
 		bool set_mustq = false;
+		bool preempted;
 
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
 		spin_begin();
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
-			bool preempted;
+			if (paravirt && pv_sleepy_lock) {
+				if (!sleepy) {
+					if (val & _Q_SLEEPY_VAL) {
+						seen_sleepy_lock();
+						sleepy = true;
+					} else if (recently_sleepy()) {
+						sleepy = true;
+					}
+				}
+				if (pv_sleepy_lock_sticky && seen_preempted &&
+						!(val & _Q_SLEEPY_VAL)) {
+					if (lock_try_set_sleepy(lock, val))
+						val |= _Q_SLEEPY_VAL;
+				}
+			}
 
 			propagate_yield_cpu(node, val, &set_yield_cpu, paravirt);
 			yield_head_to_locked_owner(lock, val, paravirt,
 					pv_yield_allow_steal && set_mustq,
 					&preempted);
+			if (preempted)
+				seen_preempted = true;
 
 			if (paravirt && preempted) {
+				sleepy = true;
+
 				if (!pv_spin_on_preempted_owner)
 					iters++;
 			} else {
 				iters++;
 			}
 
-			if (!set_mustq && iters >= get_head_spins(paravirt)) {
+			if (!set_mustq && iters >= get_head_spins(paravirt, sleepy)) {
 				set_mustq = true;
 				lock_set_mustq(lock);
 				val |= _Q_MUST_Q_VAL;
@@ -729,6 +873,70 @@ static int pv_spin_on_preempted_owner_get(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(fops_pv_spin_on_preempted_owner, pv_spin_on_preempted_owner_get, pv_spin_on_preempted_owner_set, "%llu\n");
 
+static int pv_sleepy_lock_set(void *data, u64 val)
+{
+	pv_sleepy_lock = !!val;
+
+	return 0;
+}
+
+static int pv_sleepy_lock_get(void *data, u64 *val)
+{
+	*val = pv_sleepy_lock;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock, pv_sleepy_lock_get, pv_sleepy_lock_set, "%llu\n");
+
+static int pv_sleepy_lock_sticky_set(void *data, u64 val)
+{
+	pv_sleepy_lock_sticky = !!val;
+
+	return 0;
+}
+
+static int pv_sleepy_lock_sticky_get(void *data, u64 *val)
+{
+	*val = pv_sleepy_lock_sticky;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock_sticky, pv_sleepy_lock_sticky_get, pv_sleepy_lock_sticky_set, "%llu\n");
+
+static int pv_sleepy_lock_interval_ns_set(void *data, u64 val)
+{
+	pv_sleepy_lock_interval_ns = val;
+
+	return 0;
+}
+
+static int pv_sleepy_lock_interval_ns_get(void *data, u64 *val)
+{
+	*val = pv_sleepy_lock_interval_ns;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock_interval_ns, pv_sleepy_lock_interval_ns_get, pv_sleepy_lock_interval_ns_set, "%llu\n");
+
+static int pv_sleepy_lock_factor_set(void *data, u64 val)
+{
+	pv_sleepy_lock_factor = val;
+
+	return 0;
+}
+
+static int pv_sleepy_lock_factor_get(void *data, u64 *val)
+{
+	*val = pv_sleepy_lock_factor;
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock_factor, pv_sleepy_lock_factor_get, pv_sleepy_lock_factor_set, "%llu\n");
+
 static int pv_yield_prev_set(void *data, u64 val)
 {
 	pv_yield_prev = !!val;
@@ -786,6 +994,10 @@ static __init int spinlock_debugfs_init(void)
 		debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
 		debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
 		debugfs_create_file("qspl_pv_spin_on_preempted_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_spin_on_preempted_owner);
+		debugfs_create_file("qspl_pv_sleepy_lock", 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock);
+		debugfs_create_file("qspl_pv_sleepy_lock_sticky", 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock_sticky);
+		debugfs_create_file("qspl_pv_sleepy_lock_interval_ns", 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock_interval_ns);
+		debugfs_create_file("qspl_pv_sleepy_lock_factor", 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock_factor);
 		debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
 		debugfs_create_file("qspl_pv_yield_propagate_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_propagate_owner);
 		debugfs_create_file("qspl_pv_prod_head", 0600, arch_debugfs_dir, NULL, &fops_pv_prod_head);
#5 lore raw

Re: [PATCH 01/17] powerpc/qspinlock: powerpc qspinlock implementation

In reply to Jordan Niethe

Le 10/11/2022 à 01:35, Jordan Niethe a écrit :

Quoted 16 lines · Nicholas Piggin

<snip>

arch/powerpc/include/asm/qspinlock.h line 28

Quoted 12 lines
arch/powerpc/include/asm/qspinlock.h +4 -6
-#define queued_spin_lock queued_spin_lock
 
-static inline void queued_spin_unlock(struct qspinlock *lock)
+static __always_inline int queued_spin_trylock(struct qspinlock *lock)
  {
-	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) || !is_shared_processor())
-		smp_store_release(&lock->locked, 0);
-	else
-		__pv_queued_spin_unlock(lock);
+	if (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0)
+		return 1;
+	return 0;

optional style nit: return (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0);

No parenthesis.
No == 0

Should be :

	return !atomic_cmpxchg_acquire(&lock->val, 0, 1);
Quoted 3 lines

[resend as utf-8, not utf-7]

#20 lore raw

Re: [PATCH 04/17] powerpc/qspinlock: convert atomic operations to assembly

In reply to Jordan Niethe

Le 10/11/2022 à 01:39, Jordan Niethe a écrit :

Quoted 65 lines · Nicholas Piggin

[resend as utf-8, not utf-7]

Quoted 46 lines

This uses more optimal ll/sc style access patterns (rather than cmpxchg), and also sets the EH=1 lock hint on those operations which acquire ownership of the lock.

---
  arch/powerpc/include/asm/qspinlock.h       | 25 +++++--
  arch/powerpc/include/asm/qspinlock_types.h |  6 +-
  arch/powerpc/lib/qspinlock.c               | 81 +++++++++++++++-------
  3 files changed, 79 insertions(+), 33 deletions(-)
arch/powerpc/include/asm/qspinlock.h +7 -5
index 79a1936fb68d..3ab354159e5e 100644
@@ -2,28 +2,43 @@
  #ifndef _ASM_POWERPC_QSPINLOCK_H
  #define _ASM_POWERPC_QSPINLOCK_H
 
-#include <linux/atomic.h>
  #include <linux/compiler.h>
  #include <asm/qspinlock_types.h>
 
  static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
  {
-	return atomic_read(&lock->val);
+	return READ_ONCE(lock->val);
  }
 
  static __always_inline int queued_spin_value_unlocked(struct qspinlock lock)
  {
-	return !atomic_read(&lock.val);
+	return !lock.val;
  }
 
  static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
  {
-	return !!(atomic_read(&lock->val) & _Q_TAIL_CPU_MASK);
+	return !!(READ_ONCE(lock->val) & _Q_TAIL_CPU_MASK);
  }
 
  static __always_inline int queued_spin_trylock(struct qspinlock *lock)
  {
-	if (atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL) == 0)
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	asm volatile(

arch/powerpc/include/asm/qspinlock.h line 38

Quoted 10 lines
arch/powerpc/include/asm/qspinlock.h +10 -0
+"1:	lwarx	%0,0,%1,%3	# queued_spin_trylock			\n"
+"	cmpwi	0,%0,0							\n"
+"	bne-	2f							\n"
+"	stwcx.	%2,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)

btw IS_ENABLED() already returns 1 or 0

arch/powerpc/include/asm/qspinlock.h line 39

Quoted 1 line
arch/powerpc/include/asm/qspinlock.h +1 -0
+	: "cr0", "memory");

This is the ISA's "test and set" atomic primitive. Do you think it would be worth seperating it as a helper?

Quoted 6 lines

arch/powerpc/include/asm/qspinlock.h line 43

Quoted 4 lines
arch/powerpc/include/asm/qspinlock.h +2 -0
+
+	if (likely(prev == 0))
  		return 1;
  	return 0;

same optional style nit: return likely(prev == 0);

	return likely(!prev);
Quoted 85 lines
Quoted 45 lines
arch/powerpc/include/asm/qspinlock.h +0 -0
  }
arch/powerpc/include/asm/qspinlock_types.h +3 -3
index 3425dab42576..210adf05b235 100644
@@ -7,7 +7,7 @@
 
  typedef struct qspinlock {
  	union {
-		atomic_t val;
+		u32 val;
 
  #ifdef __LITTLE_ENDIAN
  		struct {
@@ -23,10 +23,10 @@ typedef struct qspinlock {
  	};
  } arch_spinlock_t;
 
-#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { .val = ATOMIC_INIT(0) } }
+#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { .val = 0 } }
 
  /*
- * Bitfields in the atomic value:
+ * Bitfields in the lock word:
   *
   *     0: locked bit
   * 16-31: tail cpu (+1)
arch/powerpc/lib/qspinlock.c +1 -2
index 5ebb88d95636..7c71e5e287df 100644
@@ -1,5 +1,4 @@
  // SPDX-License-Identifier: GPL-2.0-or-later
-#include <linux/atomic.h>
  #include <linux/bug.h>
  #include <linux/compiler.h>
  #include <linux/export.h>
@@ -22,32 +21,59 @@ struct qnodes {
 
  static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
-static inline int encode_tail_cpu(void)
+static inline u32 encode_tail_cpu(void)
  {

arch/powerpc/lib/qspinlock.c struct qnodes { · line 34

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +1 -1
  	return (smp_processor_id() + 1) << _Q_TAIL_CPU_OFFSET;
  }
 
-static inline int get_tail_cpu(int val)
+static inline int get_tail_cpu(u32 val)
  {
  	return (val >> _Q_TAIL_CPU_OFFSET) - 1;
  }
 
  /* Take the lock by setting the bit, no other CPUs may concurrently lock it. */

I think you missed deleting the above line.

Quoted 9 lines
arch/powerpc/lib/qspinlock.c +5 -2
+/* Take the lock by setting the lock bit, no other CPUs will touch it. */
  static __always_inline void lock_set_locked(struct qspinlock *lock)
  {
-	atomic_or(_Q_LOCKED_VAL, &lock->val);
-	__atomic_acquire_fence();
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	asm volatile(

arch/powerpc/lib/qspinlock.c struct qnodes { · line 51

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +9 -0
+"1:	lwarx	%0,0,%1,%3	# lock_set_locked			\n"
+"	or	%0,%0,%2						\n"
+"	stwcx.	%0,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");
  }

This is pretty similar with the DEFINE_TESTOP() pattern from arch/powerpc/include/asm/bitops.h (such as test_and_set_bits_lock()) except for word instead of double word. Do you think it's possible / beneficial to make use of those macros?

Quoted 1 line
Quoted 14 lines

arch/powerpc/lib/qspinlock.c struct qnodes { · line 59

Quoted 12 lines
arch/powerpc/lib/qspinlock.c +6 -5
-/* Take lock, clearing tail, cmpxchg with val (which must not be locked) */
-static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int val)
+/* Take lock, clearing tail, cmpxchg with old (which must not be locked) */
+static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 old)
  {
-	int newval = _Q_LOCKED_VAL;
-
-	if (atomic_cmpxchg_acquire(&lock->val, val, newval) == val)
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	BUG_ON(old & _Q_LOCKED_VAL);

The BUG_ON() could have been introduced in an earlier patch I think.

Can we avoid the BUG_ON() at all and replace by a WARN_ON ?

Quoted 87 lines

arch/powerpc/lib/qspinlock.c struct qnodes { · line 70

Quoted 12 lines
arch/powerpc/lib/qspinlock.c +11 -0
+
+	asm volatile(
+"1:	lwarx	%0,0,%1,%4	# trylock_clear_tail_cpu		\n"
+"	cmpw	0,%0,%2							\n"
+"	bne-	2f							\n"
+"	stwcx.	%3,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r"(old), "r" (new),

Could this be like "r"(_Q_TAIL_CPU_MASK) below? i.e. "r" (_Q_LOCKED_VAL)? Makes it clear new doesn't change.

arch/powerpc/lib/qspinlock.c static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int va · line 82

Quoted 13 lines
arch/powerpc/lib/qspinlock.c +5 -2
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");
+
+	if (likely(prev == old))
  		return 1;
-	else
-		return 0;
+	return 0;
  }
 
  /*
@@ -56,20 +82,25 @@ static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int va
   * This provides a release barrier for publishing node, and an acquire barrier

Does the comment mean there needs to be an acquire barrier in this assembly?

Quoted 53 lines
arch/powerpc/lib/qspinlock.c +18 -13
   * for getting the old node.
   */
-static __always_inline int publish_tail_cpu(struct qspinlock *lock, int tail)
+static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail)
  {
-	for (;;) {
-		int val = atomic_read(&lock->val);
-		int newval = (val & ~_Q_TAIL_CPU_MASK) | tail;
-		int old;
-
-		old = atomic_cmpxchg(&lock->val, val, newval);
-		if (old == val)
-			return old;
-	}
+	u32 prev, tmp;
+
+	asm volatile(
+"\t"	PPC_RELEASE_BARRIER "						\n"
+"1:	lwarx	%0,0,%2		# publish_tail_cpu			\n"
+"	andc	%1,%0,%4						\n"
+"	or	%1,%1,%3						\n"
+"	stwcx.	%1,0,%2							\n"
+"	bne-	1b							\n"
+	: "=&r" (prev), "=&r"(tmp)
+	: "r" (&lock->val), "r" (tail), "r"(_Q_TAIL_CPU_MASK)
+	: "cr0", "memory");
+
+	return prev;
  }
 
-static struct qnode *get_tail_qnode(struct qspinlock *lock, int val)
+static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
  {
  	int cpu = get_tail_cpu(val);
  	struct qnodes *qnodesp = per_cpu_ptr(&qnodes, cpu);
@@ -88,7 +119,7 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
  {
  	struct qnodes *qnodesp;
  	struct qnode *next, *node;
-	int val, old, tail;
+	u32 val, old, tail;
  	int idx;
 
  	BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
@@ -134,7 +165,7 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
  	}
 
  	/* We're at the head of the waitqueue, wait for the lock. */
-	while ((val = atomic_read(&lock->val)) & _Q_LOCKED_VAL)
+	while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
  		cpu_relax();
 
  	/* If we're the last queued, must clean up the tail. */
author reviewer
#7 lore raw

Re: [PATCH 01/17] powerpc/qspinlock: powerpc qspinlock implementation

In reply to Jordan Niethe
Quoted 17 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
<snip>

arch/powerpc/include/asm/qspinlock.h line 28

Quoted 12 lines
arch/powerpc/include/asm/qspinlock.h +4 -6
-#define queued_spin_lock queued_spin_lock
 
-static inline void queued_spin_unlock(struct qspinlock *lock)
+static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 {
-	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) || !is_shared_processor())
-		smp_store_release(&lock->locked, 0);
-	else
-		__pv_queued_spin_unlock(lock);
+	if (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0)
+		return 1;
+	return 0;

optional style nit: return (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0);

Quoted 1 line

[resend as utf-8, not utf-7]

Thanks for the thorough review, apologies again it took me so long to get back to.

I'm not completely sold on this. I guess it's already side-effects in a control flow statement though... Maybe I will change it, not sure.

Thanks,
Nick

author reviewer
#12 lore raw

Re: [PATCH 02/17] powerpc/qspinlock: add mcs queueing for contended waiters

In reply to Jordan Niethe
Quoted 24 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
<snip>
[resend as utf-8, not utf-7]

Quoted 8 lines
arch/powerpc/include/asm/qspinlock_types.h +7 -0
+/*
+ * Bitfields in the atomic value:
+ *
+ *     0: locked bit
+ * 16-31: tail cpu (+1)
+ */
+#define	_Q_SET_MASK(type)	(((1U << _Q_ ## type ## _BITS) - 1)\

arch/powerpc/include/asm/qspinlock_types.h typedef struct qspinlock { · line 29

Quoted 10 lines
arch/powerpc/include/asm/qspinlock_types.h +10 -0
+				      << _Q_ ## type ## _OFFSET)
+#define _Q_LOCKED_OFFSET	0
+#define _Q_LOCKED_BITS		1
+#define _Q_LOCKED_MASK		_Q_SET_MASK(LOCKED)
+#define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
+
+#define _Q_TAIL_CPU_OFFSET	16
+#define _Q_TAIL_CPU_BITS	(32 - _Q_TAIL_CPU_OFFSET)
+#define _Q_TAIL_CPU_MASK	_Q_SET_MASK(TAIL_CPU)
+

Just to state the obvious this is:

Quoted 8 lines

#define _Q_LOCKED_OFFSET 0
#define _Q_LOCKED_BITS 1
#define _Q_LOCKED_MASK 0x00000001
#define _Q_LOCKED_VAL 1

#define _Q_TAIL_CPU_OFFSET 16
#define _Q_TAIL_CPU_BITS 16
#define _Q_TAIL_CPU_MASK 0xffff0000

Yeah. I'm wondering if that's a better style in the first place. Generic qspinlock this can chance so there's slightly more reason to do it that way.

Quoted 20 lines
Quoted 20 lines
arch/powerpc/include/asm/qspinlock_types.h +4 -0
+#if CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS)
+#error "qspinlock does not support such large CONFIG_NR_CPUS"
+#endif
+
 #endif /* _ASM_POWERPC_QSPINLOCK_TYPES_H */
arch/powerpc/lib/qspinlock.c +5 -1
index 8dbce99a373c..5ebb88d95636 100644
@@ -1,12 +1,172 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
+#include <linux/atomic.h>
+#include <linux/bug.h>
+#include <linux/compiler.h>
 #include <linux/export.h>
-#include <linux/processor.h>
+#include <linux/percpu.h>
+#include <linux/smp.h>
 #include <asm/qspinlock.h>
 
Quoted 15 lines

arch/powerpc/lib/qspinlock.c line 21

Quoted 13 lines
arch/powerpc/lib/qspinlock.c +12 -1
-void queued_spin_lock_slowpath(struct qspinlock *lock)
+#define MAX_NODES	4
+
+struct qnode {
+	struct qnode	*next;
+	struct qspinlock *lock;
+	u8		locked; /* 1 if lock acquired */
+};
+
+struct qnodes {
+	int		count;
+	struct qnode nodes[MAX_NODES];
+};

I think it could be worth commenting why qnodes::count instead _Q_TAIL_IDX_OFFSET.

I wasn't sure what you meant by this.

Quoted 6 lines

arch/powerpc/lib/qspinlock.c line 25

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +4 -0
+
+static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
+
+static inline int encode_tail_cpu(void)

I think the generic version that takes smp_processor_id() as a parameter is clearer - at least with this function name.

Agree.

Quoted 7 lines

arch/powerpc/lib/qspinlock.c line 30

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+{
+	return (smp_processor_id() + 1) << _Q_TAIL_CPU_OFFSET;
+}
+
+static inline int get_tail_cpu(int val)

It seems like there should be a "decode" function to pair up with the "encode" function.

Agree.

Quoted 7 lines

arch/powerpc/lib/qspinlock.c line 35

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+{
+	return (val >> _Q_TAIL_CPU_OFFSET) - 1;
+}
+
+/* Take the lock by setting the bit, no other CPUs may concurrently lock it. */

Does that comment mean it is not necessary to use an atomic_or here?

No, only that it can't be locked. It can still be modified by another queuer.

Quoted 3 lines

arch/powerpc/lib/qspinlock.c line 36

Quoted 1 line
arch/powerpc/lib/qspinlock.c +1 -0
+static __always_inline void lock_set_locked(struct qspinlock *lock)

nit: could just be called set_locked()

Yep.

Quoted 8 lines
Quoted 8 lines
arch/powerpc/lib/qspinlock.c +8 -0
+{
+	atomic_or(_Q_LOCKED_VAL, &lock->val);
+	__atomic_acquire_fence();
+}
+
+/* Take lock, clearing tail, cmpxchg with val (which must not be locked) */
+static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int val)
+{
Quoted 8 lines

arch/powerpc/lib/qspinlock.c line 50

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +6 -0
+	int newval = _Q_LOCKED_VAL;
+
+	if (atomic_cmpxchg_acquire(&lock->val, val, newval) == val)
+		return 1;
+	else
+		return 0;

same optional style nit: return (atomic_cmpxchg_acquire(&lock->val, val, newval) == val);

Am thinking about it :)

Quoted 4 lines
Quoted 4 lines
arch/powerpc/lib/qspinlock.c +4 -0
+}
+
+/*
+ * Publish our tail, replacing previous tail. Return previous value.
Quoted 8 lines

arch/powerpc/lib/qspinlock.c line 59

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+ *
+ * This provides a release barrier for publishing node, and an acquire barrier
+ * for getting the old node.
+ */
+static __always_inline int publish_tail_cpu(struct qspinlock *lock, int tail)

Did you change from the xchg_tail() name in the generic version because of the release and acquire barriers this provides? Does "publish" generally imply the old value will be returned?

Yes publish I thought is a bit more obvious that's where it becomes visible to other CPUs. It doesn't imply return, but I thought those semantis are the self-documenting part.

Quoted 37 lines
Quoted 15 lines
arch/powerpc/lib/qspinlock.c +12 -1
 {
-	while (!queued_spin_trylock(lock))
+	for (;;) {
+		int val = atomic_read(&lock->val);
+		int newval = (val & ~_Q_TAIL_CPU_MASK) | tail;
+		int old;
+
+		old = atomic_cmpxchg(&lock->val, val, newval);
+		if (old == val)
+			return old;
+	}
+}
+
+static struct qnode *get_tail_qnode(struct qspinlock *lock, int val)

arch/powerpc/lib/qspinlock.c line 82

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+{
+	int cpu = get_tail_cpu(val);
+	struct qnodes *qnodesp = per_cpu_ptr(&qnodes, cpu);
+	int idx;
+
+	for (idx = 0; idx < MAX_NODES; idx++) {
+		struct qnode *qnode = &qnodesp->nodes[idx];
+		if (qnode->lock == lock)
+			return qnode;
+	}

In case anyone else is confused by this, Nick explained each cpu can only queue on a unique spinlock once regardless of "idx" level.

Quoted 9 lines
arch/powerpc/lib/qspinlock.c +9 -0
+
+	BUG();
+}
+
+static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
+{
+	struct qnodes *qnodesp;
+	struct qnode *next, *node;
+	int val, old, tail;
Quoted 8 lines

arch/powerpc/lib/qspinlock.c line 97

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +6 -0
+	int idx;
+
+	BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
+
+	qnodesp = this_cpu_ptr(&qnodes);
+	if (unlikely(qnodesp->count == MAX_NODES)) {

The comparison is >= in the generic, I guess we've no nested NMI so this is safe?

No... we could have nested NMI so this is wrong, good catch.

Thanks,
Nick

author reviewer
#16 lore raw

Re: [PATCH 03/17] powerpc/qspinlock: use a half-word store to unlock to avoid larx/stcx.

In reply to Jordan Niethe
Quoted 17 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 15 lines

The first 16 bits of the lock are only modified by the owner, and other modifications always use atomic operations on the entire 32 bits, so unlocks can use plain stores on the 16 bits. This is the same kind of optimisation done by core qspinlock code.

---
 arch/powerpc/include/asm/qspinlock.h       |  6 +-----
 arch/powerpc/include/asm/qspinlock_types.h | 19 +++++++++++++++++--
 2 files changed, 18 insertions(+), 7 deletions(-)
arch/powerpc/include/asm/qspinlock.h +0 -0
index f06117aa60e1..79a1936fb68d 100644
@@ -38,11 +38,7 @@ static __always_inline void queued_spin_lock(struct qspinlock *lock)
 
Quoted 11 lines

arch/powerpc/include/asm/qspinlock.h static __always_inline void queued_spin_lock(struct qspinlock *lock) · line 41

Quoted 8 lines
arch/powerpc/include/asm/qspinlock.h +1 -5
 static inline void queued_spin_unlock(struct qspinlock *lock)
 {
-	for (;;) {
-		int val = atomic_read(&lock->val);
-		if (atomic_cmpxchg_release(&lock->val, val, val & ~_Q_LOCKED_VAL) == val)
-			return;
-	}
+	smp_store_release(&lock->locked, 0);

Is it also possible for lock_set_locked() to use a non-atomic acquire operation?

It has to be atomic as mentioned earlier.

Thanks,
Nick

author reviewer
#22 lore raw

Re: [PATCH 04/17] powerpc/qspinlock: convert atomic operations to assembly

In reply to Jordan Niethe
Quoted 52 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 50 lines

This uses more optimal ll/sc style access patterns (rather than cmpxchg), and also sets the EH=1 lock hint on those operations which acquire ownership of the lock.

---
 arch/powerpc/include/asm/qspinlock.h       | 25 +++++--
 arch/powerpc/include/asm/qspinlock_types.h |  6 +-
 arch/powerpc/lib/qspinlock.c               | 81 +++++++++++++++-------
 3 files changed, 79 insertions(+), 33 deletions(-)
arch/powerpc/include/asm/qspinlock.h +11 -5
index 79a1936fb68d..3ab354159e5e 100644
@@ -2,28 +2,43 @@
 #ifndef _ASM_POWERPC_QSPINLOCK_H
 #define _ASM_POWERPC_QSPINLOCK_H
 
-#include <linux/atomic.h>
 #include <linux/compiler.h>
 #include <asm/qspinlock_types.h>
 
 static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
 {
-	return atomic_read(&lock->val);
+	return READ_ONCE(lock->val);
 }
 
 static __always_inline int queued_spin_value_unlocked(struct qspinlock lock)
 {
-	return !atomic_read(&lock.val);
+	return !lock.val;
 }
 
 static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
 {
-	return !!(atomic_read(&lock->val) & _Q_TAIL_CPU_MASK);
+	return !!(READ_ONCE(lock->val) & _Q_TAIL_CPU_MASK);
 }
 
 static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 {
-	if (atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL) == 0)
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	asm volatile(
+"1:	lwarx	%0,0,%1,%3	# queued_spin_trylock			\n"
+"	cmpwi	0,%0,0							\n"
+"	bne-	2f							\n"
+"	stwcx.	%2,0,%1							\n"
Quoted 8 lines

arch/powerpc/include/asm/qspinlock.h line 38

Quoted 6 lines
arch/powerpc/include/asm/qspinlock.h +6 -0
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)

btw IS_ENABLED() already returns 1 or 0

I guess we already do that in atomic.h too. Okay.

Quoted 3 lines

arch/powerpc/include/asm/qspinlock.h line 39

Quoted 1 line
arch/powerpc/include/asm/qspinlock.h +1 -0
+	: "cr0", "memory");

This is the ISA's "test and set" atomic primitive. Do you think it would be worth seperating it as a helper?

It ends up getting more complex as we go. I might leave some of these primitives open coded for now, we could possibly look at providing them or reusing more generic primitives after the series though.

Quoted 6 lines

arch/powerpc/include/asm/qspinlock.h line 43

Quoted 4 lines
arch/powerpc/include/asm/qspinlock.h +2 -0
+
+	if (likely(prev == 0))
 		return 1;
 	return 0;

same optional style nit: return likely(prev == 0);

Will do.

Quoted 74 lines
Quoted 45 lines
arch/powerpc/include/asm/qspinlock.h +0 -0
 }
arch/powerpc/include/asm/qspinlock_types.h +3 -3
index 3425dab42576..210adf05b235 100644
@@ -7,7 +7,7 @@
 
 typedef struct qspinlock {
 	union {
-		atomic_t val;
+		u32 val;
 
 #ifdef __LITTLE_ENDIAN
 		struct {
@@ -23,10 +23,10 @@ typedef struct qspinlock {
 	};
 } arch_spinlock_t;
 
-#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { .val = ATOMIC_INIT(0) } }
+#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { .val = 0 } }
 
 /*
- * Bitfields in the atomic value:
+ * Bitfields in the lock word:
  *
  *     0: locked bit
  * 16-31: tail cpu (+1)
arch/powerpc/lib/qspinlock.c +1 -2
index 5ebb88d95636..7c71e5e287df 100644
@@ -1,5 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
-#include <linux/atomic.h>
 #include <linux/bug.h>
 #include <linux/compiler.h>
 #include <linux/export.h>
@@ -22,32 +21,59 @@ struct qnodes {
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
-static inline int encode_tail_cpu(void)
+static inline u32 encode_tail_cpu(void)
 {

arch/powerpc/lib/qspinlock.c struct qnodes { · line 34

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +1 -1
 	return (smp_processor_id() + 1) << _Q_TAIL_CPU_OFFSET;
 }
 
-static inline int get_tail_cpu(int val)
+static inline int get_tail_cpu(u32 val)
 {
 	return (val >> _Q_TAIL_CPU_OFFSET) - 1;
 }
 
 /* Take the lock by setting the bit, no other CPUs may concurrently lock it. */

I think you missed deleting the above line.

Quoted 16 lines
arch/powerpc/lib/qspinlock.c +12 -2
+/* Take the lock by setting the lock bit, no other CPUs will touch it. */
 static __always_inline void lock_set_locked(struct qspinlock *lock)
 {
-	atomic_or(_Q_LOCKED_VAL, &lock->val);
-	__atomic_acquire_fence();
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	asm volatile(
+"1:	lwarx	%0,0,%1,%3	# lock_set_locked			\n"
+"	or	%0,%0,%2						\n"
+"	stwcx.	%0,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new),
Quoted 8 lines

arch/powerpc/lib/qspinlock.c struct qnodes { · line 51

Quoted 3 lines
arch/powerpc/lib/qspinlock.c +2 -0
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");
 }

This is pretty similar with the DEFINE_TESTOP() pattern from arch/powerpc/include/asm/bitops.h (such as test_and_set_bits_lock()) except for word instead of double word. Do you think it's possible / beneficial to make use of those macros?

If we could pull almost all our atomic primitives into one place and make them usable by atomics, bitops, locks, etc. might be a good idea.

That function specifically works on a dword so we can't use it here, and I don't want to modify any files except for the new ones in this series if possible, but consolidating our primitives a bit more would be nice.

Quoted 6 lines
Quoted 6 lines
arch/powerpc/lib/qspinlock.c +2 -3
-/* Take lock, clearing tail, cmpxchg with val (which must not be locked) */
-static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int val)
+/* Take lock, clearing tail, cmpxchg with old (which must not be locked) */
+static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 old)
 {
-	int newval = _Q_LOCKED_VAL;
Quoted 8 lines

arch/powerpc/lib/qspinlock.c struct qnodes { · line 59

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +4 -2
-
-	if (atomic_cmpxchg_acquire(&lock->val, val, newval) == val)
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	BUG_ON(old & _Q_LOCKED_VAL);

The BUG_ON() could have been introduced in an earlier patch I think.

Yes.

Quoted 6 lines
Quoted 6 lines
arch/powerpc/lib/qspinlock.c +6 -0
+
+	asm volatile(
+"1:	lwarx	%0,0,%1,%4	# trylock_clear_tail_cpu		\n"
+"	cmpw	0,%0,%2							\n"
+"	bne-	2f							\n"
+"	stwcx.	%3,0,%1							\n"
Quoted 8 lines

arch/powerpc/lib/qspinlock.c struct qnodes { · line 70

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r"(old), "r" (new),

Could this be like "r"(_Q_TAIL_CPU_MASK) below? i.e. "r" (_Q_LOCKED_VAL)? Makes it clear new doesn't change.

Sure.

Quoted 11 lines
Quoted 11 lines
arch/powerpc/lib/qspinlock.c +5 -2
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");
+
+	if (likely(prev == old))
 		return 1;
-	else
-		return 0;
+	return 0;
 }
 
Quoted 5 lines

arch/powerpc/lib/qspinlock.c static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int va · line 82

Quoted 3 lines
arch/powerpc/lib/qspinlock.c +0 -0
 /*
@@ -56,20 +82,25 @@ static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, int va
  * This provides a release barrier for publishing node, and an acquire barrier

Does the comment mean there needs to be an acquire barrier in this assembly?

Yes, another good catch. What I'm going to do instead is add the acquire to get_tail_qnode() because that path is only hit when you have multiple waiters, and I think pairing it that way makes the barriers more obvious.

Thanks,
Nick

author reviewer
#26 lore raw

Re: [PATCH 05/17] powerpc/qspinlock: allow new waiters to steal the lock before queueing

In reply to Jordan Niethe
Quoted 17 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 15 lines

Allow new waiters a number of spins on the lock word before queueing, which particularly helps paravirt performance when physical CPUs are oversubscribed.

---
 arch/powerpc/lib/qspinlock.c | 152 ++++++++++++++++++++++++++++++++---
 1 file changed, 141 insertions(+), 11 deletions(-)
arch/powerpc/lib/qspinlock.c +0 -0
index 7c71e5e287df..1625cce714b2 100644
@@ -19,8 +19,17 @@ struct qnodes {
 	struct qnode nodes[MAX_NODES];
 };
 
Quoted 5 lines

arch/powerpc/lib/qspinlock.c struct qnodes { · line 24

Quoted 3 lines
arch/powerpc/lib/qspinlock.c +3 -0
+/* Tuning parameters */
+static int STEAL_SPINS __read_mostly = (1<<5);
+static bool MAYBE_STEALERS __read_mostly = true;

I can understand why, but macro case variables can be a bit confusing.

Yeah they started out as #defines. I'll change them.

Quoted 15 lines
Quoted 15 lines
arch/powerpc/lib/qspinlock.c +6 -0
+
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
+static __always_inline int get_steal_spins(void)
+{
+	return STEAL_SPINS;
+}
+
 static inline u32 encode_tail_cpu(void)
 {
 	return (smp_processor_id() + 1) << _Q_TAIL_CPU_OFFSET;
@@ -76,6 +85,39 @@ static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 ol
 	return 0;
 }
 
Quoted 10 lines

arch/powerpc/lib/qspinlock.c static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 ol · line 95

Quoted 8 lines
arch/powerpc/lib/qspinlock.c +8 -0
+static __always_inline u32 __trylock_cmpxchg(struct qspinlock *lock, u32 old, u32 new)
+{
+	u32 prev;
+
+	BUG_ON(old & _Q_LOCKED_VAL);
+
+	asm volatile(
+"1:	lwarx	%0,0,%1,%4	# queued_spin_trylock_cmpxchg		\n"

s/queued_spin_trylock_cmpxchg/__trylock_cmpxchg/

Yes.

Quoted 1 line

btw what is the format you using for the '\n's in the inline asm?

Ah, not really sure :P

Quoted 5 lines
Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+"	cmpw	0,%0,%2							\n"
+"	bne-	2f							\n"
+"	stwcx.	%3,0,%1							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
Quoted 8 lines

arch/powerpc/lib/qspinlock.c static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 ol · line 105

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r"(old), "r" (new),
+	  "i" (IS_ENABLED(CONFIG_PPC64) ? 1 : 0)
+	: "cr0", "memory");

This is very similar to trylock_clear_tail_cpu(). So maybe it is worth having some form of "test and set" primitive helper.

Yes I was able to consolidate these two, good point.

Quoted 34 lines

arch/powerpc/lib/qspinlock.c static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 ol · line 118

Quoted 13 lines
arch/powerpc/lib/qspinlock.c +13 -0
+
+	return prev;
+}
+
+/* Take lock, preserving tail, cmpxchg with val (which must not be locked) */
+static __always_inline int trylock_with_tail_cpu(struct qspinlock *lock, u32 val)
+{
+	u32 newval = _Q_LOCKED_VAL | (val & _Q_TAIL_CPU_MASK);
+
+	if (__trylock_cmpxchg(lock, val, newval) == val)
+		return 1;
+	else
+		return 0;

same optional style nit: return __trylock_cmpxchg(lock, val, newval) == val

Quoted 18 lines
arch/powerpc/lib/qspinlock.c +11 -0
+}
+
 /*
  * Publish our tail, replacing previous tail. Return previous value.
  *
@@ -115,6 +157,31 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
+static inline bool try_to_steal_lock(struct qspinlock *lock)
+{
+	int iters;
+
+	/* Attempt to steal the lock */
+	for (;;) {
+		u32 val = READ_ONCE(lock->val);
+
+		if (unlikely(!(val & _Q_LOCKED_VAL))) {
Quoted 8 lines

arch/powerpc/lib/qspinlock.c static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val) · line 172

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +4 -0
+			if (trylock_with_tail_cpu(lock, val))
+				return true;
+			continue;
+		}

The continue would bypass iters++/cpu_relax but the next time around
if (unlikely(!(val & _Q_LOCKED_VAL))) {
should fail so everything should be fine?

Yes it should. I suppose it could starve in theory though. Maybe I'll change it to count as an iteration.

Quoted 8 lines

arch/powerpc/lib/qspinlock.c void pv_spinlocks_init(void) · line 302

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +4 -0
+#include <linux/debugfs.h>
+static int steal_spins_set(void *data, u64 val)
+{
+	static DEFINE_MUTEX(lock);

I just want to check if it would be possible to get rid of the MAYBE_STEALERS variable completely and do something like:

Quoted 4 lines

bool maybe_stealers() { return STEAL_SPINS > 0; }

I guess based on the below code it wouldn't work, but I'm still not quite sure why that is.

Because the slowpath has a !maybe_stealers path which assumes the lock won't be stolen so it doesn't need to cmpxchg the lock bit on, among other things.

I'll add a bit more comment.

Quoted 10 lines
Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+
+	mutex_lock(&lock);
+	if (val && !STEAL_SPINS) {
+		MAYBE_STEALERS = true;
+		/* wait for waiter to go away */
+		synchronize_rcu();
+		STEAL_SPINS = val;
+	} else if (!val && STEAL_SPINS) {
+		STEAL_SPINS = val;
+		/* wait for all possible stealers to go away */
Quoted 8 lines

arch/powerpc/lib/qspinlock.c void pv_spinlocks_init(void) · line 318

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +6 -0
+		synchronize_rcu();
+		MAYBE_STEALERS = false;
+	} else {
+		STEAL_SPINS = val;
+	}
+	mutex_unlock(&lock);

STEAL_SPINS is an int not a u64.

Yeah but that's how the DEFINE_SIMPLE_ATTRIBUTE things seem to work, unfortunately.

Thanks,
Nick

author reviewer
#30 lore raw

Re: [PATCH 06/17] powerpc/qspinlock: theft prevention to control latency

In reply to Jordan Niethe
Quoted 41 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 25 lines

Give the queue head the ability to stop stealers. After a number of spins without sucessfully acquiring the lock, the queue head employs this, which will assure it is the next owner.

---
 arch/powerpc/include/asm/qspinlock_types.h | 10 +++-
 arch/powerpc/lib/qspinlock.c               | 56 +++++++++++++++++++++-
 2 files changed, 63 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/qspinlock_types.h +2 -1
index 210adf05b235..8b20f5e22bba 100644
@@ -29,7 +29,8 @@ typedef struct qspinlock {
  * Bitfields in the lock word:
  *
  *     0: locked bit
- * 16-31: tail cpu (+1)
+ *    16: must queue bit
+ * 17-31: tail cpu (+1)
  */
 #define	_Q_SET_MASK(type)	(((1U << _Q_ ## type ## _BITS) - 1)\
 				      << _Q_ ## type ## _OFFSET)
@@ -38,7 +39,12 @@ typedef struct qspinlock {
 #define _Q_LOCKED_MASK		_Q_SET_MASK(LOCKED)
 #define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)

arch/powerpc/include/asm/qspinlock_types.h typedef struct qspinlock { · line 49

Quoted 10 lines
arch/powerpc/include/asm/qspinlock_types.h +6 -1
 
-#define _Q_TAIL_CPU_OFFSET	16
+#define _Q_MUST_Q_OFFSET	16
+#define _Q_MUST_Q_BITS		1
+#define _Q_MUST_Q_MASK		_Q_SET_MASK(MUST_Q)
+#define _Q_MUST_Q_VAL		(1U << _Q_MUST_Q_OFFSET)
+
+#define _Q_TAIL_CPU_OFFSET	17
 #define _Q_TAIL_CPU_BITS	(32 - _Q_TAIL_CPU_OFFSET)
 #define _Q_TAIL_CPU_MASK	_Q_SET_MASK(TAIL_CPU)

Not a big deal but some of these values could be calculated like in the generic version. e.g.

Quoted 1 line
	#define _Q_PENDING_OFFSET	(_Q_LOCKED_OFFSET +_Q_LOCKED_BITS)

Yeah, we don't *really* have more than one locked bit though. Haven't made up my mind about these defines yet.

Quoted 28 lines
Quoted 28 lines
arch/powerpc/lib/qspinlock.c +6 -0
index 1625cce714b2..a906cc8f15fa 100644
@@ -22,6 +22,7 @@ struct qnodes {
 /* Tuning parameters */
 static int STEAL_SPINS __read_mostly = (1<<5);
 static bool MAYBE_STEALERS __read_mostly = true;
+static int HEAD_SPINS __read_mostly = (1<<8);
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
@@ -30,6 +31,11 @@ static __always_inline int get_steal_spins(void)
 	return STEAL_SPINS;
 }
 
+static __always_inline int get_head_spins(void)
+{
+	return HEAD_SPINS;
+}
+
 static inline u32 encode_tail_cpu(void)
 {
 	return (smp_processor_id() + 1) << _Q_TAIL_CPU_OFFSET;
@@ -142,6 +148,23 @@ static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail)
 	return prev;
 }
 
Quoted 9 lines

arch/powerpc/lib/qspinlock.c static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail) · line 157

Quoted 7 lines
arch/powerpc/lib/qspinlock.c +7 -0
+static __always_inline u32 lock_set_mustq(struct qspinlock *lock)
+{
+	u32 new = _Q_MUST_Q_VAL;
+	u32 prev;
+
+	asm volatile(
+"1:	lwarx	%0,0,%1		# lock_set_mustq			\n"

Is the EH bit not set because we don't hold the lock here?

Right, we're still waiting for it.

Quoted 39 lines

arch/powerpc/lib/qspinlock.c static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail) · line 163

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +6 -0
+"	or	%0,%0,%2						\n"
+"	stwcx.	%0,0,%1							\n"
+"	bne-	1b							\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new)
+	: "cr0", "memory");

This is another usage close to the DEFINE_TESTOP() pattern.

Quoted 30 lines
arch/powerpc/lib/qspinlock.c +11 -1
+
+	return prev;
+}
+
 static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 {
 	int cpu = get_tail_cpu(val);
@@ -165,6 +188,9 @@ static inline bool try_to_steal_lock(struct qspinlock *lock)
 	for (;;) {
 		u32 val = READ_ONCE(lock->val);
 
+		if (val & _Q_MUST_Q_VAL)
+			break;
+
 		if (unlikely(!(val & _Q_LOCKED_VAL))) {
 			if (trylock_with_tail_cpu(lock, val))
 				return true;
@@ -246,11 +272,22 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 		/* We must be the owner, just set the lock bit and acquire */
 		lock_set_locked(lock);
 	} else {
+		int iters = 0;
+		bool set_mustq = false;
+
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
-		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
+		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
 			cpu_relax();
 
Quoted 3 lines

arch/powerpc/lib/qspinlock.c static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock) · line 283

Quoted 1 line
arch/powerpc/lib/qspinlock.c +1 -0
+			iters++;

It seems instead of using set_mustq, (val & _Q_MUST_Q_VAL) could be checked?

I wanted to give the reader (and compiler for what that's worth) the idea that it won't change concurrently after we set it.

Thanks,
Nick

author reviewer
#34 lore raw

Re: [PATCH 07/17] powerpc/qspinlock: store owner CPU in lock word

In reply to Jordan Niethe
Quoted 18 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 16 lines

Store the owner CPU number in the lock word so it may be yielded to, as powerpc's paravirtualised simple spinlocks do.

---
 arch/powerpc/include/asm/qspinlock.h       |  8 +++++++-
 arch/powerpc/include/asm/qspinlock_types.h | 10 ++++++++++
 arch/powerpc/lib/qspinlock.c               |  6 +++---
 3 files changed, 20 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/qspinlock.h +0 -0
index 3ab354159e5e..44601b261e08 100644
@@ -20,9 +20,15 @@ static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
 	return !!(READ_ONCE(lock->val) & _Q_TAIL_CPU_MASK);
 }
 
Quoted 4 lines

arch/powerpc/include/asm/qspinlock.h static __always_inline int queued_spin_is_contended(struct qspinlock *lock) · line 23

Quoted 1 line
arch/powerpc/include/asm/qspinlock.h +1 -0
+static __always_inline u32 queued_spin_get_locked_val(void)

Maybe this function should have "encode" in the name to match with encode_tail_cpu().

Yep.

Quoted 4 lines

arch/powerpc/include/asm/qspinlock.h static __always_inline int queued_spin_is_contended(struct qspinlock *lock) · line 25

Quoted 2 lines
arch/powerpc/include/asm/qspinlock.h +2 -0
+{
+	/* XXX: make this use lock value in paca like simple spinlocks? */

Is that the paca's lock_token which is 0x8000?

Yes, which AFAIKS is actually unused now with queued spinlocks.

Quoted 9 lines
Quoted 9 lines
arch/powerpc/include/asm/qspinlock.h +4 -1
+	return _Q_LOCKED_VAL | (smp_processor_id() << _Q_OWNER_CPU_OFFSET);
+}
+
 static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 {
-	u32 new = _Q_LOCKED_VAL;
+	u32 new = queued_spin_get_locked_val();
 	u32 prev;
 
Quoted 15 lines

arch/powerpc/include/asm/qspinlock_types.h typedef struct qspinlock { · line 35

Quoted 13 lines
arch/powerpc/include/asm/qspinlock.h +0 -0
 	asm volatile(
arch/powerpc/include/asm/qspinlock_types.h +2 -0
index 8b20f5e22bba..35f9525381e6 100644
@@ -29,6 +29,8 @@ typedef struct qspinlock {
  * Bitfields in the lock word:
  *
  *     0: locked bit
+ *  1-14: lock holder cpu
+ *    15: unused bit
  *    16: must queue bit
  * 17-31: tail cpu (+1)

So there is one more bit to store the tail cpu vs the lock holder cpu?

Yeah but the tail has to encode it as CPU+1.

Thanks,
Nick

author reviewer
#38 lore raw

Re: [PATCH 08/17] powerpc/qspinlock: paravirt yield to lock owner

In reply to Jordan Niethe
Quoted 21 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 19 lines

Waiters spinning on the lock word should yield to the lock owner if the vCPU is preempted. This improves performance when the hypervisor has oversubscribed physical CPUs.

---
 arch/powerpc/lib/qspinlock.c | 97 ++++++++++++++++++++++++++++++------
 1 file changed, 83 insertions(+), 14 deletions(-)
arch/powerpc/lib/qspinlock.c +1 -0
index aa26cfe21f18..55286ac91da5 100644
@@ -5,6 +5,7 @@
 #include <linux/percpu.h>
 #include <linux/smp.h>
 #include <asm/qspinlock.h>
+#include <asm/paravirt.h>
 
 #define MAX_NODES	4
 
Quoted 8 lines

arch/powerpc/lib/qspinlock.c static int STEAL_SPINS __read_mostly = (1<<5); · line 28

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -24,14 +25,16 @@ static int STEAL_SPINS __read_mostly = (1<<5);
 static bool MAYBE_STEALERS __read_mostly = true;
 static int HEAD_SPINS __read_mostly = (1<<8);
 
+static bool pv_yield_owner __read_mostly = true;

Not macro case for these globals? To me name does not make it super clear this is a boolean. What about pv_yield_owner_enabled?

Hmm. Might think about doing a better prefix namespace for these tunables, which might help.

Quoted 31 lines
Quoted 31 lines
arch/powerpc/lib/qspinlock.c +8 -3
+
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
-static __always_inline int get_steal_spins(void)
+static __always_inline int get_steal_spins(bool paravirt)
 {
 	return STEAL_SPINS;
 }
 
-static __always_inline int get_head_spins(void)
+static __always_inline int get_head_spins(bool paravirt)
 {
 	return HEAD_SPINS;
 }
@@ -46,7 +49,11 @@ static inline int get_tail_cpu(u32 val)
 	return (val >> _Q_TAIL_CPU_OFFSET) - 1;
 }
 
-/* Take the lock by setting the bit, no other CPUs may concurrently lock it. */
+static inline int get_owner_cpu(u32 val)
+{
+	return (val & _Q_OWNER_CPU_MASK) >> _Q_OWNER_CPU_OFFSET;
+}
+
 /* Take the lock by setting the lock bit, no other CPUs will touch it. */
 static __always_inline void lock_set_locked(struct qspinlock *lock)
 {
@@ -180,7 +187,45 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
Quoted 4 lines

arch/powerpc/lib/qspinlock.c static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val) · line 190

Quoted 2 lines
arch/powerpc/lib/qspinlock.c +1 -1
-static inline bool try_to_steal_lock(struct qspinlock *lock)
+static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)

This name doesn't seem correct for the non paravirt case.

Well... a yield to a running CPU is just a relax in any case. I think it's okay.

Quoted 11 lines
Quoted 11 lines
arch/powerpc/lib/qspinlock.c +11 -0
+{
+	int owner;
+	u32 yield_count;
+
+	BUG_ON(!(val & _Q_LOCKED_VAL));
+
+	if (!paravirt)
+		goto relax;
+
+	if (!pv_yield_owner)
+		goto relax;
Quoted 8 lines

arch/powerpc/lib/qspinlock.c static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val) · line 207

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +6 -0
+
+	owner = get_owner_cpu(val);
+	yield_count = yield_count_of(owner);
+
+	if ((yield_count & 1) == 0)
+		goto relax; /* owner vcpu is running */

I wonder why not use vcpu_is_preempted()?

Because we use a particular yield_count for the yield hcall (it tries to avoid the situation where the owner wakes up and may release the lock and then we yield to it).

Quoted 96 lines
Quoted 77 lines
arch/powerpc/lib/qspinlock.c +27 -10
+
+	/*
+	 * Read the lock word after sampling the yield count. On the other side
+	 * there may a wmb because the yield count update is done by the
+	 * hypervisor preemption and the value update by the OS, however this
+	 * ordering might reduce the chance of out of order accesses and
+	 * improve the heuristic.
+	 */
+	smp_rmb();
+
+	if (READ_ONCE(lock->val) == val) {
+		yield_to_preempted(owner, yield_count);
+		/* Don't relax if we yielded. Maybe we should? */
+		return;
+	}
+relax:
+	cpu_relax();
+}
+
+
+static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {
 	int iters;
 
@@ -197,18 +242,18 @@ static inline bool try_to_steal_lock(struct qspinlock *lock)
 			continue;
 		}
 
-		cpu_relax();
+		yield_to_locked_owner(lock, val, paravirt);
 
 		iters++;
 
-		if (iters >= get_steal_spins())
+		if (iters >= get_steal_spins(paravirt))
 			break;
 	}
 
 	return false;
 }
 
-static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
+static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, bool paravirt)
 {
 	struct qnodes *qnodesp;
 	struct qnode *next, *node;
@@ -260,7 +305,7 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 	if (!MAYBE_STEALERS) {
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL)
-			cpu_relax();
+			yield_to_locked_owner(lock, val, paravirt);
 
 		/* If we're the last queued, must clean up the tail. */
 		if ((val & _Q_TAIL_CPU_MASK) == tail) {
@@ -278,10 +323,10 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 again:
 		/* We're at the head of the waitqueue, wait for the lock. */
 		while ((val = READ_ONCE(lock->val)) & _Q_LOCKED_VAL) {
-			cpu_relax();
+			yield_to_locked_owner(lock, val, paravirt);
 
 			iters++;
-			if (!set_mustq && iters >= get_head_spins()) {
+			if (!set_mustq && iters >= get_head_spins(paravirt)) {
 				set_mustq = true;
 				lock_set_mustq(lock);
 				val |= _Q_MUST_Q_VAL;
@@ -320,10 +365,15 @@ static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock)
 
 void queued_spin_lock_slowpath(struct qspinlock *lock)
 {
-	if (try_to_steal_lock(lock))
-		return;
-
-	queued_spin_lock_mcs_queue(lock);

arch/powerpc/lib/qspinlock.c static inline void queued_spin_lock_mcs_queue(struct qspinlock *lock) · line 377

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +9 -0
+	if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) && is_shared_processor()) {
+		if (try_to_steal_lock(lock, true))
+			return;
+		queued_spin_lock_mcs_queue(lock, true);
+	} else {
+		if (try_to_steal_lock(lock, false))
+			return;
+		queued_spin_lock_mcs_queue(lock, false);
+	}
 }

There is not really a need for a conditional:

bool paravirt = IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) && is_shared_processor();

if (try_to_steal_lock(lock, paravirt))

	return;
Quoted 7 lines

queued_spin_lock_mcs_queue(lock, paravirt);

The paravirt parameter used by the various functions seems always to be
equivalent to (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) && is_shared_processor()).
I wonder if it would be simpler testing (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) && is_shared_processor()) (using a helper function) in those functions instead passing it as a parameter?

You'd think so and yes semantically that's identical, but with my version gcc-12 seems to inline each side and with yours they are more shared. We actually want the separate versions because is_shared_processor() is set at boot so we always only run one side or the other so we want best efficiency possible and don't have the icache pollution concern because the other side never runs.

At least that's the idea, that's what generic qspinlocks do too.

Thanks,
Nick

author reviewer
#42 lore raw

Re: [PATCH 09/17] powerpc/qspinlock: implement option to yield to previous node

In reply to Jordan Niethe
Quoted 19 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 17 lines

Queued waiters which are not at the head of the queue don't spin on the lock word but their qnode lock word, waiting for the previous queued CPU to release them. Add an option which allows these waiters to yield to the previous CPU if its vCPU is preempted.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 46 +++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +0 -0
index 55286ac91da5..b39f8c5b329c 100644
@@ -26,6 +26,7 @@ static bool MAYBE_STEALERS __read_mostly = true;
 static int HEAD_SPINS __read_mostly = (1<<8);
 
Quoted 6 lines

arch/powerpc/lib/qspinlock.c static bool MAYBE_STEALERS __read_mostly = true; · line 29

Quoted 2 lines
arch/powerpc/lib/qspinlock.c +1 -0
 static bool pv_yield_owner __read_mostly = true;
+static bool pv_yield_prev __read_mostly = true;

Similiar suggestion, maybe pv_yield_prev_enabled would read better.

Isn't this enabled by default contrary to the commit message?

Yeah a few of those changelogs got out of synch.

Quoted 4 lines
Quoted 4 lines
arch/powerpc/lib/qspinlock.c +0 -0
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
Quoted 8 lines

arch/powerpc/lib/qspinlock.c static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va · line 228

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -224,6 +225,31 @@ static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va
 	cpu_relax();
 }
 
+static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)

yield_to_locked_owner() takes a raw val and works out the cpu to yield to. I think for consistency have yield_to_prev() take the raw val and work it out too.

Good thinking.

Quoted 39 lines
Quoted 28 lines
arch/powerpc/lib/qspinlock.c +24 -0
+{
+	u32 yield_count;
+
+	if (!paravirt)
+		goto relax;
+
+	if (!pv_yield_prev)
+		goto relax;
+
+	yield_count = yield_count_of(prev_cpu);
+	if ((yield_count & 1) == 0)
+		goto relax; /* owner vcpu is running */
+
+	smp_rmb(); /* See yield_to_locked_owner comment */
+
+	if (!node->locked) {
+		yield_to_preempted(prev_cpu, yield_count);
+		return;
+	}
+
+relax:
+	cpu_relax();
+}
+
 
 static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {

arch/powerpc/lib/qspinlock.c static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b · line 320

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -291,13 +317,14 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	 */
 	if (old & _Q_TAIL_CPU_MASK) {
 		struct qnode *prev = get_tail_qnode(lock, old);
+		int prev_cpu = get_tail_cpu(old);

This could then be removed.

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +0 -0
 		/* Link @node into the waitqueue. */
 		WRITE_ONCE(prev->next, node);
 
Quoted 7 lines

arch/powerpc/lib/qspinlock.c static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b · line 327

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -1
 		/* Wait for mcs node lock to be released */
 		while (!node->locked)
-			cpu_relax();
+			yield_to_prev(lock, node, prev_cpu, paravirt);

And would have this as:

			yield_to_prev(lock, node, old, paravirt);

Yep.

Thanks,
Nick

author reviewer
#46 lore raw

Re: [PATCH 10/17] powerpc/qspinlock: allow stealing when head of queue yields

In reply to Jordan Niethe
Quoted 60 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 15 lines

If the head of queue is preventing stealing but it finds the owner vCPU is preempted, it will yield its cycles to the owner which could cause it to become preempted. Add an option to re-allow stealers before yielding, and disallow them again after returning from the yield.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 56 ++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 3 deletions(-)
arch/powerpc/lib/qspinlock.c +0 -0
index b39f8c5b329c..94f007f66942 100644

arch/powerpc/lib/qspinlock.c static bool MAYBE_STEALERS __read_mostly = true; · line 29

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -26,6 +26,7 @@ static bool MAYBE_STEALERS __read_mostly = true;
 static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
+static bool pv_yield_allow_steal __read_mostly = false;

To me this one does read as a boolean, but if you go with those other changes I'd make it pv_yield_steal_enable to be consistent.

Quoted 11 lines
arch/powerpc/lib/qspinlock.c +4 -0
 static bool pv_yield_prev __read_mostly = true;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
@@ -173,6 +174,23 @@ static __always_inline u32 lock_set_mustq(struct qspinlock *lock)
 	return prev;
 }
 
+static __always_inline u32 lock_clear_mustq(struct qspinlock *lock)
+{
+	u32 new = _Q_MUST_Q_VAL;
+	u32 prev;

arch/powerpc/lib/qspinlock.c static __always_inline u32 lock_set_mustq(struct qspinlock *lock) · line 190

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+
+	asm volatile(
+"1:	lwarx	%0,0,%1		# lock_clear_mustq			\n"
+"	andc	%0,%0,%2						\n"
+"	stwcx.	%0,0,%1							\n"
+"	bne-	1b							\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r" (new)
+	: "cr0", "memory");
+

This is pretty similar to the DEFINE_TESTOP() pattern again with the same llong caveat.

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +3 -0
+	return prev;
+}
+
 static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 {
 	int cpu = get_tail_cpu(val);
@@ -188,7 +206,7 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
Quoted 4 lines

arch/powerpc/lib/qspinlock.c static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val) · line 209

Quoted 2 lines
arch/powerpc/lib/qspinlock.c +1 -1
-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)

/* See yield_to_locked_owner comment */ comment needs to be updated now.

Yep.

Quoted 19 lines
Quoted 19 lines
arch/powerpc/lib/qspinlock.c +4 -0
 {
 	int owner;
 	u32 yield_count;
@@ -217,7 +235,11 @@ static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va
 	smp_rmb();
 
 	if (READ_ONCE(lock->val) == val) {
+		if (clear_mustq)
+			lock_clear_mustq(lock);
 		yield_to_preempted(owner, yield_count);
+		if (clear_mustq)
+			lock_set_mustq(lock);
 		/* Don't relax if we yielded. Maybe we should? */
 		return;
 	}
@@ -225,6 +247,16 @@ static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va
 	cpu_relax();
 }
 
Quoted 12 lines

arch/powerpc/lib/qspinlock.c static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 va · line 256

Quoted 7 lines
arch/powerpc/lib/qspinlock.c +7 -0
+static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+{
+	__yield_to_locked_owner(lock, val, paravirt, false);
+}
+
+static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+{

The check for pv_yield_allow_steal seems like it could go here instead of being done by the caller.
__yield_to_locked_owner() checks for pv_yield_owner so it seems more
consistent.

Yeah that worked and is probably an improvement.

Thanks,
Nick

author reviewer
#51 lore raw

Re: [PATCH 11/17] powerpc/qspinlock: allow propagation of yield CPU down the queue

In reply to Jordan Niethe
Quoted 60 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 30 lines

Having all CPUs poll the lock word for the owner CPU that should be yielded to defeats most of the purpose of using MCS queueing for scalability. Yet it may be desirable for queued waiters to to yield to a preempted owner.

s390 addreses this problem by having queued waiters sample the lock word to find the owner much less frequently. In this approach, the waiters never sample it directly, but the queue head propagates the owner CPU back to the next waiter if it ever finds the owner has been preempted. Queued waiters then subsequently propagate the owner CPU back to the next waiter, and so on.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 85 +++++++++++++++++++++++++++++++++++-
 1 file changed, 84 insertions(+), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +1 -0
index 94f007f66942..28c85a2d5635 100644
@@ -12,6 +12,7 @@
 struct qnode {
 	struct qnode	*next;
 	struct qspinlock *lock;
+	int		yield_cpu;
 	u8		locked; /* 1 if lock acquired */
 };
 

arch/powerpc/lib/qspinlock.c static int HEAD_SPINS __read_mostly = (1<<8); · line 32

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -28,6 +29,7 @@ static int HEAD_SPINS __read_mostly = (1<<8);
 static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
+static bool pv_yield_propagate_owner __read_mostly = true;

This also seems to be enabled by default.

Quoted 21 lines
arch/powerpc/lib/qspinlock.c +14 -0
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
@@ -257,13 +259,66 @@ static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u
 	__yield_to_locked_owner(lock, val, paravirt, clear_mustq);
 }
 
+static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int *set_yield_cpu, bool paravirt)
+{
+	struct qnode *next;
+	int owner;
+
+	if (!paravirt)
+		return;
+	if (!pv_yield_propagate_owner)
+		return;
+
+	owner = get_owner_cpu(val);
+	if (*set_yield_cpu == owner)
+		return;
+
Quoted 8 lines

arch/powerpc/lib/qspinlock.c static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u · line 280

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+	next = READ_ONCE(node->next);
+	if (!next)
+		return;
+
+	if (vcpu_is_preempted(owner)) {

Is there a difference about using vcpu_is_preempted() here vs checking bit 0 in other places?

Yeah we weren't yielding here so didn't have to load the yield count explicitly.

Quoted 5 lines

arch/powerpc/lib/qspinlock.c static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u · line 283

Quoted 3 lines
arch/powerpc/lib/qspinlock.c +3 -0
+		next->yield_cpu = owner;
+		*set_yield_cpu = owner;
+	} else if (*set_yield_cpu != -1) {

It might be worth giving the -1 CPU a #define.

It's fairly standard to use -1 as a null value for cpu.

Quoted 38 lines

arch/powerpc/lib/qspinlock.c static __always_inline void yield_head_to_locked_owner(struct qspinlock *lock, u · line 287

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +4 -0
+		next->yield_cpu = owner;
+		*set_yield_cpu = owner;
+	}
+}

Does this need to pass set_yield_cpu by reference? Couldn't it's new value be returned? To me it makes it more clear the function is used to change set_yield_cpu. I think this would work:

int set_yield_cpu = -1;

static __always_inline int propagate_yield_cpu(struct qnode *node, u32 val, int set_yield_cpu, bool paravirt)

{
	struct qnode *next;
	int owner;

	if (!paravirt)
		goto out;
	if (!pv_yield_propagate_owner)
		goto out;

	owner = get_owner_cpu(val);
	if (set_yield_cpu == owner)
		goto out;

	next = READ_ONCE(node->next);
	if (!next)
		goto out;

	if (vcpu_is_preempted(owner)) {
		next->yield_cpu = owner;
		return owner;
	} else if (set_yield_cpu != -1) {
		next->yield_cpu = owner;
		return owner;
	}
Quoted 5 lines

out:

	return set_yield_cpu;
}

set_yield_cpu = propagate_yield_cpu(... set_yield_cpu ...);

I think I prefer as is, because the caller doesn't use it anywhere. It looks more like some temporary storage to be used by the function over multiple calls this way.

Thanks,
Nick

author reviewer
#55 lore raw

Re: [PATCH 12/17] powerpc/qspinlock: add ability to prod new queue head CPU

In reply to Jordan Niethe
Quoted 40 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 31 lines

After the head of the queue acquires the lock, it releases the next waiter in the queue to become the new head. Add an option to prod the new head if its vCPU was preempted. This may only have an effect if queue waiters are yielding.

Disable this option by default for now, i.e., no logical change.

---
 arch/powerpc/lib/qspinlock.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
arch/powerpc/lib/qspinlock.c +2 -0
index 28c85a2d5635..3b10e31bcf0a 100644
@@ -12,6 +12,7 @@
 struct qnode {
 	struct qnode	*next;
 	struct qspinlock *lock;
+	int		cpu;
 	int		yield_cpu;
 	u8		locked; /* 1 if lock acquired */
 };
@@ -30,6 +31,7 @@ static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
 static bool pv_yield_propagate_owner __read_mostly = true;
+static bool pv_prod_head __read_mostly = false;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 

arch/powerpc/lib/qspinlock.c static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b · line 397

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +1 -0
@@ -392,6 +394,7 @@ static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, b
 	node = &qnodesp->nodes[idx];
 	node->next = NULL;
 	node->lock = lock;
+	node->cpu = smp_processor_id();

I suppose this could be used in some other places too.

Quoted 4 lines

For example change:

	yield_to_prev(lock, node, prev, paravirt);

In yield_to_prev() it could then access the prev->cpu.

That case is a bit iffy. As soon as we WRITE_ONCE to prev, the prev lock holder can go away. It's a statically allocated array and per-CPU so it should actually give us the right value even if that CPU queued on some other lock again, but I think it's more straightforward just to not touch it after that point. This is also a remote and hot cache line, so avoiding any loads on it is nice (we have the store, but you don't have to wait for those), and we already have val.

Thanks,
Nick

author reviewer
#59 lore raw

Re: [PATCH 13/17] powerpc/qspinlock: trylock and initial lock attempt may steal

In reply to Jordan Niethe
Quoted 18 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 16 lines

This gives trylock slightly more strength, and it also gives most of the benefit of passing 'val' back through the slowpath without the complexity.

---
 arch/powerpc/include/asm/qspinlock.h | 39 +++++++++++++++++++++++++++-
 arch/powerpc/lib/qspinlock.c         |  9 +++++++
 2 files changed, 47 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/qspinlock.h +0 -0
index 44601b261e08..d3d2039237b2 100644
@@ -5,6 +5,8 @@
 #include <linux/compiler.h>
 #include <asm/qspinlock_types.h>
 
Quoted 3 lines

arch/powerpc/include/asm/qspinlock.h line 8

Quoted 1 line
arch/powerpc/include/asm/qspinlock.h +1 -0
+#define _Q_SPIN_TRY_LOCK_STEAL 1

Would this be a config option?

I think probably not, it's more to keep the other code variant there if we want to try tune experiment with it. We might end up cutting out a bunch of these options if we narrow down on a good configuration.

Quoted 15 lines
Quoted 15 lines
arch/powerpc/include/asm/qspinlock.h +2 -1
+
 static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
 {
 	return READ_ONCE(lock->val);
@@ -26,11 +28,12 @@ static __always_inline u32 queued_spin_get_locked_val(void)
 	return _Q_LOCKED_VAL | (smp_processor_id() << _Q_OWNER_CPU_OFFSET);
 }
 
-static __always_inline int queued_spin_trylock(struct qspinlock *lock)
+static __always_inline int __queued_spin_trylock_nosteal(struct qspinlock *lock)
 {
 	u32 new = queued_spin_get_locked_val();
 	u32 prev;
 
Quoted 5 lines

arch/powerpc/include/asm/qspinlock.h static __always_inline u32 queued_spin_get_locked_val(void) · line 38

Quoted 3 lines
arch/powerpc/include/asm/qspinlock.h +1 -0
+	/* Trylock succeeds only when unlocked and no queued nodes */
 	asm volatile(
 "1:	lwarx	%0,0,%1,%3	# queued_spin_trylock			\n"

s/queued_spin_trylock/__queued_spin_trylock_nosteal

I wanted to keep those because they (can be) inlined into the wider kernel, so you'd rather see queued_spin_trylock than this internal name.

Quoted 40 lines

arch/powerpc/include/asm/qspinlock.h static __always_inline int queued_spin_trylock(struct qspinlock *lock) · line 62

Quoted 13 lines
arch/powerpc/include/asm/qspinlock.h +8 -0
 "	cmpwi	0,%0,0							\n"
@@ -49,6 +52,40 @@ static __always_inline int queued_spin_trylock(struct qspinlock *lock)
 	return 0;
 }
 
+static __always_inline int __queued_spin_trylock_steal(struct qspinlock *lock)
+{
+	u32 new = queued_spin_get_locked_val();
+	u32 prev, tmp;
+
+	/* Trylock may get ahead of queued nodes if it finds unlocked */
+	asm volatile(
+"1:	lwarx	%0,0,%2,%5	# queued_spin_trylock			\n"

s/queued_spin_trylock/__queued_spin_trylock_steal

arch/powerpc/include/asm/qspinlock.h static __always_inline int queued_spin_trylock(struct qspinlock *lock) · line 70

Quoted 8 lines
arch/powerpc/include/asm/qspinlock.h +8 -0
+"	andc.	%1,%0,%4						\n"
+"	bne-	2f							\n"
+"	and	%1,%0,%4						\n"
+"	or	%1,%1,%3						\n"
+"	stwcx.	%1,0,%2							\n"
+"	bne-	1b							\n"
+"\t"	PPC_ACQUIRE_BARRIER "						\n"
+"2:									\n"

Just because there's a little bit more going on here...

Q_TAIL_CPU_MASK = 0xFFFE0000
~Q_TAIL_CPU_MASK = 0x1FFFF

1: lwarx prev, 0, &lock->val, IS_ENABLED_PPC64

	andc.	tmp, prev, _Q_TAIL_CPU_MASK 	(tmp = prev & ~_Q_TAIL_CPU_MASK)
	bne-	2f 				(exit if locked)
	and	tmp, prev, _Q_TAIL_CPU_MASK 	(tmp = prev & _Q_TAIL_CPU_MASK)
	or	tmp, tmp, new			(tmp |= new)					
	stwcx.	tmp, 0, &lock->val					
		
	bne-	1b							
	PPC_ACQUIRE_BARRIER		
Quoted 3 lines

2:

... which seems correct.

Thanks,
Nick

author reviewer
#63 lore raw

Re: [PATCH 14/17] powerpc/qspinlock: use spin_begin/end API

In reply to Jordan Niethe
Quoted 14 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 3 lines

Use the spin_begin/spin_cpu_relax/spin_end APIs in qspinlock, which helps to prevent threads issuing a lot of expensive priority nops which may not have much effect due to immediately executing low then medium priority.

Just a general comment regarding the spin_{begin,end} API, more complicated than something like

	spin_begin()
	for(;;)
		spin_cpu_relax()
	spin_end()
Quoted 6 lines

it becomes difficult to keep track of. Unfortunately, I don't have any good suggestions how to improve it. Hopefully with P10s wait instruction we can maybe try and move away from this.

It might be useful to comment the functions pre and post conditions regarding expectations about spin_begin() and spin_end().

Yep, added some small comments.

Thanks,
Nick

author reviewer
#67 lore raw

Re: [PATCH 15/17] powerpc/qspinlock: reduce remote node steal spins

In reply to Jordan Niethe
Quoted 50 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 48 lines

Allow for a reduction in the number of times a CPU from a different node than the owner can attempt to steal the lock before queueing. This could bias the transfer behaviour of the lock across the machine and reduce NUMA crossings.

---
 arch/powerpc/lib/qspinlock.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)
arch/powerpc/lib/qspinlock.c +8 -2
index d4594c701f7d..24f68bd71e2b 100644
@@ -4,6 +4,7 @@
 #include <linux/export.h>
 #include <linux/percpu.h>
 #include <linux/smp.h>
+#include <linux/topology.h>
 #include <asm/qspinlock.h>
 #include <asm/paravirt.h>
 
@@ -24,6 +25,7 @@ struct qnodes {
 
 /* Tuning parameters */
 static int STEAL_SPINS __read_mostly = (1<<5);
+static int REMOTE_STEAL_SPINS __read_mostly = (1<<2);
 #if _Q_SPIN_TRY_LOCK_STEAL == 1
 static const bool MAYBE_STEALERS = true;
 #else
@@ -39,9 +41,13 @@ static bool pv_prod_head __read_mostly = false;
 
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
 
-static __always_inline int get_steal_spins(bool paravirt)
+static __always_inline int get_steal_spins(bool paravirt, bool remote)
 {
-	return STEAL_SPINS;
+	if (remote) {
+		return REMOTE_STEAL_SPINS;
+	} else {
+		return STEAL_SPINS;
+	}
 }
 
 static __always_inline int get_head_spins(bool paravirt)
@@ -380,8 +386,13 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 
 		iters++;
 
Quoted 7 lines

arch/powerpc/lib/qspinlock.c static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav · line 391

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +2 -1
-		if (iters >= get_steal_spins(paravirt))
+		if (iters >= get_steal_spins(paravirt, false))
 			break;
+		if (iters >= get_steal_spins(paravirt, true)) {

There's no indication of what true and false mean here which is hard to read. To me it feels like two separate functions would be more clear.

Good point. I reworked this a bit.

Quoted 6 lines

arch/powerpc/lib/qspinlock.c static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav · line 393

Quoted 4 lines
arch/powerpc/lib/qspinlock.c +2 -0
+			int cpu = get_owner_cpu(val);
+			if (numa_node_id() != cpu_to_node(cpu))

What about using node_distance() instead?

We don't really need the distance, just whether or not it's the same or not. I think distance not only has to look up the nodes, but then has to look up a matrix to get a number.

Thanks,
Nick

author reviewer
#73 lore raw

Re: [PATCH 16/17] powerpc/qspinlock: allow indefinite spinning on a preempted owner

In reply to Jordan Niethe
Quoted 53 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 51 lines

Provide an option that holds off queueing indefinitely while the lock owner is preempted. This could reduce queueing latencies for very overcommitted vcpu situations.

This is disabled by default.

---
 arch/powerpc/lib/qspinlock.c | 91 +++++++++++++++++++++++++++++++-----
 1 file changed, 79 insertions(+), 12 deletions(-)
arch/powerpc/lib/qspinlock.c +6 -1
index 24f68bd71e2b..5cfd69931e31 100644
@@ -35,6 +35,7 @@ static int HEAD_SPINS __read_mostly = (1<<8);
 
 static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
+static bool pv_spin_on_preempted_owner __read_mostly = false;
 static bool pv_yield_prev __read_mostly = true;
 static bool pv_yield_propagate_owner __read_mostly = true;
 static bool pv_prod_head __read_mostly = false;
@@ -220,13 +221,15 @@ static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 	BUG();
 }
 
-static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq)
+static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool clear_mustq, bool *preempted)
 {
 	int owner;
 	u32 yield_count;
 
 	BUG_ON(!(val & _Q_LOCKED_VAL));
 
+	*preempted = false;
+
 	if (!paravirt)
 		goto relax;
 
@@ -241,6 +244,8 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 
 	spin_end();
 
+	*preempted = true;
+
 	/*
 	 * Read the lock word after sampling the yield count. On the other side
 	 * there may a wmb because the yield count update is done by the
@@ -265,14 +270,14 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 	spin_cpu_relax();
 }
 
Quoted 6 lines

arch/powerpc/lib/qspinlock.c static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32 · line 273

Quoted 2 lines
arch/powerpc/lib/qspinlock.c +1 -1
-static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
+static __always_inline void yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool *preempted)

It seems like preempted parameter could be the return value of yield_to_locked_owner(). Then callers that don't use the value returned in preempted don't need to create an unnecessary variable to pass in.

That works.

Thanks,
Nick

author reviewer
#77 lore raw

Re: [PATCH 17/17] powerpc/qspinlock: provide accounting and options for sleepy locks

In reply to Jordan Niethe
Quoted 49 lines · Jordan Niethe

On Thu, 2022-07-28 at 16:31 +1000, Nicholas Piggin wrote:
[resend as utf-8, not utf-7]

Quoted 47 lines

Finding the owner or a queued waiter on a lock with a preempted vcpu is indicative of an oversubscribed guest causing the lock to get into trouble. Provide some options to detect this situation and have new CPUs avoid queueing for a longer time (more steal iterations) to minimise the problems caused by vcpu preemption on the queue.

---
 arch/powerpc/include/asm/qspinlock_types.h |   7 +-
 arch/powerpc/lib/qspinlock.c               | 240 +++++++++++++++++++--
 2 files changed, 232 insertions(+), 15 deletions(-)
arch/powerpc/include/asm/qspinlock_types.h +6 -1
index 35f9525381e6..4fbcc8a4230b 100644
@@ -30,7 +30,7 @@ typedef struct qspinlock {
  *
  *     0: locked bit
  *  1-14: lock holder cpu
- *    15: unused bit
+ *    15: lock owner or queuer vcpus observed to be preempted bit
  *    16: must queue bit
  * 17-31: tail cpu (+1)
  */
@@ -49,6 +49,11 @@ typedef struct qspinlock {
 #error "qspinlock does not support such large CONFIG_NR_CPUS"
 #endif
 
+#define _Q_SLEEPY_OFFSET	15
+#define _Q_SLEEPY_BITS		1
+#define _Q_SLEEPY_MASK		_Q_SET_MASK(SLEEPY_OWNER)
+#define _Q_SLEEPY_VAL		(1U << _Q_SLEEPY_OFFSET)
+
 #define _Q_MUST_Q_OFFSET	16
 #define _Q_MUST_Q_BITS		1
 #define _Q_MUST_Q_MASK		_Q_SET_MASK(MUST_Q)
arch/powerpc/lib/qspinlock.c +1 -0
index 5cfd69931e31..c18133c01450 100644
@@ -5,6 +5,7 @@
 #include <linux/percpu.h>
 #include <linux/smp.h>
 #include <linux/topology.h>
+#include <linux/sched/clock.h>
 #include <asm/qspinlock.h>
 #include <asm/paravirt.h>
 
Quoted 8 lines

arch/powerpc/lib/qspinlock.c static int HEAD_SPINS __read_mostly = (1<<8); · line 41

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +2 -0
@@ -36,24 +37,54 @@ static int HEAD_SPINS __read_mostly = (1<<8);
 static bool pv_yield_owner __read_mostly = true;
 static bool pv_yield_allow_steal __read_mostly = false;
 static bool pv_spin_on_preempted_owner __read_mostly = false;
+static bool pv_sleepy_lock __read_mostly = true;
+static bool pv_sleepy_lock_sticky __read_mostly = false;

The sticky part could potentially be its own patch.

I'll see how that looks.

Quoted 6 lines
Quoted 6 lines
arch/powerpc/lib/qspinlock.c +2 -0
+static u64 pv_sleepy_lock_interval_ns __read_mostly = 0;
+static int pv_sleepy_lock_factor __read_mostly = 256;
 static bool pv_yield_prev __read_mostly = true;
 static bool pv_yield_propagate_owner __read_mostly = true;
 static bool pv_prod_head __read_mostly = false;
 
Quoted 8 lines

arch/powerpc/lib/qspinlock.c static int HEAD_SPINS __read_mostly = (1<<8); · line 52

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +3 -1
 static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
+static DEFINE_PER_CPU_ALIGNED(u64, sleepy_lock_seen_clock);
 
-static __always_inline int get_steal_spins(bool paravirt, bool remote)
+static __always_inline bool recently_sleepy(void)
+{

Other users of pv_sleepy_lock_interval_ns first check pv_sleepy_lock.

In this case it should be implied, I've added a comment.

Quoted 39 lines
Quoted 6 lines
arch/powerpc/lib/qspinlock.c +5 -0
+	if (pv_sleepy_lock_interval_ns) {
+		u64 seen = this_cpu_read(sleepy_lock_seen_clock);
+
+		if (seen) {
+			u64 delta = sched_clock() - seen;

arch/powerpc/lib/qspinlock.c static int HEAD_SPINS __read_mostly = (1<<8); · line 67

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +10 -0
+			if (delta < pv_sleepy_lock_interval_ns)
+				return true;
+			this_cpu_write(sleepy_lock_seen_clock, 0);
+		}
+	}
+
+	return false;
+}
+
+static __always_inline int get_steal_spins(bool paravirt, bool remote, bool sleepy)

It seems like paravirt is implied by sleepy.

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +2 -1
 {
 	if (remote) {
-		return REMOTE_STEAL_SPINS;
+		if (paravirt && sleepy)
+			return REMOTE_STEAL_SPINS * pv_sleepy_lock_factor;

arch/powerpc/lib/qspinlock.c static int HEAD_SPINS __read_mostly = (1<<8); · line 80

Quoted 10 lines
arch/powerpc/lib/qspinlock.c +6 -1
+		else
+			return REMOTE_STEAL_SPINS;
 	} else {
-		return STEAL_SPINS;
+		if (paravirt && sleepy)
+			return STEAL_SPINS * pv_sleepy_lock_factor;
+		else
+			return STEAL_SPINS;
 	}
 }

I think that separate functions would still be nicer but this could get rid of the nesting conditionals like

Quoted 9 lines
	int spins;
	if (remote)
		spins = REMOTE_STEAL_SPINS;
	else
		spins = STEAL_SPINS;

	if (sleepy)
		return spins * pv_sleepy_lock_factor;
	return spins;

Yeah it was getting a bit out of hand.

Quoted 54 lines
Quoted 54 lines
arch/powerpc/lib/qspinlock.c +42 -2
-static __always_inline int get_head_spins(bool paravirt)
+static __always_inline int get_head_spins(bool paravirt, bool sleepy)
 {
-	return HEAD_SPINS;
+	if (paravirt && sleepy)
+		return HEAD_SPINS * pv_sleepy_lock_factor;
+	else
+		return HEAD_SPINS;
 }
 
 static inline u32 encode_tail_cpu(void)
@@ -206,6 +237,60 @@ static __always_inline u32 lock_clear_mustq(struct qspinlock *lock)
 	return prev;
 }
 
+static __always_inline bool lock_try_set_sleepy(struct qspinlock *lock, u32 old)
+{
+	u32 prev;
+	u32 new = old | _Q_SLEEPY_VAL;
+
+	BUG_ON(!(old & _Q_LOCKED_VAL));
+	BUG_ON(old & _Q_SLEEPY_VAL);
+
+	asm volatile(
+"1:	lwarx	%0,0,%1		# lock_try_set_sleepy			\n"
+"	cmpw	0,%0,%2							\n"
+"	bne-	2f							\n"
+"	stwcx.	%3,0,%1							\n"
+"	bne-	1b							\n"
+"2:									\n"
+	: "=&r" (prev)
+	: "r" (&lock->val), "r"(old), "r" (new)
+	: "cr0", "memory");
+
+	if (prev == old)
+		return true;
+	return false;
+}
+
+static __always_inline void seen_sleepy_owner(struct qspinlock *lock, u32 val)
+{
+	if (pv_sleepy_lock) {
+		if (pv_sleepy_lock_interval_ns)
+			this_cpu_write(sleepy_lock_seen_clock, sched_clock());
+		if (!(val & _Q_SLEEPY_VAL))
+			lock_try_set_sleepy(lock, val);
+	}
+}
+
+static __always_inline void seen_sleepy_lock(void)
+{
+	if (pv_sleepy_lock && pv_sleepy_lock_interval_ns)
Quoted 8 lines

arch/powerpc/lib/qspinlock.c static __always_inline u32 lock_clear_mustq(struct qspinlock *lock) · line 281

Quoted 5 lines
arch/powerpc/lib/qspinlock.c +5 -0
+		this_cpu_write(sleepy_lock_seen_clock, sched_clock());
+}
+
+static __always_inline void seen_sleepy_node(struct qspinlock *lock)
+{

If yield_to_prev() was made to take a raw val, that val could be passed to seen_sleepy_node() and it would not need to get it by itself.

Yep.

Quoted 89 lines
Quoted 89 lines
arch/powerpc/lib/qspinlock.c +25 -3
+	if (pv_sleepy_lock) {
+		u32 val = READ_ONCE(lock->val);
+
+		if (pv_sleepy_lock_interval_ns)
+			this_cpu_write(sleepy_lock_seen_clock, sched_clock());
+		if (val & _Q_LOCKED_VAL) {
+			if (!(val & _Q_SLEEPY_VAL))
+				lock_try_set_sleepy(lock, val);
+		}
+	}
+}
+
 static struct qnode *get_tail_qnode(struct qspinlock *lock, u32 val)
 {
 	int cpu = get_tail_cpu(val);
@@ -244,6 +329,7 @@ static __always_inline void __yield_to_locked_owner(struct qspinlock *lock, u32
 
 	spin_end();
 
+	seen_sleepy_owner(lock, val);
 	*preempted = true;
 
 	/*
@@ -307,11 +393,13 @@ static __always_inline void propagate_yield_cpu(struct qnode *node, u32 val, int
 	}
 }
 
-static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
+static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt, bool *preempted)
 {
 	u32 yield_count;
 	int yield_cpu;
 
+	*preempted = false;
+
 	if (!paravirt)
 		goto relax;
 
@@ -332,6 +420,9 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 
 	spin_end();
 
+	*preempted = true;
+	seen_sleepy_node(lock);
+
 	smp_rmb();
 
 	if (yield_cpu == node->yield_cpu) {
@@ -353,6 +444,9 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 
 	spin_end();
 
+	*preempted = true;
+	seen_sleepy_node(lock);
+
 	smp_rmb(); /* See yield_to_locked_owner comment */
 
 	if (!node->locked) {
@@ -369,6 +463,9 @@ static __always_inline void yield_to_prev(struct qspinlock *lock, struct qnode *
 
 static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
 {
+	bool preempted;
+	bool seen_preempted = false;
+	bool sleepy = false;
 	int iters = 0;
 
 	if (!STEAL_SPINS) {
@@ -376,7 +473,6 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 			spin_begin();
 			for (;;) {
 				u32 val = READ_ONCE(lock->val);
-				bool preempted;
 
 				if (val & _Q_MUST_Q_VAL)
 					break;
@@ -395,7 +491,6 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 	spin_begin();
 	for (;;) {
 		u32 val = READ_ONCE(lock->val);
-		bool preempted;
 
 		if (val & _Q_MUST_Q_VAL)
 			break;
@@ -408,9 +503,29 @@ static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav
 			continue;
 		}
 
Quoted 6 lines

arch/powerpc/lib/qspinlock.c static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav · line 507

Quoted 2 lines
arch/powerpc/lib/qspinlock.c +2 -0
+		if (paravirt && pv_sleepy_lock && !sleepy) {
+			if (!sleepy) {

The enclosing conditional means this would always be true. I think the out conditional should be if (paravirt && pv_sleepy_lock)
otherwise the pv_sleepy_lock_sticky part wouldn't work properly.

Good catch, I think you're right.

Quoted 13 lines
Quoted 13 lines
arch/powerpc/lib/qspinlock.c +11 -0
+				if (val & _Q_SLEEPY_VAL) {
+					seen_sleepy_lock();
+					sleepy = true;
+				} else if (recently_sleepy()) {
+					sleepy = true;
+				}
+
+			if (pv_sleepy_lock_sticky && seen_preempted &&
+					!(val & _Q_SLEEPY_VAL)) {
+				if (lock_try_set_sleepy(lock, val))
+					val |= _Q_SLEEPY_VAL;
Quoted 8 lines

arch/powerpc/lib/qspinlock.c static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool parav · line 524

Quoted 6 lines
arch/powerpc/lib/qspinlock.c +5 -0
+			}
+
+
 		yield_to_locked_owner(lock, val, paravirt, &preempted);
+		if (preempted)
+			seen_preempted = true;

This could belong to the next if statement, there can not be !paravirt && preempted ?

Yep.

Thanks,
Nick

author reviewer
#6 lore raw

Re: [PATCH 01/17] powerpc/qspinlock: powerpc qspinlock implementation

In reply to Christophe Leroy
Quoted 20 lines · Christophe Leroy

Le 10/11/2022 à 01:35, Jordan Niethe a écrit :

Quoted 16 lines · Nicholas Piggin

<snip>

arch/powerpc/include/asm/qspinlock.h line 28

Quoted 12 lines
arch/powerpc/include/asm/qspinlock.h +4 -6
-#define queued_spin_lock queued_spin_lock
 
-static inline void queued_spin_unlock(struct qspinlock *lock)
+static __always_inline int queued_spin_trylock(struct qspinlock *lock)
  {
-	if (!IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) || !is_shared_processor())
-		smp_store_release(&lock->locked, 0);
-	else
-		__pv_queued_spin_unlock(lock);
+	if (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0)
+		return 1;
+	return 0;

optional style nit: return (atomic_cmpxchg_acquire(&lock->val, 0, 1) == 0);

Quoted 6 lines

No parenthesis.
No == 0

Should be :

	return !atomic_cmpxchg_acquire(&lock->val, 0, 1);

In this case I prefer the == 0 because we're testing against the 0 old parameter being passed in. This is the recognisable cmpxchg pattern.

The other style of cmpxchg returns true if it succeeded, so it's less clear we're not using that version if using !.

Thanks,
Nick

author reviewer
#21 lore raw

Re: [PATCH 04/17] powerpc/qspinlock: convert atomic operations to assembly

In reply to Christophe Leroy
Quoted 15 lines · Christophe Leroy

Le 10/11/2022 à 01:39, Jordan Niethe a écrit :

Quoted 11 lines

arch/powerpc/lib/qspinlock.c struct qnodes { · line 59

Quoted 9 lines
arch/powerpc/lib/qspinlock.c +5 -3
+static __always_inline int trylock_clear_tail_cpu(struct qspinlock *lock, u32 old)
  {
-	int newval = _Q_LOCKED_VAL;
-
-	if (atomic_cmpxchg_acquire(&lock->val, val, newval) == val)
+	u32 new = _Q_LOCKED_VAL;
+	u32 prev;
+
+	BUG_ON(old & _Q_LOCKED_VAL);

The BUG_ON() could have been introduced in an earlier patch I think.

Quoted 1 line

Can we avoid the BUG_ON() at all and replace by a WARN_ON ?

Lock has gone wrong here. Critical sections not working means data corruption and little prospect of continuing to run.

Thanks,
Nick