From patchwork Fri Apr 26 03:43:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 13644104 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A0A8C19F4E for ; Fri, 26 Apr 2024 03:43:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 223A76B0096; Thu, 25 Apr 2024 23:43:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D2C16B0098; Thu, 25 Apr 2024 23:43:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0739D6B0099; Thu, 25 Apr 2024 23:43:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id DC61A6B0096 for ; Thu, 25 Apr 2024 23:43:29 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 85BF016069F for ; Fri, 26 Apr 2024 03:43:29 +0000 (UTC) X-FDA: 82050288138.24.2A22C07 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by imf28.hostedemail.com (Postfix) with ESMTP id C8B92C0015 for ; Fri, 26 Apr 2024 03:43:27 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=soleen-com.20230601.gappssmtp.com header.s=20230601 header.b=zOafirMd; spf=pass (imf28.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.160.182 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com; dmarc=pass (policy=none) header.from=soleen.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714103007; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9nuts9l1zCDLqoFks7w3ko3FmsmK+1Col1dS1KLKxxU=; b=7qFBm1qww0vKfeAAEr389ux1tNhVPmlsdefs+afLL3VI66lStvbDRPAQ5fGxlV7ZF5pSUS nqQYAs1xbdJsVYTx9vMxlhFjunToqS9tr1JXo7syFdaNbB20Twg7RRezbe/n93cTtYl4Mw 3/IZ/+uTBAKLuNnJr8Z4TBW/oDZCAf4= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=soleen-com.20230601.gappssmtp.com header.s=20230601 header.b=zOafirMd; spf=pass (imf28.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.160.182 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com; dmarc=pass (policy=none) header.from=soleen.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714103007; a=rsa-sha256; cv=none; b=0eyN8WiZmFclYqJvFgovX/vPSw1PjdnlSQrL0A1qlei9R4JvS9Lc+PMY01PXcyXOS+nb1Q ALOP6rcornu+NVqiBses0Z6TUaCVFMTjN/hVjZ/JWP2DTOuzJ9rqKTxuKGVTjf3TeCXYl2 g1UyYgDDC1vaV5Q+VgWE4P9R3X8F7Qw= Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-439656c1b3eso10364941cf.1 for ; Thu, 25 Apr 2024 20:43:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen-com.20230601.gappssmtp.com; s=20230601; t=1714103007; x=1714707807; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=9nuts9l1zCDLqoFks7w3ko3FmsmK+1Col1dS1KLKxxU=; b=zOafirMdLJMYJ5rvqOzkixsKZ+XXfyivRzYZBRhf+yY8ltAVYXule+RaSl3Q5yUx1S VuwHAczX5XtuRe+yX0RJY9UNqKFAnu9cOANM94fVktF8dzflEEQ+3VOpkd7j56BUDbBO 8SqX593T5YsHQwyMaABbGDpio1aQiNvphtKnePTJ0al0hRcR+ltAZT1YYoS+fCbPPEgo vpZ6x69xbAAE3aVEjRnT9Q44kUr20bPPkLssQM5EcnLt5L55vVvx5pKM27nsHP16F/p/ MlnSvVLzpmu1ZcOS1Exuh8SzgleSuA/J8+rQRswJHhiTErEApr5dBSFS3fUwUTmAJgnM 8oeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714103007; x=1714707807; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9nuts9l1zCDLqoFks7w3ko3FmsmK+1Col1dS1KLKxxU=; b=Ec9uyphWiwaxpl6U3WTsH1R/8nVrthr3KKZBl2zWmV9q7Ly4eWsv4CJ/1c6GQVnpWk IFVJR6QzTXZhczklCpgp+NVlRDBF9LxWe5JyF4LO9RiPxAJuoTZvuM6l5uiUkeeHYRO7 FJ6sH0nt4GadXhdf3jP2qCkgqJoa0/nPpbt+LgScGd7ShAKer4skC9ve2ghOlerxgO8K t6H2RBfj0BHyCnJUGbtcbg3c4QMCPqj5SXnVawsj5nUBvpaNAzX6fBufdsJZR83OLSCa lxre+spwfl4GroBoTDgvsUm3XD5mz/zhSdOI/X+aADpJOLeoU65fpWsSWBrnCxkVC1T2 UlVg== X-Forwarded-Encrypted: i=1; AJvYcCVukhetj486WoLklnLG/kdJ64MnKJI9UNoVhVUWsxZOdWO90qmU6mP//h38zy7O+24DCNsNnEN3jUd9lkU8pYb8eCw= X-Gm-Message-State: AOJu0YwL26a7Tu0Ic/Pw8MwTctF2BONfPHz/c6dKdJjdPJc/1T1kzcpg SjKQyChRcQpSAW0/ZZY1kJbBqUeJiRrLFpFelr01WcB9c5mXRW8DCLTwKuyxZZ8= X-Google-Smtp-Source: AGHT+IE86KR+B7xfnZmKKOIt5yjOU6xWcIyovaxdsmvq0Zq76unVSgS9+jQ02n8Qi9Da0wnUCx3NpQ== X-Received: by 2002:ac8:5981:0:b0:439:bb89:1fe1 with SMTP id e1-20020ac85981000000b00439bb891fe1mr1607609qte.4.1714103006889; Thu, 25 Apr 2024 20:43:26 -0700 (PDT) Received: from soleen.c.googlers.com.com (129.177.85.34.bc.googleusercontent.com. [34.85.177.129]) by smtp.gmail.com with ESMTPSA id c6-20020ac80546000000b00436bb57faddsm7540815qth.25.2024.04.25.20.43.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 20:43:26 -0700 (PDT) From: Pasha Tatashin To: akpm@linux-foundation.org, linux-mm@kvack.org, pasha.tatashin@soleen.com, linux-kernel@vger.kernel.org, rientjes@google.com, dwmw2@infradead.org, baolu.lu@linux.intel.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, iommu@lists.linux.dev Subject: [RFC v2 1/3] iommu/intel: Use page->_mapcount to count number of entries in IOMMU Date: Fri, 26 Apr 2024 03:43:21 +0000 Message-ID: <20240426034323.417219-2-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog In-Reply-To: <20240426034323.417219-1-pasha.tatashin@soleen.com> References: <20240426034323.417219-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 X-Stat-Signature: hyqfpxtao79n5k4z9apr7jcj6frkh4x9 X-Rspamd-Queue-Id: C8B92C0015 X-Rspamd-Server: rspam10 X-Rspam-User: X-HE-Tag: 1714103007-19894 X-HE-Meta: U2FsdGVkX19OK004JqdclgXT6lmxypdEI2y5oOq2lc3PHDCaRBgnhlUSvjflMTcnvE/nT+nvqVzcwWESUrVrpNOvsZ+FFtMGUJlVLEVHui2z5pGX1G1pzg0iHZpEPfNe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Uh4seQ9qqyV9x5Fuo2DdZb7sRIJBMRiqtPbF7GoFLoa/1P1ez+g7QWXBaaDzdCn06KoNnCsIx0hDsvgMrPRpce8toT6cSOH2SQz558Db57+xxuKr2qSRHpDOhlaB8dS3X2BQ4wmLzDMggPl0tCX50pUu6K+sKf+Q9BzfmuqmzhtvxMg3NxceUY9x+/ucbKaCC3vfWDY10JtT8Eb8hD7DGR0EvU/NBPjoOwIxW6q9eSI9jecwSFHTm1rh4dK6O1AWF/zg3bOpW/y8rU+QiUJFwU/qfhAFrF77KEwECpTa0yOUajWEh4qJWt X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In order to be able to efficiently free empty page table levels, count the number of entries in each page table by incremeanting and decremeanting mapcount every time a PTE is inserted or removed form the page table. For this to work correctly, add two helper function: dma_clear_pte and dma_set_pte where counting is performed, Also, modify the code so every page table entry is always updated using the two new functions. Finally, before pages are freed, we must restore mapcount to its original state by calling page_mapcount_reset(). Signed-off-by: Pasha Tatashin --- drivers/iommu/intel/iommu.c | 42 ++++++++++++++++++++++--------------- drivers/iommu/intel/iommu.h | 39 ++++++++++++++++++++++++++++------ drivers/iommu/iommu-pages.h | 30 +++++++++++++++++++------- 3 files changed, 81 insertions(+), 30 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 7abe76f92a3c..1bfb6eccad05 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -862,7 +862,7 @@ static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain, if (domain->use_first_level) pteval |= DMA_FL_PTE_XD | DMA_FL_PTE_US | DMA_FL_PTE_ACCESS; - if (cmpxchg64(&pte->val, 0ULL, pteval)) + if (dma_set_pte(pte, pteval)) /* Someone else set it while we were thinking; use theirs. */ iommu_free_page(tmp_page); else @@ -934,7 +934,8 @@ static void dma_pte_clear_range(struct dmar_domain *domain, continue; } do { - dma_clear_pte(pte); + if (dma_pte_present(pte)) + dma_clear_pte(pte); start_pfn += lvl_to_nr_pages(large_page); pte++; } while (start_pfn <= last_pfn && !first_pte_in_page(pte)); @@ -975,7 +976,8 @@ static void dma_pte_free_level(struct dmar_domain *domain, int level, */ if (level < retain_level && !(start_pfn > level_pfn || last_pfn < level_pfn + level_size(level) - 1)) { - dma_clear_pte(pte); + if (dma_pte_present(pte)) + dma_clear_pte(pte); domain_flush_cache(domain, pte, sizeof(*pte)); iommu_free_page(level_pte); } @@ -1006,12 +1008,13 @@ static void dma_pte_free_pagetable(struct dmar_domain *domain, } } -/* When a page at a given level is being unlinked from its parent, we don't - need to *modify* it at all. All we need to do is make a list of all the - pages which can be freed just as soon as we've flushed the IOTLB and we - know the hardware page-walk will no longer touch them. - The 'pte' argument is the *parent* PTE, pointing to the page that is to - be freed. */ +/* + * A given page at a given level is being unlinked from its parent. + * We need to make a list of all the pages which can be freed just as soon as + * we've flushed the IOTLB and we know the hardware page-walk will no longer + * touch them. The 'pte' argument is the *parent* PTE, pointing to the page + * that is to be freed. + */ static void dma_pte_list_pagetables(struct dmar_domain *domain, int level, struct dma_pte *pte, struct list_head *freelist) @@ -1019,17 +1022,21 @@ static void dma_pte_list_pagetables(struct dmar_domain *domain, struct page *pg; pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT); - list_add_tail(&pg->lru, freelist); - - if (level == 1) - return; - pte = page_address(pg); + do { - if (dma_pte_present(pte) && !dma_pte_superpage(pte)) - dma_pte_list_pagetables(domain, level - 1, pte, freelist); + if (dma_pte_present(pte)) { + if (level > 1 && !dma_pte_superpage(pte)) { + dma_pte_list_pagetables(domain, level - 1, pte, + freelist); + } + dma_clear_pte(pte); + } pte++; } while (!first_pte_in_page(pte)); + + page_mapcount_reset(pg); + list_add_tail(&pg->lru, freelist); } static void dma_pte_clear_level(struct dmar_domain *domain, int level, @@ -1093,6 +1100,7 @@ static void domain_unmap(struct dmar_domain *domain, unsigned long start_pfn, /* free pgd */ if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { struct page *pgd_page = virt_to_page(domain->pgd); + page_mapcount_reset(pgd_page); list_add_tail(&pgd_page->lru, freelist); domain->pgd = NULL; } @@ -2113,7 +2121,7 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, /* We don't need lock here, nobody else * touches the iova range */ - tmp = cmpxchg64_local(&pte->val, 0ULL, pteval); + tmp = dma_set_pte(pte, pteval); if (tmp) { static int dumps = 5; pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n", diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 8d081d8c6f41..e5c1eb23897f 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -814,11 +814,6 @@ struct dma_pte { u64 val; }; -static inline void dma_clear_pte(struct dma_pte *pte) -{ - pte->val = 0; -} - static inline u64 dma_pte_addr(struct dma_pte *pte) { #ifdef CONFIG_64BIT @@ -830,9 +825,41 @@ static inline u64 dma_pte_addr(struct dma_pte *pte) #endif } +#define DMA_PTEVAL_PRESENT(pteval) (((pteval) & 3) != 0) static inline bool dma_pte_present(struct dma_pte *pte) { - return (pte->val & 3) != 0; + return DMA_PTEVAL_PRESENT(pte->val); +} + +static inline void dma_clear_pte(struct dma_pte *pte) +{ + u64 old_pteval; + + old_pteval = xchg(&pte->val, 0ULL); + if (DMA_PTEVAL_PRESENT(old_pteval)) { + struct page *pg = virt_to_page(pte); + + atomic_dec(&pg->_mapcount); + } else { + /* Ensure that we cleared a valid entry from the page table */ + WARN_ON_ONCE(1); + } +} + +static inline u64 dma_set_pte(struct dma_pte *pte, u64 pteval) +{ + u64 old_pteval; + + /* Ensure we about to set a valid entry to the page table */ + WARN_ON_ONCE(!DMA_PTEVAL_PRESENT(pteval)); + old_pteval = cmpxchg64(&pte->val, 0ULL, pteval); + if (old_pteval == 0) { + struct page *pg = virt_to_page(pte); + + atomic_inc(&pg->_mapcount); + } + + return old_pteval; } static inline bool dma_sl_pte_test_and_clear_dirty(struct dma_pte *pte, diff --git a/drivers/iommu/iommu-pages.h b/drivers/iommu/iommu-pages.h index 82ebf0033081..b8b332951944 100644 --- a/drivers/iommu/iommu-pages.h +++ b/drivers/iommu/iommu-pages.h @@ -119,7 +119,8 @@ static inline void *iommu_alloc_pages(gfp_t gfp, int order) } /** - * iommu_alloc_page_node - allocate a zeroed page at specific NUMA node. + * iommu_alloc_page_node - allocate a zeroed page at specific NUMA node, and set + * mapcount in its struct page to 0. * @nid: memory NUMA node id * @gfp: buddy allocator flags * @@ -127,18 +128,29 @@ static inline void *iommu_alloc_pages(gfp_t gfp, int order) */ static inline void *iommu_alloc_page_node(int nid, gfp_t gfp) { - return iommu_alloc_pages_node(nid, gfp, 0); + void *virt = iommu_alloc_pages_node(nid, gfp, 0); + + if (virt) + atomic_set(&(virt_to_page(virt))->_mapcount, 0); + + return virt; } /** - * iommu_alloc_page - allocate a zeroed page + * iommu_alloc_page - allocate a zeroed page, and set mapcount in its struct + * page to 0. * @gfp: buddy allocator flags * * returns the virtual address of the allocated page */ static inline void *iommu_alloc_page(gfp_t gfp) { - return iommu_alloc_pages(gfp, 0); + void *virt = iommu_alloc_pages(gfp, 0); + + if (virt) + atomic_set(&(virt_to_page(virt))->_mapcount, 0); + + return virt; } /** @@ -155,16 +167,19 @@ static inline void iommu_free_pages(void *virt, int order) } /** - * iommu_free_page - free page + * iommu_free_page - free page, and reset mapcount * @virt: virtual address of the page to be freed. */ static inline void iommu_free_page(void *virt) { - iommu_free_pages(virt, 0); + if (virt) { + page_mapcount_reset(virt_to_page(virt)); + iommu_free_pages(virt, 0); + } } /** - * iommu_put_pages_list - free a list of pages. + * iommu_put_pages_list - free a list of pages, and reset mapcount. * @page: the head of the lru list to be freed. * * There are no locking requirement for these pages, as they are going to be @@ -177,6 +192,7 @@ static inline void iommu_put_pages_list(struct list_head *page) while (!list_empty(page)) { struct page *p = list_entry(page->prev, struct page, lru); + page_mapcount_reset(p); list_del(&p->lru); __iommu_free_account(p, 0); put_page(p);