From nobody Sun May 19 13:14:42 2024 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 011B376408 for ; Fri, 26 Apr 2024 03:43:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714103009; cv=none; b=AjI4GyEU6lmU27/nWBFi8qwuFY0xkmQGeXD4xdhJvCETZvvTOutaok8bNTnh0KfcZrTSXw8RzRekCzsnCLDFoEjsQVjhWsq6/w9rk+t+cDHoe8weSxN0W9JV7ZKb6DW8F9Z41MErQADmYQmBH7PomEUb2OewmGN9mf47JGQSrhU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714103009; c=relaxed/simple; bh=z7tn/unxQEXHXGcuLbspU3ZbRdF8xTwEfu49p+gXjFQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=otvxA3GFOfICYAxVc98XQK5Cc79M1S6RfJG4maUYdrGqwQKEUICelGrKvQjIOcm92dTl7RRXdZhY9XPeIxRB8GXvhukW0fOYoyh5Yj/QUcxjM53PMmQYN0enzVNrplWCMzbjEmAf2ZPz8o+pAKDrOPklT1HSf6C7PjuySIxdDAM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen-com.20230601.gappssmtp.com header.i=@soleen-com.20230601.gappssmtp.com header.b=x8ZVAN9I; arc=none smtp.client-ip=209.85.160.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen-com.20230601.gappssmtp.com header.i=@soleen-com.20230601.gappssmtp.com header.b="x8ZVAN9I" Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-439dfa27003so9718101cf.2 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=vger.kernel.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=x8ZVAN9IThHFHV6tn+M2tHvtcBTVwS+69PY5P1S9D2HpBXUPsiS5ZK0bzjnnKns+UZ CWYWpPU70PfnrudbalOfNUyaA1Pgp+tBTmq/MJDBHVFtlROMUGabBDGk9fBdqHHSL1eB ydcTD+JINkbxaD3ICmHAZX3EGyfo/cPCr7QudoOTymz017eeu9JJsnKLT0o+YXmZW6li Gj1AzjZPQ2BsSicR/Kns27+PezgOiQzTmejngD3g1cwgUupaBQ5mbEJpRKyyAvFQmv58 HHwM/bkytrDhaqv3TG4ED+79Fl3WKLkX3BQudUaQ/n4ArAjrq+oGGFg/CtJVA8SsTyal DHbg== 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=UhSSBL1PkB92pq18/2Ge66f3mINynLTZBULnOVTeUO+Y/5QzU/p7RpJbcb3EMqKE+L 2YQBiInXtCXFf8SCic87aaQ757jNMU1A1jZViJ8iroPSzWJKX1JNsCoManTkIA8NIXRX n+YTxJ/WpP08Wpmmjczw5UwDf5cHzOESrgRZsTvkekNNTSHqa1Het+fOdnAAtmusKVSf 54KpNmGgaxugYqS2DaJkmFbeVK3VuOd7iWsKDMyLMUo4g0IKRZ8PVFsAabHTNxSqGDFA T2ys7fTAp4HHqyLq94Agz4jCIvUKVR6bv0bU8Tjj9aVpsQfTbHjhPYprGIneCOckIaDK a4Aw== X-Forwarded-Encrypted: i=1; AJvYcCXOih9ubjopsiItN2af5rXEIsGq/x3cO/KRMLAj3Q/c8SuuPxt2JAS4x3o/gQ103dg+FnGNaWR0PJgJOANkY826klh7Qvlct2IWxlND X-Gm-Message-State: AOJu0Yy6n3p497vAkHkhXzYXOcbhb2AbqvlOFyNzb/jEGvQteOHeFLgU 8qRjmYzsKd+bqZ0/9JMypP7H34poiQoLo/qGMRs266p3P9DwfJKmq3RJBX6NcK7mnvVYDY8UJ7a L 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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_domai= n *domain, if (domain->use_first_level) pteval |=3D DMA_FL_PTE_XD | DMA_FL_PTE_US | DMA_FL_PTE_ACCESS; =20 - 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 *dom= ain, continue; } do { - dma_clear_pte(pte); + if (dma_pte_present(pte)) + dma_clear_pte(pte); start_pfn +=3D lvl_to_nr_pages(large_page); pte++; } while (start_pfn <=3D last_pfn && !first_pte_in_page(pte)); @@ -975,7 +976,8 @@ static void dma_pte_free_level(struct dmar_domain *doma= in, 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_doma= in *domain, } } =20 -/* 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 long= er + * 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_dom= ain *domain, struct page *pg; =20 pg =3D pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT); - list_add_tail(&pg->lru, freelist); - - if (level =3D=3D 1) - return; - pte =3D 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); } =20 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 =3D=3D 0 && last_pfn =3D=3D DOMAIN_MAX_PFN(domain->gaw)) { struct page *pgd_page =3D virt_to_page(domain->pgd); + page_mapcount_reset(pgd_page); list_add_tail(&pgd_page->lru, freelist); domain->pgd =3D 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 =3D cmpxchg64_local(&pte->val, 0ULL, pteval); + tmp =3D dma_set_pte(pte, pteval); if (tmp) { static int dumps =3D 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; }; =20 -static inline void dma_clear_pte(struct dma_pte *pte) -{ - pte->val =3D 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 } =20 +#define DMA_PTEVAL_PRESENT(pteval) (((pteval) & 3) !=3D 0) static inline bool dma_pte_present(struct dma_pte *pte) { - return (pte->val & 3) !=3D 0; + return DMA_PTEVAL_PRESENT(pte->val); +} + +static inline void dma_clear_pte(struct dma_pte *pte) +{ + u64 old_pteval; + + old_pteval =3D xchg(&pte->val, 0ULL); + if (DMA_PTEVAL_PRESENT(old_pteval)) { + struct page *pg =3D 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 =3D cmpxchg64(&pte->val, 0ULL, pteval); + if (old_pteval =3D=3D 0) { + struct page *pg =3D virt_to_page(pte); + + atomic_inc(&pg->_mapcount); + } + + return old_pteval; } =20 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 or= der) } =20 /** - * iommu_alloc_page_node - allocate a zeroed page at specific NUMA node. + * iommu_alloc_page_node - allocate a zeroed page at specific NUMA node, a= nd 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 =3D iommu_alloc_pages_node(nid, gfp, 0); + + if (virt) + atomic_set(&(virt_to_page(virt))->_mapcount, 0); + + return virt; } =20 /** - * iommu_alloc_page - allocate a zeroed page + * iommu_alloc_page - allocate a zeroed page, and set mapcount in its stru= ct + * 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 =3D iommu_alloc_pages(gfp, 0); + + if (virt) + atomic_set(&(virt_to_page(virt))->_mapcount, 0); + + return virt; } =20 /** @@ -155,16 +167,19 @@ static inline void iommu_free_pages(void *virt, int o= rder) } =20 /** - * 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); + } } =20 /** - * 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_hea= d *page) while (!list_empty(page)) { struct page *p =3D list_entry(page->prev, struct page, lru); =20 + page_mapcount_reset(p); list_del(&p->lru); __iommu_free_account(p, 0); put_page(p); --=20 2.44.0.769.g3c40516874-goog From nobody Sun May 19 13:14:42 2024 Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 988F178C66 for ; Fri, 26 Apr 2024 03:43:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714103010; cv=none; b=FU7rdTlN1L/L+VaQW+Bh0tVXp/YZazKbu0za1o6m25fhiunBdRQd7Zdq7d1PnXdAAZrkPnE1mmuizEra0acENYqiysBORcvfhSfgjzkQpQ09rvxnm2opun2CBZlTYjqdofgLlu3bL84ffIgU3s9Az91SVJj7XsIuy/0xOmzn9pk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714103010; c=relaxed/simple; bh=3z+GXuQGkWoDNcuvF/alZVLngGHITQXAlp4EnS0+nzc=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NxR8IVvDlU7RQGvr/ZD03h6is19dGwiMDNvkEBFLbh6PVzzRJvWuDpuBru/ej1+M62Rba5b2k9FXFEKp89Lecyxo1pLS6CgEpzubbHYZH3kOHFNsWsxftFlAxnoiLiwEg/uD8CLcAMZfT8x9aIgGDooSpqIbY878ieDyRUkcwCE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen-com.20230601.gappssmtp.com header.i=@soleen-com.20230601.gappssmtp.com header.b=D0E/MEca; arc=none smtp.client-ip=209.85.160.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen-com.20230601.gappssmtp.com header.i=@soleen-com.20230601.gappssmtp.com header.b="D0E/MEca" Received: by mail-qt1-f176.google.com with SMTP id d75a77b69052e-4365c1451d4so11449801cf.2 for ; Thu, 25 Apr 2024 20:43:28 -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=vger.kernel.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=GURCfQOZVuUrKhF3OYuy9zEmklzBEXgHfus1Ew76lR0=; b=D0E/MEcaVUca9EgN+Gu4xAHiryQIW2fSYwh/A62FAD8M5FXI1BM1qAid+vJrIqFHHK CBI+rUIxP8wCkecZcgQTUE2OGJRvubB584ssMV3k21Mz66vuc0wp/NohWvnwzYrYG7G7 jK9ojxZM2cGg7eu5Xr1Q2GUDcbWeG6DymM2m83XBfbjPunt7LYOU463wWHenzjQwk72a jRU8CAI+OJIzGgFqJ9uCJGpf7cwzgg8Y00JqsUMad7KMfpR/yAGQtMCkJjjVCDG7RE57 ed5g4qvvBl+t5tJkTbR61fGpl530bf50sQ+DtYgImfJXhtv56tIEdh+wSHcNz/0dfOWg wO8Q== 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=GURCfQOZVuUrKhF3OYuy9zEmklzBEXgHfus1Ew76lR0=; b=a53KuBMqVdq8/NPfglVlrV+cbeS7ZQJ2YayMSlaNM5Vx71cdmpsfmOZljX7bgsKFjm gQNNgpm4tNq3g4pV3nIpJW63fgI5xamo9W+8Bsh1X7yWDY339/Ki7du1MGsejBLVD9Mn 75j6SaowOSkN0+kM26ePxfTp/u4Hvwut748Rdi/krdfG4JcgWNumQY8X4sXYoMmI9cCC HOfeEE47GfmpnklSBgC2934ozpFlMdNS3qRTPaGT4XDKb61od0jzxqQMiQKGH3RerkJ0 4G4aZHVpKYo+tsjssHtpoljxafuANVdTe1Ko9JW+vC/ONLujJLONbDyUEpuEo/MlHPfQ HtuA== X-Forwarded-Encrypted: i=1; AJvYcCU0zcepMT1BB3bjzM/YzoZH2oGKyX9/3mywoIpB61M7QUqMKFd0q7On/1KPOOkiQ3U47Vd6Ps1tP61PoC0pMu7B0FA2p2TsGwa+xB41 X-Gm-Message-State: AOJu0YylFfhK4oT1KsDksHIi7cyq71rbJDPk50x+TeN7ZnKDpDS7whBy Fm/IPKDxatCXHi2FkohRTU8Olp+14ACG8C74O+FYNCoKnQoZUHkSiHusuR0fMoI= X-Google-Smtp-Source: AGHT+IHx2jRF9sHZe0ji37o0f9d/WxOmNqlXZKb8rptZFsjIiM8LnbaBigJJCVNFjwy+WUFUifGdEw== X-Received: by 2002:ac8:7f0e:0:b0:437:a1a2:f832 with SMTP id f14-20020ac87f0e000000b00437a1a2f832mr2149126qtk.11.1714103007410; Thu, 25 Apr 2024 20:43:27 -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.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 20:43:27 -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 2/3] iommu/intel: synchronize page table map and unmap operations Date: Fri, 26 Apr 2024 03:43:22 +0000 Message-ID: <20240426034323.417219-3-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Since, we are going to update parent page table entries when lower level page tables become emtpy and we add them to the free list. We need a way to synchronize the operation. Use domain->pgd_lock to protect all map and unmap operations. This is reader/writer lock. At the beginning everything is going to be read only mode, however, later, when free page table on unmap is added we will add a writer section as well. Signed-off-by: Pasha Tatashin --- drivers/iommu/intel/iommu.c | 21 +++++++++++++++++++-- drivers/iommu/intel/iommu.h | 3 +++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 1bfb6eccad05..8c7e596728b5 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -995,11 +995,13 @@ static void dma_pte_free_pagetable(struct dmar_domain= *domain, unsigned long last_pfn, int retain_level) { + read_lock(&domain->pgd_lock); dma_pte_clear_range(domain, start_pfn, last_pfn); =20 /* We don't need lock here; nobody else touches the iova range */ dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level, domain->pgd, 0, start_pfn, last_pfn); + read_unlock(&domain->pgd_lock); =20 /* free pgd */ if (start_pfn =3D=3D 0 && last_pfn =3D=3D DOMAIN_MAX_PFN(domain->gaw)) { @@ -1093,9 +1095,11 @@ static void domain_unmap(struct dmar_domain *domain,= unsigned long start_pfn, WARN_ON(start_pfn > last_pfn)) return; =20 + read_lock(&domain->pgd_lock); /* we don't need lock here; nobody else touches the iova range */ dma_pte_clear_level(domain, agaw_to_level(domain->agaw), domain->pgd, 0, start_pfn, last_pfn, freelist); + read_unlock(&domain->pgd_lock); =20 /* free pgd */ if (start_pfn =3D=3D 0 && last_pfn =3D=3D DOMAIN_MAX_PFN(domain->gaw)) { @@ -2088,6 +2092,7 @@ __domain_mapping(struct dmar_domain *domain, unsigned= long iov_pfn, =20 pteval =3D ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | attr; =20 + read_lock(&domain->pgd_lock); while (nr_pages > 0) { uint64_t tmp; =20 @@ -2097,8 +2102,10 @@ __domain_mapping(struct dmar_domain *domain, unsigne= d long iov_pfn, =20 pte =3D pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl, gfp); - if (!pte) + if (!pte) { + read_unlock(&domain->pgd_lock); return -ENOMEM; + } first_pte =3D pte; =20 lvl_pages =3D lvl_to_nr_pages(largepage_lvl); @@ -2158,6 +2165,7 @@ __domain_mapping(struct dmar_domain *domain, unsigned= long iov_pfn, pte =3D NULL; } } + read_unlock(&domain->pgd_lock); =20 return 0; } @@ -3829,6 +3837,7 @@ static int md_domain_init(struct dmar_domain *domain,= int guest_width) domain->pgd =3D iommu_alloc_page_node(domain->nid, GFP_ATOMIC); if (!domain->pgd) return -ENOMEM; + rwlock_init(&domain->pgd_lock); domain_flush_cache(domain, domain->pgd, PAGE_SIZE); return 0; } @@ -4074,11 +4083,15 @@ static size_t intel_iommu_unmap(struct iommu_domain= *domain, unsigned long start_pfn, last_pfn; int level =3D 0; =20 + read_lock(&dmar_domain->pgd_lock); /* Cope with horrid API which requires us to unmap more than the size argument if it happens to be a large-page mapping. */ if (unlikely(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, - &level, GFP_ATOMIC))) + &level, GFP_ATOMIC))) { + read_unlock(&dmar_domain->pgd_lock); return 0; + } + read_unlock(&dmar_domain->pgd_lock); =20 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level)) size =3D VTD_PAGE_SIZE << level_to_offset_bits(level); @@ -4145,8 +4158,10 @@ static phys_addr_t intel_iommu_iova_to_phys(struct i= ommu_domain *domain, int level =3D 0; u64 phys =3D 0; =20 + read_lock(&dmar_domain->pgd_lock); pte =3D pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level, GFP_ATOMIC); + read_unlock(&dmar_domain->pgd_lock); if (pte && dma_pte_present(pte)) phys =3D dma_pte_addr(pte) + (iova & (BIT_MASK(level_to_offset_bits(level) + @@ -4801,8 +4816,10 @@ static int intel_iommu_read_and_clear_dirty(struct i= ommu_domain *domain, struct dma_pte *pte; int lvl =3D 0; =20 + read_lock(&dmar_domain->pgd_lock); pte =3D pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &lvl, GFP_ATOMIC); + read_unlock(&dmar_domain->pgd_lock); pgsize =3D level_size(lvl) << VTD_PAGE_SHIFT; if (!pte || !dma_pte_present(pte)) { iova +=3D pgsize; diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index e5c1eb23897f..2f38b087ea4f 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -615,6 +615,9 @@ struct dmar_domain { struct { /* virtual address */ struct dma_pte *pgd; + + /* Synchronizes pgd map/unmap operations */ + rwlock_t pgd_lock; /* max guest address width */ int gaw; /* --=20 2.44.0.769.g3c40516874-goog From nobody Sun May 19 13:14:42 2024 Received: from mail-qt1-f171.google.com (mail-qt1-f171.google.com [209.85.160.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56C1078C71 for ; Fri, 26 Apr 2024 03:43:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714103011; cv=none; b=B8eGO1dbtijPnXMJK56QNpmMMLn8S4Xsz94ci1T+OB0W7yCqQcy2j3GeGk1Rob1BBzynx74F8F93j9fDGkyB/FNeKB6vW6C5aAFZIHJZNHTfcm6VTj/wm8t+v9uCuxkjZOu3poV9pEHnS9xu8v2cy2o0vs6PC7bpvZwsz9N9OUc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714103011; c=relaxed/simple; bh=O9Gh1/Lqh0Xkd27Kwz4THqDapXdPbT/TrEKteKU3Atg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dIpK0iSN4HjiTq79Rnq5PXgcUkvLwzvaqne9x1xtwOlJEWGKKDqkti1e9In6xmO1NxEFUFMVbvlm//hHs276dm8V3sFM0ajKqVNZXU9yldMH2NCMmjJg4MN9er7KAEKKL34m0W4LStUmGc+sjqWEiazBYMVKGC3cCKmysc2xIRE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen-com.20230601.gappssmtp.com header.i=@soleen-com.20230601.gappssmtp.com header.b=HcycTAuf; arc=none smtp.client-ip=209.85.160.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen-com.20230601.gappssmtp.com header.i=@soleen-com.20230601.gappssmtp.com header.b="HcycTAuf" Received: by mail-qt1-f171.google.com with SMTP id d75a77b69052e-436ee76c3b8so13107501cf.1 for ; Thu, 25 Apr 2024 20:43:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen-com.20230601.gappssmtp.com; s=20230601; t=1714103008; x=1714707808; darn=vger.kernel.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=0qKgoZHcRwjMzBnVukZSI3GExhXHJMz1htG24w59mTg=; b=HcycTAufqC+ahmJzKQkH8oAyJPT7cl+H1XyxaPpy9lkdZ3u6/vCmHSH82G7FxhWZtL MbaeIGM98SOAnr1655scxiqusL5GhO7w6L+5eXba3znWFX+R/wQIurNEOW+q1uiZbtGa /C8XLKASFhT2hWMREl13jxyrEyBKvW6++iKQMwf5XrkzAzbnEgXGOGZ02LVRRTmFB2H6 Qa9B70YHfRicXID+eOEWaRA7LT5Pv1i5hLvK8tB5zVK47lrfu9b5tRQoOkMukSIpzyuG Wdj1jzUb5CDUZqZmmAtdmJvo0GURPRWTDCCFBR2wIZnukke6jbzdXFCJ9hViqAvenBtr tmFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714103008; x=1714707808; 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=0qKgoZHcRwjMzBnVukZSI3GExhXHJMz1htG24w59mTg=; b=iNLtbioYttuUifCS92qYhH9E4GJ7y129ZJLifjd9A0rmSIHP5AeOHiDAP8IYbxfw9H bswEJlBdSeBi3yd0F0em8N3ChJdj1mjbrgsSpzh0AnrSIqv9LXG4UDT6mXzv5FTVArnD EM287E1JHRK6shhIPlxJihR+Gvs6/H7OroJvoaSEQEF5erKoSzwxlXDZl62nAf4cOhWT wpKs8Db5dYRWbWdcsttXAdjU3VSnFEhNdD9d8aErw6pwovFxDKpaziQ4ojSUzgwCfVpr rfasJScEFUjZ/3GXh+sg+XNeh2Zfx7MfztV5fQMYC1l8rSwerjYoWsxbApwMdlXcJIIS fUYw== X-Forwarded-Encrypted: i=1; AJvYcCVWf8SX6SlePFxixBw1kuPi8jfJupbETWYNOOO80mrtqZnx/VgYv1ReLW+0pNn1AwkB5+0iNNepFvhpM1aKCwOIx0zM1GgZ/G9pAnWa X-Gm-Message-State: AOJu0Yz6vDl6CdWX+71I7RDILZ/vFhIm2tQnlMI02ZWV6IW4L+eLhoSB xKziANKyQNwaFFnPHFa6HgqIRUWPg4o/O8u9W0psDB0cjXHfTRuWQB/hRzDZl0M= X-Google-Smtp-Source: AGHT+IHmUgwf6vDm3AhjFFV4a78DkNgnzXWpFjgQ5FiiDRbS0nCVhPfbCsQnbjR/SyVu2r2tQ6OZiw== X-Received: by 2002:ac8:7e81:0:b0:43a:7bc9:7c2f with SMTP id w1-20020ac87e81000000b0043a7bc97c2fmr325043qtj.2.1714103008254; Thu, 25 Apr 2024 20:43:28 -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.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 20:43:27 -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 3/3] iommu/intel: free empty page tables on unmaps Date: Fri, 26 Apr 2024 03:43:23 +0000 Message-ID: <20240426034323.417219-4-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When page tables become empty, add them to the freelist so that they can also be freed. This is means that a page tables that are outside of the imediat iova range might be freed as well, therefore, only in the case where such page tables are going to be freed, we take the writer lock. Signed-off-by: Pasha Tatashin --- drivers/iommu/intel/iommu.c | 91 +++++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 14 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 8c7e596728b5..2dedcd4f6060 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1044,7 +1044,7 @@ static void dma_pte_list_pagetables(struct dmar_domai= n *domain, static void dma_pte_clear_level(struct dmar_domain *domain, int level, struct dma_pte *pte, unsigned long pfn, unsigned long start_pfn, unsigned long last_pfn, - struct list_head *freelist) + struct list_head *freelist, int *freed_level) { struct dma_pte *first_pte =3D NULL, *last_pte =3D NULL; =20 @@ -1070,11 +1070,47 @@ static void dma_pte_clear_level(struct dmar_domain = *domain, int level, first_pte =3D pte; last_pte =3D pte; } else if (level > 1) { + struct dma_pte *npte =3D phys_to_virt(dma_pte_addr(pte)); + struct page *npage =3D virt_to_page(npte); + /* Recurse down into a level that isn't *entirely* obsolete */ - dma_pte_clear_level(domain, level - 1, - phys_to_virt(dma_pte_addr(pte)), + dma_pte_clear_level(domain, level - 1, npte, level_pfn, start_pfn, last_pfn, - freelist); + freelist, freed_level); + + /* + * Free next level page table if it became empty. + * + * We only holding the reader lock, and it is possible + * that other threads are accessing page table as + * readers as well. We can only free page table that + * is outside of the request IOVA space only if + * we grab the writer lock. Since we need to drop reader + * lock, we are incrementing the mapcount in the npage + * so it (and the current page table) does not + * dissappear due to concurrent unmapping threads. + * + * Store the size maximum size of the freed page table + * into freed_level, so the size of the IOTLB flush + * can be determined. + */ + if (freed_level && !atomic_read(&npage->_mapcount)) { + atomic_inc(&npage->_mapcount); + read_unlock(&domain->pgd_lock); + write_lock(&domain->pgd_lock); + atomic_dec(&npage->_mapcount); + if (!atomic_read(&npage->_mapcount)) { + dma_clear_pte(pte); + if (!first_pte) + first_pte =3D pte; + last_pte =3D pte; + page_mapcount_reset(npage); + list_add_tail(&npage->lru, freelist); + *freed_level =3D level; + } + write_unlock(&domain->pgd_lock); + read_lock(&domain->pgd_lock); + } } next: pfn =3D level_pfn + level_size(level); @@ -1089,7 +1125,8 @@ static void dma_pte_clear_level(struct dmar_domain *d= omain, int level, the page tables, and may have cached the intermediate levels. The pages can only be freed after the IOTLB flush has been done. */ static void domain_unmap(struct dmar_domain *domain, unsigned long start_p= fn, - unsigned long last_pfn, struct list_head *freelist) + unsigned long last_pfn, struct list_head *freelist, + int *level) { if (WARN_ON(!domain_pfn_supported(domain, last_pfn)) || WARN_ON(start_pfn > last_pfn)) @@ -1098,7 +1135,8 @@ static void domain_unmap(struct dmar_domain *domain, = unsigned long start_pfn, read_lock(&domain->pgd_lock); /* we don't need lock here; nobody else touches the iova range */ dma_pte_clear_level(domain, agaw_to_level(domain->agaw), - domain->pgd, 0, start_pfn, last_pfn, freelist); + domain->pgd, 0, start_pfn, last_pfn, freelist, + level); read_unlock(&domain->pgd_lock); =20 /* free pgd */ @@ -1479,11 +1517,11 @@ static void __iommu_flush_iotlb_psi(struct intel_io= mmu *iommu, u16 did, =20 static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, struct dmar_domain *domain, - unsigned long pfn, unsigned int pages, + unsigned long pfn, unsigned long pages, int ih, int map) { - unsigned int aligned_pages =3D __roundup_pow_of_two(pages); - unsigned int mask =3D ilog2(aligned_pages); + unsigned long aligned_pages =3D __roundup_pow_of_two(pages); + unsigned long mask =3D ilog2(aligned_pages); uint64_t addr =3D (uint64_t)pfn << VTD_PAGE_SHIFT; u16 did =3D domain_id_iommu(domain, iommu); =20 @@ -1837,7 +1875,8 @@ static void domain_exit(struct dmar_domain *domain) if (domain->pgd) { LIST_HEAD(freelist); =20 - domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw), &freelist); + domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw), &freelist, + NULL); iommu_put_pages_list(&freelist); } =20 @@ -3419,7 +3458,8 @@ static int intel_iommu_memory_notifier(struct notifie= r_block *nb, struct intel_iommu *iommu; LIST_HEAD(freelist); =20 - domain_unmap(si_domain, start_vpfn, last_vpfn, &freelist); + domain_unmap(si_domain, start_vpfn, last_vpfn, + &freelist, NULL); =20 rcu_read_lock(); for_each_active_iommu(iommu, drhd) @@ -4080,6 +4120,7 @@ static size_t intel_iommu_unmap(struct iommu_domain *= domain, struct iommu_iotlb_gather *gather) { struct dmar_domain *dmar_domain =3D to_dmar_domain(domain); + bool queued =3D iommu_iotlb_gather_queued(gather); unsigned long start_pfn, last_pfn; int level =3D 0; =20 @@ -4099,7 +4140,16 @@ static size_t intel_iommu_unmap(struct iommu_domain = *domain, start_pfn =3D iova >> VTD_PAGE_SHIFT; last_pfn =3D (iova + size - 1) >> VTD_PAGE_SHIFT; =20 - domain_unmap(dmar_domain, start_pfn, last_pfn, &gather->freelist); + /* + * pass level only if !queued, which means we will do iotlb + * flush callback before freeing pages from freelist. + * + * When level is passed domain_unamp will attempt to add empty + * page tables to freelist, and pass the level number of the highest + * page table that was added to the freelist. + */ + domain_unmap(dmar_domain, start_pfn, last_pfn, &gather->freelist, + queued ? NULL : &level); =20 if (dmar_domain->max_addr =3D=3D iova + size) dmar_domain->max_addr =3D iova; @@ -4108,8 +4158,21 @@ static size_t intel_iommu_unmap(struct iommu_domain = *domain, * We do not use page-selective IOTLB invalidation in flush queue, * so there is no need to track page and sync iotlb. */ - if (!iommu_iotlb_gather_queued(gather)) - iommu_iotlb_gather_add_page(domain, gather, iova, size); + if (!queued) { + size_t sz =3D size; + + /* + * Increase iova and sz for flushing if level was returned, + * as it means we also are freeing some page tables. + */ + if (level) { + unsigned long pgsize =3D level_size(level) << VTD_PAGE_SHIFT; + + iova =3D ALIGN_DOWN(iova, pgsize); + sz =3D ALIGN(size, pgsize); + } + iommu_iotlb_gather_add_page(domain, gather, iova, sz); + } =20 return size; } --=20 2.44.0.769.g3c40516874-goog