From patchwork Sat Apr 9 14:35:24 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 695981 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p39EaurA014880 for ; Sat, 9 Apr 2011 14:36:56 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754203Ab1DIOgn (ORCPT ); Sat, 9 Apr 2011 10:36:43 -0400 Received: from mail-pv0-f174.google.com ([74.125.83.174]:56369 "EHLO mail-pv0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753379Ab1DIOgn (ORCPT ); Sat, 9 Apr 2011 10:36:43 -0400 Received: by pvg12 with SMTP id 12so1543143pvg.19 for ; Sat, 09 Apr 2011 07:36:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer; bh=BxWCPK8OHjrJ1/S2gSyXrLaD3STRD2/1DW9MUWvujoE=; b=Q3h8XVSwh0bwt0rEuXrAYqhye/O4EOcqYgAugMDiq+UokVPBN/BZA1NHnihM4KIBdb N2dS0Sffefv8c/vsstGPRJeV2aCYEr7hucXGowKTIp17y7wNoYKfgmKajgL/5fT/RQYP E13XLUFgz8aFxaXAkOvbY3XQZ4XNIJUBcT3VY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=bvVj9ou0jeNFpRyAYZfAWFEryvx/dDj1bL4TKMrJJLEAT4TSb5FT9JyL8XqiCu948B 4u9DG+2XGCVpEyQO8WNFN9Oj1YzizeWQgtm8cUbJIs6wxnAOS3jRQhdzJZHgBkO8w/ci 8OSvAbB5fjgBGWI5TKiuwEd4/w5P6kJdeYnuo= Received: by 10.142.63.6 with SMTP id l6mr614766wfa.178.1302359802352; Sat, 09 Apr 2011 07:36:42 -0700 (PDT) Received: from localhost.localdomain ([202.112.128.131]) by mx.google.com with ESMTPS id o1sm5221068wfl.9.2011.04.09.07.36.38 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 09 Apr 2011 07:36:41 -0700 (PDT) From: Asias He To: Pekka Enberg , Cyrill Gorcunov , Ingo Molnar Cc: kvm@vger.kernel.org, Asias He Subject: [PATCH 1/2] kvm tools: Make virtio console device code thread-safe Date: Sat, 9 Apr 2011 22:35:24 +0800 Message-Id: <1302359725-6457-1-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.4.1 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Sat, 09 Apr 2011 14:36:56 +0000 (UTC) In preparation for threaded execution, make the virtio console device code safe by introducing a per-device mutex. Signed-off-by: Asias He --- tools/kvm/virtio-console.c | 30 ++++++++++++++++++++++++------ 1 files changed, 24 insertions(+), 6 deletions(-) diff --git a/tools/kvm/virtio-console.c b/tools/kvm/virtio-console.c index fd79c61..a140aea 100644 --- a/tools/kvm/virtio-console.c +++ b/tools/kvm/virtio-console.c @@ -5,6 +5,7 @@ #include "kvm/ioport.h" #include "kvm/util.h" #include "kvm/term.h" +#include "kvm/mutex.h" #include "kvm/kvm.h" #include "kvm/pci.h" @@ -29,6 +30,8 @@ #define PCI_VIRTIO_CONSOLE_DEVNUM 2 struct console_device { + pthread_mutex_t mutex; + struct virt_queue vqs[VIRTIO_CONSOLE_NUM_QUEUES]; struct virtio_console_config console_config; uint32_t host_features; @@ -39,6 +42,8 @@ struct console_device { }; static struct console_device console_device = { + .mutex = PTHREAD_MUTEX_INITIALIZER, + .console_config = { .cols = 80, .rows = 24, @@ -59,6 +64,8 @@ void virtio_console__inject_interrupt(struct kvm *self) uint16_t head; int len; + mutex_lock(&console_device.mutex); + vq = &console_device.vqs[VIRTIO_CONSOLE_RX_QUEUE]; if (term_readable(CONSOLE_VIRTIO) && virt_queue__available(vq)) { @@ -67,6 +74,8 @@ void virtio_console__inject_interrupt(struct kvm *self) virt_queue__set_used_elem(vq, head, len); kvm__irq_line(self, VIRTIO_CONSOLE_IRQ, 1); } + + mutex_unlock(&console_device.mutex); } static bool virtio_console_pci_io_device_specific_in(void *data, unsigned long offset, int size, uint32_t count) @@ -87,13 +96,17 @@ static bool virtio_console_pci_io_device_specific_in(void *data, unsigned long o static bool virtio_console_pci_io_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count) { unsigned long offset = port - IOPORT_VIRTIO_CONSOLE; + bool ret = true; + + mutex_lock(&console_device.mutex); switch (offset) { case VIRTIO_PCI_HOST_FEATURES: ioport__write32(data, console_device.host_features); break; case VIRTIO_PCI_GUEST_FEATURES: - return false; + ret = false; + break; case VIRTIO_PCI_QUEUE_PFN: ioport__write32(data, console_device.vqs[console_device.queue_selector].pfn); break; @@ -102,7 +115,8 @@ static bool virtio_console_pci_io_in(struct kvm *self, uint16_t port, void *data break; case VIRTIO_PCI_QUEUE_SEL: case VIRTIO_PCI_QUEUE_NOTIFY: - return false; + ret = false; + break; case VIRTIO_PCI_STATUS: ioport__write8(data, console_device.status); break; @@ -114,10 +128,12 @@ static bool virtio_console_pci_io_in(struct kvm *self, uint16_t port, void *data ioport__write16(data, console_device.config_vector); break; default: - return virtio_console_pci_io_device_specific_in(data, offset, size, count); + ret = virtio_console_pci_io_device_specific_in(data, offset, size, count); }; - return true; + mutex_unlock(&console_device.mutex); + + return ret; } static void virtio_console_handle_callback(struct kvm *self, uint16_t queue_index) @@ -145,6 +161,7 @@ static void virtio_console_handle_callback(struct kvm *self, uint16_t queue_inde static bool virtio_console_pci_io_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count) { unsigned long offset = port - IOPORT_VIRTIO_CONSOLE; + bool ret = true; switch (offset) { case VIRTIO_PCI_GUEST_FEATURES: @@ -182,10 +199,11 @@ static bool virtio_console_pci_io_out(struct kvm *self, uint16_t port, void *dat case VIRTIO_MSI_QUEUE_VECTOR: break; default: - return false; + ret = false; }; - return true; + mutex_unlock(&console_device.mutex); + return ret; } static struct ioport_operations virtio_console_io_ops = {