Skip to main content
โšก Calmops

Rust in Linux Kernel: From Experiment to Mainline

Introduction

For decades, the Linux kernel has been written primarily in Cโ€”a language that has powered critical software infrastructure worldwide but comes with inherent memory safety challenges. In 2025, Linux 7.0 marked a historic milestone: Rust officially became part of the Linux kernel, ending its experimental status and beginning a new era of memory-safe system programming.

This article explores the journey of Rust into the Linux kernel, what it means for the future of operating systems, and how developers can get involved in this groundbreaking development.


What Is Rust in the Linux Kernel?

The Basic Concept

Rust in the Linux kernel refers to the integration of the Rust programming language as a second officially supported language for writing kernel code. This allows developers to write memory-safe kernel modules, drivers, and other kernel components using Rust instead of (or alongside) C.

Key Terms

  • Memory Safety: The guarantee that a program’s memory accesses are valid, preventing bugs like buffer overflows, use-after-free, and null pointer dereferences
  • Kernel Module: A piece of code that can be loaded into the kernel to extend functionality (like device drivers)
  • Unsafe Rust: The portion of Rust code that bypasses memory safety checks (marked with unsafe keyword)
  • Bindings: Code that allows Rust to call C functions and vice versa

Why This Matters in 2025-2026

The inclusion of Rust in the Linux kernel represents a fundamental shift in how we build critical system software:

  1. Security: Memory safety vulnerabilities account for ~70% of all security bugs in C codebases
  2. Developer Experience: Modern tooling, package management, and compile-time checks
  3. Future-Proofing: A path for the kernel to adopt safer coding practices

The Journey: From Rejection to Mainline

Historical Context

2012 - Rust 1.0 released
2020 - First RFC proposing Rust in Linux kernel
2021 - Google announces supporting Rust in Android
2022 - Rust code merged in Android kernel
2023 - Initial Rust support merged in Linux 6.1
2025 - Linux 7.0 - Rust becomes official part of kernel

Key Milestones

Year Milestone
2020 First RFC by Miguel Ojeda
2021 Google announces Rust for Android
2022 Android kernel gets Rust support
2023 Linux 6.1 - Initial Rust support merged
2024 More Rust drivers merged
2025 Linux 7.0 - Rust officially part of kernel

Architecture and Design

How Rust Integrates with the Kernel

The integration follows a layered approach:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚      Rust Kernel Code          โ”‚
โ”‚   (Drivers, Modules, etc.)     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚   Rust Abstraction Layer (RAL) โ”‚
โ”‚   (Kernel-specific bindings)   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚    Linux Kernel C API          โ”‚
โ”‚    (ioctl, syscalls, etc.)     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚      Linux Kernel Core         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Core Components

1. Rust Bindings (rust/kernel)

  • Generated bindings to kernel C APIs
  • abstractions for kernel types
  • Helper functions and traits

2. Abstraction Layer

  • Provides Rust-friendly interfaces to kernel APIs
  • Wraps unsafe operations safely
  • Implements kernel-specific traits

3. Build System

  • Integrated with kernel’s Makefile
  • CONFIG_RUST option
  • Support for out-of-tree modules

Getting Started

Setting Up the Development Environment

# Install Rust toolchain
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup install nightly
rustup target add x86_64-unknown-linux-gnu

# Clone Linux kernel
git clone https://github.com/torvalds/linux.git
cd linux

# Configure with Rust support
make menuconfig
# Enable: Kernel hacking โ†’ Rust support

A Simple Kernel Module in Rust

// rust/samples/rust_minimal_module.rs

use kernel::prelude::*;

module! {
    type: RustMinimal,
    name: b"rust_minimal",
    author: b"Your Name",
    description: b"A minimal Rust kernel module",
    license: b"GPL",
}

struct RustMinimal;

impl kernel::Module for RustMinimal {
    fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
        pr_info!("Rust minimal module loaded!\n");
        Ok(RustMinimal)
    }
}

impl Drop for RustMinimal {
    fn drop(&mut self) {
        pr_info!("Rust minimal module unloaded!\n");
    }
}

Building the Module

# Build with make
make M=samples/rust MINIMAL=m

# Or use cargo-kernel
cargo-kernel build --module rust_minimal

Best Practices

1. Prefer Safe Rust

// โœ… Good: Use safe abstractions
fn process_data(data: &[u8]) -> Result<()> {
    for byte in data {
        // Safe iteration
    }
    Ok(())
}

// โš ๏ธ Avoid: Unnecessary unsafe
unsafe fn process_data_unsafe(data: *const u8) {
    // Only use when necessary
}

2. Use Kernel Abstractions

// โœ… Good: Use kernel types
use kernel::sync::Mutex;
use kernel::alloc::flags::*;

// โš ๏ธ Avoid: Direct C interop when not needed

3. Follow Kernel Coding Style

  • Match kernel’s C coding style in comments
  • Use kernel’s error handling patterns
  • Follow the module initialization/exit conventions

4. Handle Errors Properly

fn do_something() -> Result<i32> {
    let data = kernel::alloc::kmalloc::<u64>(1, GFP_KERNEL)?;
    // Use ? for error propagation
    Ok(data as i32)
}

Common Pitfalls

1. Mixing C and Rust Incorrectly

Wrong:

// Don't directly manipulate C structures
let ptr = some_c_struct as *mut CStruct;

Correct:

// Use generated bindings
let ptr = bindings::alloc_c_struct();

2. Forgetting Error Handling

Wrong:

fn init() -> Self {
    // Panics on failure - bad for kernel!
    allocate_memory().expect("Failed")
}

Correct:

fn init() -> Result<Self> {
    // Proper error handling
    allocate_memory()?;
    Ok(...)
}

3. Memory Leaks in Module Exit

Wrong:

impl Drop for MyModule {
    fn drop(&mut self) {
        // Forgot to free allocated memory
    }
}

Correct:

impl Drop for MyModule {
    fn drop(&mut self) {
        unsafe {
            kernel::alloc::kfree(self.ptr);
        }
    }
}

Example Projects

1. NVMe Driver (In Development)

A modern Rust-written NVMe driver demonstrating:

  • Async I/O operations
  • Hardware interaction
  • Error handling

2. Android Binder Driver

Google’s Rust implementation of the Binder IPC driver:

  • Security-critical code
  • High-performance communication
  • Memory-safe implementation

3. Virtual Filesystem (VFS) Abstraction

Experimental work on VFS components in Rust:

  • File operations
  • Mount handling
  • Path resolution

External Resources

Official Documentation

GitHub Repositories

Community

Tools


Conclusion

The integration of Rust into the Linux kernel marks a pivotal moment in systems programming. As memory safety becomes increasingly critical and the kernel evolves, Rust’s role will only grow. For developers, this opens exciting opportunities to work on critical infrastructure while enjoying modern language features.

Whether you’re contributing to kernel drivers, building embedded systems, or simply interested in the future of systems programming, understanding Rust in the Linux kernel is essential for 2025 and beyond.


Key Takeaways

  • Rust in Linux 7.0 marks official kernel support for memory-safe systems code
  • Memory safety is the primary driverโ€”~70% of kernel bugs are memory-related
  • The integration uses bindings and an abstraction layer, not a complete rewrite
  • Getting started requires nightly Rust and kernel configuration
  • Best practices emphasize safe Rust, proper error handling, and kernel conventions
  • The future points to more Rust drivers and potentially more kernel subsystems

Next Steps: Explore Building Desktop Apps with Tauri 2.0 to see Rust’s versatility in desktop application development.

Comments