Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 2: Process Management and Scheduling


This substructure of scheduling entities must be considered by all scheduling-class-related operations.
Consider, for instance, how the code to enqueue a task in the completely fair scheduler really looks:

kernel/sched_fair.c
static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
{
struct cfs_rq *cfs_rq;
struct sched_entity *se = &p->se;

for_each_sched_entity(se) {
if (se->on_rq)
break;
cfs_rq = cfs_rq_of(se);
enqueue_entity(cfs_rq, se, wakeup);
wakeup = 1;
}
}

for_each_sched_entitytraverses the scheduling hierarchy defined by theparentelements of
sched_entity, and each entity is enqueued on the run queue.

Notice thatfor_each_sched_entitywill resolve to a trivial loop that executes the code contained in the
loop body exactly once when support for group scheduling is not selected, so the behavior described in
the previous discussion is regained.

2.8.3 Kernel Preemption and Low Latency Efforts


Let us now turn our attention to kernel preemption, which allows for a smoother experience of the sys-
tem, especially in multimedia environments. Closely related are low latency efforts performed by the
kernel, which I will discuss afterward.

KernelPreemption


As described above, the scheduler is invoked before returning to user mode after system calls or at
certain designated points in the kernel. This ensures that the kernel, unlike user processes, cannot be
interrupted unless it explicitly wants to be. This behavior can be problematic if the kernel is in the middle
of a relatively long operation — this may well be the case with filesystem, or memory-management-
related tasks. The kernel is executing on behalf of a specific process for a long amount of time, and other
processes do not get to run in the meantime. This may result in deteriorating system latency, which users
experience as ‘‘sluggish‘‘ response. Video and audio dropouts may also occur in multimedia applications
if they are denied CPU time for too long.

These problems can be resolved by compiling the kernel with support forkernel preemption.Thisallows
not only userspace applications but also the kernel to be interrupted if a high-priority process has some
things to do. Keep in mind that kernel preemption and preemption of userland tasks by other userland
tasks are two different concepts!

Kernel preemption was added during the development of kernel 2.5. Although astonishingly few
changes were required to make the kernel preemptible, the mechanism is not as easy to implement
as preemption of tasks running in userspace. If the kernel cannot complete certain actions in a single
operation — manipulation of data structures, for instance —race conditionsmay occur and render the
system inconsistent. The same problems arise on multiprocessor systems discussed in Chapter 5.
Free download pdf