All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Tejun Heo <tj@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>,
	Sridhar Samudrala <sri@us.ibm.com>,
	netdev <netdev@vger.kernel.org>,
	lkml <linux-kernel@vger.kernel.org>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Dmitri Vorobiev <dmitri.vorobiev@movial.com>,
	Jiri Kosina <jkosina@suse.cz>,
	Thomas Gleixner <tglx@linutronix.de>, Ingo Molnar <mingo@elte.hu>,
	Andi Kleen <ak@linux.intel.com>
Subject: Re: [PATCH UPDATED 1/3] vhost: replace vhost_workqueue with per-vhost kthread
Date: Thu, 22 Jul 2010 18:58:40 +0300	[thread overview]
Message-ID: <20100722155840.GA1743@redhat.com> (raw)
In-Reply-To: <4C06A580.9060300@kernel.org>

On Wed, Jun 02, 2010 at 08:40:00PM +0200, Tejun Heo wrote:
> Replace vhost_workqueue with per-vhost kthread.  Other than callback
> argument change from struct work_struct * to struct vhost_work *,
> there's no visible change to vhost_poll_*() interface.
> 
> This conversion is to make each vhost use a dedicated kthread so that
> resource control via cgroup can be applied.
> 
> Partially based on Sridhar Samudrala's patch.
> 
> * Updated to use sub structure vhost_work instead of directly using
>   vhost_poll at Michael's suggestion.
> 
> * Added flusher wake_up() optimization at Michael's suggestion.
> 
> Signed-off-by: Tejun Heo <tj@kernel.org>
> Cc: Michael S. Tsirkin <mst@redhat.com>
> Cc: Sridhar Samudrala <samudrala.sridhar@gmail.com>

All the tricky barrier pairing made me uncomfortable.  So I came up with
this on top (untested): if we do all operations under the spinlock, we
can get by without barriers and atomics.  And since we need the lock for
list operations anyway, this should have no paerformance impact.

What do you think?


diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 0c6b533..7730a30 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -73,7 +73,7 @@ void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
 	INIT_LIST_HEAD(&work->node);
 	work->fn = fn;
 	init_waitqueue_head(&work->done);
-	atomic_set(&work->flushing, 0);
+	work->flushing = 0;
 	work->queue_seq = work->done_seq = 0;
 }
 
@@ -99,13 +99,23 @@ void vhost_poll_stop(struct vhost_poll *poll)
 void vhost_poll_flush(struct vhost_poll *poll)
 {
 	struct vhost_work *work = &poll->work;
-	int seq = work->queue_seq;
+	unsigned seq, left;
+	int flushing;
 
-	atomic_inc(&work->flushing);
-	smp_mb__after_atomic_inc();	/* mb flush-b0 paired with worker-b1 */
-	wait_event(work->done, seq - work->done_seq <= 0);
-	atomic_dec(&work->flushing);
-	smp_mb__after_atomic_dec();	/* rmb flush-b1 paired with worker-b0 */
+	spin_lock_irq(&dev->work_lock);
+	seq = work->queue_seq;
+	work->flushing++;
+	spin_unlock_irq(&dev->work_lock);
+	wait_event(work->done, {
+		   spin_lock_irq(&dev->work_lock);
+		   left = work->done_seq - seq;
+		   spin_unlock_irq(&dev->work_lock);
+		   left < UINT_MAX / 2;
+	});
+	spin_lock_irq(&dev->work_lock);
+	flushing = --work->flushing;
+	spin_unlock_irq(&dev->work_lock);
+	BUG_ON(flushing < 0);
 }
 
 void vhost_poll_queue(struct vhost_poll *poll)
@@ -151,37 +161,37 @@ static void vhost_vq_reset(struct vhost_dev *dev,
 static int vhost_worker(void *data)
 {
 	struct vhost_dev *dev = data;
-	struct vhost_work *work;
+	struct vhost_work *work = NULL;
 
-repeat:
-	set_current_state(TASK_INTERRUPTIBLE);	/* mb paired w/ kthread_stop */
+	for (;;) {
+		set_current_state(TASK_INTERRUPTIBLE);	/* mb paired w/ kthread_stop */
 
-	if (kthread_should_stop()) {
-		__set_current_state(TASK_RUNNING);
-		return 0;
-	}
+		if (kthread_should_stop()) {
+			__set_current_state(TASK_RUNNING);
+			return 0;
+		}
 
-	work = NULL;
-	spin_lock_irq(&dev->work_lock);
-	if (!list_empty(&dev->work_list)) {
-		work = list_first_entry(&dev->work_list,
-					struct vhost_work, node);
-		list_del_init(&work->node);
-	}
-	spin_unlock_irq(&dev->work_lock);
+		spin_lock_irq(&dev->work_lock);
+		if (work) {
+			work->done_seq = work->queue_seq;
+			if (work->flushing)
+				wake_up_all(&work->done);
+		}
+		if (!list_empty(&dev->work_list)) {
+			work = list_first_entry(&dev->work_list,
+						struct vhost_work, node);
+			list_del_init(&work->node);
+		} else
+			work = NULL;
+		spin_unlock_irq(&dev->work_lock);
+
+		if (work) {
+			__set_current_state(TASK_RUNNING);
+			work->fn(work);
+		} else
+			schedule();
 
-	if (work) {
-		__set_current_state(TASK_RUNNING);
-		work->fn(work);
-		smp_wmb();	/* wmb worker-b0 paired with flush-b1 */
-		work->done_seq = work->queue_seq;
-		smp_mb();	/* mb worker-b1 paired with flush-b0 */
-		if (atomic_read(&work->flushing))
-			wake_up_all(&work->done);
-	} else
-		schedule();
-
-	goto repeat;
+	}
 }
 
 long vhost_dev_init(struct vhost_dev *dev,
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index 0e63091..3693327 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h
@@ -27,9 +27,9 @@ struct vhost_work {
 	struct list_head	  node;
 	vhost_work_fn_t		  fn;
 	wait_queue_head_t	  done;
-	atomic_t		  flushing;
-	int			  queue_seq;
-	int			  done_seq;
+	int			  flushing;
+	unsigned		  queue_seq;
+	unsigned		  done_seq;
 };
 
 /* Poll a file (eventfd or socket) */

  parent reply	other threads:[~2010-07-22 16:04 UTC|newest]

Thread overview: 115+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-19  0:04 [PATCH 2/3] workqueue: Add an API to create a singlethread workqueue attached to the current task's cgroup Sridhar Samudrala
2010-05-27  9:14 ` Michael S. Tsirkin
2010-05-27 12:44   ` Oleg Nesterov
2010-05-27 13:12     ` Michael S. Tsirkin
2010-05-27 13:48       ` Oleg Nesterov
2010-05-27 16:15       ` Tejun Heo
2010-05-27 16:39         ` Michael S. Tsirkin
2010-05-27 16:56           ` Tejun Heo
2010-05-27 17:32             ` Michael S. Tsirkin
2010-05-27 21:20               ` Tejun Heo
2010-05-28 15:08                 ` Michael S. Tsirkin
2010-05-28 15:54                   ` Tejun Heo
2010-05-30 11:29                     ` Michael S. Tsirkin
2010-05-30 20:24                       ` [PATCH 1/3] vhost: replace vhost_workqueue with per-vhost kthread Tejun Heo
2010-05-31 14:39                         ` Oleg Nesterov
2010-05-31 15:07                           ` Tejun Heo
2010-05-31 15:31                             ` Oleg Nesterov
2010-05-31 15:38                               ` Tejun Heo
2010-05-31 15:22                         ` Michael S. Tsirkin
2010-05-31 15:45                           ` Tejun Heo
2010-05-31 16:00                             ` Michael S. Tsirkin
2010-06-01  9:34                               ` Tejun Heo
2010-06-02 18:40                                 ` [PATCH UPDATED " Tejun Heo
2010-06-02 21:34                                   ` Sridhar Samudrala
2010-07-22 15:58                                   ` Michael S. Tsirkin [this message]
2010-07-22 21:21                                     ` Tejun Heo
2010-07-24 19:14                                       ` Michael S. Tsirkin
2010-07-25  7:41                                         ` Tejun Heo
2010-07-25 10:04                                           ` Michael S. Tsirkin
2010-07-26 15:25                                           ` Michael S. Tsirkin
2010-07-26 15:34                                             ` Tejun Heo
2010-07-26 15:46                                               ` Tejun Heo
2010-07-26 15:51                                                 ` Michael S. Tsirkin
2010-07-26 15:50                                               ` Michael S. Tsirkin
2010-07-26 16:05                                                 ` Tejun Heo
2010-07-26 16:14                                                   ` Tejun Heo
2010-07-26 16:31                                                     ` Michael S. Tsirkin
2010-07-26 18:51                                                       ` Tejun Heo
2010-07-26 19:57                                                         ` Michael S. Tsirkin
2010-07-27  8:18                                                           ` Tejun Heo
2010-07-26 16:51                                                     ` Michael S. Tsirkin
2010-07-26 19:14                                                       ` Tejun Heo
2010-07-26 19:31                                                         ` Tejun Heo
2010-07-26 19:59                                                           ` Michael S. Tsirkin
2010-07-27 19:19                                                           ` Michael S. Tsirkin
2010-07-28  7:48                                                             ` Tejun Heo
2010-07-28 10:48                                                               ` Michael S. Tsirkin
2010-07-28 12:00                                                                 ` Tejun Heo
2010-07-26 16:57                                                     ` Michael S. Tsirkin
2010-07-26 16:23                                                   ` Michael S. Tsirkin
2010-07-26 19:04                                                     ` Tejun Heo
2010-07-26 20:19                                                       ` Michael S. Tsirkin
2010-07-27  8:21                                                         ` Tejun Heo
2010-06-01  9:34                               ` [PATCH 2/3] cgroups: Add an API to attach a task to current task's cgroup Tejun Heo
2010-06-01  9:35                               ` [PATCH 3/3] vhost: apply cpumask and cgroup to vhost workers Tejun Heo
2010-06-01 10:17                                 ` Michael S. Tsirkin
2010-06-01 10:56                                   ` Tejun Heo
2010-06-01 17:19                                 ` Sridhar Samudrala
2010-06-01 23:59                                   ` Tejun Heo
2010-06-01 14:13                           ` [PATCH 1/3] vhost: replace vhost_workqueue with per-vhost kthread Paul E. McKenney
2010-05-30 20:24                       ` [PATCH 2/3] cgroups: Add an API to attach a task to current task's cgroup Tejun Heo
2010-05-31  1:07                         ` Li Zefan
2010-05-31  7:00                           ` Tejun Heo
2010-05-30 20:25                       ` [PATCH 3/3] vhost: apply cpumask and cgroup to vhost pollers Tejun Heo
2010-05-31  1:11                         ` Li Zefan
2010-05-31  6:58                           ` [PATCH UPDATED " Tejun Heo
2010-05-31  7:48                             ` Li Zefan
2010-05-31 10:20                               ` [PATCH UPDATED2 " Tejun Heo
2010-06-24  8:11                         ` [PATCH " Michael S. Tsirkin
2010-06-24 22:45                           ` Sridhar Samudrala
2010-06-25 10:10                             ` [PATCH] sched: export sched_set/getaffinity (was Re: [PATCH 3/3] vhost: apply cpumask and cgroup to vhost pollers) Michael S. Tsirkin
2010-07-01 11:07                               ` [PATCH repost] sched: export sched_set/getaffinity to modules Michael S. Tsirkin
2010-07-01 11:19                                 ` Peter Zijlstra
2010-07-01 11:43                                   ` Peter Zijlstra
2010-07-01 11:55                                     ` Michael S. Tsirkin
2010-07-01 12:23                                       ` Michael S. Tsirkin
2010-07-01 12:34                                         ` Peter Zijlstra
2010-07-01 12:46                                           ` Peter Zijlstra
2010-07-01 13:08                                             ` Michael S. Tsirkin
2010-07-01 13:30                                               ` Peter Zijlstra
2010-07-01 13:39                                                 ` Michael S. Tsirkin
2010-07-01 13:57                                                   ` Peter Zijlstra
2010-07-01 14:27                                                   ` Tejun Heo
2010-07-01 14:46                                                     ` Oleg Nesterov
2010-07-01 14:53                                                       ` Tejun Heo
2010-07-01 14:55                                                         ` Peter Zijlstra
2010-07-02 18:01                                                           ` Sridhar Samudrala
2010-07-02 18:11                                                             ` Peter Zijlstra
2010-07-02 21:06                                                               ` Oleg Nesterov
2010-07-04  9:00                                                                 ` Michael S. Tsirkin
2010-07-13  6:59                                                                   ` Sridhar Samudrala
2010-07-13 11:09                                                                     ` Michael S. Tsirkin
2010-07-14 23:26                                                                       ` Sridhar Samudrala
2010-07-15  0:05                                                                         ` Oleg Nesterov
2010-07-15  5:29                                                                           ` Sridhar Samudrala
2010-07-26 17:12                                                                 ` Michael S. Tsirkin
2010-07-26 17:51                                                                   ` Sridhar Samudrala
2010-07-26 18:08                                                                     ` Oleg Nesterov
2010-07-26 19:55                                                                       ` Michael S. Tsirkin
2010-07-26 20:27                                                                       ` Michael S. Tsirkin
2010-07-27  4:55                                                                       ` Michael S. Tsirkin
2010-08-04 10:45                                                                         ` Peter Zijlstra
2010-07-27 15:41                                                                       ` Michael S. Tsirkin
2010-07-30 14:19                                                                         ` Oleg Nesterov
2010-07-30 14:31                                                                           ` Tejun Heo
2010-08-01  8:50                                                                           ` Michael S. Tsirkin
2010-08-02 15:02                                                                             ` Oleg Nesterov
2010-07-01 14:33                                                 ` Oleg Nesterov
2010-07-01 12:32                                       ` Peter Zijlstra
2010-07-01 12:50                                         ` Michael S. Tsirkin
2010-07-01 13:07                                           ` Peter Zijlstra
2010-07-01 13:22                                             ` Michael S. Tsirkin
2010-05-27 16:24     ` [PATCH 2/3] workqueue: Add an API to create a singlethread workqueue attached to the current task's cgroup Sridhar Samudrala
2010-05-27 16:41       ` Michael S. Tsirkin
2010-05-27 17:30       ` Oleg Nesterov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100722155840.GA1743@redhat.com \
    --to=mst@redhat.com \
    --cc=ak@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=dmitri.vorobiev@movial.com \
    --cc=jkosina@suse.cz \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=netdev@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=sri@us.ibm.com \
    --cc=tglx@linutronix.de \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.