Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Queued spinlock |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P. |
Waiman Long | 64d816c | 2015-11-09 19:09:21 -0500 | [diff] [blame] | 15 | * (C) Copyright 2015 Hewlett-Packard Enterprise Development LP |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 16 | * |
Waiman Long | 64d816c | 2015-11-09 19:09:21 -0500 | [diff] [blame] | 17 | * Authors: Waiman Long <waiman.long@hpe.com> |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 18 | */ |
| 19 | #ifndef __ASM_GENERIC_QSPINLOCK_H |
| 20 | #define __ASM_GENERIC_QSPINLOCK_H |
| 21 | |
| 22 | #include <asm-generic/qspinlock_types.h> |
| 23 | |
| 24 | /** |
Peter Zijlstra | 2c61002 | 2016-06-08 10:19:51 +0200 | [diff] [blame] | 25 | * queued_spin_unlock_wait - wait until the _current_ lock holder releases the lock |
| 26 | * @lock : Pointer to queued spinlock structure |
| 27 | * |
| 28 | * There is a very slight possibility of live-lock if the lockers keep coming |
| 29 | * and the waiter is just unfortunate enough to not see any unlock state. |
| 30 | */ |
| 31 | #ifndef queued_spin_unlock_wait |
| 32 | extern void queued_spin_unlock_wait(struct qspinlock *lock); |
| 33 | #endif |
| 34 | |
| 35 | /** |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 36 | * queued_spin_is_locked - is the spinlock locked? |
| 37 | * @lock: Pointer to queued spinlock structure |
| 38 | * Return: 1 if it is locked, 0 otherwise |
| 39 | */ |
Peter Zijlstra | 2c61002 | 2016-06-08 10:19:51 +0200 | [diff] [blame] | 40 | #ifndef queued_spin_is_locked |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 41 | static __always_inline int queued_spin_is_locked(struct qspinlock *lock) |
| 42 | { |
Peter Zijlstra | 54cf809 | 2016-05-20 18:04:36 +0200 | [diff] [blame] | 43 | /* |
Peter Zijlstra | 2c61002 | 2016-06-08 10:19:51 +0200 | [diff] [blame] | 44 | * See queued_spin_unlock_wait(). |
Peter Zijlstra | 54cf809 | 2016-05-20 18:04:36 +0200 | [diff] [blame] | 45 | * |
Peter Zijlstra | 2c61002 | 2016-06-08 10:19:51 +0200 | [diff] [blame] | 46 | * Any !0 state indicates it is locked, even if _Q_LOCKED_VAL |
| 47 | * isn't immediately observable. |
Peter Zijlstra | 54cf809 | 2016-05-20 18:04:36 +0200 | [diff] [blame] | 48 | */ |
Peter Zijlstra | 2c61002 | 2016-06-08 10:19:51 +0200 | [diff] [blame] | 49 | return atomic_read(&lock->val); |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 50 | } |
Peter Zijlstra | 2c61002 | 2016-06-08 10:19:51 +0200 | [diff] [blame] | 51 | #endif |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * queued_spin_value_unlocked - is the spinlock structure unlocked? |
| 55 | * @lock: queued spinlock structure |
| 56 | * Return: 1 if it is unlocked, 0 otherwise |
| 57 | * |
| 58 | * N.B. Whenever there are tasks waiting for the lock, it is considered |
| 59 | * locked wrt the lockref code to avoid lock stealing by the lockref |
| 60 | * code and change things underneath the lock. This also allows some |
| 61 | * optimizations to be applied without conflict with lockref. |
| 62 | */ |
| 63 | static __always_inline int queued_spin_value_unlocked(struct qspinlock lock) |
| 64 | { |
| 65 | return !atomic_read(&lock.val); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * queued_spin_is_contended - check if the lock is contended |
| 70 | * @lock : Pointer to queued spinlock structure |
| 71 | * Return: 1 if lock contended, 0 otherwise |
| 72 | */ |
| 73 | static __always_inline int queued_spin_is_contended(struct qspinlock *lock) |
| 74 | { |
| 75 | return atomic_read(&lock->val) & ~_Q_LOCKED_MASK; |
| 76 | } |
| 77 | /** |
| 78 | * queued_spin_trylock - try to acquire the queued spinlock |
| 79 | * @lock : Pointer to queued spinlock structure |
| 80 | * Return: 1 if lock acquired, 0 if failed |
| 81 | */ |
| 82 | static __always_inline int queued_spin_trylock(struct qspinlock *lock) |
| 83 | { |
| 84 | if (!atomic_read(&lock->val) && |
Waiman Long | 64d816c | 2015-11-09 19:09:21 -0500 | [diff] [blame] | 85 | (atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL) == 0)) |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 86 | return 1; |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | extern void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val); |
| 91 | |
| 92 | /** |
| 93 | * queued_spin_lock - acquire a queued spinlock |
| 94 | * @lock: Pointer to queued spinlock structure |
| 95 | */ |
| 96 | static __always_inline void queued_spin_lock(struct qspinlock *lock) |
| 97 | { |
| 98 | u32 val; |
| 99 | |
Waiman Long | 64d816c | 2015-11-09 19:09:21 -0500 | [diff] [blame] | 100 | val = atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL); |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 101 | if (likely(val == 0)) |
| 102 | return; |
| 103 | queued_spin_lock_slowpath(lock, val); |
| 104 | } |
| 105 | |
| 106 | #ifndef queued_spin_unlock |
| 107 | /** |
| 108 | * queued_spin_unlock - release a queued spinlock |
| 109 | * @lock : Pointer to queued spinlock structure |
| 110 | */ |
| 111 | static __always_inline void queued_spin_unlock(struct qspinlock *lock) |
| 112 | { |
| 113 | /* |
Pan Xinhui | ca50e42 | 2016-06-03 16:38:14 +0800 | [diff] [blame] | 114 | * unlock() needs release semantics: |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 115 | */ |
Pan Xinhui | ca50e42 | 2016-06-03 16:38:14 +0800 | [diff] [blame] | 116 | (void)atomic_sub_return_release(_Q_LOCKED_VAL, &lock->val); |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 117 | } |
| 118 | #endif |
| 119 | |
Peter Zijlstra | 43b3f02 | 2015-09-04 17:25:23 +0200 | [diff] [blame] | 120 | #ifndef virt_spin_lock |
| 121 | static __always_inline bool virt_spin_lock(struct qspinlock *lock) |
Peter Zijlstra (Intel) | 2aa79af | 2015-04-24 14:56:36 -0400 | [diff] [blame] | 122 | { |
| 123 | return false; |
| 124 | } |
| 125 | #endif |
| 126 | |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 127 | /* |
Waiman Long | a33fda3 | 2015-04-24 14:56:30 -0400 | [diff] [blame] | 128 | * Remapping spinlock architecture specific functions to the corresponding |
| 129 | * queued spinlock functions. |
| 130 | */ |
| 131 | #define arch_spin_is_locked(l) queued_spin_is_locked(l) |
| 132 | #define arch_spin_is_contended(l) queued_spin_is_contended(l) |
| 133 | #define arch_spin_value_unlocked(l) queued_spin_value_unlocked(l) |
| 134 | #define arch_spin_lock(l) queued_spin_lock(l) |
| 135 | #define arch_spin_trylock(l) queued_spin_trylock(l) |
| 136 | #define arch_spin_unlock(l) queued_spin_unlock(l) |
| 137 | #define arch_spin_lock_flags(l, f) queued_spin_lock(l) |
| 138 | #define arch_spin_unlock_wait(l) queued_spin_unlock_wait(l) |
| 139 | |
| 140 | #endif /* __ASM_GENERIC_QSPINLOCK_H */ |