git: fb110e510438 - stable/13 - iommu_gas: Eliminate redundant parameters and push down lock acquisition

From: Doug Moore <dougm_at_FreeBSD.org>
Date: Mon, 08 Aug 2022 07:08:09 UTC
The branch stable/13 has been updated by dougm:

URL: https://cgit.FreeBSD.org/src/commit/?id=fb110e510438ecf93e834843add23fde9f965a4c

commit fb110e510438ecf93e834843add23fde9f965a4c
Author:     Alan Cox <alc@FreeBSD.org>
AuthorDate: 2022-07-29 06:14:46 +0000
Commit:     Doug Moore <dougm@FreeBSD.org>
CommitDate: 2022-08-08 07:07:50 +0000

    iommu_gas: Eliminate redundant parameters and push down lock acquisition
    
    Since IOMMU map entries store a reference to the domain in which they
    reside, there is no need to pass the domain to iommu_gas_free_entry(),
    iommu_gas_free_space(), and iommu_gas_free_region().
    
    Push down the acquisition and release of the IOMMU domain lock into
    iommu_gas_free_space() and iommu_gas_free_region().
    
    Both of these changes allow for simplifications in the callers of the
    functions without really complicating the functions themselves.
    Moreover, the latter change eliminates the direct use of the IOMMU
    domain lock from the x86-specific DMAR code.
    
    Reviewed by:    kib
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D35995
    
    (cherry picked from commit 4670f90846d49027bf23435a30895a74264f1e79)
---
 sys/arm64/iommu/iommu.c      | 10 ++--------
 sys/dev/iommu/busdma_iommu.c |  4 ++--
 sys/dev/iommu/iommu.h        |  9 +++------
 sys/dev/iommu/iommu_gas.c    | 44 +++++++++++++++++++++++++-------------------
 sys/x86/iommu/intel_ctx.c    | 13 ++++---------
 sys/x86/iommu/intel_qi.c     | 10 +++-------
 6 files changed, 39 insertions(+), 51 deletions(-)

diff --git a/sys/arm64/iommu/iommu.c b/sys/arm64/iommu/iommu.c
index 0a7503976036..e8aa56f92c4f 100644
--- a/sys/arm64/iommu/iommu.c
+++ b/sys/arm64/iommu/iommu.c
@@ -280,16 +280,10 @@ iommu_free_ctx(struct iommu_ctx *ioctx)
 static void
 iommu_domain_free_entry(struct iommu_map_entry *entry, bool free)
 {
-	struct iommu_domain *iodom;
-
-	iodom = entry->domain;
-
-	IOMMU_DOMAIN_LOCK(iodom);
-	iommu_gas_free_space(iodom, entry);
-	IOMMU_DOMAIN_UNLOCK(iodom);
+	iommu_gas_free_space(entry);
 
 	if (free)
-		iommu_gas_free_entry(iodom, entry);
+		iommu_gas_free_entry(entry);
 	else
 		entry->flags = 0;
 }
diff --git a/sys/dev/iommu/busdma_iommu.c b/sys/dev/iommu/busdma_iommu.c
index 4accafc64377..99960daa3219 100644
--- a/sys/dev/iommu/busdma_iommu.c
+++ b/sys/dev/iommu/busdma_iommu.c
@@ -1019,7 +1019,7 @@ bus_dma_iommu_load_ident(bus_dma_tag_t dmat, bus_dmamap_t map1,
 	ma = malloc(sizeof(vm_page_t) * atop(length), M_TEMP, waitok ?
 	    M_WAITOK : M_NOWAIT);
 	if (ma == NULL) {
-		iommu_gas_free_entry(domain, entry);
+		iommu_gas_free_entry(entry);
 		return (ENOMEM);
 	}
 	for (i = 0; i < atop(length); i++) {
@@ -1034,7 +1034,7 @@ bus_dma_iommu_load_ident(bus_dma_tag_t dmat, bus_dmamap_t map1,
 		TAILQ_INSERT_TAIL(&map->map_entries, entry, dmamap_link);
 		IOMMU_DMAMAP_UNLOCK(map);
 	} else {
-		iommu_gas_free_entry(domain, entry);
+		iommu_gas_free_entry(entry);
 	}
 	for (i = 0; i < atop(length); i++)
 		vm_page_putfake(ma[i]);
diff --git a/sys/dev/iommu/iommu.h b/sys/dev/iommu/iommu.h
index fefd0f615be5..ae4022c5c4f7 100644
--- a/sys/dev/iommu/iommu.h
+++ b/sys/dev/iommu/iommu.h
@@ -169,15 +169,12 @@ void iommu_gas_init_domain(struct iommu_domain *domain);
 void iommu_gas_fini_domain(struct iommu_domain *domain);
 struct iommu_map_entry *iommu_gas_alloc_entry(struct iommu_domain *domain,
     u_int flags);
-void iommu_gas_free_entry(struct iommu_domain *domain,
-    struct iommu_map_entry *entry);
-void iommu_gas_free_space(struct iommu_domain *domain,
-    struct iommu_map_entry *entry);
+void iommu_gas_free_entry(struct iommu_map_entry *entry);
+void iommu_gas_free_space(struct iommu_map_entry *entry);
 int iommu_gas_map(struct iommu_domain *domain,
     const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset,
     u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res);
-void iommu_gas_free_region(struct iommu_domain *domain,
-    struct iommu_map_entry *entry);
+void iommu_gas_free_region(struct iommu_map_entry *entry);
 int iommu_gas_map_region(struct iommu_domain *domain,
     struct iommu_map_entry *entry, u_int eflags, u_int flags, vm_page_t *ma);
 int iommu_gas_reserve_region(struct iommu_domain *domain, iommu_gaddr_t start,
diff --git a/sys/dev/iommu/iommu_gas.c b/sys/dev/iommu/iommu_gas.c
index bac15edcf849..bad56ab9140e 100644
--- a/sys/dev/iommu/iommu_gas.c
+++ b/sys/dev/iommu/iommu_gas.c
@@ -107,12 +107,11 @@ iommu_gas_alloc_entry(struct iommu_domain *domain, u_int flags)
 }
 
 void
-iommu_gas_free_entry(struct iommu_domain *domain, struct iommu_map_entry *entry)
+iommu_gas_free_entry(struct iommu_map_entry *entry)
 {
+	struct iommu_domain *domain;
 
-	KASSERT(domain == entry->domain,
-	    ("mismatched free domain %p entry %p entry->domain %p", domain,
-	    entry, entry->domain));
+	domain = entry->domain;
 	if (domain != NULL)
 		atomic_subtract_int(&domain->entries_cnt, 1);
 	uma_zfree(iommu_map_entry_zone, entry);
@@ -261,7 +260,7 @@ iommu_gas_fini_domain(struct iommu_domain *domain)
 	    (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED),
 	    ("start entry flags %p", domain));
 	RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root, entry);
-	iommu_gas_free_entry(domain, entry);
+	iommu_gas_free_entry(entry);
 
 	entry = RB_MAX(iommu_gas_entries_tree, &domain->rb_root);
 	KASSERT(entry->start == domain->end, ("end entry start %p", domain));
@@ -270,7 +269,7 @@ iommu_gas_fini_domain(struct iommu_domain *domain)
 	    (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_UNMAPPED),
 	    ("end entry flags %p", domain));
 	RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root, entry);
-	iommu_gas_free_entry(domain, entry);
+	iommu_gas_free_entry(entry);
 
 	RB_FOREACH_SAFE(entry, iommu_gas_entries_tree, &domain->rb_root,
 	    entry1) {
@@ -278,7 +277,7 @@ iommu_gas_fini_domain(struct iommu_domain *domain)
 		    ("non-RMRR entry left %p", domain));
 		RB_REMOVE(iommu_gas_entries_tree, &domain->rb_root,
 		    entry);
-		iommu_gas_free_entry(domain, entry);
+		iommu_gas_free_entry(entry);
 	}
 }
 
@@ -558,32 +557,37 @@ iommu_gas_alloc_region(struct iommu_domain *domain, struct iommu_map_entry *entr
 }
 
 void
-iommu_gas_free_space(struct iommu_domain *domain, struct iommu_map_entry *entry)
+iommu_gas_free_space(struct iommu_map_entry *entry)
 {
+	struct iommu_domain *domain;
 
-	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
+	domain = entry->domain;
 	KASSERT((entry->flags & (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_RMRR |
 	    IOMMU_MAP_ENTRY_MAP)) == IOMMU_MAP_ENTRY_MAP,
 	    ("permanent entry %p %p", domain, entry));
 
+	IOMMU_DOMAIN_LOCK(domain);
 	iommu_gas_rb_remove(domain, entry);
 	entry->flags &= ~IOMMU_MAP_ENTRY_MAP;
 #ifdef INVARIANTS
 	if (iommu_check_free)
 		iommu_gas_check_free(domain);
 #endif
+	IOMMU_DOMAIN_UNLOCK(domain);
 }
 
 void
-iommu_gas_free_region(struct iommu_domain *domain, struct iommu_map_entry *entry)
+iommu_gas_free_region(struct iommu_map_entry *entry)
 {
+	struct iommu_domain *domain;
 	struct iommu_map_entry *next, *prev;
 
-	IOMMU_DOMAIN_ASSERT_LOCKED(domain);
+	domain = entry->domain;
 	KASSERT((entry->flags & (IOMMU_MAP_ENTRY_PLACE | IOMMU_MAP_ENTRY_RMRR |
 	    IOMMU_MAP_ENTRY_MAP)) == IOMMU_MAP_ENTRY_RMRR,
 	    ("non-RMRR entry %p %p", domain, entry));
 
+	IOMMU_DOMAIN_LOCK(domain);
 	prev = RB_PREV(iommu_gas_entries_tree, &domain->rb_root, entry);
 	next = RB_NEXT(iommu_gas_entries_tree, &domain->rb_root, entry);
 	iommu_gas_rb_remove(domain, entry);
@@ -593,6 +597,7 @@ iommu_gas_free_region(struct iommu_domain *domain, struct iommu_map_entry *entry
 		iommu_gas_rb_insert(domain, domain->first_place);
 	if (next == NULL)
 		iommu_gas_rb_insert(domain, domain->last_place);
+	IOMMU_DOMAIN_UNLOCK(domain);
 }
 
 int
@@ -621,7 +626,7 @@ iommu_gas_map(struct iommu_domain *domain,
 	error = iommu_gas_find_space(&a);
 	if (error == ENOMEM) {
 		IOMMU_DOMAIN_UNLOCK(domain);
-		iommu_gas_free_entry(domain, entry);
+		iommu_gas_free_entry(entry);
 		return (error);
 	}
 #ifdef INVARIANTS
@@ -657,6 +662,9 @@ iommu_gas_map_region(struct iommu_domain *domain, struct iommu_map_entry *entry,
 	iommu_gaddr_t start;
 	int error;
 
+	KASSERT(entry->domain == domain,
+	    ("mismatched domain %p entry %p entry->domain %p", domain,
+	    entry, entry->domain));
 	KASSERT(entry->flags == 0, ("used RMRR entry %p %p %x", domain,
 	    entry, entry->flags));
 	KASSERT((flags & ~(IOMMU_MF_CANWAIT | IOMMU_MF_RMRR)) == 0,
@@ -716,7 +724,7 @@ iommu_gas_reserve_region(struct iommu_domain *domain, iommu_gaddr_t start,
 	error = iommu_gas_reserve_region_locked(domain, start, end, entry);
 	IOMMU_DOMAIN_UNLOCK(domain);
 	if (error != 0)
-		iommu_gas_free_entry(domain, entry);
+		iommu_gas_free_entry(entry);
 	else if (entry0 != NULL)
 		*entry0 = entry;
 	return (error);
@@ -770,7 +778,7 @@ iommu_gas_reserve_region_extend(struct iommu_domain *domain,
 	}
 	/* Release a preallocated entry if it was not used. */
 	if (entry != NULL)
-		iommu_gas_free_entry(domain, entry);
+		iommu_gas_free_entry(entry);
 	return (error);
 }
 
@@ -788,11 +796,9 @@ iommu_unmap_msi(struct iommu_ctx *ctx)
 	domain->ops->unmap(domain, entry->start, entry->end -
 	    entry->start, IOMMU_PGF_WAITOK);
 
-	IOMMU_DOMAIN_LOCK(domain);
-	iommu_gas_free_space(domain, entry);
-	IOMMU_DOMAIN_UNLOCK(domain);
+	iommu_gas_free_space(entry);
 
-	iommu_gas_free_entry(domain, entry);
+	iommu_gas_free_entry(entry);
 
 	domain->msi_entry = NULL;
 	domain->msi_base = 0;
@@ -832,7 +838,7 @@ iommu_map_msi(struct iommu_ctx *ctx, iommu_gaddr_t size, int offset,
 				 * We lost the race and already have an
 				 * MSI page allocated. Free the unneeded entry.
 				 */
-				iommu_gas_free_entry(domain, entry);
+				iommu_gas_free_entry(entry);
 			}
 		} else if (domain->msi_entry != NULL) {
 			/*
diff --git a/sys/x86/iommu/intel_ctx.c b/sys/x86/iommu/intel_ctx.c
index b36531ec6d17..a51f799ccbd8 100644
--- a/sys/x86/iommu/intel_ctx.c
+++ b/sys/x86/iommu/intel_ctx.c
@@ -307,7 +307,7 @@ domain_init_rmrr(struct dmar_domain *domain, device_t dev, int bus,
 				error = error1;
 			}
 			TAILQ_REMOVE(&rmrr_entries, entry, dmamap_link);
-			iommu_gas_free_entry(DOM2IODOM(domain), entry);
+			iommu_gas_free_entry(entry);
 		}
 		for (i = 0; i < size; i++)
 			vm_page_putfake(ma[i]);
@@ -848,17 +848,12 @@ dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid)
 void
 dmar_domain_free_entry(struct iommu_map_entry *entry, bool free)
 {
-	struct iommu_domain *domain;
-
-	domain = entry->domain;
-	IOMMU_DOMAIN_LOCK(domain);
 	if ((entry->flags & IOMMU_MAP_ENTRY_RMRR) != 0)
-		iommu_gas_free_region(domain, entry);
+		iommu_gas_free_region(entry);
 	else
-		iommu_gas_free_space(domain, entry);
-	IOMMU_DOMAIN_UNLOCK(domain);
+		iommu_gas_free_space(entry);
 	if (free)
-		iommu_gas_free_entry(domain, entry);
+		iommu_gas_free_entry(entry);
 	else
 		entry->flags = 0;
 }
diff --git a/sys/x86/iommu/intel_qi.c b/sys/x86/iommu/intel_qi.c
index 3a0012763000..baaf5b472a2c 100644
--- a/sys/x86/iommu/intel_qi.c
+++ b/sys/x86/iommu/intel_qi.c
@@ -415,7 +415,6 @@ static void
 dmar_qi_task(void *arg, int pending __unused)
 {
 	struct dmar_unit *unit;
-	struct iommu_domain *domain;
 	struct iommu_map_entry *entry, *head;
 	uint32_t ics;
 
@@ -440,14 +439,11 @@ dmar_qi_task(void *arg, int pending __unused)
 		if (!dmar_qi_seq_processed(unit, &entry->gseq))
 			break;
 		unit->tlb_flush_head = entry;
-		iommu_gas_free_entry(head->domain, head);
-		domain = entry->domain;
-		IOMMU_DOMAIN_LOCK(domain);
+		iommu_gas_free_entry(head);
 		if ((entry->flags & IOMMU_MAP_ENTRY_RMRR) != 0)
-			iommu_gas_free_region(domain, entry);
+			iommu_gas_free_region(entry);
 		else
-			iommu_gas_free_space(domain, entry);
-		IOMMU_DOMAIN_UNLOCK(domain);
+			iommu_gas_free_space(entry);
 	}
 	if (unit->inv_seq_waiters > 0) {
 		/*