2020-02-26 15:55:59

by Marc Gonzalez

[permalink] [raw]
Subject: [RFC PATCH v4 0/2] Small devm helper for devm implementations

Hello,

Differences from v3 to v4
x Add a bunch of kerneldoc above devm_add() [Greg KH]
x Split patch in two [Greg KH]

Differences from v2 to v3
x Make devm_add() return an error-code rather than the raw data pointer
(in case devres_alloc ever returns an ERR_PTR) as suggested by Geert
x Provide a variadic version devm_vadd() to work with structs as suggested
by Geert
x Don't use nested ifs in clk_devm* implementations (hopefully simpler
code logic to follow) as suggested by Geert

Marc Gonzalez (2):
devres: Provide new helper for devm functions
clk: Use devm_add in managed functions

drivers/base/devres.c | 28 +++++++++++
drivers/clk/clk-devres.c | 101 ++++++++++++++-------------------------
include/linux/device.h | 3 ++
3 files changed, 68 insertions(+), 64 deletions(-)

--
2.17.1


2020-02-26 15:56:01

by Marc Gonzalez

[permalink] [raw]
Subject: [RFC PATCH v4 1/2] devres: Provide new helper for devm functions

Provide a simple wrapper for devres_alloc / devres_add.

Signed-off-by: Marc Gonzalez <[email protected]>
---
drivers/base/devres.c | 28 ++++++++++++++++++++++++++++
include/linux/device.h | 3 +++
2 files changed, 31 insertions(+)

diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index 0bbb328bd17f..7fe6cc34411e 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -685,6 +685,34 @@ int devres_release_group(struct device *dev, void *id)
}
EXPORT_SYMBOL_GPL(devres_release_group);

+/**
+ * devm_add - allocate and register new device resource
+ * @dev: device to add resource to
+ * @func: resource release function
+ * @arg: resource data
+ * @size: resource data size
+ *
+ * Simple wrapper for devres_alloc / devres_add.
+ * Release the resource if the allocation fails.
+ *
+ * RETURNS:
+ * 0 if the allocation succeeds, -ENOMEM otherwise.
+ */
+int devm_add(struct device *dev, dr_release_t func, void *arg, size_t size)
+{
+ void *data = devres_alloc(func, size, GFP_KERNEL);
+
+ if (!data) {
+ func(dev, arg);
+ return -ENOMEM;
+ }
+
+ memcpy(data, arg, size);
+ devres_add(dev, data);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(devm_add);
+
/*
* Custom devres actions allow inserting a simple function call
* into the teadown sequence.
diff --git a/include/linux/device.h b/include/linux/device.h
index 0cd7c647c16c..55be3be9b276 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -247,6 +247,9 @@ void __iomem *devm_of_iomap(struct device *dev,
struct device_node *node, int index,
resource_size_t *size);

+int devm_add(struct device *dev, dr_release_t func, void *arg, size_t size);
+#define devm_vadd(dev, func, type, args...) \
+ devm_add(dev, func, &(struct type){args}, sizeof(struct type))
/* allows to add/remove a custom action to devres stack */
int devm_add_action(struct device *dev, void (*action)(void *), void *data);
void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
--
2.17.1

2020-02-27 13:29:19

by Geert Uytterhoeven

[permalink] [raw]
Subject: Re: [RFC PATCH v4 1/2] devres: Provide new helper for devm functions

Hi Marc,

On Wed, Feb 26, 2020 at 4:55 PM Marc Gonzalez <[email protected]> wrote:
> Provide a simple wrapper for devres_alloc / devres_add.
>
> Signed-off-by: Marc Gonzalez <[email protected]>

With the grammar fixed, as per below:
Reviewed-by: Geert Uytterhoeven <[email protected]>

> --- a/drivers/base/devres.c
> +++ b/drivers/base/devres.c
> @@ -685,6 +685,34 @@ int devres_release_group(struct device *dev, void *id)
> }
> EXPORT_SYMBOL_GPL(devres_release_group);
>
> +/**
> + * devm_add - allocate and register new device resource
> + * @dev: device to add resource to
> + * @func: resource release function
> + * @arg: resource data
> + * @size: resource data size
> + *
> + * Simple wrapper for devres_alloc / devres_add.
> + * Release the resource if the allocation fails.

Releases ... failed.

> + *
> + * RETURNS:
> + * 0 if the allocation succeeds, -ENOMEM otherwise.

"0 on success" would avoid any discussion about "succeeds" or "succeeded" ;-)

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds