6 ms·
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?
- KingLancelot 3y agoI hear that Akashi uses it for Apple Mx graphics drivers, but I’m not aware of any widespread real world use.
- snakey 3y agoThey did! They wrote an excellent blog post about it too: https://asahilinux.org/2022/11/tales-of-the-m1-gpu/ https://asahilinux.org/2022/11/tales-of-the-m1-gpu/
- unrealhoang 3y agoYou can check out asahi linux GPU driver.
- gregkh 3y agoNo, 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 3y agoThis 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 https://en.wikipedia.org/wiki/Greg_Kroah-Hartman > broodbucket
- dharmab 3y agoAsahi'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/ https://asahilinux.org/2022/11/tales-of-the-m1-gpu/
- rob74 3y agoActually 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 3y agoWell 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.
- jorvi 3y agoNot “for the heck of it”, it brings significant benefits compared to rawdogging it in C++.
- rob74 3y agoWell no, actually they explain their reasons for doing it in the article others linked here (https://asahilinux.org/2022/11/tales-of-the-m1-gpu/ https://asahilinux.org/2022/11/tales-of-the-m1-gpu/, section "A new language for the Linux kernel"), and it sounds pretty convincing - if you start a basically new and very complex driver project, and if there is a language more modern than C that might help you and is officially supported by the kernel, then why not use it?
- broodbucket 3y agoThe 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.
- phdstudent 3y 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 3y agoThe 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.
- rob74 3y agoBasically, you have to review code generated by an LLM the same way that you would review code written by a human programmer, which requires (at least) the same level of proficiency as writing code yourself - if not, as you wrote, higher proficiency.
- raybb 3y agoHeres to hoping that one day maybe someone writes a Bluetooth driver in rust to improve the reliability a bit. But then again as I understand it the spec is so massive and has plenty of problems it may not be something that is improved by rust.
- sheepz 3y agoWhy do you think rewriting it in Rust would improve its reliability?
- sakex 3y agoSimply rewriting it in any language would improve reliability
- lelanthran 3y agoThe problems with Bluetooth have nothing to do with the programming language used to write the stack.
- sakex 3y agoMy comment is not about changing the language but about the fact that whenever we rewrite it, we improve the quality because of the lessons learned during past iterations.
- lelanthran 3y agoYou're correct; sorry. I dunno why I misread "write in any language" with "write in another language". I'm still skeptical about rewriting - the bluetooth spec is notoriously buggy itself, and many "bugs" and glitches in BT are due to how poorly the spec is written.
- TotempaaltJ 3y agoA rewrite might simply make it more resilient through changes in the base architecture. However, I know nothing about Linux's bluetooth stack and I assume that it's probably taking into account a lot of those glitches already.
- i_am_a_peasant 3y agoFunny 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-2e06-2b207b608485@usi.ch/ https://lore.kernel.org/rust-for-linux/bddea099-4468-4f96-2e... https://lore.kernel.org/rust-for-linux/c61a60ef-fa9d-44ea-9852-ae7b18bc1ab1@lunn.ch/ 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 3y 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-2e06-2b207b608485@usi.ch/ https://lore.kernel.org/rust-for-linux/bddea099-4468-4f96-2e... > https://lore.kernel.org/rust-for-linux/c61a60ef-fa9d-44ea-9852-ae7b18bc1ab1@lunn.ch/ 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.
- 2112syrinx 3y agohttps://www.linuxfoundation.org/webinars/rust-for-linux-writing-abstractions-and-drivers https://www.linuxfoundation.org/webinars/rust-for-linux-writ...
- ActorNightly 3y agoYou 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.
- rohit122 3y agohey
- gulshan212 3y agoWell. 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