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 06/17] iommufd: Install software MSI map ranges atomically
Date: Mon, 31 Aug 2026 16:59:32 +0200 [thread overview]
Message-ID: <20260831145943.313726-7-andrew.jones@oss.qualcomm.com> (raw)
In-Reply-To: <20260831145943.313726-1-andrew.jones@oss.qualcomm.com>
Add a range installation helper which installs every map in a contiguous
software MSI range and rolls back only mappings added by the failed
operation. Use the helper when replaying range mappings into a new
paging domain.
This prepares iommufd to publish and install physical address lists
without exposing partially installed ranges.
Signed-off-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
---
drivers/iommu/iommufd/driver.c | 76 ++++++++++++++++++++++++++++++----
1 file changed, 68 insertions(+), 8 deletions(-)
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index 6a0527f4b058..a43a25078f87 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -178,6 +178,17 @@ int iommufd_viommu_report_event(struct iommufd_viommu *viommu,
EXPORT_SYMBOL_NS_GPL(iommufd_viommu_report_event, "IOMMUFD");
#ifdef CONFIG_IRQ_MSI_IOMMU
+static bool iommufd_sw_msi_range_index(const struct iommufd_sw_msi_map *map,
+ const struct iommufd_sw_msi_map *base_map,
+ unsigned int nr_addrs, unsigned int *index)
+{
+ if (map->sw_msi_start != base_map->sw_msi_start || map->pgoff < base_map->pgoff)
+ return false;
+
+ *index = map->pgoff - base_map->pgoff;
+ return *index < nr_addrs;
+}
+
/*
* Get an iommufd_sw_msi_map for the msi physical addresses requested by the irq
* layer. The mapping to IOVA is global to the iommufd file descriptor, every
@@ -207,11 +218,7 @@ iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, const phys_addr_t *phys_addrs,
list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
unsigned int index;
- if (cur->sw_msi_start != sw_msi_range->start ||
- cur->pgoff < msi_map->pgoff)
- continue;
- index = cur->pgoff - msi_map->pgoff;
- if (index >= nr_addrs)
+ if (!iommufd_sw_msi_range_index(cur, msi_map, nr_addrs, &index))
continue;
if (cur->msi_addr != phys_addrs[index])
break;
@@ -306,9 +313,9 @@ iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, const phys_addr_t *phys_addrs
return ERR_PTR(-ENOMEM);
}
-int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
- struct iommufd_hwpt_paging *hwpt_paging,
- struct iommufd_sw_msi_map *msi_map)
+static int iommufd_sw_msi_install_one(struct iommufd_ctx *ictx,
+ struct iommufd_hwpt_paging *hwpt_paging,
+ struct iommufd_sw_msi_map *msi_map)
{
unsigned long iova;
int rc;
@@ -332,6 +339,59 @@ int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
__set_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap);
return 0;
}
+
+int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
+ struct iommufd_hwpt_paging *hwpt_paging,
+ struct iommufd_sw_msi_map *base_map)
+{
+ struct iommufd_sw_msi_map *msi_map;
+ struct list_head *msi_maps = &ictx->sw_msi_list;
+ unsigned long *newly_mapped;
+ unsigned int nr_addrs = base_map->range_size ? base_map->range_size / PAGE_SIZE : 1;
+ unsigned int nr_found = 0;
+ unsigned int index;
+ int rc = 0;
+
+ newly_mapped = bitmap_zalloc(nr_addrs, GFP_KERNEL_ACCOUNT);
+ if (!newly_mapped)
+ return -ENOMEM;
+
+ list_for_each_entry(msi_map, msi_maps, sw_msi_item) {
+ if (!iommufd_sw_msi_range_index(msi_map, base_map, nr_addrs, &index))
+ continue;
+ if (iommufd_sw_msi_maps_test_bit(&hwpt_paging->present_sw_msi, msi_map->id)) {
+ nr_found++;
+ continue;
+ }
+ rc = iommufd_sw_msi_install_one(ictx, hwpt_paging, msi_map);
+ if (rc)
+ goto err_unmap;
+ __set_bit(index, newly_mapped);
+ nr_found++;
+ }
+ if (nr_found != nr_addrs) {
+ rc = -EINVAL;
+ goto err_unmap;
+ }
+
+ bitmap_free(newly_mapped);
+ return 0;
+
+err_unmap:
+ list_for_each_entry(msi_map, msi_maps, sw_msi_item) {
+ unsigned long iova;
+
+ if (!iommufd_sw_msi_range_index(msi_map, base_map, nr_addrs, &index) ||
+ !test_bit(index, newly_mapped))
+ continue;
+
+ iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
+ WARN_ON_ONCE(iommu_unmap(hwpt_paging->common.domain, iova, PAGE_SIZE) != PAGE_SIZE);
+ __clear_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap);
+ }
+ bitmap_free(newly_mapped);
+ return rc;
+}
EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi_install, "IOMMUFD_INTERNAL");
/*
--
2.43.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2026-08-31 15:00 UTC|newest]
Thread overview: 21+ 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 ` [PATCH v5 04/17] iommufd: Bound software MSI mappings to the reserved range Andrew Jones
2026-08-31 14:59 ` [PATCH v5 05/17] iommufd: Prepare software MSI maps for address lists Andrew Jones
2026-08-31 14:59 ` Andrew Jones [this message]
2026-08-31 14:59 ` [PATCH v5 07/17] iommufd: Prepare software MSI installation " 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-08-31 14:59 ` [PATCH v5 12/17] irqchip/riscv-imsic: Support IOMMU MSI address lists Andrew Jones
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-7-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