/* SPDX-License-Identifier: GPL-2.0-only */12#ifndef WW_RT34#define MUTEX mutex5#define MUTEX_WAITER mutex_waiter67static inline struct mutex_waiter *8__ww_waiter_first(struct mutex *lock)9{10struct mutex_waiter *w;1112w = list_first_entry(&lock->wait_list, struct mutex_waiter, list);13if (list_entry_is_head(w, &lock->wait_list, list))14return NULL;1516return w;17}1819static inline struct mutex_waiter *20__ww_waiter_next(struct mutex *lock, struct mutex_waiter *w)21{22w = list_next_entry(w, list);23if (list_entry_is_head(w, &lock->wait_list, list))24return NULL;2526return w;27}2829static inline struct mutex_waiter *30__ww_waiter_prev(struct mutex *lock, struct mutex_waiter *w)31{32w = list_prev_entry(w, list);33if (list_entry_is_head(w, &lock->wait_list, list))34return NULL;3536return w;37}3839static inline struct mutex_waiter *40__ww_waiter_last(struct mutex *lock)41{42struct mutex_waiter *w;4344w = list_last_entry(&lock->wait_list, struct mutex_waiter, list);45if (list_entry_is_head(w, &lock->wait_list, list))46return NULL;4748return w;49}5051static inline void52__ww_waiter_add(struct mutex *lock, struct mutex_waiter *waiter, struct mutex_waiter *pos)53{54struct list_head *p = &lock->wait_list;55if (pos)56p = &pos->list;57__mutex_add_waiter(lock, waiter, p);58}5960static inline struct task_struct *61__ww_mutex_owner(struct mutex *lock)62{63return __mutex_owner(lock);64}6566static inline bool67__ww_mutex_has_waiters(struct mutex *lock)68{69return atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS;70}7172static inline void lock_wait_lock(struct mutex *lock, unsigned long *flags)73{74raw_spin_lock_irqsave(&lock->wait_lock, *flags);75}7677static inline void unlock_wait_lock(struct mutex *lock, unsigned long *flags)78{79raw_spin_unlock_irqrestore(&lock->wait_lock, *flags);80}8182static inline void lockdep_assert_wait_lock_held(struct mutex *lock)83{84lockdep_assert_held(&lock->wait_lock);85}8687#else /* WW_RT */8889#define MUTEX rt_mutex90#define MUTEX_WAITER rt_mutex_waiter9192static inline struct rt_mutex_waiter *93__ww_waiter_first(struct rt_mutex *lock)94{95struct rb_node *n = rb_first(&lock->rtmutex.waiters.rb_root);96if (!n)97return NULL;98return rb_entry(n, struct rt_mutex_waiter, tree.entry);99}100101static inline struct rt_mutex_waiter *102__ww_waiter_next(struct rt_mutex *lock, struct rt_mutex_waiter *w)103{104struct rb_node *n = rb_next(&w->tree.entry);105if (!n)106return NULL;107return rb_entry(n, struct rt_mutex_waiter, tree.entry);108}109110static inline struct rt_mutex_waiter *111__ww_waiter_prev(struct rt_mutex *lock, struct rt_mutex_waiter *w)112{113struct rb_node *n = rb_prev(&w->tree.entry);114if (!n)115return NULL;116return rb_entry(n, struct rt_mutex_waiter, tree.entry);117}118119static inline struct rt_mutex_waiter *120__ww_waiter_last(struct rt_mutex *lock)121{122struct rb_node *n = rb_last(&lock->rtmutex.waiters.rb_root);123if (!n)124return NULL;125return rb_entry(n, struct rt_mutex_waiter, tree.entry);126}127128static inline void129__ww_waiter_add(struct rt_mutex *lock, struct rt_mutex_waiter *waiter, struct rt_mutex_waiter *pos)130{131/* RT unconditionally adds the waiter first and then removes it on error */132}133134static inline struct task_struct *135__ww_mutex_owner(struct rt_mutex *lock)136{137return rt_mutex_owner(&lock->rtmutex);138}139140static inline bool141__ww_mutex_has_waiters(struct rt_mutex *lock)142{143return rt_mutex_has_waiters(&lock->rtmutex);144}145146static inline void lock_wait_lock(struct rt_mutex *lock, unsigned long *flags)147{148raw_spin_lock_irqsave(&lock->rtmutex.wait_lock, *flags);149}150151static inline void unlock_wait_lock(struct rt_mutex *lock, unsigned long *flags)152{153raw_spin_unlock_irqrestore(&lock->rtmutex.wait_lock, *flags);154}155156static inline void lockdep_assert_wait_lock_held(struct rt_mutex *lock)157{158lockdep_assert_held(&lock->rtmutex.wait_lock);159}160161#endif /* WW_RT */162163/*164* Wait-Die:165* The newer transactions are killed when:166* It (the new transaction) makes a request for a lock being held167* by an older transaction.168*169* Wound-Wait:170* The newer transactions are wounded when:171* An older transaction makes a request for a lock being held by172* the newer transaction.173*/174175/*176* Associate the ww_mutex @ww with the context @ww_ctx under which we acquired177* it.178*/179static __always_inline void180ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)181{182#ifdef DEBUG_WW_MUTEXES183/*184* If this WARN_ON triggers, you used ww_mutex_lock to acquire,185* but released with a normal mutex_unlock in this call.186*187* This should never happen, always use ww_mutex_unlock.188*/189DEBUG_LOCKS_WARN_ON(ww->ctx);190191/*192* Not quite done after calling ww_acquire_done() ?193*/194DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);195196if (ww_ctx->contending_lock) {197/*198* After -EDEADLK you tried to199* acquire a different ww_mutex? Bad!200*/201DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);202203/*204* You called ww_mutex_lock after receiving -EDEADLK,205* but 'forgot' to unlock everything else first?206*/207DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);208ww_ctx->contending_lock = NULL;209}210211/*212* Naughty, using a different class will lead to undefined behavior!213*/214DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);215#endif216ww_ctx->acquired++;217ww->ctx = ww_ctx;218}219220/*221* Determine if @a is 'less' than @b. IOW, either @a is a lower priority task222* or, when of equal priority, a younger transaction than @b.223*224* Depending on the algorithm, @a will either need to wait for @b, or die.225*/226static inline bool227__ww_ctx_less(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)228{229/*230* Can only do the RT prio for WW_RT, because task->prio isn't stable due to PI,231* so the wait_list ordering will go wobbly. rt_mutex re-queues the waiter and232* isn't affected by this.233*/234#ifdef WW_RT235/* kernel prio; less is more */236int a_prio = a->task->prio;237int b_prio = b->task->prio;238239if (rt_or_dl_prio(a_prio) || rt_or_dl_prio(b_prio)) {240241if (a_prio > b_prio)242return true;243244if (a_prio < b_prio)245return false;246247/* equal static prio */248249if (dl_prio(a_prio)) {250if (dl_time_before(b->task->dl.deadline,251a->task->dl.deadline))252return true;253254if (dl_time_before(a->task->dl.deadline,255b->task->dl.deadline))256return false;257}258259/* equal prio */260}261#endif262263/* FIFO order tie break -- bigger is younger */264return (signed long)(a->stamp - b->stamp) > 0;265}266267/*268* Wait-Die; wake a lesser waiter context (when locks held) such that it can269* die.270*271* Among waiters with context, only the first one can have other locks acquired272* already (ctx->acquired > 0), because __ww_mutex_add_waiter() and273* __ww_mutex_check_kill() wake any but the earliest context.274*/275static bool276__ww_mutex_die(struct MUTEX *lock, struct MUTEX_WAITER *waiter,277struct ww_acquire_ctx *ww_ctx, struct wake_q_head *wake_q)278{279if (!ww_ctx->is_wait_die)280return false;281282if (waiter->ww_ctx->acquired > 0 && __ww_ctx_less(waiter->ww_ctx, ww_ctx)) {283#ifndef WW_RT284debug_mutex_wake_waiter(lock, waiter);285#endif286/*287* When waking up the task to die, be sure to clear the288* blocked_on pointer. Otherwise we can see circular289* blocked_on relationships that can't resolve.290*/291__clear_task_blocked_on(waiter->task, lock);292wake_q_add(wake_q, waiter->task);293}294295return true;296}297298/*299* Wound-Wait; wound a lesser @hold_ctx if it holds the lock.300*301* Wound the lock holder if there are waiters with more important transactions302* than the lock holders. Even if multiple waiters may wound the lock holder,303* it's sufficient that only one does.304*/305static bool __ww_mutex_wound(struct MUTEX *lock,306struct ww_acquire_ctx *ww_ctx,307struct ww_acquire_ctx *hold_ctx,308struct wake_q_head *wake_q)309{310struct task_struct *owner = __ww_mutex_owner(lock);311312lockdep_assert_wait_lock_held(lock);313314/*315* Possible through __ww_mutex_add_waiter() when we race with316* ww_mutex_set_context_fastpath(). In that case we'll get here again317* through __ww_mutex_check_waiters().318*/319if (!hold_ctx)320return false;321322/*323* Can have !owner because of __mutex_unlock_slowpath(), but if owner,324* it cannot go away because we'll have FLAG_WAITERS set and hold325* wait_lock.326*/327if (!owner)328return false;329330if (ww_ctx->acquired > 0 && __ww_ctx_less(hold_ctx, ww_ctx)) {331hold_ctx->wounded = 1;332333/*334* wake_up_process() paired with set_current_state()335* inserts sufficient barriers to make sure @owner either sees336* it's wounded in __ww_mutex_check_kill() or has a337* wakeup pending to re-read the wounded state.338*/339if (owner != current) {340/*341* When waking up the task to wound, be sure to clear the342* blocked_on pointer. Otherwise we can see circular343* blocked_on relationships that can't resolve.344*345* NOTE: We pass NULL here instead of lock, because we346* are waking the mutex owner, who may be currently347* blocked on a different mutex.348*/349__clear_task_blocked_on(owner, NULL);350wake_q_add(wake_q, owner);351}352return true;353}354355return false;356}357358/*359* We just acquired @lock under @ww_ctx, if there are more important contexts360* waiting behind us on the wait-list, check if they need to die, or wound us.361*362* See __ww_mutex_add_waiter() for the list-order construction; basically the363* list is ordered by stamp, smallest (oldest) first.364*365* This relies on never mixing wait-die/wound-wait on the same wait-list;366* which is currently ensured by that being a ww_class property.367*368* The current task must not be on the wait list.369*/370static void371__ww_mutex_check_waiters(struct MUTEX *lock, struct ww_acquire_ctx *ww_ctx,372struct wake_q_head *wake_q)373{374struct MUTEX_WAITER *cur;375376lockdep_assert_wait_lock_held(lock);377378for (cur = __ww_waiter_first(lock); cur;379cur = __ww_waiter_next(lock, cur)) {380381if (!cur->ww_ctx)382continue;383384if (__ww_mutex_die(lock, cur, ww_ctx, wake_q) ||385__ww_mutex_wound(lock, cur->ww_ctx, ww_ctx, wake_q))386break;387}388}389390/*391* After acquiring lock with fastpath, where we do not hold wait_lock, set ctx392* and wake up any waiters so they can recheck.393*/394static __always_inline void395ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)396{397DEFINE_WAKE_Q(wake_q);398unsigned long flags;399400ww_mutex_lock_acquired(lock, ctx);401402/*403* The lock->ctx update should be visible on all cores before404* the WAITERS check is done, otherwise contended waiters might be405* missed. The contended waiters will either see ww_ctx == NULL406* and keep spinning, or it will acquire wait_lock, add itself407* to waiter list and sleep.408*/409smp_mb(); /* See comments above and below. */410411/*412* [W] ww->ctx = ctx [W] MUTEX_FLAG_WAITERS413* MB MB414* [R] MUTEX_FLAG_WAITERS [R] ww->ctx415*416* The memory barrier above pairs with the memory barrier in417* __ww_mutex_add_waiter() and makes sure we either observe ww->ctx418* and/or !empty list.419*/420if (likely(!__ww_mutex_has_waiters(&lock->base)))421return;422423/*424* Uh oh, we raced in fastpath, check if any of the waiters need to425* die or wound us.426*/427lock_wait_lock(&lock->base, &flags);428__ww_mutex_check_waiters(&lock->base, ctx, &wake_q);429preempt_disable();430unlock_wait_lock(&lock->base, &flags);431wake_up_q(&wake_q);432preempt_enable();433}434435static __always_inline int436__ww_mutex_kill(struct MUTEX *lock, struct ww_acquire_ctx *ww_ctx)437{438if (ww_ctx->acquired > 0) {439#ifdef DEBUG_WW_MUTEXES440struct ww_mutex *ww;441442ww = container_of(lock, struct ww_mutex, base);443DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock);444ww_ctx->contending_lock = ww;445#endif446return -EDEADLK;447}448449return 0;450}451452/*453* Check the wound condition for the current lock acquire.454*455* Wound-Wait: If we're wounded, kill ourself.456*457* Wait-Die: If we're trying to acquire a lock already held by an older458* context, kill ourselves.459*460* Since __ww_mutex_add_waiter() orders the wait-list on stamp, we only have to461* look at waiters before us in the wait-list.462*/463static inline int464__ww_mutex_check_kill(struct MUTEX *lock, struct MUTEX_WAITER *waiter,465struct ww_acquire_ctx *ctx)466{467struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);468struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx);469struct MUTEX_WAITER *cur;470471if (ctx->acquired == 0)472return 0;473474if (!ctx->is_wait_die) {475if (ctx->wounded)476return __ww_mutex_kill(lock, ctx);477478return 0;479}480481if (hold_ctx && __ww_ctx_less(ctx, hold_ctx))482return __ww_mutex_kill(lock, ctx);483484/*485* If there is a waiter in front of us that has a context, then its486* stamp is earlier than ours and we must kill ourself.487*/488for (cur = __ww_waiter_prev(lock, waiter); cur;489cur = __ww_waiter_prev(lock, cur)) {490491if (!cur->ww_ctx)492continue;493494return __ww_mutex_kill(lock, ctx);495}496497return 0;498}499500/*501* Add @waiter to the wait-list, keep the wait-list ordered by stamp, smallest502* first. Such that older contexts are preferred to acquire the lock over503* younger contexts.504*505* Waiters without context are interspersed in FIFO order.506*507* Furthermore, for Wait-Die kill ourself immediately when possible (there are508* older contexts already waiting) to avoid unnecessary waiting and for509* Wound-Wait ensure we wound the owning context when it is younger.510*/511static inline int512__ww_mutex_add_waiter(struct MUTEX_WAITER *waiter,513struct MUTEX *lock,514struct ww_acquire_ctx *ww_ctx,515struct wake_q_head *wake_q)516{517struct MUTEX_WAITER *cur, *pos = NULL;518bool is_wait_die;519520if (!ww_ctx) {521__ww_waiter_add(lock, waiter, NULL);522return 0;523}524525is_wait_die = ww_ctx->is_wait_die;526527/*528* Add the waiter before the first waiter with a higher stamp.529* Waiters without a context are skipped to avoid starving530* them. Wait-Die waiters may die here. Wound-Wait waiters531* never die here, but they are sorted in stamp order and532* may wound the lock holder.533*/534for (cur = __ww_waiter_last(lock); cur;535cur = __ww_waiter_prev(lock, cur)) {536537if (!cur->ww_ctx)538continue;539540if (__ww_ctx_less(ww_ctx, cur->ww_ctx)) {541/*542* Wait-Die: if we find an older context waiting, there543* is no point in queueing behind it, as we'd have to544* die the moment it would acquire the lock.545*/546if (is_wait_die) {547int ret = __ww_mutex_kill(lock, ww_ctx);548549if (ret)550return ret;551}552553break;554}555556pos = cur;557558/* Wait-Die: ensure younger waiters die. */559__ww_mutex_die(lock, cur, ww_ctx, wake_q);560}561562__ww_waiter_add(lock, waiter, pos);563564/*565* Wound-Wait: if we're blocking on a mutex owned by a younger context,566* wound that such that we might proceed.567*/568if (!is_wait_die) {569struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);570571/*572* See ww_mutex_set_context_fastpath(). Orders setting573* MUTEX_FLAG_WAITERS vs the ww->ctx load,574* such that either we or the fastpath will wound @ww->ctx.575*/576smp_mb();577__ww_mutex_wound(lock, ww_ctx, ww->ctx, wake_q);578}579580return 0;581}582583static inline void __ww_mutex_unlock(struct ww_mutex *lock)584{585if (lock->ctx) {586#ifdef DEBUG_WW_MUTEXES587DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);588#endif589if (lock->ctx->acquired > 0)590lock->ctx->acquired--;591lock->ctx = NULL;592}593}594595596