Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3845360imw; Mon, 11 Jul 2022 17:38:57 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u3C3nXwYKBASlBNwKIYCNb/dUQIjauGCzhMQV8f2UGLq7o8/doRAoCnLu5poak3/tso+rs X-Received: by 2002:a17:906:794e:b0:726:3fbb:5761 with SMTP id l14-20020a170906794e00b007263fbb5761mr20875576ejo.346.1657586337786; Mon, 11 Jul 2022 17:38:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657586337; cv=none; d=google.com; s=arc-20160816; b=n4neNQ41FEA6zXCQi6hLck8bNjCNo8SXygnZ/aofptKOEu3eCF6zIkfRf7y6OGyXou 76DhD9DECgt84O9c3AbLnJhhLgM6rQPG0SgGW76fsDxBWeXcNUk8tgt9v9L3N4T9AVGi 2CF0LijQNquTvO+WSNl1N99jjAm/ArkV0nVcPe9wmllc5s+EsmSylgSiNvttE4Ytohzp bSBtzLtbzElEhP7tROZlhopukMxVN5SL66W78dexrk5DbKguz4BXx4Jzqf0doxG0KoUK BuEdOqr4mZnvh/XvApc9LseLgGY91xZdpQ4XYaciZe4PKdHkZTvha0zeNMp4R6tC0pa/ KWxA== 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 :dkim-signature; bh=HToABuZp89vO7QB4sgYmj+ogO+ke+G3ik7PEVOz1Xvs=; b=EbP6Hh0NTN/ZeWt+m/FL99N7ERFfcTZN3BQs2llACHNEXSh3ZIVjmSfdNN2MvJFuQi 6eEqRz4Fy/5Bx++JoQckW1d1UsglDjqIYdLsX0XHZ9KwK8dQvtdjEtmID5fbJb6l+2xV 3WpyzmTrXEdOOBxHKZCUxguLRJkQoXSo5U9I921mlrVnW9zcz4+urv0F+A7vIRCjqhPO ZM6M0o5YYqCAmPDZJ/5y+XKJ405AE3lN3cAJ3DP+6wJLjBTcmcFP9c4QLv4SSzFRkzow n4OyNJQrryyGA5tY/aNxots6Q+h49Mmj73dMenL/rARYpDIQZ1n10F5ZxADnw+fIGYSY /LMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ggFkal6T; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ho7-20020a1709070e8700b0072b4b8a0ce7si11460468ejc.946.2022.07.11.17.38.33; Mon, 11 Jul 2022 17:38:57 -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; dkim=pass header.i=@intel.com header.s=Intel header.b=ggFkal6T; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231894AbiGLAP2 (ORCPT + 99 others); Mon, 11 Jul 2022 20:15:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229929AbiGLAPF (ORCPT ); Mon, 11 Jul 2022 20:15:05 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8296193DD for ; Mon, 11 Jul 2022 17:14:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1657584868; x=1689120868; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ja5IsQKhb4CVYQ9JckY0NN2jrC0vCxngqZ6SO6WGVVI=; b=ggFkal6Tn+lNim+5u/pE4hiHzotISJ42KIKtEoOjrM2yhXlzGK0EEVnd Glb/M9PWoA3A6MaElZ2zqaJyvRR/UiqjTG4qSHUYMNcvN2x1PQ3zlPnak q34yggDghaCaU2QdPiS9vnzl+cai1iXbn7npxr3pELLrY6klCU3nDLwEl jcs+I5lRN3tY8BOtQYwBwr/UVqfEPluoWmGBN+JsqHxi2QprAhQt4w1N3 DJYiycJ8Kzo+5BDVy+JZi0XNA7wbLyN4Ywft94VS+hclklb8IHzK4a8ab fqkrxzaH9Vg0ay4jW0bLFjg8lDdUSWnpRbZLLOKBy7aj+rSUVh1CzS8Wl A==; X-IronPort-AV: E=McAfee;i="6400,9594,10405"; a="285929300" X-IronPort-AV: E=Sophos;i="5.92,264,1650956400"; d="scan'208";a="285929300" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jul 2022 17:14:26 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,264,1650956400"; d="scan'208";a="592445821" Received: from allen-box.sh.intel.com ([10.239.159.48]) by orsmga007.jf.intel.com with ESMTP; 11 Jul 2022 17:14:02 -0700 From: Lu Baolu To: Joerg Roedel Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 14/25] iommu/vt-d: Acquiring lock in domain ID allocation helpers Date: Tue, 12 Jul 2022 08:08:57 +0800 Message-Id: <20220712000908.3431936-15-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220712000908.3431936-1-baolu.lu@linux.intel.com> References: <20220712000908.3431936-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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 The iommu->lock is used to protect the per-IOMMU domain ID resource. Moving the lock into the ID alloc/free helpers makes the code more compact. At the same time, the device_domain_lock is irrelevant to the domain ID resource, remove its assertion as well. Signed-off-by: Lu Baolu Reviewed-by: Kevin Tian Link: https://lore.kernel.org/r/20220706025524.2904370-7-baolu.lu@linux.intel.com --- drivers/iommu/intel/iommu.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 93f01082dce1..7f03576e72d7 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1779,16 +1779,13 @@ static struct dmar_domain *alloc_domain(unsigned int type) return domain; } -/* Must be called with iommu->lock */ static int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) { unsigned long ndomains; - int num; - - assert_spin_locked(&device_domain_lock); - assert_spin_locked(&iommu->lock); + int num, ret = 0; + spin_lock(&iommu->lock); domain->iommu_refcnt[iommu->seq_id] += 1; if (domain->iommu_refcnt[iommu->seq_id] == 1) { ndomains = cap_ndoms(iommu->cap); @@ -1797,7 +1794,8 @@ static int domain_attach_iommu(struct dmar_domain *domain, if (num >= ndomains) { pr_err("%s: No free domain ids\n", iommu->name); domain->iommu_refcnt[iommu->seq_id] -= 1; - return -ENOSPC; + ret = -ENOSPC; + goto out_unlock; } set_bit(num, iommu->domain_ids); @@ -1806,7 +1804,9 @@ static int domain_attach_iommu(struct dmar_domain *domain, domain_update_iommu_cap(domain); } - return 0; +out_unlock: + spin_unlock(&iommu->lock); + return ret; } static void domain_detach_iommu(struct dmar_domain *domain, @@ -1814,9 +1814,7 @@ static void domain_detach_iommu(struct dmar_domain *domain, { int num; - assert_spin_locked(&device_domain_lock); - assert_spin_locked(&iommu->lock); - + spin_lock(&iommu->lock); domain->iommu_refcnt[iommu->seq_id] -= 1; if (domain->iommu_refcnt[iommu->seq_id] == 0) { num = domain->iommu_did[iommu->seq_id]; @@ -1824,6 +1822,7 @@ static void domain_detach_iommu(struct dmar_domain *domain, domain_update_iommu_cap(domain); domain->iommu_did[iommu->seq_id] = 0; } + spin_unlock(&iommu->lock); } static inline int guestwidth_to_adjustwidth(int gaw) @@ -2472,9 +2471,7 @@ static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev) spin_lock(&device_domain_lock); info->domain = domain; - spin_lock(&iommu->lock); ret = domain_attach_iommu(domain, iommu); - spin_unlock(&iommu->lock); if (ret) { spin_unlock(&device_domain_lock); return ret; @@ -4178,10 +4175,7 @@ static void __dmar_remove_one_dev_info(struct device_domain_info *info) } list_del(&info->link); - - spin_lock(&iommu->lock); domain_detach_iommu(domain, iommu); - spin_unlock(&iommu->lock); } static void dmar_remove_one_dev_info(struct device *dev) -- 2.25.1