Skip to content

Commit 5640e62

Browse files
Danilo Krummrichgregkh
authored andcommitted
rust: devres: do not dereference to the internal Revocable
commit 20c96ed upstream. We can't expose direct access to the internal Revocable, since this allows users to directly revoke the internal Revocable without Devres having the chance to synchronize with the devres callback -- we have to guarantee that the internal Revocable has been fully revoked before the device is fully unbound. Hence, remove the corresponding Deref implementation and, instead, provide indirect accessors for the internal Revocable. Note that we can still support Devres::revoke() by implementing the required synchronization (which would be almost identical to the synchronization in Devres::drop()). Fixes: 76c01de ("rust: add devres abstraction") Reviewed-by: Benno Lossin <lossin@kernel.org> Link: https://lore.kernel.org/r/20250611174827.380555-1-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8b60b1d commit 5640e62

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

rust/kernel/devres.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ use crate::{
1212
error::{Error, Result},
1313
ffi::c_void,
1414
prelude::*,
15-
revocable::Revocable,
16-
sync::{Arc, Completion},
15+
revocable::{Revocable, RevocableGuard},
16+
sync::{rcu, Arc, Completion},
1717
types::ARef,
1818
};
1919

20-
use core::ops::Deref;
21-
2220
#[pin_data]
2321
struct DevresInner<T> {
2422
dev: ARef<Device>,
@@ -196,21 +194,23 @@ impl<T> Devres<T> {
196194

197195
Ok(())
198196
}
199-
}
200197

201-
impl<T> Deref for Devres<T> {
202-
type Target = Revocable<T>;
198+
/// [`Devres`] accessor for [`Revocable::try_access`].
199+
pub fn try_access(&self) -> Option<RevocableGuard<'_, T>> {
200+
self.0.data.try_access()
201+
}
203202

204-
fn deref(&self) -> &Self::Target {
205-
&self.0.data
203+
/// [`Devres`] accessor for [`Revocable::try_access_with_guard`].
204+
pub fn try_access_with_guard<'a>(&'a self, guard: &'a rcu::Guard) -> Option<&'a T> {
205+
self.0.data.try_access_with_guard(guard)
206206
}
207207
}
208208

209209
impl<T> Drop for Devres<T> {
210210
fn drop(&mut self) {
211211
// SAFETY: When `drop` runs, it is guaranteed that nobody is accessing the revocable data
212212
// anymore, hence it is safe not to wait for the grace period to finish.
213-
if unsafe { self.revoke_nosync() } {
213+
if unsafe { self.0.data.revoke_nosync() } {
214214
// We revoked `self.0.data` before the devres action did, hence try to remove it.
215215
if !DevresInner::remove_action(&self.0) {
216216
// We could not remove the devres action, which means that it now runs concurrently,

0 commit comments

Comments
 (0)