From nobody Thu Dec 18 20:23:23 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C496FC04FE0 for ; Mon, 14 Aug 2023 01:21:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231838AbjHNBVG (ORCPT ); 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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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_gro= up *group) return false; } =20 +/* + * 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 *de= v) 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 =3D -EBUSY; goto unlock_out; } @@ -3177,7 +3190,7 @@ void iommu_device_unuse_default_domain(struct device = *dev) return; =20 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--; =20 mutex_unlock(&group->mutex); @@ -3211,7 +3224,7 @@ static int __iommu_take_dma_ownership(struct iommu_gr= oup *group, void *owner) int ret; =20 if ((group->domain && group->domain !=3D group->default_domain) || - !xa_empty(&group->pasid_array)) + !assert_pasid_dma_ownership(group)) return -EBUSY; =20 ret =3D __iommu_group_alloc_blocking_domain(group); @@ -3296,8 +3309,8 @@ EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner); =20 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; =20 group->owner_cnt =3D 0; --=20 2.34.1