Preemption
The kernel can preempt a task running in the kernel so long as it does not hold a lock. That is, locks are used as markers of regions of non-preemptibility.
The first change in supporting kernel preemption was the addition of a preemption counter, preempt_count
, to each process’s thread_info
.
This counter begins at zero and increments once for each lock that is acquired and decrements once for each lock that is released. When the counter is zero, the kernel is preemptible.
If need_resched
is set and preempt_count
is zero, then a more important task is runnable, and it is safe to preempt. If preempt_count
is nonzero, a lock is held, and it is unsafe to reschedule. In that case, the interrupt returns as usual to the currently executing task.