Currently, the IOMMU subsystem tracks the total number of pages
allocated for IO page tables globally via nr_iommu_pages, but lacks the
ability to attribute these allocations to specific domains / users.

Add an atomic `nr_pages` counter to struct iommu_domain and introduce an
anon union to store the iommu_domain (aliased with private) in ioptdesc
for the accounting infra. The union ensures that the ioptdesc layout
remains identical to struct page, maintaining the integrity of existing
static_assert checks. Utilizing the `private` field is safe here as IOMMU
page table memory is exclusively owned by the IOMMU subsystem and is not
managed by other core MM subsystems that typically claim this slot.

Co-developed-by: Logan Odell <[email protected]>
Signed-off-by: Logan Odell <[email protected]>
Signed-off-by: Pranjal Shrivastava <[email protected]>
---
 drivers/iommu/iommu-pages.h | 8 +++++++-
 include/linux/iommu.h       | 2 ++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/iommu-pages.h b/drivers/iommu/iommu-pages.h
index e9e605b5fa3a..edf75c81054f 100644
--- a/drivers/iommu/iommu-pages.h
+++ b/drivers/iommu/iommu-pages.h
@@ -25,7 +25,13 @@ struct ioptdesc {
                u8 incoherent;
                pgoff_t __index;
        };
-       void *_private;
+       /*
+        * Alias iommu_domain with page->private for IOPT memory accounting.
+        */
+       union {
+               void *_private;
+               struct iommu_domain *domain;
+       };
 
        unsigned int __page_type;
        atomic_t __page_refcount;
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index ac43b8b93f14..478b64f4084f 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -231,6 +231,8 @@ struct iommu_domain {
        struct iommu_domain_geometry geometry;
        int (*iopf_handler)(struct iopf_group *group);
 
+       atomic_long_t nr_pages;
+
        union { /* cookie */
                struct iommu_dma_cookie *iova_cookie;
                struct iommu_dma_msi_cookie *msi_cookie;
-- 
2.56.0.rc1.315.gc6ed9934b7-goog


Reply via email to