public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Long, Wai Man" <waiman.long@hp.com>
To: Jason Low <jason.low2@hp.com>
Cc: Davidlohr Bueso <davidlohr@hp.com>,
	Peter Zijlstra <peterz@infradead.org>,
	mingo@kernel.org, tglx@linutronix.de, akpm@linux-foundation.org,
	linux-kernel@vger.kernel.org, paulmck@linux.vnet.ibm.com,
	tim.c.chen@linux.intel.com, hpa@zytor.com, aswin@hp.com,
	scott.norton@hp.com, chegu_vinod@hp.com
Subject: Re: [RFC PATCH 1/3] locking/mutex: Try to acquire mutex only if it is unlocked
Date: Wed, 11 Jun 2014 21:25:04 -0400	[thread overview]
Message-ID: <53990170.2010506@hp.com> (raw)
In-Reply-To: <1402523283.4035.23.camel@j-VirtualBox>


On 6/11/2014 5:48 PM, Jason Low wrote:
> On Wed, 2014-06-11 at 17:00 -0400, Long, Wai Man wrote:
>> On 6/9/2014 1:38 PM, Jason Low wrote:
>>> On Wed, 2014-06-04 at 13:58 -0700, Davidlohr Bueso wrote:
>>>> On Wed, 2014-06-04 at 13:57 -0700, Davidlohr Bueso wrote:
>>>>> In addition, how about the following helpers instead:
>>>>> - mutex_is_unlocked() : count > 0
>>>>> - mutex_has_waiters() : count < 0, or list_empty(->wait_list)
>>>>                                         ^ err, that's !list_empty()
>>> Between checking for (count < 0) or checking for !list_empty(wait_list)
>>> for waiters:
>>>
>>> Now that I think about it, I would expect a mutex_has_waiters() function
>>> to return !list_empty(wait_list) as that really tells whether or not
>>> there are waiters. For example, in highly contended cases, there can
>>> still be waiters on the mutex if count is 1.
>>>
>>> Likewise, in places where we currently use "MUTEX_SHOW_NO_WAITER", we
>>> need to check for (count < 0) to ensure lock->count is a negative value
>>> before the thread sleeps on the mutex.
>>>
>>> One option would be to still remove MUTEX_SHOW_NO_WAITER(), directly use
>>> atomic_read() in place of the macro, and just comment on why we have an
>>> extra atomic_read() that may "appear redundant". Another option could be
>>> to provide a function that checks for "potential waiters" on the mutex.
>>>
>>> Any thoughts?
>>>
>> For the first MUTEX_SHOW_NO_WAITER() call site, you can replace it with
>> a check for (count > 0).
> Yup, in my v2 patch, the first call site becomes !mutex_is_locked(lock)
> which is really a check for (count == 1).

Yes, your v2 patch looks fine to me.

>> The second call site within the for loop,
>> however, is a bit more tricky. It has to serve 2 purposes:
>>
>> 1. Opportunistically get the lock
>> 2. Set the count value to -1 to indicate someone is waiting on the lock,
>> that is why an xchg() operation has to be done even if its value is 0.
>>
>> I do agree that the naming isn't that good. Maybe it can be changed to
>> something like
>>
>> static inline int mutex_value_has_waiters(mutex *lock)    { return
>> lock->count < 0; }
> So I can imagine that a mutex_value_has_waiters() function might still
> not be a great name, since the mutex can have waiters in the case that
> the value lock->count >= 0.
>
> In the second call site, do you think we should just do a direct
> atomic_read(lock->count) >= 0 and comment that we only do the xchg if
> the count is non-negative to avoid unnecessary xchg? That what I did in
> my v2 patch.

I think that is a good idea to avoid any controversy in naming.

-Longman

  reply	other threads:[~2014-06-12  1:25 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-04 19:08 [RFC PATCH 0/3] locking/mutex: Modifications to mutex Jason Low
2014-06-04 19:08 ` [RFC PATCH 1/3] locking/mutex: Try to acquire mutex only if it is unlocked Jason Low
2014-06-04 19:43   ` Peter Zijlstra
2014-06-04 20:57     ` Davidlohr Bueso
2014-06-04 20:58       ` Davidlohr Bueso
2014-06-09 17:38         ` Jason Low
2014-06-11 21:00           ` Long, Wai Man
2014-06-11 21:48             ` Jason Low
2014-06-12  1:25               ` Long, Wai Man [this message]
2014-06-04 21:53       ` Jason Low
2014-06-04 21:26     ` Jason Low
2014-06-04 21:54       ` Thomas Gleixner
2014-06-04 22:13         ` Jason Low
2014-06-05  3:24       ` Waiman Long
2014-06-05 19:21         ` Jason Low
2014-06-04 19:08 ` [RFC PATCH 2/3] locking/mutex: Correct documentation on mutex optimistic spinning Jason Low
2014-06-04 20:11   ` Davidlohr Bueso
2014-06-04 20:30     ` Jason Low
2014-06-04 19:08 ` [RFC PATCH 3/3] locking/mutex: Optimize mutex trylock slowpath Jason Low
2014-06-04 20:28   ` Davidlohr Bueso
2014-06-04 21:47     ` Jason Low
2014-06-05  1:10       ` Davidlohr Bueso
2014-06-05  3:08         ` Jason Low
2014-06-04 20:13 ` [RFC PATCH 0/3] locking/mutex: Modifications to mutex Davidlohr Bueso

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=53990170.2010506@hp.com \
    --to=waiman.long@hp.com \
    --cc=akpm@linux-foundation.org \
    --cc=aswin@hp.com \
    --cc=chegu_vinod@hp.com \
    --cc=davidlohr@hp.com \
    --cc=hpa@zytor.com \
    --cc=jason.low2@hp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=scott.norton@hp.com \
    --cc=tglx@linutronix.de \
    --cc=tim.c.chen@linux.intel.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