From nobody Mon Feb 9 17:37:46 2026 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=1652738974; cv=none; d=zohomail.com; s=zohoarc; b=FndUuGd0zT3ubrLTCGtn59EkgAGlafhYDNROOTY/te2BGCxj6lfwepzR4PorlKZOFR9DX80jFzF02OLOGI3z50RkrITq6xSGu759u8Prqg5bDaropp3DDtXUcUdJH153v4l0f/+OM4+8bGJSTcJCH3FRa+rt++LE2LbZwLejPPQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1652738974; 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=2U6nu0PeOfVApPP2ohRjcoN1Q5IaLX0N9Wz0iqJcU9Q=; b=eOkDzlZQAy1vYPIE1oOD6sY/Ad94KFUQsKLBWgvjFZDUE5F8PtvzaUGe9cXv5vBxmS/urTS7+jhy7Sy/U0dgWl5afiN3TXKQen+Rs07+yuUh1Q+WrTY4S2UdAeKmrDSbx7k9w2fG5nVpbewfZMTkL8iY/NT43IQNEa174mm5pDk= 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 1652738974843134.3016295401669; Mon, 16 May 2022 15:09:34 -0700 (PDT) Received: from localhost ([::1]:55656 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nqiuD-0007ik-Hh for importer@patchew.org; Mon, 16 May 2022 18:09:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59938) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqhks-0003iF-4f for qemu-devel@nongnu.org; Mon, 16 May 2022 16:55:50 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:42580) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqhkp-00077b-R1 for qemu-devel@nongnu.org; Mon, 16 May 2022 16:55:49 -0400 Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-668-yZnEOJTLMaaCERzlx8rn-w-1; Mon, 16 May 2022 16:55:46 -0400 Received: by mail-ed1-f69.google.com with SMTP id ay24-20020a056402203800b0042a96a76ba5so4447176edb.20 for ; Mon, 16 May 2022 13:55:45 -0700 (PDT) Received: from redhat.com ([2.55.131.38]) by smtp.gmail.com with ESMTPSA id v24-20020a17090651d800b006f3ef214e15sm169354ejk.123.2022.05.16.13.55.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 May 2022 13:55:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652734547; 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=2U6nu0PeOfVApPP2ohRjcoN1Q5IaLX0N9Wz0iqJcU9Q=; b=ZREMnyqNlSGp/SqOEQeApCUS2VdnsERcaCGibHLKB3TN77e1H0XPkv/pudM5xQWRc5M+6P AzbDJjHXOK4v/k8wMWVEKK524wvzOVS/Jvhk8T94Yn3gZgGNNXVy4Slb02sbu0BUNnrqgQ 6gbJs8AqoEkMM196g6YDH8RAAAy43Fw= X-MC-Unique: yZnEOJTLMaaCERzlx8rn-w-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=2U6nu0PeOfVApPP2ohRjcoN1Q5IaLX0N9Wz0iqJcU9Q=; b=dhCEPlTLNJAFwrIiaFeiQGJpSlUwsbC8EJ7cHxD89PPGvI9fbkFiCsynUJFTzAFUVY Lj/o27dsIy4dkbvBMod9G1OnZXq1Bm4Y2GFjps2NhwU06u74EZYHA5EDwDW82M+ypLYV 3Rf2fdX3HD0cnnk6OCm/FJCZz1cgwcuW8g1qlGLQA/xHFi8dWNPlCN7A996OmbdkPtKl P9NOre1zzaoZ4c3nPQulwwwAEOIdP+ziFazkQqUsDnT2eGQIw/yfdHkrUjFP8GcoxmVd VlKJm7SxN6zEXBnQ8veUjqgsxnerjALiWloG8IBALKT+IVDui2oKPAzcfCQoSuez0XUR RtLg== X-Gm-Message-State: AOAM531QTC+XAwTM4mshY4O+5jGp02si/mHJvjCUrFso9rcLmaq0hMld ouUkOdxOvffGx6UXnUCX/XwsmHzs8MEAg5P3od2iepPTicPYU9cbfrVDCScXOtvvL3Zn+j1h20M 8ISXCQeOfbSUzcCuUs20tHhmRyb9Txnp9S4h9H84ejLfsd51Km0JyvBDpU85q X-Received: by 2002:a05:6402:3682:b0:42a:c348:7248 with SMTP id ej2-20020a056402368200b0042ac3487248mr969045edb.197.1652734544317; Mon, 16 May 2022 13:55:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzMqWW2IiANP2QDv1AiOnkk4An88jsHYKr0LJPrCvv6Lk/HfvfbsD6zLRgcVgGsYit/bflBeA== X-Received: by 2002:a05:6402:3682:b0:42a:c348:7248 with SMTP id ej2-20020a056402368200b0042ac3487248mr969022edb.197.1652734544082; Mon, 16 May 2022 13:55:44 -0700 (PDT) Date: Mon, 16 May 2022 16:55:40 -0400 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Cc: Peter Maydell , Si-Wei Liu , Jason Wang Subject: [PULL v2 80/86] virtio-net: align ctrl_vq index for non-mq guest for vhost_vdpa Message-ID: <20220516204913.542894-81-mst@redhat.com> References: <20220516204913.542894-1-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20220516204913.542894-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.133.124; envelope-from=mst@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1652738975556100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Si-Wei Liu With MQ enabled vdpa device and non-MQ supporting guest e.g. booting vdpa with mq=3Don over OVMF of single vqp, below assert failure is seen: ../hw/virtio/vhost-vdpa.c:560: vhost_vdpa_get_vq_index: Assertion `idx >=3D= dev->vq_index && idx < dev->vq_index + dev->nvqs' failed. 0 0x00007f8ce3ff3387 in raise () at /lib64/libc.so.6 1 0x00007f8ce3ff4a78 in abort () at /lib64/libc.so.6 2 0x00007f8ce3fec1a6 in __assert_fail_base () at /lib64/libc.so.6 3 0x00007f8ce3fec252 in () at /lib64/libc.so.6 4 0x0000558f52d79421 in vhost_vdpa_get_vq_index (dev=3D, id= x=3D) at ../hw/virtio/vhost-vdpa.c:563 5 0x0000558f52d79421 in vhost_vdpa_get_vq_index (dev=3D, id= x=3D) at ../hw/virtio/vhost-vdpa.c:558 6 0x0000558f52d7329a in vhost_virtqueue_mask (hdev=3D0x558f55c01800, vdev= =3D0x558f568f91f0, n=3D2, mask=3D) at ../hw/virtio/vhost.c:1= 557 7 0x0000558f52c6b89a in virtio_pci_set_guest_notifier (d=3Dd@entry=3D0x558= f568f0f60, n=3Dn@entry=3D2, assign=3Dassign@entry=3Dtrue, with_irqfd=3Dwith= _irqfd@entry=3Dfalse) at ../hw/virtio/virtio-pci.c:974 8 0x0000558f52c6c0d8 in virtio_pci_set_guest_notifiers (d=3D0x558f568f0f60= , nvqs=3D3, assign=3Dtrue) at ../hw/virtio/virtio-pci.c:1019 9 0x0000558f52bf091d in vhost_net_start (dev=3Ddev@entry=3D0x558f568f91f0,= ncs=3D0x558f56937cd0, data_queue_pairs=3Ddata_queue_pairs@entry=3D1, cvq= =3Dcvq@entry=3D1) at ../hw/net/vhost_net.c:361 10 0x0000558f52d4e5e7 in virtio_net_set_status (status=3D, n= =3D0x558f568f91f0) at ../hw/net/virtio-net.c:289 11 0x0000558f52d4e5e7 in virtio_net_set_status (vdev=3D0x558f568f91f0, stat= us=3D15 '\017') at ../hw/net/virtio-net.c:370 12 0x0000558f52d6c4b2 in virtio_set_status (vdev=3Dvdev@entry=3D0x558f568f9= 1f0, val=3Dval@entry=3D15 '\017') at ../hw/virtio/virtio.c:1945 13 0x0000558f52c69eff in virtio_pci_common_write (opaque=3D0x558f568f0f60, = addr=3D, val=3D, size=3D) at .= ./hw/virtio/virtio-pci.c:1292 14 0x0000558f52d15d6e in memory_region_write_accessor (mr=3D0x558f568f19d0,= addr=3D20, value=3D, size=3D1, shift=3D, mas= k=3D, attrs=3D...) at ../softmmu/memory.c:492 15 0x0000558f52d127de in access_with_adjusted_size (addr=3Daddr@entry=3D20,= value=3Dvalue@entry=3D0x7f8cdbffe748, size=3Dsize@entry=3D1, access_size_m= in=3D, access_size_max=3D, access_fn=3D0x558f= 52d15cf0 , mr=3D0x558f568f19d0, attrs=3D...) = at ../softmmu/memory.c:554 16 0x0000558f52d157ef in memory_region_dispatch_write (mr=3Dmr@entry=3D0x55= 8f568f19d0, addr=3D20, data=3D, op=3D, attrs= =3Dattrs@entry=3D...) at ../softmmu/memory.c:1504 17 0x0000558f52d078e7 in flatview_write_continue (fv=3Dfv@entry=3D0x7f8accb= c3b90, addr=3Daddr@entry=3D103079215124, attrs=3D..., ptr=3Dptr@entry=3D0x7= f8ce6300028, len=3Dlen@entry=3D1, addr1=3D, l=3D, mr=3D0x558f568f19d0) at /home/opc/qemu-upstream/include/qemu/host-util= s.h:165 18 0x0000558f52d07b06 in flatview_write (fv=3D0x7f8accbc3b90, addr=3D103079= 215124, attrs=3D..., buf=3D0x7f8ce6300028, len=3D1) at ../softmmu/physmem.c= :2822 19 0x0000558f52d0b36b in address_space_write (as=3D, addr=3D= , attrs=3D..., buf=3Dbuf@entry=3D0x7f8ce6300028, len=3D) at ../softmmu/physmem.c:2914 20 0x0000558f52d0b3da in address_space_rw (as=3D, addr=3D, attrs=3D..., attrs@entry=3D..., buf=3Dbuf@entry=3D0x7f8ce6300028, len=3D, is_write=3D) at ../softmmu/physmem.c:2924 21 0x0000558f52dced09 in kvm_cpu_exec (cpu=3Dcpu@entry=3D0x558f55c2da60) at= ../accel/kvm/kvm-all.c:2903 22 0x0000558f52dcfabd in kvm_vcpu_thread_fn (arg=3Darg@entry=3D0x558f55c2da= 60) at ../accel/kvm/kvm-accel-ops.c:49 23 0x0000558f52f9f04a in qemu_thread_start (args=3D) at ../u= til/qemu-thread-posix.c:556 24 0x00007f8ce4392ea5 in start_thread () at /lib64/libpthread.so.0 25 0x00007f8ce40bb9fd in clone () at /lib64/libc.so.6 The cause for the assert failure is due to that the vhost_dev index for the ctrl vq was not aligned with actual one in use by the guest. Upon multiqueue feature negotiation in virtio_net_set_multiqueue(), if guest doesn't support multiqueue, the guest vq layout would shrink to a single queue pair, consisting of 3 vqs in total (rx, tx and ctrl). This results in ctrl_vq taking a different vhost_dev group index than the default. We can map vq to the correct vhost_dev group by checking if MQ is supported by guest and successfully negotiated. Since the MQ feature is only present along with CTRL_VQ, we ensure the index 2 is only meant for the control vq while MQ is not supported by guest. Fixes: 22288fe ("virtio-net: vhost control virtqueue support") Suggested-by: Jason Wang Signed-off-by: Si-Wei Liu Acked-by: Jason Wang Message-Id: <1651890498-24478-3-git-send-email-si-wei.liu@oracle.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/net/virtio-net.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 7545a16ed7..1ea524ff09 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -14,6 +14,7 @@ #include "qemu/osdep.h" #include "qemu/atomic.h" #include "qemu/iov.h" +#include "qemu/log.h" #include "qemu/main-loop.h" #include "qemu/module.h" #include "hw/virtio/virtio.h" @@ -3172,8 +3173,22 @@ static NetClientInfo net_virtio_info =3D { static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) { VirtIONet *n =3D VIRTIO_NET(vdev); - NetClientState *nc =3D qemu_get_subqueue(n->nic, vq2q(idx)); + NetClientState *nc; assert(n->vhost_started); + if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx =3D=3D 2) { + /* Must guard against invalid features and bogus queue index + * from being set by malicious guest, or penetrated through + * buggy migration stream. + */ + if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: bogus vq index ignored\n", __func__); + return false; + } + nc =3D qemu_get_subqueue(n->nic, n->max_queue_pairs); + } else { + nc =3D qemu_get_subqueue(n->nic, vq2q(idx)); + } return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx); } =20 @@ -3181,8 +3196,22 @@ static void virtio_net_guest_notifier_mask(VirtIODev= ice *vdev, int idx, bool mask) { VirtIONet *n =3D VIRTIO_NET(vdev); - NetClientState *nc =3D qemu_get_subqueue(n->nic, vq2q(idx)); + NetClientState *nc; assert(n->vhost_started); + if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ) && idx =3D=3D 2) { + /* Must guard against invalid features and bogus queue index + * from being set by malicious guest, or penetrated through + * buggy migration stream. + */ + if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: bogus vq index ignored\n", __func__); + return; + } + nc =3D qemu_get_subqueue(n->nic, n->max_queue_pairs); + } else { + nc =3D qemu_get_subqueue(n->nic, vq2q(idx)); + } vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask); } --=20 MST