Why can't schedule() be called directly from a hardware interrupt?
Why can't schedule() be called directly from a hardware interrupt? For example, why can't I call schedule() directly from scheduler_tick() and instead I have to use need_resched flag?
I tried looking for an answer but I came empty handed. Any help would be much appreciated.
Scheduling happens on timer interrupts. The basic rule is that only one interrupt can be open at a time, so if you go to sleep in the "got data from device X" interrupt, the timer interrupt cannot run to schedule it out.
Interrupts also happen many times and overlap. If you put the "got data" interrupt to sleep, and then get more data, what happens? It's confusing (and fragile) enough that the catch-all rule is: no sleeping in interrupts. You will do it wrong.
Consider a cpu with a spin lock which now serves an interrupt. If you schedule() out, you violate the invariant of spin lock owners not going off cpu. Note that for the most part spin locks DON'T disable interrupts. Sometimes there are locks relevant to interrupt handlers and in those cases spin_lock_irq and/or spin_lock_irqsave is used.
链接地址: http://www.djcxy.com/p/14860.html