Re: [TECH TOPIC] Rust for Linux

James Bottomley
2021-07-07T14:08:18Z

On Wed, 2021-07-07 at 15:49 +0200, Miguel Ojeda wrote:
> On Wed, Jul 7, 2021 at 9:52 AM James Bottomley
> <James.Bottomley@hansenpartnership.com> wrote:
> > Rust has a reference counted pointer: Rc<T>. However the problem
> > is the rust memory model is via strict accounting, so once you
> > start using refcounts, which need shared accounting, you can then
> > get memory leaks and it's unclear if rust actually buys you
> > anything over C for this use case.
>
> Even if you are using `Rc<T>`, Rust buys you a *lot*. For starters,
> it still prevents UB

UB is Undefined Behaviour, right? C plus the memory model should mean
we don't have that in the kernel, although that's enforced by
inspection and checking rather than the compiler.

> and data races -- e.g. it prevents mistakenly sharing the `Rc` with
> other threads.

I'm not sure I get the point here: all the kernel refcount models are
explicitly multi-threaded, because we usually expect multiple CPU
threads to be using objects simultaneously ... it's why the base
implementation, kref, uses atomics. That's not to say we don't have
single threaded use cases ... it's just that's not what's commonly
used.

That does beg another question which I couldn't find the answer to in
the base docs: the Rust Rc<T> is saturation protected, isn't it? or
would the rust implementation inherit that from the kernel?

James