linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH, RFC] reimplement flush_workqueue()
@ 2006-12-17 22:34 Oleg Nesterov
  2006-12-18  3:09 ` Linus Torvalds
                   ` (2 more replies)
  0 siblings, 3 replies; 78+ messages in thread
From: Oleg Nesterov @ 2006-12-17 22:34 UTC (permalink / raw)
  To: Andrew Morton
  Cc: David Howells, Christoph Hellwig, Ingo Molnar, Linus Torvalds,
	linux-kernel

Remove ->remove_sequence, ->insert_sequence, and ->work_done from
struct cpu_workqueue_struct. To implement flush_workqueue() we can
queue a barrier work on each CPU and wait for its completition.

We don't need to worry about CPU going down while we are are sleeping
on the completition. take_over_work() will move this work on another
CPU, and the handler will wake up us eventually.

NOTE: I removed 'int cpu' parameter, flush_workqueue() locks/unlocks
workqueue_mutex unconditionally. It may be restored, but I think it
doesn't make much sense, we take the mutex for the very short time,
and the code becomes simpler.

Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru>

 workqueue.c |   87 ++++++++++++++++++++----------------------------------------
 1 files changed, 29 insertions(+), 58 deletions(-)

--- mm-6.20-rc1/kernel/workqueue.c~1_flush_q	2006-12-18 00:56:58.000000000 +0300
+++ mm-6.20-rc1/kernel/workqueue.c	2006-12-18 01:13:22.000000000 +0300
@@ -36,23 +36,13 @@
 /*
  * The per-CPU workqueue (if single thread, we always use the first
  * possible cpu).
- *
- * The sequence counters are for flush_scheduled_work().  It wants to wait
- * until all currently-scheduled works are completed, but it doesn't
- * want to be livelocked by new, incoming ones.  So it waits until
- * remove_sequence is >= the insert_sequence which pertained when
- * flush_scheduled_work() was called.
  */
 struct cpu_workqueue_struct {
 
 	spinlock_t lock;
 
-	long remove_sequence;	/* Least-recently added (next to run) */
-	long insert_sequence;	/* Next to add */
-
 	struct list_head worklist;
 	wait_queue_head_t more_work;
-	wait_queue_head_t work_done;
 
 	struct workqueue_struct *wq;
 	struct task_struct *thread;
@@ -138,8 +128,6 @@ static int __run_work(struct cpu_workque
 		f(work);
 
 		spin_lock_irqsave(&cwq->lock, flags);
-		cwq->remove_sequence++;
-		wake_up(&cwq->work_done);
 		ret = 1;
 	}
 	spin_unlock_irqrestore(&cwq->lock, flags);
@@ -187,7 +175,6 @@ static void __queue_work(struct cpu_work
 	spin_lock_irqsave(&cwq->lock, flags);
 	set_wq_data(work, cwq);
 	list_add_tail(&work->entry, &cwq->worklist);
-	cwq->insert_sequence++;
 	wake_up(&cwq->more_work);
 	spin_unlock_irqrestore(&cwq->lock, flags);
 }
@@ -338,8 +325,6 @@ static void run_workqueue(struct cpu_wor
 		}
 
 		spin_lock_irqsave(&cwq->lock, flags);
-		cwq->remove_sequence++;
-		wake_up(&cwq->work_done);
 	}
 	cwq->run_depth--;
 	spin_unlock_irqrestore(&cwq->lock, flags);
@@ -394,45 +379,39 @@ static int worker_thread(void *__cwq)
 	return 0;
 }
 
-/*
- * If cpu == -1 it's a single-threaded workqueue and the caller does not hold
- * workqueue_mutex
- */
-static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq, int cpu)
+struct wq_barrier {
+	struct work_struct	work;
+	struct completion	done;
+};
+
+static void wq_barrier_func(struct work_struct *work)
+{
+	struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
+	complete(&barr->done);
+}
+
+static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
 {
 	if (cwq->thread == current) {
 		/*
 		 * Probably keventd trying to flush its own queue. So simply run
 		 * it by hand rather than deadlocking.
 		 */
-		if (cpu != -1)
-			mutex_unlock(&workqueue_mutex);
+		mutex_unlock(&workqueue_mutex);
 		run_workqueue(cwq);
-		if (cpu != -1)
-			mutex_lock(&workqueue_mutex);
+		mutex_lock(&workqueue_mutex);
 	} else {
-		DEFINE_WAIT(wait);
-		long sequence_needed;
+		struct wq_barrier barr = {
+			.work = __WORK_INITIALIZER(barr.work, wq_barrier_func),
+			.done = COMPLETION_INITIALIZER_ONSTACK(barr.done),
+		};
 
-		spin_lock_irq(&cwq->lock);
-		sequence_needed = cwq->insert_sequence;
+		__set_bit(WORK_STRUCT_PENDING, &barr.work.management);
+		__queue_work(cwq, &barr.work);
 
-		while (sequence_needed - cwq->remove_sequence > 0) {
-			prepare_to_wait(&cwq->work_done, &wait,
-					TASK_UNINTERRUPTIBLE);
-			spin_unlock_irq(&cwq->lock);
-			if (cpu != -1)
-				mutex_unlock(&workqueue_mutex);
-			schedule();
-			if (cpu != -1) {
-				mutex_lock(&workqueue_mutex);
-				if (!cpu_online(cpu))
-					return; /* oops, CPU unplugged */
-			}
-			spin_lock_irq(&cwq->lock);
-		}
-		finish_wait(&cwq->work_done, &wait);
-		spin_unlock_irq(&cwq->lock);
+		mutex_unlock(&workqueue_mutex);
+		wait_for_completion(&barr.done);
+		mutex_lock(&workqueue_mutex);
 	}
 }
 
@@ -443,30 +422,25 @@ static void flush_cpu_workqueue(struct c
  * Forces execution of the workqueue and blocks until its completion.
  * This is typically used in driver shutdown handlers.
  *
- * This function will sample each workqueue's current insert_sequence number and
- * will sleep until the head sequence is greater than or equal to that.  This
- * means that we sleep until all works which were queued on entry have been
- * handled, but we are not livelocked by new incoming ones.
+ * We sleep until all works which were queued on entry have been handled,
+ * but we are not livelocked by new incoming ones.
  *
  * This function used to run the workqueues itself.  Now we just wait for the
  * helper threads to do it.
  */
 void fastcall flush_workqueue(struct workqueue_struct *wq)
 {
-	might_sleep();
-
+	mutex_lock(&workqueue_mutex);
 	if (is_single_threaded(wq)) {
 		/* Always use first cpu's area. */
-		flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu),
-					-1);
+		flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
 	} else {
 		int cpu;
 
-		mutex_lock(&workqueue_mutex);
 		for_each_online_cpu(cpu)
-			flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu), cpu);
-		mutex_unlock(&workqueue_mutex);
+			flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
 	}
+	mutex_unlock(&workqueue_mutex);
 }
 EXPORT_SYMBOL_GPL(flush_workqueue);
 
@@ -479,12 +453,9 @@ static struct task_struct *create_workqu
 	spin_lock_init(&cwq->lock);
 	cwq->wq = wq;
 	cwq->thread = NULL;
-	cwq->insert_sequence = 0;
-	cwq->remove_sequence = 0;
 	cwq->freezeable = freezeable;
 	INIT_LIST_HEAD(&cwq->worklist);
 	init_waitqueue_head(&cwq->more_work);
-	init_waitqueue_head(&cwq->work_done);
 
 	if (is_single_threaded(wq))
 		p = kthread_create(worker_thread, cwq, "%s", wq->name);


^ permalink raw reply	[flat|nested] 78+ messages in thread

end of thread, other threads:[~2007-01-17 17:03 UTC | newest]

Thread overview: 78+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-17 22:34 [PATCH, RFC] reimplement flush_workqueue() Oleg Nesterov
2006-12-18  3:09 ` Linus Torvalds
2006-12-19  0:27 ` Andrew Morton
2006-12-19  0:43   ` Oleg Nesterov
2006-12-19  1:00     ` Andrew Morton
2007-01-04 11:32     ` Srivatsa Vaddagiri
2007-01-04 14:29       ` Oleg Nesterov
2007-01-04 15:56         ` Srivatsa Vaddagiri
2007-01-04 16:31           ` Oleg Nesterov
2007-01-04 16:57             ` Srivatsa Vaddagiri
2007-01-04 17:18         ` Andrew Morton
2007-01-04 18:09           ` Oleg Nesterov
2007-01-04 18:31             ` Andrew Morton
2007-01-05  9:03               ` Srivatsa Vaddagiri
2007-01-05 14:07                 ` Oleg Nesterov
2007-01-06 15:24                   ` Srivatsa Vaddagiri
2007-01-05  8:56           ` Srivatsa Vaddagiri
2007-01-05 12:42             ` Oleg Nesterov
2007-01-06 15:11               ` Srivatsa Vaddagiri
2007-01-06 15:10           ` [PATCH] fix-flush_workqueue-vs-cpu_dead-race-update Oleg Nesterov
2007-01-06 15:45             ` Srivatsa Vaddagiri
2007-01-06 16:30               ` Oleg Nesterov
2007-01-06 16:38                 ` Srivatsa Vaddagiri
2007-01-06 17:34                   ` Oleg Nesterov
2007-01-07 10:43                     ` Srivatsa Vaddagiri
2007-01-07 12:56                       ` Oleg Nesterov
2007-01-07 14:22                         ` Oleg Nesterov
2007-01-07 14:42                           ` Oleg Nesterov
2007-01-07 16:43                           ` Srivatsa Vaddagiri
2007-01-07 17:01                             ` Srivatsa Vaddagiri
2007-01-07 17:33                               ` Oleg Nesterov
2007-01-07 17:18                             ` Oleg Nesterov
2007-01-07 16:21                         ` Srivatsa Vaddagiri
2007-01-07 17:09                           ` Oleg Nesterov
2007-01-06 19:11                   ` Andrew Morton
2007-01-06 19:13                     ` Ingo Molnar
2007-01-07 11:00                     ` Srivatsa Vaddagiri
2007-01-07 19:59                       ` Andrew Morton
2007-01-07 21:01                         ` [PATCH] flush_cpu_workqueue: don't flush an empty ->worklist Oleg Nesterov
2007-01-08 23:54                           ` Andrew Morton
2007-01-09  5:04                             ` Srivatsa Vaddagiri
2007-01-09  5:26                               ` Andrew Morton
2007-01-09  6:56                                 ` Ingo Molnar
2007-01-09  9:33                                 ` Srivatsa Vaddagiri
2007-01-09  9:44                                   ` Ingo Molnar
2007-01-09  9:51                                   ` Andrew Morton
2007-01-09 10:09                                     ` Srivatsa Vaddagiri
2007-01-09 10:15                                       ` Andrew Morton
2007-01-09 15:07                                 ` Oleg Nesterov
2007-01-09 15:59                                   ` Srivatsa Vaddagiri
2007-01-09 16:38                                     ` Oleg Nesterov
2007-01-09 16:46                                       ` Srivatsa Vaddagiri
2007-01-09 16:56                                         ` Oleg Nesterov
2007-01-14 23:54                                           ` Oleg Nesterov
2007-01-15  4:33                                             ` Srivatsa Vaddagiri
2007-01-15 12:54                                               ` Oleg Nesterov
2007-01-15 13:08                                                 ` Oleg Nesterov
2007-01-15 16:18                                                 ` Srivatsa Vaddagiri
2007-01-15 16:55                                                   ` Oleg Nesterov
2007-01-16  5:26                                                     ` Srivatsa Vaddagiri
2007-01-16 13:27                                                       ` Oleg Nesterov
2007-01-17  6:17                                                         ` Srivatsa Vaddagiri
2007-01-17 15:47                                                           ` Oleg Nesterov
2007-01-17 16:12                                                             ` Srivatsa Vaddagiri
2007-01-17 17:01                                                               ` Oleg Nesterov
2007-01-17 16:25                                                             ` Srivatsa Vaddagiri
2007-01-07 21:51                         ` [PATCH] fix-flush_workqueue-vs-cpu_dead-race-update Oleg Nesterov
2007-01-08 15:22                           ` Srivatsa Vaddagiri
2007-01-08 15:56                             ` Oleg Nesterov
2007-01-08 16:31                               ` Srivatsa Vaddagiri
2007-01-08 17:06                                 ` Oleg Nesterov
2007-01-08 18:37                                   ` Pallipadi, Venkatesh
2007-01-09  1:11                                     ` Srivatsa Vaddagiri
2007-01-09  4:39                                   ` Srivatsa Vaddagiri
2007-01-09 14:38                                     ` Oleg Nesterov
2007-01-08 15:37                         ` Srivatsa Vaddagiri
2007-01-04 12:02 ` [PATCH, RFC] reimplement flush_workqueue() Srivatsa Vaddagiri
2007-01-04 14:38   ` Oleg Nesterov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).