Skip to content

Commit 35033fe

Browse files
pmladektorvalds
authored andcommitted
kthread: add kthread_destroy_worker()
The current kthread worker users call flush() and stop() explicitly. This function does the same plus it frees the kthread_worker struct in one call. It is supposed to be used together with kthread_create_worker*() that allocates struct kthread_worker. Link: http://lkml.kernel.org/r/1470754545-17632-7-git-send-email-pmladek@suse.com Signed-off-by: Petr Mladek <pmladek@suse.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Tejun Heo <tj@kernel.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> Cc: Josh Triplett <josh@joshtriplett.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Jiri Kosina <jkosina@suse.cz> Cc: Borislav Petkov <bp@suse.de> Cc: Michal Hocko <mhocko@suse.cz> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fbae2d4 commit 35033fe

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

include/linux/kthread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,6 @@ bool kthread_queue_work(struct kthread_worker *worker,
136136
void kthread_flush_work(struct kthread_work *work);
137137
void kthread_flush_worker(struct kthread_worker *worker);
138138

139+
void kthread_destroy_worker(struct kthread_worker *worker);
140+
139141
#endif /* _LINUX_KTHREAD_H */

kernel/kthread.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,26 @@ void kthread_flush_worker(struct kthread_worker *worker)
823823
wait_for_completion(&fwork.done);
824824
}
825825
EXPORT_SYMBOL_GPL(kthread_flush_worker);
826+
827+
/**
828+
* kthread_destroy_worker - destroy a kthread worker
829+
* @worker: worker to be destroyed
830+
*
831+
* Flush and destroy @worker. The simple flush is enough because the kthread
832+
* worker API is used only in trivial scenarios. There are no multi-step state
833+
* machines needed.
834+
*/
835+
void kthread_destroy_worker(struct kthread_worker *worker)
836+
{
837+
struct task_struct *task;
838+
839+
task = worker->task;
840+
if (WARN_ON(!task))
841+
return;
842+
843+
kthread_flush_worker(worker);
844+
kthread_stop(task);
845+
WARN_ON(!list_empty(&worker->work_list));
846+
kfree(worker);
847+
}
848+
EXPORT_SYMBOL(kthread_destroy_worker);

0 commit comments

Comments
 (0)