From: Benno Lossin <benno.lossin@proton.me>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] rust: sync: add `Arc::into_unique_or_drop`
Date: Mon, 11 Mar 2024 15:15:00 +0000 [thread overview]
Message-ID: <7976d136-3cf7-427d-a047-41c29d25ad32@proton.me> (raw)
In-Reply-To: <CAH5fLggrRANz1GrjEa671Vj0m9=UDeEcGV5vhOxq8XtR6EjUSg@mail.gmail.com>
On 3/11/24 10:03, Alice Ryhl wrote:
> On Sat, Mar 9, 2024 at 2:02 PM Benno Lossin <benno.lossin@proton.me> wrote:
>>
>> On 2/28/24 14:00, Alice Ryhl wrote:
>>> + // SAFETY: If the refcount reaches a non-zero value, then we have destroyed this `Arc` and
>>> + // will return without further touching the `Arc`. If the refcount reaches zero, then there
>>> + // are no other arcs, and we can create a `UniqueArc`.
>>
>> This comment is not explaining why it is safe to call
>> `refcount_dec_and_test` on `refcount`.
>> It dose however explain what you are going to do, so please keep it, but
>> not as a SAFETY comment.
>
> I'll reword.
>
>>> + let is_zero = unsafe { bindings::refcount_dec_and_test(refcount) };
>>> + if is_zero {
>>> + // SAFETY: We have exclusive access to the arc, so we can perform unsynchronized
>>> + // accesses to the refcount.
>>> + unsafe { core::ptr::write(refcount, bindings::REFCOUNT_INIT(1)) };
>>> +
>>> + // SAFETY: We own one refcount, so we can create a `UniqueArc`. It needs to be pinned,
>>> + // since an `Arc` is pinned.
>>
>> The `unsafe` block is only needed due to the `new_unchecked` call, which
>> you could avoid by using `.into()`. The `SAFETY` should also be an
>> `INVARIANT` comment instead.
>>
>>> + unsafe {
>>> + Some(Pin::new_unchecked(UniqueArc {
>>> + inner: Arc::from_inner(me.ptr),
>>> + }))
>>> + }
>
> The from_inner method is also unsafe.
Ah I missed that, might be a good reason to split the block.
It confused me that the SAFETY comment did not mention why calling
`new_unchecked` is sound.
> I think that using new_unchecked here makes more sense. That method is
> usually used in the case where something is already pinned, whereas
> into() is usually used to pin something that was not previously
> pinned.
I get your argument, but doing it this way avoids an unsafe function
call. I think it would be fine to use `.into()` in this case.
Splitting the unsafe block would also be fine with me.
--
Cheers,
Benno
next prev parent reply other threads:[~2024-03-11 15:15 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-28 13:00 [PATCH v2 0/2] Arc methods for linked list Alice Ryhl
2024-02-28 13:00 ` [PATCH v2 1/2] rust: sync: add `ArcBorrow::from_raw` Alice Ryhl
2024-03-09 12:55 ` Benno Lossin
2024-03-11 8:58 ` Alice Ryhl
2024-03-11 15:04 ` Benno Lossin
2024-02-28 13:00 ` [PATCH v2 2/2] rust: sync: add `Arc::into_unique_or_drop` Alice Ryhl
2024-03-09 13:02 ` Benno Lossin
2024-03-11 9:03 ` Alice Ryhl
2024-03-11 15:15 ` Benno Lossin [this message]
2024-03-11 15:35 ` Alice Ryhl
2024-03-11 15:45 ` Alice Ryhl
2024-03-11 15:47 ` Benno Lossin
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=7976d136-3cf7-427d-a047-41c29d25ad32@proton.me \
--to=benno.lossin@proton.me \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--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;
as well as URLs for NNTP newsgroup(s).