Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Jonathan Austin <jonathan.austin@arm.com>
To: kvm@vger.kernel.org
Cc: penberg@kernel.org, marc.zyngier@arm.com, will.deacon@arm.com,
	Matt.Evans@arm.com, Jonathan Austin <jonathan.austin@arm.com>
Subject: [PATCH v2 3/3] kvm tools: stop virtio console doing unnecessary input handling
Date: Wed,  4 Sep 2013 14:25:48 +0100	[thread overview]
Message-ID: <1378301148-18823-4-git-send-email-jonathan.austin@arm.com> (raw)
In-Reply-To: <1378301148-18823-1-git-send-email-jonathan.austin@arm.com>

The asynchronous nature of the virtio input handling (using a job queue)
can result in unnecessary jobs being created if there is some delay in
handing input (the original function to handle the input returns immediately
without the file having been read, and hence poll returns immediately
informing us of data to read).

This patch adds synchronisation to the threads so that we don't start
polling input files again until we've read from the console.

Signed-off-by: Jonathan Austin <jonathan.austin@arm.com>
Acked-by: Marc Zyngier <marc.zyngier@arm.com>
---
 tools/kvm/virtio/console.c |   23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/tools/kvm/virtio/console.c b/tools/kvm/virtio/console.c
index 83c58bf..f982dab7 100644
--- a/tools/kvm/virtio/console.c
+++ b/tools/kvm/virtio/console.c
@@ -36,12 +36,17 @@ struct con_dev {
 	struct virtio_console_config	config;
 	u32				features;
 
+	pthread_cond_t			poll_cond;
+	int				vq_ready;
+
 	struct thread_pool__job		jobs[VIRTIO_CONSOLE_NUM_QUEUES];
 };
 
 static struct con_dev cdev = {
 	.mutex				= MUTEX_INITIALIZER,
 
+	.vq_ready			= 0,
+
 	.config = {
 		.cols			= 80,
 		.rows			= 24,
@@ -69,6 +74,9 @@ static void virtio_console__inject_interrupt_callback(struct kvm *kvm, void *par
 
 	vq = param;
 
+	if (!cdev.vq_ready)
+		pthread_cond_wait(&cdev.poll_cond, &cdev.mutex.mutex);
+
 	if (term_readable(0) && virt_queue__available(vq)) {
 		head = virt_queue__get_iov(vq, iov, &out, &in, kvm);
 		len = term_getc_iov(kvm, iov, in, 0);
@@ -81,7 +89,8 @@ static void virtio_console__inject_interrupt_callback(struct kvm *kvm, void *par
 
 void virtio_console__inject_interrupt(struct kvm *kvm)
 {
-	thread_pool__do_job(&cdev.jobs[VIRTIO_CONSOLE_RX_QUEUE]);
+	virtio_console__inject_interrupt_callback(kvm,
+					&cdev.vqs[VIRTIO_CONSOLE_RX_QUEUE]);
 }
 
 static void virtio_console_handle_callback(struct kvm *kvm, void *param)
@@ -141,10 +150,16 @@ static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align,
 
 	vring_init(&queue->vring, VIRTIO_CONSOLE_QUEUE_SIZE, p, align);
 
-	if (vq == VIRTIO_CONSOLE_TX_QUEUE)
+	if (vq == VIRTIO_CONSOLE_TX_QUEUE) {
 		thread_pool__init_job(&cdev.jobs[vq], kvm, virtio_console_handle_callback, queue);
-	else if (vq == VIRTIO_CONSOLE_RX_QUEUE)
+	} else if (vq == VIRTIO_CONSOLE_RX_QUEUE) {
 		thread_pool__init_job(&cdev.jobs[vq], kvm, virtio_console__inject_interrupt_callback, queue);
+		/* Tell the waiting poll thread that we're ready to go */
+		mutex_lock(&cdev.mutex);
+		cdev.vq_ready = 1;
+		pthread_cond_signal(&cdev.poll_cond);
+		mutex_unlock(&cdev.mutex);
+	}
 
 	return 0;
 }
@@ -192,6 +207,8 @@ int virtio_console__init(struct kvm *kvm)
 	if (kvm->cfg.active_console != CONSOLE_VIRTIO)
 		return 0;
 
+	pthread_cond_init(&cdev.poll_cond, NULL);
+
 	virtio_init(kvm, &cdev, &cdev.vdev, &con_dev_virtio_ops,
 		    VIRTIO_DEFAULT_TRANS, PCI_DEVICE_ID_VIRTIO_CONSOLE,
 		    VIRTIO_ID_CONSOLE, PCI_CLASS_CONSOLE);
-- 
1.7.9.5



      parent reply	other threads:[~2013-09-04 13:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-04 13:25 [PATCH v2 0/3] kvm tools: remove periodic tick Jonathan Austin
2013-09-04 13:25 ` [PATCH v2 1/3] kvm tools: use #define for maximum number of terminal devices Jonathan Austin
2013-09-04 13:25 ` [PATCH v2 2/3] kvm tools: remove periodic tick in favour of a polling thread Jonathan Austin
2013-09-04 15:58   ` Pekka Enberg
2013-09-04 17:40     ` Jonathan Austin
2013-09-04 17:48       ` Pekka Enberg
2013-09-04 18:01         ` Sasha Levin
2013-09-05 16:39           ` Jonathan Austin
2013-09-07 17:21             ` Sasha Levin
2013-09-04 13:25 ` Jonathan Austin [this message]

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=1378301148-18823-4-git-send-email-jonathan.austin@arm.com \
    --to=jonathan.austin@arm.com \
    --cc=Matt.Evans@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=marc.zyngier@arm.com \
    --cc=penberg@kernel.org \
    --cc=will.deacon@arm.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox