From nobody Sun Apr 28 17:21:39 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; 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=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1577089548; cv=none; d=zohomail.com; s=zohoarc; b=efRuV4D1xu0GqgtQmeWUFxuSflup3Ha6eGui1JAM9ZQzJjfMMA0Ll9Kdo6ixJDBfJi4KCFY2jklvkTQvbjZMicSJ7zcneixmfec7XLFpDDzhxfhpRHzlyTq1yAVJ8w0BgkrseJmkLtcH0Tw0YamIcVcp6LLTMYr1hhJcaJBXSZQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1577089548; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=IloyVDsWSgEGtIywh/4NwzHsneaa0UU+0sM/TJOI460=; b=Snt8YPnonPc71vwKKMtRcSZTYgSzHIV9JNel8Cwt0LevwtrEAGDWL5fExN0xjUeFnXlTU0M2ZPzqR8v3avlYnVulh0tzst/4UErQRx5YblPhdUXMUTphhLXwEK2t4uJzl1N0tJ48k5WLaQfnDo97+3AjHpcIOZnK9Ee5KgQh5jA= ARC-Authentication-Results: i=1; mx.zohomail.com; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1577089548686384.57563156701895; Mon, 23 Dec 2019 00:25:48 -0800 (PST) Received: from localhost ([::1]:54598 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ijJ2B-0004NK-Ng for importer@patchew.org; Mon, 23 Dec 2019 03:25:47 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:52573) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ijJ1F-0003jV-Oo for qemu-devel@nongnu.org; Mon, 23 Dec 2019 03:24:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ijJ1D-00047w-Rl for qemu-devel@nongnu.org; Mon, 23 Dec 2019 03:24:48 -0500 Received: from mga03.intel.com ([134.134.136.65]:25328) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ijJ1D-0003Ul-Hw for qemu-devel@nongnu.org; Mon, 23 Dec 2019 03:24:47 -0500 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Dec 2019 00:24:39 -0800 Received: from he.bj.intel.com ([10.238.157.85]) by orsmga003.jf.intel.com with ESMTP; 23 Dec 2019 00:24:37 -0800 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,346,1571727600"; d="scan'208";a="219436161" From: Yang Zhong To: pbonzini@redhat.com, mst@redhat.com, stefanha@redhat.com Subject: [PATCH] virtio: add the queue number check Date: Mon, 23 Dec 2019 16:28:13 +0800 Message-Id: <20191223082813.28930-1-yang.zhong@intel.com> X-Mailer: git-send-email 2.17.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.65 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhong@intel.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In the guest kernel driver, like virtio_blk.c and virtio_scsi.c, there are some definitions like below: num_vqs =3D min_t(unsigned int, nr_cpu_ids, num_vqs) If the queue number is bigger than vcpu number, the VM will be stuck in the guest driver because the qemu and guest driver have different queue number. So, this check can avoid this issues. Signed-off-by: Yang Zhong --- hw/block/vhost-user-blk.c | 11 +++++++++++ hw/block/virtio-blk.c | 11 ++++++++++- hw/scsi/virtio-scsi.c | 12 ++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/hw/block/vhost-user-blk.c b/hw/block/vhost-user-blk.c index 63da9bb619..250e72abe4 100644 --- a/hw/block/vhost-user-blk.c +++ b/hw/block/vhost-user-blk.c @@ -23,6 +23,8 @@ #include "qom/object.h" #include "hw/qdev-core.h" #include "hw/qdev-properties.h" +#include "qemu/option.h" +#include "qemu/config-file.h" #include "hw/virtio/vhost.h" #include "hw/virtio/vhost-user-blk.h" #include "hw/virtio/virtio.h" @@ -391,6 +393,7 @@ static void vhost_user_blk_device_realize(DeviceState *= dev, Error **errp) VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); VHostUserBlk *s =3D VHOST_USER_BLK(vdev); Error *err =3D NULL; + unsigned cpus; int i, ret; =20 if (!s->chardev.chr) { @@ -403,6 +406,14 @@ static void vhost_user_blk_device_realize(DeviceState = *dev, Error **errp) return; } =20 + cpus =3D qemu_opt_get_number(qemu_opts_find(qemu_find_opts("smp-opts")= , NULL), + "cpus", 0); + if (s->num_queues > cpus ) { + error_setg(errp, "vhost-user-blk: the queue number should be equal= " + "or less than vcpu number"); + return; + } + if (!s->queue_size) { error_setg(errp, "vhost-user-blk: queue size must be non-zero"); return; diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c index d62e6377c2..b2f4d01148 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -18,6 +18,8 @@ #include "qemu/error-report.h" #include "qemu/main-loop.h" #include "trace.h" +#include "qemu/option.h" +#include "qemu/config-file.h" #include "hw/block/block.h" #include "hw/qdev-properties.h" #include "sysemu/blockdev.h" @@ -1119,7 +1121,7 @@ static void virtio_blk_device_realize(DeviceState *de= v, Error **errp) VirtIOBlock *s =3D VIRTIO_BLK(dev); VirtIOBlkConf *conf =3D &s->conf; Error *err =3D NULL; - unsigned i; + unsigned i,cpus; =20 if (!conf->conf.blk) { error_setg(errp, "drive property not set"); @@ -1133,6 +1135,13 @@ static void virtio_blk_device_realize(DeviceState *d= ev, Error **errp) error_setg(errp, "num-queues property must be larger than 0"); return; } + cpus =3D qemu_opt_get_number(qemu_opts_find(qemu_find_opts("smp-opts")= , NULL), + "cpus", 0); + if (conf->num_queues > cpus ) { + error_setg(errp, "virtio-blk: the queue number should be equal " + "or less than vcpu number"); + return; + } if (!is_power_of_2(conf->queue_size) || conf->queue_size > VIRTQUEUE_MAX_SIZE) { error_setg(errp, "invalid queue-size property (%" PRIu16 "), " diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index e8b2b64d09..8e3e44f6b9 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -21,6 +21,8 @@ #include "qemu/error-report.h" #include "qemu/iov.h" #include "qemu/module.h" +#include "qemu/option.h" +#include "qemu/config-file.h" #include "sysemu/block-backend.h" #include "hw/qdev-properties.h" #include "hw/scsi/scsi.h" @@ -880,6 +882,7 @@ void virtio_scsi_common_realize(DeviceState *dev, { VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); VirtIOSCSICommon *s =3D VIRTIO_SCSI_COMMON(dev); + unsigned cpus; int i; =20 virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI, @@ -893,6 +896,15 @@ void virtio_scsi_common_realize(DeviceState *dev, virtio_cleanup(vdev); return; } + + cpus =3D qemu_opt_get_number(qemu_opts_find(qemu_find_opts("smp-opts")= , NULL), + "cpus", 0); + if (s->conf.num_queues > cpus ) { + error_setg(errp, "virtio-scsi: the queue number should be equal " + "or less than vcpu number"); + return; + } + s->cmd_vqs =3D g_new0(VirtQueue *, s->conf.num_queues); s->sense_size =3D VIRTIO_SCSI_SENSE_DEFAULT_SIZE; s->cdb_size =3D VIRTIO_SCSI_CDB_DEFAULT_SIZE; --=20 2.17.1