From patchwork Mon Feb 27 19:54:13 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 9594131 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1DC5760453 for ; Mon, 27 Feb 2017 20:35:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0CCC420453 for ; Mon, 27 Feb 2017 20:35:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F3E42223A6; Mon, 27 Feb 2017 20:35:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6F57020453 for ; Mon, 27 Feb 2017 20:35:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751516AbdB0Ufq (ORCPT ); Mon, 27 Feb 2017 15:35:46 -0500 Received: from foss.arm.com ([217.140.101.70]:58902 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750971AbdB0Ufp (ORCPT ); Mon, 27 Feb 2017 15:35:45 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 7853D687; Mon, 27 Feb 2017 11:58:01 -0800 (PST) Received: from e106794-lin.cambridge.arm.com (e106794-lin.cambridge.arm.com [10.1.210.60]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id CE2153F3E1; Mon, 27 Feb 2017 11:57:58 -0800 (PST) From: Jean-Philippe Brucker Cc: Harv Abdulhamid , Will Deacon , Shanker Donthineni , Bjorn Helgaas , Sinan Kaya , Lorenzo Pieralisi , Catalin Marinas , Robin Murphy , Joerg Roedel , Nate Watterson , Alex Williamson , David Woodhouse , linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, iommu@lists.linux-foundation.org, kvm@vger.kernel.org Subject: [RFC PATCH 02/30] iommu/arm-smmu-v3: Link groups and domains Date: Mon, 27 Feb 2017 19:54:13 +0000 Message-Id: <20170227195441.5170-3-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170227195441.5170-1-jean-philippe.brucker@arm.com> References: <20170227195441.5170-1-jean-philippe.brucker@arm.com> To: unlisted-recipients:; (no To-header on input) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When removing a mapping from a domain, we need to send an invalidation to all of devices that might have stored it in their Address Translation Cache (ATC). This requires a lookup from smmu_domain to smmu_group. Add a list of groups in each domain. Although this domain-group link is already protected by iommu_group->mutex, the domain-to-group lookup will be done outside of the sections protected by that mutex. Add a spinlock for this case. Signed-off-by: Jean-Philippe Brucker --- drivers/iommu/arm-smmu-v3.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c index ce8b68fe254b..69d00416990d 100644 --- a/drivers/iommu/arm-smmu-v3.c +++ b/drivers/iommu/arm-smmu-v3.c @@ -651,9 +651,15 @@ struct arm_smmu_domain { }; struct iommu_domain domain; + + struct list_head groups; + spinlock_t groups_lock; }; struct arm_smmu_group { + struct arm_smmu_domain *domain; + struct list_head domain_head; + struct list_head devices; spinlock_t devices_lock; }; @@ -1408,6 +1414,9 @@ static struct iommu_domain *arm_smmu_domain_alloc(unsigned type) mutex_init(&smmu_domain->init_mutex); spin_lock_init(&smmu_domain->pgtbl_lock); + INIT_LIST_HEAD(&smmu_domain->groups); + spin_lock_init(&smmu_domain->groups_lock); + return &smmu_domain->domain; } @@ -1641,6 +1650,7 @@ static void arm_smmu_detach_dev(struct device *dev) static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) { int ret = 0; + unsigned long flags; struct iommu_group *group; struct arm_smmu_device *smmu; struct arm_smmu_group *smmu_group; @@ -1666,9 +1676,31 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return -ENOMEM; } - /* Already attached to a different domain? */ - if (!ste->bypass) + /* + * Already attached to a different domain? This happens when we're + * switching from default domain to unmanaged domain, and back. We + * assume here that, when switching from old domain to new domain, old + * domain doesn't have any live mapping anymore. This is an important + * requirement because here we remove the group-domain link when we + * re-attach the first device in a group. Other devices in that group + * might still be attached to the old domain, and will be reattached in + * a moment. + * + * We also take this path when attaching for the very first time, just + * after the STE is initialized. + */ + if (!ste->bypass) { + struct arm_smmu_domain *other_domain = smmu_group->domain; + + if (other_domain) { + spin_lock_irqsave(&other_domain->groups_lock, flags); + list_del(&smmu_group->domain_head); + spin_unlock_irqrestore(&other_domain->groups_lock, flags); + + smmu_group->domain = NULL; + } arm_smmu_detach_dev(dev); + } mutex_lock(&smmu_domain->init_mutex); @@ -1688,6 +1720,14 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) goto out_unlock; } + if (!smmu_group->domain) { + smmu_group->domain = smmu_domain; + + spin_lock_irqsave(&smmu_domain->groups_lock, flags); + list_add(&smmu_group->domain_head, &smmu_domain->groups); + spin_unlock_irqrestore(&smmu_domain->groups_lock, flags); + } + ste->bypass = false; ste->valid = true;