From 9dc696cc4d2dff595bced7842b66575ddbea56c6 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 7 Nov 2026 13:29:15 +0000 Subject: [PATCH] kernel/linux/{scripts,src}: add a KASAN/lockdep debug kernel, and rename slab caches gokrazy-debug-kernel.sh builds the gokrazy kernel package's kernel again with KASAN, lockdep, DEBUG_VM and DEBUG_NET, for tailscale.com's lean harness to boot (TSWG_DEBUG_KERNEL=1). build-for-gokrazy.sh builds against it when given TSWG_KDIR, since those options change kernel data structures. Its first find: our slab caches had upstream WireGuard's names (via KMEM_CACHE), which clash with wireguard.ko or a built-in WireGuard, as gokrazy has, and make slab_common warn at load. Give them tswg_ names. Updates #426 Signed-off-by: Brad Fitzpatrick Change-Id: I2e7d5448bcf8b2fb68810486f7e5d4bf2af4558d --- kernel/linux/scripts/build-for-gokrazy.sh | 5 +- kernel/linux/scripts/gokrazy-debug-kernel.sh | 68 ++++++++++++++++++++ kernel/linux/src/allowedips.c | 6 +- kernel/linux/src/peer.c | 4 +- kernel/linux/src/ratelimiter.c | 6 +- 5 files changed, 85 insertions(+), 4 deletions(-) create mode 100755 kernel/linux/scripts/gokrazy-debug-kernel.sh diff --git a/kernel/linux/scripts/build-for-gokrazy.sh b/kernel/linux/scripts/build-for-gokrazy.sh index 142c870e6..9dca2fbd8 100755 --- a/kernel/linux/scripts/build-for-gokrazy.sh +++ b/kernel/linux/scripts/build-for-gokrazy.sh @@ -5,6 +5,9 @@ # gokrazy-kdir.sh) in a private copy of src/ under OUTDIR, and prints the # path of the resulting tswg.ko to stdout. All build output goes to stderr. # +# If TSWG_KDIR is set, it builds against that kernel build directory +# instead (such as one from gokrazy-debug-kernel.sh). +# # usage: build-for-gokrazy.sh GOKRAZY_KERNEL_PACKAGE_DIR OUTDIR set -eu @@ -14,5 +17,5 @@ if [ $# -ne 2 ]; then exit 2 fi scripts=$(dirname "$0") -kdir=$("$scripts/gokrazy-kdir.sh" "$1") +kdir=${TSWG_KDIR:-$("$scripts/gokrazy-kdir.sh" "$1")} exec "$scripts/build.sh" "$kdir" "$2" diff --git a/kernel/linux/scripts/gokrazy-debug-kernel.sh b/kernel/linux/scripts/gokrazy-debug-kernel.sh new file mode 100755 index 000000000..b3979f5b8 --- /dev/null +++ b/kernel/linux/scripts/gokrazy-debug-kernel.sh @@ -0,0 +1,68 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# +# gokrazy-debug-kernel.sh builds a debug variant of a gokrazy kernel package's +# kernel (see gokrazy-kdir.sh): the same release, patches and config, plus +# KASAN, lockdep, DEBUG_VM and DEBUG_NET, for catching memory and locking +# bugs in tswg. It prints the build directory to stdout; the kernel is +# arch/x86/boot/bzImage there, and tswg.ko must be built against that +# directory (TSWG_KDIR=... build-for-gokrazy.sh), since the debug options +# change kernel data structures. +# +# Results are cached in $TSLINUX_CACHE (default ~/.cache/tslinux). +# +# usage: gokrazy-debug-kernel.sh GOKRAZY_KERNEL_PACKAGE_DIR + +set -eu + +if [ $# -ne 1 ]; then + echo "usage: $0 GOKRAZY_KERNEL_PACKAGE_DIR" >&2 + exit 2 +fi +pkg=$1 +cache=${TSLINUX_CACHE:-$HOME/.cache/tslinux} + +url=$(cat "$pkg/_build/upstream-url.txt") +tarball=${url##*/} +srcname=${tarball%.tar.*} + +config=$(mktemp) +trap 'rm -f "$config"' EXIT +"$(dirname "$0")/extract-ikconfig" "$pkg/vmlinuz" > "$config" +hash=$(sha256sum "$config" "$0" | sha256sum | cut -c1-12) + +kdir=$cache/gokrazy/$srcname-$hash-debug +if [ -f "$kdir/.tswg-built" ]; then + echo "$kdir" + exit 0 +fi + +mkdir -p "$cache/tarballs" +if [ ! -f "$cache/tarballs/$tarball" ]; then + echo "downloading $url" >&2 + curl -sSfL -o "$cache/tarballs/$tarball.tmp" "$url" + mv "$cache/tarballs/$tarball.tmp" "$cache/tarballs/$tarball" +fi + +rm -rf "$kdir" +mkdir -p "$kdir" +echo "unpacking $tarball" >&2 +tar -xf "$cache/tarballs/$tarball" -C "$kdir" --strip-components=1 +if [ -f "$pkg/_build/series" ]; then + while read -r patch; do + [ -n "$patch" ] || continue + patch -s -d "$kdir" -p1 < "$pkg/_build/$patch" + done < "$pkg/_build/series" +fi +cp "$config" "$kdir/.config" +"$kdir/scripts/config" --file "$kdir/.config" \ + -e DEBUG_KERNEL \ + -e KASAN -e KASAN_GENERIC -e KASAN_INLINE \ + -e PROVE_LOCKING -e DEBUG_ATOMIC_SLEEP \ + -e DEBUG_VM -e DEBUG_NET \ + -d DEBUG_INFO_BTF -d RANDOMIZE_BASE +echo "building debug $srcname" >&2 +make -s -C "$kdir" olddefconfig >&2 +make -s -C "$kdir" -j"$(nproc)" bzImage modules_prepare >&2 +touch "$kdir/.tswg-built" +echo "$kdir" diff --git a/kernel/linux/src/allowedips.c b/kernel/linux/src/allowedips.c index 5ece9acad..33df2419d 100644 --- a/kernel/linux/src/allowedips.c +++ b/kernel/linux/src/allowedips.c @@ -411,7 +411,11 @@ struct wg_peer *wg_allowedips_lookup_src(struct allowedips *table, int __init wg_allowedips_slab_init(void) { - node_cache = KMEM_CACHE(allowedips_node, 0); + /* TSWG: not KMEM_CACHE, whose names would clash with wireguard.ko. */ + node_cache = kmem_cache_create("tswg_allowedips_node", + sizeof(struct allowedips_node), + __alignof__(struct allowedips_node), + 0, NULL); return node_cache ? 0 : -ENOMEM; } diff --git a/kernel/linux/src/peer.c b/kernel/linux/src/peer.c index 1cb502a93..ed87a422e 100644 --- a/kernel/linux/src/peer.c +++ b/kernel/linux/src/peer.c @@ -229,7 +229,9 @@ void wg_peer_put(struct wg_peer *peer) int __init wg_peer_init(void) { - peer_cache = KMEM_CACHE(wg_peer, 0); + /* TSWG: not KMEM_CACHE, whose names would clash with wireguard.ko. */ + peer_cache = kmem_cache_create("tswg_peer", sizeof(struct wg_peer), + __alignof__(struct wg_peer), 0, NULL); return peer_cache ? 0 : -ENOMEM; } diff --git a/kernel/linux/src/ratelimiter.c b/kernel/linux/src/ratelimiter.c index 2b3867f69..b14ada937 100644 --- a/kernel/linux/src/ratelimiter.c +++ b/kernel/linux/src/ratelimiter.c @@ -161,7 +161,11 @@ int wg_ratelimiter_init(void) if (++init_refcnt != 1) goto out; - entry_cache = KMEM_CACHE(ratelimiter_entry, 0); + /* TSWG: not KMEM_CACHE, whose names would clash with wireguard.ko. */ + entry_cache = kmem_cache_create("tswg_ratelimiter_entry", + sizeof(struct ratelimiter_entry), + __alignof__(struct ratelimiter_entry), + 0, NULL); if (!entry_cache) goto err;