* [PATCH v5 00/18] iommu: Add live update state preservation
@ 2026-09-21 0:48 Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 01/18] memfd: export memfd_get_seals() Samiullah Khawaja
` (17 more replies)
0 siblings, 18 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pratyush Yadav,
Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
Hi,
This patch series introduces a mechanism for IOMMU state preservation
across live update, including the Intel VT-d driver support
implementation.
Please take a look at the following LWN article to learn about KHO and
Live Update Orchestrator:
https://lwn.net/Articles/1033364/
This work depends on,
- VFIO CDEV preservation series (v5):
https://lore.kernel.org/kvm/20260714151505.3466855-1-vipinsh@google.com/
- PCI base device preservation series (v9):
https://lore.kernel.org/all/20260918200640.887030-1-dmatlack@google.com/
The kernel tree with all dependencies is uploaded to the following
Github location:
https://github.com/samikhawaja/linux/tree/iommu/phase1-v5
The series is targeting IOMMU tree.
Overall Goals:
The goal of this effort is to preserve the IOMMU domains, managed by
iommufd, attached to devices preserved through VFIO cdev. This allows
DMA mappings and IOMMU context of a device assigned to a VM to be
maintained across a kexec live update.
This is achieved by preserving IOMMU page tables using Generic Page
Table support, IOMMU root table and the relevant context entries across
live update.
The functionality in the previously sent RFC is split into two phases
and this series implements the Phase 1. Phase 1 implements the following
functionality:
- Foundational work in IOMMU core and VT-d driver to preserve and
restore IOMMU translation units, IOMMU domains and devices across
liveupdate kexec.
- The preservation is triggered by preserving vfio cdev FD and bound
iommufd FD into a live update session.
- An HWPT (and backing IOMMU domain) is only preserved if it contains
only file type DMA mappings. Also the memfd behind such a mapping
must have been sealed with F_SEAL_SEAL, F_SEAL_GROW and
F_SEAL_SHRINK at the time the mapping was created.
- During live update boot, the state of preserved Intel VT-d, IOMMU
domain and devices is restored.
- The restored IOMMU domains are reattached to the preserved devices
during early boot.
- The DMA ownership of restored devices is also claimed during
live update boot. This means that any attempt to bind a non-vfio
drivers with them or binding a new iommufd with them will fail.
Architectural Overview:
The target architecture for IOMMU state preservation across a live
update involves coordination between the Live Update Orchestrator,
iommufd, and the IOMMU drivers.
The core design uses the Live Update Orchestrator's file descriptor
preservation mechanism to preserve iommufd file descriptors. The user
marks the iommufd HWPTs for preservation using a new ioctl added in this
series. Once done, the preservation of iommufd inside an LUO session is
triggered using LUO ioctls. During preservation, the LUO preserve
callback for an iommufd walks through the HWPTs it manages to identify
the ones that need to be preserved. Once identified, a new IOMMU core
API is used to preserve the iommu domain. The IOMMU core uses Generic
Page Table to preserve the page tables of these domains. The domains are
then marked as preserved.
When the user triggers the preservation of a VFIO cdev that is attached
to an iommufd that is preserved, the device attachment state of that
VFIO cdev is also preserved using an API exported by iommufd. IOMMUFD
fetches all the information that needs to be preserved and calls the
IOMMU core API to preserve the device state. The IOMMU core also
preserves state of IOMMU that is associated with this device.
The IOMMU core has LUO FLB registered with the iommufd LUO file handler
so the preserved iommu domain and iommu hardware unit state is available
during boot for early restore in the next kernel.
During boot the driver fetches the preserved state from the IOMMU core
and restores the state of preserved IOMMUs. Later when IOMMU core goes
through the devices and probes them, the iommu domains of preserved
devices are restored and the preserved devices are attached to them.
During attachment, the DMA ownership of these devices is also claimed.
Tested:
The new iommufd_liveupdate_kexec_test selftest was used to verify the
preservation logic. It was tested using QEMU with virtual IOMMU (VT-d)
support with virtio pcie device bound to the vfio-pci driver.
Also Tested on an Intel machine with DSA device bound to vfio-pci
driver. Note that preservation is refused for devices where ATS is
supported, so the machine must be booted with pci=noats.
Following steps were followed for verification,
- Bind the test device with vfio-pci driver
- Run test on the machine by running
./iommufd_liveupdate_kexec_test <vfio-cdev-path>
- Trigger Kexec.
- After reboot, try binding the device to a non-vfio pci driver,
echo <device bdf> > /sys/bus/pci/drivers/pci-pf-stub/bind
- This should fail with "Device or resource busy".
- Bind the device with vfio-pci driver and run the test again.
./iommufd_liveupdate_kexec_test <vfio-cdev-path> -s 2
- Test verifies that the device cannot be bound with a new iommufd and
the session cannot be finished.
Future and in progress work:
- Phase 2 with IOMMUFD restore to reclaim the preserved vfio cdev and
restore the preserved HWPTs.
- Full support for PASID preservation, i.e. non-zero PASIDs.
- Nested IOMMU preservation.
- Extend support to other IOMMU architectures (e.g., AMD-Vi).
- DMA Alloc preservation support, buddy allocator only.
https://lore.kernel.org/all/20260708234854.4044652-1-skhawaja@google.com/
- Arm sMMUv3 preservation support
https://github.com/pran005/linux/tree/wip-arm-smmu-lu
Roadmap:
The doc below gives a break down of the overall work into Patch series
needed to complete Liveupdate feature from IOMMU perspective:
https://docs.google.com/document/d/1enDn-uPE9U77U-xHEnzn6HHGKiePSAtMIP8EDU3NO0M
High-Level Sequence Flow:
The following diagrams illustrate the high-level interactions during the
preservation phase. Note that function names in the diagram are kept
abbreviated to save horizontal space.
Preserve:
Before live update the PREPARE event of Liveupdate Orchestrator invokes
callbacks of the registered file and subsystem handlers.
Userspace (VMM) | LUO Core | iommufd | IOMMU Core | IOMMU Driver
-----------------|----------|---------------|---------------|-------------
| | | |
MARK_HWPT | | | |
---------------------------> | |
| | Mark HWPT for | |
| | preservation | |
| | | |
PRESERVE | | | |
iommufd_fd | | | |
-----------------> | | |
| preserve | | |
|----------> | |
| | For each HWPT | |
| |--------------> |
| | | domain_presrv |
| | |-------------->
| | | | gpt(preserve)
| | |<--------------|
| |<--------------| |
|<---------| | |
| | | |
... | | | |
| | | |
PRESERVE, | | | |
vfio_cdev_fd | | | |
-----------------> | | |
| preserve | | |
|----------> | |
| | | |
| | iommu_preserv | |
| | _device() | |
| |--------------> |
| | | preserve |
| | | (iommu_hw) |
| | |-------------->
| | | | preserve(root)
| | |<--------------|
| | | |
| | | preserve |
| | | _device(dev) |
| | |-------------->
| | | | preserve(pasid)
| | |<--------------|
| |<--------------| |
|<---------| | |
Restore:
After a live update, the preserved state is restored during boot.
Userspace (VMM) | LUO Core | iommufd | IOMMU Core | IOMMU Driver
-----------------|----------|---------------|---------------|-------------
| | | |
| | | | Restore
| | | | Root, DIDs
| | | |
| | | | Register
| | | probe devices |
| | | |
| | | restore |
| | | domain |
| | |-------------->
| | | | restore
| | | reattach |
| | | domain |
| | |-------------->
| | | |
Looking forward to your feedback on this.
Changelog:
v5:
- Rebased on top of PCI preservation v9 and VFIO preservation v5.
- Do not preserve devices that share iommu groups.
- Save the DMA owner (iommufd) token in the serialized device state.
- Use READ_ONCE/WRITE_ONCE for dev->iommu->device_ser.
- Refuse preservation of devices where ATS is supported. Also drop PRE
disable as it depends on ATS.
- Drop the fallback stubs for the preserve/unpreserve ops.
- Clear the present bit and flush before clearing the context entry.
- Add comments on global invalidation and why the IOMMU lock is not
needed during shutdown.
- Treat -ENODATA, -ENOENT and -EOPNOTSUPP as absent FLB during
retrieve and fail only on other errors.
- Reclaim DID by first checking if the DID is already reclaimed by
another preserved device.
- Handle release device by doing teardown and reattach of restored
domain to restored devices.
- Validate the domain physical address, the restored domain identity
and the IOMMU identity before reattaching.
v4:
- Add kdocs.
- Add 64BIT Kconfig dependency.
- Add FLB version into the serialized state.
- Replace bitfield in KHO state with u8 and padding.
- Add fallback stub for intel_iommu_unpreserve_device.
- Reject real DMA subdevices with -EOPNOTSUPP.
- Add VTD_PRESERVED_BITMAP_LONGS macro for context tables.
- Disable PRE in preserved context entries during shutdown.
- Unify DMAR free and disable into release_dmar_iommu.
- Move Intel IOMMU domain reattach logic into separate function and
file.
- Rename intel_pasid_try_restore_table to intel_pasid_restore_table.
- Use __aligned_u64 for hwpt_token in UAPI struct.
- Reject PRI usage in HWPT preservation.
- Remove liveupdate_preserve bitfield from hwpt_paging.
- Convert liveupdate_preserved bitfield to bool.
- Replace group-level boolean with per-device flag and group refcount.
- Check device_array in pasid attachment before preserving.
- Collect reviews and acks.
v3:
- Add Reviewed-by tags.
- Export memfd_get_seals for iommufd module usage.
- Move iommu_flb_obj to liveupdate.c.
- Replace bitfields with u32 flags.
- Move callbacks from iommu_domain_ops to pt_iommu_ops.
- Fix DYNAMIC_TOP support by saving range.max_vasz_lg2.
- Replace BUG_ON with iommu_restore_pages helper.
- Update to use IOMMU_SER_FLAG_INCOMING and IOMMU_SER_FLAG_DELETED.
- Split out shutdown context-entry clearing logic into separate patch.
- Add context_tables_bitmap to track and preserve only active tables.
- Skip iommu_set_root_entry if restored.
- Fix array iteration bug by splitting macros.
- Move restore logic into caller instead of iommu_alloc_root_entry.
- Add robust IS_ERR checks for iommu_ser.
- Add safety checks for array traversal and domain physical address.
- Remove pasid_cleanup_preserved_table.
- Update handle_pd to dynamically iterate over valid PDE entries.
- Link liveupdate.o into iommufd.o to resolve modpost errors.
- Refactor liveupdate_immutable into nr_preserved_domains counter.
- Save actual number of preserved HWPTs to TOCTOU.
v2:
- Move IOMMU_LIVEUPDATE under IOMMU_SUPPORT dependencies.
- Update copyright year to 2026.
- Add Kernel-doc for FLB struct.
- Add an ASCII diagram for FLB memory layout.
- Change compatibility to iommu-liveupdate-v1.
- Rename structs to more reasonable names.
- Add comment explaining the rationale for BUG_ON during restoration.
- Rename 'did' to 'attachment_id'.
- Use phys_to_virt() and virt_to_phys() consistently.
- Create separate functions for FLB unpreserve and folio_put.
- Return the virtual address from the restore_array function.
- Free serialized state on finish without checking for NULL.
- Rename 'iommu-lu.h' to 'iommu-liveupdate.h'.
- Suffix max with _per_page (iommu_max_objs_per_page).
- Move unused helpers to the patches that actually need them.
- Rename reserve_obj_ser to alloc_object_ser.
- Only allow vfio drivers to use preserve_device APIs.
- Move ops declarations under CONFIG_IOMMU_LIVEUPDATE guards.
- Add lockdep_assert_held() validations in locked functions.
- Rename device_ser_match to match_device_ser.
- Create iommu_folio_update_stats(folio, nr_pages) helper.
- Explicitly set incoherent flag to false for restored pages.
- Do not use unpreserve_pages() function for error handling.
- Preserve vasz and sign_extend of a domain.
- Make the restored domain free-only.
- Ignore error of pt_descend() during restore as it should never fail (dead code).
- On domain restore, clear all features except the preserved.
- Add a KUnit test to verify that a restored domain can be freed with zeroed features.
- Bypass paging_domain_compatible() checks for restored domains.
- Use an explicit if-check for preserved state to clear the context entries.
- Rename clean_context to clear_unpreserved_context_entries.
- Use "DMAR:" in pr_fmt instead of "iommu:".
- Pass ROOT_ENTRY_NR directly into unpreserve functions.
- Remove IOMMU lock during preserve as not needed.
- Iterate all devices when clearing context entries instead of just PCIe devices.
- Do not populate empty unpreserve_device callbacks if not needed.
- Rename unpreserve_iommu_context to unpreserve_iommu_context_table.
- Check the associated IOMMU in _restore_used_domain_id.
- Add a comment that MMIO base is used as token for identifying IOMMU.
- Add a comment in _restore_used_domain_ids that IDA allocation can safely fail.
- Mark IOMMU state restored after restoring to prevent double restoration and UAF.
- Fix domain leak in iommu_restore_domain error path.
- Check DID start range before allocation.
- Add lockdep_assert_held in group domain restoration path.
- Add WARN_ON to catch unexpected group ownership during device probe.
- Add comment explaining group ownership setup and reclaim during device probe.
- Cleanup the full PASID directory instead of only 4K.
- Rename ioctl to IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE.
- Use XArray to save the HWPT liveupdate marks.
- Add the ioctl struct to ucmd_buffer.
- Make the ioctl 64-bit aligned (struct padding).
- Add UAPI documentation stating that HWPTs cannot be unmarked.
- Add UAPI documentation stating that only file-based mappings are allowed.
- Rename structs to use "liveupdate" instead of "lu".
- Rework HWPT preservation to prevent TOCTOU and mutex lock inside XA spin lock.
- Check preserved pages at the IOPT level, not the HWPT level.
- Return -EOPNOTSUPP if there are PASID attachments.
- Do not allow detach (or attach) once the device is preserved.
- Add MODULE_IMPORT_NS("IOMMUFD") in vfio_pci_liveupdate.c.
- Make token u64.
- Modify selftest to use live update kexec_test pattern.
- Use helper macros in test instead of repeating ksft_assert
- Rename setup_cdev to open_cdev.
- Define constants (tokens) as u64 to avoid ABI warnings.
v4: https://lore.kernel.org/kvm/20260808022723.3893618-1-skhawaja@google.com/
v3: https://lore.kernel.org/all/20260614233728.2212104-1-skhawaja@google.com/
v2: https://lore.kernel.org/all/20260427175633.1978233-1-skhawaja@google.com/
v1: https://lore.kernel.org/all/20260203220948.2176157-1-skhawaja@google.com/
rfcv2: https://lore.kernel.org/all/20251202230303.1017519-1-skhawaja@google.com/
rfcv1: https://lore.kernel.org/all/20250928190624.3735830-1-skhawaja@google.com/
Samiullah Khawaja (16):
memfd: export memfd_get_seals()
iommu: Implement IOMMU Live update FLB callbacks
iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
iommupt: Implement preserve/unpreserve/restore callbacks
iommu: Implement IOMMU domain preservation
iommu: Implement device and IOMMU HW preservation
iommu/vt-d: Implement device and iommu preserve/unpreserve ops
iommu/vt-d: Clear unpreserved context entries during shutdown
iommu: Add APIs to get iommu and device preserved state
iommu/vt-d: Restore IOMMU state and reclaimed domain ids
iommu: Restore and reattach preserved domains to devices
iommu/vt-d: Handle reattach of the restored domain
iommu/vt-d: Preserve PASID table of preserved device
iommufd: Add APIs to preserve/unpreserve a vfio cdev
vfio/pci: Preserve the iommufd state of the vfio cdev
iommufd/selftest: Add test to verify iommufd preservation
YiFei Zhu (2):
iommufd: Implement ioctl to mark HWPT for preservation
iommufd: Persist iommu hardware pagetables for live update
MAINTAINERS | 19 +
drivers/iommu/Kconfig | 12 +
drivers/iommu/Makefile | 1 +
drivers/iommu/generic_pt/iommu_pt.h | 152 ++++
drivers/iommu/generic_pt/kunit_iommu_pt.h | 30 +
drivers/iommu/intel/Makefile | 1 +
drivers/iommu/intel/iommu.c | 167 ++--
drivers/iommu/intel/iommu.h | 39 +
drivers/iommu/intel/liveupdate.c | 740 ++++++++++++++++
drivers/iommu/intel/pasid.c | 10 +-
drivers/iommu/intel/pasid.h | 8 +
drivers/iommu/iommu-pages.c | 108 ++-
drivers/iommu/iommu-pages.h | 30 +
drivers/iommu/iommu-priv.h | 2 +
drivers/iommu/iommu.c | 176 +++-
drivers/iommu/iommufd/Makefile | 1 +
drivers/iommu/iommufd/device.c | 142 +++
drivers/iommu/iommufd/io_pagetable.c | 11 +
drivers/iommu/iommufd/io_pagetable.h | 1 +
drivers/iommu/iommufd/iommufd_private.h | 49 +
drivers/iommu/iommufd/liveupdate.c | 379 ++++++++
drivers/iommu/iommufd/main.c | 19 +-
drivers/iommu/iommufd/pages.c | 11 +
drivers/iommu/liveupdate.c | 834 ++++++++++++++++++
drivers/vfio/device_cdev.c | 10 +
drivers/vfio/pci/vfio_pci_liveupdate.c | 28 +-
include/linux/generic_pt/iommu.h | 30 +
include/linux/iommu-liveupdate.h | 204 +++++
include/linux/iommu.h | 47 +
include/linux/iommufd.h | 29 +
include/linux/kho/abi/iommu.h | 253 ++++++
include/linux/kho/abi/iommufd.h | 51 ++
include/uapi/linux/iommufd.h | 27 +
mm/memfd.c | 8 +
tools/testing/selftests/iommu/Makefile | 12 +
.../iommu/iommufd_liveupdate_kexec_test.c | 241 +++++
36 files changed, 3812 insertions(+), 70 deletions(-)
create mode 100644 drivers/iommu/intel/liveupdate.c
create mode 100644 drivers/iommu/iommufd/liveupdate.c
create mode 100644 drivers/iommu/liveupdate.c
create mode 100644 include/linux/iommu-liveupdate.h
create mode 100644 include/linux/kho/abi/iommu.h
create mode 100644 include/linux/kho/abi/iommufd.h
create mode 100644 tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c
base-commit: 945d61765894dda2f0344de50cb1ade2e51b66fd
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 01/18] memfd: export memfd_get_seals()
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 02/18] iommu: Implement IOMMU Live update FLB callbacks Samiullah Khawaja
` (16 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Hugh Dickins, Baolin Wang, Andrew Morton,
linux-mm, Pratyush Yadav (Google),
Ankit Soni, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pasha Tatashin,
David Matlack, Pranjal Shrivastava, Vipin Sharma
memfd_get_seals() is used by iommufd during preservation to make sure
that he preserved memfd was sealed when it was mapped into iommufd.
Since iommufd can be built as a module, export memfd_get_seals() to
avoid linker error.
Cc: Hugh Dickins <hughd@google.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org
Acked-by: Pratyush Yadav (Google) <pratyush@kernel.org>
Reviewed-by: Ankit Soni <Ankit.Soni@amd.com>
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
mm/memfd.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/mm/memfd.c b/mm/memfd.c
index c708d92533f4..888ccf3778c7 100644
--- a/mm/memfd.c
+++ b/mm/memfd.c
@@ -6,6 +6,7 @@
* use by hugetlbfs as well as tmpfs.
*/
+#include <linux/export.h>
#include <linux/fs.h>
#include <linux/vfs.h>
#include <linux/pagemap.h>
@@ -310,12 +311,19 @@ int memfd_add_seals(struct file *file, unsigned int seals)
return error;
}
+/**
+ * memfd_get_seals - Gets current seals on a memfd
+ * @file: struct file representing the memfd
+ *
+ * Returns seals if file is a memfd, otherwise returns -EINVAL
+ */
int memfd_get_seals(struct file *file)
{
unsigned int *seals = memfd_file_seals_ptr(file);
return seals ? *seals : -EINVAL;
}
+EXPORT_SYMBOL_GPL(memfd_get_seals);
long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg)
{
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 02/18] iommu: Implement IOMMU Live update FLB callbacks
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 01/18] memfd: export memfd_get_seals() Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 03/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages Samiullah Khawaja
` (15 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pratyush Yadav,
Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
Add liveupdate FLB for IOMMU state preservation. Use KHO preserve memory
alloc/free helper functions to allocate memory for the IOMMU Live update
FLB object and the serialization structs for device, domain and iommu.
During retrieve, walk through the preserved obj array headers and
restore each folio. Also recreate the FLB obj.
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
MAINTAINERS | 9 ++
drivers/iommu/Kconfig | 12 ++
drivers/iommu/Makefile | 1 +
drivers/iommu/liveupdate.c | 253 +++++++++++++++++++++++++++++++
include/linux/iommu-liveupdate.h | 28 ++++
include/linux/kho/abi/iommu.h | 212 ++++++++++++++++++++++++++
6 files changed, 515 insertions(+)
create mode 100644 drivers/iommu/liveupdate.c
create mode 100644 include/linux/iommu-liveupdate.h
create mode 100644 include/linux/kho/abi/iommu.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 2b62a0b8a48c..2114b50412ee 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13715,6 +13715,15 @@ F: include/linux/iova.h
F: include/linux/of_iommu.h
F: rust/kernel/iommu/
+IOMMU LIVEUPDATE
+M: Samiullah Khawaja <skhawaja@google.com>
+R: Pranjal Shrivastava <praan@google.com>
+L: iommu@lists.linux.dev
+S: Maintained
+F: drivers/iommu/liveupdate.c
+F: include/linux/iommu-liveupdate.h
+F: include/linux/kho/abi/iommu.h
+
IOMMUFD
M: Jason Gunthorpe <jgg@nvidia.com>
M: Kevin Tian <kevin.tian@intel.com>
diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index 6e07bd69467a..56a96d22cc8b 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -405,6 +405,18 @@ config IOMMU_DEBUG_PAGEALLOC
line to activate the runtime checks.
If unsure, say N.
+
+config IOMMU_LIVEUPDATE
+ bool "IOMMU live update state preservation support"
+ depends on LIVEUPDATE && IOMMUFD && 64BIT
+ help
+ Enable support for preserving IOMMU state across a kexec live update.
+
+ This allows devices managed by iommufd to maintain their DMA mappings
+ during kexec base kernel update.
+
+ If unsure, say N.
+
endif # IOMMU_SUPPORT
source "drivers/iommu/generic_pt/Kconfig"
diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile
index 2f05725eaab1..a775fb526e91 100644
--- a/drivers/iommu/Makefile
+++ b/drivers/iommu/Makefile
@@ -17,6 +17,7 @@ obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE) += io-pgtable-arm.o
obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE_KUNIT_TEST) += io-pgtable-arm-selftests.o
obj-$(CONFIG_IOMMU_IO_PGTABLE_DART) += io-pgtable-dart.o
obj-$(CONFIG_IOMMU_IOVA) += iova.o
+obj-$(CONFIG_IOMMU_LIVEUPDATE) += liveupdate.o
obj-$(CONFIG_OF_IOMMU) += of_iommu.o
obj-$(CONFIG_MSM_IOMMU) += msm_iommu.o
obj-$(CONFIG_IPMMU_VMSA) += ipmmu-vmsa.o
diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c
new file mode 100644
index 000000000000..b644f4792532
--- /dev/null
+++ b/drivers/iommu/liveupdate.c
@@ -0,0 +1,253 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2026, Google LLC
+ * Author: Samiullah Khawaja <skhawaja@google.com>
+ */
+
+/**
+ * DOC: IOMMU Live Update
+ *
+ * The IOMMU subsystem participates in the Live Update process to preserve its
+ * IOMMU domains and IOMMU specific state of preserved devices.
+ *
+ * File-Lifecycle-Bound (FLB) Data
+ * ===============================
+ *
+ * Preserved state of IOMMU HW needs to be restored during boot when the IOMMU is
+ * initialized and registered. The preserved IOMMU domains also need to be
+ * restored and associated with the preserved devices early during boot. For
+ * this reason, IOMMU subsystem uses LUO File-Lifecycle-Bound Data to store some
+ * state globally.
+ *
+ * During preservation of IOMMUFD into LUO, some of the state is stored in the
+ * IOMMU FLB so it can be restored during boot. Once the FDs are retrieved from LUO
+ * the restored state can be reassociated with the relevant IOMMUFDs.
+ *
+ * The FLB also contains the state of IOMMU HW that might be shared between
+ * multiple devices, domains and iommufds. This is restored early during boot
+ * and no re-association of this state is needed later.
+ *
+ * As the state is preserved using KHO, the FLB handler callbacks are called by
+ * LUO only when the KHO is enabled. So there is no need to check whether KHO is
+ * enabled in the callback implementation.
+ *
+ */
+
+#define pr_fmt(fmt) "iommu: liveupdate: " fmt
+
+#include <linux/errno.h>
+#include <linux/iommu-liveupdate.h>
+#include <linux/iommu.h>
+#include <linux/kexec_handover.h>
+#include <linux/liveupdate.h>
+
+struct iommu_flb_obj {
+ struct mutex lock;
+ struct iommu_flb_ser *ser;
+
+ struct iommu_hw_array_ser *curr_iommu_array;
+ struct iommu_domain_array_ser *curr_domain_array;
+ struct iommu_device_array_ser *curr_device_array;
+};
+
+static void *iommu_liveupdate_restore_array(u64 array_phys)
+{
+ struct iommu_array_hdr_ser *array_hdr;
+ void *vaddr = array_phys ? phys_to_virt(array_phys) : NULL;
+
+ while (array_phys) {
+ /*
+ * Failure to restore preserved IOMMU state is considered fatal.
+ *
+ * This is because the IOMMU translations for preserved IOMMUs
+ * were kept enabled in the previous kernel and the preserved
+ * devices have their IOMMU domains still present. Not being
+ * able to restore means that the memory mapped into preserved
+ * domains might be already corrupted by the preserved devices.
+ *
+ * There is no way to confirm the integrity of the memory that
+ * was mapped. BUG_ON is the safest option at this point.
+ */
+ BUG_ON(!kho_restore_folio(array_phys));
+ array_hdr = phys_to_virt(array_phys);
+ array_phys = array_hdr->next_array_phys;
+ }
+
+ return vaddr;
+}
+
+static void iommu_liveupdate_unpreserve_free(u64 array_phys)
+{
+ struct iommu_array_hdr_ser *array_hdr;
+
+ while (array_phys) {
+ array_hdr = phys_to_virt(array_phys);
+ array_phys = array_hdr->next_array_phys;
+ kho_unpreserve_free(array_hdr);
+ }
+}
+
+static void iommu_liveupdate_folio_put(u64 array_phys)
+{
+ struct iommu_array_hdr_ser *array_hdr;
+
+ while (array_phys) {
+ array_hdr = phys_to_virt(array_phys);
+ array_phys = array_hdr->next_array_phys;
+ folio_put(virt_to_folio(array_hdr));
+ }
+}
+
+static void iommu_liveupdate_flb_free(struct iommu_flb_obj *obj)
+{
+ iommu_liveupdate_unpreserve_free(obj->ser->iommu_domain_array_phys);
+ iommu_liveupdate_unpreserve_free(obj->ser->device_array_phys);
+ iommu_liveupdate_unpreserve_free(obj->ser->iommu_array_phys);
+
+ kho_unpreserve_free(obj->ser);
+ mutex_destroy(&obj->lock);
+ kfree(obj);
+}
+
+static int iommu_liveupdate_flb_preserve(struct liveupdate_flb_op_args *argp)
+{
+ struct iommu_flb_obj *obj;
+ struct iommu_flb_ser *ser;
+ void *mem;
+
+ /* obj exists only in the current kernel to track preserved state */
+ obj = kzalloc_obj(*obj, GFP_KERNEL);
+ if (!obj)
+ return -ENOMEM;
+
+ mutex_init(&obj->lock);
+
+ /* mem is allocated via KHO and will survive the kexec */
+ mem = kho_alloc_preserve(sizeof(*ser));
+ if (IS_ERR(mem))
+ goto err_free_obj;
+
+ ser = mem;
+ obj->ser = ser;
+ ser->version = IOMMU_LUO_FLB_VERSION;
+
+ mem = kho_alloc_preserve(PAGE_SIZE);
+ if (IS_ERR(mem))
+ goto err_free_ser;
+
+ obj->curr_domain_array = mem;
+ ser->iommu_domain_array_phys = virt_to_phys(obj->curr_domain_array);
+
+ mem = kho_alloc_preserve(PAGE_SIZE);
+ if (IS_ERR(mem))
+ goto err_free_domains;
+
+ obj->curr_device_array = mem;
+ ser->device_array_phys = virt_to_phys(obj->curr_device_array);
+
+ mem = kho_alloc_preserve(PAGE_SIZE);
+ if (IS_ERR(mem))
+ goto err_free_devices;
+
+ obj->curr_iommu_array = mem;
+ ser->iommu_array_phys = virt_to_phys(obj->curr_iommu_array);
+
+ argp->obj = obj;
+ argp->data = virt_to_phys(ser);
+ return 0;
+
+err_free_devices:
+ kho_unpreserve_free(obj->curr_device_array);
+err_free_domains:
+ kho_unpreserve_free(obj->curr_domain_array);
+err_free_ser:
+ kho_unpreserve_free(obj->ser);
+err_free_obj:
+ mutex_destroy(&obj->lock);
+ kfree(obj);
+ return PTR_ERR(mem);
+}
+
+static void iommu_liveupdate_flb_unpreserve(struct liveupdate_flb_op_args *argp)
+{
+ iommu_liveupdate_flb_free(argp->obj);
+}
+
+static void iommu_liveupdate_flb_finish(struct liveupdate_flb_op_args *argp)
+{
+ struct iommu_flb_obj *obj = argp->obj;
+
+ iommu_liveupdate_folio_put(obj->ser->iommu_domain_array_phys);
+ iommu_liveupdate_folio_put(obj->ser->device_array_phys);
+ iommu_liveupdate_folio_put(obj->ser->iommu_array_phys);
+
+ folio_put(virt_to_folio(obj->ser));
+ mutex_destroy(&obj->lock);
+ kfree(obj);
+}
+
+static int iommu_liveupdate_flb_retrieve(struct liveupdate_flb_op_args *argp)
+{
+ struct iommu_flb_obj *obj;
+ struct iommu_flb_ser *ser;
+
+ obj = kzalloc_obj(*obj, GFP_KERNEL);
+ if (!obj) {
+ /*
+ * If retrieve fails, the finish path won't be called as
+ * can_finish() will fail, preventing the restore.
+ */
+ return -ENOMEM;
+ }
+
+ /* Data must be present and valid from the previous kernel */
+ BUG_ON(!kho_restore_folio(argp->data));
+
+ mutex_init(&obj->lock);
+ ser = phys_to_virt(argp->data);
+ obj->ser = ser;
+
+ obj->curr_domain_array = iommu_liveupdate_restore_array(ser->iommu_domain_array_phys);
+ obj->curr_device_array = iommu_liveupdate_restore_array(ser->device_array_phys);
+ obj->curr_iommu_array = iommu_liveupdate_restore_array(ser->iommu_array_phys);
+ argp->obj = obj;
+ return 0;
+}
+
+static struct liveupdate_flb_ops iommu_flb_ops = {
+ .preserve = iommu_liveupdate_flb_preserve,
+ .unpreserve = iommu_liveupdate_flb_unpreserve,
+ .retrieve = iommu_liveupdate_flb_retrieve,
+ .finish = iommu_liveupdate_flb_finish,
+};
+
+static struct liveupdate_flb iommu_flb = {
+ .compatible = IOMMU_LUO_FLB_COMPATIBLE,
+ .ops = &iommu_flb_ops,
+};
+
+/**
+ * iommu_liveupdate_register_flb() - Register IOMMU Live Update FLB
+ * @handler: Live Update file handler to associate FLB with
+ *
+ * Register a Live Update FLB for IOMMU state preservation with the file
+ * handler.
+ *
+ * Return: 0 on success, or negative error code.
+ */
+int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler)
+{
+ return liveupdate_register_flb(handler, &iommu_flb);
+}
+EXPORT_SYMBOL(iommu_liveupdate_register_flb);
+
+/**
+ * iommu_liveupdate_unregister_flb() - Unregister IOMMU Live Update FLB
+ * @handler: Live Update file handler to associate FLB with
+ */
+void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)
+{
+ liveupdate_unregister_flb(handler, &iommu_flb);
+}
+EXPORT_SYMBOL(iommu_liveupdate_unregister_flb);
diff --git a/include/linux/iommu-liveupdate.h b/include/linux/iommu-liveupdate.h
new file mode 100644
index 000000000000..4755ab3cd67a
--- /dev/null
+++ b/include/linux/iommu-liveupdate.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (C) 2026, Google LLC
+ * Author: Samiullah Khawaja <skhawaja@google.com>
+ */
+
+#ifndef _LINUX_IOMMU_LIVEUPDATE_H
+#define _LINUX_IOMMU_LIVEUPDATE_H
+
+#include <linux/iommu.h>
+#include <linux/liveupdate.h>
+#include <linux/kho/abi/iommu.h>
+
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler);
+void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler);
+#else
+static inline int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler)
+{
+ return 0;
+}
+
+static inline void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)
+{
+}
+#endif
+#endif /* _LINUX_IOMMU_LIVEUPDATE_H */
diff --git a/include/linux/kho/abi/iommu.h b/include/linux/kho/abi/iommu.h
new file mode 100644
index 000000000000..aa42085409e5
--- /dev/null
+++ b/include/linux/kho/abi/iommu.h
@@ -0,0 +1,212 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (C) 2026, Google LLC
+ * Author: Samiullah Khawaja <skhawaja@google.com>
+ */
+
+#ifndef _LINUX_KHO_ABI_IOMMU_H
+#define _LINUX_KHO_ABI_IOMMU_H
+
+#include <linux/mutex_types.h>
+#include <linux/compiler.h>
+#include <linux/types.h>
+
+/**
+ * DOC: IOMMU File-Lifecycle Bound (FLB) Live Update ABI
+ *
+ * This header defines the ABI for preserving IOMMU state across kexec using
+ * Live Update File-Lifecycle Bound (FLB) data.
+ *
+ * This interface is a contract. Any modification to any of the serialization
+ * structs defined here constitutes a breaking change. Such changes require
+ * incrementing the version number in IOMMU_LUO_FLB_VERSION.
+ *
+ * Memory Layout of Serialization Structures:
+ * ==========================================
+ *
+ * Each serialized type (IOMMU, Domain, Device) is stored in a linked list of
+ * arrays. The first array is allocated initially. When an array is full, a new
+ * array is allocated and its physical address is stored in the next_array_phys
+ * field of the hdr of the current array.
+ *
+ * Top Level (struct iommu_flb_ser):
+ * +---------------------------+
+ * | - iommu_array_phys |
+ * | - iommu_domain_array_phys |
+ * | - device_array_phys |
+ * +---------------------------+
+ *
+ * Each Array contains the serialized objects of the respective type. For
+ * example see below the representation of struct iommu_domain_array_ser.
+ *
+ * +---------------------------+ +---------------------------+
+ * | iommu_domain_array_ser |-->| iommu_domain_array_ser |--> NULL
+ * | - hdr.next_array_phys | | - hdr.next_array_phys |
+ * | - hdr.nr_objects | | - hdr.nr_objects |
+ * | | | |
+ * | objects[]: | | objects[]: |
+ * | [ iommu_domain_ser ] | | [ iommu_domain_ser ] |
+ * | [ iommu_domain_ser ] | | [ iommu_domain_ser ] |
+ * | ... | | ... |
+ * +---------------------------+ +---------------------------+
+ *
+ * Each object in the array starts with a common header (iommu_hdr_ser).
+ * For example, the layout of struct iommu_domain_ser is:
+ *
+ * +-----------------------------+
+ * | iommu_domain_ser |
+ * | +-------------------------+ |
+ * | | hdr (iommu_hdr_ser) | |
+ * | | - ref_count | |
+ * | | - deleted / incoming | |
+ * | +-------------------------+ |
+ * | - top_table_phys | |
+ * | - top_level | |
+ * | - restored_domain | |
+ * +-----------------------------+
+ *
+ * This pattern applies identically to iommu_device_ser and iommu_hw_ser.
+ */
+
+#define IOMMU_LUO_FLB_COMPATIBLE "iommu-liveupdate"
+#define IOMMU_LUO_FLB_VERSION 1
+
+/**
+ * enum iommu_type_ser - Type of the IOMMU being preserved
+ * @IOMMU_INVALID: Invalid type of IOMMU
+ *
+ * IOMMU type is stored in the IOMMU HW state to differentiate between various
+ * IOMMU HWs.
+ */
+enum iommu_type_ser {
+ IOMMU_INVALID,
+};
+
+#define IOMMU_SER_FLAG_DELETED (1 << 0)
+#define IOMMU_SER_FLAG_INCOMING (1 << 1)
+
+/**
+ * struct iommu_hdr_ser - Common header for all serialized IOMMU objects
+ * @ref_count: Reference count for the object
+ * @flags: Bitmask of IOMMU_SER_FLAG_* flags indicating object state
+ */
+struct iommu_hdr_ser {
+ u32 ref_count;
+ u32 flags;
+} __packed;
+
+/**
+ * struct iommu_domain_ser - Serialized state of an IOMMU domain
+ * @hdr: Common object header
+ * @top_table_phys: Physical address of the top-level page table
+ * @top_level: Level of the top-level page table
+ * @vasz: Virtual Address Size
+ * @sign_extend: FEAT_SIGN_EXTEND is enabled for this domain
+ * @restored_domain: Pointer to the restored domain (valid only after restore)
+ */
+struct iommu_domain_ser {
+ struct iommu_hdr_ser hdr;
+ u64 top_table_phys;
+ u64 top_level;
+ u32 vasz;
+ u8 sign_extend;
+ u8 padding[3];
+ struct iommu_domain *restored_domain;
+} __packed;
+
+/**
+ * struct iommu_dev_map_ser - Serialized mapping between device, domain,
+ * and IOMMU instance.
+ * @attachment_id: ID of the attachment between device and domain.
+ * @domain_phys: Physical address of the domain
+ * @iommu_phys: Physical address of the IOMMU
+ */
+struct iommu_dev_map_ser {
+ u64 attachment_id;
+ u64 domain_phys;
+ u64 iommu_phys;
+} __packed;
+
+/**
+ * struct iommu_device_ser - Serialized state of a device
+ * @hdr: Common object header
+ * @devid: Device ID
+ * @pci_domain_nr: PCI domain number
+ * @dma_owner_token: Token to identify the DMA owner of this device
+ * @domain_iommu_ser: Domain and IOMMU mapping
+ */
+struct iommu_device_ser {
+ struct iommu_hdr_ser hdr;
+ u32 devid;
+ u32 pci_domain_nr;
+ u64 dma_owner_token;
+ struct iommu_dev_map_ser domain_iommu_ser;
+} __packed;
+
+/**
+ * struct iommu_hw_ser - Serialized state of an IOMMU instance
+ * @hdr: Common object header
+ * @token: Unique token for the IOMMU
+ * @type: IOMMU type serialized state belongs to
+ */
+struct iommu_hw_ser {
+ struct iommu_hdr_ser hdr;
+ u64 token;
+ u64 type;
+} __packed;
+
+/**
+ * struct iommu_array_hdr_ser - Header for an array of serialized objects
+ * @next_array_phys: Physical address of the next array of objects
+ * @nr_objects: Number of objects in the current array
+ */
+struct iommu_array_hdr_ser {
+ u64 next_array_phys;
+ u64 nr_objects;
+} __packed;
+
+/**
+ * struct iommu_hw_array_ser - An array containing serialized IOMMU HWs
+ * @hdr: Array header
+ * @objects: Array of serialized IOMMU devices
+ */
+struct iommu_hw_array_ser {
+ struct iommu_array_hdr_ser hdr;
+ struct iommu_hw_ser objects[];
+} __packed;
+
+/**
+ * struct iommu_domain_array_ser - An array containing serialized domains
+ * @hdr: Array header
+ * @objects: Array of serialized domains
+ */
+struct iommu_domain_array_ser {
+ struct iommu_array_hdr_ser hdr;
+ struct iommu_domain_ser objects[];
+} __packed;
+
+/**
+ * struct iommu_device_array_ser - An array containing serialized devices
+ * @hdr: Array header
+ * @objects: Array of serialized devices
+ */
+struct iommu_device_array_ser {
+ struct iommu_array_hdr_ser hdr;
+ struct iommu_device_ser objects[];
+} __packed;
+
+/**
+ * struct iommu_flb_ser - Top-level serialization structure
+ * @iommu_array_phys: Physical address of the first array of IOMMU HWs
+ * @iommu_domain_array_phys: Physical address of the first array of domains
+ * @device_array_phys: Physical address of the first array of devices
+ */
+struct iommu_flb_ser {
+ u64 version;
+ u64 iommu_array_phys;
+ u64 iommu_domain_array_phys;
+ u64 device_array_phys;
+} __packed;
+
+#endif /* _LINUX_KHO_ABI_IOMMU_H */
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 03/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 01/18] memfd: export memfd_get_seals() Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 02/18] iommu: Implement IOMMU Live update FLB callbacks Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 04/18] iommupt: Implement preserve/unpreserve/restore callbacks Samiullah Khawaja
` (14 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Pranjal Shrivastava, Robin Murphy, Kevin Tian,
Alex Williamson, Shuah Khan, iommu, linux-kernel, kvm,
Pratyush Yadav, Pasha Tatashin, David Matlack, Andrew Morton,
Vipin Sharma
IOMMU pages are allocated/freed using APIs using struct ioptdesc. For
the proper preservation and restoration of ioptdesc add helper
functions.
Reviewed-by: Pranjal Shrivastava <praan@google.com>
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/iommu-pages.c | 108 ++++++++++++++++++++++++++++++++++--
drivers/iommu/iommu-pages.h | 30 ++++++++++
2 files changed, 134 insertions(+), 4 deletions(-)
diff --git a/drivers/iommu/iommu-pages.c b/drivers/iommu/iommu-pages.c
index 3bab175d8557..96cc935fb4e1 100644
--- a/drivers/iommu/iommu-pages.c
+++ b/drivers/iommu/iommu-pages.c
@@ -6,6 +6,7 @@
#include "iommu-pages.h"
#include <linux/dma-mapping.h>
#include <linux/gfp.h>
+#include <linux/kexec_handover.h>
#include <linux/mm.h>
#define IOPTDESC_MATCH(pg_elm, elm) \
@@ -28,6 +29,13 @@ static inline size_t ioptdesc_mem_size(struct ioptdesc *desc)
return 1UL << (folio_order(ioptdesc_folio(desc)) + PAGE_SHIFT);
}
+static inline void iommu_folio_update_stats(struct folio *folio,
+ unsigned long nr_pages)
+{
+ mod_node_page_state(folio_pgdat(folio), NR_IOMMU_PAGES, nr_pages);
+ lruvec_stat_mod_folio(folio, NR_SECONDARY_PAGETABLE, nr_pages);
+}
+
/**
* iommu_alloc_pages_node_sz - Allocate a zeroed page of a given size from
* specific NUMA node
@@ -80,8 +88,7 @@ void *iommu_alloc_pages_node_sz(int nid, gfp_t gfp, size_t size)
* rather large, i.e. multiple gigabytes in size.
*/
pgcnt = 1UL << order;
- mod_node_page_state(folio_pgdat(folio), NR_IOMMU_PAGES, pgcnt);
- lruvec_stat_mod_folio(folio, NR_SECONDARY_PAGETABLE, pgcnt);
+ iommu_folio_update_stats(folio, pgcnt);
return folio_address(folio);
}
@@ -95,8 +102,7 @@ static void __iommu_free_desc(struct ioptdesc *iopt)
if (IOMMU_PAGES_USE_DMA_API)
WARN_ON_ONCE(iopt->incoherent);
- mod_node_page_state(folio_pgdat(folio), NR_IOMMU_PAGES, -pgcnt);
- lruvec_stat_mod_folio(folio, NR_SECONDARY_PAGETABLE, -pgcnt);
+ iommu_folio_update_stats(folio, -pgcnt);
folio_put(folio);
}
@@ -131,6 +137,100 @@ void iommu_put_pages_list(struct iommu_pages_list *list)
}
EXPORT_SYMBOL_GPL(iommu_put_pages_list);
+#if IS_ENABLED(CONFIG_IOMMU_LIVEUPDATE)
+/**
+ * iommu_unpreserve_pages - Unpreserve pages that were preserved in KHO
+ * @virt: Virtual address of page to be unpreserved
+ */
+void iommu_unpreserve_pages(void *virt)
+{
+ kho_unpreserve_folio(ioptdesc_folio(virt_to_ioptdesc(virt)));
+}
+EXPORT_SYMBOL_GPL(iommu_unpreserve_pages);
+
+/**
+ * iommu_preserve_pages - Preserve pages using KHO
+ * @virt: Virtual address of page to preserve
+ *
+ * Returns 0 on success, negative error on failure
+ */
+int iommu_preserve_pages(void *virt)
+{
+ return kho_preserve_folio(ioptdesc_folio(virt_to_ioptdesc(virt)));
+}
+EXPORT_SYMBOL_GPL(iommu_preserve_pages);
+
+/**
+ * iommu_unpreserve_pages_list - Unpreserve a list of KHO preserved pages
+ * @list: List of pages to unpreserve
+ */
+void iommu_unpreserve_pages_list(struct iommu_pages_list *list)
+{
+ struct ioptdesc *iopt;
+
+ list_for_each_entry(iopt, &list->pages, iopt_freelist_elm)
+ kho_unpreserve_folio(ioptdesc_folio(iopt));
+}
+EXPORT_SYMBOL_GPL(iommu_unpreserve_pages_list);
+
+/**
+ * iommu_restore_pages - Restore pages that were preserved in KHO
+ * @phys: Physical address of page to restore
+ */
+void iommu_restore_pages(u64 phys)
+{
+ struct ioptdesc *iopt;
+ struct folio *folio;
+ unsigned long pgcnt;
+ unsigned int order;
+
+ folio = kho_restore_folio(phys);
+ BUG_ON(!folio);
+
+ iopt = folio_ioptdesc(folio);
+
+ /*
+ * For the restored pages incoherent is set to false as these are not
+ * mapped using the DMA_API. The remapping of these pages using DMA_API
+ * is not needed as these are not going to be written to by the new
+ * kernel.
+ */
+ iopt->incoherent = false;
+
+ order = folio_order(folio);
+ pgcnt = 1UL << order;
+ iommu_folio_update_stats(folio, pgcnt);
+}
+EXPORT_SYMBOL_GPL(iommu_restore_pages);
+
+/**
+ * iommu_preserve_pages_list - Preserve list of pages using KHO
+ * @list: List of pages to preserve
+ *
+ * Returns 0 on success, negative error on failure
+ */
+int iommu_preserve_pages_list(struct iommu_pages_list *list)
+{
+ struct ioptdesc *iopt;
+ int ret;
+
+ list_for_each_entry(iopt, &list->pages, iopt_freelist_elm) {
+ ret = kho_preserve_folio(ioptdesc_folio(iopt));
+ if (ret)
+ goto err;
+ }
+
+ return 0;
+
+err:
+ list_for_each_entry_continue_reverse(iopt, &list->pages, iopt_freelist_elm)
+ kho_unpreserve_folio(ioptdesc_folio(iopt));
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(iommu_preserve_pages_list);
+#endif
+
/**
* iommu_pages_start_incoherent - Setup the page for cache incoherent operation
* @virt: The page to setup
diff --git a/drivers/iommu/iommu-pages.h b/drivers/iommu/iommu-pages.h
index e9e605b5fa3a..3c409ec3d170 100644
--- a/drivers/iommu/iommu-pages.h
+++ b/drivers/iommu/iommu-pages.h
@@ -53,6 +53,36 @@ void *iommu_alloc_pages_node_sz(int nid, gfp_t gfp, size_t size);
void iommu_free_pages(void *virt);
void iommu_put_pages_list(struct iommu_pages_list *list);
+#if IS_ENABLED(CONFIG_IOMMU_LIVEUPDATE)
+int iommu_preserve_pages(void *virt);
+void iommu_unpreserve_pages(void *virt);
+int iommu_preserve_pages_list(struct iommu_pages_list *list);
+void iommu_unpreserve_pages_list(struct iommu_pages_list *list);
+void iommu_restore_pages(u64 phys);
+#else
+static inline int iommu_preserve_pages(void *virt)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline void iommu_unpreserve_pages(void *virt)
+{
+}
+
+static inline int iommu_preserve_pages_list(struct iommu_pages_list *list)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline void iommu_unpreserve_pages_list(struct iommu_pages_list *list)
+{
+}
+
+static inline void iommu_restore_pages(u64 phys)
+{
+}
+#endif
+
/**
* iommu_pages_list_add - add the page to a iommu_pages_list
* @list: List to add the page to
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 04/18] iommupt: Implement preserve/unpreserve/restore callbacks
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (2 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 03/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 05/18] iommu: Implement IOMMU domain preservation Samiullah Khawaja
` (13 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pratyush Yadav,
Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
Add iommupt ops for presevation, unpresevation and restoration of iommu
page tables for liveupdate. Use the existing page walker to preserve the
ioptdesc of the top_table and the lower tables.
Preserve top_level, VASZ and FEAT Sign Extended to restore the domain in
the next kernel. On restore, the domain has only the preserved features
enabled and all the other features are zeroed. This is ok since the
restored domain is made immutable and can only be freed. A kunit test is
added to verify that the IOMMU domain free can be done with trimmed
features.
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/generic_pt/iommu_pt.h | 152 ++++++++++++++++++++++
drivers/iommu/generic_pt/kunit_iommu_pt.h | 30 +++++
include/linux/generic_pt/iommu.h | 30 +++++
3 files changed, 212 insertions(+)
diff --git a/drivers/iommu/generic_pt/iommu_pt.h b/drivers/iommu/generic_pt/iommu_pt.h
index 07ec2b3ab986..2945ddb771d1 100644
--- a/drivers/iommu/generic_pt/iommu_pt.h
+++ b/drivers/iommu/generic_pt/iommu_pt.h
@@ -16,6 +16,7 @@
#include "../iommu-pages.h"
#include <linux/cleanup.h>
#include <linux/dma-mapping.h>
+#include <linux/iommu-liveupdate.h>
enum {
SW_BIT_CACHE_FLUSH_DONE = 0,
@@ -1005,6 +1006,152 @@ static int NS(map_range)(struct pt_iommu *iommu_table, dma_addr_t iova,
return ret;
}
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+static void NS(unpreserve)(struct pt_iommu *iommu_table, struct iommu_domain_ser *ser)
+{
+ struct pt_common *common = common_from_iommu(iommu_table);
+ struct pt_range range = pt_all_range(common);
+ struct pt_iommu_collect_args collect = {
+ .pending.free_list = IOMMU_PAGES_LIST_INIT(
+ collect.pending.free_list),
+ };
+
+ iommu_pages_list_add(&collect.pending.free_list, range.top_table);
+
+ /*
+ * pt_walk_range() will never fail as check_mapped is not set in the
+ * collect args.
+ */
+ pt_walk_range(&range, __collect_tables, &collect);
+
+ iommu_unpreserve_pages_list(&collect.pending.free_list);
+}
+
+static int NS(preserve)(struct pt_iommu *iommu_table, struct iommu_domain_ser *ser)
+{
+ struct pt_common *common = common_from_iommu(iommu_table);
+ struct pt_range range = pt_all_range(common);
+ struct pt_iommu_collect_args collect = {
+ .pending.free_list = IOMMU_PAGES_LIST_INIT(
+ collect.pending.free_list),
+ };
+ int ret;
+
+ iommu_pages_list_add(&collect.pending.free_list, range.top_table);
+
+ /*
+ * pt_walk_range() will never fail as check_mapped is not set in the
+ * collect args.
+ */
+ pt_walk_range(&range, __collect_tables, &collect);
+
+ ret = iommu_preserve_pages_list(&collect.pending.free_list);
+ if (ret)
+ return ret;
+
+ ser->top_table_phys = virt_to_phys(range.top_table);
+ ser->top_level = range.top_level;
+
+ /*
+ * VASZ and SIGN_EXTEND will be needed in next kernel for collector page
+ * table walk to restore and free pages.
+ *
+ * Use the max_vasz_lg2 from range, as that is the current one if
+ * DYNAMIC_TOP is supported.
+ */
+ ser->vasz = range.max_vasz_lg2;
+ ser->sign_extend = pt_feature(common, PT_FEAT_SIGN_EXTEND);
+
+ return 0;
+}
+
+static int __restore_tables(struct pt_range *range, void *arg,
+ unsigned int level, struct pt_table_p *table)
+{
+ struct pt_state pts = pt_init(range, level, table);
+ int ret;
+
+ for_each_pt_level_entry(&pts) {
+ if (pts.type == PT_ENTRY_TABLE) {
+ iommu_restore_pages(virt_to_phys(pts.table_lower));
+
+ /*
+ * pt_descend can only fail if pts.table_lower is not
+ * init. So the if statement below is dead code.
+ */
+ ret = pt_descend(&pts, arg, __restore_tables);
+ if (ret)
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+static void NS(deinit)(struct pt_iommu *iommu_table);
+static const struct pt_iommu_ops NS(ops_immutable) = {
+ .deinit = NS(deinit),
+};
+
+static int NS(restore)(struct pt_iommu *iommu_table, struct iommu_domain_ser *ser)
+{
+ struct pt_common *common = common_from_iommu(iommu_table);
+ struct pt_range range;
+
+ if (ser->vasz > common->max_vasz_lg2 ||
+ ser->top_level > PT_MAX_TOP_LEVEL)
+ return -EINVAL;
+
+ /*
+ * Override the max_vasz_lg2 here to the one calculated using top_range
+ * in previous kernel. This makes sure that any later page walks use the
+ * clamped max_vasz_lg2 instead of using the full hardware maximum
+ * max_vasz_lg2.
+ */
+ common->max_vasz_lg2 = ser->vasz;
+
+ /*
+ * Restored page tables are strictly transient and only permitted to be
+ * destroyed via deinit() op. Because we only preserve user-assigned
+ * devices utilizing pass-through frameworks (VFIO / IOMMUFD), any
+ * concurrent or subsequent map/unmap operations on a restored domain
+ * are explicitly blocked at the subsystem boundary (e.g., via IOAS
+ * immutability).
+ */
+ iommu_table->ops = &NS(ops_immutable);
+
+ /*
+ * It is safe to override this here since this domain is immutable and
+ * can only be freed. This also means that for non-coherent pages DMA
+ * mappings will not be created using the DMA API.
+ *
+ * Note that the FMT specific bits are not cleared as they might be
+ * set/preserved/restored by the driver to handle special cases that are
+ * required for page walks.
+ */
+ common->features &= ~GENMASK(PT_FEAT_FMT_START - 1, 0);
+ if (ser->sign_extend)
+ common->features |= BIT(PT_FEAT_SIGN_EXTEND);
+
+ range = pt_all_range(common);
+ iommu_restore_pages(ser->top_table_phys);
+
+ /*
+ * The old top_table can be freed here as it is not used for any IOMMU
+ * mappings yet, because the restore happens during device probe.
+ */
+ iommu_pages_free_incoherent(range.top_table,
+ iommu_table->iommu_device);
+
+ /* Set the restored top table */
+ pt_top_set(common, phys_to_virt(ser->top_table_phys), ser->top_level);
+
+ /* Restore all pages*/
+ range = pt_all_range(common);
+ return pt_walk_range(&range, __restore_tables, NULL);
+}
+#endif
+
struct pt_unmap_args {
struct iommupt_pending_gather pending;
pt_vaddr_t unmapped;
@@ -1188,6 +1335,11 @@ static const struct pt_iommu_ops NS(ops) = {
#endif
.get_info = NS(get_info),
.deinit = NS(deinit),
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ .preserve = NS(preserve),
+ .unpreserve = NS(unpreserve),
+ .restore = NS(restore),
+#endif
};
static int pt_init_common(struct pt_common *common)
diff --git a/drivers/iommu/generic_pt/kunit_iommu_pt.h b/drivers/iommu/generic_pt/kunit_iommu_pt.h
index ece1c9b8c55d..83bf18be8c1e 100644
--- a/drivers/iommu/generic_pt/kunit_iommu_pt.h
+++ b/drivers/iommu/generic_pt/kunit_iommu_pt.h
@@ -427,6 +427,35 @@ static void test_mixed(struct kunit *test)
check_iova(test, start, oa, len);
}
+static void test_restore_free(struct kunit *test)
+{
+ struct kunit_iommu_priv *priv = test->priv;
+ struct pt_range top_range = pt_top_range(priv->common);
+ u64 start = 0x3fe400ULL << 12;
+ u64 end = 0x4c0600ULL << 12;
+ pt_vaddr_t len = end - start;
+
+ if (top_range.last_va <= start || sizeof(unsigned long) == 4)
+ kunit_skip(test, "range is too small");
+ if ((priv->safe_pgsize_bitmap & GENMASK(30, 21)) != (BIT(30) | BIT(21)))
+ kunit_skip(test, "incompatible psize");
+
+ /* Map a large mixed range to populate multiple levels of page tables */
+ do_map(test, start, start, len);
+
+ /*
+ * Simulate a restored state by clearing all features except SIGN_EXTEND
+ * and DMA_INCOHERENT. This verifies that the generic page table free
+ * walker can correctly tear down a populated domain when other features
+ * are zeroed. Also set max_vasz_lg2 as done by the actual restore()
+ * function.
+ */
+ priv->common->features &= (BIT(PT_FEAT_SIGN_EXTEND) | BIT(PT_FEAT_DMA_INCOHERENT));
+ priv->common->max_vasz_lg2 = top_range.max_vasz_lg2;
+
+ /* The domain will be freed when the test exits. */
+}
+
static struct kunit_case iommu_test_cases[] = {
KUNIT_CASE_FMT(test_increase_level),
KUNIT_CASE_FMT(test_map_simple),
@@ -435,6 +464,7 @@ static struct kunit_case iommu_test_cases[] = {
KUNIT_CASE_FMT(test_random_map),
KUNIT_CASE_FMT(test_pgsize_boundary),
KUNIT_CASE_FMT(test_mixed),
+ KUNIT_CASE_FMT(test_restore_free),
{},
};
diff --git a/include/linux/generic_pt/iommu.h b/include/linux/generic_pt/iommu.h
index dd0edd02a48a..faa41d8032fe 100644
--- a/include/linux/generic_pt/iommu.h
+++ b/include/linux/generic_pt/iommu.h
@@ -13,6 +13,7 @@ struct iommu_iotlb_gather;
struct pt_iommu_ops;
struct pt_iommu_driver_ops;
struct iommu_dirty_bitmap;
+struct iommu_domain_ser;
/**
* DOC: IOMMU Radix Page Table
@@ -166,6 +167,35 @@ struct pt_iommu_ops {
* table from all HW access and all caches.
*/
void (*deinit)(struct pt_iommu *iommu_table);
+
+ /**
+ * @preserve: Preserve the iommu page table for liveupdate
+ * @iommu_table: Table to preserve
+ * @ser: Serialization struct to fill with preserved state
+ *
+ * Preserve iommu page table and the relevant state for liveupdate. The
+ * caller must make sure that the page table is not updated during and
+ * after preservation.
+ */
+ int (*preserve)(struct pt_iommu *iommu_table, struct iommu_domain_ser *ser);
+
+ /**
+ * @unpreserve: Unpreserve the iommu page table
+ * @iommu_table: Table to unpreserve
+ * @ser: Serialization struct that contains preserved state
+ */
+ void (*unpreserve)(struct pt_iommu *iommu_table, struct iommu_domain_ser *ser);
+
+ /**
+ * @restore: Restore the iommu page table after liveupdate
+ * @iommu_table: Table to restore the state into
+ * @ser: Serialization struct that contains preserved state
+ *
+ * The iommu_table is back filled with the restored state that was
+ * preserved in the serialization struct.
+ */
+ int (*restore)(struct pt_iommu *iommu_table, struct iommu_domain_ser *ser);
+
};
/**
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 05/18] iommu: Implement IOMMU domain preservation
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (3 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 04/18] iommupt: Implement preserve/unpreserve/restore callbacks Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 06/18] iommu: Implement device and IOMMU HW preservation Samiullah Khawaja
` (12 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Pranjal Shrivastava, Robin Murphy, Kevin Tian,
Alex Williamson, Shuah Khan, iommu, linux-kernel, kvm,
Pratyush Yadav, Pasha Tatashin, David Matlack, Andrew Morton,
Vipin Sharma
Add IOMMU domain ops that can be implemented by the IOMMU drivers if
they support IOMMU domain preservation across liveupdate. The new IOMMU
domain preserve, unpreserve and restore APIs call these ops to perform
respective live update operations.
Reviewed-by: Pranjal Shrivastava <praan@google.com>
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/liveupdate.c | 128 +++++++++++++++++++++++++++++++
include/linux/iommu-liveupdate.h | 11 +++
include/linux/iommu.h | 5 ++
3 files changed, 144 insertions(+)
diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c
index b644f4792532..be542efbb023 100644
--- a/drivers/iommu/liveupdate.c
+++ b/drivers/iommu/liveupdate.c
@@ -37,11 +37,15 @@
#define pr_fmt(fmt) "iommu: liveupdate: " fmt
#include <linux/errno.h>
+#include <linux/generic_pt/iommu.h>
#include <linux/iommu-liveupdate.h>
#include <linux/iommu.h>
#include <linux/kexec_handover.h>
#include <linux/liveupdate.h>
+#define iommu_max_objs_per_page(_array) \
+ ((PAGE_SIZE - sizeof(struct iommu_array_hdr_ser)) / sizeof((_array)->objects[0]))
+
struct iommu_flb_obj {
struct mutex lock;
struct iommu_flb_ser *ser;
@@ -251,3 +255,127 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)
liveupdate_unregister_flb(handler, &iommu_flb);
}
EXPORT_SYMBOL(iommu_liveupdate_unregister_flb);
+
+static int alloc_object_ser(void **curr_array_ptr, u64 max_objs)
+{
+ struct iommu_array_hdr_ser *curr_array = *curr_array_ptr;
+ struct iommu_array_hdr_ser *next_array;
+
+ /*
+ * The objects marked as deleted are not reused to avoid traversal of
+ * linked-list and arrays.
+ */
+ if (curr_array->nr_objects >= max_objs) {
+ next_array = kho_alloc_preserve(PAGE_SIZE);
+ if (IS_ERR(next_array))
+ return PTR_ERR(next_array);
+
+ curr_array->next_array_phys = virt_to_phys(next_array);
+ *curr_array_ptr = next_array;
+ curr_array = next_array;
+ }
+
+ return curr_array->nr_objects++;
+}
+
+static struct iommu_domain_ser *alloc_iommu_domain_ser(struct iommu_flb_obj *flb)
+{
+ int idx;
+
+ idx = alloc_object_ser((void **) &flb->curr_domain_array,
+ iommu_max_objs_per_page(flb->curr_domain_array));
+ if (idx < 0)
+ return ERR_PTR(idx);
+
+ flb->curr_domain_array->objects[idx].hdr.ref_count = 1;
+ return &flb->curr_domain_array->objects[idx];
+}
+
+/**
+ * iommu_preserve_domain() - Preserve an IOMMU domain across live update
+ * @domain: Domain to preserve
+ * @ser: Pointer to receive the virtual serialized domain state handle
+ *
+ * Return: 0 on success, or negative error code.
+ */
+int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser)
+{
+ struct pt_iommu *pt = iommupt_from_domain(domain);
+ struct iommu_domain_ser *domain_ser;
+ struct iommu_flb_obj *flb_obj;
+ int ret;
+
+ if (!pt || !pt->ops->preserve || !pt->ops->unpreserve)
+ return -EOPNOTSUPP;
+
+ ret = liveupdate_flb_get_outgoing(&iommu_flb, (void **)&flb_obj);
+ if (ret)
+ return ret;
+
+ mutex_lock(&flb_obj->lock);
+ if (domain->preserved_state) {
+ ret = -EBUSY;
+ goto out_unlock;
+ }
+
+ domain_ser = alloc_iommu_domain_ser(flb_obj);
+ if (IS_ERR(domain_ser)) {
+ ret = PTR_ERR(domain_ser);
+ goto out_unlock;
+ }
+
+ ret = pt->ops->preserve(pt, domain_ser);
+ if (ret) {
+ domain_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED;
+ goto out_unlock;
+ }
+
+ domain->preserved_state = domain_ser;
+ *ser = domain_ser;
+ ret = 0;
+out_unlock:
+ mutex_unlock(&flb_obj->lock);
+ liveupdate_flb_put_outgoing(&iommu_flb);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(iommu_preserve_domain);
+
+/**
+ * iommu_unpreserve_domain() - Unpreserve a preserved IOMMU domain
+ * @domain: Domain to unpreserve
+ */
+void iommu_unpreserve_domain(struct iommu_domain *domain)
+{
+ struct pt_iommu *pt = iommupt_from_domain(domain);
+ struct iommu_domain_ser *domain_ser;
+ struct iommu_flb_obj *flb_obj;
+ int ret;
+
+ if (WARN_ON(!pt || !pt->ops->unpreserve))
+ return;
+
+ ret = liveupdate_flb_get_outgoing(&iommu_flb, (void **)&flb_obj);
+ if (WARN_ON(ret))
+ return;
+
+ mutex_lock(&flb_obj->lock);
+ if (!domain->preserved_state)
+ goto out_unlock;
+
+ /*
+ * There is no check for attached devices here. The correctness relies
+ * on the Live Update Orchestrator's session lifecycle. All resources
+ * (iommufd, vfio devices) are preserved within a single session. If the
+ * session is torn down, the .unpreserve callbacks for all files will be
+ * invoked, ensuring a consistent cleanup without needing explicit
+ * refcounting for the serialized objects here.
+ */
+ domain_ser = domain->preserved_state;
+ pt->ops->unpreserve(pt, domain_ser);
+ domain_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED;
+ domain->preserved_state = NULL;
+out_unlock:
+ mutex_unlock(&flb_obj->lock);
+ liveupdate_flb_put_outgoing(&iommu_flb);
+}
+EXPORT_SYMBOL_GPL(iommu_unpreserve_domain);
diff --git a/include/linux/iommu-liveupdate.h b/include/linux/iommu-liveupdate.h
index 4755ab3cd67a..caa9778eee2d 100644
--- a/include/linux/iommu-liveupdate.h
+++ b/include/linux/iommu-liveupdate.h
@@ -15,6 +15,8 @@
#ifdef CONFIG_IOMMU_LIVEUPDATE
int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler);
void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler);
+int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser);
+void iommu_unpreserve_domain(struct iommu_domain *domain);
#else
static inline int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler)
{
@@ -24,5 +26,14 @@ static inline int iommu_liveupdate_register_flb(struct liveupdate_file_handler *
static inline void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)
{
}
+
+static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline void iommu_unpreserve_domain(struct iommu_domain *domain)
+{
+}
#endif
#endif /* _LINUX_IOMMU_LIVEUPDATE_H */
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index ac43b8b93f14..26de40d5a98e 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -14,6 +14,7 @@
#include <linux/err.h>
#include <linux/of.h>
#include <linux/iova_bitmap.h>
+#include <linux/kho/abi/iommu.h>
#include <uapi/linux/iommufd.h>
#define IOMMU_READ (1 << 0)
@@ -249,6 +250,10 @@ struct iommu_domain {
struct list_head next;
};
};
+
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ struct iommu_domain_ser *preserved_state;
+#endif
};
static inline bool iommu_is_dma_domain(struct iommu_domain *domain)
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 06/18] iommu: Implement device and IOMMU HW preservation
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (4 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 05/18] iommu: Implement IOMMU domain preservation Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops Samiullah Khawaja
` (11 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pratyush Yadav,
Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
Add IOMMU ops to preserve/unpreserve the device state handled by the
IOMMU driver. This is IOMMU specific state of the device that needs to
be preserved for liveupdate. These ops can be implemented by the IOMMU
drivers that support preservation of devices that have their IOMMU
domains preserved. During device preservation the state of the
associated IOMMU is also preserved as dependency.
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/iommu-priv.h | 2 +
drivers/iommu/iommu.c | 62 ++++++++-
drivers/iommu/liveupdate.c | 212 +++++++++++++++++++++++++++++++
include/linux/iommu-liveupdate.h | 56 ++++++++
include/linux/iommu.h | 42 ++++++
5 files changed, 373 insertions(+), 1 deletion(-)
diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h
index aaffad5854fc..62612b1fc890 100644
--- a/drivers/iommu/iommu-priv.h
+++ b/drivers/iommu/iommu-priv.h
@@ -21,6 +21,8 @@ static inline const struct iommu_ops *dev_iommu_ops(struct device *dev)
void dev_iommu_free(struct device *dev);
+bool iommu_group_is_singleton(struct iommu_group *group);
+
const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode);
static inline const struct iommu_ops *iommu_fwspec_ops(struct iommu_fwspec *fwspec)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index cd1bca7ede9a..9c1ad8de3ad7 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -18,6 +18,7 @@
#include <linux/errno.h>
#include <linux/host1x_context_bus.h>
#include <linux/iommu.h>
+#include <linux/iommu-liveupdate.h>
#include <linux/iommufd.h>
#include <linux/idr.h>
#include <linux/err.h>
@@ -328,6 +329,37 @@ void iommu_device_unregister(struct iommu_device *iommu)
}
EXPORT_SYMBOL_GPL(iommu_device_unregister);
+static int _iommu_for_each_dev_cb(struct device *dev, void *data)
+{
+ struct iommu_dev_iter *iter = data;
+
+ if (dev->iommu && dev->iommu->iommu_dev == iter->iommu)
+ return iter->fn(dev, iter->iommu, iter->arg);
+
+ return 0;
+}
+
+/**
+ * iommu_for_each_dev() - Iterate over all devices attached to an IOMMU
+ * @iter: Device iterator context
+ *
+ * Return: 0 on success, or negative error code.
+ */
+int iommu_for_each_dev(struct iommu_dev_iter *iter)
+{
+ int ret;
+
+ for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {
+ ret = bus_for_each_dev(iommu_buses[i], NULL, iter,
+ _iommu_for_each_dev_cb);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(iommu_for_each_dev);
+
#if IS_ENABLED(CONFIG_IOMMUFD_TEST)
void iommu_device_unregister_bus(struct iommu_device *iommu,
const struct bus_type *bus,
@@ -626,8 +658,8 @@ DEFINE_MUTEX(iommu_probe_device_lock);
static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
{
+ struct group_device *gdev, *gdev2;
struct iommu_group *group;
- struct group_device *gdev;
int ret;
/*
@@ -661,6 +693,13 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
goto err_put_group;
}
+ for_each_group_device(group, gdev2) {
+ if (dev_iommu_preserved_state(gdev2->dev)) {
+ ret = -EBUSY;
+ goto err_free_gdev;
+ }
+ }
+
/*
* The gdev must be in the list before calling
* iommu_setup_default_domain()
@@ -697,6 +736,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
err_remove_gdev:
list_del(&gdev->list);
+err_free_gdev:
__iommu_group_free_device(group, gdev);
err_put_group:
iommu_deinit_device(dev);
@@ -3543,6 +3583,26 @@ bool iommu_group_dma_owner_claimed(struct iommu_group *group)
}
EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed);
+/**
+ * iommu_group_is_singleton() - Query if group contains exactly one device
+ * @group: The group.
+ *
+ * Return: true if group contains exactly 1 device, false otherwise.
+ */
+bool iommu_group_is_singleton(struct iommu_group *group)
+{
+ bool ret;
+
+ if (!group)
+ return false;
+
+ mutex_lock(&group->mutex);
+ ret = (list_count_nodes(&group->devices) == 1);
+ mutex_unlock(&group->mutex);
+
+ return ret;
+}
+
static void iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid,
struct iommu_domain *domain)
{
diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c
index be542efbb023..5f92a023ea65 100644
--- a/drivers/iommu/liveupdate.c
+++ b/drivers/iommu/liveupdate.c
@@ -42,6 +42,9 @@
#include <linux/iommu.h>
#include <linux/kexec_handover.h>
#include <linux/liveupdate.h>
+#include <linux/pci.h>
+
+#include "iommu-priv.h"
#define iommu_max_objs_per_page(_array) \
((PAGE_SIZE - sizeof(struct iommu_array_hdr_ser)) / sizeof((_array)->objects[0]))
@@ -379,3 +382,212 @@ void iommu_unpreserve_domain(struct iommu_domain *domain)
liveupdate_flb_put_outgoing(&iommu_flb);
}
EXPORT_SYMBOL_GPL(iommu_unpreserve_domain);
+
+static struct iommu_hw_ser *alloc_iommu_hw_ser(struct iommu_flb_obj *flb)
+{
+ int idx;
+
+ idx = alloc_object_ser((void **)&flb->curr_iommu_array,
+ iommu_max_objs_per_page(flb->curr_iommu_array));
+ if (idx < 0)
+ return ERR_PTR(idx);
+
+ flb->curr_iommu_array->objects[idx].hdr.ref_count = 1;
+ return &flb->curr_iommu_array->objects[idx];
+}
+
+static int iommu_preserve_locked(struct iommu_device *iommu,
+ struct iommu_flb_obj *flb_obj)
+{
+ struct iommu_hw_ser *iommu_hw_ser;
+ int ret;
+
+ if (!iommu->ops->preserve || !iommu->ops->unpreserve)
+ return -EOPNOTSUPP;
+
+ lockdep_assert_held(&flb_obj->lock);
+ if (iommu->outgoing_preserved_state) {
+ iommu->outgoing_preserved_state->hdr.ref_count++;
+ return 0;
+ }
+
+ iommu_hw_ser = alloc_iommu_hw_ser(flb_obj);
+ if (IS_ERR(iommu_hw_ser))
+ return PTR_ERR(iommu_hw_ser);
+
+ ret = iommu->ops->preserve(iommu, iommu_hw_ser);
+ if (ret) {
+ iommu_hw_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED;
+ return ret;
+ }
+
+ iommu->outgoing_preserved_state = iommu_hw_ser;
+ return ret;
+}
+
+static void iommu_unpreserve_locked(struct iommu_device *iommu,
+ struct iommu_flb_obj *flb_obj)
+{
+ struct iommu_hw_ser *iommu_hw_ser = iommu->outgoing_preserved_state;
+
+ lockdep_assert_held(&flb_obj->lock);
+ if (WARN_ON(!iommu_hw_ser))
+ return;
+
+ if (WARN_ON_ONCE(!iommu->ops->preserve ||
+ !iommu->ops->unpreserve))
+ return;
+
+ iommu_hw_ser->hdr.ref_count--;
+ if (iommu_hw_ser->hdr.ref_count)
+ return;
+
+ iommu->outgoing_preserved_state = NULL;
+ iommu->ops->unpreserve(iommu, iommu_hw_ser);
+ iommu_hw_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED;
+}
+
+static struct iommu_device_ser *alloc_iommu_device_ser(struct iommu_flb_obj *flb)
+{
+ int idx;
+
+ idx = alloc_object_ser((void **)&flb->curr_device_array,
+ iommu_max_objs_per_page(flb->curr_device_array));
+ if (idx < 0)
+ return ERR_PTR(idx);
+
+ flb->curr_device_array->objects[idx].hdr.ref_count = 1;
+ return &flb->curr_device_array->objects[idx];
+}
+
+/**
+ * iommu_preserve_device() - Preserve device state across live update
+ * @domain: Associated IOMMU domain
+ * @dev: Device to preserve
+ * @dma_owner_token: Token to identify DMA owner of this device
+ *
+ * Return: 0 on success, or negative error code.
+ */
+int iommu_preserve_device(struct iommu_domain *domain,
+ struct device *dev, u64 dma_owner_token)
+{
+ struct iommu_device_ser *device_ser;
+ struct iommu_flb_obj *flb_obj;
+ struct dev_iommu *iommu;
+ struct pci_dev *pdev;
+ int ret;
+
+ if (!dev_is_pci(dev))
+ return -EOPNOTSUPP;
+
+ if (!iommu_group_dma_owner_claimed(dev->iommu_group))
+ return -EINVAL;
+
+ pdev = to_pci_dev(dev);
+ iommu = dev->iommu;
+ if (!iommu->iommu_dev->ops->preserve_device ||
+ !iommu->iommu_dev->ops->unpreserve_device ||
+ !iommu->iommu_dev->ops->preserve ||
+ !iommu->iommu_dev->ops->unpreserve)
+ return -EOPNOTSUPP;
+
+ ret = liveupdate_flb_get_outgoing(&iommu_flb, (void **)&flb_obj);
+ if (ret)
+ return ret;
+
+ mutex_lock(&flb_obj->lock);
+ if (!domain->preserved_state) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
+
+ if (dev_iommu_preserved_state(dev)) {
+ ret = -EBUSY;
+ goto out_unlock;
+ }
+
+ device_ser = alloc_iommu_device_ser(flb_obj);
+ if (IS_ERR(device_ser)) {
+ ret = PTR_ERR(device_ser);
+ goto out_unlock;
+ }
+
+ ret = iommu_preserve_locked(iommu->iommu_dev, flb_obj);
+ if (ret) {
+ device_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED;
+ goto out_unlock;
+ }
+
+ device_ser->domain_iommu_ser.domain_phys = virt_to_phys(domain->preserved_state);
+ device_ser->domain_iommu_ser.iommu_phys = virt_to_phys(iommu->iommu_dev->outgoing_preserved_state);
+ device_ser->devid = pci_dev_id(pdev);
+ device_ser->pci_domain_nr = pci_domain_nr(pdev->bus);
+ device_ser->dma_owner_token = dma_owner_token;
+
+ ret = iommu->iommu_dev->ops->preserve_device(dev, device_ser);
+ if (!ret) {
+ WRITE_ONCE(dev->iommu->device_ser, device_ser);
+
+ /* Validate that no sibling device was added concurrently */
+ if (!iommu_group_is_singleton(dev->iommu_group)) {
+ iommu->iommu_dev->ops->unpreserve_device(dev, device_ser);
+ WRITE_ONCE(dev->iommu->device_ser, NULL);
+ ret = -EOPNOTSUPP;
+ }
+ }
+
+ if (ret) {
+ device_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED;
+ iommu_unpreserve_locked(iommu->iommu_dev, flb_obj);
+ goto out_unlock;
+ }
+
+out_unlock:
+ mutex_unlock(&flb_obj->lock);
+ liveupdate_flb_put_outgoing(&iommu_flb);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(iommu_preserve_device);
+
+/**
+ * iommu_unpreserve_device() - Unpreserve device state
+ * @domain: Associated IOMMU domain
+ * @dev: Device to unpreserve
+ */
+void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev)
+{
+ struct iommu_device_ser *iommu_device_ser;
+ struct iommu_flb_obj *flb_obj;
+ struct dev_iommu *iommu;
+ int ret;
+
+ if (!dev_is_pci(dev))
+ return;
+
+ if (!iommu_group_dma_owner_claimed(dev->iommu_group))
+ return;
+
+ iommu = dev->iommu;
+ if (WARN_ON(!iommu->iommu_dev->ops->unpreserve_device ||
+ !iommu->iommu_dev->ops->unpreserve))
+ return;
+
+ ret = liveupdate_flb_get_outgoing(&iommu_flb, (void **)&flb_obj);
+ if (WARN_ON(ret))
+ return;
+
+ mutex_lock(&flb_obj->lock);
+ iommu_device_ser = dev_iommu_preserved_state(dev);
+ if (WARN_ON(!iommu_device_ser))
+ goto out_unlock;
+
+ iommu_device_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED;
+ iommu->iommu_dev->ops->unpreserve_device(dev, iommu_device_ser);
+ WRITE_ONCE(dev->iommu->device_ser, NULL);
+
+ iommu_unpreserve_locked(iommu->iommu_dev, flb_obj);
+out_unlock:
+ mutex_unlock(&flb_obj->lock);
+ liveupdate_flb_put_outgoing(&iommu_flb);
+}
+EXPORT_SYMBOL_GPL(iommu_unpreserve_device);
diff --git a/include/linux/iommu-liveupdate.h b/include/linux/iommu-liveupdate.h
index caa9778eee2d..536bea09d064 100644
--- a/include/linux/iommu-liveupdate.h
+++ b/include/linux/iommu-liveupdate.h
@@ -8,6 +8,7 @@
#ifndef _LINUX_IOMMU_LIVEUPDATE_H
#define _LINUX_IOMMU_LIVEUPDATE_H
+#include <linux/device.h>
#include <linux/iommu.h>
#include <linux/liveupdate.h>
#include <linux/kho/abi/iommu.h>
@@ -15,8 +16,43 @@
#ifdef CONFIG_IOMMU_LIVEUPDATE
int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler);
void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler);
+
+/**
+ * dev_iommu_preserved_state() - Get preserved state of a device
+ * @dev: Target device
+ *
+ * Return: Pointer to preserved device state, or NULL if not preserved.
+ */
+static inline void *dev_iommu_preserved_state(struct device *dev)
+{
+ struct iommu_device_ser *ser;
+
+ if (!dev->iommu)
+ return NULL;
+
+ ser = READ_ONCE(dev->iommu->device_ser);
+ if (ser && !(ser->hdr.flags & IOMMU_SER_FLAG_INCOMING))
+ return ser;
+
+ return NULL;
+}
+
int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser);
void iommu_unpreserve_domain(struct iommu_domain *domain);
+int iommu_preserve_device(struct iommu_domain *domain,
+ struct device *dev, u64 dma_owner_token);
+void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev);
+
+/**
+ * iommu_preserved_state() - Get preserved state of an IOMMU instance
+ * @iommu: IOMMU hardware instance
+ *
+ * Return: Pointer to preserved state, or NULL if not preserved.
+ */
+static inline void *iommu_preserved_state(struct iommu_device *iommu)
+{
+ return iommu->outgoing_preserved_state;
+}
#else
static inline int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler)
{
@@ -27,6 +63,11 @@ static inline void iommu_liveupdate_unregister_flb(struct liveupdate_file_handle
{
}
+static inline void *dev_iommu_preserved_state(struct device *dev)
+{
+ return NULL;
+}
+
static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser)
{
return -EOPNOTSUPP;
@@ -35,5 +76,20 @@ static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iomm
static inline void iommu_unpreserve_domain(struct iommu_domain *domain)
{
}
+
+static inline int iommu_preserve_device(struct iommu_domain *domain,
+ struct device *dev, u64 dma_owner_token)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev)
+{
+}
+
+static inline void *iommu_preserved_state(struct iommu_device *iommu)
+{
+ return NULL;
+}
#endif
#endif /* _LINUX_IOMMU_LIVEUPDATE_H */
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 26de40d5a98e..920abd777879 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -684,6 +684,10 @@ __iommu_copy_struct_to_user(const struct iommu_user_data *dst_data,
* resources shared/passed to user space IOMMU instance. Associate
* it with a nesting @parent_domain. It is required for driver to
* set @viommu->ops pointing to its own viommu_ops
+ * @preserve_device: Preserve state of a device for liveupdate.
+ * @unpreserve_device: Unpreserve state that was preserved earlier.
+ * @preserve: Preserve state of iommu translation hardware for liveupdate.
+ * @unpreserve: Unpreserve state of iommu that was preserved earlier.
* @owner: Driver module providing these ops
* @identity_domain: An always available, always attachable identity
* translation.
@@ -740,6 +744,13 @@ struct iommu_ops {
struct iommu_domain *parent_domain,
const struct iommu_user_data *user_data);
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ int (*preserve_device)(struct device *dev, struct iommu_device_ser *device_ser);
+ void (*unpreserve_device)(struct device *dev, struct iommu_device_ser *device_ser);
+ int (*preserve)(struct iommu_device *iommu, struct iommu_hw_ser *iommu_ser);
+ void (*unpreserve)(struct iommu_device *iommu, struct iommu_hw_ser *iommu_ser);
+#endif
+
const struct iommu_domain_ops *default_domain_ops;
struct module *owner;
struct iommu_domain *identity_domain;
@@ -827,6 +838,8 @@ struct iommu_domain_ops {
* @singleton_group: Used internally for drivers that have only one group
* @max_pasids: number of supported PASIDs
* @ready: set once iommu_device_register() has completed successfully
+ * @outgoing_preserved_state: preserved iommu state of outgoing kernel for
+ * liveupdate.
*/
struct iommu_device {
struct list_head list;
@@ -836,6 +849,10 @@ struct iommu_device {
struct iommu_group *singleton_group;
u32 max_pasids;
bool ready;
+
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ struct iommu_hw_ser *outgoing_preserved_state;
+#endif
};
/**
@@ -890,6 +907,9 @@ struct dev_iommu {
u32 pci_32bit_workaround:1;
u32 require_direct:1;
u32 shadow_on_flush:1;
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ struct iommu_device_ser *device_ser;
+#endif
};
int iommu_device_register(struct iommu_device *iommu,
@@ -1206,6 +1226,28 @@ static inline void *dev_iommu_priv_get(struct device *dev)
void dev_iommu_priv_set(struct device *dev, void *priv);
+/**
+ * typedef iommu_dev_iter_fn - Callback for iterating IOMMU attached devices
+ * @dev: Attached device
+ * @iommu: IOMMU instance
+ * @arg: Private argument passed to iterator
+ *
+ * Return: 0 on success, or negative error code.
+ */
+typedef int (*iommu_dev_iter_fn)(struct device *dev,
+ struct iommu_device *iommu, void *arg);
+
+/**
+ * struct iommu_dev_iter - Iterator for devices attached to an IOMMU
+ */
+struct iommu_dev_iter {
+ struct iommu_device *iommu;
+ iommu_dev_iter_fn fn;
+ void *arg;
+};
+
+int iommu_for_each_dev(struct iommu_dev_iter *iter);
+
extern struct mutex iommu_probe_device_lock;
int iommu_probe_device(struct device *dev);
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (5 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 06/18] iommu: Implement device and IOMMU HW preservation Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 08/18] iommu/vt-d: Clear unpreserved context entries during shutdown Samiullah Khawaja
` (10 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pratyush Yadav,
Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
Add implementation of the device and iommu preservation in a separate
file. Also set the device and iommu preserve/unpreserve ops in the
struct iommu_ops.
Preservation is refused for devices where ATS is supported. Carrying an
active ATS configuration across kexec would require adopting the ATS
state in the PCI core and in the context entries, and handling the
disable-to-enable, enable-to-disable and STU mismatch cases for a live
device.
Note that the preserved iommu root table, context table and device pasid
table will need cleanup as devices do not go through the release flow
during kexec. So the stale entries need to be removed during shutdown.
This is done in the next commit.
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
MAINTAINERS | 8 ++
drivers/iommu/intel/Makefile | 1 +
drivers/iommu/intel/iommu.c | 9 +-
drivers/iommu/intel/iommu.h | 13 ++
drivers/iommu/intel/liveupdate.c | 232 +++++++++++++++++++++++++++++++
include/linux/kho/abi/iommu.h | 24 ++++
6 files changed, 285 insertions(+), 2 deletions(-)
create mode 100644 drivers/iommu/intel/liveupdate.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 2114b50412ee..ae6df95dc398 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13220,6 +13220,14 @@ S: Supported
T: git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
F: drivers/iommu/intel/
+INTEL IOMMU LIVEUPDATE (VT-d)
+M: Samiullah Khawaja <skhawaja@google.com>
+M: Lu Baolu <baolu.lu@linux.intel.com>
+L: iommu@lists.linux.dev
+S: Maintained
+T: git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
+F: drivers/iommu/intel/liveupdate.c
+
INTEL IPU3 CSI-2 CIO2 DRIVER
M: Yong Zhi <yong.zhi@intel.com>
M: Sakari Ailus <sakari.ailus@linux.intel.com>
diff --git a/drivers/iommu/intel/Makefile b/drivers/iommu/intel/Makefile
index ada651c4a01b..d38fc101bc35 100644
--- a/drivers/iommu/intel/Makefile
+++ b/drivers/iommu/intel/Makefile
@@ -6,3 +6,4 @@ obj-$(CONFIG_INTEL_IOMMU_DEBUGFS) += debugfs.o
obj-$(CONFIG_INTEL_IOMMU_SVM) += svm.o
obj-$(CONFIG_IRQ_REMAP) += irq_remapping.o
obj-$(CONFIG_INTEL_IOMMU_PERF_EVENTS) += perfmon.o
+obj-$(CONFIG_IOMMU_LIVEUPDATE) += liveupdate.o
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 2e3b3ab216f8..4bfc2f173010 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -16,6 +16,7 @@
#include <linux/crash_dump.h>
#include <linux/dma-direct.h>
#include <linux/dmi.h>
+#include <linux/iommu-liveupdate.h>
#include <linux/memory.h>
#include <linux/pci.h>
#include <linux/pci-ats.h>
@@ -58,8 +59,6 @@ static int rwbf_quirk;
*/
int intel_iommu_tboot_noforce;
-#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
-
/*
* Take a root_entry and return the Lower Context Table Pointer (LCTP)
* if marked present.
@@ -3961,6 +3960,12 @@ const struct iommu_ops intel_iommu_ops = {
.is_attach_deferred = intel_iommu_is_attach_deferred,
.def_domain_type = device_def_domain_type,
.page_response = intel_iommu_page_response,
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ .preserve_device = intel_iommu_preserve_device,
+ .unpreserve_device = intel_iommu_unpreserve_device,
+ .preserve = intel_iommu_preserve,
+ .unpreserve = intel_iommu_unpreserve,
+#endif
};
static void quirk_iommu_igfx(struct pci_dev *dev)
diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
index 23dbe6c24439..785057236e7c 100644
--- a/drivers/iommu/intel/iommu.h
+++ b/drivers/iommu/intel/iommu.h
@@ -552,6 +552,8 @@ struct root_entry {
u64 hi;
};
+#define ROOT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(struct root_entry))
+
/*
* low 64 bits:
* 0: present
@@ -1296,6 +1298,17 @@ static inline int iopf_for_domain_replace(struct iommu_domain *new,
return 0;
}
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+int intel_iommu_preserve_device(struct device *dev,
+ struct iommu_device_ser *device_ser);
+void intel_iommu_unpreserve_device(struct device *dev,
+ struct iommu_device_ser *device_ser);
+int intel_iommu_preserve(struct iommu_device *iommu,
+ struct iommu_hw_ser *iommu_ser);
+void intel_iommu_unpreserve(struct iommu_device *iommu,
+ struct iommu_hw_ser *iommu_ser);
+#endif
+
#ifdef CONFIG_INTEL_IOMMU_SVM
void intel_svm_check(struct intel_iommu *iommu);
struct iommu_domain *intel_svm_domain_alloc(struct device *dev,
diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupdate.c
new file mode 100644
index 000000000000..0837bb889fed
--- /dev/null
+++ b/drivers/iommu/intel/liveupdate.c
@@ -0,0 +1,232 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2026, Google LLC
+ * Author: Samiullah Khawaja <skhawaja@google.com>
+ */
+
+#define pr_fmt(fmt) "DMAR: liveupdate: " fmt
+
+#include <linux/kexec_handover.h>
+#include <linux/liveupdate.h>
+#include <linux/iommu-liveupdate.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+
+#include "iommu.h"
+#include "../iommu-pages.h"
+
+/* 2 tables per bus in scalable mode with upper table at odd bit */
+#define CONTEXT_TABLE_PRESERVED_BIT(bus, devfn) (((bus) << 1) + ((devfn) >> 7))
+static bool is_context_table_preserved(struct intel_iommu *iommu,
+ struct iommu_hw_ser *ser,
+ u8 bus, u8 devfn)
+{
+ return test_bit(CONTEXT_TABLE_PRESERVED_BIT(bus, devfn),
+ (unsigned long *)&ser->intel.context_tables_bitmap[0]);
+}
+
+static void unpreserve_context_table(struct intel_iommu *iommu,
+ struct iommu_hw_ser *ser,
+ u8 bus, u8 devfn)
+{
+ struct context_entry *context;
+
+ /*
+ * In the Intel IOMMU driver, context tables are never freed once they
+ * are allocated during runtime, as they can be shared across multiple
+ * devices. So taking the iommu lock here to protect against concurrent
+ * allocations inside iommu_context_addr() should be enough. Once the
+ * address is read, it is safe to use it without holding the lock.
+ */
+ spin_lock(&iommu->lock);
+ context = iommu_context_addr(iommu, bus, devfn, 0);
+ spin_unlock(&iommu->lock);
+ if (context && is_context_table_preserved(iommu, ser, bus, devfn)) {
+ iommu_unpreserve_pages(context);
+ clear_bit(CONTEXT_TABLE_PRESERVED_BIT(bus, devfn),
+ (unsigned long *)&ser->intel.context_tables_bitmap[0]);
+ }
+}
+
+static int preserve_context_table(struct intel_iommu *iommu,
+ struct iommu_hw_ser *ser,
+ u8 bus, u8 devfn)
+{
+ struct context_entry *context;
+ int ret;
+
+ spin_lock(&iommu->lock);
+ context = iommu_context_addr(iommu, bus, devfn, 0);
+ spin_unlock(&iommu->lock);
+
+ /*
+ * Intel IOMMU context tables are never freed by the driver once
+ * allocated. It is safe to access the context pointer outside of the
+ * iommu->lock.
+ */
+ if (context && !is_context_table_preserved(iommu, ser, bus, devfn)) {
+ ret = iommu_preserve_pages(context);
+ if (ret)
+ return ret;
+
+ set_bit(CONTEXT_TABLE_PRESERVED_BIT(bus, devfn),
+ (unsigned long *)&ser->intel.context_tables_bitmap[0]);
+ }
+
+ return 0;
+}
+
+static void unpreserve_iommu_context_tables(struct intel_iommu *iommu,
+ struct iommu_hw_ser *ser)
+{
+ int i;
+
+ for (i = 0; i < ROOT_ENTRY_NR; i++) {
+ unpreserve_context_table(iommu, ser, i, 0);
+
+ if (!sm_supported(iommu))
+ continue;
+
+ unpreserve_context_table(iommu, ser, i, 0x80);
+ }
+}
+
+static int preserve_iommu_context_tables(struct device_domain_info *info)
+{
+ struct iommu_hw_ser *iommu_ser;
+ struct intel_iommu *iommu;
+ int ret;
+ int i;
+
+ /* IOMMU for this device should already preserved.*/
+ iommu = info->iommu;
+ iommu_ser = iommu_preserved_state(&iommu->iommu);
+ if (!iommu_ser)
+ return -EINVAL;
+
+ /*
+ * We could do preservation of context tables only for the bus of this
+ * device, but these devices can have PCI aliases, so context tables for
+ * those will also require preservation. Also unpreserve would require
+ * some kind of refcounting where the context table will only be
+ * unpreserved when the last device associated with it is unpreserved.
+ *
+ * This introduces unnecessary complication with minimum benefits as the
+ * unpreserved context tables will probably be recreated by the next
+ * kernel as these are all active devices. We follow simpler approach by
+ * just preserving the currently active context tables.
+ */
+ for (i = 0; i < ROOT_ENTRY_NR; i++) {
+ ret = preserve_context_table(iommu, iommu_ser, i, 0);
+ if (ret)
+ return ret;
+
+ if (!sm_supported(iommu))
+ continue;
+
+ ret = preserve_context_table(iommu, iommu_ser, i, 0x80);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+/**
+ * intel_iommu_preserve_device() - Intel IOMMU callback to preserve device state
+ * @dev: Target device
+ * @device_ser: Struct to populate with serialized device state
+ *
+ * Return: 0 on success, or negative error code.
+ */
+int intel_iommu_preserve_device(struct device *dev,
+ struct iommu_device_ser *device_ser)
+{
+ struct device_domain_info *info = dev_iommu_priv_get(dev);
+ int ret;
+
+ if (!dev_is_pci(dev)) {
+ dev_err(dev, "Cannot preserve non-PCI device\n");
+ return -EOPNOTSUPP;
+ }
+
+ if (dev_is_real_dma_subdevice(dev))
+ return -EOPNOTSUPP;
+
+ if (!info || !info->domain)
+ return -EINVAL;
+
+ if (info->ats_supported)
+ return -EOPNOTSUPP;
+
+ ret = preserve_iommu_context_tables(info);
+ if (ret)
+ return ret;
+
+ device_ser->domain_iommu_ser.attachment_id = domain_id_iommu(info->domain,
+ info->iommu);
+ return 0;
+}
+
+/**
+ * intel_iommu_unpreserve_device() - Intel IOMMU callback to unpreserve device state
+ * @dev: Target device
+ * @device_ser: Struct containing serialized device state
+ */
+void intel_iommu_unpreserve_device(struct device *dev,
+ struct iommu_device_ser *device_ser)
+{
+ /*
+ * 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.
+ */
+}
+
+/**
+ * intel_iommu_preserve() - Intel IOMMU callback to preserve hardware state
+ * @iommu_dev: Generic IOMMU device handle
+ * @ser: Struct to populate with serialized hardware state
+ *
+ * Return: 0 on success, or negative error code.
+ */
+int intel_iommu_preserve(struct iommu_device *iommu_dev,
+ struct iommu_hw_ser *ser)
+{
+ struct intel_iommu *iommu;
+ int ret;
+
+ iommu = container_of(iommu_dev, struct intel_iommu, iommu);
+
+ ret = iommu_preserve_pages(iommu->root_entry);
+ if (ret)
+ return ret;
+
+ ser->intel.phys_addr = iommu->reg_phys;
+ ser->intel.root_table = __pa(iommu->root_entry);
+ ser->type = IOMMU_INTEL;
+ ser->token = ser->intel.phys_addr;
+
+ return 0;
+}
+
+/**
+ * intel_iommu_unpreserve() - Intel IOMMU callback to unpreserve hardware state
+ * @iommu_dev: Generic IOMMU device handle
+ * @ser: Struct containing serialized hardware state
+ */
+void intel_iommu_unpreserve(struct iommu_device *iommu_dev,
+ struct iommu_hw_ser *ser)
+{
+ struct intel_iommu *iommu;
+
+ iommu = container_of(iommu_dev, struct intel_iommu, iommu);
+
+ unpreserve_iommu_context_tables(iommu, ser);
+ iommu_unpreserve_pages(iommu->root_entry);
+}
diff --git a/include/linux/kho/abi/iommu.h b/include/linux/kho/abi/iommu.h
index aa42085409e5..5aaa29da6832 100644
--- a/include/linux/kho/abi/iommu.h
+++ b/include/linux/kho/abi/iommu.h
@@ -81,6 +81,7 @@
*/
enum iommu_type_ser {
IOMMU_INVALID,
+ IOMMU_INTEL,
};
#define IOMMU_SER_FLAG_DELETED (1 << 0)
@@ -144,16 +145,39 @@ struct iommu_device_ser {
struct iommu_dev_map_ser domain_iommu_ser;
} __packed;
+/* There are maximum 256 buses, so maximum 512 context tables */
+#define VTD_PRESERVED_BITMAP_LONGS DIV_ROUND_UP(512, BITS_PER_LONG_LONG)
+
+/**
+ * struct iommu_intel_ser - Serialized state of an Intel IOMMU instance
+ * @restored: Whether IOMMU state is restored
+ * @phys_addr: Physical address of the IOMMU register base
+ * @root_table: Physical address of the root entry table
+ * @context_tables_bitmap: Bitmap representing the context tables that are
+ * preserved.
+ */
+struct iommu_intel_ser {
+ u8 restored;
+ u8 padding[7];
+ u64 phys_addr;
+ u64 root_table;
+ u64 context_tables_bitmap[VTD_PRESERVED_BITMAP_LONGS];
+};
+
/**
* struct iommu_hw_ser - Serialized state of an IOMMU instance
* @hdr: Common object header
* @token: Unique token for the IOMMU
* @type: IOMMU type serialized state belongs to
+ * @intel: Intel specific serialization data
*/
struct iommu_hw_ser {
struct iommu_hdr_ser hdr;
u64 token;
u64 type;
+ union {
+ struct iommu_intel_ser intel;
+ };
} __packed;
/**
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 08/18] iommu/vt-d: Clear unpreserved context entries during shutdown
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (6 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 09/18] iommu: Add APIs to get iommu and device preserved state Samiullah Khawaja
` (9 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pratyush Yadav,
Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
During normal shutdown the iommu translation is disabled. Since the root
table is preserved during live update, it needs to be cleaned up and the
context entries of the unpreserved devices and root entries for the
unpreserved context tables need to be cleared.
This is required because during kexec reboot and shutdown the devices do
not go through the release flow, so there might be stale entries in the
root table and context tables. Also note that new unpreserved context
tables for unpreserved devices might have been added after preservation,
so the root table entries for unpreserved context tables also need to
removed.
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/intel/iommu.c | 15 +++-
drivers/iommu/intel/iommu.h | 5 ++
drivers/iommu/intel/liveupdate.c | 139 +++++++++++++++++++++++++++++++
3 files changed, 157 insertions(+), 2 deletions(-)
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 4bfc2f173010..474c926172c5 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -1852,6 +1852,14 @@ static int iommu_suspend(void *data)
iommu_flush_all();
+ /*
+ * Note that IOMMU suspend doesn't affect live update. The state
+ * preserved during live update is not released and remains valid during
+ * suspend and reused during IOMMU resume.
+ *
+ * Also note deployment of suspend/resume and live updated use case
+ * should be mostly mutually exclusive.
+ */
for_each_active_iommu(iommu, drhd) {
iommu_disable_translation(iommu);
@@ -2397,8 +2405,11 @@ void intel_iommu_shutdown(void)
/* Disable PMRs explicitly here. */
iommu_disable_protect_mem_regions(iommu);
- /* Make sure the IOMMUs are switched off */
- iommu_disable_translation(iommu);
+ /* Make sure the IOMMUs are switched off if not preserved. */
+ if (iommu_preserved_state(&iommu->iommu))
+ clear_unpreserved_context_entries(iommu);
+ else
+ iommu_disable_translation(iommu);
}
}
diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
index 785057236e7c..4feb5bd76b18 100644
--- a/drivers/iommu/intel/iommu.h
+++ b/drivers/iommu/intel/iommu.h
@@ -1307,6 +1307,11 @@ int intel_iommu_preserve(struct iommu_device *iommu,
struct iommu_hw_ser *iommu_ser);
void intel_iommu_unpreserve(struct iommu_device *iommu,
struct iommu_hw_ser *iommu_ser);
+void clear_unpreserved_context_entries(struct intel_iommu *iommu);
+#else
+static inline void clear_unpreserved_context_entries(struct intel_iommu *iommu)
+{
+}
#endif
#ifdef CONFIG_INTEL_IOMMU_SVM
diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupdate.c
index 0837bb889fed..501dc0e9cc0a 100644
--- a/drivers/iommu/intel/liveupdate.c
+++ b/drivers/iommu/intel/liveupdate.c
@@ -77,6 +77,145 @@ static int preserve_context_table(struct intel_iommu *iommu,
return 0;
}
+static void clear_unpreserved_context_root_entries(struct intel_iommu *iommu,
+ struct iommu_hw_ser *ser)
+{
+ struct root_entry *root;
+ int i;
+
+ /*
+ * Individual invalidations for each context table removal are not
+ * needed as we issue global invalidations later.
+ */
+ for (i = 0; i < ROOT_ENTRY_NR; i++) {
+ root = &iommu->root_entry[i];
+
+ if (!is_context_table_preserved(iommu, ser, i, 0) && (root->lo & 1)) {
+ root->lo = 0;
+ __iommu_flush_cache(iommu,
+ &root->lo,
+ sizeof(root->lo));
+ }
+
+ if (!sm_supported(iommu))
+ continue;
+
+ if (!is_context_table_preserved(iommu, ser, i, 0x80) && (root->hi & 1)) {
+ root->hi = 0;
+ __iommu_flush_cache(iommu,
+ &root->hi,
+ sizeof(root->hi));
+ }
+ }
+}
+
+static void clear_unpreserved_context(struct device_domain_info *info, u8 bus, u8 devfn)
+{
+ struct context_entry *context;
+
+ /*
+ * This cleanup is done during shutdown, so it should be fine to only
+ * clear the entries here and issue one global invalidation later to
+ * invalidate all cleared entries.
+ *
+ * Note that the device IOTLB invalidation for unpreserved devices is
+ * skipped this way, but that should not be needed as the devices are
+ * quiesced at this point. This should improve the performance of the
+ * cleanup process and avoids any invalidation timeouts because drivers
+ * might have moved devices to D3 state.
+ *
+ * The iommu lock is not needed as the context tables are never removed
+ * and the new ones are not added during shutdown.
+ */
+ context = iommu_context_addr(info->iommu, bus, devfn, 0);
+ if (context) {
+ /*
+ * Individual invalidations are not needed as we issue global
+ * invalidations later once all the cleanups are done.
+ */
+ context_clear_present(context);
+ __iommu_flush_cache(info->iommu, context, sizeof(*context));
+ context_clear_entry(context);
+ __iommu_flush_cache(info->iommu, context, sizeof(*context));
+ }
+}
+
+static int clear_unpreserved_alias_cb(struct pci_dev *pdev, u16 alias, void *data)
+{
+ struct device_domain_info *info = data;
+
+ clear_unpreserved_context(info, PCI_BUS_NUM(alias), alias & 0xff);
+ return 0;
+}
+
+static int clear_unpreserve_context_entry_fn(struct device *dev,
+ struct iommu_device *iommu_dev,
+ void *arg)
+{
+ struct device_domain_info *info;
+
+ info = dev_iommu_priv_get(dev);
+ if (!info)
+ return 0;
+
+ if (dev_iommu_preserved_state(dev))
+ return 0;
+
+ if (dev_is_pci(dev))
+ pci_for_each_dma_alias(to_pci_dev(dev),
+ clear_unpreserved_alias_cb, info);
+ else
+ clear_unpreserved_context(info, info->bus, info->devfn);
+
+ return 0;
+}
+
+/**
+ * clear_unpreserved_context_entries() - Clear context entries for unpreserved devices
+ * @iommu: Target IOMMU
+ *
+ * Clear the context entries of unpreserved devices during shutdown before kexec.
+ */
+void clear_unpreserved_context_entries(struct intel_iommu *iommu)
+{
+ struct iommu_dev_iter iter = {
+ .fn = clear_unpreserve_context_entry_fn,
+ .iommu = &iommu->iommu,
+ .arg = NULL,
+
+ };
+
+ /*
+ * Clear context entries for unpreserved devices because during kexec
+ * reboot and shutdown the devices do not go through the release flow,
+ * so there might be stale entries in the root table and context tables.
+ *
+ * Note that the error can be ignored as the iterator function does not
+ * fail.
+ */
+ iommu_for_each_dev(&iter);
+
+ /*
+ * New unpreserved context tables for unpreserved devices might have
+ * been added after preservation, so the root table entries for
+ * unpreserved context tables need to be removed.
+ */
+ clear_unpreserved_context_root_entries(iommu,
+ iommu_preserved_state(&iommu->iommu));
+
+ /*
+ * Some devices might not have teardown/detached properly depending on
+ * whether a proper device remove is done before kexec is triggered.
+ * Also unpreserved context tables and entries are removed during
+ * shutdown. So issue global invalidations to remove references to
+ * unpreserved tables and entries.
+ */
+ iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
+ if (sm_supported(iommu))
+ qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0);
+ iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
+}
+
static void unpreserve_iommu_context_tables(struct intel_iommu *iommu,
struct iommu_hw_ser *ser)
{
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 09/18] iommu: Add APIs to get iommu and device preserved state
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (7 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 08/18] iommu/vt-d: Clear unpreserved context entries during shutdown Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 10/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids Samiullah Khawaja
` (8 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Pranjal Shrivastava, Robin Murphy, Kevin Tian,
Alex Williamson, Shuah Khan, iommu, linux-kernel, kvm,
Pratyush Yadav, Pasha Tatashin, David Matlack, Andrew Morton,
Vipin Sharma
The preserved state of the device and IOMMU needs to be fetched during
shutdown and boot in the next kernel. Add APIs that can be used to fetch
the preserved state of a device and IOMMU. The APIs will only be used
during shutdown and after liveupdate so no locking needed.
Reviewed-by: Pranjal Shrivastava <praan@google.com>
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/liveupdate.c | 125 +++++++++++++++++++++++++++++++
include/linux/iommu-liveupdate.h | 45 +++++++++++
2 files changed, 170 insertions(+)
diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c
index 5f92a023ea65..ff76a23d9583 100644
--- a/drivers/iommu/liveupdate.c
+++ b/drivers/iommu/liveupdate.c
@@ -49,6 +49,17 @@
#define iommu_max_objs_per_page(_array) \
((PAGE_SIZE - sizeof(struct iommu_array_hdr_ser)) / sizeof((_array)->objects[0]))
+#define iommu_liveupdate_for_each_obj(_arr, _obj, _idx) \
+ for ((_idx) = 0, (_obj) = (_arr)->objects; \
+ (_idx) < (_arr)->hdr.nr_objects; (_idx)++, (_obj)++) \
+ if (((_obj)->hdr.flags & IOMMU_SER_FLAG_DELETED)) \
+ continue; \
+ else
+
+#define iommu_liveupdate_for_each_arr(_arr) \
+ for (; (_arr); (_arr) = (_arr)->hdr.next_array_phys ? \
+ phys_to_virt((_arr)->hdr.next_array_phys) : NULL)
+
struct iommu_flb_obj {
struct mutex lock;
struct iommu_flb_ser *ser;
@@ -259,6 +270,120 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)
}
EXPORT_SYMBOL(iommu_liveupdate_unregister_flb);
+/*
+ * iommu_liveupdate_flb_get_incoming() - Helper function to get FLB state
+ * @flb_objp: Pointer to get the restored FLB object
+ *
+ * Return: 0 if FLB state found and restored, error if no data found
+ */
+static int iommu_liveupdate_flb_get_incoming(struct iommu_flb_obj **flb_objp)
+{
+ struct iommu_flb_obj *flb_obj;
+ int ret;
+
+ ret = liveupdate_flb_get_incoming(&iommu_flb, (void **)flb_objp);
+ if (ret == -ENODATA || ret == -ENOENT || ret == -EOPNOTSUPP)
+ return ret;
+
+ if (ret)
+ goto err_fatal;
+
+ flb_obj = *flb_objp;
+ mutex_lock(&flb_obj->lock);
+
+ /*
+ * FLB version mismatch is considered fatal for security reasons for
+ * now.
+ */
+ if (flb_obj->ser->version != IOMMU_LUO_FLB_VERSION)
+ goto err_fatal;
+
+ /*
+ * Array Phys of each type should be valid if the FLB was created for
+ * preservation. This is true even if no devices, iommus or domains were
+ * preserved.
+ */
+ if (!flb_obj->ser->iommu_array_phys ||
+ !flb_obj->ser->device_array_phys ||
+ !flb_obj->ser->iommu_domain_array_phys)
+ goto err_fatal;
+
+ mutex_unlock(&flb_obj->lock);
+ return 0;
+
+err_fatal:
+ panic("Failed to restore IOMMU Live Update FLB\n");
+}
+
+/**
+ * iommu_for_each_preserved_device() - Iterate on preserved devices
+ * @fn: Iterator function to call for each preserved device
+ * @arg: Argument to pass to iterator function
+ *
+ * Return: 0 on success, or an error.
+ */
+int iommu_for_each_preserved_device(iommu_preserved_device_iter_fn fn,
+ void *arg)
+{
+ struct iommu_device_array_ser *array;
+ struct iommu_device_ser *device_ser;
+ struct iommu_flb_obj *flb_obj;
+ int ret, idx;
+
+ ret = iommu_liveupdate_flb_get_incoming(&flb_obj);
+ if (ret)
+ return ret;
+
+ array = phys_to_virt(flb_obj->ser->device_array_phys);
+ iommu_liveupdate_for_each_arr(array) {
+ iommu_liveupdate_for_each_obj(array, device_ser, idx) {
+ ret = fn(device_ser, arg);
+ if (ret)
+ goto out;
+ }
+ }
+
+out:
+ liveupdate_flb_put_incoming(&iommu_flb);
+ return ret;
+}
+EXPORT_SYMBOL(iommu_for_each_preserved_device);
+
+/**
+ * iommu_get_preserved_data() - Get preserved data for an IOMMU HW
+ * @token: Token used to preserve this IOMMU HW
+ * @type: IOMMU type in preserved state
+ *
+ * Gets the preserved state of an IOMMU HW using token and the IOMMU type.
+ *
+ * Return: struct iommu_hw_ser on success, NULL if no preserved state found.
+ */
+struct iommu_hw_ser *iommu_get_preserved_data(u64 token, enum iommu_type_ser type)
+{
+ struct iommu_hw_ser *iommu_ser = NULL;
+ struct iommu_hw_array_ser *array;
+ struct iommu_flb_obj *flb_obj;
+ int ret, idx;
+
+ ret = iommu_liveupdate_flb_get_incoming(&flb_obj);
+ if (ret)
+ return NULL;
+
+ array = phys_to_virt(flb_obj->ser->iommu_array_phys);
+ iommu_liveupdate_for_each_arr(array) {
+ iommu_liveupdate_for_each_obj(array, iommu_ser, idx) {
+ if (iommu_ser->token == token && iommu_ser->type == type)
+ goto out;
+ }
+ }
+
+ iommu_ser = NULL;
+out:
+ liveupdate_flb_put_incoming(&iommu_flb);
+ return iommu_ser;
+}
+EXPORT_SYMBOL(iommu_get_preserved_data);
+
static int alloc_object_ser(void **curr_array_ptr, u64 max_objs)
{
struct iommu_array_hdr_ser *curr_array = *curr_array_ptr;
diff --git a/include/linux/iommu-liveupdate.h b/include/linux/iommu-liveupdate.h
index 536bea09d064..06f66cc8f526 100644
--- a/include/linux/iommu-liveupdate.h
+++ b/include/linux/iommu-liveupdate.h
@@ -13,6 +13,16 @@
#include <linux/liveupdate.h>
#include <linux/kho/abi/iommu.h>
+/**
+ * iommu_preserved_device_iter_fn - Callback for iterating preserved devices
+ * @ser: Pointer to serialized device state
+ * @arg: User-defined context argument
+ *
+ * Return: 0 to continue iteration, or a negative error code to abort.
+ */
+typedef int (*iommu_preserved_device_iter_fn)(struct iommu_device_ser *ser,
+ void *arg);
+
#ifdef CONFIG_IOMMU_LIVEUPDATE
int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler);
void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler);
@@ -37,6 +47,26 @@ static inline void *dev_iommu_preserved_state(struct device *dev)
return NULL;
}
+/**
+ * iommu_domain_restored_state() - Get restored state of an IOMMU domain
+ * @domain: Pointer to struct iommu_domain to get restored state of.
+ *
+ * Return: Pointer to restored state on success, NULL on error.
+ */
+static inline void *iommu_domain_restored_state(struct iommu_domain *domain)
+{
+ struct iommu_domain_ser *ser;
+
+ ser = domain->preserved_state;
+ if (ser && (ser->hdr.flags & IOMMU_SER_FLAG_INCOMING))
+ return ser;
+
+ return NULL;
+}
+
+int iommu_for_each_preserved_device(iommu_preserved_device_iter_fn fn,
+ void *arg);
+struct iommu_hw_ser *iommu_get_preserved_data(u64 token, enum iommu_type_ser type);
int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser);
void iommu_unpreserve_domain(struct iommu_domain *domain);
int iommu_preserve_device(struct iommu_domain *domain,
@@ -68,6 +98,21 @@ static inline void *dev_iommu_preserved_state(struct device *dev)
return NULL;
}
+static inline void *iommu_domain_restored_state(struct iommu_domain *domain)
+{
+ return NULL;
+}
+
+static inline int iommu_for_each_preserved_device(iommu_preserved_device_iter_fn fn, void *arg)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline struct iommu_hw_ser *iommu_get_preserved_data(u64 token, enum iommu_type_ser type)
+{
+ return NULL;
+}
+
static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser)
{
return -EOPNOTSUPP;
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 10/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (8 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 09/18] iommu: Add APIs to get iommu and device preserved state Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 11/18] iommu: Restore and reattach preserved domains to devices Samiullah Khawaja
` (7 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pratyush Yadav,
Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
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.
- Reclaim the domain ids of the preserved domains for each preserved
devices so these are not acquired by another domain.
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/intel/iommu.c | 111 +++++++++++++++++++------------
drivers/iommu/intel/iommu.h | 7 ++
drivers/iommu/intel/liveupdate.c | 79 ++++++++++++++++++++++
3 files changed, 154 insertions(+), 43 deletions(-)
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 474c926172c5..c5044e834337 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -987,28 +987,30 @@ static void iommu_disable_translation(struct intel_iommu *iommu)
raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
}
-static void disable_dmar_iommu(struct intel_iommu *iommu)
+static void release_dmar_iommu(struct intel_iommu *iommu)
{
- /*
- * All iommu domains must have been detached from the devices,
- * hence there should be no domain IDs in use.
- */
- if (WARN_ON(!ida_is_empty(&iommu->domain_ida)))
- return;
+ struct iommu_hw_ser *iommu_ser;
- if (iommu->gcmd & DMA_GCMD_TE)
- iommu_disable_translation(iommu);
-}
+ iommu_ser = iommu_get_preserved_data(iommu->reg_phys, IOMMU_INTEL);
+ if (!iommu_ser) {
+ /*
+ * All iommu domains must have been detached from the devices,
+ * hence there should be no domain IDs in use.
+ */
+ WARN_ON(!ida_is_empty(&iommu->domain_ida));
+
+ if ((iommu->gcmd & DMA_GCMD_TE))
+ iommu_disable_translation(iommu);
+ }
-static void free_dmar_iommu(struct intel_iommu *iommu)
-{
if (iommu->copied_tables) {
bitmap_free(iommu->copied_tables);
iommu->copied_tables = NULL;
}
- /* free context mapping */
- free_context_table(iommu);
+ /* free context mapping if there is no serialized state. */
+ if (!iommu_ser)
+ free_context_table(iommu);
if (ecap_prs(iommu->ecap))
intel_iommu_finish_prq(iommu);
@@ -1632,12 +1634,19 @@ static int copy_translation_tables(struct intel_iommu *iommu)
static int __init init_dmars(void)
{
+ struct iommu_hw_ser *iommu_ser;
struct dmar_drhd_unit *drhd;
struct intel_iommu *iommu;
int ret;
for_each_iommu(iommu, drhd) {
+ iommu_ser = iommu_get_preserved_data(iommu->reg_phys, IOMMU_INTEL);
if (drhd->ignored) {
+ if (WARN_ON(iommu_ser)) {
+ ret = -EINVAL;
+ goto free_iommu;
+ }
+
iommu_disable_translation(iommu);
continue;
}
@@ -1655,7 +1664,9 @@ static int __init init_dmars(void)
}
intel_iommu_init_qi(iommu);
- init_translation_status(iommu);
+
+ if (!iommu_ser)
+ init_translation_status(iommu);
if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
iommu_disable_translation(iommu);
@@ -1664,14 +1675,18 @@ static int __init init_dmars(void)
iommu->name);
}
- /*
- * TBD:
- * we could share the same root & context tables
- * among all IOMMU's. Need to Split it later.
- */
- ret = iommu_alloc_root_entry(iommu);
- if (ret)
- goto free_iommu;
+ if (iommu_ser) {
+ intel_iommu_liveupdate_restore_root_table(iommu, iommu_ser);
+ } else {
+ /*
+ * TBD:
+ * we could share the same root & context tables
+ * among all IOMMU's. Need to Split it later.
+ */
+ ret = iommu_alloc_root_entry(iommu);
+ if (ret)
+ goto free_iommu;
+ }
if (translation_pre_enabled(iommu)) {
pr_info("Translation already enabled - trying to copy translation structures\n");
@@ -1707,7 +1722,10 @@ static int __init init_dmars(void)
*/
for_each_active_iommu(iommu, drhd) {
iommu_flush_write_buffer(iommu);
- iommu_set_root_entry(iommu);
+
+ iommu_ser = iommu_get_preserved_data(iommu->reg_phys, IOMMU_INTEL);
+ if (!iommu_ser)
+ iommu_set_root_entry(iommu);
}
check_tylersburg_isoch();
@@ -1752,10 +1770,8 @@ static int __init init_dmars(void)
return 0;
free_iommu:
- for_each_active_iommu(iommu, drhd) {
- disable_dmar_iommu(iommu);
- free_dmar_iommu(iommu);
- }
+ for_each_active_iommu(iommu, drhd)
+ release_dmar_iommu(iommu);
return ret;
}
@@ -2136,17 +2152,28 @@ 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_hw_ser *iommu_ser;
int ret;
+ /* Use IOMMU HW unit MMIO base to identify the preserved state. */
+ iommu_ser = iommu_get_preserved_data(iommu->reg_phys, IOMMU_INTEL);
+
/*
* Disable translation if already enabled prior to OS handover.
*/
- if (iommu->gcmd & DMA_GCMD_TE)
+ if (!iommu_ser && iommu->gcmd & DMA_GCMD_TE)
iommu_disable_translation(iommu);
- ret = iommu_alloc_root_entry(iommu);
- if (ret)
- goto out;
+ if (iommu_ser) {
+ if (WARN_ON(dmaru->ignored))
+ return -EINVAL;
+
+ intel_iommu_liveupdate_restore_root_table(iommu, iommu_ser);
+ } else {
+ ret = iommu_alloc_root_entry(iommu);
+ if (ret)
+ goto out;
+ }
intel_svm_check(iommu);
@@ -2165,23 +2192,23 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
if (ecap_prs(iommu->ecap)) {
ret = intel_iommu_enable_prq(iommu);
if (ret)
- goto disable_iommu;
+ goto out;
}
ret = dmar_set_interrupt(iommu);
if (ret)
- goto disable_iommu;
+ goto out;
+
+ if (!iommu_ser)
+ iommu_set_root_entry(iommu);
- iommu_set_root_entry(iommu);
iommu_enable_translation(iommu);
iommu_disable_protect_mem_regions(iommu);
return 0;
-disable_iommu:
- disable_dmar_iommu(iommu);
out:
- free_dmar_iommu(iommu);
+ release_dmar_iommu(iommu);
return ret;
}
@@ -2195,12 +2222,10 @@ int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
if (iommu == NULL)
return -EINVAL;
- if (insert) {
+ if (insert)
ret = intel_iommu_add(dmaru);
- } else {
- disable_dmar_iommu(iommu);
- free_dmar_iommu(iommu);
- }
+ else
+ release_dmar_iommu(iommu);
return ret;
}
diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
index 4feb5bd76b18..3a2cb08c0ac1 100644
--- a/drivers/iommu/intel/iommu.h
+++ b/drivers/iommu/intel/iommu.h
@@ -1308,10 +1308,17 @@ int intel_iommu_preserve(struct iommu_device *iommu,
void intel_iommu_unpreserve(struct iommu_device *iommu,
struct iommu_hw_ser *iommu_ser);
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);
#else
static inline void clear_unpreserved_context_entries(struct intel_iommu *iommu)
{
}
+
+static inline void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu,
+ struct iommu_hw_ser *iommu_ser)
+{
+}
#endif
#ifdef CONFIG_INTEL_IOMMU_SVM
diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupdate.c
index 501dc0e9cc0a..c9e553379683 100644
--- a/drivers/iommu/intel/liveupdate.c
+++ b/drivers/iommu/intel/liveupdate.c
@@ -272,6 +272,85 @@ static int preserve_iommu_context_tables(struct device_domain_info *info)
return 0;
}
+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)
+ iommu_restore_pages(virt_to_phys(context));
+
+ if (!sm_supported(iommu))
+ continue;
+
+ context = iommu_context_addr(iommu, i, 0x80, 0);
+ if (context)
+ iommu_restore_pages(virt_to_phys(context));
+ }
+}
+
+static int _restore_used_domain_ids(struct iommu_device_ser *ser, void *arg)
+{
+ int id = ser->domain_iommu_ser.attachment_id;
+ struct iommu_hw_ser *iommu_hw_ser;
+ struct intel_iommu *iommu = arg;
+ int ret;
+
+ if (WARN_ON(!ser->domain_iommu_ser.iommu_phys))
+ return 0;
+
+ iommu_hw_ser = phys_to_virt(ser->domain_iommu_ser.iommu_phys);
+ if (iommu_hw_ser->type != IOMMU_INTEL)
+ return 0;
+
+ /* Only allocate domain ID from associated IOMMU HW unit */
+ if (iommu_hw_ser->intel.phys_addr != iommu->reg_phys)
+ return 0;
+
+ guard(mutex)(&iommu->did_lock);
+
+ /*
+ * The domain IDs are reclaimed while the IOMMU HW unit is being
+ * restored and not registered with the IOMMU core. So if the ID already
+ * exists, it is safe to assume that a preserved device sharing the same
+ * domain ID reclaimed it.
+ */
+ if (ida_exists(&iommu->domain_ida, id))
+ return 0;
+
+ ret = ida_alloc_range(&iommu->domain_ida, id, id, GFP_KERNEL);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+/**
+ * intel_iommu_liveupdate_restore_root_table() - Restore root table and reclaim domain IDs
+ * @iommu: Target IOMMU
+ * @iommu_ser: Serialized IOMMU hardware state from previous kernel
+ *
+ * Restores the preserved root table and context tables for the IOMMU hardware
+ * instance across Live Update, and reclaims all domain IDs previously allocated
+ * to preserved devices so they are not reused.
+ */
+void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu,
+ struct iommu_hw_ser *iommu_ser)
+{
+ if (!iommu_ser->intel.restored)
+ iommu_restore_pages(iommu_ser->intel.root_table);
+
+ iommu->root_entry = __va(iommu_ser->intel.root_table);
+
+ if (!iommu_ser->intel.restored)
+ restore_iommu_context(iommu);
+
+ iommu_ser->intel.restored = 1;
+ BUG_ON(iommu_for_each_preserved_device(_restore_used_domain_ids, iommu));
+}
+
/**
* intel_iommu_preserve_device() - Intel IOMMU callback to preserve device state
* @dev: Target device
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 11/18] iommu: Restore and reattach preserved domains to devices
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (9 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 10/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 12/18] iommu/vt-d: Handle reattach of the restored domain Samiullah Khawaja
` (6 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pratyush Yadav,
Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
During default domain setup, restore the preserved domains by restoring
the page tables using restore() iommupt op. Associated the restored
domain with the iommu group of the preserved device, and reattach the
domain to the device.
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/iommu.c | 116 +++++++++++++++++++++++++++++--
drivers/iommu/liveupdate.c | 116 +++++++++++++++++++++++++++++++
include/linux/iommu-liveupdate.h | 64 +++++++++++++++++
3 files changed, 290 insertions(+), 6 deletions(-)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 9c1ad8de3ad7..cca1eb332a43 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -158,6 +158,7 @@ static void __iommu_group_set_domain_nofail(struct iommu_group *group,
WARN_ON(__iommu_group_set_domain_internal(
group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED));
}
+static int __iommu_group_alloc_blocking_domain(struct iommu_group *group);
static int iommu_setup_default_domain(struct iommu_group *group,
int target_type);
@@ -540,6 +541,10 @@ static int iommu_init_device(struct device *dev)
goto err_free;
}
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ iommu_init_device_preserved_data(dev);
+#endif
+
iommu_dev = ops->probe_device(dev);
if (IS_ERR(iommu_dev)) {
ret = PTR_ERR(iommu_dev);
@@ -604,7 +609,8 @@ static void iommu_deinit_device(struct device *dev)
* Regardless, if a delayed attach never occurred, then the release
* should still avoid touching any hardware configuration either.
*/
- if (!dev->iommu->attach_deferred && ops->release_domain) {
+ if (!dev->iommu->attach_deferred && ops->release_domain &&
+ !dev_iommu_restored_state(dev)) {
struct iommu_domain *release_domain = ops->release_domain;
/*
@@ -694,7 +700,8 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
}
for_each_group_device(group, gdev2) {
- if (dev_iommu_preserved_state(gdev2->dev)) {
+ if (dev_iommu_preserved_state(gdev2->dev) ||
+ dev_iommu_restored_state(gdev2->dev)) {
ret = -EBUSY;
goto err_free_gdev;
}
@@ -764,6 +771,27 @@ int iommu_probe_device(struct device *dev)
return 0;
}
+static void __iommu_group_remove_restored_device(struct iommu_group *group,
+ struct device *dev)
+{
+ struct iommu_device_ser *device_ser;
+
+ lockdep_assert_held(&group->mutex);
+ device_ser = dev_iommu_restored_state(dev);
+ if (!device_ser)
+ return;
+
+ if (!group->owner_cnt || group->owner != device_ser)
+ return;
+
+ if (group->owner_cnt > 1) {
+ group->owner_cnt--;
+ } else {
+ group->owner_cnt = 0;
+ group->owner = NULL;
+ }
+}
+
static void __iommu_group_free_device(struct iommu_group *group,
struct group_device *grp_dev)
{
@@ -775,13 +803,15 @@ static void __iommu_group_free_device(struct iommu_group *group,
trace_remove_device_from_group(group->id, dev);
/*
- * If the group has become empty then ownership must have been
- * released, and the current domain must be set back to NULL or
- * the default domain.
+ * If the group has become empty then ownership must have been released,
+ * and the current domain must be set back to NULL or the default
+ * domain. A restored domain remains attached to the restored device on
+ * removal.
*/
if (list_empty(&group->devices))
WARN_ON(group->owner_cnt ||
- group->domain != group->default_domain);
+ (group->domain != group->default_domain &&
+ !iommu_domain_restored_state(group->domain)));
kfree(grp_dev->name);
kfree(grp_dev);
@@ -794,6 +824,7 @@ static void __iommu_group_remove_device(struct device *dev)
struct group_device *device;
mutex_lock(&group->mutex);
+ __iommu_group_remove_restored_device(group, dev);
for_each_group_device(group, device) {
if (device->dev != dev)
continue;
@@ -2211,6 +2242,7 @@ static int __iommu_attach_device(struct iommu_domain *domain,
ret = domain->ops->attach_dev(domain, dev, old);
if (ret)
return ret;
+
dev->iommu->attach_deferred = 0;
trace_attach_device_to_domain(dev);
return 0;
@@ -3175,6 +3207,62 @@ int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids)
}
EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
+static struct device *__iommu_group_restored_device(struct iommu_group *group)
+{
+ struct group_device *gdev;
+
+ lockdep_assert_held(&group->mutex);
+ for_each_group_device(group, gdev) {
+ if (!dev_is_pci(gdev->dev))
+ continue;
+
+ if (dev_iommu_restored_state(gdev->dev))
+ return gdev->dev;
+ }
+
+ return NULL;
+}
+
+static int __iommu_group_restore_domain(struct iommu_group *group)
+{
+ struct iommu_device_ser *device_ser;
+ struct iommu_domain *domain;
+ struct device *dev;
+ void *owner;
+ int ret;
+
+ lockdep_assert_held(&group->mutex);
+ if (group->domain)
+ return -EBUSY;
+
+ dev = __iommu_group_restored_device(group);
+ device_ser = dev_iommu_restored_state(dev);
+ if (!device_ser)
+ return -ENOENT;
+
+ ret = __iommu_group_alloc_blocking_domain(group);
+ if (ret)
+ return ret;
+
+ domain = iommu_restore_domain(dev, device_ser, &owner);
+ if (WARN_ON(IS_ERR(domain)))
+ return PTR_ERR(domain);
+
+ /* The restored domain is attached with the restored device. */
+ ret = __iommu_group_set_domain(group, domain);
+ if (ret)
+ return ret;
+
+ /*
+ * Ownership of groups with preserved devices is set during boot. These
+ * will be reclaimed later by the entity (iommufd) that preserved them.
+ */
+ WARN_ON(group->owner);
+ group->owner = owner;
+ group->owner_cnt = 1;
+ return ret;
+}
+
/**
* iommu_setup_default_domain - Set the default_domain for the group
* @group: Group to change
@@ -3233,6 +3321,16 @@ static int iommu_setup_default_domain(struct iommu_group *group,
/* We must set default_domain early for __iommu_device_set_domain */
group->default_domain = dom;
+
+ /* Preserved devices need to be attached to the restore domain */
+ if (__iommu_group_restored_device(group)) {
+ ret = __iommu_group_restore_domain(group);
+ if (ret)
+ goto err_restore_def_domain;
+
+ goto out_free_old;
+ }
+
if (!group->domain) {
/*
* Drivers are not allowed to fail the first domain attach.
@@ -4102,6 +4200,9 @@ int pci_dev_reset_iommu_prepare(struct pci_dev *pdev)
if (!pci_ats_supported(pdev) || !dev_has_iommu(&pdev->dev))
return 0;
+ if (dev_iommu_restored_state(&pdev->dev))
+ return 0;
+
guard(mutex)(&group->mutex);
gdev = __dev_to_gdev(&pdev->dev);
@@ -4213,6 +4314,9 @@ void pci_dev_reset_iommu_done(struct pci_dev *pdev)
if (!pci_ats_supported(pdev) || !dev_has_iommu(&pdev->dev))
return;
+ if (dev_iommu_restored_state(&pdev->dev))
+ return;
+
guard(mutex)(&group->mutex);
gdev = __dev_to_gdev(&pdev->dev);
diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c
index ff76a23d9583..4fa12b4efea5 100644
--- a/drivers/iommu/liveupdate.c
+++ b/drivers/iommu/liveupdate.c
@@ -716,3 +716,119 @@ void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev)
liveupdate_flb_put_outgoing(&iommu_flb);
}
EXPORT_SYMBOL_GPL(iommu_unpreserve_device);
+
+static inline bool match_device_ser(struct iommu_device_ser *match,
+ struct pci_dev *pdev)
+{
+ return match->devid == pci_dev_id(pdev) && match->pci_domain_nr == pci_domain_nr(pdev->bus);
+}
+
+/**
+ * iommu_init_device_preserved_data() - Initialize preserved state for device
+ * @dev: Target device
+ *
+ * Looks up incoming Live Update state for @dev and attaches it to the device if
+ * found.
+ */
+void iommu_init_device_preserved_data(struct device *dev)
+{
+ struct iommu_device_ser *device_ser = NULL;
+ struct iommu_device_array_ser *array;
+ struct iommu_flb_obj *flb_obj;
+ int ret, idx;
+
+ if (!dev_is_pci(dev))
+ return;
+
+ ret = iommu_liveupdate_flb_get_incoming(&flb_obj);
+ if (ret)
+ return;
+
+ mutex_lock(&flb_obj->lock);
+ array = phys_to_virt(flb_obj->ser->device_array_phys);
+ iommu_liveupdate_for_each_arr(array) {
+ iommu_liveupdate_for_each_obj(array, device_ser, idx) {
+ if (match_device_ser(device_ser, to_pci_dev(dev))) {
+ device_ser->hdr.flags |= IOMMU_SER_FLAG_INCOMING;
+ goto out;
+ }
+ }
+ }
+
+ device_ser = NULL;
+out:
+ WRITE_ONCE(dev->iommu->device_ser, device_ser);
+ mutex_unlock(&flb_obj->lock);
+ liveupdate_flb_put_incoming(&iommu_flb);
+}
+EXPORT_SYMBOL(iommu_init_device_preserved_data);
+
+/**
+ * iommu_restore_domain() - Restore a preserved domain for a device
+ * @dev: Target device
+ * @ser: Serialized device state
+ * @owner: Pointer to store group owner handle
+ *
+ * Restores or reuses a restored preserved domain for @dev from serialized state
+ * @ser.
+ *
+ * Return: Restored iommu_domain pointer, or ERR_PTR.
+ */
+struct iommu_domain *iommu_restore_domain(struct device *dev,
+ struct iommu_device_ser *ser,
+ void **owner)
+{
+ struct iommu_domain_ser *domain_ser;
+ struct iommu_flb_obj *flb_obj;
+ struct iommu_domain *domain;
+ struct pt_iommu *pt;
+ int ret;
+
+ ret = iommu_liveupdate_flb_get_incoming(&flb_obj);
+ if (ret)
+ return ERR_PTR(ret);
+
+ mutex_lock(&flb_obj->lock);
+
+ /* Preserved device should have a preserved domain */
+ if (!ser->domain_iommu_ser.domain_phys) {
+ domain = ERR_PTR(-EINVAL);
+ goto out;
+ }
+
+ domain_ser = phys_to_virt(ser->domain_iommu_ser.domain_phys);
+ if (domain_ser->restored_domain) {
+ *owner = ser;
+ domain = domain_ser->restored_domain;
+ goto out;
+ }
+
+ domain_ser->hdr.flags |= IOMMU_SER_FLAG_INCOMING;
+ domain = iommu_paging_domain_alloc(dev);
+ if (IS_ERR(domain))
+ goto out;
+
+ pt = iommupt_from_domain(domain);
+ if (!pt) {
+ iommu_domain_free(domain);
+ domain = ERR_PTR(-EOPNOTSUPP);
+ goto out;
+ }
+
+ ret = pt->ops->restore(pt, domain_ser);
+ if (ret) {
+ iommu_domain_free(domain);
+ domain = ERR_PTR(ret);
+ goto out;
+ }
+
+ /* The device is owned by the preserved state. */
+ *owner = ser;
+ domain->preserved_state = domain_ser;
+ domain_ser->restored_domain = domain;
+
+out:
+ mutex_unlock(&flb_obj->lock);
+ liveupdate_flb_put_incoming(&iommu_flb);
+ return domain;
+}
diff --git a/include/linux/iommu-liveupdate.h b/include/linux/iommu-liveupdate.h
index 06f66cc8f526..3ff537ae081d 100644
--- a/include/linux/iommu-liveupdate.h
+++ b/include/linux/iommu-liveupdate.h
@@ -64,8 +64,51 @@ static inline void *iommu_domain_restored_state(struct iommu_domain *domain)
return NULL;
}
+/**
+ * dev_iommu_restored_state() - Get restored state of a device
+ * @dev: Target device
+ *
+ * Return: Restored state pointer or NULL.
+ */
+static inline void *dev_iommu_restored_state(struct device *dev)
+{
+ struct iommu_device_ser *ser;
+
+ if (!dev->iommu)
+ return NULL;
+
+ ser = READ_ONCE(dev->iommu->device_ser);
+ if (ser && (ser->hdr.flags & IOMMU_SER_FLAG_INCOMING))
+ return ser;
+
+ return NULL;
+}
+
+/**
+ * dev_iommu_restore_did() - Get restored domain ID for a device
+ * @dev: Target device
+ * @domain: Target domain
+ *
+ * Fetches the domain ID preserved for @dev and @domain across Live Update.
+ *
+ * Return: Domain ID or -1 on error.
+ */
+static inline int dev_iommu_restore_did(struct device *dev, struct iommu_domain *domain)
+{
+ struct iommu_device_ser *ser = dev_iommu_restored_state(dev);
+
+ if (ser && iommu_domain_restored_state(domain))
+ return ser->domain_iommu_ser.attachment_id;
+
+ return -1;
+}
+
+struct iommu_domain *iommu_restore_domain(struct device *dev,
+ struct iommu_device_ser *ser,
+ void **owner);
int iommu_for_each_preserved_device(iommu_preserved_device_iter_fn fn,
void *arg);
+void iommu_init_device_preserved_data(struct device *dev);
struct iommu_hw_ser *iommu_get_preserved_data(u64 token, enum iommu_type_ser type);
int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser);
void iommu_unpreserve_domain(struct iommu_domain *domain);
@@ -98,16 +141,37 @@ static inline void *dev_iommu_preserved_state(struct device *dev)
return NULL;
}
+static inline void *dev_iommu_restored_state(struct device *dev)
+{
+ return NULL;
+}
+
+static inline int dev_iommu_restore_did(struct device *dev, struct iommu_domain *domain)
+{
+ return -1;
+}
+
static inline void *iommu_domain_restored_state(struct iommu_domain *domain)
{
return NULL;
}
+static inline struct iommu_domain *iommu_restore_domain(struct device *dev,
+ struct iommu_device_ser *ser,
+ void **owner)
+{
+ return NULL;
+}
+
static inline int iommu_for_each_preserved_device(iommu_preserved_device_iter_fn fn, void *arg)
{
return -EOPNOTSUPP;
}
+static inline void iommu_init_device_preserved_data(struct device *dev)
+{
+}
+
static inline struct iommu_hw_ser *iommu_get_preserved_data(u64 token, enum iommu_type_ser type)
{
return NULL;
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 12/18] iommu/vt-d: Handle reattach of the restored domain
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (10 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 11/18] iommu: Restore and reattach preserved domains to devices Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 13/18] iommu/vt-d: Preserve PASID table of preserved device Samiullah Khawaja
` (5 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pratyush Yadav,
Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
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 | 32 +++++--
drivers/iommu/intel/iommu.h | 14 +++
drivers/iommu/intel/liveupdate.c | 159 +++++++++++++++++++++++++++++++
3 files changed, 197 insertions(+), 8 deletions(-)
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index c5044e834337..6d3cbe0745c9 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -2819,6 +2819,9 @@ static int blocking_domain_attach_dev(struct iommu_domain *domain,
{
struct device_domain_info *info = dev_iommu_priv_get(dev);
+ if (dev_iommu_restored_state(dev))
+ return -EBUSY;
+
iopf_for_domain_remove(info->domain ? &info->domain->domain : NULL, dev);
device_block_translation(dev);
return 0;
@@ -3187,6 +3190,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);
@@ -3317,7 +3323,8 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev)
info->iommu = iommu;
RB_CLEAR_NODE(&info->node);
if (dev_is_pci(dev)) {
- if (ecap_dev_iotlb_support(iommu->ecap) &&
+ if (!dev_iommu_restored_state(dev) &&
+ ecap_dev_iotlb_support(iommu->ecap) &&
pci_ats_supported(pdev) &&
dmar_ats_supported(pdev, iommu)) {
info->ats_supported = 1;
@@ -3421,12 +3428,16 @@ static void intel_iommu_release_device(struct device *dev)
struct device_domain_info *info = dev_iommu_priv_get(dev);
struct intel_iommu *iommu = info->iommu;
- iommu_disable_pci_pri(info);
- iommu_disable_pci_ats(info);
+ if (!dev_iommu_restored_state(dev)) {
+ iommu_disable_pci_pri(info);
+ iommu_disable_pci_ats(info);
- if (info->pasid_enabled) {
- pci_disable_pasid(to_pci_dev(dev));
- info->pasid_enabled = 0;
+ if (info->pasid_enabled) {
+ pci_disable_pasid(to_pci_dev(dev));
+ info->pasid_enabled = 0;
+ }
+ } else {
+ intel_iommu_detach_restored_device(dev);
}
mutex_lock(&iommu->iopf_lock);
@@ -3434,11 +3445,13 @@ static void intel_iommu_release_device(struct device *dev)
device_rbtree_remove(info);
mutex_unlock(&iommu->iopf_lock);
- if (sm_supported(iommu) && !dev_is_real_dma_subdevice(dev) &&
+ if (!dev_iommu_restored_state(dev) && sm_supported(iommu) &&
+ !dev_is_real_dma_subdevice(dev) &&
!context_copied(iommu, info->bus, info->devfn))
intel_pasid_teardown_sm_context(dev);
- intel_pasid_free_table(dev);
+ if (!dev_iommu_restored_state(dev))
+ intel_pasid_free_table(dev);
intel_iommu_debugfs_remove_dev(info);
kfree(info);
}
@@ -3900,6 +3913,9 @@ static int identity_domain_attach_dev(struct iommu_domain *domain,
struct intel_iommu *iommu = info->iommu;
int ret;
+ if (dev_iommu_restored_state(dev))
+ return -EBUSY;
+
device_block_translation(dev);
if (dev_is_real_dma_subdevice(dev))
diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
index 3a2cb08c0ac1..25104644317c 100644
--- a/drivers/iommu/intel/iommu.h
+++ b/drivers/iommu/intel/iommu.h
@@ -1310,6 +1310,9 @@ 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);
+int intel_iommu_detach_restored_device(struct device *dev);
#else
static inline void clear_unpreserved_context_entries(struct intel_iommu *iommu)
{
@@ -1319,6 +1322,17 @@ 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;
+}
+
+static inline int intel_iommu_detach_restored_device(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 c9e553379683..6e6707eefc8c 100644
--- a/drivers/iommu/intel/liveupdate.c
+++ b/drivers/iommu/intel/liveupdate.c
@@ -351,6 +351,165 @@ 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;
+ struct iommu_domain_ser *domain_ser;
+ struct iommu_hw_ser *iommu_hw_ser;
+ int restored_did;
+ int ret;
+
+ if (!iommu_domain_restored_state(&domain->domain))
+ return -EINVAL;
+
+ if (!device_ser->domain_iommu_ser.domain_phys ||
+ !device_ser->domain_iommu_ser.iommu_phys)
+ return -EINVAL;
+
+ domain_ser = phys_to_virt(device_ser->domain_iommu_ser.domain_phys);
+ if (domain_ser->restored_domain != &domain->domain)
+ return -EINVAL;
+
+ iommu_hw_ser = phys_to_virt(device_ser->domain_iommu_ser.iommu_phys);
+ if (iommu_hw_ser->type != IOMMU_INTEL ||
+ iommu_hw_ser->intel.phys_addr != iommu->reg_phys)
+ return -EINVAL;
+
+ restored_did = device_ser->domain_iommu_ser.attachment_id;
+ if (!ida_exists(&iommu->domain_ida, restored_did))
+ return -EINVAL;
+
+ 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;
+
+ 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);
+
+ 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;
+}
+
+int intel_iommu_detach_restored_device(struct device *dev)
+{
+ struct device_domain_info *info = dev_iommu_priv_get(dev);
+ struct intel_iommu *iommu = info->iommu;
+ struct iommu_domain *domain;
+ unsigned long flags;
+
+ if (!info->domain_attached || !info->domain)
+ return -EINVAL;
+
+ domain = &info->domain->domain;
+ if (!iommu_domain_restored_state(domain))
+ return -EINVAL;
+
+ iopf_for_domain_remove(domain, dev);
+ cache_tag_unassign_domain(info->domain, dev, IOMMU_NO_PASID);
+ info->domain_attached = false;
+
+ 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 0;
+}
+
/**
* intel_iommu_preserve_device() - Intel IOMMU callback to preserve device state
* @dev: Target device
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 13/18] iommu/vt-d: Preserve PASID table of preserved device
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (11 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 12/18] iommu/vt-d: Handle reattach of the restored domain Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 14/18] iommufd: Implement ioctl to mark HWPT for preservation Samiullah Khawaja
` (4 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Robin Murphy, Kevin Tian, Alex Williamson,
Shuah Khan, iommu, linux-kernel, kvm, Pratyush Yadav,
Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
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/liveupdate.c | 141 +++++++++++++++++++++++++++++--
drivers/iommu/intel/pasid.c | 10 ++-
drivers/iommu/intel/pasid.h | 8 ++
include/linux/kho/abi/iommu.h | 17 ++++
4 files changed, 169 insertions(+), 7 deletions(-)
diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupdate.c
index 6e6707eefc8c..b9467fb7195f 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 */
@@ -510,6 +511,69 @@ int intel_iommu_detach_restored_device(struct device *dev)
return 0;
}
+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
@@ -521,6 +585,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)) {
@@ -543,6 +608,22 @@ int intel_iommu_preserve_device(struct device *dev,
device_ser->domain_iommu_ser.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;
}
@@ -554,15 +635,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);
}
/**
@@ -607,3 +706,35 @@ 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.
+ */
+ BUG_ON(ser->intel.max_pasid != max_pasid);
+
+ if (ser->intel.restored)
+ goto out;
+
+ BUG_ON(pasid_lu_handle_pd(phys_to_virt(ser->intel.pasid_table),
+ ser->intel.max_pasid,
+ PASID_LU_OP_RESTORE));
+ 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 5aaa29da6832..308cd83fd3e3 100644
--- a/include/linux/kho/abi/iommu.h
+++ b/include/linux/kho/abi/iommu.h
@@ -129,6 +129,19 @@ struct iommu_dev_map_ser {
u64 iommu_phys;
} __packed;
+/**
+ * struct iommu_device_intel_ser - Intel specific state of serialized device
+ * @restored: Whether the device state is restored
+ * @pasid_table: Physical address of pasid table
+ * @max_pasid: Maximum supported pasid
+ */
+struct iommu_device_intel_ser {
+ u8 restored;
+ u8 padding[7];
+ u64 pasid_table;
+ u64 max_pasid;
+} __packed;
+
/**
* struct iommu_device_ser - Serialized state of a device
* @hdr: Common object header
@@ -136,6 +149,7 @@ struct iommu_dev_map_ser {
* @pci_domain_nr: PCI domain number
* @dma_owner_token: Token to identify the DMA owner of this device
* @domain_iommu_ser: Domain and IOMMU mapping
+ * @intel: Intel specific serialization data
*/
struct iommu_device_ser {
struct iommu_hdr_ser hdr;
@@ -143,6 +157,9 @@ struct iommu_device_ser {
u32 pci_domain_nr;
u64 dma_owner_token;
struct iommu_dev_map_ser domain_iommu_ser;
+ union {
+ struct iommu_device_intel_ser intel;
+ };
} __packed;
/* There are maximum 256 buses, so maximum 512 context tables */
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 14/18] iommufd: Implement ioctl to mark HWPT for preservation
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (12 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 13/18] iommu/vt-d: Preserve PASID table of preserved device Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 15/18] iommufd: Persist iommu hardware pagetables for live update Samiullah Khawaja
` (3 subsequent siblings)
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: YiFei Zhu, Pranjal Shrivastava, Samiullah Khawaja, Robin Murphy,
Kevin Tian, Alex Williamson, Shuah Khan, iommu, linux-kernel,
kvm, Pratyush Yadav, Pasha Tatashin, David Matlack,
Andrew Morton, Vipin Sharma
From: YiFei Zhu <zhuyifei@google.com>
Userspace provides a token to mark the HWPT for preservation. Note that
this token is not the LUO token that is used to preserve the iommufd.
Once all the required HWPT are marked for preservation, the user can
preserve the iommufd into LUO. The iommufd will preserve the HWPTs that
are marked for preservation.
The marked HWPTs are tracked using a new XArray mark protected by a new
liveupdate mutex. This mutex will also be used during iommufd
preservation to protect against any race with the mark preserve ioctl.
The HWPT token will be used during restore to identify this HWPT. The
restoration logic is not implemented and will be added later.
Reviewed-by: Pranjal Shrivastava <praan@google.com>
Signed-off-by: YiFei Zhu <zhuyifei@google.com>
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
MAINTAINERS | 1 +
drivers/iommu/iommufd/Makefile | 1 +
drivers/iommu/iommufd/iommufd_private.h | 17 ++++++
drivers/iommu/iommufd/liveupdate.c | 71 +++++++++++++++++++++++++
drivers/iommu/iommufd/main.c | 9 ++++
include/uapi/linux/iommufd.h | 27 ++++++++++
6 files changed, 126 insertions(+)
create mode 100644 drivers/iommu/iommufd/liveupdate.c
diff --git a/MAINTAINERS b/MAINTAINERS
index ae6df95dc398..d4848ec3a9d6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13728,6 +13728,7 @@ M: Samiullah Khawaja <skhawaja@google.com>
R: Pranjal Shrivastava <praan@google.com>
L: iommu@lists.linux.dev
S: Maintained
+F: drivers/iommu/iommufd/liveupdate.c
F: drivers/iommu/liveupdate.c
F: include/linux/iommu-liveupdate.h
F: include/linux/kho/abi/iommu.h
diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile
index 67207914bb6e..cfd5b4b14ccb 100644
--- a/drivers/iommu/iommufd/Makefile
+++ b/drivers/iommu/iommufd/Makefile
@@ -12,6 +12,7 @@ iommufd-y := \
iommufd-$(CONFIG_IOMMUFD_NOIOMMU) += hwpt_noiommu.o
iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o
+iommufd-$(CONFIG_IOMMU_LIVEUPDATE) += liveupdate.o
obj-$(CONFIG_IOMMUFD) += iommufd.o
obj-$(CONFIG_IOMMUFD_DRIVER) += iova_bitmap.o
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index eb2e85b27e42..a33b32708afa 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -44,6 +44,11 @@ struct iommufd_ctx {
struct file *file;
struct xarray objects;
struct xarray groups;
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+#define IOMMUFD_OBJ_LIVEUPDATE_MARK XA_MARK_1
+ /* @liveupdate_mutex: Protects the preservation of HWPTs. */
+ struct mutex liveupdate_mutex;
+#endif
wait_queue_head_t destroy_wait;
struct rw_semaphore ioas_creation_lock;
struct maple_tree mt_mmap;
@@ -392,6 +397,9 @@ struct iommufd_hwpt_paging {
bool auto_domain : 1;
bool enforce_cache_coherency : 1;
bool nest_parent : 1;
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ u64 liveupdate_token;
+#endif
/* Head at iommufd_ioas::hwpt_list */
struct list_head hwpt_item;
struct iommufd_sw_msi_maps present_sw_msi;
@@ -729,6 +737,15 @@ void iommufd_vdevice_abort(struct iommufd_object *obj);
int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd);
void iommufd_hw_queue_destroy(struct iommufd_object *obj);
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd);
+#else
+static inline int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd)
+{
+ return -ENOTTY;
+}
+#endif
+
#ifdef CONFIG_IOMMUFD_TEST
int iommufd_test(struct iommufd_ucmd *ucmd);
void iommufd_selftest_destroy(struct iommufd_object *obj);
diff --git a/drivers/iommu/iommufd/liveupdate.c b/drivers/iommu/iommufd/liveupdate.c
new file mode 100644
index 000000000000..96f01ee5a1e8
--- /dev/null
+++ b/drivers/iommu/iommufd/liveupdate.c
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (C) 2026, Google LLC
+ * Author: Samiullah Khawaja <skhawaja@google.com>
+ */
+
+#define pr_fmt(fmt) "iommufd: " fmt
+
+#include <linux/file.h>
+#include <linux/iommufd.h>
+#include <linux/liveupdate.h>
+
+#include "iommufd_private.h"
+
+int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd)
+{
+ struct iommu_hwpt_liveupdate_mark_preserve *cmd = ucmd->cmd;
+ struct iommufd_hwpt_paging *hwpt_target;
+ struct iommufd_hwpt_paging *hwpt_paging;
+ struct iommufd_ctx *ictx = ucmd->ictx;
+ struct iommufd_object *obj;
+ unsigned long index;
+ bool marked = false;
+ int rc = 0;
+
+ hwpt_target = iommufd_get_hwpt_paging(ucmd, cmd->hwpt_id);
+ if (IS_ERR(hwpt_target))
+ return PTR_ERR(hwpt_target);
+
+ mutex_lock(&ictx->liveupdate_mutex);
+
+ xa_lock(&ictx->objects);
+
+ /* PRI use cases are not supported. */
+ if (hwpt_target->common.fault) {
+ rc = -EOPNOTSUPP;
+ goto out_unlock;
+ }
+
+ xa_for_each_marked(&ictx->objects, index, obj, IOMMUFD_OBJ_LIVEUPDATE_MARK) {
+ if (WARN_ON_ONCE(obj->type != IOMMUFD_OBJ_HWPT_PAGING))
+ continue;
+
+ hwpt_paging = to_hwpt_paging(container_of(obj, struct iommufd_hw_pagetable, obj));
+
+ if (hwpt_paging == hwpt_target)
+ marked = true;
+
+ if (hwpt_paging->liveupdate_token == cmd->hwpt_token) {
+ if (hwpt_paging == hwpt_target)
+ goto out_unlock;
+
+ rc = -EADDRINUSE;
+ goto out_unlock;
+ }
+ }
+
+ __xa_set_mark(&ictx->objects, hwpt_target->common.obj.id, IOMMUFD_OBJ_LIVEUPDATE_MARK);
+
+ if (marked)
+ pr_warn_ratelimited("Overwriting HWPT liveupdate token from: %llu to %llu\n",
+ hwpt_target->liveupdate_token, cmd->hwpt_token);
+ hwpt_target->liveupdate_token = cmd->hwpt_token;
+
+out_unlock:
+ xa_unlock(&ictx->objects);
+ mutex_unlock(&ictx->liveupdate_mutex);
+ iommufd_put_object(ictx, &hwpt_target->common.obj);
+ return rc;
+}
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index 9a921b153162..fe8610fd58e3 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -333,6 +333,9 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp)
init_rwsem(&ictx->ioas_creation_lock);
xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT);
xa_init(&ictx->groups);
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ mutex_init(&ictx->liveupdate_mutex);
+#endif
ictx->file = filp;
mt_init_flags(&ictx->mt_mmap, MT_FLAGS_ALLOC_RANGE);
init_waitqueue_head(&ictx->destroy_wait);
@@ -395,6 +398,9 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp)
* iommufd_object_tombstone_user()
*/
xa_destroy(&ictx->objects);
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ mutex_destroy(&ictx->liveupdate_mutex);
+#endif
WARN_ON(!xa_empty(&ictx->groups));
@@ -440,6 +446,7 @@ union ucmd_buffer {
struct iommu_hwpt_alloc hwpt;
struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap;
struct iommu_hwpt_invalidate cache;
+ struct iommu_hwpt_liveupdate_mark_preserve mark_preserve;
struct iommu_hwpt_set_dirty_tracking set_dirty_tracking;
struct iommu_ioas_alloc alloc;
struct iommu_ioas_allow_iovas allow_iovas;
@@ -516,6 +523,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
__reserved),
IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl,
struct iommu_viommu_alloc, out_viommu_id),
+ IOCTL_OP(IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE, iommufd_hwpt_liveupdate_mark_preserve,
+ struct iommu_hwpt_liveupdate_mark_preserve, hwpt_token),
#ifdef CONFIG_IOMMUFD_TEST
IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
#endif
diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
index 206fa667c782..34637712c873 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -58,6 +58,7 @@ enum {
IOMMUFD_CMD_VEVENTQ_ALLOC = 0x93,
IOMMUFD_CMD_HW_QUEUE_ALLOC = 0x94,
IOMMUFD_CMD_IOAS_NOIOMMU_GET_PA = 0x95,
+ IOMMUFD_CMD_HWPT_LIVEUPDATE_MARK_PRESERVE = 0x96,
};
/**
@@ -1390,4 +1391,30 @@ struct iommu_hw_queue_alloc {
__aligned_u64 length;
};
#define IOMMU_HW_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HW_QUEUE_ALLOC)
+
+/**
+ * struct iommu_hwpt_liveupdate_mark_preserve - ioctl(IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE)
+ * @size: sizeof(struct iommu_hwpt_liveupdate_mark_preserve)
+ * @hwpt_id: Iommufd object ID of the target HWPT
+ * @hwpt_token: Token to identify this hwpt upon restore
+ *
+ * The target HWPT will be preserved during iommufd preservation.
+ * Only file-based memory mappings (e.g. memfd) are supported for HWPTs marked
+ * for preservation. Mapping anonymous memory into a preserved HWPT will result
+ * in a failure during the preservation phase.
+ *
+ * The hwpt_token is provided by userspace. If userspace enters a token
+ * already in use within this iommufd, -EADDRINUSE is returned from this ioctl.
+ *
+ * Note: There is no 'unmark' operation, so any HWPTs pooled in userspace that
+ * are marked for preservation must be destroyed after use.
+ */
+struct iommu_hwpt_liveupdate_mark_preserve {
+ __u32 size;
+ __u32 hwpt_id;
+ __aligned_u64 hwpt_token;
+};
+#define IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE \
+ _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_LIVEUPDATE_MARK_PRESERVE)
+
#endif
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 15/18] iommufd: Persist iommu hardware pagetables for live update
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (13 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 14/18] iommufd: Implement ioctl to mark HWPT for preservation Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-23 23:59 ` John Starks
2026-09-21 0:48 ` [PATCH v5 16/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev Samiullah Khawaja
` (2 subsequent siblings)
17 siblings, 1 reply; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: YiFei Zhu, Samiullah Khawaja, Robin Murphy, Kevin Tian,
Alex Williamson, Shuah Khan, iommu, linux-kernel, kvm,
Pratyush Yadav, Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
From: YiFei Zhu <zhuyifei@google.com>
Register iommufd with the LUO framework and implement the preserve and
unpreserve ops to save marked HWPTs.
To make sure mappings do not change during preserved state, add a
liveupdate_immutable flag to IOAS. When an HWPT is preserved, its IOAS
is marked immutable and any map/unmap attempts will fail with -EBUSY.
This is synchronized using the domains_rwsem to prevent races with
concurrent mapping operations.
The preserve callback iterates over the marked HWPTs, verifies that the
backing memory pages are preserved, and calls iommu_preserve_domain() to
preserve the associated IOMMU domain.
Signed-off-by: YiFei Zhu <zhuyifei@google.com>
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
MAINTAINERS | 1 +
drivers/iommu/iommufd/io_pagetable.c | 11 +
drivers/iommu/iommufd/io_pagetable.h | 1 +
drivers/iommu/iommufd/iommufd_private.h | 26 ++
drivers/iommu/iommufd/liveupdate.c | 308 ++++++++++++++++++++++++
drivers/iommu/iommufd/main.c | 10 +-
drivers/iommu/iommufd/pages.c | 11 +
include/linux/kho/abi/iommufd.h | 51 ++++
8 files changed, 418 insertions(+), 1 deletion(-)
create mode 100644 include/linux/kho/abi/iommufd.h
diff --git a/MAINTAINERS b/MAINTAINERS
index d4848ec3a9d6..058a227e99d9 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13732,6 +13732,7 @@ F: drivers/iommu/iommufd/liveupdate.c
F: drivers/iommu/liveupdate.c
F: include/linux/iommu-liveupdate.h
F: include/linux/kho/abi/iommu.h
+F: include/linux/kho/abi/iommufd.h
IOMMUFD
M: Jason Gunthorpe <jgg@nvidia.com>
diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c
index 4e447ce74cf6..bad302e9e282 100644
--- a/drivers/iommu/iommufd/io_pagetable.c
+++ b/drivers/iommu/iommufd/io_pagetable.c
@@ -384,6 +384,11 @@ int iopt_map_pages(struct io_pagetable *iopt, struct list_head *pages_list,
return rc;
down_read(&iopt->domains_rwsem);
+ if (iopt_liveupdate_immutable(iopt)) {
+ rc = -EBUSY;
+ goto out_unlock_domains;
+ }
+
rc = iopt_fill_domains_pages(pages_list);
if (rc)
goto out_unlock_domains;
@@ -755,6 +760,12 @@ static int iopt_unmap_iova_range(struct io_pagetable *iopt, unsigned long start,
again:
down_read(&iopt->domains_rwsem);
down_write(&iopt->iova_rwsem);
+
+ if (iopt_liveupdate_immutable(iopt)) {
+ rc = -EBUSY;
+ goto out_unlock_iova;
+ }
+
while ((area = iopt_area_iter_first(iopt, start, last))) {
unsigned long area_last = iopt_area_last_iova(area);
unsigned long area_first = iopt_area_iova(area);
diff --git a/drivers/iommu/iommufd/io_pagetable.h b/drivers/iommu/iommufd/io_pagetable.h
index 27e3e311d395..207ff368d412 100644
--- a/drivers/iommu/iommufd/io_pagetable.h
+++ b/drivers/iommu/iommufd/io_pagetable.h
@@ -234,6 +234,7 @@ struct iopt_pages {
struct { /* IOPT_ADDRESS_FILE */
struct file *file;
unsigned long start;
+ u32 seals;
};
/* IOPT_ADDRESS_DMABUF */
struct iopt_pages_dmabuf dmabuf;
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index a33b32708afa..a4ddc29ec5ae 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -98,6 +98,9 @@ struct io_pagetable {
/* IOVA that cannot be allocated, struct iopt_reserved */
struct rb_root_cached reserved_itree;
u8 disable_large_pages;
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ u32 nr_preserved_domains;
+#endif
unsigned long iova_alignment;
};
@@ -398,6 +401,7 @@ struct iommufd_hwpt_paging {
bool enforce_cache_coherency : 1;
bool nest_parent : 1;
#ifdef CONFIG_IOMMU_LIVEUPDATE
+ bool liveupdate_preserved;
u64 liveupdate_token;
#endif
/* Head at iommufd_ioas::hwpt_list */
@@ -738,12 +742,34 @@ int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd);
void iommufd_hw_queue_destroy(struct iommufd_object *obj);
#ifdef CONFIG_IOMMU_LIVEUPDATE
+int iommufd_liveupdate_register(void);
+void iommufd_liveupdate_unregister(void);
+
int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd);
+
+static inline bool iopt_liveupdate_immutable(const struct io_pagetable *iopt)
+{
+ return iopt->nr_preserved_domains > 0;
+}
#else
+static inline int iommufd_liveupdate_register(void)
+{
+ return 0;
+}
+
+static inline void iommufd_liveupdate_unregister(void)
+{
+}
+
static inline int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd)
{
return -ENOTTY;
}
+
+static inline bool iopt_liveupdate_immutable(const struct io_pagetable *iopt)
+{
+ return false;
+}
#endif
#ifdef CONFIG_IOMMUFD_TEST
diff --git a/drivers/iommu/iommufd/liveupdate.c b/drivers/iommu/iommufd/liveupdate.c
index 96f01ee5a1e8..423369e3a8af 100644
--- a/drivers/iommu/iommufd/liveupdate.c
+++ b/drivers/iommu/iommufd/liveupdate.c
@@ -9,9 +9,31 @@
#include <linux/file.h>
#include <linux/iommufd.h>
+#include <linux/kexec_handover.h>
+#include <linux/kho/abi/iommufd.h>
#include <linux/liveupdate.h>
+#include <linux/iommu-liveupdate.h>
+#include <linux/mm.h>
+#include <linux/pci.h>
#include "iommufd_private.h"
+#include "io_pagetable.h"
+
+static bool ioas_set_immutable(struct iommufd_ioas *ioas, bool set)
+{
+ bool was_immutable;
+
+ down_write(&ioas->iopt.domains_rwsem);
+ was_immutable = ioas->iopt.nr_preserved_domains > 0;
+ if (set)
+ ioas->iopt.nr_preserved_domains++;
+ else if (!WARN_ON(!was_immutable))
+ ioas->iopt.nr_preserved_domains--;
+
+ up_write(&ioas->iopt.domains_rwsem);
+
+ return was_immutable;
+}
int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd)
{
@@ -69,3 +91,289 @@ int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd)
iommufd_put_object(ictx, &hwpt_target->common.obj);
return rc;
}
+
+static int check_iopt_pages_preserved(struct liveupdate_session *s,
+ struct iommufd_hwpt_paging *hwpt)
+{
+ u32 req_seals = F_SEAL_SEAL | F_SEAL_GROW | F_SEAL_SHRINK;
+ struct iopt_area *area;
+ int ret = 0;
+
+ down_read(&hwpt->ioas->iopt.iova_rwsem);
+ for (area = iopt_area_iter_first(&hwpt->ioas->iopt, 0, ULONG_MAX); area;
+ area = iopt_area_iter_next(area, 0, ULONG_MAX)) {
+ struct iopt_pages *pages = area->pages;
+
+ if (!pages)
+ continue;
+
+ /* Only allow file based mapping */
+ if (pages->type != IOPT_ADDRESS_FILE) {
+ ret = -EINVAL;
+ break;
+ }
+
+ /*
+ * When this memory file was mapped it should be sealed and seal
+ * should be sealed. This means that since mapping was done the
+ * memory file was not grown or shrink and the pages being used
+ * until now remain pinned and preserved.
+ */
+ if ((pages->seals & req_seals) != req_seals) {
+ ret = -EINVAL;
+ break;
+ }
+
+ /* Make sure that the file was preserved. */
+ ret = liveupdate_get_token_outgoing(s, pages->file, NULL);
+ if (ret)
+ break;
+ }
+ up_read(&hwpt->ioas->iopt.iova_rwsem);
+
+ return ret;
+}
+
+static int iommufd_preserve_hwpt(struct iommufd_hwpt_paging *hwpt,
+ struct iommufd_hwpt_ser *hwpt_ser,
+ struct liveupdate_session *session)
+{
+ struct iommu_domain_ser *domain_ser;
+ bool was_immutable;
+ int rc;
+
+ /*
+ * Make IOAS immutable so the DMA mappings do not change while
+ * the HWPT is preserved. Since one IOAS can have multiple
+ * HWPTs, if an error occurs this call needs to make the IOAS
+ * mutable again if it was the one that made it immutable.
+ */
+ was_immutable = ioas_set_immutable(hwpt->ioas, true);
+
+ if (!was_immutable) {
+ rc = check_iopt_pages_preserved(session, hwpt);
+ if (rc)
+ goto err;
+ }
+
+ hwpt_ser->token = hwpt->liveupdate_token;
+ hwpt_ser->reclaimed = false;
+
+ rc = iommu_preserve_domain(hwpt->common.domain, &domain_ser);
+ if (rc < 0)
+ goto err;
+
+ hwpt_ser->domain_data = virt_to_phys(domain_ser);
+ return 0;
+
+err:
+ ioas_set_immutable(hwpt->ioas, false);
+ return rc;
+}
+
+static void _iommufd_unpreserve(struct iommufd_ctx *ictx,
+ struct iommufd_ser *ser)
+{
+ struct iommufd_hwpt_paging *hwpt;
+ struct iommufd_object *obj;
+ unsigned long index;
+
+ xa_lock(&ictx->objects);
+ xa_for_each_marked(&ictx->objects, index, obj, IOMMUFD_OBJ_LIVEUPDATE_MARK) {
+ if (obj->type != IOMMUFD_OBJ_HWPT_PAGING)
+ continue;
+
+ hwpt = to_hwpt_paging(container_of(obj, struct iommufd_hw_pagetable, obj));
+ if (!hwpt->liveupdate_preserved)
+ continue;
+
+ xa_unlock(&ictx->objects);
+
+ iommu_unpreserve_domain(hwpt->common.domain);
+ ioas_set_immutable(hwpt->ioas, false);
+
+ hwpt->liveupdate_preserved = false;
+ iommufd_put_object(ictx, obj);
+
+ xa_lock(&ictx->objects);
+ }
+ xa_unlock(&ictx->objects);
+
+ kho_unpreserve_free(ser);
+}
+
+static int iommufd_liveupdate_preserve(struct liveupdate_file_op_args *args)
+{
+ struct iommufd_ctx *ictx;
+ struct iommufd_hwpt_paging *hwpt;
+ struct iommufd_ser *iommufd_ser;
+ struct iommufd_object *obj;
+ unsigned int nr_hwpts;
+ unsigned long index;
+ unsigned int i;
+ void *mem;
+ int rc;
+
+ ictx = iommufd_ctx_from_file(args->file);
+ if (IS_ERR(ictx))
+ return PTR_ERR(ictx);
+
+ mutex_lock(&ictx->liveupdate_mutex);
+
+ /* Count the number of HWPTs to preserve */
+ nr_hwpts = 0;
+ xa_lock(&ictx->objects);
+ xa_for_each_marked(&ictx->objects, index, obj, IOMMUFD_OBJ_LIVEUPDATE_MARK) {
+ if (obj->type != IOMMUFD_OBJ_HWPT_PAGING)
+ continue;
+
+ hwpt = to_hwpt_paging(container_of(obj, struct iommufd_hw_pagetable, obj));
+ if (!hwpt->common.domain) {
+ rc = -EINVAL;
+ xa_unlock(&ictx->objects);
+ goto out_unlock;
+ }
+ nr_hwpts++;
+ }
+ xa_unlock(&ictx->objects);
+
+ mem = kho_alloc_preserve(struct_size(iommufd_ser,
+ hwpt_array, nr_hwpts));
+ if (IS_ERR(mem)) {
+ rc = PTR_ERR(mem);
+ goto out_unlock;
+ }
+
+ iommufd_ser = mem;
+ iommufd_ser->nr_hwpts = nr_hwpts;
+
+ /* Preserve HWPTs */
+ i = 0;
+ xa_lock(&ictx->objects);
+ xa_for_each_marked(&ictx->objects, index, obj, IOMMUFD_OBJ_LIVEUPDATE_MARK) {
+ if (obj->type != IOMMUFD_OBJ_HWPT_PAGING)
+ continue;
+
+ if (!iommufd_lock_obj(obj)) {
+ rc = -ENOENT;
+ xa_unlock(&ictx->objects);
+ goto out_unpreserve;
+ }
+
+ /*
+ * HWPT is locked so it will not be destroyed. The xarray lock
+ * can be released here before preserving the HWPT.
+ */
+ xa_unlock(&ictx->objects);
+ hwpt = to_hwpt_paging(container_of(obj, struct iommufd_hw_pagetable, obj));
+ rc = iommufd_preserve_hwpt(hwpt, &iommufd_ser->hwpt_array[i++], args->session);
+ if (rc) {
+ iommufd_put_object(ictx, obj);
+ goto out_unpreserve;
+ }
+
+ /*
+ * Mark the HWPT as successfully preserved. This is distinct
+ * from IOMMUFD_OBJ_LIVEUPDATE_MARK, which only indicates the
+ * userspace intent to preserve.
+ */
+ hwpt->liveupdate_preserved = true;
+ xa_lock(&ictx->objects);
+ }
+ xa_unlock(&ictx->objects);
+
+ /* Store the actual number of HWPTs that are preserved */
+ iommufd_ser->nr_hwpts = i;
+
+ args->serialized_data = virt_to_phys(iommufd_ser);
+ mutex_unlock(&ictx->liveupdate_mutex);
+ iommufd_ctx_put(ictx);
+ return 0;
+
+out_unpreserve:
+ _iommufd_unpreserve(ictx, iommufd_ser);
+out_unlock:
+ mutex_unlock(&ictx->liveupdate_mutex);
+ iommufd_ctx_put(ictx);
+ return rc;
+}
+
+static void iommufd_liveupdate_unpreserve(struct liveupdate_file_op_args *args)
+{
+ struct iommufd_ctx *ictx;
+
+ ictx = iommufd_ctx_from_file(args->file);
+ if (WARN_ON(IS_ERR(ictx)))
+ return;
+
+ mutex_lock(&ictx->liveupdate_mutex);
+ _iommufd_unpreserve(ictx, phys_to_virt(args->serialized_data));
+ mutex_unlock(&ictx->liveupdate_mutex);
+
+ iommufd_ctx_put(ictx);
+}
+
+static int iommufd_liveupdate_retrieve(struct liveupdate_file_op_args *args)
+{
+ return -EOPNOTSUPP;
+}
+
+static bool iommufd_liveupdate_can_finish(struct liveupdate_file_op_args *args)
+{
+ return false;
+}
+
+static void iommufd_liveupdate_finish(struct liveupdate_file_op_args *args)
+{
+}
+
+static bool iommufd_liveupdate_can_preserve(struct liveupdate_file_handler *handler,
+ struct file *file)
+{
+ struct iommufd_ctx *ictx = iommufd_ctx_from_file(file);
+
+ if (IS_ERR(ictx))
+ return false;
+
+ iommufd_ctx_put(ictx);
+ return true;
+}
+
+static struct liveupdate_file_ops iommufd_ser_file_ops = {
+ .can_preserve = iommufd_liveupdate_can_preserve,
+ .preserve = iommufd_liveupdate_preserve,
+ .unpreserve = iommufd_liveupdate_unpreserve,
+ .retrieve = iommufd_liveupdate_retrieve,
+ .can_finish = iommufd_liveupdate_can_finish,
+ .finish = iommufd_liveupdate_finish,
+};
+
+static struct liveupdate_file_handler iommufd_ser_handler = {
+ .compatible = IOMMUFD_LUO_COMPATIBLE,
+ .ops = &iommufd_ser_file_ops,
+};
+
+int iommufd_liveupdate_register(void)
+{
+ int ret;
+
+ ret = liveupdate_register_file_handler(&iommufd_ser_handler);
+ if (ret)
+ goto out;
+
+ ret = iommu_liveupdate_register_flb(&iommufd_ser_handler);
+ if (ret)
+ liveupdate_unregister_file_handler(&iommufd_ser_handler);
+
+out:
+ if (ret == -EOPNOTSUPP)
+ return 0;
+
+ return ret;
+}
+
+void iommufd_liveupdate_unregister(void)
+{
+ iommu_liveupdate_unregister_flb(&iommufd_ser_handler);
+ liveupdate_unregister_file_handler(&iommufd_ser_handler);
+}
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index fe8610fd58e3..86a578fb8e18 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -805,11 +805,18 @@ static int __init iommufd_init(void)
if (ret)
goto err_misc;
}
- ret = iommufd_test_init();
+
+ ret = iommufd_liveupdate_register();
if (ret)
goto err_vfio_misc;
+
+ ret = iommufd_test_init();
+ if (ret)
+ goto err_liveupdate;
return 0;
+err_liveupdate:
+ iommufd_liveupdate_unregister();
err_vfio_misc:
if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER))
misc_deregister(&vfio_misc_dev);
@@ -821,6 +828,7 @@ static int __init iommufd_init(void)
static void __exit iommufd_exit(void)
{
iommufd_test_exit();
+ iommufd_liveupdate_unregister();
if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER))
misc_deregister(&vfio_misc_dev);
misc_deregister(&iommu_misc_dev);
diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
index 404f31d8f729..08e7b882e467 100644
--- a/drivers/iommu/iommufd/pages.c
+++ b/drivers/iommu/iommufd/pages.c
@@ -55,6 +55,7 @@
#include <linux/overflow.h>
#include <linux/slab.h>
#include <linux/sched/mm.h>
+#include <linux/memfd.h>
#include <linux/vfio_pci_core.h>
#include "double_span.h"
@@ -1421,6 +1422,7 @@ struct iopt_pages *iopt_alloc_file_pages(struct file *file,
{
struct iopt_pages *pages;
+ int seals;
pages = iopt_alloc_pages(start_byte, length, writable);
if (IS_ERR(pages))
@@ -1428,6 +1430,15 @@ struct iopt_pages *iopt_alloc_file_pages(struct file *file,
pages->file = get_file(file);
pages->start = start - start_byte;
pages->type = IOPT_ADDRESS_FILE;
+
+ /*
+ * Get seals from the memfd during mapping to verify that these did not
+ * change before iommufd preservation.
+ */
+ seals = memfd_get_seals(file);
+ if (seals > 0)
+ pages->seals = seals;
+
return pages;
}
diff --git a/include/linux/kho/abi/iommufd.h b/include/linux/kho/abi/iommufd.h
new file mode 100644
index 000000000000..e0c13b965cb9
--- /dev/null
+++ b/include/linux/kho/abi/iommufd.h
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (C) 2026, Google LLC
+ * Author: Samiullah Khawaja <skhawaja@google.com>
+ */
+
+#ifndef _LINUX_KHO_ABI_IOMMUFD_H
+#define _LINUX_KHO_ABI_IOMMUFD_H
+
+#include <linux/mutex_types.h>
+#include <linux/compiler.h>
+#include <linux/types.h>
+
+/**
+ * DOC: IOMMUFD Live Update ABI
+ *
+ * This header defines the ABI for preserving the state of an IOMMUFD file
+ * across a kexec reboot using LUO.
+ *
+ * This interface is a contract. Any modification to any of the serialization
+ * structs defined here constitutes a breaking change. Such changes require
+ * incrementing the version number in the IOMMUFD_LUO_COMPATIBLE string.
+ */
+
+#define IOMMUFD_LUO_COMPATIBLE "iommufd-v1"
+
+/**
+ * struct iommu_hwpt_ser - IOMMUFD HWPT serialized state
+ * @domain_data: Physical address of the serialized state of associated domain
+ * @token: User provided token
+ * @reclaimed: Whether the HWPT is reclaimed
+ */
+struct iommufd_hwpt_ser {
+ u64 domain_data;
+ u64 token;
+ u8 reclaimed;
+ u8 padding[7];
+} __packed;
+
+/**
+ * struct iommu_ser - IOMMUFD serialized state
+ * @nr_hwpts: Number of preserved HWPTs
+ * @hwpt_array: Array of serialized state of preserved HWPTs
+ */
+struct iommufd_ser {
+ u64 nr_hwpts;
+ struct iommufd_hwpt_ser hwpt_array[];
+} __packed;
+
+#endif /* _LINUX_KHO_ABI_IOMMUFD_H */
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 16/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (14 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 15/18] iommufd: Persist iommu hardware pagetables for live update Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 17/18] vfio/pci: Preserve the iommufd state of the " Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 18/18] iommufd/selftest: Add test to verify iommufd preservation Samiullah Khawaja
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Pranjal Shrivastava, Robin Murphy, Kevin Tian,
Alex Williamson, Shuah Khan, iommu, linux-kernel, kvm,
Pratyush Yadav, Pasha Tatashin, David Matlack, Andrew Morton,
Vipin Sharma
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.
Reviewed-by: Pranjal Shrivastava <praan@google.com>
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/iommufd/device.c | 142 ++++++++++++++++++++++++
drivers/iommu/iommufd/iommufd_private.h | 6 +
include/linux/iommufd.h | 29 +++++
3 files changed, 177 insertions(+)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 5c4b06eda546..d4974238ba9a 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);
@@ -1747,3 +1752,140 @@ 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;
+}
+
+/**
+ * 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;
+
+ 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;
+ hwpt_paging = find_hwpt_paging(hwpt);
+ if (!hwpt_paging || !hwpt_paging->liveupdate_preserved) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ ret = liveupdate_get_token_outgoing(s, idev->ictx->file, iommufd_tokenp);
+ if (ret)
+ goto out;
+
+ ret = iommu_preserve_device(hwpt_paging->common.domain,
+ idev->dev,
+ *iommufd_tokenp);
+
+ if (!ret) {
+ igroup->nr_liveupdate_preserved++;
+ 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
+ * @s: Live update session
+ * @idev: Target iommufd device
+ */
+void iommufd_device_unpreserve(struct liveupdate_session *s,
+ 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;
+ hwpt_paging = find_hwpt_paging(hwpt);
+ if (!hwpt_paging || !hwpt_paging->liveupdate_preserved) {
+ WARN(1, "Attached domain is not preserved");
+ goto out;
+ }
+
+ iommu_unpreserve_device(hwpt_paging->common.domain, idev->dev);
+ igroup->nr_liveupdate_preserved--;
+ 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->igroup && idev->igroup->nr_liveupdate_preserved;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_device_is_preserved, "IOMMUFD");
+#endif
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index a4ddc29ec5ae..131544626bf2 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -507,6 +507,9 @@ struct iommufd_group {
struct xarray pasid_attach;
struct iommufd_sw_msi_maps required_sw_msi;
phys_addr_t sw_msi_start;
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ int nr_liveupdate_preserved;
+#endif
};
/*
@@ -524,6 +527,9 @@ struct iommufd_device {
bool enforce_cache_coherency;
struct iommufd_vdevice *vdev;
bool destroying;
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ bool liveupdate_preserved;
+#endif
};
static inline struct iommufd_device *
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index 6e7efe83bc5d..80382aceca18 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>
#include <linux/refcount.h>
#include <linux/types.h>
#include <linux/xarray.h>
@@ -213,6 +214,15 @@ 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 liveupdate_session *s,
+ 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)
{
@@ -397,4 +407,23 @@ 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 0;
+}
+
+static inline void iommufd_device_unpreserve(struct liveupdate_session *s,
+ struct iommufd_device *idev)
+{
+}
+
+static inline bool iommufd_device_is_preserved(struct iommufd_device *idev)
+{
+ return false;
+}
+#endif
#endif
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 17/18] vfio/pci: Preserve the iommufd state of the vfio cdev
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (15 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 16/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 18/18] iommufd/selftest: Add test to verify iommufd preservation Samiullah Khawaja
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, Pranjal Shrivastava, Robin Murphy, Kevin Tian,
Alex Williamson, Shuah Khan, iommu, linux-kernel, kvm,
Pratyush Yadav, Pasha Tatashin, David Matlack, Andrew Morton,
Vipin Sharma
If the vfio cdev is attached to an iommufd, preserve the state of the
attached iommufd also. Basically preserve the iommu specific state of
the device and also the attach iommu HW unit.
Once the device and its iommufd attachment is preserved, it cannot be
detached or attached to another IOAS until it is unpreserved.
Reviewed-by: Pranjal Shrivastava <praan@google.com>
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/vfio/device_cdev.c | 10 +++++++++
drivers/vfio/pci/vfio_pci_liveupdate.c | 28 ++++++++++++++++++++++++--
2 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c
index b818aab92f76..4eb7ffd489bd 100644
--- a/drivers/vfio/device_cdev.c
+++ b/drivers/vfio/device_cdev.c
@@ -284,6 +284,11 @@ int vfio_df_ioctl_attach_pt(struct vfio_device_file *df,
}
mutex_lock(&device->dev_set->lock);
+ if (iommufd_device_is_preserved(device->iommufd_device)) {
+ ret = -EBUSY;
+ goto out_unlock;
+ }
+
if (attach.flags & VFIO_DEVICE_ATTACH_PASID)
ret = device->ops->pasid_attach_ioas(device,
attach.pasid,
@@ -342,6 +347,11 @@ int vfio_df_ioctl_detach_pt(struct vfio_device_file *df,
}
mutex_lock(&device->dev_set->lock);
+ if (iommufd_device_is_preserved(device->iommufd_device)) {
+ mutex_unlock(&device->dev_set->lock);
+ return -EBUSY;
+ }
+
if (detach.flags & VFIO_DEVICE_DETACH_PASID)
device->ops->pasid_detach_ioas(device, detach.pasid);
else
diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio_pci_liveupdate.c
index f0ea37d98696..fb2141ee8942 100644
--- a/drivers/vfio/pci/vfio_pci_liveupdate.c
+++ b/drivers/vfio/pci/vfio_pci_liveupdate.c
@@ -106,6 +106,7 @@
#include <linux/errno.h>
#include <linux/file.h>
+#include <linux/iommufd.h>
#include <linux/kexec_handover.h>
#include <linux/kho/abi/vfio_pci.h>
#include <linux/liveupdate.h>
@@ -114,6 +115,8 @@
#include "vfio_pci_priv.h"
+MODULE_IMPORT_NS("IOMMUFD");
+
static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handler *handler,
struct file *file)
{
@@ -154,15 +157,25 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
struct vfio_pci_core_device_ser *ser;
struct vfio_pci_core_device *vdev;
struct pci_dev *pdev;
- int ret;
+ u64 iommufd_token;
+ int ret = 0;
vdev = container_of(device, struct vfio_pci_core_device, vdev);
pdev = vdev->pdev;
- ret = pci_liveupdate_preserve(pdev);
+ mutex_lock(&device->dev_set->lock);
+ ret = iommufd_device_preserve(args->session,
+ device->iommufd_device,
+ &iommufd_token);
+ mutex_unlock(&device->dev_set->lock);
+
if (ret)
return ret;
+ ret = pci_liveupdate_preserve(pdev);
+ if (ret)
+ goto err_iommufd_unpreserve;
+
ser = kho_alloc_preserve(sizeof(*ser));
if (IS_ERR(ser)) {
ret = PTR_ERR(ser);
@@ -177,6 +190,12 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
err_unpreserve:
pci_liveupdate_unpreserve(pdev);
+
+err_iommufd_unpreserve:
+ mutex_lock(&device->dev_set->lock);
+ iommufd_device_unpreserve(args->session,
+ device->iommufd_device);
+ mutex_unlock(&device->dev_set->lock);
return ret;
}
@@ -184,6 +203,11 @@ static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args)
{
struct vfio_device *device = vfio_device_from_file(args->file);
+ mutex_lock(&device->dev_set->lock);
+ iommufd_device_unpreserve(args->session,
+ device->iommufd_device);
+ mutex_unlock(&device->dev_set->lock);
+
pci_liveupdate_unpreserve(to_pci_dev(device->dev));
kho_unpreserve_free(phys_to_virt(args->serialized_data));
}
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v5 18/18] iommufd/selftest: Add test to verify iommufd preservation
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
` (16 preceding siblings ...)
2026-09-21 0:48 ` [PATCH v5 17/18] vfio/pci: Preserve the iommufd state of the " Samiullah Khawaja
@ 2026-09-21 0:48 ` Samiullah Khawaja
17 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-21 0:48 UTC (permalink / raw)
To: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon, Jason Gunthorpe
Cc: Samiullah Khawaja, YiFei Zhu, Robin Murphy, Kevin Tian,
Alex Williamson, Shuah Khan, iommu, linux-kernel, kvm,
Pratyush Yadav, Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma
Test iommufd preservation by setting up an iommufd and vfio cdev and
preserve it across live update. Test takes VFIO cdev path of a device
bound to vfio-pci driver and binds it to an iommufd being preserved. It
also preserves the vfio cdev so the iommufd state associated with it is
also preserved.
The restore path is tested by restoring the preserved vfio cdev only. On
restore, test verifies that the bind with a new iommufd fails as the
device is attached to the restored IOMMU domain. Also the LUO session
finish fails as the preserved iommufd is not restored.
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
Signed-off-by: YiFei Zhu <zhuyifei@google.com>
---
tools/testing/selftests/iommu/Makefile | 12 +
.../iommu/iommufd_liveupdate_kexec_test.c | 241 ++++++++++++++++++
2 files changed, 253 insertions(+)
create mode 100644 tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c
diff --git a/tools/testing/selftests/iommu/Makefile b/tools/testing/selftests/iommu/Makefile
index 84abeb2f0949..ab35e8b21580 100644
--- a/tools/testing/selftests/iommu/Makefile
+++ b/tools/testing/selftests/iommu/Makefile
@@ -7,4 +7,16 @@ TEST_GEN_PROGS :=
TEST_GEN_PROGS += iommufd
TEST_GEN_PROGS += iommufd_fail_nth
+TEST_GEN_PROGS_EXTENDED += iommufd_liveupdate_kexec_test
+
include ../lib.mk
+include ../liveupdate/lib/libliveupdate.mk
+
+CFLAGS += -I$(top_srcdir)/tools/include
+CFLAGS += -MD
+CFLAGS += $(EXTRA_CFLAGS)
+
+$(TEST_GEN_PROGS_EXTENDED): %: %.o $(LIBLIVEUPDATE_O)
+ $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBLIVEUPDATE_O) $(LDLIBS) -o $@
+
+EXTRA_CLEAN += $(LIBLIVEUPDATE_O)
diff --git a/tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c b/tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c
new file mode 100644
index 000000000000..26c7e15b6187
--- /dev/null
+++ b/tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c
@@ -0,0 +1,241 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright (c) 2026, Google LLC.
+ * Samiullah Khawaja <skhawaja@google.com>
+ */
+
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <limits.h>
+
+#define __EXPORTED_HEADERS__
+#include <linux/iommufd.h>
+#include <linux/types.h>
+#include <linux/vfio.h>
+#include <linux/sizes.h>
+#include <libliveupdate.h>
+
+#include "../kselftest.h"
+
+#define ksft_assert(condition) \
+ do { \
+ if (!(condition)) \
+ fail_exit("Failed: %s", #condition); \
+ } while (0)
+
+static const char *device_cdev_path;
+static char state_session[LIVEUPDATE_SESSION_NAME_LENGTH];
+static char iommufd_session[LIVEUPDATE_SESSION_NAME_LENGTH];
+
+static const uint64_t STATE_TOKEN;
+static const uint64_t IOMMUFD_TOKEN = 0x123456;
+static const uint64_t CDEV_TOKEN = 0x654321;
+static const uint64_t HWPT_TOKEN = 0x789012;
+static const uint64_t MEMFD_TOKEN = 0x890123;
+
+static int open_cdev(const char *vfio_cdev_path)
+{
+ int cdev_fd;
+
+ cdev_fd = open(vfio_cdev_path, O_RDWR);
+ if (cdev_fd < 0)
+ ksft_exit_skip("Failed to open VFIO cdev: %s\n", vfio_cdev_path);
+
+ return cdev_fd;
+}
+
+static int open_iommufd(void)
+{
+ int iommufd;
+
+ iommufd = open("/dev/iommu", O_RDWR);
+ if (iommufd < 0)
+ ksft_exit_skip("Failed to open /dev/iommu. IOMMUFD support not enabled.\n");
+
+ return iommufd;
+}
+
+static int create_sealed_memfd(size_t size)
+{
+ int fd, ret;
+
+ fd = memfd_create("buffer", MFD_ALLOW_SEALING);
+ if (fd < 0)
+ fail_exit("memfd_create failed");
+
+ ret = ftruncate(fd, size);
+ if (ret)
+ fail_exit("ftruncate failed");
+
+ ret = fcntl(fd, F_ADD_SEALS, F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL);
+ if (ret)
+ fail_exit("fcntl F_ADD_SEALS failed");
+
+ return fd;
+}
+
+#define test_ioctl(fd, cmd, arg) \
+ do { \
+ if (ioctl(fd, cmd, arg)) \
+ fail_exit("ioctl(%s) failed", #cmd); \
+ } while (0)
+
+#define test_luo_session_preserve_fd(session, fd, token) \
+ do { \
+ if (luo_session_preserve_fd(session, fd, token)) \
+ fail_exit("luo_session_preserve_fd(%s) failed", #token); \
+ } while (0)
+
+#define test_luo_session_retrieve_fd(session, token) \
+ ({ \
+ int _fd = luo_session_retrieve_fd(session, token); \
+ if (_fd < 0) \
+ fail_exit("luo_session_retrieve_fd(%s) failed", #token); \
+ _fd; \
+ })
+
+static void setup_iommufd(int iommufd, int memfd, int cdev_fd)
+{
+ struct vfio_device_bind_iommufd bind = {
+ .argsz = sizeof(bind),
+ .flags = 0,
+ .iommufd = iommufd,
+ };
+ struct iommu_ioas_alloc alloc_data = {
+ .size = sizeof(alloc_data),
+ .flags = 0,
+ };
+ struct iommu_hwpt_alloc hwpt_alloc = {
+ .size = sizeof(hwpt_alloc),
+ .flags = 0,
+ };
+ struct vfio_device_attach_iommufd_pt attach_data = {
+ .argsz = sizeof(attach_data),
+ .flags = 0,
+ };
+ struct iommu_hwpt_liveupdate_mark_preserve mark_preserve = {
+ .size = sizeof(mark_preserve),
+ .hwpt_token = HWPT_TOKEN,
+ };
+ struct iommu_ioas_map_file map_file = {
+ .size = sizeof(map_file),
+ .length = SZ_1M,
+ .flags = IOMMU_IOAS_MAP_WRITEABLE |
+ IOMMU_IOAS_MAP_READABLE |
+ IOMMU_IOAS_MAP_FIXED_IOVA,
+ .iova = SZ_4G,
+ .fd = memfd,
+ .start = 0,
+ };
+
+ test_ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind);
+
+ test_ioctl(iommufd, IOMMU_IOAS_ALLOC, &alloc_data);
+
+ hwpt_alloc.dev_id = bind.out_devid;
+ hwpt_alloc.pt_id = alloc_data.out_ioas_id;
+ test_ioctl(iommufd, IOMMU_HWPT_ALLOC, &hwpt_alloc);
+
+ attach_data.pt_id = hwpt_alloc.out_hwpt_id;
+ test_ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data);
+
+ map_file.ioas_id = alloc_data.out_ioas_id;
+ test_ioctl(iommufd, IOMMU_IOAS_MAP_FILE, &map_file);
+
+ mark_preserve.hwpt_id = attach_data.pt_id;
+ test_ioctl(iommufd, IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE, &mark_preserve);
+}
+
+static void before_kexec(int luo_fd)
+{
+ int iommufd, cdev_fd, memfd, session;
+
+ create_state_file(luo_fd, state_session, STATE_TOKEN, /*next_stage=*/2);
+
+ session = luo_create_session(luo_fd, iommufd_session);
+ if (session < 0)
+ fail_exit("luo_create_session failed");
+
+ iommufd = open_iommufd();
+ memfd = create_sealed_memfd(SZ_1M);
+ cdev_fd = open_cdev(device_cdev_path);
+
+ setup_iommufd(iommufd, memfd, cdev_fd);
+
+ /* Cannot preserve cdev without iommufd */
+ if (!luo_session_preserve_fd(session, cdev_fd, CDEV_TOKEN))
+ fail_exit("Preserving cdev without iommufd should fail");
+
+ /* Cannot preserve iommufd without preserving memfd. */
+ if (!luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN))
+ fail_exit("Preserving iommufd without memfd should fail");
+
+ test_luo_session_preserve_fd(session, memfd, MEMFD_TOKEN);
+ test_luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN);
+ test_luo_session_preserve_fd(session, cdev_fd, CDEV_TOKEN);
+
+ close(session);
+ session = luo_create_session(luo_fd, iommufd_session);
+ if (session < 0)
+ fail_exit("luo_create_session failed");
+
+ test_luo_session_preserve_fd(session, memfd, MEMFD_TOKEN);
+ test_luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN);
+ test_luo_session_preserve_fd(session, cdev_fd, CDEV_TOKEN);
+
+ close(luo_fd);
+ daemonize_and_wait();
+}
+
+static void after_kexec(int luo_fd, int state_session_fd)
+{
+ int iommufd, cdev_fd, session, stage;
+ struct vfio_device_bind_iommufd bind = {
+ .argsz = sizeof(bind),
+ .flags = 0,
+ };
+
+ restore_and_read_stage(state_session_fd, STATE_TOKEN, &stage);
+ ksft_assert(stage == 2);
+
+ session = luo_retrieve_session(luo_fd, iommufd_session);
+ if (session < 0)
+ fail_exit("luo_retrieve_session failed");
+
+ cdev_fd = test_luo_session_retrieve_fd(session, CDEV_TOKEN);
+
+ iommufd = luo_session_retrieve_fd(session, IOMMUFD_TOKEN);
+ if (iommufd >= 0)
+ fail_exit("iommufd should not be retrievable yet");
+
+ iommufd = open_iommufd();
+
+ bind.iommufd = iommufd;
+ if (ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind) == 0 || errno != EPERM)
+ fail_exit("Binding cdev to new iommufd should fail with EPERM");
+
+ /* Should fail */
+ if (luo_session_finish(session) == 0)
+ fail_exit("luo_session_finish should fail if iommufd is not restored");
+
+ close(iommufd);
+ close(cdev_fd);
+}
+
+int main(int argc, char *argv[])
+{
+ if (argc < 2) {
+ printf("Usage: %s <vfio_cdev_path>\n", argv[0]);
+ return 1;
+ }
+
+ device_cdev_path = argv[1];
+ sprintf(iommufd_session, "iommufd-test-%s", "cdev");
+ sprintf(state_session, "state-%s", "iommufd-cdev");
+
+ return luo_test(argc, argv, state_session, before_kexec, after_kexec);
+}
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v5 15/18] iommufd: Persist iommu hardware pagetables for live update
2026-09-21 0:48 ` [PATCH v5 15/18] iommufd: Persist iommu hardware pagetables for live update Samiullah Khawaja
@ 2026-09-23 23:59 ` John Starks
2026-09-24 17:49 ` Samiullah Khawaja
0 siblings, 1 reply; 21+ messages in thread
From: John Starks @ 2026-09-23 23:59 UTC (permalink / raw)
To: Samiullah Khawaja
Cc: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon,
Jason Gunthorpe, YiFei Zhu, Robin Murphy, Kevin Tian,
Alex Williamson, Shuah Khan, iommu, linux-kernel, kvm,
Pratyush Yadav, Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma, John Starks
On Mon, Sep 21, 2026 at 12:48:31AM +0000, Samiullah Khawaja wrote:
> From: YiFei Zhu <zhuyifei@google.com>
> +
> + /*
> + * When this memory file was mapped it should be sealed and seal
> + * should be sealed. This means that since mapping was done the
> + * memory file was not grown or shrink and the pages being used
> + * until now remain pinned and preserved.
> + */
> + if ((pages->seals & req_seals) != req_seals) {
> + ret = -EINVAL;
> + break;
> + }
> +
Is this sealing business sufficient? Even with the specified
seals, user mode can still punch a hole in the memfd after it
has been mapped. This will disassociate those pages from the memfd
but leave them referenced by the HWPT.
Then, when the memfd gets preserved, the hole will be filled
by a new set of pages, and only those pages that will be
preserved across the kexec. The original set, unless I'm missing
something, will be dangling in the HWPT, allowing attached
devices to DMA to the wrong memory.
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v5 15/18] iommufd: Persist iommu hardware pagetables for live update
2026-09-23 23:59 ` John Starks
@ 2026-09-24 17:49 ` Samiullah Khawaja
0 siblings, 0 replies; 21+ messages in thread
From: Samiullah Khawaja @ 2026-09-24 17:49 UTC (permalink / raw)
To: John Starks
Cc: David Woodhouse, Lu Baolu, Joerg Roedel, Will Deacon,
Jason Gunthorpe, YiFei Zhu, Robin Murphy, Kevin Tian,
Alex Williamson, Shuah Khan, iommu, linux-kernel, kvm,
Pratyush Yadav, Pasha Tatashin, David Matlack, Andrew Morton,
Pranjal Shrivastava, Vipin Sharma, John Starks
On Wed, Sep 23, 2026 at 04:59:30PM -0700, John Starks wrote:
>On Mon, Sep 21, 2026 at 12:48:31AM +0000, Samiullah Khawaja wrote:
>> From: YiFei Zhu <zhuyifei@google.com>
>> +
>> + /*
>> + * When this memory file was mapped it should be sealed and seal
>> + * should be sealed. This means that since mapping was done the
>> + * memory file was not grown or shrink and the pages being used
>> + * until now remain pinned and preserved.
>> + */
>> + if ((pages->seals & req_seals) != req_seals) {
>> + ret = -EINVAL;
>> + break;
>> + }
>> +
>
>Is this sealing business sufficient? Even with the specified
>seals, user mode can still punch a hole in the memfd after it
>has been mapped. This will disassociate those pages from the memfd
>but leave them referenced by the HWPT.
Nice catch. Since punch hole doesn't change size, the shrink/grow seals
will not stop that from happening.
Once the memfd is preserved it is frozen, so punch hole is not allowed
after that. To catch the punch hole between map and iommufd preserve, we
need a truncate count on the memfd that can be checked here. Or we can
iterate through the iopt pages and verify that they are still associated
with the preserved memfd, similar to how memfd preserve already loops
through all the pages. I am inclined towards the iterative solution as
it handles all the future cases.
I will fix this in the next revision.
Thanks,
Sami
>
>Then, when the memfd gets preserved, the hole will be filled
>by a new set of pages, and only those pages that will be
>preserved across the kexec. The original set, unless I'm missing
>something, will be dangling in the HWPT, allowing attached
>devices to DMA to the wrong memory.
^ permalink raw reply [flat|nested] 21+ messages in thread
end of thread, other threads:[~2026-09-24 17:49 UTC | newest]
Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-21 0:48 [PATCH v5 00/18] iommu: Add live update state preservation Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 01/18] memfd: export memfd_get_seals() Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 02/18] iommu: Implement IOMMU Live update FLB callbacks Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 03/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 04/18] iommupt: Implement preserve/unpreserve/restore callbacks Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 05/18] iommu: Implement IOMMU domain preservation Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 06/18] iommu: Implement device and IOMMU HW preservation Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 08/18] iommu/vt-d: Clear unpreserved context entries during shutdown Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 09/18] iommu: Add APIs to get iommu and device preserved state Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 10/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 11/18] iommu: Restore and reattach preserved domains to devices Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 12/18] iommu/vt-d: Handle reattach of the restored domain Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 13/18] iommu/vt-d: Preserve PASID table of preserved device Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 14/18] iommufd: Implement ioctl to mark HWPT for preservation Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 15/18] iommufd: Persist iommu hardware pagetables for live update Samiullah Khawaja
2026-09-23 23:59 ` John Starks
2026-09-24 17:49 ` Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 16/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 17/18] vfio/pci: Preserve the iommufd state of the " Samiullah Khawaja
2026-09-21 0:48 ` [PATCH v5 18/18] iommufd/selftest: Add test to verify iommufd preservation Samiullah Khawaja
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®