Rust is a second language supported in the Linux kernel alongside C. The motivation is safety: Rust’s type system and ownership model eliminate entire classes of bugs — use-after-free, data races, null pointer dereferences — that have historically plagued kernel code. Rust support was merged into the mainline kernel starting with Linux 6.1, and the set of abstractions covering kernel APIs continues to grow with each release.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/deelerdev/linux/llms.txt
Use this file to discover all available pages before exploring further.
Why Rust in the kernel?
Kernel code runs in a privileged environment where a single memory safety bug can compromise an entire system. C gives you the control you need to write a kernel but no static guardrails to prevent common errors. Rust provides the same low-level control as C while using its type system and borrow checker to make many of those errors impossible to compile. The goal is not to rewrite the kernel in Rust but to allow new components — especially drivers and file systems — to be written safely without giving up performance.Rust kernel code can only link against
core, not std. All crates intended for use in the kernel must include #![no_std] at the crate root.Installing the required toolchain
You need three components beyond a standard Rust installation: the Rust compiler itself, the standard library source (so the build system can cross-compilecore), and bindgen (which generates Rust bindings from C headers at build time).
Install via your Linux distribution
Several distributions ship packages that work out of the box.- Arch Linux
- Debian / Ubuntu (recent)
- Fedora Linux
- openSUSE
- Gentoo Linux
Install via rustup
If your distribution does not ship a suitable version, userustup:
bindgen requires libclang. Install LLVM from your distribution or build it following the instructions at llvm.org/docs/GettingStarted.html. You can also use the prebuilt LLVM+Rust toolchains hosted at kernel.org/pub/tools/llvm/rust/.
Verify the requirements
Once everything is installed, run the following to confirm the build system can find the toolchain:Kconfig and prints a diagnostic if any requirement is missing.
Enabling Rust support in the build
Open the kernel configuration menu and enable Rust support (CONFIG_RUST) under General setup. The option is only visible when make LLVM=1 rustavailable passes.
y) or as loadable modules (m).
Building with Rust enabled
Always build with a full LLVM toolchain when Rust is involved:The abstraction model
Rust kernel code is organized into two layers: bindings and abstractions.bindgen from C headers in include/. They live in rust/bindings/ and expose unsafe Rust declarations of C types and functions. For C inline functions or complex macros that bindgen cannot handle automatically, small wrapper functions can be added to rust/helpers/.
Abstractions live in rust/kernel/ and wrap the unsafe bindings behind a safe, idiomatic Rust API. For example, the Mutex abstraction wraps struct mutex from the C side and exposes methods that use Rust’s ownership model to enforce correct locking discipline. Leaf code such as drivers must use abstractions rather than calling bindings directly.
The kernel crate and the prelude
Thekernel crate is the central library for Rust kernel code. It re-exports common types and brings frequently used items into scope via the prelude. C FFI types such as c_int and c_char are available from the kernel prelude — do not use core::ffi aliases, as they may not map to the correct types for the target architecture.
cfg attribute to conditionalize on kernel configuration symbols:
Formatting and style
Rust code is formatted withrustfmt using the default settings (4-space indentation, idiomatic Rust style). Configure your editor to format on save. To reformat all Rust sources in the tree:
//) on the last item to prevent rustfmt from collapsing the list:
unsafe block must be preceded by a // SAFETY: comment that explains why the code is correct:
Documentation
Rust kernel code is documented withrustdoc using Markdown, not with the kernel-doc format used for C. The first line of a doc comment must be a single sentence summarizing the item. Unsafe functions must document their safety preconditions under a # Safety section.
rust.docs.kernel.org/6.10/).
Where Rust code lives in the tree
rust/
The core Rust support code: the
kernel crate (rust/kernel/), auto-generated bindings (rust/bindings/), C helper wrappers (rust/helpers/), and build infrastructure.samples/rust/
Sample kernel modules written in Rust. A good starting point for understanding how a minimal Rust module is structured.
drivers/
Production Rust drivers live alongside their C counterparts in the normal driver directories.
Kernel hacking → Rust hacking
Kconfig options under the Rust hacking menu provide debugging and instrumentation features specific to Rust kernel code.Setting up rust-analyzer
Therust-analyzer language server provides IDE features (completion, go-to-definition, inline errors) for Rust kernel code. Generate its configuration file with:
rust-project.json file that you can point your editor at.