workqueue: warn if memory reclaim tries to flush !WQ_MEM_RECLAIM workqueue
authorTejun Heo <tj@kernel.org>
Mon, 7 Dec 2015 15:58:57 +0000 (10:58 -0500)
committerTejun Heo <tj@kernel.org>
Tue, 8 Dec 2015 16:29:36 +0000 (11:29 -0500)
Task or work item involved in memory reclaim trying to flush a
non-WQ_MEM_RECLAIM workqueue or one of its work items can lead to
deadlock.  Trigger WARN_ONCE() if such conditions are detected.

Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
kernel/workqueue.c

index c579dbab2e36ab20dd94a5f753a480df17ae28ff..c7769c507bf510ad691c0e904e8246fce9bae1c5 100644 (file)
@@ -2316,6 +2316,37 @@ repeat:
        goto repeat;
 }
 
+/**
+ * check_flush_dependency - check for flush dependency sanity
+ * @target_wq: workqueue being flushed
+ * @target_work: work item being flushed (NULL for workqueue flushes)
+ *
+ * %current is trying to flush the whole @target_wq or @target_work on it.
+ * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not
+ * reclaiming memory or running on a workqueue which doesn't have
+ * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to
+ * a deadlock.
+ */
+static void check_flush_dependency(struct workqueue_struct *target_wq,
+                                  struct work_struct *target_work)
+{
+       work_func_t target_func = target_work ? target_work->func : NULL;
+       struct worker *worker;
+
+       if (target_wq->flags & WQ_MEM_RECLAIM)
+               return;
+
+       worker = current_wq_worker();
+
+       WARN_ONCE(current->flags & PF_MEMALLOC,
+                 "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%pf",
+                 current->pid, current->comm, target_wq->name, target_func);
+       WARN_ONCE(worker && (worker->current_pwq->wq->flags & WQ_MEM_RECLAIM),
+                 "workqueue: WQ_MEM_RECLAIM %s:%pf is flushing !WQ_MEM_RECLAIM %s:%pf",
+                 worker->current_pwq->wq->name, worker->current_func,
+                 target_wq->name, target_func);
+}
+
 struct wq_barrier {
        struct work_struct      work;
        struct completion       done;
@@ -2525,6 +2556,8 @@ void flush_workqueue(struct workqueue_struct *wq)
                list_add_tail(&this_flusher.list, &wq->flusher_overflow);
        }
 
+       check_flush_dependency(wq, NULL);
+
        mutex_unlock(&wq->mutex);
 
        wait_for_completion(&this_flusher.done);
@@ -2697,6 +2730,8 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
                pwq = worker->current_pwq;
        }
 
+       check_flush_dependency(pwq->wq, work);
+
        insert_wq_barrier(pwq, barr, work, worker);
        spin_unlock_irq(&pool->lock);