1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2c9122da1SPeter Zijlstra /*
3c9122da1SPeter Zijlstra * MCS lock defines
4c9122da1SPeter Zijlstra *
5c9122da1SPeter Zijlstra * This file contains the main data structure and API definitions of MCS lock.
6c9122da1SPeter Zijlstra *
7c9122da1SPeter Zijlstra * The MCS lock (proposed by Mellor-Crummey and Scott) is a simple spin-lock
8c9122da1SPeter Zijlstra * with the desirable properties of being fair, and with each cpu trying
9c9122da1SPeter Zijlstra * to acquire the lock spinning on a local variable.
10e2db7592SIngo Molnar * It avoids expensive cache bounces that common test-and-set spin-lock
11c9122da1SPeter Zijlstra * implementations incur.
12c9122da1SPeter Zijlstra */
13c9122da1SPeter Zijlstra #ifndef __LINUX_MCS_SPINLOCK_H
14c9122da1SPeter Zijlstra #define __LINUX_MCS_SPINLOCK_H
15c9122da1SPeter Zijlstra
16c9122da1SPeter Zijlstra #include <asm/mcs_spinlock.h>
17c9122da1SPeter Zijlstra
18c9122da1SPeter Zijlstra #ifndef arch_mcs_spin_lock_contended
19c9122da1SPeter Zijlstra /*
207f56b58aSJason Low * Using smp_cond_load_acquire() provides the acquire semantics
217f56b58aSJason Low * required so that subsequent operations happen after the
227f56b58aSJason Low * lock is acquired. Additionally, some architectures such as
237f56b58aSJason Low * ARM64 would like to do spin-waiting instead of purely
247f56b58aSJason Low * spinning, and smp_cond_load_acquire() provides that behavior.
25c9122da1SPeter Zijlstra */
26c9122da1SPeter Zijlstra #define arch_mcs_spin_lock_contended(l) \
27*c0149a03SKumar Kartikeya Dwivedi smp_cond_load_acquire(l, VAL)
28c9122da1SPeter Zijlstra #endif
29c9122da1SPeter Zijlstra
30c9122da1SPeter Zijlstra #ifndef arch_mcs_spin_unlock_contended
31c9122da1SPeter Zijlstra /*
32c9122da1SPeter Zijlstra * smp_store_release() provides a memory barrier to ensure all
33c9122da1SPeter Zijlstra * operations in the critical section has been completed before
34c9122da1SPeter Zijlstra * unlocking.
35c9122da1SPeter Zijlstra */
36c9122da1SPeter Zijlstra #define arch_mcs_spin_unlock_contended(l) \
37c9122da1SPeter Zijlstra smp_store_release((l), 1)
38c9122da1SPeter Zijlstra #endif
39c9122da1SPeter Zijlstra
40c9122da1SPeter Zijlstra /*
41c9122da1SPeter Zijlstra * Note: the smp_load_acquire/smp_store_release pair is not
42c9122da1SPeter Zijlstra * sufficient to form a full memory barrier across
43c9122da1SPeter Zijlstra * cpus for many architectures (except x86) for mcs_unlock and mcs_lock.
44c9122da1SPeter Zijlstra * For applications that need a full barrier across multiple cpus
45c9122da1SPeter Zijlstra * with mcs_unlock and mcs_lock pair, smp_mb__after_unlock_lock() should be
46c9122da1SPeter Zijlstra * used after mcs_lock.
47c9122da1SPeter Zijlstra */
48c9122da1SPeter Zijlstra
49c9122da1SPeter Zijlstra /*
50c9122da1SPeter Zijlstra * In order to acquire the lock, the caller should declare a local node and
51c9122da1SPeter Zijlstra * pass a reference of the node to this function in addition to the lock.
52c9122da1SPeter Zijlstra * If the lock has already been acquired, then this will proceed to spin
53c9122da1SPeter Zijlstra * on this node->locked until the previous lock holder sets the node->locked
54c9122da1SPeter Zijlstra * in mcs_spin_unlock().
55c9122da1SPeter Zijlstra */
56c9122da1SPeter Zijlstra static inline
mcs_spin_lock(struct mcs_spinlock ** lock,struct mcs_spinlock * node)57c9122da1SPeter Zijlstra void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node)
58c9122da1SPeter Zijlstra {
59c9122da1SPeter Zijlstra struct mcs_spinlock *prev;
60c9122da1SPeter Zijlstra
61c9122da1SPeter Zijlstra /* Init node */
62c9122da1SPeter Zijlstra node->locked = 0;
63c9122da1SPeter Zijlstra node->next = NULL;
64c9122da1SPeter Zijlstra
65920c720aSPeter Zijlstra /*
66920c720aSPeter Zijlstra * We rely on the full barrier with global transitivity implied by the
67920c720aSPeter Zijlstra * below xchg() to order the initialization stores above against any
68920c720aSPeter Zijlstra * observation of @node. And to provide the ACQUIRE ordering associated
69920c720aSPeter Zijlstra * with a LOCK primitive.
70920c720aSPeter Zijlstra */
71920c720aSPeter Zijlstra prev = xchg(lock, node);
72c9122da1SPeter Zijlstra if (likely(prev == NULL)) {
73c9122da1SPeter Zijlstra /*
74c9122da1SPeter Zijlstra * Lock acquired, don't need to set node->locked to 1. Threads
75c9122da1SPeter Zijlstra * only spin on its own node->locked value for lock acquisition.
76c9122da1SPeter Zijlstra * However, since this thread can immediately acquire the lock
77c9122da1SPeter Zijlstra * and does not proceed to spin on its own node->locked, this
78c9122da1SPeter Zijlstra * value won't be used. If a debug mode is needed to
79c9122da1SPeter Zijlstra * audit lock status, then set node->locked value here.
80c9122da1SPeter Zijlstra */
81c9122da1SPeter Zijlstra return;
82c9122da1SPeter Zijlstra }
834d3199e4SDavidlohr Bueso WRITE_ONCE(prev->next, node);
84c9122da1SPeter Zijlstra
85c9122da1SPeter Zijlstra /* Wait until the lock holder passes the lock down. */
86c9122da1SPeter Zijlstra arch_mcs_spin_lock_contended(&node->locked);
87c9122da1SPeter Zijlstra }
88c9122da1SPeter Zijlstra
89c9122da1SPeter Zijlstra /*
90c9122da1SPeter Zijlstra * Releases the lock. The caller should pass in the corresponding node that
91c9122da1SPeter Zijlstra * was used to acquire the lock.
92c9122da1SPeter Zijlstra */
93c9122da1SPeter Zijlstra static inline
mcs_spin_unlock(struct mcs_spinlock ** lock,struct mcs_spinlock * node)94c9122da1SPeter Zijlstra void mcs_spin_unlock(struct mcs_spinlock **lock, struct mcs_spinlock *node)
95c9122da1SPeter Zijlstra {
964d3199e4SDavidlohr Bueso struct mcs_spinlock *next = READ_ONCE(node->next);
97c9122da1SPeter Zijlstra
98c9122da1SPeter Zijlstra if (likely(!next)) {
99c9122da1SPeter Zijlstra /*
100c9122da1SPeter Zijlstra * Release the lock by setting it to NULL
101c9122da1SPeter Zijlstra */
1023552a07aSDavidlohr Bueso if (likely(cmpxchg_release(lock, node, NULL) == node))
103c9122da1SPeter Zijlstra return;
104c9122da1SPeter Zijlstra /* Wait until the next pointer is set */
1054d3199e4SDavidlohr Bueso while (!(next = READ_ONCE(node->next)))
106f2f09a4cSChristian Borntraeger cpu_relax();
107c9122da1SPeter Zijlstra }
108c9122da1SPeter Zijlstra
109c9122da1SPeter Zijlstra /* Pass lock to next waiter. */
110c9122da1SPeter Zijlstra arch_mcs_spin_unlock_contended(&next->locked);
111c9122da1SPeter Zijlstra }
112c9122da1SPeter Zijlstra
113c9122da1SPeter Zijlstra #endif /* __LINUX_MCS_SPINLOCK_H */
114