From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933245AbdBVSUq (ORCPT ); Wed, 22 Feb 2017 13:20:46 -0500 Received: from h1.radempa.de ([176.9.142.194]:34782 "EHLO mail.cosmopool.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932913AbdBVSUO (ORCPT ); Wed, 22 Feb 2017 13:20:14 -0500 From: Harald Geyer To: Liam Girdwood , Mark Brown , Tejun Heo , Lai Jiangshan Cc: linux-kernel@vger.kernel.org, Harald Geyer Subject: [PATCH 1/2] workqueue: Add new function mod_fwd_delayed_work() Date: Wed, 22 Feb 2017 17:41:24 +0000 Message-Id: <1487785285-3567-1-git-send-email-harald@ccbib.org> X-Mailer: git-send-email 2.1.4 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Drivers calling queue_delayed_work() or mod_delayed_work() multiple times on the same work without coordination get undefined behaviour. Add a new function, which is easier to use. Signed-off-by: Harald Geyer --- include/linux/workqueue.h | 17 +++++++++++++++++ kernel/workqueue.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h index fc6e221..d79421c 100644 --- a/include/linux/workqueue.h +++ b/include/linux/workqueue.h @@ -433,6 +433,8 @@ extern bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, struct delayed_work *work, unsigned long delay); extern bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, struct delayed_work *dwork, unsigned long delay); +extern bool mod_fwd_delayed_work_on(int cpu, struct workqueue_struct *wq, + struct delayed_work *dwork, unsigned long delay); extern void flush_workqueue(struct workqueue_struct *wq); extern void drain_workqueue(struct workqueue_struct *wq); @@ -505,6 +507,21 @@ static inline bool mod_delayed_work(struct workqueue_struct *wq, } /** + * mod_fwd_delayed_work - queue a delayed work or increase delay + * @wq: workqueue to use + * @dwork: work to queue + * @delay: number of jiffies to wait before queueing + * + * mod_fwd_delayed_work_on() on local CPU. + */ +static inline bool mod_fwd_delayed_work(struct workqueue_struct *wq, + struct delayed_work *dwork, + unsigned long delay) +{ + return mod_fwd_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); +} + +/** * schedule_work_on - put work task on a specific cpu * @cpu: cpu to put the work task on * @work: job to be done diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 479d840..30837e6 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -1603,6 +1603,47 @@ bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, EXPORT_SYMBOL_GPL(mod_delayed_work_on); /** + * mod_fwd_delayed_work_on - like mod_delayed_work(), but only increase delay + * @cpu: CPU number to execute work on + * @wq: workqueue to use + * @dwork: work to queue + * @delay: number of jiffies to wait before queueing + * + * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, + * compare the old expiration time with @delay and set @dwork's timer + * so that it expires after the later time. + * + * Return: %false if @dwork was idle and queued, %true if @dwork was + * pending and its timer was modified. + * + * This function is safe to call from any context including IRQ handler. + * See try_to_grab_pending() for details. + */ +bool mod_fwd_delayed_work_on(int cpu, struct workqueue_struct *wq, + struct delayed_work *dwork, unsigned long delay) +{ + unsigned long flags; + int ret; + + do { + ret = try_to_grab_pending(&dwork->work, true, &flags); + } while (unlikely(ret == -EAGAIN)); + + if (unlikely(ret == 1 && + time_after(dwork->timer.expires, jiffies + delay))) + delay = dwork->timer.expires - jiffies; + + if (likely(ret >= 0)) { + __queue_delayed_work(cpu, wq, dwork, delay); + local_irq_restore(flags); + } + + /* -ENOENT from try_to_grab_pending() becomes %true */ + return ret; +} +EXPORT_SYMBOL_GPL(mod_fwd_delayed_work_on); + +/** * worker_enter_idle - enter idle state * @worker: worker which is entering idle state * -- 2.1.4