xref: /linux-6.15/kernel/locking/osq_lock.c (revision 0d75e0c4)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d84b6728SDavidlohr Bueso #include <linux/percpu.h>
3d84b6728SDavidlohr Bueso #include <linux/sched.h>
4d84b6728SDavidlohr Bueso #include <linux/osq_lock.h>
5d84b6728SDavidlohr Bueso 
6d84b6728SDavidlohr Bueso /*
7d84b6728SDavidlohr Bueso  * An MCS like lock especially tailored for optimistic spinning for sleeping
8d84b6728SDavidlohr Bueso  * lock implementations (mutex, rwsem, etc).
9d84b6728SDavidlohr Bueso  *
10d84b6728SDavidlohr Bueso  * Using a single mcs node per CPU is safe because sleeping locks should not be
11d84b6728SDavidlohr Bueso  * called from interrupt context and we have preemption disabled while
12d84b6728SDavidlohr Bueso  * spinning.
13d84b6728SDavidlohr Bueso  */
147c223098SDavid Laight 
157c223098SDavid Laight struct optimistic_spin_node {
167c223098SDavid Laight 	struct optimistic_spin_node *next, *prev;
177c223098SDavid Laight 	int locked; /* 1 if lock acquired */
187c223098SDavid Laight 	int cpu; /* encoded CPU # + 1 value */
197c223098SDavid Laight };
207c223098SDavid Laight 
21d84b6728SDavidlohr Bueso static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
22d84b6728SDavidlohr Bueso 
23d84b6728SDavidlohr Bueso /*
24d84b6728SDavidlohr Bueso  * We use the value 0 to represent "no CPU", thus the encoded value
25d84b6728SDavidlohr Bueso  * will be the CPU number incremented by 1.
26d84b6728SDavidlohr Bueso  */
encode_cpu(int cpu_nr)27d84b6728SDavidlohr Bueso static inline int encode_cpu(int cpu_nr)
28d84b6728SDavidlohr Bueso {
29d84b6728SDavidlohr Bueso 	return cpu_nr + 1;
30d84b6728SDavidlohr Bueso }
31d84b6728SDavidlohr Bueso 
node_cpu(struct optimistic_spin_node * node)325aff60a1SPan Xinhui static inline int node_cpu(struct optimistic_spin_node *node)
335aff60a1SPan Xinhui {
345aff60a1SPan Xinhui 	return node->cpu - 1;
355aff60a1SPan Xinhui }
365aff60a1SPan Xinhui 
decode_cpu(int encoded_cpu_val)37d84b6728SDavidlohr Bueso static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
38d84b6728SDavidlohr Bueso {
39d84b6728SDavidlohr Bueso 	int cpu_nr = encoded_cpu_val - 1;
40d84b6728SDavidlohr Bueso 
41d84b6728SDavidlohr Bueso 	return per_cpu_ptr(&osq_node, cpu_nr);
42d84b6728SDavidlohr Bueso }
43d84b6728SDavidlohr Bueso 
44d84b6728SDavidlohr Bueso /*
45d84b6728SDavidlohr Bueso  * Get a stable @node->next pointer, either for unlock() or unqueue() purposes.
46d84b6728SDavidlohr Bueso  * Can return NULL in case we were the last queued and we updated @lock instead.
47563adbfcSDavid Laight  *
48563adbfcSDavid Laight  * If osq_lock() is being cancelled there must be a previous node
49563adbfcSDavid Laight  * and 'old_cpu' is its CPU #.
50563adbfcSDavid Laight  * For osq_unlock() there is never a previous node and old_cpu is
51563adbfcSDavid Laight  * set to OSQ_UNLOCKED_VAL.
52d84b6728SDavidlohr Bueso  */
53d84b6728SDavidlohr Bueso static inline struct optimistic_spin_node *
osq_wait_next(struct optimistic_spin_queue * lock,struct optimistic_spin_node * node,int old_cpu)54d84b6728SDavidlohr Bueso osq_wait_next(struct optimistic_spin_queue *lock,
55d84b6728SDavidlohr Bueso 	      struct optimistic_spin_node *node,
56563adbfcSDavid Laight 	      int old_cpu)
57d84b6728SDavidlohr Bueso {
58d84b6728SDavidlohr Bueso 	int curr = encode_cpu(smp_processor_id());
59d84b6728SDavidlohr Bueso 
60d84b6728SDavidlohr Bueso 	for (;;) {
61d84b6728SDavidlohr Bueso 		if (atomic_read(&lock->tail) == curr &&
62563adbfcSDavid Laight 		    atomic_cmpxchg_acquire(&lock->tail, curr, old_cpu) == curr) {
63d84b6728SDavidlohr Bueso 			/*
64d84b6728SDavidlohr Bueso 			 * We were the last queued, we moved @lock back. @prev
65d84b6728SDavidlohr Bueso 			 * will now observe @lock and will complete its
66d84b6728SDavidlohr Bueso 			 * unlock()/unqueue().
67d84b6728SDavidlohr Bueso 			 */
68b106bcf0SDavid Laight 			return NULL;
69d84b6728SDavidlohr Bueso 		}
70d84b6728SDavidlohr Bueso 
71d84b6728SDavidlohr Bueso 		/*
72d84b6728SDavidlohr Bueso 		 * We must xchg() the @node->next value, because if we were to
73d84b6728SDavidlohr Bueso 		 * leave it in, a concurrent unlock()/unqueue() from
74d84b6728SDavidlohr Bueso 		 * @node->next might complete Step-A and think its @prev is
75d84b6728SDavidlohr Bueso 		 * still valid.
76d84b6728SDavidlohr Bueso 		 *
77d84b6728SDavidlohr Bueso 		 * If the concurrent unlock()/unqueue() wins the race, we'll
78d84b6728SDavidlohr Bueso 		 * wait for either @lock to point to us, through its Step-B, or
79d84b6728SDavidlohr Bueso 		 * wait for a new @node->next from its Step-C.
80d84b6728SDavidlohr Bueso 		 */
81d84b6728SDavidlohr Bueso 		if (node->next) {
82b106bcf0SDavid Laight 			struct optimistic_spin_node *next;
83b106bcf0SDavid Laight 
84d84b6728SDavidlohr Bueso 			next = xchg(&node->next, NULL);
85d84b6728SDavidlohr Bueso 			if (next)
86b106bcf0SDavid Laight 				return next;
87d84b6728SDavidlohr Bueso 		}
88d84b6728SDavidlohr Bueso 
89f2f09a4cSChristian Borntraeger 		cpu_relax();
90d84b6728SDavidlohr Bueso 	}
91d84b6728SDavidlohr Bueso }
92d84b6728SDavidlohr Bueso 
osq_lock(struct optimistic_spin_queue * lock)93d84b6728SDavidlohr Bueso bool osq_lock(struct optimistic_spin_queue *lock)
94d84b6728SDavidlohr Bueso {
95d84b6728SDavidlohr Bueso 	struct optimistic_spin_node *node = this_cpu_ptr(&osq_node);
96d84b6728SDavidlohr Bueso 	struct optimistic_spin_node *prev, *next;
97d84b6728SDavidlohr Bueso 	int curr = encode_cpu(smp_processor_id());
98d84b6728SDavidlohr Bueso 	int old;
99d84b6728SDavidlohr Bueso 
100d84b6728SDavidlohr Bueso 	node->locked = 0;
101d84b6728SDavidlohr Bueso 	node->next = NULL;
102d84b6728SDavidlohr Bueso 	node->cpu = curr;
103d84b6728SDavidlohr Bueso 
104c55a6ffaSDavidlohr Bueso 	/*
105b4b29f94SWill Deacon 	 * We need both ACQUIRE (pairs with corresponding RELEASE in
106b4b29f94SWill Deacon 	 * unlock() uncontended, or fastpath) and RELEASE (to publish
107b4b29f94SWill Deacon 	 * the node fields we just initialised) semantics when updating
108b4b29f94SWill Deacon 	 * the lock tail.
109c55a6ffaSDavidlohr Bueso 	 */
110b4b29f94SWill Deacon 	old = atomic_xchg(&lock->tail, curr);
111d84b6728SDavidlohr Bueso 	if (old == OSQ_UNLOCKED_VAL)
112d84b6728SDavidlohr Bueso 		return true;
113d84b6728SDavidlohr Bueso 
114d84b6728SDavidlohr Bueso 	prev = decode_cpu(old);
115d84b6728SDavidlohr Bueso 	node->prev = prev;
11650972fe7SPrateek Sood 
11750972fe7SPrateek Sood 	/*
11850972fe7SPrateek Sood 	 * osq_lock()			unqueue
11950972fe7SPrateek Sood 	 *
12050972fe7SPrateek Sood 	 * node->prev = prev		osq_wait_next()
12150972fe7SPrateek Sood 	 * WMB				MB
12250972fe7SPrateek Sood 	 * prev->next = node		next->prev = prev // unqueue-C
12350972fe7SPrateek Sood 	 *
12450972fe7SPrateek Sood 	 * Here 'node->prev' and 'next->prev' are the same variable and we need
12550972fe7SPrateek Sood 	 * to ensure these stores happen in-order to avoid corrupting the list.
12650972fe7SPrateek Sood 	 */
12750972fe7SPrateek Sood 	smp_wmb();
12850972fe7SPrateek Sood 
1294d3199e4SDavidlohr Bueso 	WRITE_ONCE(prev->next, node);
130d84b6728SDavidlohr Bueso 
131d84b6728SDavidlohr Bueso 	/*
132d84b6728SDavidlohr Bueso 	 * Normally @prev is untouchable after the above store; because at that
133d84b6728SDavidlohr Bueso 	 * moment unlock can proceed and wipe the node element from stack.
134d84b6728SDavidlohr Bueso 	 *
135d84b6728SDavidlohr Bueso 	 * However, since our nodes are static per-cpu storage, we're
136d84b6728SDavidlohr Bueso 	 * guaranteed their existence -- this allows us to apply
137d84b6728SDavidlohr Bueso 	 * cmpxchg in an attempt to undo our queueing.
138d84b6728SDavidlohr Bueso 	 */
139d84b6728SDavidlohr Bueso 
140d84b6728SDavidlohr Bueso 	/*
141e2db7592SIngo Molnar 	 * Wait to acquire the lock or cancellation. Note that need_resched()
142f5bfdc8eSWaiman Long 	 * will come with an IPI, which will wake smp_cond_load_relaxed() if it
143f5bfdc8eSWaiman Long 	 * is implemented with a monitor-wait. vcpu_is_preempted() relies on
144f5bfdc8eSWaiman Long 	 * polling, be careful.
145d84b6728SDavidlohr Bueso 	 */
146f5bfdc8eSWaiman Long 	if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
147f5bfdc8eSWaiman Long 				  vcpu_is_preempted(node_cpu(node->prev))))
148d84b6728SDavidlohr Bueso 		return true;
149d84b6728SDavidlohr Bueso 
150f5bfdc8eSWaiman Long 	/* unqueue */
151d84b6728SDavidlohr Bueso 	/*
152d84b6728SDavidlohr Bueso 	 * Step - A  -- stabilize @prev
153d84b6728SDavidlohr Bueso 	 *
154d84b6728SDavidlohr Bueso 	 * Undo our @prev->next assignment; this will make @prev's
155d84b6728SDavidlohr Bueso 	 * unlock()/unqueue() wait for a next pointer since @lock points to us
156d84b6728SDavidlohr Bueso 	 * (or later).
157d84b6728SDavidlohr Bueso 	 */
158d84b6728SDavidlohr Bueso 
159d84b6728SDavidlohr Bueso 	for (;;) {
16033190b67SQian Cai 		/*
16133190b67SQian Cai 		 * cpu_relax() below implies a compiler barrier which would
16233190b67SQian Cai 		 * prevent this comparison being optimized away.
16333190b67SQian Cai 		 */
16433190b67SQian Cai 		if (data_race(prev->next) == node &&
165d84b6728SDavidlohr Bueso 		    cmpxchg(&prev->next, node, NULL) == node)
166d84b6728SDavidlohr Bueso 			break;
167d84b6728SDavidlohr Bueso 
168d84b6728SDavidlohr Bueso 		/*
169d84b6728SDavidlohr Bueso 		 * We can only fail the cmpxchg() racing against an unlock(),
170e2db7592SIngo Molnar 		 * in which case we should observe @node->locked becoming
171d84b6728SDavidlohr Bueso 		 * true.
172d84b6728SDavidlohr Bueso 		 */
173d84b6728SDavidlohr Bueso 		if (smp_load_acquire(&node->locked))
174d84b6728SDavidlohr Bueso 			return true;
175d84b6728SDavidlohr Bueso 
176f2f09a4cSChristian Borntraeger 		cpu_relax();
177d84b6728SDavidlohr Bueso 
178d84b6728SDavidlohr Bueso 		/*
179d84b6728SDavidlohr Bueso 		 * Or we race against a concurrent unqueue()'s step-B, in which
180d84b6728SDavidlohr Bueso 		 * case its step-C will write us a new @node->prev pointer.
181d84b6728SDavidlohr Bueso 		 */
1824d3199e4SDavidlohr Bueso 		prev = READ_ONCE(node->prev);
183d84b6728SDavidlohr Bueso 	}
184d84b6728SDavidlohr Bueso 
185d84b6728SDavidlohr Bueso 	/*
186d84b6728SDavidlohr Bueso 	 * Step - B -- stabilize @next
187d84b6728SDavidlohr Bueso 	 *
188d84b6728SDavidlohr Bueso 	 * Similar to unlock(), wait for @node->next or move @lock from @node
189d84b6728SDavidlohr Bueso 	 * back to @prev.
190d84b6728SDavidlohr Bueso 	 */
191d84b6728SDavidlohr Bueso 
192563adbfcSDavid Laight 	next = osq_wait_next(lock, node, prev->cpu);
193d84b6728SDavidlohr Bueso 	if (!next)
194d84b6728SDavidlohr Bueso 		return false;
195d84b6728SDavidlohr Bueso 
196d84b6728SDavidlohr Bueso 	/*
197d84b6728SDavidlohr Bueso 	 * Step - C -- unlink
198d84b6728SDavidlohr Bueso 	 *
199d84b6728SDavidlohr Bueso 	 * @prev is stable because its still waiting for a new @prev->next
200d84b6728SDavidlohr Bueso 	 * pointer, @next is stable because our @node->next pointer is NULL and
201d84b6728SDavidlohr Bueso 	 * it will wait in Step-A.
202d84b6728SDavidlohr Bueso 	 */
203d84b6728SDavidlohr Bueso 
2044d3199e4SDavidlohr Bueso 	WRITE_ONCE(next->prev, prev);
2054d3199e4SDavidlohr Bueso 	WRITE_ONCE(prev->next, next);
206d84b6728SDavidlohr Bueso 
207d84b6728SDavidlohr Bueso 	return false;
208d84b6728SDavidlohr Bueso }
209d84b6728SDavidlohr Bueso 
osq_unlock(struct optimistic_spin_queue * lock)210d84b6728SDavidlohr Bueso void osq_unlock(struct optimistic_spin_queue *lock)
211d84b6728SDavidlohr Bueso {
212d84b6728SDavidlohr Bueso 	struct optimistic_spin_node *node, *next;
213d84b6728SDavidlohr Bueso 	int curr = encode_cpu(smp_processor_id());
214d84b6728SDavidlohr Bueso 
215d84b6728SDavidlohr Bueso 	/*
216d84b6728SDavidlohr Bueso 	 * Fast path for the uncontended case.
217d84b6728SDavidlohr Bueso 	 */
218*0d75e0c4SUros Bizjak 	if (atomic_try_cmpxchg_release(&lock->tail, &curr, OSQ_UNLOCKED_VAL))
219d84b6728SDavidlohr Bueso 		return;
220d84b6728SDavidlohr Bueso 
221d84b6728SDavidlohr Bueso 	/*
222d84b6728SDavidlohr Bueso 	 * Second most likely case.
223d84b6728SDavidlohr Bueso 	 */
224d84b6728SDavidlohr Bueso 	node = this_cpu_ptr(&osq_node);
225d84b6728SDavidlohr Bueso 	next = xchg(&node->next, NULL);
226d84b6728SDavidlohr Bueso 	if (next) {
2274d3199e4SDavidlohr Bueso 		WRITE_ONCE(next->locked, 1);
228d84b6728SDavidlohr Bueso 		return;
229d84b6728SDavidlohr Bueso 	}
230d84b6728SDavidlohr Bueso 
231563adbfcSDavid Laight 	next = osq_wait_next(lock, node, OSQ_UNLOCKED_VAL);
232d84b6728SDavidlohr Bueso 	if (next)
2334d3199e4SDavidlohr Bueso 		WRITE_ONCE(next->locked, 1);
234d84b6728SDavidlohr Bueso }
235