MRMCD2024 Rust in the Linux Kernel - A Status Check

Explore the current state of Rust in the Linux kernel, including available drivers, key differences from userspace development, benefits, challenges, and integration methods.

Key takeaways
  • Rust integration in the Linux kernel is still limited, with only a few drivers available in mainline (e.g., R null block driver, some file drivers)

  • Key differences from userspace Rust development:

    • No standard library available, only core crate
    • Custom allocators required
    • No cargo build system
    • Different printing functions (print_k instead of println)
    • All C function calls must be wrapped in unsafe blocks
  • Benefits of Rust in kernel development:

    • Eliminates entire classes of bugs (buffer overflows, memory leaks, race conditions)
    • Clearer error handling through Result types
    • Generally requires less code (30% reduction observed in some cases)
    • Better type safety and ownership model
  • Current challenges:

    • Longer compilation times compared to C
    • Higher learning curve for kernel developers
    • Limited platform support
    • Social resistance from long-time kernel maintainers
    • Need to create custom bindings for C functions
  • Development process requires:

    • Enabling Rust support in kernel config
    • Creating bindings for C functions using bindgen
    • Implementing proper FFI (Foreign Function Interface) wrappers
    • Following kernel module conventions
    • Testing through QEMU or actual hardware
  • Integration with existing kernel code:

    • Uses bindings_helper.h for C function calls
    • Requires explicit handling of mutable/immutable parameters
    • Must conform to C calling conventions
    • Drivers can be written in either C or Rust, but not both simultaneously
  • Best use cases for Rust in kernel:

    • New drivers without existing C implementation
    • Code handling complex logic and user input
    • Security-critical components
    • Areas where memory safety is crucial