[RFC PATCH 3/5] iommupt: Add lockless sever_branch helper

From: Pranjal Shrivastava

Date: Thu Oct 01 2026 - 19:07:10 EST


Introduce a new sever_branch pt_iommu_op. This helper performs a
top-down walk to locate the target parent slot and safely executes a
lockless cmpxchg to 0x0. Because generic_pt abstracts all hardware PTEs
as u64, this safely utilizes the existing pt_table_install64 to cleanly
sever the branch regardless of the underlying IOMMU format.

Signed-off-by: Pranjal Shrivastava <praan@xxxxxxxxxx>
---
drivers/iommu/generic_pt/iommu_pt.h | 40 +++++++++++++++++++++++++++++
include/linux/generic_pt/iommu.h | 14 ++++++++++
2 files changed, 54 insertions(+)

diff --git a/drivers/iommu/generic_pt/iommu_pt.h b/drivers/iommu/generic_pt/iommu_pt.h
index c3d09b746eec..868242eb2d55 100644
--- a/drivers/iommu/generic_pt/iommu_pt.h
+++ b/drivers/iommu/generic_pt/iommu_pt.h
@@ -1155,6 +1155,45 @@ static size_t NS(unmap_range)(struct pt_iommu *iommu_table, dma_addr_t iova,
return unmap.unmapped;
}

+struct pt_sever_args {
+ phys_addr_t expected_phys;
+ bool success;
+};
+
+static int __sever_branch(struct pt_range *range, void *arg,
+ unsigned int level, struct pt_table_p *table)
+{
+ struct pt_state pts = pt_init(range, level, table);
+ struct pt_sever_args *sever = arg;
+
+ switch (pt_load_single_entry(&pts)) {
+ case PT_ENTRY_TABLE:
+ if (virt_to_phys(pt_table_ptr(&pts)) == sever->expected_phys) {
+ sever->success = pt_table_install64(&pts, 0x0);
+ return 1; /* Stop walking */
+ }
+ return pt_descend(&pts, arg, __sever_branch);
+ default:
+ break;
+ }
+ return 0;
+}
+
+static bool NS(sever_branch)(struct pt_iommu *iommu_table, dma_addr_t iova,
+ phys_addr_t expected_phys)
+{
+ struct pt_range range;
+ struct pt_sever_args sever = { .expected_phys = expected_phys, .success = false };
+ int ret;
+
+ ret = make_range(common_from_iommu(iommu_table), &range, iova, 1);
+ if (ret)
+ return false;
+
+ pt_walk_range(&range, __sever_branch, &sever);
+ return sever.success;
+}
+
static void NS(get_info)(struct pt_iommu *iommu_table,
struct pt_iommu_info *info)
{
@@ -1208,6 +1247,7 @@ static void NS(deinit)(struct pt_iommu *iommu_table)
static const struct pt_iommu_ops NS(ops) = {
.map_range = NS(map_range),
.unmap_range = NS(unmap_range),
+ .sever_branch = NS(sever_branch),
#if IS_ENABLED(CONFIG_IOMMUFD_DRIVER) && defined(pt_entry_is_write_dirty) && \
IS_ENABLED(CONFIG_IOMMUFD_TEST) && defined(pt_entry_make_write_dirty)
.set_dirty = NS(set_dirty),
diff --git a/include/linux/generic_pt/iommu.h b/include/linux/generic_pt/iommu.h
index dd0edd02a48a..d1ade1767a9a 100644
--- a/include/linux/generic_pt/iommu.h
+++ b/include/linux/generic_pt/iommu.h
@@ -137,6 +137,20 @@ struct pt_iommu_ops {
dma_addr_t len,
struct iommu_iotlb_gather *iotlb_gather);

+ /**
+ * @sever_branch: Locklessly sever an empty page table branch
+ * @iommu_table: Table to manipulate
+ * @iova: IO virtual address associated with the branch
+ * @expected_phys: Expected physical address of the child directory
+ *
+ * Context: Executed locklessly by a background Shrinker.
+ * Uses cmpxchg to write 0x0 to the parent slot.
+ *
+ * Returns: true if successfully severed, false if aborted.
+ */
+ bool (*sever_branch)(struct pt_iommu *iommu_table, dma_addr_t iova,
+ phys_addr_t expected_phys);
+
/**
* @set_dirty: Make the iova write dirty
* @iommu_table: Table to manipulate
--
2.56.0.rc1.315.gc6ed9934b7-goog