Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1536848imw; Tue, 5 Jul 2022 11:07:19 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u8ZAJHvUaANV+GGvyyke+w2O5TZcUBkpzj63GkN20aC2DLoIifdtE5Im2LOaDosXTdHBGS X-Received: by 2002:a17:90a:5207:b0:1ee:dfa1:afa9 with SMTP id v7-20020a17090a520700b001eedfa1afa9mr42844708pjh.246.1657044439218; Tue, 05 Jul 2022 11:07:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657044439; cv=none; d=google.com; s=arc-20160816; b=fvaMla85hxd998cENwtbNfeTyZmDXhMgPvEa+qP+g7McrAVNmMl75qTkB7UxtvzN9T USGQE/20LchvMHhu55cAucGPT0WfM23CBgNGtF8vrLdpFYbXnkW33i0ODidAk7e/9oiM JDOTomRISflwg+Y2dtj44+QCoiac9b1ExPSFRzDqJsiXwUPX0gdRt3UJ5gpagLqrkwIo uyp4IiDbS1iq/f4fErvhdMiOawhoG3s/fjw85E+AwK9vbWIMPKqI7CcfpZsGORa5mQq3 BGkFbuN++gQv94AbJWPbbem8FQwjnYdBIw020VrRlmYWOJ5Ia1GD31PA0NpqtXYKK2wB ZNgA== 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=Hz746eR6IDW7jeksH11KHuxgsHmGfIbM1NN31W44Zdg=; b=ONyyu0YC3lYQEUwnHKHlO/VWtkhjzhsSJVgDOQjqQJTA3rMUVdzUbn2rPwJEkFN7gY Gb2b2n7dvRe3I/i/ZVMneWs7j8JViUqB4y61pvMUYkdfmBN7mgznntvoLqzOBmrM0dqd 6tMdSsaeFeicstmbGWaLvVyG2fFcS+aWpof9IQQpnnwxMbkVqLd3+LyZGvUGuq6hw0xI 8B25j0H/cTHBlwjnsDrJJWz+PbLip4VA8uBZV0N10vnntXmm1gfso/Yy9jW7iYl3uL9T 4MHT0PLCoiuzK/V/6aE+8ohfCLY7uRc5lYpA8vLXvdLOUakwLPFnU9d8l/1gs6zfDTcx 7DFg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v15-20020a056a00148f00b0051c6152700asi25743734pfu.136.2022.07.05.11.07.07; Tue, 05 Jul 2022 11:07:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233218AbiGERJA (ORCPT + 99 others); Tue, 5 Jul 2022 13:09:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232822AbiGERIw (ORCPT ); Tue, 5 Jul 2022 13:08:52 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CCA391CFDB; Tue, 5 Jul 2022 10:08:50 -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 DE83B152B; Tue, 5 Jul 2022 10:08:50 -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 095E03F66F; Tue, 5 Jul 2022 10:08:48 -0700 (PDT) From: Robin Murphy To: joro@8bytes.org Cc: will@kernel.org, iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org, baolu.lu@linux.intel.com, suravee.suthikulpanit@amd.com, vasant.hegde@amd.com, mjrosato@linux.ibm.com, gerald.schaefer@linux.ibm.com, schnelle@linux.ibm.com, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 04/15] iommu: Move bus setup to IOMMU device registration Date: Tue, 5 Jul 2022 18:08:27 +0100 Message-Id: <5b9b608af21b3c4353af042355973bac55397962.1657034828.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 Move the bus setup to iommu_device_register(). This should allow bus_iommu_probe() to be correctly replayed for multiple IOMMU instances, and leaves bus_set_iommu() as a glorified no-op to be cleaned up next. At this point we can also handle cleanup better than just rolling back the most-recently-touched bus upon failure - which may release devices owned by other already-registered instances, and still leave devices on other buses with dangling pointers to the failed instance. Now it's easy to clean up the exact footprint of a given instance, no more, no less. Tested-by: Marek Szyprowski Reviewed-By: Krishna Reddy Signed-off-by: Robin Murphy --- v3: Have iommu_device_register() return -EBUSY for conflicting ops rather than change that behaviour just yet. drivers/iommu/iommu.c | 54 ++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 514edc0eaa94..acb7b2ab0b79 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -180,6 +180,14 @@ static int __init iommu_subsys_init(void) } subsys_initcall(iommu_subsys_init); +static int remove_iommu_group(struct device *dev, void *data) +{ + if (dev->iommu && dev->iommu->iommu_dev == data) + iommu_release_device(dev); + + return 0; +} + /** * iommu_device_register() - Register an IOMMU hardware instance * @iommu: IOMMU handle for the instance @@ -202,12 +210,32 @@ int iommu_device_register(struct iommu_device *iommu, spin_lock(&iommu_device_lock); list_add_tail(&iommu->list, &iommu_device_list); spin_unlock(&iommu_device_lock); + + for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) { + struct bus_type *bus = iommu_buses[i]; + int err; + + if (bus->iommu_ops && bus->iommu_ops != ops) { + err = -EBUSY; + } else { + bus->iommu_ops = ops; + err = bus_iommu_probe(bus); + } + if (err) { + iommu_device_unregister(iommu); + return err; + } + } + return 0; } EXPORT_SYMBOL_GPL(iommu_device_register); void iommu_device_unregister(struct iommu_device *iommu) { + for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) + bus_for_each_dev(iommu_buses[i], NULL, iommu, remove_iommu_group); + spin_lock(&iommu_device_lock); list_del(&iommu->list); spin_unlock(&iommu_device_lock); @@ -1633,13 +1661,6 @@ static int probe_iommu_group(struct device *dev, void *data) return ret; } -static int remove_iommu_group(struct device *dev, void *data) -{ - iommu_release_device(dev); - - return 0; -} - static int iommu_bus_notifier(struct notifier_block *nb, unsigned long action, void *data) { @@ -1828,27 +1849,12 @@ static int iommu_bus_init(struct bus_type *bus) */ int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops) { - int err; - - if (ops == NULL) { - bus->iommu_ops = NULL; - return 0; - } - - if (bus->iommu_ops != NULL) + if (bus->iommu_ops && ops && bus->iommu_ops != ops) return -EBUSY; bus->iommu_ops = ops; - /* Do IOMMU specific setup for this bus-type */ - err = bus_iommu_probe(bus); - if (err) { - /* Clean up */ - bus_for_each_dev(bus, NULL, NULL, remove_iommu_group); - bus->iommu_ops = NULL; - } - - return err; + return 0; } EXPORT_SYMBOL_GPL(bus_set_iommu); -- 2.36.1.dirty