From eed7a146b86cf95441d5563c7a99cd02f8a6c3a4 Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Tue, 18 Apr 2023 23:43:46 +0200 Subject: rust: arc: fix intra-doc link in `Arc::init` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Arc::init` refers to `Arc::pin_init` via an intra-doc link using the text `pin_init`, rather than more explicitly, which makes `rustdoc` point it to the `pin_init!` macro instead. This is required for the compiler upgrade since the newer `rustdoc` would trigger the `broken_intra_doc_links` lint [1], but in this case the macro was not the intended target to begin with, and so the actual fix is to make it point to the right place, regardless of the upgrade. Thus make it more explicit. Fixes: 92c4a1e7e81c ("rust: init/sync: add `InPlaceInit` trait to pin-initialize smart pointers") Link: https://github.com/rust-lang/rust/issues/106142 [1] Reviewed-by: Gary Guo Reviewed-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Björn Roy Baron Tested-by: Ariel Miculas Tested-by: David Gow Tested-by: Boqun Feng Link: https://lore.kernel.org/r/20230418214347.324156-3-ojeda@kernel.org Signed-off-by: Miguel Ojeda --- rust/kernel/sync/arc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rust/kernel/sync/arc.rs') diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index e6d206242465..1b0734fdf6a7 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -185,7 +185,7 @@ impl Arc { /// Use the given initializer to in-place initialize a `T`. /// - /// This is equivalent to [`pin_init`], since an [`Arc`] is always pinned. + /// This is equivalent to [`Arc::pin_init`], since an [`Arc`] is always pinned. #[inline] pub fn init(init: impl Init) -> error::Result where -- cgit v1.2.3 From bd780aea671eda9660837c05d9ba58de22ceecad Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 17 May 2023 20:08:13 +0000 Subject: rust: sync: add `Arc::ptr_eq` Add a method for comparing whether two `Arc` pointers reference the same underlying object. This comparison can already be done by getting a reference to the inner values and comparing whether the references have the same address. However, writing `Arc::ptr_eq(a, b)` is generally less error-prone than doing the same check on the references, since you might otherwise accidentally compare the two `&Arc` references instead, which wont work because those are pointers to pointers to the inner value, when you just want to compare the pointers to the inner value. Also, this method might optimize better because getting a reference to the inner value involves offsetting the pointer, which this method does not need to do. Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Signed-off-by: Alice Ryhl Reviewed-by: Benno Lossin Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Andreas Hindborg Link: https://lore.kernel.org/r/20230517200814.3157916-1-aliceryhl@google.com Signed-off-by: Miguel Ojeda --- rust/kernel/sync/arc.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'rust/kernel/sync/arc.rs') diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 1b0734fdf6a7..b03d84f7ce87 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -221,6 +221,11 @@ impl Arc { // reference can be created. unsafe { ArcBorrow::new(self.ptr) } } + + /// Compare whether two [`Arc`] pointers reference the same underlying object. + pub fn ptr_eq(this: &Self, other: &Self) -> bool { + core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr()) + } } impl ForeignOwnable for Arc { -- cgit v1.2.3 From 47329ba14b5aac1ac975544eee71ecc888557d23 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 17 May 2023 20:08:14 +0000 Subject: rust: sync: implement `AsRef` for `Arc` This trait lets you use `Arc` in code that is generic over smart pointer types. The `AsRef` trait should be implemented on all smart pointers. The standard library also implements it on the ordinary `Arc`. Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Signed-off-by: Alice Ryhl Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin Reviewed-by: Gary Guo Reviewed-by: Andreas Hindborg Link: https://lore.kernel.org/r/20230517200814.3157916-2-aliceryhl@google.com Signed-off-by: Miguel Ojeda --- rust/kernel/sync/arc.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'rust/kernel/sync/arc.rs') diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index b03d84f7ce87..ace478442998 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -264,6 +264,12 @@ impl Deref for Arc { } } +impl AsRef for Arc { + fn as_ref(&self) -> &T { + self.deref() + } +} + impl Clone for Arc { fn clone(&self) -> Self { // INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero. -- cgit v1.2.3 From f8110cd157833e721f50f779dc70f8ae5b429832 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 31 May 2023 14:59:36 +0000 Subject: rust: sync: reword the `Arc` safety comment for `Send` The safety comment on `impl Send for Arc` talks about "directly" accessing the value, when it really means "accessing the value with a mutable reference". This commit clarifies that. Suggested-by: Boqun Feng Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg Reviewed-by: Boqun Feng Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin Link: https://lore.kernel.org/r/20230531145939.3714886-2-aliceryhl@google.com Signed-off-by: Miguel Ojeda --- rust/kernel/sync/arc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'rust/kernel/sync/arc.rs') diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index ace478442998..a8c2177950cd 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -146,8 +146,8 @@ impl, U: ?Sized> core::ops::DispatchFromDyn> for Ar // SAFETY: It is safe to send `Arc` to another thread when the underlying `T` is `Sync` because // it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs -// `T` to be `Send` because any thread that has an `Arc` may ultimately access `T` directly, for -// example, when the reference count reaches zero and `T` is dropped. +// `T` to be `Send` because any thread that has an `Arc` may ultimately access `T` using a +// mutable reference when the reference count reaches zero and `T` is dropped. unsafe impl Send for Arc {} // SAFETY: It is safe to send `&Arc` to another thread when the underlying `T` is `Sync` for the -- cgit v1.2.3 From d701e061cb14f589f8c4f48fd7fbe81c0e34b7e7 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 31 May 2023 14:59:37 +0000 Subject: rust: sync: reword the `Arc` safety comment for `Sync` The safety comment on `impl Sync for Arc` references the Send safety comment. This commit avoids that in case the two comments drift apart in the future. Suggested-by: Andreas Hindborg Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg Reviewed-by: Boqun Feng Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin Link: https://lore.kernel.org/r/20230531145939.3714886-3-aliceryhl@google.com Signed-off-by: Miguel Ojeda --- rust/kernel/sync/arc.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'rust/kernel/sync/arc.rs') diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index a8c2177950cd..a89843cacaad 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -150,9 +150,11 @@ impl, U: ?Sized> core::ops::DispatchFromDyn> for Ar // mutable reference when the reference count reaches zero and `T` is dropped. unsafe impl Send for Arc {} -// SAFETY: It is safe to send `&Arc` to another thread when the underlying `T` is `Sync` for the -// same reason as above. `T` needs to be `Send` as well because a thread can clone an `&Arc` -// into an `Arc`, which may lead to `T` being accessed by the same reasoning as above. +// SAFETY: It is safe to send `&Arc` to another thread when the underlying `T` is `Sync` +// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, +// it needs `T` to be `Send` because any thread that has a `&Arc` may clone it and get an +// `Arc` on that thread, so the thread may ultimately access `T` using a mutable reference when +// the reference count reaches zero and `T` is dropped. unsafe impl Sync for Arc {} impl Arc { -- cgit v1.2.3