From patchwork Sun Apr 10 05:01:05 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 696331 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 p3A52OlH019914 for ; Sun, 10 Apr 2011 05:02:25 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751068Ab1DJFCV (ORCPT ); Sun, 10 Apr 2011 01:02:21 -0400 Received: from mail-px0-f179.google.com ([209.85.212.179]:39705 "EHLO mail-px0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750817Ab1DJFCV (ORCPT ); Sun, 10 Apr 2011 01:02:21 -0400 Received: by pxi2 with SMTP id 2so2469641pxi.10 for ; Sat, 09 Apr 2011 22:02:20 -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=xusv6J7bSQp/PBX3XBpUpfmfXAN8oYOs32+fe8rCX1A=; b=dOU/GIapZVdNqRk/rjTa0O1e0VUqdP5j658QJ46Ys+fmf58CF6LunuA9ljonwc/TsB vnA02uNZOfrBUr0KuXp2Phco+6v7I15THgkOMbhXWfnE3DKheNDTkHt8wOLZPROj9+XG 05i6Euia1LZMJvcIXapZETAW5bYdzQNQTriQs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=BgnwkJH8uBtkGBcbwA2jdBh/9nvjeBet51FjzNsmPV5prkT21mwqw5hsBO4/MCvAmp 3T5HIhwSSukGGqnjAkjnnE2LtyG+PbfBwLrlRcQx/YJoaZd85y4EVUR04cq2++4leb8R 1aWYnGeemA+nIZ0NVjPD3GeKi3ke0icws/6hM= Received: by 10.142.144.20 with SMTP id r20mr3674724wfd.76.1302411740134; Sat, 09 Apr 2011 22:02:20 -0700 (PDT) Received: from localhost.localdomain ([202.112.128.131]) by mx.google.com with ESMTPS id x11sm6177831wfd.13.2011.04.09.22.02.16 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 09 Apr 2011 22:02:19 -0700 (PDT) From: Asias He To: Pekka Enberg , Cyrill Gorcunov , Ingo Molnar , akong@redhat.com Cc: kvm@vger.kernel.org, Asias He Subject: [PATCH] kvm tools: Make virt_queue__available return false if queue is not initialized. Date: Sun, 10 Apr 2011 13:01:05 +0800 Message-Id: <1302411665-5349-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]); Sun, 10 Apr 2011 05:02:27 +0000 (UTC) Also add a check in virt_queue__get_iov to make sure queue is initialized. virtio_console__inject_interrupt tries to use virt queues before guest tell us to initialize them. Besides, commit b55da01875101b55a882618f7f9af3099af21a11 kvm tools: Make virtio console device code thread-safe has made virtio console device code thread safe. (gdb) r run -i linux-0.2.img -k ./vmlinuz-2.6.38-rc6+ -r ./initrd.img-2.6.38-rc6+ -p=init=1 -m 500 -c Starting program: /project/rh/kvm-tools/tools/kvm/kvm run -i linux-0.2.img -k ./vmlinuz-2.6.38-rc6+ -r ./initrd.img-2.6.38-rc6+ -p=init=1 -m 500 -c [Thread debugging using libthread_db enabled] [New Thread 0x7fffd6e2d700 (LWP 19280)] Warning: request type 8 Program received signal SIGSEGV, Segmentation fault. 0x00000000004026ca in virt_queue__available (vq=0x60d3c8) at include/kvm/virtio.h:31 31 return vq->vring.avail->idx != vq->last_avail_idx; (gdb) (gdb) bt (gdb) p *vq $2 = {vring = {num = 0, desc = 0x0, avail = 0x0, used = 0x0}, pfn = 0, last_avail_idx = 0} I added the check of vq->vring.avail in virt_queue__available(), but it also failed. ... static inline bool virt_queue__available(struct virt_queue *vq) { + if (!vq->vring.avail) + return -1; return vq->vring.avail->idx != vq->last_avail_idx; } ... (gdb) r run -i linux-0.2.img -k ./vmlinuz-2.6.38-rc6+ -r ./initrd.img-2.6.38-rc6+ -p=init=1 -m 500 -c Starting program: /project/rh/kvm-tools/tools/kvm/kvm run -i linux-0.2.img -k ./vmlinuz-2.6.38-rc6+ -r ./initrd.img-2.6.38-rc6+ -p=init=1 -m 500 -c [Thread debugging using libthread_db enabled] [New Thread 0x7fffd6e2d700 (LWP 19434)] Warning: request type 8 Program received signal SIGFPE, Arithmetic exception. 0x00000000004066cd in virt_queue__pop (queue=0x60d3c8) at include/kvm/virtio.h:21 21 return queue->vring.avail->ring[queue->last_avail_idx++ % queue->vring.num]; (gdb) bt (gdb) p *queue $2 = {vring = {num = 0, desc = 0x0, avail = 0x0, used = 0x0}, pfn = 0, last_avail_idx = 0} Reported-by: Amos Kong Signed-off-by: Asias He --- tools/kvm/include/kvm/virtio.h | 22 ++++------------------ tools/kvm/virtio.c | 32 +++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/tools/kvm/include/kvm/virtio.h b/tools/kvm/include/kvm/virtio.h index 9f892a1..c64ae29 100644 --- a/tools/kvm/include/kvm/virtio.h +++ b/tools/kvm/include/kvm/virtio.h @@ -16,23 +16,9 @@ struct virt_queue { uint16_t last_avail_idx; }; -static inline uint16_t virt_queue__pop(struct virt_queue *queue) -{ - return queue->vring.avail->ring[queue->last_avail_idx++ % queue->vring.num]; -} - -static inline struct vring_desc *virt_queue__get_desc(struct virt_queue *queue, uint16_t desc_ndx) -{ - return &queue->vring.desc[desc_ndx]; -} - -static inline bool virt_queue__available(struct virt_queue *vq) -{ - return vq->vring.avail->idx != vq->last_avail_idx; -} - -struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *queue, uint32_t head, uint32_t len); - -uint16_t virt_queue__get_iov(struct virt_queue *queue, struct iovec iov[], uint16_t *out, uint16_t *in, struct kvm *kvm); +uint16_t virt_queue__get_iov(struct virt_queue *vq, struct iovec iov[], uint16_t *out, uint16_t *in, struct kvm *kvm); +struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *vq, uint32_t head, uint32_t len); +struct vring_desc *virt_queue__get_desc(struct virt_queue *vq, uint16_t desc_ndx); +bool virt_queue__available(struct virt_queue *vq); #endif /* KVM__VIRTIO_H */ diff --git a/tools/kvm/virtio.c b/tools/kvm/virtio.c index 6249521..2a19a14 100644 --- a/tools/kvm/virtio.c +++ b/tools/kvm/virtio.c @@ -4,25 +4,43 @@ #include "kvm/kvm.h" #include "kvm/virtio.h" -struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *queue, uint32_t head, uint32_t len) +bool virt_queue__available(struct virt_queue *vq) +{ + if (!vq->vring.avail) + return false; + + return vq->vring.avail->idx != vq->last_avail_idx; +} + +struct vring_desc *virt_queue__get_desc(struct virt_queue *vq, uint16_t desc_ndx) +{ + return &vq->vring.desc[desc_ndx]; +} + +struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *vq, uint32_t head, uint32_t len) { struct vring_used_elem *used_elem; - used_elem = &queue->vring.used->ring[queue->vring.used->idx++ % queue->vring.num]; + used_elem = &vq->vring.used->ring[vq->vring.used->idx++ % vq->vring.num]; used_elem->id = head; used_elem->len = len; return used_elem; } -uint16_t virt_queue__get_iov(struct virt_queue *queue, struct iovec iov[], uint16_t *out, uint16_t *in, struct kvm *kvm) +uint16_t virt_queue__get_iov(struct virt_queue *vq, struct iovec iov[], uint16_t *out, uint16_t *in, struct kvm *kvm) { struct vring_desc *desc; uint16_t head, idx; - idx = head = virt_queue__pop(queue); - *out = *in = 0; + if (!virt_queue__available(vq)) + return -1; + + head = vq->vring.avail->ring[vq->last_avail_idx++ % vq->vring.num]; + idx = head; + *out = 0; + *in = 0; do { - desc = virt_queue__get_desc(queue, idx); + desc = virt_queue__get_desc(vq, idx); iov[*out + *in].iov_base = guest_flat_to_host(kvm, desc->addr); iov[*out + *in].iov_len = desc->len; if (desc->flags & VRING_DESC_F_WRITE) @@ -30,7 +48,7 @@ uint16_t virt_queue__get_iov(struct virt_queue *queue, struct iovec iov[], uint1 else (*out)++; if (desc->flags & VRING_DESC_F_NEXT) - idx = desc->next; + idx = desc->next; else break; } while (1);