Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out-of-tree module failed to build if the kernel source path is different #792

Closed
Leo1003 opened this issue Jun 17, 2022 · 7 comments · Fixed by rust-lang/rust#98225
Closed
Labels
• kbuild Related to building the kernel, `make`, `Kbuild`, `Kconfig` options...

Comments

@Leo1003
Copy link

Leo1003 commented Jun 17, 2022

As most of the Linux distribution install the kernel with their package (.deb, .rpm, ...). I also tried to do the same thing to test Linux kernel with Rust support. However, when I build some out-of-tree Rust kernel module, it failed to compile with the following message:

error[E0461]: couldn't find crate `core` with expected target triple target-13637732820183936185
  |
  = note: the following crate versions were found:
          crate `core`, target triple target-4719486650351723570: /usr/src/linux-headers-5.18.0-rc2-2-rust/rust/libcore.rmeta

error[E0461]: couldn't find crate `compiler_builtins` with expected target triple target-13637732820183936185
  |
  = note: the following crate versions were found:
          crate `compiler_builtins`, target triple target-4719486650351723570: /usr/src/linux-headers-5.18.0-rc2-2-rust/rust/libcompiler_builtins
.rmeta

error[E0461]: couldn't find crate `kernel` with expected target triple target-13637732820183936185
  --> /home/leo/rust-kernel-modules/rust-random/rust_random.rs:11:5
   |
11 | use kernel::{
   |     ^^^^^^
   |
   = note: the following crate versions were found:
           crate `kernel`, target triple target-4719486650351723570: /usr/src/linux-headers-5.18.0-rc2-2-rust/rust/libkernel.rmeta

After digging some codes, I found that rustc record the target triple as the path of target.json file.
https://doc.rust-lang.org/stable/nightly-rustc/rustc_target/spec/enum.TargetTriple.html

And the target triple appears in the error messages can be calculated with the following codes:

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;

#[allow(unused_variables)]
fn main() {
    let mut hasher = DefaultHasher::new();
    // target-4719486650351723570
    let path = PathBuf::from("/home/leo/linux-rust/src/build/rust/target.json");
    // target-13637732820183936185
    let path = PathBuf::from("/usr/src/linux-headers-5.18.0-rc2-2-rust/rust/target.json");
    path.hash(&mut hasher);
    let hash = hasher.finish();
    println!("target-{}", hash)
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=aa52d204a7d4167cfd84076776ab957a

The above codes is reference from rust-lang/rust/compiler/rustc_target/src/spec/mod.rs:2445

P.s. I think that this is the problem of the compiler, however, this issue must be resolved if we want to support out-of-tree Rust kernel module in the future.

@Leo1003 Leo1003 added the • kbuild Related to building the kernel, `make`, `Kbuild`, `Kconfig` options... label Jun 17, 2022
@bjorn3
Copy link
Member

bjorn3 commented Jun 17, 2022

Yeah, I think rustc should hash the file contents rather than the file path.

JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 19, 2022
…agisa

Make debug_triple depend on target json file content rather than file path

This ensures that changes to target json files will force a recompilation. And more importantly that moving the files doesn't force a recompilation.

This should fix Rust-for-Linux/linux#792 (cc `@ojeda)`
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 19, 2022
…agisa

Make debug_triple depend on target json file content rather than file path

This ensures that changes to target json files will force a recompilation. And more importantly that moving the files doesn't force a recompilation.

This should fix Rust-for-Linux/linux#792 (cc ``@ojeda)``
@bjorn3
Copy link
Member

bjorn3 commented Jun 20, 2022

rust-lang/rust#98225 has landed. This issue should be fixed in rustc 1.63.0.

@ojeda
Copy link
Member

ojeda commented Jun 20, 2022

Thanks a lot @bjorn3!

@Leo1003
Copy link
Author

Leo1003 commented Jun 22, 2022

Thank you for fixing this issue which troubled me for a long time!

@YakoYakoYokuYoku
Copy link

Now with Rust 1.63.0, which btw hasn't required further patching, I could confirm that I'm able to build out of tree modules with the kernel build system using v9 of the patchset.

$ make KDIR=/lib/modules/5.19.1-arch2-1/build LLVM=1
make -C /lib/modules/5.19.1-arch2-1/build M=$PWD
make[1]: Entering directory '/usr/lib/modules/5.19.1-arch2-1/build'
  RUSTC [M] ~/Codes/Rust/Tests/kernelmoduletest/printk/rust_printk.o
  MODPOST ~/Codes/Rust/Tests/kernelmoduletest/printk/Module.symvers
  CC [M]  ~/Codes/Rust/Tests/kernelmoduletest/printk/rust_printk.mod.o
  LD [M]  ~/Codes/Rust/Tests/kernelmoduletest/printk/rust_printk.ko
  BTF [M] ~/Codes/Rust/Tests/kernelmoduletest/printk/rust_printk.ko
die__process_class: tag not supported 0x33 (variant_part)!
die__process_class: tag not supported 0x2f (template_type_parameter)!
make[1]: Leaving directory '/usr/lib/modules/5.19.1-arch2-1/build'

Said module, alongside a C counterpart, was tested in a QEMU VM, both loading and removing work as expected. If Rust 1.63.0 becomes mandatory then this issue can be closed indeed.

@bjorn3
Copy link
Member

bjorn3 commented Aug 14, 2022

Thanks for confirming the fix @YakoYakoYokuYoku! @ojeda will probably update to Rust 1.63.0 soon.

@ojeda
Copy link
Member

ojeda commented Oct 29, 2024

Nowadays we require a way higher version.

@ojeda ojeda closed this as completed Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
• kbuild Related to building the kernel, `make`, `Kbuild`, `Kconfig` options...
Development

Successfully merging a pull request may close this issue.

4 participants