summaryrefslogtreecommitdiffstatshomepage
path: root/content/16/index.md
blob: 2cb12b38282c4bb3ce77fa34634e77adeee8c9b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
+++
date = 2022-02-05T19:29:44+01:00
title = "Building the Linux kernel with clang and full LTO"

[taxonomies]
tags = ["experiments"]
+++

My main desktop PC tracks the latest LTS [release](https://kernel.org/) of the
Linux kernel which very recently switched to the 5.15 line. Along with neat new
features like the
[NTSF3 driver](https://lore.kernel.org/all/aa4aa155-b9b2-9099-b7a2-349d8d9d8fbd@paragon-software.com/)
it also includes experimental support for
[Link Time Optimization](https://llvm.org/docs/LinkTimeOptimization.html)
through LLVM's
[`clang` compiler](https://clang.llvm.org/).

I'm not really one to shy away from weird experiments, so I decided to run a
full LTO kernel for a while. If you have a recent version of `clang` and the
[`lld` linker](https://lld.llvm.org/), building one is as easy as toggling
`CONFIG_LTO_CLANG_FULL` and exporting the right flags to `make`:

	make CC=clang LLVM=1 menuconfig
		# CONFIG_LTO_CLANG_FULL=y
	make CC=clang LLVM=1

Subsequent steps are the same as with a normal build:

	sudo make install
	sudo make modules_install

Keep in mind, however, that any out-of-tree modules such as ZFS must also be
built with `clang`. Here I ran into
[this bug](https://bugs.gentoo.org/show_bug.cgi?id=814194) which should soon be
[fixed upstream](https://github.com/openzfs/zfs/pull/13046). For now I
backported that fix locally to ZFS 2.1.2 and am building it like so:

	sudo CC=clang LLVM=1 emerge zfs-kmod

Build times and memory usage when building are increased dramatically with full
LTO. Optimizing `vmlinux.o` alone allocates about 3 to 4 GiB of memory. If you
rely a lot on incremental builds, thin LTO might be the better option here.