Re: [RFC][PATCH 1/4] locking/mutex: Use try_cmpxchg()

From: Peter Zijlstra
Date: Mon Jul 05 2021 - 10:00:27 EST


On Mon, Jul 05, 2021 at 07:59:12PM +0800, Xu, Yanfei wrote:
>
>
> On 6/30/21 11:35 PM, Peter Zijlstra wrote:
> > For simpler and better code.
> >
> > Signed-off-by: Peter Zijlstra (Intel)<peterz@xxxxxxxxxxxxx>
> > ---
> > kernel/locking/mutex.c | 27 ++++++---------------------
> > 1 file changed, 6 insertions(+), 21 deletions(-)
>
> Hi Peter,
>
> I read the mutex codes today, and find there seems something wrong for the
> patch. Should we consider the race condition as blow?
>
> From 4035f50c96e17cbe3febab768b64da5c000e5b76 Mon Sep 17 00:00:00 2001
> From: Yanfei Xu <yanfei.xu@xxxxxxxxxxxxx>
> Date: Mon, 5 Jul 2021 17:56:58 +0800
> Subject: [PATCH] locking/mutex: fix the endless loop when racing against
> mutex.owner
>
> if a race condition happened on mutex.owner after we fetch its value,
> atomic_long_try_cmpxchg_acquire/release invoked on &mutex.owner will
> return false. Then we need to reassign the temporary variable which
> saves mutex.owner value if in loop, or it will lead an endless loop.

No, when try_cmpxchg() fails it will update oldp. This is the reason old
is now a pointer too.