public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC/PATCH] devres: allow adding custom actions to the stack
@ 2013-02-24  4:05 Dmitry Torokhov
  2013-02-25 23:48 ` Tejun Heo
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Torokhov @ 2013-02-24  4:05 UTC (permalink / raw)
  To: Tejun Heo; +Cc: Greg Kroah-Hartman, linux-kernel

Sometimes drivers need to execute one-off actions in their error handling
or device teardown paths. An example would be toggling a GPIO line to
reset the controlled device into predefined state.

To allow performing such actions when using managed resources let's allow
adding them to stack/group of devres resources.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/base/devres.c  | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/device.h |  4 +++
 2 files changed, 78 insertions(+)

diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index 6683906..507379e 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -671,6 +671,80 @@ int devres_release_group(struct device *dev, void *id)
 EXPORT_SYMBOL_GPL(devres_release_group);
 
 /*
+ * Custom devres actions allow inserting a simple function call
+ * into the teadown sequence.
+ */
+
+struct action_devres {
+	void *data;
+	void (*action)(void *);
+};
+
+static int devm_action_match(struct device *dev, void *res, void *p)
+{
+	struct action_devres *devres = res;
+	struct action_devres *target = p;
+
+	return devres->action == target->action &&
+	       devres->data == target->data;
+}
+
+static void devm_action_release(struct device *dev, void *res)
+{
+	struct action_devres *devres = res;
+
+	devres->action(devres->data);
+}
+
+/**
+ * devm_add_action() - add a custom action to list of managed resources
+ * @dev: Device that owns the action
+ * @action: Function that should be called
+ * @data: Pointer to data passed to @action implementation
+ *
+ * This adds a custom action to the list of managed resources so that
+ * it gets executed as part of standard resource unwinding.
+ */
+int devm_add_action(struct device *dev, void (*action)(void *), void *data)
+{
+	struct action_devres *devres;
+
+	devres = devres_alloc(devm_action_release,
+			      sizeof(struct action_devres), GFP_KERNEL);
+	if (!devres)
+		return -ENOMEM;
+
+	devres->data = data;
+	devres->action = action;
+
+	devres_add(dev, devres);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_add_action);
+
+/**
+ * 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.
+ */
+void devm_remove_action(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));
+
+}
+EXPORT_SYMBOL_GPL(devm_remove_action);
+
+/*
  * Managed kzalloc/kfree
  */
 static void devm_kzalloc_release(struct device *dev, void *res)
diff --git a/include/linux/device.h b/include/linux/device.h
index 2fbf088..ba7c802 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -578,6 +578,10 @@ extern void devm_kfree(struct device *dev, void *p);
 void __iomem *devm_request_and_ioremap(struct device *dev,
 			struct resource *res);
 
+/* 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);
+
 struct device_dma_parameters {
 	/*
 	 * a low level driver may set these to teach IOMMU code about
-- 
1.7.11.7


-- 
Dmitry

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [RFC/PATCH] devres: allow adding custom actions to the stack
  2013-02-24  4:05 [RFC/PATCH] devres: allow adding custom actions to the stack Dmitry Torokhov
@ 2013-02-25 23:48 ` Tejun Heo
  2013-02-26  0:11   ` Dmitry Torokhov
  0 siblings, 1 reply; 4+ messages in thread
From: Tejun Heo @ 2013-02-25 23:48 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: Greg Kroah-Hartman, linux-kernel

On Sat, Feb 23, 2013 at 08:05:57PM -0800, Dmitry Torokhov wrote:
> Sometimes drivers need to execute one-off actions in their error handling
> or device teardown paths. An example would be toggling a GPIO line to
> reset the controlled device into predefined state.
> 
> To allow performing such actions when using managed resources let's allow
> adding them to stack/group of devres resources.
> 
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

Acked-by: Tejun Heo <tj@kernel.org>

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC/PATCH] devres: allow adding custom actions to the stack
  2013-02-25 23:48 ` Tejun Heo
@ 2013-02-26  0:11   ` Dmitry Torokhov
  2013-02-26  0:32     ` Greg Kroah-Hartman
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Torokhov @ 2013-02-26  0:11 UTC (permalink / raw)
  To: Tejun Heo, Greg Kroah-Hartman; +Cc: linux-kernel

On Mon, Feb 25, 2013 at 03:48:01PM -0800, Tejun Heo wrote:
> On Sat, Feb 23, 2013 at 08:05:57PM -0800, Dmitry Torokhov wrote:
> > Sometimes drivers need to execute one-off actions in their error handling
> > or device teardown paths. An example would be toggling a GPIO line to
> > reset the controlled device into predefined state.
> > 
> > To allow performing such actions when using managed resources let's allow
> > adding them to stack/group of devres resources.
> > 
> > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> 
> Acked-by: Tejun Heo <tj@kernel.org>
> 

Thanks Tejun.

Greg, if possible I'd like to carry this in my tree as I have driver
changes depending on this.

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC/PATCH] devres: allow adding custom actions to the stack
  2013-02-26  0:11   ` Dmitry Torokhov
@ 2013-02-26  0:32     ` Greg Kroah-Hartman
  0 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2013-02-26  0:32 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: Tejun Heo, linux-kernel

On Mon, Feb 25, 2013 at 04:11:51PM -0800, Dmitry Torokhov wrote:
> On Mon, Feb 25, 2013 at 03:48:01PM -0800, Tejun Heo wrote:
> > On Sat, Feb 23, 2013 at 08:05:57PM -0800, Dmitry Torokhov wrote:
> > > Sometimes drivers need to execute one-off actions in their error handling
> > > or device teardown paths. An example would be toggling a GPIO line to
> > > reset the controlled device into predefined state.
> > > 
> > > To allow performing such actions when using managed resources let's allow
> > > adding them to stack/group of devres resources.
> > > 
> > > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > 
> > Acked-by: Tejun Heo <tj@kernel.org>
> > 
> 
> Thanks Tejun.
> 
> Greg, if possible I'd like to carry this in my tree as I have driver
> changes depending on this.

No objection from me:

Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-02-26  0:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-24  4:05 [RFC/PATCH] devres: allow adding custom actions to the stack Dmitry Torokhov
2013-02-25 23:48 ` Tejun Heo
2013-02-26  0:11   ` Dmitry Torokhov
2013-02-26  0:32     ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox