From patchwork Tue Sep 3 18:10:55 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Austin X-Patchwork-Id: 2853368 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E6300C0AB5 for ; Tue, 3 Sep 2013 18:12:06 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E4E28204D8 for ; Tue, 3 Sep 2013 18:12:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id AD079201F7 for ; Tue, 3 Sep 2013 18:12:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760332Ab3ICSLw (ORCPT ); Tue, 3 Sep 2013 14:11:52 -0400 Received: from service87.mimecast.com ([91.220.42.44]:40812 "EHLO service87.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1760322Ab3ICSLu (ORCPT ); Tue, 3 Sep 2013 14:11:50 -0400 Received: from cam-owa1.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.21]) by service87.mimecast.com; Tue, 03 Sep 2013 19:11:49 +0100 Received: from e102895-lin.cambridge.arm.com ([10.1.255.212]) by cam-owa1.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.0); Tue, 3 Sep 2013 19:11:48 +0100 From: Jonathan Austin To: kvm@vger.kernel.org Cc: penberg@kernel.org, marc.zyngier@arm.com, will.deacon@arm.com, Matt.Evans@arm.com, Jonathan Austin Subject: [PATCH 3/3] kvm tools: stop virtio console doing unnecessary input handling Date: Tue, 3 Sep 2013 19:10:55 +0100 Message-Id: <1378231855-13834-4-git-send-email-jonathan.austin@arm.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1378231855-13834-1-git-send-email-jonathan.austin@arm.com> References: <1378231855-13834-1-git-send-email-jonathan.austin@arm.com> X-OriginalArrivalTime: 03 Sep 2013 18:11:48.0239 (UTC) FILETIME=[0E54C1F0:01CEA8D1] X-MC-Unique: 113090319114901601 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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);