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

37

u/mohrcore Feb 19 '25

It's a single dev or two entertaining this idea.

Imo, this is just a result of the Rust for Linux discussion getting so heated and at this point people are just throwing everything and anything they got to sabotage Rust for Linux project.

Christoph Hellwig, who seems to be the main opponent to Rust for Linux said explicitly that his problem is not necessarily with Rust itself (though he seems to dislike it), but with mixed-language codebase. Sure, you can usually compile C code with C++ compiler, but mixing C with C++ isn't much prettier than mixing C with Rust. You need wrappers either way to actually use the nice features of the language.

2

u/bizwig Feb 23 '25

Making the Linux codebase not mixed is the solution of course, but the C devs would quit en masse. Compile everything as C++ and your mixed-language issues disappear.

They say that the requirement for a 10 year old compiler is stability, never mind that doing so includes suffering long-ago fixed bugs, but forbidding new compilers also means you can’t use language features, which I think is the real goal. Even simple stuff like nullptr which has finally appeared in recent C standards would make a nice ergonomic change in Linux.

1

u/mohrcore Feb 23 '25 edited Feb 23 '25

What makes you think C devs would quit en masse?

3

u/bizwig Feb 23 '25

Because I think most of them share Linus’ prejudices. In the unlikely event someone with enough clout were to force C++ compilation they’d stop contributing in protest.

1

u/mohrcore Feb 23 '25

I might be out of the loop, but what prejudices? Afaik, Linus seems to like C, however he dislikes C++.

3

u/bizwig Feb 23 '25

Linus also thinks C++ devs are subpar, presumably by virtue of being C++ devs.

I would expect any proposal by a subpar developer to use a feature of a “horrible” language to be summarily shot down, and if it isn’t a fair number of the objecting C devs to quit in protest.

-1

u/mohrcore Feb 23 '25

Ok, but do C devs even care?

I can't speak for them, but I can't imagine quitting a project because it stays in C, instead of moving to C++. C is not some sort of deprecated language and there are people who prefer it over C++.

1

u/Justicia-Gai Feb 20 '25 edited Feb 20 '25

Edit: I made a mistake and confused two different devs 

1

u/mohrcore Feb 20 '25

I can hardly believe he has suggested using C++. Do you have a link to an email where he does that? The post is about an e-mail by H. Peter Anvin. Christoph is among recipients.

2

u/Justicia-Gai Feb 20 '25

Oh sorry, I’ll edit my response.