mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] workqueue: Add new function mod_fwd_delayed_work()
@ 2017-02-22 17:41 Harald Geyer
  2017-02-22 17:41 ` [PATCH 2/2] regulator: core: Fix race on multiple calls to regulator_disable_deferred() Harald Geyer
  2017-02-22 18:21 ` [PATCH 1/2] workqueue: Add new function mod_fwd_delayed_work() Mark Brown
  0 siblings, 2 replies; 14+ messages in thread
From: Harald Geyer @ 2017-02-22 17:41 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown, Tejun Heo, Lai Jiangshan
  Cc: linux-kernel, Harald Geyer

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 <harald@ccbib.org>
---
 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

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2017-03-10 18:44 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-22 17:41 [PATCH 1/2] workqueue: Add new function mod_fwd_delayed_work() Harald Geyer
2017-02-22 17:41 ` [PATCH 2/2] regulator: core: Fix race on multiple calls to regulator_disable_deferred() Harald Geyer
2017-02-22 18:21 ` [PATCH 1/2] workqueue: Add new function mod_fwd_delayed_work() Mark Brown
2017-02-22 20:06   ` Harald Geyer
2017-02-23 17:34     ` Mark Brown
2017-02-23 23:22       ` Harald Geyer
2017-02-27 12:54         ` Mark Brown
2017-02-27 19:17           ` Harald Geyer
2017-03-07 12:42             ` Mark Brown
2017-03-10 18:44               ` Harald Geyer
2017-03-06 22:22       ` Tejun Heo
2017-03-07 11:29         ` Harald Geyer
2017-03-07 19:16           ` Tejun Heo
2017-03-07 12:45         ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome