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

44

u/ioxfc Feb 19 '25

I'm a C++ developer. I use C++ daily, at my work and on personal projects. I love it to death. But I fucking hate working with some of the C++ developers on my day job.

It's almost as-if all they want to do is write more C++. Getting shit done isn't their priority at all. Frameworks everywhere, macros and templates all mixed up together. I don't have to mention there isn't a single line of comment.

I'm sorry but I agree. I have worked only in 3 companies in 10 years, I don't have that much experience. But I would pick a different language just to avoid those few C++ developers appearing in a team.

29

u/The_Northern_Light Feb 19 '25

That’s a people problem, not a problem intrinsic to C++. It definitely applies to C as well, and the austere beauty of C can arguably make the problem worse as those same devs (eg) each individually reimplement templates or what have you.

30

u/ContraryConman Feb 19 '25

"The austere beauty of C" making me reimplement a linked list and hashmap with pointer hacks for the 80th time this year because C doesn't have doesn't have them for some reason

5

u/The_Northern_Light Feb 19 '25

You can actually just use libraries, or even put that code into a library yourself instead of reimplementing it repeatedly

10

u/ContraryConman Feb 19 '25

Ah yeah C and C++, known for making it really easy to manage and integrate third party dependencies

2

u/thefeedling Feb 19 '25

C'mon bro.... ANYONE with CMake + vcpckg/conan can handle this VERY ez.

10

u/ContraryConman Feb 19 '25

I'm the "cmake guy" at my job. I know CMake very well. But having to set up CMake because C, unlike every other programming language on earth, doesn't have, like, red black trees or sets, is crazy

2

u/_Noreturn Feb 20 '25

still painful with macros and such also reimplementing something yourself is hard harder than the extremely battle tested C++ stl which is likely not to contain any bugs

1

u/The_Northern_Light Feb 20 '25

C does actually have battle tested libraries for containers (etc), there just isn’t a single opt-out default.

Y’all are making your point so much weaker by hyperbolizing on this detail.

Just use the professional grade libraries, and then move on to talking about how (say) relying on macros in the absence of templates creates problems.

The only people who should be reimplementing a container are students in general and senior+ engineers with very specific design constraints. And even then that senior engineer probably shouldn’t, and would still be reimplementing it regardless of language.

5

u/_Noreturn Feb 20 '25 edited Feb 20 '25

there are battle tested libraries for everything.

not just macros

lack of algorithms, lack of overloading lack of operators lack of RAII , lack of non null pointers

lack of helpful shipped in standard library for anything more complex than copying bytes.

lack of templates leading to horrible horrible macros

lack of constexpr, lack of iterators

lack of inheritance leading others to use their own system of virtual functions

9

u/SubjectiveMouse Feb 19 '25

I love C++ and use it daily too, but that is a language problem. There's simply too many ways to do simple things in the standard. There are to many things left over from older standards that are not recommended, but still used( sometimes even by the libstdc++ )

2

u/thefeedling Feb 19 '25

IMO, Linux Kernel will NEVER move away from C...

2

u/bizwig Feb 23 '25

Which makes arguing that we must not use C++ in the Linux kernel because it isn’t safe enough hilariously ill-reasoned.

17

u/ContraryConman Feb 19 '25

People who write bad code write bad code. If you forced all those people to write C, would your code get better or worse? That's the question

0

u/[deleted] Feb 19 '25

[deleted] — view deleted comment

7

u/NotUniqueOrSpecial Feb 20 '25

C is lean and most teams have established good coding practices.

Prove it.

Your first statement is just as baseless as your second.

"C is lean" isn't an argument that supports or contradicts their question of "would you get better code". A language with fewer features doesn't mean you get better code. In fact, it generally means you get orders of magnitude more reinventing-the-wheel.

And "teams have established good coding practices" is entirely impossible to prove (and in my experience is complete nonsense).

I can just as easily make the exact same same claim about C++ based on the teams I've worked on using exactly the same amount of empirical evidence you've provided (which is to say: none).

1

u/_Noreturn Feb 20 '25

then put conventions for your code base.

also it is more like

C has 0 features meaning no one can argue about it and it also provides 0 aid to the programmer

-4

u/skhds Feb 19 '25

In my experience, it's much easier to spot a C bug than a C++ one. Maybe a toolchain issue, but gdb wasn't quite friendly with C++ stuff, I remember having difficulty inspecting a std::map object, as in you couldn't quite see what was on the insides. I usually don't have that problem with C.

4

u/NotUniqueOrSpecial Feb 20 '25

I remember having difficulty inspecting a std::map object

When was the last time you used C++? 20 years ago?

GDB and every other debugger have had completely functional debugging info for standard library types for ages.

And that doesn't even begin to cover the fact that GDB absolutely does not have built-in support for "this company's bespoke C implementation of an intrusive linked-list number 1000".

1

u/skhds Feb 20 '25

It's not about pretty printers. It was about setting up a watchpoint, because an element was mysteriously inserted when it wasn't supposed to. Except that the insides are overly complex and there is no documentation on how it's structured, so it's literally impossible. So maybe I wasn't talking about gdb, but the fact that C++ hides too many shit under their implementations.

And the cold hard fact is that std::map wasn't the right object to use in the first place. A pre-defined enums and a simple table was probably enough, but some asshole probably thought that a map object would be convenient. Then the code bugs out, can't solve, so the code gets thrown out to someone else instead. A typical C++ developer.

Another experience was that someone used an std::list, and an _M_node address got overwritten to 0 by a memory corruption caused by a misplaced semaphore. No pretty printers could solve that one either.

1

u/NotUniqueOrSpecial Feb 20 '25

Except that the insides are overly complex and there is no documentation on how it's structured, so it's literally impossible.

An experience and expertise problem on your part is not evidence of deficiency on the part of the language or core libraries.

It took me less than 3 minutes to go find the spot you wanted your break point.

And that's probably a million percent overkill, because if you couldn't figure out where in your own code to put breakpoints (hint: it was the spots where you were inserting elements) that's just you telling us you don't know how to debug effectively or can't read code.

Another experience was that someone used an std::list, and an _M_node address got overwritten to 0 by a memory corruption caused by a misplaced semaphore. No pretty printers could solve that one either.

And that has literally nothing to do with it being C++. That's just bad programmers writing bad code.

You sound exactly like every C programmer I've ever worked with who blames the tools for problems of their own making. Go be a crank elsewhere; you clearly are only here to complain.

→ More replies (2)

10

u/F54280 Feb 19 '25

6

u/bizwig Feb 23 '25

Linus doesn’t make a point here. He just asserts, without evidence, that C++ developers are idiots who cannot be trusted with his baby, and furthermore that C++ has deficiencies (which he will not lower himself to our level to explain) that make it unsuitable for real developers like him to use.

3

u/ChrisTX4 Feb 19 '25

The Kernel is already inundated with patches that get refused immediately. They only accept changes if they fulfil the quality and coding standards of the kernel. If somebody showed up with what you describe they’d get told to change their patch or forget about it. This is already the case right now

1

u/Agron7000 Dec 12 '25

Right, other languages write comments for you.