* [PATCH v2] rust: sync: set_once: Implement Send and Sync
@ 2025-12-16 0:09 ` FUJITA Tomonori
2025-12-16 12:32 ` Gary Guo
` (4 more replies)
0 siblings, 5 replies; 8+ messages in thread
From: FUJITA Tomonori @ 2025-12-16 0:09 UTC (permalink / raw)
To: ojeda, a.hindborg, gary
Cc: aliceryhl, bjorn3_gh, boqun.feng, dakr, lossin, tmgross,
rust-for-linux
Implement Send and Sync for SetOnce<T> to allow it to be used across
thread boundaries.
Send: SetOnce<T> can be transferred across threads when T: Send, as
the contained value is also transferred and will be dropped on the
destination thread.
Sync: SetOnce<T> can be shared across threads when T: Sync, as
as_ref() provides shared references &T and atomic operations ensure
proper synchronization. Since the inner T may be dropped on any
thread, we also require T: Send.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
v2:
- update the `Sync` impl to require `T: Send + Sync` with the comment adjusted
v1: https://lore.kernel.org/rust-for-linux/20251211230919.1303926-2-fujita.tomonori@gmail.com/
rust/kernel/sync/set_once.rs | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs
index bdba601807d8..139cef05e935 100644
--- a/rust/kernel/sync/set_once.rs
+++ b/rust/kernel/sync/set_once.rs
@@ -123,3 +123,11 @@ fn drop(&mut self) {
}
}
}
+
+// SAFETY: `SetOnce` can be transferred across thread boundaries iff the data it contains can.
+unsafe impl<T: Send> Send for SetOnce<T> {}
+
+// SAFETY: `SetOnce` synchronises access to the inner value via atomic operations,
+// so shared references are safe when `T: Sync`. Since the inner `T` may be dropped
+// on any thread, we also require `T: Send`.
+unsafe impl<T: Send + Sync> Sync for SetOnce<T> {}
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v2] rust: sync: set_once: Implement Send and Sync
2025-12-16 0:09 ` [PATCH v2] rust: sync: set_once: Implement Send and Sync FUJITA Tomonori
@ 2025-12-16 12:32 ` Gary Guo
2025-12-17 10:38 ` Andreas Hindborg
` (3 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Gary Guo @ 2025-12-16 12:32 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: ojeda, a.hindborg, aliceryhl, bjorn3_gh, boqun.feng, dakr, lossin,
tmgross, rust-for-linux
On Tue, 16 Dec 2025 09:09:01 +0900
FUJITA Tomonori <fujita.tomonori@gmail.com> wrote:
> Implement Send and Sync for SetOnce<T> to allow it to be used across
> thread boundaries.
>
> Send: SetOnce<T> can be transferred across threads when T: Send, as
> the contained value is also transferred and will be dropped on the
> destination thread.
>
> Sync: SetOnce<T> can be shared across threads when T: Sync, as
> as_ref() provides shared references &T and atomic operations ensure
> proper synchronization. Since the inner T may be dropped on any
> thread, we also require T: Send.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
> ---
> v2:
> - update the `Sync` impl to require `T: Send + Sync` with the comment adjusted
> v1: https://lore.kernel.org/rust-for-linux/20251211230919.1303926-2-fujita.tomonori@gmail.com/
>
> rust/kernel/sync/set_once.rs | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs
> index bdba601807d8..139cef05e935 100644
> --- a/rust/kernel/sync/set_once.rs
> +++ b/rust/kernel/sync/set_once.rs
> @@ -123,3 +123,11 @@ fn drop(&mut self) {
> }
> }
> }
> +
> +// SAFETY: `SetOnce` can be transferred across thread boundaries iff the data it contains can.
> +unsafe impl<T: Send> Send for SetOnce<T> {}
> +
> +// SAFETY: `SetOnce` synchronises access to the inner value via atomic operations,
> +// so shared references are safe when `T: Sync`. Since the inner `T` may be dropped
> +// on any thread, we also require `T: Send`.
> +unsafe impl<T: Send + Sync> Sync for SetOnce<T> {}
>
> base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] rust: sync: set_once: Implement Send and Sync
2025-12-16 0:09 ` [PATCH v2] rust: sync: set_once: Implement Send and Sync FUJITA Tomonori
2025-12-16 12:32 ` Gary Guo
@ 2025-12-17 10:38 ` Andreas Hindborg
2025-12-17 20:44 ` Boqun Feng
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Andreas Hindborg @ 2025-12-17 10:38 UTC (permalink / raw)
To: FUJITA Tomonori, ojeda, gary
Cc: aliceryhl, bjorn3_gh, boqun.feng, dakr, lossin, tmgross,
rust-for-linux
"FUJITA Tomonori" <fujita.tomonori@gmail.com> writes:
> Implement Send and Sync for SetOnce<T> to allow it to be used across
> thread boundaries.
>
> Send: SetOnce<T> can be transferred across threads when T: Send, as
> the contained value is also transferred and will be dropped on the
> destination thread.
>
> Sync: SetOnce<T> can be shared across threads when T: Sync, as
> as_ref() provides shared references &T and atomic operations ensure
> proper synchronization. Since the inner T may be dropped on any
> thread, we also require T: Send.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Best regards,
Andreas Hindborg
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] rust: sync: set_once: Implement Send and Sync
2025-12-16 0:09 ` [PATCH v2] rust: sync: set_once: Implement Send and Sync FUJITA Tomonori
2025-12-16 12:32 ` Gary Guo
2025-12-17 10:38 ` Andreas Hindborg
@ 2025-12-17 20:44 ` Boqun Feng
2025-12-17 20:45 ` Alice Ryhl
2026-01-13 10:50 ` [tip: locking/core] " tip-bot2 for FUJITA Tomonori
4 siblings, 0 replies; 8+ messages in thread
From: Boqun Feng @ 2025-12-17 20:44 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: ojeda, a.hindborg, gary, aliceryhl, bjorn3_gh, dakr, lossin,
tmgross, rust-for-linux
On Tue, Dec 16, 2025 at 09:09:01AM +0900, FUJITA Tomonori wrote:
> Implement Send and Sync for SetOnce<T> to allow it to be used across
> thread boundaries.
>
> Send: SetOnce<T> can be transferred across threads when T: Send, as
> the contained value is also transferred and will be dropped on the
> destination thread.
>
> Sync: SetOnce<T> can be shared across threads when T: Sync, as
> as_ref() provides shared references &T and atomic operations ensure
> proper synchronization. Since the inner T may be dropped on any
> thread, we also require T: Send.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---
I queued this into my rust-sync branch for 7.0:
https://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git/ rust-sync
Thanks!
Regards,
Boqun
> v2:
> - update the `Sync` impl to require `T: Send + Sync` with the comment adjusted
> v1: https://lore.kernel.org/rust-for-linux/20251211230919.1303926-2-fujita.tomonori@gmail.com/
>
> rust/kernel/sync/set_once.rs | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs
> index bdba601807d8..139cef05e935 100644
> --- a/rust/kernel/sync/set_once.rs
> +++ b/rust/kernel/sync/set_once.rs
> @@ -123,3 +123,11 @@ fn drop(&mut self) {
> }
> }
> }
> +
> +// SAFETY: `SetOnce` can be transferred across thread boundaries iff the data it contains can.
> +unsafe impl<T: Send> Send for SetOnce<T> {}
> +
> +// SAFETY: `SetOnce` synchronises access to the inner value via atomic operations,
> +// so shared references are safe when `T: Sync`. Since the inner `T` may be dropped
> +// on any thread, we also require `T: Send`.
> +unsafe impl<T: Send + Sync> Sync for SetOnce<T> {}
>
> base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] rust: sync: set_once: Implement Send and Sync
2025-12-16 0:09 ` [PATCH v2] rust: sync: set_once: Implement Send and Sync FUJITA Tomonori
` (2 preceding siblings ...)
2025-12-17 20:44 ` Boqun Feng
@ 2025-12-17 20:45 ` Alice Ryhl
2025-12-17 21:10 ` Boqun Feng
2026-01-13 10:50 ` [tip: locking/core] " tip-bot2 for FUJITA Tomonori
4 siblings, 1 reply; 8+ messages in thread
From: Alice Ryhl @ 2025-12-17 20:45 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: ojeda, a.hindborg, gary, bjorn3_gh, boqun.feng, dakr, lossin,
tmgross, rust-for-linux
On Tue, Dec 16, 2025 at 09:09:01AM +0900, FUJITA Tomonori wrote:
> Implement Send and Sync for SetOnce<T> to allow it to be used across
> thread boundaries.
>
> Send: SetOnce<T> can be transferred across threads when T: Send, as
> the contained value is also transferred and will be dropped on the
> destination thread.
>
> Sync: SetOnce<T> can be shared across threads when T: Sync, as
> as_ref() provides shared references &T and atomic operations ensure
> proper synchronization. Since the inner T may be dropped on any
> thread, we also require T: Send.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---
> v2:
> - update the `Sync` impl to require `T: Send + Sync` with the comment adjusted
> v1: https://lore.kernel.org/rust-for-linux/20251211230919.1303926-2-fujita.tomonori@gmail.com/
>
> rust/kernel/sync/set_once.rs | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs
> index bdba601807d8..139cef05e935 100644
> --- a/rust/kernel/sync/set_once.rs
> +++ b/rust/kernel/sync/set_once.rs
> @@ -123,3 +123,11 @@ fn drop(&mut self) {
> }
> }
> }
> +
> +// SAFETY: `SetOnce` can be transferred across thread boundaries iff the data it contains can.
> +unsafe impl<T: Send> Send for SetOnce<T> {}
> +
> +// SAFETY: `SetOnce` synchronises access to the inner value via atomic operations,
> +// so shared references are safe when `T: Sync`. Since the inner `T` may be dropped
> +// on any thread, we also require `T: Send`.
> +unsafe impl<T: Send + Sync> Sync for SetOnce<T> {}
The reason you need Sync is because &SetOnce<T> allows you to access &T.
It's not because atomic operations are used.
Alice
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] rust: sync: set_once: Implement Send and Sync
2025-12-17 20:45 ` Alice Ryhl
@ 2025-12-17 21:10 ` Boqun Feng
2025-12-18 7:58 ` Alice Ryhl
0 siblings, 1 reply; 8+ messages in thread
From: Boqun Feng @ 2025-12-17 21:10 UTC (permalink / raw)
To: Alice Ryhl
Cc: FUJITA Tomonori, ojeda, a.hindborg, gary, bjorn3_gh, dakr, lossin,
tmgross, rust-for-linux
On Wed, Dec 17, 2025 at 08:45:35PM +0000, Alice Ryhl wrote:
> On Tue, Dec 16, 2025 at 09:09:01AM +0900, FUJITA Tomonori wrote:
> > Implement Send and Sync for SetOnce<T> to allow it to be used across
> > thread boundaries.
> >
> > Send: SetOnce<T> can be transferred across threads when T: Send, as
> > the contained value is also transferred and will be dropped on the
> > destination thread.
> >
> > Sync: SetOnce<T> can be shared across threads when T: Sync, as
> > as_ref() provides shared references &T and atomic operations ensure
> > proper synchronization. Since the inner T may be dropped on any
> > thread, we also require T: Send.
> >
> > Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> > ---
> > v2:
> > - update the `Sync` impl to require `T: Send + Sync` with the comment adjusted
> > v1: https://lore.kernel.org/rust-for-linux/20251211230919.1303926-2-fujita.tomonori@gmail.com/
> >
> > rust/kernel/sync/set_once.rs | 8 ++++++++
> > 1 file changed, 8 insertions(+)
> >
> > diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs
> > index bdba601807d8..139cef05e935 100644
> > --- a/rust/kernel/sync/set_once.rs
> > +++ b/rust/kernel/sync/set_once.rs
> > @@ -123,3 +123,11 @@ fn drop(&mut self) {
> > }
> > }
> > }
> > +
> > +// SAFETY: `SetOnce` can be transferred across thread boundaries iff the data it contains can.
> > +unsafe impl<T: Send> Send for SetOnce<T> {}
> > +
> > +// SAFETY: `SetOnce` synchronises access to the inner value via atomic operations,
> > +// so shared references are safe when `T: Sync`. Since the inner `T` may be dropped
> > +// on any thread, we also require `T: Send`.
> > +unsafe impl<T: Send + Sync> Sync for SetOnce<T> {}
>
> The reason you need Sync is because &SetOnce<T> allows you to access &T.
> It's not because atomic operations are used.
>
I think the point here is that atomic operations contribute to the
safety of sharing &SetOnce<T> between threads, if the synchronization
part of SetOnce is not done via atomics (or any other synchronization),
then SetOnce is not Sync. In other words, making `SetOnce<T>: Sync`
requires all the following:
* The internal operation is proper synchronized
* T is Sync because a `&T` can be derived from `&SetOnce<T>`
* T is Send because copy() may create a `T` on another thread.
(hmm... the third condition does looks weird to me, because if `T` is
`Sync` and `Copy`, the copy on `T` should guarante the drop of the
copied `T` is safe, otherwise `T` itself is not `Sync`. Gary, do you
have an example in mind that requires `T` being `Send` for `SetOnce<T>`
being `Sync`?)
Regards,
Boqun
> Alice
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] rust: sync: set_once: Implement Send and Sync
2025-12-17 21:10 ` Boqun Feng
@ 2025-12-18 7:58 ` Alice Ryhl
0 siblings, 0 replies; 8+ messages in thread
From: Alice Ryhl @ 2025-12-18 7:58 UTC (permalink / raw)
To: Boqun Feng
Cc: FUJITA Tomonori, ojeda, a.hindborg, gary, bjorn3_gh, dakr, lossin,
tmgross, rust-for-linux
On Thu, Dec 18, 2025 at 06:10:37AM +0900, Boqun Feng wrote:
> On Wed, Dec 17, 2025 at 08:45:35PM +0000, Alice Ryhl wrote:
> > On Tue, Dec 16, 2025 at 09:09:01AM +0900, FUJITA Tomonori wrote:
> > > Implement Send and Sync for SetOnce<T> to allow it to be used across
> > > thread boundaries.
> > >
> > > Send: SetOnce<T> can be transferred across threads when T: Send, as
> > > the contained value is also transferred and will be dropped on the
> > > destination thread.
> > >
> > > Sync: SetOnce<T> can be shared across threads when T: Sync, as
> > > as_ref() provides shared references &T and atomic operations ensure
> > > proper synchronization. Since the inner T may be dropped on any
> > > thread, we also require T: Send.
> > >
> > > Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> > > ---
> > > v2:
> > > - update the `Sync` impl to require `T: Send + Sync` with the comment adjusted
> > > v1: https://lore.kernel.org/rust-for-linux/20251211230919.1303926-2-fujita.tomonori@gmail.com/
> > >
> > > rust/kernel/sync/set_once.rs | 8 ++++++++
> > > 1 file changed, 8 insertions(+)
> > >
> > > diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs
> > > index bdba601807d8..139cef05e935 100644
> > > --- a/rust/kernel/sync/set_once.rs
> > > +++ b/rust/kernel/sync/set_once.rs
> > > @@ -123,3 +123,11 @@ fn drop(&mut self) {
> > > }
> > > }
> > > }
> > > +
> > > +// SAFETY: `SetOnce` can be transferred across thread boundaries iff the data it contains can.
> > > +unsafe impl<T: Send> Send for SetOnce<T> {}
> > > +
> > > +// SAFETY: `SetOnce` synchronises access to the inner value via atomic operations,
> > > +// so shared references are safe when `T: Sync`. Since the inner `T` may be dropped
> > > +// on any thread, we also require `T: Send`.
> > > +unsafe impl<T: Send + Sync> Sync for SetOnce<T> {}
> >
> > The reason you need Sync is because &SetOnce<T> allows you to access &T.
> > It's not because atomic operations are used.
> >
>
> I think the point here is that atomic operations contribute to the
> safety of sharing &SetOnce<T> between threads, if the synchronization
> part of SetOnce is not done via atomics (or any other synchronization),
> then SetOnce is not Sync. In other words, making `SetOnce<T>: Sync`
> requires all the following:
>
> * The internal operation is proper synchronized
> * T is Sync because a `&T` can be derived from `&SetOnce<T>`
> * T is Send because copy() may create a `T` on another thread.
>
> (hmm... the third condition does looks weird to me, because if `T` is
> `Sync` and `Copy`, the copy on `T` should guarante the drop of the
> copied `T` is safe, otherwise `T` itself is not `Sync`. Gary, do you
> have an example in mind that requires `T` being `Send` for `SetOnce<T>`
> being `Sync`?)
I guess in general safety comments for Send/Sync are a bit tricky ...
you really need to argue that the entire API is designed correctly to
show that having &SetOnce<T> on multiple threads in parallel is sound
(in the case of Sync).
Alice
^ permalink raw reply [flat|nested] 8+ messages in thread
* [tip: locking/core] rust: sync: set_once: Implement Send and Sync
2025-12-16 0:09 ` [PATCH v2] rust: sync: set_once: Implement Send and Sync FUJITA Tomonori
` (3 preceding siblings ...)
2025-12-17 20:45 ` Alice Ryhl
@ 2026-01-13 10:50 ` tip-bot2 for FUJITA Tomonori
4 siblings, 0 replies; 8+ messages in thread
From: tip-bot2 for FUJITA Tomonori @ 2026-01-13 10:50 UTC (permalink / raw)
To: linux-tip-commits
Cc: FUJITA Tomonori, Andreas Hindborg, Gary Guo, Boqun Feng, x86,
linux-kernel
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 8a581130b1cbc17c702298b8325e3df98c792760
Gitweb: https://git.kernel.org/tip/8a581130b1cbc17c702298b8325e3df98c792760
Author: FUJITA Tomonori <fujita.tomonori@gmail.com>
AuthorDate: Tue, 16 Dec 2025 09:09:01 +09:00
Committer: Boqun Feng <boqun.feng@gmail.com>
CommitterDate: Fri, 09 Jan 2026 19:01:40 +08:00
rust: sync: set_once: Implement Send and Sync
Implement Send and Sync for SetOnce<T> to allow it to be used across
thread boundaries.
Send: SetOnce<T> can be transferred across threads when T: Send, as
the contained value is also transferred and will be dropped on the
destination thread.
Sync: SetOnce<T> can be shared across threads when T: Sync, as
as_ref() provides shared references &T and atomic operations ensure
proper synchronization. Since the inner T may be dropped on any
thread, we also require T: Send.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://patch.msgid.link/20251216000901.221375-1-fujita.tomonori@gmail.com
---
rust/kernel/sync/set_once.rs | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs
index bdba601..139cef0 100644
--- a/rust/kernel/sync/set_once.rs
+++ b/rust/kernel/sync/set_once.rs
@@ -123,3 +123,11 @@ impl<T> Drop for SetOnce<T> {
}
}
}
+
+// SAFETY: `SetOnce` can be transferred across thread boundaries iff the data it contains can.
+unsafe impl<T: Send> Send for SetOnce<T> {}
+
+// SAFETY: `SetOnce` synchronises access to the inner value via atomic operations,
+// so shared references are safe when `T: Sync`. Since the inner `T` may be dropped
+// on any thread, we also require `T: Send`.
+unsafe impl<T: Send + Sync> Sync for SetOnce<T> {}
^ permalink raw reply related [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-01-13 10:50 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <M2WR1VEvXZ1Q6GMTaONdJJTInO1AhDjAHfEKH0smTXBb9W-NUBo297K6Vzaubm_onBroEaFLIrzsgMDQbU0pbw==@protonmail.internalid>
2025-12-16 0:09 ` [PATCH v2] rust: sync: set_once: Implement Send and Sync FUJITA Tomonori
2025-12-16 12:32 ` Gary Guo
2025-12-17 10:38 ` Andreas Hindborg
2025-12-17 20:44 ` Boqun Feng
2025-12-17 20:45 ` Alice Ryhl
2025-12-17 21:10 ` Boqun Feng
2025-12-18 7:58 ` Alice Ryhl
2026-01-13 10:50 ` [tip: locking/core] " tip-bot2 for FUJITA Tomonori
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.