From patchwork Mon Aug 14 01:17:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 135096 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2466149vqi; Sun, 13 Aug 2023 19:00:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IETwDdQbMNFn11++85fSBbenlOht21ZijM41rDkqyf3WBWkirJkiFkUGXLrOwJGAkRk4lFr X-Received: by 2002:a05:6512:3a8b:b0:4f9:5396:ed1b with SMTP id q11-20020a0565123a8b00b004f95396ed1bmr6115989lfu.28.1691978449927; Sun, 13 Aug 2023 19:00:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691978449; cv=none; d=google.com; s=arc-20160816; b=QzHoyKLVg24P+U0jbwgTXiwoptAkOBYG0ZpDDzE6cZcuHgis3OU5cuXhBu8HY5CpII sMFa7e1zZNxP4hBskaGQQtEMxhilmQPZxwZITSLqiavamROSgBAWypobTmdGM+qmPlrl M1SWMbSYS1/fPt+zf3khK+ETsw3cwvCGNC30foUnb0JKQhkwOyoXkshF35N4uShEStyI oXdFWhPDR+hD05d5JqczhKzh5ggy4kVoPj6HM1D0TRtStQYjmlfPhM3+/A9GGtXUxR4c PjFAh2tpzSGSr+BJenX74S+v2OPWc1x9AbOcbjvrH1QANGXf/4O1O9msTnTyw34KKLvc hQYw== 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=caGdfsl8qrD1hqsG+ui4CV7T4ymj7b+qIJviCi6ZRbI=; fh=5v0QJkujOnxKSzhmeWiMX/0q9X35NwMz+SRMkKa4moc=; b=VdOpebJhvu51QzVWgrUMksV6bm46mDf7WIL9eSOieQmvFsbh0fX9nF3tbQvfBPgupa IZGcGRMWsrmU4KkLei4EZhnRpMIUHcK0V0eNGJyi8LAtTREmEDDE11Ea944ndBfzVUn8 BiBJeNsIsVozbqt744Gxg/R6ZsuWW747vvd19fQ0vOwlXBx78Bx5Vxrl/UGv6X2C+bSV DIXoW7SZDH1qB37wQB1FbwQkE+sAqKFLOt6sgE2CViN2c1kGDQ2vwvcdx9ZEMRXT8Kem eLXDCXKnfrzFyb16pqKAohTyQRtbUscthapbQpnoF0DX8oEKUGh7a1w+q3U7BgZYuSf2 SwDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MrspJpIn; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n4-20020aa7db44000000b00523384d6840si6942652edt.453.2023.08.13.19.00.26; Sun, 13 Aug 2023 19:00:49 -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=MrspJpIn; 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 S231834AbjHNBVG (ORCPT + 99 others); Sun, 13 Aug 2023 21:21:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231724AbjHNBUg (ORCPT ); Sun, 13 Aug 2023 21:20:36 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85B72E75; Sun, 13 Aug 2023 18:20:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1691976035; x=1723512035; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=277c02PzldzlnRnH2jM0ESjUVEL/whZkd2HGaLLh2vY=; b=MrspJpInasTenaA2hCo/inKO4d/avRNFMUOSiPLb82QNmcj33UBG6udA ol5MzsfKHPt7gkChUZwECEzxDDeabyAsIQGPK5hfWMhyIje2JSrqQd3YS snPTKpMxIDR4coxd2KXwE+Ycj/4cpFTd6wsI6ChRQRzT1aHu4h0dThJCz fhR5bvzoQ8HSonD3HYnO/uIcqp28KsHRV0q6ATV/eZLFZ3pewyV9aOv85 zt0gOdJLC8AGdsNDWTWRHoW6aZLIc3NzmR6OIquDqwpuPrLc9nFBhwa69 UNDkssJAMg1gLfxXWuhipbsAOmxepnEGneh933wwH540mU9AJ/j4ttb7k w==; X-IronPort-AV: E=McAfee;i="6600,9927,10801"; a="375645319" X-IronPort-AV: E=Sophos;i="6.01,171,1684825200"; d="scan'208";a="375645319" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Aug 2023 18:20:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10801"; a="726842296" X-IronPort-AV: E=Sophos;i="6.01,171,1684825200"; d="scan'208";a="726842296" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga007.jf.intel.com with ESMTP; 13 Aug 2023 18:20:31 -0700 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian , Jean-Philippe Brucker , Nicolin Chen Cc: Yi Liu , Jacob Pan , iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v2 2/3] iommu: Consolidate pasid dma ownership check Date: Mon, 14 Aug 2023 09:17:58 +0800 Message-Id: <20230814011759.102089-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230814011759.102089-1-baolu.lu@linux.intel.com> References: <20230814011759.102089-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774167994607554829 X-GMAIL-MSGID: 1774167994607554829 When switching device DMA ownership, it is required that all the device's pasid DMA be disabled. This is done by checking if the pasid array of the group is empty. Consolidate all the open code into a single helper. No intentional functionality change. Signed-off-by: Lu Baolu --- drivers/iommu/iommu.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index f1eba60e573f..d4a06a37ce39 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3127,6 +3127,19 @@ static bool iommu_is_default_domain(struct iommu_group *group) return false; } +/* + * Assert no PASID DMA when claiming or releasing group's DMA ownership. + * The device pasid interfaces are only for device drivers that have + * claimed the DMA ownership. Return true if no pasid DMA setup, otherwise + * return false with a WARN(). + */ +static bool assert_pasid_dma_ownership(struct iommu_group *group) +{ + lockdep_assert_held(&group->mutex); + + return !WARN_ON(!xa_empty(&group->pasid_array)); +} + /** * iommu_device_use_default_domain() - Device driver wants to handle device * DMA through the kernel DMA API. @@ -3147,7 +3160,7 @@ int iommu_device_use_default_domain(struct device *dev) mutex_lock(&group->mutex); if (group->owner_cnt) { if (group->owner || !iommu_is_default_domain(group) || - !xa_empty(&group->pasid_array)) { + !assert_pasid_dma_ownership(group)) { ret = -EBUSY; goto unlock_out; } @@ -3177,7 +3190,7 @@ void iommu_device_unuse_default_domain(struct device *dev) return; mutex_lock(&group->mutex); - if (!WARN_ON(!group->owner_cnt || !xa_empty(&group->pasid_array))) + if (!WARN_ON(!group->owner_cnt) && assert_pasid_dma_ownership(group)) group->owner_cnt--; mutex_unlock(&group->mutex); @@ -3211,7 +3224,7 @@ static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner) int ret; if ((group->domain && group->domain != group->default_domain) || - !xa_empty(&group->pasid_array)) + !assert_pasid_dma_ownership(group)) return -EBUSY; ret = __iommu_group_alloc_blocking_domain(group); @@ -3296,8 +3309,8 @@ EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner); static void __iommu_release_dma_ownership(struct iommu_group *group) { - if (WARN_ON(!group->owner_cnt || !group->owner || - !xa_empty(&group->pasid_array))) + if (WARN_ON(!group->owner_cnt || !group->owner) || + !assert_pasid_dma_ownership(group)) return; group->owner_cnt = 0;