From: Andrew Jones <andrew.jones@oss.qualcomm.com>
To: linux-riscv@lists.infradead.org, iommu@lists.linux.dev
Cc: linux-kernel@vger.kernel.org, tomasz.jeznach@linux.dev,
tjeznach@rivosinc.com, jgg@ziepe.ca, jgg@nvidia.com,
joro@8bytes.org, will@kernel.org, robin.murphy@arm.com,
pjw@kernel.org, palmer@dabbelt.com, anup@brainfault.org,
tglx@kernel.org, kevin.tian@intel.com,
fangyu.yu@linux.alibaba.com
Subject: [PATCH v5 04/17] iommufd: Bound software MSI mappings to the reserved range
Date: Mon, 31 Aug 2026 16:59:30 +0200 [thread overview]
Message-ID: <20260831145943.313726-5-andrew.jones@oss.qualcomm.com> (raw)
In-Reply-To: <20260831145943.313726-1-andrew.jones@oss.qualcomm.com>
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@oss.qualcomm.com>
---
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
next prev parent reply other threads:[~2026-08-31 14:59 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-31 14:59 [PATCH v5 00/17] iommu/riscv: Enable MSI remapping, IOMMU_DMA and VFIO Andrew Jones
2026-08-31 14:59 ` [PATCH v5 01/17] iommu/dma: Prepare MSI physical address lists Andrew Jones
2026-08-31 14:59 ` [PATCH v5 02/17] iommufd: Convert struct iommufd_sw_msi_maps to a growable bitmap Andrew Jones
2026-09-01 7:52 ` Nutty.Liu
2026-08-31 14:59 ` [PATCH v5 03/17] iommufd: Split software MSI map lookup and allocation Andrew Jones
2026-08-31 14:59 ` Andrew Jones [this message]
2026-08-31 14:59 ` [PATCH v5 05/17] iommufd: Prepare software MSI maps for address lists Andrew Jones
2026-08-31 14:59 ` [PATCH v5 06/17] iommufd: Install software MSI map ranges atomically Andrew Jones
2026-08-31 14:59 ` [PATCH v5 07/17] iommufd: Prepare software MSI installation for address lists Andrew Jones
2026-08-31 14:59 ` [PATCH v5 08/17] iommu/dma: Introduce iommu_dma_prepare_msi_list() Andrew Jones
2026-08-31 14:59 ` [PATCH v5 09/17] iommu/riscv: Report cache coherency capability Andrew Jones
2026-08-31 14:59 ` [PATCH v5 10/17] iommu/riscv: Reserve an MSI IOVA window for iommufd Andrew Jones
2026-08-31 14:59 ` [PATCH v5 11/17] irqchip/riscv-imsic: Add S-mode MSI address list Andrew Jones
2026-09-01 13:38 ` Andrew Jones
2026-09-23 11:55 ` Anup Patel
2026-08-31 14:59 ` [PATCH v5 12/17] irqchip/riscv-imsic: Support IOMMU MSI address lists Andrew Jones
2026-09-23 12:01 ` Anup Patel
2026-08-31 14:59 ` [PATCH v5 13/17] iommu/dma: Enable IOMMU_DMA for 64-bit RISC-V Andrew Jones
2026-08-31 14:59 ` [PATCH v5 14/17] vfio: enable IOMMU_TYPE1 for RISC-V Andrew Jones
2026-08-31 14:59 ` [PATCH v5 15/17] RISC-V: KVM: Enable KVM_VFIO interfaces on RISC-V arch Andrew Jones
2026-08-31 14:59 ` [PATCH v5 16/17] riscv: defconfig: Enable IOMMUFD and VFIO Andrew Jones
2026-08-31 14:59 ` [PATCH v5 17/17] selftests/vfio: Allow building on RISC-V Andrew Jones
2026-09-08 13:08 ` [PATCH v5 00/17] iommu/riscv: Enable MSI remapping, IOMMU_DMA and VFIO fangyu.yu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260831145943.313726-5-andrew.jones@oss.qualcomm.com \
--to=andrew.jones@oss.qualcomm.com \
--cc=anup@brainfault.org \
--cc=fangyu.yu@linux.alibaba.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=robin.murphy@arm.com \
--cc=tglx@kernel.org \
--cc=tjeznach@rivosinc.com \
--cc=tomasz.jeznach@linux.dev \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®