Rust in the Linux Kernel

Suparna Ganguly
4 min readOct 22, 2021

Since the beginning, C has been the primary programming language when it comes to writing Linux kernel. Want to know the reason behind it? This is because C provides a particular level of control and predictability other programming languages cannot offer. And due to the best quality of code, memory issues are quite low in the Linux kernel. However, there still can be seen memory safety issues in the Linux kernel. Today a large number of enterprise businesses depend on Linux. So, even one memory safety issue is enough to create a huge mess.

That’s where Rust can help. Google seems to be quite supportive of this programming language. Rust has the potential to reduce bugs and security vulnerabilities. You may not convert the whole Linux kernel from C to Rust, but in new code blocks, Rust contributions can be included. Now Google is funding a project led by Miguel Ojeda to write elements in Linux kernel using Rust.

Ojeda told, “The project is not finished, but we are ready to get mainlined if high-level maintainers accept the current changes and prefer that we work inside the kernel”. For now, the target is to write Linux kernel components, such as drivers in Rust language, and possibly rewrite some riskier C code.

What is Rust?

Rust is a high-level, multi-paradigm, general-purpose programming language originally designed by Graydon Hoare of Mozilla Research. It was designed for performance enhancements and safety. Rust has a syntactic resemblance with C++. Rust guarantees memory safety via using a borrow checker for validating references. Rust achieves a high level of memory safety without garbage collection. Also, reference counting is optional.

Rust has achieved increasing use in industry. Also, the language has been voted as the “most loved programming language” by the Stack Overflow Developer Survey conducted every year since 2016.

Why You Should Use Rust

The main reasons for using Rust are:

Easy to Maintain

Rust language is statically typed. In Rust, all types are at the compile-time. Rust is also quite strong, so it’s difficult to write incorrect code using Rust. Generally, with the code grows the complexity. Statically typed languages are simple and allow you to keep track of your code.

In Rust, you don’t have to repeat variables multiple times, meaning long-term maintainability. Rust allows closures and nesting which improve the code maintainability.

Memory Management Capacity

Usually, system programming requires low-level memory control. With C’s memory management done manually, this job can become a nightmare. Rust offers convenience on low-level details. It doesn’t need a continuously running garbage collector in the background, hence, Rust has direct control over hardware and memory. When it comes to low-level code, coding in Rust feels like microcontroller programming. You can replace chunks of code without having risks of memory safety.

Also, Rust doesn’t allow to build the potential for buffer overflows. Hence, different points of hacking can be closed

Ease of Development

Rust is pretty fast and cross-platform solutions can be easily developed. You can work in both the front-end and the backend of an application using Rust. Rust web frameworks include Nickel, Rocket, and Actix. To start developing with Rust, just open Rustup. The Rustup is a version management tool and a toolchain installer, and then follow its instructions. You can also format your code with the code formatting automation tool Rustfmt. Another tool Clippy helps maintain an idiomatic code style.

What Linus Thinks

On July 10, 2020, Linus stated, “No, please make it a ‘is rust available’ automatic config option. The exact same way we already do the compiler versions and check for various availability of compiler flags at config time.” He wanted to make sure there was widespread testing. Torvalds wanted Rust to be enabled in the kernel by default. But he wasn’t against a requirement of Rust in the kernel.

In this year’s Linux Plumbers Conference, Linus and the core kernel maintainers welcomed the principle of supporting Linux kernel development in Rust. However, it’s going to take some time before that happens. Linus’ take is logical. He understands how many businesses and individuals are dependent on Linux all over the world. So, everything needs to be planned and done diligently.

Conclusion

Well, it seems like we can see Linux drivers to be written in Rust and accepted into the Linux kernel. Once those have proven their capabilities, Rust will keep going deeper into the kernel until it’s able to share a good piece of code with C. Can Rust replace C? It may not, completely. But in places where security is the key element, Rust might take over the default programming.

--

--

Suparna Ganguly

Freelance Tech Writer @openreplay @linuxhint @linuxjournal @shells.com, @doublemesh.com. Twitter: https://twitter.com/Suparna92300666