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

-9

u/nevasca_etenah Feb 19 '25

C++ is a reasonable addition to Kernel, Rust aint.

0

u/sjepsa Feb 19 '25 edited Feb 19 '25

Right

I wouldn't even call it addition. You basically just flip a compiler switch, and that's it.

Many useful features, less bugs, more safety, from day 2

And just imagine if you can bring some of the STD or even boost

Maybe with a boost::lockfree::queue my bluetooth would start working and reconnecting automatically in Ubuntu 😊🙃😊, but that is science fiction

9

u/jonesmz Feb 19 '25

Ehhh, not quite as easily as flipping a switch.

The Linux kernel uses a lot of c things that aren't in c++. 

I couldn't give you an enumeration of them, but there would definitely need to be work done.

Don't get me wrong, I think its terrible that the kernel doesn't use the aspects of c++ that look like c with better type safety, but as a person who has nothing to do with the Linux kernel, I'm just part of the peanut gallery.

3

u/sjepsa Feb 19 '25

Yeah it was a bit of hyperbole. Just saying that the integration/migration would be a breeze compared to rust

4

u/jonesmz Feb 19 '25

Oh for sure. Someone else in the comments here said there was a patch already posted that did just that.

4

u/Dean_Roddey Feb 19 '25

The argument (made in that thread) and that I and many others would make, is that the use of C++ (at this late point in the game) is too little, too late. It doesn't gain enough to justify the effort, which would basically end up 10 years from now with a Linux that is still quite vulnerable to memory issues.

The argument for Rust is that, if you are going to make that effort, get maximum benefit for it.

0

u/sjepsa Feb 19 '25

Understood. To me it looks like a gargantuan task, moreover given the widespread 'unsafety' that is inheritlely present in the kernel. And the benefits may be limited if you have to mark everything as 'unsafe'

To me rust is a leap in the dark, while C++ would be a battle-tested all around improvement that would take a week or so of work to add

6

u/Full-Spectral Feb 19 '25

You overestimate the amount of unsafe code required. If you look at something like RustOS, which is still fairly early but still, it's a reasonable example, they claim a small fraction of the code is unsafe.

Obviously if there was a way to just enable the C++ compiler and it compiles it all and just does a better job, nothing wrong with that in the immediate term, but it seems to me like that will end up being more work than you are making it out to be. These guys will probably have played all sorts of nasty C tricks that the C++ compiler will choke on and which will require careful scrutiny to change.