iommu: Add live update state preservation
Subsystems (10)
v3 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 work depends on,
- VFIO CDEV preservation series (v4):
https://lore.kernel.org/kvm/[email protected]/
- PCI device preservation series (v6):
https://lore.kernel.org/all/[email protected]/
- LUO file dependency preservation series:
https://lore.kernel.org/all/[email protected]/
The kernel tree with all dependencies is uploaded to the following Github location:
https://github.com/samikhawaja/linux/tree/iommu/phase1-v3
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 being used for such mapping should be SEAL SEAL'd during mapping.
- During live update boot, the state of preserved Intel VT-d, IOMMU domain and devices is restored.
- The restored IOMMU domains are reattached to the preserved devices during early boot.
- The DMA ownership of restored devices is also claimed during live update boot. This means that any attempt to bind a non-vfio drivers with them or binding a new iommufd with them will fail.
Architectural Overview:
The target architecture for IOMMU state preservation across a live update involves coordination between the Live Update Orchestrator, iommufd, and the IOMMU drivers.
The core design uses the Live Update Orchestrator's file descriptor preservation mechanism to preserve iommufd file descriptors. The user marks the iommufd HWPTs for preservation using a new ioctl added in this series. Once done, the preservation of iommufd inside an LUO session is triggered using LUO ioctls. During preservation, the LUO preserve callback for an iommufd walks through the HWPTs it manages to identify the ones that need to be preserved. Once identified, a new IOMMU core API is used to preserve the iommu domain. The IOMMU core uses Generic Page Table to preserve the page tables of these domains. The domains are then marked as preserved.
When the user triggers the preservation of a VFIO cdev that is attached to an iommufd that is preserved, the device attachment state of that VFIO cdev is also preserved using an API exported by iommufd. IOMMUFD fetches all the information that needs to be preserved and calls the IOMMU core API to preserve the device state. The IOMMU core also preserves state of IOMMU that is associated with this device.
The IOMMU core has LUO FLB registered with the iommufd LUO file handler so the preserved iommu domain and iommu hardware unit state is available during boot for early restore in the next kernel.
During boot the driver fetches the preserved state from the IOMMU core and restores the state of preserved IOMMUs. Later when IOMMU core goes through the devices and probes them, the iommu domains of preserved devices are restored and the preserved devices are attached to them. During attachment, the DMA ownership of these devices is also claimed.
Tested:
The new iommufd_liveupdate_kexec_test selftest was used to verify the preservation logic. It was tested using QEMU with virtual IOMMU (VT-d) support with virtio pcie device bound to the vfio-pci driver.
Also Tested on an Intel machine with DSA device bound to vfio-pci driver.
Following steps were followed for verification,
- Bind the test device with vfio-pci driver
- Run test on the machine by running
./iommufd_liveupdate_kexec_test <vfio-cdev-path>
- Trigger Kexec.
- After reboot, try binding the device to a non-vfio pci driver,
echo <device bdf> > /sys/class/bus/drivers/pci-pf-stub/bind
- This should fail with "Device or resource busy".
- Bind the device with vfio-pci driver and run the test again.
./iommufd_liveupdate_kexec_test <vfio-cdev-path> --stage 2
- Test verifies that the device cannot be bound with a new iommufd and the session cannot be finished.
Future Work:
- Phase 2 with IOMMUFD restore to reclaim the preserved vfio cdev and restore the preserved HWPTs.
- Full support for PASID preservation.
- Nested IOMMU preservation.
- Extend support to other IOMMU architectures (e.g., AMD-Vi, Arm SMMUv3).
- DMA Alloc preservation support buddy allocator only.
https://github.com/samikhawaja/linux/tree/dma-alloc-preserve
Roadmap:
The doc below gives a break down of the overall work into Patch series needed to complete Liveupdate feature from IOMMU perspective:
https://docs.google.com/document/d/1enDn-uPE9U77U-xHEnzn6HHGKiePSAtMIP8EDU3NO0M
High-Level Sequence Flow:
The following diagrams illustrate the high-level interactions during the preservation phase. Note that function names in the diagram are kept abbreviated to save horizontal space.
Preserve:
Before live update the PREPARE event of Liveupdate Orchestrator invokes callbacks of the registered file and subsystem handlers.
Userspace (VMM) | LUO Core | iommufd | IOMMU Core | IOMMU Driver
-----------------|----------|---------------|---------------|-------------
| | | |
MARK_HWPT | | | |
---------------------------> | |
| | Mark HWPT for | |
| | preservation | |
| | | |
PRESERVE | | | |
iommufd_fd | | | |
-----------------> | | |
| preserve | | |
|----------> | |
| | For each HWPT | |
| |--------------> |
| | | domain_presrv |
| | |-------------->
| | | | gpt(preserve)
| | |<--------------|
| |<--------------| |
|<---------| | |
| | | |
... | | | |
| | | |
PRESERVE, | | | |
vfio_cdev_fd | | | |
-----------------> | | |
| preserve | | |
|----------> | |
| | | |
| | iommu_preserv | |
| | _device() | |
| |--------------> |
| | | preserve |
| | | (iommu_hw) |
| | |-------------->
| | | | preserve(root)
| | | | preserve(pasid)
| | |<--------------|
| | | |
| | | preserve |
| | | _device(dev) |
| | |-------------->
| | | |
| | |<--------------|
| |<--------------| |
|<---------| | |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:
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.
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 (16):
memfd: export memfd_get_seals()
iommu: Implement IOMMU Live update FLB callbacks iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
iommupt: Implement preserve/unpreserve/restore callbacks
iommu: Implement IOMMU domain preservation
iommu: Implement device and IOMMU HW preservation iommu/vt-d: Implement device and iommu preserve/unpreserve ops iommu/vt-d: clear unpreserved context entries during shutdown
iommu: Add APIs to get iommu and device preserved state iommu/vt-d: Restore IOMMU state and reclaimed domain ids
iommu: Restore and reattach preserved domains to devices iommu/vt-d: Handle reattach of the restored domain iommu/vt-d: preserve PASID table of preserved device
iommufd: Add APIs to preserve/unpreserve a vfio cdev vfio/pci: Preserve the iommufd state of the vfio cdev iommufd/selftest: Add test to verify iommufd preservation
YiFei Zhu (2):
iommufd: Implement ioctl to mark HWPT for preservation
iommufd: Persist iommu hardware pagetables for live update
MAINTAINERS | 19 + drivers/iommu/Kconfig | 12 + drivers/iommu/Makefile | 1 + drivers/iommu/generic_pt/iommu_pt.h | 122 ++++ drivers/iommu/generic_pt/kunit_iommu_pt.h | 28 + drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 150 ++-- drivers/iommu/intel/iommu.h | 58 ++ drivers/iommu/intel/liveupdate.c | 477 +++++++++++++ drivers/iommu/intel/pasid.c | 7 +- drivers/iommu/intel/pasid.h | 9 + drivers/iommu/iommu-pages.c | 108 ++- drivers/iommu/iommu-pages.h | 30 + drivers/iommu/iommu.c | 83 ++- drivers/iommu/iommufd/Makefile | 2 + drivers/iommu/iommufd/device.c | 102 +++ drivers/iommu/iommufd/io_pagetable.c | 11 + drivers/iommu/iommufd/io_pagetable.h | 1 + drivers/iommu/iommufd/iommufd_private.h | 46 ++ drivers/iommu/iommufd/liveupdate.c | 354 ++++++++++ drivers/iommu/iommufd/main.c | 19 +- drivers/iommu/iommufd/pages.c | 11 + drivers/iommu/liveupdate.c | 660 ++++++++++++++++++ drivers/vfio/device_cdev.c | 10 + drivers/vfio/pci/vfio_pci_liveupdate.c | 37 +- include/linux/generic_pt/iommu.h | 30 + include/linux/iommu-liveupdate.h | 156 +++++ include/linux/iommu.h | 39 ++ include/linux/iommufd.h | 29 + include/linux/kho/abi/iommu.h | 241 +++++++ include/linux/kho/abi/iommufd.h | 51 ++ include/uapi/linux/iommufd.h | 26 + mm/memfd.c | 1 + tools/testing/selftests/iommu/Makefile | 12 + .../iommu/iommufd_liveupdate_kexec_test.c | 241 +++++++ 35 files changed, 3134 insertions(+), 50 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: 34e8f02817e31826e76bb2ded48bf28fe921f20b
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
PATCH v3 1/18 memfd: export memfd_get_seals()
memfd_get_seals() is used by iommufd during preservation to make sure that he preserved memfd was sealed when it was mapped into iommufd. Since iommufd can be built as a module, export memfd_get_seals() to avoid linker error.
--- mm/memfd.c | 1 + 1 file changed, 1 insertion(+)
mm/memfd.c +1 -0
@@ -315,6 +315,7 @@ int memfd_get_seals(struct file *file)return seals ? *seals : -EINVAL ; } +EXPORT_SYMBOL_GPL (memfd_get_seals);long memfd_fcntl (struct file *file,unsigned int cmd,unsigned int arg) {
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v3 01/18] memfd: export memfd_get_seals()
Quoted 2 lines · Samiullah Khawaja
memfd_get_seals()is used by iommufd during preservation to make sure that he preserved memfd was sealed when it was mapped into iommufd.
Hi,
Minor nit s/he/the
Quoted 8 lines
Since iommufd can be built as a module, export
memfd_get_seals()to avoid linker error.Cc Hugh Dickins <[email protected]>Cc Baolin Wang <[email protected]>Cc Andrew Morton <[email protected]>
Quoted 20 lines
Signed-off-by Samiullah Khawaja <[email protected]>--- mm/memfd.c | 1 + 1 file changed, 1 insertion(+)mm/memfd.c +1
-0@@ -315,6 +315,7 @@ int memfd_get_seals(struct file *file)return seals ? *seals : -EINVAL ; } +EXPORT_SYMBOL_GPL (memfd_get_seals);long memfd_fcntl (struct file *file,unsigned int cmd,unsigned int arg) {Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v3 01/18] memfd: export memfd_get_seals()
In reply to Samiullah KhawajaQuoted 11 lines · Samiullah Khawaja
memfd_get_seals()is used by iommufd during preservation to make sure that he preserved memfd was sealed when it was mapped into iommufd. Since iommufd can be built as a module, exportmemfd_get_seals()to avoid linker error.Cc Hugh Dickins <[email protected]>Cc Baolin Wang <[email protected]>Cc Andrew Morton <[email protected]>Signed-off-by Samiullah Khawaja <[email protected]>
Signature · 2 lines
Regards, Pratyush Yadav
PATCH v3 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 | 212 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 18 +++ include/linux/kho/abi/iommu.h | 207 ++++++++++++++++++++++++++++++ 6 files changed, 459 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
@@ -13485,6 +13485,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
@@ -403,6 +403,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 + 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
@@ -16,6 +16,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 +212 -0
212 lines not on this page · raw message
include/linux/iommu-liveupdate.h new file +18 -0
@@ -0,0 +1,18 @@ +/* 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> + +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler); +void iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler); + +#endif /* _LINUX_IOMMU_LIVEUPDATE_H */
include/linux/kho/abi/iommu.h new file +207 -0
207 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v3 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 12 lines
--- MAINTAINERS | 9 ++ drivers/iommu/Kconfig | 12 ++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 212 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 18 +++ include/linux/kho/abi/iommu.h | 207 ++++++++++++++++++++++++++++++ 6 files changed, 459 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
../..
Quoted 111 lines
include/linux/kho/abi/iommu.h new file +105
-0@@ -0,0 +1,207 @@ +/* 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/compiler.h> +#include <linux/types.h> + +/** +* DOC: IOMMU File-Lifecycle Bound (FLB) Live Update ABI +* +* This header defines the ABI for preserving IOMMU state across kexec using +* Live Update File-Lifecycle Bound (FLB) data. +* +* This interface is a contract. Any modification to any of the serialization +* structs defined here constitutes a breaking change. Such changes require +* incrementing the version number in the IOMMU_LUO_FLB_COMPATIBLE string. +* +* Memory Layout of Serialization Structures: +* ========================================== +* +* Each serialized type (IOMMU, Domain, Device) is stored in a linked list of +* arrays. The first array is allocated initially. When an array is full, a new +* array is allocated and its physical address is stored in the next_array_phys +* field of the hdr of the current array. +* +* Top Level (struct iommu_flb_ser): +* +---------------------------+ +* | - iommu_array_phys | +* | - iommu_domain_array_phys | +* | - device_array_phys | +* +---------------------------+ +* +* Each Array contains the serialized objects of the respective type. For +* example see below the representation of struct iommu_domain_array_ser. +* +* +---------------------------+ +---------------------------+ +* | iommu_domain_array_ser |-->| iommu_domain_array_ser |--> NULL +* | - hdr.next_array_phys | | - hdr.next_array_phys | +* | - hdr.nr_objects | | - hdr.nr_objects | +* | | | | +* | objects[]: | | objects[]: | +* | [ iommu_domain_ser ] | | [ iommu_domain_ser ] | +* | [ iommu_domain_ser ] | | [ iommu_domain_ser ] | +* | ... | | ... | +* +---------------------------+ +---------------------------+ +* +* Each object in the array starts with a common header (iommu_hdr_ser). +* For example, the layout of struct iommu_domain_ser is: +* +* +-----------------------------+ +* | iommu_domain_ser | +* | +-------------------------+ | +* | | hdr (iommu_hdr_ser) | | +* | | - ref_count | | +* | | - deleted / incoming | | +* | +-------------------------+ | +* | - top_table_phys | | +* | - top_level | | +* | - restored_domain | | +* +-----------------------------+ +* +* This pattern applies identically to iommu_device_ser and iommu_hw_ser. +*/ + +#define IOMMU_LUO_FLB_COMPATIBLE "iommu-liveupdate-v1" + +/** +* enum iommu_type_ser - Type of the IOMMU being preserved +* @IOMMU_INVALID: Invalid type of IOMMU +* +* IOMMU type is stored in the IOMMU HW state to differentiate between various +* IOMMU HWs. +*/ +enum iommu_type_ser { +IOMMU_INVALID , +}; + +#define IOMMU_SER_FLAG_DELETED (1 << 0) +#define IOMMU_SER_FLAG_INCOMING (1 << 1) + +/** +* struct iommu_hdr_ser - Common header for all serialized IOMMU objects +* @ref_count: Reference count for the object +* @flags: Bitmask of IOMMU_SER_FLAG_* flags indicating object state +*/ +struct iommu_hdr_ser { +u32 ref_count; +u32 flags; +} __packed; + +/** +* struct iommu_domain_ser - Serialized state of an IOMMU domain +* @hdr: Common object header +* @top_table_phys: Physical address of the top-level page table +* @top_level: Level of the top-level page table +* @vasz: Virtual Address Size +* @sign_extend: FEAT_SIGN_EXTEND is enabled for this domain +* @restored_domain: Pointer to the restored domain (valid only after restore)
include/linux/kho/abi/iommu.h line 115
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+*/ +struct iommu_domain_ser { +struct iommu_hdr_ser hdr; +u64 top_table_phys; +u64 top_level; +u32 vasz; +u32 sign_extend:1 ; +struct iommu_domain *restored_domain; +} __packed; +
Hi,
nit, The v3 changelog has "Replace bitfields with u32 flags", but sign_extend:1 looks like it was missed. this is 37 bytes, the only arrayed record in this header that isn't a multiple of 8 (iommu_device_ser is 56, iommu_hw_ser 112). The arrays are strided with pointer arithmetic, so the skew accumulates: from objects[1] onwards none of the u64s are naturally aligned.
Looks like a leftover from the bitfield cleanup.
-Ankit
Quoted 95 lines
include/linux/kho/abi/iommu.h new file +92
-0+/** +* struct iommu_dev_map_ser - Serialized mapping between device, domain, +* and IOMMU instance. +* @attachment_id: ID of the attachment between device and domain. +* @domain_phys: Physical address of the domain +* @iommu_phys: Physical address of the IOMMU +*/ +struct iommu_dev_map_ser { +u64 attachment_id; +u64 domain_phys; +u64 iommu_phys; +} __packed; + +/** +* struct iommu_device_ser - Serialized state of a device +* @hdr: Common object header +* @devid: Device ID +* @pci_domain_nr: PCI domain number +* @domain_iommu_ser: Domain and IOMMU mapping +*/ +struct iommu_device_ser { +struct iommu_hdr_ser hdr; +u32 devid; +u32 pci_domain_nr; +struct iommu_dev_map_ser domain_iommu_ser; +} __packed; + +/** +* struct iommu_hw_ser - Serialized state of an IOMMU instance +* @hdr: Common object header +* @token: Unique token for the IOMMU +* @type: IOMMU type serialized state belongs to +*/ +struct iommu_hw_ser { +struct iommu_hdr_ser hdr; +u64 token; +u64 type; +} __packed; + +/** +* struct iommu_array_hdr_ser - Header for an array of serialized objects +* @next_array_phys: Physical address of the next array of objects +* @nr_objects: Number of objects in the current array +*/ +struct iommu_array_hdr_ser { +u64 next_array_phys; +u64 nr_objects; +} __packed; + +/** +* struct iommu_hw_array_ser - An array containing serialized IOMMU HWs +* @hdr: Array header +* @objects: Array of serialized IOMMU devices +*/ +struct iommu_hw_array_ser { +struct iommu_array_hdr_ser hdr; +struct iommu_hw_ser objects[]; +} __packed; + +/** +* struct iommu_domain_array_ser - An array containing serialized domains +* @hdr: Array header +* @objects: Array of serialized domains +*/ +struct iommu_domain_array_ser { +struct iommu_array_hdr_ser hdr; +struct iommu_domain_ser objects[]; +} __packed; + +/** +* struct iommu_device_array_ser - An array containing serialized devices +* @hdr: Array header +* @objects: Array of serialized devices +*/ +struct iommu_device_array_ser { +struct iommu_array_hdr_ser hdr; +struct iommu_device_ser objects[]; +} __packed; + +/** +* struct iommu_flb_ser - Top-level serialization structure +* @iommu_array_phys: Physical address of the first array of IOMMU HWs +* @iommu_domain_array_phys: Physical address of the first array of domains +* @device_array_phys: Physical address of the first array of devices +*/ +struct iommu_flb_ser { +u64 iommu_array_phys; +u64 iommu_domain_array_phys; +u64 device_array_phys; +} __packed; + +#endif /* _LINUX_KHO_ABI_IOMMU_H */ Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Re: [PATCH v3 02/18] iommu: Implement IOMMU Live update FLB callbacks
Quoted 24 lines · Ankit Soni
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 12 lines
--- MAINTAINERS | 9 ++ drivers/iommu/Kconfig | 12 ++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 212 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 18 +++ include/linux/kho/abi/iommu.h | 207 ++++++++++++++++++++++++++++++ 6 files changed, 459 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../..
[snip]
Quoted 10 lines
Hi,
nit, The v3 changelog has "Replace bitfields with u32 flags", but sign_extend:1 looks like it was missed. this is 37 bytes, the only arrayed record in this header that isn't a multiple of 8 (
iommu_device_seris 56,iommu_hw_ser112). The arrays are strided with pointer arithmetic, so the skew accumulates: from objects[1] onwards none of the u64s are naturally aligned.Looks like a leftover from the bitfield cleanup.
Yes, missed this one in v3. Just sent out v4, it should be cleaned up now.
Thanks,
Sami
Quoted 3 lines
-Ankit
PATCH v3 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 | 108 ++++++++++++++++++++++++++++++++++-- drivers/iommu/iommu-pages.h | 30 ++++++++++ 2 files changed, 134 insertions(+), 4 deletions(-)
drivers/iommu/iommu-pages.c +104 -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,100 @@ void iommu_put_pages_list(struct iommu_pages_list *list) }EXPORT_SYMBOL_GPL (iommu_put_pages_list); +#if IS_ENABLED (CONFIG_IOMMU_LIVEUPDATE ) +/** +* iommu_unpreserve_pages - Unpreserve pages that were preserved in KHO +* @virt: Virtual address of page to be unpreserved +*/ +void iommu_unpreserve_pages (void *virt) +{ +kho_unpreserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_pages); + +/** +* iommu_preserve_pages - Preserve pages using KHO +* @virt: Virtual address of page to preserve +* +* Returns 0 on success, negative error on failure +*/ +int iommu_preserve_pages (void *virt) +{ +return kho_preserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_preserve_pages); + +/** +* iommu_unpreserve_pages_list - Unpreserve a list of KHO preserved pages +* @list: List of pages to unpreserve +*/ +void iommu_unpreserve_pages_list (struct iommu_pages_list *list) +{ +struct ioptdesc *iopt; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) +kho_unpreserve_folio (ioptdesc_folio (iopt)); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_pages_list); + +/** +* iommu_restore_pages - Restore pages that were preserved in KHO +* @phys: Physical address of page to restore +*/ +void iommu_restore_pages (u64 phys) +{ +struct ioptdesc *iopt; +struct folio *folio; +unsigned long pgcnt; +unsigned int order; + + folio =kho_restore_folio (phys); +BUG_ON (!folio); + + iopt =folio_ioptdesc (folio); + +/* +* For the restored pages incoherent is set to false as these are not +* mapped using the DMA_API. The remapping of these pages using DMA_API +* is not needed as these are not going to be written to by the new +* kernel. +*/ + iopt->incoherent = false; + + order =folio_order (folio); + pgcnt =1UL << order; +iommu_folio_update_stats (folio, pgcnt); +} +EXPORT_SYMBOL_GPL (iommu_restore_pages); + +/** +* iommu_preserve_pages_list - Preserve list of pages using KHO +* @list: List of pages to preserve +* +* Returns 0 on success, negative error on failure +*/ +int iommu_preserve_pages_list (struct iommu_pages_list *list) +{ +struct ioptdesc *iopt; +int ret; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { + ret =kho_preserve_folio (ioptdesc_folio (iopt)); +if (ret) + goto err; + } + +return 0 ; + +err: +list_for_each_entry_continue_reverse (iopt, &list->pages, iopt_freelist_elm) +kho_unpreserve_folio (ioptdesc_folio (iopt)); + +return ret; +} +EXPORT_SYMBOL_GPL (iommu_preserve_pages_list); +#endif +/** * iommu_pages_start_incoherent - Setup the page for cache incoherent operation * @virt: The page to setup
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.54.0.1136.gdb2ca164c4-goog
PATCH v3 4/18 iommupt: Implement preserve/unpreserve/restore callbacks
In reply to Samiullah KhawajaAdd iommupt ops for presevation, unpresevation and restoration of iommu page tables for liveupdate. Use the existing page walker to preserve the ioptdesc of the top_table and the lower tables.
Preserve top_level, VASZ and FEAT Sign Extended to restore the domain in the next kernel. On restore, the domain has only the preserved features enabled and all the other features are zeroed. This is ok since the restored domain is made immutable and can only be freed. A kunit test is added to verify that the IOMMU domain free can be done with trimmed features.
--- drivers/iommu/generic_pt/iommu_pt.h | 122 ++++++++++++++++++++++ drivers/iommu/generic_pt/kunit_iommu_pt.h | 28 +++++ include/linux/generic_pt/iommu.h | 30 ++++++ 3 files changed, 180 insertions(+)
drivers/iommu/generic_pt/iommu_pt.h +122 -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 ,
@@ -961,6 +962,118 @@ 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 = { + .free_list =IOMMU_PAGES_LIST_INIT (collect.free_list), + }; + +iommu_pages_list_add (&collect.free_list, range.top_table); +pt_walk_range (&range, __collect_tables, &collect); + +iommu_unpreserve_pages_list (&collect.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 = { + .free_list =IOMMU_PAGES_LIST_INIT (collect.free_list), + }; +int ret; + +iommu_pages_list_add (&collect.free_list, range.top_table); +pt_walk_range (&range, __collect_tables, &collect); + + ret =iommu_preserve_pages_list (&collect.free_list); +if (ret) +return ret; + + ser->top_table_phys =virt_to_phys (range.top_table); + ser->top_level = range.top_level; + +/* +* VASZ and SIGN_EXTEND will be needed in next kernel for collector page +* table walk to restore and free pages. +* +* Use the max_vasz_lg2 from range, as that is the current one if +* DYNAMIC_TOP is supported. +*/ + ser->vasz = range.max_vasz_lg2; + ser->sign_extend =pt_feature (common,PT_FEAT_SIGN_EXTEND ); + +return 0 ; +} + +static int __restore_tables (struct pt_range *range,void *arg, +unsigned int level,struct pt_table_p *table) +{ +struct pt_state pts =pt_init (range, level, table); +int ret; + +for_each_pt_level_entry (&pts) { +if (pts.type ==PT_ENTRY_TABLE ) { +iommu_restore_pages (virt_to_phys (pts.table_lower)); + +/* +* pt_descend can only fail if pts.table_lower is not +* init. So the if statement below is dead code. +*/ + ret =pt_descend (&pts, arg, __restore_tables); +if (ret) +return ret; + } + } + +return 0 ; +} + +static const struct pt_iommu_ops NS (ops_immutable ); + +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; + + common->max_vasz_lg2 = ser->vasz; + +/* +* Restored page tables are strictly transient and only permitted to be +* destroyed via deinit() op. Because we only preserve user-assigned +* devices utilizing pass-through frameworks (VFIO / IOMMUFD), any +* concurrent or subsequent map/unmap operations on a restored domain +* are explicitly blocked at the subsystem boundary (e.g., via IOAS +* immutability). +*/ + iommu_table->ops = &NS (ops_immutable); + +/* +* It is safe to override this here since this domain is immutable and +* can only be freed. +*/ + common->features =0 ; +if (ser->sign_extend) + common->features |=BIT (PT_FEAT_SIGN_EXTEND ); + + range =pt_all_range (common); +iommu_restore_pages (ser->top_table_phys); + +/* Free new table */ +iommu_free_pages (range.top_table); + +/* 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 iommu_pages_list free_list;pt_vaddr_t unmapped;
@@ -1136,6 +1249,15 @@ 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 const struct pt_iommu_ops NS (ops_immutable ) = { + .deinit =NS (deinit), };static int pt_init_common (struct pt_common *common)
drivers/iommu/generic_pt/kunit_iommu_pt.h +28 -0
@@ -426,6 +426,33 @@ static void test_mixed(struct kunit *test)check_iova (test, start, oa, len); } +static void test_restore_free (struct kunit *test) +{ +struct kunit_iommu_priv *priv = test->priv; +struct pt_range top_range =pt_top_range (priv->common); +u64 start =0x3fe400ULL <<12 ; +u64 end =0x4c0600ULL <<12 ; +pt_vaddr_t len = end - start; + +if (top_range.last_va <= start ||sizeof (unsigned long ) ==4 ) +kunit_skip (test,"range is too small" ); +if ((priv->safe_pgsize_bitmap &GENMASK (30 ,21 )) != (BIT (30 ) |BIT (21 ))) +kunit_skip (test,"incompatible psize" ); + +/* Map a large mixed range to populate multiple levels of page tables */ +do_map (test, start, start, len); + +/* +* Simulate a restored state by clearing all features except +* SIGN_EXTEND. This verifies that the generic page table free walker +* can correctly tear down a populated domain when other features are +* zeroed. +*/ + priv->common->features &=BIT (PT_FEAT_SIGN_EXTEND ); + +/* 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),
@@ -434,6 +461,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.54.0.1136.gdb2ca164c4-goog
PATCH v3 5/18 iommu: Implement IOMMU domain preservation
In reply to Samiullah KhawajaAdd IOMMU domain ops that can be implemented by the IOMMU drivers if they support IOMMU domain preservation across liveupdate. The new IOMMU domain preserve, unpreserve and restore APIs call these ops to perform respective live update operations.
--- drivers/iommu/liveupdate.c | 104 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 14 +++++ include/linux/iommu.h | 5 ++ 3 files changed, 123 insertions(+)
drivers/iommu/liveupdate.c +104 -0
@@ -8,11 +8,15 @@#define pr_fmt (fmt) "iommu: liveupdate: " fmt#include <linux/errno.h> +#include <linux/generic_pt/iommu.h> #include <linux/iommu-liveupdate.h> #include <linux/iommu.h> #include <linux/kexec_handover.h> #include <linux/liveupdate.h> +#define iommu_max_objs_per_page (_array) \ + ((PAGE_SIZE - sizeof(struct iommu_array_hdr_ser)) / sizeof((_array)->objects[0])) +struct iommu_flb_obj {struct mutex lock;struct iommu_flb_ser *ser;
@@ -210,3 +214,103 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)liveupdate_unregister_flb (handler, &iommu_flb); }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); + +static int alloc_object_ser (void **curr_array_ptr,u64 max_objs) +{ +struct iommu_array_hdr_ser *curr_array = *curr_array_ptr; +struct iommu_array_hdr_ser *next_array; + +/* +* The objects marked as deleted are not reused to avoid traversal of +* linked-list and arrays. +*/ +if (curr_array->nr_objects >= max_objs) { + next_array =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (next_array)) +return PTR_ERR (next_array); + + curr_array->next_array_phys =virt_to_phys (next_array); + *curr_array_ptr = next_array; + curr_array = next_array; + } + +return curr_array->nr_objects++; +} + +static struct iommu_domain_ser *alloc_iommu_domain_ser (struct iommu_flb_obj *flb) +{ +int idx; + + idx =alloc_object_ser ((void **) &flb->curr_domain_array, +iommu_max_objs_per_page (flb->curr_domain_array)); +if (idx <0 ) +return ERR_PTR (idx); + + flb->curr_domain_array->objects[idx].hdr.ref_count =1 ; +return &flb->curr_domain_array->objects[idx]; +} + +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; + +guard (mutex)(&flb_obj->lock); + domain_ser =alloc_iommu_domain_ser (flb_obj); +if (IS_ERR (domain_ser)) +return PTR_ERR (domain_ser); + + ret = pt->ops->preserve (pt, domain_ser); +if (ret) { + domain_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; +return ret; + } + + domain->preserved_state = domain_ser; + *ser = domain_ser; +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_preserve_domain); + +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 ; + +guard (mutex)(&flb_obj->lock); + +if (!domain->preserved_state) +return ; + +/* +* There is no check for attached devices here. The correctness relies +* on the Live Update Orchestrator's session lifecycle. All resources +* (iommufd, vfio devices) are preserved within a single session. If the +* session is torn down, the .unpreserve callbacks for all files will be +* invoked, ensuring a consistent cleanup without needing explicit +* refcounting for the serialized objects here. +*/ + domain_ser = domain->preserved_state; + pt->ops->unpreserve (pt, domain_ser); + domain_ser->hdr.flags |=IOMMU_SER_FLAG_DELETED ; + domain->preserved_state =NULL ; +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_domain);
include/linux/iommu-liveupdate.h +14 -0
@@ -12,6 +12,20 @@#include <linux/liveupdate.h> #include <linux/kho/abi/iommu.h> +#ifdef CONFIG_IOMMU_LIVEUPDATE +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_preserve_domain (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_domain (struct iommu_domain *domain) +{ +} +#endif +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler);void iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler);
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 *preserved_state; +#endif };static inline bool iommu_is_dma_domain (struct iommu_domain *domain)
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
PATCH v3 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.c | 18 ++++ drivers/iommu/liveupdate.c | 172 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 43 ++++++++ include/linux/iommu.h | 34 ++++++ 4 files changed, 267 insertions(+)
drivers/iommu/iommu.c +18 -0
@@ -307,6 +307,24 @@ void iommu_device_unregister(struct iommu_device *iommu) }EXPORT_SYMBOL_GPL (iommu_device_unregister); +static int _iommu_for_each_dev_cb (struct device *dev,void *data) +{ +struct iommu_dev_iter *iter = data; + +if (dev->iommu && dev->iommu->iommu_dev == iter->iommu) +return iter->fn (dev, iter->iommu, iter->arg); + +return 0 ; +} + +void iommu_for_each_dev (struct iommu_dev_iter *iter) +{ +for (int i =0 ; i <ARRAY_SIZE (iommu_buses); i++) +bus_for_each_dev (iommu_buses[i],NULL , iter, + _iommu_for_each_dev_cb); +} +EXPORT_SYMBOL_GPL (iommu_for_each_dev); +#if IS_ENABLED (CONFIG_IOMMUFD_TEST ) void iommu_device_unregister_bus(struct iommu_device *iommu, const struct bus_type *bus,
drivers/iommu/liveupdate.c +172 -0
181 lines not on this page · raw message
include/linux/iommu-liveupdate.h +43 -0
63 lines not on this page · raw message
include/linux/iommu.h +34 -0
@@ -655,6 +655,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.
@@ -711,6 +715,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;
@@ -798,6 +809,8 @@ struct iommu_domain_ops {* @singleton_group: Used internally for drivers that have only one group * @max_pasids: number of supported PASIDs * @ready: set once iommu_device_register() has completed successfully +* @outgoing_preserved_state: preserved iommu state of outgoing kernel for +* liveupdate. */ struct iommu_device {struct list_head list;
@@ -807,6 +820,10 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_hw_ser *outgoing_preserved_state; +#endif };/**
@@ -861,6 +878,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,
@@ -1177,6 +1197,20 @@ static inline void *dev_iommu_priv_get(struct device *dev)void dev_iommu_priv_set (struct device *dev,void *priv); +typedef int (*iommu_dev_iter_fn )(struct device *dev, +struct iommu_device *iommu,void *arg); + +/** +* struct iommu_dev_iter - Iterator for devices attached to an IOMMU +*/ +struct iommu_dev_iter { +struct iommu_device *iommu; +iommu_dev_iter_fn fn; +void *arg; +}; + +void iommu_for_each_dev (struct iommu_dev_iter *iter); +extern struct mutex iommu_probe_device_lock;int iommu_probe_device (struct device *dev);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
PATCH v3 7/18 iommu/vt-d: Implement device and iommu preserve/unpreserve ops
In reply to Samiullah KhawajaAdd implementation of the device and iommu presevation in a separate file. Also set the device and iommu preserve/unpreserve ops in the struct iommu_ops.
--- MAINTAINERS | 8 ++ drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 8 +- drivers/iommu/intel/iommu.h | 28 +++++ drivers/iommu/intel/liveupdate.c | 175 +++++++++++++++++++++++++++++++ include/linux/kho/abi/iommu.h | 21 ++++ 6 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 drivers/iommu/intel/liveupdate.c
MAINTAINERS +8 -0
@@ -13002,6 +13002,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 IPU3 CSI-2 CIO2 DRIVER M: Yong Zhi <[email protected]> M: Sakari Ailus <[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 +6 -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>
@@ -60,8 +61,6 @@ static int force_on = 0;static int intel_iommu_tboot_noforce;static int no_platform_optin; -#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.
@@ -3926,6 +3925,11 @@ 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, + .preserve = intel_iommu_preserve, + .unpreserve = intel_iommu_unpreserve, +#endif };static void quirk_iommu_igfx (struct pci_dev *dev)
drivers/iommu/intel/iommu.h +28 -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
@@ -1284,6 +1286,32 @@ 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); +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); +#else +static inline int intel_iommu_preserve_device (struct device *dev, +struct iommu_device_ser *device_ser) +{ +return -EOPNOTSUPP ; +} + +static inline int intel_iommu_preserve (struct iommu_device *iommu, +struct iommu_hw_ser *iommu_ser) +{ +return -EOPNOTSUPP ; +} + +static inline 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 +175 -0
175 lines not on this page · raw message
include/linux/kho/abi/iommu.h +21 -0
@@ -80,6 +80,7 @@*/ enum iommu_type_ser {IOMMU_INVALID , +IOMMU_INTEL , };#define IOMMU_SER_FLAG_DELETED (1 << 0)
@@ -140,16 +141,36 @@ struct iommu_device_ser {struct iommu_dev_map_ser domain_iommu_ser; } __packed; +/** +* struct iommu_intel_ser - Serialized state of an Intel IOMMU instance +* @restored: Whether IOMMU state is restored +* @phys_addr: Physical address of the IOMMU register base +* @root_table: Physical address of the root entry table +* @context_tables_bitmap: Bitmap representing the context tables that are +* preserved. +*/ +struct iommu_intel_ser { +u8 restored; +u8 padding[7 ]; +u64 phys_addr; +u64 root_table; +u64 context_tables_bitmap[8 ];/* Tracks upto 512 context tables */ +}; +/** * struct iommu_hw_ser - Serialized state of an IOMMU instance * @hdr: Common object header * @token: Unique token for the IOMMU * @type: IOMMU type serialized state belongs to +* @intel: Intel specific serialization data */ struct iommu_hw_ser {struct iommu_hdr_ser hdr;u64 token;u64 type; +union { +struct iommu_intel_ser intel; + }; } __packed;/**
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Baolu Lu
maintainerRe: [PATCH v3 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops
Quoted 65 lines · Samiullah Khawaja
Add implementation of the device and iommu presevation in a separate file. Also set the device and iommu preserve/unpreserve ops in the struct
iommu_ops.Signed-off-by Samiullah Khawaja <[email protected]>--- MAINTAINERS | 8 ++ drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 8 +- drivers/iommu/intel/iommu.h | 28 +++++ drivers/iommu/intel/liveupdate.c | 175 +++++++++++++++++++++++++++++++ include/linux/kho/abi/iommu.h | 21 ++++ 6 files changed, 239 insertions(+), 2 deletions(-)create mode 100644
drivers/iommu/intel/liveupdate.cMAINTAINERS +8
-0@@ -13002,6 +13002,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 IPU3 CSI-2 CIO2 DRIVER M: Yong Zhi <[email protected]> M: Sakari Ailus <[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.odrivers/iommu/intel/iommu.c +1
-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> @@ -60,8 +61,6 @@ static int force_on = 0;static int intel_iommu_tboot_noforce;static int no_platform_optin; -#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.
drivers/iommu/intel/iommu.c const struct iommu_ops intel_iommu_ops = { · line 3932
Quoted 8 lines
drivers/iommu/intel/iommu.c +5
-0@@ -3926,6 +3925,11 @@ 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, + .preserve = intel_iommu_preserve, + .unpreserve = intel_iommu_unpreserve, +#endif
Any reason why an unpreserve_device callback is missing here?
Quoted 82 lines
drivers/iommu/intel/iommu.c +0
-0};static void quirk_iommu_igfx (struct pci_dev *dev)drivers/iommu/intel/iommu.h +28
-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 @@ -1284,6 +1286,32 @@ 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); +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); +#else +static inline int intel_iommu_preserve_device (struct device *dev, +struct iommu_device_ser *device_ser) +{ +return -EOPNOTSUPP ; +} + +static inline int intel_iommu_preserve (struct iommu_device *iommu, +struct iommu_hw_ser *iommu_ser) +{ +return -EOPNOTSUPP ; +} + +static inline 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 +27
-0@@ -0,0 +1,175 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2026, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#define pr_fmt (fmt) "DMAR: liveupdate: " fmt + +#include <linux/kexec_handover.h> +#include <linux/liveupdate.h> +#include <linux/iommu-liveupdate.h> +#include <linux/module.h> +#include <linux/pci.h> + +#include "iommu.h" +#include "../iommu-pages.h" + +/* 2 tables per bus in scalable mode with upper table at odd bit */ +#define CONTEXT_TABLE_PRESERVED_BIT (bus, devfn) ((bus << 1) + (devfn >> 7)) +static bool is_context_table_preserved (struct intel_iommu *iommu, +struct iommu_hw_ser *ser, +u8 bus,u8 devfn) +{ +return test_bit (CONTEXT_TABLE_PRESERVED_BIT (bus, devfn), + (unsigned long *)&ser->intel.context_tables_bitmap[0 ]); +}
drivers/iommu/intel/liveupdate.c line 37
Quoted 10 lines
drivers/iommu/intel/liveupdate.c new file +10
-0+ +static void unpreserve_context_table (struct intel_iommu *iommu, +struct iommu_hw_ser *ser, +u8 bus,u8 devfn) +{ +struct context_entry *context; + +spin_lock (&iommu->lock); + context =iommu_context_addr (iommu, bus, devfn,0 ); +spin_unlock (&iommu->lock);
The spinlock is dropped immediately after reading the address pointer. If this is guaranteed to be safe, please add a comment to explain why a UAF or race is avoided here. Otherwise, the locking scope needs to be extened to protect both the pointer lookup and use.
Quoted 7 lines
drivers/iommu/intel/liveupdate.c new file +7
-0+if (context &&is_context_table_preserved (iommu, ser, bus, devfn)) { +iommu_unpreserve_pages (context); +clear_bit (CONTEXT_TABLE_PRESERVED_BIT (bus, devfn), + (unsigned long *)&ser->intel.context_tables_bitmap[0 ]); + } +} +
drivers/iommu/intel/liveupdate.c line 54
Quoted 10 lines
drivers/iommu/intel/liveupdate.c new file +10
-0+static int preserve_context_table (struct intel_iommu *iommu, +struct iommu_hw_ser *ser, +u8 bus,u8 devfn) +{ +struct context_entry *context; +int ret; + +spin_lock (&iommu->lock); + context =iommu_context_addr (iommu, bus, devfn,0 ); +spin_unlock (&iommu->lock);
Ditto.
Quoted 141 lines
drivers/iommu/intel/liveupdate.c new file +121
-0+if (context && !is_context_table_preserved (iommu, ser, bus, devfn)) { + ret =iommu_preserve_pages (context); +if (ret) +return ret; + +set_bit (CONTEXT_TABLE_PRESERVED_BIT (bus, devfn), + (unsigned long *)&ser->intel.context_tables_bitmap[0 ]); + } + +return 0 ; +} + +static void unpreserve_iommu_context_tables (struct intel_iommu *iommu, +struct iommu_hw_ser *ser) +{ +int i; + +for (i =0 ; i <ROOT_ENTRY_NR ; i++) { +unpreserve_context_table (iommu, ser, i,0 ); + +if (!sm_supported (iommu)) +continue ; + +unpreserve_context_table (iommu, ser, i,0x80 ); + } +} + +static int preserve_iommu_context_tables (struct device_domain_info *info) +{ +struct iommu_hw_ser *iommu_ser; +struct intel_iommu *iommu; +int ret; +int i; + +/* IOMMU for this device should already preserved.*/ + iommu = info->iommu; + iommu_ser =iommu_preserved_state (&iommu->iommu); +if (!iommu_ser) +return -EINVAL ; + +/* +* We could do preservation of context tables only for the bus of this +* device, but these devices can have PCI aliases, so context tables for +* those will also require preservation. Also unpreserve would require +* some kind of refcounting where the context table will only be +* unpreserved when the last device associated with it is unpreserved. +* +* This introduces unnecessary complication with minimum benefits as the +* unpreserved context tables will probably be recreated by the next +* kernel as these are all active devices. We follow simpler approach by +* just preserving the currently active context tables. +*/ +for (i =0 ; i <ROOT_ENTRY_NR ; i++) { + ret =preserve_context_table (iommu, iommu_ser, i,0 ); +if (ret) +return ret; + +if (!sm_supported (iommu)) +continue ; + + ret =preserve_context_table (iommu, iommu_ser, i,0x80 ); +if (ret) +return ret; + } + +return 0 ; +} + +int intel_iommu_preserve_device (struct device *dev, +struct iommu_device_ser *device_ser) +{ +struct device_domain_info *info =dev_iommu_priv_get (dev); +int ret; + +if (!dev_is_pci (dev)) { +dev_err (dev,"Cannot preserve non-PCI device\n" ); +return -EOPNOTSUPP ; + } + +if (!info || !info->domain) +return -EINVAL ; + + ret =preserve_iommu_context_tables (info); +if (ret) +return ret; + + device_ser->domain_iommu_ser.attachment_id =domain_id_iommu (info->domain, + info->iommu); +return 0 ; +} + +int intel_iommu_preserve (struct iommu_device *iommu_dev, +struct iommu_hw_ser *ser) +{ +struct intel_iommu *iommu; +int ret; + + iommu = container_of(iommu_dev, struct intel_iommu, iommu); + + ret =iommu_preserve_pages (iommu->root_entry); +if (ret) +return ret; + + ser->intel.phys_addr = iommu->reg_phys; + ser->intel.root_table =__pa (iommu->root_entry); + ser->type =IOMMU_INTEL ; + ser->token = ser->intel.phys_addr; + +return 0 ; +} + +void intel_iommu_unpreserve (struct iommu_device *iommu_dev, +struct iommu_hw_ser *ser) +{ +struct intel_iommu *iommu; + + iommu = container_of(iommu_dev, struct intel_iommu, iommu); + +unpreserve_iommu_context_tables (iommu, ser); +iommu_unpreserve_pages (iommu->root_entry); +}include/linux/kho/abi/iommu.h +5
-0@@ -80,6 +80,7 @@*/ enum iommu_type_ser {IOMMU_INVALID , +IOMMU_INTEL , };#define IOMMU_SER_FLAG_DELETED (1 << 0)@@ -140,16 +141,36 @@ struct iommu_device_ser {struct iommu_dev_map_ser domain_iommu_ser; } __packed; +/** +* struct iommu_intel_ser - Serialized state of an Intel IOMMU instance +* @restored: Whether IOMMU state is restored +* @phys_addr: Physical address of the IOMMU register base
include/linux/kho/abi/iommu.h struct iommu_device_ser { · line 157
Quoted 10 lines
include/linux/kho/abi/iommu.h +10
-0+* @root_table: Physical address of the root entry table +* @context_tables_bitmap: Bitmap representing the context tables that are +* preserved. +*/ +struct iommu_intel_ser { +u8 restored; +u8 padding[7 ]; +u64 phys_addr; +u64 root_table; +u64 context_tables_bitmap[8 ];/* Tracks upto 512 context tables */
To avoid open-coded magic numbers, how about something like,
#define VTD_PRESERVED_BITMAP_LONGS DIV_ROUND_UP(512, BITS_PER_LONG_LONG) u64 context_tables_bitmap[VTD_PRESERVED_BITMAP_LONGS];
?
Quoted 9 lines
include/linux/kho/abi/iommu.h +3
-0+}; +/** * struct iommu_hw_ser - Serialized state of an IOMMU instance * @hdr: Common object header * @token: Unique token for the IOMMU * @type: IOMMU type serialized state belongs to +* @intel: Intel specific serialization data */
include/linux/kho/abi/iommu.h struct iommu_device_ser { · line 176
Quoted 10 lines
include/linux/kho/abi/iommu.h +3
-0struct iommu_hw_ser {struct iommu_hdr_ser hdr;u64 token;u64 type; +union { +struct iommu_intel_ser intel; + }; } __packed;/**
Thanks,
baolu
Re: [PATCH v3 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops
Quoted 16 lines · Baolu Lu
Quoted 15 lines · Samiullah Khawaja
Add implementation of the device and iommu presevation in a separate file. Also set the device and iommu preserve/unpreserve ops in the struct
iommu_ops.Signed-off-by Samiullah Khawaja <[email protected]>--- MAINTAINERS | 8 ++ drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 8 +- drivers/iommu/intel/iommu.h | 28 +++++ drivers/iommu/intel/liveupdate.c | 175 +++++++++++++++++++++++++++++++ include/linux/kho/abi/iommu.h | 21 ++++ 6 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 drivers/iommu/intel/liveupdate.c
[snip]
Quoted 7 lines
Quoted 7 lines
drivers/iommu/intel/iommu.c +0
-1-/* * Take a root_entry and return the Lower Context Table Pointer (LCTP) * if marked present. @@ -3926,6 +3925,11 @@ const struct iommu_ops intel_iommu_ops = { .is_attach_deferred = intel_iommu_is_attach_deferred, .def_domain_type = device_def_domain_type,
Quoted 8 lines
drivers/iommu/intel/iommu.c const struct iommu_ops intel_iommu_ops = { · line 3932
Quoted 6 lines
drivers/iommu/intel/iommu.c +5
-0.page_response = intel_iommu_page_response, +#ifdef CONFIG_IOMMU_LIVEUPDATE + .preserve_device = intel_iommu_preserve_device, + .preserve = intel_iommu_preserve, + .unpreserve = intel_iommu_unpreserve, +#endif Any reason why an
unpreserve_devicecallback is missing here?
I added unpreserve_device in a later patch when PASID support is added, as the context tables are currently unpreserved globally during unpreserve(iommu).
But I agree this looks incomplete and I will add a stub unpreserve_device in this patch.
Quoted 3 lines
Quoted 3 lines
drivers/iommu/intel/iommu.c +0
-0};static void quirk_iommu_igfx (struct pci_dev *dev)
[snip]
Quoted 7 lines
Quoted 7 lines
drivers/iommu/intel/liveupdate.c new file +7
-0+ +static void unpreserve_context_table (struct intel_iommu *iommu, +struct iommu_hw_ser *ser, +u8 bus,u8 devfn) +{ +struct context_entry *context; +
Quoted 8 lines
drivers/iommu/intel/liveupdate.c line 37
Quoted 3 lines
drivers/iommu/intel/liveupdate.c new file +3
-0+spin_lock (&iommu->lock); + context =iommu_context_addr (iommu, bus, devfn,0 ); +spin_unlock (&iommu->lock);The spinlock is dropped immediately after reading the address pointer. If this is guaranteed to be safe, please add a comment to explain why a UAF or race is avoided here. Otherwise, the locking scope needs to be extened to protect both the pointer lookup and use.
In the Intel VT-d driver, context tables are never freed once they are allocated during runtime, as they can be shared across multiple devices. So I took the lock here to protect against concurrent allocations inside iommu_context_addr(). Once the address is read, it is safe to use without holding the lock until the DMAR unit itself is torn down.
I will add a comment explaining this here.
Quoted 12 lines
Quoted 12 lines
drivers/iommu/intel/liveupdate.c new file +11
-0+if (context &&is_context_table_preserved (iommu, ser, bus, devfn)) { +iommu_unpreserve_pages (context); +clear_bit (CONTEXT_TABLE_PRESERVED_BIT (bus, devfn), + (unsigned long *)&ser->intel.context_tables_bitmap[0 ]); + } +} + +static int preserve_context_table (struct intel_iommu *iommu, +struct iommu_hw_ser *ser, +u8 bus,u8 devfn) +{
Quoted 8 lines
drivers/iommu/intel/liveupdate.c line 54
Quoted 6 lines
drivers/iommu/intel/liveupdate.c new file +6
-0+struct context_entry *context; +int ret; + +spin_lock (&iommu->lock); + context =iommu_context_addr (iommu, bus, devfn,0 ); +spin_unlock (&iommu->lock);Ditto.
Answered above.
Quoted 13 lines
Quoted 13 lines
drivers/iommu/intel/liveupdate.c new file +12
-0+if (context && !is_context_table_preserved (iommu, ser, bus, devfn)) { + ret =iommu_preserve_pages (context); +if (ret) +return ret; + +set_bit (CONTEXT_TABLE_PRESERVED_BIT (bus, devfn), + (unsigned long *)&ser->intel.context_tables_bitmap[0 ]); + } + +return 0 ; +} +
[snip]
Quoted 31 lines
Quoted 18 lines
include/linux/kho/abi/iommu.h +5
-0@@ -80,6 +80,7 @@*/ enum iommu_type_ser {IOMMU_INVALID , +IOMMU_INTEL , };#define IOMMU_SER_FLAG_DELETED (1 << 0)@@ -140,16 +141,36 @@ struct iommu_device_ser {struct iommu_dev_map_ser domain_iommu_ser; } __packed; +/** +* struct iommu_intel_ser - Serialized state of an Intel IOMMU instance +* @restored: Whether IOMMU state is restored +* @phys_addr: Physical address of the IOMMU register base include/linux/kho/abi/iommu.h struct iommu_device_ser { · line 156
Quoted 10 lines
include/linux/kho/abi/iommu.h +10
-0+* @root_table: Physical address of the root entry table +* @context_tables_bitmap: Bitmap representing the context tables that are +* preserved. +*/ +struct iommu_intel_ser { +u8 restored; +u8 padding[7 ]; +u64 phys_addr; +u64 root_table; +u64 context_tables_bitmap[8 ];/* Tracks upto 512 context tables */ To avoid open-coded magic numbers, how about something like,
Quoted 4 lines
#define
VTD_PRESERVED_BITMAP_LONGSDIV_ROUND_UP(512, BITS_PER_LONG_LONG)u64 context_tables_bitmap[VTD_PRESERVED_BITMAP_LONGS];?
This looks great to me. I will update this here.
Quoted 14 lines
Quoted 14 lines
include/linux/kho/abi/iommu.h +3
-0+}; +/** * struct iommu_hw_ser - Serialized state of an IOMMU instance * @hdr: Common object header * @token: Unique token for the IOMMU * @type: IOMMU type serialized state belongs to +* @intel: Intel specific serialization data */ struct iommu_hw_ser {struct iommu_hdr_ser hdr;u64 token;u64 type;
Quoted 9 lines
include/linux/kho/abi/iommu.h struct iommu_device_ser { · line 176
Quoted 5 lines
include/linux/kho/abi/iommu.h +3
-0+union { +struct iommu_intel_ser intel; + }; } __packed;/** Thanks,
baolu
Thanks,
Sami
Baolu Lu
maintainerRe: [PATCH v3 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops
Quoted 17 lines · Samiullah Khawaja
Quoted 16 lines
drivers/iommu/intel/liveupdate.c line 37
Quoted 11 lines
drivers/iommu/intel/liveupdate.c new file +10
-0+ +static void unpreserve_context_table (struct intel_iommu *iommu, + struct iommu_hw_ser *ser, + u8 bus, u8 devfn) +{ + struct context_entry *context; + + spin_lock(&iommu->lock); + context = iommu_context_addr(iommu, bus, devfn, 0); + spin_unlock(&iommu->lock);The spinlock is dropped immediately after reading the address pointer. If this is guaranteed to be safe, please add a comment to explain why a UAF or race is avoided here. Otherwise, the locking scope needs to be extened to protect both the pointer lookup and use.
Quoted 7 lines
In the Intel VT-d driver, context tables are never freed once they are allocated during runtime, as they can be shared across multiple devices. So I took the lock here to protect against concurrent allocations inside
iommu_context_addr(). Once the address is read, it is safe to use without holding the lock until the DMAR unit itself is torn down.I will add a comment explaining this here.
As part of this series, I think this is acceptable. However, there is room for further improvement in iommu_context_addr(). We could avoid concurrent allocations by replacing the code below with try_cmpxchg64():
phy_addr = virt_to_phys((void *)context);
*entry = phy_addr | 1;At the same time, we could also replace the spinlock with an rwsem to eliminate the spinlock critical section. Consequently, the GFP_ATOMIC allocation in iommu_alloc_pages_node_sz() could then be replaced with a normal GFP_KERNEL.
Thanks,
baolu
Re: [PATCH v3 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops
Quoted 24 lines · Baolu Lu
Quoted 23 lines · Samiullah Khawaja
Quoted 16 lines
drivers/iommu/intel/liveupdate.c line 37
Quoted 11 lines
drivers/iommu/intel/liveupdate.c new file +10
-0+ +static void unpreserve_context_table (struct intel_iommu *iommu, + struct iommu_hw_ser *ser, + u8 bus, u8 devfn) +{ + struct context_entry *context; + + spin_lock(&iommu->lock); + context = iommu_context_addr(iommu, bus, devfn, 0); + spin_unlock(&iommu->lock);The spinlock is dropped immediately after reading the address pointer. If this is guaranteed to be safe, please add a comment to explain why a UAF or race is avoided here. Otherwise, the locking scope needs to be extened to protect both the pointer lookup and use.
In the Intel VT-d driver, context tables are never freed once they are allocated during runtime, as they can be shared across multiple devices. So I took the lock here to protect against concurrent allocations inside
iommu_context_addr(). Once the address is read, it is safe to use without holding the lock until the DMAR unit itself is torn down.
Quoted 8 lines
Quoted 1 line
I will add a comment explaining this here.
As part of this series, I think this is acceptable. However, there is room for further improvement in
iommu_context_addr(). We could avoid concurrent allocations by replacing the code below withtry_cmpxchg64():phy_addr = virt_to_phys((void *)context); *entry = phy_addr | 1;
This sounds great.
Quoted 5 lines
At the same time, we could also replace the spinlock with an rwsem to eliminate the spinlock critical section. Consequently, the
GFP_ATOMICallocation iniommu_alloc_pages_node_sz()could then be replaced with a normalGFP_KERNEL.
+1 to this also.
Quoted 3 lines
Thanks,
baolu
Thanks,
Sami
PATCH v3 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.
--- drivers/iommu/intel/iommu.c | 9 ++-- drivers/iommu/intel/iommu.h | 6 +++ drivers/iommu/intel/liveupdate.c | 74 ++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-)
drivers/iommu/intel/iommu.c +6 -3
@@ -2374,8 +2374,11 @@ void intel_iommu_shutdown(void)/* Disable PMRs explicitly here. */ iommu_disable_protect_mem_regions (iommu); -/* Make sure the IOMMUs are switched off */ -iommu_disable_translation (iommu); +/* Make sure the IOMMUs are switched off if not preserved. */ +if (iommu_preserved_state (&iommu->iommu)) +clear_unpreserved_context_entries (iommu); +else +iommu_disable_translation (iommu); } }
@@ -2699,7 +2702,7 @@ static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *op* devices, unbinding the driver from any one of them will possibly leave * the others unable to operate. */ -static void domain_context_clear (struct device_domain_info *info) +void domain_context_clear (struct device_domain_info *info) {if (!dev_is_pci (info->dev)) {domain_context_clear_one (info, info->bus, info->devfn);
drivers/iommu/intel/iommu.h +6 -0
@@ -1241,6 +1241,7 @@ void cache_tag_flush_all(struct dmar_domain *domain);void cache_tag_flush_range_np (struct dmar_domain *domain,unsigned long start,unsigned long end); +void domain_context_clear (struct device_domain_info *info);void intel_context_flush_no_pasid (struct device_domain_info *info,struct context_entry *context,u16 did);
@@ -1293,6 +1294,7 @@ int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_hw_ser *iommu_ser);void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_hw_ser *iommu_ser); +void clear_unpreserved_context_entries (struct intel_iommu *iommu);#else static inline int intel_iommu_preserve_device (struct device *dev,struct iommu_device_ser *device_ser)
@@ -1310,6 +1312,10 @@ static inline void intel_iommu_unpreserve(struct iommu_device *iommu, struct iommu_hw_ser *iommu_ser) { } + +static inline void clear_unpreserved_context_entries (struct intel_iommu *iommu) +{ +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM
drivers/iommu/intel/liveupdate.c +74 -0
80 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Baolu Lu
maintainerRe: [PATCH v3 08/18] iommu/vt-d: clear unpreserved context entries during shutdown
Quoted 11 lines · Samiullah Khawaja
During normal shutdown the iommu translation is disabled. Since the root table is preserved during live update, it needs to be cleaned up and the context entries of the unpreserved devices and root entries for the unpreserved context tables need to be cleared.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 9 ++-- drivers/iommu/intel/iommu.h | 6 +++ drivers/iommu/intel/liveupdate.c | 74 ++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-)
Please tweak the commit title to: "iommu/vt-d: Clear unpreserved..." (capitalize "Clear") to match the driver's commit history style.
A high-level question: have you looked at how the suspend/resume path behaves with the iommu and device preservation? DMA translation is disabled and re-enabled there. I don't see any immediate changes are needed there, but it would be good to call it out explicitly if it was overlooked.
Quoted 6 lines
drivers/iommu/intel/iommu.c +0
-0
drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2379
Quoted 8 lines
drivers/iommu/intel/iommu.c +3
-2@@ -2374,8 +2374,11 @@ void intel_iommu_shutdown(void)/* Disable PMRs explicitly here. */ iommu_disable_protect_mem_regions (iommu); -/* Make sure the IOMMUs are switched off */ -iommu_disable_translation (iommu); +/* Make sure the IOMMUs are switched off if not preserved. */ +if (iommu_preserved_state (&iommu->iommu)) +clear_unpreserved_context_entries (iommu);
How are PCI devices handled during a live update kexec? Do they go through the standard iommu_release_device() path?
I assume they do not, because if they did, the context entries for preserved devices could be updated after preservation. If they do bypass the release_device path, why not just explicitly invoke iommu_release_device() for all devices that are not preserved?
Using iommu_release_device() for the unpreserved devices would naturally erase their context entries and securely park those devices in a DMA blocking state.
drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2384
Quoted 5 lines
drivers/iommu/intel/iommu.c +2
-0+else +iommu_disable_translation (iommu); } }
[...]
Thanks,
baolu
Re: [PATCH v3 08/18] iommu/vt-d: clear unpreserved context entries during shutdown
Quoted 13 lines · Baolu Lu
Quoted 11 lines · Samiullah Khawaja
During normal shutdown the iommu translation is disabled. Since the root table is preserved during live update, it needs to be cleaned up and the context entries of the unpreserved devices and root entries for the unpreserved context tables need to be cleared.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 9 ++-- drivers/iommu/intel/iommu.h | 6 +++ drivers/iommu/intel/liveupdate.c | 74 ++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-)
Quoted 2 lines
Please tweak the commit title to: "iommu/vt-d: Clear unpreserved..." (capitalize "Clear") to match the driver's commit history style.
Agreed. Will do.
Quoted 6 lines
A high-level question: have you looked at how the suspend/resume path behaves with the iommu and device preservation? DMA translation is disabled and re-enabled there. I don't see any immediate changes are needed there, but it would be good to call it out explicitly if it was overlooked.
I looked into this during early implementation. The preserved state data structures are not affected by the suspension of the IOMMU, as during resume it reuses those exact same data structures in RAM. But I have not tested this since it is unlikely that in the environments where liveupdate is used, the suspend/resume functionality is also used.
But I plan to simply return -EBUSY from iommu_suspend() if the IOMMU is currently preserved. This explicitly prevents the two states from overlapping and avoids any complex edge cases. I'll add that check in v4.
Quoted 18 lines
Quoted 7 lines
drivers/iommu/intel/iommu.c +0
-0drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2378
Quoted 7 lines
drivers/iommu/intel/iommu.c +3
-2@@ -2374,8 +2374,11 @@ void intel_iommu_shutdown(void)/* Disable PMRs explicitly here. */ iommu_disable_protect_mem_regions (iommu); -/* Make sure the IOMMUs are switched off */ -iommu_disable_translation (iommu); +/* Make sure the IOMMUs are switched off if not preserved. */ +if (iommu_preserved_state (&iommu->iommu)) +clear_unpreserved_context_entries (iommu);How are PCI devices handled during a live update kexec? Do they go through the standard
iommu_release_device()path?
Quoted 8 lines
I assume they do not, because if they did, the context entries for preserved devices could be updated after preservation. If they do bypass the
release_devicepath, why not just explicitly invokeiommu_release_device()for all devices that are not preserved?Using
iommu_release_device()for the unpreserved devices would naturally erase their context entries and securely park those devices in a DMA blocking state.
I considered reusing iommu_release_device(), but it leaves the hardware in a state that is unsafe to carry across a kexec boundary when global translation remains enabled.
- In legacy mode, iommu_release_device() does not clear the context entries. It assumes device_block_translation() was called during a prior domain detach to clear context entries.
- In scalable mode, while it does clear the context entries, it skips the PASID cache invalidations (per VT-d spec 6.5.3.3 Table 25, second row). It assumes intel_pasid_tear_down_entry() was already called during domain detach to handle the invalidations.
During a normal shutdown, this is safe because the IOMMU driver disables translation globally. However, since live update keeps global translation enabled across the kexec, relying on iommu_release_device() would lead to DMAR faults from unpreserved devices.
Manually clearing the entries and issuing global invalidations is the safest way to guarantee the unpreserved devices are securely parked.
Quoted 11 lines
drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2384
Quoted 6 lines
drivers/iommu/intel/iommu.c +2
-0+else +iommu_disable_translation (iommu); } }[...]
Thanks,
baolu
Thanks,
Sami
PATCH v3 9/18 iommu: Add APIs to get iommu and device preserved state
In reply to Samiullah KhawajaThe preserved state of the device and IOMMU needs to be fetched during shutdown and boot in the next kernel. Add APIs that can be used to fetch the preserved state of a device and IOMMU. The APIs will only be used during shutdown and after liveupdate so no locking needed.
--- drivers/iommu/liveupdate.c | 75 ++++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 31 +++++++++++++ 2 files changed, 106 insertions(+)
drivers/iommu/liveupdate.c +75 -0
@@ -18,6 +18,17 @@#define iommu_max_objs_per_page (_array) \ ((PAGE_SIZE - sizeof(struct iommu_array_hdr_ser)) / sizeof((_array)->objects[0])) +#define iommu_liveupdate_for_each_obj (_arr, _obj, _idx) \ + for ((_idx) = 0, (_obj) = (_arr)->objects; \ + (_idx) < (_arr)->hdr.nr_objects; (_idx)++, (_obj)++) \ + if (((_obj)->hdr.flags & IOMMU_SER_FLAG_DELETED)) \ + continue; \ + else + +#define iommu_liveupdate_for_each_arr (_arr) \ + for (; (_arr); (_arr) = (_arr)->hdr.next_array_phys ? \ + phys_to_virt((_arr)->hdr.next_array_phys) : NULL) +struct iommu_flb_obj {struct mutex lock;struct iommu_flb_ser *ser;
@@ -216,6 +227,70 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_flb_obj *flb_obj; +struct iommu_device_array_ser *array; +struct iommu_device_ser *device_ser; +int ret, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret) +return -ENOENT ; + +if (!flb_obj->ser->device_array_phys) +return -ENOENT ; + + array =phys_to_virt (flb_obj->ser->device_array_phys); +iommu_liveupdate_for_each_arr (array) { +iommu_liveupdate_for_each_obj (array, device_ser, idx) { + ret =fn (device_ser, arg); +if (ret) + goto out; + } + } + +out: +liveupdate_flb_put_incoming (&iommu_flb); +return ret; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device); + +struct iommu_hw_ser *iommu_get_preserved_data (u64 token,enum iommu_type_ser type) +{ +struct iommu_hw_ser *iommu_ser =NULL ; +struct iommu_hw_array_ser *array; +struct iommu_flb_obj *flb_obj; +int ret, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret == -ENODATA || ret == -ENOENT ) +return NULL ; + +if (ret) +return ERR_PTR (ret); + +if (!flb_obj->ser->iommu_array_phys) { + iommu_ser =ERR_PTR (-EINVAL ); + goto out; + } + + array =phys_to_virt (flb_obj->ser->iommu_array_phys); +iommu_liveupdate_for_each_arr (array) { +iommu_liveupdate_for_each_obj (array, iommu_ser, idx) { +if (iommu_ser->token == token && iommu_ser->type == type) + goto out; + } + } + + iommu_ser =NULL ; +out: +liveupdate_flb_put_incoming (&iommu_flb); +return iommu_ser; +} +EXPORT_SYMBOL (iommu_get_preserved_data); +static int alloc_object_ser (void **curr_array_ptr,u64 max_objs) {struct iommu_array_hdr_ser *curr_array = *curr_array_ptr;
include/linux/iommu-liveupdate.h +31 -0
@@ -13,6 +13,8 @@#include <linux/liveupdate.h> #include <linux/kho/abi/iommu.h> +typedef int (*iommu_preserved_device_iter_fn )(struct iommu_device_ser *ser, +void *arg);#ifdef CONFIG_IOMMU_LIVEUPDATE static inline void *dev_iommu_preserved_state (struct device *dev) {
@@ -28,6 +30,20 @@ static inline void *dev_iommu_preserved_state(struct device *dev)return NULL ; } +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +struct iommu_domain_ser *ser; + + ser = domain->preserved_state; +if (ser && (ser->hdr.flags &IOMMU_SER_FLAG_INCOMING )) +return ser; + +return NULL ; +} + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg); +struct iommu_hw_ser *iommu_get_preserved_data (u64 token,enum iommu_type_ser type);int iommu_preserve_domain (struct iommu_domain *domain,struct iommu_domain_ser **ser);void iommu_unpreserve_domain (struct iommu_domain *domain); int iommu_preserve_device(struct iommu_domain *domain,
@@ -44,6 +60,21 @@ static inline void *dev_iommu_preserved_state(struct device *dev)return NULL ; } +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +return NULL ; +} + +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) +{ +return -EOPNOTSUPP ; +} + +static inline struct iommu_hw_ser *iommu_get_preserved_data (u64 token,enum iommu_type_ser type) +{ +return NULL ; +} +static inline int iommu_preserve_domain (struct iommu_domain *domain,struct iommu_domain_ser **ser) {return -EOPNOTSUPP ;
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Baolu Lu
maintainerRe: [PATCH v3 09/18] iommu: Add APIs to get iommu and device preserved state
Quoted 71 lines · Samiullah Khawaja
The preserved state of the device and IOMMU needs to be fetched during shutdown and boot in the next kernel. Add APIs that can be used to fetch the preserved state of a device and IOMMU. The APIs will only be used during shutdown and after liveupdate so no locking needed.
Reviewed-by Pranjal Shrivastava <[email protected]>Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/liveupdate.c | 75 ++++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 31 +++++++++++++ 2 files changed, 106 insertions(+)drivers/iommu/liveupdate.c +44
-0@@ -18,6 +18,17 @@#define iommu_max_objs_per_page (_array) \ ((PAGE_SIZE - sizeof(struct iommu_array_hdr_ser)) / sizeof((_array)->objects[0])) +#define iommu_liveupdate_for_each_obj (_arr, _obj, _idx) \ + for ((_idx) = 0, (_obj) = (_arr)->objects; \ + (_idx) < (_arr)->hdr.nr_objects; (_idx)++, (_obj)++) \ + if (((_obj)->hdr.flags & IOMMU_SER_FLAG_DELETED)) \ + continue; \ + else + +#define iommu_liveupdate_for_each_arr (_arr) \ + for (; (_arr); (_arr) = (_arr)->hdr.next_array_phys ? \ + phys_to_virt((_arr)->hdr.next_array_phys) : NULL) +struct iommu_flb_obj {struct mutex lock;struct iommu_flb_ser *ser;@@ -216,6 +227,70 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_flb_obj *flb_obj; +struct iommu_device_array_ser *array; +struct iommu_device_ser *device_ser; +int ret, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret) +return -ENOENT ; + +if (!flb_obj->ser->device_array_phys) +return -ENOENT ; + + array =phys_to_virt (flb_obj->ser->device_array_phys); +iommu_liveupdate_for_each_arr (array) { +iommu_liveupdate_for_each_obj (array, device_ser, idx) { + ret =fn (device_ser, arg); +if (ret) + goto out; + } + } + +out: +liveupdate_flb_put_incoming (&iommu_flb); +return ret; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device); + +struct iommu_hw_ser *iommu_get_preserved_data (u64 token,enum iommu_type_ser type) +{ +struct iommu_hw_ser *iommu_ser =NULL ;
drivers/iommu/liveupdate.c void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 272
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+struct iommu_hw_array_ser *array; +struct iommu_flb_obj *flb_obj; +int ret, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret == -ENODATA || ret == -ENOENT ) +return NULL ; + +if (ret) +return ERR_PTR (ret);
Why does this helper return both NULL and an ERR_PTR()? Is NULL considered an error case here? Also, why are -ENODATA and -ENOENT treated differently?
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+ +if (!flb_obj->ser->iommu_array_phys) { + iommu_ser =ERR_PTR (-EINVAL ); + goto out; + } + + array =phys_to_virt (flb_obj->ser->iommu_array_phys); +iommu_liveupdate_for_each_arr (array) { +iommu_liveupdate_for_each_obj (array, iommu_ser, idx) { +if (iommu_ser->token == token && iommu_ser->type == type)
drivers/iommu/liveupdate.c void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 292
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+ goto out; + } + } + + iommu_ser =NULL ; +out: +liveupdate_flb_put_incoming (&iommu_flb); +return iommu_ser; +} +EXPORT_SYMBOL (iommu_get_preserved_data);
These two helpers are intended for use by IOMMU drivers, correct? If so, please add kernel-doc style comments to document their usage and guide how drivers should use them.
Quoted 50 lines
drivers/iommu/liveupdate.c +1
-0+static int alloc_object_ser (void **curr_array_ptr,u64 max_objs) {struct iommu_array_hdr_ser *curr_array = *curr_array_ptr;include/linux/iommu-liveupdate.h +24
-0@@ -13,6 +13,8 @@#include <linux/liveupdate.h> #include <linux/kho/abi/iommu.h> +typedef int (*iommu_preserved_device_iter_fn )(struct iommu_device_ser *ser, +void *arg);#ifdef CONFIG_IOMMU_LIVEUPDATE static inline void *dev_iommu_preserved_state (struct device *dev) {@@ -28,6 +30,20 @@ static inline void *dev_iommu_preserved_state(struct device *dev)return NULL ; } +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +struct iommu_domain_ser *ser; + + ser = domain->preserved_state; +if (ser && (ser->hdr.flags &IOMMU_SER_FLAG_INCOMING )) +return ser; + +return NULL ; +} + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg); +struct iommu_hw_ser *iommu_get_preserved_data (u64 token,enum iommu_type_ser type);int iommu_preserve_domain (struct iommu_domain *domain,struct iommu_domain_ser **ser);void iommu_unpreserve_domain (struct iommu_domain *domain); int iommu_preserve_device(struct iommu_domain *domain,@@ -44,6 +60,21 @@ static inline void *dev_iommu_preserved_state(struct device *dev)return NULL ; } +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +return NULL ; +} + +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) +{ +return -EOPNOTSUPP ;
include/linux/iommu-liveupdate.h static inline void *dev_iommu_preserved_state(struct device *dev) · line 80
Quoted 10 lines
include/linux/iommu-liveupdate.h +7
-0+} + +static inline struct iommu_hw_ser *iommu_get_preserved_data (u64 token,enum iommu_type_ser type) +{ +return NULL ; +} +static inline int iommu_preserve_domain (struct iommu_domain *domain,struct iommu_domain_ser **ser) {return -EOPNOTSUPP ;
Thanks,
baolu
Re: [PATCH v3 09/18] iommu: Add APIs to get iommu and device preserved state
Quoted 76 lines · Baolu Lu
Quoted 75 lines · Samiullah Khawaja
The preserved state of the device and IOMMU needs to be fetched during shutdown and boot in the next kernel. Add APIs that can be used to fetch the preserved state of a device and IOMMU. The APIs will only be used during shutdown and after liveupdate so no locking needed.
Reviewed-by Pranjal Shrivastava <[email protected]>Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/liveupdate.c | 75 ++++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 31 +++++++++++++ 2 files changed, 106 insertions(+)drivers/iommu/liveupdate.c +50
-0@@ -18,6 +18,17 @@#define iommu_max_objs_per_page (_array) \ ((PAGE_SIZE - sizeof(struct iommu_array_hdr_ser)) / sizeof((_array)->objects[0])) +#define iommu_liveupdate_for_each_obj (_arr, _obj, _idx) \ + for ((_idx) = 0, (_obj) = (_arr)->objects; \ + (_idx) < (_arr)->hdr.nr_objects; (_idx)++, (_obj)++) \ + if (((_obj)->hdr.flags & IOMMU_SER_FLAG_DELETED)) \ + continue; \ + else + +#define iommu_liveupdate_for_each_arr (_arr) \ + for (; (_arr); (_arr) = (_arr)->hdr.next_array_phys ? \ + phys_to_virt((_arr)->hdr.next_array_phys) : NULL) +struct iommu_flb_obj {struct mutex lock;struct iommu_flb_ser *ser;@@ -216,6 +227,70 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_flb_obj *flb_obj; +struct iommu_device_array_ser *array; +struct iommu_device_ser *device_ser; +int ret, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret) +return -ENOENT ; + +if (!flb_obj->ser->device_array_phys) +return -ENOENT ; + + array =phys_to_virt (flb_obj->ser->device_array_phys); +iommu_liveupdate_for_each_arr (array) { +iommu_liveupdate_for_each_obj (array, device_ser, idx) { + ret =fn (device_ser, arg); +if (ret) + goto out; + } + } + +out: +liveupdate_flb_put_incoming (&iommu_flb); +return ret; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device); + +struct iommu_hw_ser *iommu_get_preserved_data (u64 token,enum iommu_type_ser type) +{ +struct iommu_hw_ser *iommu_ser =NULL ; +struct iommu_hw_array_ser *array; +struct iommu_flb_obj *flb_obj; +int ret, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret == -ENODATA || ret == -ENOENT )
Quoted 8 lines
drivers/iommu/liveupdate.c void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 272
Quoted 4 lines
drivers/iommu/liveupdate.c +4
-0+return NULL ; + +if (ret) +return ERR_PTR (ret);Why does this helper return both NULL and an
ERR_PTR()? Is NULL considered an error case here? Also, why are-ENODATAand-ENOENTtreated differently?
NULL indicates that no preserved state exists for this iommu HW, so setup normally as cold boot. ERR_PTR() indicates a fatal parsing or corruption error in the KHO tree that the driver needs to explicitly handle or abort on.
liveupdate_flb_get_incoming() returns two different errors for two different missing-data scenarios:
-ENODATA: The system did not perform a live update.-ENOENT: The system did perform a live update, but no data was preserved for this specific FLB.
From the IOMMU driver's perspective, both scenarios simply mean "proceed with a normal cold boot," which is why we intercept them and return NULL.
But as you suggested later, I will add kdocs for these to document all these things.
Quoted 17 lines
Quoted 17 lines
drivers/iommu/liveupdate.c +16
-0+ +if (!flb_obj->ser->iommu_array_phys) { + iommu_ser =ERR_PTR (-EINVAL ); + goto out; + } + + array =phys_to_virt (flb_obj->ser->iommu_array_phys); +iommu_liveupdate_for_each_arr (array) { +iommu_liveupdate_for_each_obj (array, iommu_ser, idx) { +if (iommu_ser->token == token && iommu_ser->type == type) + goto out; + } + } + + iommu_ser =NULL ; +out:
Quoted 8 lines
drivers/iommu/liveupdate.c void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 292
Quoted 4 lines
drivers/iommu/liveupdate.c +4
-0+liveupdate_flb_put_incoming (&iommu_flb); +return iommu_ser; +} +EXPORT_SYMBOL (iommu_get_preserved_data);These two helpers are intended for use by IOMMU drivers, correct? If so, please add kernel-doc style comments to document their usage and guide how drivers should use them.
Agreed. Will do.
Quoted 53 lines
Quoted 53 lines
drivers/iommu/liveupdate.c +1
-0+static int alloc_object_ser (void **curr_array_ptr,u64 max_objs) {struct iommu_array_hdr_ser *curr_array = *curr_array_ptr;include/linux/iommu-liveupdate.h +29
-0@@ -13,6 +13,8 @@#include <linux/liveupdate.h> #include <linux/kho/abi/iommu.h> +typedef int (*iommu_preserved_device_iter_fn )(struct iommu_device_ser *ser, +void *arg);#ifdef CONFIG_IOMMU_LIVEUPDATE static inline void *dev_iommu_preserved_state (struct device *dev) {@@ -28,6 +30,20 @@ static inline void *dev_iommu_preserved_state(struct device *dev)return NULL ; } +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +struct iommu_domain_ser *ser; + + ser = domain->preserved_state; +if (ser && (ser->hdr.flags &IOMMU_SER_FLAG_INCOMING )) +return ser; + +return NULL ; +} + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg); +struct iommu_hw_ser *iommu_get_preserved_data (u64 token,enum iommu_type_ser type);int iommu_preserve_domain (struct iommu_domain *domain,struct iommu_domain_ser **ser);void iommu_unpreserve_domain (struct iommu_domain *domain); int iommu_preserve_device(struct iommu_domain *domain,@@ -44,6 +60,21 @@ static inline void *dev_iommu_preserved_state(struct device *dev)return NULL ; } +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +return NULL ; +} + +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) +{ +return -EOPNOTSUPP ; +} + +static inline struct iommu_hw_ser *iommu_get_preserved_data (u64 token,enum iommu_type_ser type) +{ +return NULL ;
Quoted 9 lines
include/linux/iommu-liveupdate.h static inline void *dev_iommu_preserved_state(struct device *dev) · line 80
Quoted 5 lines
include/linux/iommu-liveupdate.h +2
-0+} +static inline int iommu_preserve_domain (struct iommu_domain *domain,struct iommu_domain_ser **ser) {return -EOPNOTSUPP ;Thanks,
baolu
Thanks,
Sami
PATCH v3 10/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 | 87 +++++++++++++++++++++++--------- drivers/iommu/intel/iommu.h | 7 +++ drivers/iommu/intel/liveupdate.c | 60 ++++++++++++++++++++++ 3 files changed, 130 insertions(+), 24 deletions(-)
drivers/iommu/intel/iommu.c +63 -24
@@ -990,15 +990,16 @@ static void disable_dmar_iommu(struct intel_iommu *iommu)iommu_disable_translation (iommu); } -static void free_dmar_iommu (struct intel_iommu *iommu) +static void free_dmar_iommu (struct intel_iommu *iommu,struct iommu_hw_ser *iommu_ser) {if (iommu->copied_tables) {bitmap_free (iommu->copied_tables); iommu->copied_tables =NULL ; } -/* free context mapping */ -free_context_table (iommu); +/* free context mapping if there is no serialized state. */ +if (!iommu_ser) +free_context_table (iommu);if (ecap_prs (iommu->ecap))intel_iommu_finish_prq (iommu);
@@ -1609,6 +1610,7 @@ static int copy_translation_tables(struct intel_iommu *iommu)static int __init init_dmars (void ) { +struct iommu_hw_ser *iommu_ser =NULL ;struct dmar_drhd_unit *drhd;struct intel_iommu *iommu;int ret;
@@ -1631,8 +1633,16 @@ static int __init init_dmars(void) intel_pasid_max_id); } + iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +if (iommu_ser &&IS_ERR (iommu_ser)) { + ret =PTR_ERR (iommu_ser); + goto free_iommu; + } +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);
@@ -1641,14 +1651,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" );
@@ -1684,7 +1698,10 @@ static int __init init_dmars(void)*/ for_each_active_iommu (iommu, drhd) {iommu_flush_write_buffer (iommu); -iommu_set_root_entry (iommu); + + iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +if (!iommu_ser) +iommu_set_root_entry (iommu); }check_tylersburg_isoch ();
@@ -1730,8 +1747,11 @@ static int __init init_dmars(void) free_iommu:for_each_active_iommu (iommu, drhd) { -disable_dmar_iommu (iommu); -free_dmar_iommu (iommu); + iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +if (!iommu_ser) +disable_dmar_iommu (iommu); + +free_dmar_iommu (iommu, iommu_ser); }return ret;
@@ -2105,17 +2125,29 @@ int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg)static int intel_iommu_add (struct dmar_drhd_unit *dmaru) {struct intel_iommu *iommu = dmaru->iommu; +struct iommu_hw_ser *iommu_ser =NULL ;int ret; +/* Use IOMMU HW unit MMIO base to identify the preserved state. */ + iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +if (iommu_ser &&IS_ERR (iommu_ser)) { + ret =PTR_ERR (iommu_ser); + goto out; + } +/* * Disable translation if already enabled prior to OS handover. */ -if (iommu->gcmd &DMA_GCMD_TE ) +if (!iommu_ser && iommu->gcmd &DMA_GCMD_TE )iommu_disable_translation (iommu); - ret =iommu_alloc_root_entry (iommu); -if (ret) - goto out; +if (iommu_ser) { +intel_iommu_liveupdate_restore_root_table (iommu, iommu_ser); + }else { + ret =iommu_alloc_root_entry (iommu); +if (ret) + goto out; + }intel_svm_check (iommu);
@@ -2141,16 +2173,19 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru)if (ret) goto disable_iommu; -iommu_set_root_entry (iommu); +if (!iommu_ser) +iommu_set_root_entry (iommu); +iommu_enable_translation (iommu);iommu_disable_protect_mem_regions (iommu);return 0 ; disable_iommu: -disable_dmar_iommu (iommu); +if (!iommu_ser) +disable_dmar_iommu (iommu); out: -free_dmar_iommu (iommu); +free_dmar_iommu (iommu, iommu_ser);return ret; }
@@ -2158,6 +2193,7 @@ int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert) {int ret =0 ;struct intel_iommu *iommu = dmaru->iommu; +struct iommu_hw_ser *iommu_ser;if (!intel_iommu_enabled)return 0 ;
@@ -2167,8 +2203,11 @@ int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)if (insert) { ret =intel_iommu_add (dmaru); }else { -disable_dmar_iommu (iommu); -free_dmar_iommu (iommu); + iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +if (!iommu_ser) +disable_dmar_iommu (iommu); + +free_dmar_iommu (iommu, iommu_ser); }return ret;
drivers/iommu/intel/iommu.h +7 -0
@@ -1295,6 +1295,8 @@ int intel_iommu_preserve(struct iommu_device *iommu,void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_hw_ser *iommu_ser);void clear_unpreserved_context_entries (struct intel_iommu *iommu); +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_hw_ser *iommu_ser);#else static inline int intel_iommu_preserve_device (struct device *dev,struct iommu_device_ser *device_ser)
@@ -1316,6 +1318,11 @@ static inline void intel_iommu_unpreserve(struct iommu_device *iommu,static inline void clear_unpreserved_context_entries (struct intel_iommu *iommu) { } + +static inline void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_hw_ser *iommu_ser) +{ +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM
drivers/iommu/intel/liveupdate.c +60 -0
66 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Baolu Lu
maintainerRe: [PATCH v3 10/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
Quoted 28 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.Signed-off-by Samiullah Khawaja<[email protected]>--- drivers/iommu/intel/iommu.c | 87 +++++++++++++++++++++++--------- drivers/iommu/intel/iommu.h | 7 +++ drivers/iommu/intel/liveupdate.c | 60 ++++++++++++++++++++++ 3 files changed, 130 insertions(+), 24 deletions(-)drivers/iommu/intel/iommu.c +1
-1@@ -990,15 +990,16 @@ static void disable_dmar_iommu(struct intel_iommu *iommu)iommu_disable_translation (iommu); } -static void free_dmar_iommu (struct intel_iommu *iommu) +static void free_dmar_iommu (struct intel_iommu *iommu,struct iommu_hw_ser *iommu_ser) {if (iommu->copied_tables) {bitmap_free (iommu->copied_tables); iommu->copied_tables =NULL ;
drivers/iommu/intel/iommu.c static void disable_dmar_iommu(struct intel_iommu *iommu) · line 1005
Quoted 10 lines
drivers/iommu/intel/iommu.c +3
-2} -/* free context mapping */ -free_context_table (iommu); +/* free context mapping if there is no serialized state. */ +if (!iommu_ser) +free_context_table (iommu);if (ecap_prs (iommu->ecap))intel_iommu_finish_prq (iommu);
disable_dmar_iommu() and free_dmar_iommu() are always paired in the code. How about merging them into a single helper that is aware of iommu preservation? Something like the following?
static void release_dmar_iommu(struct intel_iommu *iommu) {
struct iommu_hw_ser *iommu_ser =
iommu_get_preserved_data(iommu->reg_phys,IOMMU_INTEL);
/*
* 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;
if ((iommu->gcmd & DMA_GCMD_TE) && !iommu_ser)
iommu_disable_translation(iommu);
if (iommu->copied_tables) {
bitmap_free(iommu->copied_tables);
iommu->copied_tables = NULL;
}
/* free context mapping if there is no serialized state. */
if (!iommu_ser)
free_context_table(iommu);
if (ecap_prs(iommu->ecap))
intel_iommu_finish_prq(iommu);
}Thanks,
baolu
Re: [PATCH v3 10/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
Quoted 62 lines · Baolu Lu
Quoted 25 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.Signed-off-by Samiullah Khawaja<[email protected]>--- drivers/iommu/intel/iommu.c | 87 +++++++++++++++++++++++--------- drivers/iommu/intel/iommu.h | 7 +++ drivers/iommu/intel/liveupdate.c | 60 ++++++++++++++++++++++ 3 files changed, 130 insertions(+), 24 deletions(-)drivers/iommu/intel/iommu.c +1
-1@@ -990,15 +990,16 @@ static void disable_dmar_iommu(struct intel_iommu *iommu)iommu_disable_translation (iommu); } -static void free_dmar_iommu (struct intel_iommu *iommu) +static void free_dmar_iommu (struct intel_iommu *iommu,struct iommu_hw_ser *iommu_ser) {if (iommu->copied_tables) {drivers/iommu/intel/iommu.c static void disable_dmar_iommu(struct intel_iommu *iommu) · line 1002
Quoted 10 lines
drivers/iommu/intel/iommu.c +3
-2bitmap_free (iommu->copied_tables); iommu->copied_tables =NULL ; } -/* free context mapping */ -free_context_table (iommu); +/* free context mapping if there is no serialized state. */ +if (!iommu_ser) +free_context_table (iommu);if (ecap_prs (iommu->ecap))intel_iommu_finish_prq (iommu);
disable_dmar_iommu()andfree_dmar_iommu()are always paired in the code. How about merging them into a single helper that is aware of iommu preservation? Something like the following?static void
release_dmar_iommu(struct intel_iommu *iommu){
structiommu_hw_ser*iommu_ser=
iommu_get_preserved_data(iommu->reg_phys,IOMMU_INTEL);/* * 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; if ((iommu->gcmd & DMA_GCMD_TE) && !iommu_ser) iommu_disable_translation(iommu); if (iommu->copied_tables) { bitmap_free(iommu->copied_tables); iommu->copied_tables = NULL; }
Quoted 7 lines
/* free context mapping if there is no serialized state. */
if (!iommu_ser)
free_context_table(iommu);
if (ecap_prs(iommu->ecap))
intel_iommu_finish_prq(iommu);
}This looks great. I will update this in v4.
Quoted 4 lines
Thanks,
baolu
Thanks,
Sami
PATCH v3 11/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 | 65 ++++++++++++++++++++- drivers/iommu/liveupdate.c | 97 ++++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 50 ++++++++++++++++ 3 files changed, 210 insertions(+), 2 deletions(-)
drivers/iommu/iommu.c +63 -2
@@ -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>
@@ -505,6 +506,14 @@ static int iommu_init_device(struct device *dev) goto err_free; } +#ifdef CONFIG_IOMMU_LIVEUPDATE + dev->iommu->device_ser =iommu_get_device_preserved_data (dev); +if (IS_ERR (dev->iommu->device_ser)) { + ret =PTR_ERR (dev->iommu->device_ser); + goto err_module_put; + } +#endif + iommu_dev = ops->probe_device (dev);if (IS_ERR (iommu_dev)) { ret =PTR_ERR (iommu_dev);
@@ -2169,6 +2178,13 @@ static int __iommu_attach_device(struct iommu_domain *domain, ret = domain->ops->attach_dev (domain, dev, old);if (ret)return ret; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +/* The associated state can be unset once restored. */ +if (dev_iommu_restored_state (dev)) +WRITE_ONCE (dev->iommu->device_ser,NULL ); +#endif + dev->iommu->attach_deferred =0 ;trace_attach_device_to_domain (dev);return 0 ;
@@ -3124,6 +3140,47 @@ int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids) }EXPORT_SYMBOL_GPL (iommu_fwspec_add_ids); +static inline void *__iommu_group_restored_state (struct iommu_group *group) +{ +struct device *dev; + + dev =iommu_group_first_dev (group); +if (!dev_is_pci (dev)) +return NULL ; + +return dev_iommu_restored_state (dev); +} + +static struct iommu_domain *__iommu_group_restore_domain (struct iommu_group *group) +{ +struct iommu_device_ser *device_ser; +struct iommu_domain *domain; +struct device *dev; +void *owner; + +lockdep_assert_held (&group->mutex); + dev =iommu_group_first_dev (group); +if (!dev_is_pci (dev)) +return NULL ; + + device_ser =dev_iommu_restored_state (dev); +if (!device_ser) +return NULL ; + + domain =iommu_restore_domain (dev, device_ser, &owner); +if (WARN_ON (IS_ERR (domain))) +return NULL ; + +/* +* Ownership of groups with preserved devices is set during boot. These +* will be reclaimed later by the entity (iommufd) that preserved them. +*/ +WARN_ON (group->owner); + group->owner = owner; + group->owner_cnt =1 ; +return domain; +} +/** * iommu_setup_default_domain - Set the default_domain for the group * @group: Group to change
@@ -3138,8 +3195,8 @@ static int iommu_setup_default_domain(struct iommu_group *group, int target_type) {struct iommu_domain *old_dom = group->default_domain; +struct iommu_domain *dom, *restored_domain;struct group_device *gdev; -struct iommu_domain *dom;bool direct_failed;int req_type;int ret;
@@ -3183,6 +3240,10 @@ 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;if (!group->domain) { +if (__iommu_group_restored_state (group)) + restored_domain =__iommu_group_restore_domain (group); +else + restored_domain = dom;/* * Drivers are not allowed to fail the first domain attach. * The only way to recover from this is to fail attaching the
@@ -3190,7 +3251,7 @@ static int iommu_setup_default_domain(struct iommu_group *group,* in group->default_domain so it is freed after. */ ret =__iommu_group_set_domain_internal ( - group, dom,IOMMU_SET_DOMAIN_MUST_SUCCEED ); + group, restored_domain,IOMMU_SET_DOMAIN_MUST_SUCCEED );if (WARN_ON (ret)) goto out_free_old; }else {
drivers/iommu/liveupdate.c +97 -0
106 lines not on this page · raw message
include/linux/iommu-liveupdate.h +50 -0
80 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
PATCH v3 12/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.
--- drivers/iommu/intel/iommu.c | 46 ++++++++++--- drivers/iommu/intel/iommu.h | 17 +++++ drivers/iommu/intel/liveupdate.c | 111 +++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 11 deletions(-)
drivers/iommu/intel/iommu.c +35 -11
@@ -1311,10 +1311,16 @@ static int dmar_domain_attach_device(struct dmar_domain *domain, {struct device_domain_info *info =dev_iommu_priv_get (dev);struct intel_iommu *iommu = info->iommu; +struct iommu_device_ser *device_ser;unsigned long flags;int ret; - ret =domain_attach_iommu (domain, iommu); + device_ser =dev_iommu_restored_state (dev); +if (!device_ser) + ret =domain_attach_iommu (domain, iommu); +else + ret =intel_iommu_domain_reattach_iommu (domain, + iommu, device_ser);if (ret)return ret;
@@ -1327,16 +1333,20 @@ static int dmar_domain_attach_device(struct dmar_domain *domain,if (dev_is_real_dma_subdevice (dev))return 0 ; -if (!sm_supported (iommu)) - ret =domain_context_mapping (domain, dev); -else if (intel_domain_is_fs_paging (domain)) - ret =domain_setup_first_level (iommu, domain, dev, -IOMMU_NO_PASID ,NULL ); -else if (intel_domain_is_ss_paging (domain)) - ret =domain_setup_second_level (iommu, domain, dev, -IOMMU_NO_PASID ,NULL ); -else if (WARN_ON (true)) - ret = -EINVAL ; +if (!device_ser) { +if (!sm_supported (iommu)) + ret =domain_context_mapping (domain, dev); +else if (intel_domain_is_fs_paging (domain)) + ret =domain_setup_first_level (iommu, domain, dev, +IOMMU_NO_PASID ,NULL ); +else if (intel_domain_is_ss_paging (domain)) + ret =domain_setup_second_level (iommu, domain, dev, +IOMMU_NO_PASID ,NULL ); +else if (WARN_ON (true)) + ret = -EINVAL ; + }else if (!sm_supported (iommu)) { +iommu_enable_pci_ats (info); + }if (ret) goto out_block_translation;
@@ -3145,6 +3155,19 @@ int paging_domain_compatible(struct iommu_domain *domain, struct device *dev)struct intel_iommu *iommu = info->iommu;int ret = -EINVAL ; +#ifdef CONFIG_IOMMU_LIVEUPDATE +/* +* Restored IOMMU domains are already attached to the device and can +* only be freed. So no need to check the compatibility. +*/ +if (iommu_domain_restored_state (domain)) { +if (!dev_iommu_restored_state (dev)) +return -EINVAL ; + +return 0 ; + } +#endif +if (intel_domain_is_fs_paging (dmar_domain)) ret =paging_domain_compatible_first_stage (dmar_domain, iommu);else if (intel_domain_is_ss_paging (dmar_domain))
@@ -3969,6 +3992,7 @@ const struct iommu_ops intel_iommu_ops = { .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
drivers/iommu/intel/iommu.h +17 -0
@@ -1290,6 +1290,8 @@ static inline int iopf_for_domain_replace(struct iommu_domain *new,#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,
@@ -1297,6 +1299,9 @@ void intel_iommu_unpreserve(struct iommu_device *iommu,void clear_unpreserved_context_entries (struct intel_iommu *iommu);void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu,struct iommu_hw_ser *iommu_ser); +int intel_iommu_domain_reattach_iommu (struct dmar_domain *domain, +struct intel_iommu *iommu, +struct iommu_device_ser *device_ser);#else static inline int intel_iommu_preserve_device (struct device *dev,struct iommu_device_ser *device_ser)
@@ -1304,6 +1309,11 @@ static inline int intel_iommu_preserve_device(struct device *dev,return -EOPNOTSUPP ; } +static inline void intel_iommu_unpreserve_device (struct device *dev, +struct iommu_device_ser *device_ser) +{ +} +static inline int intel_iommu_preserve (struct iommu_device *iommu,struct iommu_hw_ser *iommu_ser) {
@@ -1323,6 +1333,13 @@ static inline void intel_iommu_liveupdate_restore_root_table(struct intel_iommu struct iommu_hw_ser *iommu_ser) { } + +static inline int intel_iommu_domain_reattach_iommu (struct dmar_domain *domain, +struct intel_iommu *iommu, +struct iommu_device_ser *device_ser) +{ +return -EOPNOTSUPP ; +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM
drivers/iommu/intel/liveupdate.c +111 -0
117 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Baolu Lu
maintainerRe: [PATCH v3 12/18] iommu/vt-d: Handle reattach of the restored domain
Quoted 51 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.
Signed-off-by Samiullah Khawaja<[email protected]>--- drivers/iommu/intel/iommu.c | 46 ++++++++++--- drivers/iommu/intel/iommu.h | 17 +++++ drivers/iommu/intel/liveupdate.c | 111 +++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 11 deletions(-)drivers/iommu/intel/iommu.c +11
-11@@ -1311,10 +1311,16 @@ static int dmar_domain_attach_device(struct dmar_domain *domain, {struct device_domain_info *info =dev_iommu_priv_get (dev);struct intel_iommu *iommu = info->iommu; +struct iommu_device_ser *device_ser;unsigned long flags;int ret; - ret =domain_attach_iommu (domain, iommu); + device_ser =dev_iommu_restored_state (dev); +if (!device_ser) + ret =domain_attach_iommu (domain, iommu); +else + ret =intel_iommu_domain_reattach_iommu (domain, + iommu, device_ser);if (ret)return ret;@@ -1327,16 +1333,20 @@ static int dmar_domain_attach_device(struct dmar_domain *domain,if (dev_is_real_dma_subdevice (dev))return 0 ; -if (!sm_supported (iommu)) - ret =domain_context_mapping (domain, dev); -else if (intel_domain_is_fs_paging (domain)) - ret =domain_setup_first_level (iommu, domain, dev, -IOMMU_NO_PASID ,NULL ); -else if (intel_domain_is_ss_paging (domain)) - ret =domain_setup_second_level (iommu, domain, dev, -IOMMU_NO_PASID ,NULL ); -else if (WARN_ON (true)) - ret = -EINVAL ; +if (!device_ser) { +if (!sm_supported (iommu)) + ret =domain_context_mapping (domain, dev); +else if (intel_domain_is_fs_paging (domain))
drivers/iommu/intel/iommu.c static int dmar_domain_attach_device(struct dmar_domain *domain, · line 1349
Quoted 10 lines
drivers/iommu/intel/iommu.c +10
-0+ ret =domain_setup_first_level (iommu, domain, dev, +IOMMU_NO_PASID ,NULL ); + else if (intel_domain_is_ss_paging(domain)) + ret =domain_setup_second_level (iommu, domain, dev, +IOMMU_NO_PASID ,NULL ); + else if (WARN_ON(true)) + ret = -EINVAL ; + }else if (!sm_supported (iommu)) { +iommu_enable_pci_ats (info); + }
Instead of merging domain restoration into the attach_dev path, how about adding a new callback to restore a preserved domain for a device? Something like:
include/linux/iommu.h +0 -0
@@ -748,6 +748,8 @@ struct iommu_ops {* * <others> - treated as ENODEV by the caller. Use is discouraged * @set_dev_pasid: set or replace an iommu domain to a pasid of
device. The pasid of
* the device should be left in the old config in
error case.
+ * @restore_dev: Set a domain that is restored from the previous live-updated
+ * kernel to a device.
* @map_pages: map a physically contiguous set of pages of the same
size to
* an iommu domain.
* @unmap_pages: unmap a number of pages of the same size from an
iommu domain
@@ -772,6 +774,9 @@ struct iommu_ops {
struct iommu_domain_ops {
int (*attach_dev)(struct iommu_domain *domain, struct device *dev,
struct iommu_domain *old);
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ int (*restore_dev)(struct iommu_domain *domain, struct device *dev);
+#endif
int (*set_dev_pasid)(struct iommu_domain *domain, struct device
*dev,
ioasid_t pasid, struct iommu_domain *old);
Thanks,
baolu
Re: [PATCH v3 12/18] iommu/vt-d: Handle reattach of the restored domain
Quoted 54 lines · Baolu Lu
Quoted 53 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.
Signed-off-by Samiullah Khawaja<[email protected]>--- drivers/iommu/intel/iommu.c | 46 ++++++++++--- drivers/iommu/intel/iommu.h | 17 +++++ drivers/iommu/intel/liveupdate.c | 111 +++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 11 deletions(-)drivers/iommu/intel/iommu.c +16
-11@@ -1311,10 +1311,16 @@ static int dmar_domain_attach_device(struct dmar_domain *domain, {struct device_domain_info *info =dev_iommu_priv_get (dev);struct intel_iommu *iommu = info->iommu; +struct iommu_device_ser *device_ser;unsigned long flags;int ret; - ret =domain_attach_iommu (domain, iommu); + device_ser =dev_iommu_restored_state (dev); +if (!device_ser) + ret =domain_attach_iommu (domain, iommu); +else + ret =intel_iommu_domain_reattach_iommu (domain, + iommu, device_ser);if (ret)return ret;@@ -1327,16 +1333,20 @@ static int dmar_domain_attach_device(struct dmar_domain *domain,if (dev_is_real_dma_subdevice (dev))return 0 ; -if (!sm_supported (iommu)) - ret =domain_context_mapping (domain, dev); -else if (intel_domain_is_fs_paging (domain)) - ret =domain_setup_first_level (iommu, domain, dev, -IOMMU_NO_PASID ,NULL ); -else if (intel_domain_is_ss_paging (domain)) - ret =domain_setup_second_level (iommu, domain, dev, -IOMMU_NO_PASID ,NULL ); -else if (WARN_ON (true)) - ret = -EINVAL ; +if (!device_ser) { +if (!sm_supported (iommu)) + ret =domain_context_mapping (domain, dev); +else if (intel_domain_is_fs_paging (domain)) + ret =domain_setup_first_level (iommu, domain, dev, +IOMMU_NO_PASID ,NULL ); +else if (intel_domain_is_ss_paging (domain)) + ret =domain_setup_second_level (iommu, domain, dev, +IOMMU_NO_PASID ,NULL );
Quoted 8 lines
drivers/iommu/intel/iommu.c static int dmar_domain_attach_device(struct dmar_domain *domain, · line 1348
Quoted 5 lines
drivers/iommu/intel/iommu.c +5
-0+else if (WARN_ON (true)) + ret = -EINVAL ; + }else if (!sm_supported (iommu)) { +iommu_enable_pci_ats (info); + }Instead of merging domain restoration into the
attach_devpath, how about adding a new callback to restore a preserved domain for a device?
Even with a new callback, the driver still just fetches the restored state and takes a different path. I am guessing we can just do the following inside the driver to keep it clean:
static int intel_iommu_attach_device(struct iommu_domain *domain,
struct device *dev, struct iommu_domain *old)
{
struct iommu_device_ser *device_ser = dev_iommu_restored_state(dev);
if (device_ser)
return _intel_iommu_restore_dev(domain, dev, device_ser);
return _intel_iommu_attach_device(domain, dev, old);
}This keeps the separation you want without touching the generic ops. WDYT?
With the new callback, this check is just moved into the core inside __iommu_attach_device(). I am concerned that later down the road when we add PASID support, we will add restore_dev_pasid().
Quoted 31 lines
Something like:
include/linux/iommu.h +0
-0@@ -748,6 +748,8 @@ struct iommu_ops {* * <others> - treated as ENODEV by the caller. Use is discouraged * @set_dev_pasid: set or replace an iommu domain to a pasid of device. The pasid of
* the device should be left in the old config in
error case.
+ * @restore_dev: Set a domain that is restored from the previous live-updated
+ * kernel to a device.
* @map_pages: map a physically contiguous set of pages of the same
size to
* an iommu domain.
* @unmap_pages: unmap a number of pages of the same size from an
iommu domaininclude/linux/iommu.h +3
-0@@ -772,6 +774,9 @@ struct iommu_ops {struct iommu_domain_ops {int (*attach_dev)(struct iommu_domain *domain,struct device *dev,struct iommu_domain *old); +#ifdef CONFIG_IOMMU_LIVEUPDATE +int (*restore_dev)(struct iommu_domain *domain,struct device *dev); +#endif int (*set_dev_pasid)(struct iommu_domain *domain,struct device *dev,ioasid_t pasid,struct iommu_domain *old);
Quoted 2 lines
Thanks,
baolu
Thanks,
Sami
Baolu Lu
maintainerRe: [PATCH v3 12/18] iommu/vt-d: Handle reattach of the restored domain
Quoted 82 lines · Samiullah Khawaja
Quoted 56 lines · Baolu Lu
Quoted 55 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.
Signed-off-by Samiullah Khawaja<[email protected]>---
drivers/iommu/intel/iommu.c | 46 ++++++++++--- drivers/iommu/intel/iommu.h | 17 +++++drivers/iommu/intel/liveupdate.c| 111 +++++++++++++++++++++++++++++++3 files changed, 163 insertions(+), 11 deletions(-)drivers/iommu/intel/iommu.c +16
-11@@ -1311,10 +1311,16 @@ static int dmar_domain_attach_device(struct dmar_domain *domain, { struct device_domain_info *info = dev_iommu_priv_get(dev); struct intel_iommu *iommu = info->iommu; + struct iommu_device_ser *device_ser; unsigned long flags; int ret; - ret = domain_attach_iommu(domain, iommu); + device_ser = dev_iommu_restored_state(dev); + if (!device_ser) + ret = domain_attach_iommu(domain, iommu); + else + ret = intel_iommu_domain_reattach_iommu(domain, + iommu, device_ser); if (ret) return ret;@@ -1327,16 +1333,20 @@ static int dmar_domain_attach_device(structdmar_domain *domain, if (dev_is_real_dma_subdevice(dev)) return 0; - if (!sm_supported(iommu)) - ret = domain_context_mapping(domain, dev); - else if (intel_domain_is_fs_paging(domain)) - ret = domain_setup_first_level(iommu, domain, dev, - IOMMU_NO_PASID, NULL); - else if (intel_domain_is_ss_paging(domain)) - ret = domain_setup_second_level(iommu, domain, dev, - IOMMU_NO_PASID, NULL); - else if (WARN_ON(true)) - ret = -EINVAL; + if (!device_ser) { + if (!sm_supported(iommu)) + ret = domain_context_mapping(domain, dev); + else if (intel_domain_is_fs_paging(domain)) + ret = domain_setup_first_level(iommu, domain, dev, + IOMMU_NO_PASID, NULL); + else if (intel_domain_is_ss_paging(domain)) + ret = domain_setup_second_level(iommu, domain, dev, + IOMMU_NO_PASID, NULL);Quoted 8 lines
drivers/iommu/intel/iommu.c static int dmar_domain_attach_device(struct · line 1349
Quoted 5 lines
drivers/iommu/intel/iommu.c +5
-0+ else if (WARN_ON(true)) + ret = -EINVAL; + } else if (!sm_supported(iommu)) { + iommu_enable_pci_ats(info); + }Instead of merging domain restoration into the
attach_devpath, how about adding a new callback to restore a preserved domain for a device?Even with a new callback, the driver still just fetches the restored state and takes a different path. I am guessing we can just do the following inside the driver to keep it clean:
static int intel_iommu_attach_device(struct
iommu_domain*domain,
struct device *dev, structiommu_domain*old)
{
structiommu_device_ser*device_ser=dev_iommu_restored_state(dev);if (
device_ser)
return_intel_iommu_restore_dev(domain, dev, device_ser);return
_intel_iommu_attach_device(domain, dev, old);
}
Quoted 6 lines
This keeps the separation you want without touching the generic ops. WDYT?
With the new callback, this check is just moved into the core inside
__iommu_attach_device(). I am concerned that later down the road when we add PASID support, we will addrestore_dev_pasid().
Okay, that also works for me.
Thanks,
baolu
PATCH v3 13/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/liveupdate.c | 57 ++++++++++++++++++++++++++++++++ drivers/iommu/intel/pasid.c | 7 ++-- drivers/iommu/intel/pasid.h | 9 +++++ include/linux/kho/abi/iommu.h | 13 ++++++++ 4 files changed, 84 insertions(+), 2 deletions(-)
drivers/iommu/intel/liveupdate.c +57 -0
@@ -14,6 +14,7 @@#include <linux/pci.h> #include "iommu.h" +#include "pasid.h" #include "../iommu-pages.h" /* 2 tables per bus in scalable mode with upper table at odd bit */
@@ -369,6 +370,7 @@ int intel_iommu_preserve_device(struct device *dev, struct iommu_device_ser *device_ser) {struct device_domain_info *info =dev_iommu_priv_get (dev); +struct pasid_table *pasid_table;int ret;if (!dev_is_pci (dev)) {
@@ -385,9 +387,49 @@ int intel_iommu_preserve_device(struct device *dev, device_ser->domain_iommu_ser.attachment_id =domain_id_iommu (info->domain, info->iommu); + +if (!sm_supported (info->iommu)) +return 0 ; + + pasid_table =intel_pasid_get_table (dev); +if (!pasid_table) +return -EINVAL ; + + ret =pasid_lu_handle_pd (pasid_table->table, + pasid_table->max_pasid, +PASID_LU_OP_PRESERVE ); +if (ret) +return ret; + + device_ser->intel.pasid_table =virt_to_phys (pasid_table->table); + device_ser->intel.max_pasid = pasid_table->max_pasid;return 0 ; } +void intel_iommu_unpreserve_device (struct device *dev, +struct iommu_device_ser *device_ser) +{ +struct device_domain_info *info =dev_iommu_priv_get (dev); +struct pasid_table *pasid_table; + +if (!dev_is_pci (dev)) +return ; + +if (!info) +return ; + +if (!sm_supported (info->iommu)) +return ; + + pasid_table =intel_pasid_get_table (dev); +if (!pasid_table) +return ; + +pasid_lu_handle_pd (pasid_table->table, + pasid_table->max_pasid, +PASID_LU_OP_UNPRESERVE ); +} +int intel_iommu_preserve (struct iommu_device *iommu_dev,struct iommu_hw_ser *ser) {
@@ -418,3 +460,18 @@ void intel_iommu_unpreserve(struct iommu_device *iommu_dev,unpreserve_iommu_context_tables (iommu, ser);iommu_unpreserve_pages (iommu->root_entry); } + +void *intel_pasid_try_restore_table (struct device *dev,u64 max_pasid) +{ +struct iommu_device_ser *ser =dev_iommu_restored_state (dev); + +if (!ser || !ser->intel.pasid_table) +return NULL ; + +BUG_ON (pasid_lu_handle_pd (phys_to_virt (ser->intel.pasid_table), + ser->intel.max_pasid, +PASID_LU_OP_RESTORE )); +BUG_ON (ser->intel.max_pasid < max_pasid); + +return phys_to_virt (ser->intel.pasid_table); +}
drivers/iommu/intel/pasid.c +5 -2
@@ -60,8 +60,11 @@ 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 )); + + dir =intel_pasid_try_restore_table (dev,1 << (order +PAGE_SHIFT +3 )); +if (!dir) + 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 +9 -0
@@ -301,6 +301,15 @@ 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_try_restore_table (struct device *dev,u64 max_pasid); +#else +static inline void *intel_pasid_try_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 +13 -0
@@ -127,6 +127,16 @@ struct iommu_dev_map_ser {u64 iommu_phys; } __packed; +/** +* struct iommu_device_intel_ser - Intel specific state of serialized device +* @pasid_table: Physical address of pasid table +* @max_pasid: Maximum supported pasid +*/ +struct iommu_device_intel_ser { +u64 pasid_table; +u64 max_pasid; +} __packed; +/** * struct iommu_device_ser - Serialized state of a device * @hdr: Common object header
@@ -139,6 +149,9 @@ struct iommu_device_ser {u32 devid;u32 pci_domain_nr;struct iommu_dev_map_ser domain_iommu_ser; +union { +struct iommu_device_intel_ser intel; + }; } __packed;/**
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Baolu Lu
maintainerRe: [PATCH v3 13/18] iommu/vt-d: preserve PASID table of preserved device
Quoted 8 lines · Samiullah Khawaja
drivers/iommu/intel/pasid.c +0
-0@@ -60,8 +60,11 @@ int intel_pasid_alloc_table(struct device *dev) size = max_pasid >> (PASID_PDE_SHIFT -3 ); order = size ?get_order (size) :0 ;
drivers/iommu/intel/pasid.c int intel_pasid_alloc_table(struct device *dev) · line 70
Quoted 10 lines
drivers/iommu/intel/pasid.c +5
-2- dir =iommu_alloc_pages_node_sz (info->iommu->node,GFP_KERNEL , -1 << (order +PAGE_SHIFT )); + + dir =intel_pasid_try_restore_table (dev,1 << (order +PAGE_SHIFT +3 )); +if (!dir) + dir =iommu_alloc_pages_node_sz (info->iommu->node,GFP_KERNEL , +1 << (order +PAGE_SHIFT ));if (!dir) {kfree (pasid_table);return -ENOMEM ;
This reads as if PASID table restoration fails, it tries to allocate a native PASID table instead. This doesn't match my understanding. It would be a disaster, and the system should panic if the table restoration fails. Or is there something I'm overlooking?
Thanks,
baolu
Re: [PATCH v3 13/18] iommu/vt-d: preserve PASID table of preserved device
Quoted 15 lines · Baolu Lu
Quoted 14 lines · Samiullah Khawaja
drivers/iommu/intel/pasid.c +5
-2@@ -60,8 +60,11 @@ 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 )); + + dir =intel_pasid_try_restore_table (dev,1 << (order +PAGE_SHIFT +3 )); +if (!dir) + dir =iommu_alloc_pages_node_sz (info->iommu->node,GFP_KERNEL , +1 << (order +PAGE_SHIFT ));
Quoted 8 lines
drivers/iommu/intel/pasid.c int intel_pasid_alloc_table(struct device *dev) · line 70
Quoted 3 lines
drivers/iommu/intel/pasid.c +0
-0if (!dir) {kfree (pasid_table);return -ENOMEM ;This reads as if PASID table restoration fails, it tries to allocate a native PASID table instead. This doesn't match my understanding. It would be a disaster, and the system should panic if the table restoration fails. Or is there something I'm overlooking?
Your understanding is correct, and that is exactly what intel_pasid_try_restore_table() does if the device state was preserved. If the restoration fails, it fails hard with a panic.
The reason I have _try_ in the name is to account for devices that simply have no preserved state to begin with. In that case, a new PASID table needs to be allocated.
I agree the naming makes it sound like we ignore restoration failures. I will rename the function to make this less confusing in v4.
Quoted 3 lines
Thanks,
baolu
PATCH v3 14/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 | 2 + drivers/iommu/iommufd/iommufd_private.h | 18 ++++++++ drivers/iommu/iommufd/liveupdate.c | 56 +++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 9 ++++ include/uapi/linux/iommufd.h | 26 ++++++++++++ 6 files changed, 112 insertions(+) create mode 100644 drivers/iommu/iommufd/liveupdate.c
MAINTAINERS +1 -0
@@ -13498,6 +13498,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 +2 -0
@@ -11,9 +11,11 @@ iommufd-y := \ viommu.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 iommufd_driver-y := driver.o obj-$(CONFIG_IOMMUFD_DRIVER_CORE) += iommufd_driver.o +
drivers/iommu/iommufd/iommufd_private.h +18 -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;
@@ -373,6 +378,10 @@ struct iommufd_hwpt_paging { bool auto_domain : 1; bool enforce_cache_coherency : 1; bool nest_parent : 1; +#ifdef CONFIG_IOMMU_LIVEUPDATE + bool liveupdate_preserve : 1; +u64 liveupdate_token; +#endif /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item;struct iommufd_sw_msi_maps present_sw_msi;
@@ -706,6 +715,15 @@ iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id) struct iommufd_vdevice, 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 +56 -0
56 lines not on this page · raw message
drivers/iommu/iommufd/main.c +9 -0
@@ -313,6 +313,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);
@@ -375,6 +378,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));
@@ -420,6 +426,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;
@@ -493,6 +500,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 +26 -0
@@ -57,6 +57,7 @@ enum {IOMMUFD_CMD_IOAS_CHANGE_PROCESS =0x92 ,IOMMUFD_CMD_VEVENTQ_ALLOC =0x93 ,IOMMUFD_CMD_HW_QUEUE_ALLOC =0x94 , + IOMMUFD_CMD_HWPT_LU_MARK_PRESERVE = 0x95, };/**
@@ -1347,4 +1348,29 @@ struct iommu_hw_queue_alloc { __aligned_u64 length; };#define IOMMU_HW_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HW_QUEUE_ALLOC) + +/** +* struct iommu_hwpt_liveupdate_mark_preserve - ioctl(IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE) +* @size: sizeof(struct iommu_hwpt_liveupdate_mark_preserve) +* @hwpt_id: Iommufd object ID of the target HWPT +* @hwpt_token: Token to identify this hwpt upon restore +* +* The target HWPT will be preserved during iommufd preservation. +* Only file-based memory mappings (e.g. memfd) are supported for HWPTs marked +* for preservation. Mapping anonymous memory into a preserved HWPT will result +* in a failure during the preservation phase. +* +* The hwpt_token is provided by userspace. If userspace enters a token +* already in use within this iommufd, -EADDRINUSE is returned from this ioctl. +* +* Note: There is no 'unmark' operation, so any HWPTs pooled in userspace that +* are marked for preservation must be destroyed after use. +*/ +struct iommu_hwpt_liveupdate_mark_preserve { +__u32 size; +__u32 hwpt_id; +__u64 hwpt_token; +}; +#define IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_LU_MARK_PRESERVE) +#endif
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
PATCH v3 15/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 | 11 + drivers/iommu/iommufd/io_pagetable.h | 1 + drivers/iommu/iommufd/iommufd_private.h | 27 ++- drivers/iommu/iommufd/liveupdate.c | 298 ++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 10 +- drivers/iommu/iommufd/pages.c | 11 + include/linux/kho/abi/iommufd.h | 51 ++++ 8 files changed, 408 insertions(+), 2 deletions(-) create mode 100644 include/linux/kho/abi/iommufd.h
MAINTAINERS +1 -0
@@ -13502,6 +13502,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 +11 -0
@@ -384,6 +384,11 @@ int iopt_map_pages(struct io_pagetable *iopt, struct list_head *pages_list,return rc;down_read (&iopt->domains_rwsem); +if (iopt_liveupdate_immutable (iopt)) { + rc = -EBUSY ; + goto out_unlock_domains; + } + rc =iopt_fill_domains_pages (pages_list);if (rc) goto out_unlock_domains;
@@ -755,6 +760,12 @@ static int iopt_unmap_iova_range(struct io_pagetable *iopt, unsigned long start, again:down_read (&iopt->domains_rwsem);down_write (&iopt->iova_rwsem); + +if (iopt_liveupdate_immutable (iopt)) { + rc = -EBUSY ; + goto out_unlock_iova; + } +while ((area =iopt_area_iter_first (iopt, start, last))) {unsigned long area_last =iopt_area_last_iova (area);unsigned long area_first =iopt_area_iova (area);
drivers/iommu/iommufd/io_pagetable.h +1 -0
@@ -234,6 +234,7 @@ struct iopt_pages {struct {/* IOPT_ADDRESS_FILE */ struct file *file;unsigned long start; +u32 seals; };/* IOPT_ADDRESS_DMABUF */ struct iopt_pages_dmabuf dmabuf;
drivers/iommu/iommufd/iommufd_private.h +26 -1
@@ -98,6 +98,9 @@ struct io_pagetable {/* IOVA that cannot be allocated, struct iopt_reserved */ struct rb_root_cached reserved_itree;u8 disable_large_pages; +#ifdef CONFIG_IOMMU_LIVEUPDATE +u32 nr_preserved_domains; +#endif unsigned long iova_alignment; };
@@ -379,7 +382,7 @@ struct iommufd_hwpt_paging { bool enforce_cache_coherency : 1; bool nest_parent : 1;#ifdef CONFIG_IOMMU_LIVEUPDATE - bool liveupdate_preserve : 1; + bool liveupdate_preserved : 1;u64 liveupdate_token;#endif /* Head at iommufd_ioas::hwpt_list */
@@ -716,12 +719,34 @@ iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id) }#ifdef CONFIG_IOMMU_LIVEUPDATE +int iommufd_liveupdate_register (void ); +void iommufd_liveupdate_unregister (void ); +int iommufd_hwpt_liveupdate_mark_preserve (struct iommufd_ucmd *ucmd); + +static inline bool iopt_liveupdate_immutable (const struct io_pagetable *iopt) +{ +return iopt->nr_preserved_domains >0 ; +}#else +static inline int iommufd_liveupdate_register (void ) +{ +return 0 ; +} + +static inline void iommufd_liveupdate_unregister (void ) +{ +} +static inline int iommufd_hwpt_liveupdate_mark_preserve (struct iommufd_ucmd *ucmd) {return -ENOTTY ; } + +static inline bool iopt_liveupdate_immutable (const struct io_pagetable *iopt) +{ +return false; +}#endif #ifdef CONFIG_IOMMUFD_TEST
drivers/iommu/iommufd/liveupdate.c +298 -0
310 lines not on this page · raw message
drivers/iommu/iommufd/main.c +9 -1
@@ -782,11 +782,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);
@@ -798,6 +805,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);
drivers/iommu/iommufd/pages.c +11 -0
@@ -55,6 +55,7 @@#include <linux/overflow.h> #include <linux/slab.h> #include <linux/sched/mm.h> +#include <linux/memfd.h> #include <linux/vfio_pci_core.h> #include "double_span.h"
@@ -1421,6 +1422,7 @@ struct iopt_pages *iopt_alloc_file_pages(struct file *file, {struct iopt_pages *pages; +int seals; pages =iopt_alloc_pages (start_byte, length, writable);if (IS_ERR (pages))
@@ -1428,6 +1430,15 @@ struct iopt_pages *iopt_alloc_file_pages(struct file *file, pages->file =get_file (file); pages->start = start - start_byte; pages->type =IOPT_ADDRESS_FILE ; + +/* +* Get seals from the memfd during mapping to verify that these did not +* change before iommufd preservation. +*/ + seals =memfd_get_seals (file); +if (seals >0 ) + pages->seals = seals; +return pages; }
include/linux/kho/abi/iommufd.h new file +51 -0
51 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
PATCH v3 16/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. Handle to the preserved state of the device is also returned to reassociate with the restored state after live update kexec.
--- drivers/iommu/iommufd/device.c | 102 ++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 3 + include/linux/iommufd.h | 29 +++++++ 3 files changed, 134 insertions(+)
drivers/iommu/iommufd/device.c +102 -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>
@@ -610,6 +611,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 );
@@ -1665,3 +1670,100 @@ 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; +} + +int iommufd_device_preserve (struct liveupdate_session *s, +struct iommufd_device *idev, +u64 *iommufd_tokenp, +u64 *preserved_state) +{ +struct iommufd_group *igroup = idev->igroup; +struct iommufd_hwpt_paging *hwpt_paging; +struct iommufd_hw_pagetable *hwpt; +struct iommufd_attach *attach; +int ret; + +mutex_lock (&igroup->lock); +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; + } + + hwpt = attach->hwpt; + hwpt_paging =find_hwpt_paging (hwpt); +if (!hwpt_paging || !hwpt_paging->liveupdate_preserved) { + ret = -EINVAL ; + goto out; + } + + ret =liveupdate_get_token_outgoing (s, idev->ictx->file, iommufd_tokenp); +if (ret) + goto out; + + ret =iommu_preserve_device (hwpt_paging->common.domain, + idev->dev, + preserved_state); + +if (!ret) + igroup->liveupdate_preserved = true; +out: +mutex_unlock (&igroup->lock); +return ret; +} +EXPORT_SYMBOL_NS_GPL (iommufd_device_preserve,"IOMMUFD" ); + +void iommufd_device_unpreserve (struct liveupdate_session *s, +struct iommufd_device *idev) +{ +struct iommufd_group *igroup = idev->igroup; +struct iommufd_hwpt_paging *hwpt_paging; +struct iommufd_hw_pagetable *hwpt; +struct iommufd_attach *attach; + +mutex_lock (&igroup->lock); + attach =xa_load (&igroup->pasid_attach,IOMMU_NO_PASID ); +if (!attach) { +WARN (1 ,"IOMMU_NO_PASID attachment not found" ); + igroup->liveupdate_preserved = false; + goto out; + } + + hwpt = attach->hwpt; + hwpt_paging =find_hwpt_paging (hwpt); +if (!hwpt_paging || !hwpt_paging->liveupdate_preserved) { +WARN (1 ,"Attached domain is not preserved" ); + igroup->liveupdate_preserved = false; + goto out; + } + +iommu_unpreserve_device (hwpt_paging->common.domain, idev->dev); + igroup->liveupdate_preserved = false; +out: +mutex_unlock (&igroup->lock); +} +EXPORT_SYMBOL_NS_GPL (iommufd_device_unpreserve,"IOMMUFD" ); + +bool iommufd_device_is_preserved (struct iommufd_device *idev) +{ +return idev && idev->igroup && idev->igroup->liveupdate_preserved; +} +EXPORT_SYMBOL_NS_GPL (iommufd_device_is_preserved,"IOMMUFD" ); +#endif
drivers/iommu/iommufd/iommufd_private.h +3 -0
@@ -486,6 +486,9 @@ struct iommufd_group {struct xarray pasid_attach;struct iommufd_sw_msi_maps required_sw_msi;phys_addr_t sw_msi_start; +#ifdef CONFIG_IOMMU_LIVEUPDATE +bool liveupdate_preserved; +#endif };/*
include/linux/iommufd.h +29 -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>
@@ -71,6 +72,34 @@ void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid);struct iommufd_ctx *iommufd_device_to_ictx (struct iommufd_device *idev);u32 iommufd_device_to_id (struct iommufd_device *idev); +#ifdef CONFIG_IOMMU_LIVEUPDATE +int iommufd_device_preserve (struct liveupdate_session *s, +struct iommufd_device *idev, +u64 *iommufd_tokenp, +u64 *preserved_state); +void iommufd_device_unpreserve (struct liveupdate_session *s, +struct iommufd_device *idev); +bool iommufd_device_is_preserved (struct iommufd_device *idev); +#else +static inline int iommufd_device_preserve (struct liveupdate_session *s, +struct iommufd_device *idev, +u64 *iommufd_tokenp, +u64 *preserved_state) +{ +return -EOPNOTSUPP ; +} + +static inline void iommufd_device_unpreserve (struct liveupdate_session *s, +struct iommufd_device *idev) +{ +} + +static inline bool iommufd_device_is_preserved (struct iommufd_device *idev) +{ +return false; +} +#endif +struct iommufd_access_ops {u8 needs_pin_pages :1 ;void (*unmap)(void *data,unsigned long iova,unsigned long length);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
PATCH v3 17/18 vfio/pci: Preserve the iommufd state of the vfio cdev
In reply to Samiullah KhawajaIf the vfio cdev is attached to an iommufd, preserve the state of the attached iommufd also. Basically preserve the iommu specific state of the device and also the attach iommu HW unit.
Once the device and its iommufd attachment is preserved, it cannot be detached or attached to another IOAS until it is unpreserved.
--- drivers/vfio/device_cdev.c | 10 +++++++ drivers/vfio/pci/vfio_pci_liveupdate.c | 37 +++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-)
drivers/vfio/device_cdev.c +10 -0
@@ -275,6 +275,11 @@ int vfio_df_ioctl_attach_pt(struct vfio_device_file *df, }mutex_lock (&device->dev_set->lock); +if (iommufd_device_is_preserved (device->iommufd_device)) { + ret = -EBUSY ; + goto out_unlock; + } +if (attach.flags &VFIO_DEVICE_ATTACH_PASID ) ret = device->ops->pasid_attach_ioas(device, attach.pasid,
@@ -333,6 +338,11 @@ int vfio_df_ioctl_detach_pt(struct vfio_device_file *df, }mutex_lock (&device->dev_set->lock); +if (iommufd_device_is_preserved (device->iommufd_device)) { +mutex_unlock (&device->dev_set->lock); +return -EBUSY ; + } +if (detach.flags &VFIO_DEVICE_DETACH_PASID ) device->ops->pasid_detach_ioas (device, detach.pasid);else
drivers/vfio/pci/vfio_pci_liveupdate.c +36 -1
@@ -106,6 +106,7 @@#include <linux/errno.h> #include <linux/file.h> +#include <linux/iommufd.h> #include <linux/kexec_handover.h> #include <linux/kho/abi/vfio_pci.h> #include <linux/liveupdate.h>
@@ -114,6 +115,8 @@#include "vfio_pci_priv.h" +MODULE_IMPORT_NS ("IOMMUFD" ); +static bool vfio_pci_liveupdate_can_preserve (struct liveupdate_file_handler *handler,struct file *file) {
@@ -155,9 +158,26 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) vdev = container_of(device, struct vfio_pci_core_device, vdev); pdev = vdev->pdev; +#ifdef CONFIG_IOMMU_LIVEUPDATE +/* If iommufd is attached, preserve the underlying domain */ +mutex_lock (&device->dev_set->lock); +if (device->iommufd_attached) { +u64 token, preserved_state; + + ret =iommufd_device_preserve (args->session, + device->iommufd_device, + &token, &preserved_state); +if (ret) { +mutex_unlock (&device->dev_set->lock); +return ret; + } + } +mutex_unlock (&device->dev_set->lock); +#endif + ret =pci_liveupdate_preserve (pdev);if (ret) -return ret; + goto err_iommufd_unpreserve; ser =kho_alloc_preserve (sizeof (*ser));if (IS_ERR (ser)) {
@@ -173,12 +193,27 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) err_unpreserve:pci_liveupdate_unpreserve (pdev); + +err_iommufd_unpreserve: +mutex_lock (&device->dev_set->lock); +if (device->iommufd_attached) +iommufd_device_unpreserve (args->session, + device->iommufd_device); +mutex_unlock (&device->dev_set->lock);return ret; }static void vfio_pci_liveupdate_unpreserve (struct liveupdate_file_op_args *args) {struct vfio_device *device =vfio_device_from_file (args->file); +struct vfio_pci_core_device_ser *ser; + + ser =phys_to_virt (args->serialized_data); +mutex_lock (&device->dev_set->lock); +if (device->iommufd_attached) +iommufd_device_unpreserve (args->session, + device->iommufd_device); +mutex_unlock (&device->dev_set->lock);pci_liveupdate_unpreserve (to_pci_dev (device->dev));kho_unpreserve_free (phys_to_virt (args->serialized_data));
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
PATCH v3 18/18 iommufd/selftest: Add test to verify iommufd preservation
In reply to Samiullah KhawajaTest iommufd preservation by setting up an iommufd and vfio cdev and preserve it across live update. Test takes VFIO cdev path of a device bound to vfio-pci driver and binds it to an iommufd being preserved. It also preserves the vfio cdev so the iommufd state associated with it is also preserved.
The restore path is tested by restoring the preserved vfio cdev only. On restore, test verifies that the bind with a new iommufd fails as the device is attached to the restored IOMMU domain. Also the LUO session finish fails as the preserved iommufd is not restored.
--- tools/testing/selftests/iommu/Makefile | 12 + .../iommu/iommufd_liveupdate_kexec_test.c | 241 ++++++++++++++++++ 2 files changed, 253 insertions(+) create mode 100644 tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c
tools/testing/selftests/iommu/Makefile +12 -0
@@ -7,4 +7,16 @@ TEST_GEN_PROGS := TEST_GEN_PROGS += iommufd TEST_GEN_PROGS += iommufd_fail_nth +TEST_GEN_PROGS_EXTENDED += iommufd_liveupdate_kexec_test + include ../lib.mk +include ../liveupdate/lib/libliveupdate.mk + +CFLAGS += -I$(top_srcdir)/tools/include +CFLAGS += -MD +CFLAGS += $(EXTRA_CFLAGS) + +$(TEST_GEN_PROGS_EXTENDED): %: %.o $(LIBLIVEUPDATE_O) + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBLIVEUPDATE_O) $(LDLIBS) -o $@ + +EXTRA_CLEAN += $(LIBLIVEUPDATE_O)
tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c new file +241 -0
241 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog