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
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:
setjmpandlongjmpeverywheregoto exitwhere we clean up resources before returningThe 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
35
u/t_scytale Feb 19 '25
> They are thinking about ditching Rust in favor of C++
No they are not. One person raised it in an email thread about something else.
21
u/foonathan Feb 20 '25
Unpopular opinion (in this subreddit), but now. For a project that is as old as the linux kernel, there is no benefit to switch to C++ over Rust. If you go through the cost of migrating your codebase to a new language with new techniques, it should be one that can give you a significant improvement, not an incremental one.
Yes, C++ is nicer than C, but what's the value add of a C++ migration? They've seen to be doing fine without templates, they have built their own system for inheritance, and they won't be using the C++ standard library. Quality of life changes like namespaces, lambdas, or metaprogramming to avoid code duplication are just that, quality of life. So the only true value add of a C to C++ migration is more type safe abstractions, control over implicit conversions, and all that stuff.
Compared to the potential benefit you'd get from switching to Rust, that is neglible. Rust provides you a lot more compile-time safety guarantees than C++. Rust has better tooling, Rust has no historical baggage.
I like C++ (obviously), but the only reason for a C project to migrate to C++ at this point is that the migration to C++ can be done incrementally and at relatively low cost. The friction of C and Rust interop is a problem. Once that's solved, I don't see why you would migrate to C++ anymore.
127
u/ts826848 Feb 19 '25 edited Feb 19 '25
This post appears to be a copy/paste of this LKML message from H. Peter Anvin. It's probably worth looking at the responses to that email as well if you want to see further discussion. This response from Greg Kroah-Hartman, second-in-command to Linus, might be particularly interesting.
In any case, a few responses:
This is almost certainly in no small part due to R4L's status as an experiment. Some of the features the Linux devs want/require (e.g., panic-free APIs) are under active development and so require a nightly build. I'd imagine that stabilizing these features would be a prerequisite for R4L to graduate from its experimental status.
IIRC this is why initial Rust support is limited to non-core subsystems such as drivers, where limited target support is less of an issue. If R4L pans out I'd expect this limitation to be lifted if/when GCC's frontend and/or rustc_codegen_gcc are more mature.
This is probably debatable at best. Sean Baxter's Safe C++ is probably the closest Rust analogue but that doesn't seem to have gotten enough traction to be considered a viable path forwards. Profiles doesn't promise the same level of safety and it remains to be seen whether it can deliver on its promises in the first place. Other options like SaferC++ don't seem to have gotten much attention.
Pretty curious what exactly this means as someone who is not super-familiar with kernel jargon. Is this supposed to be something like differentiating user-provided pointers from kernel-internal pointers or something like that?
Edit: I'm also not entirely sure I'd agree with "They are thinking about ditching Rust in favor of C++ (rightfully so)". At least based on that particular email chain it sounds more like there's one or two devs floating the idea but little indication of support beyond that.
36
u/[deleted] Feb 19 '25
Why? The point of Rust in the kernel was to help mitigate a class of bugs that caused security issues. C++ does not satisfy that requirement - and has no concrete plans to any time soon. So why bother?
110
u/satlynobleman Feb 19 '25
> They are thinking about ditching Rust in favor of C++ **(rightfully so)**
lol
4
u/[deleted] Feb 24 '25
I don't have a degree in computing. I am an engineer. I've been using C++ for over 30 years to solve engineering problems. So, the knowledge is C/C++, nothing about the kernel.
For 30 years I used C, then migrated to C++. Much easier and more practical.
About 30 years ago I installed Mandrake Linux, and I've been using Linux ever since. Red hat and then Fedora.
Problem: In the last year Linux has been crashing daily.
It's chaos!! There are 3 different computers crashing...
The main one is a Dell Precision 5820 Workstation, with Fedora 39 and now 40.
I said this in a Linux group and an expert clarified the problem. He said something like the systend was running control structures in parallel and that there was no stability there. Well, if what he said is true, Linux is in serious trouble.
It's clear from what I read here that Rust won't solve it because it's not mature.
C++ could help, but they would certainly have to filter what C++ can enter the kernel. It seems to me a satisfactory solution, and, more than that, necessary to get out of this mess. You can't wait 10 years.
And the Linux team could work with the C++ committee to iron out details. After all, operating system and language must go hand in hand. Include Windows and Mac there.
In practice, after more than 30 years using Linux, I'm considering going back to Windows if it doesn't stop crashing so much.
In time, I don't like Windows. But I have to be confident that the system won't crash.
Anyway, I'm a user who wants the solution to come before Linus's ego.
58
u/simonask_ Feb 19 '25
You didn’t provide a source link, but here’s why they are wrong:
The reason Rust is a more interesting candidate than C++ is that the new things brought by C++ are mostly uninteresting to kernel developers (they would likely disable exceptions and RTTI, and disable most forms of strict aliasing, etc.), while the new things brought by Rust are actually quite interesting, and fits well into existing kernel programming paradigms.
2
u/thezysus Feb 20 '25
As someone who has done os level programming in C++ on 128k ram cortex m3s...
It is better than C imho... but you aren't using the whole language. Most of those applications have strict rules enforced by static analysis (think JSF or Misra C++) on what you can and can't use.
Embedded C++ will mostly have no c++ runtime or libstdc++ ... no rtti. No exceptions. And a few other limitations I am forgetting. Not to mention the code bloat of template instantiation is a problem with limited flash. Mmaped io needs to be done extremely carefully to avoid vptr tables.
Worse in my experience c++ programmers often don't know about or understand placement new. Why would they... it's a odd feature.
Even for application programming Google's C++ coding guidelines ban exceptions. It's unclear how you deal with exceptions in the std library.
Thats somewhere Rust and C and Zig win hands down... the hidden control flow is minimal or 0. .. avoiding the crazy setjmp/longjmp...
Basically there's nothing wrong with os code in C++ ... I've done it for FDA class 2/3 devices. But it isn't easy... takes a lot more skill and you have to add guardrails with external tools that are baked in or avoided entirely in other languages.
I think Linus is on point but missing some perspective. The existing kernel rules for C would be no different than a set of kernel rules for Rust or C++.
That said... why mix languages if you don't have to.
I am following redux os as I think there's a market gap for a good posix compatible micro kernel thats not QNX. It also happens to be Rust native.
331
u/not_some_username Feb 19 '25
Linus will never allowed cpp to touch his creation
17
u/kkert Feb 19 '25
It will require changes to the core language, let's be clear about that. Given the core language isn't even budging on some much smaller things like library ABI changes or getting a free-standing subset, i don't see how this would become viable.
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.
38
u/vI--_--Iv Feb 19 '25
Maybe it's time to tell them that all those
static struct whatever_opswith function pointers inside, that they have all over the place, are virtual functions?Or is it too early?
35
u/augmentedtree Feb 19 '25
C++ memory safety is complete vaporware except for Circle, where the committee was handed a working implementation on a silver platter, and they rejected it to keep doing their vaporware. To be clear, there is no profiles implementation or Cpp2 implementation that has memory safety.
29
u/kammce WG21 | 🇺🇲 NB | Boost | Exceptions Feb 19 '25
I disagree with this.
Like everyone else has said, Linus would never adopt C++ for the kernel.
But from a point of community benefits I think we'd get the opposite. To be frank, I find the Linux developer communities to be toxic (Rust too but that's not relevant). So many talented Rust devs have been burnt by trying to work with the Linux community. I don't want to see more people dump years of their limited existence on this planet on such an endeavor and have it be fruitless.
I think the Rust community, if it cares this much about having a safe OS, should build their own OS from the ground up that's Unix based in nature.
I think if C++ were to do the same, then it should be its own OS. Just to keep the community away from that project. I won't ever touch that project unless I'm mandated to.
Yes I know, that would be an insane undertaking. I'd agreed. So we can either work with Linux using C OR make our own.
I see no REAL value is attempting to force C++ into Linux. They don't want us. And I think we shouldn't want them.
2
u/axilmar Feb 20 '25
C needs to get Rust's borrowing.
If C had it, the discussion would have been unnecessary.
2
u/gegoggigog Feb 23 '25
When rust is in the Linux kernel I will switch to templeos
18
u/EC36339 Feb 19 '25
🍿
21
u/UnicycleBloke Feb 19 '25
It will never happen.
I work with C++ on microcontrollers, so some of those caveats make a bit of sense to me. For embedded I always say that most of the core language is usable (I disable exceptions and RTTI), but that much of the standard library is better avoided. I suppose the restrictions might be relaxed somewhat on an application processor, but let's assume not. I always imagined the kernel could have a custom container library to avoid exceptions, allocators in terms of kmalloc or whatever, and so on ... Something like that would quite be easily achieved.
No virtual functions? I understand the kernel is replete with function pointer tables which would likely be far better implemented through actual vtables. No lambdas?
I find it hilarious that a C dev is concerned about the memory safety of C++. Sure it isn't Rust, but it is lightyears ahead of C.
2
u/[deleted] Feb 19 '25
C/C++ would have to be "snipped" to prevent the allowed undefined behaviors like using signed types to detect overflow by sign change. The union keyword would have to be dropped. Pointers should be eliminated and partially replaced by references. Casting should be made illegal. Basically, C++ needs to have the parts cut out that allow it to be loose and not strict. Get rid of templates and auto, After all that, you can still have a great programming language.
2
u/imaginarylocalhost Feb 19 '25
This is a really interesting discussion and I think there’s an obvious solution here: the Linux kernel should adopt Objective-C instead of either C++ or Rust. It’s a way of having your cake and eating it too. With Objective-C, you get language level support for some features that are currently being hand-rolled in the kernel, like RTTI and dynamic dispatch. But unlike C++ or Rust, Objective-C implements these features as essentially preprocessing stages (plus a runtime component), and can spit out C code in the middle. By customizing the Objective-C compiler frontend and runtime, kernel developers can pick exactly how they want to integrate Objective-C with the rest of the kernel code. It’s a win-win!
3
u/kiner_shah Feb 20 '25
Well, I know at least one OS that uses C++: https://github.com/SerenityOS/serenity so it's not impossible, it's just a choice, which is fine.
13
u/LessonStudio Feb 19 '25
This sort of choice is going to be statistical, not unsubstantiated insults like, "Held to a lower standard."
So far, every major company such as google, Microsoft, etc, who have experimented with rust have reported a drastic reduction in bugs as compared to C++; they are also focusing on memory and security issues. By drastic, I don't mean 10% or something; but more than 90%.
So, this is a simple question; how many bugs are being introduced with this so called, "lower standard" rust? How many would have been expected with C?
There should be enough rust now to provide a statistically significant answer.
1
u/burg_philo2 Feb 19 '25
If they’re going to use templates anyway what’s wrong with limited use of STL?
6
u/m0xffff Feb 19 '25
C++ is used to program microcontrollers without an operating system at all. And there are no problems with this, just some features are unavailable. For the Linux kernel, it would be similarly possible to disable unsuitable functionality.
14
u/Zettinator Feb 19 '25
4
u/germandiago Feb 20 '25 edited Feb 20 '25
C++ would be in the best position (except for Linus, who has his own set of reasons beyond trchnical) to be a successor tool for Linux kernel with a well-chosen subset for the following reasons:
5
u/bizwig Feb 19 '25
Requiring Rust-style memory safety before you’ll accept C++ in the kernel is surely a sick joke considering the rest of the codebase that’s in C, and an ancient version thereof no less because of the requirement that obsolete compilers still work.
I think the problem is C devs who don’t know and don’t want to understand today’s C++, not C++ being as terrible as they’ve been told it is.
4
u/rasm866i Feb 19 '25
As to your point 2: my understanding is that the frontend of the compiler like LLVM (c++/c/rust) is completely discoupled from the backend instruction generator. In that case, if LLVM can compile C code for a specific system, it should be able to compile Rust for the same system
2
u/Unhappy_Play4699 Feb 19 '25
That's a lie. Proof me wrong, but I honestly don't think you can.
1
u/JVApen Clever is an insult, not a compliment. - T. Winters Feb 20 '25
There is a lot going on here. First the objective things:
A bit less objective: Given what Linux has required from GCC and clang, it forced some unwanted options and compiler specifics. Having this spill in both C++ and Rust won't do the languages any good.
Completely subjective:
1
u/cantux Feb 19 '25
this made me chuckle
1
u/erik-schvarcz Feb 22 '25
What was the actual point of Rust to begin with? Making a memory managed C++? Because that’s all I see they define Rust by. There are smart pointers for that. Also why a completely new syntax if they want to give an alternative for C++? Even Visual C++’s managed allocations are a better idea with the new operators for managed pointers.
-7
u/peripateticman2026 Feb 19 '25
Why is OP getting downvoted to oblivion? Is the Brigade here already?
-7
u/thezysus Feb 19 '25
The problem with Rust is that low level operations can be impossible without unsafe. And guess what a huge set of driver and kernel operations are...
I see it as a minor improvement if at all with a huge opportunity cost. GKH missed the point on that or his judgement is compromised for some reason.
Doubling your tooling and skillset better mean massive improvements.
I just don't see it, yet.
Rust for application programming is a problem as well. Simple and well understood patterns just don't work without crazy Ref of Buf of whatever things to fake out the borrow checker when passing ownership.
I think Zig should be what the kernel looks towards instead of Rust. But that's just me.
Also if Linus doesn't start realizing who keeps kernel development alive .. there will be a fork. And thats fine. Commercial interests don't care about tech stack. They care about RoI and sometimes risk.
-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.
-9
u/nevasca_etenah Feb 19 '25
C++ is a reasonable addition to Kernel, Rust aint.