History

Merge branch 'for-3.1' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq

* 'for-3.1' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq: workqueue: separate out drain_workqueue() from destroy_workqueue() workqueue: remove cancel_rearming_delayed_work[queue]()

Linus Torvalds committed 15 years ago

Commit 5a791ea2 parents 8209f53 9c5a2ba3 files · +55 −54Browse files

Showing the changes to kernel/workqueue.c (1 of 3 files). Show all

Unified · Split

kernel/workqueue.c+53−28
@@ -221,7 +221,7 @@ typedef unsigned long mayday_mask_t; · 220 unmodified lines
221221 * per-CPU workqueues:
222222 */
223223 struct workqueue_struct {
224− unsigned int flags; /* I: WQ_* flags */
224+ unsigned int flags; /* W: WQ_* flags */
225225 union {
226226 struct cpu_workqueue_struct __percpu *pcpu;
227227 struct cpu_workqueue_struct *single;
@@ -240,6 +240,7 @@ struct workqueue_struct { · 12 unmodified lines
240240 mayday_mask_t mayday_mask; /* cpus requesting rescue */
241241 struct worker *rescuer; /* I: rescue worker */
242242
243+ int nr_drainers; /* W: drain in progress */
243244 int saved_max_active; /* W: saved cwq max_active */
244245 const char *name; /* I: workqueue name */
245246 #ifdef CONFIG_LOCKDEP
@@ -990,7 +991,7 @@ static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, · 744 unmodified lines
990991 debug_work_activate(work);
991992
992993 /* if dying, only works from the same workqueue are allowed */
993− if (unlikely(wq->flags & WQ_DYING) &&
994+ if (unlikely(wq->flags & WQ_DRAINING) &&
994995 WARN_ON_ONCE(!is_chained_work(wq)))
995996 return;
996997
@@ -2381,6 +2382,54 @@ out_unlock: · 1384 unmodified lines
23812382 }
23822383 EXPORT_SYMBOL_GPL(flush_workqueue);
23832384
2385+/**
2386+ * drain_workqueue - drain a workqueue
2387+ * @wq: workqueue to drain
2388+ *
2389+ * Wait until the workqueue becomes empty. While draining is in progress,
2390+ * only chain queueing is allowed. IOW, only currently pending or running
2391+ * work items on @wq can queue further work items on it. @wq is flushed
2392+ * repeatedly until it becomes empty. The number of flushing is detemined
2393+ * by the depth of chaining and should be relatively short. Whine if it
2394+ * takes too long.
2395+ */
2396+void drain_workqueue(struct workqueue_struct *wq)
2397+{
2398+ unsigned int flush_cnt = 0;
2399+ unsigned int cpu;
2400+
2401+ /*
2402+ * __queue_work() needs to test whether there are drainers, is much
2403+ * hotter than drain_workqueue() and already looks at @wq->flags.
2404+ * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2405+ */
2406+ spin_lock(&workqueue_lock);
2407+ if (!wq->nr_drainers++)
2408+ wq->flags |= WQ_DRAINING;
2409+ spin_unlock(&workqueue_lock);
2410+reflush:
2411+ flush_workqueue(wq);
2412+
2413+ for_each_cwq_cpu(cpu, wq) {
2414+ struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2415+
2416+ if (!cwq->nr_active && list_empty(&cwq->delayed_works))
2417+ continue;
2418+
2419+ if (++flush_cnt == 10 ||
2420+ (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2421+ pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2422+ wq->name, flush_cnt);
2423+ goto reflush;
2424+ }
2425+
2426+ spin_lock(&workqueue_lock);
2427+ if (!--wq->nr_drainers)
2428+ wq->flags &= ~WQ_DRAINING;
2429+ spin_unlock(&workqueue_lock);
2430+}
2431+EXPORT_SYMBOL_GPL(drain_workqueue);
2432+
23842433 static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
23852434 bool wait_executing)
23862435 {
@@ -3009,34 +3058,10 @@ EXPORT_SYMBOL_GPL(__alloc_workqueue_key); · 622 unmodified lines
30093058 */
30103059 void destroy_workqueue(struct workqueue_struct *wq)
30113060 {
3012− unsigned int flush_cnt = 0;
30133061 unsigned int cpu;
30143062
3015− /*
3016− * Mark @wq dying and drain all pending works. Once WQ_DYING is
3017− * set, only chain queueing is allowed. IOW, only currently
3018− * pending or running work items on @wq can queue further work
3019− * items on it. @wq is flushed repeatedly until it becomes empty.
3020− * The number of flushing is detemined by the depth of chaining and
3021− * should be relatively short. Whine if it takes too long.
3022− */
3023− wq->flags |= WQ_DYING;
3024−reflush:
3025− flush_workqueue(wq);
3026−
3027− for_each_cwq_cpu(cpu, wq) {
3028− struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3029−
3030− if (!cwq->nr_active && list_empty(&cwq->delayed_works))
3031− continue;
3032−
3033− if (++flush_cnt == 10 ||
3034− (flush_cnt % 100 == 0 && flush_cnt <= 1000))
3035− printk(KERN_WARNING "workqueue %s: flush on "
3036− "destruction isn't complete after %u tries\n",
3037− wq->name, flush_cnt);
3038− goto reflush;
3039− }
3063+ /* drain it before proceeding with destruction */
3064+ drain_workqueue(wq);
30403065
30413066 /*
30423067 * wq list is used to freeze wq, remove from list after