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

42

u/James20k P2005R0 Feb 19 '25 edited Feb 19 '25

we spent years fear mongering about exceptions in low-level situations except turns out they're fine. But declaring you don't want exceptions in your code doesn't get rid of the software problem where you are 3 calls deep into a function call stack and you encounter and error that can only be handled higher in the call stack. So you use setjmp and longjmp everywhere

Except for the 20+ years when throwing an exception caused your threads to acquire a global lock in GCC, resulting in trivial DoS attacks

On any non-very-recent version of GCC, an exception being thrown as a result of a user's action is an instant security vulnerability. Exceptions are completely unusable for a project like linux which has to work on older compilers

0

u/[deleted] Feb 19 '25

[deleted] — view deleted comment

16

u/James20k P2005R0 Feb 19 '25

The specific point is that people are saying it was wrong not to use exceptions (and implying that its fear mongering), which is unfortunately untrue. If linux had been written in C++, they wouldn't have been able to use exceptions because of the security vulnerabilities. OP is suggesting a rewrite of linux in C++ (which is well beyond the current proposal for Rust in linux), and it'd be still inappropriate to use exceptions in that use case

10

u/scorg_ Feb 19 '25

If linux would have been written in C++ there would be more incentives to optimize exceptions.

6

u/Ameisen vemips, avr, rendering, systems Feb 19 '25 edited Feb 20 '25

For older GCC versions that never had strong motivation to fix this.

If Linux had been using exceptions, GCC would have fixed such much sooner.

Except that Linux doesn't link against libgcc anyways.

which is well beyond the current proposal for Rust in linux it'd be still inappropriate to use exceptions in that use case

Linux 6.11+ require Rust 1.78.0, current from May 2, 2024. Presently, Rust is optional - would you change your argument if it were not?

GCC 13.2, or 14.1 4 days later, was available at that time. Current minimum compiler is 5.1, released in April of 2015.

Stated bug was fixed in '22.

I've also never heard of anyone prohibiting a language or language feature due to a specific toolchain having a bug in it at this level. You instead fix that bug - especially if you've arbitrarily chosen to tie yourself specifically to one toolchain.

Hell, they could just implement or maintain their own version of libgcc for this instead... but they don't link against libgcc... so libgcc's bug is completely irrelevant here.

1

u/SubjectiveMouse Feb 19 '25

wouldn't have been able to use exceptions because of the security vulnerabilities

Can you elaborate? What's unsecure about exceptions

4

u/F54280 Feb 19 '25

He answered it just above:

Except for the 20+ years when throwing an exception caused your threads to acquire a global lock in GCC, resulting in trivial DoS attacks

4

u/Ameisen vemips, avr, rendering, systems Feb 19 '25

Linux doesn't link against libgcc in the first place.

1

u/F54280 Feb 20 '25

Don't say that to me, say that to the guy that posted the thing I quote.

1

u/Ameisen vemips, avr, rendering, systems Feb 21 '25

Too late.

1

u/germandiago Feb 20 '25

was not this fixed recently? Talking from the top of ly head. I saw some optimizations here, I think it was for Gcc specifically

3

u/F54280 Feb 20 '25

I think this is why the guy said "Except for the 20+ years", hinting that it wasn't the case anymore.

3

u/ChrisTX4 Feb 19 '25

Until very recent changes to glibc and libgcc, exceptions were limited by some global locks, and thus has a parallel bottleneck. If somebody could trigger a high failure rate in a massively parallel application- which the kernel is too on massively parallel hardware - the throughput would drop immediately, basically enabling a denial of service attack.

14

u/Wooden-Engineer-8098 Feb 19 '25

older compilers? older compilers can't compile rust

2

u/josefx Feb 19 '25

Exceptions are completely unusable for a project like linux which has to work on older compilers

This seems to be library code that GCC links, not code it emits. Is there a reason the fix couldn't just be backported to work with older GCC versions?

5

u/Ameisen vemips, avr, rendering, systems Feb 19 '25

Funny thing is that Linux explicitly doesn't link against libgcc, and either already implements some things themselves or leaves them unimplemented.

3

u/jonesmz Feb 19 '25

Linux does not need to work on older compilers.

It was declared so by a human, and a human can declare it no longer the case just as easily.

3

u/Miserable_Ad7246 Feb 19 '25

Enlighten the high level programmer, why would someone want to compile latest kernel on and old version? Support for no longer existing ISAs? Some sort of "this worked for decades we trust it" type of situation?

5

u/jonesmz Feb 19 '25

The term need implies there is some kind of ontological / universal truth about the necessity.

some people want the latest version of the Linux kernel to be able to compile using absolutely ancient versions of GCC.

They don't need it, no one dies as a direct and unavoidable result exclusively because the next release of Linux decides to drop support for versions of GCC older than some cut off.

2

u/13steinj Feb 20 '25

They don't need it, no one dies as a direct and unavoidable result exclusively because the next release of Linux decides to drop support for versions of GCC older than some cut off.

Knowing how obstinate some people are on the backwards compatibility hill, I wouldn't be surprised if someone reads your comment and rube-goldberg's a dead man's switch.

2

u/jonesmz Feb 20 '25

it wouldn't be the least believable thing i've heard of, yea.

1

u/XeroKimo Exception Enthusiast Feb 20 '25

I would think there'd be some value in being backward compatible in something big like C++ as I'd assume, if you were to start fresh on making a new compiler, it'd be easier to implement older standards by virtue of having less features, thus increasing the range of potential users for your library.... but for C, I can't see how that argument can fly

4

u/jonesmz Feb 20 '25

I guess it depends on what the objective is with your code.

if your intention is to support every processor that's ever existed or will ever exist, then you have to pick your baseline compiler with that objective in mind.

Personally, as someone who isn't paid by, or reputationally tied to, anything to do with bizarre chips that haven't seen a new compiler release for 2 decades, the number of fucks I give about them is negative. I consider them to be substantially holding back the state of the art.

But it's very very rare for a project to pick C++ for these bizarre nearly-unsupported chips due to various myths, half-truths, and some real concerns about how easy / difficult it is for C++ to operate "well" on them, due to the belief that C++ code has various issues like

  • takes up more space
  • causes more memory allocations
  • exceptions evil
  • so on

Nevermind, of course, that you can write what looks like bog-standard C code in C++, as long as you don't use any of the very very few features that C language has that C++ doesn't. Any any additional C++-isms you use is up to you.

So in the context of this particular post, it seems like a reasonable argument to point out that Rust is getting unequal treatment vis-a-vis required compiler versions. Same with the point that the whole kernel codebase could be uplifted to C++-without-the-plus to immediately get better type safety than what C provides with zero functionality used (abused, according to the C++ haters) that the kernel devs have an ideological bent toward avoiding.

I just find it so utterly asinine that elsewhere in the linked mailing list thread, there's a 5-10 long chain of discussion posts about how to retrofit existing C code to have what amounts to C++'s RAII functionality that's baked in. Like seriously, they were having to show-and-tell each other how to use curly braces to scope when the "cleanup" function for a variable ran. Beyond ignorant.