From nobody Sun Dec 14 06:32:57 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1667863008; cv=none; d=zohomail.com; s=zohoarc; b=dlEVSGO4dC211xTHpYP/mvExsnfqLMEDa088rKqLi37mwBGMd7MyN6Ad7RzV/KdHrJBv24UZzylDhqQ8HzAIGvG494taKbX2c3vdN3SIESy/anTmoZhcNdzZZVcjJVheVdoK2gMhqt+ILKf/6zyukaBG4I09CCuIA3YvnklxZCU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1667863008; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kVRFsZ5tOlNpwt90iJgpb5in64yP3rEEY2+3nFX4gMU=; b=C1bIP4PuptBKGG4MzTVU2c9fckfIc/cRXPigXo2+G2nPQuXDtupCwN96Ak1zC8d5/ojNkMoEmsb9w8ivWvT816I3+kWWe/TnDTcK7RMPOQEb9ZKQYGfMjKtiXqYbr4e9DphbD07uLd+OpuzQ9gjBmtUzI3MO0TcWvp59Aqc3DHk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1667863008758489.20908751254; Mon, 7 Nov 2022 15:16:48 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1osAyi-0004LM-DV; Mon, 07 Nov 2022 17:52:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1osAyd-0004Ao-6E for qemu-devel@nongnu.org; Mon, 07 Nov 2022 17:52:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1osAyZ-0006Sx-4G for qemu-devel@nongnu.org; Mon, 07 Nov 2022 17:52:22 -0500 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-195-CcC4KSL8OPug7xyeh2yjQg-1; Mon, 07 Nov 2022 17:52:16 -0500 Received: by mail-qk1-f200.google.com with SMTP id az31-20020a05620a171f00b006fa2cc1b0bfso11498143qkb.23 for ; Mon, 07 Nov 2022 14:52:16 -0800 (PST) Received: from redhat.com ([87.249.138.11]) by smtp.gmail.com with ESMTPSA id bk37-20020a05620a1a2500b006ee77f1ecc3sm7815770qkb.31.2022.11.07.14.52.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 14:52:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667861537; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=kVRFsZ5tOlNpwt90iJgpb5in64yP3rEEY2+3nFX4gMU=; b=doxcqz82RmPCyySzDDul5GO9Wlpak/MBGgTs1Kgew/BDgu+5wLhDpOn9tHIF67+uuQNH2w pihOtKPPOkXgo2PLf9HUtLOaDry2KnOW4KYA5mBHLQDWiyHcFfNLmQqzJO0I+wJqRfAUJW 3G/IXvjsmJDvM3hGS6SzX8RDjNMY/RE= X-MC-Unique: CcC4KSL8OPug7xyeh2yjQg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=kVRFsZ5tOlNpwt90iJgpb5in64yP3rEEY2+3nFX4gMU=; b=eS9eJ+QDpEpdDg7wb6zZc1hTVcGv53Ntu7huzXRPCwHnr5AxnPwW4eJM8sXF+hRKoS 3LFcDjBwqXd7RDwGcBOr1w/Gg23Oi4PJSjg7BhSw5vsvCJ2V77xlSmbcSj0W5CFxXsLb ipErkmmC+Bj7EA33+4gP+g9nGcQ/Zig0aW9oYEpXjEmrtD6wpqv67+tqKG0PDSTyCPG1 vc6Je/3f0yIrRVW0GoCyOPHlWNH2xOquqwrz7n+Gzf6J11ZE9Dp73DHG5OueVAQiNAzc yFdCEO1nOODyCRccNDQkHP/2F0FOCJKrxX2WD7clPWu2Zs7Sm6ZNqcwHA2M8RFNDPtRe JjaA== X-Gm-Message-State: ACrzQf18irlKoXDVYSaWUepGhdoqq61moZgaNETYOb12IriFdhHlnPDO 7toaT4RlnbI5l09s+8E4L8vHZcNPSicD9SXgz6rfcmetbLeNGW5A40H/v633UvNRkoQ8NzcvptO TBuM3Hg48cr+ZGkmWSvySjgQXmLA4IgUzMSwRjudDJewcWXb3kEWxZ1LKIMKv X-Received: by 2002:ae9:edd8:0:b0:6fa:26ad:26b9 with SMTP id c207-20020ae9edd8000000b006fa26ad26b9mr32219769qkg.551.1667861536153; Mon, 07 Nov 2022 14:52:16 -0800 (PST) X-Google-Smtp-Source: AMsMyM7x6U7S0f3/DP+wZ3fzXY+yHNrlw0TMybhtw9oCc84kwe5ULB6I4H5Y3iWRlYP/heYyVWxxLQ== X-Received: by 2002:ae9:edd8:0:b0:6fa:26ad:26b9 with SMTP id c207-20020ae9edd8000000b006fa26ad26b9mr32219730qkg.551.1667861535709; Mon, 07 Nov 2022 14:52:15 -0800 (PST) Date: Mon, 7 Nov 2022 17:52:08 -0500 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Cc: Peter Maydell , Akihiko Odaki , Akihiko Odaki , Yuval Shaia , Akihiko@redhat.com, Odaki@redhat.com, <@redhat.com, Dmitry Fleytman , Jason Wang , Jiri Pirko , Keith Busch , Klaus Jensen , Marcel Apfelbaum , Elena Ufimtseva , Jagannathan Raman , John G Johnson , qemu-block@nongnu.org Subject: [PULL v4 57/83] msix: Assert that specified vector is in range Message-ID: <20221107224600.934080-58-mst@redhat.com> References: <20221107224600.934080-1-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20221107224600.934080-1-mst@redhat.com> X-Mailer: git-send-email 2.27.0.106.g8ac3dc51b1 X-Mutt-Fcc: =sent Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=mst@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1667863010047100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Akihiko Odaki There were several different ways to deal with the situation where the vector specified for a msix function is out of bound: - early return a function and keep progresssing - propagate the error to the caller - mark msix unusable - assert it is in bound - just ignore An out-of-bound vector should not be specified if the device implementation is correct so let msix functions always assert that the specified vector is in range. An exceptional case is virtio-pci, which allows the guest to configure vectors. For virtio-pci, it is more appropriate to introduce its own checks because it is sometimes too late to check the vector range in msix functions. Signed-off-by: Akihiko Odaki Message-Id: <20220829083524.143640-1-akihiko.odaki@daynix.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin Reviewed-by: Yuval Shaia Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
--- include/hw/pci/msix.h | 4 +-- hw/net/e1000e.c | 15 ++------- hw/net/rocker/rocker.c | 23 ++------------ hw/net/vmxnet3.c | 27 +++------------- hw/nvme/ctrl.c | 5 +-- hw/pci/msix.c | 24 ++++++-------- hw/rdma/vmw/pvrdma_main.c | 7 +--- hw/remote/vfio-user-obj.c | 9 +----- hw/virtio/virtio-pci.c | 67 ++++++++++++++++++++++++++++----------- 9 files changed, 74 insertions(+), 107 deletions(-) diff --git a/include/hw/pci/msix.h b/include/hw/pci/msix.h index 4f1cda0ebe..0e6f257e45 100644 --- a/include/hw/pci/msix.h +++ b/include/hw/pci/msix.h @@ -33,10 +33,10 @@ bool msix_is_masked(PCIDevice *dev, unsigned vector); void msix_set_pending(PCIDevice *dev, unsigned vector); void msix_clr_pending(PCIDevice *dev, int vector); =20 -int msix_vector_use(PCIDevice *dev, unsigned vector); +void msix_vector_use(PCIDevice *dev, unsigned vector); void msix_vector_unuse(PCIDevice *dev, unsigned vector); void msix_unuse_all_vectors(PCIDevice *dev); -void msix_set_mask(PCIDevice *dev, int vector, bool mask, Error **errp); +void msix_set_mask(PCIDevice *dev, int vector, bool mask); =20 void msix_notify(PCIDevice *dev, unsigned vector); =20 diff --git a/hw/net/e1000e.c b/hw/net/e1000e.c index ac96f7665a..7523e9f5d2 100644 --- a/hw/net/e1000e.c +++ b/hw/net/e1000e.c @@ -276,25 +276,18 @@ e1000e_unuse_msix_vectors(E1000EState *s, int num_vec= tors) } } =20 -static bool +static void e1000e_use_msix_vectors(E1000EState *s, int num_vectors) { int i; for (i =3D 0; i < num_vectors; i++) { - int res =3D msix_vector_use(PCI_DEVICE(s), i); - if (res < 0) { - trace_e1000e_msix_use_vector_fail(i, res); - e1000e_unuse_msix_vectors(s, i); - return false; - } + msix_vector_use(PCI_DEVICE(s), i); } - return true; } =20 static void e1000e_init_msix(E1000EState *s) { - PCIDevice *d =3D PCI_DEVICE(s); int res =3D msix_init(PCI_DEVICE(s), E1000E_MSIX_VEC_NUM, &s->msix, E1000E_MSIX_IDX, E1000E_MSIX_TABLE, @@ -305,9 +298,7 @@ e1000e_init_msix(E1000EState *s) if (res < 0) { trace_e1000e_msix_init_fail(res); } else { - if (!e1000e_use_msix_vectors(s, E1000E_MSIX_VEC_NUM)) { - msix_uninit(d, &s->msix, &s->msix); - } + e1000e_use_msix_vectors(s, E1000E_MSIX_VEC_NUM); } } =20 diff --git a/hw/net/rocker/rocker.c b/hw/net/rocker/rocker.c index d8f3f16fe8..281d43e6cf 100644 --- a/hw/net/rocker/rocker.c +++ b/hw/net/rocker/rocker.c @@ -1212,24 +1212,14 @@ static void rocker_msix_vectors_unuse(Rocker *r, } } =20 -static int rocker_msix_vectors_use(Rocker *r, - unsigned int num_vectors) +static void rocker_msix_vectors_use(Rocker *r, unsigned int num_vectors) { PCIDevice *dev =3D PCI_DEVICE(r); - int err; int i; =20 for (i =3D 0; i < num_vectors; i++) { - err =3D msix_vector_use(dev, i); - if (err) { - goto rollback; - } + msix_vector_use(dev, i); } - return 0; - -rollback: - rocker_msix_vectors_unuse(r, i); - return err; } =20 static int rocker_msix_init(Rocker *r, Error **errp) @@ -1247,16 +1237,9 @@ static int rocker_msix_init(Rocker *r, Error **errp) return err; } =20 - err =3D rocker_msix_vectors_use(r, ROCKER_MSIX_VEC_COUNT(r->fp_ports)); - if (err) { - goto err_msix_vectors_use; - } + rocker_msix_vectors_use(r, ROCKER_MSIX_VEC_COUNT(r->fp_ports)); =20 return 0; - -err_msix_vectors_use: - msix_uninit(dev, &r->msix_bar, &r->msix_bar); - return err; } =20 static void rocker_msix_uninit(Rocker *r) diff --git a/hw/net/vmxnet3.c b/hw/net/vmxnet3.c index 0b7acf7f89..d2ab527ef4 100644 --- a/hw/net/vmxnet3.c +++ b/hw/net/vmxnet3.c @@ -2110,20 +2110,14 @@ vmxnet3_unuse_msix_vectors(VMXNET3State *s, int num= _vectors) } } =20 -static bool +static void vmxnet3_use_msix_vectors(VMXNET3State *s, int num_vectors) { PCIDevice *d =3D PCI_DEVICE(s); int i; for (i =3D 0; i < num_vectors; i++) { - int res =3D msix_vector_use(d, i); - if (0 > res) { - VMW_WRPRN("Failed to use MSI-X vector %d, error %d", i, res); - vmxnet3_unuse_msix_vectors(s, i); - return false; - } + msix_vector_use(d, i); } - return true; } =20 static bool @@ -2141,13 +2135,8 @@ vmxnet3_init_msix(VMXNET3State *s) VMW_WRPRN("Failed to initialize MSI-X, error %d", res); s->msix_used =3D false; } else { - if (!vmxnet3_use_msix_vectors(s, VMXNET3_MAX_INTRS)) { - VMW_WRPRN("Failed to use MSI-X vectors, error %d", res); - msix_uninit(d, &s->msix_bar, &s->msix_bar); - s->msix_used =3D false; - } else { - s->msix_used =3D true; - } + vmxnet3_use_msix_vectors(s, VMXNET3_MAX_INTRS); + s->msix_used =3D true; } return s->msix_used; } @@ -2412,19 +2401,13 @@ static const VMStateDescription vmstate_vmxnet3_rxq= _descr =3D { static int vmxnet3_post_load(void *opaque, int version_id) { VMXNET3State *s =3D opaque; - PCIDevice *d =3D PCI_DEVICE(s); =20 net_tx_pkt_init(&s->tx_pkt, PCI_DEVICE(s), s->max_tx_frags, s->peer_has_vhdr); net_rx_pkt_init(&s->rx_pkt, s->peer_has_vhdr); =20 if (s->msix_used) { - if (!vmxnet3_use_msix_vectors(s, VMXNET3_MAX_INTRS)) { - VMW_WRPRN("Failed to re-use MSI-X vectors"); - msix_uninit(d, &s->msix_bar, &s->msix_bar); - s->msix_used =3D false; - return -1; - } + vmxnet3_use_msix_vectors(s, VMXNET3_MAX_INTRS); } =20 if (!vmxnet3_validate_queues(s)) { diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index 87aeba0564..d38fdd990e 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -4744,11 +4744,8 @@ static void nvme_init_cq(NvmeCQueue *cq, NvmeCtrl *n= , uint64_t dma_addr, uint16_t cqid, uint16_t vector, uint16_t size, uint16_t irq_enabled) { - int ret; - if (msix_enabled(&n->parent_obj)) { - ret =3D msix_vector_use(&n->parent_obj, vector); - assert(ret =3D=3D 0); + msix_vector_use(&n->parent_obj, vector); } cq->ctrl =3D n; cq->cqid =3D cqid; diff --git a/hw/pci/msix.c b/hw/pci/msix.c index 1e381a9813..9e70fcd6fa 100644 --- a/hw/pci/msix.c +++ b/hw/pci/msix.c @@ -136,17 +136,12 @@ static void msix_handle_mask_update(PCIDevice *dev, i= nt vector, bool was_masked) } } =20 -void msix_set_mask(PCIDevice *dev, int vector, bool mask, Error **errp) +void msix_set_mask(PCIDevice *dev, int vector, bool mask) { - ERRP_GUARD(); unsigned offset; bool was_masked; =20 - if (vector > dev->msix_entries_nr) { - error_setg(errp, "msix: vector %d not allocated. max vector is %d", - vector, dev->msix_entries_nr); - return; - } + assert(vector < dev->msix_entries_nr); =20 offset =3D vector * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_VECTOR_CTRL; =20 @@ -522,7 +517,9 @@ void msix_notify(PCIDevice *dev, unsigned vector) { MSIMessage msg; =20 - if (vector >=3D dev->msix_entries_nr || !dev->msix_entry_used[vector])= { + assert(vector < dev->msix_entries_nr); + + if (!dev->msix_entry_used[vector]) { return; } =20 @@ -558,20 +555,17 @@ void msix_reset(PCIDevice *dev) * don't want to follow the spec suggestion can declare all vectors as use= d. */ =20 /* Mark vector as used. */ -int msix_vector_use(PCIDevice *dev, unsigned vector) +void msix_vector_use(PCIDevice *dev, unsigned vector) { - if (vector >=3D dev->msix_entries_nr) { - return -EINVAL; - } - + assert(vector < dev->msix_entries_nr); dev->msix_entry_used[vector]++; - return 0; } =20 /* Mark vector as unused. */ void msix_vector_unuse(PCIDevice *dev, unsigned vector) { - if (vector >=3D dev->msix_entries_nr || !dev->msix_entry_used[vector])= { + assert(vector < dev->msix_entries_nr); + if (!dev->msix_entry_used[vector]) { return; } if (--dev->msix_entry_used[vector]) { diff --git a/hw/rdma/vmw/pvrdma_main.c b/hw/rdma/vmw/pvrdma_main.c index 58db0b8e3b..4fc6712025 100644 --- a/hw/rdma/vmw/pvrdma_main.c +++ b/hw/rdma/vmw/pvrdma_main.c @@ -307,12 +307,7 @@ static int init_msix(PCIDevice *pdev) } =20 for (i =3D 0; i < RDMA_MAX_INTRS; i++) { - rc =3D msix_vector_use(PCI_DEVICE(dev), i); - if (rc < 0) { - rdma_error_report("Fail mark MSI-X vector %d", i); - uninit_msix(pdev, i); - return rc; - } + msix_vector_use(PCI_DEVICE(dev), i); } =20 return 0; diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c index c6cc53acf2..4e36bb8bcf 100644 --- a/hw/remote/vfio-user-obj.c +++ b/hw/remote/vfio-user-obj.c @@ -602,17 +602,10 @@ static void vfu_msix_irq_state(vfu_ctx_t *vfu_ctx, ui= nt32_t start, uint32_t count, bool mask) { VfuObject *o =3D vfu_get_private(vfu_ctx); - Error *err =3D NULL; uint32_t vector; =20 for (vector =3D start; vector < count; vector++) { - msix_set_mask(o->pci_dev, vector, mask, &err); - if (err) { - VFU_OBJECT_ERROR(o, "vfu: %s: %s", o->device, - error_get_pretty(err)); - error_free(err); - err =3D NULL; - } + msix_set_mask(o->pci_dev, vector, mask); } } =20 diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index 855718d586..a1c9dfa7bb 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -71,9 +71,11 @@ static void virtio_pci_notify(DeviceState *d, uint16_t v= ector) { VirtIOPCIProxy *proxy =3D to_virtio_pci_proxy_fast(d); =20 - if (msix_enabled(&proxy->pci_dev)) - msix_notify(&proxy->pci_dev, vector); - else { + if (msix_enabled(&proxy->pci_dev)) { + if (vector !=3D VIRTIO_NO_VECTOR) { + msix_notify(&proxy->pci_dev, vector); + } + } else { VirtIODevice *vdev =3D virtio_bus_get_device(&proxy->bus); pci_set_irq(&proxy->pci_dev, qatomic_read(&vdev->isr) & 1); } @@ -175,6 +177,7 @@ static int virtio_pci_load_config(DeviceState *d, QEMUF= ile *f) { VirtIOPCIProxy *proxy =3D to_virtio_pci_proxy(d); VirtIODevice *vdev =3D virtio_bus_get_device(&proxy->bus); + uint16_t vector; =20 int ret; ret =3D pci_device_load(&proxy->pci_dev, f); @@ -184,12 +187,17 @@ static int virtio_pci_load_config(DeviceState *d, QEM= UFile *f) msix_unuse_all_vectors(&proxy->pci_dev); msix_load(&proxy->pci_dev, f); if (msix_present(&proxy->pci_dev)) { - qemu_get_be16s(f, &vdev->config_vector); + qemu_get_be16s(f, &vector); + + if (vector !=3D VIRTIO_NO_VECTOR && vector >=3D proxy->nvectors) { + return -EINVAL; + } } else { - vdev->config_vector =3D VIRTIO_NO_VECTOR; + vector =3D VIRTIO_NO_VECTOR; } - if (vdev->config_vector !=3D VIRTIO_NO_VECTOR) { - return msix_vector_use(&proxy->pci_dev, vdev->config_vector); + vdev->config_vector =3D vector; + if (vector !=3D VIRTIO_NO_VECTOR) { + msix_vector_use(&proxy->pci_dev, vector); } return 0; } @@ -202,12 +210,15 @@ static int virtio_pci_load_queue(DeviceState *d, int = n, QEMUFile *f) uint16_t vector; if (msix_present(&proxy->pci_dev)) { qemu_get_be16s(f, &vector); + if (vector !=3D VIRTIO_NO_VECTOR && vector >=3D proxy->nvectors) { + return -EINVAL; + } } else { vector =3D VIRTIO_NO_VECTOR; } virtio_queue_set_vector(vdev, n, vector); if (vector !=3D VIRTIO_NO_VECTOR) { - return msix_vector_use(&proxy->pci_dev, vector); + msix_vector_use(&proxy->pci_dev, vector); } =20 return 0; @@ -299,6 +310,7 @@ static void virtio_ioport_write(void *opaque, uint32_t = addr, uint32_t val) { VirtIOPCIProxy *proxy =3D opaque; VirtIODevice *vdev =3D virtio_bus_get_device(&proxy->bus); + uint16_t vector; hwaddr pa; =20 switch (addr) { @@ -352,18 +364,28 @@ static void virtio_ioport_write(void *opaque, uint32_= t addr, uint32_t val) } break; case VIRTIO_MSI_CONFIG_VECTOR: - msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); + if (vdev->config_vector !=3D VIRTIO_NO_VECTOR) { + msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); + } /* Make it possible for guest to discover an error took place. */ - if (msix_vector_use(&proxy->pci_dev, val) < 0) + if (val < proxy->nvectors) { + msix_vector_use(&proxy->pci_dev, val); + } else { val =3D VIRTIO_NO_VECTOR; + } vdev->config_vector =3D val; break; case VIRTIO_MSI_QUEUE_VECTOR: - msix_vector_unuse(&proxy->pci_dev, - virtio_queue_vector(vdev, vdev->queue_sel)); + vector =3D virtio_queue_vector(vdev, vdev->queue_sel); + if (vector !=3D VIRTIO_NO_VECTOR) { + msix_vector_unuse(&proxy->pci_dev, vector); + } /* Make it possible for guest to discover an error took place. */ - if (msix_vector_use(&proxy->pci_dev, val) < 0) + if (val < proxy->nvectors) { + msix_vector_use(&proxy->pci_dev, val); + } else { val =3D VIRTIO_NO_VECTOR; + } virtio_queue_set_vector(vdev, vdev->queue_sel, val); break; default: @@ -1266,6 +1288,7 @@ static void virtio_pci_common_write(void *opaque, hwa= ddr addr, { VirtIOPCIProxy *proxy =3D opaque; VirtIODevice *vdev =3D virtio_bus_get_device(&proxy->bus); + uint16_t vector; =20 if (vdev =3D=3D NULL) { return; @@ -1287,9 +1310,13 @@ static void virtio_pci_common_write(void *opaque, hw= addr addr, } break; case VIRTIO_PCI_COMMON_MSIX: - msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); + if (vdev->config_vector !=3D VIRTIO_NO_VECTOR) { + msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); + } /* Make it possible for guest to discover an error took place. */ - if (msix_vector_use(&proxy->pci_dev, val) < 0) { + if (val < proxy->nvectors) { + msix_vector_use(&proxy->pci_dev, val); + } else { val =3D VIRTIO_NO_VECTOR; } vdev->config_vector =3D val; @@ -1321,10 +1348,14 @@ static void virtio_pci_common_write(void *opaque, h= waddr addr, proxy->vqs[vdev->queue_sel].num); break; case VIRTIO_PCI_COMMON_Q_MSIX: - msix_vector_unuse(&proxy->pci_dev, - virtio_queue_vector(vdev, vdev->queue_sel)); + vector =3D virtio_queue_vector(vdev, vdev->queue_sel); + if (vector !=3D VIRTIO_NO_VECTOR) { + msix_vector_unuse(&proxy->pci_dev, vector); + } /* Make it possible for guest to discover an error took place. */ - if (msix_vector_use(&proxy->pci_dev, val) < 0) { + if (val < proxy->nvectors) { + msix_vector_use(&proxy->pci_dev, val); + } else { val =3D VIRTIO_NO_VECTOR; } virtio_queue_set_vector(vdev, vdev->queue_sel, val); --=20 MST