Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756078AbbKRNcc (ORCPT ); Wed, 18 Nov 2015 08:32:32 -0500 Received: from mx2.suse.de ([195.135.220.15]:36327 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932087AbbKRN0h (ORCPT ); Wed, 18 Nov 2015 08:26:37 -0500 From: Petr Mladek To: Andrew Morton , Oleg Nesterov , Tejun Heo , Ingo Molnar , Peter Zijlstra Cc: Steven Rostedt , "Paul E. McKenney" , Josh Triplett , Thomas Gleixner , Linus Torvalds , Jiri Kosina , Borislav Petkov , Michal Hocko , linux-mm@kvack.org, Vlastimil Babka , linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek Subject: [PATCH v3 05/22] kthread: Add drain_kthread_worker() Date: Wed, 18 Nov 2015 14:25:10 +0100 Message-Id: <1447853127-3461-6-git-send-email-pmladek@suse.com> X-Mailer: git-send-email 1.8.5.6 In-Reply-To: <1447853127-3461-1-git-send-email-pmladek@suse.com> References: <1447853127-3461-1-git-send-email-pmladek@suse.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2280 Lines: 67 flush_kthread_worker() returns when the currently queued works are proceed. But some other works might have been queued in the meantime. This patch adds drain_kthread_work() that is inspired by drain_workqueue(). It returns when the queue is completely empty and warns when it takes too long. The initial implementation does not block queuing new works when draining. It makes things much easier. The blocking would be useful to debug potential problems but it is not clear if it is worth the complication at the moment. Signed-off-by: Petr Mladek --- kernel/kthread.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/kernel/kthread.c b/kernel/kthread.c index df402e18bb5a..a18ad3b58f61 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -804,3 +804,37 @@ void flush_kthread_worker(struct kthread_worker *worker) wait_for_completion(&fwork.done); } EXPORT_SYMBOL_GPL(flush_kthread_worker); + +/** + * drain_kthread_worker - drain a kthread worker + * @worker: worker to be drained + * + * Wait until there is no work queued for the given kthread worker. + * @worker is flushed repeatedly until it becomes empty. The number + * of flushing is determined by the depth of chaining and should + * be relatively short. Whine if it takes too long. + * + * The caller is responsible for blocking all users of this kthread + * worker from queuing new works. Also it is responsible for blocking + * the already queued works from an infinite re-queuing! + */ +void drain_kthread_worker(struct kthread_worker *worker) +{ + int flush_cnt = 0; + + spin_lock_irq(&worker->lock); + + while (!list_empty(&worker->work_list)) { + spin_unlock_irq(&worker->lock); + + flush_kthread_worker(worker); + WARN_ONCE(flush_cnt++ > 10, + "kthread worker %s: drain_kthread_worker() isn't complete after %u tries\n", + worker->task->comm, flush_cnt); + + spin_lock_irq(&worker->lock); + } + + spin_unlock_irq(&worker->lock); +} +EXPORT_SYMBOL(drain_kthread_worker); -- 1.8.5.6 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/