[PATCH] rust: sync: implement Unpin for ARef

Alice Ryhl posted 1 patch 1 month, 3 weeks ago
There is a newer version of this series
rust/kernel/sync/aref.rs | 3 +++
1 file changed, 3 insertions(+)
[PATCH] rust: sync: implement Unpin for ARef
Posted by Alice Ryhl 1 month, 3 weeks ago
The default implementation of Unpin for ARef<T> is conditional on T
being Unpin due to its PhantomData<T> field. However, this is overly
strict as pointers to T are legal to move even if T itself cannot move.

Since commit 66f1ea83d9f8 ("rust: lock: Add a Pin<&mut T> accessor")
this causes build failures when combined with a Mutex that contains an
field ARef<T>, because almost any type that ARef is used with is !Unpin.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/sync/aref.rs | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
index 0d24a0432015d09509a255df0a4f114171ae9fb0..c4be6f5d4884ddd6cbfbd7c27d396310a7cde30e 100644
--- a/rust/kernel/sync/aref.rs
+++ b/rust/kernel/sync/aref.rs
@@ -83,6 +83,9 @@ unsafe impl<T: AlwaysRefCounted + Sync + Send> Send for ARef<T> {}
 // example, when the reference count reaches zero and `T` is dropped.
 unsafe impl<T: AlwaysRefCounted + Sync + Send> Sync for ARef<T> {}
 
+// Even if T is pinned, pointers to T can still move.
+impl<T> Unpin for ARef<T> {}
+
 impl<T: AlwaysRefCounted> ARef<T> {
     /// Creates a new instance of [`ARef`].
     ///

---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20251217-unpin-for-aref-3dc0fe3fd5b0

Best regards,
-- 
Alice Ryhl <aliceryhl@google.com>
Re: [PATCH] rust: sync: implement Unpin for ARef
Posted by kernel test robot 1 month, 2 weeks ago
Hi Alice,

kernel test robot noticed the following build errors:

[auto build test ERROR on 8f0b4cce4481fb22653697cced8d0d04027cb1e8]

url:    https://github.com/intel-lab-lkp/linux/commits/Alice-Ryhl/rust-sync-implement-Unpin-for-ARef/20251218-013900
base:   8f0b4cce4481fb22653697cced8d0d04027cb1e8
patch link:    https://lore.kernel.org/r/20251217-unpin-for-aref-v1-1-84711b747d02%40google.com
patch subject: [PATCH] rust: sync: implement Unpin for ARef
config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20251221/202512211302.CuYHRqLX-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251221/202512211302.CuYHRqLX-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512211302.CuYHRqLX-lkp@intel.com/

All errors (new ones prefixed by >>):

>> error[E0277]: the trait bound `T: sync::aref::AlwaysRefCounted` is not satisfied
   --> rust/kernel/sync/aref.rs:87:19
   |
   87 | impl<T> Unpin for ARef<T> {}
   |                   ^^^^^^^ the trait `sync::aref::AlwaysRefCounted` is not implemented for `T`
   |
   note: required by a bound in `sync::aref::ARef`
   --> rust/kernel/sync/aref.rs:68:20
   |
   68 | pub struct ARef<T: AlwaysRefCounted> {
   |                    ^^^^^^^^^^^^^^^^ required by this bound in `ARef`
   help: consider restricting type parameter `T` with trait `AlwaysRefCounted`
   |
   87 | impl<T: sync::aref::AlwaysRefCounted> Unpin for ARef<T> {}
   |       ++++++++++++++++++++++++++++++

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] rust: sync: implement Unpin for ARef
Posted by Alice Ryhl 1 month, 3 weeks ago
On Wed, Dec 17, 2025 at 6:37 PM Alice Ryhl <aliceryhl@google.com> wrote:
>
> The default implementation of Unpin for ARef<T> is conditional on T
> being Unpin due to its PhantomData<T> field. However, this is overly
> strict as pointers to T are legal to move even if T itself cannot move.
>
> Since commit 66f1ea83d9f8 ("rust: lock: Add a Pin<&mut T> accessor")
> this causes build failures when combined with a Mutex that contains an
> field ARef<T>, because almost any type that ARef is used with is !Unpin.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
>  rust/kernel/sync/aref.rs | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
> index 0d24a0432015d09509a255df0a4f114171ae9fb0..c4be6f5d4884ddd6cbfbd7c27d396310a7cde30e 100644
> --- a/rust/kernel/sync/aref.rs
> +++ b/rust/kernel/sync/aref.rs
> @@ -83,6 +83,9 @@ unsafe impl<T: AlwaysRefCounted + Sync + Send> Send for ARef<T> {}
>  // example, when the reference count reaches zero and `T` is dropped.
>  unsafe impl<T: AlwaysRefCounted + Sync + Send> Sync for ARef<T> {}
>
> +// Even if T is pinned, pointers to T can still move.
> +impl<T> Unpin for ARef<T> {}

This needs an AlwaysRefCounted bound. I sent a v2:

https://lore.kernel.org/all/20251218-unpin-for-aref-v2-1-30d77129cbc6@google.com/

Alice
Re: [PATCH] rust: sync: implement Unpin for ARef
Posted by Daniel Almeida 1 month, 3 weeks ago

> On 17 Dec 2025, at 14:37, Alice Ryhl <aliceryhl@google.com> wrote:
> 
> The default implementation of Unpin for ARef<T> is conditional on T
> being Unpin due to its PhantomData<T> field. However, this is overly
> strict as pointers to T are legal to move even if T itself cannot move.
> 
> Since commit 66f1ea83d9f8 ("rust: lock: Add a Pin<&mut T> accessor")
> this causes build failures when combined with a Mutex that contains an
> field ARef<T>, because almost any type that ARef is used with is !Unpin.
> 
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> rust/kernel/sync/aref.rs | 3 +++
> 1 file changed, 3 insertions(+)
> 
> diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
> index 0d24a0432015d09509a255df0a4f114171ae9fb0..c4be6f5d4884ddd6cbfbd7c27d396310a7cde30e 100644
> --- a/rust/kernel/sync/aref.rs
> +++ b/rust/kernel/sync/aref.rs
> @@ -83,6 +83,9 @@ unsafe impl<T: AlwaysRefCounted + Sync + Send> Send for ARef<T> {}
> // example, when the reference count reaches zero and `T` is dropped.
> unsafe impl<T: AlwaysRefCounted + Sync + Send> Sync for ARef<T> {}
> 
> +// Even if T is pinned, pointers to T can still move.
> +impl<T> Unpin for ARef<T> {}
> +
> impl<T: AlwaysRefCounted> ARef<T> {
>     /// Creates a new instance of [`ARef`].
>     ///
> 
> ---
> base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
> change-id: 20251217-unpin-for-aref-3dc0fe3fd5b0
> 
> Best regards,
> -- 
> Alice Ryhl <aliceryhl@google.com>
> 
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>