Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Vasant Hegde <vasant.hegde@amd.com>
To: <iommu@lists.linux.dev>, <joro@8bytes.org>
Cc: <will@kernel.org>, <robin.murphy@arm.com>,
	<suravee.suthikulpanit@amd.com>, <jgg@nvidia.com>,
	Vasant Hegde <vasant.hegde@amd.com>, <stable@vger.kernel.org>,
	Santosh Shukla <santosh.shukla@amd.com>
Subject: [PATCH v2 05/12] iommu/amd: Keep the GCR3 table while PASIDs are attached
Date: Fri, 27 Nov 2026 11:54:40 +0000	[thread overview]
Message-ID: <20261127115447.209792-6-vasant.hegde@amd.com> (raw)
In-Reply-To: <20261127115447.209792-1-vasant.hegde@amd.com>

pci_dev_reset_iommu_prepare() attaches the blocked domain before clearing
the device's PASIDs. detach_device() frees the GCR3 table during that
transition, so a later amd_iommu_clear_gcr3() dereferences a NULL root.

Do not free the GCR3 table while it still has attached PASIDs. The RID
GCR3[0] entry is still cleared before detach, and setup_gcr3_table()
reuses the retained table on re-attach. Free it from
amd_iommu_release_device() instead, after the blocked-domain attach has
cleared the DTE.

Fixes: c9e8701132e6 ("iommu/amd: Setup GCR3 table in advance if domain is SVA capable")
Cc: stable@vger.kernel.org
Reported-by: Santosh Shukla <santosh.shukla@amd.com>
Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
---
 drivers/iommu/amd/iommu.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 0a1722110934..9c32dcb921f3 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -1949,12 +1949,12 @@ static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info,
 	int nid = iommu ? dev_to_node(&iommu->dev->dev) : NUMA_NO_NODE;
 	int domid;
 
+	if (gcr3_info->gcr3_tbl)
+		return 0;
+
 	if (levels > amd_iommu_max_glx_val)
 		return -EINVAL;
 
-	if (gcr3_info->gcr3_tbl)
-		return -EBUSY;
-
 	/* Allocate per device domain ID */
 	domid = amd_iommu_pdom_id_alloc();
 	if (domid <= 0)
@@ -2247,7 +2247,7 @@ static void destroy_gcr3_table(struct iommu_dev_data *dev_data,
 	if (pdom_is_v2_pgtbl_mode(pdom))
 		update_gcr3(dev_data, 0, 0, false);
 
-	if (gcr3_info->gcr3_tbl == NULL)
+	if (gcr3_info->gcr3_tbl == NULL || gcr3_info->pasid_cnt)
 		return;
 
 	free_gcr3_table(gcr3_info);
@@ -2523,6 +2523,21 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)
 	return iommu_dev;
 }
 
+static void amd_iommu_release_device(struct device *dev)
+{
+	struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
+
+	/*
+	 * A device that requires direct mappings is released to the identity
+	 * domain, whose DTE can still point at the GCR3 table. Keep the table
+	 * in that case; a later probe reuses it.
+	 */
+	if (dev_data->domain)
+		return;
+
+	destroy_gcr3_table(dev_data, NULL);
+}
+
 static struct iommu_group *amd_iommu_device_group(struct device *dev)
 {
 	if (dev_is_pci(dev))
@@ -3227,6 +3242,7 @@ const struct iommu_ops amd_iommu_ops = {
 	.domain_alloc_paging_flags = amd_iommu_domain_alloc_paging_flags,
 	.domain_alloc_sva = amd_iommu_domain_alloc_sva,
 	.probe_device = amd_iommu_probe_device,
+	.release_device = amd_iommu_release_device,
 	.device_group = amd_iommu_device_group,
 	.get_resv_regions = amd_iommu_get_resv_regions,
 	.is_attach_deferred = amd_iommu_is_attach_deferred,
-- 
2.31.1


      parent reply	other threads:[~2026-11-27 11:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261127115447.209792-1-vasant.hegde@amd.com>
2026-11-27 11:54 ` [PATCH v2 04/12] iommu/amd: Restore an SVA PASID after PCI reset Vasant Hegde
2026-11-27 11:54 ` Vasant Hegde [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127115447.209792-6-vasant.hegde@amd.com \
    --to=vasant.hegde@amd.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=robin.murphy@arm.com \
    --cc=santosh.shukla@amd.com \
    --cc=stable@vger.kernel.org \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox