1 /* SPDX-License-Identifier: GPL-2.0-only */ 1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 #ifndef __LINUX_DEVM_HELPERS_H 2 #ifndef __LINUX_DEVM_HELPERS_H 3 #define __LINUX_DEVM_HELPERS_H 3 #define __LINUX_DEVM_HELPERS_H 4 4 5 /* 5 /* 6 * Functions which do automatically cancel ope 6 * Functions which do automatically cancel operations or release resources upon 7 * driver detach. 7 * driver detach. 8 * 8 * 9 * These should be helpful to avoid mixing the 9 * These should be helpful to avoid mixing the manual and devm-based resource 10 * management which can be source of annoying, 10 * management which can be source of annoying, rarely occurring, 11 * hard-to-reproduce bugs. 11 * hard-to-reproduce bugs. 12 * 12 * 13 * Please take into account that devm based ca 13 * Please take into account that devm based cancellation may be performed some 14 * time after the remove() is ran. 14 * time after the remove() is ran. 15 * 15 * 16 * Thus mixing devm and manual resource manage 16 * Thus mixing devm and manual resource management can easily cause problems 17 * when unwinding operations with dependencies 17 * when unwinding operations with dependencies. IRQ scheduling a work in a queue 18 * is typical example where IRQs are often dev 18 * is typical example where IRQs are often devm-managed and WQs are manually 19 * cleaned at remove(). If IRQs are not manual 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) w 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 21 * remove() - and before devm managed IRQs are freed - where new IRQ may fire 22 * and schedule a work item which won't be can 22 * and schedule a work item which won't be cancelled because remove() was 23 * already ran. 23 * already ran. 24 */ 24 */ 25 25 26 #include <linux/device.h> 26 #include <linux/device.h> 27 #include <linux/workqueue.h> 27 #include <linux/workqueue.h> 28 28 29 static inline void devm_delayed_work_drop(void 29 static inline void devm_delayed_work_drop(void *res) 30 { 30 { 31 cancel_delayed_work_sync(res); 31 cancel_delayed_work_sync(res); 32 } 32 } 33 33 34 /** 34 /** 35 * devm_delayed_work_autocancel - Resource-man 35 * devm_delayed_work_autocancel - Resource-managed delayed work allocation 36 * @dev: Device which lifetime work is 36 * @dev: Device which lifetime work is bound to 37 * @w: Work item to be queued 37 * @w: Work item to be queued 38 * @worker: Worker function 38 * @worker: Worker function 39 * 39 * 40 * Initialize delayed work which is automatica 40 * Initialize delayed work which is automatically cancelled when driver is 41 * detached. A few drivers need delayed work w 41 * detached. A few drivers need delayed work which must be cancelled before 42 * driver is detached to avoid accessing remov 42 * driver is detached to avoid accessing removed resources. 43 * devm_delayed_work_autocancel() can be used 43 * devm_delayed_work_autocancel() can be used to omit the explicit 44 * cancellation when driver is detached. !! 44 * cancelleation when driver is detached. 45 */ 45 */ 46 static inline int devm_delayed_work_autocancel 46 static inline int devm_delayed_work_autocancel(struct device *dev, 47 47 struct delayed_work *w, 48 48 work_func_t worker) 49 { 49 { 50 INIT_DELAYED_WORK(w, worker); 50 INIT_DELAYED_WORK(w, worker); 51 return devm_add_action(dev, devm_delay 51 return devm_add_action(dev, devm_delayed_work_drop, w); 52 } << 53 << 54 static inline void devm_work_drop(void *res) << 55 { << 56 cancel_work_sync(res); << 57 } << 58 << 59 /** << 60 * devm_work_autocancel - Resource-managed wor << 61 * @dev: Device which lifetime work is << 62 * @w: Work to be added (and automati << 63 * @worker: Worker function << 64 * << 65 * Initialize work which is automatically canc << 66 * A few drivers need to queue work which must << 67 * is detached to avoid accessing removed reso << 68 * devm_work_autocancel() can be used to omit << 69 * cancellation when driver is detached. << 70 */ << 71 static inline int devm_work_autocancel(struct << 72 struct << 73 work_fu << 74 { << 75 INIT_WORK(w, worker); << 76 return devm_add_action(dev, devm_work_ << 77 } 52 } 78 53 79 #endif 54 #endif 80 55
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.