From patchwork Thu Jan 26 18:26:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 48837 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp423117wrn; Thu, 26 Jan 2023 10:30:38 -0800 (PST) X-Google-Smtp-Source: AK7set8OHCg638PSA2F1p/0P9HH1XMQ3uI/5hBJM/Yy8VaMS6dllO13sGoeTau5pu9H7M//3hLcL X-Received: by 2002:a17:906:f215:b0:878:6f47:5f73 with SMTP id gt21-20020a170906f21500b008786f475f73mr2601788ejb.3.1674757838631; Thu, 26 Jan 2023 10:30:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674757838; cv=none; d=google.com; s=arc-20160816; b=a8RY4YeJpVi/aszIBf5Ro/cuUSotGzLr0QIdWnG/FHyeEypak1303zHic0Gxqb855l 40HmRyJvLuxK+yZfSzmtSZM72X/3+2XKanTsorc6ejIZ2LKZ7+HOYT8E8cw23zsoWs3F QLXSxlaiOzxDAY3Kp7DDVZ+jo9Y5wwnuxtmJB4lz3IudRKc1j4ryd9Ds0/yieV6vJuxx c0pHI5yPIrG0vslkXtM/oVAolV0RSDxrFKsPUCptp1r/5+BLmsax5ayymqozmktMbP9x a21E1nI9ZLJkkqux7xPTMIK8kuCCxeajVXMuWND8p37KhK1fCXOkQvF7TedGBm/gsJJQ ZCtw== 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=OwCf2bBRO5r6E+h3sMQgBfXvvrcBbCRWYz/K52xkWBs=; b=ozNxI4lSTOfxREMdhB3aoPeMQl7LVMfh8i2CRLHPoBh/mKb1836BqkSjjRdTcBhsKH /b4BOkkw3eWUmtP+VxrLBlbyluNo5aQyrMemrYwxp0C7ILhF856uw5GWGeiMcEVgOz0K sI3R02s17P8saYo+MOAHcrv7jc8v0sWG7RNS8jBcPjh3gywCAtFabLc78PcMv9vA7wGt RvqOU/goNV06qs7dDq9SHtBuBU0qNfb+dNydk9IeO4GS2t/FEo9JrhYRUiTsQvGJvKA4 1Zk6ZB5zYidaSJnjWzqEwa5OVd38P+lpiCZiebxp//JStADehryhiTD6fUJmX+H72LXw 16lw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fv34-20020a17090750a200b008778e608891si2044666ejc.256.2023.01.26.10.30.14; Thu, 26 Jan 2023 10:30:38 -0800 (PST) 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 S230516AbjAZS0d (ORCPT + 99 others); Thu, 26 Jan 2023 13:26:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229874AbjAZS0b (ORCPT ); Thu, 26 Jan 2023 13:26:31 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 19AAE599A8 for ; Thu, 26 Jan 2023 10:26:31 -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 B3B86C14; Thu, 26 Jan 2023 10:27:12 -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 C25DF3F71E; Thu, 26 Jan 2023 10:26:29 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de, jgg@nvidia.com, baolu.lu@linux.intel.com Subject: [PATCH v2 1/8] iommu: Decouple iommu_present() from bus ops Date: Thu, 26 Jan 2023 18:26:16 +0000 Message-Id: X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756110875132421088?= X-GMAIL-MSGID: =?utf-8?q?1756110875132421088?= Much as I'd like to remove iommu_present(), the final remaining users are proving stubbornly difficult to clean up, so kick that can down the road and just rework it to preserve the current behaviour without depending on bus ops. Signed-off-by: Robin Murphy Reviewed-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- v2: No change drivers/iommu/iommu.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 42591766266d..b27f5d3453bb 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1871,9 +1871,24 @@ int bus_iommu_probe(struct bus_type *bus) return ret; } +static int __iommu_present(struct device *dev, void *unused) +{ + return device_iommu_mapped(dev); +} + +/** + * iommu_present() - make platform-specific assumptions about an IOMMU + * @bus: bus to check + * + * Do not use this function. You want device_iommu_mapped() instead. + * + * Return: true if some IOMMU is present for some device on the given bus. In + * general it may not be the only IOMMU, and it may not be for the device you + * are ultimately interested in. + */ bool iommu_present(struct bus_type *bus) { - return bus->iommu_ops != NULL; + return bus_for_each_dev(bus, NULL, NULL, __iommu_present) > 0; } EXPORT_SYMBOL_GPL(iommu_present); From patchwork Thu Jan 26 18:26:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 48838 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp423447wrn; Thu, 26 Jan 2023 10:31:16 -0800 (PST) X-Google-Smtp-Source: AK7set99QFhyjg9/GrJ89EnBzYU4wlohgfqYeiK2D4+RC0RBn1J0zwvKVySR/BZoFcHM31TjUaSC X-Received: by 2002:a17:906:d974:b0:878:7a0e:5730 with SMTP id rp20-20020a170906d97400b008787a0e5730mr1840059ejb.56.1674757876447; Thu, 26 Jan 2023 10:31:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674757876; cv=none; d=google.com; s=arc-20160816; b=OK1h6BwvrU3nhH/j6ahl5fC6gsh1etVa/lVJTfe0a8ILfVhmjgiAi7kTPWeHHL3+9l +bqu0U2Iy45Ima2BFLsPKmfzCXnYieV8ECfSLlsibR6JOW0nD7gvt+WUv/RtLgGTr67w 7T1KQV3ck/dnMVs2KMtS+IuVYt2e/qQS9seJj4KSj/EPAsWfCg9LucsrwkXx9Pjzk5+i 2ZaCRjsvS5FFOXK+dMVtZc4fYlIUuuNbX7N9upW82i7dAewk1vBWQvGotKoLbMOMkNh5 8Jvs2Imr02qAvdWtCnGphWF+1io4M7v/bTGC2ZqUdPODvQanShPeC9/46B/+VEKyItmG qkKQ== 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=1HBhK89fua2rsU3gipADzrT3ryao/E2fxKK5YUWGTYE=; b=GPIEFVDHr3pHO3TgTG2LTy+tquEhHNDYXc3ZZ0Qode11hDunz2fBI5D2X8SK7v7ujU xIOjHb/dJpOvRT6DOHLWBdbu2NzS5G/AktvwkYylBxsqZG+YmuMU8iJPk9207+DJbryj zhTGazsflnwPvqt5FHm0Rwbl0oLfoYU0evpiaA6U/qUex+yPxjaJypnqNa+RSaDAClx1 F/mjDHWLC6kVhXwAqr/PiNNhJshiPgz1KM64EBnJDgYEE3Z37urEZvs2WAC7apCr+zoh 1PCJalvfQ+Gi0o6Y+yHqXlfR6SmOjxNFiKtC2hQN8GeaisGYf0iyvvOj1UzqE9lQ29tA qulQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y14-20020a17090668ce00b007ae10525573si2258331ejr.671.2023.01.26.10.30.41; Thu, 26 Jan 2023 10:31:16 -0800 (PST) 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 S231960AbjAZS0f (ORCPT + 99 others); Thu, 26 Jan 2023 13:26:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230363AbjAZS0d (ORCPT ); Thu, 26 Jan 2023 13:26:33 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 452C62E811 for ; Thu, 26 Jan 2023 10:26:32 -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 E49AA1424; Thu, 26 Jan 2023 10:27:13 -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 F33893F71E; Thu, 26 Jan 2023 10:26:30 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de, jgg@nvidia.com, baolu.lu@linux.intel.com Subject: [PATCH v2 2/8] iommu: Validate that devices match domains Date: Thu, 26 Jan 2023 18:26:17 +0000 Message-Id: <0f78ba36a7b31a0d534416e56ea0f1af0efc2659.1674753627.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756110915141148200?= X-GMAIL-MSGID: =?utf-8?q?1756110915141148200?= Before we can allow drivers to coexist, we need to make sure that one driver's domain ops can't misinterpret another driver's dev_iommu_priv data. To that end, add a token to the domain so we can remember how it was allocated - for now this may as well be the device ops, since they still correlate 1:1 with drivers. We can trust ourselves for internal default domain attachment, so add the check where it covers both the external attach interfaces. Signed-off-by: Robin Murphy Reviewed-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- v2: Tweaked commit message drivers/iommu/iommu.c | 13 +++++++++---- include/linux/iommu.h | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index b27f5d3453bb..d48e5499e0fa 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1941,20 +1941,22 @@ EXPORT_SYMBOL_GPL(iommu_set_fault_handler); static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, unsigned type) { + const struct iommu_ops *ops = bus ? bus->iommu_ops : NULL; struct iommu_domain *domain; - if (bus == NULL || bus->iommu_ops == NULL) + if (!ops) return NULL; - domain = bus->iommu_ops->domain_alloc(type); + domain = ops->domain_alloc(type); if (!domain) return NULL; domain->type = type; + domain->owner = ops; /* Assume all sizes by default; the driver may override this later */ - domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; + domain->pgsize_bitmap = ops->pgsize_bitmap; if (!domain->ops) - domain->ops = bus->iommu_ops->default_domain_ops; + domain->ops = ops->default_domain_ops; if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) { iommu_domain_free(domain); @@ -2120,6 +2122,9 @@ static int iommu_group_do_attach_device(struct device *dev, void *data) { struct iommu_domain *domain = data; + if (dev_iommu_ops(dev) != domain->owner) + return -EINVAL; + return __iommu_attach_device(domain, dev); } diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 3589d1b8f922..86fa52025e75 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -95,6 +95,7 @@ struct iommu_domain_geometry { struct iommu_domain { unsigned type; const struct iommu_domain_ops *ops; + const struct iommu_ops *owner; /* Whose domain_alloc we came from */ unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */ struct iommu_domain_geometry geometry; struct iommu_dma_cookie *iova_cookie; From patchwork Thu Jan 26 18:26:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 48839 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp423474wrn; Thu, 26 Jan 2023 10:31:20 -0800 (PST) X-Google-Smtp-Source: AMrXdXtUsoPcgWHEE12zZOLOpEYr12sNfmwNqNpZ69984mVobR7ssBXESmS5GkpfagJDtvyEalCP X-Received: by 2002:a17:906:ce23:b0:870:c363:599d with SMTP id sd3-20020a170906ce2300b00870c363599dmr40551195ejb.51.1674757879870; Thu, 26 Jan 2023 10:31:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674757879; cv=none; d=google.com; s=arc-20160816; b=YkRpQ1xWKLHhIxYDZfV0H3ugSZ/BvDzf21wiZIAOen0SQqR5yP52DVWoWPKGNwuLXn m8n4ciZiYcGGJS1Bw+KlnvELAeuHO30R7hNa1AfOGWPSWvc2qynuRY0encQB4N9ZlvHs gIZ94kOlWL2asAIIyujAp1AgMR1CtKwQOthmQL4QHpj0anG/wm+2pd4euGuEsjETyUn7 F+y+qRqcAam8wbaf+27jw9tOZk1RLbom0gYQS7/M367ENpj2uVQdaoaG/r1q69B150pQ YsxFzj8tHNWRvIXPlei8KylI98eATt1aEEVTgILKtjs3Oze0nLTJuxDr19H7Cafv3tBm FnsA== 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=47+d+dbsYt1NTyDkdHhV/0k9W5vxD9U2tVtMotW+qgY=; b=GXFnmuGEtP/aY7eoheOPnd3wwBIIWmbq/18Kx7zbzL8RXNh2fhHBk1eG0XX6HximKX 05hgWlb4K/8wIX3KM2FcN8ZGQrVdm6IL7BqKxF/IrZZmwX7DAynJnxJvUtadE12gWeQs YtJQr34U3UQ9GukUSJMW4DVT2P8z04YQli7J/hmS2N4RZTMWZKDqehjR1tCcphDbDV+j gfRolt3TChnPrsc7pwyBAI4tdfz6P8Snxmqo0oNVVmZH5A09ZWvtwX7E2vDfo5+57IiA /v+mvwSyMAZC77iekXPdIwqghJtAWD0Q5UrUasKTGPT6/AI8rFeonCeoIWdbiVFtbTEB eeJg== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fe19-20020a1709072a5300b0084ccee74c20si1776104ejc.497.2023.01.26.10.30.50; Thu, 26 Jan 2023 10:31:19 -0800 (PST) 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 S231989AbjAZS0i (ORCPT + 99 others); Thu, 26 Jan 2023 13:26:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231302AbjAZS0e (ORCPT ); Thu, 26 Jan 2023 13:26:34 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AA0A0677A7 for ; Thu, 26 Jan 2023 10:26:33 -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 216944B3; Thu, 26 Jan 2023 10:27:15 -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 2FF463F71E; Thu, 26 Jan 2023 10:26:32 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de, jgg@nvidia.com, baolu.lu@linux.intel.com Subject: [PATCH v2 3/8] iommu: Add lockdep annotations for group list iterators Date: Thu, 26 Jan 2023 18:26:18 +0000 Message-Id: <57cba0cf89556d586e554a9c64cbd28205d67c5c.1674753627.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756110919064468293?= X-GMAIL-MSGID: =?utf-8?q?1756110919064468293?= Before we add any more common helpers for iterating or otherwise accessing the group device list, let's start the good habit of annotating their locking expectations for robustness. Signed-off-by: Robin Murphy Reviewed-by: Lu Baolu --- v2: New. Note that I've left the group_pasid helpers, since they're pretty much right next to where the relevant locking happens, and highly unlikely to be reused from anywhere else anyway. drivers/iommu/iommu.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index d48e5499e0fa..77f076030995 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -1100,6 +1101,7 @@ static int iommu_group_device_count(struct iommu_group *group) struct group_device *entry; int ret = 0; + lockdep_assert_held(&group->mutex); list_for_each_entry(entry, &group->devices, list) ret++; @@ -1112,6 +1114,7 @@ static int __iommu_group_for_each_dev(struct iommu_group *group, void *data, struct group_device *device; int ret = 0; + lockdep_assert_held(&group->mutex); list_for_each_entry(device, &group->devices, list) { ret = fn(device->dev, data); if (ret) From patchwork Thu Jan 26 18:26:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 48840 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp423768wrn; Thu, 26 Jan 2023 10:31:50 -0800 (PST) X-Google-Smtp-Source: AK7set8R+SSjKr8Hm4eboT6QkgCiuMraebfbC/6OID+Nt1tZNG2/70KKbC4I/lqbvRveHwzEw/1A X-Received: by 2002:a17:907:a0d3:b0:878:54f4:ffe9 with SMTP id hw19-20020a170907a0d300b0087854f4ffe9mr3926573ejc.0.1674757910712; Thu, 26 Jan 2023 10:31:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674757910; cv=none; d=google.com; s=arc-20160816; b=oDLWV2P1NFv1kr/wuvsiNx+5BaQO4LoXaGGX/GDoC9G2hE/ef5JlZz6HgC4GHBDNLM 50Oq8ArlWUp+O9uf75IJgQmxVLqg4H8c5DoNleWqN89DFxmbkkdJsLFWGiNwKKHeUgSx hN6pS30KrrXvwX8gIaEBXWylZ5NjGKWQg8ruCix3dcmzLwjDKR/5XydSui2mIWQ7r3Ry tCZ/KqsXR2wQLkNshUg1BoP3X5fsjwPwQSuAKE115EdIbBK2T8DkG2wFYiWdBkmBPjON eu/n9HrjPv6d7F1lRlArQbp2jDvk0++ippLoK7bRay/Pb+nRbHFH0ThHrKbSs3m6/Pzs 49MA== 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=D6C2deZe1/i/tgKVya4AKYL2JwQGus2nrjFDc/OQJ3k=; b=s/IG0HbXpa3VpLohb/2h3tscYzDr+OgblBlojeIX1/vcniBcU7hqoApGB28A5s4cWG i4YYH8K4pXWAlRr/kd5lZwc1Lj63pbTpS3BQrRpMpPssc5PyEIvARXMeLGqt+7XrmPOK cB9r5ME7VXc9TfAqaDAcVKJxZ3PK9YlN1JPACFirXvDvFrOS5x3KRD4pqa/ZLHzAYMNy ghN1mE1ieKA/u6jBk4BbP8XAp8R7JiooINDOsxEy50Lzl8W1O5od4SsjtdDmxZTTLIBV 6wObrLbY6gI6m9ScgjZwMI2d5J0Imdmc2jUWSAzIdkKqlIay2Ihelxi3kWVlQW1xCNeb uorQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id az19-20020a170907905300b00877aab5d463si1791285ejc.477.2023.01.26.10.31.02; Thu, 26 Jan 2023 10:31:50 -0800 (PST) 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 S232026AbjAZS0l (ORCPT + 99 others); Thu, 26 Jan 2023 13:26:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231969AbjAZS0g (ORCPT ); Thu, 26 Jan 2023 13:26:36 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B767559B46 for ; Thu, 26 Jan 2023 10:26:34 -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 53EA3C14; Thu, 26 Jan 2023 10:27:16 -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 613063F71E; Thu, 26 Jan 2023 10:26:33 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de, jgg@nvidia.com, baolu.lu@linux.intel.com Subject: [PATCH v2 4/8] iommu: Factor out some helpers Date: Thu, 26 Jan 2023 18:26:19 +0000 Message-Id: <959a1e8d598c0a82f94123e017cafb273784f848.1674753627.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756110950810228007?= X-GMAIL-MSGID: =?utf-8?q?1756110950810228007?= 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 to hide the group_device detail from places that don't need to know. Similarly, the safety check for dev_iommu_ops() at 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. Signed-off-by: Robin Murphy Reviewed-by: Lu Baolu --- v2: - Add dev_iommu_ops_valid() helper - Add lockdep assertion [Jason] drivers/iommu/iommu.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 77f076030995..440bb3b7bded 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -284,6 +284,12 @@ static void dev_iommu_free(struct device *dev) kfree(param); } +/* Only needed in public APIs which allow unchecked devices for convenience */ +static bool dev_iommu_ops_valid(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; @@ -1096,6 +1102,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; +} + static int iommu_group_device_count(struct iommu_group *group) { struct group_device *entry; @@ -1907,7 +1919,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_iommu_ops_valid(dev)) return false; ops = dev_iommu_ops(dev); @@ -2770,8 +2782,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_iommu_ops_valid(dev)) { + const struct iommu_ops *ops = dev_iommu_ops(dev); if (ops->dev_enable_feat) return ops->dev_enable_feat(dev, feat); @@ -2786,8 +2798,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_iommu_ops_valid(dev)) { + const struct iommu_ops *ops = dev_iommu_ops(dev); if (ops->dev_disable_feat) return ops->dev_disable_feat(dev, feat); @@ -2816,7 +2828,6 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, struct device *prev_dev, int type) { struct iommu_domain *prev_dom; - struct group_device *grp_dev; int ret, dev_def_dom; struct device *dev; @@ -2848,8 +2859,7 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, } /* Since group has only one device */ - grp_dev = list_first_entry(&group->devices, struct group_device, list); - dev = grp_dev->dev; + dev = iommu_group_first_dev(group); if (prev_dev != dev) { dev_err_ratelimited(prev_dev, "Cannot change default domain: Device has been changed\n"); @@ -2946,7 +2956,6 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, static ssize_t iommu_group_store_type(struct iommu_group *group, const char *buf, size_t count) { - struct group_device *grp_dev; struct device *dev; int ret, req_type; @@ -2981,8 +2990,7 @@ static ssize_t iommu_group_store_type(struct iommu_group *group, } /* Since group has only one device */ - grp_dev = list_first_entry(&group->devices, struct group_device, list); - dev = grp_dev->dev; + dev = iommu_group_first_dev(group); get_device(dev); /* @@ -3107,21 +3115,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; } From patchwork Thu Jan 26 18:26:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 48842 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp423785wrn; Thu, 26 Jan 2023 10:31:52 -0800 (PST) X-Google-Smtp-Source: AMrXdXvkxMcBKagugB/jV2uDHuUEV0Ozbz+BKz/8pN6Bty0VXcrh94HzAflFRyrUbw87NkcgT/S3 X-Received: by 2002:a05:6402:4026:b0:49e:4786:a0e2 with SMTP id d38-20020a056402402600b0049e4786a0e2mr38546697eda.14.1674757912550; Thu, 26 Jan 2023 10:31:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674757912; cv=none; d=google.com; s=arc-20160816; b=tP3DZsO77vAfCnjT4/FOM57JBRsjoyz1GAubat3pE8GAyimFX+F74g0NJ99tlpIV/W ZienaHMEpV89ifP95DlTNAnymHLycm1OUXiUWyuqt+8QFJ49pSTAf0XkGWOL5Wql1F1g BIbdBYpjY2GWRj1g2K1kx/NzR6K44Td1ajCPD5k4cjl8kyHcdqvUvvsDVAvOQ+7qecpD IVKfTFCHk8fiabx7KXz74mdTP0Vu5XwZ9hXGENnnndb78UMhlWlwG8nzg2Yl3glJGxCI JAnspM4pq8L+55YcCjRVikfrCHS7+jpAURcBeELRj++iYscx/wisozhHc6BXXIEMie/S 73UQ== 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=JAnB+eIrWaS1743VmZQnfmJ1tIg6UxpRenNfjZfR3uE=; b=ZvhLkvX5cq54ZjR6b1ZZpO+/jJm1ACJjObGVXNvvMVu4qvEXjY+sbVgfBflITR4ghg IFDL15GzYbZvERnND9KXoYJ7WLwoiMw3cOZH51xOZ7kaumGsEJMHcUwh9TjfxvxSqVO4 /MyCRfsv7b7zS2mZBwtlYjT06sLo6smnGP4MsQogqljrWm+Hk3eG9j3d4Pwe+Rbgf2JE gohnZGMDQCdSYvZFkqmNnUnld2nlFK4viLBfxvJpQIyFyCyrv9IoeLSsJrIbMHyT+jFp SiaksC3f9suBiEL2l1y9etVGW5ANyW18wy635YDHVkfJGjQ3fTB8aTX7BPM63Am6Nxqn IBzw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id el3-20020a056402360300b004a18f2ffb81si321790edb.321.2023.01.26.10.31.19; Thu, 26 Jan 2023 10:31:52 -0800 (PST) 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 S232052AbjAZS0p (ORCPT + 99 others); Thu, 26 Jan 2023 13:26:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231995AbjAZS0i (ORCPT ); Thu, 26 Jan 2023 13:26:38 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5ED236A731 for ; Thu, 26 Jan 2023 10:26:35 -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 84C931424; Thu, 26 Jan 2023 10:27:17 -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 93A133F71E; Thu, 26 Jan 2023 10:26:34 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de, jgg@nvidia.com, baolu.lu@linux.intel.com Subject: [PATCH v2 5/8] iommu: Switch __iommu_domain_alloc() to device ops Date: Thu, 26 Jan 2023 18:26:20 +0000 Message-Id: <23b51c84247cb36e96c242d3aef8ef555b6d05cd.1674753627.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756110952629480522?= X-GMAIL-MSGID: =?utf-8?q?1756110952629480522?= 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. Signed-off-by: Robin Murphy Reviewed-by: Lu Baolu --- v2: - Explain the mitigation better in the warning message - Fix theoretical bug if alloc_dev is never assigned because the bus has no devices - Use new dev_iommu_ops_valid() since in theory VFIO noiommu makes device_iommu_mapped() -> dev_iommu_ops() an unsafe assumption [Baolu] drivers/iommu/iommu.c | 59 ++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 440bb3b7bded..bdc5fdf39d2b 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -89,7 +89,7 @@ static int iommu_bus_notifier(struct notifier_block *nb, unsigned long action, void *data); static int iommu_alloc_default_domain(struct iommu_group *group, struct device *dev); -static struct iommu_domain *__iommu_domain_alloc(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); @@ -1641,15 +1641,15 @@ static int iommu_get_def_domain_type(struct device *dev) return 0; } -static int iommu_group_alloc_default_domain(struct bus_type *bus, - struct iommu_group *group, +static int iommu_group_alloc_default_domain(struct iommu_group *group, + struct device *dev, unsigned int type) { struct iommu_domain *dom; - dom = __iommu_domain_alloc(bus, type); + dom = __iommu_domain_alloc(dev, type); if (!dom && type != IOMMU_DOMAIN_DMA) { - dom = __iommu_domain_alloc(bus, IOMMU_DOMAIN_DMA); + dom = __iommu_domain_alloc(dev, IOMMU_DOMAIN_DMA); if (dom) pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA", type, group->name); @@ -1674,7 +1674,7 @@ static int iommu_alloc_default_domain(struct iommu_group *group, type = iommu_get_def_domain_type(dev) ? : iommu_def_domain_type; - return iommu_group_alloc_default_domain(dev->bus, group, type); + return iommu_group_alloc_default_domain(group, dev, type); } /** @@ -1787,8 +1787,7 @@ static int probe_get_default_domain_type(struct device *dev, void *data) return 0; } -static void probe_alloc_default_domain(struct bus_type *bus, - struct iommu_group *group) +static void probe_alloc_default_domain(struct iommu_group *group) { struct __group_domain_type gtype; @@ -1798,10 +1797,12 @@ static void probe_alloc_default_domain(struct bus_type *bus, __iommu_group_for_each_dev(group, >ype, probe_get_default_domain_type); - if (!gtype.type) + if (!gtype.type) { gtype.type = iommu_def_domain_type; + gtype.dev = iommu_group_first_dev(group); + } - iommu_group_alloc_default_domain(bus, group, gtype.type); + iommu_group_alloc_default_domain(group, gtype.dev, gtype.type); } @@ -1864,7 +1865,7 @@ int bus_iommu_probe(struct bus_type *bus) list_del_init(&group->entry); /* Try to allocate default domain */ - probe_alloc_default_domain(bus, group); + probe_alloc_default_domain(group); if (!group->default_domain) { mutex_unlock(&group->mutex); @@ -1953,15 +1954,12 @@ void iommu_set_fault_handler(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_set_fault_handler); -static struct iommu_domain *__iommu_domain_alloc(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; - if (!ops) - return NULL; - domain = ops->domain_alloc(type); if (!domain) return NULL; @@ -1980,9 +1978,30 @@ static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, return domain; } +static int __iommu_domain_alloc_dev(struct device *dev, void *data) +{ + struct device **alloc_dev = data; + + if (!dev_iommu_ops_valid(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(struct bus_type *bus) { - return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); + 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(dev, IOMMU_DOMAIN_UNMANAGED); } EXPORT_SYMBOL_GPL(iommu_domain_alloc); @@ -2906,7 +2925,7 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, } /* Sets group->default_domain to the newly allocated domain */ - ret = iommu_group_alloc_default_domain(dev->bus, group, type); + ret = iommu_group_alloc_default_domain(group, dev, type); if (ret) goto out; @@ -3120,13 +3139,13 @@ 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); + 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; } From patchwork Thu Jan 26 18:26:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 48844 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp423934wrn; Thu, 26 Jan 2023 10:32:09 -0800 (PST) X-Google-Smtp-Source: AMrXdXtyluct5qh7D10NOIvBO0V8aly503pjAfxvRZLo8Liz95EMhTD2WEt06W49bMj4X9P74Xw+ X-Received: by 2002:a17:906:abd8:b0:7b1:316c:38f5 with SMTP id kq24-20020a170906abd800b007b1316c38f5mr40852280ejb.30.1674757929716; Thu, 26 Jan 2023 10:32:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674757929; cv=none; d=google.com; s=arc-20160816; b=gUHcgV7+VDR3sStRwt9EuVvSc4UvOHQX/nhGhLa3jQ8rU4fhfZSrcCY7boCNiANfY3 Geo2aoag5XXUFC/wBGgfjYZwLMatzrTO+nrt4g81+ok1rZnfY64MNGbCcGRhljCzRg3D aK1gKW0sL1ydIUUZXJb+efsieUPK6q1BcD1gWWyjj6dp5LyKJyfmQv5te9bmP60qGUJ/ 4Tysl9nYwa+8BIm+miW9hQhIJwNkG4KQplgDXan7JKlRyJ59SJ5IEAovGTlOfc+kVqDB c3Q9V7hJ5tSSJaY9L3KxUQd131scUhcng7TAefgxw5fHtnUriMwfN3dkZsKr9xKj5WMt v4kA== 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=J2JghlX4AhSQc4KETHhd8fje5a82UgciajbtaMwNL1w=; b=z0biezKPBykhlhM+WykJYtQZ4gYu3hpHOfZO7wfAbSxq+oH4pJpridgOERwNMmBtRp CjK1Kf9Kfzhb2J9GAfQ/OPtb/ifgVgl2jY95QEFLJ0AYCNQEjs81i6LcEXvmjnjAvPXp k0wZm8AZ+ejwc6ODsG+AfRou18qd3jV8qg57cgEKwLu59cTlC7lKQtCheSq7VdOLUbCB db0hkRVEgoy825/2AoWHxQpT40GNXgArcfIurSxGUN1B3NvSpXQyn/BC5zzzvES9qTQ0 UVTboT8OzEOZO54tzRpWp6PqL2Yk8Jy1CBHAr35YXyVk6cHlDcc7G7SZm1fiuyfvq0dv Avdw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gn19-20020a1709070d1300b0087777bb4127si2627391ejc.172.2023.01.26.10.31.23; Thu, 26 Jan 2023 10:32:09 -0800 (PST) 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 S232124AbjAZS0v (ORCPT + 99 others); Thu, 26 Jan 2023 13:26:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232039AbjAZS0m (ORCPT ); Thu, 26 Jan 2023 13:26:42 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2E73168103 for ; Thu, 26 Jan 2023 10:26:37 -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 BEC2F14BF; Thu, 26 Jan 2023 10:27:18 -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 C45D63F71E; Thu, 26 Jan 2023 10:26:35 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de, jgg@nvidia.com, baolu.lu@linux.intel.com Subject: [PATCH v2 6/8] iommu/arm-smmu: Don't register fwnode for legacy binding Date: Thu, 26 Jan 2023 18:26:21 +0000 Message-Id: <93678b819d70079797734d9a8b0ebdc5beca43d2.1674753627.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756110971016095462?= X-GMAIL-MSGID: =?utf-8?q?1756110971016095462?= When using the legacy binding we bypass the of_xlate mechanism, so avoid registering the instance fwnodes which act as keys for that. This will help __iommu_probe_device() to retrieve the registered ops the same way as for x86 etc. when no fwspec has previously been set up by of_xlate. Signed-off-by: Robin Murphy Reviewed-by: Jason Gunthorpe --- v2: No change drivers/iommu/arm/arm-smmu/arm-smmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c index 719fbca1fe52..607f06af01b6 100644 --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c @@ -2156,7 +2156,8 @@ static int arm_smmu_device_probe(struct platform_device *pdev) return err; } - err = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev); + err = iommu_device_register(&smmu->iommu, &arm_smmu_ops, + using_legacy_binding ? NULL : dev); if (err) { dev_err(dev, "Failed to register iommu\n"); iommu_device_sysfs_remove(&smmu->iommu); From patchwork Thu Jan 26 18:26:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 48841 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp423775wrn; Thu, 26 Jan 2023 10:31:52 -0800 (PST) X-Google-Smtp-Source: AMrXdXuMp3xTRcy+zqeh/a9xU3rfEL9wjHc3Ni2xs0uvMxj9llXbtkinX0SY4319tCwTlWmU6bNT X-Received: by 2002:a05:6402:1f05:b0:49e:16fc:b525 with SMTP id b5-20020a0564021f0500b0049e16fcb525mr39488255edb.41.1674757911910; Thu, 26 Jan 2023 10:31:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674757911; cv=none; d=google.com; s=arc-20160816; b=iLvOFI1kM2yXmRYzUTr70sGJj9SQ5oYnsMFK5ZRW61SRMxWwUSjzLKzcPFc7mCnDet 5uyfG95zP9CjMdERX0UTJrKzCkDacb6IYE2LRnXP6XSSe31sI19VhHmYb3XTGS4RV0aO DQYPtNtYDjb7QB1xa3ayrtFeCayCaz8mwpOu9dBreA3MG/uj7YC0wkA1oa34Cx/KQVrt YDp2PF3DnXg+pmFYaVNeKO68KyfR6GJrHDlce6YVWUnIk/SjGiy8p64aF+ahMEPJoSes Vvpb4qJGM6IotfI1wtuxN7kntO1iC+D7/tdw66cW1S/1axqNkOQH6ibSqoM2uf2+HIuI 1baQ== 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=nAb/3tliCXYRvOan0UHboy2A8WTpiIsl7or48fAj6m8=; b=mXeKu07go6O3CAzWbMe1M2boFq+VJhv5c6NedIqCyWrAYX7SnEEA7gE8wZrPSZk0fU ajo/y9DyL7mi48ESkEDStxkcgRZKSDns/lrKNI6SpuIea2feV3NnrU7OBYKBPQr6iCvW RO2LN1segfBeDw6FDPCtHmHNVEJr+PMINb1gcfRREOyXMsKJFV4p6i8uxMljdniYfwNp TciCHRL2Vng05RZxIUmZfT8ZrGZmGliRea8q800Y/lEQHIF3b943cAyOiN20I37RzBw2 rPX3GW8zGTP7Nw5xX5wE+FgztxTaA+Z1w1FWIhAdxeUURBociqu0j7Vrmi6baod2gxiz k1yQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cq18-20020a170906d51200b00872c15a159csi1967936ejc.263.2023.01.26.10.31.29; Thu, 26 Jan 2023 10:31:51 -0800 (PST) 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 S231969AbjAZS0y (ORCPT + 99 others); Thu, 26 Jan 2023 13:26:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232063AbjAZS0t (ORCPT ); Thu, 26 Jan 2023 13:26:49 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 359BF6BBDA for ; Thu, 26 Jan 2023 10:26:40 -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 92D9A1515; Thu, 26 Jan 2023 10:27:21 -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 6CB963F71E; Thu, 26 Jan 2023 10:26:38 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de, jgg@nvidia.com, baolu.lu@linux.intel.com, "Rafael J . Wysocki" , Greg Kroah-Hartman Subject: [PATCH v2 7/8] iommu: Retire bus ops Date: Thu, 26 Jan 2023 18:26:22 +0000 Message-Id: <198e82a6b1a28605409c395da4ec1a67b0e1587b.1674753627.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756110952173390670?= X-GMAIL-MSGID: =?utf-8?q?1756110952173390670?= With the rest of the API internals converted, it's time to finally tackle probe_device and how we bootstrap the per-device ops association to begin with. This ends up being disappointingly straightforward, since fwspec users are already doing it in order to find their of_xlate callback, and it works out that we can easily do the equivalent for other drivers too. Then shuffle the remaining awareness of iommu_ops into the couple of core headers that still need it, and breathe a sigh of relief. Ding dong the bus ops are gone! CC: Rafael J. Wysocki CC: Christoph Hellwig Acked-by: Greg Kroah-Hartman Signed-off-by: Robin Murphy Reviewed-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- v2: Clarify the iommu_ops_from_fwnode(NULL) assumption [Baolu] drivers/iommu/iommu.c | 28 +++++++++++++++++----------- include/acpi/acpi_bus.h | 2 ++ include/linux/device.h | 1 - include/linux/device/bus.h | 5 ----- include/linux/dma-map-ops.h | 1 + 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index bdc5fdf39d2b..7fb7c84e3dc6 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -219,13 +219,6 @@ int iommu_device_register(struct iommu_device *iommu, /* We need to be able to take module references appropriately */ if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner)) return -EINVAL; - /* - * Temporarily enforce global restriction to a single driver. This was - * already the de-facto behaviour, since any possible combination of - * existing drivers would compete for at least the PCI or platform bus. - */ - if (iommu_buses[0]->iommu_ops && iommu_buses[0]->iommu_ops != ops) - return -EBUSY; iommu->ops = ops; if (hwdev) @@ -235,10 +228,8 @@ int iommu_device_register(struct iommu_device *iommu, list_add_tail(&iommu->list, &iommu_device_list); spin_unlock(&iommu_device_lock); - for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) { - iommu_buses[i]->iommu_ops = ops; + for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) err = bus_iommu_probe(iommu_buses[i]); - } if (err) iommu_device_unregister(iommu); return err; @@ -310,12 +301,27 @@ static u32 dev_iommu_get_max_pasids(struct device *dev) static int __iommu_probe_device(struct device *dev, struct list_head *group_list) { - const struct iommu_ops *ops = dev->bus->iommu_ops; + const struct iommu_ops *ops; struct iommu_device *iommu_dev; + struct iommu_fwspec *fwspec; struct iommu_group *group; static DEFINE_MUTEX(iommu_probe_device_lock); int ret; + /* + * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU + * instances with non-NULL fwnodes, and client devices should have been + * identified with a fwspec by this point. Otherwise, we can currently + * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can + * be present, and that any of their registered instances has suitable + * ops for probing, and thus cheekily co-opt the same mechanism. + */ + fwspec = dev_iommu_fwspec_get(dev); + if (fwspec && fwspec->ops) + ops = fwspec->ops; + else + ops = iommu_ops_from_fwnode(NULL); + if (!ops) return -ENODEV; /* diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index cd3b75e08ec3..067dde9291c9 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -614,6 +614,8 @@ struct acpi_pci_root { /* helper */ +struct iommu_ops; + bool acpi_dma_supported(const struct acpi_device *adev); enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev); int acpi_iommu_fwspec_init(struct device *dev, u32 id, diff --git a/include/linux/device.h b/include/linux/device.h index 44e3acae7b36..f7a7ecafedd3 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -41,7 +41,6 @@ struct class; struct subsys_private; struct device_node; struct fwnode_handle; -struct iommu_ops; struct iommu_group; struct dev_pin_info; struct dev_iommu; diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h index d8b29ccd07e5..4ece3470112f 100644 --- a/include/linux/device/bus.h +++ b/include/linux/device/bus.h @@ -63,9 +63,6 @@ struct fwnode_handle; * this bus. * @pm: Power management operations of this bus, callback the specific * device driver's pm-ops. - * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU - * driver implementations to a bus and allow the driver to do - * bus-specific setup * @p: The private data of the driver core, only the driver core can * touch this. * @lock_key: Lock class key for use by the lock validator @@ -109,8 +106,6 @@ struct bus_type { const struct dev_pm_ops *pm; - const struct iommu_ops *iommu_ops; - struct subsys_private *p; struct lock_class_key lock_key; diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index d678afeb8a13..e8ebf0bf611b 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -10,6 +10,7 @@ #include struct cma; +struct iommu_ops; /* * Values for struct dma_map_ops.flags: From patchwork Thu Jan 26 18:26:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 48843 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp423850wrn; Thu, 26 Jan 2023 10:31:59 -0800 (PST) X-Google-Smtp-Source: AK7set9SBJcNQR6WwtMfDlGJPAF3weqg/qL7bPysmNpUpNFutGkdgmRwjiY4GwwTePjZU/Utxxj6 X-Received: by 2002:a17:906:f192:b0:878:7ef1:4a20 with SMTP id gs18-20020a170906f19200b008787ef14a20mr1187649ejb.4.1674757919587; Thu, 26 Jan 2023 10:31:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674757919; cv=none; d=google.com; s=arc-20160816; b=qZgtKRciqGldw7oswpnIv5CDAXKe1g5VQjUSbj9s9RNA/DzxPm3kNEhLoK3Ss1bSr8 d0mbu4EFmrAqUiEkY5QTeZs5MJSJbCOLewKyCqDzudEve/CQ1L2V98x0zTlROIHZPsGi QQy/LqG+YGskVIpebBQH/TbvS+Woa29KzQMDhlih/xzfp/DF9rNrHtmzMNQoA/4mtYt7 Dj5+DQDMFa6r+UbX/XKFQlPSbllUrkNygPRp61USySpsFf436+IzdgfJWGsV2pMj52Jb rEuxEVvGyJsk3RGW5dj5jZHKGYq+luzXBwxGetdLzHglF7B5vKRhkP2DQcpl0H5sgTTe dExA== 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=zmjdihi/qjnzz5KvfWSsaXbANLlEsmuxZ8W6GMjK0Hk=; b=AL5+PElAKty8nCy2+OB6X7YIhUAF6je9eDLP65iAVVb7K7GJ7QvUP3G2aYLKnqiKqb KHJ+ea1rnGUzA9mfQpeigCl9jeucp+QclcLULY2GHUoDBUcNjM+sAn9ues1jJ1Xfd4rD 3AKLw/al0hsSV1gnZ0myDbqL1A3q0MJXPBImlHLJXp4yERTZHy4Qxkl8SKjNvIkz5/uy erBz5qh/Nw35AW1FB/L8oQvAuV9wZRSzdolm/Q7WRi+iYkKvHTSUQ9pHAy39lIM3++lO UF5jAEsCKNWZGALi6kGEdVfBuMeFw4i+9wbShvmJ6eomWD0hz7U2E+pImcQ5gnQ4GpHV TpBQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fg1-20020a1709069c4100b008777ffc6481si1810167ejc.110.2023.01.26.10.31.36; Thu, 26 Jan 2023 10:31:59 -0800 (PST) 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 S231774AbjAZS07 (ORCPT + 99 others); Thu, 26 Jan 2023 13:26:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232127AbjAZS0v (ORCPT ); Thu, 26 Jan 2023 13:26:51 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4DA1A6D347 for ; Thu, 26 Jan 2023 10:26:41 -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 C36E1152B; Thu, 26 Jan 2023 10:27:22 -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 D28483F71E; Thu, 26 Jan 2023 10:26:39 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de, jgg@nvidia.com, baolu.lu@linux.intel.com Subject: [PATCH v2 8/8] iommu: Clean up open-coded ownership checks Date: Thu, 26 Jan 2023 18:26:23 +0000 Message-Id: X-Mailer: git-send-email 2.36.1.dirty In-Reply-To: References: MIME-Version: 1.0 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756110960354189549?= X-GMAIL-MSGID: =?utf-8?q?1756110960354189549?= Some drivers already implement their own defence against the possibility of being given someone else's device. Since this is now taken care of by the core code (and via a slightly different path from the original fwspec-based idea), let's clean them up. Signed-off-by: Robin Murphy Reviewed-by: Jason Gunthorpe --- v2: No change, but I'll note here that it's really about the fwspec->ops checks; the !fwspec clauses are just going along for the ride where that's clearly impossible as well. I plan to sweep for other redundant checks in future when looking at the relevant flows. drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 3 --- drivers/iommu/arm/arm-smmu/arm-smmu.c | 9 +-------- drivers/iommu/arm/arm-smmu/qcom_iommu.c | 14 ++------------ drivers/iommu/mtk_iommu.c | 7 +------ drivers/iommu/mtk_iommu_v1.c | 3 --- drivers/iommu/sprd-iommu.c | 8 +------- drivers/iommu/virtio-iommu.c | 3 --- 7 files changed, 5 insertions(+), 42 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index ab160198edd6..cb05d9771192 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2642,9 +2642,6 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) struct arm_smmu_master *master; struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - if (!fwspec || fwspec->ops != &arm_smmu_ops) - return ERR_PTR(-ENODEV); - if (WARN_ON_ONCE(dev_iommu_priv_get(dev))) return ERR_PTR(-EBUSY); diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c index 607f06af01b6..235550db0d59 100644 --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c @@ -1118,11 +1118,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct arm_smmu_device *smmu; int ret; - if (!fwspec || fwspec->ops != &arm_smmu_ops) { - dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n"); - return -ENXIO; - } - /* * FIXME: The arch/arm DMA API code tries to attach devices to its own * domains between of_xlate() and probe_device() - we have no way to cope @@ -1352,10 +1347,8 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) fwspec = dev_iommu_fwspec_get(dev); if (ret) goto out_free; - } else if (fwspec && fwspec->ops == &arm_smmu_ops) { - smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode); } else { - return ERR_PTR(-ENODEV); + smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode); } ret = -EINVAL; diff --git a/drivers/iommu/arm/arm-smmu/qcom_iommu.c b/drivers/iommu/arm/arm-smmu/qcom_iommu.c index d7be3adee426..3fe02720ab29 100644 --- a/drivers/iommu/arm/arm-smmu/qcom_iommu.c +++ b/drivers/iommu/arm/arm-smmu/qcom_iommu.c @@ -79,16 +79,6 @@ static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom) static const struct iommu_ops qcom_iommu_ops; -static struct qcom_iommu_dev * to_iommu(struct device *dev) -{ - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - - if (!fwspec || fwspec->ops != &qcom_iommu_ops) - return NULL; - - return dev_iommu_priv_get(dev); -} - static struct qcom_iommu_ctx * to_ctx(struct qcom_iommu_domain *d, unsigned asid) { struct qcom_iommu_dev *qcom_iommu = d->iommu; @@ -361,7 +351,7 @@ static void qcom_iommu_domain_free(struct iommu_domain *domain) static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) { - struct qcom_iommu_dev *qcom_iommu = to_iommu(dev); + struct qcom_iommu_dev *qcom_iommu = dev_iommu_priv_get(dev); struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); int ret; @@ -486,7 +476,7 @@ static bool qcom_iommu_capable(struct device *dev, enum iommu_cap cap) static struct iommu_device *qcom_iommu_probe_device(struct device *dev) { - struct qcom_iommu_dev *qcom_iommu = to_iommu(dev); + struct qcom_iommu_dev *qcom_iommu = dev_iommu_priv_get(dev); struct device_link *link; if (!qcom_iommu) diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c index d5a4955910ff..ce19a2a21e2a 100644 --- a/drivers/iommu/mtk_iommu.c +++ b/drivers/iommu/mtk_iommu.c @@ -776,16 +776,11 @@ static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain, static struct iommu_device *mtk_iommu_probe_device(struct device *dev) { struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - struct mtk_iommu_data *data; + struct mtk_iommu_data *data = dev_iommu_priv_get(dev); struct device_link *link; struct device *larbdev; unsigned int larbid, larbidx, i; - if (!fwspec || fwspec->ops != &mtk_iommu_ops) - return ERR_PTR(-ENODEV); /* Not a iommu client device */ - - data = dev_iommu_priv_get(dev); - if (!MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) return &data->iommu; diff --git a/drivers/iommu/mtk_iommu_v1.c b/drivers/iommu/mtk_iommu_v1.c index 78d0a84c704f..328833823181 100644 --- a/drivers/iommu/mtk_iommu_v1.c +++ b/drivers/iommu/mtk_iommu_v1.c @@ -478,9 +478,6 @@ static struct iommu_device *mtk_iommu_v1_probe_device(struct device *dev) idx++; } - if (!fwspec || fwspec->ops != &mtk_iommu_v1_ops) - return ERR_PTR(-ENODEV); /* Not a iommu client device */ - data = dev_iommu_priv_get(dev); /* Link the consumer device with the smi-larb device(supplier) */ diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c index ae94d74b73f4..85378f7cfd1f 100644 --- a/drivers/iommu/sprd-iommu.c +++ b/drivers/iommu/sprd-iommu.c @@ -358,13 +358,7 @@ static phys_addr_t sprd_iommu_iova_to_phys(struct iommu_domain *domain, static struct iommu_device *sprd_iommu_probe_device(struct device *dev) { - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - struct sprd_iommu_device *sdev; - - if (!fwspec || fwspec->ops != &sprd_iommu_ops) - return ERR_PTR(-ENODEV); - - sdev = dev_iommu_priv_get(dev); + struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev); return &sdev->iommu; } diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index 5b8fe9bfa9a5..59f1abd6ee53 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -945,9 +945,6 @@ static struct iommu_device *viommu_probe_device(struct device *dev) struct viommu_dev *viommu = NULL; struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); - if (!fwspec || fwspec->ops != &viommu_ops) - return ERR_PTR(-ENODEV); - viommu = viommu_get_by_fwnode(fwspec->iommu_fwnode); if (!viommu) return ERR_PTR(-ENODEV);