All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: David Howells <dhowells@redhat.com>
Cc: linux-arch@vger.kernel.org
Subject: Re: RCU read lock vs spinlocks
Date: Fri, 26 Mar 2010 09:39:11 -0700	[thread overview]
Message-ID: <20100326163911.GD2343@linux.vnet.ibm.com> (raw)
In-Reply-To: <29095.1269514315@redhat.com>

On Thu, Mar 25, 2010 at 10:51:55AM +0000, David Howells wrote:
> 
> If a function needs both a spinlock and the RCU read lock, is there a best
> order in which they should be taken?  For instance, is:
> 
> 	spin_lock(&my_lock);
> 	rcu_read_lock();
> 	...
> 	rcu_read_unlock();
> 	spin_unlock(&my_lock);
> 
> better than:
> 
> 	rcu_read_lock();
> 	spin_lock(&my_lock);
> 	...
> 	spin_unlock(&my_lock);
> 	rcu_read_unlock();

The short answer is that it usually doesn't matter.

If you want the long answer...  In a !CONFIG_PREEMPT kernel, it won't
matter at all because rcu_read_lock() and rcu_read_unlock() generate
no code.  In a CONFIG_PREEMPT kernel, if you had a highly contended lock,
then the second approach would be marginally better, but it would be even
better to reduce that lock's contention, which would make it not matter.
On the other hand, if you were running in a -rt kernel, and again had
a highly contended lock, then the first approach would avoid blocking
in an RCU read-side critical section, which is a bit more efficient.
But again, it would be even better to reduce the contention on the lock.

> And does it make any different if spin_lock_irq*() is used?

Use of spin_lock_irq*() removes the -rt preference.

> In fact, should spin_lock_irq*() imply rcu_read_lock()?  Given that the
> current task can't be preempted if there can be no interrupt to do the
> preempting?

No, but spin_lock_irq*() -does- imply rcu_read_lock_sched() and
rcu_read_lock_bh().  As an accident of the implementation, but
-only- in non-rt kernels, spin_lock_irq*() happens to currently imply
rcu_read_lock(), but please, please, please do not write code relying
on this.  It will break.  :-(

							Thanx, Paul

      reply	other threads:[~2010-03-26 16:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-25 10:51 RCU read lock vs spinlocks David Howells
2010-03-26 16:39 ` Paul E. McKenney [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=20100326163911.GD2343@linux.vnet.ibm.com \
    --to=paulmck@linux.vnet.ibm.com \
    --cc=dhowells@redhat.com \
    --cc=linux-arch@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.