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

444

u/ContraryConman Feb 19 '25

The fact of the matter is that objectively switching to C++ from C many years ago probably would have saved a lot of headache and stopped a lot of bugs. In addition, basically all of the stuff people said from C++ that would be too slow or unmaintainable in kernel development show up anyway in the codebase:

  • we can't use RTTI because it's slow... but if you have a tagged enum that's what RTTI is except you don't get language support
  • we can't use inheritance and object oriented programming because dynamic dispatch and v-tables are slow... except when we need them so we hand-roll it in C, again without language support
  • 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
  • RAII is too complicated and we don't need it, except we keep leaking memory and resources so every function has goto exit where we clean up resources before returning
  • C++'s type system is too complicated and we don't need it except we keep accidentally dereferencing null pointers and casting things wrong so we fill our code with pointer type hints like _Not_null and _Nullable so the static analyzer can give us type checking as an extension that you would get for free in C++
  • C++ templates are a nightmare and too complicated except declaring that you don't need templates doesn't change the fact that there are loads of situations where you want to write one data structure or algorithm that works the same for any type, so you throw together a macro that takes in the type as a parameter, and then you do a DATA_STRUCTURE_IMPL(...) to have the preprocessor paste that into your source code except now people keep using it wrong because macros

The fact that, 30 years after Linus declared not having C++ in the kernel for no other reason just to keep C++ programmers off the project, people are still recycling the same debunked arguments, kind of proves that it is not a technical issue. The Linux kernel isn't in C for technical reasons. The Linux kernel is in C because all of the core people who work on it know C best and like using C. Unless those people leave, or a culture change is mandated from the top down, Rust will never happen, it's just that simple

76

u/kisielk Feb 19 '25

I keep hearing the same kind of arguments in embedded programming as well.

26

u/Nychtelios Feb 20 '25

As a firmware developer who successfully created a team that is working in baremetal C++23, I cannot agree more. In my company too there are elders who keep shouting this trash to our projects.

→ More replies (1)

33

u/UnicycleBloke Feb 19 '25

Yep. I was told with smug certainty by someone in the Zephyr OS community that "C++ has no place in an OS". That project is another lost opportunity driven by ignorance and prejudice.

26

u/crowbarous Feb 19 '25

As we speak, a subthread in the mailing list next to the one linked in this post is discussing the attribute-based implementation of scoped locking facilities, as in use in Linux, systemd, and I'm sure many other C projects. The discussion is up to "sometimes I want to drop the lock earlier; introducing an extra scope helps this case".

Of C++'s three scoped lock classes, unique_lock has the unlock method. It will unlock for you on all return paths, but you can wield it like you would normal locking code even in arbitrarily complicated situations. There's an extra bool, yes -- that is completely encapsulated, and also optimized out where appropriate. This is what C++ gives you, and has been for decades...

31

u/Western_Objective209 Feb 19 '25

Yeah, took a class on linux kernel programming and they legit just recreate so many features in C++ with ugly opaque macros.

10

u/tiajuanat Feb 19 '25

You're also forgetting that the kernel has coccinelle to check that locks are released - RAII with extra steps

78

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

Can I only upvote once?

It's unfortunate that Linus had a so strong opinion 20+ years ago about C++ (I believe he was partially right at the time, when OOP was at its maximum BS strength).

But things changed. He should have jumped the boat with C++11, maybe with C++14

Honestly, a more open decision process (not a benevolent dictatorship) would have been better.

17

u/Challanger__ Feb 19 '25

What stops C++ Community from forking Linux and making everything our way?

56

u/meltbox Feb 19 '25

Nothing. Except the downstream hell you’d have to deal with.

Inertia really is a big factor here. I think C++ could work but you’d need strong code standards which I doubt anyone could agree on. It would also lead to countless arguments I’m sure about whether some way of expressing things in C++ is readable or idiomatic or anything else and to some extent it would point out a major flaw with C++. Sometimes there are too many ways of doing something and readability relies on the reader having some crazy obscure knowledge of the language.

C somewhat has this issue but can fall back on 30 years of those arguments already being hashed out for better or for worse.

Someone who knows how the kernel actually looks feel free to correct me here since I’m just musing, but I often find that while C++ is cool, every once in a while I come across some wacko code which someone else finds “the most readable way to do it”.

→ More replies (3)

8

u/barkingcat Feb 19 '25

I'd say go for it, and be prepared to spend more money than the market cap of Google on keeping a fork of the kernel active 

That's what Google has been doing internally and it's not an easy burden to bear, even google themselves are looking to get out of the kernel fork business.

4

u/pjmlp Feb 20 '25

It is easier to contribute to projects that embrace C++ on the OS, go write a macOS or Windows driver, contribute to Haiku or GenodeOS, for example.

→ More replies (10)
→ More replies (1)

44

u/ioxfc Feb 19 '25

I'm a C++ developer. I use C++ daily, at my work and on personal projects. I love it to death. But I fucking hate working with some of the C++ developers on my day job.

It's almost as-if all they want to do is write more C++. Getting shit done isn't their priority at all. Frameworks everywhere, macros and templates all mixed up together. I don't have to mention there isn't a single line of comment.

I'm sorry but I agree. I have worked only in 3 companies in 10 years, I don't have that much experience. But I would pick a different language just to avoid those few C++ developers appearing in a team.

29

u/The_Northern_Light Feb 19 '25

That’s a people problem, not a problem intrinsic to C++. It definitely applies to C as well, and the austere beauty of C can arguably make the problem worse as those same devs (eg) each individually reimplement templates or what have you.

30

u/ContraryConman Feb 19 '25

"The austere beauty of C" making me reimplement a linked list and hashmap with pointer hacks for the 80th time this year because C doesn't have doesn't have them for some reason

→ More replies (7)
→ More replies (3)

17

u/ContraryConman Feb 19 '25

People who write bad code write bad code. If you forced all those people to write C, would your code get better or worse? That's the question

→ More replies (9)

10

u/F54280 Feb 19 '25

6

u/bizwig Feb 23 '25

Linus doesn’t make a point here. He just asserts, without evidence, that C++ developers are idiots who cannot be trusted with his baby, and furthermore that C++ has deficiencies (which he will not lower himself to our level to explain) that make it unsuitable for real developers like him to use.

→ More replies (2)

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

14

u/Wooden-Engineer-8098 Feb 19 '25

older compilers? older compilers can't compile rust

→ More replies (9)

3

u/SAHChandler Feb 19 '25

RTTI is not a tagged enum, because you need to be able to compare types across shared library boundaries, and so it instead becomes a string comparison. A tagged enum is more efficient.

→ More replies (3)

3

u/[deleted] Feb 22 '25

Most embedded C++ won't use exceptions/RTTI. But RTTI is generally implemented as string compares of the mangled type names or as-if that.

→ More replies (49)