[PATCH v2 0/2] rust: sync: Introduce LazyInit

From: Lyude Paul

Date: Fri May 29 2026 - 13:42:37 EST


While trying to write up some SGTable bindings for the GEM shmem
helpers, I discovered that SetOnce is actually quite difficult to make
use of in a number of situations.

For one: there is (upstream) currently no way to use it with fallible
initializers. Even with some of the work being done to add support for
this from Gary Guo and Alvin Sun:

https://lore.kernel.org/rust-for-linux/20260326-b4-tyr-debugfs-v1-1-074badd18716@xxxxxxxxx/

We're still left with the issue that if two callers race on trying to
call init(), there's no actual way for either of them to block until the
thread they raced with is done initializing the contents of the
container.

LazyInit is my proposed solution to this issue. It provides a container
which protects the initialization of its contents, without protecting
access to its contents (implying of course, that its contents have to
provide their own thread synchronization).

Lyude Paul (2):
rust: sync: lock: Add Lock::get_mut_pinned()
rust: sync: Introduce LazyInit

rust/kernel/sync.rs | 2 +
rust/kernel/sync/lazy_init.rs | 352 ++++++++++++++++++++++++++++++++++
rust/kernel/sync/lock.rs | 11 ++
3 files changed, 365 insertions(+)
create mode 100644 rust/kernel/sync/lazy_init.rs


base-commit: 0e42ec83d46ab8877d38d37493328ed7d1a24de8
--
2.54.0