linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: George Anzinger <george@mvista.com>
To: Chris Peterson <chris@potamus.org>
Cc: linux-kernel@vger.kernel.org
Subject: Re: question about preempt_disable()
Date: Wed, 03 Dec 2003 15:12:44 -0800	[thread overview]
Message-ID: <3FCE6DEC.1010207@mvista.com> (raw)
In-Reply-To: <000d01c3b6dd$30ab34a0$8a04a943@bananacabana>

Chris Peterson wrote:
> I just bought Robert Love's new book "Linux Kernel Development". The book
> has been very informative, but I have some unanswered questions about kernel
> preemption.
> 
>>From what I understand, SMP-safe code is also preempt-safe. The preempt
> count is the number of spinlocks held by the current kernel thread. If the
> preempt code is greater zero, then the kernel thread cannot be preempted.
> 
> My question is: if the code is already SMP-safe and holding the necessary
> spinlocks, why is the preempt count necessary? Why must preemption be
> disabled and re-enabled as spinlocks are acquired and released? Is it just
> an optimization for accessing per-cpu data? Or is it necessary to prevent
> priority inversion of kernel threads, when a low priority thread holds
> spinlock X and is preempted by a high priority thread that hogs the CPU,
> forever spinning in spin_lock(&X)?

There are a couple of things here.  First, the preempt count includes other 
reasons not to preempt.  One of these is used when working with per-cpu data and 
a cpu switch would NOT be good (a pointer would point to the wrong cpu's data). 
  Also, at one time, the bh-lock was also cause to bump the preempt count.  In 
2.6, I think that is no longer true, BUT, the bh-count is in the same word. 
This is important as we want to test just one thing at interrupt return time to 
see if it is ok to preempt.  (Well, we actually have to test the need_resched 
flag too...)  From the interrupt codes point of view, the fact that the current 
task holds one or more spin locks is ONLY visible via the preempt count.  Thus 
the count collects all these things in one, easy to check, place.  This allows 
us to keep the interrupt path as fast as possible.

-- 
George Anzinger   george@mvista.com
High-res-timers:  http://sourceforge.net/projects/high-res-timers/
Preemption patch: http://www.kernel.org/pub/linux/kernel/people/rml


      parent reply	other threads:[~2003-12-03 23:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-30  0:59 question about preempt_disable() Chris Peterson
2003-11-30 17:39 ` Matthias Urlichs
2003-12-01  3:53   ` Rob Love
2003-12-08 16:31     ` Matthias Urlichs
2003-12-03 23:12 ` George Anzinger [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=3FCE6DEC.1010207@mvista.com \
    --to=george@mvista.com \
    --cc=chris@potamus.org \
    --cc=linux-kernel@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 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).