From nobody Sat Oct 4 17:31:10 2025 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AAF032FABFB for ; Thu, 14 Aug 2025 09:30:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.191 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755163814; cv=none; b=Rsv4BYhGFlH5RHMj5KF0ClWvmdadN3wWEqziG3+tT72XW1lA5D/kKt6xmtCxB/r8s/jWQy2zCzoXzEkGGJ/Tth2T7pteHGW1UK2gulJFg5zSDHQOyeAo4x0piUzUhzxyzjoAHhyPLtHk+Fv71vZb4HzHTyztdDFLO1CLcIje+RM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755163814; c=relaxed/simple; bh=umu6p1Xnjv7Eb71dtdWTGaIJkMemCtt+dX+UMOVtnDo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OIaF76aOGXRDhoMIxQTFu0+YTOGVzg23tZiGJjOAoLy5TG8deNWgMYmXti8GQNByyW3n4R/564d4Yb53kdp60ZoV9e6cxJZNWxrZFW2GDHsaYUMFIzVIEFQGi4RkSFbehr1NR5i3EGGFykFTQ5JMBmvvQNaViTU80W4qqpVTrmc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.191 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4c2fyv6dQyz2gKxY; Thu, 14 Aug 2025 17:27:19 +0800 (CST) Received: from kwepemj200003.china.huawei.com (unknown [7.202.194.15]) by mail.maildlp.com (Postfix) with ESMTPS id 52E1D140296; Thu, 14 Aug 2025 17:30:10 +0800 (CST) Received: from localhost.huawei.com (10.90.31.46) by kwepemj200003.china.huawei.com (7.202.194.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Thu, 14 Aug 2025 17:30:09 +0800 From: Qinxin Xia To: , , CC: , , , , , , , , , Subject: [PATCH 2/2] iommu/io-pgtable: Add ARM SMMUv3 page table dump support Date: Thu, 14 Aug 2025 17:30:05 +0800 Message-ID: <20250814093005.2040511-3-xiaqinxin@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250814093005.2040511-1-xiaqinxin@huawei.com> References: <20250814093005.2040511-1-xiaqinxin@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems500001.china.huawei.com (7.221.188.70) To kwepemj200003.china.huawei.com (7.202.194.15) Content-Type: text/plain; charset="utf-8" This patch implements the debug interface for dumping ARM SMMUv3 page table entries and protection information. The functionality enables detailed inspection of IOMMU mappings for debugging and validation purposes. 1. ARM SMMUv3 driver integration: - Implemented arm_smmu_dump_iova_prot() callback - Registered dump_iova_prot in iommu_domain_ops 2. ARM LPAE io-pgtable implementation: - Added arm_lpae_dump_iova_prot() to io_pgtable_ops - Defined protection bit descriptors (prot_bits) - Created io_pgtable_fmt_names for human-readable format strings - Implemented protection flag formatting in dump_prot() 3. Core io-pgtable interface extension: - Added dump_iova_prot callback to io_pgtable_ops The implementation provides detailed output for each IOVA mapping: - lvl stage Example: 0xffff2000 - 0xffff3000 lvl 2 stage ARM_64_LPAE_S1 AF SH_NS Signed-off-by: Qinxin Xia --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 13 ++ drivers/iommu/io-pgtable-arm.c | 169 ++++++++++++++++++++ include/linux/io-pgtable.h | 4 + 3 files changed, 186 insertions(+) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/ar= m/arm-smmu-v3/arm-smmu-v3.c index 5968043ac802..4128b3307753 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3411,6 +3411,16 @@ arm_smmu_iova_to_phys(struct iommu_domain *domain, d= ma_addr_t iova) return ops->iova_to_phys(ops, iova); } =20 +#ifdef CONFIG_IO_PTDUMP +static size_t +arm_smmu_dump_iova_prot(struct seq_file *s, struct iommu_domain *domain, d= ma_addr_t iova) +{ + struct io_pgtable_ops *ops =3D to_smmu_domain(domain)->pgtbl_ops; + + return ops->dump_iova_prot(s, ops, iova); +} +#endif + static struct platform_driver arm_smmu_driver; =20 static @@ -3702,6 +3712,9 @@ static const struct iommu_ops arm_smmu_ops =3D { .flush_iotlb_all =3D arm_smmu_flush_iotlb_all, .iotlb_sync =3D arm_smmu_iotlb_sync, .iova_to_phys =3D arm_smmu_iova_to_phys, +#ifdef CONFIG_IO_PTDUMP + .dump_iova_prot =3D arm_smmu_dump_iova_prot, +#endif .free =3D arm_smmu_domain_free_paging, } }; diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c index 7e8e2216c294..5deb03a85aa6 100644 --- a/drivers/iommu/io-pgtable-arm.c +++ b/drivers/iommu/io-pgtable-arm.c @@ -754,6 +754,172 @@ static phys_addr_t arm_lpae_iova_to_phys(struct io_pg= table_ops *ops, return iopte_to_paddr(d.pte, data) | iova; } =20 +#ifdef CONFIG_IO_PTDUMP +#include + +struct io_ptdump_prot_bits { + uint64_t mask; + uint64_t val; + const char *set; + const char *clear; +}; + +static const struct io_ptdump_prot_bits prot_bits[] =3D { + { + .mask =3D ARM_LPAE_PTE_VALID, + .val =3D ARM_LPAE_PTE_VALID, + .set =3D "V", + .clear =3D " ", + }, + { + .mask =3D ARM_LPAE_PTE_XN, + .val =3D ARM_LPAE_PTE_XN, + .set =3D "XN", + .clear =3D " ", + }, + { + .mask =3D ARM_LPAE_PTE_DBM, + .val =3D ARM_LPAE_PTE_DBM, + .set =3D "DBM", + .clear =3D " ", + }, + { + .mask =3D ARM_LPAE_PTE_AF, + .val =3D ARM_LPAE_PTE_AF, + .set =3D "AF", + .clear =3D " ", + }, + { + .mask =3D ARM_LPAE_PTE_SH_NS, + .val =3D ARM_LPAE_PTE_SH_NS, + .set =3D "SH_NS", + .clear =3D " ", + }, + { + .mask =3D ARM_LPAE_PTE_SH_OS, + .val =3D ARM_LPAE_PTE_SH_OS, + .set =3D "SH_OS", + .clear =3D " ", + }, + { + .mask =3D ARM_LPAE_PTE_SH_IS, + .val =3D ARM_LPAE_PTE_SH_IS, + .set =3D "SH_IS", + .clear =3D " ", + }, + { + .mask =3D ARM_LPAE_PTE_NS, + .val =3D ARM_LPAE_PTE_NS, + .set =3D "NS", + .clear =3D " ", + }, + { + .mask =3D ARM_LPAE_PTE_NSTABLE, + .val =3D ARM_LPAE_PTE_NSTABLE, + .set =3D "NST", + .clear =3D " ", + }, +}; + +const char *io_pgtable_fmt_names[] =3D { + "ARM_32_LPAE_S1", + "ARM_32_LPAE_S2", + "ARM_64_LPAE_S1", + "ARM_64_LPAE_S2", + "ARM_V7S", + "ARM_MALI_LPAE", + "AMD_IOMMU_V1", + "AMD_IOMMU_V2", + "APPLE_DART", + "APPLE_DART2", +}; + +struct io_ptdump_prot { + int lvl; + int stage; + char *attr; + size_t size; +}; + +static void dump_prot(struct seq_file *s, arm_lpae_iopte pte) +{ + int capacity =3D 64; + const char *attr; + int length =3D 0; + + char *prot =3D kzalloc(capacity * sizeof(char), GFP_KERNEL); + + if (!prot) + return; + + /* Traverse all predefined permission bits */ + for (size_t i =3D 0; i < ARRAY_SIZE(prot_bits); i++) { + if ((pte & prot_bits[i].mask) =3D=3D prot_bits[i].val) + attr =3D prot_bits[i].set; + else + attr =3D prot_bits[i].clear; + + size_t attr_len =3D strlen(attr); + + /* Check and extend the buffer */ + while (length + attr_len > capacity) { + capacity *=3D 2; + + char *temp =3D krealloc(prot, capacity * sizeof(char), GFP_KERNEL); + + if (!temp) { + kfree(prot); + return; + } + + prot =3D temp; + } + + length +=3D snprintf(prot + length, capacity - length, "%s ", attr); + + /* Security check: prevents abnormal buffer expansion */ + if (length > PAGE_SIZE) { + pr_err("len =3D %zu, attr =3D %s, i =3D%d\n", length, attr, i); + kfree(prot); + return; + } + } + + seq_printf(s, "%s", prot); + kfree(prot); +} + +static size_t arm_lpae_dump_iova_prot(struct seq_file *s, struct io_pgtabl= e_ops *ops, + unsigned long iova) +{ + struct arm_lpae_io_pgtable *data =3D io_pgtable_ops_to_data(ops); + struct iova_to_phys_data d; + int ret; + + struct io_pgtable_walk_data walk_data =3D { + .data =3D &d, + .visit =3D visit_iova_to_phys, + .addr =3D iova, + .end =3D iova + 1, + }; + + /* Only the mapped iova will be output */ + ret =3D __arm_lpae_iopte_walk(data, &walk_data, data->pgd, data->start_le= vel); + if (ret) + return ARM_LPAE_GRANULE(data); + + seq_printf(s, "%lx - %lx lvl %d stage %s ", + iova, iova + ARM_LPAE_BLOCK_SIZE(d.lvl, data), + d.lvl, io_pgtable_fmt_names[data->iop.fmt]); + + /* TODO: The dump prot is incomplete and will be supplemented later... */ + dump_prot(s, d.pte); + seq_puts(s, "\n"); + + return ARM_LPAE_BLOCK_SIZE(d.lvl, data); +} +#endif + static int visit_pgtable_walk(struct io_pgtable_walk_data *walk_data, int = lvl, arm_lpae_iopte *ptep, size_t size) { @@ -950,6 +1116,9 @@ arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) .map_pages =3D arm_lpae_map_pages, .unmap_pages =3D arm_lpae_unmap_pages, .iova_to_phys =3D arm_lpae_iova_to_phys, +#ifdef CONFIG_IO_PTDUMP + .dump_iova_prot =3D arm_lpae_dump_iova_prot, +#endif .read_and_clear_dirty =3D arm_lpae_read_and_clear_dirty, .pgtable_walk =3D arm_lpae_pgtable_walk, }; diff --git a/include/linux/io-pgtable.h b/include/linux/io-pgtable.h index 138fbd89b1e6..307c68f0038c 100644 --- a/include/linux/io-pgtable.h +++ b/include/linux/io-pgtable.h @@ -217,6 +217,10 @@ struct io_pgtable_ops { struct iommu_iotlb_gather *gather); phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops, unsigned long iova); +#ifdef CONFIG_IO_PTDUMP + size_t (*dump_iova_prot)(struct seq_file *s, struct io_pgtable_ops *ops, + unsigned long iova); +#endif int (*pgtable_walk)(struct io_pgtable_ops *ops, unsigned long iova, void = *wd); int (*read_and_clear_dirty)(struct io_pgtable_ops *ops, unsigned long iova, size_t size, --=20 2.33.0