The new SAFETY comment style is taken from existing comments in `deref`
and `drop.
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 af383bcd003e1122ebe1b62a49fe40279458e379..9adea755a5ad1a7b03f7fc30a7abc76c1f966c6c 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
On Mon, Nov 4, 2024 at 10:20 PM Tamir Duberstein <tamird@gmail.com> wrote: > > The new SAFETY comment style is taken from existing comments in `deref` > and `drop. > > Signed-off-by: Tamir Duberstein <tamird@gmail.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index af383bcd003e1122ebe1b62a49fe40279458e379..9adea755a5ad1a7b03f7fc30a7abc76c1f966c6c 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(); I would normally prefer to avoid creating a reference to the entire ArcInner, but in this particular case it is okay due to the specifics of how Arc works. Alice
On Fri, Nov 8, 2024 at 6:38 AM Alice Ryhl <aliceryhl@google.com> wrote: > > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > > index af383bcd003e1122ebe1b62a49fe40279458e379..9adea755a5ad1a7b03f7fc30a7abc76c1f966c6c 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(); > > I would normally prefer to avoid creating a reference to the entire > ArcInner, but in this particular case it is okay due to the specifics > of how Arc works. Note that this particular line appears also in the Drop impl just below. That said, can you help me understand the concern with creating a reference to the entire ArcInner?
On Fri, Nov 8, 2024 at 1:30 PM Tamir Duberstein <tamird@gmail.com> wrote: > > On Fri, Nov 8, 2024 at 6:38 AM Alice Ryhl <aliceryhl@google.com> wrote: > > > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > > > index af383bcd003e1122ebe1b62a49fe40279458e379..9adea755a5ad1a7b03f7fc30a7abc76c1f966c6c 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(); > > > > I would normally prefer to avoid creating a reference to the entire > > ArcInner, but in this particular case it is okay due to the specifics > > of how Arc works. > > Note that this particular line appears also in the Drop impl just > below. That said, can you help me understand the concern with creating > a reference to the entire ArcInner? Creating a shared reference to the entire ArcInner is an assertion that no &mut exists to any part of the ArcInner, even though you only access the refcount field. In this particular case, asserting shared access to the `data` field is not a problem due to how Arc works, so it's okay, but the pattern is problematic in other cases. You could write `unsafe { (*self.ptr.as_ptr()).refcount.get() }` to avoid the as_ref call. Alice
On Fri, Nov 8, 2024 at 7:37 AM Alice Ryhl <aliceryhl@google.com> wrote: > You could write `unsafe { (*self.ptr.as_ptr()).refcount.get() }` to > avoid the as_ref call. Doesn't `(*self.ptr.as_ptr())` have the same problem?
On Fri, Nov 8, 2024 at 1:41 PM Tamir Duberstein <tamird@gmail.com> wrote: > > On Fri, Nov 8, 2024 at 7:37 AM Alice Ryhl <aliceryhl@google.com> wrote: > > You could write `unsafe { (*self.ptr.as_ptr()).refcount.get() }` to > > avoid the as_ref call. > > Doesn't `(*self.ptr.as_ptr())` have the same problem? It's not quite the same. Both `*ptr` and `(*ptr).field` are place expressions which do not inherently involve the creation of a reference in the way that `NonNull::as_ref` does. Alice
On Fri, Nov 8, 2024 at 7:47 AM Alice Ryhl <aliceryhl@google.com> wrote: > > On Fri, Nov 8, 2024 at 1:41 PM Tamir Duberstein <tamird@gmail.com> wrote: > > > > On Fri, Nov 8, 2024 at 7:37 AM Alice Ryhl <aliceryhl@google.com> wrote: > > > You could write `unsafe { (*self.ptr.as_ptr()).refcount.get() }` to > > > avoid the as_ref call. > > > > Doesn't `(*self.ptr.as_ptr())` have the same problem? > > It's not quite the same. Both `*ptr` and `(*ptr).field` are place > expressions which do not inherently involve the creation of a > reference in the way that `NonNull::as_ref` does. Thanks for explaining! Tamir
© 2016 - 2024 Red Hat, Inc.