From: Peter Zijlstra <peterz@infradead.org>
To: "Christian König" <ckoenig.leichtzumerken@gmail.com>
Cc: daniel@ffwll.ch, l.stach@pengutronix.de,
linux+etnaviv@armlinux.org.uk, christian.gmeiner@gmail.com,
yuq825@gmail.com, eric@anholt.net, thellstrom@vmware.com,
dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
etnaviv@lists.freedesktop.org, lima@lists.freedesktop.org
Subject: Re: [PATCH 1/6] locking: add ww_mutex_(un)lock_for_each helpers
Date: Fri, 14 Jun 2019 14:56:41 +0200 [thread overview]
Message-ID: <20190614125641.GO3436@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <20190614124125.124181-2-christian.koenig@amd.com>
On Fri, Jun 14, 2019 at 02:41:20PM +0200, Christian König wrote:
> The ww_mutex implementation allows for detection deadlocks when multiple
> threads try to acquire the same set of locks in different order.
>
> The problem is that handling those deadlocks was the burden of the user of
> the ww_mutex implementation and at least some users didn't got that right
> on the first try.
>
> I'm not a big fan of macros, but it still better then implementing the same
> logic at least halve a dozen times. So this patch adds two macros to lock
> and unlock multiple ww_mutex instances with the necessary deadlock handling.
>
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
> include/linux/ww_mutex.h | 75 ++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 75 insertions(+)
>
> diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h
> index 3af7c0e03be5..a0d893b5b114 100644
> --- a/include/linux/ww_mutex.h
> +++ b/include/linux/ww_mutex.h
> @@ -369,4 +369,79 @@ static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
> return mutex_is_locked(&lock->base);
> }
>
> +/**
> + * ww_mutex_unlock_for_each - cleanup after error or contention
> + * @loop: for loop code fragment iterating over all the locks
> + * @pos: code fragment returning the currently handled lock
> + * @contended: the last contended ww_mutex or NULL or ERR_PTR
> + *
> + * Helper to make cleanup after error or lock contention easier.
> + * First unlock the last contended lock and then all other locked ones.
> + */
> +#define ww_mutex_unlock_for_each(loop, pos, contended) \
> + if (!IS_ERR(contended)) { \
> + if (contended) \
> + ww_mutex_unlock(contended); \
> + contended = (pos); \
> + loop { \
> + if (contended == (pos)) \
> + break; \
> + ww_mutex_unlock(pos); \
> + } \
> + }
> +
> +/**
> + * ww_mutex_lock_for_each - implement ww_mutex deadlock handling
> + * @loop: for loop code fragment iterating over all the locks
> + * @pos: code fragment returning the currently handled lock
> + * @contended: ww_mutex pointer variable for state handling
> + * @ret: int variable to store the return value of ww_mutex_lock()
> + * @intr: If true ww_mutex_lock_interruptible() is used
> + * @ctx: ww_acquire_ctx pointer for the locking
> + *
> + * This macro implements the necessary logic to lock multiple ww_mutex
> + * instances. Lock contention with backoff and re-locking is handled by the
> + * macro so that the loop body only need to handle other errors and
> + * successfully acquired locks.
> + *
> + * With the @loop and @pos code fragment it is possible to apply this logic
> + * to all kind of containers (array, list, tree, etc...) holding multiple
> + * ww_mutex instances.
> + *
> + * @contended is used to hold the current state we are in. -ENOENT is used to
> + * signal that we are just starting the handling. -EDEADLK means that the
> + * current position is contended and we need to restart the loop after locking
> + * it. NULL means that there is no contention to be handled. Any other value is
> + * the last contended ww_mutex.
> + */
> +#define ww_mutex_lock_for_each(loop, pos, contended, ret, intr, ctx) \
> + for (contended = ERR_PTR(-ENOENT); ({ \
> + __label__ relock, next; \
> + ret = -ENOENT; \
> + if (contended == ERR_PTR(-ENOENT)) \
> + contended = NULL; \
> + else if (contended == ERR_PTR(-EDEADLK)) \
> + contended = (pos); \
> + else \
> + goto next; \
> + loop { \
> + if (contended == (pos)) { \
> + contended = NULL; \
> + continue; \
> + } \
> +relock: \
> + ret = !(intr) ? ww_mutex_lock(pos, ctx) : \
> + ww_mutex_lock_interruptible(pos, ctx); \
> + if (ret == -EDEADLK) { \
> + ww_mutex_unlock_for_each(loop, pos, \
> + contended); \
> + contended = ERR_PTR(-EDEADLK); \
> + goto relock; \
> + } \
> + break; \
> +next: \
> + continue; \
> + } \
> + }), ret != -ENOENT;)
> +
Yea gawds, that's eyebleeding bad, even for macros :/
It also breaks with pretty much all other *for_each*() macros in
existence by not actually including the loop itself but farming that out
to an argument statement (@loop), suggesting these really should not be
called for_each.
next prev parent reply other threads:[~2019-06-14 12:56 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-14 12:41 ww_mutex deadlock handling cleanup Christian König
2019-06-14 12:41 ` [PATCH 1/6] locking: add ww_mutex_(un)lock_for_each helpers Christian König
2019-06-14 12:56 ` Peter Zijlstra [this message]
2019-06-14 13:04 ` Christian König
2019-06-14 12:41 ` [PATCH 2/6] drm/ttm: use new ww_mutex_(un)lock_for_each macros Christian König
2019-06-14 12:41 ` [PATCH 3/6] drm/gem: " Christian König
2019-06-14 12:59 ` Peter Zijlstra
2019-06-14 13:06 ` Christian König
2019-06-14 13:21 ` Peter Zijlstra
2019-06-14 13:19 ` Peter Zijlstra
2019-06-14 15:22 ` Daniel Vetter
2019-06-14 18:10 ` Christian König
2019-06-14 18:24 ` Daniel Vetter
2019-06-14 18:51 ` Christian König
2019-06-14 20:30 ` Daniel Vetter
2019-06-15 13:56 ` Daniel Vetter
2019-06-17 9:30 ` Christian König
2019-06-14 12:41 ` [PATCH 4/6] drm/etnaviv: " Christian König
2019-06-14 12:41 ` [PATCH 5/6] drm/lima: " Christian König
2019-06-14 12:41 ` [PATCH 6/6] drm/vc4: " Christian König
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190614125641.GO3436@hirez.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=christian.gmeiner@gmail.com \
--cc=ckoenig.leichtzumerken@gmail.com \
--cc=daniel@ffwll.ch \
--cc=dri-devel@lists.freedesktop.org \
--cc=eric@anholt.net \
--cc=etnaviv@lists.freedesktop.org \
--cc=l.stach@pengutronix.de \
--cc=lima@lists.freedesktop.org \
--cc=linux+etnaviv@armlinux.org.uk \
--cc=linux-kernel@vger.kernel.org \
--cc=thellstrom@vmware.com \
--cc=yuq825@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome