Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp172777pxb; Fri, 15 Jan 2021 10:04:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJxrfDAUQM/FCiQ4S3QZmXINDNLpg3EcTjcij8OjsCvp8CCcwbmVLTiST5sMEXqgi/Is0cc7 X-Received: by 2002:a17:906:2f07:: with SMTP id v7mr9629092eji.343.1610733886397; Fri, 15 Jan 2021 10:04:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610733886; cv=none; d=google.com; s=arc-20160816; b=V+FmjxFNX/czDxKtkIIliNI76LpocyV6KGHaYVGWvse39lwv/Lds5FyxpzZiV3ZxRL tg6NfQ3NJ4ZentYlUCVlLE7T7W8apqT359XdCQGE2l9XK9CmYLkHaKeQLE/NUsFMYLis Z10B37z6/5+2JLi0+dYg5awUTXUyMRmLOkfYMl4U8Tva1kie1hmZu3OFFYdm+IZ3rB7c 1vPVeHlsJ2rcqb+Yvb80mSyAIAmD4Fv37WAJeXcViKiBk2VJ+feOnx0sL5u7QhnAM6f5 UJT8VYYANI7fQBH3txHI+zQFUWF3p0wH+i7qJbX8QoSEbYsvMS3H1E8PW6f3S5uEVS5W aUSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=Gq10Kr5xse22ieR9b4aF/0j+yRbARGW5WK7gbGTTvCI=; b=CNKega2TlqmGS+kDZDxjD12a/X5YOXs6r/it/Ik6ks14SUFc0VAHwqgRd1jTYRjPXc C7j9V2zllzM7B8Hd9uUDtq6OevutOWdpRw9pMa+wFEjQZgeM863dTQvdRXAsvlCk6X4m qDrsNMWF5XJw07+5VdFCcQ9nralyjtCcGkVfxvSUFfLcMW8h+Y1usgL1BPejhEeKxmNv H8M9XVSyvZDo8zOaW/CnRljUxSJwezKqPIj8Sc2FntHTrCDRfJNnotuItj9lIwBoPLnY B6Ln6DukYEcVfD0inO6la7TqpJv2yUpAUYKgR8j1GpMbPkhg1qKhJfKGalgVYDzsN7mZ zH8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AACCP0Vm; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a72si2159668edf.380.2021.01.15.10.04.23; Fri, 15 Jan 2021 10:04:46 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AACCP0Vm; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727443AbhAOSDV (ORCPT + 99 others); Fri, 15 Jan 2021 13:03:21 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:55347 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726434AbhAOSDV (ORCPT ); Fri, 15 Jan 2021 13:03:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610733714; 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: in-reply-to:in-reply-to:references:references; bh=Gq10Kr5xse22ieR9b4aF/0j+yRbARGW5WK7gbGTTvCI=; b=AACCP0VmzRAPiQcQ43ied0klwKAs4QBIgqTsiW81gqngo7pN+40H+8LGEWH+63exEV0WLq gy1MHqrsd5FbUIA6Un02tVaW8LertZyhPHrtnpyAB9YPE3yISoL236MOdd0TNrsI42MDdo srlZ9kb2Lt5KN7RuK+o6RfTcyyc6FyY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-280-gAhkavzzP3CD-QHWbBOljQ-1; Fri, 15 Jan 2021 13:01:50 -0500 X-MC-Unique: gAhkavzzP3CD-QHWbBOljQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A11F6107ACF8; Fri, 15 Jan 2021 18:01:47 +0000 (UTC) Received: from omen.home.shazbot.org (ovpn-112-255.phx2.redhat.com [10.3.112.255]) by smtp.corp.redhat.com (Postfix) with ESMTP id 90E4460BF3; Fri, 15 Jan 2021 18:01:45 +0000 (UTC) Date: Fri, 15 Jan 2021 11:01:44 -0700 From: Alex Williamson To: Keqian Zhu Cc: , , , , , Kirti Wankhede , Cornelia Huck , Will Deacon , Marc Zyngier , Catalin Marinas , Mark Rutland , James Morse , Robin Murphy , Joerg Roedel , "Daniel Lezcano" , Thomas Gleixner , Suzuki K Poulose , Julien Thierry , Andrew Morton , Alexios Zavras , , Subject: Re: [PATCH v2 1/2] vfio/iommu_type1: Populate full dirty when detach non-pinned group Message-ID: <20210115110144.61e3c843@omen.home.shazbot.org> In-Reply-To: <20210115092643.728-2-zhukeqian1@huawei.com> References: <20210115092643.728-1-zhukeqian1@huawei.com> <20210115092643.728-2-zhukeqian1@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 15 Jan 2021 17:26:42 +0800 Keqian Zhu wrote: > 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); > } > This doesn't do the right thing. This marks the bitmap dirty if: * The detached group dirty scope was not limited to pinned pages AND * Dirty tracking is enabled AND * The vfio_dma is *currently* (ie. after the detach) iommu_mapped We need to mark the bitmap dirty based on whether the vfio_dma *was* iommu_mapped by the group that is now detached. Thanks, Alex