[PATCH 2/5] iommu/vt-d: debugfs: Support specifying source identifier and PASID

From: Jingqi Liu
Date: Sun Jun 25 2023 - 11:18:27 EST


The original debugfs only dumps IOMMU page tables of all domains.
Usually developers want to dump the specified page table instead of all.

This patch supports users to specify the source identifier and PASID to
dump the specific page table.

For a device that only supports legacy mode, specify the source
identifier to dump its page table. For a device that supports scalable
mode, specify a {source identifier, PASID} pair to dump its page table.

Switch to dump all page tables by specifying "auto".

Examples are as follows:

1) Specify device "00:1f.0" that only supports legacy mode.
$ sudo echo 00:1f.0 >
/sys/kernel/debug/iommu/intel/domain_translation_struct

2) Specify device "00:0a.0" with PASID "1".
$ sudo echo 00:0a.0,1 >
/sys/kernel/debug/iommu/intel/domain_translation_struct

3) Specify all page tables:
$ sudo echo "auto" >
/sys/kernel/debug/iommu/intel/domain_translation_struct

Signed-off-by: Jingqi Liu <Jingqi.liu@xxxxxxxxx>
---
drivers/iommu/intel/debugfs.c | 86 ++++++++++++++++++++++++++++++++++-
1 file changed, 85 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/intel/debugfs.c b/drivers/iommu/intel/debugfs.c
index 072cfef19175..6d02cd91718a 100644
--- a/drivers/iommu/intel/debugfs.c
+++ b/drivers/iommu/intel/debugfs.c
@@ -32,6 +32,13 @@ struct iommu_regset {
const char *regs;
};

+#define BUF_SIZE 64
+
+static struct show_domain_info {
+ struct pci_dev *pdev;
+ ioasid_t pasid;
+} *show_domain_info;
+
#define DEBUG_BUFFER_SIZE 1024
static char debug_buf[DEBUG_BUFFER_SIZE];

@@ -392,6 +399,82 @@ static int domain_translation_struct_show(struct seq_file *m, void *unused)
show_device_domain_translation);
}

+static ssize_t domain_translation_struct_write(struct file *filp,
+ const char __user *ubuf,
+ size_t cnt, loff_t *ppos)
+{
+ char buf[BUF_SIZE], *srcid_ptr = NULL, *pasid_ptr = NULL;
+ unsigned int seg, bus, slot, func;
+ struct pci_dev *pdev = NULL;
+ u32 pasid = INVALID_IOASID;
+ char *key, *pbuf;
+ int i = 0;
+
+ if (cnt >= BUF_SIZE)
+ return -EINVAL;
+
+ if (copy_from_user(buf, ubuf, cnt))
+ return -EFAULT;
+
+ buf[cnt - 1] = 0;
+ if (!strcmp(buf, "auto")) {
+ if (show_domain_info)
+ show_domain_info->pdev = NULL;
+ *ppos += cnt;
+ return cnt;
+ }
+
+ pbuf = buf;
+
+ /* Seperate the input: one {source identifier, PASID} pair */
+ while ((key = strsep(&pbuf, ", ")) != NULL) {
+ if (!*key)
+ continue;
+ if (i >= 2) /* too many fields */
+ return -EINVAL;
+ if (i++ == 0)
+ srcid_ptr = key;
+ else
+ pasid_ptr = key;
+ }
+
+ if (!srcid_ptr) /* no source identifier */
+ return -EINVAL;
+
+ /*
+ * The string of source identifier must be of the form:
+ * [<domain>:]<bus>:<device>.<func>
+ */
+ i = sscanf(srcid_ptr, "%x:%x:%x.%x", &seg, &bus, &slot, &func);
+ if (i != 4) {
+ seg = 0;
+ i = sscanf(srcid_ptr, "%x:%x.%x", &bus, &slot, &func);
+ if (i != 3)
+ return -EINVAL;
+ }
+
+ pdev = pci_get_domain_bus_and_slot(seg, bus, PCI_DEVFN(slot, func));
+ if (!pdev)
+ return -EINVAL;
+
+ if (pasid_ptr &&
+ ((kstrtou32(pasid_ptr, 0, &pasid) < 0) || (pasid >= PASID_MAX)))
+ return -EINVAL;
+
+ if (!show_domain_info) {
+ show_domain_info = kzalloc(sizeof(*show_domain_info),
+ GFP_KERNEL);
+ if (!show_domain_info)
+ return -EINVAL;
+ }
+
+ show_domain_info->pdev = pdev;
+ show_domain_info->pasid = pasid;
+
+ *ppos += cnt;
+ return cnt;
+}
+
static int domain_translation_struct_open(struct inode *inode,
struct file *filp)
{
@@ -406,6 +489,7 @@ static int domain_translation_struct_open(struct inode *inode,

static const struct file_operations domain_translation_struct_fops = {
.open = domain_translation_struct_open,
+ .write = domain_translation_struct_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
@@ -691,7 +775,7 @@ void __init intel_iommu_debugfs_init(void)
&iommu_regset_fops);
debugfs_create_file("dmar_translation_struct", 0444, intel_iommu_debug,
NULL, &dmar_translation_struct_fops);
- debugfs_create_file("domain_translation_struct", 0444,
+ debugfs_create_file("domain_translation_struct", 0644,
intel_iommu_debug, NULL,
&domain_translation_struct_fops);
debugfs_create_file("invalidation_queue", 0444, intel_iommu_debug,
--
2.21.3