devres: add devm_remove_action_nowarn()
Subsystems (2)
Danilo Krummrich
maintainerPATCH v2 1/2 devres: add devm_remove_action_nowarn()
devm_remove_action() warns if the action to remove does not exist (anymore).
The Rust devres abstraction, however, has a use-case to call devm_remove_action() at a point where it can't be guaranteed that the corresponding action hasn't been released yet.
In particular, an instance of Devres<T> may be dropped after the action has been released. So far, Devres<T> worked around this by keeping the inner type alive.
Hence, add devm_remove_action_nowarn(), which returns an error code if the action has been removed already.
A subsequent patch uses devm_remove_action_nowarn() to remove the action when Devres<T> is dropped.
---
v2: clarify area of use of devm_remove_action_nowarn()
--- drivers/base/devres.c | 23 ++++++++++++++++++----- include/linux/device.h | 18 +++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-)
drivers/base/devres.c +18 -5
@@ -750,25 +750,38 @@ int __devm_add_action(struct device *dev, void (*action)(void *), void *data, coEXPORT_SYMBOL_GPL (__devm_add_action);/** -* devm_remove_action() - removes previously added custom action +* devm_remove_action_nowarn() - removes previously added custom action * @dev: Device that owns the action * @action: Function implementing the action * @data: Pointer to data passed to @action implementation * * Removes instance of @action previously added by devm_add_action(). * Both action and data should match one of the existing entries. +* +* In contrast to devm_remove_action(), this function does not WARN() if no +* entry could have been found. +* +* This should only be used if the action is contained in an object with +* independent lifetime management, e.g. the Devres rust abstraction. +* +* Causing the warning from regular driver code most likely indicates an abuse +* of the devres API. +* +* Returns: 0 on success, -ENOENT if no entry could have been found. */ -void devm_remove_action (struct device *dev,void (*action)(void *),void *data) +int devm_remove_action_nowarn (struct device *dev, +void (*action)(void *), +void *data) {struct action_devres devres = { .data = data, .action = action, }; -WARN_ON (devres_destroy (dev, devm_action_release, devm_action_match, - &devres)); +return devres_destroy (dev, devm_action_release, devm_action_match, + &devres); } -EXPORT_SYMBOL_GPL (devm_remove_action); +EXPORT_SYMBOL_GPL (devm_remove_action_nowarn);/** * devm_release_action() - release previously added custom action
include/linux/device.h +17 -1
@@ -399,7 +399,23 @@ void __iomem *devm_of_iomap(struct device *dev,#endif /* allows to add/remove a custom action to devres stack */ -void devm_remove_action (struct device *dev,void (*action)(void *),void *data); +int devm_remove_action_nowarn (struct device *dev,void (*action)(void *),void *data); + +/** +* devm_remove_action() - removes previously added custom action +* @dev: Device that owns the action +* @action: Function implementing the action +* @data: Pointer to data passed to @action implementation +* +* Removes instance of @action previously added by devm_add_action(). +* Both action and data should match one of the existing entries. +*/ +static inline +void devm_remove_action (struct device *dev,void (*action)(void *),void *data) +{ +WARN_ON (devm_remove_action_nowarn (dev, action, data)); +} +void devm_release_action (struct device *dev,void (*action)(void *),void *data);int __devm_add_action (struct device *dev,void (*action)(void *),void *data,const char *name);
base-commit: 06e843bbbf2107463249ea6f6b1a736f5647e24a
Signature · 1 line
2.47.1
Danilo Krummrich
maintainerPATCH v2 2/2 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.
---
v2:
- remove unnecessary call to revoke
- change argument of remove_action() from &Self to &Arc<Self>
--- rust/kernel/devres.rs | 47 ++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 12 deletions(-)
rust/kernel/devres.rs +35 -12
@@ -10,15 +10,19 @@ bindings, device::Device, error::{Error, Result}, + ffi::c_void, prelude::*, revocable::Revocable, sync::Arc, + types::ARef, };use core::ops::Deref ;#[pin_data] struct DevresInner <T > { + dev:ARef <Device >, + callback:unsafe extern "C" fn (*mut c_void ),#[pin] data:Revocable <T >, }
@@ -98,6 +102,8 @@ impl<T> DevresInner<T> {fn new (dev: &Device , data:T , flags:Flags ) ->Result <Arc <DevresInner <T >>> {let inner =Arc ::pin_init (pin_init! (DevresInner { + dev: dev.into (), + callback:Self ::devres_callback, data <-Revocable ::new (data), }), flags,
@@ -109,9 +115,8 @@ fn new(dev: &Device, data: T, flags: Flags) -> Result<Arc<DevresInner<T>>> {// SAFETY: `devm_add_action` guarantees to call `Self::devres_callback` once `dev` is // detached. -let ret =unsafe { - bindings::devm_add_action (dev.as_raw (),Some (Self ::devres_callback), dataas _ ) - }; +let ret = +unsafe { bindings::devm_add_action (dev.as_raw (),Some (inner.callback), dataas _ ) };if ret !=0 {// SAFETY: We just created another reference to `inner` in order to pass it to
@@ -124,6 +129,32 @@ fn new(dev: &Device, data: T, flags: Flags) -> Result<Arc<DevresInner<T>>> {Ok (inner) } +fn as_ptr (&self) -> *const Self { + selfas _ + } + +fn remove_action (this: &Arc <Self >) { +// SAFETY: +// - `self.inner.dev` is a valid `Device`, +// - the `action` and `data` pointers are the exact same ones as given to devm_add_action() +// previously, +// - `self` is always valid, even if the action has been released already. +let ret =unsafe { + bindings::devm_remove_action_nowarn ( + this.dev.as_raw (), +Some (this.callback), + this.as_ptr ()as _ , + ) + }; + +if ret ==0 { +// SAFETY: We leaked an `Arc` reference to devm_add_action() in `DevresInner::new`; if +// devm_remove_action_nowarn() was successful we can (and have to) claim back ownership +// of this reference. +let _ =unsafe {Arc ::from_raw (this.as_ptr ()) }; + } + } +#[allow(clippy::missing_safety_doc)] unsafe extern "C" fn devres_callback (ptr: *mut kernel::ffi::c_void ) {let ptr = ptras *mut DevresInner <T >;
@@ -165,14 +196,6 @@ fn deref(&self) -> &Self::Target {impl <T >Drop for Devres <T > {fn drop (&mut self) { -// Revoke the data, such that it gets dropped already and the actual resource is freed. -// -// `DevresInner` has to stay alive until the devres callback has been called. This is -// necessary since we don't know when `Devres` is dropped and calling -// `devm_remove_action()` instead could race with `devres_release_all()`. -// -// SAFETY: When `drop` runs, it's guaranteed that nobody is accessing the revocable data -// anymore, hence it is safe not to wait for the grace period to finish. -unsafe { self.revoke_nosync () }; +DevresInner ::remove_action (&self.0 ); } }
Signature · 1 line
2.47.1