Linux IOMMU Development
 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>,
	Pasha Tatashin <pasha.tatashin@soleen.com>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	iommu@lists.linux.dev
Cc: Robin Murphy <robin.murphy@arm.com>,
	Pratyush Yadav <pratyush@kernel.org>,
	Kevin Tian <kevin.tian@intel.com>,
	Alex Williamson <alex@shazbot.org>,
	linux-kernel@vger.kernel.org, Saeed Mahameed <saeedm@nvidia.com>,
	Adithya Jayachandran <ajayachandra@nvidia.com>,
	Parav Pandit <parav@nvidia.com>,
	Leon Romanovsky <leonro@nvidia.com>, William Tu <witu@nvidia.com>,
	Vipin Sharma <vipinsh@google.com>,
	dmatlack@google.com, YiFei Zhu <zhuyifei@google.com>,
	Chris Li <chrisl@kernel.org>,
	praan@google.com
Subject: Re: [RFC PATCH v2 24/32] iommu/vt-d: restore state of the preserved IOMMU
Date: Thu, 4 Dec 2025 14:43:39 +0800	[thread overview]
Message-ID: <b05a1dd0-9692-4bf7-8fa0-0b3857178d1b@linux.intel.com> (raw)
In-Reply-To: <20251202230303.1017519-25-skhawaja@google.com>

On 12/3/25 07:02, Samiullah Khawaja wrote:
> During boot fetch the preserved state of IOMMU unit and if found then
> restore the state. Reuse the root_table that was preserved in the
> previous kernel.
> 
> Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
> ---
>   drivers/iommu/intel/iommu.c      | 30 ++++++++++++++++++++++++------
>   drivers/iommu/intel/iommu.h      |  2 ++
>   drivers/iommu/intel/liveupdate.c | 30 ++++++++++++++++++++++++++++++
>   3 files changed, 56 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
> index 84fef81ecf4d..888351f91918 100644
> --- a/drivers/iommu/intel/iommu.c
> +++ b/drivers/iommu/intel/iommu.c
> @@ -224,12 +224,12 @@ static void clear_translation_pre_enabled(struct intel_iommu *iommu)
>   	iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
>   }
>   
> -static void init_translation_status(struct intel_iommu *iommu)
> +static void init_translation_status(struct intel_iommu *iommu, bool restoring)
>   {
>   	u32 gsts;
>   
>   	gsts = readl(iommu->reg + DMAR_GSTS_REG);
> -	if (gsts & DMA_GSTS_TES)
> +	if (!restoring && (gsts & DMA_GSTS_TES))
>   		iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
>   }
>   
> @@ -672,10 +672,18 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,
>   #endif
>   
>   /* iommu handling */
> -static int iommu_alloc_root_entry(struct intel_iommu *iommu)
> +static int iommu_alloc_root_entry(struct intel_iommu *iommu, struct iommu_ser *restored_state)
>   {
>   	struct root_entry *root;
>   
> +#if CONFIG_LIVEUPDATE

nit: Is it possible to hide the "IS_ENABLED(CONFIG_LIVEUPDATE)" and "#if
CONFIG_LIVEUPDATE" checks within the header files?

> +	if (restored_state) {
> +		intel_iommu_liveupdate_restore_root_table(iommu, restored_state);
> +		/* Should not be needed since the entries are already cleaned in last kernel. */
> +		__iommu_flush_cache(iommu, iommu->root_entry, ROOT_SIZE);
> +		return 0;
> +	}
> +#endif
>   	root = iommu_alloc_pages_node_sz(iommu->node, GFP_ATOMIC, SZ_4K);
>   	if (!root) {
>   		pr_err("Allocating root entry for %s failed\n",
> @@ -1616,6 +1624,7 @@ static int copy_translation_tables(struct intel_iommu *iommu)
>   
>   static int __init init_dmars(void)
>   {
> +	struct iommu_ser *iommu_ser = NULL;
>   	struct dmar_drhd_unit *drhd;
>   	struct intel_iommu *iommu;
>   	int ret;
> @@ -1638,8 +1647,12 @@ static int __init init_dmars(void)
>   						   intel_pasid_max_id);
>   		}
>   
> +#if IS_ENABLED(CONFIG_LIVEUPDATE)
> +		iommu_ser = iommu_get_preserved_data(iommu->reg_phys, IOMMU_INTEL);
> +#endif
> +
>   		intel_iommu_init_qi(iommu);
> -		init_translation_status(iommu);
> +		init_translation_status(iommu, !!iommu_ser);
>   
>   		if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
>   			iommu_disable_translation(iommu);
> @@ -1653,7 +1666,7 @@ static int __init init_dmars(void)
>   		 * we could share the same root & context tables
>   		 * among all IOMMU's. Need to Split it later.
>   		 */
> -		ret = iommu_alloc_root_entry(iommu);
> +		ret = iommu_alloc_root_entry(iommu, iommu_ser);
>   		if (ret)
>   			goto free_iommu;
>   
> @@ -2112,6 +2125,7 @@ int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg)
>   static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
>   {
>   	struct intel_iommu *iommu = dmaru->iommu;
> +	struct iommu_ser *iommu_ser = NULL;
>   	int ret;
>   
>   	/*
> @@ -2120,7 +2134,11 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
>   	if (iommu->gcmd & DMA_GCMD_TE)
>   		iommu_disable_translation(iommu);
>   
> -	ret = iommu_alloc_root_entry(iommu);
> +#if IS_ENABLED(CONFIG_LIVEUPDATE)
> +	iommu_ser = iommu_get_preserved_data(iommu->reg_phys, IOMMU_INTEL);
> +#endif
> +
> +	ret = iommu_alloc_root_entry(iommu, iommu_ser);
>   	if (ret)
>   		goto out;
>   
> diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
> index 1eb60ce1300f..b0c56e27f167 100644
> --- a/drivers/iommu/intel/iommu.h
> +++ b/drivers/iommu/intel/iommu.h
> @@ -1283,6 +1283,8 @@ int intel_iommu_preserve_device(struct device *dev, struct device_ser *device_se
>   void intel_iommu_unpreserve_device(struct device *dev, struct device_ser *device_ser);
>   int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_ser *iommu_ser);
>   void intel_iommu_unpreserve(struct iommu_device *iommu, struct iommu_ser *iommu_ser);
> +void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu,
> +					       struct iommu_ser *iommu_ser);
>   bool intel_iommu_liveupdate_clear_context_entries(struct intel_iommu *iommu);
>   #endif
>   
> diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupdate.c
> index 3f8c7f15bc36..140887187084 100644
> --- a/drivers/iommu/intel/liveupdate.c
> +++ b/drivers/iommu/intel/liveupdate.c
> @@ -73,6 +73,36 @@ static int preserve_iommu_context(struct intel_iommu *iommu)
>   	return ret;
>   }
>   
> +static void restore_iommu_context(struct intel_iommu *iommu)
> +{
> +	struct context_entry *context;
> +	int i;
> +
> +	for (i = 0; i < ROOT_ENTRY_NR; i++) {
> +		context = iommu_context_addr(iommu, i, 0, 0);
> +		if (context)
> +			BUG_ON(!kho_restore_folio(virt_to_phys(context)));
> +
> +		if (!sm_supported(iommu))
> +			continue;
> +
> +		context = iommu_context_addr(iommu, i, 0x80, 0);
> +		if (context)
> +			BUG_ON(!kho_restore_folio(virt_to_phys(context)));
> +	}
> +}
> +
> +void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu,
> +					       struct iommu_ser *iommu_ser)
> +{
> +	BUG_ON(!kho_restore_folio(iommu_ser->intel.root_table));
> +	iommu->root_entry = __va(iommu_ser->intel.root_table);
> +
> +	restore_iommu_context(iommu);
> +	pr_info("Restored IOMMU[0x%llx] Root Table at: 0x%llx\n",
> +		iommu->reg_phys, iommu_ser->intel.root_table);
> +}
> +
>   int intel_iommu_preserve_device(struct device *dev, struct device_ser *device_ser)
>   {
>   	struct device_domain_info *info = dev_iommu_priv_get(dev);

Thanks,
baolu

  reply	other threads:[~2025-12-04  6:48 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-02 23:02 [RFC PATCH v2 00/32] Add live update state preservation Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 01/32] iommufd: Allow HWPTs to have a NULL IOAS Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 02/32] iommufd: split alloc and domain assign from iommufd_hwpt_paging_alloc Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 03/32] iommufd: Add basic skeleton based on liveupdate_file_handler Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 04/32] iommufd-lu: Implement basic prepare/cancel/finish/retrieve using folios Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 05/32] iommufd-lu: Implement ioctl to let userspace mark an HWPT to be preserved Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 06/32] iommufd-lu: Persist iommu hardware pagetables for live update Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 07/32] iommu: Add liveupdate FLB for IOMMU state preservation Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 08/32] iommu: Register IOMMU FLB with iommufd file handler Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 09/32] iommu: Implement IOMMU LU FLB preserve/unpreserve callbacks Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 10/32] iommu: Add iommu_domain ops to preserve, unpreserve and restore Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 11/32] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages Samiullah Khawaja
2025-12-04  2:25   ` Baolu Lu
2025-12-04 17:39     ` Samiullah Khawaja
2025-12-05  4:58       ` Baolu Lu
2025-12-02 23:02 ` [RFC PATCH v2 12/32] iommupt: Implement preserve/unpreserve/restore callbacks Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 13/32] iommu: Add APIs to preserve/unpreserve iommu domains Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 14/32] iommufd: Use the iommu_domain_preserve/unpreserve APIs Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 15/32] iommu: Add API to keep track of iommu domain attachments Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 16/32] iommu: Add API to preserve/unpreserve a device Samiullah Khawaja
2025-12-04  5:46   ` Baolu Lu
2025-12-04 17:47     ` Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 17/32] iommu/vt-d: Implement device and iommu preserve/unpreserve ops Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 18/32] iommufd: Add APIs to preserve/unpreserve a vfio cdev Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 19/32] vfio/pci: Preserve the iommufd state of the " Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 20/32] iommu: Add APIs to get preserved state of a device Samiullah Khawaja
2025-12-04  6:19   ` Baolu Lu
2025-12-04 17:48     ` Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 21/32] iommu/vt-d: Clean the context entries of unpreserved devices Samiullah Khawaja
2025-12-04  6:28   ` Baolu Lu
2025-12-02 23:02 ` [RFC PATCH v2 22/32] iommu: Implement IOMMU FLB retrieve and finish ops Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 23/32] iommu: Add an API get the preserved state of an IOMMU Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 24/32] iommu/vt-d: restore state of the preserved IOMMU Samiullah Khawaja
2025-12-04  6:43   ` Baolu Lu [this message]
2025-12-04 17:55     ` Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 25/32] iommu: Add helper APIs to fetch preserved device state Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 26/32] iommu/vt-d: reclaim domain ids of the preserved devices Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 27/32] iommu: restore preserved domain and reattach Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 28/32] iommu/vt-d: reuse the preserved domain id for preserved devices Samiullah Khawaja
2025-12-02 23:02 ` [RFC PATCH v2 29/32] iommufd: Handle the iommufd can_finish properly Samiullah Khawaja
2025-12-02 23:03 ` [RFC PATCH v2 30/32] iommu: Transfer device ownership after liveupdate Samiullah Khawaja
2025-12-02 23:03 ` [RFC PATCH v2 31/32] iommu: Allow replacing restored domain Samiullah Khawaja
2025-12-02 23:03 ` [RFC PATCH v2 32/32] iommufd/selftest: Add test to verify iommufd preservation Samiullah Khawaja
2026-01-28 19:59 ` [RFC PATCH v2 00/32] Add live update state preservation Jason Gunthorpe
2026-01-29  1:11   ` Samiullah Khawaja
2026-02-02 22:45     ` David Matlack
2026-02-02 23:00       ` Samiullah Khawaja
2026-02-02 23:46         ` Jason Gunthorpe
2026-02-02 23:45       ` Jason Gunthorpe

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=b05a1dd0-9692-4bf7-8fa0-0b3857178d1b@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=ajayachandra@nvidia.com \
    --cc=alex@shazbot.org \
    --cc=chrisl@kernel.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=leonro@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=parav@nvidia.com \
    --cc=pasha.tatashin@soleen.com \
    --cc=praan@google.com \
    --cc=pratyush@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=saeedm@nvidia.com \
    --cc=skhawaja@google.com \
    --cc=vipinsh@google.com \
    --cc=will@kernel.org \
    --cc=witu@nvidia.com \
    --cc=zhuyifei@google.com \
    /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