Why Rust Is Coming to the Linux Kernel: Greg KH on Bindings, Bugs, and Change

Open on YouTube ↗
Overview

In this podcast excerpt, Linux kernel developer Greg KH is asked whether Linux might ever support Rust, or any language besides C. Rust is already in the kernel, Greg explains, and the harder question is how it fits alongside an enormous existing C codebase. Their position is that Rust is not a silver bullet. Still, they argue it should make drivers easier to write, it would eliminate a large class of common bugs, and the kernel has reached a tipping point where new Rust code will start appearing.

9 min read

Rust Is Already There, Mostly as Bindings

Greg answers the opening question by noting that the kernel already contains about 25,000 lines of Rust. Most of that code is bindings rather than real functionality. One visible exception is a feature from the latest release: when the kernel crashes, it can display a QR code that you photograph to retrieve the crash dump. That QR code generator is written in Rust.

Greg describes the history this way. The Rust for Linux developers had been working on the effort for a long time. A couple of years ago they came to the kernel maintainers and said they thought they were ready and asked whether the kernel wanted it. Greg says the maintainers agreed to try it as an experiment, since the Rust developers were willing to do the work.

Why Drivers Are Harder Than Core Code

Greg makes a point that may seem backwards: it would be easier to write a core piece of Linux in Rust than to write a driver. Drivers draw on everything else in the kernel. They deal with blocking, input and output, the driver model, USB ports, and more. Drivers can be very small precisely because they borrow resources from the rest of the kernel.

In Rust, each of those connections needs a binding, an intermediate layer between the C code and the Rust code. Greg explains that the C kernel has very opinionated ideas about how it handles objects and memory, including its own memory model. Rust has equally opinionated ideas about the same things. Getting the two to mesh is tough, and Greg calls the resulting binding code "the most crazy complex Rust code you've ever seen." As a self-described new Rust developer, Greg says they can barely read the bindings but trust the people writing them.

So a binding has to be written for each part of the kernel before Rust drivers can use it. The QR generator was simple because it was a single function. Over the past couple of years, people have been writing bindings and building example drivers, including a new disk driver implemented in both C and Rust for comparison.

Performance Gaps and the Rust Toolchain

Those comparisons showed that Rust code still has some performance issues relative to C, according to Greg, because kernel developers can use tricks in C that Rust can't do yet. Greg describes this as a tooling problem that the Rust developers are working on. Some of the core developers of the Rust language are also Linux kernel developers, Greg notes, and they have always wanted Rust to work for Linux.

What "Memory Safety" Actually Means at the Kernel Level

Greg is careful to separate Rust's memory safety from any claim that Rust code can't crash the kernel. At the kernel level, memory safety does not stop you from overwriting things. In Greg's framing, it means the memory you pass around is clearly owned or not owned, and things get cleaned up properly when they go out of scope.

Greg says they have seen every kernel bug for the past 18 years and estimates that half of them would be fixed by Rust. Those are the "stupid one-off bugs": overwriting an array by one, forgetting to clean up an error path, forgetting to unlock a lock. Logic bugs remain possible in Rust, and the interviewer and Greg agree those will always exist.

The QR code feature itself serves as the cautionary example. The C code passed the Rust code a pointer to a buffer along with the buffer size. The Rust code never checked the size and wrote right over memory. Greg's conclusion is that you can write memory-unsafe code in Rust and crash things with it. Memory safety here is about the safety of object life cycles, not the removal of all bugs.

The Case for Rust in Drivers

Greg still believes Rust needs to come in, mainly because it should make drivers easier to write. Devices are dynamic and can be yanked out at any time. Handling the lifetime rules and reference counting correctly is very tricky. Greg says there are parts of the kernel that still don't get this right, and the developers know it.

Greg also points to a side effect: Rust is forcing the kernel to document its C code better and to clean it up. Greg has had to clean up code in the driver core and found they could do things better and safer in C to make Rust easier. Greg says these improvements would remain even "if Rust disappeared tomorrow." The effort is making developers rethink a lot of existing kernel code.

Resistance, and Being Wrong on Both Sides

Greg acknowledges that many core kernel developers resist this. Some dislike change, and some dislike multiple languages in one project. One core developer said they didn't like working on a multi-language project because it's tricky. Greg says those developers are free to feel that way and that the Rust effort isn't stepping on anybody's toes. A lot of the conflict, in Greg's view, comes from miscommunication and from people.

Greg shares a personal example. Many years ago Greg wrote the kernel's driver core, which governs how drivers work in the system, and Rust needed a binding for it. When Greg saw the proposed binding code, their reaction was that it was horrible, wouldn't work, and was miserable. So Greg met with the developers at a Rust Linux conference, where Greg says the developers seem to have given a whole presentation just for them.

The outcome, as Greg tells it, was that both sides were wrong. The Rust developers had written about a thousand lines of Rust to do what Greg did in two lines of C. When Greg asked why, they said they hadn't wanted to change the C code. Greg told them the C code could change. Greg had only written it that way because it was easy in C, and changing it would eliminate a thousand lines of Rust. Greg's lesson is that progress came from understanding each other's problems and working together.

Real Drivers Are Starting to Appear

As a result, Greg says, the kernel now has bindings good enough to write some drivers. Red Hat developers are starting to write the new Nvidia GPU drivers in Rust and are putting proposals out. The GPU drivers for Apple MacBooks are also written in Rust. Greg notes those patches are not merged but are written and approved on a fork, and says they work great. Graphics drivers have many complex object life cycle issues, and Greg says Rust makes those much easier to handle.

Greg also expects many "simple stupid drivers" for hardware devices to be written in Rust. Many drivers only need to read and write some memory bits, which Greg says is easy in Rust and can take less code than in C. With the infrastructure now in place, Greg believes the kernel has hit the tipping point and will start seeing new Rust code.

Government Mandates and Linux's Future

Greg ties the shift partly to outside pressure, pointing to government mandates against using memory-unsafe languages like C in products. Because Greg wants Linux to succeed, they say the kernel will have to change. Going forward, Greg's stance is that if you want to write in Rust, you can.

Protecting the 40 Million Lines of C

Greg stresses that the kernel still has 40 million lines of C, and says nobody is going to rewrite it in Rust ("Don't worry about that"). Very good developers are working on reducing the problems in the C code. Greg mentions that the kernel now has bounds checking, along with what they call "seat belts and airbags" that protect C code from doing stupid things. Kernel developers are also working with compiler authors to add new C extensions that make C code safer, because the goal is to protect the code that exists today.

Greg supports this approach with a Google publication. As Greg describes it, Google wrote its new code in Rust over the past couple of years, left the old code alone, and became overwhelmingly more secure. Greg's explanation is that bugs decay over time: older code still has bugs, but most bugs appear in new code.

An Experiment That Can Still Be Reversed

When the interviewer says Greg seems excited about Rust, Greg frames it as evolution: see what happens, and if it fails tomorrow, the kernel can rip it out. What matters to Greg is that developers are willing to do the work and it doesn't intrude on other people's code. The interviewer links this to Linux's "show the work" culture: Rust got in and is progressing because people keep demonstrating that it works and solves their problems.

Greg closes with the question of other languages. People ask why not Zig or Hare. Greg says those are good languages, but nobody has proposed them. Greg adds that the developers behind those languages probably don't care about Linux, which Greg considers fine, since they don't have to.