mirror of https://lore.kernel.org/lkml/
 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, 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>,
	Ankit Soni <Ankit.Soni@amd.com>,
	Nicolin Chen <nicolinc@nvidia.com>, John Starks <dev@jstarks.net>
Subject: Re: [PATCH v6 11/18] iommu/vt-d: Preserve PASID table of preserved device
Date: Thu, 29 Oct 2026 15:42:21 +0800	[thread overview]
Message-ID: <0945bca7-fb1c-4bbe-b14d-1e9939738daf@linux.intel.com> (raw)
In-Reply-To: <20261025021930.674647-12-skhawaja@google.com>

On 10/25/2026 10:19 AM, Samiullah Khawaja wrote:
> In scalable mode the PASID table is used to fetch the io page tables.
> Preserve and restore the PASID table of the preserved devices.
> 
> Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
> ---
>   drivers/iommu/intel/iommu.c      |   3 +-
>   drivers/iommu/intel/liveupdate.c | 144 +++++++++++++++++++++++++++++--
>   drivers/iommu/intel/pasid.c      |  10 ++-
>   drivers/iommu/intel/pasid.h      |   8 ++
>   include/linux/kho/abi/iommu.h    |  19 ++++
>   5 files changed, 176 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> index 991ad9b2456e..ecec057f78dd 100644
> --- a/drivers/iommu/intel/iommu.c
> +++ b/drivers/iommu/intel/iommu.c
> @@ -3473,7 +3473,8 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev)
>   			goto clear_rbtree;
>   		}
>   
> -		if (!context_copied(iommu, info->bus, info->devfn)) {
> +		if (!context_copied(iommu, info->bus, info->devfn) &&
> +		    !dev_iommu_restored_state(dev)) {
>   			ret = intel_pasid_setup_sm_context(dev);
>   			if (ret)
>   				goto free_table;
> diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupdate.c
> index cf98b95bf8d2..36816d3651e6 100644
> --- a/drivers/iommu/intel/liveupdate.c
> +++ b/drivers/iommu/intel/liveupdate.c
> @@ -14,6 +14,7 @@
>   #include <linux/pci.h>
>   
>   #include "iommu.h"
> +#include "pasid.h"
>   #include "../iommu-pages.h"
>   
>   /* 2 tables per bus in scalable mode with upper table at odd bit */
> @@ -393,6 +394,69 @@ void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu)
>   		      iommu->name);
>   }
>   
> +enum pasid_lu_op {
> +	PASID_LU_OP_PRESERVE = 1,
> +	PASID_LU_OP_UNPRESERVE,
> +	PASID_LU_OP_RESTORE,
> +};
> +
> +static int pasid_lu_do_op(void *table, enum pasid_lu_op op)
> +{
> +	int ret = 0;
> +
> +	switch (op) {
> +	case PASID_LU_OP_PRESERVE:
> +		ret = iommu_preserve_pages(table);
> +		break;
> +	case PASID_LU_OP_UNPRESERVE:
> +		iommu_unpreserve_pages(table);
> +		break;
> +	case PASID_LU_OP_RESTORE:
> +		iommu_restore_pages(virt_to_phys(table));
> +		break;
> +	}
> +
> +	return ret;
> +}
> +
> +static int pasid_lu_handle_pd(struct pasid_dir_entry *dir,
> +			      u32 max_pasid, enum pasid_lu_op op)
> +{
> +	int max_pde = max_pasid >> PASID_PDE_SHIFT;
> +	struct pasid_entry *table;
> +	int i, ret;
> +
> +	for (i = 0; i < max_pde; i++) {
> +		table = get_pasid_table_from_pde(&dir[i]);
> +		if (!table)
> +			continue;
> +
> +		ret = pasid_lu_do_op(table, op);
> +		if (ret)
> +			goto err;
> +	}
> +
> +	ret = pasid_lu_do_op(dir, op);
> +	if (ret)
> +		goto err;
> +
> +	return 0;
> +
> +err:
> +	if (op != PASID_LU_OP_PRESERVE)
> +		return ret;
> +
> +	while (i > 0) {
> +		table = get_pasid_table_from_pde(&dir[--i]);
> +		if (!table)
> +			continue;
> +
> +		pasid_lu_do_op(table, PASID_LU_OP_UNPRESERVE);
> +	}
> +
> +	return ret;
> +}
> +
>   /**
>    * intel_iommu_preserve_device() - Intel IOMMU callback to preserve device state
>    * @dev: Target device
> @@ -404,6 +468,7 @@ int intel_iommu_preserve_device(struct device *dev,
>   				struct iommu_device_ser *device_ser)
>   {
>   	struct device_domain_info *info = dev_iommu_priv_get(dev);
> +	struct pasid_table *pasid_table;
>   	int ret;
>   
>   	if (!dev_is_pci(dev)) {
> @@ -434,6 +499,22 @@ int intel_iommu_preserve_device(struct device *dev,
>   
>   	device_ser->attachment.attachment_id = domain_id_iommu(info->domain,
>   							       info->iommu);
> +
> +	if (!sm_supported(info->iommu))
> +		return 0;
> +
> +	pasid_table = intel_pasid_get_table(dev);
> +	if (!pasid_table)
> +		return -EINVAL;
> +
> +	ret = pasid_lu_handle_pd(pasid_table->table,
> +				 pasid_table->max_pasid,
> +				 PASID_LU_OP_PRESERVE);
> +	if (ret)
> +		return ret;
> +
> +	device_ser->intel.pasid_table = virt_to_phys(pasid_table->table);
> +	device_ser->intel.max_pasid = pasid_table->max_pasid;
>   	return 0;
>   }
>   
> @@ -445,15 +526,33 @@ int intel_iommu_preserve_device(struct device *dev,
>   void intel_iommu_unpreserve_device(struct device *dev,
>   				   struct iommu_device_ser *device_ser)
>   {
> +	struct device_domain_info *info = dev_iommu_priv_get(dev);
> +	struct pasid_table *pasid_table;
> +
> +	if (!dev_is_pci(dev))
> +		return;
> +
> +	if (!info)
> +		return;
> +
> +	if (!sm_supported(info->iommu))
> +		return;
> +
>   	/*
>   	 * The context tables preserved during device preservation, in the
>   	 * preserve_device() callback, might be shared with other devices, so
> -	 * those are unpreserved in the iommu unpreserve() callback. So this
> -	 * callback is kept empty.
> -	 *
> -	 * Once device PASID tables are preserved, the unpreservation of PASID
> -	 * tables will be added here.
> +	 * those are unpreserved in the iommu unpreserve() callback.
>   	 */
> +	if (!device_ser->intel.pasid_table)
> +		return;
> +
> +	pasid_table = intel_pasid_get_table(dev);
> +	if (!pasid_table)
> +		return;
> +
> +	pasid_lu_handle_pd(pasid_table->table,
> +			   pasid_table->max_pasid,
> +			   PASID_LU_OP_UNPRESERVE);
>   }
>   
>   /**
> @@ -499,3 +598,38 @@ void intel_iommu_unpreserve(struct iommu_device *iommu_dev,
>   	unpreserve_iommu_context_tables(iommu, ser);
>   	iommu_unpreserve_pages(iommu->root_entry);
>   }
> +
> +/**
> + * intel_pasid_restore_table() - Restore preserved PASID table for a device
> + * @dev: Restored device
> + * @max_pasid: Maximum supported PASID
> + *
> + * Return: Pointer to restored PASID table directory, or NULL if not preserved.
> + */
> +void *intel_pasid_restore_table(struct device *dev, u64 max_pasid)
> +{
> +	struct iommu_device_ser *ser = dev_iommu_restored_state(dev);
> +
> +	if (!ser || !ser->intel.pasid_table)
> +		return NULL;
> +
> +	/*
> +	 * MAX PASID of a device should not change as it is read from
> +	 * capabilities.
> +	 */
> +	if (ser->intel.max_pasid != max_pasid)
> +		panic("%s: preserved max PASID %llu does not match %llu\n",
> +		      dev_name(dev), ser->intel.max_pasid, max_pasid);
> +
> +	if (ser->intel.restored)
> +		goto out;
> +
> +	if (pasid_lu_handle_pd(phys_to_virt(ser->intel.pasid_table),
> +			       ser->intel.max_pasid, PASID_LU_OP_RESTORE))
> +		panic("%s: failed to restore preserved PASID table\n",
> +		      dev_name(dev));
> +	ser->intel.restored = 1;
> +
> +out:
> +	return phys_to_virt(ser->intel.pasid_table);
> +}
> diff --git a/drivers/iommu/intel/pasid.c b/drivers/iommu/intel/pasid.c
> index e4f24d3f19a6..59cc69383799 100644
> --- a/drivers/iommu/intel/pasid.c
> +++ b/drivers/iommu/intel/pasid.c
> @@ -13,6 +13,7 @@
>   #include <linux/cpufeature.h>
>   #include <linux/dmar.h>
>   #include <linux/iommu.h>
> +#include <linux/iommu-liveupdate.h>
>   #include <linux/memory.h>
>   #include <linux/pci.h>
>   #include <linux/pci-ats.h>
> @@ -60,8 +61,13 @@ int intel_pasid_alloc_table(struct device *dev)
>   
>   	size = max_pasid >> (PASID_PDE_SHIFT - 3);
>   	order = size ? get_order(size) : 0;
> -	dir = iommu_alloc_pages_node_sz(info->iommu->node, GFP_KERNEL,
> -					1 << (order + PAGE_SHIFT));
> +
> +	max_pasid = 1 << (order + PAGE_SHIFT + 3);
> +	if (dev_iommu_restored_state(dev))
> +		dir = intel_pasid_restore_table(dev, max_pasid);
> +	else
> +		dir = iommu_alloc_pages_node_sz(info->iommu->node, GFP_KERNEL,
> +						1 << (order + PAGE_SHIFT));
>   	if (!dir) {
>   		kfree(pasid_table);
>   		return -ENOMEM;
> diff --git a/drivers/iommu/intel/pasid.h b/drivers/iommu/intel/pasid.h
> index 48d3bb6b68de..801768cdea16 100644
> --- a/drivers/iommu/intel/pasid.h
> +++ b/drivers/iommu/intel/pasid.h
> @@ -301,6 +301,14 @@ static inline void pasid_set_eafe(struct pasid_entry *pe)
>   
>   extern unsigned int intel_pasid_max_id;
>   int intel_pasid_alloc_table(struct device *dev);
> +#ifdef CONFIG_IOMMU_LIVEUPDATE
> +void *intel_pasid_restore_table(struct device *dev, u64 max_pasid);
> +#else
> +static inline void *intel_pasid_restore_table(struct device *dev, u64 max_pasid)
> +{
> +	return NULL;
> +}
> +#endif
>   void intel_pasid_free_table(struct device *dev);
>   struct pasid_table *intel_pasid_get_table(struct device *dev);
>   int intel_pasid_setup_first_level(struct intel_iommu *iommu, struct device *dev,
> diff --git a/include/linux/kho/abi/iommu.h b/include/linux/kho/abi/iommu.h
> index 95354a12ba1d..08443f6cee6f 100644
> --- a/include/linux/kho/abi/iommu.h
> +++ b/include/linux/kho/abi/iommu.h
> @@ -137,6 +137,21 @@ struct iommu_attachment_ser {
>   	u32 padding;
>   } __packed;
>   
> +/**
> + * struct iommu_device_intel_ser - Intel specific state of serialized device
> + * @features: Bitmask of Intel device specific features
> + * @restored: Whether the device state is restored
> + * @pasid_table: Physical address of pasid table
> + * @max_pasid: Maximum supported pasid
> + */
> +struct iommu_device_intel_ser {
> +	u32 features;
> +	u8 restored;
> +	u8 padding[3];
> +	u64 pasid_table;
> +	u64 max_pasid;
> +} __packed;
> +
>   /**
>    * struct iommu_device_ser - Serialized state of a device
>    * @hdr: Common object header
> @@ -146,6 +161,7 @@ struct iommu_attachment_ser {
>    * @iommu_phys: Physical address of the serialized IOMMU the device is behind
>    * @attachment: Attachment of the device to its domain
>    * @reserved: Reserved for future common fields.
> + * @intel: Intel specific serialization data
>    */
>   struct iommu_device_ser {
>   	struct iommu_hdr_ser hdr;
> @@ -155,6 +171,9 @@ struct iommu_device_ser {
>   	u64 iommu_phys;
>   	struct iommu_attachment_ser attachment;
>   	u64 reserved[2];
> +	union {
> +		struct iommu_device_intel_ser intel;
> +	};
>   } __packed;
>   
>   /* There are maximum 256 buses, so maximum 512 context tables */

Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>

  reply	other threads:[~2026-10-29  7:42 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-25  2:19 [PATCH v6 00/18] iommu: Add live update state preservation Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 01/18] iommu: Implement IOMMU Live update FLB callbacks Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 02/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages Samiullah Khawaja
2026-10-30  1:26   ` Baolu Lu
2026-10-25  2:19 ` [PATCH v6 03/18] iommupt: Implement preserve/unpreserve/restore callbacks Samiullah Khawaja
2026-10-30  3:07   ` Baolu Lu
2026-10-25  2:19 ` [PATCH v6 04/18] iommu: Implement IOMMU domain preservation Samiullah Khawaja
2026-10-30  4:24   ` Baolu Lu
2026-10-25  2:19 ` [PATCH v6 05/18] iommu: Implement device and IOMMU HW preservation Samiullah Khawaja
2026-10-30  5:12   ` Baolu Lu
2026-10-25  2:19 ` [PATCH v6 06/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops Samiullah Khawaja
2026-10-28  1:51   ` Baolu Lu
2026-10-28  3:10     ` Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 07/18] iommu/vt-d: Clear unpreserved context entries during shutdown Samiullah Khawaja
2026-10-28  3:18   ` Baolu Lu
2026-10-29  0:34     ` Samiullah Khawaja
2026-10-29  3:18   ` Baolu Lu
2026-10-29 22:00     ` Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 08/18] iommu: Add APIs to get iommu and device preserved state Samiullah Khawaja
2026-10-30  6:03   ` Baolu Lu
2026-10-25  2:19 ` [PATCH v6 09/18] iommu/vt-d: Combine disable_dmar_iommu() and free_dmar_iommu() Samiullah Khawaja
2026-10-28  6:01   ` Baolu Lu
2026-10-29  0:33     ` Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 10/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids Samiullah Khawaja
2026-10-28  7:14   ` Baolu Lu
2026-10-29  0:32     ` Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 11/18] iommu/vt-d: Preserve PASID table of preserved device Samiullah Khawaja
2026-10-29  7:42   ` Baolu Lu [this message]
2026-10-25  2:19 ` [PATCH v6 12/18] iommu: Restore and reattach preserved domains to devices Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 13/18] iommu/vt-d: Handle reattach of the restored domain Samiullah Khawaja
2026-10-29  8:28   ` Baolu Lu
2026-10-29 22:34     ` Samiullah Khawaja
2026-10-29 23:38       ` Baolu Lu
2026-10-25  2:19 ` [PATCH v6 14/18] iommufd: Implement ioctl to mark HWPT for preservation Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 15/18] iommufd: Persist iommu hardware pagetables for live update Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 16/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 17/18] vfio/pci: Preserve the iommufd state of the " Samiullah Khawaja
2026-10-25  2:19 ` [PATCH v6 18/18] iommufd/selftest: Add test to verify iommufd preservation Samiullah Khawaja

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=0945bca7-fb1c-4bbe-b14d-1e9939738daf@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®