Skip to content

Commit 2674bd1

Browse files
Peter Zijlstra (Intel)ingomolnar
authored andcommitted
locking/ww_mutex: Split out the W/W implementation logic into kernel/locking/ww_mutex.h
Split the W/W mutex helper functions out into a separate header file, so they can be shared with a rtmutex based variant later. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20210815211304.396893399@linutronix.de
1 parent aaa77de commit 2674bd1

2 files changed

Lines changed: 370 additions & 371 deletions

File tree

‎kernel/locking/mutex.c‎

Lines changed: 1 addition & 371 deletions
Original file line numberDiff line numberDiff line change
@@ -281,215 +281,7 @@ void __sched mutex_lock(struct mutex *lock)
281281
EXPORT_SYMBOL(mutex_lock);
282282
#endif
283283

284-
/*
285-
* Wait-Die:
286-
* The newer transactions are killed when:
287-
* It (the new transaction) makes a request for a lock being held
288-
* by an older transaction.
289-
*
290-
* Wound-Wait:
291-
* The newer transactions are wounded when:
292-
* An older transaction makes a request for a lock being held by
293-
* the newer transaction.
294-
*/
295-
296-
/*
297-
* Associate the ww_mutex @ww with the context @ww_ctx under which we acquired
298-
* it.
299-
*/
300-
static __always_inline void
301-
ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
302-
{
303-
#ifdef CONFIG_DEBUG_MUTEXES
304-
/*
305-
* If this WARN_ON triggers, you used ww_mutex_lock to acquire,
306-
* but released with a normal mutex_unlock in this call.
307-
*
308-
* This should never happen, always use ww_mutex_unlock.
309-
*/
310-
DEBUG_LOCKS_WARN_ON(ww->ctx);
311-
312-
/*
313-
* Not quite done after calling ww_acquire_done() ?
314-
*/
315-
DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
316-
317-
if (ww_ctx->contending_lock) {
318-
/*
319-
* After -EDEADLK you tried to
320-
* acquire a different ww_mutex? Bad!
321-
*/
322-
DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
323-
324-
/*
325-
* You called ww_mutex_lock after receiving -EDEADLK,
326-
* but 'forgot' to unlock everything else first?
327-
*/
328-
DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
329-
ww_ctx->contending_lock = NULL;
330-
}
331-
332-
/*
333-
* Naughty, using a different class will lead to undefined behavior!
334-
*/
335-
DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
336-
#endif
337-
ww_ctx->acquired++;
338-
ww->ctx = ww_ctx;
339-
}
340-
341-
/*
342-
* Determine if context @a is 'after' context @b. IOW, @a is a younger
343-
* transaction than @b and depending on algorithm either needs to wait for
344-
* @b or die.
345-
*/
346-
static inline bool __sched
347-
__ww_ctx_stamp_after(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)
348-
{
349-
350-
return (signed long)(a->stamp - b->stamp) > 0;
351-
}
352-
353-
/*
354-
* Wait-Die; wake a younger waiter context (when locks held) such that it can
355-
* die.
356-
*
357-
* Among waiters with context, only the first one can have other locks acquired
358-
* already (ctx->acquired > 0), because __ww_mutex_add_waiter() and
359-
* __ww_mutex_check_kill() wake any but the earliest context.
360-
*/
361-
static bool __sched
362-
__ww_mutex_die(struct mutex *lock, struct mutex_waiter *waiter,
363-
struct ww_acquire_ctx *ww_ctx)
364-
{
365-
if (!ww_ctx->is_wait_die)
366-
return false;
367-
368-
if (waiter->ww_ctx->acquired > 0 &&
369-
__ww_ctx_stamp_after(waiter->ww_ctx, ww_ctx)) {
370-
debug_mutex_wake_waiter(lock, waiter);
371-
wake_up_process(waiter->task);
372-
}
373-
374-
return true;
375-
}
376-
377-
/*
378-
* Wound-Wait; wound a younger @hold_ctx if it holds the lock.
379-
*
380-
* Wound the lock holder if there are waiters with older transactions than
381-
* the lock holders. Even if multiple waiters may wound the lock holder,
382-
* it's sufficient that only one does.
383-
*/
384-
static bool __ww_mutex_wound(struct mutex *lock,
385-
struct ww_acquire_ctx *ww_ctx,
386-
struct ww_acquire_ctx *hold_ctx)
387-
{
388-
struct task_struct *owner = __mutex_owner(lock);
389-
390-
lockdep_assert_held(&lock->wait_lock);
391-
392-
/*
393-
* Possible through __ww_mutex_add_waiter() when we race with
394-
* ww_mutex_set_context_fastpath(). In that case we'll get here again
395-
* through __ww_mutex_check_waiters().
396-
*/
397-
if (!hold_ctx)
398-
return false;
399-
400-
/*
401-
* Can have !owner because of __mutex_unlock_slowpath(), but if owner,
402-
* it cannot go away because we'll have FLAG_WAITERS set and hold
403-
* wait_lock.
404-
*/
405-
if (!owner)
406-
return false;
407-
408-
if (ww_ctx->acquired > 0 && __ww_ctx_stamp_after(hold_ctx, ww_ctx)) {
409-
hold_ctx->wounded = 1;
410-
411-
/*
412-
* wake_up_process() paired with set_current_state()
413-
* inserts sufficient barriers to make sure @owner either sees
414-
* it's wounded in __ww_mutex_check_kill() or has a
415-
* wakeup pending to re-read the wounded state.
416-
*/
417-
if (owner != current)
418-
wake_up_process(owner);
419-
420-
return true;
421-
}
422-
423-
return false;
424-
}
425-
426-
/*
427-
* We just acquired @lock under @ww_ctx, if there are later contexts waiting
428-
* behind us on the wait-list, check if they need to die, or wound us.
429-
*
430-
* See __ww_mutex_add_waiter() for the list-order construction; basically the
431-
* list is ordered by stamp, smallest (oldest) first.
432-
*
433-
* This relies on never mixing wait-die/wound-wait on the same wait-list;
434-
* which is currently ensured by that being a ww_class property.
435-
*
436-
* The current task must not be on the wait list.
437-
*/
438-
static void __sched
439-
__ww_mutex_check_waiters(struct mutex *lock, struct ww_acquire_ctx *ww_ctx)
440-
{
441-
struct mutex_waiter *cur;
442-
443-
lockdep_assert_held(&lock->wait_lock);
444-
445-
list_for_each_entry(cur, &lock->wait_list, list) {
446-
if (!cur->ww_ctx)
447-
continue;
448-
449-
if (__ww_mutex_die(lock, cur, ww_ctx) ||
450-
__ww_mutex_wound(lock, cur->ww_ctx, ww_ctx))
451-
break;
452-
}
453-
}
454-
455-
/*
456-
* After acquiring lock with fastpath, where we do not hold wait_lock, set ctx
457-
* and wake up any waiters so they can recheck.
458-
*/
459-
static __always_inline void
460-
ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
461-
{
462-
ww_mutex_lock_acquired(lock, ctx);
463-
464-
/*
465-
* The lock->ctx update should be visible on all cores before
466-
* the WAITERS check is done, otherwise contended waiters might be
467-
* missed. The contended waiters will either see ww_ctx == NULL
468-
* and keep spinning, or it will acquire wait_lock, add itself
469-
* to waiter list and sleep.
470-
*/
471-
smp_mb(); /* See comments above and below. */
472-
473-
/*
474-
* [W] ww->ctx = ctx [W] MUTEX_FLAG_WAITERS
475-
* MB MB
476-
* [R] MUTEX_FLAG_WAITERS [R] ww->ctx
477-
*
478-
* The memory barrier above pairs with the memory barrier in
479-
* __ww_mutex_add_waiter() and makes sure we either observe ww->ctx
480-
* and/or !empty list.
481-
*/
482-
if (likely(!(atomic_long_read(&lock->base.owner) & MUTEX_FLAG_WAITERS)))
483-
return;
484-
485-
/*
486-
* Uh oh, we raced in fastpath, check if any of the waiters need to
487-
* die or wound us.
488-
*/
489-
raw_spin_lock(&lock->base.wait_lock);
490-
__ww_mutex_check_waiters(&lock->base, ctx);
491-
raw_spin_unlock(&lock->base.wait_lock);
492-
}
284+
#include "ww_mutex.h"
493285

494286
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
495287

@@ -744,20 +536,6 @@ void __sched mutex_unlock(struct mutex *lock)
744536
}
745537
EXPORT_SYMBOL(mutex_unlock);
746538

747-
static void __ww_mutex_unlock(struct ww_mutex *lock)
748-
{
749-
/*
750-
* The unlocking fastpath is the 0->1 transition from 'locked'
751-
* into 'unlocked' state:
752-
*/
753-
if (lock->ctx) {
754-
MUTEX_WARN_ON(!lock->ctx->acquired);
755-
if (lock->ctx->acquired > 0)
756-
lock->ctx->acquired--;
757-
lock->ctx = NULL;
758-
}
759-
}
760-
761539
/**
762540
* ww_mutex_unlock - release the w/w mutex
763541
* @lock: the mutex to be released
@@ -776,154 +554,6 @@ void __sched ww_mutex_unlock(struct ww_mutex *lock)
776554
}
777555
EXPORT_SYMBOL(ww_mutex_unlock);
778556

779-
780-
static __always_inline int __sched
781-
__ww_mutex_kill(struct mutex *lock, struct ww_acquire_ctx *ww_ctx)
782-
{
783-
if (ww_ctx->acquired > 0) {
784-
#ifdef CONFIG_DEBUG_MUTEXES
785-
struct ww_mutex *ww;
786-
787-
ww = container_of(lock, struct ww_mutex, base);
788-
DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock);
789-
ww_ctx->contending_lock = ww;
790-
#endif
791-
return -EDEADLK;
792-
}
793-
794-
return 0;
795-
}
796-
797-
798-
/*
799-
* Check the wound condition for the current lock acquire.
800-
*
801-
* Wound-Wait: If we're wounded, kill ourself.
802-
*
803-
* Wait-Die: If we're trying to acquire a lock already held by an older
804-
* context, kill ourselves.
805-
*
806-
* Since __ww_mutex_add_waiter() orders the wait-list on stamp, we only have to
807-
* look at waiters before us in the wait-list.
808-
*/
809-
static inline int __sched
810-
__ww_mutex_check_kill(struct mutex *lock, struct mutex_waiter *waiter,
811-
struct ww_acquire_ctx *ctx)
812-
{
813-
struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
814-
struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx);
815-
struct mutex_waiter *cur;
816-
817-
if (ctx->acquired == 0)
818-
return 0;
819-
820-
if (!ctx->is_wait_die) {
821-
if (ctx->wounded)
822-
return __ww_mutex_kill(lock, ctx);
823-
824-
return 0;
825-
}
826-
827-
if (hold_ctx && __ww_ctx_stamp_after(ctx, hold_ctx))
828-
return __ww_mutex_kill(lock, ctx);
829-
830-
/*
831-
* If there is a waiter in front of us that has a context, then its
832-
* stamp is earlier than ours and we must kill ourself.
833-
*/
834-
cur = waiter;
835-
list_for_each_entry_continue_reverse(cur, &lock->wait_list, list) {
836-
if (!cur->ww_ctx)
837-
continue;
838-
839-
return __ww_mutex_kill(lock, ctx);
840-
}
841-
842-
return 0;
843-
}
844-
845-
/*
846-
* Add @waiter to the wait-list, keep the wait-list ordered by stamp, smallest
847-
* first. Such that older contexts are preferred to acquire the lock over
848-
* younger contexts.
849-
*
850-
* Waiters without context are interspersed in FIFO order.
851-
*
852-
* Furthermore, for Wait-Die kill ourself immediately when possible (there are
853-
* older contexts already waiting) to avoid unnecessary waiting and for
854-
* Wound-Wait ensure we wound the owning context when it is younger.
855-
*/
856-
static inline int __sched
857-
__ww_mutex_add_waiter(struct mutex_waiter *waiter,
858-
struct mutex *lock,
859-
struct ww_acquire_ctx *ww_ctx)
860-
{
861-
struct mutex_waiter *cur;
862-
struct list_head *pos;
863-
bool is_wait_die;
864-
865-
if (!ww_ctx) {
866-
__mutex_add_waiter(lock, waiter, &lock->wait_list);
867-
return 0;
868-
}
869-
870-
is_wait_die = ww_ctx->is_wait_die;
871-
872-
/*
873-
* Add the waiter before the first waiter with a higher stamp.
874-
* Waiters without a context are skipped to avoid starving
875-
* them. Wait-Die waiters may die here. Wound-Wait waiters
876-
* never die here, but they are sorted in stamp order and
877-
* may wound the lock holder.
878-
*/
879-
pos = &lock->wait_list;
880-
list_for_each_entry_reverse(cur, &lock->wait_list, list) {
881-
if (!cur->ww_ctx)
882-
continue;
883-
884-
if (__ww_ctx_stamp_after(ww_ctx, cur->ww_ctx)) {
885-
/*
886-
* Wait-Die: if we find an older context waiting, there
887-
* is no point in queueing behind it, as we'd have to
888-
* die the moment it would acquire the lock.
889-
*/
890-
if (is_wait_die) {
891-
int ret = __ww_mutex_kill(lock, ww_ctx);
892-
893-
if (ret)
894-
return ret;
895-
}
896-
897-
break;
898-
}
899-
900-
pos = &cur->list;
901-
902-
/* Wait-Die: ensure younger waiters die. */
903-
__ww_mutex_die(lock, cur, ww_ctx);
904-
}
905-
906-
__mutex_add_waiter(lock, waiter, pos);
907-
908-
/*
909-
* Wound-Wait: if we're blocking on a mutex owned by a younger context,
910-
* wound that such that we might proceed.
911-
*/
912-
if (!is_wait_die) {
913-
struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
914-
915-
/*
916-
* See ww_mutex_set_context_fastpath(). Orders setting
917-
* MUTEX_FLAG_WAITERS vs the ww->ctx load,
918-
* such that either we or the fastpath will wound @ww->ctx.
919-
*/
920-
smp_mb();
921-
__ww_mutex_wound(lock, ww_ctx, ww->ctx);
922-
}
923-
924-
return 0;
925-
}
926-
927557
/*
928558
* Lock a mutex (possibly interruptible), slowpath:
929559
*/

0 commit comments

Comments
 (0)