From nobody Sat Feb 7 19:45:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 813ABEB64DD for ; Sun, 25 Jun 2023 15:18:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230116AbjFYPSH (ORCPT ); Sun, 25 Jun 2023 11:18:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229613AbjFYPSE (ORCPT ); Sun, 25 Jun 2023 11:18:04 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 968211B9 for ; Sun, 25 Jun 2023 08:18:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1687706283; x=1719242283; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mN/2WK8J7n2rewlZ5IincQ/OigkXaXVK51epQ2blgdQ=; b=HMzrtJ+NE/XMqkhhKJAxwHNriLzW71B4XhiL10MGRytkgSDV3sIQ9ENl VRK6AX0fUG3X+YuoZEDtmShUlpObQg8PasedvcQ0ByZ9gO2/0hqSDxrW3 fPKJON1D5z1lLxDU9SeZ6vF7Df+jannAmCz8C/InnWOlwCK1LZy16eXL9 by1xDaFUs4wUY+KF3OqfbEPQ6Z0tT3iOZMr8uCLKhepcRJwT1fk+gPZHO /3SXk7dvaw7vmKDqoAt6rq7UAAjU0P0jv5VEo4ReLmYnOZ3NkjYGCEYEc 0WHc+WggR6tifyawVF1SvGBvR4cEiAO+Kk/NnYFw4dt7oDhwMvAy//4mO Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10752"; a="391255057" X-IronPort-AV: E=Sophos;i="6.01,157,1684825200"; d="scan'208";a="391255057" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jun 2023 08:18:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10752"; a="840004517" X-IronPort-AV: E=Sophos;i="6.01,157,1684825200"; d="scan'208";a="840004517" Received: from cascade.sh.intel.com ([10.239.48.162]) by orsmga004.jf.intel.com with ESMTP; 25 Jun 2023 08:18:01 -0700 From: Jingqi Liu To: iommu@lists.linux.dev, Lu Baolu , Tian Kevin , Joerg Roedel , Will Deacon , Robin Murphy Cc: linux-kernel@vger.kernel.org, Jingqi Liu Subject: [PATCH 1/5] iommu/vt-d: debugfs: Define domain_translation_struct file ops Date: Sun, 25 Jun 2023 23:04:38 +0800 Message-Id: <20230625150442.42197-2-Jingqi.liu@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230625150442.42197-1-Jingqi.liu@intel.com> References: <20230625150442.42197-1-Jingqi.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Define domain_translation_struct file_operations instead of using DEFINE_SHOW_ATTRIBUTE() in order to specify source identifier and pasid to dump the specified page table. Signed-off-by: Jingqi Liu --- drivers/iommu/intel/debugfs.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/intel/debugfs.c b/drivers/iommu/intel/debugfs.c index 1f925285104e..072cfef19175 100644 --- a/drivers/iommu/intel/debugfs.c +++ b/drivers/iommu/intel/debugfs.c @@ -391,7 +391,25 @@ static int domain_translation_struct_show(struct seq_f= ile *m, void *unused) return bus_for_each_dev(&pci_bus_type, NULL, m, show_device_domain_translation); } -DEFINE_SHOW_ATTRIBUTE(domain_translation_struct); + +static int domain_translation_struct_open(struct inode *inode, + struct file *filp) +{ + /* + * Allocate one 1Mbyte buffer to save sequential file output, + * since the default size of input buffer is 1Mbyte when the + * user reads. + */ + return single_open_size(filp, domain_translation_struct_show, + inode->i_private, SZ_1M); +} + +static const struct file_operations domain_translation_struct_fops =3D { + .open =3D domain_translation_struct_open, + .read =3D seq_read, + .llseek =3D seq_lseek, + .release =3D single_release, +}; =20 static void invalidation_queue_entry_show(struct seq_file *m, struct intel_iommu *iommu) --=20 2.21.3 From nobody Sat Feb 7 19:45:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5E6DAC0015E for ; Sun, 25 Jun 2023 15:18:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229753AbjFYPSR (ORCPT ); Sun, 25 Jun 2023 11:18:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230110AbjFYPSG (ORCPT ); Sun, 25 Jun 2023 11:18:06 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F20441B7 for ; Sun, 25 Jun 2023 08:18:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1687706285; x=1719242285; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GBzP0QOzSLMJvNAZUflo7PPRyVHgUMz92AyAiFJMx5k=; b=P0eNZqAqEvl8Wejz0ZK7AS6ZMpMpaB4JoMHlyKeIfW1+kBpv7H9BjrlU kbRggPqXdmQf1pVwmtNAwzC/gglsZi1AhjW47a6SJI5RTK9pnzRjBOzJ+ 20RqNM5NZbWDR8BIkS8iJdA2piQ8csLlO9Gu+8yOrcIjvijcp2oGgSl5I qiyXxGGtR9a8QUWFDFsz7u4yiKjWN8wCbfmCEXykwfFm7vmVLfeQHoOg3 Us9oSHD4R+JDyYmQHw8gSs9QpCqCC0MCHWzWe2mpdRsOTR2cL76NR7/Nt Nv+mqWqWSaKp8kUXnWCVAA/yAdu6ky45vfNjSs/L3h7iTiA+59yv1jHB7 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10752"; a="391255066" X-IronPort-AV: E=Sophos;i="6.01,157,1684825200"; d="scan'208";a="391255066" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jun 2023 08:18:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10752"; a="840004527" X-IronPort-AV: E=Sophos;i="6.01,157,1684825200"; d="scan'208";a="840004527" Received: from cascade.sh.intel.com ([10.239.48.162]) by orsmga004.jf.intel.com with ESMTP; 25 Jun 2023 08:18:03 -0700 From: Jingqi Liu To: iommu@lists.linux.dev, Lu Baolu , Tian Kevin , Joerg Roedel , Will Deacon , Robin Murphy Cc: linux-kernel@vger.kernel.org, Jingqi Liu Subject: [PATCH 2/5] iommu/vt-d: debugfs: Support specifying source identifier and PASID Date: Sun, 25 Jun 2023 23:04:39 +0800 Message-Id: <20230625150442.42197-3-Jingqi.liu@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230625150442.42197-1-Jingqi.liu@intel.com> References: <20230625150442.42197-1-Jingqi.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 --- 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; }; =20 +#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]; =20 @@ -392,6 +399,82 @@ static int domain_translation_struct_show(struct seq_f= ile *m, void *unused) show_device_domain_translation); } =20 +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 =3D NULL, *pasid_ptr =3D NULL; + unsigned int seg, bus, slot, func; + struct pci_dev *pdev =3D NULL; + u32 pasid =3D INVALID_IOASID; + char *key, *pbuf; + int i =3D 0; + + if (cnt >=3D BUF_SIZE) + return -EINVAL; + + if (copy_from_user(buf, ubuf, cnt)) + return -EFAULT; + + buf[cnt - 1] =3D 0; + if (!strcmp(buf, "auto")) { + if (show_domain_info) + show_domain_info->pdev =3D NULL; + *ppos +=3D cnt; + return cnt; + } + + pbuf =3D buf; + + /* Seperate the input: one {source identifier, PASID} pair */ + while ((key =3D strsep(&pbuf, ", ")) !=3D NULL) { + if (!*key) + continue; + if (i >=3D 2) /* too many fields */ + return -EINVAL; + if (i++ =3D=3D 0) + srcid_ptr =3D key; + else + pasid_ptr =3D key; + } + + if (!srcid_ptr) /* no source identifier */ + return -EINVAL; + + /* + * The string of source identifier must be of the form: + * [:]:. + */ + i =3D sscanf(srcid_ptr, "%x:%x:%x.%x", &seg, &bus, &slot, &func); + if (i !=3D 4) { + seg =3D 0; + i =3D sscanf(srcid_ptr, "%x:%x.%x", &bus, &slot, &func); + if (i !=3D 3) + return -EINVAL; + } + + pdev =3D 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 >=3D PASID_MAX))) + return -EINVAL; + + if (!show_domain_info) { + show_domain_info =3D kzalloc(sizeof(*show_domain_info), + GFP_KERNEL); + if (!show_domain_info) + return -EINVAL; + } + + show_domain_info->pdev =3D pdev; + show_domain_info->pasid =3D pasid; + + *ppos +=3D 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, =20 static const struct file_operations domain_translation_struct_fops =3D { .open =3D domain_translation_struct_open, + .write =3D domain_translation_struct_write, .read =3D seq_read, .llseek =3D seq_lseek, .release =3D 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, --=20 2.21.3 From nobody Sat Feb 7 19:45:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 358F2EB64DC for ; Sun, 25 Jun 2023 15:18:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230160AbjFYPSZ (ORCPT ); Sun, 25 Jun 2023 11:18:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229613AbjFYPSJ (ORCPT ); Sun, 25 Jun 2023 11:18:09 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 784671B7 for ; Sun, 25 Jun 2023 08:18:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1687706288; x=1719242288; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dGog1IvJBB3RYoMtBN+mKPewp6Oixq643V5vrMTp7ao=; b=frs0jY2kOoEDZsJT6mD43TcdgXbbGwopvrepz7WUpJR8Zdr4LPtNSjHq D5r82I80V8UoX2/3B+ztj2W5s0EHHVnIdnlDNul+r/p3uY4CN0GirTQUM LRhjjTxUt7ujZMdhDx98ZWIxIjJl7BT17jG0JTzETwRXV+4P6CEKsZ3bq HlRVH9s2gxcutxa7w4CrCrh5N+7DFRAz4182itNgu5+7xIIyteRun5R3d 4+2cKgizKZuxnvxLDGtKox3W+rEDcaEkxbm6gTXSMf5r8oClPampq8m7Z 5knZREGt9Yng3rzYsw9wNrfIWRocvRKNXeCLSCexXrH3q//mwdNLJujZL w==; X-IronPort-AV: E=McAfee;i="6600,9927,10752"; a="391255078" X-IronPort-AV: E=Sophos;i="6.01,157,1684825200"; d="scan'208";a="391255078" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jun 2023 08:18:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10752"; a="840004533" X-IronPort-AV: E=Sophos;i="6.01,157,1684825200"; d="scan'208";a="840004533" Received: from cascade.sh.intel.com ([10.239.48.162]) by orsmga004.jf.intel.com with ESMTP; 25 Jun 2023 08:18:05 -0700 From: Jingqi Liu To: iommu@lists.linux.dev, Lu Baolu , Tian Kevin , Joerg Roedel , Will Deacon , Robin Murphy Cc: linux-kernel@vger.kernel.org, Jingqi Liu Subject: [PATCH 3/5] iommu/vt-d: debugfs: Dump the corresponding page table of a pasid Date: Sun, 25 Jun 2023 23:04:40 +0800 Message-Id: <20230625150442.42197-4-Jingqi.liu@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230625150442.42197-1-Jingqi.liu@intel.com> References: <20230625150442.42197-1-Jingqi.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a generic helper to dump the page table contained in a pasid table entr= y. For implementations supporting Scalable Mode Translation, the PASID-table entries contain pointers to both first-stage and second-stage translation structures, along with the PASID Granular Translation Type (PGTT) field that specifies which translation process the request undergoes. The original debugfs only dumps the contents of pasid table entry when traversing the pasid table. Add a check to decide whether to dump the page table contained by a pasid table entry. Signed-off-by: Jingqi Liu --- drivers/iommu/intel/debugfs.c | 59 ++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/intel/debugfs.c b/drivers/iommu/intel/debugfs.c index 6d02cd91718a..212d33598de9 100644 --- a/drivers/iommu/intel/debugfs.c +++ b/drivers/iommu/intel/debugfs.c @@ -19,9 +19,11 @@ #include "perf.h" =20 struct tbl_walk { + u16 segment; /* PCI segment# */ u16 bus; u16 devfn; u32 pasid; + bool dump_page_table; struct root_entry *rt_entry; struct context_entry *ctx_entry; struct pasid_entry *pasid_tbl_entry; @@ -118,6 +120,8 @@ static const struct iommu_regset iommu_regs_64[] =3D { IOMMU_REGSET_ENTRY(VCRSP), }; =20 +static void dump_translation_page_table(struct seq_file *m); + static int iommu_regset_show(struct seq_file *m, void *unused) { struct dmar_drhd_unit *drhd; @@ -199,7 +203,11 @@ static void pasid_tbl_walk(struct seq_file *m, struct = pasid_entry *tbl_entry, if (pasid_pte_is_present(tbl_entry)) { tbl_wlk->pasid_tbl_entry =3D tbl_entry; tbl_wlk->pasid =3D (dir_idx << PASID_PDE_SHIFT) + tbl_idx; - print_tbl_walk(m); + + if (tbl_wlk->dump_page_table) + dump_translation_page_table(m); + else + print_tbl_walk(m); } =20 tbl_entry++; @@ -347,6 +355,55 @@ static void pgtable_walk_level(struct seq_file *m, str= uct dma_pte *pde, } } =20 +/* + * Dump the page table that contained in a pasid table entry. + * There're two consumers of this helper, as follows: + * 1) When traversing the pasid table, dump the page table + * contained in the pasid table entry. + * 2) Find the pasid table entry with a specified pasid, + * and dump the page table it contains. + */ +static void dump_translation_page_table(struct seq_file *m) +{ + struct tbl_walk *tbl_wlk =3D m->private; + u64 pgd, path[6] =3D { 0 }; + u16 pgtt; + u8 agaw; + + if (!tbl_wlk->pasid_tbl_entry) + return; + + /* + * According to PASID Granular Translation Type(PGTT), + * get the page table pointer. + */ + pgtt =3D (u16)(tbl_wlk->pasid_tbl_entry->val[0] & GENMASK_ULL(8, 6)) >> 6; + agaw =3D (u8)(tbl_wlk->pasid_tbl_entry->val[0] & GENMASK_ULL(4, 2)) >> 2; + + switch (pgtt) { + case PASID_ENTRY_PGTT_FL_ONLY: + pgd =3D tbl_wlk->pasid_tbl_entry->val[2]; + break; + case PASID_ENTRY_PGTT_SL_ONLY: + case PASID_ENTRY_PGTT_NESTED: + pgd =3D tbl_wlk->pasid_tbl_entry->val[0]; + break; + default: + return; + } + + pgd &=3D VTD_PAGE_MASK; + seq_printf(m, "Device %04x:%02x:%02x.%x with pasid %x @0x%llx\n", + tbl_wlk->segment, tbl_wlk->bus, PCI_SLOT(tbl_wlk->devfn), + PCI_FUNC(tbl_wlk->devfn), tbl_wlk->pasid, pgd); + seq_printf(m, "%-17s\t%-18s\t%-18s\t%-18s\t%-18s\t%-s\n", + "IOVA_PFN", "PML5E", "PML4E", "PDPE", "PDE", "PTE"); + pgtable_walk_level(m, phys_to_virt(pgd), agaw + 2, 0, path); + seq_putc(m, '\n'); + + return; +} + static int __show_device_domain_translation(struct device *dev, void *data) { struct dmar_domain *domain; --=20 2.21.3 From nobody Sat Feb 7 19:45:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46F9BEB64DC for ; Sun, 25 Jun 2023 15:18:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230388AbjFYPSa (ORCPT ); Sun, 25 Jun 2023 11:18:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230254AbjFYPSM (ORCPT ); Sun, 25 Jun 2023 11:18:12 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFEF41BE for ; Sun, 25 Jun 2023 08:18:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1687706290; x=1719242290; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MWvly+gTVz1Bc7knQ7hqBn1J0RvVjUjDzsQTS3OHj+c=; b=XaR/Kx0r/Ud++EzlBr2hzv3B9D6Td/FK/jtukZfcYDy4hT52gLqnAfbY xOGUmd+yiPmLAFA50tN+GmXlKWvZlwX8Xf+blAuVutuR83IEo7bnfmM41 zUzS0Z47W3XOakcqAygR/KKkcetcN5WQuprhrEp4Oz9P7Qnkhk8JoNXU7 pQqv3fFhvtiNtzJ34TEmhhBqeKFFSDW7wcw4PZTUhZddUuuZVu77bqTCG U3RZ2WR5qf0bS8r1lVcinikh5I4ovwv5g2BdgzuOSOeN1Wv35wwMe7mvJ NJiPz3b1NaxN51AdFKj+k+73f+wo6B24qbi3wzZ94btEQQty6JAzDHAaA A==; X-IronPort-AV: E=McAfee;i="6600,9927,10752"; a="391255084" X-IronPort-AV: E=Sophos;i="6.01,157,1684825200"; d="scan'208";a="391255084" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jun 2023 08:18:10 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10752"; a="840004538" X-IronPort-AV: E=Sophos;i="6.01,157,1684825200"; d="scan'208";a="840004538" Received: from cascade.sh.intel.com ([10.239.48.162]) by orsmga004.jf.intel.com with ESMTP; 25 Jun 2023 08:18:08 -0700 From: Jingqi Liu To: iommu@lists.linux.dev, Lu Baolu , Tian Kevin , Joerg Roedel , Will Deacon , Robin Murphy Cc: linux-kernel@vger.kernel.org, Jingqi Liu Subject: [PATCH 4/5] iommu/vt-d: debugfs: Support dumping a specified page table Date: Sun, 25 Jun 2023 23:04:41 +0800 Message-Id: <20230625150442.42197-5-Jingqi.liu@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230625150442.42197-1-Jingqi.liu@intel.com> References: <20230625150442.42197-1-Jingqi.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The original debugfs only dumps all page tables without pasid. With pasid supported, the page table with pasid also needs to be dumped. This patch supports dumping a specified page table or all page tables in legacy mode or scalable mode. For legacy mode, according to bus number and DEVFN, traverse the root table and context table to get the pointer of page table in the context table entry, then dump the page table. For scalable mode, according to bus number, DEVFN and pasid, traverse the root table, context table, pasid directory and pasid table to get the pointer of page table in the pasid table entry, then dump it. Examples are as follows: 1) Dump the page table of device "00:1f.0" that only supports legacy mode. $ sudo echo 00:1f.0 > /sys/kernel/debug/iommu/intel/domain_translation_struct $ sudo cat /sys/kernel/debug/iommu/intel/domain_translation_struct 2) Dump the page table of device "00:0a.0" with PASID "1". $ sudo echo 00:0a.0,1 > /sys/kernel/debug/iommu/intel/domain_translation_struct $ sudo cat /sys/kernel/debug/iommu/intel/domain_translation_struct 3) Dump all page tables. $ sudo echo "auto" > /sys/kernel/debug/iommu/intel/domain_translation_struct $ sudo cat /sys/kernel/debug/iommu/intel/domain_translation_struct Signed-off-by: Jingqi Liu --- drivers/iommu/intel/debugfs.c | 191 ++++++++++++++++++++++++++++------ 1 file changed, 159 insertions(+), 32 deletions(-) diff --git a/drivers/iommu/intel/debugfs.c b/drivers/iommu/intel/debugfs.c index 212d33598de9..e4d3b7836076 100644 --- a/drivers/iommu/intel/debugfs.c +++ b/drivers/iommu/intel/debugfs.c @@ -404,56 +404,183 @@ static void dump_translation_page_table(struct seq_f= ile *m) return; } =20 -static int __show_device_domain_translation(struct device *dev, void *data) +/* + * Dump the page table with the specified device and pasid. + * For legacy mode, search root and context tables to find + * the page table. + * For scalable mode, search root, context, pasid directory + * and pasid tables to find the page table. + * If not specify device, it will traverse all devices and + * pasid tables, and then dump all page tables. + */ +static int show_device_domain_translation(struct show_domain_info *sinfo, + void *data) { - struct dmar_domain *domain; + bool walk_tbl =3D false, found =3D false; + u16 s_devfn =3D 0, e_devfn =3D 255, devfn; + u16 s_bus =3D 0, e_bus =3D 255, bus, seg; + struct dmar_drhd_unit *drhd; + struct intel_iommu *iommu; struct seq_file *m =3D data; - u64 path[6] =3D { 0 }; + bool scalable; =20 - domain =3D to_dmar_domain(iommu_get_domain_for_dev(dev)); - if (!domain) - return 0; + if (sinfo && sinfo->pdev) { + s_bus =3D sinfo->pdev->bus->number; + e_bus =3D sinfo->pdev->bus->number; + s_devfn =3D sinfo->pdev->devfn; + e_devfn =3D sinfo->pdev->devfn; + seg =3D pci_domain_nr(sinfo->pdev->bus); + } else + walk_tbl =3D true; =20 - seq_printf(m, "Device %s @0x%llx\n", dev_name(dev), - (u64)virt_to_phys(domain->pgd)); - seq_puts(m, "IOVA_PFN\t\tPML5E\t\t\tPML4E\t\t\tPDPE\t\t\tPDE\t\t\tPTE\n"); - - pgtable_walk_level(m, domain->pgd, domain->agaw + 2, 0, path); - seq_putc(m, '\n'); + rcu_read_lock(); + for_each_active_iommu(iommu, drhd) { + struct context_entry *context; + u64 pgd, path[6] =3D { 0 }; + u32 sts, agaw; =20 - /* Don't iterate */ - return 1; -} + if (sinfo && sinfo->pdev && (seg !=3D iommu->segment)) + continue; =20 -static int show_device_domain_translation(struct device *dev, void *data) -{ - struct iommu_group *group; + sts =3D dmar_readl(iommu->reg + DMAR_GSTS_REG); + if (!(sts & DMA_GSTS_TES)) { + seq_printf(m, "DMA Remapping is not enabled on %s\n", + iommu->name); + continue; + } + if (dmar_readq(iommu->reg + DMAR_RTADDR_REG) & DMA_RTADDR_SMT) + scalable =3D true; + else + scalable =3D false; =20 - group =3D iommu_group_get(dev); - if (group) { /* - * The group->mutex is held across the callback, which will - * block calls to iommu_attach/detach_group/device. Hence, + * The iommu->lock is held across the callback, which will + * block calls to domain_attach/domain_detach. Hence, * the domain of the device will not change during traversal. * - * All devices in an iommu group share a single domain, hence - * we only dump the domain of the first device. Even though, - * this code still possibly races with the iommu_unmap() - * interface. This could be solved by RCU-freeing the page - * table pages in the iommu_unmap() path. + * Traversing page table possibly races with the iommu_unmap() + * interface. This could be solved by incrementing the + * reference count of page table page before traversal and + * decrementing the reference count after traversal. */ - iommu_group_for_each_dev(group, data, - __show_device_domain_translation); - iommu_group_put(group); + spin_lock(&iommu->lock); + for (bus =3D s_bus; bus <=3D e_bus; bus++) { + for (devfn =3D s_devfn; devfn <=3D e_devfn; devfn++) { + context =3D iommu_context_addr(iommu, bus, devfn, 0); + if (!context || !context_present(context)) + continue; + + if (!scalable) { /* legacy mode */ + pgd =3D context->lo & VTD_PAGE_MASK; + agaw =3D context->hi & 7; + + seq_printf(m, "Device %04x:%02x:%02x.%x @0x%llx\n", + iommu->segment, bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pgd); + seq_printf(m, "%-17s\t%-18s\t%-18s\t%-18s\t%-18s\t%-s\n", + "IOVA_PFN", "PML5E", "PML4E", "PDPE", "PDE", "PTE"); + pgtable_walk_level(m, phys_to_virt(pgd), agaw + 2, 0, path); + seq_putc(m, '\n'); + + found =3D true; + } else { /* scalable mode */ + struct tbl_walk tbl_wlk =3D {0}; + struct pasid_dir_entry *dir_tbl, *dir_entry; + struct pasid_entry *pasid_tbl, *pasid_tbl_entry; + u16 pasid_dir_size, dir_idx, tbl_idx; + u64 pasid_dir_ptr; + + tbl_wlk.segment =3D iommu->segment; + tbl_wlk.bus =3D bus; + tbl_wlk.devfn =3D devfn; + tbl_wlk.rt_entry =3D &iommu->root_entry[bus]; + tbl_wlk.ctx_entry =3D context; + tbl_wlk.dump_page_table =3D true; + m->private =3D &tbl_wlk; + + pasid_dir_ptr =3D context->lo & VTD_PAGE_MASK; + pasid_dir_size =3D get_pasid_dir_size(context); + + if (walk_tbl) { + pasid_dir_walk(m, pasid_dir_ptr, pasid_dir_size); + continue; + } + + if (sinfo && sinfo->pasid =3D=3D INVALID_IOASID) { + spin_unlock(&iommu->lock); + goto unlock_out; + } + + /* Dump specified device domain mappings with PASID. */ + dir_idx =3D sinfo->pasid >> PASID_PDE_SHIFT; + tbl_idx =3D sinfo->pasid & PASID_PTE_MASK; + + dir_tbl =3D phys_to_virt(pasid_dir_ptr); + dir_entry =3D &dir_tbl[dir_idx]; + + pasid_tbl =3D get_pasid_table_from_pde(dir_entry); + if (!pasid_tbl) + continue; + + pasid_tbl_entry =3D &pasid_tbl[tbl_idx]; + if (!pasid_pte_is_present(pasid_tbl_entry)) + continue; + + tbl_wlk.pasid =3D sinfo->pasid; + tbl_wlk.pasid_tbl_entry =3D pasid_tbl_entry; + dump_translation_page_table(m); + + found =3D true; + } + } + } + + spin_unlock(&iommu->lock); + if (!walk_tbl && found) + break; } =20 +unlock_out: + rcu_read_unlock(); + + if (!walk_tbl && !found && (sinfo->pasid !=3D INVALID_IOASID)) + seq_printf(m, "No mappings found on device %s with pasid %x.\n", + dev_name(&sinfo->pdev->dev), sinfo->pasid); return 0; } =20 static int domain_translation_struct_show(struct seq_file *m, void *unused) { - return bus_for_each_dev(&pci_bus_type, NULL, m, - show_device_domain_translation); + int ret; + + if (show_domain_info && show_domain_info->pdev) { + struct device_domain_info *info =3D + dev_iommu_priv_get(&show_domain_info->pdev->dev); + + if (info) { + /* + * The domain has already exited, and will + * switch to the default domain next. + */ + if (!info->domain) + return 0; + + if (info->pasid_enabled && + (show_domain_info->pasid =3D=3D INVALID_IOASID)) + show_domain_info->pasid =3D PASID_RID2PASID; + else if (!info->pasid_enabled && + (show_domain_info->pasid !=3D INVALID_IOASID)) { + seq_printf(m, "Device %s does not support PASID.\n", + dev_name(&show_domain_info->pdev->dev)); + return 0; + } + } else + show_domain_info->pasid =3D PASID_RID2PASID; + + ret =3D show_device_domain_translation(show_domain_info, m); + } else + ret =3D show_device_domain_translation(NULL, m); + + return ret; } =20 static ssize_t domain_translation_struct_write(struct file *filp, --=20 2.21.3 From nobody Sat Feb 7 19:45:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7FF89C0015E for ; Sun, 25 Jun 2023 15:18:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230503AbjFYPSd (ORCPT ); Sun, 25 Jun 2023 11:18:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230307AbjFYPSN (ORCPT ); Sun, 25 Jun 2023 11:18:13 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 042B51B8 for ; Sun, 25 Jun 2023 08:18:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1687706293; x=1719242293; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NbhV1jUvIvoPeaMjWeaCFCY5V1AWw9RzK0uK6KP563I=; b=S9m44QiBfsasQInK9mKlcQwBofYKrUAYfbXkz07OzxCorx9lKSSc36qR OCdf0guBmUHdEujJDs9GZyhWIcKj2WF469mlEo/nBIfVzOOoW5E3KbJrT dzWpgpEsFU4JAP+NbRS57jQIhda/0mOUweMh4RspgYtODIjpCK4NDbjZ9 hDY8dZaL7u3JQIFBLlbhUtnSTxLiLFjhTeEqZm4HRO9mmw+0303zYFZFb LCt9S94vunDc6mV/gDUMS6jzMFauxT+8xuRBv2lPWgu2kWVC0qHUoTj8X qVtUQcuqvWtQMENbIQMY5AYYUCFzCcqzblxUbLuBW1IJgIPNw2mIjrCKL A==; X-IronPort-AV: E=McAfee;i="6600,9927,10752"; a="391255102" X-IronPort-AV: E=Sophos;i="6.01,157,1684825200"; d="scan'208";a="391255102" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jun 2023 08:18:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10752"; a="840004542" X-IronPort-AV: E=Sophos;i="6.01,157,1684825200"; d="scan'208";a="840004542" Received: from cascade.sh.intel.com ([10.239.48.162]) by orsmga004.jf.intel.com with ESMTP; 25 Jun 2023 08:18:10 -0700 From: Jingqi Liu To: iommu@lists.linux.dev, Lu Baolu , Tian Kevin , Joerg Roedel , Will Deacon , Robin Murphy Cc: linux-kernel@vger.kernel.org, Jingqi Liu Subject: [PATCH 5/5] iommu/vt-d: debugfs: Dump entry pointing to huge page Date: Sun, 25 Jun 2023 23:04:42 +0800 Message-Id: <20230625150442.42197-6-Jingqi.liu@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230625150442.42197-1-Jingqi.liu@intel.com> References: <20230625150442.42197-1-Jingqi.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" For the page table entry pointing to a huge page, the data below the level of the huge page is meaningless and does not need to be dumped. Signed-off-by: Jingqi Liu --- drivers/iommu/intel/debugfs.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/intel/debugfs.c b/drivers/iommu/intel/debugfs.c index e4d3b7836076..f5e26cc21905 100644 --- a/drivers/iommu/intel/debugfs.c +++ b/drivers/iommu/intel/debugfs.c @@ -326,9 +326,14 @@ static inline unsigned long level_to_directory_size(in= t level) static inline void dump_page_info(struct seq_file *m, unsigned long iova, u64 *path) { - seq_printf(m, "0x%013lx |\t0x%016llx\t0x%016llx\t0x%016llx\t0x%016llx\t0x= %016llx\n", - iova >> VTD_PAGE_SHIFT, path[5], path[4], - path[3], path[2], path[1]); + seq_printf(m, "0x%013lx |\t0x%016llx\t0x%016llx\t0x%016llx", + iova >> VTD_PAGE_SHIFT, path[5], path[4], path[3]); + if (path[2]) { + seq_printf(m, "\t0x%016llx", path[2]); + if (path[1]) + seq_printf(m, "\t0x%016llx", path[1]); + } + seq_putc(m, '\n'); } =20 static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde, --=20 2.21.3