From: Tamir Duberstein <tamird@gmail.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
Tamir Duberstein <tamird@gmail.com>
Subject: [PATCH v6 3/6] rust: arc: split unsafe block, add missing comment
Date: Wed, 20 Nov 2024 06:46:02 -0500 [thread overview]
Message-ID: <20241120-borrow-mut-v6-3-80dbadd00951@gmail.com> (raw)
In-Reply-To: <20241120-borrow-mut-v6-0-80dbadd00951@gmail.com>
The new SAFETY comment style is taken from existing comments in `deref`
and `drop.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
rust/kernel/sync/arc.rs | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 50645660a9c33cb121ee1b2469003b325000d840..a11f267ce5d40b987f1f3c459271e5317ea0bae8 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -377,10 +377,14 @@ fn as_ref(&self) -> &T {
impl<T: ?Sized> Clone for Arc<T> {
fn clone(&self) -> Self {
+ // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
+ // safe to dereference it.
+ let refcount = unsafe { self.ptr.as_ref() }.refcount.get();
+
// 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(refcount) };
// SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`.
unsafe { Self::from_inner(self.ptr) }
--
2.47.0
next prev parent reply other threads:[~2024-11-20 11:46 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-20 11:45 [PATCH v6 0/6] rust: add improved version of `ForeignOwnable::borrow_mut` Tamir Duberstein
2024-11-20 11:46 ` [PATCH v6 1/6] rust: arc: use `NonNull::new_unchecked` Tamir Duberstein
2024-11-20 11:46 ` [PATCH v6 2/6] rust: types: avoid `as` casts Tamir Duberstein
2025-01-13 13:15 ` Danilo Krummrich
2024-11-20 11:46 ` Tamir Duberstein [this message]
2024-11-20 11:46 ` [PATCH v6 4/6] rust: change `ForeignOwnable` pointer to mut Tamir Duberstein
2025-01-13 13:15 ` Danilo Krummrich
2024-11-20 11:46 ` [PATCH v6 5/6] rust: reorder `ForeignOwnable` items Tamir Duberstein
2024-11-20 11:46 ` [PATCH v6 6/6] rust: add improved version of `ForeignOwnable::borrow_mut` Tamir Duberstein
2025-01-13 13:18 ` Danilo Krummrich
2025-01-13 15:45 ` Tamir Duberstein
2025-01-13 16:20 ` Miguel Ojeda
2025-01-13 23:52 ` Miguel Ojeda
2025-01-13 12:09 ` [PATCH v6 0/6] " Miguel Ojeda
2025-01-13 23:52 ` Miguel Ojeda
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=20241120-borrow-mut-v6-3-80dbadd00951@gmail.com \
--to=tamird@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.