Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 2: Process Management and Scheduling


Because kernel threads are generated by the kernel itself, two special points should be noted:


  1. They execute in the supervisor mode of the CPU, not in the user mode (see Chapter 1).

  2. They may access only the kernel part of virtual address space (all addresses above
    TASK_SIZE) but not the virtual user area.


Recall from above that the two pointers tomm_structs are contained in the task structure:

<sched.h>
struct task_struct {
...
struct mm_struct *mm, *active_mm;
...
}

The total virtual address space of a system is separated into two parts on most machines: The lower
portion is accessible by userland programs, and the upper part is reserved for the kernel. When the kernel
is running on behalf of a userland program to serve a system call, for instance, the userspace portion of
the virtual address space is described by themm_structinstance pointed to bymm(the exact content of this
structure is irrelevant for now, but is discussed in Chapter 4). Every time the kernel performs a context
switch, the userland portion of the virtual address space must be replaced to match the then-running
process.

This provides some room for optimization, which goes by the namelazy TLB handling:Sincekernel
threads are not associated with any particular userland process, the kernel does not need to rearrange
the userland portion of the virtual address space and can just leave the old setting in place. Since any
userland process can have been running before a kernel thread, the contents of the userspace part are
essentially random, and the kernel thread must notmodify it. To signalize that the userspace portion
must not be accessed,mmis set to aNULLpointer. However, since the kernel must know what is currently
contained in the userspace, a pointer to themm_structdescribing it is preserved inactive_mm.

Why are processes without anmmpointer calledlazy TLB processes? Suppose that the process that runs
after a kernel thread is the same process that has run before. In this case, the kernel does not need to
modify the userspace address tables, and the information in the translation lookaside buffers is still
valid. A switch (and a corresponding clearance of TLB data) is only required when a different userland
process from before executes after the kernel thread.

Notice that when the kernel is operating in process context,mmandactive_mmhave identical values.

A kernel thread can be implemented in one of two ways. The older variant — which is still in use in some
places in the kernel — is to pass a function directly tokernel_thread. The function is then responsible to
assist the kernel in the transformation into a daemon by invokingdaemonize. This results in the following
actions:


  1. The function frees all resources (e.g., memory context, file descriptors, etc.) of the user pro-
    cess as whose child the kernel thread was started because otherwise these would be pinned
    until the end of the thread — this is not desirable because daemons usually run until the sys-
    tem is shut down. As each daemon operates only in the address area of the kernel, it does
    not even need these resources.

Free download pdf