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
This commit is contained in:
Alan Cox 2022-07-29 01:14:46 -05:00
parent be1f485d7d
commit 4670f90846
6 changed files with 39 additions and 51 deletions

View File

@ -410,16 +410,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;
}

View File

@ -1040,7 +1040,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++) {
@ -1055,7 +1055,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]);

View File

@ -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,

View File

@ -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) {
/*

View File

@ -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]);
@ -852,17 +852,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;
}

View File

@ -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) {
/*