From: Boqun Feng <boqun.feng@gmail.com>
To: Benno Lossin <benno.lossin@proton.me>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
rust-for-linux@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
linux-kernel@vger.kernel.org,
"Wedson Almeida Filho" <walmeida@microsoft.com>
Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef`
Date: Mon, 25 Sep 2023 09:16:05 -0700 [thread overview]
Message-ID: <ZRGyRQuBcWvgtdNR@Boquns-Mac-mini.home> (raw)
In-Reply-To: <14513589-cc31-8985-8ff6-a97d2882f593@proton.me>
On Mon, Sep 25, 2023 at 03:07:44PM +0000, Benno Lossin wrote:
> On 25.09.23 16:49, Boqun Feng wrote:
> > 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@gmail.com> wrote:
> >>>>
> >>>> From: Wedson Almeida Filho <walmeida@microsoft.com>
> >>>>
> >>>> 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@gmail.com>
> >>>> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> >>>> Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
> >>>> ---
> >>>> 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?
>
> Sure, here is the problem:
>
Thanks, Benno.
> ```rust
> struct MutatingDrop {
> value: i32,
> }
>
> impl Drop for MutatingDrop {
> fn drop(&mut self) {
> self.value = 0;
> }
> }
>
> let arc = Arc::new(MutatingDrop { value: 42 });
> let wr = arc.as_with_ref(); // this creates a shared `&` reference to the MutatingDrop
> let arc2: Arc<MutatingDrop> = wr.into(); // increments the reference count to 2
More precisely, here we did a
&WithRef<_> -> NonNull<WithRef<_>>
conversion, and later on, we may use the `NonNull<WithRef<_>>` in
`drop` to get a `Box<WithRef<_>>`.
> drop(arc); // this decrements the reference count to 1
> drop(arc2); // this decrements the reference count to 0, so it will drop it
> ```
> When dropping `arc2` it will run the destructor for `MutatingDrop`,
> which mutates `value`. This is a problem, because the mutable reference
> supplied was derived from a `&`, that is not allowed in Rust.
>
Is this an UB? I kinda wonder what's the real damage we can get, because
in this case, we just use a reference to carry a value of a pointer,
i.e.
ptr -> reference -> ptr
I cannot think of any real damage compiler can make, but I'm happy to be
surprised ;-)
Regards,
Boqun
> --
> Cheers,
> Benno
>
>
next prev parent reply other threads:[~2023-09-25 16:16 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-23 14:49 [PATCH v2 0/2] Remove `ArcBorrow` Wedson Almeida Filho
2023-09-23 14:49 ` [PATCH v2 1/2] rust: arc: rename `ArcInner` to `WithRef` Wedson Almeida Filho
2023-09-23 19:31 ` Martin Rodriguez Reboredo
2023-09-24 11:59 ` Benno Lossin
2023-09-24 13:41 ` Jianguo Bao
2023-09-25 6:21 ` Alice Ryhl
2023-09-23 14:49 ` [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` Wedson Almeida Filho
2023-09-23 19:32 ` Martin Rodriguez Reboredo
2023-09-24 11:59 ` Benno Lossin
2023-09-24 13:36 ` Jianguo Bao
2023-09-25 6:29 ` Alice Ryhl
2023-09-25 9:14 ` Benno Lossin
2023-09-25 14:49 ` Boqun Feng
2023-09-25 15:00 ` Alice Ryhl
2023-09-25 15:17 ` Boqun Feng
2023-09-25 15:30 ` Alice Ryhl
2023-09-25 16:02 ` Boqun Feng
2023-09-25 16:11 ` Benno Lossin
2023-09-25 15:07 ` Benno Lossin
2023-09-25 16:16 ` Boqun Feng [this message]
2023-09-25 17:00 ` Benno Lossin
2023-09-25 18:51 ` Boqun Feng
2023-09-25 21:03 ` Benno Lossin
2023-09-25 21:55 ` Boqun Feng
2023-09-25 21:58 ` Alice Ryhl
2023-09-25 22:02 ` Boqun Feng
2023-09-25 22:06 ` Boqun Feng
2023-09-25 22:26 ` Benno Lossin
2023-09-25 22:34 ` Boqun Feng
2023-09-25 23:24 ` Boqun Feng
2023-09-26 8:26 ` Gary Guo
2023-09-26 15:24 ` Boqun Feng
2023-09-26 15:41 ` Alice Ryhl
2023-09-26 16:35 ` Boqun Feng
2023-09-26 17:15 ` Benno Lossin
2023-09-26 17:43 ` Boqun Feng
2023-09-26 18:26 ` Benno Lossin
2023-09-26 21:31 ` Alice Ryhl
2023-09-26 18:20 ` Boqun Feng
2023-09-26 21:27 ` Alice Ryhl
2023-09-25 15:04 ` Alice Ryhl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZRGyRQuBcWvgtdNR@Boquns-Mac-mini.home \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=walmeida@microsoft.com \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®