From nobody Fri Dec 19 21:48:52 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 D6AFDC433FE for ; Fri, 18 Nov 2022 13:32:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242152AbiKRNc4 (ORCPT ); Fri, 18 Nov 2022 08:32:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242097AbiKRNcR (ORCPT ); Fri, 18 Nov 2022 08:32:17 -0500 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51DFC7FC23 for ; Fri, 18 Nov 2022 05:32:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668778336; x=1700314336; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RfcpiEbdbskkPeCw2JzA4bI6d0W2gVvyufESQpbn4Bk=; b=bBHZu+ppST+XqpJhGOQLTqREmdEzh9Zj/k/iumYAJLTOnIsejkZy0QTN lzQC9HuMwbSg8Fi87chlt3enqH2s6jMlH2JJ/RSyXz4znKvNcnmF644KK jH+OOzQkYRGn0UsYjliFzEzeMaLOvuWvp1uQfjFR8S6Hc9LlEhLWnVMFK hbRszyOU7NFww1tTi7OeuzCf0pGb5n2rge9m0hojELQP3Rpn5tI18Tjdo 5fnujuBM2oO0Z9YnWk0ntdcgFlCi65kWmRxX2lwFzA6EnCz7AZwWKoRKK GXNtBZ7TSr0rIgKDLaGIDOcnczqAWSAzd8Dh9X8l1+LI+qL0twAYiklxV g==; X-IronPort-AV: E=McAfee;i="6500,9779,10534"; a="314285094" X-IronPort-AV: E=Sophos;i="5.96,174,1665471600"; d="scan'208";a="314285094" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Nov 2022 05:32:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10534"; a="642512007" X-IronPort-AV: E=Sophos;i="5.96,174,1665471600"; d="scan'208";a="642512007" Received: from allen-box.sh.intel.com ([10.239.159.48]) by fmsmga007.fm.intel.com with ESMTP; 18 Nov 2022 05:32:09 -0800 From: Lu Baolu To: iommu@lists.linux.dev Cc: Joerg Roedel , Kevin Tian , Will Deacon , Robin Murphy , Liu Yi L , Jacob jun Pan , linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v4 2/7] iommu/vt-d: Add device_block_translation() helper Date: Fri, 18 Nov 2022 21:24:46 +0800 Message-Id: <20221118132451.114406-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221118132451.114406-1-baolu.lu@linux.intel.com> References: <20221118132451.114406-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" If domain attaching to device fails, the IOMMU driver should bring the device to blocking DMA state. The upper layer is expected to recover it by attaching a new domain. Use device_block_translation() in the error path of dev_attach to make the behavior specific. The difference between device_block_translation() and the previous dmar_remove_one_dev_info() is that, in the scalable mode, it is the RID2PASID entry instead of context entry being cleared. As a result, enabling PCI capabilities is moved up. Signed-off-by: Lu Baolu Reviewed-by: Kevin Tian --- drivers/iommu/intel/iommu.c | 44 ++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 6b8a24f68da8..6aafb86ef5c3 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -277,7 +277,7 @@ static LIST_HEAD(dmar_satc_units); #define for_each_rmrr_units(rmrr) \ list_for_each_entry(rmrr, &dmar_rmrr_units, list) =20 -static void dmar_remove_one_dev_info(struct device *dev); +static void device_block_translation(struct device *dev); =20 int dmar_disabled =3D !IS_ENABLED(CONFIG_INTEL_IOMMU_DEFAULT_ON); int intel_iommu_sm =3D IS_ENABLED(CONFIG_INTEL_IOMMU_SCALABLE_MODE_DEFAULT= _ON); @@ -1400,7 +1400,7 @@ static void iommu_enable_pci_caps(struct device_domai= n_info *info) { struct pci_dev *pdev; =20 - if (!info || !dev_is_pci(info->dev)) + if (!dev_is_pci(info->dev)) return; =20 pdev =3D to_pci_dev(info->dev); @@ -2045,7 +2045,6 @@ static int domain_context_mapping_one(struct dmar_dom= ain *domain, } else { iommu_flush_write_buffer(iommu); } - iommu_enable_pci_caps(info); =20 ret =3D 0; =20 @@ -2487,7 +2486,7 @@ static int domain_add_dev_info(struct dmar_domain *do= main, struct device *dev) dev, PASID_RID2PASID); if (ret) { dev_err(dev, "Setup RID2PASID failed\n"); - dmar_remove_one_dev_info(dev); + device_block_translation(dev); return ret; } } @@ -2495,10 +2494,12 @@ static int domain_add_dev_info(struct dmar_domain *= domain, struct device *dev) ret =3D domain_context_mapping(domain, dev); if (ret) { dev_err(dev, "Domain context map failed\n"); - dmar_remove_one_dev_info(dev); + device_block_translation(dev); return ret; } =20 + iommu_enable_pci_caps(info); + return 0; } =20 @@ -4109,6 +4110,37 @@ static void dmar_remove_one_dev_info(struct device *= dev) info->domain =3D NULL; } =20 +/* + * Clear the page table pointer in context or pasid table entries so that + * all DMA requests without PASID from the device are blocked. If the page + * table has been set, clean up the data structures. + */ +static void device_block_translation(struct device *dev) +{ + struct device_domain_info *info =3D dev_iommu_priv_get(dev); + struct intel_iommu *iommu =3D info->iommu; + unsigned long flags; + + iommu_disable_dev_iotlb(info); + if (!dev_is_real_dma_subdevice(dev)) { + if (sm_supported(iommu)) + intel_pasid_tear_down_entry(iommu, dev, + PASID_RID2PASID, false); + else + domain_context_clear(info); + } + + if (!info->domain) + return; + + spin_lock_irqsave(&info->domain->lock, flags); + list_del(&info->link); + spin_unlock_irqrestore(&info->domain->lock, flags); + + domain_detach_iommu(info->domain, iommu); + info->domain =3D NULL; +} + static int md_domain_init(struct dmar_domain *domain, int guest_width) { int adjust_width; @@ -4232,7 +4264,7 @@ static int intel_iommu_attach_device(struct iommu_dom= ain *domain, struct device_domain_info *info =3D dev_iommu_priv_get(dev); =20 if (info->domain) - dmar_remove_one_dev_info(dev); + device_block_translation(dev); } =20 ret =3D prepare_domain_attach_device(domain, dev); --=20 2.34.1