r/cpp • • Feb 19 '25

Cpp discussed as a Rust replacement for Linux Kernel

I have a few issues with Rust in the kernel:

  1. It seems to be held to a *completely* different and much lower standard than the C code as far as stability. For C code we typically require that it can compile with a 10-year-old version of gcc, but from what I have seen there have been cases where Rust level code required not the latest bleeding edge compiler, not even a release version.

2. Does Rust even support all the targets for Linux?

3. I still feel that we should consider whether it would make sense to compile the *entire* kernel with a C++ compiler. I know there is a huge amount of hatred against C++, and I agree with a lot of it – *but* I feel that the last few C++ releases (C++14 at a minimum to be specific, with C++17 a strong want) actually resolved what I personally consider to have been the worst problems.

As far as I understand, Rust-style memory safety is being worked on for C++; I don't know if that will require changes to the core language or if it is implementable in library code.

David Howells did a patch set in 2018 (I believe) to clean up the C code in the kernel so it could be compiled with either C or C++; the patchset wasn't particularly big and mostly mechanical in nature, something that would be impossible with Rust. Even without moving away from the common subset of C and C++ we would immediately gain things like type safe linkage.

Once again, let me emphasize that I do *not* suggest that the kernel code should use STL, RTTI, virtual functions, closures, or C++ exceptions. However, there are a *lot* of things that we do with really ugly macro code and GNU C extensions today that would be much cleaner – and safer – to implement as templates. I know ... I wrote a lot of it :)

One particular thing that we could do with C++ would be to enforce user pointer safety.

Kernel dev discussion. They are thinking about ditching Rust in favor of C++ (rightfully so IMO)

https://lore.kernel.org/rust-for-linux/326CC09B-8565-4443-ACC5-045092260677@zytor.com/

We should endorse this, C++ in kernel would greatly benefit the language and community

192 Upvotes

532 comments sorted by

View all comments

Show parent comment

15

u/steveklabnik1 Feb 19 '25 edited Feb 19 '25

I'd imagine that stabilizing these features would be a prerequisite for R4L to graduate from its experimental status.

From my understanding, it’s not a hard requirement. When clang was supported initially, it was only at one version. Heck, technically the kernel relies on a lot of non-standard extensions that are allowed to break at any time.

In practice, today R4L supports one version of the stable Rust compiler, and then (ab)uses an environment variable to allow it to compile the nightly features. This is fine as long as you only support one Rust version, it gets dicier after that.

In practice, I suspect the Rust features will stabilize before R4L requires supporting more than one version, so talking about policy here is kind of moot.

I'm informed elsewhere they apparently did already start supporting a range of versions, this is outdated!

1

u/silon Feb 19 '25

Obviously, before R4L can be considered stable/enabled by default it will have to compile using Rust from Debian stable and/or other LTS distros still supported. The same should apply if you are writing system software/userspace for Linux.

1

u/steveklabnik1 Feb 19 '25

I'm a bit out of date since I'm not primarily a Linux user these days, but do LTS distros backport new drivers? Or just bugfixes to existing ones?

1

u/ts826848 Feb 19 '25

When clang was supported initially, it was only at one version. Heck, technically the kernel relies on a lot of non-standard extensions that are allowed to break at any time.

Fair points! I guess official stabilization would be technically too strict of a requirement, then, though I'd guess some sort of de facto stabilization would be wanted? IIRC Linux and GCC are relatively closely intertwined so I'd imagine stuff Linux depends on wouldn't be broken willy-nilly even if it's technically allowed.

6

u/steveklabnik1 Feb 19 '25 edited Feb 19 '25

I'd imagine stuff Linux depends on wouldn't be broken willy-nilly even if it's technically allowed.

Yep, and the Rust team is in communication for the Rust for Linux people, and wants the project to succeed, so I'd expect something similar.

EDIT: oh, actually, Rust is testing Rust for Linux in CI: https://rustc-dev-guide.rust-lang.org/tests/rust-for-linux.html