Re: How linux schedules things when interrupts occur




Hi Shane, this is more of a www.kernelnewbies.org question, but I have
some free time on my hands, so I'll reply to some of your questions.

On Sat, 4 Mar 2006, shane Miller wrote:

All-

I know what interrupts are. I read the Linux Device driver book and
see how to deal with them. But I don't see the full picture on how the
kernel deals with scheduling it's work. Bsically I am leaking for more
meat on this.

Good book, you might also want to read:

Understanding the Linux Kernel ISBN: 0596002130
Linux Kernel Development ISBN: 0672327201


Suppose Linux is running on a single CPU system. Conceptually the CPU
is executing a user thread/process --- whether that be user code or
library code on the user's behalf --- or the CPU is in the kernel.

When an APIC processed hardware interrupt comes and assuming the
interrupt is not masked off then if

* the CPU is doing user code, the kernel arranges to block/preempt the
current user process/thread and jumps into the kernel and runs the
registered interrupt handler.

* the CPU is in the kernel so it jumps to the interrupt handler and
runs it.

Can somebody put some more meat on this? For example,

* how does the kernel decide what to schedule once the interrupt
handler is done? Does it, as may be the case, resume the preempted user
thread or resume where it left off in the kernel? Or does it merely
call schedule().

Basically it goes back to where it was interrupted. Now there's some other
things that are affected by this.

If the timer interrupt went off it may run update_process_times and may
decide that the process ran enough. Or another interrupt went off and woke
up a higher priority process. When the current running process needs to
be preempted, it has it's need_resched flag set (set_need_resched), so
when leaving the interrupt if:
1) going back to userland
2) going back to the kernel and CONFIG_PREEMPT is set and preempt_count is
zero
Then schedule is called and it will most likely schedule another process.
If CONFIG_PREEMPT is not set, then the schedule waits till the kernel
calls schedule directly or goes back to userspace.
If CONFIG_PREEMPT is set but the preempt_count was active, then when
preempt_count gets down to zero, a check of need_resched is made, and if
set it calls schedule.

* What happens if a signal comes during this work?

Signals are added to the task and are handled when going back to userland.


Once I understand this I think I can deal with the case in which an
interrupt comes while an interrupt is already being processed.


That's called nested interrupts, and that will never schedule until the
last interrupt returns.

Then we have the issue of softirqs and tasklets :) but that's another
story.


-- Steve

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



Relevant Pages

  • Re: [parisc-linux] [patch 15/23] Add cmpxchg_local to parisc
    ... could be vastely used in the kernel. ... the local ops has just been standardized in 2.6.22 though a patchset I ... I always thought preemption required some sort of interrupt or trap. ... that only one CPU writes to the local_t data. ...
    (Linux-Kernel)
  • Re: Help with a driver im writing.
    ... Tasklets don't interfere each other on single CPU with non-preemeptable ... put it into a critical section with interrupt off anyway. ... > It's running on kernel version 2.6.14 single intel cpu, ... > My write routine is a loop in which i shift data out to the ...
    (comp.os.linux.development.system)
  • RE: Maximum frequency of re-scheduling (minimum time quantum) que stio n
    ... >interrupt will also schedule if necessary. ... >>The catch here is, without the preemptable kernel option, the kernel ... Even with the option, it can't preempt ...
    (Linux-Kernel)
  • Re: Threads and processes on Linux
    ... > task runs in the same memory space as the parent task. ... > LinuxThreads and/or older 2.4.x kernel. ... CPU, and so a lot of time is being spent loading the memory accessed by the ... I only got thread affinity - I didn't get interrupt affinity. ...
    (comp.os.linux.misc)
  • Re: APIC error on SMP machine
    ... The kernel source implies ... > listen to the CPU? ... interrupt, it was not accepted by the target. ... Do any previous kernels boot? ...
    (Linux-Kernel)