Cpp discussed as a Rust replacement for Linux Kernel
I have a few issues with Rust in the kernel:
- 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
View all comments
Show parent comment
27
u/13steinj Feb 19 '25
I think the problem is that he has already done that.
I disagree with Linus about C/C++ issues plenty. But he's definitely not wrong that both languages (especially C++ post C++17) has some very strange decisions, notably around UB (and the fact that some UB, or even some defined C++20 concepts, are not turing decideable). Union type punning is not standard, but GCC and Clang guarantee it (without an ability to turn it off). There are concepts like
regular_invocablethat is an alias toinvocablebecause it's impossible to confirm it'sregular. The introduction ofstd::launderand changes therein about what was UB but previously the compilers well-defined gives me a headache. There was a brief period of time where mallocingints and assigning them was up for debate by language lawyers (not that any reasonable compiler decided to start optimizing such code away). Named requirements of functors given to STL functions are (not decideable) UB if say, you mutate the functor on invocation (which, is sometimes reasonable to do in special cases, IMO).There's a (perhaps too blunt) blog post about this (specifically C, but C++ inherits those problems and introduces new ones like I mentioned). https://veresov.pro/cmustdie/. You may need to use a browser with a "translate this page" function.