Readit News logoReadit News
Posted by u/nequo 2 years ago
Ask HN: How is Rust used in the Linux kernel today?
Rust support was added to the Linux kernel almost a year ago, in Linux 6.1. Are there examples of Rust being used in the kernel today?
broodbucket · 2 years ago
The basic infrastructure for writing drivers in Rust is upstream but there's nothing upstream using it yet. Others have mentioned stuff that may eventually make its way upstream, but from what I've seen there's pretty heavy resistance from maintainers whenever (re)writing something in Rust gets brought up.

Before Rust starts getting used "for real" in the kernel there's a lot of barriers to overcome. Most maintainers aren't hugely proficient in Rust and absolutely do not have the time to learn. Needing yet another toolchain is annoying (especially when you need bits that aren't in stable yet), anything you write in Rust probably won't get built in distro kernels for a long time, and anything you work on today is hugely uncharted territory since it's all so new.

I think one thing a lot of people don't understand from "outside" the kernel development sphere is that standards for getting stuff upstream are typically pretty high for most subsystems, a lot higher than most open source projects. There are a lot of open questions about Rust in Linux that don't have clear answers, and Linux really struggles with consensus. Yes it has a dictator, but that dictator very rarely dictates anything that hugely moves the needle.

I do think "real" kernel bits written in Rust will get upstream and will get used, but it will be a very slow burn.

VancouverMan · 2 years ago
It might be helpful to consider the Firefox situation, too. That is also a relatively large, established, and actively-developed software system that began to include some Rust code later on in its existence.

If I'm not mistaken, the first stable Firefox releases including notable usage of Rust code came out in late 2017. So that's about 6 years of Rust in Firefox.

From a Firefox user's perspective, I can't say that I'm impressed by it.

Building Firefox from source is more involved than it was before. As you mentioned, additional tooling for Rust is required in such a situation.

I haven't really noticed any real Firefox feature, functionality, or performance improvements that I could directly attribute to the use of Rust.

It also doesn't seem like using Rust has allowed the Firefox developers to be significantly more efficient or productive than they were before.

Maybe an argument could be made that some security issues, for example, have been avoided thanks to Rust, but that's not really provable in any meaningful way.

After seeing the Firefox situation, my expectations for the use of Rust in the Linux kernel are pretty low.

cmrx64 · 2 years ago
> Maybe an argument could be made that some security issues, for example, have been avoided thanks to Rust, but that's not really provable in any meaningful way.

Google has some pretty good data on this: https://security.googleblog.com/2022/12/memory-safe-language...

As for Firefox, the CSS engine was rewritten to be parallel, among other improvements (new wasm compiler, new parallel compositor) that would have been challenging without Rust: https://wiki.mozilla.org/Oxidation

panick21_ · 2 years ago
Its a bit of strange argument that you as a users can't 'feel' it. The people picking Rust for more and more project inside of Mozilla probably know what they are more productive in (and will end up with less bugs). Just guessing at it from the outside seems like a really bad and unfair way to judge.

> but that's not really provable in any meaningful way.

Can you prove that Rust didn't make developers more productive?

Also, that there are less memory bugs in Rust then in C++ is just common sense and pretty generally accepted. It doesn't need to be proven again for every project.

> After seeing the Firefox situation

The situation where more and more code is written in Rust because most people prefer it?

lovasoa · 2 years ago
I remember seeing a blog post by Mozilla explaining how the new css engine, which is measurably faster, would never have been implemented in C++ because the engineers were not confident they could handle the tricky concurrency issues in that language.
pornel · 2 years ago
Project quantum saved Firefox.

Multi-core CSS engine and GPU-accelerated rendering in Firefox are in Rust. Without them Firefox was really behind in performance (if you're unimpressed now, imagine it being way worse).

Deleted Comment

phdstudent · 2 years ago
> Most maintainers aren't hugely proficient in Rust and absolutely do not have the time to learn.

Can LLMs help here with code (re)writing?

sundarurfriend · 2 years ago
The proficiency required to make sure the LLM is not screwing up the code in some subtle way, is probably even higher than the proficiency required to rewrite the code yourself. So probably not.
speed_spread · 2 years ago
LLMs rewriting what? A) Rust modules to C so that it can be reviewed and merged by old school kernel hackers? B) C to unsafe Rust so that you lose any advantage of using Rust and get the worst of both world? C) Or rewrite the kernel maintainer's brains while they sleep so they become magically proficient in Rust when they wake up?

If you're talking about option A) or B), we already have things like mrustc and c2rust. These are though problems, LLMs aren't _that_ smart yet.

broodbucket · 2 years ago
Others have spoken to the technical merits but I can say straight up that the community would absolutely despise this idea, even if the generated code was excellent. LLM generated code would go over about as well as implementing a blockchain in the kernel.
cpach · 2 years ago
I’m just a layman when it comes to these kind of things. But here’s my take anyway: AFAICT, the issue isn’t so much about rewriting the code. However, if we would use automation for that, we would probably need proofs that the new code is better than the old one. And creating suchs proofs seems incredibly hard as soon as we walk out of “hello world” territory. People has spent years on trying to make suchs systems, but it’s still not widely used. So until then, we need people who read and analyze the code, and also have wast knowledge of the kernel’s inner workings, and today that seems to be the bottleneck. Those people doesn’t really grow on trees.
dharmab · 2 years ago
Asahi's M1 GPU driver is the most notable example, although it is still alpha-quality and has not yet been upstreamed. https://asahilinux.org/2022/11/tales-of-the-m1-gpu/
rob74 · 2 years ago
Actually this would be a very good first candidate for a Rust module - it's a significant newly developed piece of software, so nobody can say that they "(re)wrote it in Rust" just for the heck of it, and its impact is relatively isolated (only used on Apple Silicon Macs), so if something did break, it would mostly only hurt Asahi Linux, which is currently experimental anyway.
saagarjha · 2 years ago
Well it's less that they rewrote it in Rust for the heck of it and more that they wrote it in Rust for the heck of it. Seems like it's going OK for them, though.
rowanG077 · 2 years ago
I wouldn't say it's alpha quality at this point. It's running "in-production" on all Asahi Linux laptops.
rob74 · 2 years ago
...which is currently available as an alpha release. I wonder if there's a connection here ;)
tourmalinetaco · 2 years ago
Yeah, I’d say Hector Martin’s got at least the most complete example of Rust code that can go into the kernel. Though he’d be far better off working alongside actual distro maintainers as he focuses on the M1 driver, rather than bandaid-fixing his distro because he can’t handle GRUB.
gregkh · 2 years ago
No, the only rust code accepted into any released kernels is basic framework infrastructure so that someday, maybe, in the future, real functionality could be written in rust.

There are many out-of-tree examples of rust kernel code, but as of right now, none have been merged.

sah2ed · 2 years ago
This reply is from another (major) Linux kernel developer.

For those not in the know, gregkh = Greg Kroah-Hartman

https://en.wikipedia.org/wiki/Greg_Kroah-Hartman

> broodbucket

ActorNightly · 2 years ago
You can write arbitrary kernel modules in Rust, I think there are a few floating around.

Core kernel will never have Rust in it, and that is a correct decision. Linux and C have a long history of just working, and there is value in making sure that C code you write is correct and explicitly thinking about memory and what gets modified where. Correct coding is more than just memory safety, and compilers can't check everything for you.

i_am_a_peasant · 2 years ago
Funny you ask, I was wondering the same thing last night so I went through the rust-for-linux mailing list archive. This thread is pretty telling:

https://lore.kernel.org/rust-for-linux/bddea099-4468-4f96-2e...

https://lore.kernel.org/rust-for-linux/c61a60ef-fa9d-44ea-98...

The above thread is really strange that they picked such a niche thing in networking to contribute to, as sockets don't have many uses within the kernel itself other than NFS or dhcp or stuff like that. They should have aimed to add things to the QoS layer, qdisc, classifier, whatever, there's tens of those and you can easily add another. Start small.

The problem really is that the people who want to see Rust in the linux kernel have next to no linux kernel background and have a poor understanding of how Linux works and how things are generally done.

Also the people who contribute big things to the linux kernel usually have a clear business case behind them and a big company paying them to work on it full time.

For example I could imagine Microsoft eventually contributing in this regard, say they want some more Hyper-V virtual hardware drivers in Linux and they decided to do that in Rust. They've been lately using Rust in the Windows kernel so it's not that unrealistic to think it may happen.

A filesystem written in Rust would be cool... but it's probably going to have to be a new one rather than a rewrite, I can't imagine people going through the trouble of rewriting btrfs in Rust. And big names like Facebook could actually back that effort.

I really love using Rust for middleware things, however right now I can't convince myself to use it on low level things like OSes or microcontrollers since it seems to be a lot of trouble with getting Rust to play nice with FreeRTOS for example, and there seems to be no production ready rtos written in Rust either.

Aurornis · 2 years ago
> Funny you ask, I was wondering the same thing last night so I went through the rust-for-linux mailing list archive. This thread is pretty telling:

> https://lore.kernel.org/rust-for-linux/bddea099-4468-4f96-2e...

> https://lore.kernel.org/rust-for-linux/c61a60ef-fa9d-44ea-98...

I don’t see how either post supports your points. The first is about OOT vs in-tree modules and the second isn’t about anything specific to Rust. You just picked a random post from someone learning about kernel development who happened to be using Rust.

The people doing core Rust for Linux work (not random mailing list participants) are actually very qualified and very knowledgeable about Linux. I don’t think your characterization of them is fair at all.

Grabbing random mailing list posts from non core maintainers and trying to extrapolate conclusions about an entire multi-year is not a good way to evaluate a project.

i_am_a_peasant · 2 years ago
>The people doing core Rust for Linux work (not random mailing list

>participants) are actually very qualified and very knowledgeable about

> Linux. I don’t think your characterization of them is fair at all.

Alright, they are very qualified and knowledgeable, what is a fair characterization of the work they're doing?

It's been a year, let's see how much stuff they managed to merge, My terminal window has 135 rows:

cd linux/

git log --oneline rust/

output <space> output <space> output <space> no output

Oh wow, how many people was it again?

git shortlog -s -n -e --all --no-merges rust/ | wc -l

29

Most of those guys have single digits number of commits and there are 7 guys with double digits number of commits so let's say there's about 7 people most active doing rust in linux.

Do you know Rust yourself? Take a look at the bulk of the patches that got merged, could you write that if someone paid you? My guess is yes, because it looks pretty average to me. (No offense to you personally).

patrick451 · 2 years ago
I don't have a dog in this fight, but " someone learning about kernel development who happened to be using Rust" sounds to me like anecdotal evidence for the assertion that most rust contributors don't know how kernel development works.
zare_st · 2 years ago
The biggest point is in your second paragraph.

"The problem really is that the people who want to see Rust in the linux kernel have next to no linux kernel background and have a poor understanding of how Linux works and how things are generally done."

Is this a non topic then?

Having drivers in Rust is one thing. Drivers were done in C++ before. But you use a subset because a language targeting kernel needs to use kernel facilities. You won't be able to use your standard library. Syntax needs to be resolved to what kernel provides, or dropped.

That's why C++ in kernel never took off that seriously, when you restrict it, and when you already have to accommodate for kernel's "API mindset", what's left of language in between isn't that important.

i_am_a_peasant · 2 years ago
If C could get constexpr and templates (with concepts) I would be very happy.

Everything else in C++ is just syntactic sugar to me. Very useful syntactic sugar, but just sugar.

Alright placement new is quite nice too. Since there are so many ways to allocate memory in the linux kernel.

As far as RAII goes, ehhhh I don't know. KASAN and kmemleak do a pretty ok job.

unrealhoang · 2 years ago
You can check out asahi linux GPU driver.
gulshan212 · 2 years ago
Well. integration of rust into linux kernel is still in experimental stage however there are only few things that about the use of rust in linux kernel such as progress experiment, C to rust transioning, community discussions and many other. Thanks