Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp227791pxb; Fri, 15 Jan 2021 01:31:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJzzQ2KEtxo1imcWav0IBnN6FzAtpl3lTXILmKs0J5oAGCuASy+aOnbFIticaxRgzz25wDWN X-Received: by 2002:a05:6402:40c4:: with SMTP id z4mr8691872edb.233.1610703088704; Fri, 15 Jan 2021 01:31:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610703088; cv=none; d=google.com; s=arc-20160816; b=lgowQrRYPsKRvl7BeqTIwq5Tb00RZxqh70acQ7OXcLXACDHGU1wsuMWkdrTioMTGxw 3DvhRDXavZRp77hDrA6Cb9J+L07sXUtlBrkiFs4+kBBiZMQrmaFFbM9QhLBPA2ksxqgn JAApHJNVKEjcXW5ZzAQTKmWCZsSpd2fmhFZ2YPXuowTK+20AxRloIZB+H+zxcwmc2C30 20clQX5CUkB4Y6XuR0Lzoe2A3X5lFZGYRfwR6fi4XvuaPv97yIEze2ylZnQ50XMgB1FA pDKxOC69G5wCC+a9JaSpxcDLWPXWt/3+fkgGLS3indXdE3Sp/x3jgjNe1dxsYZMdCZiw ckRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=O8JvpQ++9WNB35V5Vl4HtxbXVp9hGQsG6YK+Eq3L9po=; b=OvyHTZHuhWwhNkQH54EaHdRa9CUUglUhcdIPk+AL+A4loVCSOzg6kJDwbdG6dp1Axf O0XcvZF72HY26fiFUK2N7S9M7kcgaEoE5fscDb0+N+aEFvGquZ23rco19DML9uu3fr7d aQYl4aszHRG35kCs15UEumTPfSit+wnudm8Je7x2xPrpXiekyKH58uiTOixLswuh+uwh L/T53esReymSdHwpTZgIZLtgej8m3xz05lW/9AsJWFMJxptMMyGbfi0PXgJ2ylw9LJjm WU2XofI5vPzUow8Aypg+XaeqQG3D1ifd49TuBLBMAh1PZTIeVSVCXeEp0AJEMlhljdPu klIw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id he44si1954239ejc.386.2021.01.15.01.30.59; Fri, 15 Jan 2021 01:31:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730739AbhAOJ2D (ORCPT + 99 others); Fri, 15 Jan 2021 04:28:03 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:11536 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730633AbhAOJ16 (ORCPT ); Fri, 15 Jan 2021 04:27:58 -0500 Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4DHG4z6Q6rzMKkp; Fri, 15 Jan 2021 17:25:55 +0800 (CST) Received: from DESKTOP-5IS4806.china.huawei.com (10.174.184.42) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.498.0; Fri, 15 Jan 2021 17:27:06 +0800 From: Keqian Zhu To: , , , , , Alex Williamson , Kirti Wankhede , Cornelia Huck , Will Deacon , Marc Zyngier , Catalin Marinas CC: Mark Rutland , James Morse , Robin Murphy , Joerg Roedel , "Daniel Lezcano" , Thomas Gleixner , Suzuki K Poulose , Julien Thierry , Andrew Morton , Alexios Zavras , , Subject: [PATCH v2 1/2] vfio/iommu_type1: Populate full dirty when detach non-pinned group Date: Fri, 15 Jan 2021 17:26:42 +0800 Message-ID: <20210115092643.728-2-zhukeqian1@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20210115092643.728-1-zhukeqian1@huawei.com> References: <20210115092643.728-1-zhukeqian1@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.174.184.42] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a group with non-pinned-page dirty scope is detached with dirty logging enabled, we should fully populate the dirty bitmaps at the time it's removed since we don't know the extent of its previous DMA, nor will the group be present to trigger the full bitmap when the user retrieves the dirty bitmap. Fixes: d6a4c185660c ("vfio iommu: Implementation of ioctl for dirty pages tracking") Suggested-by: Alex Williamson Signed-off-by: Keqian Zhu --- drivers/vfio/vfio_iommu_type1.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 0b4dedaa9128..4e82b9a3440f 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -236,6 +236,19 @@ static void vfio_dma_populate_bitmap(struct vfio_dma *dma, size_t pgsize) } } +static void vfio_iommu_populate_bitmap_full(struct vfio_iommu *iommu) +{ + struct rb_node *n; + unsigned long pgshift = __ffs(iommu->pgsize_bitmap); + + for (n = rb_first(&iommu->dma_list); n; n = rb_next(n)) { + struct vfio_dma *dma = rb_entry(n, struct vfio_dma, node); + + if (dma->iommu_mapped) + bitmap_set(dma->bitmap, 0, dma->size >> pgshift); + } +} + static int vfio_dma_bitmap_alloc_all(struct vfio_iommu *iommu, size_t pgsize) { struct rb_node *n; @@ -2415,8 +2428,11 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, * Removal of a group without dirty tracking may allow the iommu scope * to be promoted. */ - if (update_dirty_scope) + if (update_dirty_scope) { update_pinned_page_dirty_scope(iommu); + if (iommu->dirty_page_tracking) + vfio_iommu_populate_bitmap_full(iommu); + } mutex_unlock(&iommu->lock); } -- 2.19.1