Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4712799rdb; Fri, 15 Sep 2023 09:59:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEiWBRP3xmgYJK4uxD1DN56024DEpnXng96B+qFtFYdJ0AAjWBm88ObcfaNCU+ukueVm+oK X-Received: by 2002:a05:6a20:1442:b0:159:fe1d:2f09 with SMTP id a2-20020a056a20144200b00159fe1d2f09mr3127121pzi.6.1694797157967; Fri, 15 Sep 2023 09:59:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694797157; cv=none; d=google.com; s=arc-20160816; b=sDNglBk0GsHPpiR8j21N4o5/Qw1yYxdBH2JE2A+Q+FYoDf8+87T4Pm44jaVPtB4tF8 HZ8Wc5g8CJevpeJJHsVRFmcHwKg05Co4i+bKi4UeuVPolP18CSmr1xQgiipo8MBU1PsY 4yWHtDCGn+q8o2mthGLjjZU1YgDXV8RcKpN6ablP29IzUyjPYBFZOFrXsskwoe1AP7OU OjJVQAYyoUKfj2NXP5VLv8LhsicfGed7jKDoVV2Knn9T5NtoFuIINkS586LZjAl/ISgI JmEAfbvHesTiWz8j/Wyqnh2YDf1mHsAtphNizQ4whKd3MJz4KF41pRurkJGXwo8pEzwC kurQ== 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=LiY9GesaVybF9y4WbIRidFyCUIQZpfvFNPduD/kDQlQ=; fh=3SO4sBNpOFNxfB4XNpCUWqe4lDXSOQ4O5f1XEPxUZRQ=; b=ry0STABvQPT4H104HV3wtl1zhBY4+ULZGnchwlAC+kfSqnl/rZJ+1+WkgV+AW6gScI xKxsiX0hPmDtUFwydz/sTVl+1U4ZmdGO2tnwfamp24H7bbhyvDU7XByRJ2Ot5t+4DGdo wRBN6Db74jh5ANG6ilPDzPyIFvnMaoHjEBY8SQ/JzGJ8Fy55XpC8Z4nmqUmrInC1WtPh cXkQELmE4SWjZixym1nIBQznuwl2ZHvbPGdxuKBp9v737vLDzG2gqC3kSX4pn4xyG59D YeUDWKXqCh2xVuJ5pJ8o+Y1l3LEc1d6fHC56uQdS8VjSrwGMJzrXDWJ/w4O5NTebaMFJ m+xg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id q8-20020a056a00084800b0068e3f3c6250si3725271pfk.343.2023.09.15.09.59.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 09:59:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 47D0182E555E; Fri, 15 Sep 2023 09:59:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235275AbjIOQ6r (ORCPT + 99 others); Fri, 15 Sep 2023 12:58:47 -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: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email 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 (agentk.vger.email [0.0.0.0]); Fri, 15 Sep 2023 09:59:13 -0700 (PDT) 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[] = { 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 *bus, +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) } 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 == req_type) return group->default_domain; - return __iommu_domain_alloc(bus, req_type); + return __iommu_domain_alloc(iommu_group_first_dev(group), req_type); } /* @@ -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 = iommu_group_first_dev(group)->bus; struct iommu_domain *dom; lockdep_assert_held(&group->mutex); if (req_type) - return __iommu_group_alloc_default_domain(bus, group, req_type); + return __iommu_group_alloc_default_domain(group, req_type); /* The driver gave no guidance on what type to use, try the default */ - dom = __iommu_group_alloc_default_domain(bus, group, iommu_def_domain_type); + dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type); if (dom) return dom; /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */ if (iommu_def_domain_type == IOMMU_DOMAIN_DMA) return NULL; - dom = __iommu_group_alloc_default_domain(bus, group, IOMMU_DOMAIN_DMA); + dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA); if (!dom) return NULL; @@ -1997,16 +1995,13 @@ void iommu_set_fault_handler(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_set_fault_handler); -static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus, +static struct iommu_domain *__iommu_domain_alloc(struct device *dev, unsigned type) { - const struct iommu_ops *ops = bus ? bus->iommu_ops : NULL; + const struct iommu_ops *ops = dev_iommu_ops(dev); struct iommu_domain *domain; unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS; - if (!ops) - return NULL; - domain = ops->domain_alloc(alloc_type); if (!domain) return NULL; @@ -2030,9 +2025,28 @@ static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus, return domain; } +static int __iommu_domain_alloc_dev(struct device *dev, void *data) +{ + struct device **alloc_dev = data; + + if (!dev_has_iommu(dev)) + return 0; + + WARN_ONCE(*alloc_dev && dev_iommu_ops(dev) != dev_iommu_ops(*alloc_dev), + "Multiple IOMMU drivers present, which the public IOMMU API can't fully support yet. You may still need to disable one or more to get the expected result here, sorry!\n"); + + *alloc_dev = 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 = 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); @@ -3228,13 +3242,17 @@ static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) if (group->blocking_domain) return 0; - group->blocking_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_BLOCKED); + /* noiommu groups should never be here */ + if (WARN_ON(!dev_has_iommu(dev))) + return -ENODEV; + + group->blocking_domain = __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 = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_UNMANAGED); + group->blocking_domain = __iommu_domain_alloc(dev, IOMMU_DOMAIN_UNMANAGED); if (!group->blocking_domain) return -EINVAL; } -- 2.39.2.101.g768bb238c484.dirty