Kbuild changes claim much faster Linux kernel builds
Lorenzo Stoakes's measurements show allmodconfig builds up to 36% quicker, with larger gains on incremental and do-nothing rebuilds.
Lorenzo Stoakes has proposed Linux kernel build changes that, on his measurements, cut full allmodconfig builds by as much as 36%, incremental rebuilds by roughly 60 to 70%, and do-nothing rebuilds by up to about 90%. The work targets single-threaded stretches that still dominate wall-clock time even when make is given every core.
"A typical kernel build consists of a frustratingly large amount of time spent stuck in single-threaded bottlenecks," Stoakes wrote on the kernel mailing list. He reported faster builds on every machine he tried: a 128-thread AMD Threadripper 9980X, a dual-socket EPYC 9754 with 512 threads, and an eight-core Arm laptop.
The largest full-build gain was the dual EPYC system with gcc, from 188.0 seconds to 121.1 seconds. The same allmodconfig on the Threadripper fell from 344.2 seconds to 278.6 with gcc and from 342.6 to 266.8 with clang. Rebuilding after touching one file dropped from about 40 seconds to about 15 on the Threadripper, and from about 71 seconds to about 24 on the EPYC box.
The time is not mostly in the compiler. Make was repeatedly deciding, for every object, whether it was built in or part of a module, so work in a directory grew with the square of its object count. Large trees such as the AMD GPU driver were especially expensive on a rebuild that changed nothing. A small C helper now checks header dependencies that make was parsing from command files listing more than a thousand headers each; in that GPU directory one make instance fell from 2.2 seconds to 0.4. modpost, on the serial tail of every vmlinux link, was also rechecking the same section-mismatch table for every relocation. Caching those lookups shaved about four seconds off an allmodconfig incremental build on the Threadripper.
A larger share of the gain is how modules are finished. An allmodconfig x86-64 tree builds 11,189 module descriptor files, each compiled as ordinary C, with headers, dependency tracking, and often an objtool pass. Stoakes measured that at about 6,300 CPU seconds, 64 seconds of wall time on 128 threads, plus roughly 1.3 GiB of dependency files later builds must reread. Emitting the same descriptors as assembly, using generated offsets so randomized structure layout still places fields correctly, skips that compile. He reported the resulting sections matched the C output byte for byte on a clang allmodconfig build. With descriptors cheap, finishing modules became tens of thousands of millisecond-long jobs. Batching them 128 at a time cut a from-scratch modules build with clang from 28.9 seconds to 15.9 on the Threadripper.
Rust also stops blocking the rest of the tree. With Rust enabled, make currently finishes the Rust crates before descending into C, which stalls every clean Rust build and every LLVM allmodconfig, since that config turns Rust on. The change builds those crates alongside C, while still making out-of-tree Rust modules wait for the core crates. On the Threadripper a clean defconfig with Rust fell from 36.5 seconds to 32.6, and a clean allmodconfig from 297.0 to 278.3.
The numbers are Stoakes's, taken against Linux 7.4-rc1 with a few other pending build changes applied. He said those extras are not required and shift times by only a few seconds. The runs also used pigz and a Rust flag spreading rustc work across eight threads.