Rust-for-Linux / linux

Adding support for the Rust language to the Linux kernel.
https://rust-for-linux.com
Other
3.83k stars 399 forks source link

Build out-of-tree kernel module "hello world" #1082

Closed frabervo closed 1 month ago

frabervo commented 1 month ago

I want to build an out-of-tree kernel module (hello world) that prints "Hello from Rust" when the module is loaded and print "Goodbye from Rust" when the module is unloaded.

Here is the Makefile:

NAME=hello_rust

ifndef KERNELRELEASE
ifndef KDIR
KDIR:=/lib/modules/`uname -r`/build
endif
PWD := $(shell pwd)
rust_flags=CROSS_COMPILE=x86_64-linux-gnu- HOSTRUSTC=rustc-1.75 RUSTC=rustc BINDGEN=bindgen-0.65 RUSTFMT=rustfmt-1.75 RUST_LIB_SRC=/usr/src/rustc-1.74.1/library
all:
    @$(MAKE) $(rust_flags) -C $(KDIR) M=$(PWD) modules
install:
    @$(MAKE) $(rust_flags) -C $(KDIR) M=$(PWD) modules_install
clean:
    @rm -f *.o *.ko *.mod* .*.cmd *.d Module.symvers modules.order
    @rm -rf .tmp_versions
else
obj-m := $(NAME).o
endif

The Rust code:

// SPDX-License-Identifier: GPL-2.0

//! Rust hello world example.

use kernel::prelude::*;

module! {
    type: HelloRust,
    name: "hello_rust",
    author: "username",
    description: "Rust hello world example",
    license: "GPL",
}

struct HelloRust {}

impl kernel::Module for HelloRust {
    fn init(_module: &'static ThisModule) -> Result<Self> {
        pr_info!("Hello from Rust\n");
        Ok(HelloRust {})
    }
}

impl Drop for HelloRust {
    fn drop(&mut self) {
        pr_info!("Goodbye from Rust\n");
    }
}

When i try to build the module with make, i get the error that rustc versions used to compile the crates core, kernel and compiler_builtins are incompatible with the one i'm using.

Example:

make[1]: Entering directory '/usr/src/linux-headers-6.8.0-31-generic'
  RUSTC [M] /home/frabervo/Documents/rust-in-linux-kernel/kernel-modules/hello-world/hello_rust.o
error[E0514]: found crate `core` compiled by an incompatible version of rustc
  |
  = note: the following crate versions were found:
          crate `core` compiled by rustc 1.75.0 (82e1608df 2023-12-21) (built from a source tarball): /usr/src/linux-lib-rust-6.8.0-31-generic/rust/libcore.rmeta
  = help: please recompile that crate using this compiler (rustc 1.75.0 (82e1608df 2023-12-21)) (consider running `cargo clean` first)

The complete error message is attached.

Please any hints that can help me? build_error.txt

bjorn3 commented 1 month ago

It looks like the kernel was built with Ubuntu's version of rustc, but you are compiling the kernel module with a version of rustc you got from rustup. This is not allowed. You need to use the exact same rustc version for compiling the kernel module as was originally used for compiling the kernel. Try installing the rustc package using sudo apt install rustc and set the RUSTC env var to /usr/bin/rustc to force usage of the Ubuntu rustc version rather than defaulting to the rustup version.

frabervo commented 1 month ago

Thank you for the hint. It was able to build.