linux-ia64.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Waiman Long <waiman.long@hpe.com>
To: Davidlohr Bueso <dave@stgolabs.net>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	linux-kernel@vger.kernel.org, x86@kernel.org,
	linux-alpha@vger.kernel.org, linux-ia64@vger.kernel.org,
	linux-s390@vger.kernel.org, linux-arch@vger.kernel.org,
	linux-doc@vger.kernel.org, Jason Low <jason.low2@hp.com>,
	Dave Chinner <david@fromorbit.com>,
	Jonathan Corbet <corbet@lwn.net>,
	Scott J Norton <scott.norton@hpe.com>,
	Douglas Hatch <doug.hatch@hpe.com>
Subject: Re: [RFC PATCH-tip v4 01/10] locking/osq: Make lock/unlock proper acquire/release barrier
Date: Wed, 05 Oct 2016 12:19:22 +0000	[thread overview]
Message-ID: <57F4EFCA.6050503@hpe.com> (raw)
In-Reply-To: <20161004190601.GD24086@linux-80c1.suse>

On 10/04/2016 03:06 PM, Davidlohr Bueso wrote:
> On Thu, 18 Aug 2016, Waiman Long wrote:
>
>> The osq_lock() and osq_unlock() function may not provide the necessary
>> acquire and release barrier in some cases. This patch makes sure
>> that the proper barriers are provided when osq_lock() is successful
>> or when osq_unlock() is called.
>
> But why do we need these guarantees given that osq is only used 
> internally
> for lock owner spinning situations? Leaking out of the critical region 
> will
> obviously be bad if using it as a full lock, but, as is, this can only 
> hurt
> performance of two of the most popular locks in the kernel -- although 
> yes,
> using smp_acquire__after_ctrl_dep is nicer for polling.

First of all, it is not obvious from the name osq_lock() that it is not 
an acquire barrier in some cases. We either need to clearly document it 
or has a variant name that indicate that, e.g. osq_lock_relaxed, for 
example.

Secondly, if we look at the use cases of osq_lock(), the additional 
latency (for non-x86 archs) only matters if the master lock is 
immediately available for acquisition after osq_lock() return. 
Otherwise, it will be hidden in the spinning loop for that master lock. 
So yes, there may be a slight performance hit in some cases, but 
certainly not always.

> If you need tighter osq for rwsems, could it be refactored such that 
> mutexes
> do not take a hit?
>

Yes, we can certainly do that like splitting into 2 variants, one with 
acquire barrier guarantee and one without.

>>
>> Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
>> Signed-off-by: Waiman Long <Waiman.Long@hpe.com>
>> ---
>> kernel/locking/osq_lock.c |   24 ++++++++++++++++++------
>> 1 files changed, 18 insertions(+), 6 deletions(-)
>>
>> diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
>> index 05a3785..3da0b97 100644
>> --- a/kernel/locking/osq_lock.c
>> +++ b/kernel/locking/osq_lock.c
>> @@ -124,6 +124,11 @@ bool osq_lock(struct optimistic_spin_queue *lock)
>>
>>         cpu_relax_lowlatency();
>>     }
>> +    /*
>> +     * Add an acquire memory barrier for pairing with the release 
>> barrier
>> +     * in unlock.
>> +     */
>> +    smp_acquire__after_ctrl_dep();
>>     return true;
>>
>> unqueue:
>> @@ -198,13 +203,20 @@ void osq_unlock(struct optimistic_spin_queue 
>> *lock)
>>      * Second most likely case.
>>      */
>>     node = this_cpu_ptr(&osq_node);
>> -    next = xchg(&node->next, NULL);
>> -    if (next) {
>> -        WRITE_ONCE(next->locked, 1);
>> +    next = xchg_relaxed(&node->next, NULL);
>> +    if (next)
>> +        goto unlock;
>> +
>> +    next = osq_wait_next(lock, node, NULL);
>> +    if (unlikely(!next)) {
>> +        /*
>> +         * In the unlikely event that the OSQ is empty, we need to
>> +         * provide a proper release barrier.
>> +         */
>> +        smp_mb();
>>         return;
>>     }
>>
>> -    next = osq_wait_next(lock, node, NULL);
>> -    if (next)
>> -        WRITE_ONCE(next->locked, 1);
>> +unlock:
>> +    smp_store_release(&next->locked, 1);
>> }
>
> As well as for the smp_acquire__after_ctrl_dep comment you have above, 
> this also
> obviously pairs with the osq_lock's smp_load_acquire while backing out 
> (unqueueing,
> step A). Given the above, for this case we might also just rely on 
> READ_ONCE(node->locked),
> if we get the conditional wrong and miss the node becoming locked, all 
> we do is another
> iteration, and while there is a cmpxchg() there, it is mitigated with 
> the ccas thingy.

Similar to osq_lock(), the current osq_unlock() does not have a release 
barrier guarantee. I think splitting into 2 variants - osq_unlock, 
osq_unlock_relaxed will help.

Cheers,
Longman

  parent reply	other threads:[~2016-10-05 12:19 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-18 21:11 [RFC PATCH-tip v4 00/10] locking/rwsem: Enable reader optimistic spinning Waiman Long
2016-08-18 21:11 ` [RFC PATCH-tip v4 01/10] locking/osq: Make lock/unlock proper acquire/release barrier Waiman Long
2016-10-04 19:06   ` Davidlohr Bueso
2016-10-04 21:28     ` Jason Low
2016-10-05 12:19     ` Waiman Long [this message]
2016-10-05 15:11       ` Waiman Long
2016-10-06  5:47         ` Davidlohr Bueso
2016-10-06 19:30           ` Waiman Long
2016-10-10  5:39             ` [PATCH] locking/osq: Provide proper lock/unlock and relaxed flavors Davidlohr Bueso
2016-10-06 19:31           ` [RFC PATCH-tip v4 01/10] locking/osq: Make lock/unlock proper acquire/release barrier Jason Low
2016-08-18 21:11 ` [RFC PATCH-tip v4 02/10] locking/rwsem: Stop active read lock ASAP Waiman Long
2016-10-06 18:17   ` Davidlohr Bueso
2016-10-06 21:47     ` Dave Chinner
2016-10-06 22:51       ` Davidlohr Bueso
2016-10-07 21:45       ` Waiman Long
2016-10-09 15:17       ` Christoph Hellwig
2016-10-10  6:07         ` Dave Chinner
2016-10-10  9:34           ` Christoph Hellwig
2016-10-11 21:06             ` Dave Chinner
2016-10-16  5:57               ` Christoph Hellwig
2016-08-18 21:11 ` [RFC PATCH-tip v4 03/10] locking/rwsem: Make rwsem_spin_on_owner() return a tri-state value Waiman Long
2016-08-18 21:11 ` [RFC PATCH-tip v4 04/10] locking/rwsem: Enable count-based spinning on reader Waiman Long
2016-08-18 21:11 ` [RFC PATCH-tip v4 05/10] locking/rwsem: move down rwsem_down_read_failed function Waiman Long
2016-08-18 21:11 ` [RFC PATCH-tip v4 06/10] locking/rwsem: Move common rwsem macros to asm-generic/rwsem_types.h Waiman Long
2016-08-18 21:11 ` [RFC PATCH-tip v4 07/10] locking/rwsem: Change RWSEM_WAITING_BIAS for better disambiguation Waiman Long
2016-08-19  5:57   ` Wanpeng Li
2016-08-19 16:21     ` Waiman Long
2016-08-22  2:15       ` Wanpeng Li
2016-08-18 21:11 ` [RFC PATCH-tip v4 08/10] locking/rwsem: Enable spinning readers Waiman Long
2016-08-18 21:11 ` [RFC PATCH-tip v4 09/10] locking/rwsem: Enable reactivation of reader spinning Waiman Long
2016-08-18 21:11 ` [RFC PATCH-tip v4 10/10] locking/rwsem: Add a boot parameter to reader spinning threshold Waiman Long
2016-08-24  4:00   ` Davidlohr Bueso
2016-08-24 18:39     ` Waiman Long

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=57F4EFCA.6050503@hpe.com \
    --to=waiman.long@hpe.com \
    --cc=corbet@lwn.net \
    --cc=dave@stgolabs.net \
    --cc=david@fromorbit.com \
    --cc=doug.hatch@hpe.com \
    --cc=jason.low2@hp.com \
    --cc=linux-alpha@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-ia64@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=scott.norton@hpe.com \
    --cc=x86@kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).