From: Jonathan Austin Subject: [PATCH v3 3/3] kvm tools: stop virtio console doing unnecessary input handling Date: Wed, 11 Sep 2013 18:32:49 +0100 Message-ID: <1378920769-24258-4-git-send-email-jonathan.austin@arm.com> References: <1378920769-24258-1-git-send-email-jonathan.austin@arm.com> Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Cc: penberg@kernel.org, marc.zyngier@arm.com, will.deacon@arm.com, Jonathan Austin To: kvm@vger.kernel.org Return-path: Received: from service87.mimecast.com ([91.220.42.44]:41207 "EHLO service87.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755045Ab3IKRdH (ORCPT ); Wed, 11 Sep 2013 13:33:07 -0400 In-Reply-To: <1378920769-24258-1-git-send-email-jonathan.austin@arm.com> Sender: kvm-owner@vger.kernel.org List-ID: 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 immediatel= y 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 Acked-by: Marc Zyngier --- 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 { =09struct virtio_console_config=09config; =09u32=09=09=09=09features; =20 +=09pthread_cond_t=09=09=09poll_cond; +=09int=09=09=09=09vq_ready; + =09struct thread_pool__job=09=09jobs[VIRTIO_CONSOLE_NUM_QUEUES]; }; =20 static struct con_dev cdev =3D { =09.mutex=09=09=09=09=3D MUTEX_INITIALIZER, =20 +=09.vq_ready=09=09=09=3D 0, + =09.config =3D { =09=09.cols=09=09=09=3D 80, =09=09.rows=09=09=09=3D 24, @@ -69,6 +74,9 @@ static void virtio_console__inject_interrupt_callback(str= uct kvm *kvm, void *par =20 =09vq =3D param; =20 +=09if (!cdev.vq_ready) +=09=09pthread_cond_wait(&cdev.poll_cond, &cdev.mutex.mutex); + =09if (term_readable(0) && virt_queue__available(vq)) { =09=09head =3D virt_queue__get_iov(vq, iov, &out, &in, kvm); =09=09len =3D term_getc_iov(kvm, iov, in, 0); @@ -81,7 +89,8 @@ static void virtio_console__inject_interrupt_callback(str= uct kvm *kvm, void *par =20 void virtio_console__inject_interrupt(struct kvm *kvm) { -=09thread_pool__do_job(&cdev.jobs[VIRTIO_CONSOLE_RX_QUEUE]); +=09virtio_console__inject_interrupt_callback(kvm, +=09=09=09=09=09&cdev.vqs[VIRTIO_CONSOLE_RX_QUEUE]); } =20 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, =20 =09vring_init(&queue->vring, VIRTIO_CONSOLE_QUEUE_SIZE, p, align); =20 -=09if (vq =3D=3D VIRTIO_CONSOLE_TX_QUEUE) +=09if (vq =3D=3D VIRTIO_CONSOLE_TX_QUEUE) { =09=09thread_pool__init_job(&cdev.jobs[vq], kvm, virtio_console_handle_cal= lback, queue); -=09else if (vq =3D=3D VIRTIO_CONSOLE_RX_QUEUE) +=09} else if (vq =3D=3D VIRTIO_CONSOLE_RX_QUEUE) { =09=09thread_pool__init_job(&cdev.jobs[vq], kvm, virtio_console__inject_in= terrupt_callback, queue); +=09=09/* Tell the waiting poll thread that we're ready to go */ +=09=09mutex_lock(&cdev.mutex); +=09=09cdev.vq_ready =3D 1; +=09=09pthread_cond_signal(&cdev.poll_cond); +=09=09mutex_unlock(&cdev.mutex); +=09} =20 =09return 0; } @@ -192,6 +207,8 @@ int virtio_console__init(struct kvm *kvm) =09if (kvm->cfg.active_console !=3D CONSOLE_VIRTIO) =09=09return 0; =20 +=09pthread_cond_init(&cdev.poll_cond, NULL); + =09virtio_init(kvm, &cdev, &cdev.vdev, &con_dev_virtio_ops, =09=09 VIRTIO_DEFAULT_TRANS, PCI_DEVICE_ID_VIRTIO_CONSOLE, =09=09 VIRTIO_ID_CONSOLE, PCI_CLASS_CONSOLE); --=20 1.7.9.5