From nobody Thu Dec 18 07:19:54 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 CDB41C61D85 for ; Tue, 21 Nov 2023 18:05:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234682AbjKUSFH (ORCPT ); Tue, 21 Nov 2023 13:05:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234552AbjKUSEo (ORCPT ); Tue, 21 Nov 2023 13:04:44 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 752EE1708 for ; Tue, 21 Nov 2023 10:04:16 -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 C1917168F; Tue, 21 Nov 2023 10:05:02 -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 0147A3F6C4; Tue, 21 Nov 2023 10:04:14 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, jgg@nvidia.com, baolu.lu@linux.intel.com, linux-kernel@vger.kernel.org, jsnitsel@redhat.com Subject: [PATCH v6 1/7] iommu: Factor out some helpers Date: Tue, 21 Nov 2023 18:03:57 +0000 Message-Id: <566cbd161546caa6aed49662c9b3e8f09dc9c3cf.1700589539.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" The pattern for picking the first device out of the group list is repeated a few times now, so it's clearly worth factoring out, which also helps hide the iommu_group_dev detail from places that don't need to know. Similarly, the safety check for dev_iommu_ops() at certain public interfaces starts looking a bit repetitive, and might not be completely obvious at first glance, so let's factor that out for clarity as well, in preparation for more uses of both. Reviewed-by: Lu Baolu Reviewed-by: Jason Gunthorpe Reviewed-by: Jerry Snitselaar Signed-off-by: Robin Murphy --- v3: Rename dev_iommu_ops_valid() to reflect what it's actually checking, rather than an implied consequence. v4: Rebase, also catch the sneaky one in iommu_get_group_resv_regions() which wasn't the full pattern (but really should be since it guards the dev_iommu_ops() invocation in iommu_get_resv_regions()). --- drivers/iommu/iommu.c | 56 ++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index f17a1113f3d6..5c555fc0d54c 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -368,6 +368,15 @@ static void dev_iommu_free(struct device *dev) kfree(param); } =20 +/* + * Internal equivalent of device_iommu_mapped() for when we care that a de= vice + * actually has API ops, and don't want false positives from VFIO-only gro= ups. + */ +static bool dev_has_iommu(struct device *dev) +{ + return dev->iommu && dev->iommu->iommu_dev; +} + static u32 dev_iommu_get_max_pasids(struct device *dev) { u32 max_pasids =3D 0, bits =3D 0; @@ -620,7 +629,7 @@ static void __iommu_group_remove_device(struct device *= dev) =20 list_del(&device->list); __iommu_group_free_device(group, device); - if (dev->iommu && dev->iommu->iommu_dev) + if (dev_has_iommu(dev)) iommu_deinit_device(dev); else dev->iommu_group =3D NULL; @@ -819,7 +828,7 @@ int iommu_get_group_resv_regions(struct iommu_group *gr= oup, * Non-API groups still expose reserved_regions in sysfs, * so filter out calls that get here that way. */ - if (!device->dev->iommu) + if (!dev_has_iommu(device->dev)) break; =20 INIT_LIST_HEAD(&dev_resv_regions); @@ -1225,6 +1234,12 @@ void iommu_group_remove_device(struct device *dev) } EXPORT_SYMBOL_GPL(iommu_group_remove_device); =20 +static struct device *iommu_group_first_dev(struct iommu_group *group) +{ + lockdep_assert_held(&group->mutex); + return list_first_entry(&group->devices, struct group_device, list)->dev; +} + /** * iommu_group_for_each_dev - iterate over each device in the group * @group: the group @@ -1752,23 +1767,6 @@ __iommu_group_alloc_default_domain(struct iommu_grou= p *group, int req_type) return __iommu_group_domain_alloc(group, req_type); } =20 -/* - * Returns the iommu_ops for the devices in an iommu group. - * - * It is assumed that all devices in an iommu group are managed by a single - * IOMMU unit. Therefore, this returns the dev_iommu_ops of the first devi= ce - * in the group. - */ -static const struct iommu_ops *group_iommu_ops(struct iommu_group *group) -{ - struct group_device *device =3D - list_first_entry(&group->devices, struct group_device, list); - - lockdep_assert_held(&group->mutex); - - return dev_iommu_ops(device->dev); -} - /* * req_type of 0 means "auto" which means to select a domain based on * iommu_def_domain_type or what the driver actually supports. @@ -1776,7 +1774,7 @@ static const struct iommu_ops *group_iommu_ops(struct= iommu_group *group) static struct iommu_domain * iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) { - const struct iommu_ops *ops =3D group_iommu_ops(group); + const struct iommu_ops *ops =3D dev_iommu_ops(iommu_group_first_dev(group= )); struct iommu_domain *dom; =20 lockdep_assert_held(&group->mutex); @@ -1854,7 +1852,7 @@ static int iommu_bus_notifier(struct notifier_block *= nb, static int iommu_get_def_domain_type(struct iommu_group *group, struct device *dev, int cur_type) { - const struct iommu_ops *ops =3D group_iommu_ops(group); + const struct iommu_ops *ops =3D dev_iommu_ops(dev); int type; =20 if (!ops->def_domain_type) @@ -2021,7 +2019,7 @@ bool device_iommu_capable(struct device *dev, enum io= mmu_cap cap) { const struct iommu_ops *ops; =20 - if (!dev->iommu || !dev->iommu->iommu_dev) + if (!dev_has_iommu(dev)) return false; =20 ops =3D dev_iommu_ops(dev); @@ -2120,11 +2118,9 @@ static struct iommu_domain *__iommu_domain_alloc(con= st struct iommu_ops *ops, static struct iommu_domain * __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type) { - struct device *dev =3D - list_first_entry(&group->devices, struct group_device, list) - ->dev; + struct device *dev =3D iommu_group_first_dev(group); =20 - return __iommu_domain_alloc(group_iommu_ops(group), dev, type); + return __iommu_domain_alloc(dev_iommu_ops(dev), dev, type); } =20 struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) @@ -2987,8 +2983,8 @@ EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); */ int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features f= eat) { - if (dev->iommu && dev->iommu->iommu_dev) { - const struct iommu_ops *ops =3D dev->iommu->iommu_dev->ops; + if (dev_has_iommu(dev)) { + const struct iommu_ops *ops =3D dev_iommu_ops(dev); =20 if (ops->dev_enable_feat) return ops->dev_enable_feat(dev, feat); @@ -3003,8 +2999,8 @@ EXPORT_SYMBOL_GPL(iommu_dev_enable_feature); */ int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features = feat) { - if (dev->iommu && dev->iommu->iommu_dev) { - const struct iommu_ops *ops =3D dev->iommu->iommu_dev->ops; + if (dev_has_iommu(dev)) { + const struct iommu_ops *ops =3D dev_iommu_ops(dev); =20 if (ops->dev_disable_feat) return ops->dev_disable_feat(dev, feat); --=20 2.39.2.101.g768bb238c484.dirty