Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef`

From: Boqun Feng
Date: Mon Sep 25 2023 - 10:49:54 EST


On Mon, Sep 25, 2023 at 09:14:50AM +0000, Benno Lossin wrote:
> On 25.09.23 08:29, Alice Ryhl wrote:
> > On Sat, Sep 23, 2023 at 4:50 PM Wedson Almeida Filho <wedsonaf@xxxxxxxxx> wrote:
> >>
> >> From: Wedson Almeida Filho <walmeida@xxxxxxxxxxxxx>
> >>
> >> With GATs, we don't need a separate type to represent a borrowed object
> >> with a refcount, we can just use Rust's regular shared borrowing. In
> >> this case, we use `&WithRef<T>` instead of `ArcBorrow<'_, T>`.
> >>
> >> Co-developed-by: Boqun Feng <boqun.feng@xxxxxxxxx>
> >> Signed-off-by: Boqun Feng <boqun.feng@xxxxxxxxx>
> >> Signed-off-by: Wedson Almeida Filho <walmeida@xxxxxxxxxxxxx>
> >> ---
> >> rust/kernel/sync.rs | 2 +-
> >> rust/kernel/sync/arc.rs | 134 ++++++++++++----------------------------
> >> 2 files changed, 39 insertions(+), 97 deletions(-)
> >
> > I'm concerned about this change, because an `&WithRef<T>` only has
> > immutable permissions for the allocation. No pointer derived from it
> > may be used to modify the value in the Arc, however, the drop
> > implementation of Arc will do exactly that.
>
> That is indeed a problem. We could put the value in an `UnsafeCell`, but
> that would lose us niche optimizations and probably also other optimizations.
>

Not sure I understand the problem here, why do we allow modifying the
value in the Arc if you only have a shared ownership? Also I fail to see
why `ArcBorrow` doesn't have the problem. Maybe I'm missing something
subtle here? Could you provide an example?

Regards,
Boqun

> > It also means that we
> > can't convert an Arc with refcount 1 into a UniqueArc.
>
> I think you still can, since to do that you would consume the `Arc<T>` by
> value, thus guaranteeing that no references (and thus no `&WithRef<T>`) exist.
> So I think this would still be fine.
>
> --
> Cheers,
> Benno
>
>