linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Waiman Long <waiman.long@hp.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@kernel.org>,
	linux-kernel@vger.kernel.org, Jason Low <jason.low2@hp.com>,
	Davidlohr Bueso <dave@stgolabs.net>,
	Scott J Norton <scott.norton@hp.com>,
	Douglas Hatch <doug.hatch@hp.com>
Subject: Re: [PATCH v3] locking/rwsem: reduce spinlock contention in wakeup after up_read/up_write
Date: Thu, 30 Apr 2015 17:34:37 -0400	[thread overview]
Message-ID: <55429FED.5090207@hp.com> (raw)
In-Reply-To: <20150428171734.GH23123@twins.programming.kicks-ass.net>

On 04/28/2015 01:17 PM, Peter Zijlstra wrote:
> On Fri, Apr 24, 2015 at 01:54:29PM -0400, Waiman Long wrote:
>> @@ -478,7 +515,40 @@ struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)
>>   {
>>   	unsigned long flags;
>>
>> -	raw_spin_lock_irqsave(&sem->wait_lock, flags);
>> +	/*
>> +	 * If a spinner is present, it is not necessary to do the wakeup.
>> +	 * Try to do wakeup only if the trylock succeeds to minimize
>> +	 * spinlock contention which may introduce too much delay in the
>> +	 * unlock operation.
>> +	 *
>> +	 *    spinning writer		up_write/up_read caller
>> +	 *    ---------------		-----------------------
>> +	 * [S]   osq_unlock()		[L]   osq
>> +	 *	 MB			      MB
>> +	 * [RmW] rwsem_try_write_lock() [RmW] spin_trylock(wait_lock)
>> +	 *
>> +	 * Here, it is important to make sure that there won't be a missed
>> +	 * wakeup while the rwsem is free and the only spinning writer goes
>> +	 * to sleep without taking the rwsem. In case the spinning writer is
>> +	 * just going to break out of the waiting loop, it will still do a
>> +	 * trylock in rwsem_down_write_failed() before sleeping. IOW, if
>> +	 * rwsem_has_spinner() is true, it will  guarantee at least one
>> +	 * trylock attempt on the rwsem.
>> +	 */
>> +	if (!rwsem_has_spinner(sem)) {
>> +		raw_spin_lock_irqsave(&sem->wait_lock, flags);
>> +	} else {
>> +		/*
>> +		 * rwsem_has_spinner() is an atomic read while spin_trylock
>> +		 * does not guarantee a full memory barrier. Insert a memory
>> +		 * barrier here to make sure that wait_lock isn't read until
>> +		 * after osq.
>> +		 * Note: smp_rmb__after_atomic() should be used if available.
>> +		 */
>> +		smp_mb__after_atomic();
>> +		if (!raw_spin_trylock_irqsave(&sem->wait_lock, flags))
>> +			return sem;
>> +	}
>>
>>   	/* do nothing if list empty */
>>   	if (!list_empty(&sem->wait_list))
> To me it makes more sense to reverse these two branches (identical code
> wise of course) and put the special case first.
>
> Alternatively we could also do something like the below, which to my
> eyes looks a little better still, but I don't care too much.
>
> 	if (rwsem_has_spinner(sem)) {
> 		/*
> 		 * comment ...
> 		 */
> 		 smp_rmb();
> 		 if (!raw_spin_trylock_irqsave(&sem->wait_lock, flags))
> 			return sem;
> 		 goto locked;
> 	}
>
> 	raw_spin_lock_irqsave(&sem->wait_lock, flags);
> locked:

Thanks for the suggested. I have implemented that in the v4 patch. Also 
thanks for correcting my misconception on how to use the 
smp_mb__after_atomic() macro.

Cheers,
Longman

      parent reply	other threads:[~2015-04-30 21:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-24 17:54 [PATCH v3] locking/rwsem: reduce spinlock contention in wakeup after up_read/up_write Waiman Long
2015-04-24 20:39 ` Davidlohr Bueso
2015-04-27 20:25   ` Waiman Long
2015-04-28 18:17     ` Davidlohr Bueso
2015-04-29 19:58       ` Waiman Long
2015-04-30 14:12         ` Davidlohr Bueso
2015-04-30 20:25           ` Waiman Long
2015-04-28 16:52 ` Peter Zijlstra
2015-04-28 17:17 ` Peter Zijlstra
2015-04-28 17:50   ` Jason Low
2015-04-28 17:59     ` Jason Low
2015-04-28 18:05     ` Davidlohr Bueso
2015-04-30 21:34   ` Waiman Long [this message]

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=55429FED.5090207@hp.com \
    --to=waiman.long@hp.com \
    --cc=dave@stgolabs.net \
    --cc=doug.hatch@hp.com \
    --cc=jason.low2@hp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=scott.norton@hp.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;
as well as URLs for NNTP newsgroup(s).