locking: Fix ww_mutex algorithm inconsistency.
Subsystems (7)
cover locking: Fix ww_mutex algorithm inconsistency.
The algorithm used for linux Wound/Wait mutexes, is actually not Wound/Wait but Wait/Die. See for example
http://www.mathcs.emory.edu/~cheung/Courses/554/Syllabus/8-recv+serial/deadlock-compare.html
Rather than renaming them across the tree to something like Wait/Die mutexes or Deadlock Avoidance mutexes, this patch set implements also the Wound/Wait algorithm It shouldn't touch the binary ordinary mutex paths when compiled with otpimization.
We use Wound/Wait for the modeset locks which in theory may benefit slightly from Wound/Wait rather than Wait/Die, but Wait/Die is actually superior for larger number of simultaneous contending transactions so we keep the original implementation as a choice, and don't touch the algorithm used by the reservation objects.
Performance- and functional testing has been done using
git://people.freedesktop.org/~thomash/ww_mutex_test
Using WW_BUILTIN and 4 and 16 competing threads.
Another option is of course to ignore the the naming / algorithm inconsistency.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
RFC PATCH 1/2 locking: Implement an algorithm choice for Wound-Wait mutexes
The current Wound-Wait mutex algorithm is actually not Wound-Wait but Wait-Die. Implement also Wound-Wait as a per-ww-class choice. Wound-Wait is, contrary to Wait-Die a preemptive algorithm and is known to generate fewer backoffs in some cases. Testing reveals that this is true if the number of simultaneous contending transactions is small.
As the number of simultaneous contending threads increases, Wait-Wound becomes inferior to Wait-Die, probably due to the larger number of held locks of sleeping transactions.
Update documentation and callers.
--- Documentation/locking/ww-mutex-design.txt | 57 +++++++++++++---- drivers/dma-buf/reservation.c | 2 +- drivers/gpu/drm/drm_modeset_lock.c | 2 +- include/linux/ww_mutex.h | 19 ++++-- kernel/locking/locktorture.c | 2 +- kernel/locking/mutex.c | 101 +++++++++++++++++++++++++++--- kernel/locking/test-ww_mutex.c | 2 +- lib/locking-selftest.c | 2 +- 8 files changed, 154 insertions(+), 33 deletions(-)
Documentation/locking/ww-mutex-design.txt +44 -13
@@ -1,4 +1,4 @@ -Wait/Wound Deadlock-Proof Mutex Design +Wound/Wait Deadlock-Proof Mutex Design ====================================== Please read mutex-design.txt first, as it applies to wait/wound mutexes too.
@@ -32,10 +32,23 @@ the oldest task) wins, and the one with the higher reservation id (i.e. the younger task) unlocks all of the buffers that it has already locked, and then tries again. -In the RDBMS literature this deadlock handling approach is called wait/wound: -The older tasks waits until it can acquire the contended lock. The younger tasks -needs to back off and drop all the locks it is currently holding, i.e. the -younger task is wounded. +In the RDBMS literature, a reservation ticket is associated with a transaction. +and the deadlock handling approach is called Wait-Die. The name is based on +the actions of a locking thread when it encounters an already locked mutex. +If the transaction holding the lock is younger, the locking transaction waits. +If the transaction holding the lock is older, the locking transaction backs off +and dies. Hence Wait-Die. +There is also another algorithm called Wound-Wait: +If the transaction holding the lock is younger, the locking transaction +preempts the transaction holding the lock, requiring it to back off. It +Wounds the other transaction. +If the transaction holding the lock is older, it waits for the other +transaction. Hence Wound-Wait. +The two algorithms are both fair in that a transaction will eventually succeed. +However, the Wound-Wait algorithm is typically stated to generate fewer backoffs +compared to Wait-Die, but is, on the other hand, associated with more work than +Wait-Die when recovering from a backoff. Wound-Wait is also a preemptive +algorithm which requires a reliable way to preempt another transaction. Concepts --------
@@ -47,10 +60,12 @@ Acquire context: To ensure eventual forward progress it is important the a task trying to acquire locks doesn't grab a new reservation id, but keeps the one it acquired when starting the lock acquisition. This ticket is stored in the acquire context. Furthermore the acquire context keeps track of debugging state -to catch w/w mutex interface abuse. +to catch w/w mutex interface abuse. An acquire context is representing a +transaction. W/w class: In contrast to normal mutexes the lock class needs to be explicit for -w/w mutexes, since it is required to initialize the acquire context. +w/w mutexes, since it is required to initialize the acquire context. The lock +class also specifies what algorithm to use, Wound-Wait or Wait-Die. Furthermore there are three different class of w/w lock acquire functions:
@@ -90,10 +105,15 @@ provided. Usage ----- +The algorithm (Wait-Die vs Wound-Wait) is chosen using the _is_wait_die +argument to DEFINE_WW_CLASS(). As a rough rule of thumb, use Wound-Wait iff you +typically expect the number of simultaneous competing transactions to be small, +and the rollback cost can be substantial. + Three different ways to acquire locks within the same w/w class. Common definitions for methods #1 and #2: -static DEFINE_WW_CLASS(ww_class); +static DEFINE_WW_CLASS(ww_class, false); struct obj { struct ww_mutex lock;
@@ -243,7 +263,7 @@ struct obj { struct list_head locked_list; }; -static DEFINE_WW_CLASS(ww_class); +static DEFINE_WW_CLASS(ww_class, false); void __unlock_objs(struct list_head *list) {
@@ -312,12 +332,23 @@ Design: We maintain the following invariants for the wait list: (1) Waiters with an acquire context are sorted by stamp order; waiters without an acquire context are interspersed in FIFO order. - (2) Among waiters with contexts, only the first one can have other locks - acquired already (ctx->acquired > 0). Note that this waiter may come - after other waiters without contexts in the list. + (2) For Wait-Die, among waiters with contexts, only the first one can have + other locks acquired already (ctx->acquired > 0). Note that this waiter + may come after other waiters without contexts in the list. + + The Wound-Wait preemption is implemented with a lazy-preemption scheme: + The wounded status of the transaction is checked only when there is + contention for a new lock and hence a true chance of deadlock. In that + situation, if the transaction is wounded, it backs off, clears the + wounded status and retries. A great benefit of implementing preemption in + this way is that the wounded transaction can identify a contending lock to + wait for before restarting the transaction. Just blindly restarting the + transaction would likely make the transaction end up in a situation where + it would have to back off again. In general, not much contention is expected. The locks are typically used to - serialize access to resources for devices. + serialize access to resources for devices, and optimization focus should + therefore be directed towards the uncontended cases. Lockdep: Special care has been taken to warn for as many cases of api abuse
drivers/dma-buf/reservation.c +1 -1
@@ -46,7 +46,7 @@* write-side updates. */ -DEFINE_WW_CLASS (reservation_ww_class); +DEFINE_WW_CLASS (reservation_ww_class, true);EXPORT_SYMBOL (reservation_ww_class);struct lock_class_key reservation_seqcount_class;
drivers/gpu/drm/drm_modeset_lock.c +1 -1
@@ -70,7 +70,7 @@* lists and lookup data structures. */ -static DEFINE_WW_CLASS (crtc_ww_class); +static DEFINE_WW_CLASS(crtc_ww_class, true);/** * drm_modeset_lock_all - take all modeset locks
include/linux/ww_mutex.h +13 -6
@@ -8,6 +8,8 @@* * Wound/wait implementation: * Copyright (C) 2013 Canonical Ltd. +* Choice of algorithm: +* Copyright (C) 2018 WMWare Inc. * * This file contains the main data structure and API definitions. */
@@ -23,15 +25,17 @@ struct ww_class {struct lock_class_key mutex_key;const char *acquire_name;const char *mutex_name; +bool is_wait_die; };struct ww_acquire_ctx {struct task_struct *task;unsigned long stamp;unsigned acquired; +bool wounded; +struct ww_class *ww_class;#ifdef CONFIG_DEBUG_MUTEXES unsigned done_acquire; -struct ww_class *ww_class;struct ww_mutex *contending_lock;#endif #ifdef CONFIG_DEBUG_LOCK_ALLOC
@@ -58,17 +62,19 @@ struct ww_mutex {# define __WW_CLASS_MUTEX_INITIALIZER (lockname, class)#endif -#define __WW_CLASS_INITIALIZER (ww_class) \ +#define __WW_CLASS_INITIALIZER (ww_class, _is_wait_die) \ { .stamp = ATOMIC_LONG_INIT(0) \ , .acquire_name = #ww_class "_acquire" \ - , .mutex_name = #ww_class "_mutex" } + , .mutex_name = #ww_class "_mutex" \ + , .is_wait_die = _is_wait_die }#define __WW_MUTEX_INITIALIZER (lockname, class) \ { .base = __MUTEX_INITIALIZER(lockname.base) \ __WW_CLASS_MUTEX_INITIALIZER(lockname, class) } -#define DEFINE_WW_CLASS (classname) \ - struct ww_class classname = __WW_CLASS_INITIALIZER(classname) +#define DEFINE_WW_CLASS (classname, _is_wait_die) \ + struct ww_class classname = __WW_CLASS_INITIALIZER(classname, \ + _is_wait_die)#define DEFINE_WW_MUTEX (mutexname, ww_class) \ struct ww_mutex mutexname = __WW_MUTEX_INITIALIZER(mutexname, ww_class)
@@ -123,8 +129,9 @@ static inline void ww_acquire_init(struct ww_acquire_ctx *ctx, ctx->task = current; ctx->stamp =atomic_long_inc_return_relaxed (&ww_class->stamp); ctx->acquired =0 ; -#ifdef CONFIG_DEBUG_MUTEXES ctx->ww_class = ww_class; + ctx->wounded = false; +#ifdef CONFIG_DEBUG_MUTEXES ctx->done_acquire =0 ; ctx->contending_lock =NULL ;#endif
kernel/locking/locktorture.c +1 -1
@@ -365,7 +365,7 @@ static struct lock_torture_ops mutex_lock_ops = { };#include <linux/ww_mutex.h> -static DEFINE_WW_CLASS (torture_ww_class); +static DEFINE_WW_CLASS(torture_ww_class, true); static DEFINE_WW_MUTEX(torture_ww_mutex_0, &torture_ww_class); static DEFINE_WW_MUTEX(torture_ww_mutex_1, &torture_ww_class); static DEFINE_WW_MUTEX(torture_ww_mutex_2, &torture_ww_class);
kernel/locking/mutex.c +92 -9
186 lines not on this page · raw message
kernel/locking/test-ww_mutex.c +1 -1
@@ -26,7 +26,7 @@#include <linux/slab.h> #include <linux/ww_mutex.h> -static DEFINE_WW_CLASS (ww_class); +static DEFINE_WW_CLASS(ww_class, true);struct workqueue_struct *wq;struct test_mutex {
lib/locking-selftest.c +1 -1
@@ -29,7 +29,7 @@*/ static unsigned int debug_locks_verbose; -static DEFINE_WW_CLASS (ww_lockdep); +static DEFINE_WW_CLASS(ww_lockdep, true);static int __init setup_debug_locks_verbose (char *str) {
Signature · 6 lines
2.14.3 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
RFC PATCH 2/2 drm: Change deadlock-avoidance algorithm for the modeset locks.
In reply to Thomas HellstromFor modeset locks we don't expect a high number of contending transactions so change algorithm from Wait-Die to Wound-Wait.
--- drivers/gpu/drm/drm_modeset_lock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_modeset_lock.c +1 -1
@@ -70,7 +70,7 @@* lists and lookup data structures. */ -static DEFINE_WW_CLASS(crtc_ww_class, true); +static DEFINE_WW_CLASS(crtc_ww_class, false);/** * drm_modeset_lock_all - take all modeset locks
Signature · 6 lines
2.14.3 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel