How Tech - Systems Programming

How Tech - Systems Programming

Writing Comprehensive KUnit Test Suites for Critical Path Drivers

Jun 21, 2026
∙ Paid

Every production outage traced back to a kernel driver shares the same origin: the error path was never tested. Not from carelessness — testing kernel code against real hardware in a fast feedback loop used to mean either a dedicated test rig or accepting that your CI ran nothing at all. KUnit changed that.

KUnit landed in Linux 5.5 and executes test suites directly inside the kernel, either on real hardware or under User Mode Linux (UML), which compiles the entire kernel as an ordinary userspace process. Your tests run in actual kernel context: real spinlocks, real kmalloc, real interrupt-disable sequences. The allocator is not mocked. The scheduler is not stubbed. You are testing the real code paths.

Where Critical Path Drivers Break

The bugs that matter are not the obvious ones. A DMA ring buffer driver with subtly wrong descriptor fence ordering works perfectly on a development machine — PCIe transaction latency is long enough that the CPU never races the ring head pointer update. Under load, with 32 concurrent I/O queues at 1M IOPS, you get one corrupted descriptor every few million completions. Silent data corruption. The kind that surfaces as a filesystem check failure six months after go-live.

Three failure classes dominate in practice: descriptor ring wraparound at power-of-two boundaries, error-path teardown that races in-flight completions, and interrupt handler code that calls a function annotated GFP_KERNEL. The last panics immediately. The first two corrupt quietly, and strace tells you nothing.

The Fake Hardware Layer

You cannot drive real PCI BAR registers under UML. You need an MMIO stub — a flat array that your driver’s register accessors write into, with hooks that let test code inject responses. The right pattern is a struct fake_hw that owns both the register file and a queue of completion events. Override readl/writel via KUnit’s test resource mechanism so your driver’s I/O accessors hit this fake, not hardware.


User's avatar

Continue reading this post for free, courtesy of Systems.

Or purchase a paid subscription.
© 2026 Sumedh S · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture