From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753109AbcBVO60 (ORCPT ); Mon, 22 Feb 2016 09:58:26 -0500 Received: from mx2.suse.de ([195.135.220.15]:36917 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753022AbcBVO6W (ORCPT ); Mon, 22 Feb 2016 09:58:22 -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 v5 09/20] kthread: Allow to modify delayed kthread work Date: Mon, 22 Feb 2016 15:56:59 +0100 Message-Id: <1456153030-12400-10-git-send-email-pmladek@suse.com> X-Mailer: git-send-email 1.8.5.6 In-Reply-To: <1456153030-12400-1-git-send-email-pmladek@suse.com> References: <1456153030-12400-1-git-send-email-pmladek@suse.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are situations when we need to modify the delay of a delayed kthread work. For example, when the work depends on an event and the initial delay means a timeout. Then we want to queue the work immediately when the event happens. This patch implements mod_delayed_kthread_work() as inspired workqueues. It cancels the timer, removes the work from any worker list and queues it again with the given timeout. A very special case is when the work is being canceled at the same time. cancel_*kthread_work_sync() operation blocks queuing until the running work finishes. Therefore we do nothing and let cancel() win. This should not normally happen as the caller is supposed to synchronize these operations a reasonable way. Signed-off-by: Petr Mladek --- include/linux/kthread.h | 4 ++++ kernel/kthread.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/kthread.h b/include/linux/kthread.h index 28ea12942878..531730f8a8a7 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h @@ -168,6 +168,10 @@ bool queue_delayed_kthread_work(struct kthread_worker *worker, struct delayed_kthread_work *dwork, unsigned long delay); +bool mod_delayed_kthread_work(struct kthread_worker *worker, + struct delayed_kthread_work *dwork, + unsigned long delay); + void flush_kthread_work(struct kthread_work *work); void flush_kthread_worker(struct kthread_worker *worker); diff --git a/kernel/kthread.c b/kernel/kthread.c index 7e6c921b2a9a..686bdd5b6936 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -959,6 +959,51 @@ static bool __cancel_kthread_work(struct kthread_work *work, bool is_dwork) return false; } +/** + * mod_delayed_kthread_work - modify delay of or queue a delayed kthread work + * @worker: kthread worker to use + * @dwork: delayed kthread work to queue + * @delay: number of jiffies to wait before queuing + * + * If @dwork is idle, equivalent to queue_delayed_kthread work(). Otherwise, + * modify @dwork's timer so that it expires after @delay. If @delay is zero, + * @work is guaranteed to be queued immediately. + * + * Return: %true if @dwork was pending and its timer was modified, + * %false otherwise. + * + * A special case is when cancel_work_sync() is running in parallel. + * It blocks further queuing. We let the cancel() win and return %false. + * The caller is supposed to synchronize these operations a reasonable way. + * + * This function is safe to call from any context including IRQ handler. + * See __cancel_kthread_work() and delayed_kthread_work_timer_fn() + * for details. + */ +bool mod_delayed_kthread_work(struct kthread_worker *worker, + struct delayed_kthread_work *dwork, + unsigned long delay) +{ + struct kthread_work *work = &dwork->work; + unsigned long flags; + int ret = false; + + spin_lock_irqsave(&worker->lock, flags); + /* Work must not be used with more workers, see queue_kthread_work() */ + WARN_ON_ONCE(work->worker && work->worker != worker); + + if (work->canceling) + goto out; + + ret = __cancel_kthread_work(work, true); + __queue_delayed_kthread_work(worker, dwork, delay); + +out: + spin_unlock_irqrestore(&worker->lock, flags); + return ret; +} +EXPORT_SYMBOL_GPL(mod_delayed_kthread_work); + static bool __cancel_kthread_work_sync(struct kthread_work *work, bool is_dwork) { struct kthread_worker *worker = work->worker; -- 1.8.5.6