All of lore.kernel.org
 help / color / mirror / Atom feed
From: Baolu Lu <baolu.lu@linux.intel.com>
To: Samiullah Khawaja <skhawaja@google.com>,
	Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	David Woodhouse <dwmw2@infradead.org>
Cc: baolu.lu@linux.intel.com, Pranjal Shrivastava <praan@google.com>,
	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>,
	Vipin Sharma <vipinsh@google.com>,
	Ankit Soni <Ankit.Soni@amd.com>,
	Nicolin Chen <nicolinc@nvidia.com>, John Starks <dev@jstarks.net>
Subject: Re: [PATCH v8 17/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev
Date: Sat, 5 Dec 2026 10:19:13 +0800	[thread overview]
Message-ID: <ba6e9695-51ae-4b9b-a10f-1257d69bb569@linux.intel.com> (raw)
In-Reply-To: <20261123044949.3610702-18-skhawaja@google.com>

On 11/23/2026 12:49 PM, Samiullah Khawaja wrote:
> Add APIs that can be used to preserve and unpreserve a vfio cdev. Use
> the APIs exported by the IOMMU core to preserve/unpreserve device.
> 
> The LUO token of the preserved iommufd is fetched and returned back to
> the caller as that can be used during restore to get the restored
> iommufd.
> 
> Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
> Reviewed-by: Pranjal Shrivastava <praan@google.com>
> ---
>   drivers/iommu/iommufd/device.c          | 154 ++++++++++++++++++++++++
>   drivers/iommu/iommufd/iommufd_private.h |   3 +
>   drivers/iommu/iommufd/liveupdate.c      |  26 ++++
>   include/linux/iommufd.h                 |  27 +++++
>   4 files changed, 210 insertions(+)
> 
> diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
> index 1b0bdf57ac8a..ebfc96445f71 100644
> --- a/drivers/iommu/iommufd/device.c
> +++ b/drivers/iommu/iommufd/device.c
> @@ -2,6 +2,7 @@
>   /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
>    */
>   #include <linux/iommu.h>
> +#include <linux/iommu-liveupdate.h>
>   #include <linux/iommufd.h>
>   #include <linux/pci-ats.h>
>   #include <linux/slab.h>
> @@ -686,6 +687,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
>   	int rc;
>   
>   	mutex_lock(&igroup->lock);
> +	if (iommufd_device_is_preserved(idev)) {
> +		rc = -EBUSY;
> +		goto err_unlock;
> +	}
>   
>   	attach = xa_cmpxchg(&igroup->pasid_attach, pasid, NULL,
>   			    XA_ZERO_ENTRY, GFP_KERNEL);
> @@ -869,6 +874,10 @@ iommufd_device_do_replace(struct iommufd_device *idev, ioasid_t pasid,
>   	int rc;
>   
>   	mutex_lock(&igroup->lock);
> +	if (iommufd_device_is_preserved(idev)) {
> +		rc = -EBUSY;
> +		goto err_unlock;
> +	}
>   
>   	attach = xa_load(&igroup->pasid_attach, pasid);
>   	if (!attach) {
> @@ -1135,6 +1144,9 @@ void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid)
>   {
>   	struct iommufd_hw_pagetable *hwpt;
>   
> +	if (pasid == IOMMU_NO_PASID)
> +		iommufd_device_unpreserve(idev);

Could you add a comment explaining why detaching IOMMU_NO_PASID silently
unpreserves the device?

> +
>   	hwpt = iommufd_hw_pagetable_detach(idev, pasid);

This also races with iommufd_device_preserve(). igroup->lock is dropped
between iommufd_device_unpreserve() and iommufd_hw_pagetable_detach(),
so a concurrent iommufd_device_preserve() can slip into that window, see
the device still attached, and succeed. The device then ends up detached
while still marked as preserved.

>   	if (!hwpt)
>   		return;
> @@ -1749,3 +1761,145 @@ int iommufd_get_hw_info(struct iommufd_ucmd *ucmd)
>   	iommufd_put_object(ucmd->ictx, &idev->obj);
>   	return rc;
>   }
> +
> +#ifdef CONFIG_IOMMU_LIVEUPDATE
> +static bool _iommufd_device_has_pasid_attachments(struct iommufd_device *idev)
> +{
> +	struct iommufd_group *igroup = idev->igroup;
> +	unsigned long start = IOMMU_NO_PASID;
> +
> +	if (xa_find_after(&igroup->pasid_attach, &start, UINT_MAX, XA_PRESENT))
> +		return true;
> +
> +	return false;

Nit: this can simply be

         return xa_find_after(...) != NULL;

?

Also, how about dropping the leading underscore. There is no non-
underscore version of this helper.

> +}
> +
> +/**
> + * iommufd_device_preserve() - Preserve an iommufd device across live update
> + * @s: Live update session
> + * @idev: Target iommufd device
> + * @iommufd_tokenp: Pointer to store outgoing iommufd token
> + *
> + * Return: 0 on success, or negative error code.
> + */
> +int iommufd_device_preserve(struct liveupdate_session *s,
> +			    struct iommufd_device *idev,
> +			    u64 *iommufd_tokenp)
> +{
> +	struct iommufd_hwpt_paging *hwpt_paging;
> +	struct iommufd_hw_pagetable *hwpt;
> +	struct iommufd_attach *attach;
> +	struct iommufd_group *igroup;
> +	int ret;
> +
> +	if (!idev)
> +		return -EINVAL;
> +
> +	if (iommufd_device_is_noiommu(idev))
> +		return -EOPNOTSUPP;
> +
> +	igroup = idev->igroup;
> +	mutex_lock(&igroup->lock);
> +	if (idev->liveupdate_preserved) {
> +		ret = -EBUSY;
> +		goto out;
> +	}
> +
> +	if (_iommufd_device_has_pasid_attachments(idev)) {
> +		ret = -EOPNOTSUPP;
> +		goto out;
> +	}
> +
> +	attach = xa_load(&igroup->pasid_attach, IOMMU_NO_PASID);
> +	if (!attach) {
> +		ret = -ENOENT;
> +		goto out;
> +	}
> +
> +	if (!xa_load(&attach->device_array, idev->obj.id)) {
> +		ret = -ENOENT;
> +		goto out;
> +	}
> +
> +	hwpt = attach->hwpt;
> +	if (!hwpt_is_paging(hwpt)) {
> +		ret = -EOPNOTSUPP;
> +		goto out;
> +	}
> +
> +	hwpt_paging = to_hwpt_paging(hwpt);
> +	ret = liveupdate_get_token_outgoing(s, idev->ictx->file, iommufd_tokenp);
> +	if (ret)
> +		goto out;
> +
> +	if (!hwpt_paging->liveupdate_preserved) {
> +		ret = -EINVAL;
> +		goto out;
> +	}
> +
> +	ret = iommu_preserve_device(hwpt_paging->common.domain,
> +				    idev->dev,
> +				    *iommufd_tokenp);
> +
> +	if (!ret)
> +		idev->liveupdate_preserved = true;
> +out:
> +	mutex_unlock(&igroup->lock);
> +	return ret;
> +}
> +EXPORT_SYMBOL_NS_GPL(iommufd_device_preserve, "IOMMUFD");
> +
> +/**
> + * iommufd_device_unpreserve() - Unpreserve an iommufd device
> + * @idev: Target iommufd device
> + */
> +void iommufd_device_unpreserve(struct iommufd_device *idev)
> +{
> +	struct iommufd_hwpt_paging *hwpt_paging;
> +	struct iommufd_hw_pagetable *hwpt;
> +	struct iommufd_attach *attach;
> +	struct iommufd_group *igroup;
> +
> +	if (!idev)
> +		return;
> +
> +	igroup = idev->igroup;
> +	mutex_lock(&igroup->lock);
> +	if (!idev->liveupdate_preserved)
> +		goto out;
> +
> +	attach = xa_load(&igroup->pasid_attach, IOMMU_NO_PASID);
> +	if (!attach) {
> +		WARN(1, "IOMMU_NO_PASID attachment not found");
> +		goto out;
> +	}
> +
> +	hwpt = attach->hwpt;
> +	if (WARN(!hwpt_is_paging(hwpt), "Attached HWPT is not a paging HWPT"))
> +		goto out;
> +
> +	hwpt_paging = to_hwpt_paging(hwpt);
> +	if (!hwpt_paging->liveupdate_preserved) {
> +		WARN(1, "Attached domain is not preserved");
> +		goto out;
> +	}
> +
> +	iommu_unpreserve_device(idev->dev);
> +	idev->liveupdate_preserved = false;
> +out:
> +	mutex_unlock(&igroup->lock);
> +}
> +EXPORT_SYMBOL_NS_GPL(iommufd_device_unpreserve, "IOMMUFD");
> +
> +/**
> + * iommufd_device_is_preserved() - Check if an iommufd device is preserved
> + * @idev: Target iommufd device
> + *
> + * Return: true if preserved, false otherwise.
> + */
> +bool iommufd_device_is_preserved(struct iommufd_device *idev)
> +{
> +	return idev && idev->liveupdate_preserved;
> +}
> +EXPORT_SYMBOL_NS_GPL(iommufd_device_is_preserved, "IOMMUFD");
> +#endif

[...]

> diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
> index 3087f5b2def8..1ad2a21cfcdf 100644
> --- a/include/linux/iommufd.h
> +++ b/include/linux/iommufd.h
> @@ -9,6 +9,7 @@
>   #include <linux/err.h>
>   #include <linux/errno.h>
>   #include <linux/iommu.h>
> +#include <linux/liveupdate.h>

Nit: Would a forward declaration of "struct liveupdate_session;" be
enough here? There seems to be no need to pull liveupdate.h into every
user of linux/iommufd.h.

>   #include <linux/refcount.h>
>   #include <linux/types.h>
>   #include <linux/xarray.h>
> @@ -215,6 +216,14 @@ int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
>   int iommufd_vfio_compat_ioas_get_id(struct iommufd_ctx *ictx, u32 *out_ioas_id);
>   int iommufd_vfio_compat_ioas_create(struct iommufd_ctx *ictx);
>   int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx);
> +
> +#ifdef CONFIG_IOMMU_LIVEUPDATE
> +int iommufd_device_preserve(struct liveupdate_session *s,
> +			    struct iommufd_device *idev,
> +			    u64 *iommufd_tokenp);
> +void iommufd_device_unpreserve(struct iommufd_device *idev);
> +bool iommufd_device_is_preserved(struct iommufd_device *idev);
> +#endif
>   #else /* !CONFIG_IOMMUFD */
>   static inline struct iommufd_ctx *iommufd_ctx_from_file(struct file *file)
>   {
> @@ -399,4 +408,22 @@ static inline void iommufd_viommu_destroy_mmap(struct iommufd_viommu *viommu,
>   {
>   	_iommufd_destroy_mmap(viommu->ictx, &viommu->obj, offset);
>   }
> +
> +#if !IS_ENABLED(CONFIG_IOMMU_LIVEUPDATE) || !IS_ENABLED(CONFIG_IOMMUFD)
> +static inline int iommufd_device_preserve(struct liveupdate_session *s,
> +					  struct iommufd_device *idev,
> +					  u64 *iommufd_tokenp)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
> +static inline void iommufd_device_unpreserve(struct iommufd_device *idev)
> +{
> +}
> +
> +static inline bool iommufd_device_is_preserved(struct iommufd_device *idev)
> +{
> +	return false;
> +}
> +#endif
>   #endif

Thanks,
baolu

  reply	other threads:[~2026-12-05  2:19 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  4:49 [PATCH v8 00/18] iommu: Add live update state preservation Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 01/18] liveupdate: Export symbols needed by modules Samiullah Khawaja
2026-12-04  1:45   ` Baolu Lu
2026-11-23  4:49 ` [PATCH v8 02/18] iommu: Implement IOMMU Live update FLB callbacks Samiullah Khawaja
2026-11-30 10:28   ` Ankit Soni
2026-11-30 18:40   ` Pranjal Shrivastava
2026-11-30 18:47     ` Pranjal Shrivastava
2026-12-02 21:34       ` Samiullah Khawaja
2026-12-01 22:04     ` Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 03/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 04/18] iommupt: Implement preserve/unpreserve/restore callbacks Samiullah Khawaja
2026-11-30 17:06   ` Dongli Zhang
2026-12-02 21:51     ` Samiullah Khawaja
2026-12-02 19:08   ` Pranjal Shrivastava
2026-11-23  4:49 ` [PATCH v8 05/18] iommu: Implement IOMMU domain preservation Samiullah Khawaja
2026-11-24  5:23   ` Ankit Soni
2026-11-24 17:40     ` Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 06/18] iommu: Implement device and IOMMU HW preservation Samiullah Khawaja
2026-11-24  4:29   ` Ankit Soni
2026-11-24 17:40     ` Samiullah Khawaja
2026-12-04  6:14   ` Baolu Lu
2026-12-05  0:19     ` Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 08/18] iommu/vt-d: Clear unpreserved context entries during shutdown Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 09/18] iommu: Add APIs to get iommu and device restored state Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 10/18] iommu/vt-d: Combine disable_dmar_iommu() and free_dmar_iommu() Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 11/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids Samiullah Khawaja
2026-12-04  6:55   ` Baolu Lu
2026-12-05  0:03     ` Samiullah Khawaja
2026-12-05  2:45       ` Baolu Lu
2026-12-05  2:47       ` Baolu Lu
2026-11-23  4:49 ` [PATCH v8 12/18] iommu/vt-d: Preserve PASID table of preserved device Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 13/18] iommu: Restore and reattach preserved domains to devices Samiullah Khawaja
2026-11-23  5:18   ` sashiko-bot
2026-11-23  4:49 ` [PATCH v8 14/18] iommu/vt-d: Handle reattach of the restored domain Samiullah Khawaja
2026-11-23  5:03   ` sashiko-bot
2026-12-04  7:33   ` Baolu Lu
2026-12-04 22:27     ` Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 15/18] iommufd: Implement ioctl to mark HWPT for preservation Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 16/18] iommufd: Persist iommu hardware pagetables for live update Samiullah Khawaja
2026-12-04 13:47   ` Baolu Lu
2026-12-04 21:46     ` Samiullah Khawaja
2026-11-23  4:49 ` [PATCH v8 17/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev Samiullah Khawaja
2026-12-05  2:19   ` Baolu Lu [this message]
2026-11-23  4:49 ` [PATCH v8 18/18] iommufd/selftest: Add test to verify iommufd preservation Samiullah Khawaja
2026-11-23  5:11   ` sashiko-bot

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=ba6e9695-51ae-4b9b-a10f-1257d69bb569@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=Ankit.Soni@amd.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex@shazbot.org \
    --cc=dev@jstarks.net \
    --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=nicolinc@nvidia.com \
    --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.