The Joy Robot
Home | Content | Services | About
Search Eng Dark

TJR-News News, Operating Systems 2022-12-12

Linux 6.1 arrives with initial Rust support: the kernel accepts its second language after C

This Sunday, December 11, Linus Torvalds released version 6.1 of the Linux kernel. Among its new features is one that marks a before and after in the project's history: initial support for the Rust programming language.

Since Torvalds released the first version of Linux in 1991, the kernel has been written almost exclusively in C, with some parts in assembly. Accepting a second language for writing kernel code is a change many believed would never come.



Linux 6.1 is the first kernel version with Rust support. Illustration: The Joy Robot.



Why Rust?

C is a very efficient language, close to the hardware, but it leaves all memory management in the programmer's hands. One slip, such as reading past the end of an array, using a pointer to memory that has already been freed or freeing it twice, can cause crashes or, worse, security vulnerabilities. Microsoft and Google have reported that about 70% of the serious vulnerabilities in their products are caused precisely by this kind of memory error.

Rust offers the performance of C, but with memory safety guaranteed by the compiler, without needing a garbage collector. Its "ownership" and borrowing system checks at compile time that the code doesn't have these errors. If the code isn't safe, it simply doesn't compile.



What does Linux 6.1 include?

It's important not to overstate it: what arrives in Linux 6.1 is only the minimal infrastructure needed to compile Rust code inside the kernel. There are no real drivers written in Rust yet, nor parts of the kernel rewritten in this language. It includes:



  • Support in the build system (Kbuild) for compiling Rust code, enabled with the CONFIG_RUST option.
  • The "bindings", generated automatically with bindgen, which let Rust use functions and structures written in C.
  • A first "kernel" crate with basic safe abstractions: memory management, errors, message printing and strings.
  • An example module, rust_minimal, showing how to write a kernel module in Rust.
  • Documentation to get started.

Support is optional: anyone who builds the kernel without enabling it won't notice any difference. Future versions will add more abstractions and, eventually, the first drivers in Rust.



The layers that connect Rust code with the kernel written in C. Diagram: The Joy Robot.



A kernel module in Rust

This is the example module included in Linux 6.1, located at samples/rust/rust_minimal.rs. When loaded, it prints a message to the kernel log and stores three numbers in a vector. When unloaded, it prints those numbers:



Language: Plain text

 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 | }

There are interesting details. For example, instead of push, it uses try_push: inside the kernel, running out of memory can't bring the whole system down, so every allocation returns a result that must be handled, and the ? operator propagates the error if something fails. Also, the vector's memory is freed automatically when the module is unloaded, without calling kfree.



How to try it

To build the kernel with Rust support you need the LLVM/Clang toolchain, the rustc compiler version 1.62.0, the Rust standard library source and bindgen. From the Linux 6.1 source directory, we can pin the required Rust version and check that everything is ready with these commands:



Terminal
$ rustup override set $(scripts/min-tool-version.sh rustc)
$ rustup component add rust-src
$ cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen
$ make LLVM=1 rustavailable

If everything is correct, all that's left is to enable the Rust support option in the General setup menu of the kernel configuration (for example, with make LLVM=1 menuconfig), enable the Rust samples in the kernel's sample code section and build with make LLVM=1.



The Rust for Linux project

Behind this milestone is the Rust for Linux project, led by Spanish developer Miguel Ojeda, which has been working on this integration since 2020 with support from companies such as Google, Microsoft and Arm. Over these years, the proposal went through many reviews and discussions on the kernel mailing list, known for how demanding it is. Torvalds agreeing to merge it shows the trust the language has earned.



Our take

Those of us who have programmed in C, especially on microcontrollers, know how easy it is to make a memory error that doesn't show up until the worst possible moment. That's why it seems a very logical step for the Linux kernel, the foundation of millions of servers, Android phones and embedded devices, to start relying on a language that eliminates an entire category of errors by design.

That said, let's not expect Linux to be rewritten in Rust: there are more than 30 million lines of C code that will remain for many years. The idea is that new code, especially drivers, can be written in Rust. And that, in the long run, can make the kernel considerably more secure. If you're interested in systems programming, this is a great time to start learning Rust.




The Joy Robot