I see your point, but here's a counterpoint: Imagine I have a driver with a subtle out-by-one error on array indexing. It's entirely probable that this error will go unnoticed. While out of bounds array access is undefined, practically speaking, in most cases, it will just hit a page of memory that's already allocated, no harm will come, and everything will keep working. Even if the driver was to hit an unallocated page, it would cause a page-fault trap, and the buggy driver would be shut down. My webcam might die, but the rest of the machine would keep on operating and the situation could even be debugged/resolved.
That same driver written in Rust would have a totally different behaviour. An out of bounds access would trigger a kernel panic, which would kill the kernel and render the machine useless.
I don't honestly know enough about Rust to even guess at how this could be resolved, but I don't disagree with Linus's point. Minor errors causing panics is simply not an option in the kernel, even if it means that undefined behaviour can be avoid. Kernel writing is pragmatic concern, not a place for purity. Rust has to offer pragmatic purity to be useful in this environment.
A Rust panic isn't a kernel panic. It can, for example, be caught. It's possible in principle to call all driver code wrapped in a catch_unwind which will turn any driver panics into an error code for the kernel.
However, this may cause unacceptable performance overhead or API complications. It's also a disaster if a panic is called during another panic unwinding, that would cause the program to abort. Overall, returning errors is definitely the preferred approach.
Blindly catching panics would also cause completely unplanned program states, with no specific reason for why they could not yield even security vulnerabilities.
This is however not unprecedented in the kernel, and arguably the risk is low enough compared to the impact of a complete kernel panic, so for ex non-panicking oops are already used, but if you encounter that the only thing you should do is to try to save any current work and reboot as soon as possible. Caught Rust panics would be even less risky, but not completely without risk, at least not enough for merely returning an errno IMO.
While out of bounds array access is undefined, practically speaking, in most cases, it will just hit a page of memory that's already allocated, no harm will come, and everything will keep working.
Maybe, but the thing about undefined behavior is that it can have any result, including demons flying out of your nose, and more importantly including security vulnerabilities.
the buggy driver would be shut down.
Is that actually possible in Linux? It's not a microkernel.
That’s exactly how it works. I think you’ll find Linux is more advanced than you expect. Perhaps a time to go and write a real device driver and see how it works before trumpeting the virtues of rust.
I've written multiple Linux kernel drivers for a living, and there is in general no such thing as Linux catching kernel-space driver's undefined behaviors and shutting them down. Often "drivers" can and should be in userspace though, at least big parts of them. A microkernel would try to push too much in "userspace", like filesystems, but really there is no reason not to have e.g. a (basic) webcam driver in userspace. Maybe very fancy webcams could make the case for a kernel space driver to be a good idea, I don't know.
But yes, there are way too many kernel space drivers in Linux. At one point there was a project to ship Linux with its own dedicated userspace for some drivers (completely distinct from Linux distro userspace, where there is no absolute standard for even low level libraries, even less so if you consider Android), I wonder what it became.
The example I gave was much subtler (and more realistic) than “catching undefined behaviour”. It was specifically about a driver accessing an unallocated page (eg past the end of an array) and the ensuing page fault, which absolutely can be (and is) handled safely without killing the whole kernel.
18
u/ischickenafruit Apr 15 '21 edited Apr 15 '21
I see your point, but here's a counterpoint: Imagine I have a driver with a subtle out-by-one error on array indexing. It's entirely probable that this error will go unnoticed. While out of bounds array access is undefined, practically speaking, in most cases, it will just hit a page of memory that's already allocated, no harm will come, and everything will keep working. Even if the driver was to hit an unallocated page, it would cause a page-fault trap, and the buggy driver would be shut down. My webcam might die, but the rest of the machine would keep on operating and the situation could even be debugged/resolved.
That same driver written in Rust would have a totally different behaviour. An out of bounds access would trigger a kernel panic, which would kill the kernel and render the machine useless.
I don't honestly know enough about Rust to even guess at how this could be resolved, but I don't disagree with Linus's point. Minor errors causing panics is simply not an option in the kernel, even if it means that undefined behaviour can be avoid. Kernel writing is pragmatic concern, not a place for purity. Rust has to offer pragmatic purity to be useful in this environment.