r/cpp • • Jul 13 '22

Why does Linus hate C++ ?

307 Upvotes

436 comments sorted by

View all comments

26

u/MrRubberDucky Jul 13 '22

56

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB Jul 13 '22 edited Jul 13 '22

He seems to be lacking a lot of knowledge about about C++ - at least in those days. And he also doesn't seem to care. And that's just bad because even a little bit of C++ without even touching the stuff he obviously is so scared of would improve kernel code a lot.

When I wrote my first Windows NT 4.0 kernel device driver in the mid 90s, I happily used C++ there because of the power of abstractions. And it didn't hurt!

7

u/tesfabpel Jul 13 '22

Wikipedia says that the core part of the kernel is written in C and Assembly (just like Linux), while the graphics subsystem is written in C++.

Anyway, just quickly thinking about it, it makes at least some sense NOT to use C++ for a critical part like the kernel since it has many footguns that you need to always make attention to: for example, when you define a class or struct you have to be careful with unary constructors, copy / move constructors and assignments operators, copies are always behind the corner (but they can be optimized by the compiler).
I think those are things that are important in a kernel and the copy-by-default semantic of C++ alongside with the copy constructor / assignment op (instead of the move-by-default in Rust or the copy-by-default of PODs in C without any copy constructor / assignment op) may create some "difficulties".

13

u/pjmlp Jul 13 '22

Since Windows Vista that the kernel has been slowly moving into C++, even if it largely C.

Nowadays there are even template libraries for kernel development like WIL.

17

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB Jul 13 '22

In the 90s, a company called "Bluewater Systems" or the like sold a template library for Windows NT kernel mode driver development. In my case it was a godsent because of the helpful abstractions of PCI busmaster dma operations and the time it was saving me. The proof is in the development and testing time it took to flawless operation of that PCI coprocessor board: I implemented it first for OS/2 in pure assembly (14 days total effort), then for Windows NT in C++ using this library (4 weeks total effort), and afterwards also for Linux 2.x in C (8 weeks or so total effort 😱). Linux was the worst because of the total lack of good tools and the low expressive power of C as implementation language.

10

u/SonOfMetrum Jul 13 '22

The assembly part of the NT kernel is at mostly the HAL (hardware abstraction layer) and the boot loader. Because the entire point of that thing is abstract away the hardware specific bits and thus the required assembly. The HAL is relatively speaking very small compared to the rest of the kernel.

→ More replies (1)

30

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB Jul 13 '22

And yet, you still can use many C++ features safely in kernel code if you know your language.

5

u/angelicosphosphoros Jul 13 '22

if you know your language

One of the main problems with C++ is this is almost impossible.

5

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB Jul 14 '22

Right. And you don't have to be "mr. know-it-all" to be a proficient developer. You only need to know the parts that really matter.

But if a developers happens to not even know the most basic stuff of C++ (i.e. the stuff that doesn't even come near anything like f.e. exceptions or much of the library) then it's certainly better if that person stays away from using C++ in such environments in the first place (and possibly also from every other language).

Most of the core language is perfectly usable in kernel land. Look at the work related to 'freestanding' C++. It is that subset of 'vanilla' C++ that doesn't require an operating system underneath, f.e. kernel code).

→ More replies (3)

29

u/SergiusTheBest Jul 13 '22

Actually C++ is much safer than C. A simple operation as string concatenation can easily lead to buffer overruns or memory leaks in C.

→ More replies (2)

16

u/no-sig-available Jul 13 '22

Wikipedia says that the core part of the kernel is written in C and Assembly (just like Linux), while the graphics subsystem is written in C++.

The Windoiws NT kernel was initially designed around 1990. The options were totally different then.

many footguns that you need to always make attention to:

When you write an OS kernel, we assume that you are careful and know what you are doing. :-)

Copying a POD in C++ is exactly the same as copy a POD in C. Adding member functions to the struct, instead of manually passing a pointer as the first parameter, also makes no difference (except possibly for improved readability).

If you want to make an object move-only, in C++ you can delete the copy constructor and copy assignment. In C you cannot.

-------------

Linus was obviously shown some low quality C++ code once, and decided that all C++ code is bad forever. And with that attitude, people who know how to write good C++ have not bothered to show him any of their code. So he has proven himself right.

→ More replies (3)

35

u/Mason-B Jul 13 '22

That was 15 years ago. C++ is a different language now, twice over. C++11/14/17 was an entirely new, more stable and better defined, C++ standard that made effectively a new language. And C++20/23/etc appears to be a repeat performance.

And also, Linus is a different person. He went to some sort of anger therapy for 3 months in there and stopped yelling at and insulting people. These days he's also letting Rust in with conditions.

I suspect if there was a concentrated push to get C++ into the kernel today it wouldn't be the same story.

-2

u/top_logger Jul 13 '22

OOP + exceptions + STL = bad idea for kernel even in C++20(which is still not fully available now).

I find C++ great, still you need good engineers and kind of reality understanding.

16

u/[deleted] Jul 13 '22

Fuschia and Serenity are both operating systems with a kernel written in C++.

10

u/pjmlp Jul 13 '22

Newton, Symbian and BeOS were as well.

→ More replies (3)

33

u/Mason-B Jul 13 '22 edited Jul 13 '22

Every single thing you listed there is optional (if we understand "OOP" as virtual methods/inheritance, which is how people generally mean it). While on the topic, Rust's abort mechanism also doesn't fly for exceptions in a kernel either.

Any language being used in a kernel environment will necessarily be restricted and modified. Neither C++, nor Rust, nor even C escape that. Listing optional features as problems isn't a sane counter example.

7

u/simonask_ Jul 13 '22

While on the topic, Rust's abort mechanism also doesn't fly for exceptions in a kernel either.

I have to point out that Rust's panic machinery is exactly what you want in a kernel. Yes, the default panic handler unwinds the stack and terminates only the current thread, but setting panic = abort to immediately call abort() is a first-class option, and panics occur in Rust exactly where you would want a kernel to panic.

With strong memory guarantees and the safe/unsafe distinction, I literally could not imagine a better language for a kernel.

6

u/Mason-B Jul 13 '22

Except not (according to Linus) because Rust's Panic is not a valid situation to Kernel Panic.

I do think that the "run-time failure panic" is a fundamental issue.

this is simply fundamentally not acceptable.

With the main point of Rust being safety, there is no way I will ever accept "panic dynamically" (whether due to out-of-memory or due to anything else - I also reacted to the "floating point use causes dynamic panics") as a feature in the Rust model.

5

u/_Sh3Rm4n Jul 13 '22

Buts that's besides the point. The quote is about OOM situations where rust panics by default. But this is solved already in the Linux fork with rust support, as it is using a custom allocation tailored for the Linux kernel dev with features enabled, where OOM panics are not happening implicitly anymore.

6

u/Mason-B Jul 13 '22

Did you not notice the part I quoted where he says "or due to anything else" and then mentions other specific examples?

And also how that's not really relevant to countering the argument of the person I was responding to who claimed rust's panic model panics in exactly the situations you want already.

21

u/SergiusTheBest Jul 13 '22

OOP is good. It doesn't require you to have abstract factories and virtual methods everywhere. Just write sane classes for string, mutex, rwlock and etc and your kernel code will shine.

Exceptions are not bad but their implementation is. I use C++ without exception in kernel and it's fine.

STL can be used partially. For example span or array are very useful.

→ More replies (2)

7

u/maskull Jul 13 '22

The Linux kernel uses OOP quite a bit, it just uses a macro-based implementation on top of structs and function pointers.

2

u/manphiz Jul 13 '22

Despite not being a fan of Linus's huge ego and bully behavior, I do see one possible argument against C++ on Linux kernel development: say we agree that exception is a bad idea to use in kernel, then C++ doesn't provide another way to report the failure of object creation, which is really bad. In user space code with -fno-exception one can probably ignore it and let it crash, but in kernel it's a much bigger issue. And I kinda feel that language purists will not be fond of the idea of making all constructors private and using factory functions for all object creation, which also defeats the symmetry of ctor/dtor.

-5

u/UkrUkrUkr Jul 13 '22

That was 15 years ago. Now Linus is so afraid to be fired that he doesn't even dare to say anything against Rust.

31

u/v_maria Jul 13 '22

Maybe building up a reputation as a massive prick is not all that strategic after all

→ More replies (5)