From nobody Wed Nov 19 08:28:02 2025 Delivered-To: importer@patchew.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 ARC-Seal: i=1; a=rsa-sha256; t=1615369411; cv=none; d=zohomail.com; s=zohoarc; b=YMVggJ/xCQtMNfPqIJM8floWUbiz7tvM/UpxCStMectM4zmCMeHHgOicVyKhhd2qkCUoTvjm+4LxwRVuCaJbOAQfROyVF/eZqCMUnu5WH1tOVjgWrWdQaLV0XYlCwuIZLALc/FoCYU40Zl9DnoQYbPviJ0gyJYyCCm/WNgJhGBI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615369411; h=Content-Type:Content-Transfer-Encoding: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=DPimhN8gsYbOoyHRtxLQvG9RPkdtxkTco25L4g/xBSE=; b=N62oyW+UGlCoVQVP2MdmlH/aKxxGRVXtLodzcz7rG4XfbFIUEaxzs8txCQNtYgpJbaHngJ69w/HSFkaQzk6CQI2dGX4JIWTNWcIIlPwd16vS+2XGDWZsNgyLaK8nnUMhTX3MoJfBiesrjRNdbec236CZCXLdO2OCE0MTQjBR7KU= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 161536941179925.952287101235925; Wed, 10 Mar 2021 01:43:31 -0800 (PST) Received: from localhost ([::1]:37668 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lJvNK-00006D-PU for importer@patchew.org; Wed, 10 Mar 2021 04:43:30 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55350) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lJvLw-0006gj-UI for qemu-devel@nongnu.org; Wed, 10 Mar 2021 04:42:04 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:4384) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lJvLu-000666-JR for qemu-devel@nongnu.org; Wed, 10 Mar 2021 04:42:04 -0500 Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DwRrV0RYXz17J1q; Wed, 10 Mar 2021 17:40:10 +0800 (CST) Received: from DESKTOP-6NKE0BC.china.huawei.com (10.174.185.210) by DGGEMS406-HUB.china.huawei.com (10.3.19.206) with Microsoft SMTP Server id 14.3.498.0; Wed, 10 Mar 2021 17:41:48 +0800 From: Kunkun Jiang To: Alex Williamson , "Michael S . Tsirkin" , Cornelia Huck , Paolo Bonzini , Kirti Wankhede , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , "Tarun Gupta" , "open list:All patches CC here" Subject: [RFC PATCH 2/3] vfio: Maintain DMA mapping range for the container Date: Wed, 10 Mar 2021 17:41:05 +0800 Message-ID: <20210310094106.2191-3-jiangkunkun@huawei.com> X-Mailer: git-send-email 2.26.2.windows.1 In-Reply-To: <20210310094106.2191-1-jiangkunkun@huawei.com> References: <20210310094106.2191-1-jiangkunkun@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.185.210] X-CFilter-Loop: Reflected 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=45.249.212.190; envelope-from=jiangkunkun@huawei.com; helo=szxga04-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, 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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: shameerali.kolothum.thodi@huawei.com, Eric Auger , Peter Xu , Zenghui Yu , wanghaibin.wang@huawei.com, Keqian Zhu Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Zenghui Yu When synchronizing dirty bitmap from kernel VFIO we do it in a per-iova-range fashion and we allocate the userspace bitmap for each of the ioctl. This patch introduces `struct VFIODMARange` to describe a range of the given DMA mapping with respect to a VFIO_IOMMU_MAP_DMA operation, and make the bitmap cache of this range be persistent so that we don't need to g_try_malloc0() every time. Note that the new structure is almost a copy of `struct vfio_iommu_type1_dma_map` but only internally used by QEMU. More importantly, the cached per-iova-range dirty bitmap will be further used when we want to add support for the CLEAR_BITMAP and this cached bitmap will be used to guarantee we don't clear any unknown dirty bits otherwise that can be a severe data loss issue for migration code. It's pretty intuitive to maintain a bitmap per container since we perform log_sync at this granule. But I don't know how to deal with things like memory hot-{un}plug, sparse DMA mappings, etc. Suggestions welcome. * yet something to-do: - can't work with guest viommu - no locks - etc [ The idea and even the commit message are largely inherited from kvm side. See commit 9f4bf4baa8b820c7930e23c9566c9493db7e1d25. ] Signed-off-by: Zenghui Yu Signed-off-by: Kunkun Jiang --- hw/vfio/common.c | 62 +++++++++++++++++++++++++++++++---- include/hw/vfio/vfio-common.h | 9 +++++ 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index c0ff20f0a2..e7cb0e2b23 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -426,6 +426,29 @@ unmap_exit: return ret; } =20 +static VFIODMARange *vfio_lookup_match_range(VFIOContainer *container, + hwaddr start_addr, hwaddr size) +{ + VFIODMARange *qrange; + + QLIST_FOREACH(qrange, &container->dma_list, next) { + if (qrange->iova =3D=3D start_addr && qrange->size =3D=3D size) { + return qrange; + } + } + return NULL; +} + +static void vfio_dma_range_init_dirty_bitmap(VFIODMARange *qrange) +{ + uint64_t pages, size; + + pages =3D REAL_HOST_PAGE_ALIGN(qrange->size) / qemu_real_host_page_siz= e; + size =3D ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) / BITS_PER_BYT= E; + + qrange->bitmap =3D g_malloc0(size); +} + /* * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86 */ @@ -439,12 +462,29 @@ static int vfio_dma_unmap(VFIOContainer *container, .iova =3D iova, .size =3D size, }; + VFIODMARange *qrange; =20 if (iotlb && container->dirty_pages_supported && vfio_devices_all_running_and_saving(container)) { return vfio_dma_unmap_bitmap(container, iova, size, iotlb); } =20 + /* + * unregister the DMA range + * + * It seems that the memory layer will give us the same section as the= one + * used in region_add(). Otherwise it'll be complicated to manipulate = the + * bitmap across region_{add,del}. Is there any guarantee? + * + * But there is really not such a restriction on the kernel interface + * (VFIO_IOMMU_DIRTY_PAGES_FLAG_{UN}MAP_DMA, etc). + */ + qrange =3D vfio_lookup_match_range(container, iova, size); + assert(qrange); + g_free(qrange->bitmap); + QLIST_REMOVE(qrange, next); + g_free(qrange); + while (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) { /* * The type1 backend has an off-by-one bug in the kernel (71a7d3d7= 8e3c @@ -481,6 +521,14 @@ static int vfio_dma_map(VFIOContainer *container, hwad= dr iova, .iova =3D iova, .size =3D size, }; + VFIODMARange *qrange; + + qrange =3D g_malloc0(sizeof(*qrange)); + qrange->iova =3D iova; + qrange->size =3D size; + QLIST_INSERT_HEAD(&container->dma_list, qrange, next); + /* XXX allocate the dirty bitmap on demand */ + vfio_dma_range_init_dirty_bitmap(qrange); =20 if (!readonly) { map.flags |=3D VFIO_DMA_MAP_FLAG_WRITE; @@ -992,9 +1040,14 @@ static int vfio_get_dirty_bitmap(VFIOContainer *conta= iner, uint64_t iova, { struct vfio_iommu_type1_dirty_bitmap *dbitmap; struct vfio_iommu_type1_dirty_bitmap_get *range; + VFIODMARange *qrange; uint64_t pages; int ret; =20 + qrange =3D vfio_lookup_match_range(container, iova, size); + /* the same as vfio_dma_unmap() */ + assert(qrange); + dbitmap =3D g_malloc0(sizeof(*dbitmap) + sizeof(*range)); =20 dbitmap->argsz =3D sizeof(*dbitmap) + sizeof(*range); @@ -1013,11 +1066,8 @@ static int vfio_get_dirty_bitmap(VFIOContainer *cont= ainer, uint64_t iova, pages =3D REAL_HOST_PAGE_ALIGN(range->size) / qemu_real_host_page_size; range->bitmap.size =3D ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) / BITS_PER_BYTE; - range->bitmap.data =3D g_try_malloc0(range->bitmap.size); - if (!range->bitmap.data) { - ret =3D -ENOMEM; - goto err_out; - } + + range->bitmap.data =3D (__u64 *)qrange->bitmap; =20 ret =3D ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, dbitmap); if (ret) { @@ -1033,7 +1083,6 @@ static int vfio_get_dirty_bitmap(VFIOContainer *conta= iner, uint64_t iova, trace_vfio_get_dirty_bitmap(container->fd, range->iova, range->size, range->bitmap.size, ram_addr); err_out: - g_free(range->bitmap.data); g_free(dbitmap); =20 return ret; @@ -1737,6 +1786,7 @@ static int vfio_connect_container(VFIOGroup *group, A= ddressSpace *as, container->dirty_pages_supported =3D false; QLIST_INIT(&container->giommu_list); QLIST_INIT(&container->hostwin_list); + QLIST_INIT(&container->dma_list); =20 ret =3D vfio_init_container(container, group->fd, errp); if (ret) { diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 6141162d7a..bd6eca9332 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -76,6 +76,14 @@ typedef struct VFIOAddressSpace { =20 struct VFIOGroup; =20 +typedef struct VFIODMARange { + QLIST_ENTRY(VFIODMARange) next; + hwaddr iova; + size_t size; + void *vaddr; /* unused */ + unsigned long *bitmap; /* dirty bitmap cache for this range */ +} VFIODMARange; + typedef struct VFIOContainer { VFIOAddressSpace *space; int fd; /* /dev/vfio/vfio, empowered by the attached groups */ @@ -91,6 +99,7 @@ typedef struct VFIOContainer { QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; QLIST_HEAD(, VFIOGroup) group_list; + QLIST_HEAD(, VFIODMARange) dma_list; QLIST_ENTRY(VFIOContainer) next; } VFIOContainer; =20 --=20 2.23.0