Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2732846pxb; Sun, 24 Jan 2021 18:49:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJwO/0cYu8v0hMkkfEFrbAK/dwaD8yloxez5q00OoNCWUcM8ZJzKQpkjfHMbzOgmvKPB4sEq X-Received: by 2002:a17:906:803:: with SMTP id e3mr81099ejd.346.1611542968237; Sun, 24 Jan 2021 18:49:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611542968; cv=none; d=google.com; s=arc-20160816; b=r0xZ61SGydXxucBS8bezjQqKpb6HzRccE88GEeCpvXfSotCC1UOBqVfnEQuXnnq573 9mXHeSQIO1Vw4O9pMsn0uF9WXOLbwzyXNNJ72f+C/zYRQ+NugS5llQ/LxkRFAFXN9ilU Zw0f/isxsUakuoD0a2HSqsva0S3GSe7yDddsQlnInsMpxgsU5oEDjBntf5G26cUNa9Fw w0sPwfOtvrKWWlNrmwj1wMhiUofVVnN0icutkeuZQNllWzZNKK/EOt3cFzSdxquvJ1PR BtudNRUkJaZyhpQ9m+i2kFTJR+sgITwv3Sh+DMEM405vX/aWYvA2caGpi2NgZIfZGf3g 8BIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=zKOV9McblVS7K3tSBxF8ZMAt4JUczpVBf4PgpYGZmY8=; b=x1ATj3fET1QMJebDD2VHBkfThMomA6rkXHfxbecT6iKuxhw0KLyMhm78tK2UdDF/4s RTEzaIXE38diRQfjJ/P1p8/uUvQQwYz/6L6v28VO+t/vlGEvY4Yaacek/+in6yVBBSIy VTu1Ezqg5csfBoayay1pl23K5ZmMbg4f1CgRlPXmTpWGVBnFwxEEU5n3HGo8zkQgefBL C+jUlnhHOKgGdXihYEHDdMGqD9o1e+1TZo4EcYNbZupxZKIUePnv8I0eH5RhVG6Wv8tF OVg9aqorrP23yvGtgjc4t8TM4rbw68WQkDQLpSW2J/RUY7Fxg4Jpwmdd93qMwCTtrNtQ vVhA== 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 gz7si5721596ejc.87.2021.01.24.18.49.04; Sun, 24 Jan 2021 18:49: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 S1726671AbhAYCrg (ORCPT + 99 others); Sun, 24 Jan 2021 21:47:36 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:11435 "EHLO szxga06-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726601AbhAYCre (ORCPT ); Sun, 24 Jan 2021 21:47:34 -0500 Received: from DGGEMS409-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4DPDkp1PRwzjBfN; Mon, 25 Jan 2021 10:45:54 +0800 (CST) Received: from DESKTOP-5IS4806.china.huawei.com (10.174.184.42) by DGGEMS409-HUB.china.huawei.com (10.3.19.209) with Microsoft SMTP Server id 14.3.498.0; Mon, 25 Jan 2021 10:46:45 +0800 From: Keqian Zhu To: , , , , , Alex Williamson , Kirti Wankhede , "Cornelia Huck" CC: 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: [PATCH] vfio/iommu_type1: Mantainance a counter for non_pinned_groups Date: Mon, 25 Jan 2021 10:46:42 +0800 Message-ID: <20210125024642.14604-1-zhukeqian1@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 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 With this counter, we never need to traverse all groups to update pinned_scope of vfio_iommu. Suggested-by: Alex Williamson Signed-off-by: Keqian Zhu --- drivers/vfio/vfio_iommu_type1.c | 40 +++++---------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 0b4dedaa9128..bb4bbcc79101 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -73,7 +73,7 @@ struct vfio_iommu { bool v2; bool nesting; bool dirty_page_tracking; - bool pinned_page_dirty_scope; + uint64_t num_non_pinned_groups; }; struct vfio_domain { @@ -148,7 +148,6 @@ static int put_pfn(unsigned long pfn, int prot); static struct vfio_group *vfio_iommu_find_iommu_group(struct vfio_iommu *iommu, struct iommu_group *iommu_group); -static void update_pinned_page_dirty_scope(struct vfio_iommu *iommu); /* * This code handles mapping and unmapping of user data buffers * into DMA'ble space using the IOMMU @@ -714,7 +713,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, group = vfio_iommu_find_iommu_group(iommu, iommu_group); if (!group->pinned_page_dirty_scope) { group->pinned_page_dirty_scope = true; - update_pinned_page_dirty_scope(iommu); + iommu->num_non_pinned_groups--; } goto pin_done; @@ -991,7 +990,7 @@ static int update_user_bitmap(u64 __user *bitmap, struct vfio_iommu *iommu, * mark all pages dirty if any IOMMU capable device is not able * to report dirty pages and all pages are pinned and mapped. */ - if (!iommu->pinned_page_dirty_scope && dma->iommu_mapped) + if (iommu->num_non_pinned_groups && dma->iommu_mapped) bitmap_set(dma->bitmap, 0, nbits); if (shift) { @@ -1622,33 +1621,6 @@ static struct vfio_group *vfio_iommu_find_iommu_group(struct vfio_iommu *iommu, return group; } -static void update_pinned_page_dirty_scope(struct vfio_iommu *iommu) -{ - struct vfio_domain *domain; - struct vfio_group *group; - - list_for_each_entry(domain, &iommu->domain_list, next) { - list_for_each_entry(group, &domain->group_list, next) { - if (!group->pinned_page_dirty_scope) { - iommu->pinned_page_dirty_scope = false; - return; - } - } - } - - if (iommu->external_domain) { - domain = iommu->external_domain; - list_for_each_entry(group, &domain->group_list, next) { - if (!group->pinned_page_dirty_scope) { - iommu->pinned_page_dirty_scope = false; - return; - } - } - } - - iommu->pinned_page_dirty_scope = true; -} - static bool vfio_iommu_has_sw_msi(struct list_head *group_resv_regions, phys_addr_t *base) { @@ -2057,8 +2029,6 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, * addition of a dirty tracking group. */ group->pinned_page_dirty_scope = true; - if (!iommu->pinned_page_dirty_scope) - update_pinned_page_dirty_scope(iommu); mutex_unlock(&iommu->lock); return 0; @@ -2188,7 +2158,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, * demotes the iommu scope until it declares itself dirty tracking * capable via the page pinning interface. */ - iommu->pinned_page_dirty_scope = false; + iommu->num_non_pinned_groups++; mutex_unlock(&iommu->lock); vfio_iommu_resv_free(&group_resv_regions); @@ -2416,7 +2386,7 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, * to be promoted. */ if (update_dirty_scope) - update_pinned_page_dirty_scope(iommu); + iommu->num_non_pinned_groups--; mutex_unlock(&iommu->lock); } -- 2.19.1