Skip to content

Commit ba26851

Browse files
author
Danilo Krummrich
committed
rust: devres: fix race condition due to nesting
Commit f5d3ef2 ("rust: devres: get rid of Devres' inner Arc") did attempt to optimize away the internal reference count of Devres. However, without an internal reference count, we can't support cases where Devres is indirectly nested, resulting into a deadlock. Such indirect nesting easily happens in the following way: A registration object (which is guarded by devres) hold a reference count of an object that holds a device resource guarded by devres itself. For instance a drm::Registration holds a reference of a drm::Device. The drm::Device itself holds a device resource in its private data. When the drm::Registration is dropped by devres, and it happens that it did hold the last reference count of the drm::Device, it also drops the device resource, which is guarded by devres itself. Thus, resulting into a deadlock in the Devres destructor of the device resource, as in the following backtrace. sysrq: Show Blocked State task:rmmod state:D stack:0 pid:1331 tgid:1331 ppid:1330 task_flags:0x400100 flags:0x00000010 Call trace: __switch_to+0x190/0x294 (T) __schedule+0x878/0xf10 schedule+0x4c/0xcc schedule_timeout+0x44/0x118 wait_for_common+0xc0/0x18c wait_for_completion+0x18/0x24 _RINvNtCs4gKlGRWyJ5S_4core3ptr13drop_in_placeINtNtNtCsgzhNYVB7wSz_6kernel4sync3arc3ArcINtNtBN_6devres6DevresmEEECsRdyc7Hyps3_15rust_driver_pci+0x68/0xe8 [rust_driver_pci] _RINvNvNtCsgzhNYVB7wSz_6kernel6devres16register_foreign8callbackINtNtCs4gKlGRWyJ5S_4core3pin3PinINtNtNtB6_5alloc4kbox3BoxINtNtNtB6_4sync3arc3ArcINtB4_6DevresmEENtNtB1A_9allocator7KmallocEEECsRdyc7Hyps3_15rust_driver_pci+0x34/0xc8 [rust_driver_pci] devm_action_release+0x14/0x20 devres_release_all+0xb8/0x118 device_release_driver_internal+0x1c4/0x28c driver_detach+0x94/0xd4 bus_remove_driver+0xdc/0x11c driver_unregister+0x34/0x58 pci_unregister_driver+0x20/0x80 __arm64_sys_delete_module+0x1d8/0x254 invoke_syscall+0x40/0xcc el0_svc_common+0x8c/0xd8 do_el0_svc+0x1c/0x28 el0_svc+0x54/0x1d4 el0t_64_sync_handler+0x84/0x12c el0t_64_sync+0x198/0x19c In order to fix this, re-introduce the internal reference count. Reported-by: Boris Brezillon <boris.brezillon@collabora.com> Closes: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/.E2.9C.94.20Deadlock.20caused.20by.20nested.20Devres/with/571242651 Reported-by: Markus Probst <markus.probst@posteo.de> Closes: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/.E2.9C.94.20Devres.20inside.20Devres.20stuck.20on.20cleanup/with/571239721 Reported-by: Alice Ryhl <aliceryhl@google.com> Closes: https://gitlab.freedesktop.org/panfrost/linux/-/merge_requests/56#note_3282757 Fixes: f5d3ef2 ("rust: devres: get rid of Devres' inner Arc") Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Tested-by: Boris Brezillon <boris.brezillon@collabora.com> Link: https://patch.msgid.link/20260205222529.91465-1-dakr@kernel.org [ Call clone() prior to devm_add_action(). - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 66fb10b commit ba26851

1 file changed

Lines changed: 40 additions & 109 deletions

File tree

rust/kernel/devres.rs

Lines changed: 40 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,11 @@ use crate::{
2121
sync::{
2222
aref::ARef,
2323
rcu,
24-
Completion, //
25-
},
26-
types::{
27-
ForeignOwnable,
28-
Opaque,
29-
ScopeGuard, //
24+
Arc, //
3025
},
26+
types::ForeignOwnable,
3127
};
3228

33-
use pin_init::Wrapper;
34-
35-
/// [`Devres`] inner data accessed from [`Devres::callback`].
36-
#[pin_data]
37-
struct Inner<T: Send> {
38-
#[pin]
39-
data: Revocable<T>,
40-
/// Tracks whether [`Devres::callback`] has been completed.
41-
#[pin]
42-
devm: Completion,
43-
/// Tracks whether revoking [`Self::data`] has been completed.
44-
#[pin]
45-
revoke: Completion,
46-
}
47-
4829
/// This abstraction is meant to be used by subsystems to containerize [`Device`] bound resources to
4930
/// manage their lifetime.
5031
///
@@ -121,108 +102,70 @@ struct Inner<T: Send> {
121102
/// # fn no_run(dev: &Device<Bound>) -> Result<(), Error> {
122103
/// // SAFETY: Invalid usage for example purposes.
123104
/// let iomem = unsafe { IoMem::<{ core::mem::size_of::<u32>() }>::new(0xBAAAAAAD)? };
124-
/// let devres = KBox::pin_init(Devres::new(dev, iomem), GFP_KERNEL)?;
105+
/// let devres = Devres::new(dev, iomem)?;
125106
///
126107
/// let res = devres.try_access().ok_or(ENXIO)?;
127108
/// res.write8(0x42, 0x0);
128109
/// # Ok(())
129110
/// # }
130111
/// ```
131-
///
132-
/// # Invariants
133-
///
134-
/// `Self::inner` is guaranteed to be initialized and is always accessed read-only.
135-
#[pin_data(PinnedDrop)]
136112
pub struct Devres<T: Send> {
137113
dev: ARef<Device>,
138114
/// Pointer to [`Self::devres_callback`].
139115
///
140116
/// Has to be stored, since Rust does not guarantee to always return the same address for a
141117
/// function. However, the C API uses the address as a key.
142118
callback: unsafe extern "C" fn(*mut c_void),
143-
/// Contains all the fields shared with [`Self::callback`].
144-
// TODO: Replace with `UnsafePinned`, once available.
145-
//
146-
// Subsequently, the `drop_in_place()` in `Devres::drop` and `Devres::new` as well as the
147-
// explicit `Send` and `Sync' impls can be removed.
148-
#[pin]
149-
inner: Opaque<Inner<T>>,
150-
_add_action: (),
119+
data: Arc<Revocable<T>>,
151120
}
152121

153122
impl<T: Send> Devres<T> {
154123
/// Creates a new [`Devres`] instance of the given `data`.
155124
///
156125
/// The `data` encapsulated within the returned `Devres` instance' `data` will be
157126
/// (revoked)[`Revocable`] once the device is detached.
158-
pub fn new<'a, E>(
159-
dev: &'a Device<Bound>,
160-
data: impl PinInit<T, E> + 'a,
161-
) -> impl PinInit<Self, Error> + 'a
127+
pub fn new<E>(dev: &Device<Bound>, data: impl PinInit<T, E>) -> Result<Self>
162128
where
163-
T: 'a,
164129
Error: From<E>,
165130
{
166-
try_pin_init!(&this in Self {
167-
dev: dev.into(),
168-
callback: Self::devres_callback,
169-
// INVARIANT: `inner` is properly initialized.
170-
inner <- Opaque::pin_init(try_pin_init!(Inner {
171-
devm <- Completion::new(),
172-
revoke <- Completion::new(),
173-
data <- Revocable::new(data),
174-
})),
175-
// TODO: Replace with "initializer code blocks" [1] once available.
176-
//
177-
// [1] https://github.com/Rust-for-Linux/pin-init/pull/69
178-
_add_action: {
179-
// SAFETY: `this` is a valid pointer to uninitialized memory.
180-
let inner = unsafe { &raw mut (*this.as_ptr()).inner };
131+
let callback = Self::devres_callback;
132+
let data = Arc::pin_init(Revocable::new(data), GFP_KERNEL)?;
133+
let devres_data = data.clone();
181134

182-
// SAFETY:
183-
// - `dev.as_raw()` is a pointer to a valid bound device.
184-
// - `inner` is guaranteed to be a valid for the duration of the lifetime of `Self`.
185-
// - `devm_add_action()` is guaranteed not to call `callback` until `this` has been
186-
// properly initialized, because we require `dev` (i.e. the *bound* device) to
187-
// live at least as long as the returned `impl PinInit<Self, Error>`.
188-
to_result(unsafe {
189-
bindings::devm_add_action(dev.as_raw(), Some(*callback), inner.cast())
190-
}).inspect_err(|_| {
191-
let inner = Opaque::cast_into(inner);
135+
// SAFETY:
136+
// - `dev.as_raw()` is a pointer to a valid bound device.
137+
// - `data` is guaranteed to be a valid for the duration of the lifetime of `Self`.
138+
// - `devm_add_action()` is guaranteed not to call `callback` for the entire lifetime of
139+
// `dev`.
140+
to_result(unsafe {
141+
bindings::devm_add_action(
142+
dev.as_raw(),
143+
Some(callback),
144+
Arc::as_ptr(&data).cast_mut().cast(),
145+
)
146+
})?;
192147

193-
// SAFETY: `inner` is a valid pointer to an `Inner<T>` and valid for both reads
194-
// and writes.
195-
unsafe { core::ptr::drop_in_place(inner) };
196-
})?;
197-
},
198-
})
199-
}
148+
// `devm_add_action()` was successful and has consumed the reference count.
149+
core::mem::forget(devres_data);
200150

201-
fn inner(&self) -> &Inner<T> {
202-
// SAFETY: By the type invairants of `Self`, `inner` is properly initialized and always
203-
// accessed read-only.
204-
unsafe { &*self.inner.get() }
151+
Ok(Self {
152+
dev: dev.into(),
153+
callback,
154+
data,
155+
})
205156
}
206157

207158
fn data(&self) -> &Revocable<T> {
208-
&self.inner().data
159+
&self.data
209160
}
210161

211162
#[allow(clippy::missing_safety_doc)]
212163
unsafe extern "C" fn devres_callback(ptr: *mut kernel::ffi::c_void) {
213-
// SAFETY: In `Self::new` we've passed a valid pointer to `Inner` to `devm_add_action()`,
214-
// hence `ptr` must be a valid pointer to `Inner`.
215-
let inner = unsafe { &*ptr.cast::<Inner<T>>() };
164+
// SAFETY: In `Self::new` we've passed a valid pointer of `Revocable<T>` to
165+
// `devm_add_action()`, hence `ptr` must be a valid pointer to `Revocable<T>`.
166+
let data = unsafe { Arc::from_raw(ptr.cast::<Revocable<T>>()) };
216167

217-
// Ensure that `inner` can't be used anymore after we signal completion of this callback.
218-
let inner = ScopeGuard::new_with_data(inner, |inner| inner.devm.complete_all());
219-
220-
if !inner.data.revoke() {
221-
// If `revoke()` returns false, it means that `Devres::drop` already started revoking
222-
// `data` for us. Hence we have to wait until `Devres::drop` signals that it
223-
// completed revoking `data`.
224-
inner.revoke.wait_for_completion();
225-
}
168+
data.revoke();
226169
}
227170

228171
fn remove_action(&self) -> bool {
@@ -234,7 +177,7 @@ impl<T: Send> Devres<T> {
234177
bindings::devm_remove_action_nowarn(
235178
self.dev.as_raw(),
236179
Some(self.callback),
237-
core::ptr::from_ref(self.inner()).cast_mut().cast(),
180+
core::ptr::from_ref(self.data()).cast_mut().cast(),
238181
)
239182
} == 0)
240183
}
@@ -313,31 +256,19 @@ unsafe impl<T: Send> Send for Devres<T> {}
313256
// SAFETY: `Devres` can be shared with any task, if `T: Sync`.
314257
unsafe impl<T: Send + Sync> Sync for Devres<T> {}
315258

316-
#[pinned_drop]
317-
impl<T: Send> PinnedDrop for Devres<T> {
318-
fn drop(self: Pin<&mut Self>) {
259+
impl<T: Send> Drop for Devres<T> {
260+
fn drop(&mut self) {
319261
// SAFETY: When `drop` runs, it is guaranteed that nobody is accessing the revocable data
320262
// anymore, hence it is safe not to wait for the grace period to finish.
321263
if unsafe { self.data().revoke_nosync() } {
322264
// We revoked `self.data` before the devres action did, hence try to remove it.
323-
if !self.remove_action() {
324-
// We could not remove the devres action, which means that it now runs concurrently,
325-
// hence signal that `self.data` has been revoked by us successfully.
326-
self.inner().revoke.complete_all();
327-
328-
// Wait for `Self::devres_callback` to be done using this object.
329-
self.inner().devm.wait_for_completion();
265+
if self.remove_action() {
266+
// SAFETY: In `Self::new` we have taken an additional reference count of `self.data`
267+
// for `devm_add_action()`. Since `remove_action()` was successful, we have to drop
268+
// this additional reference count.
269+
drop(unsafe { Arc::from_raw(Arc::as_ptr(&self.data)) });
330270
}
331-
} else {
332-
// `Self::devres_callback` revokes `self.data` for us, hence wait for it to be done
333-
// using this object.
334-
self.inner().devm.wait_for_completion();
335271
}
336-
337-
// INVARIANT: At this point it is guaranteed that `inner` can't be accessed any more.
338-
//
339-
// SAFETY: `inner` is valid for dropping.
340-
unsafe { core::ptr::drop_in_place(self.inner.get()) };
341272
}
342273
}
343274

0 commit comments

Comments
 (0)