From nobody Sun Feb 8 13:48:01 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=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1677752132; cv=none; d=zohomail.com; s=zohoarc; b=NJFyKEgSPOMN4JOVT3ny4YhyyxkmRG4/fCLxyycYM8Ou3JM7E/Osjh7Oked41QN+0yMl2aavUZGJzQ2jAgz5ENSaA6HG3Kr/2bSFc+nu/NfeOU3SWrdpyr85xHmmPtPLOnjDpchiyFfMu+3Gl2lnZdWBt6AfDQSt0v+8K7dcZ2E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677752132; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=0dFeqs925nKU+EpW5o+gz7IWAukCerMgqLwKq9Ul/ps=; b=PQsFLum2Pm3Pdnkp8YSE1Qk1JNu4eZMSlQcwCKeLtQwnSMwHJRlp8u8YkxvGM6YZZY77wskdCqUYsCGgxLU+CV+sGwHS68B00+inCGBiw8nP9NPiPGpmxZPDiX3eWGTzxgsyI5t1V39vSosuFdogTqnGEqf0fso+FqoviYYdZZQ= 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 1677752132132847.3493882119349; Thu, 2 Mar 2023 02:15:32 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pXfy9-0007Oy-1Y; Thu, 02 Mar 2023 05:15:25 -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 1pXfy6-0007OI-QB; Thu, 02 Mar 2023 05:15:22 -0500 Received: from smtp-out2.suse.de ([195.135.220.29]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pXfy3-0000L1-4S; Thu, 02 Mar 2023 05:15:22 -0500 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id B5D001FE6E; Thu, 2 Mar 2023 10:15:15 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id E38D313349; Thu, 2 Mar 2023 10:15:14 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id ma9vNDJ3AGRbVAAAMHmgww (envelope-from ); Thu, 02 Mar 2023 10:15:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1677752115; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0dFeqs925nKU+EpW5o+gz7IWAukCerMgqLwKq9Ul/ps=; b=ShWVbpdXzIk7+DPdLOoO39MmyonUAu1T9faEVo84Xwexb91nB7i4dNM8Qn3zKBTQEdOTgQ XVpqpgyb3nknWGdn64cLPjQ5stwbQ3n19BLJ4YWfSzrYjLd4VtN43LU7kx+PuPGQWFtdyy 23y7Tg+lomVOZ1Z1bMcr5TEvsQyCo3c= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1677752115; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0dFeqs925nKU+EpW5o+gz7IWAukCerMgqLwKq9Ul/ps=; b=mdIURL9a6o//Y2J5UrgXp8NfZ7MCeW9/mO2k6HFGTYyOpgii4d79CpoH8DFvIhBZ0ocHh2 M4AGmyOncGoKR2BA== From: =?UTF-8?q?Carlos=20L=C3=B3pez?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Carlos=20L=C3=B3pez?= , Cornelia Huck , Halil Pasic , Eric Farman , "Michael S. Tsirkin" , Richard Henderson , David Hildenbrand , Ilya Leoshkevich , Christian Borntraeger , Thomas Huth , qemu-s390x@nongnu.org (open list:virtio-ccw) Subject: [PATCH] virtio: refresh vring region cache after updating a virtqueue size Date: Thu, 2 Mar 2023 11:14:47 +0100 Message-Id: <20230302101447.4499-1-clopez@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=195.135.220.29; envelope-from=clopez@suse.de; helo=smtp-out2.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 @suse.de) X-ZM-MESSAGEID: 1677752132660100001 When a virtqueue size is changed by the guest via virtio_queue_set_num(), its region cache is not automatically updated. If the size was increased, this could lead to accessing the cache out of bounds. For example, in vring_get_used_event(): static inline uint16_t vring_get_used_event(VirtQueue *vq) { return vring_avail_ring(vq, vq->vring.num); } static inline uint16_t vring_avail_ring(VirtQueue *vq, int i) { VRingMemoryRegionCaches *caches =3D vring_get_region_caches(vq); hwaddr pa =3D offsetof(VRingAvail, ring[i]); if (!caches) { return 0; } return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa); } vq->vring.num will be greater than caches->avail.len, which will trigger a failed assertion down the call path of virtio_lduw_phys_cached(). Fix this by calling virtio_queue_update_rings() after virtio_queue_set_num() if we are not already calling virtio_queue_set_rings(). Signed-off-by: Carlos L=C3=B3pez Acked-by: Thomas Huth --- hw/s390x/virtio-ccw.c | 1 + hw/virtio/virtio-mmio.c | 5 ++--- hw/virtio/virtio-pci.c | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/hw/s390x/virtio-ccw.c b/hw/s390x/virtio-ccw.c index e33e5207ab..89891ac58a 100644 --- a/hw/s390x/virtio-ccw.c +++ b/hw/s390x/virtio-ccw.c @@ -237,6 +237,7 @@ static int virtio_ccw_set_vqs(SubchDev *sch, VqInfoBloc= k *info, return -EINVAL; } virtio_queue_set_num(vdev, index, num); + virtio_queue_update_rings(vdev, index); } else if (virtio_queue_get_num(vdev, index) > num) { /* Fail if we don't have a big enough queue. */ return -EINVAL; diff --git a/hw/virtio/virtio-mmio.c b/hw/virtio/virtio-mmio.c index 23ba625eb6..c74822308f 100644 --- a/hw/virtio/virtio-mmio.c +++ b/hw/virtio/virtio-mmio.c @@ -350,10 +350,9 @@ static void virtio_mmio_write(void *opaque, hwaddr off= set, uint64_t value, case VIRTIO_MMIO_QUEUE_NUM: trace_virtio_mmio_queue_write(value, VIRTQUEUE_MAX_SIZE); virtio_queue_set_num(vdev, vdev->queue_sel, value); + virtio_queue_update_rings(vdev, vdev->queue_sel); =20 - if (proxy->legacy) { - virtio_queue_update_rings(vdev, vdev->queue_sel); - } else { + if (!proxy->legacy) { proxy->vqs[vdev->queue_sel].num =3D value; } break; diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index 247325c193..a0a2f2c965 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -1554,6 +1554,7 @@ static void virtio_pci_common_write(void *opaque, hwa= ddr addr, proxy->vqs[vdev->queue_sel].num =3D val; virtio_queue_set_num(vdev, vdev->queue_sel, proxy->vqs[vdev->queue_sel].num); + virtio_queue_update_rings(vdev, vdev->queue_sel); break; case VIRTIO_PCI_COMMON_Q_MSIX: vector =3D virtio_queue_vector(vdev, vdev->queue_sel); --=20 2.35.3