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

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

Closed
frabervo opened this issue May 17, 2024 · 2 comments
Closed

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

frabervo opened this issue May 17, 2024 · 2 comments

Comments

@frabervo
Copy link

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.

  • System: Ubuntu 24.04 LTS
  • Kernel: 6.8.0-31-generic
  • rust toolchain:
 1.75-x86_64-unknown-linux-gnu (default)
  • bindgen: version 0.65

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
Copy link
Member

bjorn3 commented May 17, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants