gpiolib: finish conversion to devm_*_action*() APIs
Subsystems (2)
cover gpiolib: finish conversion to devm_*_action*() APIs
GPIOLIB has some open coded stuff that can be folded to the devm_*_action*() calls. This mini-series is for that. The necessary prerequisites are here as well, namely:
1) moving the respective APIs to the devres.h;
2) adding a couple of simple helpers that GPIOLIB will rely on;
3) finishing the GPIOLIB conversion to the device managed action APIs.
The series is based on another series that's available via immutable tag devres-iio-input-pinctrl-v6.15 [1]. The idea is to route this via GPIOLIB tree (or Intel GPIO for the starter) with an immutable tag for the device core and others if needed. Please, review and acknowledge.
Andy Shevchenko (4):
devres: Move devm_*_action*() APIs to devres.h
devres: Add devm_is_action_added() helper
devres: Add devm_remove_action_optional() helper
gpiolib: devres: Finish the conversion to use devm_add_action()
drivers/base/devres.c | 11 ++++ drivers/gpio/gpiolib-devres.c | 94 ++++++++++------------------------- include/linux/device.h | 38 -------------- include/linux/device/devres.h | 54 ++++++++++++++++++++ 4 files changed, 90 insertions(+), 107 deletions(-)
base-commit: 9deb15de8ca27cf9cba0d2bac53bbe37c836591b
Signature · 1 line
2.45.1.3035.g276e886db78b
PATCH 1/4 devres: Move devm_*_action*() APIs to devres.h
We have a newly created header linux/device/devres.h that gathers device managed APIs, so users won't need to include entire device.h for only these ones. Move devm_*_action*() APIs to devres.h as well.
--- include/linux/device.h | 38 ---------------------------------- include/linux/device/devres.h | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 38 deletions(-)
include/linux/device.h +0 -38
@@ -281,44 +281,6 @@ int __must_check device_create_bin_file(struct device *dev,void device_remove_bin_file (struct device *dev,const struct bin_attribute *attr); -/* allows to add/remove a custom action to devres stack */ -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); -#define devm_add_action (dev, action, data) \ - __devm_add_action(dev, action, data, #action) - -static inline int __devm_add_action_or_reset (struct device *dev,void (*action)(void *), -void *data,const char *name) -{ -int ret; - - ret =__devm_add_action (dev, action, data, name); -if (ret) -action (data); - -return ret; -} -#define devm_add_action_or_reset (dev, action, data) \ - __devm_add_action_or_reset(dev, action, data, #action) -/** * devm_alloc_percpu - Resource-managed alloc_percpu * @dev: Device to allocate per-cpu memory for
include/linux/device/devres.h +39 -0
@@ -8,6 +8,7 @@#include <linux/overflow.h> #include <linux/stdarg.h> #include <linux/types.h> +#include <asm/bug.h> struct device ;struct device_node ;
@@ -126,4 +127,42 @@ void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int in#endif +/* allows to add/remove a custom action to devres stack */ +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); +#define devm_add_action (dev, action, data) \ + __devm_add_action(dev, action, data, #action) + +static inline int __devm_add_action_or_reset (struct device *dev,void (*action)(void *), +void *data,const char *name) +{ +int ret; + + ret =__devm_add_action (dev, action, data, name); +if (ret) +action (data); + +return ret; +} +#define devm_add_action_or_reset (dev, action, data) \ + __devm_add_action_or_reset(dev, action, data, #action) +#endif /* _DEVICE_DEVRES_H_ */
Signature · 1 line
2.45.1.3035.g276e886db78b
Re: [PATCH v1 1/4] devres: Move devm_*_action*() APIs to devres.h
Quoted 5 lines · Andy Shevchenko
We have a newly created header
linux/device/devres.hthat gathers device managed APIs, so users won't need to include entiredevice.hfor only these ones. Move devm_*_action*() APIs todevres.has well.Signed-off-by Andy Shevchenko <[email protected]>
PATCH 2/4 devres: Add devm_is_action_added() helper
In reply to Andy ShevchenkoIn some code we would like to know if the action in device managed resources was added by devm_add_action() family of calls. Introduce a helper for that.
--- drivers/base/devres.c | 11 +++++++++++ include/linux/device/devres.h | 2 ++ 2 files changed, 13 insertions(+)
drivers/base/devres.c +11 -0
@@ -749,6 +749,17 @@ int __devm_add_action(struct device *dev, void (*action)(void *), void *data, co }EXPORT_SYMBOL_GPL (__devm_add_action); +bool devm_is_action_added (struct device *dev,void (*action)(void *),void *data) +{ +struct action_devres devres = { + .data = data, + .action = action, + }; + +return devres_find (dev, devm_action_release, devm_action_match, &devres); +} +EXPORT_SYMBOL_GPL (devm_is_action_added); +/** * devm_remove_action_nowarn() - removes previously added custom action * @dev: Device that owns the action
include/linux/device/devres.h +2 -0
@@ -165,4 +165,6 @@ static inline int __devm_add_action_or_reset(struct device *dev, void (*action)(#define devm_add_action_or_reset (dev, action, data) \ __devm_add_action_or_reset(dev, action, data, #action) +bool devm_is_action_added (struct device *dev,void (*action)(void *),void *data); +#endif /* _DEVICE_DEVRES_H_ */
Signature · 1 line
2.45.1.3035.g276e886db78b
Re: [PATCH v1 2/4] devres: Add devm_is_action_added() helper
Quoted 4 lines · Andy Shevchenko
In some code we would like to know if the action in device managed resources was added by
devm_add_action()family of calls. Introduce a helper for that.Signed-off-by Andy Shevchenko <[email protected]>
PATCH 3/4 devres: Add devm_remove_action_optional() helper
In reply to Andy ShevchenkoAdd a new helper to remove an action that was added via devm_add_action() family of calls, but not warn in the cases where action wasn't found since it is optional and wasn't even added.
--- include/linux/device/devres.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/linux/device/devres.h +13 -0
@@ -145,6 +145,19 @@ void devm_remove_action(struct device *dev, void (*action)(void *), void *data)WARN_ON (devm_remove_action_nowarn (dev, action, data)); } +/* Same as devm_remove_action(), but doesn't WARN() if action wasn't added before */ +static inline +void devm_remove_action_optional (struct device *dev,void (*action)(void *),void *data) +{ +int ret; + + ret =devm_remove_action_nowarn (dev, action, data); +if (ret == -ENOENT ) +return ; + +WARN_ON (ret); +} +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);
Signature · 1 line
2.45.1.3035.g276e886db78b
Re: [PATCH v1 3/4] devres: Add devm_remove_action_optional() helper
Quoted 19 lines · Andy Shevchenko
Add a new helper to remove an action that was added via
devm_add_action()family of calls, but not warn in the cases where action wasn't found since it is optional and wasn't even added.Signed-off-by Andy Shevchenko <[email protected]>--- include/linux/device/devres.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)include/linux/device/devres.h +2
-0@@ -145,6 +145,19 @@ void devm_remove_action(struct device *dev, void (*action)(void *), void *data)WARN_ON (devm_remove_action_nowarn (dev, action, data)); } +/* Same as devm_remove_action(), but doesn't WARN() if action wasn't added before */ +static inline
include/linux/device/devres.h void devm_remove_action(struct device *dev, void (*action)(void *), void *data) · line 159
Quoted 10 lines
include/linux/device/devres.h +10
-0+void devm_remove_action_optional (struct device *dev,void (*action)(void *),void *data) +{ +int ret; + + ret =devm_remove_action_nowarn (dev, action, data); +if (ret == -ENOENT ) +return ; + +WARN_ON (ret); +}
Trying to wrap my head around this one, can't the user simply do
if (devm_is_action_added()) devm_remove_action/_nowarn();
Raag
Re: [PATCH v1 3/4] devres: Add devm_remove_action_optional() helper
Quoted 1 line · Raag Jadav
On Thu, Feb 20, 2025 at 03:44:59PM +0200, Andy Shevchenko wrote:
...
Quoted 15 lines
include/linux/device/devres.h void devm_remove_action(struct device *dev, void (*action)(void *), void *data) · line 159
Quoted 12 lines
include/linux/device/devres.h +12
-0+/* Same as devm_remove_action(), but doesn't WARN() if action wasn't added before */ +static inline +void devm_remove_action_optional (struct device *dev,void (*action)(void *),void *data) +{ +int ret; + + ret =devm_remove_action_nowarn (dev, action, data); +if (ret == -ENOENT ) +return ; + +WARN_ON (ret); +}Trying to wrap my head around this one, can't the user simply do
Quoted 2 lines
if (devm_is_action_added()) devm_remove_action/_nowarn();
Hmm... Actually it sounds like a good point. I will check (and I like the idea of dropping this patch).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 3/4] devres: Add devm_remove_action_optional() helper
Quoted 20 lines · Andy Shevchenko
Quoted 1 line · Raag Jadav
On Thu, Feb 20, 2025 at 03:44:59PM +0200, Andy Shevchenko wrote:
...
Quoted 15 lines
include/linux/device/devres.h void devm_remove_action(struct device *dev, void (*action)(void *), void *data) · line 159
Quoted 12 lines
include/linux/device/devres.h +12
-0+/* Same as devm_remove_action(), but doesn't WARN() if action wasn't added before */ +static inline +void devm_remove_action_optional (struct device *dev,void (*action)(void *),void *data) +{ +int ret; + + ret =devm_remove_action_nowarn (dev, action, data); +if (ret == -ENOENT ) +return ; + +WARN_ON (ret); +}Trying to wrap my head around this one, can't the user simply do
Quoted 5 lines
Quoted 2 lines
if (devm_is_action_added()) devm_remove_action/_nowarn();Hmm... Actually it sounds like a good point. I will check (and I like the idea of dropping this patch).
And perhaps
s/devm_is_action_added/devm_action_is_added
But whichever you think _is best_ ;)
Raag
Re: [PATCH v1 3/4] devres: Add devm_remove_action_optional() helper
Quoted 3 lines · Raag Jadav
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Raag Jadav
On Thu, Feb 20, 2025 at 03:44:59PM +0200, Andy Shevchenko wrote:
...
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
include/linux/device/devres.h void devm_remove_action(struct device *dev, void (*action)(void *), void *data) · line 159
Quoted 12 lines
include/linux/device/devres.h +12
-0+/* Same as devm_remove_action(), but doesn't WARN() if action wasn't added before */ +static inline +void devm_remove_action_optional (struct device *dev,void (*action)(void *),void *data) +{ +int ret; + + ret =devm_remove_action_nowarn (dev, action, data); +if (ret == -ENOENT ) +return ; + +WARN_ON (ret); +}Trying to wrap my head around this one, can't the user simply do
if (devm_is_action_added()) devm_remove_action/_nowarn();
Quoted 8 lines
Quoted 2 lines
Hmm... Actually it sounds like a good point. I will check (and I like the idea of dropping this patch).
And perhaps
s/devm_is_action_added/devm_action_is_added
But whichever you think
_isbest_;)
I thought about that and that's why I would like to stick to the my variant.
Thanks for the review!
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH 4/4 gpiolib: devres: Finish the conversion to use devm_add_action()
In reply to Andy ShevchenkoWith new recently added helpers we can complete the conversion of the gpiolib code to use devm_add_action() in all suitable cases. So do this.
--- drivers/gpio/gpiolib-devres.c | 94 ++++++++++------------------------- 1 file changed, 25 insertions(+), 69 deletions(-)
drivers/gpio/gpiolib-devres.c +25 -69
@@ -19,32 +19,14 @@struct fwnode_handle ;struct lock_class_key ; -static void devm_gpiod_release (struct device *dev,void *res) +static void devm_gpiod_release (void *desc) { -struct gpio_desc **desc = res; - -gpiod_put (*desc); +gpiod_put (desc); } -static int devm_gpiod_match (struct device *dev,void *res,void *data) +static void devm_gpiod_release_array (void *descs) { -struct gpio_desc **this = res, **gpio = data; - -return *this == *gpio; -} - -static void devm_gpiod_release_array (struct device *dev,void *res) -{ -struct gpio_descs **descs = res; - -gpiod_put_array (*descs); -} - -static int devm_gpiod_match_array (struct device *dev,void *res,void *data) -{ -struct gpio_descs **this = res, **gpios = data; - -return *this == *gpios; +gpiod_put_array (descs); }/**
@@ -114,8 +96,8 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,unsigned int idx, enum gpiod_flags flags) { - struct gpio_desc **dr; struct gpio_desc *desc; +int ret; desc =gpiod_get_index (dev, con_id, idx, flags);if (IS_ERR (desc))
@@ -126,23 +108,16 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,* already under resource management by this device. */ if (flags &GPIOD_FLAGS_BIT_NONEXCLUSIVE ) { -struct devres *dres; +bool dres; - dres =devres_find (dev, devm_gpiod_release, - devm_gpiod_match, &desc); + dres =devm_is_action_added (dev, devm_gpiod_release, desc);if (dres)return desc; } - dr =devres_alloc (devm_gpiod_release,sizeof (struct gpio_desc *), -GFP_KERNEL ); -if (!dr) { -gpiod_put (desc); -return ERR_PTR (-ENOMEM ); - } - - *dr = desc; -devres_add (dev, dr); + ret =devm_add_action_or_reset (dev, devm_gpiod_release, desc); +if (ret) +return ERR_PTR (ret);return desc; }
@@ -171,22 +146,16 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,enum gpiod_flags flags, const char *label) { - struct gpio_desc **dr; struct gpio_desc *desc; - - dr =devres_alloc (devm_gpiod_release,sizeof (struct gpio_desc *), -GFP_KERNEL ); -if (!dr) -return ERR_PTR (-ENOMEM ); +int ret; desc =gpiod_find_and_request (dev, fwnode, con_id, index, flags, label, false); -if (IS_ERR (desc)) { -devres_free (dr); +if (IS_ERR (desc))return desc; - } - *dr = desc; -devres_add (dev, dr); + ret =devm_add_action_or_reset (dev, devm_gpiod_release, desc); +if (ret) +return ERR_PTR (ret);return desc; }
@@ -244,22 +213,16 @@ struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,const char *con_id, enum gpiod_flags flags) { - struct gpio_descs **dr; struct gpio_descs *descs; - - dr =devres_alloc (devm_gpiod_release_array, -sizeof (struct gpio_descs *),GFP_KERNEL ); -if (!dr) -return ERR_PTR (-ENOMEM ); +int ret; descs =gpiod_get_array (dev, con_id, flags); -if (IS_ERR (descs)) { -devres_free (dr); +if (IS_ERR (descs))return descs; - } - *dr = descs; -devres_add (dev, dr); + ret =devm_add_action_or_reset (dev, devm_gpiod_release_array, descs); +if (ret) +return ERR_PTR (ret);return descs; }
@@ -307,8 +270,7 @@ EXPORT_SYMBOL_GPL(devm_gpiod_get_array_optional);*/ void devm_gpiod_put (struct device *dev,struct gpio_desc *desc) { -WARN_ON (devres_release (dev, devm_gpiod_release, devm_gpiod_match, - &desc)); +devm_release_action (dev, devm_gpiod_release, desc); }EXPORT_SYMBOL_GPL (devm_gpiod_put);
@@ -324,21 +286,16 @@ EXPORT_SYMBOL_GPL(devm_gpiod_put);void devm_gpiod_unhinge (struct device *dev,struct gpio_desc *desc) { -int ret; -if (IS_ERR_OR_NULL (desc))return ; - ret =devres_destroy (dev, devm_gpiod_release, - devm_gpiod_match, &desc); +/* * If the GPIO descriptor is requested as nonexclusive, we * may call this function several times on the same descriptor * so it is OK if devres_destroy() returns -ENOENT. +* Anything else we should warn about. */ -if (ret == -ENOENT ) -return ; -/* Anything else we should warn about */ -WARN_ON (ret); +devm_remove_action_optional (dev, devm_gpiod_release, desc); }EXPORT_SYMBOL_GPL (devm_gpiod_unhinge);
@@ -353,8 +310,7 @@ EXPORT_SYMBOL_GPL(devm_gpiod_unhinge);*/ void devm_gpiod_put_array (struct device *dev,struct gpio_descs *descs) { -WARN_ON (devres_release (dev, devm_gpiod_release_array, - devm_gpiod_match_array, &descs)); +devm_remove_action (dev, devm_gpiod_release_array, descs); }EXPORT_SYMBOL_GPL (devm_gpiod_put_array);
Signature · 1 line
2.45.1.3035.g276e886db78b
Quoted 11 lines · Andy Shevchenko
GPIOLIB has some open coded stuff that can be folded to the devm_*_action*() calls. This mini-series is for that. The necessary prerequisites are here as well, namely:
1) moving the respective APIs to thedevres.h;
2) adding a couple of simple helpers that GPIOLIB will rely on;
3) finishing the GPIOLIB conversion to the device managed action APIs.The series is based on another series that's available via immutable tag devres-iio-input-pinctrl-v6.15 [1]. The idea is to route this via GPIOLIB tree (or Intel GPIO for the starter) with an immutable tag for the device core and others if needed. Please, review and acknowledge.
Please, don't waste time on this, I have just sent a v2 which is simpler:
https://lore.kernel.org/r/[email protected]
Signature · 2 lines
With Best Regards, Andy Shevchenko