[RFC PATCH 2/5] iommupt: Implement refcounting logic for Leaf entries

From: Pranjal Shrivastava

Date: Thu Oct 01 2026 - 19:02:49 EST


Implement a lockless state machine using the ioptdesc's __page_refcount
field. During an unmap, the driver executes atomic_sub_return() when
clearing a leaf PTE. If the refcount drops to the baseline state (1),
the directory is completely empty and its pointer is stored to the
domain's reclaim_list Xarray.

To protect against a concurrent background Shrinker pruning a directory
while a map thread is actively populating it, the map thread must acquire
a reference using atomic_inc_not_zero() before installing a new leaf.
If the acquisition fails, the page has been marked dead (0) by the
Shrinker. The map thread cleanly returns -EAGAIN, forcing the walker to
loop and re-allocate a fresh directory.

Only non-DMA domains are queued: DMA API unmaps can run in IRQ context
and the reclaim_list lock is not IRQ safe.

Signed-off-by: Pranjal Shrivastava <praan@xxxxxxxxxx>
---
drivers/iommu/generic_pt/iommu_pt.h | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)

diff --git a/drivers/iommu/generic_pt/iommu_pt.h b/drivers/iommu/generic_pt/iommu_pt.h
index 07ec2b3ab986..c3d09b746eec 100644
--- a/drivers/iommu/generic_pt/iommu_pt.h
+++ b/drivers/iommu/generic_pt/iommu_pt.h
@@ -621,6 +621,13 @@ static int __map_range_leaf(struct pt_range *range, void *arg,
PT_WARN_ON(compute_best_pgsize(&pts, oa) !=
leaf_pgsize_lg2);
}
+
+ /* If refcount = 0, shrinker killed the page, retry map */
+ if (!atomic_inc_not_zero(&virt_to_ioptdesc(pts.table)->__page_refcount)) {
+ ret = -EAGAIN;
+ break;
+ }
+
pt_install_leaf_entry(&pts, oa, leaf_pgsize_lg2, &map->attrs);

oa += log2_to_int(leaf_pgsize_lg2);
@@ -754,6 +761,11 @@ static __always_inline int __do_map_single_page(struct pt_range *range,
if (pts.level == 0) {
if (pts.type != PT_ENTRY_EMPTY)
return -EADDRINUSE;
+
+ /* If refcount = 0, shrinker killed the page, retry map */
+ if (!atomic_inc_not_zero(&virt_to_ioptdesc(pts.table)->__page_refcount))
+ return -EAGAIN;
+
pt_install_leaf_entry(&pts, map->oa, PAGE_SHIFT,
&map->attrs);
/* No flush, not used when incoherent */
@@ -1087,6 +1099,20 @@ static __maybe_unused int __unmap_range(struct pt_range *range, void *arg,
*/
num_contig_lg2 = pt_entry_num_contig_lg2(&pts);
pt_clear_entries(&pts, num_contig_lg2);
+
+ /* Drop refcount and add to reclaim_list for the last ref */
+ /*
+ * DMA API unmaps can run in IRQ context and the reclaim_list
+ * lock is not IRQ safe, so only queue for non-DMA domains.
+ */
+ if (atomic_dec_return(&virt_to_ioptdesc(pts.table)->__page_refcount) == 1 &&
+ !iommu_is_dma_domain(&iommu_from_common(range->common)->domain)) {
+ struct pt_iommu *iommu = iommu_from_common(range->common);
+
+ xa_store(&iommu->domain.reclaim_list, range->va,
+ virt_to_ioptdesc(pts.table), GFP_ATOMIC);
+ }
+
gather_add_leaf(&unmap->pending, &pts);
num_oas += log2_to_int(num_contig_lg2);
if (pts.index < flush_start_index)
--
2.56.0.rc1.315.gc6ed9934b7-goog