Skip to contentSkip to conversation

amd/iommu: Cleanup and fixes

PATCH Applied Vasant Hegde 2 participants 9 messages Sep 12, 2022 – Sep 28, 2022

Subsystems (2)
lore
Share & tools
lore

Vasant Hegde

author maintainer
#0 lore raw

cover amd/iommu: Cleanup and fixes

This series contains few fixes like better handling of error path, fixing sparse warning and trivial cleanup like removing outdated, etc.

Vasant Hegde (5):
iommu/amd: Free domain id in error path iommu/amd: Protect domain update with domain_lock iommu/amd: Free domain ID after domain_flush_pages iommu/amd: Remove outdated comment iommu/amd: Fix sparse warning

 drivers/iommu/amd/amd_iommu_types.h |  2 ++
 drivers/iommu/amd/init.c            |  2 --
 drivers/iommu/amd/iommu.c           | 22 ++++++++++++++--------
 3 files changed, 16 insertions(+), 10 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/5 iommu/amd: Free domain id in error path

Call domain_id_free() in error path.

---
 drivers/iommu/amd/iommu.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/iommu/amd/iommu.c +6 -2
index 6bfcac8de1f4..c55f4a129b1e 100644
@@ -2027,8 +2027,10 @@ static int protection_domain_init_v1(struct protection_domain *domain, int mode)
 
 	if (mode != PAGE_MODE_NONE) {
 		pt_root = (void *)get_zeroed_page(GFP_KERNEL);
-		if (!pt_root)
+		if (!pt_root) {
+			domain_id_free(domain->id);
 			return -ENOMEM;
+		}
 	}
 
 	amd_iommu_domain_set_pgtable(domain, pt_root, mode);
@@ -2092,8 +2094,10 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
 		goto out_err;
 
 	pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);
-	if (!pgtbl_ops)
+	if (!pgtbl_ops) {
+		domain_id_free(domain->id);
 		goto out_err;
+	}
 
 	return domain;
 out_err:
Signature · 1 line
2.31.1
author maintainer
#2 lore raw

PATCH 2/5 iommu/amd: Protect domain update with domain_lock

In reply to Vasant Hegde

In protection_domain_free() path it calls amd_iommu_domain_update() to update IOMMU.
protection_domain_free -> free_io_pgtable_ops ->

    v1_free_pgtable -> amd_iommu_domain_update

Make sure its protected using domain->lock.

---
 drivers/iommu/amd/iommu.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/iommu/amd/iommu.c +6 -0
index c55f4a129b1e..f49980956220 100644
@@ -2001,15 +2001,21 @@ static void cleanup_domain(struct protection_domain *domain)
 
 static void protection_domain_free(struct protection_domain *domain)
 {
+	unsigned long flags;
+
 	if (!domain)
 		return;
 
 	if (domain->id)
 		domain_id_free(domain->id);
 
+	spin_lock_irqsave(&domain->lock, flags);
+
 	if (domain->iop.pgtbl_cfg.tlb)
 		free_io_pgtable_ops(&domain->iop.iop.ops);
 
+	spin_unlock_irqrestore(&domain->lock, flags);
+
 	kfree(domain);
 }
 
Signature · 1 line
2.31.1
maintainer
#3 lore raw

Re: [PATCH 2/5] iommu/amd: Protect domain update with domain_lock

Hi Vasant,

Quoted 6 lines · Vasant Hegde

In protection_domain_free() path it calls amd_iommu_domain_update() to update IOMMU.
protection_domain_free -> free_io_pgtable_ops ->

    v1_free_pgtable -> amd_iommu_domain_update

Make sure its protected using domain->lock.

A concurrent access to the domain at this point would be a bug in itself, and the locking wouldn't help here either because the domain including the lock is freed right after release.

Do you see and concurrent accesses to the domain at this point?

Regards,

Joerg

author maintainer
#4 lore raw

Re: [PATCH 2/5] iommu/amd: Protect domain update with domain_lock

Hello Joerg,

Quoted 8 lines · Joerg Roedel

Hi Vasant,

Quoted 5 lines · Vasant Hegde

In protection_domain_free() path it calls amd_iommu_domain_update() to update IOMMU.
protection_domain_free -> free_io_pgtable_ops ->

    v1_free_pgtable -> amd_iommu_domain_update
Quoted 7 lines
Quoted 1 line

Make sure its protected using domain->lock.

A concurrent access to the domain at this point would be a bug in itself, and the locking wouldn't help here either because the domain including the lock is freed right after release.

Do you see and concurrent accesses to the domain at this point?

I didn't see any concurrent access. I was going through code and thought lock is required here.

What you said is makes sense. We can drop this patch.

-Vasant

author maintainer
#5 lore raw

PATCH 3/5 iommu/amd: Free domain ID after domain_flush_pages

In reply to Vasant Hegde

free_io_pgtable_ops() path uses domain ID to flush pages. Hence free domain ID after flushing everything.

---
 drivers/iommu/amd/iommu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/amd/iommu.c +3 -3
index f49980956220..4509c9c4ee20 100644
@@ -2006,9 +2006,6 @@ static void protection_domain_free(struct protection_domain *domain)
 	if (!domain)
 		return;
 
-	if (domain->id)
-		domain_id_free(domain->id);
-
 	spin_lock_irqsave(&domain->lock, flags);
 
 	if (domain->iop.pgtbl_cfg.tlb)
@@ -2016,6 +2013,9 @@ static void protection_domain_free(struct protection_domain *domain)
 
 	spin_unlock_irqrestore(&domain->lock, flags);
 
+	if (domain->id)
+		domain_id_free(domain->id);
+
 	kfree(domain);
 }
 
Signature · 1 line
2.31.1
author maintainer
#6 lore raw

PATCH 4/5 iommu/amd: Remove outdated comment

In reply to Vasant Hegde

Comment is not related to amd_iommu_ops variable.

---
 drivers/iommu/amd/iommu.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iommu/amd/iommu.c +0 -4
index 4509c9c4ee20..c589832695a7 100644
@@ -66,10 +66,6 @@ LIST_HEAD(ioapic_map);
 LIST_HEAD(hpet_map);
 LIST_HEAD(acpihid_map);
 
-/*
- * Domain for untranslated devices - only allocated
- * if iommu=pt passed on kernel cmd line.
- */
 const struct iommu_ops amd_iommu_ops;
 
 static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Signature · 1 line
2.31.1
author maintainer
#7 lore raw

PATCH 5/5 iommu/amd: Fix sparse warning

In reply to Vasant Hegde

CHECK drivers/iommu/amd/iommu.c
drivers/iommu/amd/iommu.c:73:24: warning: symbol 'amd_iommu_ops' was not declared. Should it be static?

---
 drivers/iommu/amd/amd_iommu_types.h | 2 ++
 drivers/iommu/amd/init.c            | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +2 -0
index 1beed57fc35d..1d0a70c85333 100644
@@ -461,6 +461,8 @@ struct irq_remap_table {
 /* Interrupt remapping feature used? */
 extern bool amd_iommu_irq_remap;
 
+extern const struct iommu_ops amd_iommu_ops;
+
 /* IVRS indicates that pre-boot remapping was enabled */
 extern bool amdr_ivrs_remap_support;
 
drivers/iommu/amd/init.c +0 -2
index 688cf8387b0b..a515e837adf1 100644
@@ -95,8 +95,6 @@
  * out of it.
  */
 
-extern const struct iommu_ops amd_iommu_ops;
-
 /*
  * structure describing one IOMMU in the ACPI table. Typically followed by one
  * or more ivhd_entrys.
Signature · 1 line
2.31.1
maintainer Applied
#8 lore raw
In reply to Vasant Hegde
Quoted 9 lines · Vasant Hegde

This series contains few fixes like better handling of error path, fixing sparse warning and trivial cleanup like removing outdated, etc.

Vasant Hegde (5):
iommu/amd: Free domain id in error path iommu/amd: Protect domain update with domain_lock iommu/amd: Free domain ID after domain_flush_pages iommu/amd: Remove outdated comment iommu/amd: Fix sparse warning

Applied patch 1 and 3-5, thanks Vasant.