by Karellen · 4y ago · 0 comments
And not because he thinks Rust might be able to solve actual problems in kernel development?
I want whatever drugs you're taking.
Memory safety is one hell of a benefit. In contrast, if you look at Linux' coding style, C++ really doesn't have any benefit over C.
Not to mention, many of C++'s advantages come from the STL, which wouldn't work without extra work in kernelspace. Rust has been designed for running on bare metal from day 1.
That aside, the kernel does do polymorphism the hard way (with unions and arrays of function pointers) in a few places; the example I saw was some filesystem code, I think. C++ is also much more type-safe than C, mostly by refusing to implicitly convert between unrelated pointer/reference types and preventing you from inadvertently doing something absurd. References also make null checks unnecessary and convey to the caller that they're supposed to pass something valid, and RAII simplifies a lot of resource management. Templates also reduce boilerplate and are type-safe, although they're also a harder sell and not everyone's cup of tea.
I agree that memory safety is a huge advantage (although on the other hand, a double linked list in Rust is currently impossible without unsafe blocks), but I refuse to believe this decision has nothing to do with stubbornness.
That hasn't really stopped anyone who wants to write their out of tree drivers in C++.
> That aside, the kernel does do polymorphism the hard way (with unions and arrays of function pointers)
C++ virtual functions are needlessly inefficient for interface classes where you only have a handful instances for each implementation (most commonly only one). Support for something like structs of function pointers with a little bit of sugar so you don't need to explicitly pass the this pointer would be very velcome even outside kernel space.
Using C++ classes has some disadvantages compared to C-style polymorphism - especially related to memory layout and allocation - in C++, memory is allocated by the code instantiating the object, not by the object's code, so you have to know the size of the type (including private fields) in any place where you are instantiating it. Not sure how relevant this is for kernel code.
> C++ is also much more type-safe than C, mostly by refusing to implicitly convert between unrelated pointer/reference types and preventing you from inadvertently doing something absurd
This part I agree with.
> References also make null checks unnecessary and convey to the caller that they're supposed to pass something valid
I think the distinction between references and pointers is one of C++'s worse ideas. You can't generally replace pointers with references either.
> RAII simplifies a lot of resource management
RAII doesn't work without bringing in a whole lot of other heavy-duty features: mainly exceptions, but also constructors and destructors.
> Templates also reduce boilerplate and are type-safe
Templates are more type-safe than macros, but not that much more (since the template language is essentially a dynamically-typed program, even though its output is a C++ AST that will be further type-checked). Like RAII, they also require you to use other features of C++, especially operator overloading, and likely also copy (and more recently, move) constructors.
> I agree that memory safety is a huge advantage (although on the other hand, a double linked list in Rust is currently impossible without unsafe blocks), but I refuse to believe this decision has nothing to do with stubbornness.
I do agree that stubbornness may well be a component. However, I believe that there is a coherent argument that can be made that supporting C++ for kernel modules is not worth it, while supporting Rust for kernel modules is. I'm not saying that this is the argument that Linus is making, but I do believe it can be made.
Especially since we also need to remember that all kernel modules are part of the kernel source tree. Supporting an additional language has a significant cost for everyone who is maintaining the kernel, since they need to add tooling to handle that extra language for all targeted platforms.
It should also be remembered that C++ compilers are even worse than C compilers at mis-using instances of undefined behavior to mis-optimize code, especially since they typically rely even more on optimization steps to have half-decent performance.
(Actually, no, strike that, I don't want to sleep forever.)