From nobody Mon Feb 9 02:52:05 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1592687319; cv=none; d=zohomail.com; s=zohoarc; b=YzqCKwdZlJ3tiCQ4JzSNF9sUQMDherd84Z7t0YC512rF5dCeHjhinafyMMh+WirxuJNT+LZcFE/lqGDc8F+AmgNkNEwODEWVHCLd+UbbgOsjv3pM1zVYIQ4PS26ohTWDIzlo8vMggqRAtbNSK+iICeIoi7UoG13rqZJ4nNQyv/0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1592687319; 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=7MOtC0a1G3F+JPPeNTGaDgzeI+ej4DZNVmcUZDoxzKg=; b=e65Nq6zwhIlaixrapZCXJ3d1tNYVhtTfWLB6vfAK1e1iz2t6LCw6XzK+wvyjasmYavI443Ddp8O7DjudU9VghCvxKVKedbHTUmwFbD7iXFp13FsJHyXWsMyh1er/SF7mzRfHOmZSsQA8qhlyYSKsfxev4SpJin1Cj5jqKwocGpE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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 1592687319067600.3456923412716; Sat, 20 Jun 2020 14:08:39 -0700 (PDT) Received: from localhost ([::1]:57768 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jmkj7-0003ee-Ku for importer@patchew.org; Sat, 20 Jun 2020 17:08:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51898) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jmkXf-00072t-7k for qemu-devel@nongnu.org; Sat, 20 Jun 2020 16:56:47 -0400 Received: from hqnvemgate26.nvidia.com ([216.228.121.65]:1197) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jmkXc-0000Yf-TV for qemu-devel@nongnu.org; Sat, 20 Jun 2020 16:56:46 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate26.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Sat, 20 Jun 2020 13:56:30 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Sat, 20 Jun 2020 13:56:43 -0700 Received: from HQMAIL107.nvidia.com (172.20.187.13) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Sat, 20 Jun 2020 20:56:36 +0000 Received: from kwankhede-dev.nvidia.com (172.20.13.39) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Sat, 20 Jun 2020 20:56:29 +0000 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Sat, 20 Jun 2020 13:56:43 -0700 From: Kirti Wankhede To: , Subject: [PATCH QEMU v25 15/17] vfio: Add ioctl to get dirty pages bitmap during dma unmap. Date: Sun, 21 Jun 2020 01:51:24 +0530 Message-ID: <1592684486-18511-16-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1592684486-18511-1-git-send-email-kwankhede@nvidia.com> References: <1592684486-18511-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1592686590; bh=7MOtC0a1G3F+JPPeNTGaDgzeI+ej4DZNVmcUZDoxzKg=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=MkzxCayj0XTgBcBWnOeKlYOfEcTssG0sLheAv+nwbtq1XvNbq6SHbhy3x6GVbf8BQ 3QeHLvQDKtdEi1mAc16dP9qGhVQ1Q7AgwNPKv0I4ZeYyRJAMHLI7DYSJJMAl33sJay ttT8aVYjvcMDXvJl9oh0YXRxkkjMoK3oNXOwKfAbvjKb4K1xT4tCsj2TcmVrqN7iSP l5NV0VijDklnWUJE8QjPykEa2fpVo1jxz/8G8qlRjme+dLWC1iNIE/QswRzo2tAn7y LvR4YpzBoABuOVLRD+7qSwzJEv5HLcZ5Rte8gu9aTcXFXWbVuY98skPifwBG2lWUxp LhSSg2wcUTizQ== 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=216.228.121.65; envelope-from=kwankhede@nvidia.com; helo=hqnvemgate26.nvidia.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/20 16:54:48 X-ACL-Warn: Detected OS = Windows 7 or 8 [fuzzy] X-Spam_score_int: -80 X-Spam_score: -8.1 X-Spam_bar: -------- X-Spam_report: (-8.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action 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: cohuck@redhat.com, aik@ozlabs.ru, Zhengxiao.zx@Alibaba-inc.com, shuangtai.tst@alibaba-inc.com, qemu-devel@nongnu.org, peterx@redhat.com, Kirti Wankhede , eauger@redhat.com, yi.l.liu@intel.com, quintela@redhat.com, ziye.yang@intel.com, armbru@redhat.com, mlevitsk@redhat.com, pasic@linux.ibm.com, felipe@nutanix.com, zhi.a.wang@intel.com, kevin.tian@intel.com, yan.y.zhao@intel.com, dgilbert@redhat.com, changpeng.liu@intel.com, eskultet@redhat.com, Ken.Xue@amd.com, jonathan.davies@nutanix.com, pbonzini@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With vIOMMU, IO virtual address range can get unmapped while in pre-copy phase of migration. In that case, unmap ioctl should return pages pinned in that range and QEMU should find its correcponding guest physical addresses and report those dirty. Suggested-by: Alex Williamson Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/common.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++= +--- 1 file changed, 81 insertions(+), 4 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 0518cf228ed5..a06b8f2f66e2 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -311,11 +311,83 @@ static bool vfio_devices_are_stopped_and_saving(void) return true; } =20 +static bool vfio_devices_are_running_and_saving(void) +{ + VFIOGroup *group; + VFIODevice *vbasedev; + + QLIST_FOREACH(group, &vfio_group_list, next) { + QLIST_FOREACH(vbasedev, &group->device_list, next) { + if ((vbasedev->device_state & VFIO_DEVICE_STATE_SAVING) && + (vbasedev->device_state & VFIO_DEVICE_STATE_RUNNING)) { + continue; + } else { + return false; + } + } + } + return true; +} + +static int vfio_dma_unmap_bitmap(VFIOContainer *container, + hwaddr iova, ram_addr_t size, + IOMMUTLBEntry *iotlb) +{ + struct vfio_iommu_type1_dma_unmap *unmap; + struct vfio_bitmap *bitmap; + uint64_t pages =3D TARGET_PAGE_ALIGN(size) >> TARGET_PAGE_BITS; + int ret; + + unmap =3D g_malloc0(sizeof(*unmap) + sizeof(*bitmap)); + + unmap->argsz =3D sizeof(*unmap) + sizeof(*bitmap); + unmap->iova =3D iova; + unmap->size =3D size; + unmap->flags |=3D VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP; + bitmap =3D (struct vfio_bitmap *)&unmap->data; + + /* + * cpu_physical_memory_set_dirty_lebitmap() expects pages in bitmap of + * TARGET_PAGE_SIZE to mark those dirty. Hence set bitmap_pgsize to + * TARGET_PAGE_SIZE. + */ + + bitmap->pgsize =3D TARGET_PAGE_SIZE; + bitmap->size =3D ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) / + BITS_PER_BYTE; + + if (bitmap->size > container->max_dirty_bitmap_size) { + error_report("UNMAP: Size of bitmap too big 0x%llx", bitmap->size); + ret =3D -E2BIG; + goto unmap_exit; + } + + bitmap->data =3D g_try_malloc0(bitmap->size); + if (!bitmap->data) { + ret =3D -ENOMEM; + goto unmap_exit; + } + + ret =3D ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, unmap); + if (!ret) { + cpu_physical_memory_set_dirty_lebitmap((uint64_t *)bitmap->data, + iotlb->translated_addr, pages); + } else { + error_report("VFIO_UNMAP_DMA with DIRTY_BITMAP : %m"); + } + + g_free(bitmap->data); +unmap_exit: + g_free(unmap); + return ret; +} + /* * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86 */ static int vfio_dma_unmap(VFIOContainer *container, - hwaddr iova, ram_addr_t size) + hwaddr iova, ram_addr_t size, + IOMMUTLBEntry *iotlb) { struct vfio_iommu_type1_dma_unmap unmap =3D { .argsz =3D sizeof(unmap), @@ -324,6 +396,11 @@ static int vfio_dma_unmap(VFIOContainer *container, .size =3D size, }; =20 + if (iotlb && container->dirty_pages_supported && + vfio_devices_are_running_and_saving()) { + return vfio_dma_unmap_bitmap(container, iova, size, iotlb); + } + while (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) { /* * The type1 backend has an off-by-one bug in the kernel (71a7d3d7= 8e3c @@ -371,7 +448,7 @@ static int vfio_dma_map(VFIOContainer *container, hwadd= r iova, * the VGA ROM space. */ if (ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) =3D=3D 0 || - (errno =3D=3D EBUSY && vfio_dma_unmap(container, iova, size) =3D= =3D 0 && + (errno =3D=3D EBUSY && vfio_dma_unmap(container, iova, size, NULL)= =3D=3D 0 && ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) =3D=3D 0)) { return 0; } @@ -542,7 +619,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOM= MUTLBEntry *iotlb) } } =20 - ret =3D vfio_dma_unmap(container, iova, iotlb->addr_mask + 1); + ret =3D vfio_dma_unmap(container, iova, iotlb->addr_mask + 1, iotl= b); if (ret) { error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx") =3D %d (%m)", @@ -853,7 +930,7 @@ static void vfio_listener_region_del(MemoryListener *li= stener, } =20 if (try_unmap) { - ret =3D vfio_dma_unmap(container, iova, int128_get64(llsize)); + ret =3D vfio_dma_unmap(container, iova, int128_get64(llsize), NULL= ); if (ret) { error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx") =3D %d (%m)", --=20 2.7.0