Skip to contentSkip to conversation

csky: Move to generic ticket-spinlock

PATCH In review [email protected] 3 participants 6 messages Mar 18, 2022 – Mar 22, 2022

Subsystems (3)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 1/1 csky: Move to generic ticket-spinlock

From: Guo Ren <[email protected]>

There is no benefit from custom implementation for ticket-spinlock, so move to generic ticket-spinlock for easy maintenance.

Cc Palmer Dabbelt <[email protected]>
---
 arch/csky/include/asm/Kbuild           |  2 +
 arch/csky/include/asm/spinlock.h       | 82 +-------------------------
 arch/csky/include/asm/spinlock_types.h | 20 +------
 3 files changed, 4 insertions(+), 100 deletions(-)
arch/csky/include/asm/Kbuild +2 -0
index 904a18a818be..d94434288c31 100644
@@ -3,6 +3,8 @@ generic-y += asm-offsets.h
 generic-y += extable.h
 generic-y += gpio.h
 generic-y += kvm_para.h
+generic-y += ticket-lock.h
+generic-y += ticket-lock-types.h
 generic-y += qrwlock.h
 generic-y += user.h
 generic-y += vmlinux.lds.h
arch/csky/include/asm/spinlock.h +1 -81

88 lines not on this page · raw message

arch/csky/include/asm/spinlock_types.h +1 -19
index db87a12c3827..0bb7f6022a3b 100644
@@ -3,25 +3,7 @@
 #ifndef __ASM_CSKY_SPINLOCK_TYPES_H
 #define __ASM_CSKY_SPINLOCK_TYPES_H
 
-#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
-# error "please don't include this file directly"
-#endif
-
-#define TICKET_NEXT	16
-
-typedef struct {
-	union {
-		u32 lock;
-		struct __raw_tickets {
-			/* little endian */
-			u16 owner;
-			u16 next;
-		} tickets;
-	};
-} arch_spinlock_t;
-
-#define __ARCH_SPIN_LOCK_UNLOCKED	{ { 0 } }
-
+#include <asm/ticket-lock-types.h>
 #include <asm-generic/qrwlock_types.h>
 
 #endif /* __ASM_CSKY_SPINLOCK_TYPES_H */
Signature · 1 line
2.25.1
maintainer
#1 lore raw

arch/csky/include/asm/Kbuild generic-y += asm-offsets.h · line 10

Quoted 9 lines · On Fri, Mar 18, 2022 at 9:34 AM <[email protected]> wrote:
arch/csky/include/asm/Kbuild +2 -0
@@ -3,6 +3,8 @@ generic-y += asm-offsets.h
 generic-y += extable.h
 generic-y += gpio.h
 generic-y += kvm_para.h
+generic-y += ticket-lock.h
+generic-y += ticket-lock-types.h
 generic-y += qrwlock.h
 generic-y += user.h
 generic-y += vmlinux.lds.h

If these headers are not included from architecture-independent code, they should
not be marked as generic-y, same as for the qrwlock.h header

arch/csky/include/asm/spinlock.h

Quoted 7 lines
arch/csky/include/asm/spinlock.h +2 -0
+#include <asm/ticket-lock.h>
 #include <asm/qrwlock.h>
 
...
+#include <asm/ticket-lock-types.h>
 #include <asm-generic/qrwlock_types.h>

So these should all become

#include <asm-generic/...h>

It would however make sense to have the trivial two-line version of the two header files and put them into asm-generic/spinlock.h and asm-generic/spinlock-types.h, replacing the current version.

If you do that, then you need a 'generic-y' line for spinlock.h, but not for the other ones.

Arnd

author maintainer
#2 lore raw

Hi Arnd,

Quoted 26 lines · Arnd Bergmann

arch/csky/include/asm/Kbuild generic-y += asm-offsets.h · line 10

Quoted 9 lines · On Fri, Mar 18, 2022 at 9:34 AM <[email protected]> wrote:
arch/csky/include/asm/Kbuild +2 -0
@@ -3,6 +3,8 @@ generic-y += asm-offsets.h
 generic-y += extable.h
 generic-y += gpio.h
 generic-y += kvm_para.h
+generic-y += ticket-lock.h
+generic-y += ticket-lock-types.h
 generic-y += qrwlock.h
 generic-y += user.h
 generic-y += vmlinux.lds.h

If these headers are not included from architecture-independent code, they should
not be marked as generic-y, same as for the qrwlock.h header

arch/csky/include/asm/spinlock.h

Quoted 7 lines
arch/csky/include/asm/spinlock.h +2 -0
+#include <asm/ticket-lock.h>
 #include <asm/qrwlock.h>
 
...
+#include <asm/ticket-lock-types.h>
 #include <asm-generic/qrwlock_types.h>

So these should all become

Quoted 8 lines

#include <asm-generic/...h>

It would however make sense to have the trivial two-line version of the two header files and put them into asm-generic/spinlock.h and asm-generic/spinlock-types.h, replacing the current version.

If you do that, then you need a 'generic-y' line for spinlock.h, but not for the other ones.

Thx for the suggestion, I've sent V2 based on Palmer's series to make the ticket spinlock more generic.
Please have a look:
https://lore.kernel.org/linux-arch/[email protected]/T/#t

Quoted 2 lines

Arnd

--
Best Regards
Guo Ren

ML: https://lore.kernel.org/linux-csky/

#3 lore raw
In reply to [email protected]
Quoted 7 lines · On Fri, 18 Mar 2022 01:34:21 PDT (-0700), [email protected] wrote:

From: Guo Ren <[email protected]>

There is no benefit from custom implementation for ticket-spinlock, so move to generic ticket-spinlock for easy maintenance.

Cc Palmer Dabbelt <[email protected]>

Thanks, one less port to look at ;)

Looks like there were a few comments on the v1, and I wasn't going to target this at the upcoming merge window anyway because I wanted to give the various RISC-V vendors time to test stuff. LMK if you want me to add this to the others, but I was planning on posting a stable tag either way so no big deal on my end.

Quoted 143 lines
---
 arch/csky/include/asm/Kbuild           |  2 +
 arch/csky/include/asm/spinlock.h       | 82 +-------------------------
 arch/csky/include/asm/spinlock_types.h | 20 +------
 3 files changed, 4 insertions(+), 100 deletions(-)
arch/csky/include/asm/Kbuild +2 -0
index 904a18a818be..d94434288c31 100644
@@ -3,6 +3,8 @@ generic-y += asm-offsets.h
 generic-y += extable.h
 generic-y += gpio.h
 generic-y += kvm_para.h
+generic-y += ticket-lock.h
+generic-y += ticket-lock-types.h
 generic-y += qrwlock.h
 generic-y += user.h
 generic-y += vmlinux.lds.h
arch/csky/include/asm/spinlock.h +1 -81
index 69f5aa249c5f..8bc179ba0d8d 100644
@@ -3,87 +3,7 @@
 #ifndef __ASM_CSKY_SPINLOCK_H
 #define __ASM_CSKY_SPINLOCK_H
 
-#include <linux/spinlock_types.h>
-#include <asm/barrier.h>
-
-/*
- * Ticket-based spin-locking.
- */
-static inline void arch_spin_lock(arch_spinlock_t *lock)
-{
-	arch_spinlock_t lockval;
-	u32 ticket_next = 1 << TICKET_NEXT;
-	u32 *p = &lock->lock;
-	u32 tmp;
-
-	asm volatile (
-		"1:	ldex.w		%0, (%2) \n"
-		"	mov		%1, %0	 \n"
-		"	add		%0, %3	 \n"
-		"	stex.w		%0, (%2) \n"
-		"	bez		%0, 1b   \n"
-		: "=&r" (tmp), "=&r" (lockval)
-		: "r"(p), "r"(ticket_next)
-		: "cc");
-
-	while (lockval.tickets.next != lockval.tickets.owner)
-		lockval.tickets.owner = READ_ONCE(lock->tickets.owner);
-
-	smp_mb();
-}
-
-static inline int arch_spin_trylock(arch_spinlock_t *lock)
-{
-	u32 tmp, contended, res;
-	u32 ticket_next = 1 << TICKET_NEXT;
-	u32 *p = &lock->lock;
-
-	do {
-		asm volatile (
-		"	ldex.w		%0, (%3)   \n"
-		"	movi		%2, 1	   \n"
-		"	rotli		%1, %0, 16 \n"
-		"	cmpne		%1, %0     \n"
-		"	bt		1f         \n"
-		"	movi		%2, 0	   \n"
-		"	add		%0, %0, %4 \n"
-		"	stex.w		%0, (%3)   \n"
-		"1:				   \n"
-		: "=&r" (res), "=&r" (tmp), "=&r" (contended)
-		: "r"(p), "r"(ticket_next)
-		: "cc");
-	} while (!res);
-
-	if (!contended)
-		smp_mb();
-
-	return !contended;
-}
-
-static inline void arch_spin_unlock(arch_spinlock_t *lock)
-{
-	smp_mb();
-	WRITE_ONCE(lock->tickets.owner, lock->tickets.owner + 1);
-}
-
-static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
-{
-	return lock.tickets.owner == lock.tickets.next;
-}
-
-static inline int arch_spin_is_locked(arch_spinlock_t *lock)
-{
-	return !arch_spin_value_unlocked(READ_ONCE(*lock));
-}
-
-static inline int arch_spin_is_contended(arch_spinlock_t *lock)
-{
-	struct __raw_tickets tickets = READ_ONCE(lock->tickets);
-
-	return (tickets.next - tickets.owner) > 1;
-}
-#define arch_spin_is_contended	arch_spin_is_contended
-
+#include <asm/ticket-lock.h>
 #include <asm/qrwlock.h>
 
 #endif /* __ASM_CSKY_SPINLOCK_H */
arch/csky/include/asm/spinlock_types.h +1 -19
index db87a12c3827..0bb7f6022a3b 100644
@@ -3,25 +3,7 @@
 #ifndef __ASM_CSKY_SPINLOCK_TYPES_H
 #define __ASM_CSKY_SPINLOCK_TYPES_H
 
-#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
-# error "please don't include this file directly"
-#endif
-
-#define TICKET_NEXT	16
-
-typedef struct {
-	union {
-		u32 lock;
-		struct __raw_tickets {
-			/* little endian */
-			u16 owner;
-			u16 next;
-		} tickets;
-	};
-} arch_spinlock_t;
-
-#define __ARCH_SPIN_LOCK_UNLOCKED	{ { 0 } }
-
+#include <asm/ticket-lock-types.h>
 #include <asm-generic/qrwlock_types.h>
 
 #endif /* __ASM_CSKY_SPINLOCK_TYPES_H */
author maintainer
#4 lore raw

Hi Palmer,

Quoted 7 lines · Palmer Dabbelt
Quoted 5 lines · On Fri, 18 Mar 2022 01:34:21 PDT (-0700), [email protected] wrote:

From: Guo Ren <[email protected]>

There is no benefit from custom implementation for ticket-spinlock, so move to generic ticket-spinlock for easy maintenance.

Quoted 7 lines
Quoted 2 lines
Cc Palmer Dabbelt <[email protected]>

Thanks, one less port to look at ;)

Looks like there were a few comments on the v1, and I wasn't going to target this at the upcoming merge window anyway because I wanted to give

Agree, we needn't so hurry.

Quoted 3 lines

the various RISC-V vendors time to test stuff. LMK if you want me to add this to the others, but I was planning on posting a stable tag either way so no big deal on my end.

Yes, I hope csky's could be in the series. And I updated V2 with Arnd's suggestion, please have a look:
https://lore.kernel.org/linux-arch/[email protected]/T/#t

Quoted 144 lines
Quoted 144 lines
---
 arch/csky/include/asm/Kbuild           |  2 +
 arch/csky/include/asm/spinlock.h       | 82 +-------------------------
 arch/csky/include/asm/spinlock_types.h | 20 +------
 3 files changed, 4 insertions(+), 100 deletions(-)
arch/csky/include/asm/Kbuild +2 -0
index 904a18a818be..d94434288c31 100644
@@ -3,6 +3,8 @@ generic-y += asm-offsets.h
 generic-y += extable.h
 generic-y += gpio.h
 generic-y += kvm_para.h
+generic-y += ticket-lock.h
+generic-y += ticket-lock-types.h
 generic-y += qrwlock.h
 generic-y += user.h
 generic-y += vmlinux.lds.h
arch/csky/include/asm/spinlock.h +1 -81
index 69f5aa249c5f..8bc179ba0d8d 100644
@@ -3,87 +3,7 @@
 #ifndef __ASM_CSKY_SPINLOCK_H
 #define __ASM_CSKY_SPINLOCK_H
 
-#include <linux/spinlock_types.h>
-#include <asm/barrier.h>
-
-/*
- * Ticket-based spin-locking.
- */
-static inline void arch_spin_lock(arch_spinlock_t *lock)
-{
-     arch_spinlock_t lockval;
-     u32 ticket_next = 1 << TICKET_NEXT;
-     u32 *p = &lock->lock;
-     u32 tmp;
-
-     asm volatile (
-             "1:     ldex.w          %0, (%2) \n"
-             "       mov             %1, %0   \n"
-             "       add             %0, %3   \n"
-             "       stex.w          %0, (%2) \n"
-             "       bez             %0, 1b   \n"
-             : "=&r" (tmp), "=&r" (lockval)
-             : "r"(p), "r"(ticket_next)
-             : "cc");
-
-     while (lockval.tickets.next != lockval.tickets.owner)
-             lockval.tickets.owner = READ_ONCE(lock->tickets.owner);
-
-     smp_mb();
-}
-
-static inline int arch_spin_trylock(arch_spinlock_t *lock)
-{
-     u32 tmp, contended, res;
-     u32 ticket_next = 1 << TICKET_NEXT;
-     u32 *p = &lock->lock;
-
-     do {
-             asm volatile (
-             "       ldex.w          %0, (%3)   \n"
-             "       movi            %2, 1      \n"
-             "       rotli           %1, %0, 16 \n"
-             "       cmpne           %1, %0     \n"
-             "       bt              1f         \n"
-             "       movi            %2, 0      \n"
-             "       add             %0, %0, %4 \n"
-             "       stex.w          %0, (%3)   \n"
-             "1:                                \n"
-             : "=&r" (res), "=&r" (tmp), "=&r" (contended)
-             : "r"(p), "r"(ticket_next)
-             : "cc");
-     } while (!res);
-
-     if (!contended)
-             smp_mb();
-
-     return !contended;
-}
-
-static inline void arch_spin_unlock(arch_spinlock_t *lock)
-{
-     smp_mb();
-     WRITE_ONCE(lock->tickets.owner, lock->tickets.owner + 1);
-}
-
-static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
-{
-     return lock.tickets.owner == lock.tickets.next;
-}
-
-static inline int arch_spin_is_locked(arch_spinlock_t *lock)
-{
-     return !arch_spin_value_unlocked(READ_ONCE(*lock));
-}
-
-static inline int arch_spin_is_contended(arch_spinlock_t *lock)
-{
-     struct __raw_tickets tickets = READ_ONCE(lock->tickets);
-
-     return (tickets.next - tickets.owner) > 1;
-}
-#define arch_spin_is_contended       arch_spin_is_contended
-
+#include <asm/ticket-lock.h>
 #include <asm/qrwlock.h>
 
 #endif /* __ASM_CSKY_SPINLOCK_H */
arch/csky/include/asm/spinlock_types.h +1 -19
index db87a12c3827..0bb7f6022a3b 100644
@@ -3,25 +3,7 @@
 #ifndef __ASM_CSKY_SPINLOCK_TYPES_H
 #define __ASM_CSKY_SPINLOCK_TYPES_H
 
-#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
-# error "please don't include this file directly"
-#endif
-
-#define TICKET_NEXT  16
-
-typedef struct {
-     union {
-             u32 lock;
-             struct __raw_tickets {
-                     /* little endian */
-                     u16 owner;
-                     u16 next;
-             } tickets;
-     };
-} arch_spinlock_t;
-
-#define __ARCH_SPIN_LOCK_UNLOCKED    { { 0 } }
-
+#include <asm/ticket-lock-types.h>
 #include <asm-generic/qrwlock_types.h>
 
 #endif /* __ASM_CSKY_SPINLOCK_TYPES_H */
Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
#5 lore raw
Quoted 17 lines · On Fri, 18 Mar 2022 21:01:49 PDT (-0700), [email protected] wrote:

Hi Palmer,

Quoted 7 lines · Palmer Dabbelt
Quoted 5 lines · On Fri, 18 Mar 2022 01:34:21 PDT (-0700), [email protected] wrote:

From: Guo Ren <[email protected]>

There is no benefit from custom implementation for ticket-spinlock, so move to generic ticket-spinlock for easy maintenance.

Quoted 7 lines
Quoted 2 lines
Cc Palmer Dabbelt <[email protected]>

Thanks, one less port to look at ;)

Looks like there were a few comments on the v1, and I wasn't going to target this at the upcoming merge window anyway because I wanted to give

Quoted 8 lines

Agree, we needn't so hurry.

Quoted 3 lines

the various RISC-V vendors time to test stuff. LMK if you want me to add this to the others, but I was planning on posting a stable tag either way so no big deal on my end.

Yes, I hope csky's could be in the series. And I updated V2 with Arnd's suggestion, please have a look:
https://lore.kernel.org/linux-arch/[email protected]/T/#t

Thanks, I'll include the csky stuff next time I re-spin this.

Looks like there's been some other comments from folks that didn't make your v2, most notably the SOB lines which makes this a bit funny. I'm also not sure I like merging the two RISC-V patches together, as they'd be nice to be able to test on their own.

I'll go poke around with this some once I'm a bit father into the merge window. I think we can both keep Arnd's generic header idea and let folks mix/match spin/rw lock flavors, which might also help with some of the other ports.

Quoted 145 lines
Quoted 145 lines
Quoted 145 lines

---
 arch/csky/include/asm/Kbuild           |  2 +
 arch/csky/include/asm/spinlock.h       | 82 +-------------------------
 arch/csky/include/asm/spinlock_types.h | 20 +------
 3 files changed, 4 insertions(+), 100 deletions(-)
arch/csky/include/asm/Kbuild +2 -0
index 904a18a818be..d94434288c31 100644
@@ -3,6 +3,8 @@ generic-y += asm-offsets.h
 generic-y += extable.h
 generic-y += gpio.h
 generic-y += kvm_para.h
+generic-y += ticket-lock.h
+generic-y += ticket-lock-types.h
 generic-y += qrwlock.h
 generic-y += user.h
 generic-y += vmlinux.lds.h
arch/csky/include/asm/spinlock.h +1 -81
index 69f5aa249c5f..8bc179ba0d8d 100644
@@ -3,87 +3,7 @@
 #ifndef __ASM_CSKY_SPINLOCK_H
 #define __ASM_CSKY_SPINLOCK_H
 
-#include <linux/spinlock_types.h>
-#include <asm/barrier.h>
-
-/*
- * Ticket-based spin-locking.
- */
-static inline void arch_spin_lock(arch_spinlock_t *lock)
-{
-     arch_spinlock_t lockval;
-     u32 ticket_next = 1 << TICKET_NEXT;
-     u32 *p = &lock->lock;
-     u32 tmp;
-
-     asm volatile (
-             "1:     ldex.w          %0, (%2) \n"
-             "       mov             %1, %0   \n"
-             "       add             %0, %3   \n"
-             "       stex.w          %0, (%2) \n"
-             "       bez             %0, 1b   \n"
-             : "=&r" (tmp), "=&r" (lockval)
-             : "r"(p), "r"(ticket_next)
-             : "cc");
-
-     while (lockval.tickets.next != lockval.tickets.owner)
-             lockval.tickets.owner = READ_ONCE(lock->tickets.owner);
-
-     smp_mb();
-}
-
-static inline int arch_spin_trylock(arch_spinlock_t *lock)
-{
-     u32 tmp, contended, res;
-     u32 ticket_next = 1 << TICKET_NEXT;
-     u32 *p = &lock->lock;
-
-     do {
-             asm volatile (
-             "       ldex.w          %0, (%3)   \n"
-             "       movi            %2, 1      \n"
-             "       rotli           %1, %0, 16 \n"
-             "       cmpne           %1, %0     \n"
-             "       bt              1f         \n"
-             "       movi            %2, 0      \n"
-             "       add             %0, %0, %4 \n"
-             "       stex.w          %0, (%3)   \n"
-             "1:                                \n"
-             : "=&r" (res), "=&r" (tmp), "=&r" (contended)
-             : "r"(p), "r"(ticket_next)
-             : "cc");
-     } while (!res);
-
-     if (!contended)
-             smp_mb();
-
-     return !contended;
-}
-
-static inline void arch_spin_unlock(arch_spinlock_t *lock)
-{
-     smp_mb();
-     WRITE_ONCE(lock->tickets.owner, lock->tickets.owner + 1);
-}
-
-static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
-{
-     return lock.tickets.owner == lock.tickets.next;
-}
-
-static inline int arch_spin_is_locked(arch_spinlock_t *lock)
-{
-     return !arch_spin_value_unlocked(READ_ONCE(*lock));
-}
-
-static inline int arch_spin_is_contended(arch_spinlock_t *lock)
-{
-     struct __raw_tickets tickets = READ_ONCE(lock->tickets);
-
-     return (tickets.next - tickets.owner) > 1;
-}
-#define arch_spin_is_contended       arch_spin_is_contended
-
+#include <asm/ticket-lock.h>
 #include <asm/qrwlock.h>
 
 #endif /* __ASM_CSKY_SPINLOCK_H */
arch/csky/include/asm/spinlock_types.h +1 -19
index db87a12c3827..0bb7f6022a3b 100644
@@ -3,25 +3,7 @@
 #ifndef __ASM_CSKY_SPINLOCK_TYPES_H
 #define __ASM_CSKY_SPINLOCK_TYPES_H
 
-#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
-# error "please don't include this file directly"
-#endif
-
-#define TICKET_NEXT  16
-
-typedef struct {
-     union {
-             u32 lock;
-             struct __raw_tickets {
-                     /* little endian */
-                     u16 owner;
-                     u16 next;
-             } tickets;
-     };
-} arch_spinlock_t;
-
-#define __ARCH_SPIN_LOCK_UNLOCKED    { { 0 } }
-
+#include <asm/ticket-lock-types.h>
 #include <asm-generic/qrwlock_types.h>
 
 #endif /* __ASM_CSKY_SPINLOCK_TYPES_H */