All of lore.kernel.org
 help / color / mirror / Atom feed
From: martinez.javier@gmail.com (Javier Martinez Canillas)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Why can not processes switch in atomic context?
Date: Wed, 4 Jul 2012 11:31:01 +0200	[thread overview]
Message-ID: <CAAwP0s071XUaZ-gq7q=CqV7ceX4h1nAuAKtFmnteG86xZ1GppQ@mail.gmail.com> (raw)
In-Reply-To: <CAK7N6vopPo5x9r84UkCeUWKMhYBxvFidJSSTTuth2xEjt2FSEg@mail.gmail.com>

On Wed, Jul 4, 2012 at 10:36 AM, anish singh
<anish198519851985@gmail.com> wrote:
> On Wed, Jul 4, 2012 at 1:51 PM, Javier Martinez Canillas
> <martinez.javier@gmail.com> wrote:
>> On Tue, Jul 3, 2012 at 4:24 PM, Parmenides <mobile.parmenides@gmail.com> wrote:
>>> Hi,
>>>
>>>     It is said that kernel can not be preempted in interrupt context
>>> and when it is in a critical section protected by a spin lock.
>>>
>>> 1. For the spinlock case, it is easy to get if preemption is allowed
>>> in critical section, the purpose of protection provided by spinlock
>>> can not be achieved readily.
>>>
>>
>> A process cannot be preempted nor sleep while holding a spinlock due
>> spinlocks behavior. If a process grabs a spinlock and goes to sleep
>> before releasing it. A second process (or an interrupt handler) that
>> to grab the spinlock will busy wait. On an uniprocessor machine the
>> second process will lock the CPU not allowing the first process to
>> wake up and release the spinlock so the second process can continue,
>> it is basically a deadlock.
>>
>> This happens since grabbing an spinlocks also disables interrupts and
>> this is required to synchronize threads with interrupt handlers.
>>
>>> 2. For the interrupt context case, I think when processing interrupt,
>>> kernel can be preempted in principle. But, this really increases the
>>> interrupt processing time which further cause longer response time and
>>> data missing in device. Except that, is there any other reasons?
>>>
>>
>> The principal reason is quite simple, processes have an associated
>> task_struct and get executed when the scheduler chose to run it but
>> interrupt handlers are executed due an event (an interrupt happened on
>> the registered IRQ line).
>>
>> So if you preempt an interrupt handler and schedule a process instead,
>> how could you execute the interrupt handler again? they don't have an
>> associated task_struct since they are not user-space process
> In case of threaded handlers also?

Threaded irqs of course are different since a kernel thread is created
for them. The question he made (and though my answer) was for
"interrupt context". Threaded irqs are not run on interrupt contexts
but on process context (i.e: in_irq() returns false).

>>
>>> 3. Kernel is responsible for prohibiiting passive process switches,
>>> namely preemption, in the above cases. But, It seems that it does not
>>> take care of active process swtiches, namely yield. For example, some
>>> code in a critical section protected by a spinlock can invoke
>>> schedule() to switch process passively. Is this the case?
>>>
>>
>> Right, the kernel just avoid process switching by disabling preemption
>> but you could still call schedule() or do a function call that sleeps
>> like allocating big chunks of memory with GFP_KERNEL flag instead of
>> GFP_ATOMIC, this is indeed a bug in the same way that is a bug
>> dereferencing a NULL pointer.
>>
>> On the kernel hacking section of the Kbuild configuration you can find
>> many kconfig options to enable different debug facilities that helps
>> you detect these scenarios and avoid deadlock.
>>
>>
>> Hope it helps,
>> Javier
>>
>> _______________________________________________
>> Kernelnewbies mailing list
>> Kernelnewbies at kernelnewbies.org
>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



-- 
Javier Mart?nez Canillas
(+34) 682 39 81 69
Barcelona, Spain

  reply	other threads:[~2012-07-04  9:31 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-03 14:24 Why can not processes switch in atomic context? Parmenides
2012-07-03 15:33 ` Subramaniam Appadodharana
2012-07-03 16:01 ` Rajesh S R
2012-07-03 16:32 ` anish kumar
     [not found]   ` <CAOXENUhvJFTnMSiqgg5LMLtnQGYxuk=6dNg1=bRCHUremHD5Dw@mail.gmail.com>
2012-07-04  5:07     ` anish singh
2012-07-04  8:21 ` Javier Martinez Canillas
2012-07-04  8:36   ` anish singh
2012-07-04  9:31     ` Javier Martinez Canillas [this message]
2012-07-04 10:44 ` 弋天 卜
2012-07-04 15:12   ` Dave Hylands
2012-07-04 19:14     ` 卜弋天
2012-07-05 14:17     ` 卜弋天
2012-07-05  2:38 ` Parmenides

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='CAAwP0s071XUaZ-gq7q=CqV7ceX4h1nAuAKtFmnteG86xZ1GppQ@mail.gmail.com' \
    --to=martinez.javier@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.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.