From nobody Fri May 10 06:06:32 2024 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=1671161800; cv=none; d=zohomail.com; s=zohoarc; b=SnkmrsPssC+bzPZvaqOQDW26ETRbcvduVmhSwRhD3MdF9hl5m+5TR8NW0ZDjrz1wluv76GE8rXB7ggsf4DZQZHrt2dApODS4S06QPWlC+v4eUbOqwcHrDcNW7mFGG3njeutYVSaGkL7FcekBpmikZdqbANPyFwgzht0moBHNB+E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1671161800; 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=n3uGwk0ZHQPIU8kuu1OTQ2VZ7978nvV4Fx9MO0AhOM0=; b=YVgBOasGKHUZoOk7Ls8EOrM+xuT8R01I5pZY9SJfvNbXRdiTQxH9h7bopyx4tuxgJTkXufigZguuyU8YmN9thE17UGz6S7CYbqfTtKj8zk85Qzs1cBX5OCWfELpR3dKFUvh1kt6BDuPgE3oNvnigZdnTIWx6nuDrMObcJsqpE5M= 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 1671161799998281.9873954185488; Thu, 15 Dec 2022 19:36:39 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p61W8-0002ps-EJ; Thu, 15 Dec 2022 22:36:13 -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 1p61W1-0002lo-3N for qemu-devel@nongnu.org; Thu, 15 Dec 2022 22:36:05 -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 1p61Vz-00019u-Fa for qemu-devel@nongnu.org; Thu, 15 Dec 2022 22:36:04 -0500 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-633-PXyo5i-1NViBclOjKuuTiQ-1; Thu, 15 Dec 2022 22:35:58 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 21DB63C0E45C; Fri, 16 Dec 2022 03:35:58 +0000 (UTC) Received: from localhost.localdomain (ovpn-12-66.pek2.redhat.com [10.72.12.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 23B9FC15BA0; Fri, 16 Dec 2022 03:35:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671161762; 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: content-transfer-encoding:content-transfer-encoding; bh=n3uGwk0ZHQPIU8kuu1OTQ2VZ7978nvV4Fx9MO0AhOM0=; b=UNcU0YvjJloJVnzo2Bq4pBoV/k/PQ4Ak2/6ckgzDch2y1u0UjErEqyG2ix9JmHSVju2U2P 9w66SLi8o6loYjmuJRTb0LISV1T93edyCgJl0TUYteH7+uAfF8+M0mOYsEOkRnabRXX/1e r9oQTdDgisePY6AMkEEarzQZaBHvDVI= X-MC-Unique: PXyo5i-1NViBclOjKuuTiQ-1 From: Jason Wang To: mst@redhat.com, qemu-devel@nongnu.org Cc: eric.auger@redhat.com, Jason Wang , qemu-stable@nongnu.org, Lei Yang , Yalan Zhang Subject: [PATCH V3] vhost: fix vq dirty bitmap syncing when vIOMMU is enabled Date: Fri, 16 Dec 2022 11:35:52 +0800 Message-Id: <20221216033552.77087-1-jasowang@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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=jasowang@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=unavailable 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: 1671161801848100003 Content-Type: text/plain; charset="utf-8" When vIOMMU is enabled, the vq->used_phys is actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory. Fixes: c471ad0e9bd46 ("vhost_net: device IOTLB support") Cc: qemu-stable@nongnu.org Tested-by: Lei Yang Reported-by: Yalan Zhang Signed-off-by: Jason Wang --- Changes since V2: - use "used_iova" instead of "used_phys" in log - store the offset in a local variable - add comment to explain the one adding outsize MIN() - silent checkpatch Changes since V1: - Fix the address calculation when used ring is not page aligned - Fix the length for each round of dirty bitmap syncing - Use LOG_GUEST_ERROR to log wrong used adddress - Various other tweaks --- hw/virtio/vhost.c | 84 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 20 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 7fb008bc9e..fdcd1a8fdf 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -20,6 +20,7 @@ #include "qemu/range.h" #include "qemu/error-report.h" #include "qemu/memfd.h" +#include "qemu/log.h" #include "standard-headers/linux/vhost_types.h" #include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-access.h" @@ -106,6 +107,24 @@ static void vhost_dev_sync_region(struct vhost_dev *de= v, } } =20 +static bool vhost_dev_has_iommu(struct vhost_dev *dev) +{ + VirtIODevice *vdev =3D dev->vdev; + + /* + * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support + * incremental memory mapping API via IOTLB API. For platform that + * does not have IOMMU, there's no need to enable this feature + * which may cause unnecessary IOTLB miss/update transactions. + */ + if (vdev) { + return virtio_bus_device_iommu_enabled(vdev) && + virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM); + } else { + return false; + } +} + static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, MemoryRegionSection *section, hwaddr first, @@ -137,8 +156,51 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *d= ev, continue; } =20 - vhost_dev_sync_region(dev, section, start_addr, end_addr, vq->used= _phys, - range_get_last(vq->used_phys, vq->used_size)= ); + if (vhost_dev_has_iommu(dev)) { + IOMMUTLBEntry iotlb; + hwaddr used_phys =3D vq->used_phys, used_size =3D vq->used_siz= e; + hwaddr phys, s, offset; + + while (used_size) { + rcu_read_lock(); + iotlb =3D address_space_get_iotlb_entry(dev->vdev->dma_as, + used_phys, + true, + MEMTXATTRS_UNSPECIFI= ED); + rcu_read_unlock(); + + if (!iotlb.target_as) { + qemu_log_mask(LOG_GUEST_ERROR, "translation " + "failure for used_iova %"PRIx64"\n", + used_phys); + return -EINVAL; + } + + offset =3D used_phys & iotlb.addr_mask; + phys =3D iotlb.translated_addr + offset; + + /* + * Distance from start of used ring until last byte of + * IOMMU page. + */ + s =3D iotlb.addr_mask - offset; + /* + * Size of used ring, or of the part of it until end + * of IOMMU page. To avoid zero result, do the adding + * outside of MIN(). + */ + s =3D MIN(s, used_size - 1) + 1; + + vhost_dev_sync_region(dev, section, start_addr, end_addr, = phys, + range_get_last(phys, s)); + used_size -=3D s; + used_phys +=3D s; + } + } else { + vhost_dev_sync_region(dev, section, start_addr, + end_addr, vq->used_phys, + range_get_last(vq->used_phys, vq->used_s= ize)); + } } return 0; } @@ -306,24 +368,6 @@ static inline void vhost_dev_log_resize(struct vhost_d= ev *dev, uint64_t size) dev->log_size =3D size; } =20 -static bool vhost_dev_has_iommu(struct vhost_dev *dev) -{ - VirtIODevice *vdev =3D dev->vdev; - - /* - * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support - * incremental memory mapping API via IOTLB API. For platform that - * does not have IOMMU, there's no need to enable this feature - * which may cause unnecessary IOTLB miss/update transactions. - */ - if (vdev) { - return virtio_bus_device_iommu_enabled(vdev) && - virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM); - } else { - return false; - } -} - static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr, hwaddr *plen, bool is_write) { --=20 2.25.1