workqueue: implement alloc_ordered_workqueue() and flush[_delayed]_work_sync()
Tejun Heo
maintainerPATCH 1/1 workqueue: implement alloc_ordered_workqueue() and flush[_delayed]_work_sync()
Hello,
This patchset implements new workqueue API fucntions - alloc_ordered_workqueue() and flush[_delayed]_work_sync().
The former is rather straight-forward replacement for alloc_singlethread/frozen_workqueue().
The latter two are similar to cancel[_delayed]_work_sync() but wait for the work to finish execution rather than cancelling them. These syncing flushes make sure that works which were queued on multiple workqueues or a reentrant workqueues have finished execution on all CPUs. One usage of these new flushes is replacing flush_scheduled_works() with finer scoped flushes.
This patchset contains the following four patches.
0001-workqueue-implement-alloc_ordered_workqueue.patch 0002-workqueue-cleanup-flush-cancel-functions.patch 0003-workqueue-factor-out-start_flush_work.patch 0004-workqueue-implement-flush-_delayed-_work_sync.patch
0001 adds alloc_ordered_workqueue(). 0002-0003 prepare for syncing flushes. 0004 implements them.
Dmitry, as I wrote before, I think serio should be able to simply use schedule_work() combined with flush_work_sync() implemented by this patchset. Would this work for you?
The patches are available in the following git tree.
git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq.git review-flush_sync
and contains the following changes.
include/linux/workqueue.h | 38 +++++- kernel/workqueue.c | 265 +++++++++++++++++++++++++++++----------------- 2 files changed, 201 insertions(+), 102 deletions(-)
Thanks.
--
tejun
Tejun Heo
maintainerPATCH 1/4 workqueue: implement alloc_ordered_workqueue()
alloc_ordered_workqueue() creates a workqueue which processes each work itemp one by one in the queued order. This will be used to replace create_freezeable_workqueue() and create_singlethread_workqueue().
--- include/linux/workqueue.h | 18 ++++++++++++++++++ 1 files changed, 18 insertions(+), 0 deletions(-)
include/linux/workqueue.h +18 -0
@@ -306,6 +306,24 @@ __alloc_workqueue_key(const char *name, unsigned int flags, int max_active, __alloc_workqueue_key((name), (flags), (max_active), NULL, NULL)#endif +/** +* alloc_ordered_workqueue - allocate an ordered workqueue +* @name: name of the workqueue +* @flags: WQ_* flags (only WQ_FREEZEABLE and WQ_RESCUER are meaningful) +* +* Allocate an ordered workqueue. An ordered workqueue executes at +* most one work item at any given time in the queued order. They are +* implemented as unbound workqueues with @max_active of one. +* +* RETURNS: +* Pointer to the allocated workqueue on success, %NULL on failure. +*/ +static inline struct workqueue_struct * +alloc_ordered_workqueue (const char *name,unsigned int flags) +{ +return alloc_workqueue (name,WQ_UNBOUND | flags,1 ); +} +#define create_workqueue (name) \ alloc_workqueue((name), WQ_RESCUER, 1)#define create_freezeable_workqueue (name) \
Signature · 1 line
1.7.1
Tejun Heo
maintainerPATCH 2/4 workqueue: cleanup flush/cancel functions
In reply to Tejun HeoMake the following cleanup changes.
* Relocate flush/cancel function prototypes and definitions.
* Relocate wait_on_cpu_work() and wait_on_work() before try_to_grab_pending(). These will be used to implement flush_work_sync().
* Make all flush/cancel functions return bool instead of int.
* Update wait_on_cpu_work() and wait_on_work() to return %true if they actually waited.
* Add / update comments.
This patch doesn't cause any functional changes.
--- include/linux/workqueue.h | 18 +++--- kernel/workqueue.c | 175 ++++++++++++++++++++++++--------------------- 2 files changed, 103 insertions(+), 90 deletions(-)
include/linux/workqueue.h +9 -9
@@ -343,7 +343,6 @@ extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,extern void flush_workqueue (struct workqueue_struct *wq);extern void flush_scheduled_work (void ); -extern void flush_delayed_work (struct delayed_work *work);extern int schedule_work (struct work_struct *work);extern int schedule_work_on (int cpu,struct work_struct *work);
@@ -355,8 +354,11 @@ extern int keventd_up(void);int execute_in_process_context (work_func_t fn,struct execute_work *); -extern int flush_work (struct work_struct *work); -extern int cancel_work_sync (struct work_struct *work); +extern bool flush_work (struct work_struct *work); +extern bool cancel_work_sync (struct work_struct *work); + +extern bool flush_delayed_work (struct delayed_work *dwork); +extern bool cancel_delayed_work_sync (struct delayed_work *dwork);extern void workqueue_set_max_active (struct workqueue_struct *wq,int max_active);
@@ -370,9 +372,9 @@ extern unsigned int work_busy(struct work_struct *work);* it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or * cancel_work_sync() to wait on it. */ -static inline int cancel_delayed_work (struct delayed_work *work) +static inline bool cancel_delayed_work (struct delayed_work *work) { -int ret; +bool ret; ret =del_timer_sync (&work->timer);if (ret)
@@ -385,9 +387,9 @@ static inline int cancel_delayed_work(struct delayed_work *work)* if it returns 0 the timer function may be running and the queueing is in * progress. */ -static inline int __cancel_delayed_work (struct delayed_work *work) +static inline bool __cancel_delayed_work (struct delayed_work *work) { -int ret; +bool ret; ret =del_timer (&work->timer);if (ret)
@@ -395,8 +397,6 @@ static inline int __cancel_delayed_work(struct delayed_work *work)return ret; } -extern int cancel_delayed_work_sync (struct delayed_work *work); -/* Obsolete. use cancel_delayed_work_sync() */ static inline void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
kernel/workqueue.c +94 -81
227 lines not on this page · raw message
Signature · 1 line
1.7.1
Tejun Heo
maintainerPATCH 3/4 workqueue: factor out start_flush_work()
In reply to Tejun HeoFactor out start_flush_work() from flush_work(). start_flush_work() has @wait_executing argument which controls whether the barrier is queued only if the work is pending or also if executing. As flush_work() needs to wait for execution too, it uses %true.
This commit doesn't cause any behavior difference. start_flush_work() will be used to implement flush_work_sync().
--- kernel/workqueue.c | 64 ++++++++++++++++++++++++++++++---------------------- 1 files changed, 37 insertions(+), 27 deletions(-)
kernel/workqueue.c +37 -27
@@ -2326,35 +2326,17 @@ out_unlock: }EXPORT_SYMBOL_GPL (flush_workqueue); -/** -* flush_work - wait for a work to finish executing the last queueing instance -* @work: the work to flush -* -* Wait until @work has finished execution. This function considers -* only the last queueing instance of @work. If @work has been -* enqueued across different CPUs on a non-reentrant workqueue or on -* multiple workqueues, @work might still be executing on return on -* some of the CPUs from earlier queueing. -* -* If @work was queued only on a non-reentrant, ordered or unbound -* workqueue, @work is guaranteed to be idle on return if it hasn't -* been requeued since flush started. -* -* RETURNS: -* %true if flush_work() waited for the work to finish execution, -* %false if it was already idle. -*/ -bool flush_work (struct work_struct *work) +static bool start_flush_work (struct work_struct *work,struct wq_barrier *barr, +bool wait_executing) {struct worker *worker =NULL ;struct global_cwq *gcwq;struct cpu_workqueue_struct *cwq; -struct wq_barrier barr;might_sleep (); gcwq =get_work_gcwq (work);if (!gcwq) -return 0 ; +return false;spin_lock_irq (&gcwq->lock);if (!list_empty (&work->entry)) {
@@ -2367,26 +2349,54 @@ bool flush_work(struct work_struct *work) cwq =get_work_cwq (work);if (unlikely (!cwq || gcwq != cwq->gcwq)) goto already_gone; - }else { + }else if (wait_executing) { worker =find_worker_executing_work (gcwq, work);if (!worker) goto already_gone; cwq = worker->current_cwq; - } + }else + goto already_gone; -insert_wq_barrier (cwq, &barr, work, worker); +insert_wq_barrier (cwq, barr, work, worker);spin_unlock_irq (&gcwq->lock);lock_map_acquire (&cwq->wq->lockdep_map);lock_map_release (&cwq->wq->lockdep_map); - -wait_for_completion (&barr.done); -destroy_work_on_stack (&barr.work);return true; already_gone:spin_unlock_irq (&gcwq->lock);return false; } + +/** +* flush_work - wait for a work to finish executing the last queueing instance +* @work: the work to flush +* +* Wait until @work has finished execution. This function considers +* only the last queueing instance of @work. If @work has been +* enqueued across different CPUs on a non-reentrant workqueue or on +* multiple workqueues, @work might still be executing on return on +* some of the CPUs from earlier queueing. +* +* If @work was queued only on a non-reentrant, ordered or unbound +* workqueue, @work is guaranteed to be idle on return if it hasn't +* been requeued since flush started. +* +* RETURNS: +* %true if flush_work() waited for the work to finish execution, +* %false if it was already idle. +*/ +bool flush_work (struct work_struct *work) +{ +struct wq_barrier barr; + +if (start_flush_work (work, &barr, true)) { +wait_for_completion (&barr.done); +destroy_work_on_stack (&barr.work); +return true; + }else +return false; +}EXPORT_SYMBOL_GPL (flush_work);static bool wait_on_cpu_work (struct global_cwq *gcwq,struct work_struct *work)
Signature · 1 line
1.7.1
Tejun Heo
maintainerPATCH 4/4 workqueue: implement flush[_delayed]_work_sync()
In reply to Tejun HeoImplement flush[_delayed]_work_sync(). These are flush functions which also make sure no CPU is still executing the target work from earlier queueing instances. These are similar to cancel[_delayed]_work_sync() except that the target work item is flushed instead of cancelled.
--- include/linux/workqueue.h | 2 + kernel/workqueue.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 0 deletions(-)
include/linux/workqueue.h +2 -0
@@ -355,9 +355,11 @@ extern int keventd_up(void);int execute_in_process_context (work_func_t fn,struct execute_work *);extern bool flush_work (struct work_struct *work); +extern bool flush_work_sync (struct work_struct *work);extern bool cancel_work_sync (struct work_struct *work);extern bool flush_delayed_work (struct delayed_work *dwork); +extern bool flush_delayed_work_sync (struct delayed_work *work);extern bool cancel_delayed_work_sync (struct delayed_work *dwork); extern void workqueue_set_max_active(struct workqueue_struct *wq,
kernel/workqueue.c +56 -0
68 lines not on this page · raw message
Signature · 1 line
1.7.1