3 ms·
> My experience is that you hardly ever need unsafe Rust code. I doubt that experience applies equally to kernel development.
by netheril96 5y ago
> My experience is that you hardly ever need unsafe Rust code.
I doubt that experience applies equally to kernel development.
- _flux 5y agoThey may apply better than one might think: the example SPI driver posted at https://lwn.net/Articles/863459/ https://lwn.net/Articles/863459/ only has two cases of unsafe. Of course, the kernel:: -modules it uses undoubtedly have more, but I imagine the idea is to provide abstractions that allow actual driver code not to use unsafe. And then there's stuff like file system code, that—I imagine—would require no more uses of unsafe than a database server written in Rust.
- tialaramex 5y agoBecause it's normal (indeed expected) to build safe abstractions on top of necessarily unsafe code, a lot of the kernel by volume shouldn't need to be unsafe at all. For example suppose that we're back in the late 1990s and so for some reason dozens of different yet roughly equivalent 100Mbps fast Ethernet PCI cards are available, each offering the same functionality but with slightly different register layouts, interrupts and quirks. We are of course writing drivers for them in Rust. Obviously at some fundamental level "configure PCI bus" isn't safe - if you get it wrong that'll be bad. But as the author of mediocre Ethernet driver #42 you aren't implementing "configure PCI bus" you're just calling a safe abstraction that worked for the other 41 drivers. Handling userspace calls to twiddle the Ethernet interface is potentially unsafe too, but likewise that will have been factored out into common code and you're just talking to a safe abstraction. Rust's type system and borrow checking makes it easier to write a safe abstraction for relatively low level ideas like "Only one device can own this MMIO register" or even "this can be 0x03 or 0x06 or even 0x18 but it can't be all zeroes, that's not a thing". This sort of stuff is boring and in principle already not particularly unsafe in C today, this sort of abstraction is already done - but of course in C you've no practical way to get the promises Rust offers for safe code.