mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Waiman Long <llong@redhat.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	linux-kernel@vger.kernel.org, "Xu,
	Yanfei" <yanfei.xu@windriver.com>
Subject: Re: [PATCH] locking/mutex: Reduce chance of setting HANDOFF bit on unlocked mutex
Date: Wed, 30 Jun 2021 09:50:11 -0400	[thread overview]
Message-ID: <ecc0cc97-23ca-5de3-2a12-ed50aa12548c@redhat.com> (raw)
In-Reply-To: <YNxFkD8qzex9MvQp@hirez.programming.kicks-ass.net>

On 6/30/21 6:21 AM, Peter Zijlstra wrote:
> On Tue, Jun 29, 2021 at 04:11:38PM -0400, Waiman Long wrote:
>
>> diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
>> index d2df5e68b503..472ab21b5b8e 100644
>> --- a/kernel/locking/mutex.c
>> +++ b/kernel/locking/mutex.c
>> @@ -118,9 +118,9 @@ static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
>>   		}
>>   
>>   		/*
>> -		 * We set the HANDOFF bit, we must make sure it doesn't live
>> -		 * past the point where we acquire it. This would be possible
>> -		 * if we (accidentally) set the bit on an unlocked mutex.
>> +		 * Always clear the HANDOFF bit before acquiring the lock.
>> +		 * Note that if the bit is accidentally set on an unlocked
>> +		 * mutex, anyone can acquire it.
>>   		 */
>>   		flags &= ~MUTEX_FLAG_HANDOFF;
>>   
>> @@ -180,6 +180,11 @@ static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
>>   	atomic_long_or(flag, &lock->owner);
>>   }
>>   
>> +static inline long __mutex_fetch_set_flag(struct mutex *lock, unsigned long flag)
>> +{
>> +	return atomic_long_fetch_or_relaxed(flag, &lock->owner);
>> +}
>> +
>>   static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
>>   {
> Hurmph, so we already have a cmpxchg loop in trylock, might as well make
> that do exactly what we want without holes on.
>
> How's something like the below? Boot tested, but please verify.
>
> ---
>   kernel/locking/mutex.c | 89 ++++++++++++++++++++++++++------------------------
>   1 file changed, 46 insertions(+), 43 deletions(-)

The code looks good to me. It is an even better approach to make sure 
that the HANDOFF will never be set on an unlocked mutex.

Reviewed-by: Waiman Long <longman@redhat.com>


  reply	other threads:[~2021-06-30 13:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-29 20:11 Waiman Long
2021-06-30 10:21 ` Peter Zijlstra
2021-06-30 13:50   ` Waiman Long [this message]
2021-06-30 13:56     ` Peter Zijlstra
2021-06-30 14:13       ` Waiman Long
2021-06-30 14:46         ` Peter Zijlstra
2021-06-30 14:43   ` Xu, Yanfei
2021-06-30 14:50     ` Peter Zijlstra

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=ecc0cc97-23ca-5de3-2a12-ed50aa12548c@redhat.com \
    --to=llong@redhat.com \
    --cc=boqun.feng@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=will@kernel.org \
    --cc=yanfei.xu@windriver.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

all inboxes | Powered by JetHome®