From: "Vincenzo Palazzo" <vincenzopalazzodev@gmail.com>
To: "Boqun Feng" <boqun.feng@gmail.com>,
<rust-for-linux@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Cc: "Will Deacon" <will@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Mark Rutland" <mark.rutland@arm.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>
Subject: Re: [RFC 3/5] rust: sync: Arc: Introduces Arc::get_inner() helper
Date: Sat, 04 Feb 2023 19:51:50 +0100 [thread overview]
Message-ID: <CQA06YLAOMGN.T1CEBGNQO34W@vincent> (raw)
In-Reply-To: <20230201232244.212908-4-boqun.feng@gmail.com>
On Thu Feb 2, 2023 at 12:22 AM CET, Boqun Feng wrote:
> Getting a `&ArcInner<T>` should always be safe from a `Arc<T>`,
> therefore add this helper function to avoid duplicate unsafe
> `ptr.as_ref()`.
>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> ---
Reviwed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
> rust/kernel/sync/arc.rs | 19 ++++++++++---------
> 1 file changed, 10 insertions(+), 9 deletions(-)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index fbfceaa3096e..4d8de20c996f 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -201,6 +201,13 @@ impl<T: ?Sized> Arc<T> {
> // reference can be created.
> unsafe { ArcBorrow::new(self.ptr) }
> }
> +
> + /// Returns a reference to the internal [`ArcInner`].
> + fn get_inner(&self) -> &ArcInner<T> {
> + // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
> + // safe to dereference it.
> + unsafe { self.ptr.as_ref() }
> + }
> }
>
> impl<T: 'static> ForeignOwnable for Arc<T> {
> @@ -233,9 +240,7 @@ impl<T: ?Sized> Deref for Arc<T> {
> type Target = T;
>
> fn deref(&self) -> &Self::Target {
> - // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
> - // safe to dereference it.
> - unsafe { &self.ptr.as_ref().data }
> + &self.get_inner().data
> }
> }
>
> @@ -244,7 +249,7 @@ impl<T: ?Sized> Clone for Arc<T> {
> // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
> // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
> // safe to increment the refcount.
> - unsafe { bindings::refcount_inc(self.ptr.as_ref().refcount.get()) };
> + unsafe { bindings::refcount_inc(self.get_inner().refcount.get()) };
>
> // SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`.
> unsafe { Self::from_inner(self.ptr) }
> @@ -253,11 +258,7 @@ impl<T: ?Sized> Clone for Arc<T> {
>
> impl<T: ?Sized> Drop for Arc<T> {
> fn drop(&mut self) {
> - // SAFETY: By the type invariant, there is necessarily a reference to the object. We cannot
> - // touch `refcount` after it's decremented to a non-zero value because another thread/CPU
> - // may concurrently decrement it to zero and free it. It is ok to have a raw pointer to
> - // freed/invalid memory as long as it is never dereferenced.
> - let refcount = unsafe { self.ptr.as_ref() }.refcount.get();
> + let refcount = self.get_inner().refcount.get();
>
> // INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and
> // this instance is being dropped, so the broken invariant is not observable.
> --
> 2.39.1
next prev parent reply other threads:[~2023-02-04 18:51 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-01 23:22 [RFC 0/5] rust: sync: Arc: Implement Debug and Display Boqun Feng
2023-02-01 23:22 ` [RFC 1/5] rust: sync: impl Display for {Unique,}Arc Boqun Feng
2023-02-02 14:15 ` Gary Guo
2023-02-02 16:50 ` Björn Roy Baron
2023-02-04 10:20 ` Finn Behrens
2023-02-04 18:47 ` Vincenzo Palazzo
2023-02-01 23:22 ` [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count() Boqun Feng
2023-02-02 9:14 ` Peter Zijlstra
2023-02-02 13:46 ` Boqun Feng
2023-02-02 14:21 ` Gary Guo
2023-02-02 15:41 ` Greg KH
2023-02-02 16:10 ` Boqun Feng
2023-02-02 16:17 ` Greg KH
2023-02-02 16:51 ` Boqun Feng
2023-02-02 21:47 ` Miguel Ojeda
2023-02-03 5:22 ` Greg KH
2023-02-03 7:25 ` Boqun Feng
2023-02-03 7:38 ` Greg KH
2023-02-03 7:43 ` Boqun Feng
2023-02-03 8:01 ` Boqun Feng
2023-02-03 19:17 ` Josh Stone
2023-02-03 19:22 ` Wedson Almeida Filho
2023-02-02 14:22 ` Gary Guo
2023-02-04 18:48 ` Vincenzo Palazzo
2023-02-01 23:22 ` [RFC 3/5] rust: sync: Arc: Introduces Arc::get_inner() helper Boqun Feng
2023-02-02 14:24 ` Gary Guo
2023-02-02 16:53 ` Björn Roy Baron
2023-02-04 18:51 ` Vincenzo Palazzo [this message]
2023-02-01 23:22 ` [RFC 4/5] rust: sync: impl Debug for {Unique,}Arc Boqun Feng
2023-02-02 14:28 ` Gary Guo
2023-02-03 19:46 ` Boqun Feng
2023-02-04 18:56 ` Vincenzo Palazzo
2023-02-01 23:22 ` [RFC 5/5] sample: rust: print: Add sampe code for Arc printing Boqun Feng
2023-02-02 16:56 ` Björn Roy Baron
2023-02-04 10:22 ` Finn Behrens
2023-02-04 19:05 ` Vincenzo Palazzo
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=CQA06YLAOMGN.T1CEBGNQO34W@vincent \
--to=vincenzopalazzodev@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
--cc=will@kernel.org \
/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).