From patchwork Wed Sep 11 17:32:49 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Austin X-Patchwork-Id: 2874031 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 22E3B9F476 for ; Wed, 11 Sep 2013 17:36:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 066FD201E7 for ; Wed, 11 Sep 2013 17:36:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D11942012F for ; Wed, 11 Sep 2013 17:36:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756359Ab3IKRdK (ORCPT ); Wed, 11 Sep 2013 13:33:10 -0400 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 Received: from cam-owa1.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.21]) by service87.mimecast.com; Wed, 11 Sep 2013 18:33:05 +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); Wed, 11 Sep 2013 18:33:02 +0100 From: Jonathan Austin To: kvm@vger.kernel.org Cc: penberg@kernel.org, marc.zyngier@arm.com, will.deacon@arm.com, 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> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1378920769-24258-1-git-send-email-jonathan.austin@arm.com> References: <1378920769-24258-1-git-send-email-jonathan.austin@arm.com> X-OriginalArrivalTime: 11 Sep 2013 17:33:02.0991 (UTC) FILETIME=[F7AE11F0:01CEAF14] X-MC-Unique: 113091118330509101 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-7.7 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 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 { 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);