Skip to contentSkip to conversation

Allocate IOMMU page table using numa locality info

PATCH In review Vasant Hegde 2 participants 6 messages Mar 10, 2023 – Mar 13, 2023

Subsystems (2)
lore
Share & tools
lore

Vasant Hegde

author maintainer
#0 lore raw

v3 cover Allocate IOMMU page table using numa locality info

By default page allocator uses running CPU info to allocate page table. If it allocates page table in remote memory then every IOMMU access to page table will have overhead of reading remote page.

This patchset uses IOMMU NUMA node info and tries to allocate memory from same NUMA node.

Vasant Hegde (3):
iommu/amd: Add node ID to domain
iommu/amd: Allocate page table using numa locality info iommu/amd: Allocate IOMMU irqs using numa locality info

 drivers/iommu/amd/amd_iommu.h       |  8 ++++++++
 drivers/iommu/amd/amd_iommu_types.h |  1 +
 drivers/iommu/amd/init.c            |  3 ++-
 drivers/iommu/amd/io_pgtable.c      |  4 ++--
 drivers/iommu/amd/io_pgtable_v2.c   | 16 ++++++----------
 drivers/iommu/amd/iommu.c           |  6 ++++++
 6 files changed, 25 insertions(+), 13 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH v3 1/3 iommu/amd: Add node ID to domain

Add node id to 'struct protection_domain'. It will be used while allocating page table.

---
 drivers/iommu/amd/amd_iommu_types.h | 1 +
 drivers/iommu/amd/iommu.c           | 6 ++++++
 2 files changed, 7 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +1 -0
index 3d684190b4d5..e5cfdeaaad48 100644
@@ -549,6 +549,7 @@ struct protection_domain {
 	spinlock_t lock;	/* mostly used to lock the page table*/
 	u16 id;			/* the domain id written to the device table */
 	int glx;		/* Number of levels for GCR3 table */
+	int nid;		/* Node ID */
 	u64 *gcr3_tbl;		/* Guest CR3 table */
 	unsigned long flags;	/* flags to find out type of domain */
 	unsigned dev_cnt;	/* devices assigned to this domain */
drivers/iommu/amd/iommu.c +6 -0
index 5a505ba5467e..5452d0dd6569 100644
@@ -1662,6 +1662,10 @@ static void do_attach(struct iommu_dev_data *dev_data,
 	dev_data->domain = domain;
 	list_add(&dev_data->list, &domain->dev_list);
 
+	/* Update NUMA Node ID */
+	if (domain->nid == NUMA_NO_NODE)
+		domain->nid = dev_to_node(dev_data->dev);
+
 	/* Do reference counting */
 	domain->dev_iommu[iommu->index] += 1;
 	domain->dev_cnt                 += 1;
@@ -2097,6 +2101,8 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
 	if (type == IOMMU_DOMAIN_IDENTITY)
 		return domain;
 
+	domain->nid = NUMA_NO_NODE;
+
 	pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);
 	if (!pgtbl_ops) {
 		domain_id_free(domain->id);
Signature · 1 line
2.31.1
author maintainer
#2 lore raw

PATCH v3 2/3 iommu/amd: Allocate page table using numa locality info

In reply to Vasant Hegde

Allocate page table pages using IOMMU numa locality info. This optimizes page table walk by IOMMU.

---
 drivers/iommu/amd/amd_iommu.h     |  8 ++++++++
 drivers/iommu/amd/io_pgtable.c    |  4 ++--
 drivers/iommu/amd/io_pgtable_v2.c | 16 ++++++----------
 3 files changed, 16 insertions(+), 12 deletions(-)
drivers/iommu/amd/amd_iommu.h +8 -0
index c160a332ce33..916e888a941a 100644
@@ -122,6 +122,14 @@ static inline int get_pci_sbdf_id(struct pci_dev *pdev)
 	return PCI_SEG_DEVID_TO_SBDF(seg, devid);
 }
 
+static inline void *alloc_pgtable_page(int nid, gfp_t gfp)
+{
+	struct page *page;
+
+	page = alloc_pages_node(nid, gfp | GFP_ATOMIC | __GFP_ZERO, 0);
+	return page ? page_address(page) : NULL;
+}
+
 extern bool translation_pre_enabled(struct amd_iommu *iommu);
 extern bool amd_iommu_is_attach_deferred(struct device *dev);
 extern int __init add_special_device(u8 type, u8 id, u32 *devid,
drivers/iommu/amd/io_pgtable.c +2 -2
index ace0e9b8b913..1b67116882be 100644
@@ -156,7 +156,7 @@ static bool increase_address_space(struct protection_domain *domain,
 	bool ret = true;
 	u64 *pte;
 
-	pte = (void *)get_zeroed_page(gfp);
+	pte = alloc_pgtable_page(domain->nid, gfp);
 	if (!pte)
 		return false;
 
@@ -250,7 +250,7 @@ static u64 *alloc_pte(struct protection_domain *domain,
 
 		if (!IOMMU_PTE_PRESENT(__pte) ||
 		    pte_level == PAGE_MODE_NONE) {
-			page = (u64 *)get_zeroed_page(gfp);
+			page = alloc_pgtable_page(domain->nid, gfp);
 
 			if (!page)
 				return NULL;
drivers/iommu/amd/io_pgtable_v2.c +6 -10
index 8638ddf6fb3b..9f8b423f7630 100644
@@ -46,11 +46,6 @@ static inline bool is_large_pte(u64 pte)
 	return (pte & IOMMU_PAGE_PSE);
 }
 
-static inline void *alloc_pgtable_page(void)
-{
-	return (void *)get_zeroed_page(GFP_KERNEL);
-}
-
 static inline u64 set_pgtable_attr(u64 *page)
 {
 	u64 prot;
@@ -138,8 +133,8 @@ static void free_pgtable(u64 *pt, int level)
 }
 
 /* Allocate page table */
-static u64 *v2_alloc_pte(u64 *pgd, unsigned long iova,
-			 unsigned long pg_size, bool *updated)
+static u64 *v2_alloc_pte(int nid, u64 *pgd, unsigned long iova,
+			 unsigned long pg_size, gfp_t gfp, bool *updated)
 {
 	u64 *pte, *page;
 	int level, end_level;
@@ -162,7 +157,7 @@ static u64 *v2_alloc_pte(u64 *pgd, unsigned long iova,
 		}
 
 		if (!IOMMU_PTE_PRESENT(__pte)) {
-			page = alloc_pgtable_page();
+			page = alloc_pgtable_page(nid, gfp);
 			if (!page)
 				return NULL;
 
@@ -262,7 +257,8 @@ static int iommu_v2_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
 
 	while (mapped_size < size) {
 		map_size = get_alloc_page_size(pgsize);
-		pte = v2_alloc_pte(pdom->iop.pgd, iova, map_size, &updated);
+		pte = v2_alloc_pte(pdom->nid, pdom->iop.pgd,
+				   iova, map_size, gfp, &updated);
 		if (!pte) {
 			ret = -EINVAL;
 			goto out;
@@ -384,7 +380,7 @@ static struct io_pgtable *v2_alloc_pgtable(struct io_pgtable_cfg *cfg, void *coo
 	struct protection_domain *pdom = (struct protection_domain *)cookie;
 	int ret;
 
-	pgtable->pgd = alloc_pgtable_page();
+	pgtable->pgd = alloc_pgtable_page(pdom->nid, GFP_ATOMIC);
 	if (!pgtable->pgd)
 		return NULL;
 
Signature · 1 line
2.31.1
#3 lore raw

Re: [PATCH 2/3] iommu/amd: Allocate page table using numa locality info

Quoted 16 lines · Vasant Hegde

Allocate page table pages using IOMMU numa locality info. This optimizes page table walk by IOMMU.

---
  drivers/iommu/amd/amd_iommu.h     |  8 ++++++++
  drivers/iommu/amd/io_pgtable.c    |  4 ++--
  drivers/iommu/amd/io_pgtable_v2.c | 16 ++++++----------
  3 files changed, 16 insertions(+), 12 deletions(-)
drivers/iommu/amd/amd_iommu.h +0 -0
index c160a332ce33..916e888a941a 100644

drivers/iommu/amd/amd_iommu.h static inline int get_pci_sbdf_id(struct pci_dev *pdev) · line 129

Quoted 8 lines
drivers/iommu/amd/amd_iommu.h +5 -0
@@ -122,6 +122,14 @@ static inline int get_pci_sbdf_id(struct pci_dev *pdev)
  	return PCI_SEG_DEVID_TO_SBDF(seg, devid);
  }
 
+static inline void *alloc_pgtable_page(int nid, gfp_t gfp)
+{
+	struct page *page;
+
+	page = alloc_pages_node(nid, gfp | GFP_ATOMIC | __GFP_ZERO, 0);

Out of curiosity, the caller has already input a gfp, why do you add GFP_ATOMIC?

drivers/iommu/amd/amd_iommu.h static inline int get_pci_sbdf_id(struct pci_dev *pdev) · line 131

Quoted 2 lines
drivers/iommu/amd/amd_iommu.h +2 -0
+	return page ? page_address(page) : NULL;
+}

Best regards,
baolu

author maintainer
#4 lore raw

Re: [PATCH 2/3] iommu/amd: Allocate page table using numa locality info

Hi Baolu,

Quoted 12 lines · Baolu Lu
Quoted 11 lines · Vasant Hegde

Allocate page table pages using IOMMU numa locality info. This optimizes page table walk by IOMMU.

---
  drivers/iommu/amd/amd_iommu.h     |  8 ++++++++ drivers/iommu/amd/io_pgtable.c    |  4 ++-- drivers/iommu/amd/io_pgtable_v2.c | 16 ++++++----------

  3 files changed, 16 insertions(+), 12 deletions(-)
Quoted 15 lines

drivers/iommu/amd/amd_iommu.h static inline int get_pci_sbdf_id(struct pci_dev *pdev) · line 128

Quoted 12 lines
drivers/iommu/amd/amd_iommu.h +4 -0
index c160a332ce33..916e888a941a 100644
@@ -122,6 +122,14 @@ static inline int get_pci_sbdf_id(struct pci_dev *pdev)
       return PCI_SEG_DEVID_TO_SBDF(seg, devid);
   }
   +static inline void *alloc_pgtable_page(int nid, gfp_t gfp)
+{
+    struct page *page;
+
+    page = alloc_pages_node(nid, gfp | GFP_ATOMIC | __GFP_ZERO, 0);

Out of curiosity, the caller has already input a gfp, why do you add GFP_ATOMIC?

I missed to remove GFP_ATOMIC. Will fix it in v2.

-Vasant

author maintainer
#5 lore raw

PATCH v3 3/3 iommu/amd: Allocate IOMMU irqs using numa locality info

In reply to Vasant Hegde

Use numa information to allocate irq resources and also to set irq affinity. This optimizes the IOMMU interrupt handling.

---
 drivers/iommu/amd/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/amd/init.c +2 -1
index 19a46b9f7357..141eca29c951 100644
@@ -2383,6 +2383,7 @@ static int iommu_setup_intcapxt(struct amd_iommu *iommu)
 	struct irq_domain *domain;
 	struct irq_alloc_info info;
 	int irq, ret;
+	int node = dev_to_node(&iommu->dev->dev);
 
 	domain = iommu_get_irqdomain();
 	if (!domain)
@@ -2392,7 +2393,7 @@ static int iommu_setup_intcapxt(struct amd_iommu *iommu)
 	info.type = X86_IRQ_ALLOC_TYPE_AMDVI;
 	info.data = iommu;
 
-	irq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, &info);
+	irq = irq_domain_alloc_irqs(domain, 1, node, &info);
 	if (irq < 0) {
 		irq_domain_remove(domain);
 		return irq;
Signature · 1 line
2.31.1