iommu: Add live update state preservation
Subsystems (7)
v8 cover iommu: Add live update state preservation
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 series is based on linux-next (next-20261120), which already has the PCI base device preservation series and the LUO internal API to get incoming/outgoing files.
The dependency on the VFIO cdev preservation series is removed in this version. The patch that triggered the preservation of the iommufd state of a vfio cdev is dropped, so this series can be applied and reviewed on its own and merged without the VFIO series. Once the VFIO series is ready, an extra patch glues it with the iommufd preservation. The series with the extra patches is available at the following Github location:
https://github.com/samikhawaja/linux/tree/iommu/phase1-v8-vfio
Also note that patch 01 is taken from the VFIO cdev preservation series. It exports the LUO file handler registration functions, which iommufd also needs as it can be built as a module. It will be dropped from this series once that patch is merged separately.
The kernel tree with all dependencies is uploaded to the following Github location:
https://github.com/samikhawaja/linux/tree/iommu/phase1-v8
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 preserved already.
- 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 driver 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 iommufd_liveupdate_kexec_test selftest was used to verify the preservation logic. Since vfio cdev preservation is not part of this series, the test uses a new iommufd selftest op to trigger the device preservation. So the kernel needs CONFIG_IOMMUFD_TEST. It was tested using QEMU with virtual IOMMU (VT-d) support, in both legacy and scalable mode, with virtio pcie device bound to the vfio-pci driver. The next kernel was also booted with intel_iommu=off to verify that VT-d is forced on.
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, 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 restored device cannot be bound to a new iommufd, that the iommufd cannot be retrieved, as the iommufd restore is not supported yet, and that 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/[email protected]/
- 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:
The preserve callbacks of the iommufd and vfio file handlers run when userspace preserves the FDs into a live update session.
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:
v8:
- Drop the dependency on the VFIO preservation series.
- Add "liveupdate: Export symbols needed by modules".
- Reserve the iommu group of a preserved device for live update with iommu_group_reserve_liveupdate().
- Force VT-d on if the previous kernel preserved any IOMMU state.
- Find the stage of the restored domain by reading the RID_PASID entry of the restored device.
- Drop the no-op restored device checks around ATS/PRI in VT-d probe and release.
- Keep the format specific features in the iommupt restore KUnit test.
- Add the missing kernel-docs.
- Update the selftest to preserve the device using a new iommufd selftest op instead of preserving the vfio cdev.
- Check all devices in a group when restoring a preserved device.
- Collect Reviewed-by tags.
v7:
- Rebased on top of iommu/next (v7.4-rc1), PCI preservation v10 and VFIO preservation v6.
- Use EXPORT_SYMBOL_GPL for all exported symbols.
- Replace iommu_group_is_singleton() with iommu_group_device_count().
- Check in iommu_preserve_device() that the domain is attached to the device, and drop the unused domain argument of iommu_unpreserve_device().
- Drop iommu_for_each_dev(). The VT-d driver now walks the PCI devices and the ACPI namespace device scope of the DMAR unit to clear the context entries of unpreserved devices during shutdown.
- Disable the invalidation queue and mask the fault and page request interrupts of preserved IOMMUs during shutdown.
- Rename iommu_get_preserved_data(), iommu_for_each_preserved_device() and iommu_init_device_preserved_data() to iommu_get_restored_state(), iommu_for_each_restored_device() and iommu_init_device_restored_data().
- Panic if the hardware translation state does not match.
- WARN if the restored domain ID does not match.
- Track preserved devices using idev->liveupdate_preserved.
- Collect Reviewed-by tags.
v6:
- Add size, features and reserved fields to the KHO ABI to manage compatibility.
- Use KHO block for preservation of KHO data structures.
- Drop "memfd: export memfd_get_seals()" and the memfd seal requirement as it is not reliable because memfd can still have holes punched after sealing.
- Split the disable_dmar_iommu()/free_dmar_iommu() merge into a separate patch.
- Move the PASID table preservation patch before the domain reattach patch.
- Move iommu_phys from the device attachment into iommu_device_ser.
- Rename vasz to max_vasz_lg2, and IOMMU_INVALID/IOMMU_INTEL to IOMMU_SER_TYPE_INVALID/IOMMU_SER_TYPE_INTEL.
- Check the FLB version in retrieve before restoring the serialized objects.
- Use _ser suffix consistently for serialized objects.
- Rework iommu_for_each_dev() to remove redundant arguments.
- Sanity check scalable mode changes across kexec.
- Use context_tables_bitmap as the source of truth during restore.
- Reuse reserve_domain_id() to reclaim the preserved domain IDs.
- Drop the IOMMU restored flag and panic if DRHD unit restore fails.
- Rename intel_iommu_restore_device() to intel_iommu_attach_restored_device(), and the domain reattach/detach helpers to domain_attach_restored_iommu()/domain_detach_restored_iommu().
- Drop the cache tag and IOPF setup for the restored domain.
- Rename nr_preserved_domains, nr_hwpts and nr_liveupdate_preserved to num_preserved_domains, num_hwpts and num_liveupdate_preserved.
- Update the selftest to use an unsealed memfd and verify that a hole-punched mapping is rejected.
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.
v7: https://lore.kernel.org/all/[email protected]/
v6: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/kvm/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
rfcv2: https://lore.kernel.org/all/[email protected]/
rfcv1: https://lore.kernel.org/all/[email protected]/
Samiullah Khawaja (15):
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 restored state iommu/vt-d: Combine disable_dmar_iommu() and free_dmar_iommu() iommu/vt-d: Restore IOMMU state and reclaimed domain ids iommu/vt-d: Preserve PASID table of preserved device
iommu: Restore and reattach preserved domains to devices iommu/vt-d: Handle reattach of the restored domain
iommufd: Add APIs to preserve/unpreserve a vfio cdev iommufd/selftest: Add test to verify iommufd preservation
Vipin Sharma (1):
liveupdate: Export symbols needed by modules
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 | 155 ++++ drivers/iommu/generic_pt/kunit_iommu_pt.h | 33 + drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 179 +++- drivers/iommu/intel/iommu.h | 52 ++ drivers/iommu/intel/liveupdate.c | 855 ++++++++++++++++++ drivers/iommu/intel/pasid.c | 10 +- drivers/iommu/intel/pasid.h | 8 + drivers/iommu/iommu-pages.c | 109 ++- drivers/iommu/iommu-pages.h | 30 + drivers/iommu/iommu-priv.h | 3 + drivers/iommu/iommu.c | 204 ++++- drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/device.c | 169 ++++ drivers/iommu/iommufd/io_pagetable.c | 24 + drivers/iommu/iommufd/iommufd_private.h | 37 + drivers/iommu/iommufd/iommufd_test.h | 1 + drivers/iommu/iommufd/liveupdate.c | 445 +++++++++ drivers/iommu/iommufd/main.c | 19 +- drivers/iommu/iommufd/selftest.c | 29 + drivers/iommu/liveupdate.c | 739 +++++++++++++++ include/linux/generic_pt/iommu.h | 30 + include/linux/iommu-liveupdate.h | 198 ++++ include/linux/iommu.h | 26 + include/linux/iommufd.h | 27 + include/linux/kho/abi/iommu.h | 238 +++++ include/linux/kho/abi/iommufd.h | 56 ++ include/uapi/linux/iommufd.h | 31 + kernel/liveupdate/luo_file.c | 2 + tools/testing/selftests/iommu/Makefile | 21 + .../iommu/iommufd_liveupdate_kexec_test.c | 276 ++++++ 34 files changed, 3987 insertions(+), 53 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: 909ef7a9a30fa139e69e555e3606680f5cca7726
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v8 1/18 liveupdate: Export symbols needed by modules
From: Vipin Sharma <[email protected]>
Export liveupdate_register_file_handler() and liveupdate_unregister_file_handler() so live update file handlers can be registered from modules. iommufd uses them in a subsequent commit, and it can be built as a module.
--- kernel/liveupdate/luo_file.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/liveupdate/luo_file.c +2 -0
@@ -931,6 +931,7 @@ int liveupdate_register_file_handler(struct liveupdate_file_handler *fh)up_write (&luo_register_rwlock);return err; } +EXPORT_SYMBOL_GPL (liveupdate_register_file_handler);/** * liveupdate_unregister_file_handler - Unregister a liveupdate file handler
@@ -948,6 +949,7 @@ void liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh)luo_flb_unregister_all (fh);list_del (&ACCESS_PRIVATE (fh, list)); } +EXPORT_SYMBOL_GPL (liveupdate_unregister_file_handler);/** * liveupdate_get_token_outgoing - Get the token for a preserved file.
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v8 01/18] liveupdate: Export symbols needed by modules
Quoted 13 lines · Samiullah Khawaja
From: Vipin Sharma<[email protected]>
Export
liveupdate_register_file_handler()andliveupdate_unregister_file_handler()so live update file handlers can be registered from modules. iommufd uses them in a subsequent commit, and it can be built as a module.Reviewed-by Samiullah Khawaja<[email protected]>Reviewed-by Pranjal Shrivastava<[email protected]>Signed-off-by David Matlack<[email protected]>Signed-off-by Vipin Sharma<[email protected]>[skhawaja: reworded the commit message for iommufd]Signed-off-by Samiullah Khawaja<[email protected]>
Quoted 3 lines
--- kernel/liveupdate/luo_file.c | 2 ++ 1 file changed, 2 insertions(+)
PATCH v8 2/18 iommu: Implement IOMMU Live update FLB callbacks
In reply to Samiullah KhawajaAdd 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.
--- MAINTAINERS | 9 ++ drivers/iommu/Kconfig | 12 ++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 241 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 28 ++++ include/linux/kho/abi/iommu.h | 190 ++++++++++++++++++++++++ 6 files changed, 481 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
MAINTAINERS +9 -0
@@ -14139,6 +14139,15 @@ F: include/linux/iova.h F: include/linux/of_iommu.h F: rust/kernel/iommu/ +IOMMU LIVEUPDATE +M: Samiullah Khawaja <[email protected]> +R: Pranjal Shrivastava <[email protected]> +L: [email protected] +S: Maintained +F: drivers/iommu/liveupdate.c +F: include/linux/iommu-liveupdate.h +F: include/linux/kho/abi/iommu.h + IOMMUFD M: Jason Gunthorpe <[email protected]> M: Kevin Tian <[email protected]>
drivers/iommu/Kconfig +12 -0
@@ -421,6 +421,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"
drivers/iommu/Makefile +1 -0
@@ -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
drivers/iommu/liveupdate.c new file +241 -0
241 lines not on this page · raw message
include/linux/iommu-liveupdate.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#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 */
include/linux/kho/abi/iommu.h new file +190 -0
190 lines not on this page · raw message
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v8 02/18] iommu: Implement IOMMU Live update FLB callbacks
Quoted 8 lines · Samiullah Khawaja
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 <[email protected]>
Quoted 547 lines
--- MAINTAINERS | 9 ++ drivers/iommu/Kconfig | 12 ++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 241 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 28 ++++ include/linux/kho/abi/iommu.h | 190 ++++++++++++++++++++++++ 6 files changed, 481 insertions(+) create mode 100644 drivers/iommu/liveupdate.c create mode 100644 include/linux/iommu-liveupdate.h create mode 100644 include/linux/kho/abi/iommu.hMAINTAINERS +9
-0@@ -14139,6 +14139,15 @@ F: include/linux/iova.h F: include/linux/of_iommu.h F: rust/kernel/iommu/ +IOMMU LIVEUPDATE +M: Samiullah Khawaja <[email protected]> +R: Pranjal Shrivastava <[email protected]> +L: [email protected] +S: Maintained +F: drivers/iommu/liveupdate.c +F: include/linux/iommu-liveupdate.h +F: include/linux/kho/abi/iommu.h + IOMMUFD M: Jason Gunthorpe <[email protected]> M: Kevin Tian <[email protected]>drivers/iommu/Kconfig +12
-0@@ -421,6 +421,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"drivers/iommu/Makefile +1
-0@@ -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.odrivers/iommu/liveupdate.c new file +241
-0@@ -0,0 +1,241 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +/** +* 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/kho_block.h> +#include <linux/liveupdate.h> + +/** +* struct iommu_ser_set - Set of serialized objects of one type +* @bs: KHO block set holding the serialized objects +* @it: Iterator used to reserve new objects at the end of @bs +* @count: Number of reserved objects, including the deleted ones +* @head_pa: Field of struct iommu_flb_ser that records the head of @bs +*/ +struct iommu_ser_set { +struct kho_block_set bs; +struct kho_block_set_it it; +u64 count; +u64 *head_pa; +}; + +struct iommu_flb_obj { +/* Protects the serialized state and the object sets */ +struct mutex lock; +struct iommu_flb_ser *ser; + +struct iommu_ser_set iommu_set; +struct iommu_ser_set domain_set; +struct iommu_ser_set device_set; +}; + +/* +* Restore a set of serialized objects from the previous kernel. The objects +* are read using the size recorded by the previous kernel in their header. +* +* 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. +*/ +static void iommu_ser_set_restore (struct iommu_ser_set *set,u64 head_pa) +{ +size_t entry_size =sizeof (struct iommu_hdr_ser ); + +if (head_pa) { +struct kho_block_header_ser *block =phys_to_virt (head_pa); +struct iommu_hdr_ser *hdr = (void *)(block +1 ); + + entry_size = hdr->size; + } + +if (entry_size <sizeof (struct iommu_hdr_ser ) || + entry_size >KHO_BLOCK_SIZE -sizeof (struct kho_block_header_ser )) + goto err_fatal; + +kho_block_set_init (&set->bs, entry_size); +if (kho_block_set_restore (&set->bs, head_pa)) + goto err_fatal; + +return ; + +err_fatal: +panic ("Failed to restore IOMMU Live Update FLB\n" ); +} + +static void iommu_liveupdate_flb_free (struct iommu_flb_obj *obj) +{ +kho_block_set_destroy (&obj->domain_set.bs); +kho_block_set_destroy (&obj->device_set.bs); +kho_block_set_destroy (&obj->iommu_set.bs); + +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; + +/* obj exists only in the current kernel to track preserved state */ + obj =kzalloc_obj (*obj,GFP_KERNEL ); +if (!obj) +return -ENOMEM ; + +/* ser is allocated via KHO and will survive the kexec */ + ser =kho_alloc_preserve (sizeof (*ser)); +if (IS_ERR (ser)) { +kfree (obj); +return PTR_ERR (ser); + } + +mutex_init (&obj->lock); + obj->ser = ser; + ser->version =IOMMU_LUO_FLB_VERSION ; + +kho_block_set_init (&obj->domain_set.bs,sizeof (struct iommu_domain_ser )); + obj->domain_set.head_pa = &ser->domain_block_phys; +kho_block_set_init (&obj->device_set.bs,sizeof (struct iommu_device_ser )); + obj->device_set.head_pa = &ser->device_block_phys; +kho_block_set_init (&obj->iommu_set.bs,sizeof (struct iommu_hw_ser )); + obj->iommu_set.head_pa = &ser->iommu_block_phys; + + argp->obj = obj; + argp->data =virt_to_phys (ser); +return 0 ; +} + +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; + +kho_block_set_destroy (&obj->domain_set.bs); +kho_block_set_destroy (&obj->device_set.bs); +kho_block_set_destroy (&obj->iommu_set.bs); + +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 */ +if (!kho_restore_folio (argp->data)) +panic ("Failed to restore IOMMU Live Update FLB\n" ); + + ser =phys_to_virt (argp->data); + +/* +* FLB version mismatch is considered fatal for security reasons for +* now. Check it before restoring the arrays, as their layout depends +* on the version. +*/ +if (ser->version !=IOMMU_LUO_FLB_VERSION ) +panic ("Incoming IOMMU FLB version (v%llu) is incompatible with this kernel (v%d)\n" , + ser->version,IOMMU_LUO_FLB_VERSION ); + +mutex_init (&obj->lock); + obj->ser = ser; + +iommu_ser_set_restore (&obj->domain_set, ser->domain_block_phys); +iommu_ser_set_restore (&obj->device_set, ser->device_block_phys); +iommu_ser_set_restore (&obj->iommu_set, ser->iommu_block_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_GPL (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_GPL (iommu_liveupdate_unregister_flb);include/linux/iommu-liveupdate.h new file +28
-0@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#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 */ include/linux/kho/abi/iommu.h new file +190
-0@@ -0,0 +1,190 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#ifndef _LINUX_KHO_ABI_IOMMU_H +#define _LINUX_KHO_ABI_IOMMU_H + +#include <linux/mutex_types.h> +#include <linux/bits.h> +#include <linux/compiler.h> +#include <linux/kho/abi/block.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 set of KHO +* serialization blocks (see include/linux/kho/abi/block.h). Each block starts +* with a struct kho_block_header_ser, followed by the serialized objects of +* the respective type. Blocks are linked through the next field of the header. +* +* Top Level (struct iommu_flb_ser): +* +---------------------------+ +* | - iommu_block_phys | +* | - domain_block_phys | +* | - device_block_phys | +* +---------------------------+ +* +* For example see below the representation of the domain blocks. +* +* +---------------------------+ +---------------------------+ +* | kho_block_header_ser |-->| kho_block_header_ser |--> NULL +* | - next | | - next | +* | - count | | - count | +* | | | | +* | [ iommu_domain_ser ] | | [ iommu_domain_ser ] | +* | [ iommu_domain_ser ] | | [ iommu_domain_ser ] | +* | ... | | ... | +* +---------------------------+ +---------------------------+ +* +* Each object in a block starts with a common header (iommu_hdr_ser). The +* size in the header is the size of each object in the block, so the next +* kernel can read the objects even if their size has changed. +* 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_SER_TYPE_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_SER_TYPE_INVALID , +}; + +#define IOMMU_SER_FLAG_DELETED BIT(0) +#define IOMMU_SER_FLAG_INCOMING BIT(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 +* @size: Size of the serialized object +* @features: Bitmask of object specific features used by the preserved state +*/ +struct iommu_hdr_ser { +u32 ref_count; +u32 flags; +u32 size; +u32 features; +} __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 +* @max_vasz_lg2: Maximum number of bits the VA can contain +* @sign_extend: FEAT_SIGN_EXTEND is enabled for this domain +* @padding: Padding +* @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 max_vasz_lg2; +u8 sign_extend; +u8 padding[3 ]; +struct iommu_domain *restored_domain; +} __packed; + +/** +* struct iommu_attachment_ser - Serialized attachment between device and domain. +* @attachment_id: ID of the attachment between device and domain. +* @domain_phys: Physical address of the domain +* @pasid: PASID of the attachment +* @padding: Padding +*/ +struct iommu_attachment_ser { +u64 attachment_id; +u64 domain_phys; +u32 pasid; +u32 padding; +} __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 +* @iommu_phys: Physical address of the serialized IOMMU the device is behind +* @attachment: Attachment of the device to its domain +* @reserved: Reserved for future common fields. +*/ +struct iommu_device_ser { +struct iommu_hdr_ser hdr; +u32 devid; +u32 pci_domain_nr; +u64 dma_owner_token; +u64 iommu_phys; +struct iommu_attachment_ser attachment; +u64 reserved[2 ]; +} __packed; + +/** +* struct iommu_hw_ser - Serialized state of an IOMMU instance +* @hdr: Common object header +* @token: Unique token to identify the IOMMU instance +* @type: IOMMU type serialized state belongs to +* @reserved: Reserved for future common fields. +*/ +struct iommu_hw_ser { +struct iommu_hdr_ser hdr; +u64 token; +u64 type; +u64 reserved[2 ]; +} __packed; + +/** +* struct iommu_flb_ser - Top-level serialization structure +* @version: Version of the serialized data, IOMMU_LUO_FLB_VERSION +* @iommu_block_phys: Physical address of the first KHO block of IOMMU HWs, or +* 0 if none +* @domain_block_phys: Physical address of the first KHO block of domains, or +* 0 if none +* @device_block_phys: Physical address of the first KHO block of devices, or 0 +* if none +*/ +struct iommu_flb_ser { +u64 version; +u64 iommu_block_phys; +u64 domain_block_phys; +u64 device_block_phys; +} __packed; + +#endif /* _LINUX_KHO_ABI_IOMMU_H */ Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v8 02/18] iommu: Implement IOMMU Live update FLB callbacks
In reply to Samiullah KhawajaQuoted 7 lines · Samiullah Khawaja
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.
Minor nit: This still describes the pre-v6 array scheme. Now, retrieve doesn't restore each folio, block folios are restored at teardown via kho_restore_free(). The FLB obj itself is kzalloc'd. Let's refresh it?
Quoted 158 lines
Signed-off-by Samiullah Khawaja <[email protected]>--- MAINTAINERS | 9 ++ drivers/iommu/Kconfig | 12 ++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 241 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 28 ++++ include/linux/kho/abi/iommu.h | 190 ++++++++++++++++++++++++ 6 files changed, 481 insertions(+) create mode 100644 drivers/iommu/liveupdate.c create mode 100644 include/linux/iommu-liveupdate.h create mode 100644 include/linux/kho/abi/iommu.hMAINTAINERS +9
-0@@ -14139,6 +14139,15 @@ F: include/linux/iova.h F: include/linux/of_iommu.h F: rust/kernel/iommu/ +IOMMU LIVEUPDATE +M: Samiullah Khawaja <[email protected]> +R: Pranjal Shrivastava <[email protected]> +L: [email protected] +S: Maintained +F: drivers/iommu/liveupdate.c +F: include/linux/iommu-liveupdate.h +F: include/linux/kho/abi/iommu.h + IOMMUFD M: Jason Gunthorpe <[email protected]> M: Kevin Tian <[email protected]>drivers/iommu/Kconfig +12
-0@@ -421,6 +421,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"drivers/iommu/Makefile +1
-0@@ -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.odrivers/iommu/liveupdate.c new file +84
-0@@ -0,0 +1,241 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +/** +* 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/kho_block.h> +#include <linux/liveupdate.h> + +/** +* struct iommu_ser_set - Set of serialized objects of one type +* @bs: KHO block set holding the serialized objects +* @it: Iterator used to reserve new objects at the end of @bs +* @count: Number of reserved objects, including the deleted ones +* @head_pa: Field of struct iommu_flb_ser that records the head of @bs +*/ +struct iommu_ser_set { +struct kho_block_set bs; +struct kho_block_set_it it; +u64 count; +u64 *head_pa; +}; + +struct iommu_flb_obj { +/* Protects the serialized state and the object sets */ +struct mutex lock; +struct iommu_flb_ser *ser; + +struct iommu_ser_set iommu_set; +struct iommu_ser_set domain_set; +struct iommu_ser_set device_set; +}; + +/* +* Restore a set of serialized objects from the previous kernel. The objects +* are read using the size recorded by the previous kernel in their header. +* +* 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. +*/ +static void iommu_ser_set_restore (struct iommu_ser_set *set,u64 head_pa) +{ +size_t entry_size =sizeof (struct iommu_hdr_ser ); + +if (head_pa) {
drivers/iommu/liveupdate.c line 94
Quoted 10 lines
drivers/iommu/liveupdate.c new file +10
-0+struct kho_block_header_ser *block =phys_to_virt (head_pa); +struct iommu_hdr_ser *hdr = (void *)(block +1 ); + + entry_size = hdr->size; + } + +if (entry_size <sizeof (struct iommu_hdr_ser ) || + entry_size >KHO_BLOCK_SIZE -sizeof (struct kho_block_header_ser )) + goto err_fatal; +
With the callers passing sizeof(struct iommu_domain_ser) etc. Or, if every layout change is supposed to bump IOMMU_LUO_FLB_VERSION (see the comment on the ABI doc below), simply require that entry_size matches sizeof() of the struct.
Quoted 21 lines
drivers/iommu/liveupdate.c new file +21
-0+kho_block_set_init (&set->bs, entry_size); +if (kho_block_set_restore (&set->bs, head_pa)) + goto err_fatal; + +return ; + +err_fatal: +panic ("Failed to restore IOMMU Live Update FLB\n" ); +} + +static void iommu_liveupdate_flb_free (struct iommu_flb_obj *obj) +{ +kho_block_set_destroy (&obj->domain_set.bs); +kho_block_set_destroy (&obj->device_set.bs); +kho_block_set_destroy (&obj->iommu_set.bs); + +kho_unpreserve_free (obj->ser); +mutex_destroy (&obj->lock); +kfree (obj); +} +
[...]
drivers/iommu/liveupdate.c line 165
Quoted 12 lines
drivers/iommu/liveupdate.c new file +12
-0+static void iommu_liveupdate_flb_finish (struct liveupdate_flb_op_args *argp) +{ +struct iommu_flb_obj *obj = argp->obj; + +kho_block_set_destroy (&obj->domain_set.bs); +kho_block_set_destroy (&obj->device_set.bs); +kho_block_set_destroy (&obj->iommu_set.bs); + +folio_put (virt_to_folio (obj->ser)); +mutex_destroy (&obj->lock); +kfree (obj); +}
Nit: these two differ only in how ser is released. Could they share a common helper?
Quoted 117 lines
drivers/iommu/liveupdate.c new file +76
-0+ +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 */ +if (!kho_restore_folio (argp->data)) +panic ("Failed to restore IOMMU Live Update FLB\n" ); + + ser =phys_to_virt (argp->data); + +/* +* FLB version mismatch is considered fatal for security reasons for +* now. Check it before restoring the arrays, as their layout depends +* on the version. +*/ +if (ser->version !=IOMMU_LUO_FLB_VERSION ) +panic ("Incoming IOMMU FLB version (v%llu) is incompatible with this kernel (v%d)\n" , + ser->version,IOMMU_LUO_FLB_VERSION ); + +mutex_init (&obj->lock); + obj->ser = ser; + +iommu_ser_set_restore (&obj->domain_set, ser->domain_block_phys); +iommu_ser_set_restore (&obj->device_set, ser->device_block_phys); +iommu_ser_set_restore (&obj->iommu_set, ser->iommu_block_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_GPL (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_GPL (iommu_liveupdate_unregister_flb);include/linux/iommu-liveupdate.h new file +28
-0@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#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 */ include/linux/kho/abi/iommu.h new file +1
-0@@ -0,0 +1,190 @@ +/* SPDX-License-Identifier: GPL-2.0 */
include/linux/kho/abi/iommu.h line 11
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+ +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#ifndef _LINUX_KHO_ABI_IOMMU_H +#define _LINUX_KHO_ABI_IOMMU_H + +#include <linux/mutex_types.h>
Nit: This seems to be unused now? Can we remove this?
Quoted 13 lines
include/linux/kho/abi/iommu.h new file +13
-0+#include <linux/bits.h> +#include <linux/compiler.h> +#include <linux/kho/abi/block.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
include/linux/kho/abi/iommu.h line 34
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+* incrementing the version number in IOMMU_LUO_FLB_VERSION. +* +* Memory Layout of Serialization Structures: +* ========================================== +* +* Each serialized type (IOMMU, Domain, Device) is stored in a set of KHO +* serialization blocks (see include/linux/kho/abi/block.h). Each block starts +* with a struct kho_block_header_ser, followed by the serialized objects of +* the respective type. Blocks are linked through the next field of the header. +*
These two paragraphs seem to contradict each other. If every change to the structs bumps IOMMU_LUO_FLB_VERSION, and retrieve panics on a version mismatch, then two kernels that successfully hand over state always have identical layouts. The object size can then never change, and hdr.size (and hdr.features) carry no information.
If instead size/features are meant to allow compatible changes (e.g. appending a field) without a version bump, the restore side needs to handle that: require entry_size >= sizeof() of the struct being read (see iommu_ser_set_restore() above) and reject unknown feature bits (see below).
Let's clarify the policy and update the doc accordingly?
Quoted 26 lines
include/linux/kho/abi/iommu.h new file +26
-0+* Top Level (struct iommu_flb_ser): +* +---------------------------+ +* | - iommu_block_phys | +* | - domain_block_phys | +* | - device_block_phys | +* +---------------------------+ +* +* For example see below the representation of the domain blocks. +* +* +---------------------------+ +---------------------------+ +* | kho_block_header_ser |-->| kho_block_header_ser |--> NULL +* | - next | | - next | +* | - count | | - count | +* | | | | +* | [ iommu_domain_ser ] | | [ iommu_domain_ser ] | +* | [ iommu_domain_ser ] | | [ iommu_domain_ser ] | +* | ... | | ... | +* +---------------------------+ +---------------------------+ +* +* Each object in a block starts with a common header (iommu_hdr_ser). The +* size in the header is the size of each object in the block, so the next +* kernel can read the objects even if their size has changed. +* For example, the layout of struct iommu_domain_ser is: +* +* +-----------------------------+ +* | iommu_domain_ser |
include/linux/kho/abi/iommu.h line 70
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+* | +-------------------------+ | +* | | hdr (iommu_hdr_ser) | | +* | | - ref_count | | +* | | - deleted / incoming | | +* | +-------------------------+ | +* | - top_table_phys | | +* | - top_level | | +* | - restored_domain | | +* +-----------------------------+ +*
This diagram seems to be out of date now, for e.g. hdr_ser now has ref_count, flags, size, features AND iommu_domain_ser also has max_vasz_lg2 and sign_extend which are missing from the diagram.
Quoted 24 lines
include/linux/kho/abi/iommu.h new file +24
-0+* 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_SER_TYPE_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_SER_TYPE_INVALID , +}; + +#define IOMMU_SER_FLAG_DELETED BIT(0) +#define IOMMU_SER_FLAG_INCOMING BIT(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
include/linux/kho/abi/iommu.h line 104
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+* @size: Size of the serialized object +* @features: Bitmask of object specific features used by the preserved state +*/ +struct iommu_hdr_ser { +u32 ref_count; +u32 flags; +u32 size; +u32 features; +} __packed; +
I'm wondering if we should reject unknown feature bits on restore?
IIUC, features (along with size and reserved) was added in v6 to manage compatibility, but it's always written as 0 and never checked on restore (same for the driver specific features fields later in the series). If a feature bit can be added later without bumping IOMMU_LUO_FLB_VERSION, a kernel that doesn't know about it (e.g. after a rollback to this kernel) would silently restore the state without it, unless it rejects unknown bits from the start.
Also, this looks like a per-object version of the generic KHO feature header being worked on in [1] (struct kho_ser_hdr, which rejects unknown required features), and also records the entry size in the KHO block header. Would it make sense to move to that once it lands, rather than carrying a separate size/features scheme in the IOMMU ABI?
Alternatively, we could cross that bridge when we get to it..
Quoted 77 lines
include/linux/kho/abi/iommu.h new file +76
-0+/** +* 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 +* @max_vasz_lg2: Maximum number of bits the VA can contain +* @sign_extend: FEAT_SIGN_EXTEND is enabled for this domain +* @padding: Padding +* @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 max_vasz_lg2; +u8 sign_extend; +u8 padding[3 ]; +struct iommu_domain *restored_domain; +} __packed; + +/** +* struct iommu_attachment_ser - Serialized attachment between device and domain. +* @attachment_id: ID of the attachment between device and domain. +* @domain_phys: Physical address of the domain +* @pasid: PASID of the attachment +* @padding: Padding +*/ +struct iommu_attachment_ser { +u64 attachment_id; +u64 domain_phys; +u32 pasid; +u32 padding; +} __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 +* @iommu_phys: Physical address of the serialized IOMMU the device is behind +* @attachment: Attachment of the device to its domain +* @reserved: Reserved for future common fields. +*/ +struct iommu_device_ser { +struct iommu_hdr_ser hdr; +u32 devid; +u32 pci_domain_nr; +u64 dma_owner_token; +u64 iommu_phys; +struct iommu_attachment_ser attachment; +u64 reserved[2 ]; +} __packed; + +/** +* struct iommu_hw_ser - Serialized state of an IOMMU instance +* @hdr: Common object header +* @token: Unique token to identify the IOMMU instance +* @type: IOMMU type serialized state belongs to +* @reserved: Reserved for future common fields. +*/ +struct iommu_hw_ser { +struct iommu_hdr_ser hdr; +u64 token; +u64 type; +u64 reserved[2 ]; +} __packed; + +/** +* struct iommu_flb_ser - Top-level serialization structure +* @version: Version of the serialized data, IOMMU_LUO_FLB_VERSION +* @iommu_block_phys: Physical address of the first KHO block of IOMMU HWs, or +* 0 if none +* @domain_block_phys: Physical address of the first KHO block of domains, or +* 0 if none +* @device_block_phys: Physical address of the first KHO block of devices, or 0
include/linux/kho/abi/iommu.h line 190
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+* if none +*/ +struct iommu_flb_ser { +u64 version; +u64 iommu_block_phys; +u64 domain_block_phys; +u64 device_block_phys; +} __packed; + +#endif /* _LINUX_KHO_ABI_IOMMU_H */
Apart from those nits:
Thanks,
Praan
Re: [PATCH v8 02/18] iommu: Implement IOMMU Live update FLB callbacks
Quoted 30 lines · Pranjal Shrivastava
Quoted 29 lines · Samiullah Khawaja
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.
drivers/iommu/liveupdate.c new file +22
-0+/* +* Restore a set of serialized objects from the previous kernel. The objects +* are read using the size recorded by the previous kernel in their header. +* +* 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. +*/ +static void iommu_ser_set_restore (struct iommu_ser_set *set,u64 head_pa) +{ +size_t entry_size =sizeof (struct iommu_hdr_ser ); + +if (head_pa) { +struct kho_block_header_ser *block =phys_to_virt (head_pa); +struct iommu_hdr_ser *hdr = (void *)(block +1 ); + + entry_size = hdr->size; + } + + if (entry_size < sizeof(struct iommu_hdr_ser) ||
Quoted 9 lines
drivers/iommu/liveupdate.c line 94
Quoted 3 lines
drivers/iommu/liveupdate.c new file +3
-0+ entry_size > KHO_BLOCK_SIZE - sizeof(struct kho_block_header_ser)) + goto err_fatal; +With the callers passing
sizeof(struct iommu_domain_ser)etc. Or, if every layout change is supposed to bumpIOMMU_LUO_FLB_VERSION(see the comment on the ABI doc below), simply require thatentry_sizematchessizeof()of the struct.
Oops, I lost a part of the comment while trimming, what I meant was:
The lower bound here only guarantees that the common header is readable, but the users of these sets dereference each entry as the full iommu_{domain,device,hw}_ser. If the previous kernel recorded a smaller size, we'd read past the end of each object into the next entry. Should the lower bound be the size of the struct the set holds, e.g.:
static void iommu_ser_set_restore(struct iommu_ser_set *set, u64 head_pa,
size_t min_size)
[...]
if (entry_size < min_size ||
entry_size > KHO_BLOCK_SIZE - sizeof(struct kho_block_header_ser))
goto err_fatal;With the callers passing sizeof(struct iommu_domain_ser) etc.? Or, if every layout change is supposed to bump IOMMU_LUO_FLB_VERSION, simply require that entry_size matches sizeof() of the struct.
Thanks,
Praan
Re: [PATCH v8 02/18] iommu: Implement IOMMU Live update FLB callbacks
Quoted 41 lines · Pranjal Shrivastava
Quoted 30 lines · Pranjal Shrivastava
Quoted 29 lines · Samiullah Khawaja
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.
drivers/iommu/liveupdate.c new file +22
-0+/* +* Restore a set of serialized objects from the previous kernel. The objects +* are read using the size recorded by the previous kernel in their header. +* +* 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. +*/ +static void iommu_ser_set_restore (struct iommu_ser_set *set,u64 head_pa) +{ +size_t entry_size =sizeof (struct iommu_hdr_ser ); + +if (head_pa) { +struct kho_block_header_ser *block =phys_to_virt (head_pa); +struct iommu_hdr_ser *hdr = (void *)(block +1 ); + + entry_size = hdr->size; + } + + if (entry_size < sizeof(struct iommu_hdr_ser) ||Quoted 9 lines
drivers/iommu/liveupdate.c line 94
Quoted 3 lines
drivers/iommu/liveupdate.c new file +3
-0+ entry_size > KHO_BLOCK_SIZE - sizeof(struct kho_block_header_ser)) + goto err_fatal; +With the callers passing
sizeof(struct iommu_domain_ser)etc. Or, if every layout change is supposed to bumpIOMMU_LUO_FLB_VERSION(see the comment on the ABI doc below), simply require thatentry_sizematchessizeof()of the struct.
Quoted 5 lines
Oops, I lost a part of the comment while trimming, what I meant was:
The lower bound here only guarantees that the common header is readable, but the users of these sets dereference each entry as the full iommu_{domain,device,hw}_ser. If the previous kernel recorded a smaller size, we'd read past the end of each object into the next
During restore we setup the block entry size according to the size specified by the previous kernel in the common header. If the previous kernel recorded a smaller size, the users of the _ser structs will only read that much.
Note that the size can increase without breaking the compatibility, so a newer kernel with larger _ser structs will be able to read the older/smaller preserved state by using size/feature bits to decide which part of the structs are valid.
I will update the documentation with all this.
But I think a buggy previous kernel can still set the size to be smaller than the minimum _ser struct, so I will add checks for that.
Quoted 10 lines
entry. Should the lower bound be the size of the struct the set holds, e.g.:
static void iommu_ser_set_restore(struct
iommu_ser_set*set, u64head_pa,
size_tmin_size)
[...]if (entry_size < min_size || entry_size > KHO_BLOCK_SIZE - sizeof(struct kho_block_header_ser)) goto err_fatal;
Quoted 6 lines
With the callers passing
sizeof(struct iommu_domain_ser)etc.? Or, if every layout change is supposed to bumpIOMMU_LUO_FLB_VERSION, simply require thatentry_sizematchessizeof()of the struct.Thanks,
Praan
Thanks,
Sami
Re: [PATCH v8 02/18] iommu: Implement IOMMU Live update FLB callbacks
In reply to Pranjal ShrivastavaQuoted 5 lines · Pranjal Shrivastava
Quoted 4 lines · Samiullah Khawaja
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.
Quoted 7 lines
Quoted 3 lines
During retrieve, walk through the preserved obj array headers and restore each folio. Also recreate the FLB obj.
Minor nit: This still describes the pre-v6 array scheme. Now, retrieve doesn't restore each folio, block folios are restored at teardown via
kho_restore_free(). The FLB obj itself is kzalloc'd. Let's refresh it?
Will update.
Quoted 166 lines
Quoted 166 lines
Signed-off-by Samiullah Khawaja <[email protected]>--- MAINTAINERS | 9 ++ drivers/iommu/Kconfig | 12 ++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 241 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 28 ++++ include/linux/kho/abi/iommu.h | 190 ++++++++++++++++++++++++ 6 files changed, 481 insertions(+) create mode 100644 drivers/iommu/liveupdate.c create mode 100644 include/linux/iommu-liveupdate.h create mode 100644 include/linux/kho/abi/iommu.hMAINTAINERS +9
-0@@ -14139,6 +14139,15 @@ F: include/linux/iova.h F: include/linux/of_iommu.h F: rust/kernel/iommu/ +IOMMU LIVEUPDATE +M: Samiullah Khawaja <[email protected]> +R: Pranjal Shrivastava <[email protected]> +L: [email protected] +S: Maintained +F: drivers/iommu/liveupdate.c +F: include/linux/iommu-liveupdate.h +F: include/linux/kho/abi/iommu.h + IOMMUFD M: Jason Gunthorpe <[email protected]> M: Kevin Tian <[email protected]>drivers/iommu/Kconfig +12
-0@@ -421,6 +421,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"drivers/iommu/Makefile +1
-0@@ -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.odrivers/iommu/liveupdate.c new file +91
-0@@ -0,0 +1,241 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +/** +* 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/kho_block.h> +#include <linux/liveupdate.h> + +/** +* struct iommu_ser_set - Set of serialized objects of one type +* @bs: KHO block set holding the serialized objects +* @it: Iterator used to reserve new objects at the end of @bs +* @count: Number of reserved objects, including the deleted ones +* @head_pa: Field of struct iommu_flb_ser that records the head of @bs +*/ +struct iommu_ser_set { +struct kho_block_set bs; +struct kho_block_set_it it; +u64 count; +u64 *head_pa; +}; + +struct iommu_flb_obj { +/* Protects the serialized state and the object sets */ +struct mutex lock; +struct iommu_flb_ser *ser; + +struct iommu_ser_set iommu_set; +struct iommu_ser_set domain_set; +struct iommu_ser_set device_set; +}; + +/* +* Restore a set of serialized objects from the previous kernel. The objects +* are read using the size recorded by the previous kernel in their header. +* +* 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. +*/ +static void iommu_ser_set_restore (struct iommu_ser_set *set,u64 head_pa) +{ +size_t entry_size =sizeof (struct iommu_hdr_ser ); + +if (head_pa) { +struct kho_block_header_ser *block =phys_to_virt (head_pa); +struct iommu_hdr_ser *hdr = (void *)(block +1 ); + + entry_size = hdr->size; + } + + if (entry_size < sizeof(struct iommu_hdr_ser) ||
Quoted 8 lines
drivers/iommu/liveupdate.c line 94
Quoted 3 lines
drivers/iommu/liveupdate.c new file +3
-0+ entry_size > KHO_BLOCK_SIZE - sizeof(struct kho_block_header_ser)) + goto err_fatal; +With the callers passing
sizeof(struct iommu_domain_ser)etc. Or, if every layout change is supposed to bumpIOMMU_LUO_FLB_VERSION(see the comment on the ABI doc below), simply require thatentry_sizematchessizeof()of the struct.
Will reply to your follow up message.
Quoted 25 lines
Quoted 22 lines
drivers/iommu/liveupdate.c new file +21
-0+kho_block_set_init (&set->bs, entry_size); +if (kho_block_set_restore (&set->bs, head_pa)) + goto err_fatal; + +return ; + +err_fatal: +panic ("Failed to restore IOMMU Live Update FLB\n" ); +} + +static void iommu_liveupdate_flb_free (struct iommu_flb_obj *obj) +{ +kho_block_set_destroy (&obj->domain_set.bs); +kho_block_set_destroy (&obj->device_set.bs); +kho_block_set_destroy (&obj->iommu_set.bs); + +kho_unpreserve_free (obj->ser); +mutex_destroy (&obj->lock); +kfree (obj); +} +[...]
Quoted 15 lines
drivers/iommu/liveupdate.c line 165
Quoted 12 lines
drivers/iommu/liveupdate.c new file +12
-0+static void iommu_liveupdate_flb_finish (struct liveupdate_flb_op_args *argp) +{ +struct iommu_flb_obj *obj = argp->obj; + +kho_block_set_destroy (&obj->domain_set.bs); +kho_block_set_destroy (&obj->device_set.bs); +kho_block_set_destroy (&obj->iommu_set.bs); + +folio_put (virt_to_folio (obj->ser)); +mutex_destroy (&obj->lock); +kfree (obj); +}Nit: these two differ only in how ser is released. Could they share a common helper?
I will create a separate helper for the common parts.
Quoted 122 lines
Quoted 122 lines
drivers/iommu/liveupdate.c new file +76
-0+ +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 */ +if (!kho_restore_folio (argp->data)) +panic ("Failed to restore IOMMU Live Update FLB\n" ); + + ser =phys_to_virt (argp->data); + +/* +* FLB version mismatch is considered fatal for security reasons for +* now. Check it before restoring the arrays, as their layout depends +* on the version. +*/ +if (ser->version !=IOMMU_LUO_FLB_VERSION ) +panic ("Incoming IOMMU FLB version (v%llu) is incompatible with this kernel (v%d)\n" , + ser->version,IOMMU_LUO_FLB_VERSION ); + +mutex_init (&obj->lock); + obj->ser = ser; + +iommu_ser_set_restore (&obj->domain_set, ser->domain_block_phys); +iommu_ser_set_restore (&obj->device_set, ser->device_block_phys); +iommu_ser_set_restore (&obj->iommu_set, ser->iommu_block_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_GPL (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_GPL (iommu_liveupdate_unregister_flb);include/linux/iommu-liveupdate.h new file +28
-0@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#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 */ include/linux/kho/abi/iommu.h new file +5
-0@@ -0,0 +1,190 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]>
Quoted 8 lines
include/linux/kho/abi/iommu.h line 11
Quoted 6 lines
include/linux/kho/abi/iommu.h new file +6
-0+*/ + +#ifndef _LINUX_KHO_ABI_IOMMU_H +#define _LINUX_KHO_ABI_IOMMU_H + +#include <linux/mutex_types.h> Nit: This seems to be unused now? Can we remove this?
Will remove.
Quoted 31 lines
Quoted 14 lines
include/linux/kho/abi/iommu.h new file +13
-0+#include <linux/bits.h> +#include <linux/compiler.h> +#include <linux/kho/abi/block.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 include/linux/kho/abi/iommu.h line 34
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+* incrementing the version number in IOMMU_LUO_FLB_VERSION. +* +* Memory Layout of Serialization Structures: +* ========================================== +* +* Each serialized type (IOMMU, Domain, Device) is stored in a set of KHO +* serialization blocks (see include/linux/kho/abi/block.h). Each block starts +* with a struct kho_block_header_ser, followed by the serialized objects of +* the respective type. Blocks are linked through the next field of the header. +* These two paragraphs seem to contradict each other. If every change to the structs bumps
IOMMU_LUO_FLB_VERSION, and retrieve panics on a version mismatch, then two kernels that successfully hand over state always have identical layouts. The object size can then never change, and hdr.size (and hdr.features) carry no information.
Quoted 7 lines
If instead size/features are meant to allow compatible changes (e.g. appending a field) without a version bump, the restore side needs to handle that: require
entry_size>=sizeof()of the struct being read (seeiommu_ser_set_restore()above) and reject unknown feature bits (see below).Let's clarify the policy and update the doc accordingly?
Agreed. This needs to be updated as now the layout can change without breaking compatibility. Will do.
Quoted 38 lines
Quoted 27 lines
include/linux/kho/abi/iommu.h new file +26
-0+* Top Level (struct iommu_flb_ser): +* +---------------------------+ +* | - iommu_block_phys | +* | - domain_block_phys | +* | - device_block_phys | +* +---------------------------+ +* +* For example see below the representation of the domain blocks. +* +* +---------------------------+ +---------------------------+ +* | kho_block_header_ser |-->| kho_block_header_ser |--> NULL +* | - next | | - next | +* | - count | | - count | +* | | | | +* | [ iommu_domain_ser ] | | [ iommu_domain_ser ] | +* | [ iommu_domain_ser ] | | [ iommu_domain_ser ] | +* | ... | | ... | +* +---------------------------+ +---------------------------+ +* +* Each object in a block starts with a common header (iommu_hdr_ser). The +* size in the header is the size of each object in the block, so the next +* kernel can read the objects even if their size has changed. +* For example, the layout of struct iommu_domain_ser is: +* +* +-----------------------------+ +* | iommu_domain_ser | include/linux/kho/abi/iommu.h line 70
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+* | +-------------------------+ | +* | | hdr (iommu_hdr_ser) | | +* | | - ref_count | | +* | | - deleted / incoming | | +* | +-------------------------+ | +* | - top_table_phys | | +* | - top_level | | +* | - restored_domain | | +* +-----------------------------+ +*
Quoted 3 lines
This diagram seems to be out of date now, for e.g.
hdr_sernow hasref_count, flags, size, features ANDiommu_domain_seralso hasmax_vasz_lg2andsign_extendwhich are missing from the diagram.
Will update.
Quoted 39 lines
Quoted 25 lines
include/linux/kho/abi/iommu.h new file +24
-0+* 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_SER_TYPE_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_SER_TYPE_INVALID , +}; + +#define IOMMU_SER_FLAG_DELETED BIT(0) +#define IOMMU_SER_FLAG_INCOMING BIT(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 include/linux/kho/abi/iommu.h line 104
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+* @size: Size of the serialized object +* @features: Bitmask of object specific features used by the preserved state +*/ +struct iommu_hdr_ser { +u32 ref_count; +u32 flags; +u32 size; +u32 features; +} __packed; +I'm wondering if we should reject unknown feature bits on restore?
Quoted 7 lines
IIUC, features (along with size and reserved) was added in v6 to manage compatibility, but it's always written as 0 and never checked on restore (same for the driver specific features fields later in the series). If a feature bit can be added later without bumping
IOMMU_LUO_FLB_VERSION, a kernel that doesn't know about it (e.g. after a rollback to this kernel) would silently restore the state without it, unless it rejects unknown bits from the start.
Agreed. I will add checks for non-zero feature flags (including the driver specific ones).
Quoted 8 lines
Also, this looks like a per-object version of the generic KHO feature header being worked on in [1] (struct
kho_ser_hdr, which rejects unknown required features), and also records the entry size in the KHO block header. Would it make sense to move to that once it lands, rather than carrying a separate size/features scheme in the IOMMU ABI?Alternatively, we could cross that bridge when we get to it..
Yes, this is my understanding also. We can move forward with this series as is or it can be updated if the feature flag series lands earlier.
Quoted 91 lines
Quoted 78 lines
include/linux/kho/abi/iommu.h new file +77
-0+/** +* 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 +* @max_vasz_lg2: Maximum number of bits the VA can contain +* @sign_extend: FEAT_SIGN_EXTEND is enabled for this domain +* @padding: Padding +* @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 max_vasz_lg2; +u8 sign_extend; +u8 padding[3 ]; +struct iommu_domain *restored_domain; +} __packed; + +/** +* struct iommu_attachment_ser - Serialized attachment between device and domain. +* @attachment_id: ID of the attachment between device and domain. +* @domain_phys: Physical address of the domain +* @pasid: PASID of the attachment +* @padding: Padding +*/ +struct iommu_attachment_ser { +u64 attachment_id; +u64 domain_phys; +u32 pasid; +u32 padding; +} __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 +* @iommu_phys: Physical address of the serialized IOMMU the device is behind +* @attachment: Attachment of the device to its domain +* @reserved: Reserved for future common fields. +*/ +struct iommu_device_ser { +struct iommu_hdr_ser hdr; +u32 devid; +u32 pci_domain_nr; +u64 dma_owner_token; +u64 iommu_phys; +struct iommu_attachment_ser attachment; +u64 reserved[2 ]; +} __packed; + +/** +* struct iommu_hw_ser - Serialized state of an IOMMU instance +* @hdr: Common object header +* @token: Unique token to identify the IOMMU instance +* @type: IOMMU type serialized state belongs to +* @reserved: Reserved for future common fields. +*/ +struct iommu_hw_ser { +struct iommu_hdr_ser hdr; +u64 token; +u64 type; +u64 reserved[2 ]; +} __packed; + +/** +* struct iommu_flb_ser - Top-level serialization structure +* @version: Version of the serialized data, IOMMU_LUO_FLB_VERSION +* @iommu_block_phys: Physical address of the first KHO block of IOMMU HWs, or +* 0 if none +* @domain_block_phys: Physical address of the first KHO block of domains, or +* 0 if none +* @device_block_phys: Physical address of the first KHO block of devices, or 0 +* if none include/linux/kho/abi/iommu.h
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +9
-1+*/ +struct iommu_flb_ser { +u64 version; +u64 iommu_block_phys; +u64 domain_block_phys; +u64 device_block_phys; +} __packed; + +#endif /* _LINUX_KHO_ABI_IOMMU_H */ --Apart from those nits:
Quoted 1 line
Reviewed-by Pranjal Shrivastava <[email protected]>
Thanks Pranjal.
Quoted 5 lines
Thanks,
Praan
PATCH v8 3/18 iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
In reply to Samiullah KhawajaIOMMU pages are allocated/freed using APIs using struct ioptdesc. For the proper preservation and restoration of ioptdesc add helper functions.
--- drivers/iommu/iommu-pages.c | 109 ++++++++++++++++++++++++++++++++++-- drivers/iommu/iommu-pages.h | 30 ++++++++++ 2 files changed, 135 insertions(+), 4 deletions(-)
drivers/iommu/iommu-pages.c +105 -4
@@ -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,101 @@ 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); +if (!folio) +panic ("Failed to restore preserved IOMMU page %llx\n" , phys); + + 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
drivers/iommu/iommu-pages.h +30 -0
@@ -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
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v8 4/18 iommupt: Implement preserve/unpreserve/restore callbacks
In reply to Samiullah KhawajaAdd iommupt ops for preservation, unpreservation 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.
--- drivers/iommu/generic_pt/iommu_pt.h | 155 ++++++++++++++++++++++ drivers/iommu/generic_pt/kunit_iommu_pt.h | 33 +++++ include/linux/generic_pt/iommu.h | 30 +++++ 3 files changed, 218 insertions(+)
drivers/iommu/generic_pt/iommu_pt.h +155 -0
@@ -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 ,
@@ -991,6 +992,155 @@ 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->max_vasz_lg2 = 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; + +if (!pt_can_have_table (&pts)) +return 0 ; + +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->max_vasz_lg2 > 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->max_vasz_lg2; + +/* +* 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;
@@ -1174,6 +1324,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)
drivers/iommu/generic_pt/kunit_iommu_pt.h +33 -0
@@ -427,6 +427,38 @@ 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 generic features except +* SIGN_EXTEND and DMA_INCOHERENT, and keeping the format specific +* features as restore() does. 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 &= ~GENMASK (PT_FEAT_FMT_START -1 ,0 ) | +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 +467,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), {}, };
include/linux/generic_pt/iommu.h +30 -0
@@ -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); + };/**
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v8 04/18] iommupt: Implement preserve/unpreserve/restore callbacks
Quoted 8 lines · Samiullah Khawaja
Add iommupt ops for preservation, unpreservation and restoration of iommu page tables for liveupdate. Use the existing page walker to preserve the ioptdesc of the
top_tableand 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
Can I ask a basic design question?
Would you mind helping me confirm that the plan is not to reuse the preserved DMA tables long term after kexec?
So the DMA tables are preserved and used during kexec and until the new DMA tables are available?
Based on the LPC 2025 talk, can I assume the plan is not to reuse the old DMA tables long term, but to put more effort into replacing them quickly?
https://lpc.events/event/19/contributions/2046/
Taking a VM as an example, that means the below?
1. Preserve the old tables.
2. Run kexec.
3. Use the old tables until the new VM is set up.
4. Build the new tables.
5. Quickly switch to the new tables.
Thank you very much!
Dongli Zhang
Quoted 291 lines
added to verify that the IOMMU domain free can be done with trimmed features.
Signed-off-by Samiullah Khawaja <[email protected]>Reviewed-by Lu Baolu <[email protected]>Reviewed-by Ankit Soni <[email protected]>--- drivers/iommu/generic_pt/iommu_pt.h | 155 ++++++++++++++++++++++ drivers/iommu/generic_pt/kunit_iommu_pt.h | 33 +++++ include/linux/generic_pt/iommu.h | 30 +++++ 3 files changed, 218 insertions(+)drivers/iommu/generic_pt/iommu_pt.h +155
-0@@ -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 ,@@ -991,6 +992,155 @@ 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->max_vasz_lg2 = 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; + +if (!pt_can_have_table (&pts)) +return 0 ; + +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->max_vasz_lg2 > 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->max_vasz_lg2; + +/* +* 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;@@ -1174,6 +1324,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)drivers/iommu/generic_pt/kunit_iommu_pt.h +33
-0@@ -427,6 +427,38 @@ 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 generic features except +* SIGN_EXTEND and DMA_INCOHERENT, and keeping the format specific +* features as restore() does. 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 &= ~GENMASK (PT_FEAT_FMT_START -1 ,0 ) | +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 +467,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), {}, };include/linux/generic_pt/iommu.h +30
-0@@ -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); + };/**
Re: [PATCH v8 04/18] iommupt: Implement preserve/unpreserve/restore callbacks
Quoted 12 lines · Dongli Zhang
Quoted 8 lines · Samiullah Khawaja
Add iommupt ops for preservation, unpreservation and restoration of iommu page tables for liveupdate. Use the existing page walker to preserve the ioptdesc of the
top_tableand 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
Quoted 7 lines
Can I ask a basic design question?
Would you mind helping me confirm that the plan is not to reuse the preserved DMA tables long term after kexec?
So the DMA tables are preserved and used during kexec and until the new DMA tables are available?
Yes this is correct.
Quoted 6 lines
Based on the LPC 2025 talk, can I assume the plan is not to reuse the old DMA tables long term, but to put more effort into replacing them quickly?
Quoted 7 lines
Taking a VM as an example, that means the below?
1. Preserve the old tables.
2. Run kexec.
3. Use the old tables until the new VM is set up.
4. Build the new tables.
5. Quickly switch to the new tables.
Yes the steps you mentioned are correct. I sent out an RFC for replacing these tables in a hitless way:
https://lore.kernel.org/all/[email protected]/
Thanks,
Sami
Re: [PATCH v8 04/18] iommupt: Implement preserve/unpreserve/restore callbacks
In reply to Samiullah KhawajaQuoted 11 lines · Samiullah Khawaja
Add iommupt ops for preservation, unpreservation and restoration of iommu page tables for liveupdate. Use the existing page walker to preserve the ioptdesc of the
top_tableand 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.
Quoted 3 lines
Signed-off-by Samiullah Khawaja <[email protected]>Reviewed-by Lu Baolu <[email protected]>Reviewed-by Ankit Soni <[email protected]>
Thanks,
Praan
PATCH v8 5/18 iommu: Implement IOMMU domain preservation
In reply to Samiullah KhawajaAdd IOMMU domain preserve and unpreserve APIs that can be used to preserve an IOMMU domain across liveupdate. These APIs use the iommupt preserve and unpreserve ops of the domain to perform the respective live update operations.
--- drivers/iommu/liveupdate.c | 119 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 11 +++ include/linux/iommu.h | 5 ++ 3 files changed, 135 insertions(+)
drivers/iommu/liveupdate.c +119 -0
@@ -37,6 +37,7 @@#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>
@@ -67,6 +68,35 @@ struct iommu_flb_obj {struct iommu_ser_set device_set; }; +/* +* Reserve a new serialized object at the end of the set. The deleted objects +* are not reused, so the objects never move and can be referenced by their +* physical address. +*/ +static void *iommu_ser_set_alloc (struct iommu_ser_set *set) +{ +struct iommu_hdr_ser *hdr; +int ret; + + ret =kho_block_set_grow (&set->bs, set->count +1 ); +if (ret) +return ERR_PTR (ret); + +if (!set->count) +kho_block_set_it_init (&set->it, &set->bs); + + hdr =kho_block_set_it_reserve_entry (&set->it); +if (WARN_ON_ONCE (!hdr)) +return ERR_PTR (-ENOSPC ); + + set->count++; + *set->head_pa =kho_block_set_head_pa (&set->bs); + + hdr->ref_count =1 ; + hdr->size = set->bs.entry_size; +return hdr; +} +/* * Restore a set of serialized objects from the previous kernel. The objects * are read using the size recorded by the previous kernel in their header.
@@ -239,3 +269,92 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)liveupdate_unregister_flb (handler, &iommu_flb); }EXPORT_SYMBOL_GPL (iommu_liveupdate_unregister_flb); + +/** +* 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->domain_ser) { + ret = -EBUSY ; + goto out_unlock; + } + + domain_ser =iommu_ser_set_alloc (&flb_obj->domain_set); +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->domain_ser = 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->domain_ser) + 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->domain_ser; + pt->ops->unpreserve (pt, domain_ser); + domain_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; + domain->domain_ser =NULL ; +out_unlock: +mutex_unlock (&flb_obj->lock); +liveupdate_flb_put_outgoing (&iommu_flb); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_domain);
include/linux/iommu-liveupdate.h +11 -0
@@ -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 */
include/linux/iommu.h +5 -0
@@ -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 *domain_ser; +#endif };static inline bool iommu_is_dma_domain (struct iommu_domain *domain)
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v8 05/18] iommu: Implement IOMMU domain preservation
Quoted 8 lines · Samiullah Khawaja
Add IOMMU domain preserve and unpreserve APIs that can be used to preserve an IOMMU domain across liveupdate. These APIs use the iommupt preserve and unpreserve ops of the domain to perform the respective live update operations.
Signed-off-by Samiullah Khawaja <[email protected]>Reviewed-by Pranjal Shrivastava <[email protected]>Reviewed-by Lu Baolu <[email protected]>
Quoted 201 lines
--- drivers/iommu/liveupdate.c | 119 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 11 +++ include/linux/iommu.h | 5 ++ 3 files changed, 135 insertions(+)drivers/iommu/liveupdate.c +119
-0@@ -37,6 +37,7 @@#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> @@ -67,6 +68,35 @@ struct iommu_flb_obj {struct iommu_ser_set device_set; }; +/* +* Reserve a new serialized object at the end of the set. The deleted objects +* are not reused, so the objects never move and can be referenced by their +* physical address. +*/ +static void *iommu_ser_set_alloc (struct iommu_ser_set *set) +{ +struct iommu_hdr_ser *hdr; +int ret; + + ret =kho_block_set_grow (&set->bs, set->count +1 ); +if (ret) +return ERR_PTR (ret); + +if (!set->count) +kho_block_set_it_init (&set->it, &set->bs); + + hdr =kho_block_set_it_reserve_entry (&set->it); +if (WARN_ON_ONCE (!hdr)) +return ERR_PTR (-ENOSPC ); + + set->count++; + *set->head_pa =kho_block_set_head_pa (&set->bs); + + hdr->ref_count =1 ; + hdr->size = set->bs.entry_size; +return hdr; +} +/* * Restore a set of serialized objects from the previous kernel. The objects * are read using the size recorded by the previous kernel in their header. @@ -239,3 +269,92 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)liveupdate_unregister_flb (handler, &iommu_flb); }EXPORT_SYMBOL_GPL (iommu_liveupdate_unregister_flb); + +/** +* 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->domain_ser) { + ret = -EBUSY ; + goto out_unlock; + } + + domain_ser =iommu_ser_set_alloc (&flb_obj->domain_set); +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->domain_ser = 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->domain_ser) + 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->domain_ser; + pt->ops->unpreserve (pt, domain_ser); + domain_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; + domain->domain_ser =NULL ; +out_unlock: +mutex_unlock (&flb_obj->lock); +liveupdate_flb_put_outgoing (&iommu_flb); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_domain);include/linux/iommu-liveupdate.h +11
-0@@ -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 */ include/linux/iommu.h +5
-0@@ -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 *domain_ser; +#endif };static inline bool iommu_is_dma_domain (struct iommu_domain *domain)Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v8 05/18] iommu: Implement IOMMU domain preservation
Quoted 11 lines · Ankit Soni
Quoted 8 lines · Samiullah Khawaja
Add IOMMU domain preserve and unpreserve APIs that can be used to preserve an IOMMU domain across liveupdate. These APIs use the iommupt preserve and unpreserve ops of the domain to perform the respective live update operations.
Signed-off-by Samiullah Khawaja <[email protected]>Reviewed-by Pranjal Shrivastava <[email protected]>Reviewed-by Lu Baolu <[email protected]>Reviewed-by Ankit Soni <[email protected]>
Thanks,
Sami
PATCH v8 6/18 iommu: Implement device and IOMMU HW preservation
In reply to Samiullah KhawajaAdd 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.
--- drivers/iommu/iommu-priv.h | 3 + drivers/iommu/iommu.c | 58 ++++++++++ drivers/iommu/liveupdate.c | 185 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 76 +++++++++++++ include/linux/iommu.h | 21 ++++ 5 files changed, 343 insertions(+)
drivers/iommu/iommu-priv.h +3 -0
@@ -21,6 +21,9 @@ static inline const struct iommu_ops *dev_iommu_ops(struct device *dev)void dev_iommu_free (struct device *dev); +int iommu_group_reserve_liveupdate (struct iommu_group *group); +void iommu_group_unreserve_liveupdate (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)
drivers/iommu/iommu.c +58 -0
94 lines not on this page · raw message
drivers/iommu/liveupdate.c +185 -0
194 lines not on this page · raw message
include/linux/iommu-liveupdate.h +76 -0
101 lines not on this page · raw message
include/linux/iommu.h +21 -0
@@ -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;
@@ -829,6 +840,7 @@ 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 +* @iommu_ser: preserved iommu state of outgoing kernel for liveupdate. */ struct iommu_device {struct list_head list;
@@ -838,6 +850,10 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_hw_ser *iommu_ser; +#endif };/**
@@ -877,6 +893,8 @@ struct iommu_fault_param {* @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs * @require_direct: device requires IOMMU_RESV_DIRECT regions * @shadow_on_flush: IOTLB flushes are used to sync shadow tables +* @device_ser: preserved (outgoing) or restored (incoming) liveupdate state +* of this device * * TODO: migrate other per device data pointers under iommu_dev_data, e.g. * struct iommu_group *iommu_group;
@@ -892,6 +910,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,
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v8 06/18] iommu: Implement device and IOMMU HW preservation
Quoted 8 lines · Samiullah Khawaja
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 <[email protected]>
Quoted 505 lines
--- drivers/iommu/iommu-priv.h | 3 + drivers/iommu/iommu.c | 58 ++++++++++ drivers/iommu/liveupdate.c | 185 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 76 +++++++++++++ include/linux/iommu.h | 21 ++++ 5 files changed, 343 insertions(+)drivers/iommu/iommu-priv.h +3
-0@@ -21,6 +21,9 @@ static inline const struct iommu_ops *dev_iommu_ops(struct device *dev)void dev_iommu_free (struct device *dev); +int iommu_group_reserve_liveupdate (struct iommu_group *group); +void iommu_group_unreserve_liveupdate (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)drivers/iommu/iommu.c +58
-0@@ -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> @@ -71,6 +72,10 @@ struct iommu_group {*/ unsigned int recovery_cnt;void *owner; +#ifdef CONFIG_IOMMU_LIVEUPDATE +/* Group is reserved for a device with live update state */ +bool liveupdate_reserved; +#endif };struct group_device {@@ -624,6 +629,19 @@ static struct iommu_domain *pasid_array_entry_to_domain(void *entry)DEFINE_MUTEX (iommu_probe_device_lock); +#ifdef CONFIG_IOMMU_LIVEUPDATE +static bool iommu_group_liveupdate_reserved (struct iommu_group *group) +{ +lockdep_assert_held (&group->mutex); +return group->liveupdate_reserved; +} +#else +static inline bool iommu_group_liveupdate_reserved (struct iommu_group *group) +{ +return false; +} +#endif +static int __iommu_probe_device (struct device *dev,struct list_head *group_list) {struct iommu_group *group;@@ -661,6 +679,11 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list goto err_put_group; } +if (iommu_group_liveupdate_reserved (group)) { + ret = -EBUSY ; + goto err_free_gdev; + } +/* * The gdev must be in the list before calling * iommu_setup_default_domain() @@ -697,6 +720,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);@@ -3545,6 +3569,40 @@ bool iommu_group_dma_owner_claimed(struct iommu_group *group) }EXPORT_SYMBOL_GPL (iommu_group_dma_owner_claimed); +#ifdef CONFIG_IOMMU_LIVEUPDATE +/** +* iommu_group_reserve_liveupdate() - Reserve a group for live update +* @group: The group. +* +* No other device can be added to a reserved group. +* +* Return: 0 on success, -EBUSY if the group is already reserved, or +* -EOPNOTSUPP if the group doesn't have exactly one device. +*/ +int iommu_group_reserve_liveupdate (struct iommu_group *group) +{ +guard (mutex)(&group->mutex); +if (group->liveupdate_reserved) +return -EBUSY ; + +if (!list_is_singular (&group->devices)) +return -EOPNOTSUPP ; + + group->liveupdate_reserved = true; +return 0 ; +} + +/** +* iommu_group_unreserve_liveupdate() - Drop the live update reservation +* @group: The group. +*/ +void iommu_group_unreserve_liveupdate (struct iommu_group *group) +{ +guard (mutex)(&group->mutex); + group->liveupdate_reserved = false; +} +#endif +static void iommu_remove_dev_pasid (struct device *dev,ioasid_t pasid,struct iommu_domain *domain) {drivers/iommu/liveupdate.c +185
-0@@ -43,6 +43,9 @@#include <linux/kexec_handover.h> #include <linux/kho_block.h> #include <linux/liveupdate.h> +#include <linux/pci.h> + +#include "iommu-priv.h" /** * struct iommu_ser_set - Set of serialized objects of one type @@ -358,3 +361,185 @@ void iommu_unpreserve_domain(struct iommu_domain *domain)liveupdate_flb_put_outgoing (&iommu_flb); }EXPORT_SYMBOL_GPL (iommu_unpreserve_domain); + +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->iommu_ser) { + iommu->iommu_ser->hdr.ref_count++; +return 0 ; + } + + iommu_hw_ser =iommu_ser_set_alloc (&flb_obj->iommu_set); +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->iommu_ser = 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->iommu_ser; + +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->iommu_ser =NULL ; + iommu->ops->unpreserve (iommu, iommu_hw_ser); + iommu_hw_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; +} + +/** +* 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 ; + +if (iommu_get_domain_for_dev (dev) != domain) +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 =iommu_group_reserve_liveupdate (dev->iommu_group); +if (ret) +return ret; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) + goto out_unreserve; + +mutex_lock (&flb_obj->lock); +if (!domain->domain_ser) { + ret = -EINVAL ; + goto out_unlock; + } + + device_ser =iommu_ser_set_alloc (&flb_obj->device_set); +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->attachment.domain_phys =virt_to_phys (domain->domain_ser); + device_ser->iommu_phys =virt_to_phys (iommu->iommu_dev->iommu_ser); + 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) { + device_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; +iommu_unpreserve_locked (iommu->iommu_dev, flb_obj); + goto out_unlock; + } + +WRITE_ONCE (dev->iommu->device_ser, device_ser); + +out_unlock: +mutex_unlock (&flb_obj->lock); +liveupdate_flb_put_outgoing (&iommu_flb); +out_unreserve: +if (ret) +iommu_group_unreserve_liveupdate (dev->iommu_group); +return ret; +} +EXPORT_SYMBOL_GPL (iommu_preserve_device); + +/** +* iommu_unpreserve_device() - Unpreserve device state +* @dev: Device to unpreserve +*/ +void iommu_unpreserve_device (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)) { + ret = -ENOENT ; + 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); +if (!ret) +iommu_group_unreserve_liveupdate (dev->iommu_group); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_device);include/linux/iommu-liveupdate.h +76
-0@@ -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,58 @@#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_has_preserved_state() - Check if a device has live update state +* @dev: Target device +* +* Unlike dev_iommu_preserved_state() and dev_iommu_restored_state(), this does +* not dereference the serialized state, so it is safe to call without holding +* a reference on the live update FLB. +* +* Return: true if @dev has preserved (outgoing) or restored (incoming) state. +*/ +static inline bool dev_iommu_has_preserved_state (struct device *dev) +{ +return dev->iommu &&READ_ONCE (dev->iommu->device_ser); +} + +/** +* 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 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->iommu_ser; +}#else static inline int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler) {@@ -27,6 +78,16 @@ static inline void iommu_liveupdate_unregister_flb(struct liveupdate_file_handle { } +static inline bool dev_iommu_has_preserved_state (struct device *dev) +{ +return false; +} + +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 +96,20 @@ static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iommstatic 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 device *dev) +{ +} + +static inline void *iommu_preserved_state (struct iommu_device *iommu) +{ +return NULL ; +}#endif #endif /* _LINUX_IOMMU_LIVEUPDATE_H */ include/linux/iommu.h +21
-0@@ -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;@@ -829,6 +840,7 @@ 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 +* @iommu_ser: preserved iommu state of outgoing kernel for liveupdate. */ struct iommu_device {struct list_head list;@@ -838,6 +850,10 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_hw_ser *iommu_ser; +#endif };/** @@ -877,6 +893,8 @@ struct iommu_fault_param {* @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs * @require_direct: device requires IOMMU_RESV_DIRECT regions * @shadow_on_flush: IOTLB flushes are used to sync shadow tables +* @device_ser: preserved (outgoing) or restored (incoming) liveupdate state +* of this device * * TODO: migrate other per device data pointers under iommu_dev_data, e.g. * struct iommu_group *iommu_group; @@ -892,6 +910,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,Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Re: [PATCH v8 06/18] iommu: Implement device and IOMMU HW preservation
Quoted 11 lines · Ankit Soni
Quoted 8 lines · Samiullah Khawaja
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 <[email protected]>Reviewed-by Ankit Soni <[email protected]>
Thanks,
Sami
Baolu Lu
maintainerRe: [PATCH v8 06/18] iommu: Implement device and IOMMU HW preservation
In reply to Samiullah KhawajaQuoted 8 lines · Samiullah Khawaja
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 <[email protected]>
[...]
Quoted 79 lines
drivers/iommu/iommu.c +36
-0@@ -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> @@ -71,6 +72,10 @@ struct iommu_group {*/ unsigned int recovery_cnt;void *owner; +#ifdef CONFIG_IOMMU_LIVEUPDATE +/* Group is reserved for a device with live update state */ +bool liveupdate_reserved; +#endif };struct group_device {@@ -624,6 +629,19 @@ static struct iommu_domain *pasid_array_entry_to_domain(void *entry)DEFINE_MUTEX (iommu_probe_device_lock); +#ifdef CONFIG_IOMMU_LIVEUPDATE +static bool iommu_group_liveupdate_reserved (struct iommu_group *group) +{ +lockdep_assert_held (&group->mutex); +return group->liveupdate_reserved; +} +#else +static inline bool iommu_group_liveupdate_reserved (struct iommu_group *group) +{ +return false; +} +#endif +static int __iommu_probe_device (struct device *dev,struct list_head *group_list) {struct iommu_group *group;@@ -661,6 +679,11 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list goto err_put_group; } +if (iommu_group_liveupdate_reserved (group)) { + ret = -EBUSY ; + goto err_free_gdev; + } +/* * The gdev must be in the list before calling * iommu_setup_default_domain() @@ -697,6 +720,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);@@ -3545,6 +3569,40 @@ bool iommu_group_dma_owner_claimed(struct iommu_group *group) }EXPORT_SYMBOL_GPL (iommu_group_dma_owner_claimed); +#ifdef CONFIG_IOMMU_LIVEUPDATE +/** +* iommu_group_reserve_liveupdate() - Reserve a group for live update +* @group: The group. +* +* No other device can be added to a reserved group. +* +* Return: 0 on success, -EBUSY if the group is already reserved, or +* -EOPNOTSUPP if the group doesn't have exactly one device. +*/ +int iommu_group_reserve_liveupdate (struct iommu_group *group) +{
drivers/iommu/iommu.c bool iommu_group_dma_owner_claimed(struct iommu_group *group) · line 3593
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+guard (mutex)(&group->mutex); +if (group->liveupdate_reserved) +return -EBUSY ; + +if (!list_is_singular (&group->devices)) +return -EOPNOTSUPP ; + + group->liveupdate_reserved = true; +return 0 ; +}
The preservation only blocks new devices from joining the group. What happens if the preserved device is released, for example by a surprise hot-unplug? It appears that the device is gone but its device_ser entry stays live, the iommu_ser refcount is leaked.
Is this guaranteed not to happen because vfio keeps the device open? If so, perhaps add a WARN_ON() in the release path when the device still has preserved state?
Quoted 32 lines
drivers/iommu/iommu.c +12
-0+ +/** +* iommu_group_unreserve_liveupdate() - Drop the live update reservation +* @group: The group. +*/ +void iommu_group_unreserve_liveupdate (struct iommu_group *group) +{ +guard (mutex)(&group->mutex); + group->liveupdate_reserved = false; +} +#endif +static void iommu_remove_dev_pasid (struct device *dev,ioasid_t pasid,struct iommu_domain *domain) {drivers/iommu/liveupdate.c +3
-0@@ -43,6 +43,9 @@#include <linux/kexec_handover.h> #include <linux/kho_block.h> #include <linux/liveupdate.h> +#include <linux/pci.h> + +#include "iommu-priv.h" /** * struct iommu_ser_set - Set of serialized objects of one type @@ -358,3 +361,185 @@ void iommu_unpreserve_domain(struct iommu_domain *domain)liveupdate_flb_put_outgoing (&iommu_flb); }
drivers/iommu/liveupdate.c void iommu_unpreserve_domain(struct iommu_domain *domain) · line 372
Quoted 10 lines
drivers/iommu/liveupdate.c +9
-0EXPORT_SYMBOL_GPL (iommu_unpreserve_domain); + +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 ;
nit: The only caller has already checked these ops. The same for WARN_ON_ONCE() in iommu_unpreserve_locked(). Both can be dropped.
Quoted 58 lines
drivers/iommu/liveupdate.c +58
-0+ +lockdep_assert_held (&flb_obj->lock); +if (iommu->iommu_ser) { + iommu->iommu_ser->hdr.ref_count++; +return 0 ; + } + + iommu_hw_ser =iommu_ser_set_alloc (&flb_obj->iommu_set); +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->iommu_ser = 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->iommu_ser; + +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->iommu_ser =NULL ; + iommu->ops->unpreserve (iommu, iommu_hw_ser); + iommu_hw_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; +} + +/** +* 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;
drivers/iommu/liveupdate.c void iommu_unpreserve_domain(struct iommu_domain *domain) · line 440
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+int ret; + +if (!dev_is_pci (dev)) +return -EOPNOTSUPP ; + +if (!iommu_group_dma_owner_claimed (dev->iommu_group)) +return -EINVAL ; + +if (iommu_get_domain_for_dev (dev) != domain) +return -EINVAL ;
It's better to check dma ownership and domain with group->mutex locked. Perhaps move them to iommu_group_reserve_liveupdate()?
drivers/iommu/liveupdate.c void iommu_unpreserve_domain(struct iommu_domain *domain) · line 448
Quoted 8 lines
drivers/iommu/liveupdate.c +8
-0+ + 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 ;
nit: how aobut
const struct iommu_ops *ops = dev_iommu_ops(dev); if (!ops->preserve_device || !ops->unpreserve_device || !ops->preserve || !ops->unpreserve) return -EOPNOTSUPP;
?
Quoted 58 lines
drivers/iommu/liveupdate.c +58
-0+ + ret =iommu_group_reserve_liveupdate (dev->iommu_group); +if (ret) +return ret; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) + goto out_unreserve; + +mutex_lock (&flb_obj->lock); +if (!domain->domain_ser) { + ret = -EINVAL ; + goto out_unlock; + } + + device_ser =iommu_ser_set_alloc (&flb_obj->device_set); +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->attachment.domain_phys =virt_to_phys (domain->domain_ser); + device_ser->iommu_phys =virt_to_phys (iommu->iommu_dev->iommu_ser); + 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) { + device_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; +iommu_unpreserve_locked (iommu->iommu_dev, flb_obj); + goto out_unlock; + } + +WRITE_ONCE (dev->iommu->device_ser, device_ser); + +out_unlock: +mutex_unlock (&flb_obj->lock); +liveupdate_flb_put_outgoing (&iommu_flb); +out_unreserve: +if (ret) +iommu_group_unreserve_liveupdate (dev->iommu_group); +return ret; +} +EXPORT_SYMBOL_GPL (iommu_preserve_device); + +/** +* iommu_unpreserve_device() - Unpreserve device state +* @dev: Device to unpreserve +*/ +void iommu_unpreserve_device (struct device *dev) +{
drivers/iommu/liveupdate.c void iommu_unpreserve_domain(struct iommu_domain *domain) · line 516
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+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 ;
Nit: iommu_unpreserve_device() is supposed to be called in preserve_device failure path or after a successful perserve_device().
How could above checks be true? If these ever trigger, the preserved state leaks silently and the group stays reserved forever.Perhaps drop these checks, or at least make it a WARN_ON()?
Quoted 9 lines
drivers/iommu/liveupdate.c +9
-0+ + 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 ;
drivers/iommu/liveupdate.c void iommu_unpreserve_domain(struct iommu_domain *domain) · line 535
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+ +mutex_lock (&flb_obj->lock); + iommu_device_ser =dev_iommu_preserved_state (dev); +if (WARN_ON (!iommu_device_ser)) { + ret = -ENOENT ; + goto out_unlock; + } + + iommu_device_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; + iommu->iommu_dev->ops->unpreserve_device (dev, iommu_device_ser);
Nit: the driver callback gets an entry that is already marked DELETED. Call ->unpreserve_device() first and then set the flag, the same order as in iommu_unpreserve_locked().
Quoted 31 lines
drivers/iommu/liveupdate.c +10
-0+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); +if (!ret) +iommu_group_unreserve_liveupdate (dev->iommu_group); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_device);include/linux/iommu-liveupdate.h +6
-0@@ -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,58 @@#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_has_preserved_state() - Check if a device has live update state +* @dev: Target device +*
include/linux/iommu-liveupdate.h line 33
Quoted 10 lines
include/linux/iommu-liveupdate.h +10
-0+* Unlike dev_iommu_preserved_state() and dev_iommu_restored_state(), this does +* not dereference the serialized state, so it is safe to call without holding +* a reference on the live update FLB. +* +* Return: true if @dev has preserved (outgoing) or restored (incoming) state. +*/ +static inline bool dev_iommu_has_preserved_state (struct device *dev) +{ +return dev->iommu &&READ_ONCE (dev->iommu->device_ser); +}
Is this used anywhere? I couldn't find a caller in the tree. Clean it up?
Quoted 10 lines
include/linux/iommu-liveupdate.h +10
-0+ +/** +* 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;
include/linux/iommu-liveupdate.h line 53
Quoted 10 lines
include/linux/iommu-liveupdate.h +10
-0+ +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 ; +}
nit: why not returning "struct iommu_device_ser *" instead of "void *"?
Quoted 7 lines
include/linux/iommu-liveupdate.h +5
-0+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 device *dev); +
include/linux/iommu-liveupdate.h line 70
Quoted 10 lines
include/linux/iommu-liveupdate.h +10
-0+/** +* 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->iommu_ser; +}
READ_ONCE()/WRITE_ONCE() used for dev->iommu->device_ser, but not here. Please handle it consistently.
Quoted 57 lines
include/linux/iommu-liveupdate.h +25
-0#else static inline int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler) {@@ -27,6 +78,16 @@ static inline void iommu_liveupdate_unregister_flb(struct liveupdate_file_handle { } +static inline bool dev_iommu_has_preserved_state (struct device *dev) +{ +return false; +} + +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 +96,20 @@ static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iommstatic 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 device *dev) +{ +} + +static inline void *iommu_preserved_state (struct iommu_device *iommu) +{ +return NULL ; +}#endif #endif /* _LINUX_IOMMU_LIVEUPDATE_H */ include/linux/iommu.h +4
-0@@ -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.
include/linux/iommu.h struct iommu_ops { · line 752
Quoted 9 lines
include/linux/iommu.h +6
-0@@ -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
nit: How about preserve_iommu/unpreserve_iommu, to match preserve_device/unpreserve_device?
Quoted 33 lines
include/linux/iommu.h +8
-0+const struct iommu_domain_ops *default_domain_ops;struct module *owner;struct iommu_domain *identity_domain;@@ -829,6 +840,7 @@ 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 +* @iommu_ser: preserved iommu state of outgoing kernel for liveupdate. */ struct iommu_device {struct list_head list;@@ -838,6 +850,10 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_hw_ser *iommu_ser; +#endif };/** @@ -877,6 +893,8 @@ struct iommu_fault_param {* @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs * @require_direct: device requires IOMMU_RESV_DIRECT regions * @shadow_on_flush: IOTLB flushes are used to sync shadow tables +* @device_ser: preserved (outgoing) or restored (incoming) liveupdate state +* of this device * * TODO: migrate other per device data pointers under iommu_dev_data, e.g. * struct iommu_group *iommu_group;
include/linux/iommu.h struct dev_iommu { · line 918
Quoted 9 lines
include/linux/iommu.h +3
-0@@ -892,6 +910,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,
Thanks,
baolu
Re: [PATCH v8 06/18] iommu: Implement device and IOMMU HW preservation
Quoted 101 lines · Baolu Lu
Quoted 8 lines · Samiullah Khawaja
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 <[email protected]>[...]
Quoted 73 lines
drivers/iommu/iommu.c +36
-0@@ -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> @@ -71,6 +72,10 @@ struct iommu_group {*/ unsigned int recovery_cnt;void *owner; +#ifdef CONFIG_IOMMU_LIVEUPDATE +/* Group is reserved for a device with live update state */ +bool liveupdate_reserved; +#endif };struct group_device {@@ -624,6 +629,19 @@ static struct iommu_domain *pasid_array_entry_to_domain(void *entry)DEFINE_MUTEX (iommu_probe_device_lock); +#ifdef CONFIG_IOMMU_LIVEUPDATE +static bool iommu_group_liveupdate_reserved (struct iommu_group *group) +{ +lockdep_assert_held (&group->mutex); +return group->liveupdate_reserved; +} +#else +static inline bool iommu_group_liveupdate_reserved (struct iommu_group *group) +{ +return false; +} +#endif +static int __iommu_probe_device (struct device *dev,struct list_head *group_list) {struct iommu_group *group;@@ -661,6 +679,11 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list goto err_put_group; } +if (iommu_group_liveupdate_reserved (group)) { + ret = -EBUSY ; + goto err_free_gdev; + } +/* * The gdev must be in the list before calling * iommu_setup_default_domain() @@ -697,6 +720,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);@@ -3545,6 +3569,40 @@ bool iommu_group_dma_owner_claimed(struct iommu_group *group) }EXPORT_SYMBOL_GPL (iommu_group_dma_owner_claimed); +#ifdef CONFIG_IOMMU_LIVEUPDATE +/** +* iommu_group_reserve_liveupdate() - Reserve a group for live update +* @group: The group. +* +* No other device can be added to a reserved group. +* +* Return: 0 on success, -EBUSY if the group is already reserved, or +* -EOPNOTSUPP if the group doesn't have exactly one device. +*/ +int iommu_group_reserve_liveupdate (struct iommu_group *group) +{drivers/iommu/iommu.c bool iommu_group_dma_owner_claimed(struct iommu_group *group) · line 3592
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+guard (mutex)(&group->mutex); +if (group->liveupdate_reserved) +return -EBUSY ; + +if (!list_is_singular (&group->devices)) +return -EOPNOTSUPP ; + + group->liveupdate_reserved = true; +return 0 ; +}The preservation only blocks new devices from joining the group. What happens if the preserved device is released, for example by a surprise hot-unplug? It appears that the device is gone but its
device_serentry stays live, theiommu_serrefcount is leaked.
Quoted 3 lines
Is this guaranteed not to happen because vfio keeps the device open? If so, perhaps add a
WARN_ON()in the release path when the device still has preserved state?
Yes, the device is not removed as vfio keeps the device open. I will add a WARN_ON() in the release path.
Quoted 38 lines
Quoted 38 lines
drivers/iommu/iommu.c +12
-0+ +/** +* iommu_group_unreserve_liveupdate() - Drop the live update reservation +* @group: The group. +*/ +void iommu_group_unreserve_liveupdate (struct iommu_group *group) +{ +guard (mutex)(&group->mutex); + group->liveupdate_reserved = false; +} +#endif +static void iommu_remove_dev_pasid (struct device *dev,ioasid_t pasid,struct iommu_domain *domain) {drivers/iommu/liveupdate.c +8
-0@@ -43,6 +43,9 @@#include <linux/kexec_handover.h> #include <linux/kho_block.h> #include <linux/liveupdate.h> +#include <linux/pci.h> + +#include "iommu-priv.h" /** * struct iommu_ser_set - Set of serialized objects of one type @@ -358,3 +361,185 @@ void iommu_unpreserve_domain(struct iommu_domain *domain)liveupdate_flb_put_outgoing (&iommu_flb); }EXPORT_SYMBOL_GPL (iommu_unpreserve_domain); + +static int iommu_preserve_locked (struct iommu_device *iommu, +struct iommu_flb_obj *flb_obj) +{ +struct iommu_hw_ser *iommu_hw_ser;
Quoted 8 lines
drivers/iommu/liveupdate.c void iommu_unpreserve_domain(struct iommu_domain *domain) · line 372
Quoted 4 lines
drivers/iommu/liveupdate.c +4
-0+int ret; + +if (!iommu->ops->preserve || !iommu->ops->unpreserve) +return -EOPNOTSUPP ;nit: The only caller has already checked these ops. The same for
WARN_ON_ONCE()iniommu_unpreserve_locked(). Both can be dropped.
Will do.
Quoted 64 lines
Quoted 64 lines
drivers/iommu/liveupdate.c +63
-0+ +lockdep_assert_held (&flb_obj->lock); +if (iommu->iommu_ser) { + iommu->iommu_ser->hdr.ref_count++; +return 0 ; + } + + iommu_hw_ser =iommu_ser_set_alloc (&flb_obj->iommu_set); +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->iommu_ser = 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->iommu_ser; + +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->iommu_ser =NULL ; + iommu->ops->unpreserve (iommu, iommu_hw_ser); + iommu_hw_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; +} + +/** +* 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 ; +
Quoted 8 lines
drivers/iommu/liveupdate.c void iommu_unpreserve_domain(struct iommu_domain *domain) · line 440
Quoted 5 lines
drivers/iommu/liveupdate.c +5
-0+if (!iommu_group_dma_owner_claimed (dev->iommu_group)) +return -EINVAL ; + +if (iommu_get_domain_for_dev (dev) != domain) +return -EINVAL ;It's better to check dma ownership and domain with
group->mutexlocked. Perhaps move them toiommu_group_reserve_liveupdate()?
Agreed. Will move this to the reserve_liveupdate().
Quoted 12 lines
drivers/iommu/liveupdate.c void iommu_unpreserve_domain(struct iommu_domain *domain) · line 448
Quoted 9 lines
drivers/iommu/liveupdate.c +8
-0+ + 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 ;nit: how aobut
Quoted 6 lines
const struct iommu_ops *ops = dev_iommu_ops(dev); if (!ops->preserve_device || !ops->unpreserve_device || !ops->preserve || !ops->unpreserve) return -EOPNOTSUPP;?
Looks great. I will update this.
Quoted 73 lines
Quoted 59 lines
drivers/iommu/liveupdate.c +58
-0+ + ret =iommu_group_reserve_liveupdate (dev->iommu_group); +if (ret) +return ret; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) + goto out_unreserve; + +mutex_lock (&flb_obj->lock); +if (!domain->domain_ser) { + ret = -EINVAL ; + goto out_unlock; + } + + device_ser =iommu_ser_set_alloc (&flb_obj->device_set); +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->attachment.domain_phys =virt_to_phys (domain->domain_ser); + device_ser->iommu_phys =virt_to_phys (iommu->iommu_dev->iommu_ser); + 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) { + device_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; +iommu_unpreserve_locked (iommu->iommu_dev, flb_obj); + goto out_unlock; + } + +WRITE_ONCE (dev->iommu->device_ser, device_ser); + +out_unlock: +mutex_unlock (&flb_obj->lock); +liveupdate_flb_put_outgoing (&iommu_flb); +out_unreserve: +if (ret) +iommu_group_unreserve_liveupdate (dev->iommu_group); +return ret; +} +EXPORT_SYMBOL_GPL (iommu_preserve_device); + +/** +* iommu_unpreserve_device() - Unpreserve device state +* @dev: Device to unpreserve +*/ +void iommu_unpreserve_device (struct device *dev) +{drivers/iommu/liveupdate.c void iommu_unpreserve_domain(struct iommu_domain *domain) · line 516
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+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 ;Nit:
iommu_unpreserve_device()is supposed to be called inpreserve_devicefailure path or after a successfulperserve_device().
Quoted 3 lines
How could above checks be true? If these ever trigger, the preserved state leaks silently and the group stays reserved forever.Perhaps drop these checks, or at least make it a
WARN_ON()?
All your points are valid and yes I will make it a WARN_ON().
Quoted 16 lines
Quoted 16 lines
drivers/iommu/liveupdate.c +15
-0+ + 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)) { + ret = -ENOENT ; + goto out_unlock;
Quoted 8 lines
drivers/iommu/liveupdate.c void iommu_unpreserve_domain(struct iommu_domain *domain) · line 535
Quoted 4 lines
drivers/iommu/liveupdate.c +4
-0+ } + + iommu_device_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; + iommu->iommu_dev->ops->unpreserve_device (dev, iommu_device_ser);Nit: the driver callback gets an entry that is already marked DELETED. Call ->unpreserve_device() first and then set the flag, the same order as in
iommu_unpreserve_locked().
Agreed. Will update.
Quoted 36 lines
Quoted 36 lines
drivers/iommu/liveupdate.c +10
-0+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); +if (!ret) +iommu_group_unreserve_liveupdate (dev->iommu_group); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_device);include/linux/iommu-liveupdate.h +11
-0@@ -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,58 @@#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_has_preserved_state() - Check if a device has live update state +* @dev: Target device +* +* Unlike dev_iommu_preserved_state() and dev_iommu_restored_state(), this does +* not dereference the serialized state, so it is safe to call without holding +* a reference on the live update FLB. +* +* Return: true if @dev has preserved (outgoing) or restored (incoming) state.
Quoted 8 lines
include/linux/iommu-liveupdate.h line 33
Quoted 5 lines
include/linux/iommu-liveupdate.h +5
-0+*/ +static inline bool dev_iommu_has_preserved_state (struct device *dev) +{ +return dev->iommu &&READ_ONCE (dev->iommu->device_ser); +}Is this used anywhere? I couldn't find a caller in the tree. Clean it up?
I will remove this.
Quoted 15 lines
Quoted 15 lines
include/linux/iommu-liveupdate.h +14
-0+ +/** +* 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 ; +
Quoted 8 lines
include/linux/iommu-liveupdate.h line 53
Quoted 6 lines
include/linux/iommu-liveupdate.h +6
-0+ ser =READ_ONCE (dev->iommu->device_ser); +if (ser && !(ser->hdr.flags &IOMMU_SER_FLAG_INCOMING )) +return ser; + +return NULL ; +}nit: why not returning "struct
iommu_device_ser*" instead of "void *"?
I will update this and return struct iommu_device_ser *.
Quoted 13 lines
Quoted 13 lines
include/linux/iommu-liveupdate.h +10
-0+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 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.
Quoted 8 lines
include/linux/iommu-liveupdate.h line 70
Quoted 5 lines
include/linux/iommu-liveupdate.h +5
-0+*/ +static inline void *iommu_preserved_state (struct iommu_device *iommu) +{ +return iommu->iommu_ser; +}READ_ONCE()/WRITE_ONCE() used for
dev->iommu->device_ser, but not here. Please handle it consistently.
Will update.
Quoted 60 lines
Quoted 60 lines
include/linux/iommu-liveupdate.h +25
-0#else static inline int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler) {@@ -27,6 +78,16 @@ static inline void iommu_liveupdate_unregister_flb(struct liveupdate_file_handle { } +static inline bool dev_iommu_has_preserved_state (struct device *dev) +{ +return false; +} + +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 +96,20 @@ static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iommstatic 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 device *dev) +{ +} + +static inline void *iommu_preserved_state (struct iommu_device *iommu) +{ +return NULL ; +}#endif #endif /* _LINUX_IOMMU_LIVEUPDATE_H */ include/linux/iommu.h +5
-0@@ -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
Quoted 8 lines
include/linux/iommu.h struct iommu_ops { · line 752
Quoted 5 lines
include/linux/iommu.h +5
-0+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 nit: How about preserve_iommu/unpreserve_iommu, to match preserve_device/unpreserve_device?
Yes that looks good. I will update this.
Quoted 36 lines
Quoted 36 lines
include/linux/iommu.h +8
-0+const struct iommu_domain_ops *default_domain_ops;struct module *owner;struct iommu_domain *identity_domain;@@ -829,6 +840,7 @@ 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 +* @iommu_ser: preserved iommu state of outgoing kernel for liveupdate. */ struct iommu_device {struct list_head list;@@ -838,6 +850,10 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_hw_ser *iommu_ser; +#endif };/** @@ -877,6 +893,8 @@ struct iommu_fault_param {* @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs * @require_direct: device requires IOMMU_RESV_DIRECT regions * @shadow_on_flush: IOTLB flushes are used to sync shadow tables +* @device_ser: preserved (outgoing) or restored (incoming) liveupdate state +* of this device * * TODO: migrate other per device data pointers under iommu_dev_data, e.g. * struct iommu_group *iommu_group; @@ -892,6 +910,9 @@ struct dev_iommu { u32 pci_32bit_workaround:1; u32 require_direct:1; u32 shadow_on_flush:1;
Quoted 8 lines
include/linux/iommu.h struct dev_iommu { · line 918
Quoted 5 lines
include/linux/iommu.h +3
-0+#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_device_ser *device_ser; +#endif }; int iommu_device_register(struct iommu_device *iommu,Thanks,
baolu
Thanks,
Sami
PATCH v8 7/18 iommu/vt-d: Implement device and iommu preserve/unpreserve ops
In reply to Samiullah KhawajaAdd 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.
--- MAINTAINERS | 8 + drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 9 +- drivers/iommu/intel/iommu.h | 13 ++ drivers/iommu/intel/liveupdate.c | 246 +++++++++++++++++++++++++++++++ include/linux/kho/abi/iommu.h | 28 ++++ 6 files changed, 303 insertions(+), 2 deletions(-) create mode 100644 drivers/iommu/intel/liveupdate.c
MAINTAINERS +8 -0
@@ -13630,6 +13630,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 <[email protected]> +M: Lu Baolu <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git +F: drivers/iommu/intel/liveupdate.c + INTEL IPU BRIDGE M: Sakari Ailus <[email protected]> M: Dan Scally <[email protected]>
drivers/iommu/intel/Makefile +1 -0
@@ -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
drivers/iommu/intel/iommu.c +7 -2
@@ -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>
@@ -56,8 +57,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.
@@ -4070,6 +4069,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)
drivers/iommu/intel/iommu.h +13 -0
@@ -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
@@ -1305,6 +1307,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,
drivers/iommu/intel/liveupdate.c new file +246 -0
246 lines not on this page · raw message
include/linux/kho/abi/iommu.h +28 -0
@@ -77,12 +77,14 @@/** * enum iommu_type_ser - Type of the IOMMU being preserved * @IOMMU_SER_TYPE_INVALID: Invalid type of IOMMU +* @IOMMU_SER_TYPE_INTEL: Intel VT-d IOMMU * * IOMMU type is stored in the IOMMU HW state to differentiate between various * IOMMU HWs. */ enum iommu_type_ser {IOMMU_SER_TYPE_INVALID , +IOMMU_SER_TYPE_INTEL , };#define IOMMU_SER_FLAG_DELETED BIT(0)
@@ -156,18 +158,44 @@ struct iommu_device_ser {u64 reserved[2 ]; } __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 +* @features: Bitmask of Intel IOMMU specific features +* @sm_enabled: Whether the IOMMU was used in scalable mode +* @padding: Padding +* @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 { +u32 features; +u8 sm_enabled; +u8 padding[3 ]; +u64 phys_addr; +u64 root_table; +u64 context_tables_bitmap[VTD_PRESERVED_BITMAP_LONGS ]; +} __packed; +/** * struct iommu_hw_ser - Serialized state of an IOMMU instance * @hdr: Common object header * @token: Unique token to identify the IOMMU instance * @type: IOMMU type serialized state belongs to * @reserved: Reserved for future common fields. +* @intel: Intel specific serialization data */ struct iommu_hw_ser {struct iommu_hdr_ser hdr;u64 token;u64 type;u64 reserved[2 ]; +union { +struct iommu_intel_ser intel; + }; } __packed;/**
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v8 8/18 iommu/vt-d: Clear unpreserved context entries during shutdown
In reply to Samiullah KhawajaDuring 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 be removed.
Only the translation state of the preserved IOMMU is preserved. The next kernel sets up its own invalidation queue and fault and page request interrupts, so disable the invalidation queue and mask these interrupts during shutdown.
--- drivers/iommu/intel/iommu.c | 15 ++- drivers/iommu/intel/iommu.h | 5 + drivers/iommu/intel/liveupdate.c | 166 +++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +13 -2
@@ -1967,6 +1967,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);
@@ -2512,8 +2520,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)) +intel_iommu_shutdown_preserved (drhd); +else +iommu_disable_translation (iommu); } }
drivers/iommu/intel/iommu.h +5 -0
@@ -1316,6 +1316,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 intel_iommu_shutdown_preserved (struct dmar_drhd_unit *drhd); +#else +static inline void intel_iommu_shutdown_preserved (struct dmar_drhd_unit *drhd) +{ +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM
drivers/iommu/intel/liveupdate.c +166 -0
172 lines not on this page · raw message
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v8 9/18 iommu: Add APIs to get iommu and device restored state
In reply to Samiullah KhawajaThe restored state of the device, domain and IOMMU needs to be fetched during boot in the next kernel. Add APIs that can be used to fetch the restored state of a device, domain and IOMMU. The APIs will only be used after liveupdate so no locking needed.
--- drivers/iommu/liveupdate.c | 88 ++++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 70 +++++++++++++++++++++++++ 2 files changed, 158 insertions(+)
drivers/iommu/liveupdate.c +88 -0
@@ -61,6 +61,14 @@ struct iommu_ser_set {u64 *head_pa; }; +/* Iterate over the serialized objects of a set, skipping the deleted ones */ +#define iommu_ser_set_for_each (_set, _it, _obj) \ + for (kho_block_set_it_init(&(_it), &(_set)->bs); \ + ((_obj) = kho_block_set_it_read_entry(&(_it)));) \ + if ((_obj)->hdr.flags & IOMMU_SER_FLAG_DELETED) \ + continue; \ + else +struct iommu_flb_obj {/* Protects the serialized state and the object sets */ struct mutex lock;
@@ -273,6 +281,86 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) }EXPORT_SYMBOL_GPL (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) +{ +int ret; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)flb_objp); +if (ret == -ENODATA || ret == -ENOENT || ret == -EOPNOTSUPP ) +return ret; + +if (ret) +panic ("Failed to restore IOMMU Live Update FLB\n" ); + +return 0 ; +} + +/** +* iommu_for_each_restored_device() - Iterate on restored devices +* @fn: Iterator function to call for each restored device +* @arg: Argument to pass to iterator function +* +* Return: 0 on success, or an error. +*/ +int iommu_for_each_restored_device (iommu_restored_device_iter_fn fn, +void *arg) +{ +struct iommu_device_ser *device_ser; +struct iommu_flb_obj *flb_obj; +struct kho_block_set_it it; +int ret; + + ret =iommu_liveupdate_flb_get_incoming (&flb_obj); +if (ret) +return ret; + +iommu_ser_set_for_each (&flb_obj->device_set, it, device_ser) { + ret =fn (device_ser, arg); +if (ret) +break ; + } + +liveupdate_flb_put_incoming (&iommu_flb); +return ret; +} +EXPORT_SYMBOL_GPL (iommu_for_each_restored_device); + +/** +* iommu_get_restored_state() - Get restored state of 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_restored_state (u64 token,enum iommu_type_ser type) +{ +struct iommu_hw_ser *iommu_ser; +struct iommu_flb_obj *flb_obj; +struct kho_block_set_it it; +int ret; + + ret =iommu_liveupdate_flb_get_incoming (&flb_obj); +if (ret) +return NULL ; + +iommu_ser_set_for_each (&flb_obj->iommu_set, it, iommu_ser) { +if (iommu_ser->token == token && iommu_ser->type == type) +break ; + } + +liveupdate_flb_put_incoming (&iommu_flb); +return iommu_ser; +} +EXPORT_SYMBOL_GPL (iommu_get_restored_state); +/** * iommu_preserve_domain() - Preserve an IOMMU domain across live update * @domain: Domain to preserve
include/linux/iommu-liveupdate.h +70 -0
88 lines not on this page · raw message
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v8 10/18 iommu/vt-d: Combine disable_dmar_iommu() and free_dmar_iommu()
In reply to Samiullah KhawajaEvery caller of disable_dmar_iommu() immediately follows it with a call to free_dmar_iommu(). Combine the two helpers into release_dmar_iommu() and simplify the teardown and error paths in init_dmars(), intel_iommu_add(), and dmar_iommu_hotplug().
Also drop the early return when WARN_ON(!ida_is_empty(&iommu->domain_ida)) triggers so translation is still disabled before freeing the IOMMU resources.
--- drivers/iommu/intel/iommu.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-)
drivers/iommu/intel/iommu.c +10 -20
@@ -985,21 +985,17 @@ 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 ; +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 ;
@@ -1867,10 +1863,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; }
@@ -2280,12 +2274,12 @@ 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;iommu_set_root_entry (iommu);iommu_enable_translation (iommu);
@@ -2293,10 +2287,8 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru)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; }
@@ -2310,12 +2302,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; }
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v8 11/18 iommu/vt-d: Restore IOMMU state and reclaimed domain ids
In reply to Samiullah KhawajaDuring 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.
--- drivers/iommu/intel/iommu.c | 86 ++++++++++++++++--- drivers/iommu/intel/iommu.h | 14 ++++ drivers/iommu/intel/liveupdate.c | 139 +++++++++++++++++++++++++++++++ 3 files changed, 226 insertions(+), 13 deletions(-)
drivers/iommu/intel/iommu.c +73 -13
@@ -987,6 +987,8 @@ static void iommu_disable_translation(struct intel_iommu *iommu)static void release_dmar_iommu (struct intel_iommu *iommu) { +intel_iommu_handle_drhd_restore_failure (iommu); +/* * All iommu domains must have been detached from the devices, * hence there should be no domain IDs in use.
@@ -1452,7 +1454,7 @@ static void intel_iommu_init_qi(struct intel_iommu *iommu)* Returns 0 when the ID is reserved, was already reserved, or cannot be * re-assigned, and a negative errno for a genuine allocation failure. */ -static int reserve_domain_id (struct intel_iommu *iommu,int did) +int reserve_domain_id (struct intel_iommu *iommu,int did) {int ret;
@@ -1743,12 +1745,16 @@ 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_restored_state (iommu->reg_phys, +IOMMU_SER_TYPE_INTEL );if (drhd->ignored) { +intel_iommu_handle_drhd_restore_failure (iommu);iommu_disable_translation (iommu);continue ; }
@@ -1766,7 +1772,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);
@@ -1775,14 +1783,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" );
@@ -1818,7 +1830,11 @@ 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_restored_state (iommu->reg_phys, +IOMMU_SER_TYPE_INTEL ); +if (!iommu_ser) +iommu_set_root_entry (iommu); }check_tylersburg_isoch ();
@@ -2247,6 +2263,15 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru)struct intel_iommu *iommu = dmaru->iommu;int ret; +/* +* Hot-add of an IOMMU with preserved state is not supported. A +* preserved IOMMU is restored during boot, so it should never get +* here. +*/ +if (WARN_ON (iommu_get_restored_state (iommu->reg_phys, +IOMMU_SER_TYPE_INTEL ))) +return -EBUSY ; +/* * Disable translation if already enabled prior to OS handover. */
@@ -2689,6 +2714,33 @@ static __init void tboot_force_iommu(void) no_iommu =0 ; } +/* +* Devices preserved across a live update keep doing DMA through the preserved +* translation tables, so the IOMMUs can't be turned off. Force them on if the +* previous kernel preserved any IOMMU state. +*/ +static void __init liveupdate_force_iommu (void ) +{ +struct dmar_drhd_unit *drhd; + +if (dmar_policy_on ()) +return ; + +for_each_drhd_unit (drhd) { +if (!iommu_get_restored_state (drhd->reg_base_addr, +IOMMU_SER_TYPE_INTEL )) +continue ; + +pr_warn ("Forcing Intel-IOMMU to enabled for live update\n" ); + +/* No concurrent access to dmar_policy at this point. */ + dmar_policy =DMAR_FORCE_ON ; + no_iommu =0 ; +pci_request_acs (); +return ; + } +} +int __init intel_iommu_init (void ) {int ret = -ENODEV ;
@@ -2716,6 +2768,8 @@ int __init intel_iommu_init(void) goto out_free_dmar; } +liveupdate_force_iommu (); +up_write (&dmar_global_lock);/*
@@ -2797,7 +2851,8 @@ int __init intel_iommu_init(void)* to avoid unnecessary lock order splat. */ up_read (&dmar_global_lock); -iommu_device_register (&iommu->iommu, &intel_iommu_ops,NULL ); +if (iommu_device_register (&iommu->iommu, &intel_iommu_ops,NULL )) +intel_iommu_handle_drhd_restore_failure (iommu);down_read (&dmar_global_lock);iommu_pmu_register (iommu);
@@ -2808,7 +2863,12 @@ int __init intel_iommu_init(void)/* Finally, we enable the DMA remapping hardware. */ for_each_iommu (iommu, drhd) { -if (!drhd->ignored && !translation_pre_enabled (iommu)) +/* +* The translation of a restored IOMMU was never disabled, so +* don't touch its GCMD register. +*/ +if (!drhd->ignored && !translation_pre_enabled (iommu) && + !iommu_get_restored_state (iommu->reg_phys,IOMMU_SER_TYPE_INTEL ))iommu_enable_translation (iommu);iommu_disable_protect_mem_regions (iommu);
drivers/iommu/intel/iommu.h +14 -0
@@ -1307,6 +1307,8 @@ static inline int iopf_for_domain_replace(struct iommu_domain *new,return 0 ; } +int reserve_domain_id (struct intel_iommu *iommu,int did); +#ifdef CONFIG_IOMMU_LIVEUPDATE int intel_iommu_preserve_device (struct device *dev,struct iommu_device_ser *device_ser);
@@ -1317,10 +1319,22 @@ int intel_iommu_preserve(struct iommu_device *iommu,void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_hw_ser *iommu_ser);void intel_iommu_shutdown_preserved (struct dmar_drhd_unit *drhd); +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_hw_ser *iommu_ser); +void intel_iommu_handle_drhd_restore_failure (struct intel_iommu *iommu);#else static inline void intel_iommu_shutdown_preserved (struct dmar_drhd_unit *drhd) { } + +static inline void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_hw_ser *iommu_ser) +{ +} + +static inline void intel_iommu_handle_drhd_restore_failure (struct intel_iommu *iommu) +{ +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM
drivers/iommu/intel/liveupdate.c +139 -0
145 lines not on this page · raw message
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Baolu Lu
maintainerRe: [PATCH v8 11/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
Quoted 6 lines · Samiullah Khawaja
During boot fetch the preserved state of IOMMU unit and if found then restore the state.
- Reuse the
root_tablethat 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.
The commit message is out of date. This change also forces the iommu on when preserved state exists, rejects hot-plug of a restored unit and panics on any restore failure. Please add these changes and the reasons for them.
Nit: "reclaimed domain ids" in the subject should be "reclaim domain IDs", and "for each preserved devices" should be "for each preserved device".
Quoted 13 lines
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 86 ++++++++++++++++--- drivers/iommu/intel/iommu.h | 14 ++++ drivers/iommu/intel/liveupdate.c | 139 +++++++++++++++++++++++++++++++ 3 files changed, 226 insertions(+), 13 deletions(-)drivers/iommu/intel/iommu.c +0
-0
drivers/iommu/intel/iommu.c static void iommu_disable_translation(struct intel_iommu *iommu) · line 990
Quoted 4 lines
drivers/iommu/intel/iommu.c +1
-0@@ -987,6 +987,8 @@ static void iommu_disable_translation(struct intel_iommu *iommu)static void release_dmar_iommu (struct intel_iommu *iommu) { +intel_iommu_handle_drhd_restore_failure (iommu);
The name is misleading. This function doesn't handle anything, it just panics if the iommu has retored state. Perhaps rename it something like intel_iommu_assert_not_restored()?
Quoted 15 lines
drivers/iommu/intel/iommu.c +2
-1+/* * All iommu domains must have been detached from the devices, * hence there should be no domain IDs in use. @@ -1452,7 +1454,7 @@ static void intel_iommu_init_qi(struct intel_iommu *iommu)* Returns 0 when the ID is reserved, was already reserved, or cannot be * re-assigned, and a negative errno for a genuine allocation failure. */ -static int reserve_domain_id (struct intel_iommu *iommu,int did) +int reserve_domain_id (struct intel_iommu *iommu,int did) {int ret;@@ -1743,12 +1745,16 @@ static int copy_translation_tables(struct intel_iommu *iommu)
drivers/iommu/intel/iommu.c static int copy_translation_tables(struct intel_iommu *iommu) · line 1755
Quoted 10 lines
drivers/iommu/intel/iommu.c +3
-0static 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_restored_state (iommu->reg_phys, +IOMMU_SER_TYPE_INTEL );
iommu_get_restored_state() is called for the same iommu in five places in iommu.c. How aobut look it up once here and cache it in struct intel_iommu, and use the cached value in all the other places?
Quoted 20 lines
drivers/iommu/intel/iommu.c +4
-1if (drhd->ignored) { +intel_iommu_handle_drhd_restore_failure (iommu);iommu_disable_translation (iommu);continue ; }@@ -1766,7 +1772,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);@@ -1775,14 +1783,18 @@ static int __init init_dmars(void) iommu->name); }
drivers/iommu/intel/iommu.c static int __init init_dmars(void) · line 1787
Quoted 10 lines
drivers/iommu/intel/iommu.c +2
-8-/* -* 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);
The check that the hardware still matches the preserved state (TES, RTADDR, SMT) runs here. How about run the sanity checks first, right after above lookup, so that on a mismatch we panic before changing anything.
Quoted 57 lines
drivers/iommu/intel/iommu.c +35
-1+ }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" );@@ -1818,7 +1830,11 @@ 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_restored_state (iommu->reg_phys, +IOMMU_SER_TYPE_INTEL ); +if (!iommu_ser) +iommu_set_root_entry (iommu); }check_tylersburg_isoch ();@@ -2247,6 +2263,15 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru)struct intel_iommu *iommu = dmaru->iommu;int ret; +/* +* Hot-add of an IOMMU with preserved state is not supported. A +* preserved IOMMU is restored during boot, so it should never get +* here. +*/ +if (WARN_ON (iommu_get_restored_state (iommu->reg_phys, +IOMMU_SER_TYPE_INTEL ))) +return -EBUSY ; +/* * Disable translation if already enabled prior to OS handover. */ @@ -2689,6 +2714,33 @@ static __init void tboot_force_iommu(void) no_iommu =0 ; } +/* +* Devices preserved across a live update keep doing DMA through the preserved +* translation tables, so the IOMMUs can't be turned off. Force them on if the +* previous kernel preserved any IOMMU state. +*/ +static void __init liveupdate_force_iommu (void ) +{ +struct dmar_drhd_unit *drhd; + +if (dmar_policy_on ()) +return ;
drivers/iommu/intel/iommu.c static __init void tboot_force_iommu(void) · line 2737
Quoted 10 lines
drivers/iommu/intel/iommu.c +10
-0+ +for_each_drhd_unit (drhd) { +if (!iommu_get_restored_state (drhd->reg_base_addr, +IOMMU_SER_TYPE_INTEL )) +continue ; + +pr_warn ("Forcing Intel-IOMMU to enabled for live update\n" ); + +/* No concurrent access to dmar_policy at this point. */ + dmar_policy =DMAR_FORCE_ON ;
This skips the precedence rules in dmar_can_force_on(). In particular, it overrides DMAR_FW_OFF, which no force_on may override. Please add a DMAR_FORCEON_LIVEUPDATE level to dmar_can_force_on(), document where it sits in the precedence, and panic if it can't be honored.
drivers/iommu/intel/iommu.c static __init void tboot_force_iommu(void) · line 2739
Quoted 2 lines
drivers/iommu/intel/iommu.c +2
-0+ no_iommu =0 ; +pci_request_acs ();
pci_request_acs() here comes too late for devices that have already been enumerated.
Quoted 40 lines
drivers/iommu/intel/iommu.c +14
-2+return ; + } +} +int __init intel_iommu_init (void ) {int ret = -ENODEV ;@@ -2716,6 +2768,8 @@ int __init intel_iommu_init(void) goto out_free_dmar; } +liveupdate_force_iommu (); +up_write (&dmar_global_lock);/* @@ -2797,7 +2851,8 @@ int __init intel_iommu_init(void)* to avoid unnecessary lock order splat. */ up_read (&dmar_global_lock); -iommu_device_register (&iommu->iommu, &intel_iommu_ops,NULL ); +if (iommu_device_register (&iommu->iommu, &intel_iommu_ops,NULL )) +intel_iommu_handle_drhd_restore_failure (iommu);down_read (&dmar_global_lock);iommu_pmu_register (iommu);@@ -2808,7 +2863,12 @@ int __init intel_iommu_init(void)/* Finally, we enable the DMA remapping hardware. */ for_each_iommu (iommu, drhd) { -if (!drhd->ignored && !translation_pre_enabled (iommu)) +/* +* The translation of a restored IOMMU was never disabled, so +* don't touch its GCMD register. +*/ +if (!drhd->ignored && !translation_pre_enabled (iommu) && + !iommu_get_restored_state (iommu->reg_phys,IOMMU_SER_TYPE_INTEL ))iommu_enable_translation (iommu);iommu_disable_protect_mem_regions (iommu);
[...]
Quoted 80 lines
drivers/iommu/intel/liveupdate.c +72
-0@@ -304,6 +304,145 @@ static int preserve_iommu_context_tables(struct device_domain_info *info)return 0 ; } +static int restore_context_table (struct intel_iommu *iommu, +struct iommu_hw_ser *ser, +u8 bus,u8 devfn,u64 root_val) +{ +bool present = root_val &1 ; + +/* +* The root entries of unpreserved context tables are cleared during +* shutdown, so the preserved bitmap must match the present bits. +*/ +if (is_context_table_preserved (iommu, ser, bus, devfn) != present) +return -EINVAL ; + +if (present) +iommu_restore_pages (root_val &VTD_PAGE_MASK ); + +return 0 ; +} + +static int restore_iommu_context (struct intel_iommu *iommu, +struct iommu_hw_ser *ser) +{ +struct root_entry *root; +int ret; +int i; + +for (i =0 ; i <ROOT_ENTRY_NR ; i++) { + root = &iommu->root_entry[i]; + + ret =restore_context_table (iommu, ser, i,0 , root->lo); +if (ret) +return ret; + +if (!sm_supported (iommu)) +continue ; + + ret =restore_context_table (iommu, ser, i,0x80 , root->hi); +if (ret) +return ret; + } + +return 0 ; +} + +static int _restore_used_domain_ids (struct iommu_device_ser *ser,void *arg) +{ +int id = ser->attachment.attachment_id; +struct iommu_hw_ser *iommu_hw_ser; +struct intel_iommu *iommu = arg; + +if (WARN_ON (!ser->iommu_phys)) +return 0 ; + + iommu_hw_ser =phys_to_virt (ser->iommu_phys); +if (iommu_hw_ser->type !=IOMMU_SER_TYPE_INTEL ) +return 0 ; + +/* Only allocate domain ID from associated IOMMU HW unit */ +if (iommu_hw_ser->intel.phys_addr != iommu->reg_phys) +return 0 ; + +/* An out of range domain ID means the preserved state is corrupt. */ +if (id <0 || id >= iommu->max_domain_id) +return -EINVAL ; + +return reserve_domain_id (iommu, id); +} + +/** +* 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
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context_tables(struct device_domain_info *info) · line 388
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +10
-0+* +* 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. +* +* The preserved devices keep doing DMA through the preserved translation +* tables, so a failure to restore them is fatal. +*/ +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_hw_ser *iommu_ser)
The name is not accurate because this function also restores the context tables and reclaims domain IDs.
How about intel_iommu_liveupdate_restore()?
Quoted 50 lines
drivers/iommu/intel/liveupdate.c +50
-0+{ +u64 rtaddr; +u32 gsts; +int ret; + +/* +* The kernel command line might have changed across kexec, e.g. +* intel_iommu=sm_off, so make sure the preserved tables are in the +* format this kernel is going to use. +*/ +if (iommu_ser->intel.sm_enabled !=sm_supported (iommu)) +panic ("%s: preserved scalable mode %u does not match current %u\n" , + iommu->name, iommu_ser->intel.sm_enabled, +sm_supported (iommu)); + +/* +* The preserved devices are still translated by the hardware, so it +* must be using the preserved root table. +*/ + gsts =readl (iommu->reg +DMAR_GSTS_REG ); + rtaddr =readq (iommu->reg +DMAR_RTADDR_REG ); +if (!(gsts &DMA_GSTS_TES ) || + (rtaddr &VTD_PAGE_MASK ) != iommu_ser->intel.root_table || + !!(rtaddr &DMA_RTADDR_SMT ) != !!iommu_ser->intel.sm_enabled) +panic ("%s: hardware state does not match preserved state (GSTS 0x%x, RTADDR 0x%llx)\n" , + iommu->name, gsts, rtaddr); + +iommu_restore_pages (iommu_ser->intel.root_table); + iommu->root_entry =phys_to_virt (iommu_ser->intel.root_table); + + ret =restore_iommu_context (iommu, iommu_ser); +if (ret) +panic ("%s: failed to restore preserved context tables\n" , + iommu->name); + + ret =iommu_for_each_restored_device (_restore_used_domain_ids, iommu); +if (ret) +panic ("%s: failed to reclaim preserved domain IDs: %d\n" , + iommu->name, ret); +} + +/** +* intel_iommu_handle_drhd_restore_failure() - Handle DRHD restore failure +* @iommu: Target IOMMU +*/ +void intel_iommu_handle_drhd_restore_failure (struct intel_iommu *iommu) +{ +/* +* The preserved devices keep doing DMA through the preserved +* translation tables, so a failure to restore and register the IOMMU
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context_tables(struct device_domain_info *info) · line 448
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +7
-0+* is fatal. +*/ +if (iommu_get_restored_state (iommu->reg_phys,IOMMU_SER_TYPE_INTEL )) +panic ("%s: failed to restore IOMMU with preserved state\n" , + iommu->name); +} +/** * intel_iommu_preserve_device() - Intel IOMMU callback to preserve device state * @dev: Target device
Thanks,
baolu
Re: [PATCH v8 11/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
Quoted 8 lines · Baolu Lu
Quoted 6 lines · Samiullah Khawaja
During boot fetch the preserved state of IOMMU unit and if found then restore the state.
- Reuse the
root_tablethat 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.
Quoted 8 lines
The commit message is out of date. This change also forces the iommu on when preserved state exists, rejects hot-plug of a restored unit and panics on any restore failure. Please add these changes and the reasons for them.
Nit: "reclaimed domain ids" in the subject should be "reclaim domain IDs", and "for each preserved devices" should be "for each preserved device".
Will update.
Quoted 9 lines
Quoted 9 lines
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 86 ++++++++++++++++--- drivers/iommu/intel/iommu.h | 14 ++++ drivers/iommu/intel/liveupdate.c | 139 +++++++++++++++++++++++++++++++ 3 files changed, 226 insertions(+), 13 deletions(-)
Quoted 12 lines
drivers/iommu/intel/iommu.c static void iommu_disable_translation(struct intel_iommu *iommu) · line 989
Quoted 8 lines
drivers/iommu/intel/iommu.c +1
-0@@ -987,6 +987,8 @@ static void iommu_disable_translation(struct intel_iommu *iommu)static void release_dmar_iommu (struct intel_iommu *iommu) { +intel_iommu_handle_drhd_restore_failure (iommu);The name is misleading. This function doesn't handle anything, it just panics if the iommu has retored state. Perhaps rename it something like
intel_iommu_assert_not_restored()?
Yes, this is much better. Will update.
Quoted 19 lines
Quoted 19 lines
drivers/iommu/intel/iommu.c +3
-1+/* * All iommu domains must have been detached from the devices, * hence there should be no domain IDs in use. @@ -1452,7 +1454,7 @@ static void intel_iommu_init_qi(struct intel_iommu *iommu)* Returns 0 when the ID is reserved, was already reserved, or cannot be * re-assigned, and a negative errno for a genuine allocation failure. */ -static int reserve_domain_id (struct intel_iommu *iommu,int did) +int reserve_domain_id (struct intel_iommu *iommu,int did) {int ret;@@ -1743,12 +1745,16 @@ 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;
Quoted 8 lines
drivers/iommu/intel/iommu.c static int copy_translation_tables(struct intel_iommu *iommu) · line 1755
Quoted 4 lines
drivers/iommu/intel/iommu.c +2
-0int ret;for_each_iommu (iommu, drhd) { + iommu_ser =iommu_get_restored_state (iommu->reg_phys, +IOMMU_SER_TYPE_INTEL );
iommu_get_restored_state()is called for the same iommu in five places iniommu.c. How aobut look it up once here and cache it in structintel_iommu, and use the cached value in all the other places?
Hmm.. yes I think that can be done. But we only want to keep that set during early boot during iommu init. We can actually use the "iommu_ser" in struct iommu_device for this and unset it for the iommus when the init is done.
Quoted 25 lines
Quoted 25 lines
drivers/iommu/intel/iommu.c +4
-8if (drhd->ignored) { +intel_iommu_handle_drhd_restore_failure (iommu);iommu_disable_translation (iommu);continue ; }@@ -1766,7 +1772,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);@@ -1775,14 +1783,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)
Quoted 8 lines
drivers/iommu/intel/iommu.c static int __init init_dmars(void) · line 1787
Quoted 3 lines
drivers/iommu/intel/iommu.c +2
-1- goto free_iommu; +if (iommu_ser) { +intel_iommu_liveupdate_restore_root_table (iommu, iommu_ser);The check that the hardware still matches the preserved state (TES, RTADDR, SMT) runs here. How about run the sanity checks first, right after above lookup, so that on a mismatch we panic before changing anything.
Sounds good. I can wrap the lookup, the sanity check and the iommu_ser caching into one function intel_iommu_liveupdate_restore() that is implemented in liveupdate.c. This can call iommu_get_restored_state() internally.
Quoted 61 lines
Quoted 61 lines
drivers/iommu/intel/iommu.c +42
-1+ }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" );@@ -1818,7 +1830,11 @@ 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_restored_state (iommu->reg_phys, +IOMMU_SER_TYPE_INTEL ); +if (!iommu_ser) +iommu_set_root_entry (iommu); }check_tylersburg_isoch ();@@ -2247,6 +2263,15 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru)struct intel_iommu *iommu = dmaru->iommu;int ret; +/* +* Hot-add of an IOMMU with preserved state is not supported. A +* preserved IOMMU is restored during boot, so it should never get +* here. +*/ +if (WARN_ON (iommu_get_restored_state (iommu->reg_phys, +IOMMU_SER_TYPE_INTEL ))) +return -EBUSY ; +/* * Disable translation if already enabled prior to OS handover. */ @@ -2689,6 +2714,33 @@ static __init void tboot_force_iommu(void) no_iommu =0 ; } +/* +* Devices preserved across a live update keep doing DMA through the preserved +* translation tables, so the IOMMUs can't be turned off. Force them on if the +* previous kernel preserved any IOMMU state. +*/ +static void __init liveupdate_force_iommu (void ) +{ +struct dmar_drhd_unit *drhd; + +if (dmar_policy_on ()) +return ; + +for_each_drhd_unit (drhd) { +if (!iommu_get_restored_state (drhd->reg_base_addr, +IOMMU_SER_TYPE_INTEL )) +continue ; + +pr_warn ("Forcing Intel-IOMMU to enabled for live update\n" );
Quoted 8 lines
drivers/iommu/intel/iommu.c static __init void tboot_force_iommu(void) · line 2737
Quoted 3 lines
drivers/iommu/intel/iommu.c +3
-0+ +/* No concurrent access to dmar_policy at this point. */ + dmar_policy =DMAR_FORCE_ON ;This skips the precedence rules in
dmar_can_force_on(). In particular, it overridesDMAR_FW_OFF, which noforce_onmay override. Please add aDMAR_FORCEON_LIVEUPDATElevel todmar_can_force_on(), document where it sits in the precedence, and panic if it can't be honored.
See below.
Quoted 6 lines
drivers/iommu/intel/iommu.c static __init void tboot_force_iommu(void) · line 2739
Quoted 3 lines
drivers/iommu/intel/iommu.c +2
-0+ no_iommu =0 ; +pci_request_acs ();
pci_request_acs()here comes too late for devices that have already been enumerated.
Hmm.. the ACS configuration of the preserved devices should also be preserved and pci_enable_acs() is called during driver probe so most devices will get it.
But this is still inconsistent and it can miss the non-preserved devices/bridges that are probed before this point.
Doing the force on properly needs the check in dmar_required(), so pci_request_acs() is called early like it is done for tboot and platform opt-in. That would require changing the point at which the KHO/LUO data is available during boot. Basically this:
https://lore.kernel.org/all/[email protected]/
I think we can panic here if preserved state is found and the IOMMU is not enabled, instead of doing force_on, and add separate patches later to add force on logic once the KHO/LUO stuff is moved early. This way the pci_request_acs() will be done at the right time also.
Note that doing a check here and doing a panic() is not too late as the ACS configuration of the preserved devices (including their parent bridges) is also preserved and the IOMMU state is also intact at this point.
Quoted 132 lines
Quoted 36 lines
drivers/iommu/intel/iommu.c +14
-2+return ; + } +} +int __init intel_iommu_init (void ) {int ret = -ENODEV ;@@ -2716,6 +2768,8 @@ int __init intel_iommu_init(void) goto out_free_dmar; } +liveupdate_force_iommu (); +up_write (&dmar_global_lock);/* @@ -2797,7 +2851,8 @@ int __init intel_iommu_init(void)* to avoid unnecessary lock order splat. */ up_read (&dmar_global_lock); -iommu_device_register (&iommu->iommu, &intel_iommu_ops,NULL ); +if (iommu_device_register (&iommu->iommu, &intel_iommu_ops,NULL )) +intel_iommu_handle_drhd_restore_failure (iommu);down_read (&dmar_global_lock);iommu_pmu_register (iommu);@@ -2808,7 +2863,12 @@ int __init intel_iommu_init(void)/* Finally, we enable the DMA remapping hardware. */ for_each_iommu (iommu, drhd) { -if (!drhd->ignored && !translation_pre_enabled (iommu)) +/* +* The translation of a restored IOMMU was never disabled, so +* don't touch its GCMD register. +*/ +if (!drhd->ignored && !translation_pre_enabled (iommu) && + !iommu_get_restored_state (iommu->reg_phys,IOMMU_SER_TYPE_INTEL ))iommu_enable_translation (iommu);iommu_disable_protect_mem_regions (iommu);[...]
Quoted 79 lines
drivers/iommu/intel/liveupdate.c +72
-0@@ -304,6 +304,145 @@ static int preserve_iommu_context_tables(struct device_domain_info *info)return 0 ; } +static int restore_context_table (struct intel_iommu *iommu, +struct iommu_hw_ser *ser, +u8 bus,u8 devfn,u64 root_val) +{ +bool present = root_val &1 ; + +/* +* The root entries of unpreserved context tables are cleared during +* shutdown, so the preserved bitmap must match the present bits. +*/ +if (is_context_table_preserved (iommu, ser, bus, devfn) != present) +return -EINVAL ; + +if (present) +iommu_restore_pages (root_val &VTD_PAGE_MASK ); + +return 0 ; +} + +static int restore_iommu_context (struct intel_iommu *iommu, +struct iommu_hw_ser *ser) +{ +struct root_entry *root; +int ret; +int i; + +for (i =0 ; i <ROOT_ENTRY_NR ; i++) { + root = &iommu->root_entry[i]; + + ret =restore_context_table (iommu, ser, i,0 , root->lo); +if (ret) +return ret; + +if (!sm_supported (iommu)) +continue ; + + ret =restore_context_table (iommu, ser, i,0x80 , root->hi); +if (ret) +return ret; + } + +return 0 ; +} + +static int _restore_used_domain_ids (struct iommu_device_ser *ser,void *arg) +{ +int id = ser->attachment.attachment_id; +struct iommu_hw_ser *iommu_hw_ser; +struct intel_iommu *iommu = arg; + +if (WARN_ON (!ser->iommu_phys)) +return 0 ; + + iommu_hw_ser =phys_to_virt (ser->iommu_phys); +if (iommu_hw_ser->type !=IOMMU_SER_TYPE_INTEL ) +return 0 ; + +/* Only allocate domain ID from associated IOMMU HW unit */ +if (iommu_hw_ser->intel.phys_addr != iommu->reg_phys) +return 0 ; + +/* An out of range domain ID means the preserved state is corrupt. */ +if (id <0 || id >= iommu->max_domain_id) +return -EINVAL ; + +return reserve_domain_id (iommu, id); +} + +/** +* 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 drivers/iommu/intel/liveupdate.c static int preserve_iommu_context_tables(struct device_domain_info *info) · line 387
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +10
-0+* +* 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. +* +* The preserved devices keep doing DMA through the preserved translation +* tables, so a failure to restore them is fatal. +*/ +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_hw_ser *iommu_ser)The name is not accurate because this function also restores the context tables and reclaims domain IDs.
Quoted 1 line
How about
intel_iommu_liveupdate_restore()?
As you suggested above and in my reply, I can move the sanity checks and other initialization like reclaiming domain ids to a separate function intel_iommu_liveupdate_restore(). And this function can continue to restore only root table and context tables?
Or this function can be renamed and called earlier in the lookup loop, but that means the root_entry will be set earlier and that is inconsistent.
Quoted 56 lines
Quoted 56 lines
drivers/iommu/intel/liveupdate.c +55
-0+{ +u64 rtaddr; +u32 gsts; +int ret; + +/* +* The kernel command line might have changed across kexec, e.g. +* intel_iommu=sm_off, so make sure the preserved tables are in the +* format this kernel is going to use. +*/ +if (iommu_ser->intel.sm_enabled !=sm_supported (iommu)) +panic ("%s: preserved scalable mode %u does not match current %u\n" , + iommu->name, iommu_ser->intel.sm_enabled, +sm_supported (iommu)); + +/* +* The preserved devices are still translated by the hardware, so it +* must be using the preserved root table. +*/ + gsts =readl (iommu->reg +DMAR_GSTS_REG ); + rtaddr =readq (iommu->reg +DMAR_RTADDR_REG ); +if (!(gsts &DMA_GSTS_TES ) || + (rtaddr &VTD_PAGE_MASK ) != iommu_ser->intel.root_table || + !!(rtaddr &DMA_RTADDR_SMT ) != !!iommu_ser->intel.sm_enabled) +panic ("%s: hardware state does not match preserved state (GSTS 0x%x, RTADDR 0x%llx)\n" , + iommu->name, gsts, rtaddr); + +iommu_restore_pages (iommu_ser->intel.root_table); + iommu->root_entry =phys_to_virt (iommu_ser->intel.root_table); + + ret =restore_iommu_context (iommu, iommu_ser); +if (ret) +panic ("%s: failed to restore preserved context tables\n" , + iommu->name); + + ret =iommu_for_each_restored_device (_restore_used_domain_ids, iommu); +if (ret) +panic ("%s: failed to reclaim preserved domain IDs: %d\n" , + iommu->name, ret); +} + +/** +* intel_iommu_handle_drhd_restore_failure() - Handle DRHD restore failure +* @iommu: Target IOMMU +*/ +void intel_iommu_handle_drhd_restore_failure (struct intel_iommu *iommu) +{ +/* +* The preserved devices keep doing DMA through the preserved +* translation tables, so a failure to restore and register the IOMMU +* is fatal. +*/ +if (iommu_get_restored_state (iommu->reg_phys,IOMMU_SER_TYPE_INTEL )) +panic ("%s: failed to restore IOMMU with preserved state\n" , + iommu->name);
Quoted 9 lines
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context_tables(struct device_domain_info *info) · line 448
Quoted 5 lines
drivers/iommu/intel/liveupdate.c +2
-0+} +/** * intel_iommu_preserve_device() - Intel IOMMU callback to preserve device state * @dev: Target device Thanks,
baolu
Thanks,
Sami
Baolu Lu
maintainerRe: [PATCH v8 11/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
Quoted 68 lines · Samiullah Khawaja
Quoted 39 lines
Quoted 39 lines
drivers/iommu/intel/iommu.c +11
-0@@ -2247,6 +2263,15 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru) struct intel_iommu *iommu = dmaru->iommu; int ret; + /* + * Hot-add of an IOMMU with preserved state is not supported. A + * preserved IOMMU is restored during boot, so it should never get + * here. + */ + if (WARN_ON(iommu_get_restored_state(iommu->reg_phys, + IOMMU_SER_TYPE_INTEL))) + return -EBUSY; + /** Disable translation if already enabled prior to OS handover. */ @@ -2689,6 +2714,33 @@ static __init void tboot_force_iommu(void) no_iommu = 0; } +/* +* Devices preserved across a live update keep doing DMA through the preserved
+ * translation tables, so the IOMMUs can't be turned off. Force them on if the
+ * previous kernel preserved any IOMMU state.
+ */drivers/iommu/intel/iommu.c +13
-0+static void __init liveupdate_force_iommu (void ) +{ + struct dmar_drhd_unit *drhd; + + if (dmar_policy_on()) + return; + + for_each_drhd_unit(drhd) { + if (!iommu_get_restored_state(drhd->reg_base_addr, + IOMMU_SER_TYPE_INTEL)) + continue; + + pr_warn("Forcing Intel-IOMMU to enabled for live update\n");Quoted 8 lines
Quoted 3 lines
+
+ /* No concurrent access todmar_policyat this point. */ +dmar_policy=DMAR_FORCE_ON;This skips the precedence rules in
dmar_can_force_on(). In particular, it overridesDMAR_FW_OFF, which noforce_onmay override. Please add aDMAR_FORCEON_LIVEUPDATElevel todmar_can_force_on(), document where it sits in the precedence, and panic if it can't be honored.See below.
Quoted 6 lines
Quoted 3 lines
+
no_iommu= 0;
+pci_request_acs();
pci_request_acs()here comes too late for devices that have already been enumerated.Hmm.. the ACS configuration of the preserved devices should also be preserved and
pci_enable_acs()is called during driver probe so most devices will get it.But this is still inconsistent and it can miss the non-preserved devices/bridges that are probed before this point.
Doing the force on properly needs the check in
dmar_required(), sopci_request_acs()is called early like it is done for tboot and platform opt-in. That would require changing the point at which the KHO/LUO data is available during boot. Basically this:
Quoted 4 lines
https://lore.kernel.org/all/[email protected]/
I think we can panic here if preserved state is found and the IOMMU is not enabled, instead of doing
force_on, and add separate patches later
Sounds good to me. We only need to support it if a real use case arises in the future.
Quoted 7 lines
to add force on logic once the KHO/LUO stuff is moved early. This way the
pci_request_acs()will be done at the right time also.Note that doing a check here and doing a
panic()is not too late as the ACS configuration of the preserved devices (including their parent bridges) is also preserved and the IOMMU state is also intact at this point.
Thanks,
baolu
Baolu Lu
maintainerRe: [PATCH v8 11/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
In reply to Samiullah KhawajaQuoted 104 lines · Samiullah Khawaja
Quoted 102 lines
Quoted 90 lines
drivers/iommu/intel/liveupdate.c +8
-0@@ -304,6 +304,145 @@ static int preserve_iommu_context_tables(struct device_domain_info *info) return 0; } +static int restore_context_table (struct intel_iommu *iommu, + struct iommu_hw_ser *ser, + u8 bus, u8 devfn, u64 root_val) +{ + bool present = root_val & 1; + + /* +* The root entries of unpreserved context tables are cleared during
+ * shutdown, so the preserved bitmap must match the present bits. + */
+ if (is_context_table_preserved(iommu, ser, bus, devfn)!= present) + return-EINVAL;drivers/iommu/intel/liveupdate.c +33
-0+ + if (present) + iommu_restore_pages(root_val & VTD_PAGE_MASK); + + return 0; +} + +static int restore_iommu_context (struct intel_iommu *iommu, + struct iommu_hw_ser *ser) +{ + struct root_entry *root; + int ret; + int i; + + for (i = 0; i < ROOT_ENTRY_NR; i++) { + root = &iommu->root_entry[i]; + + ret = restore_context_table(iommu, ser, i, 0, root->lo); + if (ret) + return ret; + + if (!sm_supported(iommu)) + continue; + + ret = restore_context_table(iommu, ser, i, 0x80, root->hi); + if (ret) + return ret; + } + + return 0; +} + +static int _restore_used_domain_ids(struct iommu_device_ser *ser,void *arg)
drivers/iommu/intel/liveupdate.c +17
-0+{ + int id = ser->attachment.attachment_id; + struct iommu_hw_ser *iommu_hw_ser; + struct intel_iommu *iommu = arg; + + if (WARN_ON(!ser->iommu_phys)) + return 0; + + iommu_hw_ser = phys_to_virt(ser->iommu_phys); + if (iommu_hw_ser->type != IOMMU_SER_TYPE_INTEL) + return 0; + + /* Only allocate domain ID from associated IOMMU HW unit */ + if (iommu_hw_ser->intel.phys_addr != iommu->reg_phys) + return 0; + + /* An out of range domain ID means the preserved state iscorrupt. */
+ if (id < 0 || id >=iommu->max_domain_id)
+ return-EINVAL;drivers/iommu/intel/liveupdate.c +6
-0+ + return reserve_domain_id(iommu, id); +} + +/** +* 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.Quoted 8 lines
+ *
+ * The preserved devices keep doing DMA through the preserved translation
+ * tables, so a failure to restore them is fatal.
+ */
+void intel_iommu_liveupdate_restore_root_table(structintel_iommu*iommu,
+ structiommu_hw_ser*iommu_ser)The name is not accurate because this function also restores the context tables and reclaims domain IDs.
Quoted 1 line
How about
intel_iommu_liveupdate_restore()?
Quoted 8 lines
As you suggested above and in my reply, I can move the sanity checks and other initialization like reclaiming domain ids to a separate function
intel_iommu_liveupdate_restore(). And this function can continue to restore only root table and context tables?Or this function can be renamed and called earlier in the lookup loop, but that means the
root_entrywill be set earlier and that is inconsistent.
Yes, either way works for me.
Thanks,
baolu
PATCH v8 12/18 iommu/vt-d: Preserve PASID table of preserved device
In reply to Samiullah KhawajaIn scalable mode the PASID table is used to fetch the io page tables. Preserve and restore the PASID table of the preserved devices.
--- drivers/iommu/intel/iommu.c | 3 +- drivers/iommu/intel/liveupdate.c | 144 +++++++++++++++++++++++++++++-- drivers/iommu/intel/pasid.c | 10 ++- drivers/iommu/intel/pasid.h | 8 ++ include/linux/kho/abi/iommu.h | 20 +++++ 5 files changed, 177 insertions(+), 8 deletions(-)
drivers/iommu/intel/iommu.c +2 -1
@@ -3504,7 +3504,8 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev) goto clear_rbtree; } -if (!context_copied (iommu, info->bus, info->devfn)) { +if (!context_copied (iommu, info->bus, info->devfn) && + !dev_iommu_restored_state (dev)) { ret =intel_pasid_setup_sm_context (dev);if (ret) goto free_table;
drivers/iommu/intel/liveupdate.c +139 -5
181 lines not on this page · raw message
drivers/iommu/intel/pasid.c +8 -2
@@ -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 ;
drivers/iommu/intel/pasid.h +8 -0
@@ -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,
include/linux/kho/abi/iommu.h +20 -0
@@ -138,6 +138,22 @@ struct iommu_attachment_ser {u32 padding; } __packed; +/** +* struct iommu_device_intel_ser - Intel specific state of serialized device +* @features: Bitmask of Intel device specific features +* @restored: Whether the device state is restored +* @padding: Padding +* @pasid_table: Physical address of pasid table +* @max_pasid: Maximum supported pasid +*/ +struct iommu_device_intel_ser { +u32 features; +u8 restored; +u8 padding[3 ]; +u64 pasid_table; +u64 max_pasid; +} __packed; +/** * struct iommu_device_ser - Serialized state of a device * @hdr: Common object header
@@ -147,6 +163,7 @@ struct iommu_attachment_ser {* @iommu_phys: Physical address of the serialized IOMMU the device is behind * @attachment: Attachment of the device to its domain * @reserved: Reserved for future common fields. +* @intel: Intel specific serialization data */ struct iommu_device_ser {struct iommu_hdr_ser hdr;
@@ -156,6 +173,9 @@ struct iommu_device_ser {u64 iommu_phys;struct iommu_attachment_ser attachment;u64 reserved[2 ]; +union { +struct iommu_device_intel_ser intel; + }; } __packed;/* There are maximum 256 buses, so maximum 512 context tables */
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v8 13/18 iommu: Restore and reattach preserved domains to devices
In reply to Samiullah KhawajaDuring 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.
--- drivers/iommu/iommu.c | 164 ++++++++++++++++++++++++++++--- drivers/iommu/liveupdate.c | 106 ++++++++++++++++++++ include/linux/iommu-liveupdate.h | 13 +++ 3 files changed, 268 insertions(+), 15 deletions(-)
drivers/iommu/iommu.c +149 -15
@@ -163,6 +163,8 @@ static void __iommu_group_set_domain_nofail(struct iommu_group *group, 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); static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
@@ -513,6 +515,10 @@ static int iommu_init_device(struct device *dev) goto err_free; } +#ifdef CONFIG_IOMMU_LIVEUPDATE +iommu_init_device_restored_data (dev); +#endif + iommu_dev = ops->probe_device (dev);if (IS_ERR (iommu_dev)) { ret =PTR_ERR (iommu_dev);
@@ -577,7 +583,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;/*
@@ -635,11 +642,39 @@ static bool iommu_group_liveupdate_reserved(struct iommu_group *group)lockdep_assert_held (&group->mutex);return group->liveupdate_reserved; } + +static int __iommu_group_reserve_liveupdate (struct iommu_group *group) +{ +lockdep_assert_held (&group->mutex); +if (group->liveupdate_reserved) +return -EBUSY ; + +if (!list_is_singular (&group->devices)) +return -EOPNOTSUPP ; + + group->liveupdate_reserved = true; +return 0 ; +} + +static void __iommu_group_unreserve_liveupdate (struct iommu_group *group) +{ +lockdep_assert_held (&group->mutex); + group->liveupdate_reserved = false; +}#else static inline bool iommu_group_liveupdate_reserved (struct iommu_group *group) {return false; } + +static inline int __iommu_group_reserve_liveupdate (struct iommu_group *group) +{ +return -EOPNOTSUPP ; +} + +static inline void __iommu_group_unreserve_liveupdate (struct iommu_group *group) +{ +}#endif static int __iommu_probe_device (struct device *dev,struct list_head *group_list)
@@ -748,6 +783,29 @@ 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 ; + +__iommu_group_unreserve_liveupdate (group); + +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) {
@@ -759,13 +817,16 @@ 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->domain != group->default_domain && + !iommu_domain_restored_state (group->domain)));kfree (grp_dev->name);kfree (grp_dev);
@@ -778,6 +839,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 ;
@@ -3161,6 +3223,66 @@ 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; + +for_each_group_device (group, gdev) { +if (dev_is_pci (gdev->dev) &&dev_iommu_restored_state (gdev->dev)) +return gdev->dev; + } + +return NULL ; +} + +static int __iommu_device_restore_domain (struct device *dev) +{ +struct iommu_group *group = dev->iommu_group; +struct iommu_device_ser *device_ser; +struct iommu_domain *domain; +int ret; + +lockdep_assert_held (&group->mutex); +if (group->domain) +return -EBUSY ; + + device_ser =dev_iommu_restored_state (dev); +if (!device_ser) +return -ENOENT ; + +/* +* Ownership of groups with preserved devices is set during boot. These +* will be reclaimed later by the entity (iommufd) that preserved them. +* Claim ownership first so that even if the reservation or the domain +* restoration fails, no driver can bind to the device using +* default_domain. +*/ +WARN_ON (group->owner); + group->owner = device_ser; + group->owner_cnt =1 ; + +/* A restored device must be the only device in its group */ + ret =__iommu_group_reserve_liveupdate (group); +if (ret) +return ret; + + ret =__iommu_group_alloc_blocking_domain (group); +if (ret) +return ret; + +/* +* If attaching the restored domain fails below, the domain is not +* leaked; it remains cached in domain_ser->restored_domain and will be +* reused if the device is rescanned. +*/ + domain =iommu_restore_domain (dev, device_ser); +if (IS_ERR (domain)) +return PTR_ERR (domain); + +/* The restored domain is attached with the restored device. */ +return __iommu_group_set_domain (group, domain); +} +/** * iommu_setup_default_domain - Set the default_domain for the group * @group: Group to change
@@ -3177,9 +3299,10 @@ static int iommu_setup_default_domain(struct iommu_group *group,struct iommu_domain *old_dom = group->default_domain;struct group_device *gdev;struct iommu_domain *dom; +struct device *dev;bool direct_failed;int req_type; -int ret; +int ret =0 ;lockdep_assert_held (&group->mutex);
@@ -3219,6 +3342,18 @@ 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 restored domain. If the +* restore fails, the group stays owned, so the default_domain is never +* attached or used. +*/ + dev =__iommu_group_restored_device (group); +if (dev) { +WARN_ON (__iommu_device_restore_domain (dev)); + goto out_free_old; + } +if (!group->domain) {/* * Drivers are not allowed to fail the first domain attach.
@@ -3582,14 +3717,7 @@ EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed);int iommu_group_reserve_liveupdate (struct iommu_group *group) {guard (mutex)(&group->mutex); -if (group->liveupdate_reserved) -return -EBUSY ; - -if (!list_is_singular (&group->devices)) -return -EOPNOTSUPP ; - - group->liveupdate_reserved = true; -return 0 ; +return __iommu_group_reserve_liveupdate (group); }/**
@@ -3599,7 +3727,7 @@ int iommu_group_reserve_liveupdate(struct iommu_group *group)void iommu_group_unreserve_liveupdate (struct iommu_group *group) {guard (mutex)(&group->mutex); - group->liveupdate_reserved = false; +__iommu_group_unreserve_liveupdate (group); }#endif
@@ -4102,6 +4230,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 +4344,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);
drivers/iommu/liveupdate.c +106 -0
109 lines not on this page · raw message
include/linux/iommu-liveupdate.h +13 -0
@@ -99,8 +99,11 @@ static inline void *dev_iommu_restored_state(struct device *dev)return NULL ; } +struct iommu_domain *iommu_restore_domain (struct device *dev, +struct iommu_device_ser *ser);int iommu_for_each_restored_device (iommu_restored_device_iter_fn fn,void *arg); +void iommu_init_device_restored_data (struct device *dev);struct iommu_hw_ser *iommu_get_restored_state (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);
@@ -148,11 +151,21 @@ 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) +{ +return ERR_PTR (-EOPNOTSUPP ); +} +static inline int iommu_for_each_restored_device (iommu_restored_device_iter_fn fn,void *arg) {return -EOPNOTSUPP ; } +static inline void iommu_init_device_restored_data (struct device *dev) +{ +} +static inline struct iommu_hw_ser *iommu_get_restored_state (u64 token,enum iommu_type_ser type) {return NULL ;
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v8 13/18] iommu: Restore and reattach preserved domains to devices
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] iommu: inconsistent group domain state in iommu_setup_default_domain() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] iommu: inconsistent group domain state in iommu_setup_default_domain()
--
commit c2881b6423bf649bce482244af7dd51d680e43e3
Author: Samiullah Khawaja <[email protected]>
iommu: Restore and reattach preserved domains to devices
This commit restores preserved IOMMU domains via the iommupt restore() operation during default domain setup and reattaches them to preserved devices and their IOMMU groups.
Quoted 4 lines
drivers/iommu/iommu.c +0
-0
[ ... ]
Quoted 63 lines
drivers/iommu/iommu.c +59
-0@@ -3161,6 +3223,66 @@ 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; + +for_each_group_device (group, gdev) { +if (dev_is_pci (gdev->dev) &&dev_iommu_restored_state (gdev->dev)) +return gdev->dev; + } + +return NULL ; +} + +static int __iommu_device_restore_domain (struct device *dev) +{ +struct iommu_group *group = dev->iommu_group; +struct iommu_device_ser *device_ser; +struct iommu_domain *domain; +int ret; + +lockdep_assert_held (&group->mutex); +if (group->domain) +return -EBUSY ; + + device_ser =dev_iommu_restored_state (dev); +if (!device_ser) +return -ENOENT ; + +/* +* Ownership of groups with preserved devices is set during boot. These +* will be reclaimed later by the entity (iommufd) that preserved them. +* Claim ownership first so that even if the reservation or the domain +* restoration fails, no driver can bind to the device using +* default_domain. +*/ +WARN_ON (group->owner); + group->owner = device_ser; + group->owner_cnt =1 ; + +/* A restored device must be the only device in its group */ + ret =__iommu_group_reserve_liveupdate (group); +if (ret) +return ret; + + ret =__iommu_group_alloc_blocking_domain (group); +if (ret) +return ret; + +/* +* If attaching the restored domain fails below, the domain is not +* leaked; it remains cached in domain_ser->restored_domain and will be +* reused if the device is rescanned. +*/ + domain =iommu_restore_domain (dev, device_ser); +if (IS_ERR (domain)) +return PTR_ERR (domain); + +/* The restored domain is attached with the restored device. */ +return __iommu_group_set_domain (group, domain); +}
[ ... ]
Quoted 5 lines
drivers/iommu/iommu.c +1
-0@@ -3219,6 +3342,18 @@ 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; +
drivers/iommu/iommu.c static int iommu_setup_default_domain(struct iommu_group *group, · line 3355
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+/* +* Preserved devices need to be attached to the restored domain. If the +* restore fails, the group stays owned, so the default_domain is never +* attached or used. +*/ + dev =__iommu_group_restored_device (group); +if (dev) { +WARN_ON (__iommu_device_restore_domain (dev)); + goto out_free_old; + }
[Severity: Medium]
Can iommu_setup_default_domain() leave group->default_domain set while group->domain is NULL when __iommu_device_restore_domain() fails?
If __iommu_device_restore_domain() fails (for example, when __iommu_group_reserve_liveupdate() returns -EOPNOTSUPP because a sibling device was added to group->devices during bus_iommu_probe(), or when blocking domain allocation, domain restoration, or domain attachment fails), iommu_setup_default_domain() jumps to out_free_old and returns 0 with group->default_domain = dom and group->domain = NULL.
That breaks the invariant checked in __iommu_probe_device():
drivers/iommu/iommu.c:__iommu_probe_device() {
...
list_add_tail(&gdev->list, &group->devices);
WARN_ON(group->default_domain && !group->domain);
...
}If the group contains another device (or one is added while liveupdate_reserved is false) and the restored device is then removed, __iommu_group_remove_restored_device() clears group->owner and group->owner_cnt without attaching group->default_domain, and iommu_deinit_device() keeps group->default_domain because group->devices is non-empty. A driver can then bind to the remaining device via iommu_device_use_default_domain() while group->domain is still NULL and default_domain was never attached in hardware, and rescanning the removed device hits WARN_ON(group->default_domain && !group->domain) in __iommu_probe_device() without setting up a domain.
Also, if a non-restored sibling device in the same group is probed first with group_list == NULL (setting group->default_domain and group->domain) and the restored device is probed second, __iommu_probe_device() takes the if (group->domain) branch and attaches group->default_domain to the restored device instead of restoring its preserved domain.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
PATCH v8 14/18 iommu/vt-d: Handle reattach of the restored domain
In reply to Samiullah KhawajaReattach 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.
Note that in this phase nothing clears IOMMU_SER_FLAG_INCOMING of the restored domain or the restored state of the device, so the restored domain cannot be replaced yet. Replacing it is left to a later phase.
--- drivers/iommu/intel/iommu.c | 36 ++++++- drivers/iommu/intel/iommu.h | 20 ++++ drivers/iommu/intel/liveupdate.c | 170 +++++++++++++++++++++++++++++++ 3 files changed, 224 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +34 -2
@@ -3174,6 +3174,25 @@ intel_iommu_domain_alloc_second_stage(struct device *dev,return &dmar_domain->domain; } +/* +* A restored device translates through the preserved page table, so use the +* stage of its preserved RID_PASID entry. The stage allocators reject flags +* that the stage doesn't support. +*/ +static struct iommu_domain * +intel_iommu_domain_alloc_restored (struct device *dev,struct intel_iommu *iommu, +u32 flags) +{ +switch (intel_iommu_restored_pgtt (dev)) { +case PASID_ENTRY_PGTT_FL_ONLY : +return intel_iommu_domain_alloc_first_stage (dev, iommu, flags); +case PASID_ENTRY_PGTT_SL_ONLY : +return intel_iommu_domain_alloc_second_stage (dev, iommu, flags); +default : +return ERR_PTR (-EINVAL ); + } +} +static struct iommu_domain *intel_iommu_domain_alloc_paging_flags (struct device *dev,u32 flags,const struct iommu_user_data *user_data)
@@ -3185,6 +3204,9 @@ intel_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,if (user_data)return ERR_PTR (-EOPNOTSUPP ); +if (dev_iommu_restored_state (dev)) +return intel_iommu_domain_alloc_restored (dev, iommu, flags); +/* Prefer first stage if possible by default. */ domain =intel_iommu_domain_alloc_first_stage (dev, iommu, flags);if (domain !=ERR_PTR (-EOPNOTSUPP ))
@@ -3325,6 +3347,9 @@ static int intel_iommu_attach_device(struct iommu_domain *domain, {int ret; +if (dev_iommu_restored_state (dev)) +return intel_iommu_attach_restored_device (domain, dev); +device_block_translation (dev); ret =paging_domain_compatible (domain, dev);
@@ -3568,16 +3593,23 @@ static void intel_iommu_release_device(struct device *dev) info->pasid_enabled =0 ; } +if (dev_iommu_restored_state (dev)) +intel_iommu_detach_restored_device (dev); +mutex_lock (&iommu->iopf_lock);if (dev_is_pci (dev) &&pci_ats_supported (to_pci_dev (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); +else +kfree (info->pasid_table);intel_iommu_debugfs_remove_dev (info);kfree (info); }
drivers/iommu/intel/iommu.h +20 -0
@@ -1322,6 +1322,10 @@ void intel_iommu_shutdown_preserved(struct dmar_drhd_unit *drhd);void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu,struct iommu_hw_ser *iommu_ser);void intel_iommu_handle_drhd_restore_failure (struct intel_iommu *iommu); +int intel_iommu_attach_restored_device (struct iommu_domain *domain, +struct device *dev); +int intel_iommu_detach_restored_device (struct device *dev); +u16 intel_iommu_restored_pgtt (struct device *dev);#else static inline void intel_iommu_shutdown_preserved (struct dmar_drhd_unit *drhd) {
@@ -1335,6 +1339,22 @@ static inline void intel_iommu_liveupdate_restore_root_table(struct intel_iommustatic inline void intel_iommu_handle_drhd_restore_failure (struct intel_iommu *iommu) { } + +static inline int intel_iommu_attach_restored_device (struct iommu_domain *domain, +struct device *dev) +{ +return -EOPNOTSUPP ; +} + +static inline int intel_iommu_detach_restored_device (struct device *dev) +{ +return -EOPNOTSUPP ; +} + +static inline u16 intel_iommu_restored_pgtt (struct device *dev) +{ +return 0 ; +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM
drivers/iommu/intel/liveupdate.c +170 -0
176 lines not on this page · raw message
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v8 14/18] iommu/vt-d: Handle reattach of the restored domain
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] iommu/vt-d: guard(mutex) mixed with goto in domain_attach_restored_iommu() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] iommu/vt-d: guard(mutex) mixed with goto in domain_attach_restored_iommu()
--
commit 693fa762fb6f04c8a284677c5b4a1aeaceac5c22
Author: Samiullah Khawaja <[email protected]>
iommu/vt-d: Handle reattach of the restored domain
This commit implements reattachment and detachment of restored domains to preserved devices across liveupdate using the preserved domain ID, without modifying preserved context and PASID table entries.
Quoted 5 lines
drivers/iommu/intel/liveupdate.c +0
-0@@ -444,6 +444,176 @@ void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu)
[ ... ]
Quoted 9 lines
drivers/iommu/intel/liveupdate.c +9
-0+static int domain_attach_restored_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;
[ ... ]
Quoted 21 lines
drivers/iommu/intel/liveupdate.c +21
-0+ info =kzalloc_obj (*info); +if (!info) +return -ENOMEM ; + +guard (mutex)(&iommu->did_lock); + curr =xa_load (&domain->iommu_array, iommu->seq_id); +if (curr) { +kfree (info); +if (WARN_ON (curr->did != restored_did)) +return -EINVAL ; + + curr->refcnt++; +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) {
drivers/iommu/intel/liveupdate.c void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu) · line 527
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +10
-0+ ret =xa_err (curr) ? : -EBUSY ; + goto err_unlock; + } + +return 0 ; + +err_unlock: +kfree (info); +return ret; +}
[Severity: Low]
This isn't a bug, but domain_attach_restored_iommu() mixes guard(mutex)(&iommu->did_lock) with a single-use goto err_unlock label (where the lock is still held at the label and released by the guard on return). Following cleanup.h conventions, info could be freed inline before returning on the xa_cmpxchg() failure path, or managed with __free(kfree).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Baolu Lu
maintainerRe: [PATCH v8 14/18] iommu/vt-d: Handle reattach of the restored domain
In reply to Samiullah KhawajaQuoted 46 lines · Samiullah Khawaja
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.
Note that in this phase nothing clears
IOMMU_SER_FLAG_INCOMINGof the restored domain or the restored state of the device, so the restored domain cannot be replaced yet. Replacing it is left to a later phase.Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 36 ++++++- drivers/iommu/intel/iommu.h | 20 ++++ drivers/iommu/intel/liveupdate.c | 170 +++++++++++++++++++++++++++++++ 3 files changed, 224 insertions(+), 2 deletions(-)drivers/iommu/intel/iommu.c +19
-0@@ -3174,6 +3174,25 @@ intel_iommu_domain_alloc_second_stage(struct device *dev,return &dmar_domain->domain; } +/* +* A restored device translates through the preserved page table, so use the +* stage of its preserved RID_PASID entry. The stage allocators reject flags +* that the stage doesn't support. +*/ +static struct iommu_domain * +intel_iommu_domain_alloc_restored (struct device *dev,struct intel_iommu *iommu, +u32 flags) +{ +switch (intel_iommu_restored_pgtt (dev)) { +case PASID_ENTRY_PGTT_FL_ONLY : +return intel_iommu_domain_alloc_first_stage (dev, iommu, flags); +case PASID_ENTRY_PGTT_SL_ONLY : +return intel_iommu_domain_alloc_second_stage (dev, iommu, flags); +default : +return ERR_PTR (-EINVAL ); + } +} +static struct iommu_domain *intel_iommu_domain_alloc_paging_flags (struct device *dev,u32 flags,const struct iommu_user_data *user_data)
drivers/iommu/intel/iommu.c intel_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags, · line 3208
Quoted 5 lines
drivers/iommu/intel/iommu.c +2
-0@@ -3185,6 +3204,9 @@ intel_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,if (user_data)return ERR_PTR (-EOPNOTSUPP ); +if (dev_iommu_restored_state (dev)) +return intel_iommu_domain_alloc_restored (dev, iommu, flags);
Hmm, this check is based on the device's state, not on the caller's intent. Could the restore path in iommu core tell the driver explicitly (through a @flags bit) that it is allocating the restored domain? Add a attribute for this domain, ...
drivers/iommu/intel/iommu.c static int intel_iommu_attach_device(struct iommu_domain *domain, · line 3351
Quoted 10 lines
drivers/iommu/intel/iommu.c +3
-0+/* Prefer first stage if possible by default. */ domain =intel_iommu_domain_alloc_first_stage (dev, iommu, flags);if (domain !=ERR_PTR (-EOPNOTSUPP ))@@ -3325,6 +3347,9 @@ static int intel_iommu_attach_device(struct iommu_domain *domain, {int ret; +if (dev_iommu_restored_state (dev)) +return intel_iommu_attach_restored_device (domain, dev);
... and, here check the domain attribute instead.
Quoted 16 lines
drivers/iommu/intel/iommu.c +4
-1+device_block_translation (dev); ret =paging_domain_compatible (domain, dev);@@ -3568,16 +3593,23 @@ static void intel_iommu_release_device(struct device *dev) info->pasid_enabled =0 ; } +if (dev_iommu_restored_state (dev)) +intel_iommu_detach_restored_device (dev); +> mutex_lock(&iommu->iopf_lock);if (dev_is_pci (dev) &&pci_ats_supported (to_pci_dev (dev)))device_rbtree_remove (info);mutex_unlock (&iommu->iopf_lock); - if (sm_supported(iommu) && !dev_is_real_dma_subdevice(dev) &&
drivers/iommu/intel/iommu.c static void intel_iommu_release_device(struct device *dev) · line 3611
Quoted 10 lines
drivers/iommu/intel/iommu.c +6
-1+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); +else +kfree (info->pasid_table);
Nit: dev_iommu_restored_state() is now evaluated three times in this function. Read it once into a local variable?
Quoted 119 lines
drivers/iommu/intel/iommu.c +0
-0intel_iommu_debugfs_remove_dev (info);kfree (info); }drivers/iommu/intel/iommu.h +20
-0@@ -1322,6 +1322,10 @@ void intel_iommu_shutdown_preserved(struct dmar_drhd_unit *drhd);void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu,struct iommu_hw_ser *iommu_ser);void intel_iommu_handle_drhd_restore_failure (struct intel_iommu *iommu); +int intel_iommu_attach_restored_device (struct iommu_domain *domain, +struct device *dev); +int intel_iommu_detach_restored_device (struct device *dev); +u16 intel_iommu_restored_pgtt (struct device *dev);#else static inline void intel_iommu_shutdown_preserved (struct dmar_drhd_unit *drhd) {@@ -1335,6 +1339,22 @@ static inline void intel_iommu_liveupdate_restore_root_table(struct intel_iommustatic inline void intel_iommu_handle_drhd_restore_failure (struct intel_iommu *iommu) { } + +static inline int intel_iommu_attach_restored_device (struct iommu_domain *domain, +struct device *dev) +{ +return -EOPNOTSUPP ; +} + +static inline int intel_iommu_detach_restored_device (struct device *dev) +{ +return -EOPNOTSUPP ; +} + +static inline u16 intel_iommu_restored_pgtt (struct device *dev) +{ +return 0 ; +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM drivers/iommu/intel/liveupdate.c +70
-0@@ -444,6 +444,176 @@ void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu) iommu->name); } +static void domain_detach_restored_iommu (struct dmar_domain *domain, +struct intel_iommu *iommu) +{ +struct iommu_domain_info *info; + +/* +* Unlike domain_detach_iommu(), the restored domain ID is not freed +* here. A restored device can be removed and probed again while its +* preserved context and PASID entries still use this domain ID, so it +* stays reserved. Once iommufd reclaims the restored domain, the +* normal detach path frees it. +*/ +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_attach_restored_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->attachment.domain_phys || + !device_ser->iommu_phys) +return -EINVAL ; + + domain_ser =phys_to_virt (device_ser->attachment.domain_phys); +if (domain_ser->restored_domain != &domain->domain) +return -EINVAL ; + + iommu_hw_ser =phys_to_virt (device_ser->iommu_phys); +if (iommu_hw_ser->type !=IOMMU_SER_TYPE_INTEL || + iommu_hw_ser->intel.phys_addr != iommu->reg_phys) +return -EINVAL ; + + restored_did = device_ser->attachment.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) { +kfree (info); +if (WARN_ON (curr->did != restored_did)) +return -EINVAL ; + + curr->refcnt++; +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 );
drivers/iommu/intel/liveupdate.c void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu) · line 526
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +10
-0+if (curr) { + ret =xa_err (curr) ? : -EBUSY ; + goto err_unlock; + } + +return 0 ; + +err_unlock: +kfree (info); +return ret;
nit: cleanup.h asks no mix of goto-type unwind and guard(). Just kfree() and return inline.
Quoted 67 lines
drivers/iommu/intel/liveupdate.c +67
-0+} + +/** +* intel_iommu_attach_restored_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_attach_restored_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 ; + + ret =domain_attach_restored_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); + +return 0 ; +} + +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 ; + + info->domain_attached = false; + +spin_lock_irqsave (&info->domain->lock, flags); +list_del (&info->link); +spin_unlock_irqrestore (&info->domain->lock, flags); + +domain_detach_restored_iommu (info->domain, iommu); + info->domain =NULL ; + +return 0 ; +} + +/** +* intel_iommu_restored_pgtt() - Get the translation type of a restored device +* @dev: Restored device +* +* Return: PGTT of the preserved RID_PASID entry of @dev, or 0 if the entry is +* not present. Without a preserved PASID table the IOMMU is in legacy mode,
drivers/iommu/intel/liveupdate.c void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu) · line 603
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +10
-0+* which only supports second stage, so PASID_ENTRY_PGTT_SL_ONLY is returned. +*/ +u16 intel_iommu_restored_pgtt (struct device *dev) +{ +struct iommu_device_ser *ser =dev_iommu_restored_state (dev); +struct pasid_entry *table, *pte; +struct pasid_dir_entry *dir; + +if (!ser->intel.pasid_table) +return PASID_ENTRY_PGTT_SL_ONLY ;
Legacy vs. scalable mode should be decided with sm_supported(iommu), not by whether a PASID table was preserved. In scalable mode, a restored device without a preserved PASID table is an error, but here it is silently treated as second stage.
The PASID table has already been restored into info->pasid_table in intel_iommu_probe_device(). It's simpler to look up the RID_PASID entry through that instead of parsing the serialized physical address again.
Quoted 6 lines
drivers/iommu/intel/liveupdate.c +6
-0+ + dir =phys_to_virt (ser->intel.pasid_table); + table =get_pasid_table_from_pde (&dir[IOMMU_NO_PASID >>PASID_PDE_SHIFT ]); +if (!table) +return 0 ; +
drivers/iommu/intel/liveupdate.c void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu) · line 619
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +7
-0+ pte = &table[IOMMU_NO_PASID &PASID_PTE_MASK ]; +if (!pasid_pte_is_present (pte)) +return 0 ; + +return pasid_pte_get_pgtt (pte); +} +enum pasid_lu_op {PASID_LU_OP_PRESERVE =1 ,PASID_LU_OP_UNPRESERVE ,
Thanks,
baolu
Re: [PATCH v8 14/18] iommu/vt-d: Handle reattach of the restored domain
Quoted 47 lines · Baolu Lu
Quoted 46 lines · Samiullah Khawaja
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.
Note that in this phase nothing clears
IOMMU_SER_FLAG_INCOMINGof the restored domain or the restored state of the device, so the restored domain cannot be replaced yet. Replacing it is left to a later phase.Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 36 ++++++- drivers/iommu/intel/iommu.h | 20 ++++ drivers/iommu/intel/liveupdate.c | 170 +++++++++++++++++++++++++++++++ 3 files changed, 224 insertions(+), 2 deletions(-)drivers/iommu/intel/iommu.c +19
-0@@ -3174,6 +3174,25 @@ intel_iommu_domain_alloc_second_stage(struct device *dev,return &dmar_domain->domain; } +/* +* A restored device translates through the preserved page table, so use the +* stage of its preserved RID_PASID entry. The stage allocators reject flags +* that the stage doesn't support. +*/ +static struct iommu_domain * +intel_iommu_domain_alloc_restored (struct device *dev,struct intel_iommu *iommu, +u32 flags) +{ +switch (intel_iommu_restored_pgtt (dev)) { +case PASID_ENTRY_PGTT_FL_ONLY : +return intel_iommu_domain_alloc_first_stage (dev, iommu, flags); +case PASID_ENTRY_PGTT_SL_ONLY : +return intel_iommu_domain_alloc_second_stage (dev, iommu, flags); +default : +return ERR_PTR (-EINVAL ); + } +} +static struct iommu_domain *intel_iommu_domain_alloc_paging_flags (struct device *dev,u32 flags,const struct iommu_user_data *user_data)@@ -3185,6 +3204,9 @@ intel_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,if (user_data)
Quoted 8 lines
drivers/iommu/intel/iommu.c intel_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags, · line 3208
Quoted 3 lines
drivers/iommu/intel/iommu.c +2
-0return ERR_PTR (-EOPNOTSUPP ); +if (dev_iommu_restored_state (dev)) +return intel_iommu_domain_alloc_restored (dev, iommu, flags);Hmm, this check is based on the device's state, not on the caller's intent. Could the restore path in iommu core tell the driver explicitly (through a @flags bit) that it is allocating the restored domain? Add a attribute for this domain, ...
Yes, that is more deterministic. I will add a flag for this and use it here.
Quoted 12 lines
drivers/iommu/intel/iommu.c static int intel_iommu_attach_device(struct iommu_domain *domain, · line 3350
Quoted 10 lines
drivers/iommu/intel/iommu.c +3
-0+/* Prefer first stage if possible by default. */ domain =intel_iommu_domain_alloc_first_stage (dev, iommu, flags);if (domain !=ERR_PTR (-EOPNOTSUPP ))@@ -3325,6 +3347,9 @@ static int intel_iommu_attach_device(struct iommu_domain *domain, {int ret; +if (dev_iommu_restored_state (dev)) +return intel_iommu_attach_restored_device (domain, dev);... and, here check the domain attribute instead.
We still need to check here that the device is a restored device when taking the attach_restored_device() path. I will also check that the domain is a restored domain here.
Quoted 18 lines
Quoted 18 lines
drivers/iommu/intel/iommu.c +6
-1+device_block_translation (dev); ret =paging_domain_compatible (domain, dev);@@ -3568,16 +3593,23 @@ static void intel_iommu_release_device(struct device *dev) info->pasid_enabled =0 ; } +if (dev_iommu_restored_state (dev)) +intel_iommu_detach_restored_device (dev); +> mutex_lock(&iommu->iopf_lock);if (dev_is_pci (dev) &&pci_ats_supported (to_pci_dev (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);
Quoted 8 lines
drivers/iommu/intel/iommu.c static void intel_iommu_release_device(struct device *dev) · line 3612
Quoted 5 lines
drivers/iommu/intel/iommu.c +4
-1-intel_pasid_free_table (dev); +if (!dev_iommu_restored_state (dev)) +intel_pasid_free_table (dev); +else +kfree (info->pasid_table);Nit:
dev_iommu_restored_state()is now evaluated three times in this function. Read it once into a local variable?
Agreed. Will update.
Quoted 123 lines
Quoted 123 lines
drivers/iommu/intel/iommu.c +0
-0intel_iommu_debugfs_remove_dev (info);kfree (info); }drivers/iommu/intel/iommu.h +20
-0@@ -1322,6 +1322,10 @@ void intel_iommu_shutdown_preserved(struct dmar_drhd_unit *drhd);void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu,struct iommu_hw_ser *iommu_ser);void intel_iommu_handle_drhd_restore_failure (struct intel_iommu *iommu); +int intel_iommu_attach_restored_device (struct iommu_domain *domain, +struct device *dev); +int intel_iommu_detach_restored_device (struct device *dev); +u16 intel_iommu_restored_pgtt (struct device *dev);#else static inline void intel_iommu_shutdown_preserved (struct dmar_drhd_unit *drhd) {@@ -1335,6 +1339,22 @@ static inline void intel_iommu_liveupdate_restore_root_table(struct intel_iommustatic inline void intel_iommu_handle_drhd_restore_failure (struct intel_iommu *iommu) { } + +static inline int intel_iommu_attach_restored_device (struct iommu_domain *domain, +struct device *dev) +{ +return -EOPNOTSUPP ; +} + +static inline int intel_iommu_detach_restored_device (struct device *dev) +{ +return -EOPNOTSUPP ; +} + +static inline u16 intel_iommu_restored_pgtt (struct device *dev) +{ +return 0 ; +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM drivers/iommu/intel/liveupdate.c +75
-0@@ -444,6 +444,176 @@ void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu) iommu->name); } +static void domain_detach_restored_iommu (struct dmar_domain *domain, +struct intel_iommu *iommu) +{ +struct iommu_domain_info *info; + +/* +* Unlike domain_detach_iommu(), the restored domain ID is not freed +* here. A restored device can be removed and probed again while its +* preserved context and PASID entries still use this domain ID, so it +* stays reserved. Once iommufd reclaims the restored domain, the +* normal detach path frees it. +*/ +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_attach_restored_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->attachment.domain_phys || + !device_ser->iommu_phys) +return -EINVAL ; + + domain_ser =phys_to_virt (device_ser->attachment.domain_phys); +if (domain_ser->restored_domain != &domain->domain) +return -EINVAL ; + + iommu_hw_ser =phys_to_virt (device_ser->iommu_phys); +if (iommu_hw_ser->type !=IOMMU_SER_TYPE_INTEL || + iommu_hw_ser->intel.phys_addr != iommu->reg_phys) +return -EINVAL ; + + restored_did = device_ser->attachment.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) { +kfree (info); +if (WARN_ON (curr->did != restored_did)) +return -EINVAL ; + + curr->refcnt++; +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; + } +
Quoted 8 lines
drivers/iommu/intel/liveupdate.c void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu) · line 526
Quoted 5 lines
drivers/iommu/intel/liveupdate.c +5
-0+return 0 ; + +err_unlock: +kfree (info); +return ret;nit:
cleanup.hasks no mix of goto-type unwind andguard(). Justkfree()and return inline.
This was also pointed out by sashiko, but I kept it as is since it is done this way in the intel/iommu.c file in domain_attach_iommu(). I will update this.
I can send out a separate patch to update iommu.c file also for consistency.
Quoted 75 lines
Quoted 75 lines
drivers/iommu/intel/liveupdate.c +74
-0+} + +/** +* intel_iommu_attach_restored_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_attach_restored_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 ; + + ret =domain_attach_restored_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); + +return 0 ; +} + +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 ; + + info->domain_attached = false; + +spin_lock_irqsave (&info->domain->lock, flags); +list_del (&info->link); +spin_unlock_irqrestore (&info->domain->lock, flags); + +domain_detach_restored_iommu (info->domain, iommu); + info->domain =NULL ; + +return 0 ; +} + +/** +* intel_iommu_restored_pgtt() - Get the translation type of a restored device +* @dev: Restored device +* +* Return: PGTT of the preserved RID_PASID entry of @dev, or 0 if the entry is +* not present. Without a preserved PASID table the IOMMU is in legacy mode, +* which only supports second stage, so PASID_ENTRY_PGTT_SL_ONLY is returned. +*/ +u16 intel_iommu_restored_pgtt (struct device *dev) +{ +struct iommu_device_ser *ser =dev_iommu_restored_state (dev); +struct pasid_entry *table, *pte; +struct pasid_dir_entry *dir;
Quoted 8 lines
drivers/iommu/intel/liveupdate.c void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu) · line 603
Quoted 3 lines
drivers/iommu/intel/liveupdate.c +3
-0+ +if (!ser->intel.pasid_table) +return PASID_ENTRY_PGTT_SL_ONLY ;Legacy vs. scalable mode should be decided with
sm_supported(iommu), not by whether a PASID table was preserved. In scalable mode, a restored device without a preserved PASID table is an error, but here it is silently treated as second stage.
Agreed. The sm_supported(iommu) and pasid_table preservation is already checked early during restore to sanity check that the configuration of the new kernel matches with the previous kernel. Here we can check sm_supported(iommu) again to use second stage.
Quoted 4 lines
The PASID table has already been restored into
info->pasid_tableinintel_iommu_probe_device(). It's simpler to look up theRID_PASIDentry through that instead of parsing the serialized physical address again.
This sounds good to me. I will update this.
Quoted 12 lines
Quoted 12 lines
drivers/iommu/intel/liveupdate.c +11
-0+ + dir =phys_to_virt (ser->intel.pasid_table); + table =get_pasid_table_from_pde (&dir[IOMMU_NO_PASID >>PASID_PDE_SHIFT ]); +if (!table) +return 0 ; + + pte = &table[IOMMU_NO_PASID &PASID_PTE_MASK ]; +if (!pasid_pte_is_present (pte)) +return 0 ; + +return pasid_pte_get_pgtt (pte);
Quoted 9 lines
drivers/iommu/intel/liveupdate.c void intel_iommu_handle_drhd_restore_failure(struct intel_iommu *iommu) · line 619
Quoted 5 lines
drivers/iommu/intel/liveupdate.c +2
-0+} +enum pasid_lu_op {PASID_LU_OP_PRESERVE =1 ,PASID_LU_OP_UNPRESERVE ,Thanks,
baolu
Thanks,
Sami
PATCH v8 15/18 iommufd: Implement ioctl to mark HWPT for preservation
In reply to Samiullah KhawajaFrom: YiFei Zhu <[email protected]>
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.
--- MAINTAINERS | 1 + drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/iommufd_private.h | 17 ++++++ drivers/iommu/iommufd/liveupdate.c | 75 +++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 9 +++ include/uapi/linux/iommufd.h | 31 ++++++++++ 6 files changed, 134 insertions(+) create mode 100644 drivers/iommu/iommufd/liveupdate.c
MAINTAINERS +1 -0
@@ -14152,6 +14152,7 @@ M: Samiullah Khawaja <[email protected]> R: Pranjal Shrivastava <[email protected]> L: [email protected] 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
drivers/iommu/iommufd/Makefile +1 -0
@@ -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
drivers/iommu/iommufd/iommufd_private.h +17 -0
@@ -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);
drivers/iommu/iommufd/liveupdate.c new file +75 -0
75 lines not on this page · raw message
drivers/iommu/iommufd/main.c +9 -0
@@ -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
include/uapi/linux/iommufd.h +31 -0
@@ -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, };/**
@@ -1398,4 +1399,34 @@ 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) +* @flags: Must be 0 +* @hwpt_id: HW pagetable ID to preserve for liveupdate +* @__reserved: Must be 0 +* @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 flags; +__u32 hwpt_id; +__u32 __reserved; +__aligned_u64 hwpt_token; +}; +#define IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE \ + _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_LIVEUPDATE_MARK_PRESERVE) +#endif
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
PATCH v8 16/18 iommufd: Persist iommu hardware pagetables for live update
In reply to Samiullah KhawajaFrom: YiFei Zhu <[email protected]>
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.
--- MAINTAINERS | 1 + drivers/iommu/iommufd/io_pagetable.c | 24 ++ drivers/iommu/iommufd/iommufd_private.h | 16 ++ drivers/iommu/iommufd/liveupdate.c | 344 ++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 10 +- include/linux/kho/abi/iommufd.h | 56 ++++ 6 files changed, 450 insertions(+), 1 deletion(-) create mode 100644 include/linux/kho/abi/iommufd.h
MAINTAINERS +1 -0
@@ -14156,6 +14156,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 <[email protected]>
drivers/iommu/iommufd/io_pagetable.c +24 -0
@@ -30,6 +30,19 @@ struct iopt_pages_list {unsigned long length; }; +static inline bool iopt_liveupdate_immutable (const struct io_pagetable *iopt) +{ +bool immutable = false; + +lockdep_assert_held (&iopt->domains_rwsem); + +#ifdef CONFIG_IOMMU_LIVEUPDATE +/* iopt becomes immutable once liveupdate preservation starts */ + immutable = iopt->num_preserved_domains; +#endif +return immutable; +} + struct iopt_area *iopt_area_contig_init(struct iopt_area_contig_iter *iter, struct io_pagetable *iopt, unsigned long iova,
@@ -384,6 +397,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;
@@ -758,6 +776,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);
drivers/iommu/iommufd/iommufd_private.h +16 -0
@@ -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 num_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,8 +742,20 @@ 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);#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 ;
drivers/iommu/iommufd/liveupdate.c +344 -0
362 lines not on this page · raw message
drivers/iommu/iommufd/main.c +9 -1
@@ -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);
include/linux/kho/abi/iommufd.h new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Baolu Lu
maintainerRe: [PATCH v8 16/18] iommufd: Persist iommu hardware pagetables for live update
Quoted 7 lines · Samiullah Khawaja
From: YiFei Zhu <[email protected]>
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_immutableflag to IOAS. When an HWPT is preserved, its IOAS
The "liveupdate_immutable flag" is out of date. The code uses iopt->num_preserved_domains instead. Please update it.
Quoted 20 lines
is marked immutable and any map/unmap attempts will fail with
-EBUSY. This is synchronized using thedomains_rwsemto 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 <[email protected]>Signed-off-by Samiullah Khawaja <[email protected]>--- MAINTAINERS | 1 + drivers/iommu/iommufd/io_pagetable.c | 24 ++ drivers/iommu/iommufd/iommufd_private.h | 16 ++ drivers/iommu/iommufd/liveupdate.c | 344 ++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 10 +- include/linux/kho/abi/iommufd.h | 56 ++++ 6 files changed, 450 insertions(+), 1 deletion(-)create mode 100644
include/linux/kho/abi/iommufd.h
[...]
Quoted 10 lines
drivers/iommu/iommufd/io_pagetable.c +2
-0@@ -30,6 +30,19 @@ struct iopt_pages_list {unsigned long length; }; +static inline bool iopt_liveupdate_immutable (const struct io_pagetable *iopt) +{
drivers/iommu/iommufd/io_pagetable.c struct iopt_pages_list { · line 44
Quoted 10 lines
drivers/iommu/iommufd/io_pagetable.c +10
-0+bool immutable = false; + +lockdep_assert_held (&iopt->domains_rwsem); + +#ifdef CONFIG_IOMMU_LIVEUPDATE +/* iopt becomes immutable once liveupdate preservation starts */ + immutable = iopt->num_preserved_domains; +#endif +return immutable; +}
Nit: drop the "inline" in a .c file.
Quoted 29 lines
drivers/iommu/iommufd/io_pagetable.c +12
-0+ struct iopt_area *iopt_area_contig_init(struct iopt_area_contig_iter *iter, struct io_pagetable *iopt, unsigned long iova,@@ -384,6 +397,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;@@ -758,6 +776,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);
[...]
Quoted 5 lines
drivers/iommu/iommufd/liveupdate.c +0
-0
drivers/iommu/iommufd/liveupdate.c line 17
Quoted 9 lines
drivers/iommu/iommufd/liveupdate.c +5
-0@@ -9,9 +9,28 @@#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>
Nit: keep the includes sorted. Also, nothing in this file seems to use linux/pci.h.
Quoted 226 lines
drivers/iommu/iommufd/liveupdate.c +210
-0#include "iommufd_private.h" +#include "io_pagetable.h" + +static u32 iopt_inc_num_preserved_domains (struct io_pagetable *iopt) +{ +guard (rwsem_write)(&iopt->domains_rwsem); +return ++iopt->num_preserved_domains; +} + +static void iopt_dec_num_preserved_domains (struct io_pagetable *iopt) +{ +guard (rwsem_write)(&iopt->domains_rwsem); +if (!WARN_ON (!iopt->num_preserved_domains)) + iopt->num_preserved_domains--; +}int iommufd_hwpt_liveupdate_mark_preserve (struct iommufd_ucmd *ucmd) {@@ -40,6 +59,12 @@ int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) goto out_unlock; } +/* The token of a preserved HWPT can't change. */ +if (hwpt->liveupdate_preserved) { + rc = -EBUSY ; + goto out_unlock; + } +xa_for_each_marked (&ictx->objects, index, obj,IOMMUFD_OBJ_LIVEUPDATE_MARK ) {struct iommufd_hwpt_paging *hwpt_marked;@@ -73,3 +98,322 @@ int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd)iommufd_put_object (ictx, &hwpt->common.obj);return rc; } + +static int iopt_area_validate_file_pages (struct iopt_area *area) +{ +unsigned long last_index =iopt_area_last_index (area); +unsigned long index =iopt_area_index (area); +unsigned int page_offset = area->page_offset; +struct iopt_pages *pages = area->pages; +unsigned long iova =iopt_area_iova (area); + +guard (mutex)(&pages->mutex); +if (WARN_ON (!area->storage_domain)) +return -EINVAL ; + +/* +* Walk the domain one page at a time instead of jumping ahead by the +* folio size. When filling a domain, iommufd reuses pages that are +* already pinned (by an access or another domain), so if the memfd +* was punched in between, an old page from a different folio can sit +* in the middle of a folio range. batch_from_domain() walks the domain +* the same way. +*/ +while (index <= last_index) { +phys_addr_t phys =iommu_iova_to_phys (area->storage_domain, iova); +struct folio *folio; + +if (!phys) +return -EINVAL ; + + folio =page_folio (pfn_to_page (PHYS_PFN (phys - page_offset))); +if (folio->mapping != pages->file->f_mapping) +return -EINVAL ; + + iova +=PAGE_SIZE - page_offset; + page_offset =0 ; + index++; + } + +return 0 ; +} + +static int iopt_validate_preserved_memory (struct io_pagetable *iopt, +struct liveupdate_session *s) +{ +struct iopt_area *area; +int ret =0 ; + +down_read (&iopt->iova_rwsem); +for (area =iopt_area_iter_first (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 ; + } + +/* Make sure that the file was preserved. */ + ret =liveupdate_get_token_outgoing (s, pages->file,NULL ); +if (ret) +break ; + +/* +* Make sure the mapped pages are still associated with the +* preserved memfd. +*/ + ret =iopt_area_validate_file_pages (area); +if (ret) +break ; + } +up_read (&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; +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. +*/ +if (iopt_inc_num_preserved_domains (&hwpt->ioas->iopt) ==1 ) { + rc =iopt_validate_preserved_memory (&hwpt->ioas->iopt, session); +if (rc) + goto err; + } + + hwpt_ser->size =sizeof (*hwpt_ser); + hwpt_ser->token = hwpt->liveupdate_token; + + rc =iommu_preserve_domain (hwpt->common.domain, &domain_ser); +if (rc <0 ) + goto err; + + hwpt_ser->domain_ser_phys =virt_to_phys (domain_ser); +return 0 ; + +err: +iopt_dec_num_preserved_domains (&hwpt->ioas->iopt); +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); +iopt_dec_num_preserved_domains (&hwpt->ioas->iopt); + + 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 num_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 */ + num_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; + } + num_hwpts++; + } +xa_unlock (&ictx->objects); + + mem =kho_alloc_preserve (struct_size (iommufd_ser, + hwpt_array, num_hwpts)); +if (IS_ERR (mem)) { + rc =PTR_ERR (mem); + goto out_unlock; + } + + iommufd_ser = mem; + iommufd_ser->size =sizeof (*iommufd_ser); + +/* Preserve HWPTs */ + i =0 ;
drivers/iommu/iommufd/liveupdate.c int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) · line 300
Quoted 10 lines
drivers/iommu/iommufd/liveupdate.c +10
-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; + }
iommufd_lock_obj() increments both users and wait_cnt, and this reference is held until unpreserve. Is the intent to keep the hwpt_paging object from being destroyed for as long as it stays preserved? If so, this conflicts with the comment in iommufd_object_remove():
/*
* The purpose of the wait_cnt is to ensure deterministic
destruction
* of objects used by external drivers and destroyed by this
function.
* Incrementing this wait_cnt should either be short lived, such as
* during ioctl execution, or be revoked and blocked during
* pre_destroy(), such as vdev holding the idev's refcount.
*/
if (flags & REMOVE_WAIT) {
ret = iommufd_object_dec_wait(ictx, to_destroy);
if (ret) {
/*
* We have a bug. Put back the callers
reference and
* defer cleaning this object until close.
*/
refcount_dec(&to_destroy->users);
return ret;
}
zerod_wait_cnt = true;
}Would holding a users reference alone be sufficient? Otherwise, need an update on the wait_cnt comment to describe this new usage.
Quoted 109 lines
drivers/iommu/iommufd/liveupdate.c +109
-0+ +/* +* 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->num_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, + .owner =THIS_MODULE , +}; + +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 )
drivers/iommu/iommufd/liveupdate.c int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) · line 419
Quoted 10 lines
drivers/iommu/iommufd/liveupdate.c +10
-0+return 0 ; + +return ret; +} + +void iommufd_liveupdate_unregister (void ) +{ +iommu_liveupdate_unregister_flb (&iommufd_ser_handler); +liveupdate_unregister_file_handler (&iommufd_ser_handler); +}
[...]
Thanks,
baolu
Re: [PATCH v8 16/18] iommufd: Persist iommu hardware pagetables for live update
Quoted 11 lines · Baolu Lu
Quoted 7 lines · Samiullah Khawaja
From: YiFei Zhu <[email protected]>
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_immutableflag to IOAS. When an HWPT is preserved, its IOASThe "
liveupdate_immutableflag" is out of date. The code usesiopt->num_preserved_domainsinstead. Please update it.
I will update this in next revision.
Quoted 37 lines
Quoted 21 lines
is marked immutable and any map/unmap attempts will fail with
-EBUSY. This is synchronized using thedomains_rwsemto 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 <[email protected]>Signed-off-by Samiullah Khawaja <[email protected]>--- MAINTAINERS | 1 + drivers/iommu/iommufd/io_pagetable.c | 24 ++ drivers/iommu/iommufd/iommufd_private.h | 16 ++ drivers/iommu/iommufd/liveupdate.c | 344 ++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 10 +- include/linux/kho/abi/iommufd.h | 56 ++++ 6 files changed, 450 insertions(+), 1 deletion(-) create mode 100644 include/linux/kho/abi/iommufd.h[...]
Quoted 13 lines
drivers/iommu/iommufd/io_pagetable.c +6
-0@@ -30,6 +30,19 @@ struct iopt_pages_list {unsigned long length; }; +static inline bool iopt_liveupdate_immutable (const struct io_pagetable *iopt) +{ +bool immutable = false; + +lockdep_assert_held (&iopt->domains_rwsem); +
Quoted 8 lines
drivers/iommu/iommufd/io_pagetable.c struct iopt_pages_list { · line 44
Quoted 6 lines
drivers/iommu/iommufd/io_pagetable.c +6
-0+#ifdef CONFIG_IOMMU_LIVEUPDATE +/* iopt becomes immutable once liveupdate preservation starts */ + immutable = iopt->num_preserved_domains; +#endif +return immutable; +}Nit: drop the "inline" in a .c file.
Will update this.
Quoted 32 lines
Quoted 29 lines
drivers/iommu/iommufd/io_pagetable.c +12
-0+ struct iopt_area *iopt_area_contig_init(struct iopt_area_contig_iter *iter, struct io_pagetable *iopt, unsigned long iova,@@ -384,6 +397,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;@@ -758,6 +776,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);[...]
Quoted 16 lines
drivers/iommu/iommufd/liveupdate.c line 16
Quoted 13 lines
drivers/iommu/iommufd/liveupdate.c +5
-0@@ -9,9 +9,28 @@#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> Nit: keep the includes sorted. Also, nothing in this file seems to use
linux/pci.h.
Agreed. Will update.
Quoted 263 lines
Quoted 223 lines
drivers/iommu/iommufd/liveupdate.c +210
-0#include "iommufd_private.h" +#include "io_pagetable.h" + +static u32 iopt_inc_num_preserved_domains (struct io_pagetable *iopt) +{ +guard (rwsem_write)(&iopt->domains_rwsem); +return ++iopt->num_preserved_domains; +} + +static void iopt_dec_num_preserved_domains (struct io_pagetable *iopt) +{ +guard (rwsem_write)(&iopt->domains_rwsem); +if (!WARN_ON (!iopt->num_preserved_domains)) + iopt->num_preserved_domains--; +}int iommufd_hwpt_liveupdate_mark_preserve (struct iommufd_ucmd *ucmd) {@@ -40,6 +59,12 @@ int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) goto out_unlock; } +/* The token of a preserved HWPT can't change. */ +if (hwpt->liveupdate_preserved) { + rc = -EBUSY ; + goto out_unlock; + } +xa_for_each_marked (&ictx->objects, index, obj,IOMMUFD_OBJ_LIVEUPDATE_MARK ) {struct iommufd_hwpt_paging *hwpt_marked;@@ -73,3 +98,322 @@ int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd)iommufd_put_object (ictx, &hwpt->common.obj);return rc; } + +static int iopt_area_validate_file_pages (struct iopt_area *area) +{ +unsigned long last_index =iopt_area_last_index (area); +unsigned long index =iopt_area_index (area); +unsigned int page_offset = area->page_offset; +struct iopt_pages *pages = area->pages; +unsigned long iova =iopt_area_iova (area); + +guard (mutex)(&pages->mutex); +if (WARN_ON (!area->storage_domain)) +return -EINVAL ; + +/* +* Walk the domain one page at a time instead of jumping ahead by the +* folio size. When filling a domain, iommufd reuses pages that are +* already pinned (by an access or another domain), so if the memfd +* was punched in between, an old page from a different folio can sit +* in the middle of a folio range. batch_from_domain() walks the domain +* the same way. +*/ +while (index <= last_index) { +phys_addr_t phys =iommu_iova_to_phys (area->storage_domain, iova); +struct folio *folio; + +if (!phys) +return -EINVAL ; + + folio =page_folio (pfn_to_page (PHYS_PFN (phys - page_offset))); +if (folio->mapping != pages->file->f_mapping) +return -EINVAL ; + + iova +=PAGE_SIZE - page_offset; + page_offset =0 ; + index++; + } + +return 0 ; +} + +static int iopt_validate_preserved_memory (struct io_pagetable *iopt, +struct liveupdate_session *s) +{ +struct iopt_area *area; +int ret =0 ; + +down_read (&iopt->iova_rwsem); +for (area =iopt_area_iter_first (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 ; + } + +/* Make sure that the file was preserved. */ + ret =liveupdate_get_token_outgoing (s, pages->file,NULL ); +if (ret) +break ; + +/* +* Make sure the mapped pages are still associated with the +* preserved memfd. +*/ + ret =iopt_area_validate_file_pages (area); +if (ret) +break ; + } +up_read (&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; +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. +*/ +if (iopt_inc_num_preserved_domains (&hwpt->ioas->iopt) ==1 ) { + rc =iopt_validate_preserved_memory (&hwpt->ioas->iopt, session); +if (rc) + goto err; + } + + hwpt_ser->size =sizeof (*hwpt_ser); + hwpt_ser->token = hwpt->liveupdate_token; + + rc =iommu_preserve_domain (hwpt->common.domain, &domain_ser); +if (rc <0 ) + goto err; + + hwpt_ser->domain_ser_phys =virt_to_phys (domain_ser); +return 0 ; + +err: +iopt_dec_num_preserved_domains (&hwpt->ioas->iopt); +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); +iopt_dec_num_preserved_domains (&hwpt->ioas->iopt); + + 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 num_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 */ + num_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; + } + num_hwpts++; + } +xa_unlock (&ictx->objects); + + mem =kho_alloc_preserve (struct_size (iommufd_ser, + hwpt_array, num_hwpts)); +if (IS_ERR (mem)) { + rc =PTR_ERR (mem); + goto out_unlock; + } + + iommufd_ser = mem; + iommufd_ser->size =sizeof (*iommufd_ser); + +/* Preserve HWPTs */ + i =0 ;drivers/iommu/iommufd/liveupdate.c int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) · line 300
Quoted 10 lines
drivers/iommu/iommufd/liveupdate.c +10
-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; + }
iommufd_lock_obj()increments both users andwait_cnt, and this reference is held until unpreserve. Is the intent to keep thehwpt_pagingobject from being destroyed for as long as it stays preserved? If so, this conflicts with the comment iniommufd_object_remove():/*
* The purpose of thewait_cntis to ensure deterministic
destruction
* of objects used by external drivers and destroyed by this
function.* Incrementing this wait_cnt should either be short lived, such as * during ioctl execution, or be revoked and blocked during * pre_destroy(), such as vdev holding the idev's refcount. */ if (flags & REMOVE_WAIT) { ret = iommufd_object_dec_wait(ictx, to_destroy); if (ret) { /* * We have a bug. Put back the callers reference and * defer cleaning this object until close. */ refcount_dec(&to_destroy->users); return ret; } zerod_wait_cnt = true; }
Quoted 2 lines
Would holding a users reference alone be sufficient? Otherwise, need an update on the
wait_cntcomment to describe this new usage.
Yes the hwpt_paging object needs to be kept alive until it stays preserved. I will update this to inc/dec users directly instead of using lock_obj() and put_object().
Quoted 56 lines
Quoted 55 lines
drivers/iommu/iommufd/liveupdate.c +54
-0+ +/* +* 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->num_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); +} +
[snip]
Quoted 2 lines
Thanks,
baolu
Thanks,
Sami
PATCH v8 17/18 iommufd: Add APIs to preserve/unpreserve a vfio cdev
In reply to Samiullah KhawajaAdd 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.
--- drivers/iommu/iommufd/device.c | 154 ++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 3 + drivers/iommu/iommufd/liveupdate.c | 26 ++++ include/linux/iommufd.h | 27 +++++ 4 files changed, 210 insertions(+)
drivers/iommu/iommufd/device.c +154 -0
@@ -2,6 +2,7 @@/* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES */ #include <linux/iommu.h> +#include <linux/iommu-liveupdate.h> #include <linux/iommufd.h> #include <linux/pci-ats.h> #include <linux/slab.h>
@@ -686,6 +687,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,int rc;mutex_lock (&igroup->lock); +if (iommufd_device_is_preserved (idev)) { + rc = -EBUSY ; + goto err_unlock; + } attach =xa_cmpxchg (&igroup->pasid_attach, pasid,NULL ,XA_ZERO_ENTRY ,GFP_KERNEL );
@@ -869,6 +874,10 @@ iommufd_device_do_replace(struct iommufd_device *idev, ioasid_t pasid,int rc;mutex_lock (&igroup->lock); +if (iommufd_device_is_preserved (idev)) { + rc = -EBUSY ; + goto err_unlock; + } attach =xa_load (&igroup->pasid_attach, pasid);if (!attach) {
@@ -1135,6 +1144,9 @@ void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid) {struct iommufd_hw_pagetable *hwpt; +if (pasid ==IOMMU_NO_PASID ) +iommufd_device_unpreserve (idev); + hwpt =iommufd_hw_pagetable_detach (idev, pasid);if (!hwpt)return ;
@@ -1749,3 +1761,145 @@ int iommufd_get_hw_info(struct iommufd_ucmd *ucmd)iommufd_put_object (ucmd->ictx, &idev->obj);return rc; } + +#ifdef CONFIG_IOMMU_LIVEUPDATE +static bool _iommufd_device_has_pasid_attachments (struct iommufd_device *idev) +{ +struct iommufd_group *igroup = idev->igroup; +unsigned long start =IOMMU_NO_PASID ; + +if (xa_find_after (&igroup->pasid_attach, &start,UINT_MAX ,XA_PRESENT )) +return true; + +return false; +} + +/** +* iommufd_device_preserve() - Preserve an iommufd device across live update +* @s: Live update session +* @idev: Target iommufd device +* @iommufd_tokenp: Pointer to store outgoing iommufd token +* +* Return: 0 on success, or negative error code. +*/ +int iommufd_device_preserve (struct liveupdate_session *s, +struct iommufd_device *idev, +u64 *iommufd_tokenp) +{ +struct iommufd_hwpt_paging *hwpt_paging; +struct iommufd_hw_pagetable *hwpt; +struct iommufd_attach *attach; +struct iommufd_group *igroup; +int ret; + +if (!idev) +return -EINVAL ; + +if (iommufd_device_is_noiommu (idev)) +return -EOPNOTSUPP ; + + igroup = idev->igroup; +mutex_lock (&igroup->lock); +if (idev->liveupdate_preserved) { + ret = -EBUSY ; + goto out; + } + +if (_iommufd_device_has_pasid_attachments (idev)) { + ret = -EOPNOTSUPP ; + goto out; + } + + attach =xa_load (&igroup->pasid_attach,IOMMU_NO_PASID ); +if (!attach) { + ret = -ENOENT ; + goto out; + } + +if (!xa_load (&attach->device_array, idev->obj.id)) { + ret = -ENOENT ; + goto out; + } + + hwpt = attach->hwpt; +if (!hwpt_is_paging (hwpt)) { + ret = -EOPNOTSUPP ; + goto out; + } + + hwpt_paging =to_hwpt_paging (hwpt); + ret =liveupdate_get_token_outgoing (s, idev->ictx->file, iommufd_tokenp); +if (ret) + goto out; + +if (!hwpt_paging->liveupdate_preserved) { + ret = -EINVAL ; + goto out; + } + + ret =iommu_preserve_device (hwpt_paging->common.domain, + idev->dev, + *iommufd_tokenp); + +if (!ret) + idev->liveupdate_preserved = true; +out: +mutex_unlock (&igroup->lock); +return ret; +} +EXPORT_SYMBOL_NS_GPL (iommufd_device_preserve,"IOMMUFD" ); + +/** +* iommufd_device_unpreserve() - Unpreserve an iommufd device +* @idev: Target iommufd device +*/ +void iommufd_device_unpreserve (struct iommufd_device *idev) +{ +struct iommufd_hwpt_paging *hwpt_paging; +struct iommufd_hw_pagetable *hwpt; +struct iommufd_attach *attach; +struct iommufd_group *igroup; + +if (!idev) +return ; + + igroup = idev->igroup; +mutex_lock (&igroup->lock); +if (!idev->liveupdate_preserved) + goto out; + + attach =xa_load (&igroup->pasid_attach,IOMMU_NO_PASID ); +if (!attach) { +WARN (1 ,"IOMMU_NO_PASID attachment not found" ); + goto out; + } + + hwpt = attach->hwpt; +if (WARN (!hwpt_is_paging (hwpt),"Attached HWPT is not a paging HWPT" )) + goto out; + + hwpt_paging =to_hwpt_paging (hwpt); +if (!hwpt_paging->liveupdate_preserved) { +WARN (1 ,"Attached domain is not preserved" ); + goto out; + } + +iommu_unpreserve_device (idev->dev); + idev->liveupdate_preserved = false; +out: +mutex_unlock (&igroup->lock); +} +EXPORT_SYMBOL_NS_GPL (iommufd_device_unpreserve,"IOMMUFD" ); + +/** +* iommufd_device_is_preserved() - Check if an iommufd device is preserved +* @idev: Target iommufd device +* +* Return: true if preserved, false otherwise. +*/ +bool iommufd_device_is_preserved (struct iommufd_device *idev) +{ +return idev && idev->liveupdate_preserved; +} +EXPORT_SYMBOL_NS_GPL (iommufd_device_is_preserved,"IOMMUFD" ); +#endif
drivers/iommu/iommufd/iommufd_private.h +3 -0
@@ -524,6 +524,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 *
drivers/iommu/iommufd/liveupdate.c +26 -0
@@ -210,6 +210,30 @@ static int iommufd_preserve_hwpt(struct iommufd_hwpt_paging *hwpt,return rc; } +/* Preserved devices must be unpreserved when the iommufd is unpreserved. */ +static void iommufd_unpreserve_devices (struct iommufd_ctx *ictx) +{ +struct iommufd_device *idev; +struct iommufd_object *obj; +unsigned long index; + +xa_lock (&ictx->objects); +xa_for_each (&ictx->objects, index, obj) { +if (obj->type !=IOMMUFD_OBJ_DEVICE ) +continue ; + + idev = container_of(obj, struct iommufd_device, obj); +if (!iommufd_device_is_preserved (idev) || !iommufd_lock_obj (obj)) +continue ; + +xa_unlock (&ictx->objects); +iommufd_device_unpreserve (idev); +iommufd_put_object (ictx, obj); +xa_lock (&ictx->objects); + } +xa_unlock (&ictx->objects); +} +static void _iommufd_unpreserve (struct iommufd_ctx *ictx,struct iommufd_ser *ser) {
@@ -217,6 +241,8 @@ static void _iommufd_unpreserve(struct iommufd_ctx *ictx,struct iommufd_object *obj;unsigned long index; +iommufd_unpreserve_devices (ictx); +xa_lock (&ictx->objects);xa_for_each_marked (&ictx->objects, index, obj,IOMMUFD_OBJ_LIVEUPDATE_MARK ) {if (obj->type !=IOMMUFD_OBJ_HWPT_PAGING )
include/linux/iommufd.h +27 -0
@@ -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>
@@ -215,6 +216,14 @@ int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,int iommufd_vfio_compat_ioas_get_id (struct iommufd_ctx *ictx,u32 *out_ioas_id);int iommufd_vfio_compat_ioas_create (struct iommufd_ctx *ictx);int iommufd_vfio_compat_set_no_iommu (struct iommufd_ctx *ictx); + +#ifdef CONFIG_IOMMU_LIVEUPDATE +int iommufd_device_preserve (struct liveupdate_session *s, +struct iommufd_device *idev, +u64 *iommufd_tokenp); +void iommufd_device_unpreserve (struct iommufd_device *idev); +bool iommufd_device_is_preserved (struct iommufd_device *idev); +#endif #else /* !CONFIG_IOMMUFD */ static inline struct iommufd_ctx *iommufd_ctx_from_file (struct file *file) {
@@ -399,4 +408,22 @@ static inline void iommufd_viommu_destroy_mmap(struct iommufd_viommu *viommu, {_iommufd_destroy_mmap (viommu->ictx, &viommu->obj, offset); } + +#if !IS_ENABLED (CONFIG_IOMMU_LIVEUPDATE ) || !IS_ENABLED (CONFIG_IOMMUFD ) +static inline int iommufd_device_preserve (struct liveupdate_session *s, +struct iommufd_device *idev, +u64 *iommufd_tokenp) +{ +return -EOPNOTSUPP ; +} + +static inline void iommufd_device_unpreserve (struct iommufd_device *idev) +{ +} + +static inline bool iommufd_device_is_preserved (struct iommufd_device *idev) +{ +return false; +} +#endif #endif
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Baolu Lu
maintainerRe: [PATCH v8 17/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev
Quoted 51 lines · Samiullah Khawaja
Add APIs that can be used to preserve and unpreserve a vfio cdev. Use the APIs exported by the IOMMU core to preserve/unpreserve device.
The LUO token of the preserved iommufd is fetched and returned back to the caller as that can be used during restore to get the restored iommufd.
Signed-off-by Samiullah Khawaja <[email protected]>Reviewed-by Pranjal Shrivastava <[email protected]>--- drivers/iommu/iommufd/device.c | 154 ++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 3 + drivers/iommu/iommufd/liveupdate.c | 26 ++++ include/linux/iommufd.h | 27 +++++ 4 files changed, 210 insertions(+)drivers/iommu/iommufd/device.c +9
-0@@ -2,6 +2,7 @@/* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES */ #include <linux/iommu.h> +#include <linux/iommu-liveupdate.h> #include <linux/iommufd.h> #include <linux/pci-ats.h> #include <linux/slab.h> @@ -686,6 +687,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,int rc;mutex_lock (&igroup->lock); +if (iommufd_device_is_preserved (idev)) { + rc = -EBUSY ; + goto err_unlock; + } attach =xa_cmpxchg (&igroup->pasid_attach, pasid,NULL ,XA_ZERO_ENTRY ,GFP_KERNEL );@@ -869,6 +874,10 @@ iommufd_device_do_replace(struct iommufd_device *idev, ioasid_t pasid,int rc;mutex_lock (&igroup->lock); +if (iommufd_device_is_preserved (idev)) { + rc = -EBUSY ; + goto err_unlock; + } attach =xa_load (&igroup->pasid_attach, pasid);if (!attach) {
drivers/iommu/iommufd/device.c void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid) · line 1148
Quoted 5 lines
drivers/iommu/iommufd/device.c +2
-0@@ -1135,6 +1144,9 @@ void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid) {struct iommufd_hw_pagetable *hwpt; +if (pasid ==IOMMU_NO_PASID ) +iommufd_device_unpreserve (idev);
Could you add a comment explaining why detaching IOMMU_NO_PASID silently unpreserves the device?
drivers/iommu/iommufd/device.c void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid) · line 1150
Quoted 2 lines
drivers/iommu/iommufd/device.c +1
-0+ hwpt =iommufd_hw_pagetable_detach (idev, pasid);
This also races with iommufd_device_preserve(). igroup->lock is dropped between iommufd_device_unpreserve() and iommufd_hw_pagetable_detach(), so a concurrent iommufd_device_preserve() can slip into that window, see the device still attached, and succeed. The device then ends up detached while still marked as preserved.
Quoted 7 lines
drivers/iommu/iommufd/device.c +1
-0if (!hwpt)return ;@@ -1749,3 +1761,145 @@ int iommufd_get_hw_info(struct iommufd_ucmd *ucmd)iommufd_put_object (ucmd->ictx, &idev->obj);return rc; } +
drivers/iommu/iommufd/device.c int iommufd_get_hw_info(struct iommufd_ucmd *ucmd) · line 1774
Quoted 10 lines
drivers/iommu/iommufd/device.c +10
-0+#ifdef CONFIG_IOMMU_LIVEUPDATE +static bool _iommufd_device_has_pasid_attachments (struct iommufd_device *idev) +{ +struct iommufd_group *igroup = idev->igroup; +unsigned long start =IOMMU_NO_PASID ; + +if (xa_find_after (&igroup->pasid_attach, &start,UINT_MAX ,XA_PRESENT )) +return true; + +return false;
Nit: this can simply be
return xa_find_after(...) != NULL;
?
Also, how about dropping the leading underscore. There is no non- underscore version of this helper.
Quoted 131 lines
drivers/iommu/iommufd/device.c +131
-0+} + +/** +* iommufd_device_preserve() - Preserve an iommufd device across live update +* @s: Live update session +* @idev: Target iommufd device +* @iommufd_tokenp: Pointer to store outgoing iommufd token +* +* Return: 0 on success, or negative error code. +*/ +int iommufd_device_preserve (struct liveupdate_session *s, +struct iommufd_device *idev, +u64 *iommufd_tokenp) +{ +struct iommufd_hwpt_paging *hwpt_paging; +struct iommufd_hw_pagetable *hwpt; +struct iommufd_attach *attach; +struct iommufd_group *igroup; +int ret; + +if (!idev) +return -EINVAL ; + +if (iommufd_device_is_noiommu (idev)) +return -EOPNOTSUPP ; + + igroup = idev->igroup; +mutex_lock (&igroup->lock); +if (idev->liveupdate_preserved) { + ret = -EBUSY ; + goto out; + } + +if (_iommufd_device_has_pasid_attachments (idev)) { + ret = -EOPNOTSUPP ; + goto out; + } + + attach =xa_load (&igroup->pasid_attach,IOMMU_NO_PASID ); +if (!attach) { + ret = -ENOENT ; + goto out; + } + +if (!xa_load (&attach->device_array, idev->obj.id)) { + ret = -ENOENT ; + goto out; + } + + hwpt = attach->hwpt; +if (!hwpt_is_paging (hwpt)) { + ret = -EOPNOTSUPP ; + goto out; + } + + hwpt_paging =to_hwpt_paging (hwpt); + ret =liveupdate_get_token_outgoing (s, idev->ictx->file, iommufd_tokenp); +if (ret) + goto out; + +if (!hwpt_paging->liveupdate_preserved) { + ret = -EINVAL ; + goto out; + } + + ret =iommu_preserve_device (hwpt_paging->common.domain, + idev->dev, + *iommufd_tokenp); + +if (!ret) + idev->liveupdate_preserved = true; +out: +mutex_unlock (&igroup->lock); +return ret; +} +EXPORT_SYMBOL_NS_GPL (iommufd_device_preserve,"IOMMUFD" ); + +/** +* iommufd_device_unpreserve() - Unpreserve an iommufd device +* @idev: Target iommufd device +*/ +void iommufd_device_unpreserve (struct iommufd_device *idev) +{ +struct iommufd_hwpt_paging *hwpt_paging; +struct iommufd_hw_pagetable *hwpt; +struct iommufd_attach *attach; +struct iommufd_group *igroup; + +if (!idev) +return ; + + igroup = idev->igroup; +mutex_lock (&igroup->lock); +if (!idev->liveupdate_preserved) + goto out; + + attach =xa_load (&igroup->pasid_attach,IOMMU_NO_PASID ); +if (!attach) { +WARN (1 ,"IOMMU_NO_PASID attachment not found" ); + goto out; + } + + hwpt = attach->hwpt; +if (WARN (!hwpt_is_paging (hwpt),"Attached HWPT is not a paging HWPT" )) + goto out; + + hwpt_paging =to_hwpt_paging (hwpt); +if (!hwpt_paging->liveupdate_preserved) { +WARN (1 ,"Attached domain is not preserved" ); + goto out; + } + +iommu_unpreserve_device (idev->dev); + idev->liveupdate_preserved = false; +out: +mutex_unlock (&igroup->lock); +} +EXPORT_SYMBOL_NS_GPL (iommufd_device_unpreserve,"IOMMUFD" ); + +/** +* iommufd_device_is_preserved() - Check if an iommufd device is preserved +* @idev: Target iommufd device +* +* Return: true if preserved, false otherwise. +*/ +bool iommufd_device_is_preserved (struct iommufd_device *idev) +{ +return idev && idev->liveupdate_preserved; +} +EXPORT_SYMBOL_NS_GPL (iommufd_device_is_preserved,"IOMMUFD" ); +#endif
[...]
include/linux/iommufd.h line 12
Quoted 9 lines
include/linux/iommufd.h +1
-0@@ -9,6 +9,7 @@#include <linux/err.h> #include <linux/errno.h> #include <linux/iommu.h> +#include <linux/liveupdate.h>
Nit: Would a forward declaration of "struct liveupdate_session;" be enough here? There seems to be no need to pull liveupdate.h into every user of linux/iommufd.h.
Quoted 31 lines
include/linux/iommufd.h +17
-0#include <linux/refcount.h> #include <linux/types.h> #include <linux/xarray.h> @@ -215,6 +216,14 @@ int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,int iommufd_vfio_compat_ioas_get_id (struct iommufd_ctx *ictx,u32 *out_ioas_id);int iommufd_vfio_compat_ioas_create (struct iommufd_ctx *ictx);int iommufd_vfio_compat_set_no_iommu (struct iommufd_ctx *ictx); + +#ifdef CONFIG_IOMMU_LIVEUPDATE +int iommufd_device_preserve (struct liveupdate_session *s, +struct iommufd_device *idev, +u64 *iommufd_tokenp); +void iommufd_device_unpreserve (struct iommufd_device *idev); +bool iommufd_device_is_preserved (struct iommufd_device *idev); +#endif #else /* !CONFIG_IOMMUFD */ static inline struct iommufd_ctx *iommufd_ctx_from_file (struct file *file) {@@ -399,4 +408,22 @@ static inline void iommufd_viommu_destroy_mmap(struct iommufd_viommu *viommu, {_iommufd_destroy_mmap (viommu->ictx, &viommu->obj, offset); } + +#if !IS_ENABLED (CONFIG_IOMMU_LIVEUPDATE ) || !IS_ENABLED (CONFIG_IOMMUFD ) +static inline int iommufd_device_preserve (struct liveupdate_session *s, +struct iommufd_device *idev, +u64 *iommufd_tokenp) +{ +return -EOPNOTSUPP ; +} +
include/linux/iommufd.h static inline void iommufd_viommu_destroy_mmap(struct iommufd_viommu *viommu, · line 429
Quoted 10 lines
include/linux/iommufd.h +9
-0+static inline void iommufd_device_unpreserve (struct iommufd_device *idev) +{ +} + +static inline bool iommufd_device_is_preserved (struct iommufd_device *idev) +{ +return false; +} +#endif #endif
Thanks,
baolu
PATCH v8 18/18 iommufd/selftest: Add test to verify iommufd preservation
In reply to Samiullah KhawajaTest iommufd preservation by setting up an iommufd with a HWPT and preserving it across live update. The test takes the VFIO cdev path of a device bound to the vfio-pci driver, binds it to the iommufd and attaches it to the HWPT that is marked for preservation. It also checks that the iommufd can't be preserved without its memfd or with hole-punched pages.
Add IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV to preserve the device, since the vfio cdev preservation is not available yet.
On restore, the test verifies that the restored device can't be bound to a new iommufd, that the iommufd can't be retrieved, as the iommufd restore is not supported yet, and that the LUO session finish fails.
--- drivers/iommu/iommufd/device.c | 47 ++- drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/iommufd_test.h | 1 + drivers/iommu/iommufd/selftest.c | 29 ++ tools/testing/selftests/iommu/Makefile | 21 ++ .../iommu/iommufd_liveupdate_kexec_test.c | 276 ++++++++++++++++++ 6 files changed, 359 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c
drivers/iommu/iommufd/device.c +31 -16
@@ -1774,17 +1774,8 @@ static bool _iommufd_device_has_pasid_attachments(struct iommufd_device *idev)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) +/* Preserve @idev using @token as its DMA owner token. */ +int __iommufd_device_preserve (struct iommufd_device *idev,u64 token) {struct iommufd_hwpt_paging *hwpt_paging;struct iommufd_hw_pagetable *hwpt;
@@ -1828,10 +1819,6 @@ int iommufd_device_preserve(struct liveupdate_session *s, } hwpt_paging =to_hwpt_paging (hwpt); - ret =liveupdate_get_token_outgoing (s, idev->ictx->file, iommufd_tokenp); -if (ret) - goto out; -if (!hwpt_paging->liveupdate_preserved) { ret = -EINVAL ; goto out;
@@ -1839,7 +1826,7 @@ int iommufd_device_preserve(struct liveupdate_session *s, ret =iommu_preserve_device (hwpt_paging->common.domain, idev->dev, - *iommufd_tokenp); + token);if (!ret) idev->liveupdate_preserved = true;
@@ -1847,6 +1834,34 @@ int iommufd_device_preserve(struct liveupdate_session *s,mutex_unlock (&igroup->lock);return ret; } + +/** +* 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) +{ +int ret; + +if (!idev) +return -EINVAL ; + +/* +* The iommufd must be preserved in the same session, which also makes +* sure its HWPT preservation is done before the device is preserved. +*/ + ret =liveupdate_get_token_outgoing (s, idev->ictx->file, iommufd_tokenp); +if (ret) +return ret; + +return __iommufd_device_preserve (idev, *iommufd_tokenp); +}EXPORT_SYMBOL_NS_GPL (iommufd_device_preserve,"IOMMUFD" );/**
drivers/iommu/iommufd/iommufd_private.h +1 -0
@@ -749,6 +749,7 @@ int iommufd_liveupdate_register(void);void iommufd_liveupdate_unregister (void );int iommufd_hwpt_liveupdate_mark_preserve (struct iommufd_ucmd *ucmd); +int __iommufd_device_preserve (struct iommufd_device *idev,u64 token);#else static inline int iommufd_liveupdate_register (void ) {
drivers/iommu/iommufd/iommufd_test.h +1 -0
@@ -31,6 +31,7 @@ enum {IOMMU_TEST_OP_PASID_CHECK_HWPT ,IOMMU_TEST_OP_DMABUF_GET ,IOMMU_TEST_OP_DMABUF_REVOKE , + IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV, }; enum {
drivers/iommu/iommufd/selftest.c +29 -0
@@ -2115,6 +2115,33 @@ static int iommufd_test_dmabuf_revoke(struct iommufd_ucmd *ucmd, int fd,return rc; } +/* +* Preserve the IOMMU state of a device bound to this iommufd, as a vfio cdev +* preserved in the same live update session would. The iommufd must already +* be preserved, so the HWPT of the device is preserved. +*/ +static int iommufd_test_liveupdate_preserve_dev (struct iommufd_ucmd *ucmd, +u32 dev_id) +{ +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommufd_device *idev; +int rc; + + idev =iommufd_get_device (ucmd, dev_id); +if (IS_ERR (idev)) +return PTR_ERR (idev); + +mutex_lock (&ucmd->ictx->liveupdate_mutex); + rc =__iommufd_device_preserve (idev,0 ); +mutex_unlock (&ucmd->ictx->liveupdate_mutex); + +iommufd_put_object (ucmd->ictx, &idev->obj); +return rc; +#else +return -EOPNOTSUPP ; +#endif +} +int iommufd_test (struct iommufd_ucmd *ucmd) {struct iommu_test_cmd *cmd = ucmd->cmd;
@@ -2200,6 +2227,8 @@ int iommufd_test(struct iommufd_ucmd *ucmd)return iommufd_test_dmabuf_revoke (ucmd, cmd->dmabuf_revoke.dmabuf_fd, cmd->dmabuf_revoke.revoked); +case IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV : +return iommufd_test_liveupdate_preserve_dev (ucmd, cmd->id);default :return -EOPNOTSUPP ; }
tools/testing/selftests/iommu/Makefile +21 -0
@@ -7,4 +7,25 @@ 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_O := $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED)) +$(TEST_O): $(OUTPUT)/%.o: %.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ + +TEST_DEP_FILES := $(patsubst %.o, %.d, $(LIBLIVEUPDATE_O) $(TEST_O)) +-include $(TEST_DEP_FILES) + +$(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: $(OUTPUT)/%.o $(LIBLIVEUPDATE_O) + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBLIVEUPDATE_O) $(LDLIBS) -o $@ + +EXTRA_CLEAN += $(TEST_O) +EXTRA_CLEAN += $(TEST_DEP_FILES) +EXTRA_CLEAN += $(patsubst %, %.d, $(TEST_GEN_PROGS))
tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c new file +276 -0
276 lines not on this page · raw message
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v8 18/18] iommufd/selftest: Add test to verify iommufd preservation
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] iommufd: race between iommufd_test_liveupdate_preserve_dev() and detach --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] iommufd: race between iommufd_test_liveupdate_preserve_dev() and detach
--
commit f9841f2ccd5ea9e7ec8822d894220423dd77c296
Author: Samiullah Khawaja <[email protected]>
iommufd/selftest: Add test to verify iommufd preservation
This commit adds a kexec selftest to verify iommufd preservation across live update, along with an IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV test operation to preserve an iommufd device.
Quoted 17 lines
drivers/iommu/iommufd/selftest.c +9
-0@@ -2115,6 +2115,33 @@ static int iommufd_test_dmabuf_revoke(struct iommufd_ucmd *ucmd, int fd,return rc; } +/* +* Preserve the IOMMU state of a device bound to this iommufd, as a vfio cdev +* preserved in the same live update session would. The iommufd must already +* be preserved, so the HWPT of the device is preserved. +*/ +static int iommufd_test_liveupdate_preserve_dev (struct iommufd_ucmd *ucmd, +u32 dev_id) +{ +#ifdef CONFIG_IOMMU_LIVEUPDATE
drivers/iommu/iommufd/selftest.c static int iommufd_test_dmabuf_revoke(struct iommufd_ucmd *ucmd, int fd, · line 2136
Quoted 10 lines
drivers/iommu/iommufd/selftest.c +10
-0+struct iommufd_device *idev; +int rc; + + idev =iommufd_get_device (ucmd, dev_id); +if (IS_ERR (idev)) +return PTR_ERR (idev); + +mutex_lock (&ucmd->ictx->liveupdate_mutex); + rc =__iommufd_device_preserve (idev,0 ); +mutex_unlock (&ucmd->ictx->liveupdate_mutex);
[Severity: Medium]
Can iommufd_test_liveupdate_preserve_dev() race with a concurrent iommufd_device_detach() and leave idev preserved after its attachment is removed?
In iommufd_device_detach(), igroup->lock is dropped between iommufd_device_unpreserve() and iommufd_hw_pagetable_detach():
drivers/iommu/iommufd/device.c:iommufd_device_detach() {
if (pasid == IOMMU_NO_PASID)
iommufd_device_unpreserve(idev);
hwpt = iommufd_hw_pagetable_detach(idev, pasid);
if (!hwpt)
return;
...
}Because IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV runs on the iommufd file descriptor without the VFIO device lock that serializes detach and close, the two paths can interleave:
CPU0 CPU1iommufd_device_detach(idev, ...)
iommufd_device_unpreserve(idev)
mutex_lock(&igroup->lock)
/* !idev->liveupdate_preserved */
mutex_unlock(&igroup->lock)
iommufd_test_liveupdate_preserve_dev()
__iommufd_device_preserve(idev, 0)
mutex_lock(&igroup->lock)
iommu_preserve_device(...)
idev->liveupdate_preserved = true
mutex_unlock(&igroup->lock)
iommufd_hw_pagetable_detach(...)
mutex_lock(&igroup->lock)
xa_erase(&igroup->pasid_attach,
IOMMU_NO_PASID)
mutex_unlock(&igroup->lock)When the liveupdate session closes (or idev is later unpreserved), iommufd_device_unpreserve() sees no IOMMU_NO_PASID attachment in igroup->pasid_attach, triggers WARN(1, "IOMMU_NO_PASID attachment not found"), and skips iommu_unpreserve_device(), leaking the preserved IOMMU device state.
Unpreserving the device inside iommufd_hw_pagetable_detach() under the same igroup->lock critical section that removes the IOMMU_NO_PASID attachment would close this window.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18