Re: bottom-half functions

Theodore Y. Ts'o (tytso@MIT.EDU)
Mon, 1 Feb 1999 21:51:57 -0500 (EST)


From: Mirian Crzig Lennox <mirian@xensei.com>
Date: 30 Jan 1999 08:49:28 -0500

> Can someone give me a one-liner on what bottom-half functions are?

every device driver in unix (and operating systems modelled after
unix, such as linux) has two parts: the part which reads and writes
directly to the device, which runs in an interrupt context, and the
part which manipulates the i/o buffers, which runs in a process
context. the first part is called the bottom half or lower half; the
second part is called the top or upper half.

the reason for this separation is that bottom half functions have to
be as minimal as possible, because they run with interrupts disabled
and they can't block. also, since they don't run in a process context
they have no access to information about the process on whose behalf
the i/o is taking place. normally, a bottom half function merely
moves data between the device and an i/o buffer, and then immediately
awakens anything which is blocking on the buffer. when the process
which wants the buffer is scheduled in again, the top half functions
actually deal with the data.

While the above is generally true, under Linux, bottom-half functions
have a slightly different meaning. Under Linux, bottom-half functions
are very similar to what BSD-style kernels call "software interrupts".
A typical use for them is in a device driver for a tty or a network
device will very quickly dequeue incoming data from the hardware, and
then push it into an incoming buffer. It then pushes the result where
the bottom-half handler can catch it, and returns from the interrupt,
thus enabling interrupts to be serviced for that device. The kernel
will run any bottom-half handlers that have been marked as pending when
returning from interrupts or from system calls --- i.e, before allowing
control to get transferred back to user-mode code.

So in the Linux context, there are three classes of functions:

* Interrupt handlers
* Bottom-half functions
* System call functions

System call functions are run when a process requests some kernel
services, and belong to a specific process context. Interrupt handlers
and bottom-half functions do not have a specific process context, and
should therefore not reference the "current" pointer. They should also
be careful not to block, or call functions that might block.

Bottom-half functions are used for a number of different contexts, but
it's basically about deferring work until a later time, usually to
reduce the amount of time actually spent in a interrupt handler, or to
allow data from multiple interrupts to be processed at one fell swoop,
in order to reduce processing overhead. Task queues (tqueue's) are also
used for the latter purpose, but that's a topic for another day.

- Ted

P.S. Anyone who wishes to fold this into some GPL'ed Linux kernel
programming documentation is welcome to take this text. (hint, hint)

-
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/