aboutsummaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
authorPetr Mladek <pmladek@suse.com>2016-10-11 16:55:43 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-10-11 18:06:33 -0400
commit37be45d49dec2a411e29d50c9597cfe8184b5645 (patch)
tree4c15dc2b6a78ee47256e57af9a2aa672c344f751 /kernel
parent22597dc3d97b1ead2aca201397415a1a84bf2b26 (diff)
kthread: allow to cancel kthread work
We are going to use kthread workers more widely and sometimes we will need to make sure that the work is neither pending nor running. This patch implements cancel_*_sync() operations as inspired by workqueues. Well, we are synchronized against the other operations via the worker lock, we use del_timer_sync() and a counter to count parallel cancel operations. Therefore the implementation might be easier. First, we check if a worker is assigned. If not, the work has newer been queued after it was initialized. Second, we take the worker lock. It must be the right one. The work must not be assigned to another worker unless it is initialized in between. Third, we try to cancel the timer when it exists. The timer is deleted synchronously to make sure that the timer call back is not running. We need to temporary release the worker->lock to avoid a possible deadlock with the callback. In the meantime, we set work->canceling counter to avoid any queuing. Fourth, we try to remove the work from a worker list. It might be the list of either normal or delayed works. Fifth, if the work is running, we call kthread_flush_work(). It might take an arbitrary time. We need to release the worker-lock again. In the meantime, we again block any queuing by the canceling counter. As already mentioned, the check for a pending kthread work is done under a lock. In compare with workqueues, we do not need to fight for a single PENDING bit to block other operations. Therefore we do not suffer from the thundering storm problem and all parallel canceling jobs might use kthread_flush_work(). Any queuing is blocked until the counter gets zero. Link: http://lkml.kernel.org/r/1470754545-17632-10-git-send-email-pmladek@suse.com Signed-off-by: Petr Mladek <pmladek@suse.com> Acked-by: Tejun Heo <tj@kernel.org> Cc: Oleg Nesterov <oleg@redhat.com> 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>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/kthread.c132
1 files changed, 130 insertions, 2 deletions
diff --git a/kernel/kthread.c b/kernel/kthread.c
index a5c6d709349d..20505c632bc3 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -718,6 +718,19 @@ kthread_create_worker_on_cpu(int cpu, const char namefmt[], ...)
718} 718}
719EXPORT_SYMBOL(kthread_create_worker_on_cpu); 719EXPORT_SYMBOL(kthread_create_worker_on_cpu);
720 720
721/*
722 * Returns true when the work could not be queued at the moment.
723 * It happens when it is already pending in a worker list
724 * or when it is being cancelled.
725 */
726static inline bool queuing_blocked(struct kthread_worker *worker,
727 struct kthread_work *work)
728{
729 lockdep_assert_held(&worker->lock);
730
731 return !list_empty(&work->node) || work->canceling;
732}
733
721static void kthread_insert_work_sanity_check(struct kthread_worker *worker, 734static void kthread_insert_work_sanity_check(struct kthread_worker *worker,
722 struct kthread_work *work) 735 struct kthread_work *work)
723{ 736{
@@ -759,7 +772,7 @@ bool kthread_queue_work(struct kthread_worker *worker,
759 unsigned long flags; 772 unsigned long flags;
760 773
761 spin_lock_irqsave(&worker->lock, flags); 774 spin_lock_irqsave(&worker->lock, flags);
762 if (list_empty(&work->node)) { 775 if (!queuing_blocked(worker, work)) {
763 kthread_insert_work(worker, work, &worker->work_list); 776 kthread_insert_work(worker, work, &worker->work_list);
764 ret = true; 777 ret = true;
765 } 778 }
@@ -859,7 +872,7 @@ bool kthread_queue_delayed_work(struct kthread_worker *worker,
859 872
860 spin_lock_irqsave(&worker->lock, flags); 873 spin_lock_irqsave(&worker->lock, flags);
861 874
862 if (list_empty(&work->node)) { 875 if (!queuing_blocked(worker, work)) {
863 __kthread_queue_delayed_work(worker, dwork, delay); 876 __kthread_queue_delayed_work(worker, dwork, delay);
864 ret = true; 877 ret = true;
865 } 878 }
@@ -919,6 +932,121 @@ void kthread_flush_work(struct kthread_work *work)
919} 932}
920EXPORT_SYMBOL_GPL(kthread_flush_work); 933EXPORT_SYMBOL_GPL(kthread_flush_work);
921 934
935/*
936 * This function removes the work from the worker queue. Also it makes sure
937 * that it won't get queued later via the delayed work's timer.
938 *
939 * The work might still be in use when this function finishes. See the
940 * current_work proceed by the worker.
941 *
942 * Return: %true if @work was pending and successfully canceled,
943 * %false if @work was not pending
944 */
945static bool __kthread_cancel_work(struct kthread_work *work, bool is_dwork,
946 unsigned long *flags)
947{
948 /* Try to cancel the timer if exists. */
949 if (is_dwork) {
950 struct kthread_delayed_work *dwork =
951 container_of(work, struct kthread_delayed_work, work);
952 struct kthread_worker *worker = work->worker;
953
954 /*
955 * del_timer_sync() must be called to make sure that the timer
956 * callback is not running. The lock must be temporary released
957 * to avoid a deadlock with the callback. In the meantime,
958 * any queuing is blocked by setting the canceling counter.
959 */
960 work->canceling++;
961 spin_unlock_irqrestore(&worker->lock, *flags);
962 del_timer_sync(&dwork->timer);
963 spin_lock_irqsave(&worker->lock, *flags);
964 work->canceling--;
965 }
966
967 /*
968 * Try to remove the work from a worker list. It might either
969 * be from worker->work_list or from worker->delayed_work_list.
970 */
971 if (!list_empty(&work->node)) {
972 list_del_init(&work->node);
973 return true;
974 }
975
976 return false;
977}
978
979static bool __kthread_cancel_work_sync(struct kthread_work *work, bool is_dwork)
980{
981 struct kthread_worker *worker = work->worker;
982 unsigned long flags;
983 int ret = false;
984
985 if (!worker)
986 goto out;
987
988 spin_lock_irqsave(&worker->lock, flags);
989 /* Work must not be used with >1 worker, see kthread_queue_work(). */
990 WARN_ON_ONCE(work->worker != worker);
991
992 ret = __kthread_cancel_work(work, is_dwork, &flags);
993
994 if (worker->current_work != work)
995 goto out_fast;
996
997 /*
998 * The work is in progress and we need to wait with the lock released.
999 * In the meantime, block any queuing by setting the canceling counter.
1000 */
1001 work->canceling++;
1002 spin_unlock_irqrestore(&worker->lock, flags);
1003 kthread_flush_work(work);
1004 spin_lock_irqsave(&worker->lock, flags);
1005 work->canceling--;
1006
1007out_fast:
1008 spin_unlock_irqrestore(&worker->lock, flags);
1009out:
1010 return ret;
1011}
1012
1013/**
1014 * kthread_cancel_work_sync - cancel a kthread work and wait for it to finish
1015 * @work: the kthread work to cancel
1016 *
1017 * Cancel @work and wait for its execution to finish. This function
1018 * can be used even if the work re-queues itself. On return from this
1019 * function, @work is guaranteed to be not pending or executing on any CPU.
1020 *
1021 * kthread_cancel_work_sync(&delayed_work->work) must not be used for
1022 * delayed_work's. Use kthread_cancel_delayed_work_sync() instead.
1023 *
1024 * The caller must ensure that the worker on which @work was last
1025 * queued can't be destroyed before this function returns.
1026 *
1027 * Return: %true if @work was pending, %false otherwise.
1028 */
1029bool kthread_cancel_work_sync(struct kthread_work *work)
1030{
1031 return __kthread_cancel_work_sync(work, false);
1032}
1033EXPORT_SYMBOL_GPL(kthread_cancel_work_sync);
1034
1035/**
1036 * kthread_cancel_delayed_work_sync - cancel a kthread delayed work and
1037 * wait for it to finish.
1038 * @dwork: the kthread delayed work to cancel
1039 *
1040 * This is kthread_cancel_work_sync() for delayed works.
1041 *
1042 * Return: %true if @dwork was pending, %false otherwise.
1043 */
1044bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *dwork)
1045{
1046 return __kthread_cancel_work_sync(&dwork->work, true);
1047}
1048EXPORT_SYMBOL_GPL(kthread_cancel_delayed_work_sync);
1049
922/** 1050/**
923 * kthread_flush_worker - flush all current works on a kthread_worker 1051 * kthread_flush_worker - flush all current works on a kthread_worker
924 * @worker: worker to flush 1052 * @worker: worker to flush