From patchwork Wed Sep 27 15:15:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Liu, Jingqi" X-Patchwork-Id: 145444 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2729512vqu; Wed, 27 Sep 2023 09:02:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGnEKdN+jpftl+SfrY2Aw6giUZx8As7AKpgA39p29zqWchiDkoXxQ3gghYrIeQk2XSwdqcS X-Received: by 2002:a05:6a00:9a3:b0:68e:3eab:9e1b with SMTP id u35-20020a056a0009a300b0068e3eab9e1bmr2810728pfg.22.1695830531386; Wed, 27 Sep 2023 09:02:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695830531; cv=none; d=google.com; s=arc-20160816; b=Cump51qp1LScJ/EI0tQZiHR2vh0AzToDrwnc//kFhpDD7IwC7nLfrEKgF3zHRMNcjv wSrRazjQzGLVMgb0T97a/W9c0gsxoLkmxk0BInHi7fTt904rerw2/ERVAFRZjKN6Eut3 rmajg4O3SmI94yNaXt/e9RGSeKJkEPeCARv8p3Ozk8kiXiuy7UkWpAu3m03UqPLMVsZh 78mTm0k3Z9E7xXiyri9q1CVxyAI+24EKy+kYqsN0Am6z2vVyHM+JeHawiCKG2fBO7gy0 wi5aZRcaG2YfYuF7SD0LOmfHNYJ8aEeQOoHsWLJbw3pH7tIYx5dJZ9uVlbl9LfsaUlVh hxUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=imG8JPpgV8r0JPJlJ6B1TjhmgorlrMS8NxH9eZf6xs8=; fh=XFDRxS7z/mPiLWcvDFNXxtez86tw5VSUEAZ1VPulNcQ=; b=GkxTsnpxVzkx8lJoWidR1mPdCp2UbKml4QLaHtBVlCl6xtjh5XbgIgEaslFrelBsbW 1VFwYKy5eCfdbpz6fyeFohW/3V6VmWjp5QnRYN3c4+lYjKAZ9vYvCArCbBhZOWETjQCM /4Ck7a+dxXx1EJE0ob9QzIZ/bnuq6DVOqw4WG1X6zSVCI05bsVCuWiSIUH/2cxBW+3gW 3RW5KBtyQ2wukOoahxa51vkM9CySsiJtU/uuhFsMWKCzOfPu3Ccv0VyH3PSzceQG3KJy xE1ta5peuG7zLohWYGeXz91LtXFEEhqj8oq4sw+UWbcbiIM6IYCTccYjkk9dOuZoOryJ 38pQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N6YDpCLB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id z17-20020aa78891000000b0068fb5cec8cfsi16964034pfe.267.2023.09.27.09.02.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 09:02:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N6YDpCLB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 2FBFF8057E23; Wed, 27 Sep 2023 08:19:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232350AbjI0PTP (ORCPT + 19 others); Wed, 27 Sep 2023 11:19:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232022AbjI0PTO (ORCPT ); Wed, 27 Sep 2023 11:19:14 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E69A136 for ; Wed, 27 Sep 2023 08:19: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=1695827953; x=1727363953; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lpncFO9EBL1O72rF23q9xrrFLTa4Sw3ZpdC9K4pRN7g=; b=N6YDpCLB72dXEHUnTYck8Jy11GsmWtuNCt4V4TRVjU9EjouAdeuGkGIb RTApJS9jookRSpVZkOcb7Q4dgTs7wPiM/GQnOqWx27XZ5ZIT2KlwL5+3B LJnGT8Ews88QLcQePHSmfrsHx/Yc9P24g8foAeiBO/ZjaK53cHvHZcGQU sAKsrbwDgK2fNuoMtuZOj6xChhaOCuuDxTmHT/RsknLwfmRyk0sNA11WC /8SSDDvTATu1VhS1aM7HRseko1mbZ3rC3jsthTR7VLCLE+I552oA5qzr2 ec8h8STe0uhRFRiDm06MHHXctiXi57lNPUHePMRZSN2CpC56sfJC6Bvxw w==; X-IronPort-AV: E=McAfee;i="6600,9927,10846"; a="362098905" X-IronPort-AV: E=Sophos;i="6.03,181,1694761200"; d="scan'208";a="362098905" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2023 08:19:13 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10846"; a="819461120" X-IronPort-AV: E=Sophos;i="6.03,181,1694761200"; d="scan'208";a="819461120" Received: from cascade.sh.intel.com ([10.239.48.35]) by fmsmga004.fm.intel.com with ESMTP; 27 Sep 2023 08:19:11 -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: [PATH v3 1/3] iommu/vt-d: debugfs: Dump entry pointing to huge page Date: Wed, 27 Sep 2023 23:15:34 +0800 Message-Id: <20230927151536.67319-2-Jingqi.liu@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230927151536.67319-1-Jingqi.liu@intel.com> References: <20230927151536.67319-1-Jingqi.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 27 Sep 2023 08:19:19 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778207195173270021 X-GMAIL-MSGID: 1778207195173270021 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 0255c4a23269..e2a3c37943a0 100644 --- a/drivers/iommu/intel/debugfs.c +++ b/drivers/iommu/intel/debugfs.c @@ -311,9 +311,14 @@ static inline unsigned long level_to_directory_size(int 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'); } static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde, From patchwork Wed Sep 27 15:15:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Liu, Jingqi" X-Patchwork-Id: 145492 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2789097vqu; Wed, 27 Sep 2023 10:35:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEHvIBYAEGFpbSiXt1rBCyZ5Jpqg3v+L5ZmhH0/akhku9WDsSa3q1hpJze7sfIF9UnZAJC7 X-Received: by 2002:a17:903:648:b0:1c6:3228:c2ca with SMTP id kh8-20020a170903064800b001c63228c2camr2319817plb.29.1695836124916; Wed, 27 Sep 2023 10:35:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695836124; cv=none; d=google.com; s=arc-20160816; b=NOXO1Z1iRgSG0IylBrXvm/Ph6Hb+/KOSOhKl2AKl6wxRJ5MXqK0Dkfom2Dn+Iyf+p2 mdfXJf+gYHH1SP9pb6mOlIEeirKzW4oTgSiddy8mb9ZjQeB5vws/fZG0L1pOgVNTEMv9 NKkd2SXPTB4V0qHeQX4mD4osyHXMM1pyw03YnwxLmSXaIPWGfKAuWSe0rNxqkpbi+jUh SGY5l5a9h9bl1tmfLj+v6HjBjMRVLP2qQDRE4j0dwhlCmuHiQAu6joTf2rTArWMiX/cc yNqDVjI4H7PCq4dsek+kAz/42C7wynx3FexAs5kf62+6x4lxOfgXRYLbuYVFkafO3OLp 59xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+w1I//W85ODct4CRxf6bGdQhBBdYsoEm0jDUPRWlFus=; fh=XFDRxS7z/mPiLWcvDFNXxtez86tw5VSUEAZ1VPulNcQ=; b=nvnxMtjvU3yGQZwEVXAk6PY0v+b58hA1aXns+HGPK9137OiW6uUfeAbuWxhNz9tXzj t+F0PWIgJWM+ITpXr6w656jlBfdohITOwFKo52TLKPy1ttRxvaNB4vK5wf8hDZEARXyC FoCsJe2urZcxAciXFTx/QtHv9KdM9gIGNRsYQJxxDkk6+Yway7GM3uRXPyoW4Te4rQct TxGXzqD+Qcp1TrMqqWewxhlb5xw9tQUE4rBCXVEWY6TPHdv7sFaHiLYSOogWi2sx4JIZ SxMfGwrWgkAHdlknjDbli1okAfkhbU6RmwGw8X8iRRCCoY1L/ZKVvMkErBfGnB9l5vjp NXtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dACwAvyT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id l12-20020a170903244c00b001bb02e4b739si17716377pls.475.2023.09.27.10.35.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 10:35:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dACwAvyT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 13B5C801CF96; Wed, 27 Sep 2023 08:19:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232399AbjI0PTV (ORCPT + 19 others); Wed, 27 Sep 2023 11:19:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232392AbjI0PTR (ORCPT ); Wed, 27 Sep 2023 11:19:17 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C001C121 for ; Wed, 27 Sep 2023 08:19:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695827955; x=1727363955; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OsrFI+aCDANZLn+fG167H9Cm0xlCT0Xu8vBM5CGocag=; b=dACwAvyT1aqZ4j6/2oaR5vzDXE3wlISaGiyFHBTfni8I4OvfSMXS7RD6 4DWqyOoYGudn9d+y2BhvsDDlrrnQt4lqw0WzSrIcFqBwnT3gxIG2uyteV QjimSNuhUvGNezEakuadZNcD4urce04VXGAvaxotUUfLoIpY1aaIXqLuE iJgrvKFt+XuId9VK8tucilHvhSX/WUSnC8XFV59J3avZwtNZJ+U/DgJeG FPFGZsSmlwLdCMHMZBmtH2psDU9k29k0LtMkRdkFIadMq9oOACqwanfMJ KOF0exufEOt8LDtZAhvDvfOgyFTHZnv6mQvXTU3/xdzGJGCzQn8KvetRw Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10846"; a="362098913" X-IronPort-AV: E=Sophos;i="6.03,181,1694761200"; d="scan'208";a="362098913" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2023 08:19:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10846"; a="819461130" X-IronPort-AV: E=Sophos;i="6.03,181,1694761200"; d="scan'208";a="819461130" Received: from cascade.sh.intel.com ([10.239.48.35]) by fmsmga004.fm.intel.com with ESMTP; 27 Sep 2023 08:19:13 -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: [PATH v3 2/3] iommu/vt-d: debugfs: Create/remove debugfs file per {device, pasid} Date: Wed, 27 Sep 2023 23:15:35 +0800 Message-Id: <20230927151536.67319-3-Jingqi.liu@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230927151536.67319-1-Jingqi.liu@intel.com> References: <20230927151536.67319-1-Jingqi.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 27 Sep 2023 08:19:35 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778213060723023474 X-GMAIL-MSGID: 1778213060723023474 Add a debugfs directory per pair of {device, pasid} if the mappings of its page table are created and destroyed by the iommu_map/unmap() interfaces. i.e. /sys/kernel/debug/iommu/intel//. Create a debugfs file in the directory for users to dump the page table corresponding to {device, pasid}. e.g. /sys/kernel/debug/iommu/intel/0000:00:02.0/0/domain_translation_struct. When attaching device without pasid, create a debugfs file with PASID#0, i.e. RID_PASID. When attaching a domain to a pasid of device, create a debugfs file with the specified pasid. When detaching without pasid, remove the directory and file for PASID#0. When detaching with pasid, remove the debugfs directory and file of the specified pasid. Remove the entire debugfs directory of the specified device for releasing device. e.g. /sys/kernel/debug/iommu/intel/0000:00:01.0 Signed-off-by: Jingqi Liu --- drivers/iommu/intel/debugfs.c | 133 ++++++++++++++++++++++++++++++++-- drivers/iommu/intel/iommu.c | 29 ++++++++ drivers/iommu/intel/iommu.h | 12 +++ 3 files changed, 169 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/intel/debugfs.c b/drivers/iommu/intel/debugfs.c index e2a3c37943a0..50dea53ba87c 100644 --- a/drivers/iommu/intel/debugfs.c +++ b/drivers/iommu/intel/debugfs.c @@ -32,6 +32,11 @@ struct iommu_regset { const char *regs; }; +struct show_domain_info { + struct device *dev; + ioasid_t pasid; +}; + #define DEBUG_BUFFER_SIZE 1024 static char debug_buf[DEBUG_BUFFER_SIZE]; @@ -111,6 +116,8 @@ static const struct iommu_regset iommu_regs_64[] = { IOMMU_REGSET_ENTRY(VCRSP), }; +static struct dentry *intel_iommu_debug; + static int iommu_regset_show(struct seq_file *m, void *unused) { struct dmar_drhd_unit *drhd; @@ -673,16 +680,12 @@ static const struct file_operations dmar_perf_latency_fops = { void __init intel_iommu_debugfs_init(void) { - struct dentry *intel_iommu_debug = debugfs_create_dir("intel", - iommu_debugfs_dir); + intel_iommu_debug = debugfs_create_dir("intel", iommu_debugfs_dir); debugfs_create_file("iommu_regset", 0444, intel_iommu_debug, NULL, &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, - intel_iommu_debug, NULL, - &domain_translation_struct_fops); debugfs_create_file("invalidation_queue", 0444, intel_iommu_debug, NULL, &invalidation_queue_fops); #ifdef CONFIG_IRQ_REMAP @@ -692,3 +695,123 @@ void __init intel_iommu_debugfs_init(void) debugfs_create_file("dmar_perf_latency", 0644, intel_iommu_debug, NULL, &dmar_perf_latency_fops); } + +/* Create a debugfs directory for each device. */ +void intel_iommu_debugfs_create_dev(struct device *dev) +{ + struct dentry *dev_dir; + + dev_dir = debugfs_lookup(dev_name(dev), intel_iommu_debug); + if (!dev_dir) { + dev_dir = debugfs_create_dir(dev_name(dev), intel_iommu_debug); + if (IS_ERR(dev_dir)) + pr_info("%s: Failed to create debugfs directory.\n", + dev_name(dev)); + } else + dput(dev_dir); +} + +void intel_iommu_debugfs_remove_dev(struct device *dev) +{ + struct dentry *dev_dir, *sub_dir, *dentry; + struct list_head *plist; + + dev_dir = debugfs_lookup(dev_name(dev), intel_iommu_debug); + if (!dev_dir) + return; + + list_for_each(plist, &(dev_dir->d_subdirs)) { + sub_dir = list_entry(plist, struct dentry, d_child); + if(sub_dir) { + dentry = debugfs_lookup("domain_translation_struct", + sub_dir); + if (!dentry) + continue; + + if (dentry->d_inode->i_private) + kfree(dentry->d_inode->i_private); + + dput(dentry); + } + } + + debugfs_remove_recursive(dev_dir); + dput(dev_dir); +} + +/* + * Create a debugfs directory per pair of {device, pasid}, + * then create the corresponding debugfs file in this directory + * for user to dump its page table. e.g. + * /sys/kernel/debug/iommu/intel/0000:00:01.0/0/domain_translation_struct + */ +void intel_iommu_debugfs_create_dev_pasid(struct iommu_domain *domain, + struct device *dev, u32 pasid) +{ + struct dentry *dev_dir, *pasid_dir; + struct show_domain_info *sinfo; + char dir_name[10]; + + /* + * The debugfs only dumps the page tables whose mappings are created + * and destroyed by the iommu_map/unmap() interfaces. Check the + * mapping type of the domain before creating debugfs directory. + */ + if (!domain || !(domain->type & __IOMMU_DOMAIN_PAGING)) + return; + + dev_dir = debugfs_lookup(dev_name(dev), intel_iommu_debug); + if (!dev_dir) + return; + + sprintf(dir_name, "%x", pasid); + pasid_dir = debugfs_create_dir(dir_name, dev_dir); + if (IS_ERR(pasid_dir)) + goto dput_out; + + sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL); + if (!sinfo) + goto dput_out; + + sinfo->dev = dev; + sinfo->pasid = pasid; + debugfs_create_file("domain_translation_struct", 0444, + pasid_dir, sinfo, + &domain_translation_struct_fops); +dput_out: + dput(dev_dir); +} + +/* + * Remove the debugfs directory and file corresponding to each pair of + * {device, pasid}. + */ +void intel_iommu_debugfs_remove_dev_pasid(struct device *dev, u32 pasid) +{ + struct dentry *dev_dir, *pasid_dir, *dentry; + char dir_name[10]; + + dev_dir = debugfs_lookup(dev_name(dev), intel_iommu_debug); + if (!dev_dir) + return; + + sprintf(dir_name, "%x", pasid); + pasid_dir = debugfs_lookup(dir_name, dev_dir); + if (!pasid_dir) + goto dput_dev; + + dentry = debugfs_lookup("domain_translation_struct", pasid_dir); + if (!dentry) + goto dput_pasid; + + if (dentry->d_inode->i_private) + kfree(dentry->d_inode->i_private); + + debugfs_remove_recursive(pasid_dir); + + dput(dentry); +dput_pasid: + dput(pasid_dir); +dput_dev: + dput(dev_dir); +} diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index dd8ff358867d..af9c989035a2 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -2488,6 +2488,13 @@ static int dmar_domain_attach_device(struct dmar_domain *domain, iommu_enable_pci_caps(info); + /* + * Create a debugfs directory specified by RID_PASID + * in the debugfs device directory. + */ + intel_iommu_debugfs_create_dev_pasid(&info->domain->domain, + dev, IOMMU_NO_PASID); + return 0; } @@ -3997,6 +4004,12 @@ static void device_block_translation(struct device *dev) domain_detach_iommu(info->domain, iommu); info->domain = NULL; + + /* + * Remove the debugfs directory specified by RID_PASID + * in the device debugfs directory. + */ + intel_iommu_debugfs_remove_dev_pasid(dev, IOMMU_NO_PASID); } static int md_domain_init(struct dmar_domain *domain, int guest_width) @@ -4424,6 +4437,9 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev) } } + /* Create a debugfs directory per device. */ + intel_iommu_debugfs_create_dev(dev); + return &iommu->iommu; } @@ -4436,6 +4452,7 @@ static void intel_iommu_release_device(struct device *dev) dev_iommu_priv_set(dev, NULL); kfree(info); set_dma_ops(dev, NULL); + intel_iommu_debugfs_remove_dev(dev); } static void intel_iommu_probe_finalize(struct device *dev) @@ -4729,6 +4746,12 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid) out_tear_down: intel_pasid_tear_down_entry(iommu, dev, pasid, false); intel_drain_pasid_prq(dev, pasid); + + /* + * Remove the debugfs directory of the specified pasid + * in the device debugfs directory. + */ + intel_iommu_debugfs_remove_dev_pasid(dev, pasid); } static int intel_iommu_set_dev_pasid(struct iommu_domain *domain, @@ -4777,6 +4800,12 @@ static int intel_iommu_set_dev_pasid(struct iommu_domain *domain, list_add(&dev_pasid->link_domain, &dmar_domain->dev_pasids); spin_unlock_irqrestore(&dmar_domain->lock, flags); + /* + * Create a debugfs directory with specified pasid + * in the debugfs device directory. + */ + intel_iommu_debugfs_create_dev_pasid(domain, dev, pasid); + return 0; out_detach_iommu: domain_detach_iommu(dmar_domain, iommu); diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index c18fb699c87a..f1c27e928886 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -883,8 +883,20 @@ static inline void intel_svm_remove_dev_pasid(struct device *dev, ioasid_t pasid #ifdef CONFIG_INTEL_IOMMU_DEBUGFS void intel_iommu_debugfs_init(void); +void intel_iommu_debugfs_create_dev(struct device *dev); +void intel_iommu_debugfs_create_dev_pasid(struct iommu_domain *domain, + struct device *dev, u32 pasid); +void intel_iommu_debugfs_remove_dev(struct device *dev); +void intel_iommu_debugfs_remove_dev_pasid(struct device *dev, u32 pasid); #else static inline void intel_iommu_debugfs_init(void) {} +static inline void intel_iommu_debugfs_create_dev(struct device *dev) {} +static inline +void intel_iommu_debugfs_create_dev_pasid(struct iommu_domain *domain, + struct device *dev, u32 pasid) {} +static inline void intel_iommu_debugfs_remove_dev(struct device *dev) {} +static inline +void intel_iommu_debugfs_remove_dev_pasid(struct device *dev, u32 pasid) {} #endif /* CONFIG_INTEL_IOMMU_DEBUGFS */ extern const struct attribute_group *intel_iommu_groups[]; From patchwork Wed Sep 27 15:15:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Liu, Jingqi" X-Patchwork-Id: 145495 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2790178vqu; Wed, 27 Sep 2023 10:37:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE/KFLza9duxBR7t3sLBIb2C2mpW8KJhdZHKv3jWpezBSViFFwnzIAPDDzlffX3lniiCaUK X-Received: by 2002:a05:6a20:4403:b0:15d:e68d:a855 with SMTP id ce3-20020a056a20440300b0015de68da855mr3065251pzb.29.1695836246365; Wed, 27 Sep 2023 10:37:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695836246; cv=none; d=google.com; s=arc-20160816; b=GeKkwjjQoHEvE8VqE6lRFnvEuZJCKii23clsoG3ZCLN9Uc7cWC+8ECIWxEHxIuvyKp vxu7z+aX8YgFwcgL/edb1Kouakw1KcPj6SxwogCgL4o3WVpXEWMXrSgpE5oySv8wZuZC EwxIMoC7D9nP4yaCo2gWHZhqtqJ2XKOBHGtORI92hlccUjg/+V6NFkAaBz2IlZnHZITh CSuWYXlj5/Z3lK0ThbFu6rFdcJkdk2NENfOAbn3Sdmq+mTruNHPIPl/YG1AnV07fseiN iufENPS0351nebU82QaEoLheNNMFeqZ6Nte/4mc9qaARe6pJmnY9DNh9tUui915cRS7e YnVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jEuAW+wD2uJJ5/Q7yCOXZOLaK84KfpORRt82GWF7PVA=; fh=XFDRxS7z/mPiLWcvDFNXxtez86tw5VSUEAZ1VPulNcQ=; b=F/az1mEIZKk6wjVqYi1jfWhHbVA6hAV8aGtpsLcU0yixMmfAc+uYwoqdVqriCW4mh5 e8sgV/Iaul9wG/3zNoULigVbYw8FPaoyDoP1OPYIOkgRSVxRw54Er0dL2GGwl2jswby1 z0Cwm5wxdbcrTokD1vgp6NQQAdy46Q4BoXMKd3nkvE8dN2CDlqMMwkT9YxhMVz4YduMK ansQzBJuJFY78OXQFfczPykFerzhjXDXm2S6OegHME1g5t61uyD5Ed4FQ7qH0AsbFBWw bOWASVFFXmXvN6l1IYnoOYfbs9abjm+zp7ajYlJNP4vy880V1MpV3qcfe/abGOpv5aTC Yyfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nq1Jx8Kf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id g16-20020a631110000000b00573f7ef60aesi16374321pgl.154.2023.09.27.10.37.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 10:37:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nq1Jx8Kf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 02916801CF95; Wed, 27 Sep 2023 08:19:58 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232416AbjI0PTX (ORCPT + 19 others); Wed, 27 Sep 2023 11:19:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232396AbjI0PTT (ORCPT ); Wed, 27 Sep 2023 11:19:19 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09632126 for ; Wed, 27 Sep 2023 08:19:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695827958; x=1727363958; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZpwehE9umF+E2WRJZPjrRmVCii85Hu05XDSIsfB4XV0=; b=nq1Jx8Kfy6eRKmoQSkgTOFDmN1RS+Q2ushdmvF4r7QsbGRzMb5//r+c1 NI43EXXxXALuvyXYLxn1Mg8qhjfTz2GQob9qL8zKNZZui9tA86mkIsAxk GiMXZPIv18mSiosyLRg7zMv+kVCJYq2AjKg5c2p5+evJkYVaV+yeaNJNe Q/zJ+tGEUnzR9DGyw9u1Rkx14lwK/FtHKeL31ruUZS4CDh9Yyvo2do2qJ V8U0SRttOeDyF2G8bKnpjAmSUyq+Fr52BpGLh5oIchSiT7LB7cmmNV+9y 2ff3NCP4eaCuEQ8e0RxvB+vZBgWmOD/A5ffFtGuYTffEyKvEWXy97z/r6 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10846"; a="362098924" X-IronPort-AV: E=Sophos;i="6.03,181,1694761200"; d="scan'208";a="362098924" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Sep 2023 08:19:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10846"; a="819461136" X-IronPort-AV: E=Sophos;i="6.03,181,1694761200"; d="scan'208";a="819461136" Received: from cascade.sh.intel.com ([10.239.48.35]) by fmsmga004.fm.intel.com with ESMTP; 27 Sep 2023 08:19:15 -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: [PATH v3 3/3] iommu/vt-d: debugfs: Support dumping a specified page table Date: Wed, 27 Sep 2023 23:15:36 +0800 Message-Id: <20230927151536.67319-4-Jingqi.liu@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230927151536.67319-1-Jingqi.liu@intel.com> References: <20230927151536.67319-1-Jingqi.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 27 Sep 2023 08:19:58 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778213187838391208 X-GMAIL-MSGID: 1778213187838391208 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 in legacy mode or scalable mode with or without a specified pasid. 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 specified 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 the specified page table.. Examples are as follows: 1) Dump the page table of device "0000:00:1f.0" that only supports legacy mode. $ sudo cat /sys/kernel/debug/iommu/intel/0000:00:1f.0/0/domain_translation_struct 2) Dump the page table of device "0000:00:0a.0" with PASID "1" that supports scalable mode. $ sudo cat /sys/kernel/debug/iommu/intel/0000:00:0a.0/1/domain_translation_struct Suggested-by: Kevin Tian Signed-off-by: Jingqi Liu --- drivers/iommu/intel/debugfs.c | 163 +++++++++++++++++++++++++--------- 1 file changed, 121 insertions(+), 42 deletions(-) diff --git a/drivers/iommu/intel/debugfs.c b/drivers/iommu/intel/debugfs.c index 50dea53ba87c..a0e7f2b4d4d1 100644 --- a/drivers/iommu/intel/debugfs.c +++ b/drivers/iommu/intel/debugfs.c @@ -352,58 +352,137 @@ static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde, } } -static int __show_device_domain_translation(struct device *dev, void *data) +static int domain_translation_struct_show(struct seq_file *m, void *unused) { - struct dmar_domain *domain; - struct seq_file *m = data; - u64 path[6] = { 0 }; + struct device_domain_info *info; + struct show_domain_info *sinfo; + bool scalable, found = false; + struct dmar_drhd_unit *drhd; + struct intel_iommu *iommu; + u16 devfn, bus, seg; - domain = to_dmar_domain(iommu_get_domain_for_dev(dev)); - if (!domain) - return 0; + if (!m || !m->private) { + seq_puts(m, "Invalid device or pasid!\n"); + return -EINVAL; + } - 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"); + sinfo = (struct show_domain_info*)m->private; + if (!sinfo->dev || + !dev_iommu_priv_get(sinfo->dev) || + (sinfo->pasid == IOMMU_PASID_INVALID)) { + seq_puts(m, "Please specify device or pasid!\n"); + return -ENODEV; + } - pgtable_walk_level(m, domain->pgd, domain->agaw + 2, 0, path); - seq_putc(m, '\n'); + info = dev_iommu_priv_get(sinfo->dev); + bus = info->bus; + devfn = info->devfn; + seg = info->segment; - /* Don't iterate */ - return 1; -} + rcu_read_lock(); + for_each_active_iommu(iommu, drhd) { + struct context_entry *context; + u64 pgd, path[6] = { 0 }; + u32 sts, agaw; -static int show_device_domain_translation(struct device *dev, void *data) -{ - struct iommu_group *group; + if (seg != iommu->segment) + continue; - device_lock(dev); - group = iommu_group_get(dev); - device_unlock(dev); - if (!group) - return 0; + sts = 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 = true; + else + scalable = false; - /* - * The group->mutex is held across the callback, which will - * block calls to iommu_attach/detach_group/device. 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. - */ - iommu_group_for_each_dev(group, data, __show_device_domain_translation); - iommu_group_put(group); + /* + * 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. + * + * Traversing page table possibly races with the iommu_unmap() + * interface. This could be solved by RCU-freeing the page + * table pages in the iommu_unmap() path. + */ + spin_lock(&iommu->lock); - return 0; -} + context = iommu_context_addr(iommu, bus, devfn, 0); + if (!context || !context_present(context)) + goto iommu_unlock; -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); + if (scalable) { /* scalable mode */ + struct pasid_dir_entry *dir_tbl, *dir_entry; + struct pasid_entry *pasid_tbl, *pasid_tbl_entry; + u16 pasid_dir_size, dir_idx, tbl_idx, pgtt; + u64 pasid_dir_ptr; + + pasid_dir_ptr = context->lo & VTD_PAGE_MASK; + pasid_dir_size = get_pasid_dir_size(context); + + /* Dump specified device domain mappings with PASID. */ + dir_idx = sinfo->pasid >> PASID_PDE_SHIFT; + tbl_idx = sinfo->pasid & PASID_PTE_MASK; + + dir_tbl = phys_to_virt(pasid_dir_ptr); + dir_entry = &dir_tbl[dir_idx]; + + pasid_tbl = get_pasid_table_from_pde(dir_entry); + if (!pasid_tbl) + goto iommu_unlock; + + pasid_tbl_entry = &pasid_tbl[tbl_idx]; + if (!pasid_pte_is_present(pasid_tbl_entry)) + goto iommu_unlock; + + /* + * According to PASID Granular Translation Type(PGTT), + * get the page table pointer. + */ + pgtt = (u16)(pasid_tbl_entry->val[0] & GENMASK_ULL(8, 6)) >> 6; + agaw = (u8)(pasid_tbl_entry->val[0] & GENMASK_ULL(4, 2)) >> 2; + + switch (pgtt) { + case PASID_ENTRY_PGTT_FL_ONLY: + pgd = pasid_tbl_entry->val[2]; + break; + case PASID_ENTRY_PGTT_SL_ONLY: + case PASID_ENTRY_PGTT_NESTED: + pgd = pasid_tbl_entry->val[0]; + break; + default: + goto iommu_unlock; + } + pgd &= VTD_PAGE_MASK; + } else { /* legacy mode */ + pgd = context->lo & VTD_PAGE_MASK; + agaw = context->hi & 7; + } + + seq_printf(m, "Device %04x:%02x:%02x.%x ", + iommu->segment, bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); + + if (scalable) + seq_printf(m, "with pasid %x @0x%llx\n", sinfo->pasid, pgd); + else + seq_printf(m, "@0x%llx\n", 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); + + found = true; +iommu_unlock: + spin_unlock(&iommu->lock); + if (found) + break; + } + rcu_read_unlock(); + + return 0; } DEFINE_SHOW_ATTRIBUTE(domain_translation_struct);