Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4851271rdb; Fri, 15 Sep 2023 14:30:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFwMQg484u94J9fc1U3vf/4iEaxOzXT3HvPcU6213CBK7hht5oWn5WLcxN6iM9QAN4tANvG X-Received: by 2002:a05:6a21:3984:b0:14c:de3:95d6 with SMTP id ad4-20020a056a21398400b0014c0de395d6mr2615829pzc.45.1694813452333; Fri, 15 Sep 2023 14:30:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694813452; cv=none; d=google.com; s=arc-20160816; b=WTl9uvVKF3Jgb9jnVc2WpCL+bI3d8UWnLYkv8lZO8xhP5pBboSlBozyefPIGtoQQXK xPwgm5T+tBVmNazTrap8rYkjgPKzEd+mcgwakKS3KZBLjIEqGJVWuHckoSlj158RTR+L yWaHQbJUoem/esVNZWDRyqmkrubsR1BkTkdF3puebxPjnARbL8bwvgngPiBH7stxCH9c +wuPXKwU8dOxmUyI4t2FAkf2XXMlSZ+Lg+OfEFwb9gn/wNeSIVOlgYax21PzdadmqaoJ IyGthjxzI3QCr2KSFLMcU78Fn1tVTenmk4x92mcfcl3ubm+DpwJx9ku6B7CAtIgE+gug EcLQ== 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; bh=ruVglepVb75sdveTwhjok3FwgkUdeVudCV5g/RSf/zs=; fh=3SO4sBNpOFNxfB4XNpCUWqe4lDXSOQ4O5f1XEPxUZRQ=; b=yDtTeJre30uVh1fJJvJelyFEuvUfP8/VO+CXGLSNHxNWJGUPttTSurbgSr2wfm5XEn FC8U7Qb7T+vAwX4jNRJgn8aiJb4hKIUbO5JLBsOw//yMbmtnaqU818RJ7zFidT98V14l vzgMVsPzA1mSwg8f9CMAQtb/jss654xRizjqBVbE6YFt6lyWZlMtYrs46/uN436tS8PB iJHejrUhwz52wo52N1KGp6R1JyPgjxwPmn0n7HjSaP2wBgYFul3Vy0ijrUsi7ucad3z6 BSBGOUn+fxoERr8bmTojXB/+duNq9ZmkvsuOWnLYofyfkAoJL7zWFo912yxjEorMjkTZ 3ULQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id t17-20020a63d251000000b005777b0c8f59si3750348pgi.478.2023.09.15.14.30.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 14:30:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 063C9834E751; Fri, 15 Sep 2023 09:59:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234470AbjIOQ6l (ORCPT + 99 others); Fri, 15 Sep 2023 12:58:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234435AbjIOQ63 (ORCPT ); Fri, 15 Sep 2023 12:58:29 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 91805211E for ; Fri, 15 Sep 2023 09:58:23 -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 7A023C15; Fri, 15 Sep 2023 09:59:00 -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 2912D3F5A1; Fri, 15 Sep 2023 09:58:22 -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 1/7] iommu: Factor out some helpers Date: Fri, 15 Sep 2023 17:58:05 +0100 Message-Id: X-Mailer: git-send-email 2.39.2.101.g768bb238c484.dirty In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,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 (snail.vger.email [0.0.0.0]); Fri, 15 Sep 2023 09:59:23 -0700 (PDT) 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 Signed-off-by: Robin Murphy --- v3: Rename dev_iommu_ops_valid() to reflect what it's actually checking, rather than an implied consequence. --- drivers/iommu/iommu.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 3bfc56df4f78..4566d0001cd3 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -363,6 +363,15 @@ static void dev_iommu_free(struct device *dev) kfree(param); } +/* + * Internal equivalent of device_iommu_mapped() for when we care that a device + * actually has API ops, and don't want false positives from VFIO-only groups. + */ +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 = 0, bits = 0; @@ -614,7 +623,7 @@ static void __iommu_group_remove_device(struct device *dev) 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 = NULL; @@ -1218,6 +1227,12 @@ void iommu_group_remove_device(struct device *dev) } EXPORT_SYMBOL_GPL(iommu_group_remove_device); +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 @@ -1745,9 +1760,7 @@ __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 = - list_first_entry(&group->devices, struct group_device, list) - ->dev->bus; + const struct bus_type *bus = iommu_group_first_dev(group)->bus; struct iommu_domain *dom; lockdep_assert_held(&group->mutex); @@ -1912,7 +1925,7 @@ bool device_iommu_capable(struct device *dev, enum iommu_cap cap) { const struct iommu_ops *ops; - if (!dev->iommu || !dev->iommu->iommu_dev) + if (!dev_has_iommu(dev)) return false; ops = dev_iommu_ops(dev); @@ -2903,8 +2916,8 @@ EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); */ int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat) { - if (dev->iommu && dev->iommu->iommu_dev) { - const struct iommu_ops *ops = dev->iommu->iommu_dev->ops; + if (dev_has_iommu(dev)) { + const struct iommu_ops *ops = dev_iommu_ops(dev); if (ops->dev_enable_feat) return ops->dev_enable_feat(dev, feat); @@ -2919,8 +2932,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 = dev->iommu->iommu_dev->ops; + if (dev_has_iommu(dev)) { + const struct iommu_ops *ops = dev_iommu_ops(dev); if (ops->dev_disable_feat) return ops->dev_disable_feat(dev, feat); @@ -3190,21 +3203,18 @@ void iommu_device_unuse_default_domain(struct device *dev) static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) { - struct group_device *dev = - list_first_entry(&group->devices, struct group_device, list); + struct device *dev = iommu_group_first_dev(group); if (group->blocking_domain) return 0; - group->blocking_domain = - __iommu_domain_alloc(dev->dev->bus, IOMMU_DOMAIN_BLOCKED); + group->blocking_domain = __iommu_domain_alloc(dev->bus, 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 = __iommu_domain_alloc( - dev->dev->bus, IOMMU_DOMAIN_UNMANAGED); + group->blocking_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_UNMANAGED); if (!group->blocking_domain) return -EINVAL; } -- 2.39.2.101.g768bb238c484.dirty