Friday, May 12, 2006

Kernel Syncronization

kernel control path

sequence of instruction executed by the kernel to handle a system call, an exception, or an interrupt


Nested Execution of Exception and Interrupt Handlers

Kernel control paths may be arbitrarily nested; an interrupt handler may be interrupted by another interrupt handler

The price to pay for allowing nested kernel control paths is that an interrupt handler must never block, that is, no process switch can take place until an interrupt handler is running. In fact, all the data needed to resume a nested kernel control path is stored in ther Kernel Mode stack, which is tightly bound to the current process

Interrupt handler may preempt both other interrupt handlers and exception handlers. But an exception handler never preempts an interrpt handler. only exception is "Page Fault"

Interrupt handler never perform operations that can induce page faluts, and thus, potentially, a process switch


Kernel Preemption

reference : Performing the Process Switch

Essentially, every process switch consists of two steps:

1. Switching the Page Global Directory to install a new address space
2. Switching the Kernel Mode stack and the hardware context, which provides all the information needed by the kernel to execute the new process, including the CPU registers.

just a moment here. :-)

Hardware Context ?

the set of data that must be loaded into the registers before the process resumes its execution on the CPU. The hardware context is a subset of the process execution context, which includes all information needed for the process execution. In Linux, a part of the hardware context of a process is stored in the process descriptor, while the remaining part is saved in the Kernel Mode stack.

Process switching occurs only in Kernel Mode. The contents of all registers used by a process in User Mode have already been saved on the Kernel Mode stack before performing process switching.

The main motivation for making a kernel preemptive is to reduce the dispatch latency of the User Mode process, that is, the delay between the time they become runnable and the time they actually begin running.

Kernel don't allowed kernel preemption when any of the following cases occurs:

1. Then kernel is executing an interrupt sevice routine.
2. The deferrable functions are desabled(always true when the kernel is executing a softirq or tasklet)
3. The kernel preemption has been explictly disabled by setting the preemption counter to a positive value
Tje above rules tell us that the kernel can be preempted only when it is executing an exception handler(in particular a system call) and the kernel preemption has not been explictly disabled.

Critical Region

If the sytstem includes a single CPU, the critical region can be implemented by disableing interrupts while accessing the shared data structure, because nesting of kernel control paths can only occur when interrupts are enabled.
On the other hand, if the same data structure is accessed only by the service routines of system call, and if the system includes a single CPU, the critical region can be implemented quite simply by disabling kernel preemption while accessing the shared data structure.

When Synchronization is Not Necessay

1. All interupt handlers acknowledge the interrupt on the PIC and also disable the IRQ line. Further occurrences of the same interrupt cannot occur until the handler terminates.

2. Interrupt handler, softirqs, and tasklets are both nonpreemptable and non-blocking, so they cannot be suspended for a long time interval. In the worst case, their execution will be slightly delayed, because other interrupts occur during their execution

3. A kernel control path performing interrupt handling cannot be interrupted by a kernel control path executing a deferrable function or a system call service routine.

4. Softirqs and tasklets cannot be interleabed on a given CPU.
5. The same tasklet cannot be executed simultaneously on several CPUs.


So,

1. Interrupt handlers and tasklets need not to be coded as reentrant fuctions.
2. Per-CPU varables accessed by softirqs and tasklets only do not require synchronization.
3. A data structure accessed by only one kind of tasklet does not require synchronization.

Synchornization Primitives.