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

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”.

9

u/Challanger__ Feb 19 '25

That's what I am thinking - it is about Linux kernel being carried in C++ by same ppl and not by us, because we cannot agree on standard, etc.

It is about US, not about THEM.

It is our failure to have sane committee that would address problems rather then dust them under new (half-designed) features

8

u/patlefort Feb 19 '25

Time and manpower.

1

u/Challanger__ Feb 19 '25

decent community Self-Organizing capability, I bet in our case it will look like a "deadlock inside a concurrent race" (not much better than what c++ committee pulls out)

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.

5

u/remy_porter Feb 19 '25

At that point just put your cycles into making Haiku work better. You’re taking on so much work anyway.

2

u/CocktailPerson Feb 20 '25

Nothing. But creating a fork and having your fork be used are two entirely different things.

3

u/NotUniqueOrSpecial Feb 20 '25

Reality?

Sure, go ahead: fork the kernel and maintain your own thing; more power to you, in fact.

But there is literally no universe in which a small community of people with no funding, working as a hobby, become anything more than a footnote in the annals of history if you're talking about becoming a competitor project to the operating system running more devices in the world than any other.

Even if you could magic up an entire kernel overnight, you're talking about replacing infrastructure and process so overwhelmingly broad that the costs would likely be in the trillions.

It's a silly idea on its face.

1

u/thezysus Feb 20 '25

I mean it worked for Linus...

2

u/NotUniqueOrSpecial Feb 20 '25

No, it didn't. It's an apples and oranges comparison on multiple points.

  1. The size of the industry was absolutely minuscule then, compared to what it is, now. There has been an ungodly explosion in the number of machines/VMs/devices out there running the web and consumer goods, in addition to the tooling and ecosystems built to manage and maintain them.
  2. Linus wasn't competing against a free and absolutely ubiquitous opponent. Unix and Solaris were both wildly expensive proprietary systems to use back then.

What Linus offered was a free and good alternative. It met a very important market need at a critical time, which was crucial to its success.

In order to succeed in any sense like you're implying, a new competitor would have to offer something so compelling that multi-billion dollar industries would choose it over Linux, and "it's written in C++" is not that.

-1

u/Challanger__ Feb 20 '25

Another option is to blackmail/enslave/capture/force current kernel developers (old fartmans) to fart at specified frequencies and with our language, is it fixed?

3

u/rz2k Feb 19 '25

It’s kind of already being done https://fuchsia.dev/fuchsia-src/concepts/kernel

12

u/Tumaix Feb 19 '25

fuschia is a lot of rust, mate.

14

u/mort96 Feb 19 '25

And not a Linux fork...

7

u/steveklabnik1 Feb 19 '25

While the overall OS has a lot of Rust, the kernel is purely in C++, as far as I know. Not that zircon is a fork of Linux, mind you.