Re: [PATCH v2 11/12] iommu/amd: Add support for nested domain attach/detach

From: Nicolin Chen

Date: Thu Oct 02 2025 - 15:05:25 EST


On Wed, Oct 01, 2025 at 06:09:53AM +0000, Suravee Suthikulpanit wrote:
> /* sets a specific bit in the device table entry. */
> diff --git a/drivers/iommu/amd/nested.c b/drivers/iommu/amd/nested.c
> index 11a0237174bb..5a0c369ba283 100644
> --- a/drivers/iommu/amd/nested.c
> +++ b/drivers/iommu/amd/nested.c
> @@ -11,9 +11,7 @@
> #include "amd_iommu.h"
> #include "amd_iommu_types.h"
>
> -static const struct iommu_domain_ops nested_domain_ops = {
> - .free = amd_iommu_domain_free,
> -};

Oh no, amd_iommu_domain_free() with to_pdomain() won't work. So
you should move the nested_domain_free() to the previous patch,
pairing with amd_iommu_alloc_domain_nested().

> +static void set_dte_nested(struct amd_iommu *iommu,
> + struct dev_table_entry *gdte,
> + struct nested_domain *ndom,
> + struct iommu_dev_data *dev_data)
> +{
> + struct dev_table_entry *initial_dte;
> + struct dev_table_entry new = {0};
> + struct protection_domain *pdom = dev_data->parent;
> +
> + if (WARN_ON(!ndom || !pdom || (pdom->iop.mode == PAGE_MODE_NONE)))
> + return;
> +
> + amd_iommu_make_clear_dte(dev_data, &new);
> +
> + new.data[0] |= iommu_virt_to_phys(pdom->iop.root);
> + new.data[0] |= FIELD_PREP(DTE_MODE_MASK, pdom->iop.mode);
> + new.data[0] |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_TV;
> + new.data[0] |= (DTE_FLAG_PPR & gdte->data[0]);

new.data[0] |= DTE_FLAG_PPR & gdte->data[0];

> + /* Guest translation stuff */
> + new.data[0] |= (gdte->data[0] & (DTE_GLX | DTE_FLAG_GV | DTE_FLAG_GIOV));

new.data[0] |= gdte->data[0] & (DTE_GLX | DTE_FLAG_GV | DTE_FLAG_GIOV);
> +
> + /* GCR3 table */
> + new.data[0] |= (gdte->data[0] & DTE_GCR3_14_12);
> + new.data[1] |= (gdte->data[1] & (DTE_GCR3_30_15 | DTE_GCR3_51_31));
> +
> + /* Guest paging mode */
> + new.data[2] |= (gdte->data[2] & DTE_GPT_LEVEL_MASK);

All these outer parentheses are redundant.

> +static int nested_attach_device(struct iommu_domain *dom, struct device *dev)
> +{
> + struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
> + struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
> + struct nested_domain *ndom = to_ndomain(dom);
> + struct dev_table_entry *gdte = &ndom->guest_dte;
> + int ret = 0;
> +
> + if (dev_data->ndom == ndom)
> + return ret;
> +
> + if (!dev_is_pci(dev))
> + return -EINVAL;
> +
> + /* Currently only support GCR3TRPMode with nested translation */
> + if (!check_feature2(FEATURE_GCR3TRPMODE))
> + return -EOPNOTSUPP;

The amd_iommu_alloc_domain_nested() should probably validate this
feature, so !FEATURE_GCR3TRPMODE wouldn't allocate a nested domain
at the first place, and then no need to revalidate it in attach().

> +
> + /* We need to check host capability before setting the mode */
> + if ((FIELD_GET(DTE_GPT_LEVEL_MASK, gdte->data[2]) == GUEST_PGTABLE_5_LEVEL) &&
> + (amd_iommu_gpt_level < PAGE_MODE_5_LEVEL))
> + return -EOPNOTSUPP;

Ditto.

The attach callback function should only check things related to
the compatibility between a device and a domain, while this is a
domain specific validation. Better do it in alloc() IMHO.

> + WARN_ON(dev_data->ndom);

return -EBUSY;
?

With these fixed,

Reviewed-by: Nicolin Chen <nicolinc@xxxxxxxxxx>