From patchwork Thu Dec 10 07:34:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhukeqian X-Patchwork-Id: 11963555 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BAE04C4361B for ; Thu, 10 Dec 2020 07:37:08 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5743B20727 for ; Thu, 10 Dec 2020 07:37:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5743B20727 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-ID:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=RuP7+oRhCbE1a7/1L9dSjhrkw/UPMJ2rDviGvncvIMU=; b=20GqvHje7KyltUTuLVvXOPVVb fVLI5g/dtCFIiHeaOsG0e8T8bTxGJdX3OC8MtVCHU3ck6/JO1FQSBrWixrn2v1ax5TgJ8n3bindxx ycudr+2cFAgwVCAlIAOmiBIc7n5a1O4xtx+LoqptWsy8dHKx3VwnnehZoxEQzqm52qBhUTnnWzXEC CekHFOq8Ts5ASe2GQ7Hqu3Y3Ta+mnAiLlCC12GrAkSJWNJtqx2cPN2Gd2Mn4ZpLEQXVVNlX9nJzl/ 4OGLkvZEfO0qoWCeyFSJAmj6GSclXdNV3jFKqzxlzUlE5nsXpPQqvfs01aVRGycKOdIhP0X+y+aHH 96Kc9jLrg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1knGUS-0007Gc-2k; Thu, 10 Dec 2020 07:35:52 +0000 Received: from szxga05-in.huawei.com ([45.249.212.191]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1knGUM-0007Aw-FQ for linux-arm-kernel@lists.infradead.org; Thu, 10 Dec 2020 07:35:47 +0000 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Cs5KV1ZwPzM365; Thu, 10 Dec 2020 15:34:54 +0800 (CST) Received: from DESKTOP-5IS4806.china.huawei.com (10.174.187.37) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.487.0; Thu, 10 Dec 2020 15:35:28 +0800 From: Keqian Zhu To: , , , , , Alex Williamson , Cornelia Huck , Marc Zyngier , Will Deacon , Robin Murphy Subject: [PATCH 4/7] vfio: iommu_type1: Fix missing dirty page when promote pinned_scope Date: Thu, 10 Dec 2020 15:34:22 +0800 Message-ID: <20201210073425.25960-5-zhukeqian1@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20201210073425.25960-1-zhukeqian1@huawei.com> References: <20201210073425.25960-1-zhukeqian1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.174.187.37] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201210_023546_868569_A9A610AC X-CRM114-Status: GOOD ( 12.54 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Suzuki K Poulose , Catalin Marinas , Joerg Roedel , jiangkunkun@huawei.com, Sean Christopherson , Alexios Zavras , Mark Brown , James Morse , wanghaibin.wang@huawei.com, Thomas Gleixner , Keqian Zhu , Andrew Morton , Julien Thierry Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org When we pin or detach a group which is not dirty tracking capable, we will try to promote pinned_scope of vfio_iommu. If we succeed to do so, vfio only report pinned_scope as dirty to userspace next time, but these memory written before pin or detach is missed. The solution is that we must populate all dma range as dirty before promoting pinned_scope of vfio_iommu. Signed-off-by: Keqian Zhu --- drivers/vfio/vfio_iommu_type1.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index bd9a94590ebc..00684597b098 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1633,6 +1633,20 @@ static struct vfio_group *vfio_iommu_find_iommu_group(struct vfio_iommu *iommu, return group; } +static void vfio_populate_bitmap_all(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); + unsigned long nbits = dma->size >> pgshift; + + if (dma->iommu_mapped) + bitmap_set(dma->bitmap, 0, nbits); + } +} + static void promote_pinned_page_dirty_scope(struct vfio_iommu *iommu) { struct vfio_domain *domain; @@ -1657,6 +1671,10 @@ static void promote_pinned_page_dirty_scope(struct vfio_iommu *iommu) } iommu->pinned_page_dirty_scope = true; + + /* Set all bitmap to avoid missing dirty page */ + if (iommu->dirty_page_tracking) + vfio_populate_bitmap_all(iommu); } static bool vfio_iommu_has_sw_msi(struct list_head *group_resv_regions,