kthread_work: add cancel_kthread_work[_sync]()
PATCH 1/1 kthread_work: add cancel_kthread_work[_sync]()
When an object or a subsystem quits, we need to destroy the kthread_work which is used by the object or the subsystem. We used to use flush_kthread_work(). But flush_kthread_work() has not any guarantee about the suspension of the work, this duty is pushed to the users.
So we introduce the cancel_kthread_work_sync() with a strict guarantee like cancel_work_sync() (workqueue). We also introduce cancel_kthread_work() which can be used by users on some conditions. And it is required for making the implementation of the cancel_kthread_work_sync() simpler. kthread_flush_work_fn() owns the running state of the kthread_worker and calls cancel_kthread_work() to cancel the possible requeued work.
Both cancel_kthread_work_sync() and cancel_kthread_work() share the code of flush_kthread_work() which also make the implementation simpler.
--- include/linux/kthread.h | 2 + kernel/kthread.c | 78 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 14 deletions(-)
include/linux/kthread.h +2 -0
@@ -129,6 +129,8 @@ int kthread_worker_fn(void *worker_ptr);bool queue_kthread_work (struct kthread_worker *worker,struct kthread_work *work);void flush_kthread_work (struct kthread_work *work); +void cancel_kthread_work (struct kthread_work *work); +void cancel_kthread_work_sync (struct kthread_work *work);void flush_kthread_worker (struct kthread_worker *worker);#endif /* _LINUX_KTHREAD_H */
kernel/kthread.c +64 -14
123 lines not on this page · raw message
Signature · 1 line
1.7.4.4
PATCH 1/1 kthread_work: add cancel_kthread_work[_sync]()
Hello,
Updated description and dropped DEFINE_KTHREAD_WORK_ONSTACK(). Applied to wq/for-3.17.
Thanks.
------ 8< ------
Quoted 1 line
From
95847e1bd34c0de86039408b24a05f07e788061dMon Sep 17 00:00:00 2001
From: Lai Jiangshan <[email protected]>
Date: Sat, 26 Jul 2014 12:04:00 +0800
The wait_queue_head_t kthread_work->done is unused since flush_kthread_work() has been re-implemented. Let's remove it including the initialization code. This makes DEFINE_KTHREAD_WORK_ONSTACK() unnecessary, removed.
tj: Updated description. Removed DEFINE_KTHREAD_WORK_ONSTACK().
--- include/linux/kthread.h | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-)
include/linux/kthread.h +2 -11
@@ -73,7 +73,6 @@ struct kthread_worker {struct kthread_work {struct list_head node;kthread_work_func_t func; -wait_queue_head_t done;struct kthread_worker *worker; };
@@ -85,7 +84,6 @@ struct kthread_work {#define KTHREAD_WORK_INIT (work, fn) { \ .node = LIST_HEAD_INIT((work).node), \ .func = (fn), \ - .done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done), \ }#define DEFINE_KTHREAD_WORKER (worker) \
@@ -95,22 +93,16 @@ struct kthread_work {struct kthread_work work =KTHREAD_WORK_INIT (work, fn)/* -* kthread_worker.lock and kthread_work.done need their own lockdep class -* keys if they are defined on stack with lockdep enabled. Use the -* following macros when defining them on stack. +* kthread_worker.lock needs its own lockdep class key when defined on +* stack with lockdep enabled. Use the following macros in such cases. */#ifdef CONFIG_LOCKDEP # define KTHREAD_WORKER_INIT_ONSTACK (worker) \ ({ init_kthread_worker(&worker); worker; })# define DEFINE_KTHREAD_WORKER_ONSTACK (worker) \ struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker) -# define KTHREAD_WORK_INIT_ONSTACK (work, fn) \ - ({ init_kthread_work((&work), fn); work; }) -# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) \ - struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn)#else # define DEFINE_KTHREAD_WORKER_ONSTACK (worker) DEFINE_KTHREAD_WORKER(worker) -# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) DEFINE_KTHREAD_WORK(work, fn)#endif extern void __init_kthread_worker(struct kthread_worker *worker,
@@ -127,7 +119,6 @@ extern void __init_kthread_worker(struct kthread_worker *worker,memset ((work),0 ,sizeof (struct kthread_work )); \INIT_LIST_HEAD (&(work)->node); \ (work)->func = (fn); \ -init_waitqueue_head (&(work)->done); \ }while (0 ) int kthread_worker_fn(void *worker_ptr);
Signature · 1 line
1.9.3
Quoted 4 lines · Tejun Heo
Hello,
Updated description and dropped
DEFINE_KTHREAD_WORK_ONSTACK(). Applied to wq/for-3.17.
Oops this should have been the reply to another patch. Sorry.
Signature · 1 line
tejun
Quoted 12 lines · Lai Jiangshan
When an object or a subsystem quits, we need to destroy the
kthread_workwhich is used by the object or the subsystem. We used to useflush_kthread_work(). Butflush_kthread_work()has not any guarantee about the suspension of the work, this duty is pushed to the users.So we introduce the
cancel_kthread_work_sync()with a strict guarantee likecancel_work_sync()(workqueue). We also introducecancel_kthread_work()which can be used by users on some conditions. And it is required for making the implementation of thecancel_kthread_work_sync()simpler.kthread_flush_work_fn()owns the running state of thekthread_workerand callscancel_kthread_work()to cancel the possible requeued work.
Quoted 8 lines
Both
cancel_kthread_work_sync()andcancel_kthread_work()share the code offlush_kthread_work()which also make the implementation simpler.Signed-off-by Lai Jiangshan <[email protected]>--- include/linux/kthread.h | 2 + kernel/kthread.c | 78 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 14 deletions(-)
We don't have any user. Let's not implement features which aren't used just for completeness. If you can spot and convert users, please be my guest.
Thanks.
Signature · 1 line
tejun