Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp4696247pxb; Tue, 5 Oct 2021 08:27:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwIRoWbWkDPoZPISskgPSeKKJ5gAbPYruU49oTim6QurCPWJMDm6ENkbVdoXeomoe4cacsl X-Received: by 2002:aa7:d59a:: with SMTP id r26mr26553761edq.86.1633447678498; Tue, 05 Oct 2021 08:27:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633447678; cv=none; d=google.com; s=arc-20160816; b=pulEWw/bccS4OnbLTWe7QmThNoajrTRpJPBd5U/TGCyWveE+UnamU9mxUtJqMzilvX yHe7Ifz4O/Ci7jXPJvSSGoCL5kZ0guVfRCjAf19pnzrWEsNl2b+ernUK2xYCU3GDCWNo deC86MJDAjRKjKKMFOuJZ0pYlsTMyXsPKhSWEGipHg27i3jNI3NPcS7r2PrxXeJtRat1 R0RCg59NN3iRFn91+pueKxD7qYt3YP3RR7XpO4K57YLJfjQjvFeeH97F0ONSciyBmbU6 ElOrOULTWI443YPhkNPXkUu+CCA91ELntoGZy75idDdv0QQ+Krm239UX3eJS/109HmL8 tUzg== 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=B8krIkX//npJjPvWNvpQBSpQm72GB9q4LDjyjUEh6as=; b=bah9busm/EtruInH9gkqrqNp46eR6mHXUOZOF3ZMG1+q7u8j/Gl0Hf8+59qUn2KHVQ XnylS5aQeaCreV7zMVGr6g8zfUCI1dqs/lXzEbAwW97cgkC9CjDrqCu4i7g9Q+MjeQ/O XYUEVcCq5TDX5UsM1jHQ6mA+yNmAVU35NYzLATR6A5C672vYvaptCHs/hSng5E7YUkFn ASh0xtZrnLaDti1ttpjxODce4xrCnMWQPWxY/dl4tPLYCn1vRF1PKdeiNenozn5Gc+w7 gqBD+gBOPo27mZQdAiwwX9vSPkELQZOt+MVgfdAp6mU+7uUV0twfBsOv1jXmE5irob2p 4CZw== 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 bo1si21439935ejb.535.2021.10.05.08.27.34; Tue, 05 Oct 2021 08:27:58 -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 S235979AbhJEPZ0 (ORCPT + 99 others); Tue, 5 Oct 2021 11:25:26 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:24219 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235974AbhJEPZW (ORCPT ); Tue, 5 Oct 2021 11:25:22 -0400 Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.55]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4HP1Y11zrmzGp6H; Tue, 5 Oct 2021 23:22:29 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Tue, 5 Oct 2021 23:23:27 +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; Tue, 5 Oct 2021 23:23:27 +0800 From: "Longpeng(Mike)" To: , , , CC: , , , "Longpeng(Mike)" Subject: [PATCH v2 2/2] iommu/vt-d: avoid duplicated removing in __domain_mapping Date: Tue, 5 Oct 2021 23:23:08 +0800 Message-ID: <20211005152308.1061-3-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20211005152308.1061-1-longpeng2@huawei.com> References: <20211005152308.1061-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: dggems702-chm.china.huawei.com (10.3.19.179) 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 | 12 +++++++----- include/linux/intel-iommu.h | 6 ++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index d75f59a..87cbf34 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -2354,12 +2354,18 @@ static void switch_to_super_page(struct dmar_domain *domain, return -ENOMEM; first_pte = pte; + lvl_pages = lvl_to_nr_pages(largepage_lvl); + BUG_ON(nr_pages < lvl_pages); + /* 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 +2387,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 a590b00..623b407 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 !((unsigned long)pte & ~VTD_PAGE_MASK); } +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