summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorTamir Duberstein <tamird@gmail.com>2024-11-20 06:46:02 -0500
committerMiguel Ojeda <ojeda@kernel.org>2025-01-13 23:45:31 +0100
commit5d385a356f628bd4a1d9f403588272d9626e3245 (patch)
tree3453afc71e8769f7e26a9c078c97feb010eb39c1 /rust
parentaa991a2a819535a0014e1159b455b64e3db87510 (diff)
rust: arc: split unsafe block, add missing comment
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> Link: https://lore.kernel.org/r/20241120-borrow-mut-v6-3-80dbadd00951@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust')
-rw-r--r--rust/kernel/sync/arc.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 378925e553ec..93eac650146a 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -389,10 +389,14 @@ impl<T: ?Sized> AsRef<T> for Arc<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) }