ENع
←Top and bottom halves
Lesson 49 · I/O, virtualisation and isolation

Interrupt handlers: top halves, bottom halves and deferred work

14 min11 steps5 questionsAdvanced
About this lesson

When a device interrupts, the CPU drops what it was doing and runs the kernel's handler, and while that handler runs, interrupts on that CPU are off: every other device has to wait. So Linux keeps the handler tiny (the top half) and does the real work later with interrupts on (the bottom half): in a softirq, or in a kworker thread when the work must sleep. Follow one network packet from the card, through a three-line handler, to the program waiting for it. The Hardware lesson on interrupts and I/O covers polling, interrupts and DMA, and Device drivers and the I/O stack shows where the handler sits in a driver.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 49 · Interactive
01 / 11

Short now, the rest later

A device interrupt stops whatever the CPU is running. While its handler runs, interrupts on that CPU are off, so other devices must wait. So Linux splits the work: a short top half now, a bottom half later.

top half      the interrupt handler itself (hard IRQ)
              interrupts off on this CPU: be quick
              acknowledge the device, note what is pending
bottom half   the real work, later, interrupts on
              softirq   (cannot sleep)
              workqueue (a kernel thread, may sleep)

xv6 and CS162 use the two names the other way round:
see the notes.
Loading figure…
or ← →
Read the lesson step by step
  1. Short now, the rest later

    A device interrupt stops whatever the CPU is running. While its handler runs, interrupts on that CPU are off, so other devices must wait. So Linux splits the work: a short top half now, a bottom half later.

    top half      the interrupt handler itself (hard IRQ)
                  interrupts off on this CPU: be quick
                  acknowledge the device, note what is pending
    bottom half   the real work, later, interrupts on
                  softirq   (cannot sleep)
                  workqueue (a kernel thread, may sleep)
    
    xv6 and CS162 use the two names the other way round:
    see the notes.
  2. A packet arrives

    A packet arrives at the network card. The card copies it straight into a ring of buffers in memory by DMA (direct memory access), with no work from the CPU. Then it raises its interrupt line: new data.

    card     receives a packet
    DMA      card -> receive ring in memory, slot 0
             (the CPU is not involved in the copy)
    IRQ      the card raises its interrupt line
    program  still asleep in recv(): no data yet
  3. The CPU drops everything

    The CPU finishes its current instruction, saves its place and switches to kernel mode. Through the interrupt table it jumps to the card's handler, the top half. Interrupts on this CPU are now off.

    CPU         finish the current instruction
                save its place, switch to kernel mode
    table       interrupt number -> the card's handler
    interrupts  OFF on this CPU
    
    the top half (an irq_handler_t):
    irqreturn_t nic_irq(int irq, void *dev)
    {
        nic_ack(nic);
        napi_schedule(&napi);
        return IRQ_HANDLED;
    }
  4. Line 1: tell the card

    Line 1 tells the card its interrupt was seen and masks the card's receive interrupt. More packets may land in the ring, but they will not interrupt again until the bottom half has caught up.

    line 1   nic_ack(nic);
             nic_ack is this driver's own helper:
             acknowledge   "interrupt seen"
             mask          card receive interrupt -> off
    
    NAPI rule: keep the interrupt masked after
    scheduling; unmask only after napi_complete_done()
  5. Line 2: leave a note

    Line 2, napi_schedule, does not touch the packet. It only marks the network receive softirq as pending: a note that says run the bottom half soon. A softirq is a bottom half the kernel runs itself.

    line 2   napi_schedule(&napi);
             puts this card on the CPU's poll list
             raises NET_RX_SOFTIRQ  (number 3 of 10 kinds)
    softirq  pending
    packet   still in the ring, untouched
  6. Line 3: interrupts back on

    Line 3 returns IRQ_HANDLED: yes, this was my device. The top half ran three short lines and never waited for anything. The CPU leaves the handler and switches interrupts back on.

    line 3   return IRQ_HANDLED;
             IRQ_HANDLED = 1   (IRQ_NONE = 0: not my device)
    interrupts  ON again
    top half    3 lines, no waiting, no packet work
  7. The bottom half runs

    On its way out of the interrupt, the kernel runs any pending softirqs, now with interrupts on. The network receive softirq takes the packet from the ring and passes it up the network stack.

    irq exit     pending softirqs? yes: NET_RX
    interrupts   ON while softirqs run
    NET_RX       calls the card's poll function
                 ring slot 0 -> network stack (IP, TCP/UDP)
    
    limit        up to 10 rounds or 2 ms, then the rest
                 goes to the ksoftirqd kernel thread
  8. A timer interrupt cuts in

    Mid-way, the timer interrupts. Because interrupts are on, its top half runs at once, then the softirq carries on where it stopped. Had the packet work been inside one long handler, the timer would have waited.

    timer        raises its interrupt
    interrupts   on -> timer top half runs at once
                 (interrupts off just for its few lines)
    softirq      paused, then resumes
    
    see it live: /proc/interrupts counts interrupts
    per CPU per device (LOC = local timer)
  9. The program wakes

    The softirq puts the data on the socket's queue and wakes the program asleep in recv(). The ring is empty again, so it unmasks the card's receive interrupt, ready for the next packet.

    socket   data queued
    program  woken: recv() returns the data
    ring     empty -> napi_complete_done()
    card     receive interrupt -> on again
  10. Work that must sleep

    A softirq, like a top half, must never sleep: it belongs to no process. Work that may wait, for a lock or for memory, goes on a workqueue instead. A kernel thread called kworker runs it later, and it may sleep.

    may it sleep?
      top half (hard IRQ)    no
      softirq / tasklet      no
      workqueue (kworker)    yes: a real kernel thread
    
    schedule_work(&w);      queue a work item
                            (a struct with a function pointer)
    request_threaded_irq()  top half returns IRQ_WAKE_THREAD
                            and a kernel thread does the rest
  11. Why split the handler?

    Interrupts-off time is waiting time for every other device. The top half keeps it to a few lines; the bottom half does the real work with interrupts on, as a softirq, or in a kworker thread when it must sleep.

    how long the timer waits, as a Python simulation:
    
    # one CPU; times are made-up units
    TIMER_AT = 3          # the timer interrupt arrives here
    WHOLE = 40            # all the packet work in one handler
    TOP = 2               # top half only: ack + napi_schedule
    
    def timer_wait(irqs_off_until):
        # interrupts are off until the hard handler returns
        return max(0, irqs_off_until - TIMER_AT)
    
    print("one long handler: timer waits", timer_wait(WHOLE))
    print("top half + softirq: timer waits", timer_wait(TOP))
    
    prints:
    one long handler: timer waits 37
    top half + softirq: timer waits 0

Quiz Unlocks after the last step.