]> git.ipfire.org Git - thirdparty/kernel/stable.git/commit
rust: devres: get rid of Devres' inner Arc
authorDanilo Krummrich <dakr@kernel.org>
Thu, 26 Jun 2025 20:00:41 +0000 (22:00 +0200)
committerDanilo Krummrich <dakr@kernel.org>
Sat, 28 Jun 2025 16:08:50 +0000 (18:08 +0200)
commitf5d3ef25d238901a76fe0277787afa44f7714739
tree9c57a14c2cddb8d78f710f8d26e6896398650486
parent46ae8fd7386abf809355d1857abac5cf2d7c3f62
rust: devres: get rid of Devres' inner Arc

So far Devres uses an inner memory allocation and reference count, i.e.
an inner Arc, in order to ensure that the devres callback can't run into
a use-after-free in case where the Devres object is dropped while the
devres callback runs concurrently.

Instead, use a completion in order to avoid a potential UAF: In
Devres::drop(), if we detect that we can't remove the devres action
anymore, we wait for the completion that is completed from the devres
callback. If, in turn, we were able to successfully remove the devres
action, we can just go ahead.

This, again, allows us to get rid of the internal Arc, and instead let
Devres consume an `impl PinInit<T, E>` in order to return an
`impl PinInit<Devres<T>, E>`, which enables us to get away with less
memory allocations.

Additionally, having the resulting explicit synchronization in
Devres::drop() prevents potential subtle undesired side effects of the
devres callback dropping the final Arc reference asynchronously within
the devres callback.

Reviewed-by: Benno Lossin <lossin@kernel.org>
Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20250626200054.243480-4-dakr@kernel.org
[ Move '# Invariants' below '# Examples'. - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
drivers/gpu/nova-core/driver.rs
drivers/gpu/nova-core/gpu.rs
rust/kernel/devres.rs
rust/kernel/pci.rs
samples/rust/rust_driver_pci.rs