From: Logan Odell <[email protected]>

Collect the IO page table usage statistics from each IOMMU domain
attached to a legacy VFIO container and expose that data to userspace via
the new iommu-nr-pages field in fdinfo.

Introduce and implement the get_nr_pages callback for vfio-iommu-type1,
that iterates over all attached domains and aggregates their nr_pages
counters.

Only domains whose page table is implemented by the generic_pt library
account their page table memory. If any domain attached to the container
does not, get_nr_pages() returns -EOPNOTSUPP and the field is omitted
instead of reporting a partial count.

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

Signed-off-by: Logan Odell <[email protected]>
Signed-off-by: Pranjal Shrivastava <[email protected]>
---
 Documentation/filesystems/proc.rst | 17 +++++++++++++++++
 drivers/vfio/container.c           | 21 +++++++++++++++++++++
 drivers/vfio/vfio.h                |  2 ++
 drivers/vfio/vfio_iommu_type1.c    | 21 +++++++++++++++++++++
 4 files changed, 61 insertions(+)

diff --git a/Documentation/filesystems/proc.rst 
b/Documentation/filesystems/proc.rst
index c102b62023cd..2d06c9930fcf 100644
--- a/Documentation/filesystems/proc.rst
+++ b/Documentation/filesystems/proc.rst
@@ -2200,6 +2200,23 @@ VFIO Device files
 where 'vfio-device-syspath' is the sysfs path corresponding to the VFIO device
 file.
 
+VFIO Container files
+~~~~~~~~~~~~~~~~~~~~
+
+::
+
+       pos:    0
+       flags:  02000002
+       mnt_id: 17
+       ino:    1103
+       iommu-nr-pages: 11
+
+where 'iommu-nr-pages' is the number of pages of memory used by the IO page
+tables of all IOMMU domains attached to the container. It is omitted when no
+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.
+
 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/vfio/container.c b/drivers/vfio/container.c
index 003281dbf8bc..2cb6c5373626 100644
--- a/drivers/vfio/container.c
+++ b/drivers/vfio/container.c
@@ -7,6 +7,7 @@
 #include <linux/file.h>
 #include <linux/slab.h>
 #include <linux/fs.h>
+#include <linux/seq_file.h>
 #include <linux/capability.h>
 #include <linux/iommu.h>
 #include <linux/miscdevice.h>
@@ -384,12 +385,32 @@ static int vfio_fops_release(struct inode *inode, struct 
file *filep)
        return 0;
 }
 
+#ifdef CONFIG_PROC_FS
+static void vfio_fops_show_fdinfo(struct seq_file *m, struct file *filep)
+{
+       struct vfio_container *container = filep->private_data;
+       long nr_pages = -EOPNOTSUPP;
+
+       down_read(&container->group_lock);
+       if (container->iommu_driver && 
container->iommu_driver->ops->get_nr_pages)
+               nr_pages = 
container->iommu_driver->ops->get_nr_pages(container->iommu_data);
+       up_read(&container->group_lock);
+
+       /* Only report a count that covers every domain in the container */
+       if (nr_pages >= 0)
+               seq_printf(m, "iommu-nr-pages:\t%ld\n", nr_pages);
+}
+#endif
+
 static const struct file_operations vfio_fops = {
        .owner          = THIS_MODULE,
        .open           = vfio_fops_open,
        .release        = vfio_fops_release,
        .unlocked_ioctl = vfio_fops_unl_ioctl,
        .compat_ioctl   = compat_ptr_ioctl,
+#ifdef CONFIG_PROC_FS
+       .show_fdinfo    = vfio_fops_show_fdinfo,
+#endif
 };
 
 struct vfio_container *vfio_container_from_file(struct file *file)
diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
index 7728bc99b63d..37e8841ad0e9 100644
--- a/drivers/vfio/vfio.h
+++ b/drivers/vfio/vfio.h
@@ -226,6 +226,8 @@ struct vfio_iommu_driver_ops {
                                  void *data, size_t count, bool write);
        struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
                                                   struct iommu_group *group);
+       /* Returns IO page table pages used, or -EOPNOTSUPP if not accounted */
+       long            (*get_nr_pages)(void *iommu_data);
 };
 
 struct vfio_iommu_driver {
diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index c8151ba54de3..fc7c1553d574 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -3249,6 +3249,26 @@ vfio_iommu_type1_group_iommu_domain(void *iommu_data,
        return domain;
 }
 
+static long vfio_iommu_type1_get_nr_pages(void *iommu_data)
+{
+       struct vfio_iommu *iommu = iommu_data;
+       struct vfio_domain *d;
+       long total = 0;
+
+       mutex_lock(&iommu->lock);
+       list_for_each_entry(d, &iommu->domain_list, next) {
+               /* Only generic_pt page tables account their pages */
+               if (!d->domain->is_iommupt) {
+                       total = -EOPNOTSUPP;
+                       break;
+               }
+               total += atomic_long_read(&d->domain->nr_pages);
+       }
+       mutex_unlock(&iommu->lock);
+
+       return total;
+}
+
 static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
        .name                   = "vfio-iommu-type1",
        .owner                  = THIS_MODULE,
@@ -3263,6 +3283,7 @@ static const struct vfio_iommu_driver_ops 
vfio_iommu_driver_ops_type1 = {
        .unregister_device      = vfio_iommu_type1_unregister_device,
        .dma_rw                 = vfio_iommu_type1_dma_rw,
        .group_iommu_domain     = vfio_iommu_type1_group_iommu_domain,
+       .get_nr_pages           = vfio_iommu_type1_get_nr_pages,
 };
 
 static int __init vfio_iommu_type1_init(void)
-- 
2.56.0.rc1.315.gc6ed9934b7-goog


Reply via email to