Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 2: Process Management and Scheduling


Finally, the task switch is finished withswitch_to, which switches the register state and the stack — the
new process will be running after the call:

kernel/sched.c
/* Here we just switch the register state and the stack. */
switch_to(prev, next, prev);

barrier();
/*
* this_rq must be evaluated again because prev may have moved
* CPUs since it called schedule(), thus the ’rq’ on its stack
* frame will be invalid.
*/
finish_task_switch(this_rq(), prev);
}

Thecodefollowingafterswitch_towill only be executed when the current process is selected to run
next time.finish_task_switchperforms some cleanups and allows for correctly releasing locks, which,
however, we will not discuss in detail. It also gives individual architectures another possibility to hook
into the context switching process, but this is only required on a few machines. Thebarrierstatement is
a directive for the compiler that ensures that the order in which theswitch_toandfinish_task_switch
statements are executed is not changed by any unfortunate optimizations (see Chapter 5 for more details).

Intricacies ofswitch_to


The interesting thing aboutfinish_task_switchis that the cleanups are performed for the task that has
been active before the running task has been selected for execution. Notice that this is not the task that
has initiated the context switch, but some random other task in the system! The kernel must find a way
to communicate this task to thecontext_switchroutine, and this is achieved with theswitch_tomacro.
It must be implemented by every architecture and has a very unusual calling convention: Two variables
are handed over, but inthreeparameters! This is because not only two, but three processes are involved
in a context switch. The situation is illustrated in Figure 2-16.

Kernel Mappings


stack

next = B
prev = A

next = C
prev = B

next = A
prev = C

next = B
prev = A
prev = C

Process ABCA

A = switch_to(A,B)
B = switch_to(B,C)

C = switch_to(C,A)

Before switch_to

After switch_to returns

Figure 2-16: Behavior of theprevandnextvariables during context switches.

Suppose that three processes A, B, and C are runningon the system. At some point in time, the kernel
decides to switch from A to B, then from B to C, and then from C back to A again. Before eachswitch_to
call, the pointersnextandprevlocated on the stacks of the individual processes are set such thatprev
Free download pdf