Skip to contentSkip to conversation

drain_workqueue vs scsi_flush_work

PATCH In review Dan Williams 1 participant 6 messages Dec 2, 2011 – Dec 4, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover drain_workqueue vs scsi_flush_work

Hi Tejun, James,

While trying to use drain_workqueue() in libsas I hit the WARN_ON_ONCE(!is_chained_work(wq)) in __queue_work(). However, as I mention in patch-2 libsas expects that new unchained work can be submitted during a drain. So, patch-2 is a hack to accept new work when drain_workqueue() is used publicly as a "stronger flush", while keeping the same warn+drop behavior for internal usages of drain_workqueue() in the destroy_workqueue() case.

Smoke tested on sas topoloogy of:
host-->ata
host-->expander-->ata
host-->expander-->expander-->ata

--
Dan

---

Dan Williams (3):
workqueue: promote workqueue_lock to hard-irq safe
workqueue: defer work to a draining queue
scsi: use drain_workqueue

 drivers/scsi/hosts.c      |    8 ++-
 drivers/scsi/isci/host.c  |    3 -
 include/linux/workqueue.h |    3 +
 include/scsi/scsi_host.h  |    2 -
 kernel/workqueue.c        |  114 ++++++++++++++++++++++++++++++++-------------
 5 files changed, 89 insertions(+), 41 deletions(-)
#1 lore raw

PATCH 1/3 workqueue: promote workqueue_lock to hard-irq safe

In preparation for a deferred work implementation to queue unchained work at the conclusion of a drain_workqueue() event.

---
 kernel/workqueue.c |   44 ++++++++++++++++++++++----------------------
 1 files changed, 22 insertions(+), 22 deletions(-)
kernel/workqueue.c +22 -22
index 1783aab..f476895 100644
@@ -2403,10 +2403,10 @@ void drain_workqueue(struct workqueue_struct *wq)
 	 * hotter than drain_workqueue() and already looks at @wq->flags.
 	 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
 	 */
-	spin_lock(&workqueue_lock);
+	spin_lock_irq(&workqueue_lock);
 	if (!wq->nr_drainers++)
 		wq->flags |= WQ_DRAINING;
-	spin_unlock(&workqueue_lock);
+	spin_unlock_irq(&workqueue_lock);
 reflush:
 	flush_workqueue(wq);
 
@@ -2428,10 +2428,10 @@ reflush:
 		goto reflush;
 	}
 
-	spin_lock(&workqueue_lock);
+	spin_lock_irq(&workqueue_lock);
 	if (!--wq->nr_drainers)
 		wq->flags &= ~WQ_DRAINING;
-	spin_unlock(&workqueue_lock);
+	spin_unlock_irq(&workqueue_lock);
 }
 EXPORT_SYMBOL_GPL(drain_workqueue);
 
@@ -3033,7 +3033,7 @@ struct workqueue_struct *__alloc_workqueue_key(const char *name,
 	 * list.  Grab it, set max_active accordingly and add the new
 	 * workqueue to workqueues list.
 	 */
-	spin_lock(&workqueue_lock);
+	spin_lock_irq(&workqueue_lock);
 
 	if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
 		for_each_cwq_cpu(cpu, wq)
@@ -3041,7 +3041,7 @@ struct workqueue_struct *__alloc_workqueue_key(const char *name,
 
 	list_add(&wq->list, &workqueues);
 
-	spin_unlock(&workqueue_lock);
+	spin_unlock_irq(&workqueue_lock);
 
 	return wq;
 err:
@@ -3072,9 +3072,9 @@ void destroy_workqueue(struct workqueue_struct *wq)
 	 * wq list is used to freeze wq, remove from list after
 	 * flushing is complete in case freeze races us.
 	 */
-	spin_lock(&workqueue_lock);
+	spin_lock_irq(&workqueue_lock);
 	list_del(&wq->list);
-	spin_unlock(&workqueue_lock);
+	spin_unlock_irq(&workqueue_lock);
 
 	/* sanity check */
 	for_each_cwq_cpu(cpu, wq) {
@@ -3114,23 +3114,23 @@ void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
 
 	max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
 
-	spin_lock(&workqueue_lock);
+	spin_lock_irq(&workqueue_lock);
 
 	wq->saved_max_active = max_active;
 
 	for_each_cwq_cpu(cpu, wq) {
 		struct global_cwq *gcwq = get_gcwq(cpu);
 
-		spin_lock_irq(&gcwq->lock);
+		spin_lock(&gcwq->lock);
 
 		if (!(wq->flags & WQ_FREEZABLE) ||
 		    !(gcwq->flags & GCWQ_FREEZING))
 			get_cwq(gcwq->cpu, wq)->max_active = max_active;
 
-		spin_unlock_irq(&gcwq->lock);
+		spin_unlock(&gcwq->lock);
 	}
 
-	spin_unlock(&workqueue_lock);
+	spin_unlock_irq(&workqueue_lock);
 }
 EXPORT_SYMBOL_GPL(workqueue_set_max_active);
 
@@ -3642,7 +3642,7 @@ void freeze_workqueues_begin(void)
 {
 	unsigned int cpu;
 
-	spin_lock(&workqueue_lock);
+	spin_lock_irq(&workqueue_lock);
 
 	BUG_ON(workqueue_freezing);
 	workqueue_freezing = true;
@@ -3651,7 +3651,7 @@ void freeze_workqueues_begin(void)
 		struct global_cwq *gcwq = get_gcwq(cpu);
 		struct workqueue_struct *wq;
 
-		spin_lock_irq(&gcwq->lock);
+		spin_lock(&gcwq->lock);
 
 		BUG_ON(gcwq->flags & GCWQ_FREEZING);
 		gcwq->flags |= GCWQ_FREEZING;
@@ -3663,10 +3663,10 @@ void freeze_workqueues_begin(void)
 				cwq->max_active = 0;
 		}
 
-		spin_unlock_irq(&gcwq->lock);
+		spin_unlock(&gcwq->lock);
 	}
 
-	spin_unlock(&workqueue_lock);
+	spin_unlock_irq(&workqueue_lock);
 }
 
 /**
@@ -3687,7 +3687,7 @@ bool freeze_workqueues_busy(void)
 	unsigned int cpu;
 	bool busy = false;
 
-	spin_lock(&workqueue_lock);
+	spin_lock_irq(&workqueue_lock);
 
 	BUG_ON(!workqueue_freezing);
 
@@ -3711,7 +3711,7 @@ bool freeze_workqueues_busy(void)
 		}
 	}
 out_unlock:
-	spin_unlock(&workqueue_lock);
+	spin_unlock_irq(&workqueue_lock);
 	return busy;
 }
 
@@ -3728,7 +3728,7 @@ void thaw_workqueues(void)
 {
 	unsigned int cpu;
 
-	spin_lock(&workqueue_lock);
+	spin_lock_irq(&workqueue_lock);
 
 	if (!workqueue_freezing)
 		goto out_unlock;
@@ -3737,7 +3737,7 @@ void thaw_workqueues(void)
 		struct global_cwq *gcwq = get_gcwq(cpu);
 		struct workqueue_struct *wq;
 
-		spin_lock_irq(&gcwq->lock);
+		spin_lock(&gcwq->lock);
 
 		BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
 		gcwq->flags &= ~GCWQ_FREEZING;
@@ -3758,12 +3758,12 @@ void thaw_workqueues(void)
 
 		wake_up_worker(gcwq);
 
-		spin_unlock_irq(&gcwq->lock);
+		spin_unlock(&gcwq->lock);
 	}
 
 	workqueue_freezing = false;
 out_unlock:
-	spin_unlock(&workqueue_lock);
+	spin_unlock_irq(&workqueue_lock);
 }
 #endif /* CONFIG_FREEZER */
 
#2 lore raw

PATCH 2/3 workqueue: defer work to a draining queue

In reply to Dan Williams

commit 9c5a2ba7 "workqueue: separate out drain_workqueue() from destroy_workqueue()" provided drain_workqueue() for users like libsas to use for flushing events.

When libsas drains it wants currently queued and chained events to be flushed, but it fully expects to continue issuing unchained events with the expectation that they are serviced sometime after the drain.

For external users of drain_workqueue() let unchained work commence after the drain completes, if the caller cares if unchained work was queued as a result of the drain it can check for a non-zero return value.

Unfortunately this causes the promotion of workqueue_lock to hard-irq safe and can't support queue_work_on() users.

---
 include/linux/workqueue.h |    3 +-
 kernel/workqueue.c        |   70 +++++++++++++++++++++++++++++++++++++++------
 2 files changed, 62 insertions(+), 11 deletions(-)
include/linux/workqueue.h +2 -1
index 0d556de..37de207 100644
@@ -257,6 +257,7 @@ enum {
 
 	WQ_DRAINING		= 1 << 6, /* internal: workqueue is draining */
 	WQ_RESCUER		= 1 << 7, /* internal: workqueue has rescuer */
+	WQ_NO_DEFER		= 1 << 8, /* internal: workqueue destructing */
 
 	WQ_MAX_ACTIVE		= 512,	  /* I like 512, better ideas? */
 	WQ_MAX_UNBOUND_PER_CPU	= 4,	  /* 4 * #cpus for unbound wq */
@@ -355,7 +356,7 @@ extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
 			struct delayed_work *work, unsigned long delay);
 
 extern void flush_workqueue(struct workqueue_struct *wq);
-extern void drain_workqueue(struct workqueue_struct *wq);
+extern int drain_workqueue(struct workqueue_struct *wq);
 extern void flush_scheduled_work(void);
 
 extern int schedule_work(struct work_struct *work);
kernel/workqueue.c +60 -10

130 lines not on this page · raw message

#5 lore raw

PATCH 3/3 scsi: use drain_workqueue

In reply to Dan Williams

Use 'drain' versus 'flush' as the former additionally flushes chained operations. libsas uses chained operations when it posts discovery work in response to a port event.

As a result the hardcoded double-flush can be removed from the isci driver.

Cc Mike Christie <[email protected]>
Cc Robert Love <[email protected]>
---
 drivers/scsi/hosts.c     |    8 ++++----
 drivers/scsi/isci/host.c |    3 ---
 include/scsi/scsi_host.h |    2 +-
 3 files changed, 5 insertions(+), 8 deletions(-)
drivers/scsi/hosts.c +4 -4
index 351dc0b..37155d1 100644
@@ -564,19 +564,19 @@ int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
 EXPORT_SYMBOL_GPL(scsi_queue_work);
 
 /**
- * scsi_flush_work - Flush a Scsi_Host's workqueue.
+ * scsi_flush_work - Drain a Scsi_Host's workqueue.
  * @shost:	Pointer to Scsi_Host.
  **/
-void scsi_flush_work(struct Scsi_Host *shost)
+int scsi_flush_work(struct Scsi_Host *shost)
 {
 	if (!shost->work_q) {
 		printk(KERN_ERR
 			"ERROR: Scsi host '%s' attempted to flush scsi-work, "
 			"when no workqueue created.\n", shost->hostt->name);
 		dump_stack();
-		return;
+		return 0;
 	}
 
-	flush_workqueue(shost->work_q);
+	return drain_workqueue(shost->work_q);
 }
 EXPORT_SYMBOL_GPL(scsi_flush_work);
drivers/scsi/isci/host.c +0 -3
index e7fe9c4..240779a 100644
@@ -655,9 +655,6 @@ int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
 	if (test_bit(IHOST_START_PENDING, &ihost->flags))
 		return 0;
 
-	/* todo: use sas_flush_discovery once it is upstream */
-	scsi_flush_work(shost);
-
 	scsi_flush_work(shost);
 
 	dev_dbg(&ihost->pdev->dev,
include/scsi/scsi_host.h +1 -1
index 50266c9..505bc34 100644
@@ -770,7 +770,7 @@ static inline int scsi_host_in_recovery(struct Scsi_Host *shost)
 }
 
 extern int scsi_queue_work(struct Scsi_Host *, struct work_struct *);
-extern void scsi_flush_work(struct Scsi_Host *);
+extern int scsi_flush_work(struct Scsi_Host *);
 
 extern struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *, int);
 extern int __must_check scsi_add_host_with_dma(struct Scsi_Host *,
#3 lore raw

Re: [PATCH 2/3] workqueue: defer work to a draining queue

In reply to Dan Williams

On Fri, Dec 2, 2011 at 3:56 PM, Dan Williams <[email protected]> wrote:
[..]

Quoted 17 lines
kernel/workqueue.c +1 -1
@@ -2405,7 +2434,7 @@ void drain_workqueue(struct workqueue_struct *wq)
         */
        spin_lock_irq(&workqueue_lock);
        if (!wq->nr_drainers++)
-               wq->flags |= WQ_DRAINING;
+               wq->flags |= WQ_DRAINING | flags;
        spin_unlock_irq(&workqueue_lock);

 reflush:
       flush_workqueue(wq);

kernel/workqueue.c +2 -2
@@ -2429,9 +2458,25 @@ reflush:
        }
 
        spin_lock_irq(&workqueue_lock);
-       if (!--wq->nr_drainers)
-               wq->flags &= ~WQ_DRAINING;
+       if (!--wq->nr_drainers) {
+               wq->flags &= ~(WQ_DRAINING | WQ_NO_DEFER);

kernel/workqueue.c reflush: · line 2472

Quoted 10 lines
kernel/workqueue.c +9 -0
+               list_splice_init(&wq->drain_defer, &drain_defer);
+               ret = !list_empty(&drain_defer);
+       }
        spin_unlock_irq(&workqueue_lock);
+
+       /* requeue work on this queue provided it was not being destroyed */
+       list_for_each_entry_safe(work, w, &drain_defer, entry) {
+               list_del_init(&work->entry);
+               queue_work(wq, work);
+       }

Actually, this won't work. The queuing of deferred work would need to be under the lock and then flushed again to guarantee workqueue semantics which just devolves into a "wait until all work stops being submitted to the queue for one drain_workqueue() duration". Which is more than what we need for libsas.
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

Re: [PATCH 2/3] workqueue: defer work to a draining queue

In reply to Dan Williams
Quoted 3 lines · Dan Williams

commit 9c5a2ba7 "workqueue: separate out drain_workqueue() from destroy_workqueue()" provided drain_workqueue() for users like libsas to use for flushing events.

Any reason to allow drain requests to stack? If draining is under a mutex then we don't break workqueue semantics (at least with respect to draining), all chained work is flushed and all unchained work is registered in the queue. But it still leaves deferred work invisible to flush_workqueue(). drain_workqueue() users would need to be careful not to mix 'flush' and 'drain'.

Untested incremental changes to implement above:

kernel/workqueue.c +13 -14
index 363a4ef..24563d6 100644
@@ -236,12 +236,12 @@ struct workqueue_struct {
 	struct wq_flusher	*first_flusher;	/* F: first flusher */
 	struct list_head	flusher_queue;	/* F: flush waiters */
 	struct list_head	flusher_overflow; /* F: flush overflow list */
+	struct mutex		drain_mutex;	/* 1 drainer at a time */
 	struct list_head	drain_defer;	/* W: unchained work to defer */
 
 	mayday_mask_t		mayday_mask;	/* cpus requesting rescue */
 	struct worker		*rescuer;	/* I: rescue worker */
 
-	int			nr_drainers;	/* W: drain in progress */
 	int			saved_max_active; /* W: saved cwq max_active */
 	const char		*name;		/* I: workqueue name */
 #ifdef CONFIG_LOCKDEP
@@ -2427,14 +2427,10 @@ static int __drain_workqueue(struct workqueue_struct *wq, int flags)
 	unsigned int cpu;
 	int ret = 0;
 
-	/*
-	 * __queue_work() needs to test whether there are drainers, is much
-	 * hotter than drain_workqueue() and already looks at @wq->flags.
-	 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
-	 */
+	mutex_lock(&wq->drain_mutex);
+
 	spin_lock_irq(&workqueue_lock);
-	if (!wq->nr_drainers++)
-		wq->flags |= WQ_DRAINING | flags;
+	wq->flags |= WQ_DRAINING | flags;
 	spin_unlock_irq(&workqueue_lock);
 reflush:
 	flush_workqueue(wq);
@@ -2458,24 +2454,26 @@ reflush:
 	}
 
 	spin_lock_irq(&workqueue_lock);
-	if (!--wq->nr_drainers) {
-		wq->flags &= ~(WQ_DRAINING | WQ_NO_DEFER);
-		list_splice_init(&wq->drain_defer, &drain_defer);
-		ret = !list_empty(&drain_defer);
-	}
+	wq->flags &= ~(WQ_DRAINING | WQ_NO_DEFER);
+	list_splice_init(&wq->drain_defer, &drain_defer);
+	ret = !list_empty(&drain_defer);
 	spin_unlock_irq(&workqueue_lock);
 
-	/* requeue work on this queue provided it was not being destroyed */
+	/* submit deferred work provided wq was not being destroyed */
 	list_for_each_entry_safe(work, w, &drain_defer, entry) {
 		list_del_init(&work->entry);
 		queue_work(wq, work);
 	}
 
+	mutex_unlock(&wq->drain_mutex);
+
 	return ret;
 }
 
 int drain_workqueue(struct workqueue_struct *wq)
 {
+	if (WARN_ON_ONCE(wq->flags & WQ_NO_DEFER))
+		return 0; /* lost drain vs destroy race */
 	return __drain_workqueue(wq, 0);
 }
 EXPORT_SYMBOL_GPL(drain_workqueue);
@@ -3032,6 +3030,7 @@ struct workqueue_struct *__alloc_workqueue_key(const char *name,
 	wq->flags = flags;
 	wq->saved_max_active = max_active;
 	mutex_init(&wq->flush_mutex);
+	mutex_init(&wq->drain_mutex);
 	atomic_set(&wq->nr_cwqs_to_flush, 0);
 	INIT_LIST_HEAD(&wq->flusher_queue);
 	INIT_LIST_HEAD(&wq->flusher_overflow);