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: embed struct devres_node directly

Currently, the Devres<T> container uses devm_add_action() to register a
devres callback.

devm_add_action() allocates a struct action_devres, which on top of
struct devres_node, just keeps a data pointer and release function
pointer.

This is an unnecessary indirection, given that analogous to struct
devres, the Devres<T> container can just embed a struct devres_node
directly without an additional allocation.

In contrast to struct devres, we don't need to force an alignment of
ARCH_DMA_MINALIGN (as struct devres does to account for the worst case)
since we have generics in Rust. I.e. the compiler already ensures
correct alignment of the embedded T in Devres<T>.

Thus, get rid of devm_add_action() and instead embed a struct
devres_node directly.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://patch.msgid.link/20260213220718.82835-6-dakr@kernel.org
[ * Improve comment about core::any::type_name(),
* add #[must_use] to devres_node_remove(),
* use container_of!() in devres_node_free_node().

- Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

+139 -46
+139 -46
rust/kernel/devres.rs
··· 23 23 rcu, 24 24 Arc, // 25 25 }, 26 - types::ForeignOwnable, 26 + types::{ 27 + ForeignOwnable, 28 + Opaque, // 29 + }, 27 30 }; 31 + 32 + /// Inner type that embeds a `struct devres_node` and the `Revocable<T>`. 33 + #[repr(C)] 34 + #[pin_data] 35 + struct Inner<T> { 36 + #[pin] 37 + node: Opaque<bindings::devres_node>, 38 + #[pin] 39 + data: Revocable<T>, 40 + } 28 41 29 42 /// This abstraction is meant to be used by subsystems to containerize [`Device`] bound resources to 30 43 /// manage their lifetime. ··· 124 111 /// ``` 125 112 pub struct Devres<T: Send> { 126 113 dev: ARef<Device>, 127 - /// Pointer to [`Self::devres_callback`]. 128 - /// 129 - /// Has to be stored, since Rust does not guarantee to always return the same address for a 130 - /// function. However, the C API uses the address as a key. 131 - callback: unsafe extern "C" fn(*mut c_void), 132 - data: Arc<Revocable<T>>, 114 + inner: Arc<Inner<T>>, 115 + } 116 + 117 + // Calling the FFI functions from the `base` module directly from the `Devres<T>` impl may result in 118 + // them being called directly from driver modules. This happens since the Rust compiler will use 119 + // monomorphisation, so it might happen that functions are instantiated within the calling driver 120 + // module. For now, work around this with `#[inline(never)]` helpers. 121 + // 122 + // TODO: Remove once a more generic solution has been implemented. For instance, we may be able to 123 + // leverage `bindgen` to take care of this depending on whether a symbol is (already) exported. 124 + mod base { 125 + use kernel::{ 126 + bindings, 127 + prelude::*, // 128 + }; 129 + 130 + #[inline(never)] 131 + #[allow(clippy::missing_safety_doc)] 132 + pub(super) unsafe fn devres_node_init( 133 + node: *mut bindings::devres_node, 134 + release: bindings::dr_node_release_t, 135 + free: bindings::dr_node_free_t, 136 + ) { 137 + // SAFETY: Safety requirements are the same as `bindings::devres_node_init`. 138 + unsafe { bindings::devres_node_init(node, release, free) } 139 + } 140 + 141 + #[inline(never)] 142 + #[allow(clippy::missing_safety_doc)] 143 + pub(super) unsafe fn devres_set_node_dbginfo( 144 + node: *mut bindings::devres_node, 145 + name: *const c_char, 146 + size: usize, 147 + ) { 148 + // SAFETY: Safety requirements are the same as `bindings::devres_set_node_dbginfo`. 149 + unsafe { bindings::devres_set_node_dbginfo(node, name, size) } 150 + } 151 + 152 + #[inline(never)] 153 + #[allow(clippy::missing_safety_doc)] 154 + pub(super) unsafe fn devres_node_add( 155 + dev: *mut bindings::device, 156 + node: *mut bindings::devres_node, 157 + ) { 158 + // SAFETY: Safety requirements are the same as `bindings::devres_node_add`. 159 + unsafe { bindings::devres_node_add(dev, node) } 160 + } 161 + 162 + #[must_use] 163 + #[inline(never)] 164 + #[allow(clippy::missing_safety_doc)] 165 + pub(super) unsafe fn devres_node_remove( 166 + dev: *mut bindings::device, 167 + node: *mut bindings::devres_node, 168 + ) -> bool { 169 + // SAFETY: Safety requirements are the same as `bindings::devres_node_remove`. 170 + unsafe { bindings::devres_node_remove(dev, node) } 171 + } 133 172 } 134 173 135 174 impl<T: Send> Devres<T> { ··· 193 128 where 194 129 Error: From<E>, 195 130 { 196 - let callback = Self::devres_callback; 197 - let data = Arc::pin_init(Revocable::new(data), GFP_KERNEL)?; 198 - let devres_data = data.clone(); 131 + let inner = Arc::pin_init::<Error>( 132 + try_pin_init!(Inner { 133 + node <- Opaque::ffi_init(|node: *mut bindings::devres_node| { 134 + // SAFETY: `node` is a valid pointer to an uninitialized `struct devres_node`. 135 + unsafe { 136 + base::devres_node_init( 137 + node, 138 + Some(Self::devres_node_release), 139 + Some(Self::devres_node_free_node), 140 + ) 141 + }; 142 + 143 + // SAFETY: `node` is a valid pointer to an uninitialized `struct devres_node`. 144 + unsafe { 145 + base::devres_set_node_dbginfo( 146 + node, 147 + // TODO: Use `core::any::type_name::<T>()` once it is a `const fn`, 148 + // such that we can convert the `&str` to a `&CStr` at compile-time. 149 + c"Devres<T>".as_char_ptr(), 150 + core::mem::size_of::<Revocable<T>>(), 151 + ) 152 + }; 153 + }), 154 + data <- Revocable::new(data), 155 + }), 156 + GFP_KERNEL, 157 + )?; 199 158 200 159 // SAFETY: 201 - // - `dev.as_raw()` is a pointer to a valid bound device. 202 - // - `data` is guaranteed to be a valid for the duration of the lifetime of `Self`. 203 - // - `devm_add_action()` is guaranteed not to call `callback` for the entire lifetime of 204 - // `dev`. 205 - to_result(unsafe { 206 - bindings::devm_add_action( 207 - dev.as_raw(), 208 - Some(callback), 209 - Arc::as_ptr(&data).cast_mut().cast(), 210 - ) 211 - })?; 160 + // - `dev` is a valid pointer to a bound `struct device`. 161 + // - `node` is a valid pointer to a `struct devres_node`. 162 + // - `devres_node_add()` is guaranteed not to call `devres_node_release()` for the entire 163 + // lifetime of `dev`. 164 + unsafe { base::devres_node_add(dev.as_raw(), inner.node.get()) }; 212 165 213 - // `devm_add_action()` was successful and has consumed the reference count. 214 - core::mem::forget(devres_data); 166 + // Take additional reference count for `devres_node_add()`. 167 + core::mem::forget(inner.clone()); 215 168 216 169 Ok(Self { 217 170 dev: dev.into(), 218 - callback, 219 - data, 171 + inner, 220 172 }) 221 173 } 222 174 223 175 fn data(&self) -> &Revocable<T> { 224 - &self.data 176 + &self.inner.data 225 177 } 226 178 227 179 #[allow(clippy::missing_safety_doc)] 228 - unsafe extern "C" fn devres_callback(ptr: *mut kernel::ffi::c_void) { 229 - // SAFETY: In `Self::new` we've passed a valid pointer of `Revocable<T>` to 230 - // `devm_add_action()`, hence `ptr` must be a valid pointer to `Revocable<T>`. 231 - let data = unsafe { Arc::from_raw(ptr.cast::<Revocable<T>>()) }; 180 + unsafe extern "C" fn devres_node_release( 181 + _dev: *mut bindings::device, 182 + node: *mut bindings::devres_node, 183 + ) { 184 + let node = Opaque::cast_from(node); 232 185 233 - data.revoke(); 186 + // SAFETY: `node` is in the same allocation as its container. 187 + let inner = unsafe { kernel::container_of!(node, Inner<T>, node) }; 188 + 189 + // SAFETY: `inner` is a valid `Inner<T>` pointer. 190 + let inner = unsafe { &*inner }; 191 + 192 + inner.data.revoke(); 234 193 } 235 194 236 - fn remove_action(&self) -> bool { 195 + #[allow(clippy::missing_safety_doc)] 196 + unsafe extern "C" fn devres_node_free_node(node: *mut bindings::devres_node) { 197 + let node = Opaque::cast_from(node); 198 + 199 + // SAFETY: `node` is in the same allocation as its container. 200 + let inner = unsafe { kernel::container_of!(node, Inner<T>, node) }; 201 + 202 + // SAFETY: `inner` points to the entire `Inner<T>` allocation. 203 + drop(unsafe { Arc::from_raw(inner) }); 204 + } 205 + 206 + fn remove_node(&self) -> bool { 237 207 // SAFETY: 238 - // - `self.dev` is a valid `Device`, 239 - // - the `action` and `data` pointers are the exact same ones as given to 240 - // `devm_add_action()` previously, 241 - (unsafe { 242 - bindings::devm_remove_action_nowarn( 243 - self.dev.as_raw(), 244 - Some(self.callback), 245 - core::ptr::from_ref(self.data()).cast_mut().cast(), 246 - ) 247 - } == 0) 208 + // - `self.device().as_raw()` is a valid pointer to a bound `struct device`. 209 + // - `self.inner.node.get()` is a valid pointer to a `struct devres_node`. 210 + unsafe { base::devres_node_remove(self.device().as_raw(), self.inner.node.get()) } 248 211 } 249 212 250 213 /// Return a reference of the [`Device`] this [`Devres`] instance has been created with. ··· 354 261 // SAFETY: When `drop` runs, it is guaranteed that nobody is accessing the revocable data 355 262 // anymore, hence it is safe not to wait for the grace period to finish. 356 263 if unsafe { self.data().revoke_nosync() } { 357 - // We revoked `self.data` before the devres action did, hence try to remove it. 358 - if self.remove_action() { 264 + // We revoked `self.data` before devres did, hence try to remove it. 265 + if self.remove_node() { 359 266 // SAFETY: In `Self::new` we have taken an additional reference count of `self.data` 360 - // for `devm_add_action()`. Since `remove_action()` was successful, we have to drop 267 + // for `devres_node_add()`. Since `remove_node()` was successful, we have to drop 361 268 // this additional reference count. 362 - drop(unsafe { Arc::from_raw(Arc::as_ptr(&self.data)) }); 269 + drop(unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) }); 363 270 } 364 271 } 365 272 }