Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

samples: add first Rust examples

The beginning of a set of Rust modules that showcase how Rust
modules look like and how to use the abstracted kernel features.

It also includes an example of a Rust host program with
several modules.

These samples also double as tests in the CI.

Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
Co-developed-by: Finn Behrens <me@kloenk.de>
Signed-off-by: Finn Behrens <me@kloenk.de>
Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
Co-developed-by: Milan Landaverde <milan@mdaverde.com>
Signed-off-by: Milan Landaverde <milan@mdaverde.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

+108
+2
samples/Kconfig
··· 263 263 This demonstrates how a user may create their own CoreSight 264 264 configurations and easily load them into the system at runtime. 265 265 266 + source "samples/rust/Kconfig" 267 + 266 268 endif # SAMPLES 267 269 268 270 config HAVE_SAMPLE_FTRACE_DIRECT
+1
samples/Makefile
··· 35 35 obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak/ 36 36 obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) += coresight/ 37 37 obj-$(CONFIG_SAMPLE_FPROBE) += fprobe/ 38 + obj-$(CONFIG_SAMPLES_RUST) += rust/
+30
samples/rust/Kconfig
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + 3 + menuconfig SAMPLES_RUST 4 + bool "Rust samples" 5 + depends on RUST 6 + help 7 + You can build sample Rust kernel code here. 8 + 9 + If unsure, say N. 10 + 11 + if SAMPLES_RUST 12 + 13 + config SAMPLE_RUST_MINIMAL 14 + tristate "Minimal" 15 + help 16 + This option builds the Rust minimal module sample. 17 + 18 + To compile this as a module, choose M here: 19 + the module will be called rust_minimal. 20 + 21 + If unsure, say N. 22 + 23 + config SAMPLE_RUST_HOSTPROGS 24 + bool "Host programs" 25 + help 26 + This option builds the Rust host program samples. 27 + 28 + If unsure, say N. 29 + 30 + endif # SAMPLES_RUST
+5
samples/rust/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + 3 + obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o 4 + 5 + subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs
+3
samples/rust/hostprogs/.gitignore
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + 3 + single
+5
samples/rust/hostprogs/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + 3 + hostprogs-always-y := single 4 + 5 + single-rust := y
+7
samples/rust/hostprogs/a.rs
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + //! Rust single host program sample: module `a`. 4 + 5 + pub(crate) fn f(x: i32) { 6 + println!("The number is {}.", x); 7 + }
+5
samples/rust/hostprogs/b.rs
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + //! Rust single host program sample: module `b`. 4 + 5 + pub(crate) const CONSTANT: i32 = 42;
+12
samples/rust/hostprogs/single.rs
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + //! Rust single host program sample. 4 + 5 + mod a; 6 + mod b; 7 + 8 + fn main() { 9 + println!("Hello world!"); 10 + 11 + a::f(b::CONSTANT); 12 + }
+38
samples/rust/rust_minimal.rs
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + //! Rust minimal sample. 4 + 5 + use kernel::prelude::*; 6 + 7 + module! { 8 + type: RustMinimal, 9 + name: b"rust_minimal", 10 + author: b"Rust for Linux Contributors", 11 + description: b"Rust minimal sample", 12 + license: b"GPL", 13 + } 14 + 15 + struct RustMinimal { 16 + numbers: Vec<i32>, 17 + } 18 + 19 + impl kernel::Module for RustMinimal { 20 + fn init(_module: &'static ThisModule) -> Result<Self> { 21 + pr_info!("Rust minimal sample (init)\n"); 22 + pr_info!("Am I built-in? {}\n", !cfg!(MODULE)); 23 + 24 + let mut numbers = Vec::new(); 25 + numbers.try_push(72)?; 26 + numbers.try_push(108)?; 27 + numbers.try_push(200)?; 28 + 29 + Ok(RustMinimal { numbers }) 30 + } 31 + } 32 + 33 + impl Drop for RustMinimal { 34 + fn drop(&mut self) { 35 + pr_info!("My numbers are {:?}\n", self.numbers); 36 + pr_info!("Rust minimal sample (exit)\n"); 37 + } 38 + }