From nobody Wed Nov 19 08:26:28 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=1615369410; cv=none; d=zohomail.com; s=zohoarc; b=JeYjUCtHoEkDwFRXu0CDrHnEqoAZ20O5f683tPt7wP6ooOXznvSG2Y93Duy0IeYBw+Zfk9caj1do0Ke28u/U2pGLNA7vcSmHnPnTGtG+Hf9VbS4yDxFT3/KO2YFkuEb7PTnZeJx70n6QZrnDplrjhE9D24f7WSmGTYmajvXKpTk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615369410; 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=Vtvb9MuruWx6uum1yorCqf6kq1F2RCuDfkWy2WzxC/k=; b=IgBGcuyKTZNg5A7HBgHcijfTpOD7s5BGAJgv1xgudQXbCJ4KTcwonrTQ3rYuIvidS7IG+P9chUhBKw8G1O9SHqphU1HRE10nXO+QePJP5qUlW0zN0nnWFdpmvow2U693+fg/TirmZ6NB8sm8i69UmD7nu9w4JpOHm4Bg1NozvSU= 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 1615369410581828.9848926147573; Wed, 10 Mar 2021 01:43:30 -0800 (PST) Received: from localhost ([::1]:37518 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lJvNJ-0008Tn-Ei for importer@patchew.org; Wed, 10 Mar 2021 04:43:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55348) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lJvLw-0006fg-5t for qemu-devel@nongnu.org; Wed, 10 Mar 2021 04:42:04 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:4385) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lJvLt-000668-LW for qemu-devel@nongnu.org; Wed, 10 Mar 2021 04:42:03 -0500 Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DwRrV0rfHz17J1y; 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:50 +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 3/3] vfio/migration: Support VFIO_IOMMU_DIRTY_PAGES_FLAG_CLEAR_BITMAP Date: Wed, 10 Mar 2021 17:41:06 +0800 Message-ID: <20210310094106.2191-4-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 The new capability VFIO_DIRTY_LOG_MANUAL_CLEAR and the new ioctl VFIO_IOMMU_DIRTY_PAGES_FLAG_CLEAR_BITMAP have been introduced in the kernel, tweak the userspace side to use them. Check if the kernel supports VFIO_DIRTY_LOG_MANUAL_CLEAR and provide the log_clear() hook for vfio_memory_listener. If the kernel supports it, deliever the clear message to kernel. Signed-off-by: Zenghui Yu Signed-off-by: Kunkun Jiang --- hw/vfio/common.c | 145 +++++++++++++++++++++++++++++++++- include/hw/vfio/vfio-common.h | 1 + 2 files changed, 145 insertions(+), 1 deletion(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index e7cb0e2b23..713515b86a 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1182,10 +1182,146 @@ static void vfio_listener_log_sync(MemoryListener = *listener, } } =20 +/* + * I'm not sure if there's any alignment requirement for the CLEAR_BITMAP + * ioctl. But copy from kvm side and align {start, size} with 64 pages. + * + * I think the code can be simplified a lot if no alignment requirement. + */ +#define VFIO_CLEAR_LOG_SHIFT 6 +#define VFIO_CLEAR_LOG_ALIGN (qemu_real_host_page_size << VFIO_CLEAR_LOG_= SHIFT) +#define VFIO_CLEAR_LOG_MASK (-VFIO_CLEAR_LOG_ALIGN) + +static int vfio_log_clear_one_range(VFIOContainer *container, + VFIODMARange *qrange, uint64_t start, uint64_t size) +{ + struct vfio_iommu_type1_dirty_bitmap *dbitmap; + struct vfio_iommu_type1_dirty_bitmap_get *range; + + dbitmap =3D g_malloc0(sizeof(*dbitmap) + sizeof(*range)); + + dbitmap->argsz =3D sizeof(*dbitmap) + sizeof(*range); + dbitmap->flags =3D VFIO_IOMMU_DIRTY_PAGES_FLAG_CLEAR_BITMAP; + range =3D (struct vfio_iommu_type1_dirty_bitmap_get *)&dbitmap->data; + + /* + * Now let's deal with the actual bitmap, which is almost the same + * as the kvm side. + */ + uint64_t end, bmap_start, start_delta, bmap_npages; + unsigned long *bmap_clear =3D NULL, psize =3D qemu_real_host_page_size; + int ret; + + bmap_start =3D start & VFIO_CLEAR_LOG_MASK; + start_delta =3D start - bmap_start; + bmap_start /=3D psize; + + bmap_npages =3D DIV_ROUND_UP(size + start_delta, VFIO_CLEAR_LOG_ALIGN) + << VFIO_CLEAR_LOG_SHIFT; + end =3D qrange->size / psize; + if (bmap_npages > end - bmap_start) { + bmap_npages =3D end - bmap_start; + } + start_delta /=3D psize; + + if (start_delta) { + bmap_clear =3D bitmap_new(bmap_npages); + bitmap_copy_with_src_offset(bmap_clear, qrange->bitmap, + bmap_start, start_delta + size / psize= ); + bitmap_clear(bmap_clear, 0, start_delta); + range->bitmap.data =3D (__u64 *)bmap_clear; + } else { + range->bitmap.data =3D (__u64 *)(qrange->bitmap + BIT_WORD(bmap_st= art)); + } + + range->iova =3D qrange->iova + bmap_start * psize; + range->size =3D bmap_npages * psize; + range->bitmap.size =3D ROUND_UP(bmap_npages, sizeof(__u64) * BITS_PER_= BYTE) / + BITS_PER_BYTE; + range->bitmap.pgsize =3D qemu_real_host_page_size; + + ret =3D ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, dbitmap); + if (ret) { + error_report("Failed to clear dirty log for iova: 0x%"PRIx64 + " size: 0x%"PRIx64" err: %d", (uint64_t)range->iova, + (uint64_t)range->size, errno); + goto err_out; + } + + bitmap_clear(qrange->bitmap, bmap_start + start_delta, size / psize); +err_out: + g_free(bmap_clear); + g_free(dbitmap); + return 0; +} + +static int vfio_physical_log_clear(VFIOContainer *container, + MemoryRegionSection *section) +{ + uint64_t start, size, offset, count; + VFIODMARange *qrange; + int ret =3D 0; + + if (!container->dirty_log_manual_clear) { + /* No need to do explicit clear */ + return ret; + } + + start =3D section->offset_within_address_space; + size =3D int128_get64(section->size); + + if (!size) { + return ret; + } + + QLIST_FOREACH(qrange, &container->dma_list, next) { + /* + * Discard ranges that do not overlap the section (e.g., the + * Memory BAR regions of the device) + */ + if (qrange->iova > start + size - 1 || + start > qrange->iova + qrange->size - 1) { + continue; + } + + if (start >=3D qrange->iova) { + /* The range starts before section or is aligned to it. */ + offset =3D start - qrange->iova; + count =3D MIN(qrange->size - offset, size); + } else { + /* The range starts after section. */ + offset =3D 0; + count =3D MIN(qrange->size, size - (qrange->iova - start)); + } + ret =3D vfio_log_clear_one_range(container, qrange, offset, count); + if (ret < 0) { + break; + } + } + + return ret; +} + +static void vfio_listener_log_clear(MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container =3D container_of(listener, VFIOContainer, lis= tener); + + if (vfio_listener_skipped_section(section) || + !container->dirty_pages_supported) { + return; + } + + if (vfio_devices_all_saving(container)) { + vfio_physical_log_clear(container, section); + } +} + static const MemoryListener vfio_memory_listener =3D { .region_add =3D vfio_listener_region_add, .region_del =3D vfio_listener_region_del, .log_sync =3D vfio_listener_log_sync, + .log_clear =3D vfio_listener_log_clear, }; =20 static void vfio_listener_release(VFIOContainer *container) @@ -1613,7 +1749,7 @@ static int vfio_get_iommu_type(VFIOContainer *contain= er, static int vfio_init_container(VFIOContainer *container, int group_fd, Error **errp) { - int iommu_type, ret; + int iommu_type, dirty_log_manual_clear, ret; =20 iommu_type =3D vfio_get_iommu_type(container, errp); if (iommu_type < 0) { @@ -1642,6 +1778,13 @@ static int vfio_init_container(VFIOContainer *contai= ner, int group_fd, } =20 container->iommu_type =3D iommu_type; + + dirty_log_manual_clear =3D ioctl(container->fd, VFIO_CHECK_EXTENSION, + VFIO_DIRTY_LOG_MANUAL_CLEAR); + if (dirty_log_manual_clear) { + container->dirty_log_manual_clear =3D dirty_log_manual_clear; + } + return 0; } =20 diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index bd6eca9332..bcd6a0c440 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -93,6 +93,7 @@ typedef struct VFIOContainer { Error *error; bool initialized; bool dirty_pages_supported; + bool dirty_log_manual_clear; uint64_t dirty_pgsizes; uint64_t max_dirty_bitmap_size; unsigned long pgsizes; --=20 2.23.0