Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp1857990pxb; Thu, 7 Oct 2021 17:08:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJynOlKimiLOxd8YxN8s4k73BO+1b/5V81bPNoR1WUHYNcbv6H5Q8jzoxqqDjlPvmyr/dkLt X-Received: by 2002:a17:906:f98c:: with SMTP id li12mr98684ejb.108.1633651703329; Thu, 07 Oct 2021 17:08:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633651703; cv=none; d=google.com; s=arc-20160816; b=KyLg6RZatDaMGYE5GbKT7fe8p0fcm2nVv+oxaLOmVwQ2A9B5Wt5vimFq6YA6q9Wup5 bD7WpzU+jLqN12jD7scIoOQdzfB96CViArCZGOwZVehXj89ZDFS/QpsotwqRRpGvkX2m Morf0WsS8nASOQmjgxdgHYvoAXpES+AAHLYfa8NNCUROvyWf4e2WVB4yABUmxstqsk94 4RI1k6IGp9w57qPZitm1CIL1pd2vgC9yMrBQhgC/p51sakwsI0cJshsAloRy+RfdZ8yJ l8XpvFDkPUKoAwO4hjHTuX/j87wDwzAGLp+O0bidUPF1tp966T+B9nfUSn+6BL5lX/tr IR8w== 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:date:subject:cc:to:from; bh=pwvT18aG2bLUwTDfCjHMMbtcU5k/aG/HJ5R8nPOKWxI=; b=y5W4LOhcSA3kTtffOm59D3V/1f3quBO1AIMAbPkDLYFusjqa0LCUXZckjSTpnHGcVv JaWKSZX0m7gYf0Oi1BmbhkaVMgtuAVPU6ILxTPCRqZSEOjGiK2LKC0lb7bs3u0vDqVDw wc/x3OAdZKVQZRB0oQ90oKaohIjdwHQbkNRYf9OI0i4Ttbxx+8Swa8Kilz8uTr7iMwqv JCwNZM0uIrgnfDJhFrPzXgWYufMevny3gL3cm+HO2IHetoXhb7i5mSammb7rvoJQlfRD ZeUaOs1Weo8Y4W8qGJbUzv5Ojwv7rLPYXLJTpn/EtxfBaYKia+SNHq3A6TGpd929EkeL BJ1Q== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hk18si1150718ejb.238.2021.10.07.17.07.59; Thu, 07 Oct 2021 17:08:23 -0700 (PDT) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240778AbhJHAGx (ORCPT + 99 others); Thu, 7 Oct 2021 20:06:53 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:13706 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232682AbhJHAGx (ORCPT ); Thu, 7 Oct 2021 20:06:53 -0400 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4HQT1C2hm6zWlV2; Fri, 8 Oct 2021 08:03:27 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Fri, 8 Oct 2021 08:04:50 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Fri, 8 Oct 2021 08:04:50 +0800 From: "Longpeng(Mike)" To: , , , CC: , , , "Longpeng(Mike)" Subject: [PATCH v3 2/2] iommu/vt-d: avoid duplicated removing in __domain_mapping Date: Fri, 8 Oct 2021 08:04:33 +0800 Message-ID: <20211008000433.1115-3-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20211008000433.1115-1-longpeng2@huawei.com> References: <20211008000433.1115-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org __domain_mapping() always removes the pages in the range from 'iov_pfn' to 'end_pfn', but the 'end_pfn' is always the last pfn of the range that the caller wants to map. This would introduce too many duplicated removing and leads the map operation take too long, for example: Map iova=0x100000,nr_pages=0x7d61800 iov_pfn: 0x100000, end_pfn: 0x7e617ff iov_pfn: 0x140000, end_pfn: 0x7e617ff iov_pfn: 0x180000, end_pfn: 0x7e617ff iov_pfn: 0x1c0000, end_pfn: 0x7e617ff iov_pfn: 0x200000, end_pfn: 0x7e617ff ... it takes about 50ms in total. We can reduce the cost by recalculate the 'end_pfn' and limit it to the boundary of the end of this pte page. Map iova=0x100000,nr_pages=0x7d61800 iov_pfn: 0x100000, end_pfn: 0x13ffff iov_pfn: 0x140000, end_pfn: 0x17ffff iov_pfn: 0x180000, end_pfn: 0x1bffff iov_pfn: 0x1c0000, end_pfn: 0x1fffff iov_pfn: 0x200000, end_pfn: 0x23ffff ... it only need 9ms now. Signed-off-by: Longpeng(Mike) --- drivers/iommu/intel/iommu.c | 11 ++++++----- include/linux/intel-iommu.h | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index d75f59a..46edae6 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -2354,12 +2354,17 @@ static void switch_to_super_page(struct dmar_domain *domain, return -ENOMEM; first_pte = pte; + lvl_pages = lvl_to_nr_pages(largepage_lvl); + /* It is large page*/ if (largepage_lvl > 1) { unsigned long end_pfn; + unsigned long pages_to_remove; pteval |= DMA_PTE_LARGE_PAGE; - end_pfn = ((iov_pfn + nr_pages) & level_mask(largepage_lvl)) - 1; + pages_to_remove = min_t(unsigned long, nr_pages, + nr_pte_to_next_page(pte) * lvl_pages); + end_pfn = iov_pfn + pages_to_remove - 1; switch_to_super_page(domain, iov_pfn, end_pfn, largepage_lvl); } else { pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE; @@ -2381,10 +2386,6 @@ static void switch_to_super_page(struct dmar_domain *domain, WARN_ON(1); } - lvl_pages = lvl_to_nr_pages(largepage_lvl); - - BUG_ON(nr_pages < lvl_pages); - nr_pages -= lvl_pages; iov_pfn += lvl_pages; phys_pfn += lvl_pages; diff --git a/include/linux/intel-iommu.h b/include/linux/intel-iommu.h index 9bcabc7..b29b2a3 100644 --- a/include/linux/intel-iommu.h +++ b/include/linux/intel-iommu.h @@ -713,6 +713,12 @@ static inline bool first_pte_in_page(struct dma_pte *pte) return IS_ALIGNED((unsigned long)pte, VTD_PAGE_SIZE); } +static inline int nr_pte_to_next_page(struct dma_pte *pte) +{ + return first_pte_in_page(pte) ? BIT_ULL(VTD_STRIDE_SHIFT) : + (struct dma_pte *)ALIGN((unsigned long)pte, VTD_PAGE_SIZE) - pte; +} + extern struct dmar_drhd_unit * dmar_find_matched_drhd_unit(struct pci_dev *dev); extern int dmar_find_matched_atsr_unit(struct pci_dev *dev); -- 1.8.3.1