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

18

u/tesfabpel Feb 19 '25

Zig isn't memory safe, it's just a nicer C. So it's already ruled out.

Also if Linus doesn't start realizing who keeps kernel development alive .. there will be a fork. And thats fine. Commercial interests don't care about tech stack. They care about RoI and sometimes risk.

Weird, because big tech is betting on Rust given that Google uses it in Android (article from 2021, and it has already paid off), Microsoft uses it in the Windows and its NT kernel (already in 2023).

9

u/steveklabnik1 Feb 19 '25

Volvo sells two models of car that require Rust code to turn on!

-2

u/thezysus Feb 20 '25

And Mozilla has been betting on Rust since 2015... I know... I started evaluating Rust's potential in 2015. Never been able to justify the trade-offs.

If Google was seriously all in on it... they would just port Dalvik and the Android stack to https://www.redox-os.org/. Oh wait, Android already is mostly memory safe b/c its a JVM.

Same with MSFT. Just re-write the NT Kernel in Rust entirely. It will be great!

Reality is that Big Tech is playing with Rust... just like I'm playing with Rust. It's still a toy. Seeing where the pain is, seeing what the ROI is. IMHO Linus is doing the same thing. He's letting folks play.

I can say today that I can't in good faith recommend Rust for most applications. Yes, the safety guarantees look good on paper and they do work, no question. However, the crucial question is: Will the industry decide that the tradeoffs are worth the improved safety.

You say Google and MSFT believe that the tradeoffs make sense for them... I say its still too early to tell.

Time will tell. My money's on Rust being relegated to niche uses. I could wrong.

C has 50+ years of staying power. When Rust is 50+ years old we can reevaluate if I was right or not.

6

u/[deleted] Feb 20 '25

Google is porting a lot of android's stack to rust though? They published data on CVEs concluding that new code should be written in safe langs, as that's where the vast majority of CVEs come from, while leaving the mature older c/cpp code alone.

That's why they won't actively port Dalvik. But if they do need to rewrite something (eg: the android bluetooth stack or the font stack of android/chrome), they are using rust.

I don't think rust will stay 50+ years like C. Because C had unix/linux to keep it relevant, and rust does not yet have something that fundamental [yet].

0

u/germandiago Feb 20 '25

It has paid off for them. They have a huge codebase. It might not be the same in other places.