Skip to content

eBPF-based security platform

treewide: compile every BPF object for instruction set v4

Daniel Borkmanneda017488800
diff --git a/AGENTS.md b/AGENTS.md
index e38aa02c..290475c6 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -320,6 +320,10 @@ one usually still compiles and passes tests.
- **BPF is CO-RE.** Hard-coded struct offsets sign fine and fail on the
wrong kernel, where the verifier rejects the payload
(`verifier_rejected`).
+- **BPF targets instruction set v4.** Every `clang -target bpf` in the
+ tree passes `-mcpu=v4`: the upatch build, the embedded objects, the
+ configure probe and the tests. Not `-mcpu=probe`, which describes the
+ build host's kernel, not the targets'.
- **Upatch state lives in BPF arenas where possible.** A upatch that
accumulates records keeps them in its one `BPF_MAP_TYPE_ARENA` map and
ships a `shirudo_serialize__<map>` program that walks them; hash and
diff --git a/Documentation/concepts/background/co-re-btf.rst b/Documentation/concepts/background/co-re-btf.rst
index 04e20aae..e99b9538 100644
--- a/Documentation/concepts/background/co-re-btf.rst
+++ b/Documentation/concepts/background/co-re-btf.rst
@@ -109,7 +109,7 @@ How shirudo uses CO-RE
----------------------
``shirudo build`` compiles a ``.bpf.c`` file with
-``clang -target bpf -O2 -g`` against a ``vmlinux.h`` and hands the object
+``clang -target bpf -mcpu=v4 -O2 -g`` against a ``vmlinux.h`` and hands the object
to gen_loader. Unless
the operator names a header, the build uses ``./vmlinux.h``, then
``./vendor/linux/vmlinux.h``, then the copy installed with shirudo
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index df8e1494..c78eb44d 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -89,6 +89,9 @@ signed config image.
boot settings, the bundle stamp
* - ``t1008-kernel-ami-config``
- The slim kernel configuration for NitroTPM CRB, EC2, EFI and SSM
+ * - ``t1009-upatch-isa``
+ - The upatch build compiles for BPF instruction set v4, and a
+ ``-mcpu`` in ``--extra-cflags`` replaces it
``signing`` (2)
~~~~~~~~~~~~~~~
diff --git a/Documentation/reference/cli/build.rst b/Documentation/reference/cli/build.rst
index cd20386c..166dddc6 100644
--- a/Documentation/reference/cli/build.rst
+++ b/Documentation/reference/cli/build.rst
@@ -60,11 +60,13 @@ Building a upatch
``./vmlinux.h``, else ``./vendor/linux/vmlinux.h``, else the installed
``vmlinux.h`` (``/usr/share/shirudo/vmlinux.h`` in the operator
package, ``/usr/local/share/shirudo/vmlinux.h`` after ``make
- install``). It then runs ``/usr/bin/clang -target bpf -O2 -g -Wall
- -Wextra -Wshadow -Wtype-limits`` with the ``vmlinux.h`` directory,
- the libbpf headers and the shirudo BPF headers on the include path,
- plus ``--extra-cflags``. The clang path is fixed when the helper is built,
- and ``PATH`` is not searched. The object goes to a temporary file in
+ install``). It then runs ``/usr/bin/clang -target bpf -mcpu=v4 -O2
+ -g -Wall -Wextra -Wshadow -Wtype-limits`` with the ``vmlinux.h``
+ directory, the libbpf headers and the shirudo BPF headers on the
+ include path, plus ``--extra-cflags``. ``-mcpu=v4`` selects BPF
+ instruction set v4, the newest clang knows and one every target kernel
+ runs. The clang path is fixed when the helper is built, and ``PATH`` is
+ not searched. The object goes to a temporary file in
``/tmp``, or with ``--emit-artifacts`` beside the output.
#. A ``.bpf.o`` or ``.o`` input is packaged as it is. Any other input is
refused.
@@ -160,7 +162,8 @@ Options
* - ``--extra-cflags``
- ``<flags>``, one argument
- none
- - Further clang flags.
+ - Further clang flags. They come after the built-in ones, so a
+ ``-mcpu`` among them replaces ``-mcpu=v4``.
* - ``--emit-artifacts``
- none
- off
diff --git a/Documentation/upatches/write/memory-consistency/blitmus.rst b/Documentation/upatches/write/memory-consistency/blitmus.rst
index f4ac5b91..ec374407 100644
--- a/Documentation/upatches/write/memory-consistency/blitmus.rst
+++ b/Documentation/upatches/write/memory-consistency/blitmus.rst
@@ -72,9 +72,9 @@ Write the test
clang emits ``atomic_fetch_add`` for ``__sync_fetch_and_add()`` even
when the result is unused, so that call is fully ordered either way.
- ``load_acquire`` and ``store_release`` need clang's ``-mcpu=v4``, which
- ``shirudo build --extra-cflags=-mcpu=v4`` passes. ``bpm build``
- compiles a package's sources without it.
+ ``load_acquire`` and ``store_release`` need instruction set v4, which
+ the upatch build selects with ``-mcpu=v4``, and a current clang, such
+ as clang 23, to lower them.
blitmus has no ``smp_rmb()`` or ``smp_wmb()``, as BPF has no
instruction for a read or a write barrier alone, and no RCU or lock
diff --git a/Makefile.config b/Makefile.config
index d0f461a1..33579a41 100644
--- a/Makefile.config
+++ b/Makefile.config
@@ -194,7 +194,7 @@ probe-static-target-link := $(shell \
endif
probe-clang-bpf := $(shell printf 'int main(void){return 0;}\n' | \
- clang -target bpf -x c -c -o /dev/null - 2>/dev/null && echo yes)
+ clang -target bpf -mcpu=v4 -x c -c -o /dev/null - 2>/dev/null && echo yes)
# Probe the vendored BPF UAPI headers shirudo builds against (not the
# host's /usr/include/linux/bpf.h, which on stock distros lags the kernel
@@ -258,7 +258,7 @@ config.mk:
fi; \
check "luajit" "$(probe-luajit)" \
"install libluajit-5.1-dev / luajit-devel"; \
- check "clang -target bpf" "$(probe-clang-bpf)" \
+ check "clang -target bpf -mcpu=v4" "$(probe-clang-bpf)" \
"install current upstream clang/llvm"; \
check "vendored BPF UAPI keyring_id" "$(probe-keyring-id)" \
"vendor/libbpf/include/uapi predates signed BPF; run make bpf-deps"; \
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 4c2e49d1..c658c44d 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -192,8 +192,8 @@ endif
# -Wmissing-declarations; the BPF_PROG() / BPF_KPROBE() macros
# generate auto-named params that often go unused. Neither is
# fixable upstream of us, so silence both for BPF compilation.
-CLANG_BPF := $(CLANG) -target bpf -O2 -g -Wall -Wextra -Wvla -Wshadow -Wtype-limits \
- -Wimplicit-fallthrough \
+CLANG_BPF := $(CLANG) -target bpf -mcpu=v4 -O2 -g -Wall -Wextra -Wvla -Wshadow \
+ -Wtype-limits -Wimplicit-fallthrough \
-Wno-missing-declarations -Wno-unused-parameter \
-Wdeclaration-after-statement $(WERROR_CFLAGS)
diff --git a/testing/functional/build/t1009-upatch-isa.sh b/testing/functional/build/t1009-upatch-isa.sh
new file mode 100644
index 00000000..79e02056
--- /dev/null
+++ b/testing/functional/build/t1009-upatch-isa.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t1009 -- the upatch build compiles for BPF instruction set v4, which
+# load-acquire and store-release need, unless --extra-cflags names another
+# -mcpu. A sign-extending load is one instruction, BPF_MEMSX, only in v4,
+# and every clang that builds shirudo emits it there.
+
+cd "$(dirname "$0")/../.."
+. ./test-lib.sh
+S=../shirudo
+SHB=../tools/shirudo-build/shirudo-build
+command -v clang >/dev/null 2>&1 || test_skip_all "no clang"
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+[ -x "$S" ] || test_skip_all "shirudo not built"
+[ -x "$SHB" ] || test_skip_all "shirudo-build not built (make shirudo-build)"
+[ -e ../vendor/linux/vmlinux.h ] || test_skip_all "no vendor/linux/vmlinux.h"
+W=$(mktemp -d)
+trap 'rm -rf "$W"' EXIT
+$S genkey --keys-dir "$W/keys" >/dev/null 2>&1 ||
+ test_skip_all "genkey failed (OpenSSL without ML-DSA?)"
+
+cat >"$W/isa.bpf.c" <<'EOF'
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+volatile __s8 shirudo_test_isa_byte;
+__s64 shirudo_test_isa_wide;
+
+SEC("fentry/do_nanosleep")
+int BPF_PROG(shirudo_test_isa)
+{
+ shirudo_test_isa_wide = shirudo_test_isa_byte;
+ return 0;
+}
+EOF
+
+# Builds $W/<name>.upatch and keeps the object as $W/<name>.o.
+build() {
+ name=$1
+ shift
+ (cd .. && tools/shirudo-build/shirudo-build "$W/isa.bpf.c" \
+ -o "$W/$name.upatch" --name "t1009-$name" \
+ --vmlinux vendor/linux/vmlinux.h --keys-dir "$W/keys" \
+ --emit-artifacts "$@") >"$W/$name.log" 2>&1
+}
+
+# Whether an executable section of the BPF object holds a BPF_MEMSX load.
+memsx() {
+ python3 - "$1" <<'EOF'
+import struct
+import sys
+
+data = open(sys.argv[1], "rb").read()
+shoff, = struct.unpack_from("<Q", data, 0x28)
+shentsize, shnum = struct.unpack_from("<HH", data, 0x3a)
+for i in range(shnum):
+ sh = shoff + i * shentsize
+ flags, = struct.unpack_from("<Q", data, sh + 0x08)
+ offset, size = struct.unpack_from("<QQ", data, sh + 0x18)
+ if not flags & 0x4:
+ continue
+ for pc in range(offset, offset + size, 8):
+ if data[pc] in (0x81, 0x89, 0x91):
+ sys.exit(0)
+sys.exit(1)
+EOF
+}
+
+test_expect_success 'the upatch build compiles for BPF instruction set v4' '
+ build default &&
+ [ -s "$W/default.upatch" ] &&
+ memsx "$W/default.o"
+'
+
+test_expect_success '--extra-cflags overrides the instruction set' '
+ build v3 --extra-cflags -mcpu=v3 &&
+ [ -s "$W/v3.upatch" ] &&
+ ! memsx "$W/v3.o"
+'
+
+test_done
diff --git a/testing/functional/upatch/t3004-upatch-serializer-metadata.sh b/testing/functional/upatch/t3004-upatch-serializer-metadata.sh
index 49986a07..fdc7611d 100644
--- a/testing/functional/upatch/t3004-upatch-serializer-metadata.sh
+++ b/testing/functional/upatch/t3004-upatch-serializer-metadata.sh
@@ -116,7 +116,7 @@ test_expect_success 'builder rejects serializer targeting reserved output map' '
test_expect_success 'builder rejects duplicate serializer targets' '
if ! command -v llvm-objcopy >/dev/null 2>&1; then exit 0; fi
(cd .. &&
- clang -target bpf -O2 -g -I vendor/linux -I vendor/libbpf \
+ clang -target bpf -mcpu=v4 -O2 -g -I vendor/linux -I vendor/libbpf \
-I bpf/lib -c testing/fixtures/arena-tree-serializer.bpf.c \
-o "$WORK/arena-tree-serializer.o" &&
llvm-objcopy --redefine-sym \
diff --git a/testing/helper/test-seal-seccomp.py b/testing/helper/test-seal-seccomp.py
index 303e770d..bf94c587 100644
--- a/testing/helper/test-seal-seccomp.py
+++ b/testing/helper/test-seal-seccomp.py
@@ -20,7 +20,8 @@ with tempfile.TemporaryDirectory(prefix="shirudo-seal-seccomp-") as directory:
work = Path(directory)
(work / "vmlinux.h").write_text(header)
command = shlex.split(os.environ.get("CLANG", "clang"))
- command += ["-target", "bpf", "-O2", "-g", "-Wall", "-Wextra", "-Werror",
+ command += ["-target", "bpf", "-mcpu=v4", "-O2", "-g", "-Wall", "-Wextra",
+ "-Werror",
"-Wno-missing-declarations", "-Wno-unused-parameter",
"-Wdeclaration-after-statement", "-I" + str(work),
"-I" + str(root / "include"),
diff --git a/tools/shirudo-build/compile.c b/tools/shirudo-build/compile.c
index a5f46674..edbccbf2 100644
--- a/tools/shirudo-build/compile.c
+++ b/tools/shirudo-build/compile.c
@@ -105,6 +105,12 @@ int build_compile(const char *source, const char *output,
push(argv, &n, CLANG_PATH);
push(argv, &n, "-target");
push(argv, &n, "bpf");
+ /*
+ * The newest instruction set, which every kernel with signed BPF
+ * loaders runs, and not -mcpu=probe: the operator builds for its
+ * targets, not for the kernel it runs on.
+ */
+ push(argv, &n, "-mcpu=v4");
push(argv, &n, "-O2");
push(argv, &n, "-g");
push(argv, &n, "-Wall");