[PATCH v5 04/17] iommufd: Bound software MSI mappings to the reserved range
From: Andrew Jones
Date: Mon Aug 31 2026 - 13:06:49 EST
Track the start and length of the software MSI reserved region as one
range and pass it to the mapping lookup and allocation helpers.
Reject cached mappings outside the region and new mappings when no
complete page remains. This enforces the reserved region bounds and
prepares the helpers to allocate contiguous MSI address lists.
Signed-off-by: Andrew Jones <andrew.jones@xxxxxxxxxxxxxxxx>
---
drivers/iommu/iommufd/device.c | 17 +++++++++++------
drivers/iommu/iommufd/driver.c | 21 ++++++++++++---------
drivers/iommu/iommufd/io_pagetable.c | 9 +++++----
drivers/iommu/iommufd/iommufd_private.h | 9 +++++++--
4 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 868f1e591208..7b83810a1c90 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -97,7 +97,7 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx,
kref_init(&new_igroup->ref);
mutex_init(&new_igroup->lock);
xa_init(&new_igroup->pasid_attach);
- new_igroup->sw_msi_start = PHYS_ADDR_MAX;
+ new_igroup->sw_msi_range.start = PHYS_ADDR_MAX;
/* group reference moves into new_igroup */
new_igroup->group = group;
@@ -374,7 +374,7 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup,
struct iommufd_ctx *ictx = igroup->ictx;
struct iommufd_sw_msi_map *cur;
- if (igroup->sw_msi_start == PHYS_ADDR_MAX)
+ if (igroup->sw_msi_range.start == PHYS_ADDR_MAX)
return 0;
/*
@@ -384,7 +384,7 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup,
list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
int rc;
- if (cur->sw_msi_start != igroup->sw_msi_start ||
+ if (cur->sw_msi_start != igroup->sw_msi_range.start ||
!iommufd_sw_msi_maps_test_bit(&igroup->required_sw_msi, cur->id))
continue;
@@ -414,18 +414,23 @@ static int
iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
struct iommufd_hwpt_paging *hwpt_paging)
{
+ struct iommufd_sw_msi_range sw_msi_range = {
+ .start = PHYS_ADDR_MAX,
+ };
struct iommufd_group *igroup = idev->igroup;
+ bool first_attach;
int rc;
lockdep_assert_held(&igroup->lock);
+ first_attach = iommufd_group_first_attach(igroup, IOMMU_NO_PASID);
rc = iopt_table_enforce_dev_resv_regions(&hwpt_paging->ioas->iopt,
- idev->dev,
- &igroup->sw_msi_start);
+ idev->dev, &sw_msi_range);
if (rc)
return rc;
- if (iommufd_group_first_attach(igroup, IOMMU_NO_PASID)) {
+ if (first_attach) {
+ igroup->sw_msi_range = sw_msi_range;
rc = iommufd_group_setup_msi(igroup, hwpt_paging);
if (rc) {
iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt,
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index 3856989f5c4b..8bb47a81fed1 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -186,14 +186,15 @@ EXPORT_SYMBOL_NS_GPL(iommufd_viommu_report_event, "IOMMUFD");
*/
static struct iommufd_sw_msi_map *
iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
- phys_addr_t sw_msi_start)
+ const struct iommufd_sw_msi_range *sw_msi_range)
{
struct iommufd_sw_msi_map *cur;
lockdep_assert_held(&ictx->sw_msi_lock);
list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
- if (cur->sw_msi_start != sw_msi_start)
+ if (cur->sw_msi_start != sw_msi_range->start ||
+ cur->pgoff >= sw_msi_range->length / PAGE_SIZE)
continue;
if (cur->msi_addr == msi_addr)
return cur;
@@ -203,7 +204,7 @@ iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
static struct iommufd_sw_msi_map *
iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
- phys_addr_t sw_msi_start)
+ const struct iommufd_sw_msi_range *sw_msi_range)
{
struct iommufd_sw_msi_map *cur;
unsigned int max_pgoff = 0;
@@ -211,7 +212,7 @@ iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
lockdep_assert_held(&ictx->sw_msi_lock);
list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
- if (cur->sw_msi_start != sw_msi_start)
+ if (cur->sw_msi_start != sw_msi_range->start)
continue;
if (cur->pgoff == UINT_MAX)
return ERR_PTR(-EOVERFLOW);
@@ -219,14 +220,16 @@ iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
}
if (ictx->sw_msi_id > IOMMUFD_SW_MSI_MAX_ID ||
- max_pgoff > (ULONG_MAX - sw_msi_start) / PAGE_SIZE)
+ max_pgoff > (ULONG_MAX - sw_msi_range->start) / PAGE_SIZE)
return ERR_PTR(-EOVERFLOW);
+ if (max_pgoff >= sw_msi_range->length / PAGE_SIZE)
+ return ERR_PTR(-ENOSPC);
cur = kzalloc_obj(*cur);
if (!cur)
return ERR_PTR(-ENOMEM);
- cur->sw_msi_start = sw_msi_start;
+ cur->sw_msi_start = sw_msi_range->start;
cur->msi_addr = msi_addr;
cur->pgoff = max_pgoff;
cur->id = ictx->sw_msi_id++;
@@ -295,7 +298,7 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
handle = to_iommufd_handle(raw_handle);
/* No IOMMU_RESV_SW_MSI means no change to the msi_msg */
- if (handle->idev->igroup->sw_msi_start == PHYS_ADDR_MAX)
+ if (handle->idev->igroup->sw_msi_range.start == PHYS_ADDR_MAX)
return 0;
ictx = handle->idev->ictx;
@@ -307,11 +310,11 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
*/
msi_map = iommufd_sw_msi_get_map(handle->idev->ictx,
msi_addr & PAGE_MASK,
- handle->idev->igroup->sw_msi_start);
+ &handle->idev->igroup->sw_msi_range);
if (!msi_map)
msi_map = iommufd_sw_msi_alloc_map(handle->idev->ictx,
msi_addr & PAGE_MASK,
- handle->idev->igroup->sw_msi_start);
+ &handle->idev->igroup->sw_msi_range);
if (IS_ERR(msi_map))
return PTR_ERR(msi_map);
diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c
index 24d4917105d9..8bf2554af334 100644
--- a/drivers/iommu/iommufd/io_pagetable.c
+++ b/drivers/iommu/iommufd/io_pagetable.c
@@ -1501,7 +1501,7 @@ void iopt_remove_access(struct io_pagetable *iopt,
/* Narrow the valid_iova_itree to include reserved ranges from a device. */
int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt,
struct device *dev,
- phys_addr_t *sw_msi_start)
+ struct iommufd_sw_msi_range *sw_msi_range)
{
struct iommu_resv_region *resv;
LIST_HEAD(resv_regions);
@@ -1520,10 +1520,11 @@ int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt,
if (resv->type == IOMMU_RESV_DIRECT_RELAXABLE)
continue;
- if (sw_msi_start && resv->type == IOMMU_RESV_MSI)
+ if (sw_msi_range && resv->type == IOMMU_RESV_MSI)
num_hw_msi++;
- if (sw_msi_start && resv->type == IOMMU_RESV_SW_MSI) {
- *sw_msi_start = resv->start;
+ if (sw_msi_range && resv->type == IOMMU_RESV_SW_MSI) {
+ sw_msi_range->start = resv->start;
+ sw_msi_range->length = resv->length;
num_sw_msi++;
}
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 9ca5f9f92cdf..4e2d32809695 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -22,6 +22,11 @@ struct iommu_option;
struct iommufd_device;
struct dma_buf_attachment;
+struct iommufd_sw_msi_range {
+ phys_addr_t start;
+ size_t length;
+};
+
struct iommufd_sw_msi_map {
struct list_head sw_msi_item;
phys_addr_t sw_msi_start;
@@ -171,7 +176,7 @@ void iopt_table_remove_domain(struct io_pagetable *iopt,
struct iommu_domain *domain);
int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt,
struct device *dev,
- phys_addr_t *sw_msi_start);
+ struct iommufd_sw_msi_range *sw_msi_range);
int iopt_set_allow_iova(struct io_pagetable *iopt,
struct rb_root_cached *allowed_iova);
int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start,
@@ -510,7 +515,7 @@ struct iommufd_group {
struct iommu_group *group;
struct xarray pasid_attach;
struct iommufd_sw_msi_maps required_sw_msi;
- phys_addr_t sw_msi_start;
+ struct iommufd_sw_msi_range sw_msi_range;
};
/*
--
2.43.0