How a device talks to the CPU: DMA and interrupts

Video 050 of 140 in Virtualization Foundations, in the unit “Storage devices, buses and network cards”. 3:28, with English and Hindi audio.

A network card or a disk is a separate chip with its own job. It needs two things from the processor: a way to get attention, and a way to move data without the processor copying every byte. Interrupts and direct memory access meet those two needs. This article follows one packet from arrival to the waiting program, and shows how to look at interrupt counts on a Linux machine.

What you will learn

How a packet arrives, step by step

The driver, the software that runs the card, starts first. Before any data arrives, it sets aside empty memory areas, called buffers, and tells the card where they are.

The card then writes incoming data straight into those buffers. This is direct memory access, or DMA. The processor never has to copy each byte.

Next, the card raises an interrupt. An interrupt is a signal that says look at this. The processor stops what it is doing and runs a handler.

The handler is part of the driver. It takes the data from the buffer and passes it up to the waiting program. Then the processor returns to its work.

Polling or interrupts: two ways to learn about new data

With polling, the processor keeps asking the card whether it has anything new. That wastes processor time when nothing has arrived.

With interrupts, the card taps the processor only when it has something to say. An idle card costs nothing until a signal arrives.

Under heavy load, though, a flood of interrupts can swamp one core. Work that could be shared across the machine then piles up on a single processor.

See where interrupts land on Linux

Linux keeps a file at /proc/interrupts that lists every interrupt source, with counts for each processor core. The command below shows the first lines of it.

Show the first lines of the interrupt table
head -n 4 /proc/interrupts

Watch the counts over time, not just once. If one core takes almost every network interrupt, that core may become the bottleneck.

What goes wrong with interrupts

You notice slow traffic, one core near full load, and one core's count growing much faster than the others.

Terms

Driver
The software that runs a device, such as a network card, and talks to it.
Buffer
An empty area of memory set aside to hold incoming data.
Direct memory access (DMA)
A way for a device to write data into memory without the processor copying each byte.
Interrupt
A signal from a device that asks the processor to stop and look at something.
Polling
Checking a device again and again for news, even when nothing has arrived.

Check yourself

What does direct memory access save the processor from doing?

Copying each byte of incoming data. The device writes straight into memory.

Why can many interrupts on one core cause trouble?

That core can run out of time while the other cores sit idle, so traffic slows down.

What is the difference between polling and interrupts?

Polling keeps checking whether data has arrived. An interrupt is a signal that the device sends only when it has something.

Previous: Listing devices with lspci and lsblk. Next: What a network card is.