Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1350429rdb; Mon, 2 Oct 2023 07:06:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHU9wBTuNetaiFvdH39uZOp2wSGrDuJH4rpoSLwVgYGqwoPzaK94aUfLCTk5VhnF0DbNr4J X-Received: by 2002:a05:6a21:7783:b0:15d:e68d:a850 with SMTP id bd3-20020a056a21778300b0015de68da850mr13900949pzc.29.1696255574763; Mon, 02 Oct 2023 07:06:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696255574; cv=none; d=google.com; s=arc-20160816; b=SDEGYEw8+pnVdTaEb2IXjkUHNFIrdiekYQ+TnXpn/00vR0OWWE9J1EEe4uJiOwchIf 0G/WLogjOUCou6qhM8RWHiGB6mX470jcVUCZHrlHUEkY9H02igWc2IQdeL5rarJU+ljJ tCjYjZ4YWr0w4eovUIKndK+bx+bKLLn//eV+7DjPISVVxUtYry7kZV9kUfjrTc3vGtnT 2MUvpoAuSQ//pUOrzZNdDJVK9NOQRSfltCOBox9nIgK0Tsg4YwFSo7xUaqaAGKn5/PZo gnL6PPhovRHnm4HG4TUdcGBOhRn8jgP3NtolCNAH2J9443bIuNCRju6RpJaCQKJ/nDbt 4Vvg== 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=AhGgYCAts/1aJ8aYNNR1TkqnYJ5d0j50OhmXxo3IxU4=; fh=3nQM0QufZPSZ94JO+WDmHkrFziuG1/NJr0OD1jy0Wvc=; b=uZiud4Kf75UdPu92UfMSFSgp5WDXQbezJwYPQstKdL/3xF+oETnHh4tRTeuY1zppg1 7z8GQQt7eD2v4MtrzxO2SuNClFh4aF1iCsExeELkO8W2q3goCLI6Mst6ILDMe5oJ9N20 Jg3HBAtX0TjZwi+vFXofYJQSXQUeN3OCYGXA6um2F0AphWwdHl0gvNU7S9MbAYMJA1rO yqd1zSSHt5IkJT0SXrQ6bMfkv7Y7CkXE3nAeYTt9EuVmphYv+7UAvXquDUXCRuPUVbfQ D9LkBD1guuQLpK9GWKRW5M5JSvqqOZiK4MoX9XOVpICN8JlCtDrtcLeDDEHWWpYKMuOk 7ZYg== 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 bs5-20020a632805000000b005855f67e48csi12590528pgb.697.2023.10.02.07.06.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 07:06:14 -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 3A703802A6C9; Mon, 2 Oct 2023 06:49:45 -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 S237571AbjJBNth (ORCPT + 99 others); Mon, 2 Oct 2023 09:49:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237515AbjJBNta (ORCPT ); Mon, 2 Oct 2023 09:49:30 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9E0A9D9 for ; Mon, 2 Oct 2023 06:49:26 -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 D950CC15; Mon, 2 Oct 2023 06:50:04 -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 8FC013F762; Mon, 2 Oct 2023 06:49:25 -0700 (PDT) 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 Subject: [PATCH v4 4/7] iommu: Switch __iommu_domain_alloc() to device ops Date: Mon, 2 Oct 2023 14:49:12 +0100 Message-Id: <458dd0ed839541693a49da33239b33cf4c48b8ec.1696253096.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=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,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]); Mon, 02 Oct 2023 06:49:45 -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 v4: Reinstate correct bus_for_each_dev() handling from v2 --- drivers/iommu/iommu.c | 48 ++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 7c79a58ef010..c5b5408d1dd7 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -95,8 +95,8 @@ 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_group_domain_alloc(struct iommu_group *group, unsigned int type); +static struct iommu_domain *__iommu_domain_alloc(struct device *dev, + unsigned type); static int __iommu_attach_device(struct iommu_domain *domain, struct device *dev); static int __iommu_attach_group(struct iommu_domain *domain, @@ -1763,7 +1763,7 @@ __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_group_domain_alloc(group, req_type); + return __iommu_domain_alloc(iommu_group_first_dev(group), req_type); } /* @@ -2082,10 +2082,10 @@ 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 iommu_ops *ops, - struct device *dev, - unsigned int type) +static struct iommu_domain *__iommu_domain_alloc(struct device *dev, + unsigned type) { + const struct iommu_ops *ops = dev_iommu_ops(dev); struct iommu_domain *domain; unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS; @@ -2120,20 +2120,30 @@ static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops, return domain; } -static struct iommu_domain * -__iommu_group_domain_alloc(struct iommu_group *group, unsigned int type) +static int __iommu_domain_alloc_dev(struct device *dev, void *data) { - struct device *dev = iommu_group_first_dev(group); + struct device **alloc_dev = data; - return __iommu_domain_alloc(dev_iommu_ops(dev), dev, type); + 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) { - if (bus == NULL || bus->iommu_ops == NULL) + struct device *dev = NULL; + + /* We always check the whole bus, so the return value isn't useful */ + bus_for_each_dev(bus, NULL, &dev, __iommu_domain_alloc_dev); + if (!dev) return NULL; - return __iommu_domain_alloc(bus->iommu_ops, NULL, - IOMMU_DOMAIN_UNMANAGED); + + return __iommu_domain_alloc(dev, IOMMU_DOMAIN_UNMANAGED); } EXPORT_SYMBOL_GPL(iommu_domain_alloc); @@ -3256,18 +3266,22 @@ void iommu_device_unuse_default_domain(struct device *dev) static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) { + struct device *dev = iommu_group_first_dev(group); + if (group->blocking_domain) return 0; - group->blocking_domain = - __iommu_group_domain_alloc(group, 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_group_domain_alloc( - group, 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