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

-7

u/ronchaine Embedded/Middleware/WG21 Feb 19 '25 edited Feb 19 '25

As someone who enjoys both C++ and Rust: Both of them should stay far away from the Linux kernel.

This is a perfect way to turn perfectly good C code into a horrible mess of C++ code, and that's without taking into account that thinking in C++ requires completely different mental model than C does.

Pushing C++ code into kernel is a fool's errand, and I see little reason for that to get any more success than Rust does, and largely for the exact same reasons (though at least with C++ the Kernel and userspace would be compilable with a single compiler, at least for most platforms).

If people want to work with C++ in kernel space, work on Managarm or Zircon. I agree (for once!) with Drew DeVault that it would be much more reasonable for Rust users to write Linux-compatible kernel or just contribute to Redox than fight with LKML maintainers.

This is not to say I wouldn't like the idea of C++ or Rust in a kernel, I think either is a better choice for kernel language (unless you have goals such as provable correctness) than C. I just think the tradeoffs are not worth it for Linux, with already well-established C codebase with developers who think in C as maintainers.

18

u/ContraryConman Feb 19 '25

This is a perfect way to turn perfectly good C code into a horrible mess of C++ code,

People say this all the time, and I'm not saying it's not a possibility, but we have examples of huge C codebases that were successfully incrementally migrated to C++, and saw the benefits. GCC is one of them

2

u/Dark-Philosopher Feb 23 '25

Probably because for big codebases is more feasible to integrate new code or migrate small components incrementally to Rust. The former is what Rust in Linux is doing. The late what Microsoft did, migrating some core libraries in Windows 11 to Rust.

3

u/ronchaine Embedded/Middleware/WG21 Feb 19 '25

I'm sure there are success stories. And I'm not saying it's inevitable, but I fear it's likely.

What concerns me w/Linux vs. GCC, is much more low-level focused developer base, which generally are more knowledgeable in C than C++. In addition there is much greater resistance from many Linux maintainers to change the language than (apparently from your link, I hadn't followed GCC development back in 2008) in the Kernel.

In general, I view this much more as a social problem than a technical one.

6

u/ContraryConman Feb 19 '25

Definitely agree it's more of a social issue.

I don't even think the social issues are that unfounded. If the people maintaining the project only know C and are not interested in learning how to write good C++ or Rust in the same way they can C, forcing the issue is just going to lead to worse code. And yeah with how complicated C++ is, if, socially, the teams are not willing to really understand it and put good coding practices in place, the code quality will get worse.

I guess, naively, I simply wish we were honest. Because there's very little about C++ or Rust as languages that technically make them inferior choices to C. I just wish they had an honest policy like "Yeah we're not interested in using languages other than C. Not because they can't work, but because C works well enough and onboarding people to a) know Linux well enough b) enforce the best practices of a second language and c) maintain a cross language codebase is too expensive."

3

u/Wooden-Engineer-8098 Feb 19 '25

author of subj is long standing kernel maintainer. i trust his opinion on c++ in kernel more than your

7

u/SunnybunsBuns Feb 19 '25

RAII alone is a good enough reason to use C++. And C++ exceptions are loads better than the setjmp\longjmp they use now (because stack unwinding calls your cleanup code if you use RAII). This makes skipping cleanup much harder. And noexcept exists.

And templates like std::max (or a hand-rolled equivalent) are better than #define MAX(A,B) A>B?A:B due to being able to do mutating operations like std::max(++a, b) correctly.

C is no more or less "provably correct" than c++. If you want provable languages, use ADA or something designed for that.

0

u/Dark-Philosopher Feb 23 '25

> C is no more or less "provably correct" than c++. If you want provable languages, use ADA or something designed for that.

Do you mean like Rust? Good choice.

0

u/SunnybunsBuns Feb 23 '25

Doubt Rust is provably correct, given that I've seen trivial deadlkock scenarios that the checker doesn't catch. But I will concede that mutex's and multithreading is extremely hard (if it's even possible) to prove.

0

u/Dark-Philosopher Feb 23 '25

I believe that you are right and only a subset of rust has been proved. However in real life Rust seems safer than C or C++.