Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: devres: remove action in `Devres::drop`

So far `DevresInner` is kept alive, even if `Devres` is dropped until
the devres callback is executed to avoid a WARN() when the action has
been released already.

With the introduction of devm_remove_action_nowarn() we can remove the
action in `Devres::drop`, handle the case where the action has been
released already and hence also free `DevresInner`.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Link: https://lore.kernel.org/r/20250107122609.8135-2-dakr@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Danilo Krummrich and committed by
Greg Kroah-Hartman
8ff65664 f1725160

+35 -12
+35 -12
rust/kernel/devres.rs
··· 10 10 bindings, 11 11 device::Device, 12 12 error::{Error, Result}, 13 + ffi::c_void, 13 14 prelude::*, 14 15 revocable::Revocable, 15 16 sync::Arc, 17 + types::ARef, 16 18 }; 17 19 18 20 use core::ops::Deref; 19 21 20 22 #[pin_data] 21 23 struct DevresInner<T> { 24 + dev: ARef<Device>, 25 + callback: unsafe extern "C" fn(*mut c_void), 22 26 #[pin] 23 27 data: Revocable<T>, 24 28 } ··· 102 98 fn new(dev: &Device, data: T, flags: Flags) -> Result<Arc<DevresInner<T>>> { 103 99 let inner = Arc::pin_init( 104 100 pin_init!( DevresInner { 101 + dev: dev.into(), 102 + callback: Self::devres_callback, 105 103 data <- Revocable::new(data), 106 104 }), 107 105 flags, ··· 115 109 116 110 // SAFETY: `devm_add_action` guarantees to call `Self::devres_callback` once `dev` is 117 111 // detached. 118 - let ret = unsafe { 119 - bindings::devm_add_action(dev.as_raw(), Some(Self::devres_callback), data as _) 120 - }; 112 + let ret = 113 + unsafe { bindings::devm_add_action(dev.as_raw(), Some(inner.callback), data as _) }; 121 114 122 115 if ret != 0 { 123 116 // SAFETY: We just created another reference to `inner` in order to pass it to ··· 127 122 } 128 123 129 124 Ok(inner) 125 + } 126 + 127 + fn as_ptr(&self) -> *const Self { 128 + self as _ 129 + } 130 + 131 + fn remove_action(this: &Arc<Self>) { 132 + // SAFETY: 133 + // - `self.inner.dev` is a valid `Device`, 134 + // - the `action` and `data` pointers are the exact same ones as given to devm_add_action() 135 + // previously, 136 + // - `self` is always valid, even if the action has been released already. 137 + let ret = unsafe { 138 + bindings::devm_remove_action_nowarn( 139 + this.dev.as_raw(), 140 + Some(this.callback), 141 + this.as_ptr() as _, 142 + ) 143 + }; 144 + 145 + if ret == 0 { 146 + // SAFETY: We leaked an `Arc` reference to devm_add_action() in `DevresInner::new`; if 147 + // devm_remove_action_nowarn() was successful we can (and have to) claim back ownership 148 + // of this reference. 149 + let _ = unsafe { Arc::from_raw(this.as_ptr()) }; 150 + } 130 151 } 131 152 132 153 #[allow(clippy::missing_safety_doc)] ··· 196 165 197 166 impl<T> Drop for Devres<T> { 198 167 fn drop(&mut self) { 199 - // Revoke the data, such that it gets dropped already and the actual resource is freed. 200 - // 201 - // `DevresInner` has to stay alive until the devres callback has been called. This is 202 - // necessary since we don't know when `Devres` is dropped and calling 203 - // `devm_remove_action()` instead could race with `devres_release_all()`. 204 - // 205 - // SAFETY: When `drop` runs, it's guaranteed that nobody is accessing the revocable data 206 - // anymore, hence it is safe not to wait for the grace period to finish. 207 - unsafe { self.revoke_nosync() }; 168 + DevresInner::remove_action(&self.0); 208 169 } 209 170 }