/* SPDX-License-Identifier: GPL-2.0 */1/*2* MCS lock defines3*4* This file contains the main data structure and API definitions of MCS lock.5*6* The MCS lock (proposed by Mellor-Crummey and Scott) is a simple spin-lock7* with the desirable properties of being fair, and with each cpu trying8* to acquire the lock spinning on a local variable.9* It avoids expensive cache bounces that common test-and-set spin-lock10* implementations incur.11*/12#ifndef __LINUX_MCS_SPINLOCK_H13#define __LINUX_MCS_SPINLOCK_H1415#include <asm/mcs_spinlock.h>1617#ifndef arch_mcs_spin_lock_contended18/*19* Using smp_cond_load_acquire() provides the acquire semantics20* required so that subsequent operations happen after the21* lock is acquired. Additionally, some architectures such as22* ARM64 would like to do spin-waiting instead of purely23* spinning, and smp_cond_load_acquire() provides that behavior.24*/25#define arch_mcs_spin_lock_contended(l) \26smp_cond_load_acquire(l, VAL)27#endif2829#ifndef arch_mcs_spin_unlock_contended30/*31* smp_store_release() provides a memory barrier to ensure all32* operations in the critical section has been completed before33* unlocking.34*/35#define arch_mcs_spin_unlock_contended(l) \36smp_store_release((l), 1)37#endif3839/*40* Note: the smp_load_acquire/smp_store_release pair is not41* sufficient to form a full memory barrier across42* cpus for many architectures (except x86) for mcs_unlock and mcs_lock.43* For applications that need a full barrier across multiple cpus44* with mcs_unlock and mcs_lock pair, smp_mb__after_unlock_lock() should be45* used after mcs_lock.46*/4748/*49* In order to acquire the lock, the caller should declare a local node and50* pass a reference of the node to this function in addition to the lock.51* If the lock has already been acquired, then this will proceed to spin52* on this node->locked until the previous lock holder sets the node->locked53* in mcs_spin_unlock().54*/55static inline56void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node)57{58struct mcs_spinlock *prev;5960/* Init node */61node->locked = 0;62node->next = NULL;6364/*65* We rely on the full barrier with global transitivity implied by the66* below xchg() to order the initialization stores above against any67* observation of @node. And to provide the ACQUIRE ordering associated68* with a LOCK primitive.69*/70prev = xchg(lock, node);71if (likely(prev == NULL)) {72/*73* Lock acquired, don't need to set node->locked to 1. Threads74* only spin on its own node->locked value for lock acquisition.75* However, since this thread can immediately acquire the lock76* and does not proceed to spin on its own node->locked, this77* value won't be used. If a debug mode is needed to78* audit lock status, then set node->locked value here.79*/80return;81}82WRITE_ONCE(prev->next, node);8384/* Wait until the lock holder passes the lock down. */85arch_mcs_spin_lock_contended(&node->locked);86}8788/*89* Releases the lock. The caller should pass in the corresponding node that90* was used to acquire the lock.91*/92static inline93void mcs_spin_unlock(struct mcs_spinlock **lock, struct mcs_spinlock *node)94{95struct mcs_spinlock *next = READ_ONCE(node->next);9697if (likely(!next)) {98/*99* Release the lock by setting it to NULL100*/101if (likely(cmpxchg_release(lock, node, NULL) == node))102return;103/* Wait until the next pointer is set */104while (!(next = READ_ONCE(node->next)))105cpu_relax();106}107108/* Pass lock to next waiter. */109arch_mcs_spin_unlock_contended(&next->locked);110}111112#endif /* __LINUX_MCS_SPINLOCK_H */113114115