Re: Context switch in the kernel processes

From: Ingo Molnar (mingo@chiara.csoma.elte.hu)
Date: Wed Feb 09 2000 - 09:10:24 EST


On Wed, 9 Feb 2000, Hari Krishna wrote:

> May be basic, but i would like to know whether the switching
> between two kernel processes ( those which are created by using
> kernel_thread()) is faster than the switching between two user level
> processes(those which are created by using fork())?. Is there any
> advantage for kernel processes?

yes. There are two items that make it cheaper: no TLB flush and less
system entry costs.

1) smaller TLB flush cost:

While the cost of the 'context switch' is the same in both cases, normal
fork()-ed processes have different page tables, while kernel-space threads
share page tables. Page table switching can be slow and has 'secondary'
costs as well: first the TLB flush itself takes 1-2 microseconds, plus the
newly switched process will see lots of TLB-misses (which add up as well
to 1-2 microseconds even if the thread is doing almost nothing).
Additionally, kernel-threads do 'lazy TLB switching', which means that
they can share the TLB with any other process (they are 'generic
threads'), so they will see even less TLB flushes. 'lazy TLB' is a new
feature introduced in the 2.3 kernel.

additionally, kernel-space threads running on i686 or better CPUs take
advantage of 'global TLBs', these TLBs are not flushed even if an explicit
TLB flush is requested.

2) reduced system entry costs

two user-space processes (threads) cannot do a context switch just out of
nowhere. They will either be context-switched after entering the kernel
through a system call, or through an asynchron event such as an interrupt,
trap or exception. The cost of entering the kernel is 1-2 microseconds,
and this is zero for kernel-space threads.

there are limited cases where a system call will initiate several context
switches without leaving/entering the kernel, such as the pipe code. In
these cases Linux also has a speedup to do 'synchronous wakeups', ie. when
the kernel knows that it's going to sleep then it will not go through the
(potentially more costy) process of finding a new CPU for a freshly
scheduled process. [this is a new feature of the 2.3 kernel as well]

of course coding a user-space thread is vastly simpler and more robust
than coding a kernel-space system thread. The speed difference of context
switches is not very big (around 30-40% total i'd estimate), unless the
particular task to be solved is very scheduling-intensive.

-- mingo

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



This archive was generated by hypermail 2b29 : Tue Feb 15 2000 - 21:00:15 EST