All of lore.kernel.org
 help / color / mirror / Atom feed
From: Baolu Lu <baolu.lu@linux.intel.com>
To: Samiullah Khawaja <skhawaja@google.com>,
	David Woodhouse <dwmw2@infradead.org>,
	Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
	Jason Gunthorpe <jgg@ziepe.ca>
Cc: Robin Murphy <robin.murphy@arm.com>,
	Kevin Tian <kevin.tian@intel.com>,
	Alex Williamson <alex@shazbot.org>, Shuah Khan <shuah@kernel.org>,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
	kvm@vger.kernel.org, Pratyush Yadav <pratyush@kernel.org>,
	Pasha Tatashin <pasha.tatashin@soleen.com>,
	David Matlack <dmatlack@google.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Pranjal Shrivastava <praan@google.com>,
	Vipin Sharma <vipinsh@google.com>
Subject: Re: [PATCH v4 12/18] iommu/vt-d: Handle reattach of the restored domain
Date: Thu, 27 Aug 2026 16:12:01 +0800	[thread overview]
Message-ID: <5b920299-260b-4025-ac4f-e8f83beebf95@linux.intel.com> (raw)
In-Reply-To: <20260808022723.3893618-13-skhawaja@google.com>

On 8/8/26 10:27, Samiullah Khawaja wrote:
> Reattach the restored domain to the preserved device using restored
> domain ID. While reattaching do not setup the context and PASID entries
> as those are preserved during liveupdate.
> 
> Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
> ---
>   drivers/iommu/intel/iommu.c      |   9 ++-
>   drivers/iommu/intel/iommu.h      |  10 +++
>   drivers/iommu/intel/liveupdate.c | 120 +++++++++++++++++++++++++++++++
>   3 files changed, 136 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> index 42d3ff6db281..5370214629f4 100644
> --- a/drivers/iommu/intel/iommu.c
> +++ b/drivers/iommu/intel/iommu.c
> @@ -864,7 +864,7 @@ static bool dev_needs_extra_dtlb_flush(struct pci_dev *pdev)
>   	return true;
>   }
>   
> -static void iommu_enable_pci_ats(struct device_domain_info *info)
> +void intel_iommu_enable_pci_ats(struct device_domain_info *info)
>   {
>   	struct pci_dev *pdev;
>   
> @@ -1225,7 +1225,7 @@ domain_context_mapping(struct dmar_domain *domain, struct device *dev)
>   	if (ret)
>   		return ret;
>   
> -	iommu_enable_pci_ats(info);
> +	intel_iommu_enable_pci_ats(info);
>   
>   	return 0;
>   }
> @@ -3164,6 +3164,9 @@ static int intel_iommu_attach_device(struct iommu_domain *domain,
>   {
>   	int ret;
>   
> +	if (dev_iommu_restored_state(dev))
> +		return intel_iommu_restore_device(domain, dev);
> +
>   	device_block_translation(dev);
>   
>   	ret = paging_domain_compatible(domain, dev);
> @@ -3376,7 +3379,7 @@ static void intel_iommu_probe_finalize(struct device *dev)
>   		info->pasid_enabled = 1;
>   
>   	if (sm_supported(iommu) && !dev_is_real_dma_subdevice(dev)) {
> -		iommu_enable_pci_ats(info);
> +		intel_iommu_enable_pci_ats(info);
>   		/* Assign a DEVTLB cache tag to the default domain. */
>   		if (info->ats_enabled && info->domain) {
>   			u16 did = domain_id_iommu(info->domain, iommu);
> diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
> index b33a12528066..1ef3b9309d44 100644
> --- a/drivers/iommu/intel/iommu.h
> +++ b/drivers/iommu/intel/iommu.h
> @@ -1187,6 +1187,8 @@ void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu);
>   void device_block_translation(struct device *dev);
>   int paging_domain_compatible(struct iommu_domain *domain, struct device *dev);
>   
> +void intel_iommu_enable_pci_ats(struct device_domain_info *info);
> +
>   struct dev_pasid_info *
>   domain_add_dev_pasid(struct iommu_domain *domain,
>   		     struct device *dev, ioasid_t pasid);
> @@ -1309,6 +1311,8 @@ void intel_iommu_unpreserve(struct iommu_device *iommu,
>   void clear_unpreserved_context_entries(struct intel_iommu *iommu);
>   void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu,
>   					       struct iommu_hw_ser *iommu_ser);
> +int intel_iommu_restore_device(struct iommu_domain *domain,
> +			       struct device *dev);
>   #else
>   static inline int intel_iommu_preserve_device(struct device *dev,
>   					      struct iommu_device_ser *device_ser)
> @@ -1340,6 +1344,12 @@ static inline void intel_iommu_liveupdate_restore_root_table(struct intel_iommu
>   							     struct iommu_hw_ser *iommu_ser)
>   {
>   }
> +
> +static inline int intel_iommu_restore_device(struct iommu_domain *domain,
> +					     struct device *dev)
> +{
> +	return -EOPNOTSUPP;
> +}
>   #endif
>   
>   #ifdef CONFIG_INTEL_IOMMU_SVM
> diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupdate.c
> index 480eab2d966b..05dea3893399 100644
> --- a/drivers/iommu/intel/liveupdate.c
> +++ b/drivers/iommu/intel/liveupdate.c
> @@ -12,6 +12,7 @@
>   #include <linux/iommu-liveupdate.h>
>   #include <linux/module.h>
>   #include <linux/pci.h>
> +#include <linux/pci-ats.h>
>   
>   #include "iommu.h"
>   #include "../iommu-pages.h"
> @@ -342,6 +343,125 @@ void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu,
>   	BUG_ON(iommu_for_each_preserved_device(_restore_used_domain_ids, iommu));
>   }
>   
> +static void domain_detach_reattached_iommu(struct dmar_domain *domain,
> +					   struct intel_iommu *iommu)
> +{
> +	struct iommu_domain_info *info;
> +
> +	guard(mutex)(&iommu->did_lock);
> +	info = xa_load(&domain->iommu_array, iommu->seq_id);
> +	if (--info->refcnt == 0) {
> +		xa_erase(&domain->iommu_array, iommu->seq_id);
> +		kfree(info);
> +	}
> +}
> +
> +static int domain_reattach_iommu(struct dmar_domain *domain,
> +				 struct intel_iommu *iommu,
> +				 struct iommu_device_ser *device_ser)
> +{
> +	struct iommu_domain_info *info, *curr;
> +	int restored_did;
> +	int ret;
> +
> +	if (!iommu_domain_restored_state(&domain->domain))
> +		return -EINVAL;
> +
> +	restored_did = device_ser->domain_iommu_ser.attachment_id;
> +	if (!ida_exists(&iommu->domain_ida, restored_did))
> +		return -EINVAL;

It seems that checking only whether the domain ID is reserved on this
IOMMU may not be sufficient. It would be safer to also verify that:

- device_ser->domain_iommu_ser.iommu_phys matches iommu->reg_phys, and
- device_ser->domain_iommu_ser.domain_phys matches @domain.

?

> +
> +	info = kzalloc_obj(*info);
> +	if (!info)
> +		return -ENOMEM;
> +
> +	guard(mutex)(&iommu->did_lock);
> +	curr = xa_load(&domain->iommu_array, iommu->seq_id);
> +	if (curr) {
> +		curr->refcnt++;
> +		kfree(info);
> +		return 0;
> +	}
> +
> +	info->refcnt	= 1;
> +	info->did	= restored_did;
> +	info->iommu	= iommu;
> +	curr = xa_cmpxchg(&domain->iommu_array, iommu->seq_id,
> +			  NULL, info, GFP_KERNEL);
> +	if (curr) {
> +		ret = xa_err(curr) ? : -EBUSY;
> +		goto err_unlock;
> +	}
> +
> +	return 0;
> +
> +err_unlock:
> +	kfree(info);
> +	return ret;
> +}
> +
> +/**
> + * intel_iommu_restore_device() - Restore device domain attachment after live update
> + * @domain: Restored domain
> + * @dev: Restored device
> + *
> + * Return: 0 on success, or negative error code.
> + */
> +int intel_iommu_restore_device(struct iommu_domain *domain,
> +			       struct device *dev)
> +{
> +	struct iommu_device_ser *device_ser = dev_iommu_restored_state(dev);
> +	struct device_domain_info *info = dev_iommu_priv_get(dev);
> +	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
> +	struct intel_iommu *iommu = info->iommu;
> +	unsigned long flags;
> +	int ret;
> +
> +	if (!device_ser)
> +		return -EINVAL;
> +
> +	if (dev_is_real_dma_subdevice(dev))
> +		return -EOPNOTSUPP;

... or, move above check here to ensure the attachment relationship
between the @device and @domain?

> +
> +	ret = domain_reattach_iommu(dmar_domain, iommu, device_ser);
> +	if (ret)
> +		return ret;
> +
> +	info->domain = dmar_domain;
> +	info->domain_attached = true;
> +	spin_lock_irqsave(&dmar_domain->lock, flags);
> +	list_add(&info->link, &dmar_domain->devices);
> +	spin_unlock_irqrestore(&dmar_domain->lock, flags);
> +
> +	if (!sm_supported(iommu))
> +		intel_iommu_enable_pci_ats(info);

Could you please clarify the PCI ATS behavior across live update?

My understanding is that PCI devices may bypass reset during kexec and
are then re-initialized by the new kernel (is that correct?). If so, ATS
state in hardware would depend on its state before kexec in the old
kernel.

In a normal reboot, ATS is expected to be disabled and the device ATC is
empty. But that assumption may not hold for live update. If that is
true, can we still use the same approach to keep hardware ATS state and
info->ats_enabled in sync?

> +
> +	ret = cache_tag_assign_domain(dmar_domain, dev, IOMMU_NO_PASID);
> +	if (ret)
> +		goto err;
> +
> +	ret = iopf_for_domain_set(domain, dev);
> +	if (ret)
> +		goto err;
> +
> +	return 0;
> +
> +err:
> +	/*
> +	 * Detach the restored domain from device and iommu on failure, but keep
> +	 * the hardware state intact.
> +	 */
> +	info->domain_attached = false;
> +	cache_tag_unassign_domain(info->domain, dev, IOMMU_NO_PASID);
> +	spin_lock_irqsave(&info->domain->lock, flags);
> +	list_del(&info->link);
> +	spin_unlock_irqrestore(&info->domain->lock, flags);
> +
> +	domain_detach_reattached_iommu(info->domain, iommu);
> +	info->domain = NULL;
> +	return ret;
> +}
> +
>   /**
>    * intel_iommu_preserve_device() - Intel IOMMU callback to preserve device state
>    * @dev: Target device

Thanks,
baolu

  reply	other threads:[~2026-08-27  8:12 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-08  2:27 [PATCH v4 00/18] iommu: Add live update state preservation Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 01/18] memfd: export memfd_get_seals() Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 02/18] iommu: Implement IOMMU Live update FLB callbacks Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 03/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 04/18] iommupt: Implement preserve/unpreserve/restore callbacks Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 05/18] iommu: Implement IOMMU domain preservation Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 06/18] iommu: Implement device and IOMMU HW preservation Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops Samiullah Khawaja
2026-08-26  7:10   ` Baolu Lu
2026-08-26 20:46     ` Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 08/18] iommu/vt-d: Clear unpreserved context entries during shutdown Samiullah Khawaja
2026-08-26  8:05   ` Baolu Lu
2026-08-26 12:30     ` Pranjal Shrivastava
2026-08-26 20:37       ` Samiullah Khawaja
2026-08-26 20:30     ` Samiullah Khawaja
2026-08-27  6:09       ` Baolu Lu
2026-08-08  2:27 ` [PATCH v4 09/18] iommu: Add APIs to get iommu and device preserved state Samiullah Khawaja
2026-08-12  6:29   ` Ankit Soni
2026-08-12 23:23     ` Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 10/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids Samiullah Khawaja
2026-08-27  7:22   ` Baolu Lu
2026-08-08  2:27 ` [PATCH v4 11/18] iommu: Restore and reattach preserved domains to devices Samiullah Khawaja
2026-08-14 16:59   ` Ankit Soni
2026-08-14 19:46     ` Samiullah Khawaja
2026-08-17 15:32       ` Ankit Soni
2026-08-08  2:27 ` [PATCH v4 12/18] iommu/vt-d: Handle reattach of the restored domain Samiullah Khawaja
2026-08-27  8:12   ` Baolu Lu [this message]
2026-08-08  2:27 ` [PATCH v4 13/18] iommu/vt-d: Preserve PASID table of preserved device Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 14/18] iommufd: Implement ioctl to mark HWPT for preservation Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 15/18] iommufd: Persist iommu hardware pagetables for live update Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 16/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 17/18] vfio/pci: Preserve the iommufd state of the " Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 18/18] iommufd/selftest: Add test to verify iommufd preservation Samiullah Khawaja
2026-08-26  8:12 ` [PATCH v4 00/18] iommu: Add live update state preservation David Woodhouse

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=5b920299-260b-4025-ac4f-e8f83beebf95@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex@shazbot.org \
    --cc=dmatlack@google.com \
    --cc=dwmw2@infradead.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pasha.tatashin@soleen.com \
    --cc=praan@google.com \
    --cc=pratyush@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=shuah@kernel.org \
    --cc=skhawaja@google.com \
    --cc=vipinsh@google.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.