Skip to contentSkip to conversation

implement flush_work()

RFC PATCH In review Oleg Nesterov 2 participants 2 messages Dec 18, 2006 – Dec 19, 2006 Workqueue

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 implement flush_work()

On top of "[PATCH, RFC] reimplement flush_workqueue()", see

	http://marc.theaimsgroup.com/?l=linux-kernel&m=116639510029010
Quoted 6 lines · Andrew Morton

A basic problem with flush_scheduled_work() is that it blocks behind _all_ presently-queued works, rather than just the work whcih the caller wants to flush. If the caller holds some lock, and if one of the queued work happens to want that lock as well then accidental deadlocks can occur.

Quoted 7 lines

One example of this is the phy layer: it wants to flush work while holding rtnl_lock(). But if a linkwatch event happens to be queued, the phy code will deadlock because the linkwatch callback function takes rtnl_lock.

So we implement a new function which will flush a *single* work - just the one which the caller wants to free up. Thus we avoid the accidental deadlocks which can arise from unrelated subsystems' callbacks taking shared locks.

Add ->current_work to the "struct cpu_workqueue_struct", it points to currently running "struct queue_work". When flush_work(work) detects ->current_work == work, it inserts a barrier at the _head_ of ->worklist (and thus right _after_ that work) and waits for completition. This means that the next work fired on that CPU will be this barrier, or another barrier queued by concurrent flush_work(), so the caller of flush_work() will be woken before any "regular" work has a chance to run.

Since __queue_work() does both set_wq_data() and list_add_tail() atomically under cwq->lock, flush_work() can remove the pending work from queue when it sees "get_wq_data(work) == cwq".

NOTE: flush_work() doesn't like no-auto-release works. Unless they go away we can fix this later or add the "don't do this" comment.

include/linux/workqueue.h renamed from include/linux/workqueue.h~2_flush_w +2 -1
@@ -160,6 +160,7 @@ extern int FASTCALL(queue_delayed_work(s
 extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
 	struct delayed_work *work, unsigned long delay);
 extern void FASTCALL(flush_workqueue(struct workqueue_struct *wq));
+extern void flush_work(struct workqueue_struct *wq, struct work_struct *work);
 
 extern int FASTCALL(schedule_work(struct work_struct *work));
 extern int FASTCALL(run_scheduled_work(struct work_struct *work));
@@ -180,7 +181,7 @@ int execute_in_process_context(work_func
 /*
  * Kill off a pending schedule_delayed_work().  Note that the work callback
  * function may still be running on return from cancel_delayed_work().  Run
- * flush_scheduled_work() to wait on it.
+ * flush_scheduled_work() or flush_work() to wait on it.
  */
 static inline int cancel_delayed_work(struct delayed_work *work)
 {
kernel/workqueue.c renamed from kernel/workqueue.c~2_flush_w +82 -8

151 lines not on this page · raw message

#1 lore raw
Quoted 11 lines · On Mon, 18 Dec 2006 23:17:14 +0300 Oleg Nesterov <[email protected]> wrote:

Add ->current_work to the "struct cpu_workqueue_struct", it points to currently running "struct queue_work". When flush_work(work) detects ->current_work == work, it inserts a barrier at the _head_ of ->worklist (and thus right _after_ that work) and waits for completition. This means that the next work fired on that CPU will be this barrier, or another barrier queued by concurrent flush_work(), so the caller of flush_work() will be woken before any "regular" work has a chance to run.

Since __queue_work() does both set_wq_data() and list_add_tail() atomically under cwq->lock, flush_work() can remove the pending work from queue when it sees "get_wq_data(work) == cwq".

Seems sane.

Quoted 2 lines

NOTE: flush_work() doesn't like no-auto-release works. Unless they go away we can fix this later or add the "don't do this" comment.

Yes, let's make the _NAR stuff go away pleeeeze. It's fairly straightforward, and is on my todo list somewhere.