From nobody Sun Sep 14 06:39:33 2025 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 CB0C9C05027 for ; Thu, 26 Jan 2023 18:27:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232114AbjAZS1D (ORCPT ); Thu, 26 Jan 2023 13:27:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232127AbjAZS0v (ORCPT ); Thu, 26 Jan 2023 13:26:51 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4DA1A6D347 for ; Thu, 26 Jan 2023 10:26:41 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C36E1152B; Thu, 26 Jan 2023 10:27:22 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id D28483F71E; Thu, 26 Jan 2023 10:26:39 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de, jgg@nvidia.com, baolu.lu@linux.intel.com Subject: [PATCH v2 8/8] iommu: Clean up open-coded ownership checks Date: Thu, 26 Jan 2023 18:26:23 +0000 Message-Id: X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: 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" Some drivers already implement their own defence against the possibility of being given someone else's device. Since this is now taken care of by the core code (and via a slightly different path from the original fwspec-based idea), let's clean them up. Signed-off-by: Robin Murphy Reviewed-by: Jason Gunthorpe --- v2: No change, but I'll note here that it's really about the fwspec->ops checks; the !fwspec clauses are just going along for the ride where that's clearly impossible as well. I plan to sweep for other redundant checks in future when looking at the relevant flows. drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 3 --- drivers/iommu/arm/arm-smmu/arm-smmu.c | 9 +-------- drivers/iommu/arm/arm-smmu/qcom_iommu.c | 14 ++------------ drivers/iommu/mtk_iommu.c | 7 +------ drivers/iommu/mtk_iommu_v1.c | 3 --- drivers/iommu/sprd-iommu.c | 8 +------- drivers/iommu/virtio-iommu.c | 3 --- 7 files changed, 5 insertions(+), 42 deletions(-) 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 ab160198edd6..cb05d9771192 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2642,9 +2642,6 @@ static struct iommu_device *arm_smmu_probe_device(str= uct device *dev) struct arm_smmu_master *master; struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); =20 - if (!fwspec || fwspec->ops !=3D &arm_smmu_ops) - return ERR_PTR(-ENODEV); - if (WARN_ON_ONCE(dev_iommu_priv_get(dev))) return ERR_PTR(-EBUSY); =20 diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-= smmu/arm-smmu.c index 607f06af01b6..235550db0d59 100644 --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c @@ -1118,11 +1118,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *= domain, struct device *dev) struct arm_smmu_device *smmu; int ret; =20 - if (!fwspec || fwspec->ops !=3D &arm_smmu_ops) { - dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n"); - return -ENXIO; - } - /* * FIXME: The arch/arm DMA API code tries to attach devices to its own * domains between of_xlate() and probe_device() - we have no way to cope @@ -1352,10 +1347,8 @@ static struct iommu_device *arm_smmu_probe_device(st= ruct device *dev) fwspec =3D dev_iommu_fwspec_get(dev); if (ret) goto out_free; - } else if (fwspec && fwspec->ops =3D=3D &arm_smmu_ops) { - smmu =3D arm_smmu_get_by_fwnode(fwspec->iommu_fwnode); } else { - return ERR_PTR(-ENODEV); + smmu =3D arm_smmu_get_by_fwnode(fwspec->iommu_fwnode); } =20 ret =3D -EINVAL; diff --git a/drivers/iommu/arm/arm-smmu/qcom_iommu.c b/drivers/iommu/arm/ar= m-smmu/qcom_iommu.c index d7be3adee426..3fe02720ab29 100644 --- a/drivers/iommu/arm/arm-smmu/qcom_iommu.c +++ b/drivers/iommu/arm/arm-smmu/qcom_iommu.c @@ -79,16 +79,6 @@ static struct qcom_iommu_domain *to_qcom_iommu_domain(st= ruct iommu_domain *dom) =20 static const struct iommu_ops qcom_iommu_ops; =20 -static struct qcom_iommu_dev * to_iommu(struct device *dev) -{ - struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); - - if (!fwspec || fwspec->ops !=3D &qcom_iommu_ops) - return NULL; - - return dev_iommu_priv_get(dev); -} - static struct qcom_iommu_ctx * to_ctx(struct qcom_iommu_domain *d, unsigne= d asid) { struct qcom_iommu_dev *qcom_iommu =3D d->iommu; @@ -361,7 +351,7 @@ static void qcom_iommu_domain_free(struct iommu_domain = *domain) =20 static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct devic= e *dev) { - struct qcom_iommu_dev *qcom_iommu =3D to_iommu(dev); + struct qcom_iommu_dev *qcom_iommu =3D dev_iommu_priv_get(dev); struct qcom_iommu_domain *qcom_domain =3D to_qcom_iommu_domain(domain); int ret; =20 @@ -486,7 +476,7 @@ static bool qcom_iommu_capable(struct device *dev, enum= iommu_cap cap) =20 static struct iommu_device *qcom_iommu_probe_device(struct device *dev) { - struct qcom_iommu_dev *qcom_iommu =3D to_iommu(dev); + struct qcom_iommu_dev *qcom_iommu =3D dev_iommu_priv_get(dev); struct device_link *link; =20 if (!qcom_iommu) diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c index d5a4955910ff..ce19a2a21e2a 100644 --- a/drivers/iommu/mtk_iommu.c +++ b/drivers/iommu/mtk_iommu.c @@ -776,16 +776,11 @@ static phys_addr_t mtk_iommu_iova_to_phys(struct iomm= u_domain *domain, static struct iommu_device *mtk_iommu_probe_device(struct device *dev) { struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); - struct mtk_iommu_data *data; + struct mtk_iommu_data *data =3D dev_iommu_priv_get(dev); struct device_link *link; struct device *larbdev; unsigned int larbid, larbidx, i; =20 - if (!fwspec || fwspec->ops !=3D &mtk_iommu_ops) - return ERR_PTR(-ENODEV); /* Not a iommu client device */ - - data =3D dev_iommu_priv_get(dev); - if (!MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) return &data->iommu; =20 diff --git a/drivers/iommu/mtk_iommu_v1.c b/drivers/iommu/mtk_iommu_v1.c index 78d0a84c704f..328833823181 100644 --- a/drivers/iommu/mtk_iommu_v1.c +++ b/drivers/iommu/mtk_iommu_v1.c @@ -478,9 +478,6 @@ static struct iommu_device *mtk_iommu_v1_probe_device(s= truct device *dev) idx++; } =20 - if (!fwspec || fwspec->ops !=3D &mtk_iommu_v1_ops) - return ERR_PTR(-ENODEV); /* Not a iommu client device */ - data =3D dev_iommu_priv_get(dev); =20 /* Link the consumer device with the smi-larb device(supplier) */ diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c index ae94d74b73f4..85378f7cfd1f 100644 --- a/drivers/iommu/sprd-iommu.c +++ b/drivers/iommu/sprd-iommu.c @@ -358,13 +358,7 @@ static phys_addr_t sprd_iommu_iova_to_phys(struct iomm= u_domain *domain, =20 static struct iommu_device *sprd_iommu_probe_device(struct device *dev) { - struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); - struct sprd_iommu_device *sdev; - - if (!fwspec || fwspec->ops !=3D &sprd_iommu_ops) - return ERR_PTR(-ENODEV); - - sdev =3D dev_iommu_priv_get(dev); + struct sprd_iommu_device *sdev =3D dev_iommu_priv_get(dev); =20 return &sdev->iommu; } diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index 5b8fe9bfa9a5..59f1abd6ee53 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -945,9 +945,6 @@ static struct iommu_device *viommu_probe_device(struct = device *dev) struct viommu_dev *viommu =3D NULL; struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); =20 - if (!fwspec || fwspec->ops !=3D &viommu_ops) - return ERR_PTR(-ENODEV); - viommu =3D viommu_get_by_fwnode(fwspec->iommu_fwnode); if (!viommu) return ERR_PTR(-ENODEV); --=20 2.36.1.dirty