From nobody Thu Feb 12 09:30:20 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 CD29AEED621 for ; Fri, 15 Sep 2023 16:59:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235291AbjIOQ6s (ORCPT ); Fri, 15 Sep 2023 12:58:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234662AbjIOQ6g (ORCPT ); Fri, 15 Sep 2023 12:58:36 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 386F819BC for ; Fri, 15 Sep 2023 09:58:30 -0700 (PDT) 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 3C1FB139F; Fri, 15 Sep 2023 09:59:07 -0700 (PDT) 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 DCAF13F5A1; Fri, 15 Sep 2023 09:58:28 -0700 (PDT) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, jgg@nvidia.com, baolu.lu@linux.intel.com Subject: [PATCH v3 4/7] iommu: Switch __iommu_domain_alloc() to device ops Date: Fri, 15 Sep 2023 17:58:08 +0100 Message-Id: <404d8395cf4252c6fe6d98f317f3570127451778.1694693889.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.39.2.101.g768bb238c484.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" In all the places we allocate default domains, we have (or can easily get hold of) a device from which to resolve the right IOMMU ops; only the public iommu_domain_alloc() interface actually depends on bus ops. Reworking the public API is a big enough mission in its own right, but in the meantime we can still decouple it from bus ops internally to move forward. Reviewed-by: Lu Baolu Signed-off-by: Robin Murphy --- v3: Make sure blocking domains are covered as well --- drivers/iommu/iommu.c | 50 +++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index f4cc91227b22..29ebb4b57df4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -96,7 +96,7 @@ static const char * const iommu_group_resv_type_string[] = =3D { static int iommu_bus_notifier(struct notifier_block *nb, unsigned long action, void *data); static void iommu_release_device(struct device *dev); -static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bu= s, +static struct iommu_domain *__iommu_domain_alloc(struct device *dev, unsigned type); static int __iommu_attach_device(struct iommu_domain *domain, struct device *dev); @@ -1745,12 +1745,11 @@ static int iommu_get_def_domain_type(struct device = *dev) } =20 static struct iommu_domain * -__iommu_group_alloc_default_domain(const struct bus_type *bus, - struct iommu_group *group, int req_type) +__iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) { if (group->default_domain && group->default_domain->type =3D=3D req_type) return group->default_domain; - return __iommu_domain_alloc(bus, req_type); + return __iommu_domain_alloc(iommu_group_first_dev(group), req_type); } =20 /* @@ -1760,23 +1759,22 @@ __iommu_group_alloc_default_domain(const struct bus= _type *bus, static struct iommu_domain * iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) { - const struct bus_type *bus =3D iommu_group_first_dev(group)->bus; struct iommu_domain *dom; =20 lockdep_assert_held(&group->mutex); =20 if (req_type) - return __iommu_group_alloc_default_domain(bus, group, req_type); + return __iommu_group_alloc_default_domain(group, req_type); =20 /* The driver gave no guidance on what type to use, try the default */ - dom =3D __iommu_group_alloc_default_domain(bus, group, iommu_def_domain_t= ype); + dom =3D __iommu_group_alloc_default_domain(group, iommu_def_domain_type); if (dom) return dom; =20 /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */ if (iommu_def_domain_type =3D=3D IOMMU_DOMAIN_DMA) return NULL; - dom =3D __iommu_group_alloc_default_domain(bus, group, IOMMU_DOMAIN_DMA); + dom =3D __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA); if (!dom) return NULL; =20 @@ -1997,16 +1995,13 @@ void iommu_set_fault_handler(struct iommu_domain *d= omain, } EXPORT_SYMBOL_GPL(iommu_set_fault_handler); =20 -static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bu= s, +static struct iommu_domain *__iommu_domain_alloc(struct device *dev, unsigned type) { - const struct iommu_ops *ops =3D bus ? bus->iommu_ops : NULL; + const struct iommu_ops *ops =3D dev_iommu_ops(dev); struct iommu_domain *domain; unsigned int alloc_type =3D type & IOMMU_DOMAIN_ALLOC_FLAGS; =20 - if (!ops) - return NULL; - domain =3D ops->domain_alloc(alloc_type); if (!domain) return NULL; @@ -2030,9 +2025,28 @@ static struct iommu_domain *__iommu_domain_alloc(con= st struct bus_type *bus, return domain; } =20 +static int __iommu_domain_alloc_dev(struct device *dev, void *data) +{ + struct device **alloc_dev =3D data; + + if (!dev_has_iommu(dev)) + return 0; + + WARN_ONCE(*alloc_dev && dev_iommu_ops(dev) !=3D dev_iommu_ops(*alloc_dev), + "Multiple IOMMU drivers present, which the public IOMMU API can't full= y support yet. You may still need to disable one or more to get the expecte= d result here, sorry!\n"); + + *alloc_dev =3D dev; + return 0; +} + struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) { - return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); + struct device *dev =3D NULL; + + if (bus_for_each_dev(bus, NULL, &dev, __iommu_domain_alloc_dev)) + return NULL; + + return __iommu_domain_alloc(dev, IOMMU_DOMAIN_UNMANAGED); } EXPORT_SYMBOL_GPL(iommu_domain_alloc); =20 @@ -3228,13 +3242,17 @@ static int __iommu_group_alloc_blocking_domain(stru= ct iommu_group *group) if (group->blocking_domain) return 0; =20 - group->blocking_domain =3D __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_BL= OCKED); + /* noiommu groups should never be here */ + if (WARN_ON(!dev_has_iommu(dev))) + return -ENODEV; + + group->blocking_domain =3D __iommu_domain_alloc(dev, IOMMU_DOMAIN_BLOCKED= ); if (!group->blocking_domain) { /* * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED * create an empty domain instead. */ - group->blocking_domain =3D __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_U= NMANAGED); + group->blocking_domain =3D __iommu_domain_alloc(dev, IOMMU_DOMAIN_UNMANA= GED); if (!group->blocking_domain) return -EINVAL; } --=20 2.39.2.101.g768bb238c484.dirty