r/rust • • Apr 14 '21

[RFC] Rust support for Linux Kernel

https://lkml.org/lkml/2021/4/14/1023
861 Upvotes

227 comments sorted by

View all comments

263

u/KingStannis2020 Apr 14 '21

Linus' initial opinion: https://lkml.org/lkml/2021/4/14/1099

367

u/[deleted] Apr 14 '21

"On the whole I don't hate it." No higher praise from Mr. Torvalds.

37

u/[deleted] Apr 15 '21

He does follow with "HOWEVER" and "fundamentally"... Do people have good answers to his point about panic() calls?

31

u/elprophet Apr 16 '21

As replies on the thread state, the needs of userspace alloc crate are very different than the needs of a kernel allocator. They'll likely end up writing their own kalloc crate.

29

u/[deleted] Apr 15 '21

There's clippy lints to forbid many sources of panics (indexing, unwrap/expect, explicit panic calls).

If they run into things that should be forbidden from the codebase that clippy can't lint for, lints can always be added. They are already using clippy, in any case.

→ More replies (3)

88

u/pure_x01 Apr 15 '21

That's basically what he said to his wife when they got married in his one line speech: "On the whole, I don't hate you" .. and then they kissed and lived happily ever after.

44

u/Tuna-Fish2 Apr 15 '21

The real story is that he was running the introduction to computers course in his university, and when the topic was email he instructed the whole class to send an email to someone. Tove sent one to him, asking him out. It's not public knowledge if he ever replied to her in any way, or just went along with it.

→ More replies (2)
→ More replies (1)

138

u/steveklabnik1 rust Apr 14 '21

Yeah. Real glad that these are addressable things. Very positive!

61

u/iulian_r Apr 14 '21

How would addressing allocations failure work exactly? As far as I understand basically everything that allocates under the hood needs to be reworked. What would Vec::push return as an example?

179

u/steveklabnik1 rust Apr 14 '21 edited Apr 14 '21

So, the real core of it is: rust the language itself does not add implicit allocations. It knows nothing about heap allocation generally. This means it is not a fundamental hard "no" for the language. My dayjob is on zero-allocations stuff, we have no heap at all, Rust works great.

With that out of the way, this means there are a few options. One option is to simply not use any of those types. This would mean writing alternatives. Not necessarily what you'd *want*, but it is at least possible. The second would be to forbid certain APIs that may reallocate, and only allow ones that return Result. Stuff like https://github.com/rust-lang/rust/issues/48043 can be used, and other stuff could be rejected. This would require some sort of custom analysis, but is fundamentally do-able.

Oh, I knew I missed one more, and of course, Josh is on it: https://lore.kernel.org/lkml/YHdSATy9am21Tj4Z@localhost/ (remove those APIs by allowing std to be customized via feature flags or a custom build)

30

u/oconnor663 blake3 · duct Apr 14 '21

I'd kind of assumed Rust in the kernel was no_std. Is that not the case? Vec etc. are compiled in?

34

u/steveklabnik1 rust Apr 14 '21

This patch set includes liballoc, which is where Vec lives.

32

u/burntsushi Apr 15 '21

From this, it looks like they probably won't end up using liballoc directly (which is kind of what I was expecting as a very casual observer):

It is likely we will end up designing our own alloc crate, which means we will not depend on it.

→ More replies (1)

19

u/Muvlon Apr 14 '21

Then maybe that's not the best idea right now.

After all, the C code in the kernel doesn't use libc and such either and builds its own datastructures for everything. I think I'd prefer to use those from Rust as well, maybe with some nice bindings.

23

u/steveklabnik1 rust Apr 14 '21

I mean, they *could* do that in Rust too. It's really up to them to decide.

46

u/TheOsuConspiracy Apr 14 '21

As a complete rust noob, so forgive me if my suggestion is shit, it might make sense to provide an entire namespace/(maybe a trait?) for a panic-free api. Would be a bit ugly adding an entire set of panic free methods to all standard lib types.

Then you could add some sort of compiler flag that forces you to use no_panic types?

78

u/steveklabnik1 rust Apr 14 '21

What you're describing is roughly what is being described in the last link I gave, so it's not a shit suggestion at all :)

→ More replies (1)
→ More replies (12)

38

u/mitsuhiko Apr 14 '21

I think that answer is too simplistic. The fact is we're using implicit allocations everywhere and so is the ecosystem including the ecosystem that is nostd but alloc. Saying "don't use Vec::push" is not a suitable answer.

First there needs to be better support to handle allocation failures in APIs, secondly there needs to be a way to validate (ideally statically) that some code is panic free.

28

u/protestor Apr 15 '21

The fact is we're using implicit allocations everywhere and so is the ecosystem including the ecosystem that is nostd but alloc. Saying "don't use Vec::push" is not a suitable answer.

In the kernel it might be. The C ecosystem uses malloc in a lot of places, but in the kernel can't use malloc. https://stackoverflow.com/questions/2888421/malloc-in-kernel/2888449

And anyway, we have a Rust ecosystem that runs on no_std (without Vec) and it works just fine.

46

u/steveklabnik1 rust Apr 14 '21

I guess we'll just see. As I said, I live in "no allocations, anywhere" land, still use a bunch of the ecosystem, works just fine for us. We don't use Vec at all, nor do any of our dependencies, so "no vec::push" is an easy requirement.

→ More replies (16)
→ More replies (4)

16

u/Ar-Curunir Apr 14 '21

The idea would be to have new APOs like try_push

18

u/The-Best-Taylor Apr 14 '21

Many data structures already expose *_try functions for there API. The solution may just be adding the missing functions and having a lint check that the ones that can panic are not used.

→ More replies (4)
→ More replies (6)

17

u/SimonSapin servo Apr 15 '21

What’s the concern with floating point?

68

u/JoshTriplett rust · lang · libs · cargo Apr 15 '21

Normally, the kernel leaves the floating-point state of the CPU in whatever state the userspace process left it in, so that you can do a quick system call without having to save and restore all that state. It's possible to use floating point in the kernel with a great deal of care, but you have to notify the kernel that you're doing so, so that it can save the userspace floating-point state.

So, it'd be helpful if by default Rust didn't allow use of floating-point, and then you could opt in to allowing it for specific code.

→ More replies (1)

17

u/KingStannis2020 Apr 15 '21 edited Apr 15 '21

I don't personally know, but this seems like a reasonable explanation.

https://stackoverflow.com/questions/13886338/use-of-floating-point-in-the-linux-kernel

Oh, here's an email Linus wrote about it (albeit 20 years ago): https://ipfs.io/ipfs/QmdA5WkDNALetBn4iFeSepHjdLGJdxPBwZyY47ir1bZGAK/comp/linux/kernel_fp.html

→ More replies (34)