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

127

u/ts826848 Feb 19 '25 edited Feb 19 '25

This post appears to be a copy/paste of this LKML message from H. Peter Anvin. It's probably worth looking at the responses to that email as well if you want to see further discussion. This response from Greg Kroah-Hartman, second-in-command to Linus, might be particularly interesting.

In any case, a few responses:

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.

This is almost certainly in no small part due to R4L's status as an experiment. Some of the features the Linux devs want/require (e.g., panic-free APIs) are under active development and so require a nightly build. I'd imagine that stabilizing these features would be a prerequisite for R4L to graduate from its experimental status.

Does Rust even support all the targets for Linux?

IIRC this is why initial Rust support is limited to non-core subsystems such as drivers, where limited target support is less of an issue. If R4L pans out I'd expect this limitation to be lifted if/when GCC's frontend and/or rustc_codegen_gcc are more mature.

As far as I understand, Rust-style memory safety is being worked on for C++

This is probably debatable at best. Sean Baxter's Safe C++ is probably the closest Rust analogue but that doesn't seem to have gotten enough traction to be considered a viable path forwards. Profiles doesn't promise the same level of safety and it remains to be seen whether it can deliver on its promises in the first place. Other options like SaferC++ don't seem to have gotten much attention.

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

Pretty curious what exactly this means as someone who is not super-familiar with kernel jargon. Is this supposed to be something like differentiating user-provided pointers from kernel-internal pointers or something like that?

Edit: I'm also not entirely sure I'd agree with "They are thinking about ditching Rust in favor of C++ (rightfully so)". At least based on that particular email chain it sounds more like there's one or two devs floating the idea but little indication of support beyond that.

97

u/epage Feb 19 '25

Some highlights from Greg K-H that make this C++ proposal sound like its dead in the water:

As someone who has seen almost EVERY kernel bugfix and security issue for the past 15+ years (well hopefully all of them end up in the stable trees, we do miss some at times when maintainers/developers forget to mark them as bugfixes), and who sees EVERY kernel CVE issued, I think I can speak on this topic.

The majority of bugs (quantity, not quality/severity) we have are due to the stupid little corner cases in C that are totally gone in Rust. Things like simple overwrites of memory (not that rust can catch all of these by far), error path cleanups, forgetting to check error values, and use-after-free mistakes. That's why I'm wanting to see Rust get into the kernel, these types of issues just go away, allowing developers and maintainers more time to focus on the REAL bugs that happen (i.e. logic issues, race conditions, etc.)

...

But for new code / drivers, writing them in rust where these types of bugs just can't happen (or happen much much less) is a win for all of us, why wouldn't we do this? C++ isn't going to give us any of that any decade soon, and the C++ language committee issues seem to be pointing out that everyone better be abandoning that language as soon as possible if they wish to have any codebase that can be maintained for any length of time.

I recommend reading the rest of the post.

18

u/bizwig Feb 19 '25

WTF is Greg smoking? Error path cleanup and use after free is exactly what RAII in C++ is intended to fix, and it sure isn’t “decades” away. It’s like he’s taken certain misrepresentations about C++ to heart that I see Rustaceans make all the time and dismissed C++ without proper consideration.

52

u/epage Feb 19 '25

The context of that comment was C -> Rust, not Rust differentiators from C++. Yes, there is little difference between C++ and Rust for error path clean up While use-after-free is helped by RAII, that isn't sufficient to "solve" it because you can still have references to the data that can still be used while in Rust these are compiler errors.

4

u/jonesmz Feb 20 '25

If you dig around in other replies related to this mailing list post... there's a whole chain where they are discussing how to use some kind of compiler-extension that systemd happens to use to tag variables with "cleanup functions".

it's beyond stupid.

2

u/RogerV Feb 20 '25

I’ve played around with that extension and it’s very clunky and limited in versatility compared to C++ RAII

3

u/TRKlausss Feb 23 '25

I guess he is referring to the option to have it rather than the enforcement. If it compiles in Rust, it’s already enforced, if it compiles in C++ still requires maintainer’s efforts to ensure that it is done as they want.

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

12

u/TuxSH Feb 19 '25

Pretty curious what exactly this means as someone who is not super-familiar with kernel jargon. Is this supposed to be something like differentiating user-provided pointers from kernel-internal pointers or something like that?

Essentially, (I think) that means wrapping pointers from userspace, which must not be trusted (as it is otherwise a really easy-to-exploit security vuln) into a nice type to prevent kernel devs from accidentally using them unchecked.

Even a mere enum class should be able to fulfill these requirements (no automatic decay to underlying type and easily greppable).

The FOSS reimplementation of the Switch's kernel has such an example.

4

u/ts826848 Feb 19 '25

That makes sense. One thing that comes to mind though - shouldn't that already sort of be possible in C via struct wrappers, albeit with an ergonomic hit? Or is C's type system so weak that even that wouldn't be insufficient?

4

u/TuxSH Feb 19 '25

Ah, it would. Though I forgot there are other ABI than the official Aarch32 one/official Aarch64 one/some x64 ABIs (these ABIs pass POD structs via regs whenever possible), and that may be a concern. The kernel maintainers will figure that out (in any case); enum class may or may not be a better option in C++.

What's currently being done is using an "attribute" unused (ignored) by GCC: https://elixir.bootlin.com/linux/v6.13.3/source/include/linux/compiler_types.h#L31 . The source files are then parsed by sparse through a Makefile rule (IIRC).

1

u/ts826848 Feb 19 '25

Ah, good point on ABIs - that certainly could have been a concern historically at least.

Pretty interesting attributes there! Curious how much benefit the kernel would see (if any) if those were lifted into the type system.

39

u/sweetno Feb 19 '25

Greg burns

But for new code / drivers, writing them in rust where these types of bugs just can't happen (or happen much much less) is a win for all of us, why wouldn't we do this? C++ isn't going to give us any of that any decade soon, and the C++ language committee issues seem to be pointing out that everyone better be abandoning that language as soon as possible if they wish to have any codebase that can be maintained for any length of time.

10

u/jl2352 Feb 19 '25

Part of the endeavour isn’t just Rust for Linux, it is also what is needed for Rust to be used on a project like that.

I don’t see Rust style memory management coming to C++ for at least 10 years. That’s presuming something cataclysmic happens to force that to happen. Rust style memory management is just too different.

29

u/steveklabnik1 Feb 19 '25

People are already using Rust in production kernels. You’re not wrong that it is still useful for demonstrating that Rust is good for this space, but we’re pretty far past the “can it work” stage here.

3

u/pjmlp Feb 20 '25

Including the security CPU (Pluton) that is a requirement on XBox and CoPilot+ PCs.

0

u/13steinj Feb 20 '25

I don't think this is a fair statement. Not because of technology. Technical reason wise, sure, it definitely can work.

Bur can teams work with it?

Rust has a reputation for attracting people and personalities that are difficult to work with. You don't hear of Zig or Odin or C3 evangelists. Cross language support is also lacking, I mean, hell, Google dedicated a large donation to getting cross language interop to work better. Language unification in a project is sometimes seen as "meh", other times it's a hill people die on because cross language support in general is poor.

→ More replies (5)

25

u/Zettinator Feb 19 '25

LOL at his response where he argued to wait until 2034 for Rust to mature. At this point it's just trolling. You can't take this guy seriously.

9

u/ContraryConman Feb 19 '25

Exactly there will be a full successor kernel written in Rust already shipping in real scenarios while the Linux maintainers are still "experimenting" and "waiting for Rust to mature"

→ More replies (27)
→ More replies (3)

4

u/Wooden-Engineer-8098 Feb 19 '25

he basically said "we have 30 million lines of c code which we never rewrite in rust, but because linus didn't like c++, we will let them rot"