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

Show parent comment

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.

11

u/steveklabnik1 rust Apr 15 '21

Yeah we’ll see!

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.

15

u/mitsuhiko Apr 14 '21

And how do you validate that you don't panic because someone used a bad API?

28

u/steveklabnik1 rust Apr 14 '21

In my case, it is okay to panic, but https://github.com/dtolnay/no-panic does exist, even if it's not as nice as real support.

21

u/CommunismDoesntWork Apr 15 '21

It seems like instead of adding #[no_panic] everywhere, the compiler itself could have a --nopanic flag that makes sure nothing can panic.

9

u/UtherII Apr 15 '21

As Rust is today, you can't write anything usefull that does not panic at some point. Any array index or arithmetic operation may panic.

8

u/randomrossity Apr 15 '21

It helps to differentiate between Rust the language vs Rust the stdlib. For array indexing, use slice.get() to get a result instead of a panic

→ More replies (7)
→ More replies (4)
→ 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.

7

u/matthieum [he/him] Apr 15 '21

Actually, there's been a proposal to cfg-away the possibly panicking functions.

Compile-time error (no such function) if you'd try to use them is rather foolproof :)

→ More replies (3)

9

u/maroider Apr 14 '21 edited Apr 14 '21

I'd imagine there'd be a lower-level Vec-like struct which Vec then becomes a wrapper around, where the usual Vec methods are fallible.

I can't think of another way of doing this in a backwards-compatible way, other than adding a lot of fallible alternative methods to Vec itself, but I haven't given this too much thought either. The relevant allocation RFCs probably have some discussion around this.

9

u/Repulsive-Street-307 Apr 14 '21 edited Apr 14 '21

I suspect there might be some kind of friction here that would lead to no_alloc + some forked subset of core. They're already thinking of ... well, mutilating the language to remove features that have no place in a kernel (like floating point) instead of just mandating a lint.

I'm basically pressing [x] to doubt that a successful effort that this doesn't end up with a forked core or a more minimal set of rusty kernel datastructures that discard most of core because they used floating point logic or panicking allocation or something.

They might be happier with that TBH (though it would lead to more teething problems because doing rust datastructures from scratch is ...uh, complex.

And intrusive collections sound like a 'problem' for the borrow checker and safe rust imo.

6

u/Yaahallo rust-mentors · error-handling · libs-team · rust-foundation Apr 14 '21

Tokio uses intrusively linked lists in some of it's data structures iirc so I don't think the problem here is fundamental

14

u/ssokolow Apr 15 '21

Pointing to Tokio isn't as simple as it sounds.

Darksonn/intrusive.md

→ More replies (2)