Skip to content

Linux Kernel Toolchain Selection Fixes#390631

Closed
blitz wants to merge 5 commits intoNixOS:masterfrom
blitz:kernel-compiler-sanity
Closed

Linux Kernel Toolchain Selection Fixes#390631
blitz wants to merge 5 commits intoNixOS:masterfrom
blitz:kernel-compiler-sanity

Conversation

@blitz
Copy link
Contributor

@blitz blitz commented Mar 17, 2025

This is another approach to hopefully finally defeat the compiler/toolchain selection bugs in our Linux kernel derivations. See #389485 for a previous approach at fixing this.

I added basic tests that check whether the right compiler is picked up and whether the configfile and kernel build deriviations agree on the toolchain.

To make clang/LLVM toolchains working again, I switched back to explicitly configuring the right tools to use.

Help is very welcome!

From my perspective, the following attributes must work before we can merge this:

let
  pkgs = import /path/to/nixpkgs {};
  pkgsLLVM = pkgs.pkgsLLVM;
  pkgsAarch64 = pkgs.pkgsCross.aarch64-multiplatform;
in {
  # Vanilla
  linux_latest = pkgs.linux_latest;

  # Vanilla cross
  cross_linux_latest = pkgsAarch64.linux_latest;

  # Vanilla/Clang
  clang_stdenv_linux_latest = pkgs.linux_latest.override {
    # This is the same as clangStdenv.
    stdenv = pkgs.llvmPackages.stdenv;
  };

  # Vanilla/Clang/lld
  llvm_stdenv_linux_latest = let
    stdenv = pkgs.overrideCC pkgs.llvmPackages.stdenv (
      pkgs.llvmPackages.stdenv.cc.override {
        bintools = pkgs.llvmPackages.bintools;
      }
    );
  in pkgs.linux_latest.override {
    inherit stdenv;
  };

  # Full LLVM toolchain
  llvm_linux_latest = pkgsLLVM.linux_latest;
}

Status Overview

  • Vanilla GCC: Works 💚
  • Vanilla cross: Works 💚
  • Vanilla/Clang: Works, but without Rust 💛
  • Vanilla/Clang/lld: Works, but without Rust 💛
  • Full LLVM toolchain: Works, but without Rust 💛
  • Repair Rust support 🦀

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added the 6.topic: kernel The Linux kernel label Mar 17, 2025
@blitz blitz requested review from K900 and alyssais March 17, 2025 10:38
@github-actions github-actions bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. labels Mar 17, 2025
@blitz
Copy link
Contributor Author

blitz commented Mar 17, 2025

@RossComputerGuy Are you interested in collaborating here? :)

@blitz
Copy link
Contributor Author

blitz commented Mar 18, 2025

Even on 48a0850, pkgsLLVM.linux_latest fails to build with:

       >   RUSTC P rust/libmacros.so
       >   BINDGEN rust/bindings/bindings_generated.rs
       > panicked at main.rs:45:36:
       > Unable to generate bindings: ClangDiagnostic("error: argument unused during compilation: '-nostdlibinc' [-Wunused-command-line-argument]\n")
       > make[3]: *** [../rust/Makefile:284: rust/bindings/bindings_generated.rs] Error 101
       > make[3]: *** Deleting file 'rust/bindings/bindings_generated.rs'
       > make[2]: *** [/build/linux-6.12.5/Makefile:1210: prepare] Error 2

@github-actions github-actions bot added the 6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. label Mar 19, 2025
@ofborg ofborg bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Mar 19, 2025
blitz added 3 commits March 20, 2025 00:46
We had a couple of cases where we used the wrong compiler, let's
detect them early.
When the build environment for the configuration and the kernel
building differ, make oldconfig might actually change the
configuration. This happens for example when the kernel build picks up
GCC instead of clang in pkgsLLVM.
This reverts commit 70cc251.

This commit caused gcc to be pulled in as a target compiler for
pkgsLLVM.
@blitz blitz force-pushed the kernel-compiler-sanity branch from d51b831 to 7837f3d Compare March 20, 2025 00:06
@ofborg ofborg bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Mar 20, 2025
@JohnRTitor JohnRTitor requested a review from a team March 20, 2025 04:57
@blitz blitz force-pushed the kernel-compiler-sanity branch from 7837f3d to 54b69f1 Compare March 20, 2025 09:55
@blitz
Copy link
Contributor Author

blitz commented Mar 20, 2025

The kernels above work now, but without Rust support.

blitz added 2 commits March 20, 2025 12:23
Cargo is currently broken for cross-compilation setups like pkgsLLVM.
There is also some sadness with the bindgen wrapper that isn't solved
yet:

>   BINDGEN rust/bindings/bindings_generated.rs
> Unable to generate bindings: clang diagnosed error: error: argument unused during compilation: '-nostdlibinc' [-Wunused-command-line-argument]
>
> make[3]: *** [../rust/Makefile:311: rust/bindings/bindings_generated.rs] Error 1
> make[3]: *** Deleting file 'rust/bindings/bindings_generated.rs'
> make[2]: *** [/build/linux-6.13.7/Makefile:1274: prepare] Error 2
> make[1]: *** [/build/linux-6.13.7/Makefile:251: __sub-make] Error 2
> make[1]: Leaving directory '/build/linux-6.13.7/build'
> make: *** [Makefile:251: __sub-make] Error 2

Opt-out of Rust support for now to be able to build the Linux kernel
with clang at all.
We get lots of these from the clang-wrapper:

  Warning: supplying the --target argument to a nix-wrapped compiler
  may not work correctly - cc-wrapper is currently not designed with
  multi-target compilers in mind. You may want to use an un-wrapped
  compiler instead.

Let's follow its advice.
@blitz blitz force-pushed the kernel-compiler-sanity branch from 54b69f1 to fbc5923 Compare March 20, 2025 11:23
@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Apr 2, 2025
@RossComputerGuy
Copy link
Member

I need this so I can compile ZFS and use an LLVM built kernel. Any updates?

@RossComputerGuy
Copy link
Member

Applying this diff on top of this:

diff --git a/pkgs/os-specific/linux/kernel/common-config.nix b/pkgs/os-specific/linux/kernel/common-config.nix
index 08d2291d686c..d1a7565f5dbf 100644
--- a/pkgs/os-specific/linux/kernel/common-config.nix
+++ b/pkgs/os-specific/linux/kernel/common-config.nix
@@ -511,7 +511,7 @@ let
         DRM_AMD_DC_DCN = lib.mkIf (with stdenv.hostPlatform; isx86 || isPower64) (
           whenBetween "5.11" "6.4" yes
         );
-        DRM_AMD_DC_FP = whenAtLeast "6.4" yes;
+        DRM_AMD_DC_FP = lib.mkIf (!stdenv.cc.isClang) (whenAtLeast "6.4" yes);
         DRM_AMD_DC_HDCP = whenBetween "5.5" "6.4" yes;
         DRM_AMD_DC_SI = whenAtLeast "5.10" yes;

diff --git a/pkgs/os-specific/linux/kernel/generic.nix b/pkgs/os-specific/linux/kernel/generic.nix
index 77bebfa4a9e9..2f4854fb7266 100644
--- a/pkgs/os-specific/linux/kernel/generic.nix
+++ b/pkgs/os-specific/linux/kernel/generic.nix
@@ -112,9 +112,7 @@ let
     rustAvailable = lib.meta.availableOn stdenv.hostPlatform rustc
       # Cargo is broken for cross-compilation. See here for context:
       # pkgs/development/compilers/rust/cargo.nix
-      && !(stdenv.hostPlatform.isx86 && stdenv.buildPlatform != stdenv.hostPlatform)
-      # Bindgen and clang don't get along right now.
-      && !stdenv.cc.isClang;
+      && !(stdenv.hostPlatform.isx86 && stdenv.buildPlatform != stdenv.hostPlatform);

     features = kernelFeatures; # Ensure we know of all extra patches, etc.
   };
diff --git a/pkgs/os-specific/linux/kernel/manual-config.nix b/pkgs/os-specific/linux/kernel/manual-config.nix
index 0b590ad798dd..2b91cdd0aa87 100644
--- a/pkgs/os-specific/linux/kernel/manual-config.nix
+++ b/pkgs/os-specific/linux/kernel/manual-config.nix
@@ -459,6 +459,7 @@ let
     "HOSTAR=${buildPackages.stdenv.cc.bintools}/bin/${buildPackages.stdenv.cc.targetPrefix}ar"
     "HOSTLD=${buildPackages.stdenv.cc.bintools}/bin/${buildPackages.stdenv.cc.targetPrefix}ld"
     "ARCH=${stdenv.hostPlatform.linuxArch}"
+    "CFLAGS=-Wno-unused-command-line-argument"
   ] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
     "CROSS_COMPILE=${stdenv.cc.targetPrefix}"
   ] ++ (stdenv.hostPlatform.linux-kernel.makeFlags or [])

I get this error:

linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/libbpf.h                                                                                                             21:59:57 [88/1969]
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/btf.h
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/libbpf_common.h
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/libbpf_legacy.h
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/bpf_helpers.h
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/bpf_tracing.h
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/bpf_endian.h
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/bpf_core_read.h
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/skel_internal.h
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/libbpf_version.h
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/usdt.bpf.h
linux>   INSTALL /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libbpf//include/bpf/bpf_helper_defs.h
linux>   INSTALL libbpf_headers
linux>   MKDIR     /build/linux-6.12.21/build/tools/bpf/resolve_btfids//libsubcmd
linux>   CC      /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libsubcmd/exec-cmd.o
linux> exec-cmd.c:2:10: fatal error: linux/compiler.h: No such file or directory
linux>     2 | #include <linux/compiler.h>
linux>       |          ^~~~~~~~~~~~~~~~~~
linux> compilation terminated.
linux> make[6]: *** [/build/linux-6.12.21/tools/build/Makefile.build:106: /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libsubcmd/exec-cmd.o] Error 1
linux> make[5]: *** [Makefile:78: /build/linux-6.12.21/build/tools/bpf/resolve_btfids/libsubcmd/libsubcmd-in.o] Error 2
linux> make[4]: *** [Makefile:58: /build/linux-6.12.21/build/tools/bpf/resolve_btfids//libsubcmd/libsubcmd.a] Error 2
linux> make[3]: *** [Makefile:76: bpf/resolve_btfids] Error 2
linux> make[2]: *** [/build/linux-6.12.21/Makefile:1378: tools/bpf/resolve_btfids] Error 2
linux> make[1]: *** [/build/linux-6.12.21/Makefile:224: __sub-make] Error 2
linux> make[1]: Leaving directory '/build/linux-6.12.21/build'
linux> make: *** [Makefile:224: __sub-make] Error 2

@zowoq
Copy link
Contributor

zowoq commented Apr 26, 2025

This seems to work:

(reverted linux: disable Rust support by default for clang commit)

diff --git a/pkgs/os-specific/linux/kernel/manual-config.nix b/pkgs/os-specific/linux/kernel/manual-config.nix
index c10d5f6b13b2..3473a3ead8c0 100644
--- a/pkgs/os-specific/linux/kernel/manual-config.nix
+++ b/pkgs/os-specific/linux/kernel/manual-config.nix
@@ -245,6 +245,8 @@ lib.makeOverridable (
 
         RUST_LIB_SRC = lib.optionalString withRust rustPlatform.rustLibSrc;
 
+        BINDGEN_EXTRA_CLANG_ARGS = "-Qunused-arguments";
+
         # avoid leaking Rust source file names into the final binary, which adds
         # a false dependency on rust-lib-src on targets with uncompressed kernels
         KRUSTFLAGS = lib.optionalString withRust "--remap-path-prefix ${rustPlatform.rustLibSrc}=/";

@RossComputerGuy
Copy link
Member

Thank you for that fix, it builds! I've opened #402198 with the new fixes.

@blitz
Copy link
Contributor Author

blitz commented Apr 27, 2025

Closing in favor of #402198! Thanks for carrying this forward! 🙏🏻

@blitz blitz closed this Apr 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 6.topic: kernel The Linux kernel 6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants