1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #ifndef WW_RT
4
5 #define MUTEX mutex
6 #define MUTEX_WAITER mutex_waiter
7 #define WAIT_LOCK wait_lock
8
9 static inline struct mutex_waiter *
__ww_waiter_first(struct mutex * lock)10 __ww_waiter_first(struct mutex *lock)
11 __must_hold(&lock->wait_lock)
12 {
13 return lock->first_waiter;
14 }
15
16 static inline struct mutex_waiter *
__ww_waiter_next(struct mutex * lock,struct mutex_waiter * w)17 __ww_waiter_next(struct mutex *lock, struct mutex_waiter *w)
18 __must_hold(&lock->wait_lock)
19 {
20 w = list_next_entry(w, list);
21 if (lock->first_waiter == w)
22 return NULL;
23
24 return w;
25 }
26
27 static inline struct mutex_waiter *
__ww_waiter_prev(struct mutex * lock,struct mutex_waiter * w)28 __ww_waiter_prev(struct mutex *lock, struct mutex_waiter *w)
29 __must_hold(&lock->wait_lock)
30 {
31 w = list_prev_entry(w, list);
32 if (lock->first_waiter == w)
33 return NULL;
34
35 return w;
36 }
37
38 static inline struct mutex_waiter *
__ww_waiter_last(struct mutex * lock)39 __ww_waiter_last(struct mutex *lock)
40 __must_hold(&lock->wait_lock)
41 {
42 struct mutex_waiter *w = lock->first_waiter;
43
44 if (w)
45 w = list_prev_entry(w, list);
46 return w;
47 }
48
49 static inline void
__ww_waiter_add(struct mutex * lock,struct mutex_waiter * waiter,struct mutex_waiter * pos)50 __ww_waiter_add(struct mutex *lock, struct mutex_waiter *waiter, struct mutex_waiter *pos)
51 __must_hold(&lock->wait_lock)
52 {
53 __mutex_add_waiter(lock, waiter, pos);
54 }
55
56 static inline struct task_struct *
__ww_mutex_owner(struct mutex * lock)57 __ww_mutex_owner(struct mutex *lock)
58 {
59 return __mutex_owner(lock);
60 }
61
62 static inline bool
__ww_mutex_has_waiters(struct mutex * lock)63 __ww_mutex_has_waiters(struct mutex *lock)
64 {
65 return atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS;
66 }
67
lock_wait_lock(struct mutex * lock,unsigned long * flags)68 static inline void lock_wait_lock(struct mutex *lock, unsigned long *flags)
69 __acquires(&lock->wait_lock)
70 {
71 raw_spin_lock_irqsave(&lock->wait_lock, *flags);
72 }
73
unlock_wait_lock(struct mutex * lock,unsigned long * flags)74 static inline void unlock_wait_lock(struct mutex *lock, unsigned long *flags)
75 __releases(&lock->wait_lock)
76 {
77 raw_spin_unlock_irqrestore(&lock->wait_lock, *flags);
78 }
79
lockdep_assert_wait_lock_held(struct mutex * lock)80 static inline void lockdep_assert_wait_lock_held(struct mutex *lock)
81 __must_hold(&lock->wait_lock)
82 {
83 lockdep_assert_held(&lock->wait_lock);
84 }
85
86 #else /* WW_RT */
87
88 #define MUTEX rt_mutex
89 #define MUTEX_WAITER rt_mutex_waiter
90 #define WAIT_LOCK rtmutex.wait_lock
91
92 static inline struct rt_mutex_waiter *
__ww_waiter_first(struct rt_mutex * lock)93 __ww_waiter_first(struct rt_mutex *lock)
94 __must_hold(&lock->rtmutex.wait_lock)
95 {
96 struct rb_node *n = rb_first(&lock->rtmutex.waiters.rb_root);
97 if (!n)
98 return NULL;
99 return rb_entry(n, struct rt_mutex_waiter, tree.entry);
100 }
101
102 static inline struct rt_mutex_waiter *
__ww_waiter_next(struct rt_mutex * lock,struct rt_mutex_waiter * w)103 __ww_waiter_next(struct rt_mutex *lock, struct rt_mutex_waiter *w)
104 {
105 struct rb_node *n = rb_next(&w->tree.entry);
106 if (!n)
107 return NULL;
108 return rb_entry(n, struct rt_mutex_waiter, tree.entry);
109 }
110
111 static inline struct rt_mutex_waiter *
__ww_waiter_prev(struct rt_mutex * lock,struct rt_mutex_waiter * w)112 __ww_waiter_prev(struct rt_mutex *lock, struct rt_mutex_waiter *w)
113 {
114 struct rb_node *n = rb_prev(&w->tree.entry);
115 if (!n)
116 return NULL;
117 return rb_entry(n, struct rt_mutex_waiter, tree.entry);
118 }
119
120 static inline struct rt_mutex_waiter *
__ww_waiter_last(struct rt_mutex * lock)121 __ww_waiter_last(struct rt_mutex *lock)
122 __must_hold(&lock->rtmutex.wait_lock)
123 {
124 struct rb_node *n = rb_last(&lock->rtmutex.waiters.rb_root);
125 if (!n)
126 return NULL;
127 return rb_entry(n, struct rt_mutex_waiter, tree.entry);
128 }
129
130 static inline void
__ww_waiter_add(struct rt_mutex * lock,struct rt_mutex_waiter * waiter,struct rt_mutex_waiter * pos)131 __ww_waiter_add(struct rt_mutex *lock, struct rt_mutex_waiter *waiter, struct rt_mutex_waiter *pos)
132 {
133 /* RT unconditionally adds the waiter first and then removes it on error */
134 }
135
136 static inline struct task_struct *
__ww_mutex_owner(struct rt_mutex * lock)137 __ww_mutex_owner(struct rt_mutex *lock)
138 {
139 return rt_mutex_owner(&lock->rtmutex);
140 }
141
142 static inline bool
__ww_mutex_has_waiters(struct rt_mutex * lock)143 __ww_mutex_has_waiters(struct rt_mutex *lock)
144 __must_hold(&lock->rtmutex.wait_lock)
145 {
146 return rt_mutex_has_waiters(&lock->rtmutex);
147 }
148
lock_wait_lock(struct rt_mutex * lock,unsigned long * flags)149 static inline void lock_wait_lock(struct rt_mutex *lock, unsigned long *flags)
150 __acquires(&lock->rtmutex.wait_lock)
151 {
152 raw_spin_lock_irqsave(&lock->rtmutex.wait_lock, *flags);
153 }
154
unlock_wait_lock(struct rt_mutex * lock,unsigned long * flags)155 static inline void unlock_wait_lock(struct rt_mutex *lock, unsigned long *flags)
156 __releases(&lock->rtmutex.wait_lock)
157 {
158 raw_spin_unlock_irqrestore(&lock->rtmutex.wait_lock, *flags);
159 }
160
lockdep_assert_wait_lock_held(struct rt_mutex * lock)161 static inline void lockdep_assert_wait_lock_held(struct rt_mutex *lock)
162 __must_hold(&lock->rtmutex.wait_lock)
163 {
164 lockdep_assert_held(&lock->rtmutex.wait_lock);
165 }
166
167 #endif /* WW_RT */
168
169 /*
170 * Wait-Die:
171 * The newer transactions are killed when:
172 * It (the new transaction) makes a request for a lock being held
173 * by an older transaction.
174 *
175 * Wound-Wait:
176 * The newer transactions are wounded when:
177 * An older transaction makes a request for a lock being held by
178 * the newer transaction.
179 */
180
181 /*
182 * Associate the ww_mutex @ww with the context @ww_ctx under which we acquired
183 * it.
184 */
185 static __always_inline void
ww_mutex_lock_acquired(struct ww_mutex * ww,struct ww_acquire_ctx * ww_ctx)186 ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
187 {
188 #ifdef DEBUG_WW_MUTEXES
189 /*
190 * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
191 * but released with a normal mutex_unlock in this call.
192 *
193 * This should never happen, always use ww_mutex_unlock.
194 */
195 DEBUG_LOCKS_WARN_ON(ww->ctx);
196
197 /*
198 * Not quite done after calling ww_acquire_done() ?
199 */
200 DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
201
202 if (ww_ctx->contending_lock) {
203 /*
204 * After -EDEADLK you tried to
205 * acquire a different ww_mutex? Bad!
206 */
207 DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
208
209 /*
210 * You called ww_mutex_lock after receiving -EDEADLK,
211 * but 'forgot' to unlock everything else first?
212 */
213 DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
214 ww_ctx->contending_lock = NULL;
215 }
216
217 /*
218 * Naughty, using a different class will lead to undefined behavior!
219 */
220 DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
221 #endif
222 ww_ctx->acquired++;
223 ww->ctx = ww_ctx;
224 }
225
226 /*
227 * Determine if @a is 'less' than @b. IOW, either @a is a lower priority task
228 * or, when of equal priority, a younger transaction than @b.
229 *
230 * Depending on the algorithm, @a will either need to wait for @b, or die.
231 */
232 static inline bool
__ww_ctx_less(struct ww_acquire_ctx * a,struct ww_acquire_ctx * b)233 __ww_ctx_less(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)
234 {
235 /*
236 * Can only do the RT prio for WW_RT, because task->prio isn't stable due to PI,
237 * so the wait_list ordering will go wobbly. rt_mutex re-queues the waiter and
238 * isn't affected by this.
239 */
240 #ifdef WW_RT
241 /* kernel prio; less is more */
242 int a_prio = a->task->prio;
243 int b_prio = b->task->prio;
244
245 if (rt_or_dl_prio(a_prio) || rt_or_dl_prio(b_prio)) {
246
247 if (a_prio > b_prio)
248 return true;
249
250 if (a_prio < b_prio)
251 return false;
252
253 /* equal static prio */
254
255 if (dl_prio(a_prio)) {
256 if (dl_time_before(b->task->dl.deadline,
257 a->task->dl.deadline))
258 return true;
259
260 if (dl_time_before(a->task->dl.deadline,
261 b->task->dl.deadline))
262 return false;
263 }
264
265 /* equal prio */
266 }
267 #endif
268
269 /* FIFO order tie break -- bigger is younger */
270 return (signed long)(a->stamp - b->stamp) > 0;
271 }
272
273 /*
274 * Wait-Die; wake a lesser waiter context (when locks held) such that it can
275 * die.
276 *
277 * Among waiters with context, only the first one can have other locks acquired
278 * already (ctx->acquired > 0), because __ww_mutex_add_waiter() and
279 * __ww_mutex_check_kill() wake any but the earliest context.
280 */
281 static bool
__ww_mutex_die(struct MUTEX * lock,struct MUTEX_WAITER * waiter,struct ww_acquire_ctx * ww_ctx,struct wake_q_head * wake_q)282 __ww_mutex_die(struct MUTEX *lock, struct MUTEX_WAITER *waiter,
283 struct ww_acquire_ctx *ww_ctx, struct wake_q_head *wake_q)
284 {
285 if (!ww_ctx->is_wait_die)
286 return false;
287
288 if (waiter->ww_ctx->acquired > 0 && __ww_ctx_less(waiter->ww_ctx, ww_ctx)) {
289 #ifndef WW_RT
290 debug_mutex_wake_waiter(lock, waiter);
291 #endif
292 /*
293 * When waking up the task to die, be sure to set the
294 * blocked_on to PROXY_WAKING. Otherwise we can see
295 * circular blocked_on relationships that can't resolve.
296 */
297 set_task_blocked_on_waking(waiter->task, lock);
298 wake_q_add(wake_q, waiter->task);
299 }
300
301 return true;
302 }
303
304 /*
305 * Wound-Wait; wound a lesser @hold_ctx if it holds the lock.
306 *
307 * Wound the lock holder if there are waiters with more important transactions
308 * than the lock holders. Even if multiple waiters may wound the lock holder,
309 * it's sufficient that only one does.
310 */
__ww_mutex_wound(struct MUTEX * lock,struct ww_acquire_ctx * ww_ctx,struct ww_acquire_ctx * hold_ctx,struct wake_q_head * wake_q)311 static bool __ww_mutex_wound(struct MUTEX *lock,
312 struct ww_acquire_ctx *ww_ctx,
313 struct ww_acquire_ctx *hold_ctx,
314 struct wake_q_head *wake_q)
315 __must_hold(&lock->WAIT_LOCK)
316 {
317 struct task_struct *owner = __ww_mutex_owner(lock);
318
319 lockdep_assert_wait_lock_held(lock);
320
321 /*
322 * Possible through __ww_mutex_add_waiter() when we race with
323 * ww_mutex_set_context_fastpath(). In that case we'll get here again
324 * through __ww_mutex_check_waiters().
325 */
326 if (!hold_ctx)
327 return false;
328
329 /*
330 * Can have !owner because of __mutex_unlock_slowpath(), but if owner,
331 * it cannot go away because we'll have FLAG_WAITERS set and hold
332 * wait_lock.
333 */
334 if (!owner)
335 return false;
336
337 if (ww_ctx->acquired > 0 && __ww_ctx_less(hold_ctx, ww_ctx)) {
338 hold_ctx->wounded = 1;
339
340 /*
341 * wake_up_process() paired with set_current_state()
342 * inserts sufficient barriers to make sure @owner either sees
343 * it's wounded in __ww_mutex_check_kill() or has a
344 * wakeup pending to re-read the wounded state.
345 */
346 if (owner != current) {
347 /*
348 * When waking up the task to wound, be sure to set the
349 * blocked_on to PROXY_WAKING. Otherwise we can see
350 * circular blocked_on relationships that can't resolve.
351 *
352 * NOTE: We pass NULL here instead of lock, because we
353 * are waking the mutex owner, who may be currently
354 * blocked on a different mutex.
355 */
356 set_task_blocked_on_waking(owner, NULL);
357 wake_q_add(wake_q, owner);
358 }
359 return true;
360 }
361
362 return false;
363 }
364
365 /*
366 * We just acquired @lock under @ww_ctx, if there are more important contexts
367 * waiting behind us on the wait-list, check if they need to die, or wound us.
368 *
369 * See __ww_mutex_add_waiter() for the list-order construction; basically the
370 * list is ordered by stamp, smallest (oldest) first.
371 *
372 * This relies on never mixing wait-die/wound-wait on the same wait-list;
373 * which is currently ensured by that being a ww_class property.
374 *
375 * The current task must not be on the wait list.
376 */
377 static void
__ww_mutex_check_waiters(struct MUTEX * lock,struct ww_acquire_ctx * ww_ctx,struct wake_q_head * wake_q)378 __ww_mutex_check_waiters(struct MUTEX *lock, struct ww_acquire_ctx *ww_ctx,
379 struct wake_q_head *wake_q)
380 __must_hold(&lock->WAIT_LOCK)
381 {
382 struct MUTEX_WAITER *cur;
383
384 lockdep_assert_wait_lock_held(lock);
385
386 for (cur = __ww_waiter_first(lock); cur;
387 cur = __ww_waiter_next(lock, cur)) {
388
389 if (!cur->ww_ctx)
390 continue;
391
392 if (__ww_mutex_die(lock, cur, ww_ctx, wake_q) ||
393 __ww_mutex_wound(lock, cur->ww_ctx, ww_ctx, wake_q))
394 break;
395 }
396 }
397
398 /*
399 * After acquiring lock with fastpath, where we do not hold wait_lock, set ctx
400 * and wake up any waiters so they can recheck.
401 */
402 static __always_inline void
ww_mutex_set_context_fastpath(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)403 ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
404 {
405 DEFINE_WAKE_Q(wake_q);
406 unsigned long flags;
407 bool has_waiters;
408
409 ww_mutex_lock_acquired(lock, ctx);
410
411 /*
412 * The lock->ctx update should be visible on all cores before
413 * the WAITERS check is done, otherwise contended waiters might be
414 * missed. The contended waiters will either see ww_ctx == NULL
415 * and keep spinning, or it will acquire wait_lock, add itself
416 * to waiter list and sleep.
417 */
418 smp_mb(); /* See comments above and below. */
419
420 /*
421 * [W] ww->ctx = ctx [W] MUTEX_FLAG_WAITERS
422 * MB MB
423 * [R] MUTEX_FLAG_WAITERS [R] ww->ctx
424 *
425 * The memory barrier above pairs with the memory barrier in
426 * __ww_mutex_add_waiter() and makes sure we either observe ww->ctx
427 * and/or !empty list.
428 */
429 has_waiters = data_race(__ww_mutex_has_waiters(&lock->base));
430 if (likely(!has_waiters))
431 return;
432
433 /*
434 * Uh oh, we raced in fastpath, check if any of the waiters need to
435 * die or wound us.
436 */
437 lock_wait_lock(&lock->base, &flags);
438 __ww_mutex_check_waiters(&lock->base, ctx, &wake_q);
439 preempt_disable();
440 unlock_wait_lock(&lock->base, &flags);
441 wake_up_q(&wake_q);
442 preempt_enable();
443 }
444
445 static __always_inline int
__ww_mutex_kill(struct MUTEX * lock,struct ww_acquire_ctx * ww_ctx)446 __ww_mutex_kill(struct MUTEX *lock, struct ww_acquire_ctx *ww_ctx)
447 {
448 if (ww_ctx->acquired > 0) {
449 #ifdef DEBUG_WW_MUTEXES
450 struct ww_mutex *ww;
451
452 ww = container_of(lock, struct ww_mutex, base);
453 DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock);
454 ww_ctx->contending_lock = ww;
455 #endif
456 return -EDEADLK;
457 }
458
459 return 0;
460 }
461
462 /*
463 * Check the wound condition for the current lock acquire.
464 *
465 * Wound-Wait: If we're wounded, kill ourself.
466 *
467 * Wait-Die: If we're trying to acquire a lock already held by an older
468 * context, kill ourselves.
469 *
470 * Since __ww_mutex_add_waiter() orders the wait-list on stamp, we only have to
471 * look at waiters before us in the wait-list.
472 */
473 static inline int
__ww_mutex_check_kill(struct MUTEX * lock,struct MUTEX_WAITER * waiter,struct ww_acquire_ctx * ctx)474 __ww_mutex_check_kill(struct MUTEX *lock, struct MUTEX_WAITER *waiter,
475 struct ww_acquire_ctx *ctx)
476 __must_hold(&lock->WAIT_LOCK)
477 {
478 struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
479 struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx);
480 struct MUTEX_WAITER *cur;
481
482 if (ctx->acquired == 0)
483 return 0;
484
485 if (!ctx->is_wait_die) {
486 if (ctx->wounded)
487 return __ww_mutex_kill(lock, ctx);
488
489 return 0;
490 }
491
492 if (hold_ctx && __ww_ctx_less(ctx, hold_ctx))
493 return __ww_mutex_kill(lock, ctx);
494
495 /*
496 * If there is a waiter in front of us that has a context, then its
497 * stamp is earlier than ours and we must kill ourself.
498 */
499 for (cur = __ww_waiter_prev(lock, waiter); cur;
500 cur = __ww_waiter_prev(lock, cur)) {
501
502 if (!cur->ww_ctx)
503 continue;
504
505 return __ww_mutex_kill(lock, ctx);
506 }
507
508 return 0;
509 }
510
511 /*
512 * Add @waiter to the wait-list, keep the wait-list ordered by stamp, smallest
513 * first. Such that older contexts are preferred to acquire the lock over
514 * younger contexts.
515 *
516 * Waiters without context are interspersed in FIFO order.
517 *
518 * Furthermore, for Wait-Die kill ourself immediately when possible (there are
519 * older contexts already waiting) to avoid unnecessary waiting and for
520 * Wound-Wait ensure we wound the owning context when it is younger.
521 */
522 static inline int
__ww_mutex_add_waiter(struct MUTEX_WAITER * waiter,struct MUTEX * lock,struct ww_acquire_ctx * ww_ctx,struct wake_q_head * wake_q)523 __ww_mutex_add_waiter(struct MUTEX_WAITER *waiter,
524 struct MUTEX *lock,
525 struct ww_acquire_ctx *ww_ctx,
526 struct wake_q_head *wake_q)
527 __must_hold(&lock->WAIT_LOCK)
528 {
529 struct MUTEX_WAITER *cur, *pos = NULL;
530 bool is_wait_die;
531
532 if (!ww_ctx) {
533 __ww_waiter_add(lock, waiter, NULL);
534 return 0;
535 }
536
537 is_wait_die = ww_ctx->is_wait_die;
538
539 /*
540 * Add the waiter before the first waiter with a higher stamp.
541 * Waiters without a context are skipped to avoid starving
542 * them. Wait-Die waiters may die here. Wound-Wait waiters
543 * never die here, but they are sorted in stamp order and
544 * may wound the lock holder.
545 */
546 for (cur = __ww_waiter_last(lock); cur;
547 cur = __ww_waiter_prev(lock, cur)) {
548
549 if (!cur->ww_ctx)
550 continue;
551
552 if (__ww_ctx_less(ww_ctx, cur->ww_ctx)) {
553 /*
554 * Wait-Die: if we find an older context waiting, there
555 * is no point in queueing behind it, as we'd have to
556 * die the moment it would acquire the lock.
557 */
558 if (is_wait_die) {
559 int ret = __ww_mutex_kill(lock, ww_ctx);
560
561 if (ret)
562 return ret;
563 }
564
565 break;
566 }
567
568 pos = cur;
569
570 /* Wait-Die: ensure younger waiters die. */
571 __ww_mutex_die(lock, cur, ww_ctx, wake_q);
572 }
573
574 __ww_waiter_add(lock, waiter, pos);
575
576 /*
577 * Wound-Wait: if we're blocking on a mutex owned by a younger context,
578 * wound that such that we might proceed.
579 */
580 if (!is_wait_die) {
581 struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
582
583 /*
584 * See ww_mutex_set_context_fastpath(). Orders setting
585 * MUTEX_FLAG_WAITERS vs the ww->ctx load,
586 * such that either we or the fastpath will wound @ww->ctx.
587 */
588 smp_mb();
589 __ww_mutex_wound(lock, ww_ctx, ww->ctx, wake_q);
590 }
591
592 return 0;
593 }
594
__ww_mutex_unlock(struct ww_mutex * lock)595 static inline void __ww_mutex_unlock(struct ww_mutex *lock)
596 {
597 if (lock->ctx) {
598 #ifdef DEBUG_WW_MUTEXES
599 DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
600 #endif
601 if (lock->ctx->acquired > 0)
602 lock->ctx->acquired--;
603 lock->ctx = NULL;
604 }
605 }
606