Re: Interruption and preemption

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wednesday 05 May 2004 07.15, Nilanjan Roychowdhury wrote:
> I've some questions about the preemption of the kernel. In the Linux 2.2
> and 2.4, there are a lot of spin_lock_irqsave to disable interrupt like :
> spin_lock_irqsave() ;
> CRITICAL SECTION
> spin_lock_irqrestore() ;
> In a non preemptive kernel, the critical section is executed as an
> atomic operation that's sure. But in a preemptive kernel, the kernel can
> be interrupted by a process. So the kernel has a timeslice and if the
> timeslice finishes in the CRITICAL section, there's a problem.
Are you just thinking about a hypothetical kernel which is preemptive or
the linux kernel itself? Anyway, for Linux:

I don't really know about the 2.4 kernel (i checked on lxr, but I
did not find any preemption functionality).
But in 2.6 preemption works differently than you imagine it.
Actually it is not like the kernel has a timeslice. It is like:
1st, the kernel can be preempted only if it is in user context (correct me if
i'm wrong). It is for making it possible to reschedule.
2nd, only if there are no locks held. If you look at any spinlock code
you'll see that they preempt_disable() on lock and preempt_enable() on
unlock.
Actually preemption can occur:
1. On returning from an interrupt to kernel space. By checking
   preempt_count() and need_resched.
2. On unlocking by checing if preempt_count haa reached zero.
3. By kernel code using schedule().
4. By kernel code when it blocks on something.

> And this code in SMP environment doesn't work neither, does it ? It only
> protects the interrupts of 1 processor no ?
> Just some thoughts about synchronization :)

It works. That's why it is the kernel :) But yes, it disables ints only on
the local CPU. This is to avoid an irq handler to grab the lock. But irqs on
other CPUs can occur since no deadlock will happen cause the local CPU will
unlock sooner or later.

Did you ask this, anyway? :)
Bye,
Gabor

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux