From: Scott Wood Date: Fri, 07 Feb 2014 17:05:00 +0000 Subject: [Fwd: [PATCH v2] powerpc ticket locks] Message-Id: <1391792700.6733.299.camel@snotra.buserror.net> MIME-Version: 1 Content-Type: multipart/mixed; boundary="=-LKjCw78LMFVN+h3zSpkD" List-Id: To: kvm-ppc@vger.kernel.org --=-LKjCw78LMFVN+h3zSpkD Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit I guess we need to start looking at paravirt ticketlocks for PPC now... -Scott --=-LKjCw78LMFVN+h3zSpkD Content-Disposition: inline Content-Description: Forwarded message - [PATCH v2] powerpc ticket locks Content-Type: message/rfc822 MIME-Version: 1.0 Received: from outlook-namnortheast.office365.com [2a01:111:f400:878::6] by snotra.buserror.net with IMAP (fetchmail-6.3.26) for (single-drop); Fri, 07 Feb 2014 10:58:23 -0600 (CST) Received: from BY2PR03MB396.namprd03.prod.outlook.com (10.141.141.26) by DM2PR03MB398.namprd03.prod.outlook.com (10.141.84.140) with Microsoft SMTP Server (TLS) id 15.0.868.8 via Mailbox Transport; Fri, 7 Feb 2014 16:58:22 +0000 Received: from BY2PR03CA030.namprd03.prod.outlook.com (10.242.234.151) by BY2PR03MB396.namprd03.prod.outlook.com (10.141.141.26) with Microsoft SMTP Server (TLS) id 15.0.868.8; Fri, 7 Feb 2014 16:58:20 +0000 Received: from BY2FFO11FD018.protection.gbl (2a01:111:f400:7c0c::138) by BY2PR03CA030.outlook.office365.com (2a01:111:e400:2c2c::23) with Microsoft SMTP Server (TLS) id 15.0.868.8 via Frontend Transport; Fri, 7 Feb 2014 16:58:20 +0000 Received: from va3outboundpool.messaging.microsoft.com (216.32.180.12) by BY2FFO11FD018.mail.protection.outlook.com (10.1.14.106) with Microsoft SMTP Server (TLS) id 15.0.868.13 via Frontend Transport; Fri, 7 Feb 2014 16:58:20 +0000 Received: from mail113-va3-R.bigfish.com (10.7.14.253) by VA3EHSOBE010.bigfish.com (10.7.40.12) with Microsoft SMTP Server id 14.1.225.22; Fri, 7 Feb 2014 16:58:19 +0000 Received: from mail113-va3 (localhost [127.0.0.1]) by mail113-va3-R.bigfish.com (Postfix) with ESMTP id 1ADFB202BF for ; Fri, 7 Feb 2014 16:58:19 +0000 (UTC) X-Forefront-Antispam-Report-Untrusted: CIP:65.52.41.222;KIP:(null);UIP:(null);(null);H:mail.freescale.net;R:internal;EFV:INT X-SpamScore: 5 X-BigFish: VS5(zzc8kzz1f42h1d74h2148h208ch1ee6h1de0h1d18h1fdah2073h2146h1202h1e76h2189h1d1ah1d2ah21bch1fc6hzz1de098h8275bh1de097hz2dh87h2a8h65bh839h944hd25he5bhfa3h107ah11b5h121eh1220h1288h12a5h12a9h12bdh137ah13b6h13eah1441h14afh1504h1537h153bh162dh1631h1758h18e1h1946h19b5h1ad9h1b0ah1b2fh2222h224fh1fb3h1d0ch1d2eh1d3fh1dc1h1dfeh1dffh1e1dh1fe8h1ff5h209eh2216h22d0h2336h2438h2461h2487h24d7h2516h2545h5c5i2388i1155h) X-FFO-Routing-Override: onmicrosoft.com%pending-onmicrosoft.com; X-FB-DOMAIN-IP-MATCH: fail X-Forefront-Antispam-Report-Untrusted: CIP:213.95.11.211;KIP:(null);UIP:(null);IPV:NLI;H:newverein.lst.de;RD:verein.lst.de;EFVD:NLI Received: from mail113-va3 (localhost.localdomain [127.0.0.1]) by mail113-va3 (MessageSwitch) id 1391792296989764_5160; Fri, 7 Feb 2014 16:58:16 +0000 (UTC) Received: from VA3EHSMHS002.bigfish.com (unknown [10.7.14.236]) by mail113-va3.bigfish.com (Postfix) with ESMTP id D9D9730004D for ; Fri, 7 Feb 2014 16:58:16 +0000 (UTC) Received: from mail.freescale.net (65.52.41.222) by VA3EHSMHS002.bigfish.com (10.7.99.12) with Microsoft SMTP Server (TLS) id 14.16.227.3; Fri, 7 Feb 2014 16:58:09 +0000 Received: from mail24-ch1-R.bigfish.com (216.32.181.174) by mail.freescale.net (10.29.6.32) with Microsoft SMTP Server (TLS) id 14.3.158.2; Fri, 7 Feb 2014 16:58:07 +0000 Received: from mail24-ch1 (localhost [127.0.0.1]) by mail24-ch1-R.bigfish.com (Postfix) with ESMTP id 075DF3E0544 for ; Fri, 7 Feb 2014 16:58:08 +0000 (UTC) Received: from mail24-ch1 (localhost.localdomain [127.0.0.1]) by mail24-ch1 (MessageSwitch) id 1391792285404759_18023; Fri, 7 Feb 2014 16:58:05 +0000 (UTC) Received: from CH1EHSMHS034.bigfish.com (snatpool3.int.messaging.microsoft.com [10.43.68.229]) by mail24-ch1.bigfish.com (Postfix) with ESMTP id 54AA4440072 for ; Fri, 7 Feb 2014 16:58:05 +0000 (UTC) Received: from newverein.lst.de (213.95.11.211) by CH1EHSMHS034.bigfish.com (10.43.70.34) with Microsoft SMTP Server (TLS) id 14.16.227.3; Fri, 7 Feb 2014 16:58:03 +0000 Received: by newverein.lst.de (Postfix, from userid 2005) id 525D713E23; Fri, 7 Feb 2014 17:58:01 +0100 (CET) Date: Fri, 7 Feb 2014 17:58:01 +0100 From: Torsten Duwe To: Benjamin Herrenschmidt , Paul Mackerras , Anton Blanchard , "Paul E. McKenney" , Peter Zijlstra , "Scott Wood" , Tom Musta , "Ingo Molnar" CC: , Subject: [PATCH v2] powerpc ticket locks Message-ID: <20140207165801.GC2107@lst.de> Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline User-Agent: Mutt/1.5.17 (2007-11-01) Return-Path: duwe@lst.de X-FOPE-CONNECTOR: Id%0$Dn%*$RO%0$TLS%0$FQDN%$TlsDn% X-FOPE-CONNECTOR: Id%0$Dn%FREESCALE.MAIL.ONMICROSOFT.COM$RO%1$TLS%0$FQDN%$TlsDn% X-EOPAttributedMessage: 0 X-MS-Exchange-Organization-MessageDirectionality: Incoming X-MS-Exchange-Organization-SCL: -1 X-Forefront-Antispam-Report: CIP:216.32.180.12;CTRY:US;IPV:CAL;IPV:NLI;EFV:NLI;SFV:SKN;SFS:;DIR:INB;SFP:;SCL:-1;SRVR:BY2PR03MB396;H:va3outboundpool.messaging.microsoft.com;CLIP:216.32.180.12;FPR:;LANG:en; X-MS-Exchange-Organization-Network-Message-Id: 4c9142fe-6b3a-43af-724e-08d0f23cf0bc X-MS-Exchange-Organization-AVStamp-Service: 1.0 X-MS-Exchange-Organization-AuthSource: BY2FFO11FD018.protection.gbl X-MS-Exchange-Organization-AuthAs: Anonymous Content-Transfer-Encoding: 7bit Ticket locks for ppc, version 2. Changes since v1: * The atomically exchanged entity is always 32 bits. * asm inline string variations thus removed. * Carry the additional holder hint only #if defined(CONFIG_PPC_SPLPAR) Signed-off-by: Torsten Duwe -- arch/powerpc/include/asm/spinlock_types.h | 29 +++++ arch/powerpc/include/asm/spinlock.h | 146 +++++++++++++++++++++++------- arch/powerpc/lib/locks.c | 6 - 3 files changed, 144 insertions(+), 37 deletions(-) diff --git a/arch/powerpc/include/asm/spinlock_types.h b/arch/powerpc/include/asm/spinlock_types.h index 2351adc..fce1383 100644 --- a/arch/powerpc/include/asm/spinlock_types.h +++ b/arch/powerpc/include/asm/spinlock_types.h @@ -5,11 +5,34 @@ # error "please don't include this file directly" #endif +typedef u16 __ticket_t; +typedef u32 __ticketpair_t; + +#define TICKET_LOCK_INC ((__ticket_t)1) + +#define TICKET_SHIFT (sizeof(__ticket_t) * 8) + typedef struct { - volatile unsigned int slock; -} arch_spinlock_t; + union { + __ticketpair_t head_tail; + struct __raw_tickets { +#ifdef __BIG_ENDIAN__ /* The "tail" part should be in the MSBs */ + __ticket_t tail, head; +#else + __ticket_t head, tail; +#endif + } tickets; + }; +#if defined(CONFIG_PPC_SPLPAR) + u32 holder; +#endif +} arch_spinlock_t __aligned(4); -#define __ARCH_SPIN_LOCK_UNLOCKED { 0 } +#if defined(CONFIG_PPC_SPLPAR) +#define __ARCH_SPIN_LOCK_UNLOCKED { { 0 }, 0 } +#else +#define __ARCH_SPIN_LOCK_UNLOCKED { { 0 } } +#endif typedef struct { volatile signed int lock; diff --git a/arch/powerpc/include/asm/spinlock.h b/arch/powerpc/include/asm/spinlock.h index 5f54a74..a931514 100644 --- a/arch/powerpc/include/asm/spinlock.h +++ b/arch/powerpc/include/asm/spinlock.h @@ -9,8 +9,7 @@ * Copyright (C) 2001 Anton Blanchard , IBM * Copyright (C) 2002 Dave Engebretsen , IBM * Rework to support virtual processors - * - * Type of int is used as a full 64b word is not necessary. + * Copyright (C) 2014 Torsten Duwe , ticket lock port * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -28,7 +27,20 @@ #include #include -#define arch_spin_is_locked(x) ((x)->slock != 0) +static inline int arch_spin_is_locked(arch_spinlock_t *lock) +{ + struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets); + + return tmp.tail != tmp.head; +} + +static inline int arch_spin_is_contended(arch_spinlock_t *lock) +{ + struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets); + + return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC; +} +#define arch_spin_is_contended arch_spin_is_contended #ifdef CONFIG_PPC64 /* use 0x800000yy when locked, where yy == CPU number */ @@ -55,33 +67,59 @@ #endif /* - * This returns the old value in the lock, so we succeeded - * in getting the lock if the return value is 0. + * Our own cmpxchg, operating on spinlock_t's. Returns 0 iff value + * read at lock was equal to "old" AND the cmpxchg succeeded + * uninterruptedly. */ -static inline unsigned long __arch_spin_trylock(arch_spinlock_t *lock) +static __always_inline int __arch_spin_cmpxchg_eq(arch_spinlock_t *lock, + __ticketpair_t old, + __ticketpair_t new) { - unsigned long tmp, token; + register int retval = 1; + register __ticketpair_t tmp; - token = LOCK_TOKEN; - __asm__ __volatile__( -"1: " PPC_LWARX(%0,0,%2,1) "\n\ - cmpwi 0,%0,0\n\ - bne- 2f\n\ - stwcx. %1,0,%2\n\ - bne- 1b\n" - PPC_ACQUIRE_BARRIER + __asm__ __volatile__ ( +" li %0,1\n" /* default to "fail" */ + PPC_RELEASE_BARRIER +"1: lwarx %2,0,%5 # __arch_spin_cmpxchg_eq\n" +" cmp 0,0,%3,%2\n" +" bne- 2f\n" + PPC405_ERR77(0, "%5") +" stwcx. %4,0,%5\n" +" bne- 1b\n" +" isync\n" +" li %0,0\n" "2:" - : "=&r" (tmp) - : "r" (token), "r" (&lock->slock) - : "cr0", "memory"); + : "=&r" (retval), "+m" (*lock) + : "r" (tmp), "r" (old), "r" (new), "r" (lock) + : "cc", "memory"); - return tmp; + return retval; +} + +static __always_inline int __arch_spin_trylock(arch_spinlock_t *lock) +{ + arch_spinlock_t old, new; + + old.tickets = ACCESS_ONCE(lock->tickets); + if (old.tickets.head != old.tickets.tail) + return 0; + + new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT); + + if (__arch_spin_cmpxchg_eq(lock, old.head_tail, new.head_tail)) + return 0; + +#if defined(CONFIG_PPC_SPLPAR) + lock->holder = LOCK_TOKEN; +#endif + return 1; } static inline int arch_spin_trylock(arch_spinlock_t *lock) { CLEAR_IO_SYNC; - return __arch_spin_trylock(lock) == 0; + return __arch_spin_trylock(lock); } /* @@ -93,9 +131,8 @@ static inline int arch_spin_trylock(arch_spinlock_t *lock) * rest of our timeslice to the lock holder. * * So that we can tell which virtual processor is holding a lock, - * we put 0x80000000 | smp_processor_id() in the lock when it is - * held. Conveniently, we have a word in the paca that holds this - * value. + * we put 0x80000000 | smp_processor_id() into lock->holder. + * Conveniently, we have a word in the paca that holds this value. */ #if defined(CONFIG_PPC_SPLPAR) @@ -109,19 +146,55 @@ extern void __rw_yield(arch_rwlock_t *lock); #define SHARED_PROCESSOR 0 #endif -static inline void arch_spin_lock(arch_spinlock_t *lock) +/* + * Ticket locks are conceptually two parts, one indicating the current head of + * the queue, and the other indicating the current tail. The lock is acquired + * by atomically noting the tail and incrementing it by one (thus adding + * ourself to the queue and noting our position), then waiting until the head + * becomes equal to the the initial value of the tail. + * + * We use an asm covering *both* parts of the lock, to increment the tail and + * also load the position of the head, which takes care of memory ordering + * issues and should be optimal for the uncontended case. Note the tail must be + * in the high part, because a wide add increment of the low part would carry + * up and contaminate the high part. + */ +static __always_inline void arch_spin_lock(arch_spinlock_t *lock) { + register struct __raw_tickets old, tmp, + inc = { .tail = TICKET_LOCK_INC }; + CLEAR_IO_SYNC; - while (1) { - if (likely(__arch_spin_trylock(lock) == 0)) - break; + __asm__ __volatile__( +"1: lwarx %0,0,%4 # arch_spin_lock\n" +" add %1,%3,%0\n" + PPC405_ERR77(0, "%4") +" stwcx. %1,0,%4\n" +" bne- 1b" + : "=&r" (old), "=&r" (tmp), "+m" (lock->tickets) + : "r" (inc), "r" (&lock->tickets) + : "cc"); + + if (likely(old.head == old.tail)) + goto out; + + for (;;) { + unsigned count = 100; + do { + if (ACCESS_ONCE(lock->tickets.head) == old.tail) + goto out; HMT_low(); if (SHARED_PROCESSOR) __spin_yield(lock); - } while (unlikely(lock->slock != 0)); + } while (--count); HMT_medium(); } +out: +#if defined(CONFIG_PPC_SPLPAR) + lock->holder = LOCK_TOKEN; +#endif + barrier(); /* make sure nothing creeps before the lock is taken */ } static inline @@ -131,7 +204,7 @@ void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags) CLEAR_IO_SYNC; while (1) { - if (likely(__arch_spin_trylock(lock) == 0)) + if (likely(__arch_spin_trylock(lock))) break; local_save_flags(flags_dis); local_irq_restore(flags); @@ -139,7 +212,7 @@ void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags) HMT_low(); if (SHARED_PROCESSOR) __spin_yield(lock); - } while (unlikely(lock->slock != 0)); + } while (arch_spin_is_locked(lock)); HMT_medium(); local_irq_restore(flags_dis); } @@ -147,10 +220,21 @@ void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags) static inline void arch_spin_unlock(arch_spinlock_t *lock) { + arch_spinlock_t old, new; + +#if defined(CONFIG_PPC_SPLPAR) + lock->holder = 0; +#endif + do { + old.tickets = ACCESS_ONCE(lock->tickets); + new.tickets.head = old.tickets.head + TICKET_LOCK_INC; + new.tickets.tail = old.tickets.tail; + } while (unlikely(__arch_spin_cmpxchg_eq(lock, + old.head_tail, + new.head_tail))); SYNC_IO; __asm__ __volatile__("# arch_spin_unlock\n\t" PPC_RELEASE_BARRIER: : :"memory"); - lock->slock = 0; } #ifdef CONFIG_PPC64 diff --git a/arch/powerpc/lib/locks.c b/arch/powerpc/lib/locks.c index 0c9c8d7..4a57e32 100644 --- a/arch/powerpc/lib/locks.c +++ b/arch/powerpc/lib/locks.c @@ -27,7 +27,7 @@ void __spin_yield(arch_spinlock_t *lock) { unsigned int lock_value, holder_cpu, yield_count; - lock_value = lock->slock; + lock_value = lock->holder; if (lock_value == 0) return; holder_cpu = lock_value & 0xffff; @@ -36,7 +36,7 @@ void __spin_yield(arch_spinlock_t *lock) if ((yield_count & 1) == 0) return; /* virtual cpu is currently running */ rmb(); - if (lock->slock != lock_value) + if (lock->holder != lock_value) return; /* something has changed */ plpar_hcall_norets(H_CONFER, get_hard_smp_processor_id(holder_cpu), yield_count); @@ -70,7 +70,7 @@ void __rw_yield(arch_rwlock_t *rw) void arch_spin_unlock_wait(arch_spinlock_t *lock) { - while (lock->slock) { + while (arch_spin_is_locked(lock)) { HMT_low(); if (SHARED_PROCESSOR) __spin_yield(lock); --=-LKjCw78LMFVN+h3zSpkD--