Skip to content
/ linux Public

Commit 0341ce5

Browse files
M-Vaittinengregkh
authored andcommitted
workqueue: Add resource managed version of delayed work init
A few drivers which need a delayed work-queue must cancel work at driver detach. Some of those implement remove() solely for this purpose. Help drivers to avoid unnecessary remove and error-branch implementation by adding managed verision of delayed work initialization. This will also help drivers to avoid mixing manual and devm based unwinding when other resources are handled by devm. Reviewed-by: Hans de Goede <hdegoede@redhat.com> Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com> Link: https://lore.kernel.org/r/51769ea4668198deb798fe47fcfb5f5288d61586.1616506559.git.matti.vaittinen@fi.rohmeurope.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 53f95c5 commit 0341ce5

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

include/linux/devm-helpers.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
#ifndef __LINUX_DEVM_HELPERS_H
3+
#define __LINUX_DEVM_HELPERS_H
4+
5+
/*
6+
* Functions which do automatically cancel operations or release resources upon
7+
* driver detach.
8+
*
9+
* These should be helpful to avoid mixing the manual and devm-based resource
10+
* management which can be source of annoying, rarely occurring,
11+
* hard-to-reproduce bugs.
12+
*
13+
* Please take into account that devm based cancellation may be performed some
14+
* time after the remove() is ran.
15+
*
16+
* Thus mixing devm and manual resource management can easily cause problems
17+
* when unwinding operations with dependencies. IRQ scheduling a work in a queue
18+
* is typical example where IRQs are often devm-managed and WQs are manually
19+
* cleaned at remove(). If IRQs are not manually freed at remove() (and this is
20+
* often the case when we use devm for IRQs) we have a period of time after
21+
* remove() - and before devm managed IRQs are freed - where new IRQ may fire
22+
* and schedule a work item which won't be cancelled because remove() was
23+
* already ran.
24+
*/
25+
26+
#include <linux/device.h>
27+
#include <linux/workqueue.h>
28+
29+
static inline void devm_delayed_work_drop(void *res)
30+
{
31+
cancel_delayed_work_sync(res);
32+
}
33+
34+
/**
35+
* devm_delayed_work_autocancel - Resource-managed work allocation
36+
* @dev: Device which lifetime work is bound to
37+
* @pdata: work to be cancelled when driver is detached
38+
*
39+
* Initialize work which is automatically cancelled when driver is detached.
40+
* A few drivers need delayed work which must be cancelled before driver
41+
* is detached to avoid accessing removed resources.
42+
* devm_delayed_work_autocancel() can be used to omit the explicit
43+
* cancelleation when driver is detached.
44+
*/
45+
static inline int devm_delayed_work_autocancel(struct device *dev,
46+
struct delayed_work *w,
47+
work_func_t worker)
48+
{
49+
INIT_DELAYED_WORK(w, worker);
50+
return devm_add_action(dev, devm_delayed_work_drop, w);
51+
}
52+
53+
#endif

0 commit comments

Comments
 (0)