Collect the IO page table usage statistics from each paging HWPT object
within an iommufd context and expose that data to userspace via the new
iommu-nr-pages field in fdinfo.

Implement a show_fdinfo hook for iommufd_fops that iterates over the
context's object xarray, identifies paging HWPTs, and aggregates their
nr_pages counters. Nested HWPTs are skipped as their stage-1 page tables
are owned by userspace.

Only domains whose page table is implemented by the generic_pt library
account their page table memory. If the domain of any paging HWPT does
not, the field is omitted instead of reporting a partial count.

Document the new field in Documentation/filesystems/proc.rst.

Signed-off-by: Pranjal Shrivastava <[email protected]>
---
 Documentation/filesystems/proc.rst | 17 +++++++++++++
 drivers/iommu/iommufd/main.c       | 38 ++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/Documentation/filesystems/proc.rst 
b/Documentation/filesystems/proc.rst
index 2d06c9930fcf..90ba32e9e3b1 100644
--- a/Documentation/filesystems/proc.rst
+++ b/Documentation/filesystems/proc.rst
@@ -2217,6 +2217,23 @@ IOMMU backend is set, or when any attached domain does 
not account its IO page
 table memory. Currently only domains using the generic_pt page table library
 account their IO page table memory.
 
+IOMMUFD files
+~~~~~~~~~~~~~
+
+::
+
+       pos:    0
+       flags:  02000002
+       mnt_id: 17
+       ino:    1104
+       iommu-nr-pages: 11
+
+where 'iommu-nr-pages' is the number of pages of memory used by the IO page
+tables of all paging HW pagetables owned by the iommufd context. Nested HW
+pagetables are not included since their page tables are owned by userspace.
+The field is omitted when any paging HW pagetable does not account its IO page
+table memory.
+
 3.9    /proc/<pid>/map_files - Information about memory mapped files
 ---------------------------------------------------------------------
 This directory contains symbolic links which represent memory mapped files
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index 9a921b153162..51d0d100dc91 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -11,6 +11,7 @@
 #include <linux/bug.h>
 #include <linux/file.h>
 #include <linux/fs.h>
+#include <linux/seq_file.h>
 #include <linux/iommufd.h>
 #include <linux/miscdevice.h>
 #include <linux/module.h>
@@ -633,12 +634,49 @@ static int iommufd_fops_mmap(struct file *filp, struct 
vm_area_struct *vma)
        return rc;
 }
 
+#ifdef CONFIG_PROC_FS
+static void iommufd_fops_show_fdinfo(struct seq_file *m, struct file *filep)
+{
+       struct iommufd_ctx *ictx = filep->private_data;
+       struct iommufd_object *obj;
+       unsigned long nr_pages = 0;
+       bool supported = true;
+       unsigned long index;
+
+       xa_lock(&ictx->objects);
+       xa_for_each(&ictx->objects, index, obj) {
+               struct iommufd_hw_pagetable *hwpt;
+
+               /* Nested stage-1 page tables are owned by the guest */
+               if (obj->type != IOMMUFD_OBJ_HWPT_PAGING)
+                       continue;
+
+               hwpt = container_of(obj, struct iommufd_hw_pagetable, obj);
+               if (!hwpt->domain)
+                       continue;
+               /* Only generic_pt page tables account their pages */
+               if (!hwpt->domain->is_iommupt) {
+                       supported = false;
+                       break;
+               }
+               nr_pages += atomic_long_read(&hwpt->domain->nr_pages);
+       }
+       xa_unlock(&ictx->objects);
+
+       if (supported)
+               seq_printf(m, "iommu-nr-pages:\t%lu\n", nr_pages);
+}
+#endif
+
 static const struct file_operations iommufd_fops = {
        .owner = THIS_MODULE,
        .open = iommufd_fops_open,
        .release = iommufd_fops_release,
        .unlocked_ioctl = iommufd_fops_ioctl,
        .mmap = iommufd_fops_mmap,
+#ifdef CONFIG_PROC_FS
+       .show_fdinfo = iommufd_fops_show_fdinfo,
+#endif
 };
 
 /**
-- 
2.56.0.rc1.315.gc6ed9934b7-goog


Reply via email to