iommu: Add live update state preservation
Subsystems (8)
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.
This is a non-RFC version of the previously sent RFC:
https://lore.kernel.org/all/[email protected]/
Please take a look at the following LWN article to learn about KHO and Live Update Orchestrator:
https://lwn.net/Articles/1033364/
This work is based on,
- linux-next (tag: next-20260115)
- MEMFD SEAL preservation series:
https://lore.kernel.org/all/[email protected]/
- VFIO CDEV preservation series (v2):
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-v1
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 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 <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.
- 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).
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.
Prepare:
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.
Pasha Tatashin (1):
liveupdate: luo_file: Add internal APIs for file preservation
Samiullah Khawaja (11):
iommu: Implement IOMMU LU FLB callbacks
iommu: Implement IOMMU core liveupdate skeleton iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
iommupt: Implement preserve/unpreserve/restore callbacks iommu/vt-d: Implement device and iommu preserve/unpreserve ops iommu/vt-d: Restore IOMMU state and reclaimed domain ids
iommu: Restore and reattach preserved domains to devices 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-lu: Implement ioctl to let userspace mark an HWPT to be
preserved
iommufd-lu: Persist iommu hardware pagetables for live update
drivers/iommu/Kconfig | 11 + drivers/iommu/Makefile | 1 + drivers/iommu/generic_pt/iommu_pt.h | 96 ++++ drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 115 +++- drivers/iommu/intel/iommu.h | 42 +- drivers/iommu/intel/liveupdate.c | 304 ++++++++++ drivers/iommu/intel/nested.c | 2 +- drivers/iommu/intel/pasid.c | 7 +- drivers/iommu/intel/pasid.h | 9 + drivers/iommu/iommu-pages.c | 74 +++ drivers/iommu/iommu-pages.h | 30 + drivers/iommu/iommu.c | 50 +- drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/device.c | 69 +++ drivers/iommu/iommufd/io_pagetable.c | 17 + drivers/iommu/iommufd/io_pagetable.h | 1 + drivers/iommu/iommufd/iommufd_private.h | 38 ++ drivers/iommu/iommufd/liveupdate.c | 349 ++++++++++++ drivers/iommu/iommufd/main.c | 16 +- drivers/iommu/iommufd/pages.c | 8 + drivers/iommu/liveupdate.c | 534 ++++++++++++++++++ drivers/vfio/pci/vfio_pci_liveupdate.c | 28 +- include/linux/generic_pt/iommu.h | 10 + include/linux/iommu-lu.h | 144 +++++ include/linux/iommu.h | 32 ++ include/linux/iommufd.h | 23 + include/linux/kho/abi/iommu.h | 127 +++++ include/linux/kho/abi/iommufd.h | 39 ++ include/linux/kho/abi/vfio_pci.h | 10 + include/linux/liveupdate.h | 21 + include/uapi/linux/iommufd.h | 19 + kernel/liveupdate/luo_file.c | 71 +++ kernel/liveupdate/luo_internal.h | 16 + tools/testing/selftests/iommu/Makefile | 12 + .../selftests/iommu/iommufd_liveupdate.c | 209 +++++++ 36 files changed, 2502 insertions(+), 34 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-lu.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.c
base-commit: 9b7977f9e39b7768c70c2aa497f04e7569fd3e00
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
PATCH 1/14 iommu: Implement IOMMU LU FLB callbacks
Add liveupdate FLB for IOMMU state preservation. Use KHO preserve memory alloc/free helper functions to allocate memory for the IOMMU LU FLB object and the serialization structs for device, domain and iommu.
During retrieve, walk through the preserved objs nodes and restore each folio. Also recreate the FLB obj.
--- drivers/iommu/Kconfig | 11 +++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 177 ++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 17 ++++ include/linux/kho/abi/iommu.h | 119 +++++++++++++++++++++++ 5 files changed, 325 insertions(+) create mode 100644 drivers/iommu/liveupdate.c create mode 100644 include/linux/iommu-lu.h create mode 100644 include/linux/kho/abi/iommu.h
drivers/iommu/Kconfig +11 -0
@@ -11,6 +11,17 @@ config IOMMUFD_DRIVER bool default 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. + menuconfig IOMMU_SUPPORT bool "IOMMU Hardware Support" depends on MMU
drivers/iommu/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o 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_LIVEUPDATE) += liveupdate.o obj-$(CONFIG_IOMMU_IOVA) += iova.o obj-$(CONFIG_OF_IOMMU) += of_iommu.o obj-$(CONFIG_MSM_IOMMU) += msm_iommu.o
drivers/iommu/liveupdate.c new file +177 -0
177 lines not on this page · raw message
include/linux/iommu-lu.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2025, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#ifndef _LINUX_IOMMU_LU_H +#define _LINUX_IOMMU_LU_H + +#include <linux/liveupdate.h> +#include <linux/kho/abi/iommu.h> + +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler); +int iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler); + +#endif /* _LINUX_IOMMU_LU_H */
include/linux/kho/abi/iommu.h new file +119 -0
119 lines not on this page · raw message
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
Quoted 28 lines · Samiullah Khawaja
Add liveupdate FLB for IOMMU state preservation. Use KHO preserve memory alloc/free helper functions to allocate memory for the IOMMU LU FLB object and the serialization structs for device, domain and iommu.
During retrieve, walk through the preserved objs nodes and restore each folio. Also recreate the FLB obj.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/Kconfig | 11 +++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 177 ++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 17 ++++ include/linux/kho/abi/iommu.h | 119 +++++++++++++++++++++++ 5 files changed, 325 insertions(+) create mode 100644 drivers/iommu/liveupdate.c create mode 100644 include/linux/iommu-lu.h create mode 100644 include/linux/kho/abi/iommu.hdrivers/iommu/Kconfig +1
-0@@ -11,6 +11,17 @@ config IOMMUFD_DRIVER bool default n +config IOMMU_LIVEUPDATE
drivers/iommu/Kconfig config IOMMUFD_DRIVER · line 24
Quoted 10 lines
drivers/iommu/Kconfig +10
-0+ 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. +
I'm wondering if this should be under the if IOMMU_SUPPORT below? I believe this was added here because IOMMUFD isn't under IOMMU_SUPPORT, but it wouldn't make sense to "preserve" IOMMU across a liveupdate if IOMMU_SUPPORT is disabled? Should we probably be move it inside the if IOMMU_SUPPORT block for better organization, or at least have a depends on IOMMU_SUPPORT added to it? The IOMMU_LUO still depends on the IOMMU_SUPPORT infrastructure to actually function.. as we add calls within core functions like dev_iommu_get etc.
Quoted 21 lines
drivers/iommu/Kconfig +0
-0menuconfig IOMMU_SUPPORT bool "IOMMU Hardware Support" depends on MMUdrivers/iommu/Makefile +1
-0@@ -15,6 +15,7 @@ obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o 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_LIVEUPDATE) += liveupdate.o obj-$(CONFIG_IOMMU_IOVA) += iova.o obj-$(CONFIG_OF_IOMMU) += of_iommu.o obj-$(CONFIG_MSM_IOMMU) += msm_iommu.odrivers/iommu/liveupdate.c new file +0
-0
drivers/iommu/liveupdate.c line 4
Quoted 4 lines
drivers/iommu/liveupdate.c new file +4
-0@@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2025, Google LLC
Minor nit: 2026 OR 2025-26, here and everywhere else
Quoted 7 lines
drivers/iommu/liveupdate.c new file +7
-0+* Author: Samiullah Khawaja <[email protected]> +*/ + +#define pr_fmt (fmt) "iommu: liveupdate: " fmt + +#include <linux/kexec_handover.h> +#include <linux/liveupdate.h>
drivers/iommu/liveupdate.c line 21
Quoted 10 lines
drivers/iommu/liveupdate.c new file +10
-0+#include <linux/iommu-lu.h> +#include <linux/iommu.h> +#include <linux/errno.h> + +static void iommu_liveupdate_restore_objs (u64 next) +{ +struct iommu_objs_ser *objs; + +while (next) { +BUG_ON (!kho_restore_folio (next));
Same thing about BUG_ON [1] as mentioned below in the iommu_liveupdate_flb_retrieve() function, can we consider returning an error which can be checked in the caller and the error can be bubbled up as -ENODATA?
Quoted 7 lines
drivers/iommu/liveupdate.c new file +7
-0+ objs =__va (next); + next = objs->next_objs; + } +} + +static void iommu_liveupdate_free_objs (u64 next,bool incoming) +{
drivers/iommu/liveupdate.c line 38
Quoted 10 lines
drivers/iommu/liveupdate.c new file +10
-0+struct iommu_objs_ser *objs; + +while (next) { + objs =__va (next); + next = objs->next_objs; + +if (!incoming) +kho_unpreserve_free (objs); +else +folio_put (virt_to_folio (objs));
Interesting! kho_restore_folio already adjusts the refcount via adjust_managed_page_count which is why folio_put() is needed for a valid refcount. Sweet :)
Quoted 15 lines
drivers/iommu/liveupdate.c new file +15
-0+ } +} + +static void iommu_liveupdate_flb_free (struct iommu_lu_flb_obj *obj) +{ +if (obj->iommu_domains) +iommu_liveupdate_free_objs (obj->ser->iommu_domains_phys, false); + +if (obj->devices) +iommu_liveupdate_free_objs (obj->ser->devices_phys, false); + +if (obj->iommus) +iommu_liveupdate_free_objs (obj->ser->iommus_phys, false); + +kho_unpreserve_free (obj->ser);
drivers/iommu/liveupdate.c line 63
Quoted 10 lines
drivers/iommu/liveupdate.c new file +10
-0+kfree (obj); +} + +static int iommu_liveupdate_flb_preserve (struct liveupdate_flb_op_args *argp) +{ +struct iommu_lu_flb_obj *obj; +struct iommu_lu_flb_ser *ser; +void *mem; + + obj =kzalloc (sizeof (*obj),GFP_KERNEL );
I know this is obvious but let's add a comment that obj just exists in the "current" kernel whereas mem is supposed to survive the KHO..
Quoted 59 lines
drivers/iommu/liveupdate.c new file +59
-0+if (!obj) +return -ENOMEM ; + +mutex_init (&obj->lock); + mem =kho_alloc_preserve (sizeof (*ser)); +if (IS_ERR (mem)) + goto err_free; + + ser = mem; + obj->ser = ser; + + mem =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (mem)) + goto err_free; + + obj->iommu_domains = mem; + ser->iommu_domains_phys =virt_to_phys (obj->iommu_domains); + + mem =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (mem)) + goto err_free; + + obj->devices = mem; + ser->devices_phys =virt_to_phys (obj->devices); + + mem =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (mem)) + goto err_free; + + obj->iommus = mem; + ser->iommus_phys =virt_to_phys (obj->iommus); + + argp->obj = obj; + argp->data =virt_to_phys (ser); +return 0 ; + +err_free: +iommu_liveupdate_flb_free (obj); +return PTR_ERR (mem); +} + +static void iommu_liveupdate_flb_unpreserve (struct liveupdate_flb_op_args *argp) +{ +iommu_liveupdate_flb_free (argp->obj); +} + +static void iommu_liveupdate_flb_finish (struct liveupdate_flb_op_args *argp) +{ +struct iommu_lu_flb_obj *obj = argp->obj; + +if (obj->iommu_domains) +iommu_liveupdate_free_objs (obj->ser->iommu_domains_phys, true); + +if (obj->devices) +iommu_liveupdate_free_objs (obj->ser->devices_phys, true); + +if (obj->iommus) +iommu_liveupdate_free_objs (obj->ser->iommus_phys, true); +
drivers/iommu/liveupdate.c line 132
Quoted 10 lines
drivers/iommu/liveupdate.c new file +10
-0+folio_put (virt_to_folio (obj->ser)); +kfree (obj); +} + +static int iommu_liveupdate_flb_retrieve (struct liveupdate_flb_op_args *argp) +{ +struct iommu_lu_flb_obj *obj; +struct iommu_lu_flb_ser *ser; + + obj =kzalloc (sizeof (*obj),GFP_ATOMIC );
Why does this have to be GFP_ATOMIC? IIUC, the retrieve path is triggered by a userspace IOCTL in the new kernel. The system should be able to sleep here? (unless we have a use-case to call this in IRQ-ctx?) AFAICT, we call this under mutexes already, hence there's no situation where we could sleep in a spinlock context?
GFP_ATOMIC creates a point of failure if the system is under memory pressure. I believe we should be allowed to sleep for this allocation because the "preserved" mappings still allow DMAs to go on and we're in no hurry to restore the IOMMU state? I believe this could be GFP_KERNEL.
drivers/iommu/liveupdate.c line 137
Quoted 5 lines
drivers/iommu/liveupdate.c new file +5
-0+if (!obj) +return -ENOMEM ; + +mutex_init (&obj->lock); +BUG_ON (!kho_restore_folio (argp->data));
The use of BUG_ON in new code is heavily discouraged [1].
If KHO can't restore the folio for whatever reason, we can be treat it as a corruption of the handover data. I believe crashing the kernel for it would be an overkill?
Can we consider returning a graceful failure like -ENODATA or something? BUG_ON would instantly cause a kernel panic without providing no opportunity for the system to log the failure or attempt a graceful teardown of the 'preserved' mapping.
Quoted 86 lines
drivers/iommu/liveupdate.c new file +40
-0+ ser =phys_to_virt (argp->data); + obj->ser = ser; + +iommu_liveupdate_restore_objs (ser->iommu_domains_phys); + obj->iommu_domains =phys_to_virt (ser->iommu_domains_phys); + +iommu_liveupdate_restore_objs (ser->devices_phys); + obj->devices =phys_to_virt (ser->devices_phys); + +iommu_liveupdate_restore_objs (ser->iommus_phys); + obj->iommus =phys_to_virt (ser->iommus_phys); + + argp->obj = obj; + +return 0 ; +} + +static struct liveupdate_flb_ops iommu_flb_ops = { + .preserve = iommu_liveupdate_flb_preserve, + .unpreserve = iommu_liveupdate_flb_unpreserve, + .finish = iommu_liveupdate_flb_finish, + .retrieve = iommu_liveupdate_flb_retrieve, +}; + +static struct liveupdate_flb iommu_flb = { + .compatible =IOMMU_LUO_FLB_COMPATIBLE , + .ops = &iommu_flb_ops, +}; + +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler) +{ +return liveupdate_register_flb (handler, &iommu_flb); +} +EXPORT_SYMBOL (iommu_liveupdate_register_flb); + +int iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler) +{ +return liveupdate_unregister_flb (handler, &iommu_flb); +} +EXPORT_SYMBOL (iommu_liveupdate_unregister_flb);include/linux/iommu-lu.h new file +17
-0@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2025, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#ifndef _LINUX_IOMMU_LU_H +#define _LINUX_IOMMU_LU_H + +#include <linux/liveupdate.h> +#include <linux/kho/abi/iommu.h> + +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler); +int iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler); + +#endif /* _LINUX_IOMMU_LU_H */ include/linux/kho/abi/iommu.h new file +17
-0@@ -0,0 +1,119 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2025, 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 +*
include/linux/kho/abi/iommu.h line 27
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+* 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. +*/ + +#define IOMMU_LUO_FLB_COMPATIBLE "iommu-v1" +
Let's call this "iommu-liveupdate-v1" or "iommu-lu-v1" instead?
Quoted 81 lines
include/linux/kho/abi/iommu.h new file +81
-0+enum iommu_lu_type { +IOMMU_INVALID , +IOMMU_INTEL , +}; + +struct iommu_obj_ser { +u32 idx; +u32 ref_count; +u32 deleted:1 ; +u32 incoming:1 ; +} __packed; + +struct iommu_domain_ser { +struct iommu_obj_ser obj; +u64 top_table; +u64 top_level; +struct iommu_domain *restored_domain; +} __packed; + +struct device_domain_iommu_ser { +u32 did; +u64 domain_phys; +u64 iommu_phys; +} __packed; + +struct device_ser { +struct iommu_obj_ser obj; +u64 token; +u32 devid; +u32 pci_domain; +struct device_domain_iommu_ser domain_iommu_ser; +enum iommu_lu_type type; +} __packed; + +struct iommu_intel_ser { +u64 phys_addr; +u64 root_table; +} __packed; + +struct iommu_ser { +struct iommu_obj_ser obj; +u64 token; +enum iommu_lu_type type; +union { +struct iommu_intel_ser intel; + }; +} __packed; + +struct iommu_objs_ser { +u64 next_objs; +u64 nr_objs; +} __packed; + +struct iommus_ser { +struct iommu_objs_ser objs; +struct iommu_ser iommus[]; +} __packed; + +struct iommu_domains_ser { +struct iommu_objs_ser objs; +struct iommu_domain_ser iommu_domains[]; +} __packed; + +struct devices_ser { +struct iommu_objs_ser objs; +struct device_ser devices[]; +} __packed; + +#define MAX_IOMMU_SERS ((PAGE_SIZE - sizeof(struct iommus_ser)) / sizeof(struct iommu_ser)) +#define MAX_IOMMU_DOMAIN_SERS \ + ((PAGE_SIZE - sizeof(struct iommu_domains_ser)) / sizeof(struct iommu_domain_ser)) +#define MAX_DEVICE_SERS ((PAGE_SIZE - sizeof(struct devices_ser)) / sizeof(struct device_ser)) + +struct iommu_lu_flb_ser { +u64 iommus_phys; +u64 nr_iommus; +u64 iommu_domains_phys; +u64 nr_domains; +u64 devices_phys; +u64 nr_devices; +} __packed;
include/linux/kho/abi/iommu.h line 118
Quoted 10 lines
include/linux/kho/abi/iommu.h new file +10
-0+ +struct iommu_lu_flb_obj { +struct mutex lock; +struct iommu_lu_flb_ser *ser; + +struct iommu_domains_ser *iommu_domains; +struct iommus_ser *iommus; +struct devices_ser *devices; +} __packed; +
Please let's add some comments describing the structs & their members here like we have in memfd [2]. This should be descriptive for the user. For example:
+/**
+ * struct iommu_lu_flb_ser - Main serialization header for IOMMU state.
+ * @iommus_phys: Physical address of the first page in the IOMMU unit chain.
+ * @nr_iommus: Total number of hardware IOMMU units preserved.
+ * @iommu_domains_phys: [...]
+ * @nr_domains: [...] + * @devices_phys: [...] + * @nr_devices: [...]
+ *
+ * This structure acts as the root of the IOMMU state tree. It is hitching a ride
+ * on the iommufd file descriptor's preservation flow.
+ */
+struct iommu_lu_flb_ser {
+ u64 iommus_phys;
+ u64 nr_iommus;
+ u64 iommu_domains_phys;
+ u64 nr_domains;
+ u64 devices_phys;
+ u64 nr_devices;
+} __packed;
include/linux/kho/abi/iommu.h line 119
Quoted 1 line
include/linux/kho/abi/iommu.h new file +1
-0+#endif /* _LINUX_KHO_ABI_IOMMU_H */
Thanks,
Praan
[1] https://docs.kernel.org/process/coding-style.html#use-warn-rather-than-bug
[2] https://elixir.bootlin.com/linux/v7.0-rc3/source/include/linux/kho/abi/memfd.h
Re: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
Quoted 40 lines · Pranjal Shrivastava
Quoted 28 lines · Samiullah Khawaja
Add liveupdate FLB for IOMMU state preservation. Use KHO preserve memory alloc/free helper functions to allocate memory for the IOMMU LU FLB object and the serialization structs for device, domain and iommu.
During retrieve, walk through the preserved objs nodes and restore each folio. Also recreate the FLB obj.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/Kconfig | 11 +++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 177 ++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 17 ++++ include/linux/kho/abi/iommu.h | 119 +++++++++++++++++++++++ 5 files changed, 325 insertions(+) create mode 100644 drivers/iommu/liveupdate.c create mode 100644 include/linux/iommu-lu.h create mode 100644 include/linux/kho/abi/iommu.hdrivers/iommu/Kconfig +1
-0@@ -11,6 +11,17 @@ config IOMMUFD_DRIVER bool default n +config IOMMU_LIVEUPDATEdrivers/iommu/Kconfig config IOMMUFD_DRIVER · line 24
Quoted 10 lines
drivers/iommu/Kconfig +10
-0+ 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. +
Quoted 8 lines
I'm wondering if this should be under the if
IOMMU_SUPPORTbelow? I believe this was added here because IOMMUFD isn't underIOMMU_SUPPORT, but it wouldn't make sense to "preserve" IOMMU across a liveupdate ifIOMMU_SUPPORTis disabled? Should we probably be move it inside the ifIOMMU_SUPPORTblock for better organization, or at least have a depends onIOMMU_SUPPORTadded to it? TheIOMMU_LUOstill depends on theIOMMU_SUPPORTinfrastructure to actually function.. as we add calls within core functions likedev_iommu_getetc.
Agreed. I will move it under IOMMU_SUPPORT and sort out any other dependencies.
Quoted 20 lines
Quoted 20 lines
drivers/iommu/Kconfig +0
-0menuconfig IOMMU_SUPPORT bool "IOMMU Hardware Support" depends on MMUdrivers/iommu/Makefile +1
-0@@ -15,6 +15,7 @@ obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o 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_LIVEUPDATE) += liveupdate.o obj-$(CONFIG_IOMMU_IOVA) += iova.o obj-$(CONFIG_OF_IOMMU) += of_iommu.o obj-$(CONFIG_MSM_IOMMU) += msm_iommu.odrivers/iommu/liveupdate.c new file +0
-1--- /dev/null
Quoted 8 lines
drivers/iommu/liveupdate.c line 4
Quoted 6 lines
+++
b/drivers/iommu/liveupdate.cdrivers/iommu/liveupdate.c new file +4
-0@@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2025, Google LLC Minor nit: 2026 OR 2025-26, here and everywhere else
Will fix in next revision.
Quoted 15 lines
Quoted 15 lines
drivers/iommu/liveupdate.c new file +14
-0+* Author: Samiullah Khawaja <[email protected]> +*/ + +#define pr_fmt (fmt) "iommu: liveupdate: " fmt + +#include <linux/kexec_handover.h> +#include <linux/liveupdate.h> +#include <linux/iommu-lu.h> +#include <linux/iommu.h> +#include <linux/errno.h> + +static void iommu_liveupdate_restore_objs (u64 next) +{ +struct iommu_objs_ser *objs;
Quoted 8 lines
drivers/iommu/liveupdate.c line 21
Quoted 3 lines
drivers/iommu/liveupdate.c new file +3
-0+ +while (next) { +BUG_ON (!kho_restore_folio (next));Same thing about
BUG_ON[1] as mentioned below in theiommu_liveupdate_flb_retrieve()function, can we consider returning an error which can be checked in the caller and the error can be bubbled up as-ENODATA?
Please see the explanation below on the BUG_ON.
Quoted 43 lines
Quoted 8 lines
drivers/iommu/liveupdate.c new file +7
-0+ objs =__va (next); + next = objs->next_objs; + } +} + +static void iommu_liveupdate_free_objs (u64 next,bool incoming) +{drivers/iommu/liveupdate.c line 38
Quoted 10 lines
drivers/iommu/liveupdate.c new file +10
-0+struct iommu_objs_ser *objs; + +while (next) { + objs =__va (next); + next = objs->next_objs; + +if (!incoming) +kho_unpreserve_free (objs); +else +folio_put (virt_to_folio (objs));Interesting!
kho_restore_folioalready adjusts the refcount viaadjust_managed_page_countwhich is whyfolio_put()is needed for a valid refcount. Sweet :)Quoted 20 lines
drivers/iommu/liveupdate.c new file +20
-0+ } +} + +static void iommu_liveupdate_flb_free (struct iommu_lu_flb_obj *obj) +{ +if (obj->iommu_domains) +iommu_liveupdate_free_objs (obj->ser->iommu_domains_phys, false); + +if (obj->devices) +iommu_liveupdate_free_objs (obj->ser->devices_phys, false); + +if (obj->iommus) +iommu_liveupdate_free_objs (obj->ser->iommus_phys, false); + +kho_unpreserve_free (obj->ser); +kfree (obj); +} + +static int iommu_liveupdate_flb_preserve (struct liveupdate_flb_op_args *argp) +{
Quoted 8 lines
drivers/iommu/liveupdate.c line 63
Quoted 5 lines
drivers/iommu/liveupdate.c new file +5
-0+struct iommu_lu_flb_obj *obj; +struct iommu_lu_flb_ser *ser; +void *mem; + + obj =kzalloc (sizeof (*obj),GFP_KERNEL );I know this is obvious but let's add a comment that obj just exists in the "current" kernel whereas mem is supposed to survive the KHO..
Agreed. Will add in the next revision.
Quoted 82 lines
Quoted 60 lines
drivers/iommu/liveupdate.c new file +59
-0+if (!obj) +return -ENOMEM ; + +mutex_init (&obj->lock); + mem =kho_alloc_preserve (sizeof (*ser)); +if (IS_ERR (mem)) + goto err_free; + + ser = mem; + obj->ser = ser; + + mem =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (mem)) + goto err_free; + + obj->iommu_domains = mem; + ser->iommu_domains_phys =virt_to_phys (obj->iommu_domains); + + mem =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (mem)) + goto err_free; + + obj->devices = mem; + ser->devices_phys =virt_to_phys (obj->devices); + + mem =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (mem)) + goto err_free; + + obj->iommus = mem; + ser->iommus_phys =virt_to_phys (obj->iommus); + + argp->obj = obj; + argp->data =virt_to_phys (ser); +return 0 ; + +err_free: +iommu_liveupdate_flb_free (obj); +return PTR_ERR (mem); +} + +static void iommu_liveupdate_flb_unpreserve (struct liveupdate_flb_op_args *argp) +{ +iommu_liveupdate_flb_free (argp->obj); +} + +static void iommu_liveupdate_flb_finish (struct liveupdate_flb_op_args *argp) +{ +struct iommu_lu_flb_obj *obj = argp->obj; + +if (obj->iommu_domains) +iommu_liveupdate_free_objs (obj->ser->iommu_domains_phys, true); + +if (obj->devices) +iommu_liveupdate_free_objs (obj->ser->devices_phys, true); + +if (obj->iommus) +iommu_liveupdate_free_objs (obj->ser->iommus_phys, true); +drivers/iommu/liveupdate.c line 132
Quoted 10 lines
drivers/iommu/liveupdate.c new file +10
-0+folio_put (virt_to_folio (obj->ser)); +kfree (obj); +} + +static int iommu_liveupdate_flb_retrieve (struct liveupdate_flb_op_args *argp) +{ +struct iommu_lu_flb_obj *obj; +struct iommu_lu_flb_ser *ser; + + obj =kzalloc (sizeof (*obj),GFP_ATOMIC );Why does this have to be
GFP_ATOMIC? IIUC, the retrieve path is triggered by a userspace IOCTL in the new kernel. The system should be able to sleep here? (unless we have a use-case to call this in IRQ-ctx?) AFAICT, we call this under mutexes already, hence there's no situation where we could sleep in a spinlock context?
GFP_ATOMICcreates a point of failure if the system is under memory pressure. I believe we should be allowed to sleep for this allocation because the "preserved" mappings still allow DMAs to go on and we're in no hurry to restore the IOMMU state? I believe this could beGFP_KERNEL.
Quoted 10 lines
drivers/iommu/liveupdate.c line 137
Quoted 5 lines
drivers/iommu/liveupdate.c new file +5
-0+if (!obj) +return -ENOMEM ; + +mutex_init (&obj->lock); +BUG_ON (!kho_restore_folio (argp->data));The use of
BUG_ONin new code is heavily discouraged [1].
If KHO can't restore the folio for whatever reason, we can be treat it as a corruption of the handover data. I believe crashing the kernel for it would be an overkill?
The FLB restore is done during early boot and this has been discussed in past in KHO/LUO and IOMMU context also. But basically if this fails, the restoration of IOMMU state cannot be done, preserved devices would already have corrupted memory due to ongoing DMA as we didn't disable translation in the previous kernel. So logging an error and a BUG_ON at this point would be most appropriate.
Please see discussion here on this topic:
https://lore.kernel.org/all/[email protected]/
Quoted 5 lines
Can we consider returning a graceful failure like
-ENODATAor something?BUG_ONwould instantly cause a kernel panic without providing no opportunity for the system to log the failure or attempt a graceful teardown of the 'preserved' mapping.
I will update this by adding a comment and also log an error.
Quoted 91 lines
Quoted 91 lines
drivers/iommu/liveupdate.c new file +40
-0+ ser =phys_to_virt (argp->data); + obj->ser = ser; + +iommu_liveupdate_restore_objs (ser->iommu_domains_phys); + obj->iommu_domains =phys_to_virt (ser->iommu_domains_phys); + +iommu_liveupdate_restore_objs (ser->devices_phys); + obj->devices =phys_to_virt (ser->devices_phys); + +iommu_liveupdate_restore_objs (ser->iommus_phys); + obj->iommus =phys_to_virt (ser->iommus_phys); + + argp->obj = obj; + +return 0 ; +} + +static struct liveupdate_flb_ops iommu_flb_ops = { + .preserve = iommu_liveupdate_flb_preserve, + .unpreserve = iommu_liveupdate_flb_unpreserve, + .finish = iommu_liveupdate_flb_finish, + .retrieve = iommu_liveupdate_flb_retrieve, +}; + +static struct liveupdate_flb iommu_flb = { + .compatible =IOMMU_LUO_FLB_COMPATIBLE , + .ops = &iommu_flb_ops, +}; + +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler) +{ +return liveupdate_register_flb (handler, &iommu_flb); +} +EXPORT_SYMBOL (iommu_liveupdate_register_flb); + +int iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler) +{ +return liveupdate_unregister_flb (handler, &iommu_flb); +} +EXPORT_SYMBOL (iommu_liveupdate_unregister_flb);include/linux/iommu-lu.h new file +17
-0@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2025, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#ifndef _LINUX_IOMMU_LU_H +#define _LINUX_IOMMU_LU_H + +#include <linux/liveupdate.h> +#include <linux/kho/abi/iommu.h> + +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler); +int iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler); + +#endif /* _LINUX_IOMMU_LU_H */ include/linux/kho/abi/iommu.h new file +21
-0@@ -0,0 +1,119 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2025, 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
Quoted 8 lines
include/linux/kho/abi/iommu.h line 27
Quoted 6 lines
include/linux/kho/abi/iommu.h new file +6
-0+* structs defined here constitutes a breaking change. Such changes require +* incrementing the version number in the IOMMU_LUO_FLB_COMPATIBLE string. +*/ + +#define IOMMU_LUO_FLB_COMPATIBLE "iommu-v1" +Let's call this "iommu-liveupdate-v1" or "iommu-lu-v1" instead?
Agreed.
Quoted 88 lines
Quoted 88 lines
include/linux/kho/abi/iommu.h new file +87
-0+enum iommu_lu_type { +IOMMU_INVALID , +IOMMU_INTEL , +}; + +struct iommu_obj_ser { +u32 idx; +u32 ref_count; +u32 deleted:1 ; +u32 incoming:1 ; +} __packed; + +struct iommu_domain_ser { +struct iommu_obj_ser obj; +u64 top_table; +u64 top_level; +struct iommu_domain *restored_domain; +} __packed; + +struct device_domain_iommu_ser { +u32 did; +u64 domain_phys; +u64 iommu_phys; +} __packed; + +struct device_ser { +struct iommu_obj_ser obj; +u64 token; +u32 devid; +u32 pci_domain; +struct device_domain_iommu_ser domain_iommu_ser; +enum iommu_lu_type type; +} __packed; + +struct iommu_intel_ser { +u64 phys_addr; +u64 root_table; +} __packed; + +struct iommu_ser { +struct iommu_obj_ser obj; +u64 token; +enum iommu_lu_type type; +union { +struct iommu_intel_ser intel; + }; +} __packed; + +struct iommu_objs_ser { +u64 next_objs; +u64 nr_objs; +} __packed; + +struct iommus_ser { +struct iommu_objs_ser objs; +struct iommu_ser iommus[]; +} __packed; + +struct iommu_domains_ser { +struct iommu_objs_ser objs; +struct iommu_domain_ser iommu_domains[]; +} __packed; + +struct devices_ser { +struct iommu_objs_ser objs; +struct device_ser devices[]; +} __packed; + +#define MAX_IOMMU_SERS ((PAGE_SIZE - sizeof(struct iommus_ser)) / sizeof(struct iommu_ser)) +#define MAX_IOMMU_DOMAIN_SERS \ + ((PAGE_SIZE - sizeof(struct iommu_domains_ser)) / sizeof(struct iommu_domain_ser)) +#define MAX_DEVICE_SERS ((PAGE_SIZE - sizeof(struct devices_ser)) / sizeof(struct device_ser)) + +struct iommu_lu_flb_ser { +u64 iommus_phys; +u64 nr_iommus; +u64 iommu_domains_phys; +u64 nr_domains; +u64 devices_phys; +u64 nr_devices; +} __packed; + +struct iommu_lu_flb_obj { +struct mutex lock; +struct iommu_lu_flb_ser *ser; + +struct iommu_domains_ser *iommu_domains;
Quoted 8 lines
include/linux/kho/abi/iommu.h line 118
Quoted 4 lines
include/linux/kho/abi/iommu.h new file +4
-0+struct iommus_ser *iommus; +struct devices_ser *devices; +} __packed; +Please let's add some comments describing the structs & their members here like we have in memfd [2]. This should be descriptive for the user. For example:
I agree. Will add comments for these and others.
Quoted 22 lines
(quoted diff) +20
-0+/** + * struct iommu_lu_flb_ser - Main serialization header for IOMMU state. + * @iommus_phys: Physical address of the first page in the IOMMU unit chain. + * @nr_iommus: Total number of hardware IOMMU units preserved. + * @iommu_domains_phys: [...] + * @nr_domains: [...] + * @devices_phys: [...] + * @nr_devices: [...] + * + * This structure acts as the root of the IOMMU state tree. It is hitching a ride + * on the iommufd file descriptor's preservation flow. + */ +struct iommu_lu_flb_ser { + u64 iommus_phys; + u64 nr_iommus; + u64 iommu_domains_phys; + u64 nr_domains; + u64 devices_phys; + u64 nr_devices; +} __packed;
Quoted 8 lines
include/linux/kho/abi/iommu.h line 119
Quoted 1 line
include/linux/kho/abi/iommu.h new file +1
-0+#endif /* _LINUX_KHO_ABI_IOMMU_H */ Thanks,
Praan[1] https://docs.kernel.org/process/coding-style.html#use-warn-rather-than-bug
[2] https://elixir.bootlin.com/linux/v7.0-rc3/source/include/linux/kho/abi/memfd.h
Thanks for the review.
Sami
Re: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
Quoted 106 lines · Samiullah Khawaja
Quoted 40 lines · Pranjal Shrivastava
Quoted 28 lines · Samiullah Khawaja
Add liveupdate FLB for IOMMU state preservation. Use KHO preserve memory alloc/free helper functions to allocate memory for the IOMMU LU FLB object and the serialization structs for device, domain and iommu.
During retrieve, walk through the preserved objs nodes and restore each folio. Also recreate the FLB obj.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/Kconfig | 11 +++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 177 ++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 17 ++++ include/linux/kho/abi/iommu.h | 119 +++++++++++++++++++++++ 5 files changed, 325 insertions(+) create mode 100644 drivers/iommu/liveupdate.c create mode 100644 include/linux/iommu-lu.h create mode 100644 include/linux/kho/abi/iommu.hdrivers/iommu/Kconfig +1
-0@@ -11,6 +11,17 @@ config IOMMUFD_DRIVER bool default n +config IOMMU_LIVEUPDATEdrivers/iommu/Kconfig config IOMMUFD_DRIVER · line 24
Quoted 10 lines
drivers/iommu/Kconfig +10
-0+ 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. +Quoted 8 lines
I'm wondering if this should be under the if
IOMMU_SUPPORTbelow? I believe this was added here because IOMMUFD isn't underIOMMU_SUPPORT, but it wouldn't make sense to "preserve" IOMMU across a liveupdate ifIOMMU_SUPPORTis disabled? Should we probably be move it inside the ifIOMMU_SUPPORTblock for better organization, or at least have a depends onIOMMU_SUPPORTadded to it? TheIOMMU_LUOstill depends on theIOMMU_SUPPORTinfrastructure to actually function.. as we add calls within core functions likedev_iommu_getetc.Agreed. I will move it under
IOMMU_SUPPORTand sort out any other dependencies.Quoted 20 lines
Quoted 20 lines
drivers/iommu/Kconfig +0
-0menuconfig IOMMU_SUPPORT bool "IOMMU Hardware Support" depends on MMUdrivers/iommu/Makefile +1
-0@@ -15,6 +15,7 @@ obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o 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_LIVEUPDATE) += liveupdate.o obj-$(CONFIG_IOMMU_IOVA) += iova.o obj-$(CONFIG_OF_IOMMU) += of_iommu.o obj-$(CONFIG_MSM_IOMMU) += msm_iommu.odrivers/iommu/liveupdate.c new file +0
-1--- /dev/nullQuoted 8 lines
drivers/iommu/liveupdate.c line 4
Quoted 6 lines
+++
b/drivers/iommu/liveupdate.cdrivers/iommu/liveupdate.c new file +4
-0@@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2025, Google LLC Minor nit: 2026 OR 2025-26, here and everywhere else
Will fix in next revision.
Quoted 15 lines
Quoted 15 lines
drivers/iommu/liveupdate.c new file +14
-0+* Author: Samiullah Khawaja <[email protected]> +*/ + +#define pr_fmt (fmt) "iommu: liveupdate: " fmt + +#include <linux/kexec_handover.h> +#include <linux/liveupdate.h> +#include <linux/iommu-lu.h> +#include <linux/iommu.h> +#include <linux/errno.h> + +static void iommu_liveupdate_restore_objs (u64 next) +{ +struct iommu_objs_ser *objs;Quoted 8 lines
drivers/iommu/liveupdate.c line 21
Quoted 3 lines
drivers/iommu/liveupdate.c new file +3
-0+ +while (next) { +BUG_ON (!kho_restore_folio (next));Same thing about
BUG_ON[1] as mentioned below in theiommu_liveupdate_flb_retrieve()function, can we consider returning an error which can be checked in the caller and the error can be bubbled up as-ENODATA?
Quoted 1 line
Please see the explanation below on the
BUG_ON.
[------- snip >8 --------]
Quoted 14 lines
Quoted 14 lines
drivers/iommu/liveupdate.c line 132
Quoted 7 lines
drivers/iommu/liveupdate.c new file +7
-0+ +static int iommu_liveupdate_flb_retrieve (struct liveupdate_flb_op_args *argp) +{ +struct iommu_lu_flb_obj *obj; +struct iommu_lu_flb_ser *ser; + + obj =kzalloc (sizeof (*obj),GFP_ATOMIC );Why does this have to be
GFP_ATOMIC? IIUC, the retrieve path is triggered by a userspace IOCTL in the new kernel. The system should be able to sleep here? (unless we have a use-case to call this in IRQ-ctx?) AFAICT, we call this under mutexes already, hence there's no situation where we could sleep in a spinlock context?
Quoted 5 lines
Quoted 5 lines
GFP_ATOMICcreates a point of failure if the system is under memory pressure. I believe we should be allowed to sleep for this allocation because the "preserved" mappings still allow DMAs to go on and we're in no hurry to restore the IOMMU state? I believe this could beGFP_KERNEL.
I guess we missed discussing this comment about s/GFP_ATOMIC/GFP_KERNEL?
Quoted 18 lines
Quoted 10 lines
drivers/iommu/liveupdate.c line 137
Quoted 5 lines
drivers/iommu/liveupdate.c new file +5
-0+if (!obj) +return -ENOMEM ; + +mutex_init (&obj->lock); +BUG_ON (!kho_restore_folio (argp->data));The use of
BUG_ONin new code is heavily discouraged [1].
If KHO can't restore the folio for whatever reason, we can be treat it as a corruption of the handover data. I believe crashing the kernel for it would be an overkill?The FLB restore is done during early boot and this has been discussed in past in KHO/LUO and IOMMU context also. But basically if this fails, the restoration of IOMMU state cannot be done, preserved devices would already have corrupted memory due to ongoing DMA as we didn't disable translation in the previous kernel. So logging an error and a
BUG_ONat this point would be most appropriate.
Quoted 2 lines
Please see discussion here on this topic:
https://lore.kernel.org/all/[email protected]/
I see.. so a failure here would mean an entire VM tear-down?
Quoted 7 lines
Quoted 5 lines
Can we consider returning a graceful failure like
-ENODATAor something?BUG_ONwould instantly cause a kernel panic without providing no opportunity for the system to log the failure or attempt a graceful teardown of the 'preserved' mapping.I will update this by adding a comment and also log an error.
Ack. Thanks
[ ---- snip >8 -----]
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/iommu/liveupdate.c new file +13
-0+enum iommu_lu_type { +IOMMU_INVALID , +IOMMU_INTEL , +}; + +struct iommu_obj_ser { +u32 idx; +u32 ref_count; +u32 deleted:1 ; +u32 incoming:1 ; +} __packed; + +struct iommu_domain_ser {
Quoted 8 lines
Quoted 8 lines
drivers/iommu/liveupdate.c line 54
Quoted 8 lines
drivers/iommu/liveupdate.c new file +8
-0+struct iommu_obj_ser obj; +u64 top_table; +u64 top_level; +struct iommu_domain *restored_domain; +} __packed; + +struct device_domain_iommu_ser { +u32 did;
Nit: did sounds intel-specific, we can either call it something generic or make it into a union when we support other archs in the future.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
drivers/iommu/liveupdate.c new file +38
-0+u64 domain_phys; +u64 iommu_phys; +} __packed; + +struct device_ser { +struct iommu_obj_ser obj; +u64 token; +u32 devid; +u32 pci_domain; +struct device_domain_iommu_ser domain_iommu_ser; +enum iommu_lu_type type; +} __packed; + +struct iommu_intel_ser { +u64 phys_addr; +u64 root_table; +} __packed; + +struct iommu_ser { +struct iommu_obj_ser obj; +u64 token; +enum iommu_lu_type type; +union { +struct iommu_intel_ser intel; + }; +} __packed; + +struct iommu_objs_ser { +u64 next_objs; +u64 nr_objs; +} __packed; + +struct iommus_ser { +struct iommu_objs_ser objs; +struct iommu_ser iommus[]; +} __packed; + +struct iommu_domains_ser {
Quoted 8 lines
Quoted 8 lines
drivers/iommu/liveupdate.c line 100
Quoted 8 lines
drivers/iommu/liveupdate.c new file +8
-0+struct iommu_objs_ser objs; +struct iommu_domain_ser iommu_domains[]; +} __packed; + +struct devices_ser { +struct iommu_objs_ser objs; +struct device_ser devices[]; +} __packed;
I have a bone to pick here about the naming LOL, the names are kinda confusing and make the code unreadable, I had to keep re-visitng this patch while looking at the subsequent ones to understand what's going on..
One suggestion is to add a graphic that could help understand the layout like:
----------------------------------------------------------------------
[ PAGE START ] |
----------------------------------------------------------------------
| iommu_objs_ser (The Page Header) | | - next_objs: 0x0000 (End of the page-chain) | | - nr_objs: 2 | ---------------------------------------------------------------------- | ITEM 0: iommu_domain_ser | | [ iommu_obj_ser (The entry header) ] | | - idx: 0 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ITEM 1: iommu_domain_ser | | [ iommu_obj_ser (The Price Tag) ] | | - idx: 1 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ... (Empty space for more domains) ... | | |
----------------------------------------------------------------------
[ PAGE END ] |
----------------------------------------------------------------------
Additionally, a few naming suggestions here:
1. struct iommu_obj_ser -> struct iommu_ser_entry_hdr
2. struct iommu_objs_ser -> struct iommu_ser_page_hdr
3. struct iommu_domains_ser -> struct iommu_ser_domain_page
This makes things clearer:
struct iommu_ser_page_hdr {
u64 next_page_phys;
u64 entry_count;
} __packed;/* The Container Page */
struct iommu_ser_domain_page {
struct iommu_ser_page_hdr hdr;
struct iommu_ser_domain domain_entries[];
} __packed;Similarly, something like:
4. struct devices_ser -> struct iommu_ser_device_page
5. struct iommu_lu_flb_ser -> struct iommu_flb_metadata
6. struct iommu_lu_flb_obj -> struct iommu_flb_ctx
struct iommu_flb_ctx {
struct mutex lock;
struct iommu_flb_metadata *cookie;
struct iommu_ser_domain_page *curr_domains_page;
struct iommu_ser_iommu_ctx_page *curr_iommu_ctx_page;
struct iommu_ser_device_page *curr_devices_page;
} __packed;Makes things slightly more readable.
Quoted 2 lines
Quoted 2 lines
drivers/iommu/liveupdate.c line 102
Quoted 2 lines
drivers/iommu/liveupdate.c new file +2
-0+ +#define MAX_IOMMU_SERS ((PAGE_SIZE - sizeof(struct iommus_ser)) / sizeof(struct iommu_ser))
Nit: For clarity, can we consider adding another set of braces:
+#define MAX_IOMMU_SERS ((PAGE_SIZE - (sizeof(struct iommus_ser)) / sizeof(struct iommu_ser)))
Quoted 55 lines
Quoted 18 lines
Quoted 18 lines
drivers/iommu/liveupdate.c new file +18
-0+#define MAX_IOMMU_DOMAIN_SERS \ + ((PAGE_SIZE - sizeof(struct iommu_domains_ser)) / sizeof(struct iommu_domain_ser)) +#define MAX_DEVICE_SERS ((PAGE_SIZE - sizeof(struct devices_ser)) / sizeof(struct device_ser)) + +struct iommu_lu_flb_ser { +u64 iommus_phys; +u64 nr_iommus; +u64 iommu_domains_phys; +u64 nr_domains; +u64 devices_phys; +u64 nr_devices; +} __packed; + +struct iommu_lu_flb_obj { +struct mutex lock; +struct iommu_lu_flb_ser *ser; + +struct iommu_domains_ser *iommu_domains;Quoted 8 lines
drivers/iommu/liveupdate.c line 124
Quoted 4 lines
drivers/iommu/liveupdate.c new file +4
-0+struct iommus_ser *iommus; +struct devices_ser *devices; +} __packed; +Please let's add some comments describing the structs & their members here like we have in memfd [2]. This should be descriptive for the user. For example:
I agree. Will add comments for these and others.
Quoted 27 lines
(quoted diff) +20
-0+/** + * struct iommu_lu_flb_ser - Main serialization header for IOMMU state. + * @iommus_phys: Physical address of the first page in the IOMMU unit chain. + * @nr_iommus: Total number of hardware IOMMU units preserved. + * @iommu_domains_phys: [...] + * @nr_domains: [...] + * @devices_phys: [...] + * @nr_devices: [...] + * + * This structure acts as the root of the IOMMU state tree. It is hitching a ride + * on the iommufd file descriptor's preservation flow. + */ +struct iommu_lu_flb_ser { + u64 iommus_phys; + u64 nr_iommus; + u64 iommu_domains_phys; + u64 nr_domains; + u64 devices_phys; + u64 nr_devices; +} __packed;drivers/iommu/liveupdate.c line 125
Quoted 2 lines
drivers/iommu/liveupdate.c new file +1
-0+#endif /* _LINUX_KHO_ABI_IOMMU_H */ Thanks,
Praan
Quoted 7 lines
Quoted 3 lines
[1] https://docs.kernel.org/process/coding-style.html#use-warn-rather-than-bug
[2] https://elixir.bootlin.com/linux/v7.0-rc3/source/include/linux/kho/abi/memfd.hThanks for the review.
Sami
Thanks,
Praan
Re: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
Quoted 125 lines · Pranjal Shrivastava
Quoted 106 lines · Samiullah Khawaja
Quoted 40 lines · Pranjal Shrivastava
Quoted 28 lines · Samiullah Khawaja
Add liveupdate FLB for IOMMU state preservation. Use KHO preserve memory alloc/free helper functions to allocate memory for the IOMMU LU FLB object and the serialization structs for device, domain and iommu.
During retrieve, walk through the preserved objs nodes and restore each folio. Also recreate the FLB obj.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/Kconfig | 11 +++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 177 ++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 17 ++++ include/linux/kho/abi/iommu.h | 119 +++++++++++++++++++++++ 5 files changed, 325 insertions(+) create mode 100644 drivers/iommu/liveupdate.c create mode 100644 include/linux/iommu-lu.h create mode 100644 include/linux/kho/abi/iommu.hdrivers/iommu/Kconfig +1
-0@@ -11,6 +11,17 @@ config IOMMUFD_DRIVER bool default n +config IOMMU_LIVEUPDATEdrivers/iommu/Kconfig config IOMMUFD_DRIVER · line 24
Quoted 10 lines
drivers/iommu/Kconfig +10
-0+ 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. +Quoted 8 lines
I'm wondering if this should be under the if
IOMMU_SUPPORTbelow? I believe this was added here because IOMMUFD isn't underIOMMU_SUPPORT, but it wouldn't make sense to "preserve" IOMMU across a liveupdate ifIOMMU_SUPPORTis disabled? Should we probably be move it inside the ifIOMMU_SUPPORTblock for better organization, or at least have a depends onIOMMU_SUPPORTadded to it? TheIOMMU_LUOstill depends on theIOMMU_SUPPORTinfrastructure to actually function.. as we add calls within core functions likedev_iommu_getetc.Agreed. I will move it under
IOMMU_SUPPORTand sort out any other dependencies.Quoted 20 lines
Quoted 20 lines
drivers/iommu/Kconfig +0
-0menuconfig IOMMU_SUPPORT bool "IOMMU Hardware Support" depends on MMUdrivers/iommu/Makefile +1
-0@@ -15,6 +15,7 @@ obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o 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_LIVEUPDATE) += liveupdate.o obj-$(CONFIG_IOMMU_IOVA) += iova.o obj-$(CONFIG_OF_IOMMU) += of_iommu.o obj-$(CONFIG_MSM_IOMMU) += msm_iommu.odrivers/iommu/liveupdate.c new file +0
-1--- /dev/nullQuoted 8 lines
drivers/iommu/liveupdate.c line 4
Quoted 6 lines
+++
b/drivers/iommu/liveupdate.cdrivers/iommu/liveupdate.c new file +4
-0@@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2025, Google LLC Minor nit: 2026 OR 2025-26, here and everywhere else
Will fix in next revision.
Quoted 15 lines
Quoted 15 lines
drivers/iommu/liveupdate.c new file +14
-0+* Author: Samiullah Khawaja <[email protected]> +*/ + +#define pr_fmt (fmt) "iommu: liveupdate: " fmt + +#include <linux/kexec_handover.h> +#include <linux/liveupdate.h> +#include <linux/iommu-lu.h> +#include <linux/iommu.h> +#include <linux/errno.h> + +static void iommu_liveupdate_restore_objs (u64 next) +{ +struct iommu_objs_ser *objs;Quoted 8 lines
drivers/iommu/liveupdate.c line 21
Quoted 3 lines
drivers/iommu/liveupdate.c new file +3
-0+ +while (next) { +BUG_ON (!kho_restore_folio (next));Same thing about
BUG_ON[1] as mentioned below in theiommu_liveupdate_flb_retrieve()function, can we consider returning an error which can be checked in the caller and the error can be bubbled up as-ENODATA?Quoted 1 line
Please see the explanation below on the
BUG_ON.[------- snip >8 --------]
Quoted 14 lines
Quoted 14 lines
drivers/iommu/liveupdate.c line 132
Quoted 7 lines
drivers/iommu/liveupdate.c new file +7
-0+ +static int iommu_liveupdate_flb_retrieve (struct liveupdate_flb_op_args *argp) +{ +struct iommu_lu_flb_obj *obj; +struct iommu_lu_flb_ser *ser; + + obj =kzalloc (sizeof (*obj),GFP_ATOMIC );Why does this have to be
GFP_ATOMIC? IIUC, the retrieve path is triggered by a userspace IOCTL in the new kernel. The system should be able to sleep here? (unless we have a use-case to call this in IRQ-ctx?) AFAICT, we call this under mutexes already, hence there's no situation where we could sleep in a spinlock context?
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
GFP_ATOMICcreates a point of failure if the system is under memory pressure. I believe we should be allowed to sleep for this allocation because the "preserved" mappings still allow DMAs to go on and we're in no hurry to restore the IOMMU state? I believe this could beGFP_KERNEL.I guess we missed discussing this comment about s/GFP_ATOMIC/GFP_KERNEL?
I think this can be updated to GFP_KERNEL. My original reason of GFP_ATOMIC was since the flb_retrieve is also called during boot in driver init and vt-d seems to be using GFP_ATOMIC for root table initialization. Looking at those code paths, it seems none of it is in IRQ ctx. Same goes for other drivers.
I will update this.
Quoted 19 lines
Quoted 19 lines
Quoted 11 lines
drivers/iommu/liveupdate.c line 137
Quoted 6 lines
drivers/iommu/liveupdate.c new file +5
-0+if (!obj) +return -ENOMEM ; + +mutex_init (&obj->lock); +BUG_ON (!kho_restore_folio (argp->data));The use of
BUG_ONin new code is heavily discouraged [1].
If KHO can't restore the folio for whatever reason, we can be treat it as a corruption of the handover data. I believe crashing the kernel for it would be an overkill?The FLB restore is done during early boot and this has been discussed in past in KHO/LUO and IOMMU context also. But basically if this fails, the restoration of IOMMU state cannot be done, preserved devices would already have corrupted memory due to ongoing DMA as we didn't disable translation in the previous kernel. So logging an error and a
BUG_ONat this point would be most appropriate.
Quoted 4 lines
Quoted 2 lines
Please see discussion here on this topic:
https://lore.kernel.org/all/[email protected]/I see.. so a failure here would mean an entire VM tear-down?
Yes a corrupted FLB/KHO would make the incoming kernel unable to restore IOMMU state. This indicates a memory corruption and the devices might have corrupted kernel and/or VM memory.
Quoted 30 lines
Quoted 8 lines
Quoted 6 lines
Can we consider returning a graceful failure like
-ENODATAor something?BUG_ONwould instantly cause a kernel panic without providing no opportunity for the system to log the failure or attempt a graceful teardown of the 'preserved' mapping.I will update this by adding a comment and also log an error.
Ack. Thanks
[ ---- snip >8 -----]
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
drivers/iommu/liveupdate.c new file +17
-0+enum iommu_lu_type { +IOMMU_INVALID , +IOMMU_INTEL , +}; + +struct iommu_obj_ser { +u32 idx; +u32 ref_count; +u32 deleted:1 ; +u32 incoming:1 ; +} __packed; + +struct iommu_domain_ser { +struct iommu_obj_ser obj; +u64 top_table; +u64 top_level; +struct iommu_domain *restored_domain;
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
drivers/iommu/liveupdate.c line 54
Quoted 4 lines
drivers/iommu/liveupdate.c new file +4
-0+} __packed; + +struct device_domain_iommu_ser { +u32 did;Nit:
didsounds intel-specific, we can either call it something generic or make it into a union when we support other archs in the future.
Yes, I will change this to a union like done at other places.
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
drivers/iommu/liveupdate.c new file +40
-0+u64 domain_phys; +u64 iommu_phys; +} __packed; + +struct device_ser { +struct iommu_obj_ser obj; +u64 token; +u32 devid; +u32 pci_domain; +struct device_domain_iommu_ser domain_iommu_ser; +enum iommu_lu_type type; +} __packed; + +struct iommu_intel_ser { +u64 phys_addr; +u64 root_table; +} __packed; + +struct iommu_ser { +struct iommu_obj_ser obj; +u64 token; +enum iommu_lu_type type; +union { +struct iommu_intel_ser intel; + }; +} __packed; + +struct iommu_objs_ser { +u64 next_objs; +u64 nr_objs; +} __packed; + +struct iommus_ser { +struct iommu_objs_ser objs; +struct iommu_ser iommus[]; +} __packed; + +struct iommu_domains_ser { +struct iommu_objs_ser objs; +struct iommu_domain_ser iommu_domains[];
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
drivers/iommu/liveupdate.c line 100
Quoted 6 lines
drivers/iommu/liveupdate.c new file +6
-0+} __packed; + +struct devices_ser { +struct iommu_objs_ser objs; +struct device_ser devices[]; +} __packed;I have a bone to pick here about the naming LOL, the names are kinda
LOL
Quoted 30 lines
confusing and make the code unreadable, I had to keep re-visitng this patch while looking at the subsequent ones to understand what's going on..
One suggestion is to add a graphic that could help understand the layout like:
----------------------------------------------------------------------
[ PAGE START ] |
----------------------------------------------------------------------| iommu_objs_ser (The Page Header) | | - next_objs: 0x0000 (End of the page-chain) | | - nr_objs: 2 | ---------------------------------------------------------------------- | ITEM 0: iommu_domain_ser | | [ iommu_obj_ser (The entry header) ] | | - idx: 0 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ITEM 1: iommu_domain_ser | | [ iommu_obj_ser (The Price Tag) ] | | - idx: 1 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ... (Empty space for more domains) ... | | |
Quoted 3 lines
----------------------------------------------------------------------
[ PAGE END ] |
----------------------------------------------------------------------
I think this is a good idea. I can add a diagram like this, but maybe just for objs as all the preserved state is built on top of it.
Quoted 6 lines
Additionally, a few naming suggestions here:
1. struct
iommu_obj_ser-> structiommu_ser_entry_hdr
2. structiommu_objs_ser-> structiommu_ser_page_hdr
3. structiommu_domains_ser-> structiommu_ser_domain_page
The hdr and page stuff in the ser name will be super confusing. Those details are part of the objs->obj construction and I think with the diagram you suggested above and some comments with explanation, the objs->obj can be demistified.
Quoted 29 lines
This makes things clearer:
struct iommu_ser_page_hdr { u64 next_page_phys; u64 entry_count; } __packed;/* The Container Page */
struct iommu_ser_domain_page { struct iommu_ser_page_hdr hdr; struct iommu_ser_domain domain_entries[]; } __packed;Similarly, something like:
4. struct
devices_ser-> structiommu_ser_device_page
5. structiommu_lu_flb_ser-> structiommu_flb_metadata
6. structiommu_lu_flb_obj-> structiommu_flb_ctxstruct iommu_flb_ctx { struct mutex lock; struct iommu_flb_metadata *cookie; struct iommu_ser_domain_page *curr_domains_page; struct iommu_ser_iommu_ctx_page *curr_iommu_ctx_page; struct iommu_ser_device_page *curr_devices_page; } __packed;
Quoted 8 lines
Makes things slightly more readable.
Quoted 2 lines
Quoted 2 lines
drivers/iommu/liveupdate.c line 102
Quoted 2 lines
drivers/iommu/liveupdate.c new file +2
-0+ +#define MAX_IOMMU_SERS ((PAGE_SIZE - sizeof(struct iommus_ser)) / sizeof(struct iommu_ser))Nit: For clarity, can we consider adding another set of braces:
+#define
MAX_IOMMU_SERS((PAGE_SIZE- (sizeof(struct iommus_ser)) /sizeof(struct iommu_ser)))
Agreed.
Quoted 60 lines
Quoted 60 lines
Quoted 19 lines
Quoted 19 lines
drivers/iommu/liveupdate.c new file +18
-0+#define MAX_IOMMU_DOMAIN_SERS \ + ((PAGE_SIZE - sizeof(struct iommu_domains_ser)) / sizeof(struct iommu_domain_ser)) +#define MAX_DEVICE_SERS ((PAGE_SIZE - sizeof(struct devices_ser)) / sizeof(struct device_ser)) + +struct iommu_lu_flb_ser { +u64 iommus_phys; +u64 nr_iommus; +u64 iommu_domains_phys; +u64 nr_domains; +u64 devices_phys; +u64 nr_devices; +} __packed; + +struct iommu_lu_flb_obj { +struct mutex lock; +struct iommu_lu_flb_ser *ser; + +struct iommu_domains_ser *iommu_domains;Quoted 8 lines
drivers/iommu/liveupdate.c line 124
Quoted 4 lines
drivers/iommu/liveupdate.c new file +4
-0+struct iommus_ser *iommus; +struct devices_ser *devices; +} __packed; +Please let's add some comments describing the structs & their members here like we have in memfd [2]. This should be descriptive for the user. For example:
I agree. Will add comments for these and others.
Quoted 30 lines
(quoted diff) +20
-0+/** + * struct iommu_lu_flb_ser - Main serialization header for IOMMU state. + * @iommus_phys: Physical address of the first page in the IOMMU unit chain. + * @nr_iommus: Total number of hardware IOMMU units preserved. + * @iommu_domains_phys: [...] + * @nr_domains: [...] + * @devices_phys: [...] + * @nr_devices: [...] + * + * This structure acts as the root of the IOMMU state tree. It is hitching a ride + * on the iommufd file descriptor's preservation flow. + */ +struct iommu_lu_flb_ser { + u64 iommus_phys; + u64 nr_iommus; + u64 iommu_domains_phys; + u64 nr_domains; + u64 devices_phys; + u64 nr_devices; +} __packed;drivers/iommu/liveupdate.c line 125
Quoted 2 lines
drivers/iommu/liveupdate.c new file +1
-0+#endif /* _LINUX_KHO_ABI_IOMMU_H */ Thanks,
Praan[1] https://docs.kernel.org/process/coding-style.html#use-warn-rather-than-bug
[2] https://elixir.bootlin.com/linux/v7.0-rc3/source/include/linux/kho/abi/memfd.h
Quoted 6 lines
Quoted 3 lines
Thanks for the review.
Sami
Thanks,
Praan
Thanks,
Sami
Re: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
In reply to Samiullah KhawajaQuoted 21 lines · Samiullah Khawaja
Quoted 20 lines · Pranjal Shrivastava
Quoted 19 lines · Samiullah Khawaja
Add liveupdate FLB for IOMMU state preservation. Use KHO preserve memory alloc/free helper functions to allocate memory for the IOMMU LU FLB object and the serialization structs for device, domain and iommu.
During retrieve, walk through the preserved objs nodes and restore each folio. Also recreate the FLB obj.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/Kconfig | 11 +++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 177 ++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 17 ++++ include/linux/kho/abi/iommu.h | 119 +++++++++++++++++++++++ 5 files changed, 325 insertions(+) create mode 100644 drivers/iommu/liveupdate.c create mode 100644 include/linux/iommu-lu.h create mode 100644 include/linux/kho/abi/iommu.h
Quoted 8 lines
Quoted 8 lines
drivers/iommu/Kconfig config IOMMUFD_DRIVER · line 13
Quoted 8 lines
drivers/iommu/Kconfig +0
-0@@ -11,6 +11,17 @@ config IOMMUFD_DRIVER bool default n
[ snip ---- >8 -----]
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
drivers/iommu/Kconfig +31
-0+enum iommu_lu_type { + IOMMU_INVALID, + IOMMU_INTEL, +}; + +struct iommu_obj_ser { + u32 idx; + u32 ref_count; + u32 deleted:1; + u32 incoming:1; +} __packed; + +struct iommu_domain_ser { + struct iommu_obj_ser obj; + u64 top_table; + u64 top_level; + struct iommu_domain *restored_domain; +} __packed; + +struct device_domain_iommu_ser { + u32 did; + u64 domain_phys; + u64 iommu_phys; +} __packed; + +struct device_ser { + struct iommu_obj_ser obj; + u64 token; + u32 devid; + u32 pci_domain; + struct device_domain_iommu_ser domain_iommu_ser;
Quoted 8 lines
Quoted 8 lines
drivers/iommu/Kconfig
Quoted 8 lines
drivers/iommu/Kconfig +8
-0+ enum iommu_lu_type type; +} __packed; + +struct iommu_intel_ser { + u64 phys_addr; + u64 root_table; +} __packed; +
One more thing here, let's add the "intel" stuff with the intel patches
Thanks,
Praan
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
drivers/iommu/Kconfig +21
-0+struct iommu_ser { + struct iommu_obj_ser obj; + u64 token; + enum iommu_lu_type type; + union { + struct iommu_intel_ser intel; + }; +} __packed; + +struct iommu_objs_ser { + u64 next_objs; + u64 nr_objs; +} __packed; + +struct iommus_ser { + struct iommu_objs_ser objs; + struct iommu_ser iommus[]; +} __packed; + +struct iommu_domains_ser { + struct iommu_objs_ser objs;
Quoted 8 lines
Quoted 8 lines
drivers/iommu/Kconfig
Quoted 8 lines
drivers/iommu/Kconfig +8
-0+ struct iommu_domain_ser iommu_domains[]; +} __packed; + +struct devices_ser { + struct iommu_objs_ser objs; + struct device_ser devices[]; +} __packed; +
Thanks,
Praan
Re: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
Quoted 67 lines · Pranjal Shrivastava
Quoted 21 lines · Samiullah Khawaja
Quoted 20 lines · Pranjal Shrivastava
Quoted 19 lines · Samiullah Khawaja
Add liveupdate FLB for IOMMU state preservation. Use KHO preserve memory alloc/free helper functions to allocate memory for the IOMMU LU FLB object and the serialization structs for device, domain and iommu.
During retrieve, walk through the preserved objs nodes and restore each folio. Also recreate the FLB obj.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/Kconfig | 11 +++ drivers/iommu/Makefile | 1 + drivers/iommu/liveupdate.c | 177 ++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 17 ++++ include/linux/kho/abi/iommu.h | 119 +++++++++++++++++++++++ 5 files changed, 325 insertions(+) create mode 100644 drivers/iommu/liveupdate.c create mode 100644 include/linux/iommu-lu.h create mode 100644 include/linux/kho/abi/iommu.hQuoted 8 lines
Quoted 8 lines
drivers/iommu/Kconfig config IOMMUFD_DRIVER · line 13
Quoted 8 lines
drivers/iommu/Kconfig +0
-0@@ -11,6 +11,17 @@ config IOMMUFD_DRIVER bool default n[ snip ---- >8 -----]
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
drivers/iommu/Kconfig +33
-0+enum iommu_lu_type { + IOMMU_INVALID, + IOMMU_INTEL, +}; + +struct iommu_obj_ser { + u32 idx; + u32 ref_count; + u32 deleted:1; + u32 incoming:1; +} __packed; + +struct iommu_domain_ser { + struct iommu_obj_ser obj; + u64 top_table; + u64 top_level; + struct iommu_domain *restored_domain; +} __packed; + +struct device_domain_iommu_ser { + u32 did; + u64 domain_phys; + u64 iommu_phys; +} __packed; + +struct device_ser { + struct iommu_obj_ser obj; + u64 token; + u32 devid; + u32 pci_domain; + struct device_domain_iommu_ser domain_iommu_ser; + enum iommu_lu_type type; +} __packed;
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
drivers/iommu/Kconfig
Quoted 6 lines
drivers/iommu/Kconfig +6
-0+ +struct iommu_intel_ser { + u64 phys_addr; + u64 root_table; +} __packed; +One more thing here, let's add the "intel" stuff with the intel patches
Agreed. Will move these to intel specific patches.
Quoted 28 lines
Thanks,
PraanQuoted 24 lines
Quoted 24 lines
Quoted 24 lines
drivers/iommu/Kconfig +24
-0+struct iommu_ser { + struct iommu_obj_ser obj; + u64 token; + enum iommu_lu_type type; + union { + struct iommu_intel_ser intel; + }; +} __packed; + +struct iommu_objs_ser { + u64 next_objs; + u64 nr_objs; +} __packed; + +struct iommus_ser { + struct iommu_objs_ser objs; + struct iommu_ser iommus[]; +} __packed; + +struct iommu_domains_ser { + struct iommu_objs_ser objs; + struct iommu_domain_ser iommu_domains[]; +} __packed; +
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
drivers/iommu/Kconfig
Quoted 5 lines
drivers/iommu/Kconfig +5
-0+struct devices_ser { + struct iommu_objs_ser objs; + struct device_ser devices[]; +} __packed; +Thanks,
Praan
Thanks,
Sami
Re: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
In reply to Pranjal ShrivastavaQuoted 10 lines · Pranjal Shrivastava
drivers/iommu/liveupdate.c line 137
Quoted 5 lines
drivers/iommu/liveupdate.c new file +5
-0+if (!obj) +return -ENOMEM ; + +mutex_init (&obj->lock); +BUG_ON (!kho_restore_folio (argp->data));The use of
BUG_ONin new code is heavily discouraged [1].
If KHO can't restore the folio for whatever reason, we can be treat it as a corruption of the handover data. I believe crashing the kernel for it would be an overkill?
There should not be any new BUG_ON's added at least they need some better wrapper function or design so they are not in end-users like this.
But these cases need to fail by preventing the kernel from leaving the initial safe boot memory space (ie it is going to crash anyhow). If we can't recover things then we can't guarentee memory is not being scribbled on and we can't use it.
This is the general logic why these are made very fatal.
Jason
Tian, Kevin
maintainerRE: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
Quoted 15 lines
From: Jason Gunthorpe <[email protected]>
Sent: Friday, April 10, 2026 9:52 PMQuoted 10 lines · Pranjal Shrivastava
drivers/iommu/liveupdate.c line 137
Quoted 5 lines
drivers/iommu/liveupdate.c new file +5
-0+if (!obj) +return -ENOMEM ; + +mutex_init (&obj->lock); +BUG_ON (!kho_restore_folio (argp->data));The use of
BUG_ONin new code is heavily discouraged [1].
If KHO can't restore the folio for whatever reason, we can be treat it as a corruption of the handover data. I believe crashing the kernel for it would be an overkill?
Quoted 9 lines
There should not be any new BUG_ON's added at least they need some better wrapper function or design so they are not in end-users like this.
But these cases need to fail by preventing the kernel from leaving the initial safe boot memory space (ie it is going to crash anyhow). If we can't recover things then we can't guarentee memory is not being scribbled on and we can't use it.
sounds in a constrained context similar to kdump...
Re: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
In reply to Samiullah Khawajadrivers/iommu/Kconfig config IOMMUFD_DRIVER · line 24
Quoted 11 lines · Samiullah Khawaja
drivers/iommu/Kconfig +11
-0+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. +
Do we need a separate config? Can't we just use CONFIG_LIVEUPDATE?
drivers/iommu/Makefile obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o · line 18
Quoted 12 lines
menuconfig
IOMMU_SUPPORTdrivers/iommu/Kconfig +0
-0bool "IOMMU Hardware Support" depends on MMUdrivers/iommu/Makefile +1
-0@@ -15,6 +15,7 @@ obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o 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_LIVEUPDATE) += liveupdate.o
It seems like there is a sorted order for CONFIG_IOMMU_* in the Makefile, lets keep it same if possible.
drivers/iommu/Makefile obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o
Quoted 6 lines
drivers/iommu/Makefile +6
-0+static void iommu_liveupdate_free_objs(u64 next, bool incoming) +{ + struct iommu_objs_ser *objs; + + while (next) { + objs = __va(next);
There is also call to phys_to_virt() in other functions in this patch. Should we use the same here to be consistent?
drivers/iommu/Makefile
Quoted 8 lines
drivers/iommu/Makefile +8
-0+ next = objs->next_objs; + + if (!incoming) + kho_unpreserve_free(objs); + else + folio_put(virt_to_folio(objs)); + } +}
Instead of passing boolean, and calling with different arguments, I think it will be simpler to just have two functions
- iommu_liveupdate_unpreserve()
- iommu_liveupdate_folio_put()
Quoted 51 lines
drivers/iommu/Makefile +51
-0+ +static void iommu_liveupdate_flb_free(struct iommu_lu_flb_obj *obj) +{ + if (obj->iommu_domains) + iommu_liveupdate_free_objs(obj->ser->iommu_domains_phys, false); + + if (obj->devices) + iommu_liveupdate_free_objs(obj->ser->devices_phys, false); + + if (obj->iommus) + iommu_liveupdate_free_objs(obj->ser->iommus_phys, false); + + kho_unpreserve_free(obj->ser); + kfree(obj); +} + +static int iommu_liveupdate_flb_preserve(struct liveupdate_flb_op_args *argp) +{ + struct iommu_lu_flb_obj *obj; + struct iommu_lu_flb_ser *ser; + void *mem; + + obj = kzalloc(sizeof(*obj), GFP_KERNEL); + if (!obj) + return -ENOMEM; + + mutex_init(&obj->lock); + mem = kho_alloc_preserve(sizeof(*ser)); + if (IS_ERR(mem)) + goto err_free; + + ser = mem; + obj->ser = ser; + + mem = kho_alloc_preserve(PAGE_SIZE); + if (IS_ERR(mem)) + goto err_free; + + obj->iommu_domains = mem; + ser->iommu_domains_phys = virt_to_phys(obj->iommu_domains); + + mem = kho_alloc_preserve(PAGE_SIZE); + if (IS_ERR(mem)) + goto err_free; + + obj->devices = mem; + ser->devices_phys = virt_to_phys(obj->devices); + + mem = kho_alloc_preserve(PAGE_SIZE); + if (IS_ERR(mem)) + goto err_free;
drivers/iommu/Makefile
Quoted 10 lines
drivers/iommu/Makefile +10
-0+ + obj->iommus = mem; + ser->iommus_phys = virt_to_phys(obj->iommus); + + argp->obj = obj; + argp->data = virt_to_phys(ser); + return 0; + +err_free: + iommu_liveupdate_flb_free(obj);
Generally, I have seen in the function goto will call corresponding error tags, and free corresponding allocations and all the one which happend before. It is easier to read code that way. I know you are combining the free call from iommu_liveupdate_flb_unpreserve() also. IMHO, code readability will be better this way.
Quoted 4 lines
drivers/iommu/Makefile +4
-0+ return PTR_ERR(mem); +} + +static void iommu_liveupdate_flb_unpreserve(struct liveupdate_flb_op_args *argp)
drivers/iommu/Makefile
Quoted 10 lines
drivers/iommu/Makefile +10
-0+{ + iommu_liveupdate_flb_free(argp->obj); +} + +static void iommu_liveupdate_flb_finish(struct liveupdate_flb_op_args *argp) +{ + struct iommu_lu_flb_obj *obj = argp->obj; + + if (obj->iommu_domains) + iommu_liveupdate_free_objs(obj->ser->iommu_domains_phys, true);
Can there be the case where obj->iommu_domains is NULL but obj->ser->iommu_domains_phys is not? If that is not possible, I will just simplify the patch and unconditionally call iommu_liveupdate_free_objs()?
drivers/iommu/Makefile
Quoted 9 lines
drivers/iommu/Makefile +9
-0+ +static int iommu_liveupdate_flb_retrieve(struct liveupdate_flb_op_args *argp) +{ + struct iommu_lu_flb_obj *obj; + struct iommu_lu_flb_ser *ser; + + obj = kzalloc(sizeof(*obj), GFP_ATOMIC); + if (!obj) + return -ENOMEM;
Is kzalloc() failure here recoverable whereas iommu_liveupdate_restore_objs() below is not? If it is not recoverable should there be a BUG_ON here?
drivers/iommu/Makefile
Quoted 8 lines
drivers/iommu/Makefile +8
-0+ + mutex_init(&obj->lock); + BUG_ON(!kho_restore_folio(argp->data)); + ser = phys_to_virt(argp->data); + obj->ser = ser; + + iommu_liveupdate_restore_objs(ser->iommu_domains_phys); + obj->iommu_domains = phys_to_virt(ser->iommu_domains_phys);
Can iommu_liveupdate_restore_obj() just return virtual address and we can simplify code to:
obj->iommu_domains = iommu_liveupdate_restore_objs(ser->iommu_domains_phys);
include/linux/iommu-lu.h
Quoted 13 lines
drivers/iommu/Makefile +12
-0+ + iommu_liveupdate_restore_objs(ser->devices_phys); + obj->devices = phys_to_virt(ser->devices_phys); + + iommu_liveupdate_restore_objs(ser->iommus_phys); + obj->iommus = phys_to_virt(ser->iommus_phys); + + argp->obj = obj; + + return 0; +} +include/linux/iommu-lu.h +0
-0
I will recommend to use full name and not short "lu". iommu-liveupdate.h seems more readable and not too long.
include/linux/iommu-lu.h
Quoted 4 lines
include/linux/iommu-lu.h +4
-0+#define MAX_IOMMU_SERS ((PAGE_SIZE - sizeof(struct iommus_ser)) / sizeof(struct iommu_ser)) +#define MAX_IOMMU_DOMAIN_SERS \ + ((PAGE_SIZE - sizeof(struct iommu_domains_ser)) / sizeof(struct iommu_domain_ser)) +#define MAX_DEVICE_SERS ((PAGE_SIZE - sizeof(struct devices_ser)) / sizeof(struct device_ser))
This is per page limit, not whole serialization limit. May be we can name something like:
- MAX_IOMMU_SERS_PER_PAGE, or
- MAX_IOMMU_SERS_PAGE_CAPACITY
include/linux/iommu-lu.h
Quoted 10 lines
include/linux/iommu-lu.h +10
-0+ +struct iommu_lu_flb_obj { +struct mutex lock; +struct iommu_lu_flb_ser *ser; + +struct iommu_domains_ser *iommu_domains; +struct iommus_ser *iommus; +struct devices_ser *devices; +} __packed; +
I think naming scheme used here is little hard to absorb when we have so many individual structs in this header file. Specifically, struct names like:
- iommu_domains_ser vs iommu_domain_ser
- iommus_ser vs iommu_ser
- devices_ser vs device_ser
- iommu_objs_ser vs iommu_obj_ser
First three are showing container and its elements relation, however, last one doesn't have that relation but naming is same there.
I will recommend to change the naming scheme of containers to something like:
struct iommu_domain_ser_[hdr|header|table|arr] {};
struct iommu_ser_hdr {}
struct device_ser_hdr {}Individual element of container can be same.
For objs, something like:
iommu_objs_ser -> iommu_hdr_meta
Re: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
Quoted 14 lines · Vipin Sharma
drivers/iommu/Kconfig config IOMMUFD_DRIVER · line 24
Quoted 11 lines · Samiullah Khawaja
drivers/iommu/Kconfig +11
-0+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. +Do we need a separate config? Can't we just use
CONFIG_LIVEUPDATE?
We have a separate CONFIG here so that the phase 1/2 split for iommu preservation doesn't break the vfio preservation. See following discussion in the RFCv2:
https://lore.kernel.org/all/[email protected]/
Quoted 16 lines
drivers/iommu/Makefile obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o · line 18
Quoted 13 lines
menuconfig
IOMMU_SUPPORTdrivers/iommu/Kconfig +0
-0bool "IOMMU Hardware Support" depends on MMUdrivers/iommu/Makefile +1
-0@@ -15,6 +15,7 @@ obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o 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_LIVEUPDATE) += liveupdate.oIt seems like there is a sorted order for CONFIG_IOMMU_* in the
Makefile, lets keep it same if possible.
Will fix in the next revision.
Quoted 10 lines
drivers/iommu/Makefile obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o
Quoted 7 lines
drivers/iommu/Makefile +6
-0+static void iommu_liveupdate_free_objs(u64 next, bool incoming) +{ + struct iommu_objs_ser *objs; + + while (next) { + objs = __va(next);There is also call to
phys_to_virt()in other functions in this patch. Should we use the same here to be consistent?
Agreed. I will fix this.
Quoted 13 lines
drivers/iommu/Makefile
Quoted 9 lines
drivers/iommu/Makefile +8
-0+ next = objs->next_objs; + + if (!incoming) + kho_unpreserve_free(objs); + else + folio_put(virt_to_folio(objs)); + } +}Instead of passing boolean, and calling with different arguments, I think it will be simpler to just have two functions
Quoted 2 lines
-
iommu_liveupdate_unpreserve()
-iommu_liveupdate_folio_put()
This is a helper function to free the serialized state without duplicating multiple checks for various type of state (iommu, iommu_domain and devices).
Do you think maybe I should add these two functions and make it call the helper?
Quoted 60 lines
Quoted 60 lines
drivers/iommu/Makefile +59
-0+ +static void iommu_liveupdate_flb_free(struct iommu_lu_flb_obj *obj) +{ + if (obj->iommu_domains) + iommu_liveupdate_free_objs(obj->ser->iommu_domains_phys, false); + + if (obj->devices) + iommu_liveupdate_free_objs(obj->ser->devices_phys, false); + + if (obj->iommus) + iommu_liveupdate_free_objs(obj->ser->iommus_phys, false); + + kho_unpreserve_free(obj->ser); + kfree(obj); +} + +static int iommu_liveupdate_flb_preserve(struct liveupdate_flb_op_args *argp) +{ + struct iommu_lu_flb_obj *obj; + struct iommu_lu_flb_ser *ser; + void *mem; + + obj = kzalloc(sizeof(*obj), GFP_KERNEL); + if (!obj) + return -ENOMEM; + + mutex_init(&obj->lock); + mem = kho_alloc_preserve(sizeof(*ser)); + if (IS_ERR(mem)) + goto err_free; + + ser = mem; + obj->ser = ser; + + mem = kho_alloc_preserve(PAGE_SIZE); + if (IS_ERR(mem)) + goto err_free; + + obj->iommu_domains = mem; + ser->iommu_domains_phys = virt_to_phys(obj->iommu_domains); + + mem = kho_alloc_preserve(PAGE_SIZE); + if (IS_ERR(mem)) + goto err_free; + + obj->devices = mem; + ser->devices_phys = virt_to_phys(obj->devices); + + mem = kho_alloc_preserve(PAGE_SIZE); + if (IS_ERR(mem)) + goto err_free; + + obj->iommus = mem; + ser->iommus_phys = virt_to_phys(obj->iommus); + + argp->obj = obj; + argp->data = virt_to_phys(ser); + return 0; +
Quoted 8 lines
drivers/iommu/Makefile
Quoted 2 lines
drivers/iommu/Makefile +2
-0+err_free: + iommu_liveupdate_flb_free(obj);Generally, I have seen in the function goto will call corresponding error tags, and free corresponding allocations and all the one which happend before. It is easier to read code that way. I know you are combining the free call from
iommu_liveupdate_flb_unpreserve()also. IMHO, code readability will be better this way.
I had that originally when I was writing this function, but it gets really cluttered :(. Instead it is more clean without code duplication using this one cleanup function here to free the state on error and also when doing unpreserve. Please consider this a "destroy" function of obj and it can be called from 2 places,
- Error during allocation of internal state.
- During unpreserve.
Quoted 12 lines
Quoted 12 lines
drivers/iommu/Makefile +11
-0+ return PTR_ERR(mem); +} + +static void iommu_liveupdate_flb_unpreserve(struct liveupdate_flb_op_args *argp) +{ + iommu_liveupdate_flb_free(argp->obj); +} + +static void iommu_liveupdate_flb_finish(struct liveupdate_flb_op_args *argp) +{ + struct iommu_lu_flb_obj *obj = argp->obj;
Quoted 8 lines
drivers/iommu/Makefile
Quoted 3 lines
drivers/iommu/Makefile +3
-0+ + if (obj->iommu_domains) + iommu_liveupdate_free_objs(obj->ser->iommu_domains_phys, true);Can there be the case where
obj->iommu_domainsis NULL butobj->ser->iommu_domains_physis not? If that is not possible, I will just simplify the patch and unconditionally calliommu_liveupdate_free_objs()?
Are you suggesting that on flb_finish() the obj->iommu_domains should be non-NULL as flb_retrieve() succeeded? If yes, then that is correct. I will update this to call the free_objs() without checking obj->iommu_domains. I will do same for other types.
Quoted 13 lines
drivers/iommu/Makefile
Quoted 10 lines
drivers/iommu/Makefile +9
-0+ +static int iommu_liveupdate_flb_retrieve(struct liveupdate_flb_op_args *argp) +{ + struct iommu_lu_flb_obj *obj; + struct iommu_lu_flb_ser *ser; + + obj = kzalloc(sizeof(*obj), GFP_ATOMIC); + if (!obj) + return -ENOMEM;Is
kzalloc()failure here recoverable whereasiommu_liveupdate_restore_objs()below is not? If it is not recoverable should there be aBUG_ONhere?
Interesting... This should be recoverable as there is no corruption or bad state. LUO will propagate this to caller and it should be handle properly. I will make sure that this is handled in init.
Quoted 13 lines
drivers/iommu/Makefile
Quoted 9 lines
drivers/iommu/Makefile +8
-0+ + mutex_init(&obj->lock); + BUG_ON(!kho_restore_folio(argp->data)); + ser = phys_to_virt(argp->data); + obj->ser = ser; + + iommu_liveupdate_restore_objs(ser->iommu_domains_phys); + obj->iommu_domains = phys_to_virt(ser->iommu_domains_phys);Can
iommu_liveupdate_restore_obj()just return virtual address and we can simplify code to:
Quoted 1 line
obj->iommu_domains = iommu_liveupdate_restore_objs(ser->iommu_domains_phys);
Yes that is a good idea. I will change this.
Quoted 17 lines
include/linux/iommu-lu.h
Quoted 14 lines
drivers/iommu/Makefile +12
-0+ + iommu_liveupdate_restore_objs(ser->devices_phys); + obj->devices = phys_to_virt(ser->devices_phys); + + iommu_liveupdate_restore_objs(ser->iommus_phys); + obj->iommus = phys_to_virt(ser->iommus_phys); + + argp->obj = obj; + + return 0; +} +include/linux/iommu-lu.h +0
-0I will recommend to use full name and not short "lu".
iommu-liveupdate.hseems more readable and not too long.
Agreed. I will change this.
Quoted 11 lines
include/linux/iommu-lu.h
Quoted 5 lines
include/linux/iommu-lu.h +4
-0+#define MAX_IOMMU_SERS ((PAGE_SIZE - sizeof(struct iommus_ser)) / sizeof(struct iommu_ser)) +#define MAX_IOMMU_DOMAIN_SERS \ + ((PAGE_SIZE - sizeof(struct iommu_domains_ser)) / sizeof(struct iommu_domain_ser)) +#define MAX_DEVICE_SERS ((PAGE_SIZE - sizeof(struct devices_ser)) / sizeof(struct device_ser))This is per page limit, not whole serialization limit. May be we can name something like:
-
MAX_IOMMU_SERS_PER_PAGE, or
-MAX_IOMMU_SERS_PAGE_CAPACITY
Agreed.
Quoted 25 lines
include/linux/iommu-lu.h
Quoted 11 lines
include/linux/iommu-lu.h +10
-0+ +struct iommu_lu_flb_obj { +struct mutex lock; +struct iommu_lu_flb_ser *ser; + +struct iommu_domains_ser *iommu_domains; +struct iommus_ser *iommus; +struct devices_ser *devices; +} __packed; +I think naming scheme used here is little hard to absorb when we have so many individual structs in this header file. Specifically, struct names like:
-
iommu_domains_servsiommu_domain_ser
-iommus_servsiommu_ser
-devices_servsdevice_ser
-iommu_objs_servsiommu_obj_serFirst three are showing container and its elements relation, however, last one doesn't have that relation but naming is same there.
I will recommend to change the naming scheme of containers to something like:
Quoted 10 lines
struct iommu_domain_ser_[hdr|header|table|arr] {}; struct iommu_ser_hdr {} struct device_ser_hdr {}Individual element of container can be same.
For objs, something like:
iommu_objs_ser -> iommu_hdr_meta
Agreed. The singular vs plural for object vs aggregate is tricky. I will rework these names. I am thinking something like following based on the feedback on this patch,
struct iommu_ser_hdr; <= object hdr.
struct iommu_ser_arr_hdr <= array of objects hdr. struct iommu_domain_ser <= contains a preserved domain. struct iommu_domain_ser_arr <= array of domains.
Thanks,
Sami
Re: [PATCH 01/14] iommu: Implement IOMMU LU FLB callbacks
Quoted 16 lines · Samiullah Khawaja
Quoted 14 lines · Vipin Sharma
drivers/iommu/Kconfig config IOMMUFD_DRIVER · line 24
Quoted 11 lines · Samiullah Khawaja
drivers/iommu/Kconfig +11
-0+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. +Do we need a separate config? Can't we just use
CONFIG_LIVEUPDATE?
Quoted 5 lines
We have a separate CONFIG here so that the phase 1/2 split for iommu preservation doesn't break the vfio preservation. See following discussion in the RFCv2:
Sounds good.
Quoted 21 lines
Quoted 18 lines
Quoted 4 lines
drivers/iommu/liveupdate.c new file +4
-0+static void iommu_liveupdate_free_objs (u64 next,bool incoming) +{ +struct iommu_objs_ser *objs; +drivers/iommu/liveupdate.c line 40
Quoted 10 lines
drivers/iommu/liveupdate.c new file +10
-0+while (next) { + objs =__va (next); + next = objs->next_objs; + +if (!incoming) +kho_unpreserve_free (objs); +else +folio_put (virt_to_folio (objs)); + } +}Instead of passing boolean, and calling with different arguments, I think it will be simpler to just have two functions
Quoted 2 lines
-
iommu_liveupdate_unpreserve()
-iommu_liveupdate_folio_put()
Quoted 6 lines
This is a helper function to free the serialized state without duplicating multiple checks for various type of state (iommu,
iommu_domainand devices).Do you think maybe I should add these two functions and make it call the helper?
Read the next response.
Quoted 69 lines
Quoted 60 lines
Quoted 60 lines
drivers/iommu/liveupdate.c new file +59
-0+ +static void iommu_liveupdate_flb_free (struct iommu_lu_flb_obj *obj) +{ +if (obj->iommu_domains) +iommu_liveupdate_free_objs (obj->ser->iommu_domains_phys, false); + +if (obj->devices) +iommu_liveupdate_free_objs (obj->ser->devices_phys, false); + +if (obj->iommus) +iommu_liveupdate_free_objs (obj->ser->iommus_phys, false); + +kho_unpreserve_free (obj->ser); +kfree (obj); +} + +static int iommu_liveupdate_flb_preserve (struct liveupdate_flb_op_args *argp) +{ +struct iommu_lu_flb_obj *obj; +struct iommu_lu_flb_ser *ser; +void *mem; + + obj =kzalloc (sizeof (*obj),GFP_KERNEL ); +if (!obj) +return -ENOMEM ; + +mutex_init (&obj->lock); + mem =kho_alloc_preserve (sizeof (*ser)); +if (IS_ERR (mem)) + goto err_free; + + ser = mem; + obj->ser = ser; + + mem =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (mem)) + goto err_free; + + obj->iommu_domains = mem; + ser->iommu_domains_phys =virt_to_phys (obj->iommu_domains); + + mem =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (mem)) + goto err_free; + + obj->devices = mem; + ser->devices_phys =virt_to_phys (obj->devices); + + mem =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (mem)) + goto err_free; + + obj->iommus = mem; + ser->iommus_phys =virt_to_phys (obj->iommus); + + argp->obj = obj; + argp->data =virt_to_phys (ser); +return 0 ; +Quoted 8 lines
drivers/iommu/liveupdate.c line 101
Quoted 2 lines
drivers/iommu/liveupdate.c new file +2
-0+err_free: +iommu_liveupdate_flb_free (obj);Generally, I have seen in the function goto will call corresponding error tags, and free corresponding allocations and all the one which happend before. It is easier to read code that way. I know you are combining the free call from
iommu_liveupdate_flb_unpreserve()also. IMHO, code readability will be better this way.
Quoted 8 lines
I had that originally when I was writing this function, but it gets really cluttered :(. Instead it is more clean without code duplication using this one cleanup function here to free the state on error and also when doing unpreserve. Please consider this a "destroy" function of obj and it can be called from 2 places,
- Error during allocation of internal state.
- During unpreserve.
It is removing code duplication in
- iommu_liveupdate_flb_preserve()
- iommu_liveupdate_flb_unpreserve()
However, there is still duplicate code in iommu_liveupdate_flb_finish(). Another thing is iommu_liveupdate_free_objs() is doing two different things based on current liveupdate state (before or after kexec) passed by a bool argument. IMO, it is cleaner if we explicitly write whether we are doing unpreserve or just folio put.
I meant something like:
static void iommu_liveupdate_unpreserve_free(u64 next)
{
while (next) {
struct iommu_objs_ser *objs = __va(next);
next = objs->next_objs;
kho_unpreserve_free(objs);
}
}static void iommu_liveupdate_folio_put(u64 next)
{
while (next) {
struct iommu_objs_ser *objs = __va(next);
next = objs->next_objs;
folio_put(virt_to_folio(objs));
}
}static int iommu_liveupdate_flb_preserve(struct liveupdate_flb_op_args *argp) {
...
err_free_devices:
iommu_liveupdate_unpreserve_free(obj->ser->devices_phys); err_free_iommu_domains: iommu_liveupdate_unpreserve_free(obj->ser->iommu_domains_phys); err_free_ser: kho_unpreserve_free(obj->ser); err_free_obj: kfree(obj); return PTR_ERR(mem); }
static void iommu_liveupdate_flb_unpreserve(struct liveupdate_flb_op_args *argp)
{
struct iommu_lu_flb_obj *obj = argp->obj;
iommu_liveupdate_unpreserve_free(obj->ser->iommus_phys);
iommu_liveupdate_unpreserve_free(obj->ser->devices_phys);
iommu_liveupdate_unpreserve_free(obj->ser->iommu_domains_phys);
kho_unpreserve_free(obj->ser);
kfree(obj);
argp->obj = NULL;
}static void iommu_liveupdate_flb_finish(struct liveupdate_flb_op_args *argp)
{
struct iommu_lu_flb_obj *obj = argp->obj;
iommu_liveupdate_folio_put(obj->ser->iommus_phys);
iommu_liveupdate_folio_put(obj->ser->devices_phys);
iommu_liveupdate_folio_put(obj->ser->iommu_domains_phys);
folio_put(virt_to_folio(obj->ser));
kfree(obj);
argp->obj = NULL
}This way code is pretty explicit and clear what is happening. Let me know if you meant something else by cluttered code.
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/iommu/liveupdate.c new file +11
-0+return PTR_ERR (mem); +} + +static void iommu_liveupdate_flb_unpreserve (struct liveupdate_flb_op_args *argp) +{ +iommu_liveupdate_flb_free (argp->obj); +} + +static void iommu_liveupdate_flb_finish (struct liveupdate_flb_op_args *argp) +{ +struct iommu_lu_flb_obj *obj = argp->obj;Quoted 8 lines
drivers/iommu/liveupdate.c line 115
Quoted 3 lines
drivers/iommu/liveupdate.c new file +3
-0+ +if (obj->iommu_domains) +iommu_liveupdate_free_objs (obj->ser->iommu_domains_phys, true);Can there be the case where
obj->iommu_domainsis NULL butobj->ser->iommu_domains_physis not? If that is not possible, I will just simplify the patch and unconditionally calliommu_liveupdate_free_objs()?
Quoted 4 lines
Are you suggesting that on
flb_finish()theobj->iommu_domainsshould be non-NULL asflb_retrieve()succeeded? If yes, then that is correct. I will update this to call thefree_objs()without checkingobj->iommu_domains. I will do same for other types.
Yes.
PATCH 2/14 iommu: Implement IOMMU core liveupdate skeleton
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.
Similarly add IOMMU ops to preserve/unpreserve a device. These 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. The device can only be preserved if the attached iommu domain is preserved and the associated iommu supports preservation.
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.
--- drivers/iommu/iommu.c | 3 + drivers/iommu/liveupdate.c | 326 +++++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 119 ++++++++++++++ include/linux/iommu.h | 32 ++++ 4 files changed, 480 insertions(+)
drivers/iommu/iommu.c +3 -0
@@ -389,6 +389,9 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)mutex_init (¶m->lock); dev->iommu = param; +#ifdef CONFIG_IOMMU_LIVEUPDATE + dev->iommu->device_ser =NULL ; +#endif return param; }
drivers/iommu/liveupdate.c +326 -0
335 lines not on this page · raw message
include/linux/iommu-lu.h +119 -0
128 lines not on this page · raw message
include/linux/iommu.h +32 -0
@@ -14,6 +14,8 @@#include <linux/err.h> #include <linux/of.h> #include <linux/iova_bitmap.h> +#include <linux/atomic.h> +#include <linux/kho/abi/iommu.h> #include <uapi/linux/iommufd.h> #define IOMMU_READ (1 << 0)
@@ -248,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)
@@ -647,6 +653,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.
@@ -703,6 +713,11 @@ struct iommu_ops {struct iommu_domain *parent_domain, const struct iommu_user_data *user_data); +int (*preserve_device)(struct device *dev,struct device_ser *device_ser); +void (*unpreserve_device)(struct device *dev,struct device_ser *device_ser); +int (*preserve)(struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void (*unpreserve)(struct iommu_device *iommu,struct iommu_ser *iommu_ser); +const struct iommu_domain_ops *default_domain_ops;struct module *owner;struct iommu_domain *identity_domain;
@@ -749,6 +764,11 @@ struct iommu_ops {* specific mechanisms. * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*) * @free: Release the domain after use. +* @preserve: Preserve the iommu domain for liveupdate. +* Returns 0 on success, a negative errno on failure. +* @unpreserve: Unpreserve the iommu domain that was preserved earlier. +* @restore: Restore the iommu domain after liveupdate. +* Returns 0 on success, a negative errno on failure. */ struct iommu_domain_ops { int (*attach_dev)(struct iommu_domain *domain, struct device *dev,
@@ -779,6 +799,9 @@ struct iommu_domain_ops { unsigned long quirks);void (*free)(struct iommu_domain *domain); +int (*preserve)(struct iommu_domain *domain,struct iommu_domain_ser *ser); +void (*unpreserve)(struct iommu_domain *domain,struct iommu_domain_ser *ser); +int (*restore)(struct iommu_domain *domain,struct iommu_domain_ser *ser); };/**
@@ -790,6 +813,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;
@@ -799,6 +824,10 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_ser *outgoing_preserved_state; +#endif };/**
@@ -853,6 +882,9 @@ struct dev_iommu { u32 pci_32bit_workaround:1; u32 require_direct:1; u32 shadow_on_flush:1; +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct device_ser *device_ser; +#endif }; int iommu_device_register(struct iommu_device *iommu,
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
Quoted 145 lines · Samiullah Khawaja
Add IOMMU domain ops that can be implemented by the IOMMU drivers if they support IOMMU domain preservation across liveupdate. The new IOMMU domain preserve, unpreserve and restore APIs call these ops to perform respective live update operations.
Similarly add IOMMU ops to preserve/unpreserve a device. These 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. The device can only be preserved if the attached iommu domain is preserved and the associated iommu supports preservation.
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.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/iommu.c | 3 + drivers/iommu/liveupdate.c | 326 +++++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 119 ++++++++++++++ include/linux/iommu.h | 32 ++++ 4 files changed, 480 insertions(+)drivers/iommu/iommu.c +3
-0@@ -389,6 +389,9 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)mutex_init (¶m->lock); dev->iommu = param; +#ifdef CONFIG_IOMMU_LIVEUPDATE + dev->iommu->device_ser =NULL ; +#endif return param; }drivers/iommu/liveupdate.c +91
-0@@ -11,6 +11,7 @@#include <linux/liveupdate.h> #include <linux/iommu-lu.h> #include <linux/iommu.h> +#include <linux/pci.h> #include <linux/errno.h> static void iommu_liveupdate_restore_objs (u64 next)@@ -175,3 +176,328 @@ int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)return liveupdate_unregister_flb (handler, &iommu_flb); }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return -ENOENT ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + + ret =fn (&devices->devices[idx], arg); +if (ret) +return ret; + } + +return 0 ; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device); + +static inline bool device_ser_match (struct device_ser *match, +struct pci_dev *pdev) +{ +return match->devid ==pci_dev_id (pdev) && match->pci_domain ==pci_domain_nr (pdev->bus); +} + +struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + +if (!dev_is_pci (dev)) +return NULL ; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + +if (device_ser_match (&devices->devices[idx],to_pci_dev (dev))) { + devices->devices[idx].obj.incoming = true; +return &devices->devices[idx]; + } + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_device_preserved_data); + +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +struct iommu_lu_flb_obj *obj; +struct iommus_ser *iommus; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + iommus =__va (obj->ser->iommus_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_iommus; ++i, ++idx) { +if (idx >=MAX_IOMMU_SERS ) { + iommus =__va (iommus->objs.next_objs); + idx =0 ; + } + +if (iommus->iommus[idx].obj.deleted) +continue ; +
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 278
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+if (iommus->iommus[idx].token == token && + iommus->iommus[idx].type == type) +return &iommus->iommus[idx]; + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_preserved_data); + +static int reserve_obj_ser (struct iommu_objs_ser **objs_ptr,u64 max_objs)
Isn't this more of an "insert" / "populate" / write_ser_entry? We can rename it to something like iommu_ser_push_entry / iommu_ser_write_entry
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 280
Quoted 2 lines
drivers/iommu/liveupdate.c +2
-0+{ +struct iommu_objs_ser *next_objs, *objs = *objs_ptr;
Not loving these names :(
TBH, the reserve_obj_ser function isn't too readable, esp. with all the variable names, here and in the lu header.. I've had to go back n forth to the first patch. For example, here next_objs can be next_objs_page & objs can be curr_page. (PTAL at my reply on PATCH 01 about renaming).
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 292
Quoted 12 lines
drivers/iommu/liveupdate.c +12
-0+int idx; + +if (objs->nr_objs == max_objs) { + next_objs =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (next_objs)) +return PTR_ERR (next_objs); + + objs->next_objs =virt_to_phys (next_objs); + objs = next_objs; + *objs_ptr = objs; + objs->nr_objs =0 ; + objs->next_objs =0 ;
This seems redundant, no need to zero these out, kho_alloc_preserve passes __GFP_ZERO to folio_alloc [1], which should z-alloc the pages.
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 297
Quoted 5 lines
drivers/iommu/liveupdate.c +5
-0+ } + + idx = objs->nr_objs++; +return idx; +}
Just to give a mental model to fellow reviewers, here's how this is laid out:
----------------------------------------------------------------------
[ PAGE START ] |
----------------------------------------------------------------------
| iommu_objs_ser (The Page Header) | | - next_objs: 0x0000 (End of the page-chain) | | - nr_objs: 2 | ---------------------------------------------------------------------- | ITEM 0: iommu_domain_ser | | [ iommu_obj_ser (The entry header) ] | | - idx: 0 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ITEM 1: iommu_domain_ser | | [ iommu_obj_ser (The Price Tag) ] | | - idx: 1 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ... (Empty space for more domains) ... | | |
----------------------------------------------------------------------
[ PAGE END ] |
----------------------------------------------------------------------
Quoted 11 lines
drivers/iommu/liveupdate.c +11
-0+ +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int idx, ret; + +if (!domain->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj);
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 318
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommu_domains, +MAX_IOMMU_DOMAIN_SERS ); +if (idx <0 ) +return idx; + + domain_ser = &flb_obj->iommu_domains->iommu_domains[idx];
This is slightly less-readable as well, I understand we're trying to:
iommu_domains_ser -> iommu_domain_ser[idx] but the same name (iommu_domains) makes it difficult to read.. we should rename this as:
&flb_obj->iommu_domains_page->domain_entries[idx] or something for better readability..
Also, let's add a comment explaining that reserve_obj_ser actually advances the flb_obj ptr when necessary..
IIUC, we start with PAGE 0 initially, store it's phys in the iommu_flb_preserve op (the iommu_ser_phys et al) & then we go on alloc-ing more pages and keep storing the "current" active page with the liveupdate core. Now when we jump into the new kernel, we read the ser_phys and then follow the page chain, right?
Quoted 175 lines
drivers/iommu/liveupdate.c +175
-0+ idx = flb_obj->ser->nr_domains++; + domain_ser->obj.idx = idx; + domain_ser->obj.ref_count =1 ; + + ret = domain->ops->preserve (domain, domain_ser); +if (ret) { + domain_ser->obj.deleted = true; +return ret; + } + + domain->preserved_state = domain_ser; + *ser = domain_ser; +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_domain_preserve); + +void iommu_domain_unpreserve (struct iommu_domain *domain) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int ret; + +if (!domain->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ; + +guard (mutex)(&flb_obj->lock); + +/* +* 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; + domain->ops->unpreserve (domain, domain_ser); + domain_ser->obj.deleted = true; + domain->preserved_state =NULL ; +} +EXPORT_SYMBOL_GPL (iommu_domain_unpreserve); + +static int iommu_preserve_locked (struct iommu_device *iommu) +{ +struct iommu_lu_flb_obj *flb_obj; +struct iommu_ser *iommu_ser; +int idx, ret; + +if (!iommu->ops->preserve) +return -EOPNOTSUPP ; + +if (iommu->outgoing_preserved_state) { + iommu->outgoing_preserved_state->obj.ref_count++; +return 0 ; + } + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommus, +MAX_IOMMU_SERS ); +if (idx <0 ) +return idx; + + iommu_ser = &flb_obj->iommus->iommus[idx]; + idx = flb_obj->ser->nr_iommus++; + iommu_ser->obj.idx = idx; + iommu_ser->obj.ref_count =1 ; + + ret = iommu->ops->preserve (iommu, iommu_ser); +if (ret) + iommu_ser->obj.deleted = true; + + iommu->outgoing_preserved_state = iommu_ser; +return ret; +} + +static void iommu_unpreserve_locked (struct iommu_device *iommu) +{ +struct iommu_ser *iommu_ser = iommu->outgoing_preserved_state; + + iommu_ser->obj.ref_count--; +if (iommu_ser->obj.ref_count) +return ; + + iommu->outgoing_preserved_state =NULL ; + iommu->ops->unpreserve (iommu, iommu_ser); + iommu_ser->obj.deleted = true; +} + +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret, idx; + +if (!dev_is_pci (dev)) +return -EOPNOTSUPP ; + +if (!domain->preserved_state) +return -EINVAL ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->preserve_device || + !iommu->iommu_dev->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->devices, +MAX_DEVICE_SERS ); +if (idx <0 ) +return idx; + + device_ser = &flb_obj->devices->devices[idx]; + idx = flb_obj->ser->nr_devices++; + device_ser->obj.idx = idx; + device_ser->obj.ref_count =1 ; + + ret =iommu_preserve_locked (iommu->iommu_dev); +if (ret) { + device_ser->obj.deleted = true; +return ret; + } + + device_ser->domain_iommu_ser.domain_phys =__pa (domain->preserved_state); + device_ser->domain_iommu_ser.iommu_phys =__pa (iommu->iommu_dev->outgoing_preserved_state); + device_ser->devid =pci_dev_id (pdev); + device_ser->pci_domain =pci_domain_nr (pdev->bus); + device_ser->token = token; + + ret = iommu->iommu_dev->ops->preserve_device (dev, device_ser); +if (ret) { + device_ser->obj.deleted = true; +iommu_unpreserve_locked (iommu->iommu_dev); +return ret; + } + + dev->iommu->device_ser = device_ser; +return 0 ; +} + +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret; + +if (!dev_is_pci (dev)) +return ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->unpreserve_device || + !iommu->iommu_dev->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (WARN_ON (ret)) +return ; +
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 503
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+guard (mutex)(&flb_obj->lock); + device_ser =dev_iommu_preserved_state (dev); +if (WARN_ON (!device_ser)) +return ; + + iommu->iommu_dev->ops->unpreserve_device (dev, device_ser); + dev->iommu->device_ser =NULL ; + +iommu_unpreserve_locked (iommu->iommu_dev); +}
I'm wondering ig we should guard these APIs against accidental or potential abuse by other in-kernel drivers. Since the Live Update Orchestrator (LUO) is architecturally designed around user-space driven sequences (IOCTLs, specific mutex ordering, etc.), for now.
Since the header-file is also under include/linux, we should avoid any possibility where we end up having drivers depending on these API. We could add a check based on dma owner:
+ /* Only devices explicitly claimed by a user-space driver
+ * (VFIO/IOMMUFD) are eligible for Live Update preservation.
+ */
+ if (!iommu_dma_owner_claimed(dev))
+ return -EPERM;
This should ensure we aren't creating 'zombie' preserved states for devices not managed by IOMMUFD/VFIO.
Quoted 169 lines
include/linux/iommu-lu.h +119
-0@@ -8,9 +8,128 @@#ifndef _LINUX_IOMMU_LU_H#define _LINUX_IOMMU_LU_H +#include <linux/device.h> +#include <linux/iommu.h> #include <linux/liveupdate.h> #include <linux/kho/abi/iommu.h> +typedef int (*iommu_preserved_device_iter_fn )(struct device_ser *ser, +void *arg); +#ifdef CONFIG_IOMMU_LIVEUPDATE +static inline void *dev_iommu_preserved_state (struct device *dev) +{ +struct device_ser *ser; + +if (!dev->iommu) +return NULL ; + + ser = dev->iommu->device_ser; +if (ser && !ser->obj.incoming) +return ser; + +return NULL ; +} + +static inline void *dev_iommu_restored_state (struct device *dev) +{ +struct device_ser *ser; + +if (!dev->iommu) +return NULL ; + + ser = dev->iommu->device_ser; +if (ser && ser->obj.incoming) +return ser; + +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->obj.incoming) +return ser; + +return NULL ; +} + +static inline int dev_iommu_restore_did (struct device *dev,struct iommu_domain *domain) +{ +struct device_ser *ser =dev_iommu_restored_state (dev); + +if (ser &&iommu_domain_restored_state (domain)) +return ser->domain_iommu_ser.did; + +return -1 ; +} + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg); +struct device_ser *iommu_get_device_preserved_data (struct device *dev); +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type); +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser); +void iommu_domain_unpreserve (struct iommu_domain *domain); +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token); +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev); +#else +static inline void *dev_iommu_preserved_state (struct device *dev) +{ +return NULL ; +} + +static inline void *dev_iommu_restored_state (struct device *dev) +{ +return NULL ; +} + +static inline int dev_iommu_restore_did (struct device *dev,struct iommu_domain *domain) +{ +return -1 ; +} + +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +return NULL ; +} + +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) +{ +return -EOPNOTSUPP ; +} + +static inline struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +return NULL ; +} + +static inline struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +return NULL ; +} + +static inline int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_domain_unpreserve (struct iommu_domain *domain) +{ +} + +static inline int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +} +#endif +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler);int iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler);include/linux/iommu.h +10
-0@@ -14,6 +14,8 @@#include <linux/err.h> #include <linux/of.h> #include <linux/iova_bitmap.h> +#include <linux/atomic.h> +#include <linux/kho/abi/iommu.h> #include <uapi/linux/iommufd.h> #define IOMMU_READ (1 << 0)@@ -248,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)@@ -647,6 +653,10 @@ __iommu_copy_struct_to_user(const struct iommu_user_data *dst_data,* resources shared/passed to user space IOMMU instance. Associate * it with a nesting @parent_domain. It is required for driver to * set @viommu->ops pointing to its own viommu_ops +* @preserve_device: Preserve state of a device for liveupdate. +* @unpreserve_device: Unpreserve state that was preserved earlier. +* @preserve: Preserve state of iommu translation hardware for liveupdate. +* @unpreserve: Unpreserve state of iommu that was preserved earlier. * @owner: Driver module providing these ops * @identity_domain: An always available, always attachable identity * translation.
include/linux/iommu.h struct iommu_ops { · line 717
Quoted 5 lines
include/linux/iommu.h +2
-0@@ -703,6 +713,11 @@ struct iommu_ops {struct iommu_domain *parent_domain, const struct iommu_user_data *user_data); +int (*preserve_device)(struct device *dev,struct device_ser *device_ser); +void (*unpreserve_device)(struct device *dev,struct device_ser *device_ser);
Nit: Let's move the _device ops under the comment:/* Per device IOMMU features */
include/linux/iommu.h struct iommu_ops { · line 720
Quoted 3 lines
include/linux/iommu.h +3
-0+int (*preserve)(struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void (*unpreserve)(struct iommu_device *iommu,struct iommu_ser *iommu_ser); +
I'm wondering if there's any benefit to add these ops under a #ifdef ?
Quoted 46 lines
include/linux/iommu.h +14
-0const struct iommu_domain_ops *default_domain_ops;struct module *owner;struct iommu_domain *identity_domain;@@ -749,6 +764,11 @@ struct iommu_ops {* specific mechanisms. * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*) * @free: Release the domain after use. +* @preserve: Preserve the iommu domain for liveupdate. +* Returns 0 on success, a negative errno on failure. +* @unpreserve: Unpreserve the iommu domain that was preserved earlier. +* @restore: Restore the iommu domain after liveupdate. +* Returns 0 on success, a negative errno on failure. */ struct iommu_domain_ops { int (*attach_dev)(struct iommu_domain *domain, struct device *dev,@@ -779,6 +799,9 @@ struct iommu_domain_ops { unsigned long quirks);void (*free)(struct iommu_domain *domain); +int (*preserve)(struct iommu_domain *domain,struct iommu_domain_ser *ser); +void (*unpreserve)(struct iommu_domain *domain,struct iommu_domain_ser *ser); +int (*restore)(struct iommu_domain *domain,struct iommu_domain_ser *ser); };/** @@ -790,6 +813,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;@@ -799,6 +824,10 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_ser *outgoing_preserved_state; +#endif };/**
include/linux/iommu.h struct dev_iommu { · line 890
Quoted 9 lines
include/linux/iommu.h +3
-0@@ -853,6 +882,9 @@ struct dev_iommu { u32 pci_32bit_workaround:1; u32 require_direct:1; u32 shadow_on_flush:1; +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct device_ser *device_ser; +#endif }; int iommu_device_register(struct iommu_device *iommu,
Thanks,
Praan
[1] https://elixir.bootlin.com/linux/v7.0-rc3/source/kernel/liveupdate/kexec_handover.c#L1182
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
Quoted 151 lines · Pranjal Shrivastava
Quoted 150 lines · Samiullah Khawaja
Add IOMMU domain ops that can be implemented by the IOMMU drivers if they support IOMMU domain preservation across liveupdate. The new IOMMU domain preserve, unpreserve and restore APIs call these ops to perform respective live update operations.
Similarly add IOMMU ops to preserve/unpreserve a device. These 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. The device can only be preserved if the attached iommu domain is preserved and the associated iommu supports preservation.
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.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/iommu.c | 3 + drivers/iommu/liveupdate.c | 326 +++++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 119 ++++++++++++++ include/linux/iommu.h | 32 ++++ 4 files changed, 480 insertions(+)drivers/iommu/iommu.c +3
-0@@ -389,6 +389,9 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)mutex_init (¶m->lock); dev->iommu = param; +#ifdef CONFIG_IOMMU_LIVEUPDATE + dev->iommu->device_ser =NULL ; +#endif return param; }drivers/iommu/liveupdate.c +96
-0@@ -11,6 +11,7 @@#include <linux/liveupdate.h> #include <linux/iommu-lu.h> #include <linux/iommu.h> +#include <linux/pci.h> #include <linux/errno.h> static void iommu_liveupdate_restore_objs (u64 next)@@ -175,3 +176,328 @@ int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)return liveupdate_unregister_flb (handler, &iommu_flb); }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return -ENOENT ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + + ret =fn (&devices->devices[idx], arg); +if (ret) +return ret; + } + +return 0 ; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device); + +static inline bool device_ser_match (struct device_ser *match, +struct pci_dev *pdev) +{ +return match->devid ==pci_dev_id (pdev) && match->pci_domain ==pci_domain_nr (pdev->bus); +} + +struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + +if (!dev_is_pci (dev)) +return NULL ; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + +if (device_ser_match (&devices->devices[idx],to_pci_dev (dev))) { + devices->devices[idx].obj.incoming = true; +return &devices->devices[idx]; + } + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_device_preserved_data); + +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +struct iommu_lu_flb_obj *obj; +struct iommus_ser *iommus; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + iommus =__va (obj->ser->iommus_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_iommus; ++i, ++idx) { +if (idx >=MAX_IOMMU_SERS ) { + iommus =__va (iommus->objs.next_objs); + idx =0 ; + } + +if (iommus->iommus[idx].obj.deleted) +continue ; + +if (iommus->iommus[idx].token == token && + iommus->iommus[idx].type == type) +return &iommus->iommus[idx]; + } +
Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 278
Quoted 5 lines
drivers/iommu/liveupdate.c +5
-0+return NULL ; +} +EXPORT_SYMBOL (iommu_get_preserved_data); + +static int reserve_obj_ser (struct iommu_objs_ser **objs_ptr,u64 max_objs)Isn't this more of an "insert" / "populate" /
write_ser_entry? We can rename it to something likeiommu_ser_push_entry/iommu_ser_write_entry
This is reserving an object in the objects array. The object is filled once reservation is done. Maybe I can call it alloc_obj_ser or alloc_entry_ser.
Quoted 10 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 280
Quoted 3 lines
drivers/iommu/liveupdate.c +2
-0+{ +struct iommu_objs_ser *next_objs, *objs = *objs_ptr;Not loving these names :(
TBH, the
reserve_obj_serfunction isn't too readable, esp. with all the variable names, here and in the lu header.. I've had to go back n forth to the first patch. For example, herenext_objscan benext_objs_page& objs can becurr_page. (PTAL at my reply on PATCH 01 about renaming).
Basically with current naming there are "serialization objects" in "serializatoin object arrays". The object is a "base" type with "inherited" types for iommu, domain etc. I will add explanation of all this in the ABI header.
Maybe we can name it to:
struct iommu_obj_ser;
struct iommu_obj_array_ser; or struct iommu_obj_ser_array;
Then for reserve/alloc, we use names like "next_obj_array" and "curr_obj_array"?
Quoted 16 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 292
Quoted 13 lines
drivers/iommu/liveupdate.c +12
-0+int idx; + +if (objs->nr_objs == max_objs) { + next_objs =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (next_objs)) +return PTR_ERR (next_objs); + + objs->next_objs =virt_to_phys (next_objs); + objs = next_objs; + *objs_ptr = objs; + objs->nr_objs =0 ; + objs->next_objs =0 ;This seems redundant, no need to zero these out,
kho_alloc_preservepasses__GFP_ZEROtofolio_alloc[1], which should z-alloc the pages.
Agreed. Will remove this.
Quoted 33 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 297
Quoted 6 lines
drivers/iommu/liveupdate.c +5
-0+ } + + idx = objs->nr_objs++; +return idx; +}Just to give a mental model to fellow reviewers, here's how this is laid out:
----------------------------------------------------------------------
[ PAGE START ] |
----------------------------------------------------------------------| iommu_objs_ser (The Page Header) | | - next_objs: 0x0000 (End of the page-chain) | | - nr_objs: 2 | ---------------------------------------------------------------------- | ITEM 0: iommu_domain_ser | | [ iommu_obj_ser (The entry header) ] | | - idx: 0 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ITEM 1: iommu_domain_ser | | [ iommu_obj_ser (The Price Tag) ] | | - idx: 1 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ... (Empty space for more domains) ... | | |
Quoted 3 lines
----------------------------------------------------------------------
[ PAGE END ] |
----------------------------------------------------------------------
+1
Will add a table in the header as you suggested.
Quoted 25 lines
Quoted 12 lines
drivers/iommu/liveupdate.c +11
-0+ +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int idx, ret; + +if (!domain->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj);drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 318
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommu_domains, +MAX_IOMMU_DOMAIN_SERS ); +if (idx <0 ) +return idx; + + domain_ser = &flb_obj->iommu_domains->iommu_domains[idx];This is slightly less-readable as well, I understand we're trying to:
Quoted 2 lines
iommu_domains_ser-> iommu_domain_ser[idx] but the same name (iommu_domains) makes it difficult to read.. we should rename this as:
Agreed.
I will update it to,
&flb_obj->curr_iommu_domains->domain_array[idx]
Quoted 6 lines
&flb_obj->iommu_domains_page->domain_entries[idx] or something for better readability..
Also, let's add a comment explaining that
reserve_obj_seractually advances theflb_objptr when necessary..
Agreed.
Quoted 6 lines
IIUC, we start with PAGE 0 initially, store it's phys in the
iommu_flb_preserveop (theiommu_ser_physet al) & then we go on alloc-ing more pages and keep storing the "current" active page with the liveupdate core. Now when we jump into the new kernel, we read theser_physand then follow the page chain, right?
Yes, we do an initial allocation of arrays for each object type and then later allocate more as needed. The flb_obj holds the address of currently active array.
I will add this explanation in the header.
Quoted 202 lines
Quoted 176 lines
drivers/iommu/liveupdate.c +175
-0+ idx = flb_obj->ser->nr_domains++; + domain_ser->obj.idx = idx; + domain_ser->obj.ref_count =1 ; + + ret = domain->ops->preserve (domain, domain_ser); +if (ret) { + domain_ser->obj.deleted = true; +return ret; + } + + domain->preserved_state = domain_ser; + *ser = domain_ser; +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_domain_preserve); + +void iommu_domain_unpreserve (struct iommu_domain *domain) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int ret; + +if (!domain->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ; + +guard (mutex)(&flb_obj->lock); + +/* +* 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; + domain->ops->unpreserve (domain, domain_ser); + domain_ser->obj.deleted = true; + domain->preserved_state =NULL ; +} +EXPORT_SYMBOL_GPL (iommu_domain_unpreserve); + +static int iommu_preserve_locked (struct iommu_device *iommu) +{ +struct iommu_lu_flb_obj *flb_obj; +struct iommu_ser *iommu_ser; +int idx, ret; + +if (!iommu->ops->preserve) +return -EOPNOTSUPP ; + +if (iommu->outgoing_preserved_state) { + iommu->outgoing_preserved_state->obj.ref_count++; +return 0 ; + } + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommus, +MAX_IOMMU_SERS ); +if (idx <0 ) +return idx; + + iommu_ser = &flb_obj->iommus->iommus[idx]; + idx = flb_obj->ser->nr_iommus++; + iommu_ser->obj.idx = idx; + iommu_ser->obj.ref_count =1 ; + + ret = iommu->ops->preserve (iommu, iommu_ser); +if (ret) + iommu_ser->obj.deleted = true; + + iommu->outgoing_preserved_state = iommu_ser; +return ret; +} + +static void iommu_unpreserve_locked (struct iommu_device *iommu) +{ +struct iommu_ser *iommu_ser = iommu->outgoing_preserved_state; + + iommu_ser->obj.ref_count--; +if (iommu_ser->obj.ref_count) +return ; + + iommu->outgoing_preserved_state =NULL ; + iommu->ops->unpreserve (iommu, iommu_ser); + iommu_ser->obj.deleted = true; +} + +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret, idx; + +if (!dev_is_pci (dev)) +return -EOPNOTSUPP ; + +if (!domain->preserved_state) +return -EINVAL ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->preserve_device || + !iommu->iommu_dev->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->devices, +MAX_DEVICE_SERS ); +if (idx <0 ) +return idx; + + device_ser = &flb_obj->devices->devices[idx]; + idx = flb_obj->ser->nr_devices++; + device_ser->obj.idx = idx; + device_ser->obj.ref_count =1 ; + + ret =iommu_preserve_locked (iommu->iommu_dev); +if (ret) { + device_ser->obj.deleted = true; +return ret; + } + + device_ser->domain_iommu_ser.domain_phys =__pa (domain->preserved_state); + device_ser->domain_iommu_ser.iommu_phys =__pa (iommu->iommu_dev->outgoing_preserved_state); + device_ser->devid =pci_dev_id (pdev); + device_ser->pci_domain =pci_domain_nr (pdev->bus); + device_ser->token = token; + + ret = iommu->iommu_dev->ops->preserve_device (dev, device_ser); +if (ret) { + device_ser->obj.deleted = true; +iommu_unpreserve_locked (iommu->iommu_dev); +return ret; + } + + dev->iommu->device_ser = device_ser; +return 0 ; +} + +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret; + +if (!dev_is_pci (dev)) +return ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->unpreserve_device || + !iommu->iommu_dev->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (WARN_ON (ret)) +return ; +drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 503
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+guard (mutex)(&flb_obj->lock); + device_ser =dev_iommu_preserved_state (dev); +if (WARN_ON (!device_ser)) +return ; + + iommu->iommu_dev->ops->unpreserve_device (dev, device_ser); + dev->iommu->device_ser =NULL ; + +iommu_unpreserve_locked (iommu->iommu_dev); +}I'm wondering ig we should guard these APIs against accidental or potential abuse by other in-kernel drivers. Since the Live Update Orchestrator (LUO) is architecturally designed around user-space driven sequences (IOCTLs, specific mutex ordering, etc.), for now.
Since the header-file is also under
include/linux, we should avoid any possibility where we end up having drivers depending on these API. We could add a check based on dma owner:(quoted diff) +5
-0+ /* Only devices explicitly claimed by a user-space driver + * (VFIO/IOMMUFD) are eligible for Live Update preservation. + */ + if (!iommu_dma_owner_claimed(dev)) + return -EPERM;
Quoted 2 lines
This should ensure we aren't creating 'zombie' preserved states for devices not managed by IOMMUFD/VFIO.
Agreed. I will update this.
Quoted 174 lines
Quoted 174 lines
include/linux/iommu-lu.h +119
-0@@ -8,9 +8,128 @@#ifndef _LINUX_IOMMU_LU_H#define _LINUX_IOMMU_LU_H +#include <linux/device.h> +#include <linux/iommu.h> #include <linux/liveupdate.h> #include <linux/kho/abi/iommu.h> +typedef int (*iommu_preserved_device_iter_fn )(struct device_ser *ser, +void *arg); +#ifdef CONFIG_IOMMU_LIVEUPDATE +static inline void *dev_iommu_preserved_state (struct device *dev) +{ +struct device_ser *ser; + +if (!dev->iommu) +return NULL ; + + ser = dev->iommu->device_ser; +if (ser && !ser->obj.incoming) +return ser; + +return NULL ; +} + +static inline void *dev_iommu_restored_state (struct device *dev) +{ +struct device_ser *ser; + +if (!dev->iommu) +return NULL ; + + ser = dev->iommu->device_ser; +if (ser && ser->obj.incoming) +return ser; + +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->obj.incoming) +return ser; + +return NULL ; +} + +static inline int dev_iommu_restore_did (struct device *dev,struct iommu_domain *domain) +{ +struct device_ser *ser =dev_iommu_restored_state (dev); + +if (ser &&iommu_domain_restored_state (domain)) +return ser->domain_iommu_ser.did; + +return -1 ; +} + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg); +struct device_ser *iommu_get_device_preserved_data (struct device *dev); +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type); +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser); +void iommu_domain_unpreserve (struct iommu_domain *domain); +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token); +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev); +#else +static inline void *dev_iommu_preserved_state (struct device *dev) +{ +return NULL ; +} + +static inline void *dev_iommu_restored_state (struct device *dev) +{ +return NULL ; +} + +static inline int dev_iommu_restore_did (struct device *dev,struct iommu_domain *domain) +{ +return -1 ; +} + +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +return NULL ; +} + +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) +{ +return -EOPNOTSUPP ; +} + +static inline struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +return NULL ; +} + +static inline struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +return NULL ; +} + +static inline int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_domain_unpreserve (struct iommu_domain *domain) +{ +} + +static inline int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +} +#endif +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler);int iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler);include/linux/iommu.h +10
-0@@ -14,6 +14,8 @@#include <linux/err.h> #include <linux/of.h> #include <linux/iova_bitmap.h> +#include <linux/atomic.h> +#include <linux/kho/abi/iommu.h> #include <uapi/linux/iommufd.h> #define IOMMU_READ (1 << 0)@@ -248,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)@@ -647,6 +653,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. @@ -703,6 +713,11 @@ struct iommu_ops {struct iommu_domain *parent_domain, const struct iommu_user_data *user_data);
Quoted 5 lines
include/linux/iommu.h struct iommu_ops { · line 717
Quoted 2 lines
include/linux/iommu.h +2
-0+int (*preserve_device)(struct device *dev,struct device_ser *device_ser); +void (*unpreserve_device)(struct device *dev,struct device_ser *device_ser);Nit: Let's move the
_deviceops under the comment:/* Per device IOMMU features */
I will move these.
Quoted 6 lines
include/linux/iommu.h struct iommu_ops { · line 720
Quoted 4 lines
include/linux/iommu.h +3
-0+int (*preserve)(struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void (*unpreserve)(struct iommu_device *iommu,struct iommu_ser *iommu_ser); +I'm wondering if there's any benefit to add these ops under a #ifdef ?
These should be NULL when liveupdate is disabled, so #ifdef should not be needed. Not sure how much space we save if don't define these. I will move these in #ifdef anyway.
Quoted 61 lines
Quoted 47 lines
include/linux/iommu.h +14
-0const struct iommu_domain_ops *default_domain_ops;struct module *owner;struct iommu_domain *identity_domain;@@ -749,6 +764,11 @@ struct iommu_ops {* specific mechanisms. * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*) * @free: Release the domain after use. +* @preserve: Preserve the iommu domain for liveupdate. +* Returns 0 on success, a negative errno on failure. +* @unpreserve: Unpreserve the iommu domain that was preserved earlier. +* @restore: Restore the iommu domain after liveupdate. +* Returns 0 on success, a negative errno on failure. */ struct iommu_domain_ops { int (*attach_dev)(struct iommu_domain *domain, struct device *dev,@@ -779,6 +799,9 @@ struct iommu_domain_ops { unsigned long quirks);void (*free)(struct iommu_domain *domain); +int (*preserve)(struct iommu_domain *domain,struct iommu_domain_ser *ser); +void (*unpreserve)(struct iommu_domain *domain,struct iommu_domain_ser *ser); +int (*restore)(struct iommu_domain *domain,struct iommu_domain_ser *ser); };/** @@ -790,6 +813,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;@@ -799,6 +824,10 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_ser *outgoing_preserved_state; +#endif };/** include/linux/iommu.h struct dev_iommu { · line 890
Quoted 9 lines
include/linux/iommu.h +3
-0@@ -853,6 +882,9 @@ struct dev_iommu { u32 pci_32bit_workaround:1; u32 require_direct:1; u32 shadow_on_flush:1; +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct device_ser *device_ser; +#endif }; int iommu_device_register(struct iommu_device *iommu,Thanks,
Praan[1] https://elixir.bootlin.com/linux/v7.0-rc3/source/kernel/liveupdate/kexec_handover.c#L1182
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
Quoted 148 lines · Samiullah Khawaja
Quoted 147 lines · Pranjal Shrivastava
Quoted 146 lines · Samiullah Khawaja
Add IOMMU domain ops that can be implemented by the IOMMU drivers if they support IOMMU domain preservation across liveupdate. The new IOMMU domain preserve, unpreserve and restore APIs call these ops to perform respective live update operations.
Similarly add IOMMU ops to preserve/unpreserve a device. These 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. The device can only be preserved if the attached iommu domain is preserved and the associated iommu supports preservation.
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.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/iommu.c | 3 + drivers/iommu/liveupdate.c | 326 +++++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 119 ++++++++++++++ include/linux/iommu.h | 32 ++++ 4 files changed, 480 insertions(+)drivers/iommu/iommu.c +3
-0@@ -389,6 +389,9 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)mutex_init (¶m->lock); dev->iommu = param; +#ifdef CONFIG_IOMMU_LIVEUPDATE + dev->iommu->device_ser =NULL ; +#endif return param; }drivers/iommu/liveupdate.c +92
-0@@ -11,6 +11,7 @@#include <linux/liveupdate.h> #include <linux/iommu-lu.h> #include <linux/iommu.h> +#include <linux/pci.h> #include <linux/errno.h> static void iommu_liveupdate_restore_objs (u64 next)@@ -175,3 +176,328 @@ int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)return liveupdate_unregister_flb (handler, &iommu_flb); }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return -ENOENT ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + + ret =fn (&devices->devices[idx], arg); +if (ret) +return ret; + } + +return 0 ; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device); + +static inline bool device_ser_match (struct device_ser *match, +struct pci_dev *pdev) +{ +return match->devid ==pci_dev_id (pdev) && match->pci_domain ==pci_domain_nr (pdev->bus); +} + +struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + +if (!dev_is_pci (dev)) +return NULL ; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + +if (device_ser_match (&devices->devices[idx],to_pci_dev (dev))) { + devices->devices[idx].obj.incoming = true; +return &devices->devices[idx]; + } + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_device_preserved_data); + +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +struct iommu_lu_flb_obj *obj; +struct iommus_ser *iommus; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + iommus =__va (obj->ser->iommus_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_iommus; ++i, ++idx) { +if (idx >=MAX_IOMMU_SERS ) { + iommus =__va (iommus->objs.next_objs); + idx =0 ; + } + +if (iommus->iommus[idx].obj.deleted) +continue ; + + if (iommus->iommus[idx].token == token &&
Quoted 8 lines
Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 277
Quoted 8 lines
drivers/iommu/liveupdate.c +8
-0+ iommus->iommus[idx].type == type) +return &iommus->iommus[idx]; + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_preserved_data); +
Also, I don't see these helpers being used anywhere in this series yet? Should we add them with the phase-2 series? When we actually "Restore" the domain?
Thanks,
Praan
Quoted 600 lines
Quoted 4 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 278
Quoted 1 line
drivers/iommu/liveupdate.c +1
-0+static int reserve_obj_ser (struct iommu_objs_ser **objs_ptr,u64 max_objs)Isn't this more of an "insert" / "populate" /
write_ser_entry? We can rename it to something likeiommu_ser_push_entry/iommu_ser_write_entryThis is reserving an object in the objects array. The object is filled once reservation is done. Maybe I can call it
alloc_obj_seroralloc_entry_ser.Quoted 10 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 280
Quoted 3 lines
drivers/iommu/liveupdate.c +2
-0+{ +struct iommu_objs_ser *next_objs, *objs = *objs_ptr;Not loving these names :(
TBH, the
reserve_obj_serfunction isn't too readable, esp. with all the variable names, here and in the lu header.. I've had to go back n forth to the first patch. For example, herenext_objscan benext_objs_page& objs can becurr_page. (PTAL at my reply on PATCH 01 about renaming).Basically with current naming there are "serialization objects" in "serializatoin object arrays". The object is a "base" type with "inherited" types for iommu, domain etc. I will add explanation of all this in the ABI header.
Maybe we can name it to:
struct
iommu_obj_ser;
structiommu_obj_array_ser; or structiommu_obj_ser_array;Then for reserve/alloc, we use names like "
next_obj_array" and "curr_obj_array"?Quoted 16 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 292
Quoted 13 lines
drivers/iommu/liveupdate.c +12
-0+int idx; + +if (objs->nr_objs == max_objs) { + next_objs =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (next_objs)) +return PTR_ERR (next_objs); + + objs->next_objs =virt_to_phys (next_objs); + objs = next_objs; + *objs_ptr = objs; + objs->nr_objs =0 ; + objs->next_objs =0 ;This seems redundant, no need to zero these out,
kho_alloc_preservepasses__GFP_ZEROtofolio_alloc[1], which should z-alloc the pages.Agreed. Will remove this.
Quoted 33 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 297
Quoted 6 lines
drivers/iommu/liveupdate.c +5
-0+ } + + idx = objs->nr_objs++; +return idx; +}Just to give a mental model to fellow reviewers, here's how this is laid out:
----------------------------------------------------------------------
[ PAGE START ] |
----------------------------------------------------------------------| iommu_objs_ser (The Page Header) | | - next_objs: 0x0000 (End of the page-chain) | | - nr_objs: 2 | ---------------------------------------------------------------------- | ITEM 0: iommu_domain_ser | | [ iommu_obj_ser (The entry header) ] | | - idx: 0 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ITEM 1: iommu_domain_ser | | [ iommu_obj_ser (The Price Tag) ] | | - idx: 1 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ... (Empty space for more domains) ... | | |Quoted 3 lines
----------------------------------------------------------------------
[ PAGE END ] |
----------------------------------------------------------------------+1
Will add a table in the header as you suggested.
Quoted 25 lines
Quoted 12 lines
drivers/iommu/liveupdate.c +11
-0+ +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int idx, ret; + +if (!domain->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj);drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 318
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommu_domains, +MAX_IOMMU_DOMAIN_SERS ); +if (idx <0 ) +return idx; + + domain_ser = &flb_obj->iommu_domains->iommu_domains[idx];This is slightly less-readable as well, I understand we're trying to:
Quoted 2 lines
iommu_domains_ser-> iommu_domain_ser[idx] but the same name (iommu_domains) makes it difficult to read.. we should rename this as:Agreed.
I will update it to,
&flb_obj->curr_iommu_domains->domain_array[idx]
Quoted 6 lines
&flb_obj->iommu_domains_page->domain_entries[idx] or something for better readability..
Also, let's add a comment explaining that
reserve_obj_seractually advances theflb_objptr when necessary..Agreed.
Quoted 6 lines
IIUC, we start with PAGE 0 initially, store it's phys in the
iommu_flb_preserveop (theiommu_ser_physet al) & then we go on alloc-ing more pages and keep storing the "current" active page with the liveupdate core. Now when we jump into the new kernel, we read theser_physand then follow the page chain, right?Yes, we do an initial allocation of arrays for each object type and then later allocate more as needed. The
flb_objholds the address of currently active array.I will add this explanation in the header.
Quoted 202 lines
Quoted 176 lines
drivers/iommu/liveupdate.c +175
-0+ idx = flb_obj->ser->nr_domains++; + domain_ser->obj.idx = idx; + domain_ser->obj.ref_count =1 ; + + ret = domain->ops->preserve (domain, domain_ser); +if (ret) { + domain_ser->obj.deleted = true; +return ret; + } + + domain->preserved_state = domain_ser; + *ser = domain_ser; +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_domain_preserve); + +void iommu_domain_unpreserve (struct iommu_domain *domain) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int ret; + +if (!domain->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ; + +guard (mutex)(&flb_obj->lock); + +/* +* 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; + domain->ops->unpreserve (domain, domain_ser); + domain_ser->obj.deleted = true; + domain->preserved_state =NULL ; +} +EXPORT_SYMBOL_GPL (iommu_domain_unpreserve); + +static int iommu_preserve_locked (struct iommu_device *iommu) +{ +struct iommu_lu_flb_obj *flb_obj; +struct iommu_ser *iommu_ser; +int idx, ret; + +if (!iommu->ops->preserve) +return -EOPNOTSUPP ; + +if (iommu->outgoing_preserved_state) { + iommu->outgoing_preserved_state->obj.ref_count++; +return 0 ; + } + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommus, +MAX_IOMMU_SERS ); +if (idx <0 ) +return idx; + + iommu_ser = &flb_obj->iommus->iommus[idx]; + idx = flb_obj->ser->nr_iommus++; + iommu_ser->obj.idx = idx; + iommu_ser->obj.ref_count =1 ; + + ret = iommu->ops->preserve (iommu, iommu_ser); +if (ret) + iommu_ser->obj.deleted = true; + + iommu->outgoing_preserved_state = iommu_ser; +return ret; +} + +static void iommu_unpreserve_locked (struct iommu_device *iommu) +{ +struct iommu_ser *iommu_ser = iommu->outgoing_preserved_state; + + iommu_ser->obj.ref_count--; +if (iommu_ser->obj.ref_count) +return ; + + iommu->outgoing_preserved_state =NULL ; + iommu->ops->unpreserve (iommu, iommu_ser); + iommu_ser->obj.deleted = true; +} + +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret, idx; + +if (!dev_is_pci (dev)) +return -EOPNOTSUPP ; + +if (!domain->preserved_state) +return -EINVAL ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->preserve_device || + !iommu->iommu_dev->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->devices, +MAX_DEVICE_SERS ); +if (idx <0 ) +return idx; + + device_ser = &flb_obj->devices->devices[idx]; + idx = flb_obj->ser->nr_devices++; + device_ser->obj.idx = idx; + device_ser->obj.ref_count =1 ; + + ret =iommu_preserve_locked (iommu->iommu_dev); +if (ret) { + device_ser->obj.deleted = true; +return ret; + } + + device_ser->domain_iommu_ser.domain_phys =__pa (domain->preserved_state); + device_ser->domain_iommu_ser.iommu_phys =__pa (iommu->iommu_dev->outgoing_preserved_state); + device_ser->devid =pci_dev_id (pdev); + device_ser->pci_domain =pci_domain_nr (pdev->bus); + device_ser->token = token; + + ret = iommu->iommu_dev->ops->preserve_device (dev, device_ser); +if (ret) { + device_ser->obj.deleted = true; +iommu_unpreserve_locked (iommu->iommu_dev); +return ret; + } + + dev->iommu->device_ser = device_ser; +return 0 ; +} + +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret; + +if (!dev_is_pci (dev)) +return ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->unpreserve_device || + !iommu->iommu_dev->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (WARN_ON (ret)) +return ; +drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 503
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+guard (mutex)(&flb_obj->lock); + device_ser =dev_iommu_preserved_state (dev); +if (WARN_ON (!device_ser)) +return ; + + iommu->iommu_dev->ops->unpreserve_device (dev, device_ser); + dev->iommu->device_ser =NULL ; + +iommu_unpreserve_locked (iommu->iommu_dev); +}I'm wondering ig we should guard these APIs against accidental or potential abuse by other in-kernel drivers. Since the Live Update Orchestrator (LUO) is architecturally designed around user-space driven sequences (IOCTLs, specific mutex ordering, etc.), for now.
Since the header-file is also under
include/linux, we should avoid any possibility where we end up having drivers depending on these API. We could add a check based on dma owner:(quoted diff) +5
-0+ /* Only devices explicitly claimed by a user-space driver + * (VFIO/IOMMUFD) are eligible for Live Update preservation. + */ + if (!iommu_dma_owner_claimed(dev)) + return -EPERM;Quoted 2 lines
This should ensure we aren't creating 'zombie' preserved states for devices not managed by IOMMUFD/VFIO.
Agreed. I will update this.
Quoted 174 lines
Quoted 174 lines
include/linux/iommu-lu.h +119
-0@@ -8,9 +8,128 @@#ifndef _LINUX_IOMMU_LU_H#define _LINUX_IOMMU_LU_H +#include <linux/device.h> +#include <linux/iommu.h> #include <linux/liveupdate.h> #include <linux/kho/abi/iommu.h> +typedef int (*iommu_preserved_device_iter_fn )(struct device_ser *ser, +void *arg); +#ifdef CONFIG_IOMMU_LIVEUPDATE +static inline void *dev_iommu_preserved_state (struct device *dev) +{ +struct device_ser *ser; + +if (!dev->iommu) +return NULL ; + + ser = dev->iommu->device_ser; +if (ser && !ser->obj.incoming) +return ser; + +return NULL ; +} + +static inline void *dev_iommu_restored_state (struct device *dev) +{ +struct device_ser *ser; + +if (!dev->iommu) +return NULL ; + + ser = dev->iommu->device_ser; +if (ser && ser->obj.incoming) +return ser; + +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->obj.incoming) +return ser; + +return NULL ; +} + +static inline int dev_iommu_restore_did (struct device *dev,struct iommu_domain *domain) +{ +struct device_ser *ser =dev_iommu_restored_state (dev); + +if (ser &&iommu_domain_restored_state (domain)) +return ser->domain_iommu_ser.did; + +return -1 ; +} + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg); +struct device_ser *iommu_get_device_preserved_data (struct device *dev); +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type); +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser); +void iommu_domain_unpreserve (struct iommu_domain *domain); +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token); +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev); +#else +static inline void *dev_iommu_preserved_state (struct device *dev) +{ +return NULL ; +} + +static inline void *dev_iommu_restored_state (struct device *dev) +{ +return NULL ; +} + +static inline int dev_iommu_restore_did (struct device *dev,struct iommu_domain *domain) +{ +return -1 ; +} + +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +return NULL ; +} + +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) +{ +return -EOPNOTSUPP ; +} + +static inline struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +return NULL ; +} + +static inline struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +return NULL ; +} + +static inline int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_domain_unpreserve (struct iommu_domain *domain) +{ +} + +static inline int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +} +#endif +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler);int iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler);include/linux/iommu.h +10
-0@@ -14,6 +14,8 @@#include <linux/err.h> #include <linux/of.h> #include <linux/iova_bitmap.h> +#include <linux/atomic.h> +#include <linux/kho/abi/iommu.h> #include <uapi/linux/iommufd.h> #define IOMMU_READ (1 << 0)@@ -248,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)@@ -647,6 +653,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. @@ -703,6 +713,11 @@ struct iommu_ops {struct iommu_domain *parent_domain, const struct iommu_user_data *user_data);Quoted 5 lines
include/linux/iommu.h struct iommu_ops { · line 717
Quoted 2 lines
include/linux/iommu.h +2
-0+int (*preserve_device)(struct device *dev,struct device_ser *device_ser); +void (*unpreserve_device)(struct device *dev,struct device_ser *device_ser);Nit: Let's move the
_deviceops under the comment:/* Per device IOMMU features */I will move these.
Quoted 6 lines
include/linux/iommu.h struct iommu_ops { · line 720
Quoted 4 lines
include/linux/iommu.h +3
-0+int (*preserve)(struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void (*unpreserve)(struct iommu_device *iommu,struct iommu_ser *iommu_ser); +I'm wondering if there's any benefit to add these ops under a #ifdef ?
These should be NULL when liveupdate is disabled, so #ifdef should not be needed. Not sure how much space we save if don't define these. I will move these in #ifdef anyway.
Quoted 61 lines
Quoted 47 lines
include/linux/iommu.h +14
-0const struct iommu_domain_ops *default_domain_ops;struct module *owner;struct iommu_domain *identity_domain;@@ -749,6 +764,11 @@ struct iommu_ops {* specific mechanisms. * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*) * @free: Release the domain after use. +* @preserve: Preserve the iommu domain for liveupdate. +* Returns 0 on success, a negative errno on failure. +* @unpreserve: Unpreserve the iommu domain that was preserved earlier. +* @restore: Restore the iommu domain after liveupdate. +* Returns 0 on success, a negative errno on failure. */ struct iommu_domain_ops { int (*attach_dev)(struct iommu_domain *domain, struct device *dev,@@ -779,6 +799,9 @@ struct iommu_domain_ops { unsigned long quirks);void (*free)(struct iommu_domain *domain); +int (*preserve)(struct iommu_domain *domain,struct iommu_domain_ser *ser); +void (*unpreserve)(struct iommu_domain *domain,struct iommu_domain_ser *ser); +int (*restore)(struct iommu_domain *domain,struct iommu_domain_ser *ser); };/** @@ -790,6 +813,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;@@ -799,6 +824,10 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_ser *outgoing_preserved_state; +#endif };/** include/linux/iommu.h struct dev_iommu { · line 890
Quoted 9 lines
include/linux/iommu.h +3
-0@@ -853,6 +882,9 @@ struct dev_iommu { u32 pci_32bit_workaround:1; u32 require_direct:1; u32 shadow_on_flush:1; +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct device_ser *device_ser; +#endif }; int iommu_device_register(struct iommu_device *iommu,Thanks,
Praan[1] https://elixir.bootlin.com/linux/v7.0-rc3/source/kernel/liveupdate/kexec_handover.c#L1182
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
Quoted 153 lines · Pranjal Shrivastava
Quoted 152 lines · Samiullah Khawaja
Quoted 151 lines · Pranjal Shrivastava
Quoted 150 lines · Samiullah Khawaja
Add IOMMU domain ops that can be implemented by the IOMMU drivers if they support IOMMU domain preservation across liveupdate. The new IOMMU domain preserve, unpreserve and restore APIs call these ops to perform respective live update operations.
Similarly add IOMMU ops to preserve/unpreserve a device. These 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. The device can only be preserved if the attached iommu domain is preserved and the associated iommu supports preservation.
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.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/iommu.c | 3 + drivers/iommu/liveupdate.c | 326 +++++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 119 ++++++++++++++ include/linux/iommu.h | 32 ++++ 4 files changed, 480 insertions(+)drivers/iommu/iommu.c +3
-0@@ -389,6 +389,9 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)mutex_init (¶m->lock); dev->iommu = param; +#ifdef CONFIG_IOMMU_LIVEUPDATE + dev->iommu->device_ser =NULL ; +#endif return param; }drivers/iommu/liveupdate.c +96
-0@@ -11,6 +11,7 @@#include <linux/liveupdate.h> #include <linux/iommu-lu.h> #include <linux/iommu.h> +#include <linux/pci.h> #include <linux/errno.h> static void iommu_liveupdate_restore_objs (u64 next)@@ -175,3 +176,328 @@ int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)return liveupdate_unregister_flb (handler, &iommu_flb); }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return -ENOENT ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + + ret =fn (&devices->devices[idx], arg); +if (ret) +return ret; + } + +return 0 ; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device); + +static inline bool device_ser_match (struct device_ser *match, +struct pci_dev *pdev) +{ +return match->devid ==pci_dev_id (pdev) && match->pci_domain ==pci_domain_nr (pdev->bus); +} + +struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + +if (!dev_is_pci (dev)) +return NULL ; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + +if (device_ser_match (&devices->devices[idx],to_pci_dev (dev))) { + devices->devices[idx].obj.incoming = true; +return &devices->devices[idx]; + } + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_device_preserved_data); + +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +struct iommu_lu_flb_obj *obj; +struct iommus_ser *iommus; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + iommus =__va (obj->ser->iommus_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_iommus; ++i, ++idx) { +if (idx >=MAX_IOMMU_SERS ) { + iommus =__va (iommus->objs.next_objs); + idx =0 ; + } + +if (iommus->iommus[idx].obj.deleted) +continue ; + +if (iommus->iommus[idx].token == token && + iommus->iommus[idx].type == type) +return &iommus->iommus[idx]; + } +
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 277
Quoted 4 lines
drivers/iommu/liveupdate.c +4
-0+return NULL ; +} +EXPORT_SYMBOL (iommu_get_preserved_data); +Also, I don't see these helpers being used anywhere in this series yet? Should we add them with the phase-2 series? When we actually "Restore" the domain?
Phase 1 does restore the iommu and domains during boot. But the iommufd/hwpt state is not restored or re-associated with the restored domains.
These helpers are used to fetch the preserved state during boot.
Thanks,
Sami
Quoted 604 lines
Thanks,
PraanQuoted 600 lines
Quoted 4 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 278
Quoted 1 line
drivers/iommu/liveupdate.c +1
-0+static int reserve_obj_ser (struct iommu_objs_ser **objs_ptr,u64 max_objs)Isn't this more of an "insert" / "populate" /
write_ser_entry? We can rename it to something likeiommu_ser_push_entry/iommu_ser_write_entryThis is reserving an object in the objects array. The object is filled once reservation is done. Maybe I can call it
alloc_obj_seroralloc_entry_ser.Quoted 10 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 280
Quoted 3 lines
drivers/iommu/liveupdate.c +2
-0+{ +struct iommu_objs_ser *next_objs, *objs = *objs_ptr;Not loving these names :(
TBH, the
reserve_obj_serfunction isn't too readable, esp. with all the variable names, here and in the lu header.. I've had to go back n forth to the first patch. For example, herenext_objscan benext_objs_page& objs can becurr_page. (PTAL at my reply on PATCH 01 about renaming).Basically with current naming there are "serialization objects" in "serializatoin object arrays". The object is a "base" type with "inherited" types for iommu, domain etc. I will add explanation of all this in the ABI header.
Maybe we can name it to:
struct
iommu_obj_ser;
structiommu_obj_array_ser; or structiommu_obj_ser_array;Then for reserve/alloc, we use names like "
next_obj_array" and "curr_obj_array"?Quoted 16 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 292
Quoted 13 lines
drivers/iommu/liveupdate.c +12
-0+int idx; + +if (objs->nr_objs == max_objs) { + next_objs =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (next_objs)) +return PTR_ERR (next_objs); + + objs->next_objs =virt_to_phys (next_objs); + objs = next_objs; + *objs_ptr = objs; + objs->nr_objs =0 ; + objs->next_objs =0 ;This seems redundant, no need to zero these out,
kho_alloc_preservepasses__GFP_ZEROtofolio_alloc[1], which should z-alloc the pages.Agreed. Will remove this.
Quoted 33 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 297
Quoted 6 lines
drivers/iommu/liveupdate.c +5
-0+ } + + idx = objs->nr_objs++; +return idx; +}Just to give a mental model to fellow reviewers, here's how this is laid out:
----------------------------------------------------------------------
[ PAGE START ] |
----------------------------------------------------------------------| iommu_objs_ser (The Page Header) | | - next_objs: 0x0000 (End of the page-chain) | | - nr_objs: 2 | ---------------------------------------------------------------------- | ITEM 0: iommu_domain_ser | | [ iommu_obj_ser (The entry header) ] | | - idx: 0 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ITEM 1: iommu_domain_ser | | [ iommu_obj_ser (The Price Tag) ] | | - idx: 1 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ... (Empty space for more domains) ... | | |Quoted 3 lines
----------------------------------------------------------------------
[ PAGE END ] |
----------------------------------------------------------------------+1
Will add a table in the header as you suggested.
Quoted 25 lines
Quoted 12 lines
drivers/iommu/liveupdate.c +11
-0+ +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int idx, ret; + +if (!domain->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj);drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 318
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommu_domains, +MAX_IOMMU_DOMAIN_SERS ); +if (idx <0 ) +return idx; + + domain_ser = &flb_obj->iommu_domains->iommu_domains[idx];This is slightly less-readable as well, I understand we're trying to:
Quoted 2 lines
iommu_domains_ser-> iommu_domain_ser[idx] but the same name (iommu_domains) makes it difficult to read.. we should rename this as:Agreed.
I will update it to,
&flb_obj->curr_iommu_domains->domain_array[idx]
Quoted 6 lines
&flb_obj->iommu_domains_page->domain_entries[idx] or something for better readability..
Also, let's add a comment explaining that
reserve_obj_seractually advances theflb_objptr when necessary..Agreed.
Quoted 6 lines
IIUC, we start with PAGE 0 initially, store it's phys in the
iommu_flb_preserveop (theiommu_ser_physet al) & then we go on alloc-ing more pages and keep storing the "current" active page with the liveupdate core. Now when we jump into the new kernel, we read theser_physand then follow the page chain, right?Yes, we do an initial allocation of arrays for each object type and then later allocate more as needed. The
flb_objholds the address of currently active array.I will add this explanation in the header.
Quoted 202 lines
Quoted 176 lines
drivers/iommu/liveupdate.c +175
-0+ idx = flb_obj->ser->nr_domains++; + domain_ser->obj.idx = idx; + domain_ser->obj.ref_count =1 ; + + ret = domain->ops->preserve (domain, domain_ser); +if (ret) { + domain_ser->obj.deleted = true; +return ret; + } + + domain->preserved_state = domain_ser; + *ser = domain_ser; +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_domain_preserve); + +void iommu_domain_unpreserve (struct iommu_domain *domain) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int ret; + +if (!domain->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ; + +guard (mutex)(&flb_obj->lock); + +/* +* 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; + domain->ops->unpreserve (domain, domain_ser); + domain_ser->obj.deleted = true; + domain->preserved_state =NULL ; +} +EXPORT_SYMBOL_GPL (iommu_domain_unpreserve); + +static int iommu_preserve_locked (struct iommu_device *iommu) +{ +struct iommu_lu_flb_obj *flb_obj; +struct iommu_ser *iommu_ser; +int idx, ret; + +if (!iommu->ops->preserve) +return -EOPNOTSUPP ; + +if (iommu->outgoing_preserved_state) { + iommu->outgoing_preserved_state->obj.ref_count++; +return 0 ; + } + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommus, +MAX_IOMMU_SERS ); +if (idx <0 ) +return idx; + + iommu_ser = &flb_obj->iommus->iommus[idx]; + idx = flb_obj->ser->nr_iommus++; + iommu_ser->obj.idx = idx; + iommu_ser->obj.ref_count =1 ; + + ret = iommu->ops->preserve (iommu, iommu_ser); +if (ret) + iommu_ser->obj.deleted = true; + + iommu->outgoing_preserved_state = iommu_ser; +return ret; +} + +static void iommu_unpreserve_locked (struct iommu_device *iommu) +{ +struct iommu_ser *iommu_ser = iommu->outgoing_preserved_state; + + iommu_ser->obj.ref_count--; +if (iommu_ser->obj.ref_count) +return ; + + iommu->outgoing_preserved_state =NULL ; + iommu->ops->unpreserve (iommu, iommu_ser); + iommu_ser->obj.deleted = true; +} + +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret, idx; + +if (!dev_is_pci (dev)) +return -EOPNOTSUPP ; + +if (!domain->preserved_state) +return -EINVAL ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->preserve_device || + !iommu->iommu_dev->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->devices, +MAX_DEVICE_SERS ); +if (idx <0 ) +return idx; + + device_ser = &flb_obj->devices->devices[idx]; + idx = flb_obj->ser->nr_devices++; + device_ser->obj.idx = idx; + device_ser->obj.ref_count =1 ; + + ret =iommu_preserve_locked (iommu->iommu_dev); +if (ret) { + device_ser->obj.deleted = true; +return ret; + } + + device_ser->domain_iommu_ser.domain_phys =__pa (domain->preserved_state); + device_ser->domain_iommu_ser.iommu_phys =__pa (iommu->iommu_dev->outgoing_preserved_state); + device_ser->devid =pci_dev_id (pdev); + device_ser->pci_domain =pci_domain_nr (pdev->bus); + device_ser->token = token; + + ret = iommu->iommu_dev->ops->preserve_device (dev, device_ser); +if (ret) { + device_ser->obj.deleted = true; +iommu_unpreserve_locked (iommu->iommu_dev); +return ret; + } + + dev->iommu->device_ser = device_ser; +return 0 ; +} + +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret; + +if (!dev_is_pci (dev)) +return ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->unpreserve_device || + !iommu->iommu_dev->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (WARN_ON (ret)) +return ; +drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 503
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+guard (mutex)(&flb_obj->lock); + device_ser =dev_iommu_preserved_state (dev); +if (WARN_ON (!device_ser)) +return ; + + iommu->iommu_dev->ops->unpreserve_device (dev, device_ser); + dev->iommu->device_ser =NULL ; + +iommu_unpreserve_locked (iommu->iommu_dev); +}I'm wondering ig we should guard these APIs against accidental or potential abuse by other in-kernel drivers. Since the Live Update Orchestrator (LUO) is architecturally designed around user-space driven sequences (IOCTLs, specific mutex ordering, etc.), for now.
Since the header-file is also under
include/linux, we should avoid any possibility where we end up having drivers depending on these API. We could add a check based on dma owner:(quoted diff) +5
-0+ /* Only devices explicitly claimed by a user-space driver + * (VFIO/IOMMUFD) are eligible for Live Update preservation. + */ + if (!iommu_dma_owner_claimed(dev)) + return -EPERM;Quoted 2 lines
This should ensure we aren't creating 'zombie' preserved states for devices not managed by IOMMUFD/VFIO.
Agreed. I will update this.
Quoted 174 lines
Quoted 174 lines
include/linux/iommu-lu.h +119
-0@@ -8,9 +8,128 @@#ifndef _LINUX_IOMMU_LU_H#define _LINUX_IOMMU_LU_H +#include <linux/device.h> +#include <linux/iommu.h> #include <linux/liveupdate.h> #include <linux/kho/abi/iommu.h> +typedef int (*iommu_preserved_device_iter_fn )(struct device_ser *ser, +void *arg); +#ifdef CONFIG_IOMMU_LIVEUPDATE +static inline void *dev_iommu_preserved_state (struct device *dev) +{ +struct device_ser *ser; + +if (!dev->iommu) +return NULL ; + + ser = dev->iommu->device_ser; +if (ser && !ser->obj.incoming) +return ser; + +return NULL ; +} + +static inline void *dev_iommu_restored_state (struct device *dev) +{ +struct device_ser *ser; + +if (!dev->iommu) +return NULL ; + + ser = dev->iommu->device_ser; +if (ser && ser->obj.incoming) +return ser; + +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->obj.incoming) +return ser; + +return NULL ; +} + +static inline int dev_iommu_restore_did (struct device *dev,struct iommu_domain *domain) +{ +struct device_ser *ser =dev_iommu_restored_state (dev); + +if (ser &&iommu_domain_restored_state (domain)) +return ser->domain_iommu_ser.did; + +return -1 ; +} + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg); +struct device_ser *iommu_get_device_preserved_data (struct device *dev); +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type); +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser); +void iommu_domain_unpreserve (struct iommu_domain *domain); +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token); +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev); +#else +static inline void *dev_iommu_preserved_state (struct device *dev) +{ +return NULL ; +} + +static inline void *dev_iommu_restored_state (struct device *dev) +{ +return NULL ; +} + +static inline int dev_iommu_restore_did (struct device *dev,struct iommu_domain *domain) +{ +return -1 ; +} + +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +return NULL ; +} + +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) +{ +return -EOPNOTSUPP ; +} + +static inline struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +return NULL ; +} + +static inline struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +return NULL ; +} + +static inline int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_domain_unpreserve (struct iommu_domain *domain) +{ +} + +static inline int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +} +#endif +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler);int iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler);include/linux/iommu.h +10
-0@@ -14,6 +14,8 @@#include <linux/err.h> #include <linux/of.h> #include <linux/iova_bitmap.h> +#include <linux/atomic.h> +#include <linux/kho/abi/iommu.h> #include <uapi/linux/iommufd.h> #define IOMMU_READ (1 << 0)@@ -248,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)@@ -647,6 +653,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. @@ -703,6 +713,11 @@ struct iommu_ops {struct iommu_domain *parent_domain, const struct iommu_user_data *user_data);Quoted 5 lines
include/linux/iommu.h struct iommu_ops { · line 717
Quoted 2 lines
include/linux/iommu.h +2
-0+int (*preserve_device)(struct device *dev,struct device_ser *device_ser); +void (*unpreserve_device)(struct device *dev,struct device_ser *device_ser);Nit: Let's move the
_deviceops under the comment:/* Per device IOMMU features */I will move these.
Quoted 6 lines
include/linux/iommu.h struct iommu_ops { · line 720
Quoted 4 lines
include/linux/iommu.h +3
-0+int (*preserve)(struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void (*unpreserve)(struct iommu_device *iommu,struct iommu_ser *iommu_ser); +I'm wondering if there's any benefit to add these ops under a #ifdef ?
These should be NULL when liveupdate is disabled, so #ifdef should not be needed. Not sure how much space we save if don't define these. I will move these in #ifdef anyway.
Quoted 61 lines
Quoted 47 lines
include/linux/iommu.h +14
-0const struct iommu_domain_ops *default_domain_ops;struct module *owner;struct iommu_domain *identity_domain;@@ -749,6 +764,11 @@ struct iommu_ops {* specific mechanisms. * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*) * @free: Release the domain after use. +* @preserve: Preserve the iommu domain for liveupdate. +* Returns 0 on success, a negative errno on failure. +* @unpreserve: Unpreserve the iommu domain that was preserved earlier. +* @restore: Restore the iommu domain after liveupdate. +* Returns 0 on success, a negative errno on failure. */ struct iommu_domain_ops { int (*attach_dev)(struct iommu_domain *domain, struct device *dev,@@ -779,6 +799,9 @@ struct iommu_domain_ops { unsigned long quirks);void (*free)(struct iommu_domain *domain); +int (*preserve)(struct iommu_domain *domain,struct iommu_domain_ser *ser); +void (*unpreserve)(struct iommu_domain *domain,struct iommu_domain_ser *ser); +int (*restore)(struct iommu_domain *domain,struct iommu_domain_ser *ser); };/** @@ -790,6 +813,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;@@ -799,6 +824,10 @@ struct iommu_device {struct iommu_group *singleton_group;u32 max_pasids;bool ready; + +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct iommu_ser *outgoing_preserved_state; +#endif };/** include/linux/iommu.h struct dev_iommu { · line 890
Quoted 9 lines
include/linux/iommu.h +3
-0@@ -853,6 +882,9 @@ struct dev_iommu { u32 pci_32bit_workaround:1; u32 require_direct:1; u32 shadow_on_flush:1; +#ifdef CONFIG_IOMMU_LIVEUPDATE +struct device_ser *device_ser; +#endif }; int iommu_device_register(struct iommu_device *iommu,Thanks,
Praan[1] https://elixir.bootlin.com/linux/v7.0-rc3/source/kernel/liveupdate/kexec_handover.c#L1182
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
Quoted 163 lines · Samiullah Khawaja
Quoted 153 lines · Pranjal Shrivastava
Quoted 152 lines · Samiullah Khawaja
Quoted 151 lines · Pranjal Shrivastava
Quoted 150 lines · Samiullah Khawaja
Add IOMMU domain ops that can be implemented by the IOMMU drivers if they support IOMMU domain preservation across liveupdate. The new IOMMU domain preserve, unpreserve and restore APIs call these ops to perform respective live update operations.
Similarly add IOMMU ops to preserve/unpreserve a device. These 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. The device can only be preserved if the attached iommu domain is preserved and the associated iommu supports preservation.
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.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/iommu.c | 3 + drivers/iommu/liveupdate.c | 326 +++++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 119 ++++++++++++++ include/linux/iommu.h | 32 ++++ 4 files changed, 480 insertions(+)drivers/iommu/iommu.c +3
-0@@ -389,6 +389,9 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)mutex_init (¶m->lock); dev->iommu = param; +#ifdef CONFIG_IOMMU_LIVEUPDATE + dev->iommu->device_ser =NULL ; +#endif return param; }drivers/iommu/liveupdate.c +96
-0@@ -11,6 +11,7 @@#include <linux/liveupdate.h> #include <linux/iommu-lu.h> #include <linux/iommu.h> +#include <linux/pci.h> #include <linux/errno.h> static void iommu_liveupdate_restore_objs (u64 next)@@ -175,3 +176,328 @@ int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)return liveupdate_unregister_flb (handler, &iommu_flb); }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return -ENOENT ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + + ret =fn (&devices->devices[idx], arg); +if (ret) +return ret; + } + +return 0 ; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device); + +static inline bool device_ser_match (struct device_ser *match, +struct pci_dev *pdev) +{ +return match->devid ==pci_dev_id (pdev) && match->pci_domain ==pci_domain_nr (pdev->bus); +} + +struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + +if (!dev_is_pci (dev)) +return NULL ; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + +if (device_ser_match (&devices->devices[idx],to_pci_dev (dev))) { + devices->devices[idx].obj.incoming = true; +return &devices->devices[idx]; + } + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_device_preserved_data); + +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +struct iommu_lu_flb_obj *obj; +struct iommus_ser *iommus; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + iommus =__va (obj->ser->iommus_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_iommus; ++i, ++idx) { +if (idx >=MAX_IOMMU_SERS ) { + iommus =__va (iommus->objs.next_objs); + idx =0 ; + } + +if (iommus->iommus[idx].obj.deleted) +continue ; + +if (iommus->iommus[idx].token == token && + iommus->iommus[idx].type == type) +return &iommus->iommus[idx]; + } +Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 277
Quoted 4 lines
drivers/iommu/liveupdate.c +4
-0+return NULL ; +} +EXPORT_SYMBOL (iommu_get_preserved_data); +Also, I don't see these helpers being used anywhere in this series yet? Should we add them with the phase-2 series? When we actually "Restore" the domain?
Quoted 8 lines
Phase 1 does restore the iommu and domains during boot. But the iommufd/hwpt state is not restored or re-associated with the restored domains.
These helpers are used to fetch the preserved state during boot.
Thanks,
Sami
I see these are used in PATCH 8, should we introduce these helpers in PATCH 8 where we actually use them? As we introduce iommu_restore_domain in PATCH 8?
Thanks,
Praan
Quoted 601 lines
Quoted 601 lines
Quoted 601 lines
Quoted 5 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 278
Quoted 2 lines
drivers/iommu/liveupdate.c +1
-0+static int reserve_obj_ser (struct iommu_objs_ser **objs_ptr,u64 max_objs)Isn't this more of an "insert" / "populate" /
write_ser_entry? We can rename it to something likeiommu_ser_push_entry/iommu_ser_write_entryThis is reserving an object in the objects array. The object is filled once reservation is done. Maybe I can call it
alloc_obj_seroralloc_entry_ser.Quoted 10 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 280
Quoted 3 lines
drivers/iommu/liveupdate.c +2
-0+{ +struct iommu_objs_ser *next_objs, *objs = *objs_ptr;Not loving these names :(
TBH, the
reserve_obj_serfunction isn't too readable, esp. with all the variable names, here and in the lu header.. I've had to go back n forth to the first patch. For example, herenext_objscan benext_objs_page& objs can becurr_page. (PTAL at my reply on PATCH 01 about renaming).Basically with current naming there are "serialization objects" in "serializatoin object arrays". The object is a "base" type with "inherited" types for iommu, domain etc. I will add explanation of all this in the ABI header.
Maybe we can name it to:
struct
iommu_obj_ser;
structiommu_obj_array_ser; or structiommu_obj_ser_array;Then for reserve/alloc, we use names like "
next_obj_array" and "curr_obj_array"?Quoted 16 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 292
Quoted 13 lines
drivers/iommu/liveupdate.c +12
-0+int idx; + +if (objs->nr_objs == max_objs) { + next_objs =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (next_objs)) +return PTR_ERR (next_objs); + + objs->next_objs =virt_to_phys (next_objs); + objs = next_objs; + *objs_ptr = objs; + objs->nr_objs =0 ; + objs->next_objs =0 ;This seems redundant, no need to zero these out,
kho_alloc_preservepasses__GFP_ZEROtofolio_alloc[1], which should z-alloc the pages.Agreed. Will remove this.
Quoted 33 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 297
Quoted 6 lines
drivers/iommu/liveupdate.c +5
-0+ } + + idx = objs->nr_objs++; +return idx; +}Just to give a mental model to fellow reviewers, here's how this is laid out:
----------------------------------------------------------------------
[ PAGE START ] |
----------------------------------------------------------------------| iommu_objs_ser (The Page Header) | | - next_objs: 0x0000 (End of the page-chain) | | - nr_objs: 2 | ---------------------------------------------------------------------- | ITEM 0: iommu_domain_ser | | [ iommu_obj_ser (The entry header) ] | | - idx: 0 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ITEM 1: iommu_domain_ser | | [ iommu_obj_ser (The Price Tag) ] | | - idx: 1 | | - ref_count: 1 | | - deleted: 0 | | [ Domain Data ] | ---------------------------------------------------------------------- | ... (Empty space for more domains) ... | | |Quoted 3 lines
----------------------------------------------------------------------
[ PAGE END ] |
----------------------------------------------------------------------+1
Will add a table in the header as you suggested.
Quoted 25 lines
Quoted 12 lines
drivers/iommu/liveupdate.c +11
-0+ +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int idx, ret; + +if (!domain->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj);drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 318
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommu_domains, +MAX_IOMMU_DOMAIN_SERS ); +if (idx <0 ) +return idx; + + domain_ser = &flb_obj->iommu_domains->iommu_domains[idx];This is slightly less-readable as well, I understand we're trying to:
Quoted 2 lines
iommu_domains_ser-> iommu_domain_ser[idx] but the same name (iommu_domains) makes it difficult to read.. we should rename this as:Agreed.
I will update it to,
&flb_obj->curr_iommu_domains->domain_array[idx]
Quoted 6 lines
&flb_obj->iommu_domains_page->domain_entries[idx] or something for better readability..
Also, let's add a comment explaining that
reserve_obj_seractually advances theflb_objptr when necessary..Agreed.
Quoted 6 lines
IIUC, we start with PAGE 0 initially, store it's phys in the
iommu_flb_preserveop (theiommu_ser_physet al) & then we go on alloc-ing more pages and keep storing the "current" active page with the liveupdate core. Now when we jump into the new kernel, we read theser_physand then follow the page chain, right?Yes, we do an initial allocation of arrays for each object type and then later allocate more as needed. The
flb_objholds the address of currently active array.I will add this explanation in the header.
Quoted 202 lines
Quoted 176 lines
drivers/iommu/liveupdate.c +175
-0+ idx = flb_obj->ser->nr_domains++; + domain_ser->obj.idx = idx; + domain_ser->obj.ref_count =1 ; + + ret = domain->ops->preserve (domain, domain_ser); +if (ret) { + domain_ser->obj.deleted = true; +return ret; + } + + domain->preserved_state = domain_ser; + *ser = domain_ser; +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_domain_preserve); + +void iommu_domain_unpreserve (struct iommu_domain *domain) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int ret; + +if (!domain->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ; + +guard (mutex)(&flb_obj->lock); + +/* +* 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; + domain->ops->unpreserve (domain, domain_ser); + domain_ser->obj.deleted = true; + domain->preserved_state =NULL ; +} +EXPORT_SYMBOL_GPL (iommu_domain_unpreserve); + +static int iommu_preserve_locked (struct iommu_device *iommu) +{ +struct iommu_lu_flb_obj *flb_obj; +struct iommu_ser *iommu_ser; +int idx, ret; + +if (!iommu->ops->preserve) +return -EOPNOTSUPP ; + +if (iommu->outgoing_preserved_state) { + iommu->outgoing_preserved_state->obj.ref_count++; +return 0 ; + } + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommus, +MAX_IOMMU_SERS ); +if (idx <0 ) +return idx; + + iommu_ser = &flb_obj->iommus->iommus[idx]; + idx = flb_obj->ser->nr_iommus++; + iommu_ser->obj.idx = idx; + iommu_ser->obj.ref_count =1 ; + + ret = iommu->ops->preserve (iommu, iommu_ser); +if (ret) + iommu_ser->obj.deleted = true; + + iommu->outgoing_preserved_state = iommu_ser; +return ret; +} + +static void iommu_unpreserve_locked (struct iommu_device *iommu) +{ +struct iommu_ser *iommu_ser = iommu->outgoing_preserved_state; + + iommu_ser->obj.ref_count--; +if (iommu_ser->obj.ref_count) +return ; + + iommu->outgoing_preserved_state =NULL ; + iommu->ops->unpreserve (iommu, iommu_ser); + iommu_ser->obj.deleted = true; +} + +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret, idx; + +if (!dev_is_pci (dev)) +return -EOPNOTSUPP ; + +if (!domain->preserved_state) +return -EINVAL ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->preserve_device || + !iommu->iommu_dev->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->devices, +MAX_DEVICE_SERS ); +if (idx <0 ) +return idx; + + device_ser = &flb_obj->devices->devices[idx]; + idx = flb_obj->ser->nr_devices++; + device_ser->obj.idx = idx; + device_ser->obj.ref_count =1 ; + + ret =iommu_preserve_locked (iommu->iommu_dev); +if (ret) { + device_ser->obj.deleted = true; +return ret; + } + + device_ser->domain_iommu_ser.domain_phys =__pa (domain->preserved_state); + device_ser->domain_iommu_ser.iommu_phys =__pa (iommu->iommu_dev->outgoing_preserved_state); + device_ser->devid =pci_dev_id (pdev); + device_ser->pci_domain =pci_domain_nr (pdev->bus); + device_ser->token = token; + + ret = iommu->iommu_dev->ops->preserve_device (dev, device_ser); +if (ret) { + device_ser->obj.deleted = true; +iommu_unpreserve_locked (iommu->iommu_dev); +return ret; + } + + dev->iommu->device_ser = device_ser; +return 0 ; +} + +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret; + +if (!dev_is_pci (dev)) +return ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->unpreserve_device || + !iommu->iommu_dev->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (WARN_ON (ret)) +return ; +drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 503
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+guard (mutex)(&flb_obj->lock); + device_ser =dev_iommu_preserved_state (dev); +if (WARN_ON (!device_ser)) +return ; + + iommu->iommu_dev->ops->unpreserve_device (dev, device_ser); + dev->iommu->device_ser =NULL ; + +iommu_unpreserve_locked (iommu->iommu_dev); +}I'm wondering ig we should guard these APIs against accidental or potential abuse by other in-kernel drivers. Since the Live Update Orchestrator (LUO) is architecturally designed around user-space driven sequences (IOCTLs, specific mutex ordering, etc.), for now.
Since the header-file is also under
include/linux, we should avoid any possibility where we end up having drivers depending on these API. We could add a check based on dma owner:(quoted diff) +5
-0+ /* Only devices explicitly claimed by a user-space driver + * (VFIO/IOMMUFD) are eligible for Live Update preservation. + */ + if (!iommu_dma_owner_claimed(dev)) + return -EPERM;Quoted 2 lines
This should ensure we aren't creating 'zombie' preserved states for devices not managed by IOMMUFD/VFIO.
Agreed. I will update this.
Quoted 174 lines
Quoted 174 lines
include/linux/iommu-lu.h +119
-0@@ -8,9 +8,128 @@#ifndef _LINUX_IOMMU_LU_H#define _LINUX_IOMMU_LU_H +#include <linux/device.h> +#include <linux/iommu.h> #include <linux/liveupdate.h> #include <linux/kho/abi/iommu.h> +typedef int (*iommu_preserved_device_iter_fn )(struct device_ser *ser, +void *arg); +#ifdef CONFIG_IOMMU_LIVEUPDATE +static inline void *dev_iommu_preserved_state (struct device *dev) +{ +struct device_ser *ser; + +if (!dev->iommu) +return NULL ; + + ser = dev->iommu->device_ser; +if (ser && !ser->obj.incoming) +return ser; + +return NULL ; +} + +static inline void *dev_iommu_restored_state (struct device *dev) +{ +struct device_ser *ser; + +if (!dev->iommu) +return NULL ; + + ser = dev->iommu->device_ser; +if (ser && ser->obj.incoming) +return ser; + +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->obj.incoming) +return ser; + +return NULL ; +} + +static inline int dev_iommu_restore_did (struct device *dev,struct iommu_domain *domain) +{ +struct device_ser *ser =dev_iommu_restored_state (dev); + +if (ser &&iommu_domain_restored_state (domain)) +return ser->domain_iommu_ser.did; + +return -1 ; +} + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg); +struct device_ser *iommu_get_device_preserved_data (struct device *dev); +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type); +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser); +void iommu_domain_unpreserve (struct iommu_domain *domain); +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token); +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev); +#else +static inline void *dev_iommu_preserved_state (struct device *dev) +{ +return NULL ; +} + +static inline void *dev_iommu_restored_state (struct device *dev) +{ +return NULL ; +} + +static inline int dev_iommu_restore_did (struct device *dev,struct iommu_domain *domain) +{ +return -1 ; +} + +static inline void *iommu_domain_restored_state (struct iommu_domain *domain) +{ +return NULL ; +} + +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) +{ +return -EOPNOTSUPP ; +} + +static inline struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +return NULL ; +} + +static inline struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +return NULL ; +} + +static inline int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_domain_unpreserve (struct iommu_domain *domain) +{ +} + +static inline int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +} +#endif +int iommu_liveupdate_register_flb (struct liveupdate_file_handler *handler);int iommu_liveupdate_unregister_flb (struct liveupdate_file_handler *handler);include/linux/iommu.h +10
-0@@ -14,6 +14,8 @@#include <linux/err.h> #include <linux/of.h> #include <linux/iova_bitmap.h> +#include <linux/atomic.h> +#include <linux/kho/abi/iommu.h> #include <uapi/linux/iommufd.h> #define IOMMU_READ (1 << 0)@@ -248,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)@@ -647,6 +653,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. @@ -703,6 +713,11 @@ struct iommu_ops {struct iommu_domain *parent_domain, const struct iommu_user_data *user_data);Quoted 5 lines
include/linux/iommu.h struct iommu_ops { · line 717
Quoted 2 lines
include/linux/iommu.h +2
-0+int (*preserve_device)(struct device *dev,struct device_ser *device_ser); +void (*unpreserve_device)(struct device *dev,struct device_ser *device_ser);Nit: Let's move the
_deviceops under the comment:/* Per device IOMMU features */71 lines not shown · raw message
Part of this message is not shown here. View the raw message.
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
Quoted 21 lines · Pranjal Shrivastava
Quoted 11 lines · Samiullah Khawaja
Quoted 9 lines · Pranjal Shrivastava
Quoted 4 lines · Samiullah Khawaja
Quoted 3 lines · Pranjal Shrivastava
Review
Quoted 2 lines · Samiullah Khawaja
(quoted diff) +2
-0+EXPORT_SYMBOL(iommu_get_preserved_data); +Also, I don't see these helpers being used anywhere in this series yet? Should we add them with the phase-2 series? When we actually "Restore" the domain?
Quoted 8 lines
Phase 1 does restore the iommu and domains during boot. But the iommufd/hwpt state is not restored or re-associated with the restored domains.
These helpers are used to fetch the preserved state during boot.
Thanks,
Sami
Quoted 7 lines
I see these are used in PATCH 8, should we introduce these helpers in PATCH 8 where we actually use them? As we introduce
iommu_restore_domainin PATCH 8?Thanks,
Praan
Please trim your responses.
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
Quoted 26 lines · Vipin Sharma
Quoted 25 lines · Pranjal Shrivastava
Quoted 11 lines · Samiullah Khawaja
Quoted 9 lines · Pranjal Shrivastava
Quoted 4 lines · Samiullah Khawaja
Quoted 3 lines · Pranjal Shrivastava
Review
Quoted 2 lines · Samiullah Khawaja
(quoted diff) +2
-0+EXPORT_SYMBOL(iommu_get_preserved_data); +Also, I don't see these helpers being used anywhere in this series yet? Should we add them with the phase-2 series? When we actually "Restore" the domain?
Quoted 8 lines
Phase 1 does restore the iommu and domains during boot. But the iommufd/hwpt state is not restored or re-associated with the restored domains.
These helpers are used to fetch the preserved state during boot.
Thanks,
SamiI see these are used in PATCH 8, should we introduce these helpers in PATCH 8 where we actually use them? As we introduce
iommu_restore_domainin PATCH 8?
Quoted 5 lines
Quoted 3 lines
Thanks,
PraanPlease trim your responses.
Ack, sorry for missing that in this thread.
Thanks,
Praan
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
In reply to Pranjal ShrivastavaQuoted 16 lines · Pranjal Shrivastava
Quoted 15 lines · Samiullah Khawaja
Quoted 9 lines · Pranjal Shrivastava
Quoted 4 lines · Samiullah Khawaja
Quoted 3 lines · Pranjal Shrivastava
Review
Quoted 2 lines · Samiullah Khawaja
(quoted diff) +2
-0+EXPORT_SYMBOL(iommu_get_preserved_data); +Also, I don't see these helpers being used anywhere in this series yet? Should we add them with the phase-2 series? When we actually "Restore" the domain?
Phase 1 does restore the iommu and domains during boot. But the iommufd/hwpt state is not restored or re-associated with the restored domains.
Quoted 8 lines
Quoted 4 lines
These helpers are used to fetch the preserved state during boot.
Thanks,
SamiI see these are used in PATCH 8, should we introduce these helpers in PATCH 8 where we actually use them? As we introduce
iommu_restore_domainin PATCH 8?
Agreed. I will move the helper for getting the device state into PATCH
8. But the one that is used to fetch the iommu state is used by the vt-d driver in the PATCH 7. I will add a new patch before PATCH 7 to add that helper.
Thanks,
Sami
Quoted 4 lines
Thanks,
Praan
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
In reply to Samiullah Khawajadrivers/iommu/iommu.c static struct dev_iommu *dev_iommu_get(struct device *dev) · line 394
Quoted 11 lines · Samiullah Khawaja
drivers/iommu/iommu.c +3
-0@@ -389,6 +389,9 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)mutex_init (¶m->lock); dev->iommu = param; +#ifdef CONFIG_IOMMU_LIVEUPDATE + dev->iommu->device_ser =NULL ; +#endif
This should already be NULL due to kzalloc() above.
Quoted 39 lines
drivers/iommu/iommu.c +0
-0return param; }drivers/iommu/liveupdate.c +21
-0@@ -11,6 +11,7 @@#include <linux/liveupdate.h> #include <linux/iommu-lu.h> #include <linux/iommu.h> +#include <linux/pci.h> #include <linux/errno.h> static void iommu_liveupdate_restore_objs (u64 next)@@ -175,3 +176,328 @@ int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)return liveupdate_unregister_flb (handler, &iommu_flb); }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return -ENOENT ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted)
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 208
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+continue ; + + ret =fn (&devices->devices[idx], arg); +if (ret) +return ret; + } + +return 0 ; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device);
IMHO, with all the obj, ser, devices->devices[..], it is little harder to follow the code. I will recommend to reconsider naming.
Also, should this function be introduced in the patch where it is getting used? Other changes in this patch are already big and complex. Same for iommu_get_device_preserved_data() and iommu_get_preserved_data().
I think this patch can be split in three.
Patch 1: Preserve iommu_domain
Patch 2: Preserve pci device and iommu device
Patch 3: The helper functions I mentioned above.
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 213
Quoted 5 lines
drivers/iommu/liveupdate.c +5
-0+ +static inline bool device_ser_match (struct device_ser *match, +struct pci_dev *pdev) +{ +return match->devid ==pci_dev_id (pdev) && match->pci_domain ==pci_domain_nr (pdev->bus);
Nit: s/match/device_ser for readability or something similar.
Quoted 73 lines
drivers/iommu/liveupdate.c +73
-0+} + +struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + +if (!dev_is_pci (dev)) +return NULL ; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + +if (device_ser_match (&devices->devices[idx],to_pci_dev (dev))) { + devices->devices[idx].obj.incoming = true; +return &devices->devices[idx]; + } + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_device_preserved_data); + +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +struct iommu_lu_flb_obj *obj; +struct iommus_ser *iommus; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + iommus =__va (obj->ser->iommus_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_iommus; ++i, ++idx) { +if (idx >=MAX_IOMMU_SERS ) { + iommus =__va (iommus->objs.next_objs); + idx =0 ; + } + +if (iommus->iommus[idx].obj.deleted) +continue ; + +if (iommus->iommus[idx].token == token && + iommus->iommus[idx].type == type) +return &iommus->iommus[idx]; + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_preserved_data); + +static int reserve_obj_ser (struct iommu_objs_ser **objs_ptr,u64 max_objs) +{ +struct iommu_objs_ser *next_objs, *objs = *objs_ptr; +int idx; + +if (objs->nr_objs == max_objs) { + next_objs =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (next_objs)) +return PTR_ERR (next_objs);
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 296
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+ + objs->next_objs =virt_to_phys (next_objs); + objs = next_objs; + *objs_ptr = objs; + objs->nr_objs =0 ; + objs->next_objs =0 ; + } + + idx = objs->nr_objs++; +return idx;
I think we should rename these variables, it is difficult to comprehend the code.
Quoted 13 lines
drivers/iommu/liveupdate.c +13
-0+} + +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int idx, ret; + +if (!domain->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret)
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 319
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommu_domains, +MAX_IOMMU_DOMAIN_SERS ); +if (idx <0 ) +return idx; + + domain_ser = &flb_obj->iommu_domains->iommu_domains[idx]; + idx = flb_obj->ser->nr_domains++;
In for loops above, nr_domains are compared with 'i', and idx is for index inside a page. Lets not reuse idx here for nr_domains, keep it consistent for easier understanding. May be use something more descriptive than 'i' like global_idx and local_idx?
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+ domain_ser->obj.idx = idx; + domain_ser->obj.ref_count =1 ; + + ret = domain->ops->preserve (domain, domain_ser); +if (ret) { + domain_ser->obj.deleted = true; +return ret; + } + + domain->preserved_state = domain_ser;
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 369
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+ *ser = domain_ser; +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_domain_preserve); + +static int iommu_preserve_locked (struct iommu_device *iommu) +{ +struct iommu_lu_flb_obj *flb_obj; +struct iommu_ser *iommu_ser; +int idx, ret;
Should there be lockdep asserts in these locked version APIs?
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 405
Quoted 8 lines
drivers/iommu/liveupdate.c +8
-0+ +} + +static void iommu_unpreserve_locked (struct iommu_device *iommu) +{ +struct iommu_ser *iommu_ser = iommu->outgoing_preserved_state; + + iommu_ser->obj.ref_count--;
Should there be a null check?
Quoted 17 lines
drivers/iommu/liveupdate.c +17
-0+if (iommu_ser->obj.ref_count) +return ; + + iommu->outgoing_preserved_state =NULL ; + iommu->ops->unpreserve (iommu, iommu_ser); + iommu_ser->obj.deleted = true; +} + +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret, idx; +
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 432
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+if (!dev_is_pci (dev)) +return -EOPNOTSUPP ; + +if (!domain->preserved_state) +return -EINVAL ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->preserve_device || + !iommu->iommu_dev->ops->preserve)
iommu_preserve_locked() is already checking for preserve(), we can just check preserve_device here.
Quoted 49 lines
drivers/iommu/liveupdate.c +49
-0+return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->devices, +MAX_DEVICE_SERS ); +if (idx <0 ) +return idx; + + device_ser = &flb_obj->devices->devices[idx]; + idx = flb_obj->ser->nr_devices++; + device_ser->obj.idx = idx; + device_ser->obj.ref_count =1 ; + + ret =iommu_preserve_locked (iommu->iommu_dev); +if (ret) { + device_ser->obj.deleted = true; +return ret; + } + + device_ser->domain_iommu_ser.domain_phys =__pa (domain->preserved_state); + device_ser->domain_iommu_ser.iommu_phys =__pa (iommu->iommu_dev->outgoing_preserved_state); + device_ser->devid =pci_dev_id (pdev); + device_ser->pci_domain =pci_domain_nr (pdev->bus); + device_ser->token = token; + + ret = iommu->iommu_dev->ops->preserve_device (dev, device_ser); +if (ret) { + device_ser->obj.deleted = true; +iommu_unpreserve_locked (iommu->iommu_dev); +return ret; + } + + dev->iommu->device_ser = device_ser; +return 0 ; +} + +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret; + +if (!dev_is_pci (dev))
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 491
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+return ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->unpreserve_device || + !iommu->iommu_dev->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (WARN_ON (ret))
Why WARN_ON here and not other places? Do we need it?
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 496
Quoted 5 lines
drivers/iommu/liveupdate.c +5
-0+return ; + +guard (mutex)(&flb_obj->lock); + device_ser =dev_iommu_preserved_state (dev); +if (WARN_ON (!device_ser))
Can't we just silently ignore this?
Quoted 7 lines
drivers/iommu/liveupdate.c +7
-0+return ; + + iommu->iommu_dev->ops->unpreserve_device (dev, device_ser); + dev->iommu->device_ser =NULL ; + +iommu_unpreserve_locked (iommu->iommu_dev); +}
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
Quoted 7 lines · Vipin Sharma
Quoted 6 lines · Samiullah Khawaja
drivers/iommu/iommu.c +0
-0@@ -389,6 +389,9 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)
Quoted 7 lines
drivers/iommu/iommu.c static struct dev_iommu *dev_iommu_get(struct device *dev) · line 394
Quoted 5 lines
drivers/iommu/iommu.c +3
-0mutex_init (¶m->lock); dev->iommu = param; +#ifdef CONFIG_IOMMU_LIVEUPDATE + dev->iommu->device_ser =NULL ; +#endif This should already be NULL due to
kzalloc()above.
Will remove.
Quoted 45 lines
Quoted 45 lines
drivers/iommu/iommu.c +0
-0return param; }drivers/iommu/liveupdate.c +26
-0@@ -11,6 +11,7 @@#include <linux/liveupdate.h> #include <linux/iommu-lu.h> #include <linux/iommu.h> +#include <linux/pci.h> #include <linux/errno.h> static void iommu_liveupdate_restore_objs (u64 next)@@ -175,3 +176,328 @@ int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler)return liveupdate_unregister_flb (handler, &iommu_flb); }EXPORT_SYMBOL (iommu_liveupdate_unregister_flb); + +int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return -ENOENT ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + + ret =fn (&devices->devices[idx], arg); +if (ret) +return ret;
Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 208
Quoted 5 lines
drivers/iommu/liveupdate.c +5
-0+ } + +return 0 ; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device);IMHO, with all the obj, ser, devices->devices[..], it is little harder to follow the code. I will recommend to reconsider naming.
Agreed. I will update this based on the feedback on the previous patch.
Quoted 5 lines
Also, should this function be introduced in the patch where it is getting used? Other changes in this patch are already big and complex. Same for
iommu_get_device_preserved_data()andiommu_get_preserved_data().
These are used by the drivers, but part of core. So need to be in this patch :(.
Note that this patch is adding core skeleton only, focusing on helpers for the serialized state. This patch is not preserving any real state of iommu, domain or devices. For example, the domains are saved through generic page table in a separate patch, and the drivers preserve the state of devices and associated iommu in separate patches.
I will add this text in the commit message to clarify the purpose of this patch.
Quoted 7 lines
I think this patch can be split in three.
Patch 1: Preserveiommu_domain
Patch 2: Preserve pci device and iommu device
Patch 3: The helper functions I mentioned above.
Quoted 7 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 213
Quoted 5 lines
drivers/iommu/liveupdate.c +5
-0+ +static inline bool device_ser_match (struct device_ser *match, +struct pci_dev *pdev) +{ +return match->devid ==pci_dev_id (pdev) && match->pci_domain ==pci_domain_nr (pdev->bus);Nit: s/match/device_ser for readability or something similar.
Agreed. Will do.
Quoted 79 lines
Quoted 79 lines
drivers/iommu/liveupdate.c +78
-0+} + +struct device_ser *iommu_get_device_preserved_data (struct device *dev) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + +if (!dev_is_pci (dev)) +return NULL ; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + +if (device_ser_match (&devices->devices[idx],to_pci_dev (dev))) { + devices->devices[idx].obj.incoming = true; +return &devices->devices[idx]; + } + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_device_preserved_data); + +struct iommu_ser *iommu_get_preserved_data (u64 token,enum iommu_lu_type type) +{ +struct iommu_lu_flb_obj *obj; +struct iommus_ser *iommus; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return NULL ; + + iommus =__va (obj->ser->iommus_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_iommus; ++i, ++idx) { +if (idx >=MAX_IOMMU_SERS ) { + iommus =__va (iommus->objs.next_objs); + idx =0 ; + } + +if (iommus->iommus[idx].obj.deleted) +continue ; + +if (iommus->iommus[idx].token == token && + iommus->iommus[idx].type == type) +return &iommus->iommus[idx]; + } + +return NULL ; +} +EXPORT_SYMBOL (iommu_get_preserved_data); + +static int reserve_obj_ser (struct iommu_objs_ser **objs_ptr,u64 max_objs) +{ +struct iommu_objs_ser *next_objs, *objs = *objs_ptr; +int idx; + +if (objs->nr_objs == max_objs) { + next_objs =kho_alloc_preserve (PAGE_SIZE ); +if (IS_ERR (next_objs)) +return PTR_ERR (next_objs); + + objs->next_objs =virt_to_phys (next_objs); + objs = next_objs; + *objs_ptr = objs; + objs->nr_objs =0 ;
Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 296
Quoted 5 lines
drivers/iommu/liveupdate.c +5
-0+ objs->next_objs =0 ; + } + + idx = objs->nr_objs++; +return idx;I think we should rename these variables, it is difficult to comprehend the code.
Will do.
Quoted 21 lines
Quoted 21 lines
drivers/iommu/liveupdate.c +20
-0+} + +int iommu_domain_preserve (struct iommu_domain *domain,struct iommu_domain_ser **ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +int idx, ret; + +if (!domain->ops->preserve) +return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->iommu_domains, +MAX_IOMMU_DOMAIN_SERS ); +if (idx <0 ) +return idx;
Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 319
Quoted 3 lines
drivers/iommu/liveupdate.c +3
-0+ + domain_ser = &flb_obj->iommu_domains->iommu_domains[idx]; + idx = flb_obj->ser->nr_domains++;In for loops above,
nr_domainsare compared with 'i', and idx is for index inside a page. Lets not reuse idx here fornr_domains, keep it consistent for easier understanding. May be use something more descriptive than 'i' likeglobal_idxandlocal_idx?
Agreed. Will change it.
Quoted 15 lines
Quoted 15 lines
drivers/iommu/liveupdate.c +14
-0+ domain_ser->obj.idx = idx; + domain_ser->obj.ref_count =1 ; + + ret = domain->ops->preserve (domain, domain_ser); +if (ret) { + domain_ser->obj.deleted = true; +return ret; + } + + domain->preserved_state = domain_ser; + *ser = domain_ser; +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_domain_preserve);
Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 369
Quoted 6 lines
drivers/iommu/liveupdate.c +6
-0+ +static int iommu_preserve_locked (struct iommu_device *iommu) +{ +struct iommu_lu_flb_obj *flb_obj; +struct iommu_ser *iommu_ser; +int idx, ret;Should there be lockdep asserts in these locked version APIs?
Will add it here.
Quoted 11 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 405
Quoted 9 lines
drivers/iommu/liveupdate.c +8
-0+ +} + +static void iommu_unpreserve_locked (struct iommu_device *iommu) +{ +struct iommu_ser *iommu_ser = iommu->outgoing_preserved_state; + + iommu_ser->obj.ref_count--;Should there be a null check?
Hmm.. There is a dependency of unpreservation of iommus with devices, so this should never be NULL unless used independently.
But I think I will add it here to protect against that.
Quoted 23 lines
Quoted 23 lines
drivers/iommu/liveupdate.c +22
-0+if (iommu_ser->obj.ref_count) +return ; + + iommu->outgoing_preserved_state =NULL ; + iommu->ops->unpreserve (iommu, iommu_ser); + iommu_ser->obj.deleted = true; +} + +int iommu_preserve_device (struct iommu_domain *domain, +struct device *dev,u64 token) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret, idx; + +if (!dev_is_pci (dev)) +return -EOPNOTSUPP ; + +if (!domain->preserved_state) +return -EINVAL ;
Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 432
Quoted 5 lines
drivers/iommu/liveupdate.c +5
-0+ + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->preserve_device || + !iommu->iommu_dev->ops->preserve)
iommu_preserve_locked()is already checking forpreserve(), we can just checkpreserve_devicehere.
iommu_preserve_locked() is called after reserving the serialization state in FLB and taking the FLB lock, so this is an early check that returns early.
Quoted 54 lines
Quoted 54 lines
drivers/iommu/liveupdate.c +53
-0+return -EOPNOTSUPP ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (ret) +return ret; + +guard (mutex)(&flb_obj->lock); + idx =reserve_obj_ser ((struct iommu_objs_ser **)&flb_obj->devices, +MAX_DEVICE_SERS ); +if (idx <0 ) +return idx; + + device_ser = &flb_obj->devices->devices[idx]; + idx = flb_obj->ser->nr_devices++; + device_ser->obj.idx = idx; + device_ser->obj.ref_count =1 ; + + ret =iommu_preserve_locked (iommu->iommu_dev); +if (ret) { + device_ser->obj.deleted = true; +return ret; + } + + device_ser->domain_iommu_ser.domain_phys =__pa (domain->preserved_state); + device_ser->domain_iommu_ser.iommu_phys =__pa (iommu->iommu_dev->outgoing_preserved_state); + device_ser->devid =pci_dev_id (pdev); + device_ser->pci_domain =pci_domain_nr (pdev->bus); + device_ser->token = token; + + ret = iommu->iommu_dev->ops->preserve_device (dev, device_ser); +if (ret) { + device_ser->obj.deleted = true; +iommu_unpreserve_locked (iommu->iommu_dev); +return ret; + } + + dev->iommu->device_ser = device_ser; +return 0 ; +} + +void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret; + +if (!dev_is_pci (dev)) +return ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu;
Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 491
Quoted 6 lines
drivers/iommu/liveupdate.c +6
-0+if (!iommu->iommu_dev->ops->unpreserve_device || + !iommu->iommu_dev->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (WARN_ON (ret))Why
WARN_ONhere and not other places? Do we need it?
Basically this means that the upper layer (iommufd/vfio) is asking to unpreserve a device, but there is no FLB found. This should not happen and should generate a warning.
Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 496
Quoted 6 lines
drivers/iommu/liveupdate.c +5
-0+return ; + +guard (mutex)(&flb_obj->lock); + device_ser =dev_iommu_preserved_state (dev); +if (WARN_ON (!device_ser))Can't we just silently ignore this?
See answer above for the previous WARN. Same applies here.
Quoted 8 lines
Quoted 8 lines
drivers/iommu/liveupdate.c +7
-0+return ; + + iommu->iommu_dev->ops->unpreserve_device (dev, device_ser); + dev->iommu->device_ser =NULL ; + +iommu_unpreserve_locked (iommu->iommu_dev); +}
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
Quoted 37 lines · Samiullah Khawaja
Quoted 27 lines · Vipin Sharma
Quoted 26 lines · Samiullah Khawaja
drivers/iommu/liveupdate.c +25
-0+int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return -ENOENT ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted) +continue ; + + ret =fn (&devices->devices[idx], arg); +if (ret) +return ret; + }Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 208
Quoted 4 lines
drivers/iommu/liveupdate.c +4
-0+ +return 0 ; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device);Also, should this function be introduced in the patch where it is getting used? Other changes in this patch are already big and complex. Same for
iommu_get_device_preserved_data()andiommu_get_preserved_data().
Quoted 2 lines
These are used by the drivers, but part of core. So need to be in this patch :(.
Sorry, I am not understanding why it has to be in this patch? Can it be its own patch?
Quoted 14 lines
Note that this patch is adding core skeleton only, focusing on helpers for the serialized state. This patch is not preserving any real state of iommu, domain or devices. For example, the domains are saved through generic page table in a separate patch, and the drivers preserve the state of devices and associated iommu in separate patches.
I will add this text in the commit message to clarify the purpose of this patch.
Quoted 5 lines
I think this patch can be split in three.
Patch 1: Preserveiommu_domain
Patch 2: Preserve pci device and iommu device
Patch 3: The helper functions I mentioned above.
I understand that this patch is adding some helper functions and not doing any actual preservation. I am suggesting to split this helper function patch into three for easier review based on the above suggestion. If I am not wrong this is biggest patch in series of approx 500 line changes.
Quoted 8 lines
Quoted 7 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 405
Quoted 5 lines
drivers/iommu/liveupdate.c +5
-0+static void iommu_unpreserve_locked (struct iommu_device *iommu) +{ +struct iommu_ser *iommu_ser = iommu->outgoing_preserved_state; + + iommu_ser->obj.ref_count--;Should there be a null check?
Quoted 4 lines
Hmm.. There is a dependency of unpreservation of iommus with devices, so this should never be NULL unless used independently.
But I think I will add it here to protect against that.
Okay. Since, it is a static function, I am fine either way.
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
drivers/iommu/liveupdate.c +13
-0+void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret; + +if (!dev_is_pci (dev)) +return ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu;Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 491
Quoted 6 lines
drivers/iommu/liveupdate.c +6
-0+if (!iommu->iommu_dev->ops->unpreserve_device || + !iommu->iommu_dev->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (WARN_ON (ret))Why
WARN_ONhere and not other places? Do we need it?
Quoted 3 lines
Basically this means that the upper layer (iommufd/vfio) is asking to unpreserve a device, but there is no FLB found. This should not happen and should generate a warning.
Yeah, but other places iommu_domain_[preserve|unpreserve](), iommu_presreve_locked(), and iommu_preserve_device() are also using this function. I am having a confusion on why it is important in this function and not others. Those functions are also called by upper layer.
Re: [PATCH 02/14] iommu: Implement IOMMU core liveupdate skeleton
Quoted 38 lines · Vipin Sharma
Quoted 37 lines · Samiullah Khawaja
Quoted 35 lines · Vipin Sharma
Quoted 20 lines · Samiullah Khawaja
drivers/iommu/liveupdate.c +19
-0+int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn, +void *arg) +{ +struct iommu_lu_flb_obj *obj; +struct devices_ser *devices; +int ret, i, idx; + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&obj); +if (ret) +return -ENOENT ; + + devices =__va (obj->ser->devices_phys); +for (i =0 , idx =0 ; i < obj->ser->nr_devices; ++i, ++idx) { +if (idx >=MAX_DEVICE_SERS ) { + devices =__va (devices->objs.next_objs); + idx =0 ; + } + +if (devices->devices[idx].obj.deleted)drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 208
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+continue ; + + ret =fn (&devices->devices[idx], arg); +if (ret) +return ret; + } + +return 0 ; +} +EXPORT_SYMBOL (iommu_for_each_preserved_device);Also, should this function be introduced in the patch where it is getting used? Other changes in this patch are already big and complex. Same for
iommu_get_device_preserved_data()andiommu_get_preserved_data().
Quoted 5 lines
Quoted 2 lines
These are used by the drivers, but part of core. So need to be in this patch :(.
Sorry, I am not understanding why it has to be in this patch? Can it be its own patch?
I will move it to a separate patch as per discussion in other thread.
See:
https://lore.kernel.org/all/[email protected]/
Quoted 15 lines
Quoted 14 lines
Note that this patch is adding core skeleton only, focusing on helpers for the serialized state. This patch is not preserving any real state of iommu, domain or devices. For example, the domains are saved through generic page table in a separate patch, and the drivers preserve the state of devices and associated iommu in separate patches.
I will add this text in the commit message to clarify the purpose of this patch.
Quoted 5 lines
I think this patch can be split in three.
Patch 1: Preserveiommu_domain
Patch 2: Preserve pci device and iommu device
Patch 3: The helper functions I mentioned above.
Quoted 5 lines
I understand that this patch is adding some helper functions and not doing any actual preservation. I am suggesting to split this helper function patch into three for easier review based on the above suggestion. If I am not wrong this is biggest patch in series of approx 500 line changes.
After moving the getter helpers out to the separate patch as mentioned above, the size of this patch should significantly reduce. I will split the remaining into domain and device + iommu preservation helper patches as you suggested.
Quoted 38 lines
Quoted 9 lines
Quoted 8 lines
drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 405
Quoted 6 lines
drivers/iommu/liveupdate.c +5
-0+static void iommu_unpreserve_locked (struct iommu_device *iommu) +{ +struct iommu_ser *iommu_ser = iommu->outgoing_preserved_state; + + iommu_ser->obj.ref_count--;Should there be a null check?
Quoted 4 lines
Hmm.. There is a dependency of unpreservation of iommus with devices, so this should never be NULL unless used independently.
But I think I will add it here to protect against that.
Okay. Since, it is a static function, I am fine either way.
Quoted 22 lines
Quoted 21 lines
Quoted 9 lines
drivers/iommu/liveupdate.c +9
-0+void iommu_unpreserve_device (struct iommu_domain *domain,struct device *dev) +{ +struct iommu_lu_flb_obj *flb_obj; +struct device_ser *device_ser; +struct dev_iommu *iommu; +struct pci_dev *pdev; +int ret; + +if (!dev_is_pci (dev))drivers/iommu/liveupdate.c int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) · line 491
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+return ; + + pdev =to_pci_dev (dev); + iommu = dev->iommu; +if (!iommu->iommu_dev->ops->unpreserve_device || + !iommu->iommu_dev->ops->unpreserve) +return ; + + ret =liveupdate_flb_get_outgoing (&iommu_flb, (void **)&flb_obj); +if (WARN_ON (ret))Why
WARN_ONhere and not other places? Do we need it?
Quoted 8 lines
Quoted 3 lines
Basically this means that the upper layer (iommufd/vfio) is asking to unpreserve a device, but there is no FLB found. This should not happen and should generate a warning.
Yeah, but other places iommu_domain_[preserve|unpreserve](),
iommu_presreve_locked(), andiommu_preserve_device()are also using this function. I am having a confusion on why it is important in his function and not others. Those functions are also called by upper layer.
Ah yes.. That makes sense. I will add it to the unpreserve variants of functions you listed, as during preservation the FLB obj should have been created.
Quoted 1 line
PATCH 3/14 liveupdate: luo_file: Add internal APIs for file preservation
In reply to Samiullah KhawajaFrom: Pasha Tatashin <[email protected]>
The core liveupdate mechanism allows userspace to preserve file descriptors. However, kernel subsystems often manage struct file objects directly and need to participate in the preservation process programmatically without relying solely on userspace interaction.
--- include/linux/liveupdate.h | 21 ++++++++++ kernel/liveupdate/luo_file.c | 71 ++++++++++++++++++++++++++++++++ kernel/liveupdate/luo_internal.h | 16 +++++++ 3 files changed, 108 insertions(+)
include/linux/liveupdate.h +21 -0
@@ -23,6 +23,7 @@ struct file;/** * struct liveupdate_file_op_args - Arguments for file operation callbacks. * @handler: The file handler being called. +* @session: The session this file belongs to. * @retrieved: The retrieve status for the 'can_finish / finish' * operation. * @file: The file object. For retrieve: [OUT] The callback sets
@@ -40,6 +41,7 @@ struct file;*/ struct liveupdate_file_op_args {struct liveupdate_file_handler *handler; +struct liveupdate_session *session;bool retrieved;struct file *file;u64 serialized_data;
@@ -234,6 +236,13 @@ int liveupdate_unregister_flb(struct liveupdate_file_handler *fh,int liveupdate_flb_get_incoming (struct liveupdate_flb *flb,void **objp);int liveupdate_flb_get_outgoing (struct liveupdate_flb *flb,void **objp); +/* kernel can internally retrieve files */ +int liveupdate_get_file_incoming (struct liveupdate_session *s,u64 token, +struct file **filep); + +/* Get a token for an outgoing file, or -ENOENT if file is not preserved */ +int liveupdate_get_token_outgoing (struct liveupdate_session *s, +struct file *file,u64 *tokenp);#else /* CONFIG_LIVEUPDATE */
@@ -281,5 +290,17 @@ static inline int liveupdate_flb_get_outgoing(struct liveupdate_flb *flb,return -EOPNOTSUPP ; } +static inline int liveupdate_get_file_incoming (struct liveupdate_session *s, +u64 token,struct file **filep) +{ +return -EOPNOTSUPP ; +} + +static inline int liveupdate_get_token_outgoing (struct liveupdate_session *s, +struct file *file,u64 *tokenp) +{ +return -EOPNOTSUPP ; +} +#endif /* CONFIG_LIVEUPDATE */ #endif /* _LINUX_LIVEUPDATE_H */
kernel/liveupdate/luo_file.c +71 -0
116 lines not on this page · raw message
kernel/liveupdate/luo_internal.h +16 -0
@@ -77,6 +77,22 @@ struct luo_session {struct mutex mutex; }; +static inline struct liveupdate_session *luo_session_from_file_set (struct luo_file_set *file_set) +{ +struct luo_session *session; + + session = container_of(file_set, struct luo_session, file_set); + +return (struct liveupdate_session *)session; +} + +static inline struct luo_file_set *luo_file_set_from_session (struct liveupdate_session *s) +{ +struct luo_session *session = (struct luo_session *)s; + +return &session->file_set; +} +int luo_session_create (const char *name,struct file **filep);int luo_session_retrieve (const char *name,struct file **filep);int __init luo_session_setup_outgoing (void *fdt);
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 03/14] liveupdate: luo_file: Add internal APIs for file preservation
Quoted 27 lines · Samiullah Khawaja
From: Pasha Tatashin <[email protected]>
The core liveupdate mechanism allows userspace to preserve file descriptors. However, kernel subsystems often manage struct file objects directly and need to participate in the preservation process programmatically without relying solely on userspace interaction.
Signed-off-by Pasha Tatashin <[email protected]>--- include/linux/liveupdate.h | 21 ++++++++++ kernel/liveupdate/luo_file.c | 71 ++++++++++++++++++++++++++++++++ kernel/liveupdate/luo_internal.h | 16 +++++++ 3 files changed, 108 insertions(+)include/linux/liveupdate.h +1
-0@@ -23,6 +23,7 @@ struct file;/** * struct liveupdate_file_op_args - Arguments for file operation callbacks. * @handler: The file handler being called. +* @session: The session this file belongs to. * @retrieved: The retrieve status for the 'can_finish / finish' * operation. * @file: The file object. For retrieve: [OUT] The callback sets
include/linux/liveupdate.h struct file; · line 45
Quoted 5 lines
include/linux/liveupdate.h +1
-0@@ -40,6 +41,7 @@ struct file;*/ struct liveupdate_file_op_args {struct liveupdate_file_handler *handler; +struct liveupdate_session *session;bool retrieved;
Nit: I don't think this is on the latest tree. I see int retrieved [1] in the latest tree. I guess we'd need to rebase it on the latest?
Quoted 112 lines
include/linux/liveupdate.h +19
-0struct file *file;u64 serialized_data;@@ -234,6 +236,13 @@ int liveupdate_unregister_flb(struct liveupdate_file_handler *fh,int liveupdate_flb_get_incoming (struct liveupdate_flb *flb,void **objp);int liveupdate_flb_get_outgoing (struct liveupdate_flb *flb,void **objp); +/* kernel can internally retrieve files */ +int liveupdate_get_file_incoming (struct liveupdate_session *s,u64 token, +struct file **filep); + +/* Get a token for an outgoing file, or -ENOENT if file is not preserved */ +int liveupdate_get_token_outgoing (struct liveupdate_session *s, +struct file *file,u64 *tokenp);#else /* CONFIG_LIVEUPDATE */ @@ -281,5 +290,17 @@ static inline int liveupdate_flb_get_outgoing(struct liveupdate_flb *flb,return -EOPNOTSUPP ; } +static inline int liveupdate_get_file_incoming (struct liveupdate_session *s, +u64 token,struct file **filep) +{ +return -EOPNOTSUPP ; +} + +static inline int liveupdate_get_token_outgoing (struct liveupdate_session *s, +struct file *file,u64 *tokenp) +{ +return -EOPNOTSUPP ; +} +#endif /* CONFIG_LIVEUPDATE */ #endif /* _LINUX_LIVEUPDATE_H */ kernel/liveupdate/luo_file.c +21
-0@@ -302,6 +302,7 @@ int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd)mutex_init (&luo_file->mutex); args.handler = fh; + args.session =luo_session_from_file_set (file_set); args.file = file; err = fh->ops->preserve (&args);if (err)@@ -355,6 +356,7 @@ void luo_file_unpreserve_files(struct luo_file_set *file_set) struct luo_file, list); args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.file = luo_file->file; args.serialized_data = luo_file->serialized_data; args.private_data = luo_file->private_data;@@ -383,6 +385,7 @@ static int luo_file_freeze_one(struct luo_file_set *file_set,struct liveupdate_file_op_args args = {0 }; args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.file = luo_file->file; args.serialized_data = luo_file->serialized_data; args.private_data = luo_file->private_data;@@ -404,6 +407,7 @@ static void luo_file_unfreeze_one(struct luo_file_set *file_set,struct liveupdate_file_op_args args = {0 }; args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.file = luo_file->file; args.serialized_data = luo_file->serialized_data; args.private_data = luo_file->private_data;@@ -590,6 +594,7 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token, } args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.serialized_data = luo_file->serialized_data; err = luo_file->fh->ops->retrieve (&args);if (!err) {@@ -615,6 +620,7 @@ static int luo_file_can_finish_one(struct luo_file_set *file_set,struct liveupdate_file_op_args args = {0 }; args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.file = luo_file->file; args.serialized_data = luo_file->serialized_data; args.retrieved = luo_file->retrieved;@@ -632,6 +638,7 @@ static void luo_file_finish_one(struct luo_file_set *file_set,guard (mutex)(&luo_file->mutex); args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.file = luo_file->file; args.serialized_data = luo_file->serialized_data; args.retrieved = luo_file->retrieved;@@ -919,3 +926,67 @@ int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh)return err; }EXPORT_SYMBOL_GPL (liveupdate_unregister_file_handler); + +/** +* liveupdate_get_token_outgoing - Get the token for a preserved file. +* @s: The outgoing liveupdate session. +* @file: The file object to search for. +* @tokenp: Output parameter for the found token. +* +* Searches the list of preserved files in an outgoing session for a matching +* file object. If found, the corresponding user-provided token is returned. +* +* This function is intended for in-kernel callers that need to correlate a +* file with its liveupdate token. +* +* Context: Can be called from any context that can acquire the session mutex.
kernel/liveupdate/luo_file.c int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh) · line 952
Quoted 10 lines
kernel/liveupdate/luo_file.c +10
-0+* Return: 0 on success, -ENOENT if the file is not preserved in this session. +*/ +int liveupdate_get_token_outgoing (struct liveupdate_session *s, +struct file *file,u64 *tokenp) +{ +struct luo_file_set *file_set =luo_file_set_from_session (s); +struct luo_file *luo_file; +int err = -ENOENT ; + +list_for_each_entry (luo_file, &file_set->files_list, list) {
Shouldn't we hold a lock while traversing the file_set? Couldn't this race with an unpreserve? If a concurrent unpreserve (writer) calls list_del while this reader is mid-iteration, we'll likely follow a stale pointer..
I noticed that luo_preserve_file / upreserve and retrieve also don't take locks while manipulating / iterating over file_set->files_list..
Shouldn't we protect these with a lock? Otherwise, how do we avoid races in situations like:
1. CPU 0 (reader) is iterating the list A->B->C ..
2. CPU 1 (writer) is handling an unpreserve which remove file B
Am I missing something? If we're expecting the callers to hold a lock, we should have a lockdep_assert in these functions..
Quoted 20 lines
kernel/liveupdate/luo_file.c +20
-0+if (luo_file->file == file) { +if (tokenp) + *tokenp = luo_file->token; + err =0 ; +break ; + } + } + +return err; +} + +/** +* liveupdate_get_file_incoming - Retrieves a preserved file for in-kernel use. +* @s: The incoming liveupdate session (restored from the previous kernel). +* @token: The unique token identifying the file to retrieve. +* @filep: On success, this will be populated with a pointer to the retrieved +* 'struct file'. +* +* Provides a kernel-internal API for other subsystems to retrieve their +* preserved files after a live update. This function is a simple wrapper
kernel/liveupdate/luo_file.c int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh) · line 982
Quoted 10 lines
kernel/liveupdate/luo_file.c +10
-0+* around luo_retrieve_file(), allowing callers to find a file by its token. +* +* The operation is idempotent; subsequent calls for the same token will return +* a pointer to the same 'struct file' object. +* +* The caller receives a new reference to the file and must call fput() when it +* is no longer needed. The file's lifetime is managed by LUO and any userspace +* file descriptors. If the caller needs to hold a reference to the file beyond +* the immediate scope, it must call get_file() itself. +*
I'm little confused here, we say the op is idempotent, but also mention that the caller receives a new reference. I'm wondering of a situation where a driver calls this multiple times, incrementing the refcount with each call. Do we rely on flb_file_finish to drop all the refcounts?
We should clarify the lifecycle requirements here: is the driver expected to call fput() for every single call to liveupdate_get_file_incoming(), or is the flb_finish callback intended to be a 'catch-all' that reaps these?
Quoted 29 lines
kernel/liveupdate/luo_file.c +10
-0+* Context: Can be called from any context in the new kernel that has a handle +* to a restored session. +* Return: 0 on success. Returns -ENOENT if no file with the matching token is +* found, or any other negative errno on failure. +*/ +int liveupdate_get_file_incoming (struct liveupdate_session *s,u64 token, +struct file **filep) +{ +return luo_retrieve_file (luo_file_set_from_session (s), token, filep); +}kernel/liveupdate/luo_internal.h +9
-0@@ -77,6 +77,22 @@ struct luo_session {struct mutex mutex; }; +static inline struct liveupdate_session *luo_session_from_file_set (struct luo_file_set *file_set) +{ +struct luo_session *session; + + session = container_of(file_set, struct luo_session, file_set); + +return (struct liveupdate_session *)session; +} +
kernel/liveupdate/luo_internal.h struct luo_session { · line 98
Quoted 10 lines
kernel/liveupdate/luo_internal.h +7
-0+static inline struct luo_file_set *luo_file_set_from_session (struct liveupdate_session *s) +{ +struct luo_session *session = (struct luo_session *)s; + +return &session->file_set; +} +int luo_session_create (const char *name,struct file **filep);int luo_session_retrieve (const char *name,struct file **filep);int __init luo_session_setup_outgoing (void *fdt);
Thanks,
Praan
Re: [PATCH 03/14] liveupdate: luo_file: Add internal APIs for file preservation
Quoted 28 lines · Pranjal Shrivastava
Quoted 27 lines · Samiullah Khawaja
From: Pasha Tatashin <[email protected]>
The core liveupdate mechanism allows userspace to preserve file descriptors. However, kernel subsystems often manage struct file objects directly and need to participate in the preservation process programmatically without relying solely on userspace interaction.
Signed-off-by Pasha Tatashin <[email protected]>--- include/linux/liveupdate.h | 21 ++++++++++ kernel/liveupdate/luo_file.c | 71 ++++++++++++++++++++++++++++++++ kernel/liveupdate/luo_internal.h | 16 +++++++ 3 files changed, 108 insertions(+)include/linux/liveupdate.h +1
-0@@ -23,6 +23,7 @@ struct file;/** * struct liveupdate_file_op_args - Arguments for file operation callbacks. * @handler: The file handler being called. +* @session: The session this file belongs to. * @retrieved: The retrieve status for the 'can_finish / finish' * operation. * @file: The file object. For retrieve: [OUT] The callback sets @@ -40,6 +41,7 @@ struct file;
Quoted 8 lines
include/linux/liveupdate.h struct file; · line 45
Quoted 5 lines
include/linux/liveupdate.h +1
-0*/ struct liveupdate_file_op_args {struct liveupdate_file_handler *handler; +struct liveupdate_session *session;bool retrieved;Nit: I don't think this is on the latest tree. I see
int retrieved[1] in the latest tree. I guess we'd need to rebase it on the latest?
Will rebase this.
Quoted 107 lines
Quoted 104 lines
include/linux/liveupdate.h +19
-0struct file *file;u64 serialized_data;@@ -234,6 +236,13 @@ int liveupdate_unregister_flb(struct liveupdate_file_handler *fh,int liveupdate_flb_get_incoming (struct liveupdate_flb *flb,void **objp);int liveupdate_flb_get_outgoing (struct liveupdate_flb *flb,void **objp); +/* kernel can internally retrieve files */ +int liveupdate_get_file_incoming (struct liveupdate_session *s,u64 token, +struct file **filep); + +/* Get a token for an outgoing file, or -ENOENT if file is not preserved */ +int liveupdate_get_token_outgoing (struct liveupdate_session *s, +struct file *file,u64 *tokenp);#else /* CONFIG_LIVEUPDATE */ @@ -281,5 +290,17 @@ static inline int liveupdate_flb_get_outgoing(struct liveupdate_flb *flb,return -EOPNOTSUPP ; } +static inline int liveupdate_get_file_incoming (struct liveupdate_session *s, +u64 token,struct file **filep) +{ +return -EOPNOTSUPP ; +} + +static inline int liveupdate_get_token_outgoing (struct liveupdate_session *s, +struct file *file,u64 *tokenp) +{ +return -EOPNOTSUPP ; +} +#endif /* CONFIG_LIVEUPDATE */ #endif /* _LINUX_LIVEUPDATE_H */ kernel/liveupdate/luo_file.c +13
-0@@ -302,6 +302,7 @@ int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd)mutex_init (&luo_file->mutex); args.handler = fh; + args.session =luo_session_from_file_set (file_set); args.file = file; err = fh->ops->preserve (&args);if (err)@@ -355,6 +356,7 @@ void luo_file_unpreserve_files(struct luo_file_set *file_set) struct luo_file, list); args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.file = luo_file->file; args.serialized_data = luo_file->serialized_data; args.private_data = luo_file->private_data;@@ -383,6 +385,7 @@ static int luo_file_freeze_one(struct luo_file_set *file_set,struct liveupdate_file_op_args args = {0 }; args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.file = luo_file->file; args.serialized_data = luo_file->serialized_data; args.private_data = luo_file->private_data;@@ -404,6 +407,7 @@ static void luo_file_unfreeze_one(struct luo_file_set *file_set,struct liveupdate_file_op_args args = {0 }; args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.file = luo_file->file; args.serialized_data = luo_file->serialized_data; args.private_data = luo_file->private_data;@@ -590,6 +594,7 @@ int luo_retrieve_file(struct luo_file_set *file_set, u64 token, } args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.serialized_data = luo_file->serialized_data; err = luo_file->fh->ops->retrieve (&args);if (!err) {@@ -615,6 +620,7 @@ static int luo_file_can_finish_one(struct luo_file_set *file_set,struct liveupdate_file_op_args args = {0 }; args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.file = luo_file->file; args.serialized_data = luo_file->serialized_data; args.retrieved = luo_file->retrieved;@@ -632,6 +638,7 @@ static void luo_file_finish_one(struct luo_file_set *file_set,guard (mutex)(&luo_file->mutex); args.handler = luo_file->fh; + args.session =luo_session_from_file_set (file_set); args.file = luo_file->file; args.serialized_data = luo_file->serialized_data; args.retrieved = luo_file->retrieved;@@ -919,3 +926,67 @@ int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh)return err; }EXPORT_SYMBOL_GPL (liveupdate_unregister_file_handler); + +/** +* liveupdate_get_token_outgoing - Get the token for a preserved file. +* @s: The outgoing liveupdate session. +* @file: The file object to search for. +* @tokenp: Output parameter for the found token.
Quoted 8 lines
kernel/liveupdate/luo_file.c int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh) · line 942
Quoted 8 lines
kernel/liveupdate/luo_file.c +8
-0+* +* Searches the list of preserved files in an outgoing session for a matching +* file object. If found, the corresponding user-provided token is returned. +* +* This function is intended for in-kernel callers that need to correlate a +* file with its liveupdate token. +* +* Context: Can be called from any context that can acquire the session mutex.
I will reword this to convey that the session mutex should be acquired.
Quoted 7 lines
Quoted 7 lines
kernel/liveupdate/luo_file.c +7
-0+* Return: 0 on success, -ENOENT if the file is not preserved in this session. +*/ +int liveupdate_get_token_outgoing (struct liveupdate_session *s, +struct file *file,u64 *tokenp) +{ +struct luo_file_set *file_set =luo_file_set_from_session (s); +struct luo_file *luo_file;
Quoted 8 lines
kernel/liveupdate/luo_file.c int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh) · line 952
Quoted 3 lines
kernel/liveupdate/luo_file.c +3
-0+int err = -ENOENT ; + +list_for_each_entry (luo_file, &file_set->files_list, list) {Shouldn't we hold a lock while traversing the
file_set? Couldn't this race with an unpreserve? If a concurrent unpreserve (writer) callslist_delwhile this reader is mid-iteration, we'll likely follow a stale pointer..
The session mutex is supposed to be acquired when this function is called. I will add a lockdep in this function to enforce that.
Also, we call this in preserve() of vfio-cdev and the session mutex is already acquired by luo in that context.
Quoted 4 lines
I noticed that
luo_preserve_file/ upreserve and retrieve also don't take locks while manipulating / iterating overfile_set->files_list..
Quoted 8 lines
Shouldn't we protect these with a lock? Otherwise, how do we avoid races in situations like:
1. CPU 0 (reader) is iterating the list
A->B->C..
2. CPU 1 (writer) is handling an unpreserve which remove file BAm I missing something? If we're expecting the callers to hold a lock, we should have a
lockdep_assertin these functions..
The LUO acquires session mutex before calling preserve/unpreserve to protect agains race conditions. See luo_session.c for details.
Quoted 37 lines
Quoted 21 lines
kernel/liveupdate/luo_file.c +20
-0+if (luo_file->file == file) { +if (tokenp) + *tokenp = luo_file->token; + err =0 ; +break ; + } + } + +return err; +} + +/** +* liveupdate_get_file_incoming - Retrieves a preserved file for in-kernel use. +* @s: The incoming liveupdate session (restored from the previous kernel). +* @token: The unique token identifying the file to retrieve. +* @filep: On success, this will be populated with a pointer to the retrieved +* 'struct file'. +* +* Provides a kernel-internal API for other subsystems to retrieve their +* preserved files after a live update. This function is a simple wrapper kernel/liveupdate/luo_file.c int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh) · line 982
Quoted 10 lines
kernel/liveupdate/luo_file.c +10
-0+* around luo_retrieve_file(), allowing callers to find a file by its token. +* +* The operation is idempotent; subsequent calls for the same token will return +* a pointer to the same 'struct file' object. +* +* The caller receives a new reference to the file and must call fput() when it +* is no longer needed. The file's lifetime is managed by LUO and any userspace +* file descriptors. If the caller needs to hold a reference to the file beyond +* the immediate scope, it must call get_file() itself. +* I'm little confused here, we say the op is idempotent, but also mention that the caller receives a new reference. I'm wondering of a situation where a driver calls this multiple times, incrementing the refcount with each call. Do we rely on
flb_file_finishto drop all the refcounts?
Quoted 4 lines
We should clarify the lifecycle requirements here: is the driver expected to call
fput()for every single call toliveupdate_get_file_incoming(), or is theflb_finishcallback intended to be a 'catch-all' that reaps these?
The user is supposed to call fput to release the reference as mentioned in the kernel-doc above.
But I agree it is a little confusing with the idempotent. I will reword the text.
Quoted 43 lines
Quoted 30 lines
kernel/liveupdate/luo_file.c +10
-0+* Context: Can be called from any context in the new kernel that has a handle +* to a restored session. +* Return: 0 on success. Returns -ENOENT if no file with the matching token is +* found, or any other negative errno on failure. +*/ +int liveupdate_get_file_incoming (struct liveupdate_session *s,u64 token, +struct file **filep) +{ +return luo_retrieve_file (luo_file_set_from_session (s), token, filep); +}kernel/liveupdate/luo_internal.h +10
-0@@ -77,6 +77,22 @@ struct luo_session {struct mutex mutex; }; +static inline struct liveupdate_session *luo_session_from_file_set (struct luo_file_set *file_set) +{ +struct luo_session *session; + + session = container_of(file_set, struct luo_session, file_set); + +return (struct liveupdate_session *)session; +} + +static inline struct luo_file_set *luo_file_set_from_session (struct liveupdate_session *s)kernel/liveupdate/luo_internal.h struct luo_session {
Quoted 10 lines
kernel/liveupdate/luo_internal.h +6
-1+{ +struct luo_session *session = (struct luo_session *)s; + +return &session->file_set; +} +int luo_session_create (const char *name,struct file **filep);int luo_session_retrieve (const char *name,struct file **filep);int __init luo_session_setup_outgoing (void *fdt); --Thanks,
Praan
PATCH 4/14 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 | 74 +++++++++++++++++++++++++++++++++++++ drivers/iommu/iommu-pages.h | 30 +++++++++++++++ 2 files changed, 104 insertions(+)
drivers/iommu/iommu-pages.c +74 -0
@@ -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) \
@@ -131,6 +132,79 @@ void iommu_put_pages_list(struct iommu_pages_list *list) }EXPORT_SYMBOL_GPL (iommu_put_pages_list); +#if IS_ENABLED (CONFIG_IOMMU_LIVEUPDATE ) +void iommu_unpreserve_page (void *virt) +{ +kho_unpreserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_page); + +int iommu_preserve_page (void *virt) +{ +return kho_preserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_preserve_page); + +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +struct ioptdesc *iopt; + +if (!count) +return ; + +/* If less than zero then unpreserve all pages. */ +if (count <0 ) + count =0 ; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { +kho_unpreserve_folio (ioptdesc_folio (iopt)); +if (count >0 && --count ==0 ) +break ; + } +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_pages); + +void iommu_restore_page (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); + + order =folio_order (folio); + pgcnt =1UL << order; +mod_node_page_state (folio_pgdat (folio),NR_IOMMU_PAGES , pgcnt); +lruvec_stat_mod_folio (folio,NR_SECONDARY_PAGETABLE , pgcnt); +} +EXPORT_SYMBOL_GPL (iommu_restore_page); + +int iommu_preserve_pages (struct iommu_pages_list *list) +{ +struct ioptdesc *iopt; +int count =0 ; +int ret; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { + ret =kho_preserve_folio (ioptdesc_folio (iopt)); +if (ret) { +iommu_unpreserve_pages (list, count); +return ret; + } + + ++count; + } + +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_preserve_pages); + +#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_page (void *virt); +void iommu_unpreserve_page (void *virt); +int iommu_preserve_pages (struct iommu_pages_list *list); +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count); +void iommu_restore_page (u64 phys); +#else +static inline int iommu_preserve_page (void *virt) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_page (void *virt) +{ +} + +static inline int iommu_preserve_pages (struct iommu_pages_list *list) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +} + +static inline void iommu_restore_page (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.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
Quoted 59 lines · Samiullah Khawaja
IOMMU pages are allocated/freed using APIs using struct ioptdesc. For the proper preservation and restoration of ioptdesc add helper functions.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/iommu-pages.c | 74 +++++++++++++++++++++++++++++++++++++ drivers/iommu/iommu-pages.h | 30 +++++++++++++++ 2 files changed, 104 insertions(+)drivers/iommu/iommu-pages.c +34
-0@@ -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) \@@ -131,6 +132,79 @@ void iommu_put_pages_list(struct iommu_pages_list *list) }EXPORT_SYMBOL_GPL (iommu_put_pages_list); +#if IS_ENABLED (CONFIG_IOMMU_LIVEUPDATE ) +void iommu_unpreserve_page (void *virt) +{ +kho_unpreserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_page); + +int iommu_preserve_page (void *virt) +{ +return kho_preserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_preserve_page); + +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +struct ioptdesc *iopt; + +if (!count) +return ; + +/* If less than zero then unpreserve all pages. */ +if (count <0 ) + count =0 ; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { +kho_unpreserve_folio (ioptdesc_folio (iopt)); +if (count >0 && --count ==0 ) +break ; + } +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_pages); + +void iommu_restore_page (u64 phys)
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 177
Quoted 10 lines
drivers/iommu/iommu-pages.c +10
-0+{ +struct ioptdesc *iopt; +struct folio *folio; +unsigned long pgcnt; +unsigned int order; + + folio =kho_restore_folio (phys); +BUG_ON (!folio); + + iopt =folio_ioptdesc (folio);
iopt->incoherent = false; should be here?
Quoted 77 lines
drivers/iommu/iommu-pages.c +30
-0+ + order =folio_order (folio); + pgcnt =1UL << order; +mod_node_page_state (folio_pgdat (folio),NR_IOMMU_PAGES , pgcnt); + lruvec_stat_mod_folio(folio, NR_SECONDARY_PAGETABLE, pgcnt); +} +EXPORT_SYMBOL_GPL (iommu_restore_page); + +int iommu_preserve_pages (struct iommu_pages_list *list) +{ +struct ioptdesc *iopt; +int count =0 ; +int ret; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { + ret =kho_preserve_folio (ioptdesc_folio (iopt)); +if (ret) { +iommu_unpreserve_pages (list, count); +return ret; + } + + ++count; + } + +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_preserve_pages); + +#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_page (void *virt); +void iommu_unpreserve_page (void *virt); +int iommu_preserve_pages (struct iommu_pages_list *list); +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count); +void iommu_restore_page (u64 phys); +#else +static inline int iommu_preserve_page (void *virt) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_page (void *virt) +{ +} + +static inline int iommu_preserve_pages (struct iommu_pages_list *list) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +} + +static inline void iommu_restore_page (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.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
Quoted 64 lines · Ankit Soni
Quoted 63 lines · Samiullah Khawaja
IOMMU pages are allocated/freed using APIs using struct ioptdesc. For the proper preservation and restoration of ioptdesc add helper functions.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/iommu-pages.c | 74 +++++++++++++++++++++++++++++++++++++ drivers/iommu/iommu-pages.h | 30 +++++++++++++++ 2 files changed, 104 insertions(+)drivers/iommu/iommu-pages.c +38
-0@@ -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) \@@ -131,6 +132,79 @@ void iommu_put_pages_list(struct iommu_pages_list *list) }EXPORT_SYMBOL_GPL (iommu_put_pages_list); +#if IS_ENABLED (CONFIG_IOMMU_LIVEUPDATE ) +void iommu_unpreserve_page (void *virt) +{ +kho_unpreserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_page); + +int iommu_preserve_page (void *virt) +{ +return kho_preserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_preserve_page); + +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +struct ioptdesc *iopt; + +if (!count) +return ; + +/* If less than zero then unpreserve all pages. */ +if (count <0 ) + count =0 ; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { +kho_unpreserve_folio (ioptdesc_folio (iopt)); +if (count >0 && --count ==0 ) +break ; + } +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_pages); + +void iommu_restore_page (u64 phys) +{ +struct ioptdesc *iopt; +struct folio *folio; +unsigned long pgcnt;
Quoted 9 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 177
Quoted 6 lines
drivers/iommu/iommu-pages.c +6
-0+unsigned int order; + + folio =kho_restore_folio (phys); +BUG_ON (!folio); + + iopt =folio_ioptdesc (folio);
iopt->incoherent= false; should be here?
Yes this should be set here. I will update this.
Quoted 69 lines
Quoted 69 lines
drivers/iommu/iommu-pages.c +30
-0+ + order =folio_order (folio); + pgcnt =1UL << order; +mod_node_page_state (folio_pgdat (folio),NR_IOMMU_PAGES , pgcnt); + lruvec_stat_mod_folio(folio, NR_SECONDARY_PAGETABLE, pgcnt); +} +EXPORT_SYMBOL_GPL (iommu_restore_page); + +int iommu_preserve_pages (struct iommu_pages_list *list) +{ +struct ioptdesc *iopt; +int count =0 ; +int ret; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { + ret =kho_preserve_folio (ioptdesc_folio (iopt)); +if (ret) { +iommu_unpreserve_pages (list, count); +return ret; + } + + ++count; + } + +return 0 ; +} +EXPORT_SYMBOL_GPL (iommu_preserve_pages); + +#endif +/** * iommu_pages_start_incoherent - Setup the page for cache incoherent operation * @virt: The page to setup drivers/iommu/iommu-pages.h +28
-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_page (void *virt); +void iommu_unpreserve_page (void *virt); +int iommu_preserve_pages (struct iommu_pages_list *list); +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count); +void iommu_restore_page (u64 phys); +#else +static inline int iommu_preserve_page (void *virt) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_page (void *virt) +{ +} + +static inline int iommu_preserve_pages (struct iommu_pages_list *list) +{ +return -EOPNOTSUPP ; +} + +static inline void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +} + +static inline void iommu_restore_page (u64 phys) +{ +}
Quoted 8 lines
Quoted 8 lines
drivers/iommu/iommu-pages.h +2
-1+#endif +/** * iommu_pages_list_add - add the page to a iommu_pages_list * @list: List to add the page to -- 2.53.0.rc2.204.g2597b5adb4-goog
Sami
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
Quoted 75 lines · Samiullah Khawaja
Quoted 64 lines · Ankit Soni
Quoted 63 lines · Samiullah Khawaja
IOMMU pages are allocated/freed using APIs using struct ioptdesc. For the proper preservation and restoration of ioptdesc add helper functions.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/iommu-pages.c | 74 +++++++++++++++++++++++++++++++++++++ drivers/iommu/iommu-pages.h | 30 +++++++++++++++ 2 files changed, 104 insertions(+)drivers/iommu/iommu-pages.c +38
-0@@ -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) \@@ -131,6 +132,79 @@ void iommu_put_pages_list(struct iommu_pages_list *list) }EXPORT_SYMBOL_GPL (iommu_put_pages_list); +#if IS_ENABLED (CONFIG_IOMMU_LIVEUPDATE ) +void iommu_unpreserve_page (void *virt) +{ +kho_unpreserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_page); + +int iommu_preserve_page (void *virt) +{ +return kho_preserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_preserve_page); + +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +struct ioptdesc *iopt; + +if (!count) +return ; + +/* If less than zero then unpreserve all pages. */ +if (count <0 ) + count =0 ; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { +kho_unpreserve_folio (ioptdesc_folio (iopt)); +if (count >0 && --count ==0 ) +break ; + } +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_pages); + +void iommu_restore_page (u64 phys) +{ +struct ioptdesc *iopt; +struct folio *folio; +unsigned long pgcnt;Quoted 9 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 177
Quoted 6 lines
drivers/iommu/iommu-pages.c +6
-0+unsigned int order; + + folio =kho_restore_folio (phys); +BUG_ON (!folio); + + iopt =folio_ioptdesc (folio);
iopt->incoherent= false; should be here?
Quoted 1 line
Yes this should be set here. I will update this.
I'm wondering if we are silently losing state here. What if the preserved page was actually incoherent in the previous kernel? I understand we likely need to initialize it to false here because we don't have a dev pointer for DMA sync operations at this low level (though x86 uses clflush).
But when is it set back to "incoherent" again? I don't see that happening during the driver re-attach phase?
Should we at least mention that this API intentionally overwrites the preserved coherency state and that these pages must explicitly be marked incoherent again later by the driver based on its preserved HW state OR by the IOMMUFD re-attach?
Quoted 7 lines
Quoted 7 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 184
Quoted 7 lines
drivers/iommu/iommu-pages.c +7
-0+ + order =folio_order (folio); + pgcnt =1UL << order; +mod_node_page_state (folio_pgdat (folio),NR_IOMMU_PAGES , pgcnt); +lruvec_stat_mod_folio (folio,NR_SECONDARY_PAGETABLE , pgcnt); +} +EXPORT_SYMBOL_GPL (iommu_restore_page);
[------ snip >8 -------]
Thanks,
Praan
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
Quoted 78 lines · Pranjal Shrivastava
Quoted 75 lines · Samiullah Khawaja
Quoted 64 lines · Ankit Soni
Quoted 63 lines · Samiullah Khawaja
IOMMU pages are allocated/freed using APIs using struct ioptdesc. For the proper preservation and restoration of ioptdesc add helper functions.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/iommu-pages.c | 74 +++++++++++++++++++++++++++++++++++++ drivers/iommu/iommu-pages.h | 30 +++++++++++++++ 2 files changed, 104 insertions(+)drivers/iommu/iommu-pages.c +38
-0@@ -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) \@@ -131,6 +132,79 @@ void iommu_put_pages_list(struct iommu_pages_list *list) }EXPORT_SYMBOL_GPL (iommu_put_pages_list); +#if IS_ENABLED (CONFIG_IOMMU_LIVEUPDATE ) +void iommu_unpreserve_page (void *virt) +{ +kho_unpreserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_page); + +int iommu_preserve_page (void *virt) +{ +return kho_preserve_folio (ioptdesc_folio (virt_to_ioptdesc (virt))); +} +EXPORT_SYMBOL_GPL (iommu_preserve_page); + +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +struct ioptdesc *iopt; + +if (!count) +return ; + +/* If less than zero then unpreserve all pages. */ +if (count <0 ) + count =0 ; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { +kho_unpreserve_folio (ioptdesc_folio (iopt)); +if (count >0 && --count ==0 ) +break ; + } +} +EXPORT_SYMBOL_GPL (iommu_unpreserve_pages); + +void iommu_restore_page (u64 phys) +{ +struct ioptdesc *iopt; +struct folio *folio; +unsigned long pgcnt;Quoted 9 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 177
Quoted 6 lines
drivers/iommu/iommu-pages.c +6
-0+unsigned int order; + + folio =kho_restore_folio (phys); +BUG_ON (!folio); + + iopt =folio_ioptdesc (folio);
iopt->incoherent= false; should be here?Quoted 1 line
Yes this should be set here. I will update this.
Quoted 8 lines
I'm wondering if we are silently losing state here. What if the preserved page was actually incoherent in the previous kernel? I understand we likely need to initialize it to false here because we don't have a dev pointer for DMA sync operations at this low level (though x86 uses clflush).
But when is it set back to "incoherent" again? I don't see that happening during the driver re-attach phase?
This can be done during restore_domain as the domain has a reference to the dev when it is recreated. I will updated the walker and add this in the next revision.
Quoted 5 lines
Should we at least mention that this API intentionally overwrites the preserved coherency state and that these pages must explicitly be marked incoherent again later by the driver based on its preserved HW state OR by the IOMMUFD re-attach?
Agreed. I will add a Note about this.
Quoted 11 lines
Quoted 8 lines
Quoted 8 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 184
Quoted 8 lines
drivers/iommu/iommu-pages.c +7
-0+ + order =folio_order (folio); + pgcnt =1UL << order; +mod_node_page_state (folio_pgdat (folio),NR_IOMMU_PAGES , pgcnt); +lruvec_stat_mod_folio (folio,NR_SECONDARY_PAGETABLE , pgcnt); +} +EXPORT_SYMBOL_GPL (iommu_restore_page);[------ snip >8 -------]
Quoted 2 lines
Thanks,
Praan
Thanks,
Sami
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
Quoted 23 lines · Samiullah Khawaja
Quoted 23 lines
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
drivers/iommu/iommu-pages.c +5
-0+void iommu_restore_page (u64 phys) +{ +struct ioptdesc *iopt; +struct folio *folio; +unsigned long pgcnt;Quoted 9 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 177
Quoted 6 lines
drivers/iommu/iommu-pages.c +6
-0+unsigned int order; + + folio =kho_restore_folio (phys); +BUG_ON (!folio); + + iopt =folio_ioptdesc (folio);
iopt->incoherent= false; should be here?Quoted 1 line
Yes this should be set here. I will update this.
I'm wondering if we are silently losing state here. What if the preserved page was actually incoherent in the previous kernel? I understand we likely need to initialize it to false here because we don't have a dev pointer for DMA sync operations at this low level (though x86 uses clflush).
Quoted 6 lines
Quoted 2 lines
But when is it set back to "incoherent" again? I don't see that happening during the driver re-attach phase?
This can be done during
restore_domainas the domain has a reference to the dev when it is recreated. I will updated the walker and add this in the next revision.
The only reason for this tracking is for DMA API debugging. It might be better to just remove the DMA API from this path and use a direct cache flush. Then you don't really need to care, the successor kernel never writes to this memory so it can just free it in a normal way.
Otherwise you have to remap it on restore so you can unmap it on free.
Jason
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
Quoted 31 lines · Jason Gunthorpe
Quoted 23 lines · Samiullah Khawaja
Quoted 23 lines
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
drivers/iommu/iommu-pages.c +5
-0+void iommu_restore_page (u64 phys) +{ +struct ioptdesc *iopt; +struct folio *folio; +unsigned long pgcnt;Quoted 9 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 177
Quoted 6 lines
drivers/iommu/iommu-pages.c +6
-0+unsigned int order; + + folio =kho_restore_folio (phys); +BUG_ON (!folio); + + iopt =folio_ioptdesc (folio);
iopt->incoherent= false; should be here?Quoted 1 line
Yes this should be set here. I will update this.
I'm wondering if we are silently losing state here. What if the preserved page was actually incoherent in the previous kernel? I understand we likely need to initialize it to false here because we don't have a dev pointer for DMA sync operations at this low level (though x86 uses clflush).
Quoted 6 lines
Quoted 2 lines
But when is it set back to "incoherent" again? I don't see that happening during the driver re-attach phase?
This can be done during
restore_domainas the domain has a reference to the dev when it is recreated. I will updated the walker and add this in the next revision.
Quoted 6 lines
The only reason for this tracking is for DMA API debugging. It might be better to just remove the DMA API from this path and use a direct cache flush. Then you don't really need to care, the successor kernel never writes to this memory so it can just free it in a normal way.
Otherwise you have to remap it on restore so you can unmap it on free.
Interesting.. That makes sense. Since these are not going to be written by the successor kernel we can keep incoherent to be false.
Quoted 2 lines
Jason
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
In reply to Samiullah KhawajaQuoted 8 lines · Samiullah Khawaja
drivers/iommu/iommu-pages.c +8
-0+ +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +struct ioptdesc *iopt; + +if (!count) +return ; +
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 164
Quoted 10 lines
drivers/iommu/iommu-pages.c +10
-0+/* If less than zero then unpreserve all pages. */ +if (count <0 ) + count =0 ; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { +kho_unpreserve_folio (ioptdesc_folio (iopt)); +if (count >0 && --count ==0 ) +break ; + } +}
I see you are trying to have common function for error handling in iommu_preserve_pages() down and unpreserving in the next patch with overloaded meaning of count.
< 0 means unpreserve all of the pages
= 0 means do nothing
Quoted 1 line
0 unpreserve these many pages.
It seems non-intuitive to me and code also end up having multiple ifs. I will recommend to just have this function go through all of the pages without passing count argument. In iommu_preserve_pages() function down, handle the error case with a "goto err" statement. If this is the way things happen in iommu codebase feel free to ignore this suggestion.
Quoted 8 lines
drivers/iommu/iommu-pages.c +8
-0+EXPORT_SYMBOL_GPL (iommu_unpreserve_pages); + +void iommu_restore_page (u64 phys) +{ +struct ioptdesc *iopt; +struct folio *folio; +unsigned long pgcnt; +unsigned int order;
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 182
Quoted 10 lines
drivers/iommu/iommu-pages.c +10
-0+ + folio =kho_restore_folio (phys); +BUG_ON (!folio); + + iopt =folio_ioptdesc (folio); + + order =folio_order (folio); + pgcnt =1UL << order; +mod_node_page_state (folio_pgdat (folio),NR_IOMMU_PAGES , pgcnt); +lruvec_stat_mod_folio (folio,NR_SECONDARY_PAGETABLE , pgcnt);
Above two functions are also used in other two places in this file, lets create a common function for these two operations.
Quoted 3 lines
drivers/iommu/iommu-pages.c +3
-0+} +EXPORT_SYMBOL_GPL (iommu_restore_page); +
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
Quoted 26 lines · Vipin Sharma
Quoted 8 lines · Samiullah Khawaja
drivers/iommu/iommu-pages.c +8
-0+ +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +struct ioptdesc *iopt; + +if (!count) +return ; +drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 164
Quoted 10 lines
drivers/iommu/iommu-pages.c +10
-0+/* If less than zero then unpreserve all pages. */ +if (count <0 ) + count =0 ; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { +kho_unpreserve_folio (ioptdesc_folio (iopt)); +if (count >0 && --count ==0 ) +break ; + } +}I see you are trying to have common function for error handling in
iommu_preserve_pages()down and unpreserving in the next patch with overloaded meaning of count.< 0 means unpreserve all of the pages
= 0 means do nothing
Quoted 8 lines
Quoted 1 line
0 unpreserve these many pages.
It seems non-intuitive to me and code also end up having multiple ifs. I will recommend to just have this function go through all of the pages without passing count argument. In
iommu_preserve_pages()function down, handle the error case with a "goto err" statement. If this is the way things happen in iommu codebase feel free to ignore this suggestion.
+1 to this. The current version of overloaded count is counter-intuitive as it expects a back-handed API contract. Since we're using lists here, I'd recommend using the list_for_each_entry_continue_reverse() helper:
+void iommu_unpreserve_pages(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);
+
+int iommu_preserve_pages(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_rollback;
+ }
+ return 0;
+
+err_rollback:
+ /* Rollback only the successfully preserved pages */
+ list_for_each_entry_continue_reverse(iopt, &list->pages, iopt_freelist_elm)
+ kho_unpreserve_folio(ioptdesc_folio(iopt));
+ return ret;
+}
[ ---- snip >8---- ]
Thanks,
Praan
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
Quoted 36 lines · Pranjal Shrivastava
Quoted 26 lines · Vipin Sharma
Quoted 8 lines · Samiullah Khawaja
drivers/iommu/iommu-pages.c +8
-0+ +void iommu_unpreserve_pages (struct iommu_pages_list *list,int count) +{ +struct ioptdesc *iopt; + +if (!count) +return ; +drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 164
Quoted 10 lines
drivers/iommu/iommu-pages.c +10
-0+/* If less than zero then unpreserve all pages. */ +if (count <0 ) + count =0 ; + +list_for_each_entry (iopt, &list->pages, iopt_freelist_elm) { +kho_unpreserve_folio (ioptdesc_folio (iopt)); +if (count >0 && --count ==0 ) +break ; + } +}I see you are trying to have common function for error handling in
iommu_preserve_pages()down and unpreserving in the next patch with overloaded meaning of count.< 0 means unpreserve all of the pages
= 0 means do nothingQuoted 8 lines
Quoted 1 line
0 unpreserve these many pages.
It seems non-intuitive to me and code also end up having multiple ifs. I will recommend to just have this function go through all of the pages without passing count argument. In
iommu_preserve_pages()function down, handle the error case with a "goto err" statement. If this is the way things happen in iommu codebase feel free to ignore this suggestion.
Quoted 3 lines
+1 to this. The current version of overloaded count is counter-intuitive as it expects a back-handed API contract. Since we're using lists here, I'd recommend using the
list_for_each_entry_continue_reverse()helper:
Agreed. I will use this approach in next revision.
Thanks,
Sami
Quoted 33 lines
drivers/iommu/iommu-pages.c +27
-0+void iommu_unpreserve_pages (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); + +int iommu_preserve_pages (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_rollback; + } +return 0 ; + +err_rollback: +/* Rollback only the successfully preserved pages */ +list_for_each_entry_continue_reverse (iopt, &list->pages, iopt_freelist_elm) +kho_unpreserve_folio (ioptdesc_folio (iopt)); +return ret; +}[ ---- snip >8---- ]
Thanks,
Praan
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
In reply to Vipin SharmaQuoted 2 lines · Vipin Sharma
Quoted 1 line · Samiullah Khawaja
+
[------- snip >8 --------]
Quoted 13 lines
Quoted 13 lines
drivers/iommu/iommu-pages.c +13
-0+EXPORT_SYMBOL_GPL (iommu_unpreserve_pages); + +void iommu_restore_page (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);
Quoted 9 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 182
Quoted 5 lines
drivers/iommu/iommu-pages.c +5
-0+ + order =folio_order (folio); + pgcnt =1UL << order; +mod_node_page_state (folio_pgdat (folio),NR_IOMMU_PAGES , pgcnt); +lruvec_stat_mod_folio (folio,NR_SECONDARY_PAGETABLE , pgcnt);Above two functions are also used in other two places in this file, lets create a common function for these two operations.
I was thinking about this, out of the 2 other places one is __iommu_free_desc where we are supposed to subtract pgcnt.. but I think we can follow the -pgcnt pattern we have currently and add a helper iommu_folio_update_stats(folio, pgcnt) where we can pass -pgcnt to subtract nr_pages? Something like:
+static inline void iommu_folio_update_stats(struct folio *folio, 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);
+}
@@ -86,8 +91,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, -(long)pgcnt);
folio_put(folio);
}
But I'm unsure if passing -(long)pgcnt is a good idea?
Quoted 3 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 185
Quoted 3 lines
drivers/iommu/iommu-pages.c +3
-0+} +EXPORT_SYMBOL_GPL (iommu_restore_page); +
Thanks,
Praan
Re: [PATCH 04/14] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages
Quoted 51 lines · Pranjal Shrivastava
Quoted 2 lines · Vipin Sharma
Quoted 1 line · Samiullah Khawaja
+
[------- snip >8 --------]
Quoted 13 lines
Quoted 13 lines
drivers/iommu/iommu-pages.c +13
-0+EXPORT_SYMBOL_GPL (iommu_unpreserve_pages); + +void iommu_restore_page (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);Quoted 9 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 182
Quoted 5 lines
drivers/iommu/iommu-pages.c +5
-0+ + order =folio_order (folio); + pgcnt =1UL << order; +mod_node_page_state (folio_pgdat (folio),NR_IOMMU_PAGES , pgcnt); +lruvec_stat_mod_folio (folio,NR_SECONDARY_PAGETABLE , pgcnt);Above two functions are also used in other two places in this file, lets create a common function for these two operations.
I was thinking about this, out of the 2 other places one is
__iommu_free_descwhere we are supposed to subtract pgcnt.. but I think we can follow the -pgcnt pattern we have currently and add a helperiommu_folio_update_stats(folio, pgcnt)where we can pass -pgcnt to subtractnr_pages? Something like:(quoted diff) +6
-2+static inline void iommu_folio_update_stats(struct folio *folio, 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); +}@@ -86,8 +91,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, -(long)pgcnt); folio_put(folio); }
Quoted 1 line
But I'm unsure if passing -(long)pgcnt is a good idea?
mod_node_page_state() takes last argument as "long" and I can see other examples of passing -folio_nr_pages(folio) directly into it. So we can just keep it as following:
iommu_folio_update_stats(folio, -pgcnt);
I think it is a good cleanup. I will update this in the next revision.
Quoted 7 lines
Quoted 4 lines
drivers/iommu/iommu-pages.c void iommu_put_pages_list(struct iommu_pages_list *list) · line 185
Quoted 4 lines
drivers/iommu/iommu-pages.c +3
-0+} +EXPORT_SYMBOL_GPL (iommu_restore_page); +Thanks,
Praan
PATCH 5/14 iommupt: Implement preserve/unpreserve/restore callbacks
In reply to Samiullah KhawajaImplement the iommu domain ops for presevation, unpresevation and restoration of iommu domains for liveupdate. Use the existing page walker to preserve the ioptdesc of the top_table and the lower tables. Preserve the top_level also so it can be restored during boot.
--- drivers/iommu/generic_pt/iommu_pt.h | 96 +++++++++++++++++++++++++++++ include/linux/generic_pt/iommu.h | 10 +++ 2 files changed, 106 insertions(+)
drivers/iommu/generic_pt/iommu_pt.h +96 -0
@@ -921,6 +921,102 @@ int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, }EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (map_pages),"GENERIC_PT_IOMMU" ); +/** +* unpreserve() - Unpreserve page tables and other state of a domain. +* @domain: Domain to unpreserve +*/ +void DOMAIN_NS (unpreserve )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +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 (&collect.free_list,-1 ); +} +EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (unpreserve),"GENERIC_PT_IOMMU" ); + +/** +* preserve() - Preserve page tables and other state of a domain. +* @domain: Domain to preserve +* +* Returns: -ERRNO on failure, on success. +*/ +int DOMAIN_NS (preserve )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +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 (&collect.free_list); +if (ret) +return ret; + + ser->top_table =virt_to_phys (range.top_table); + ser->top_level = range.top_level; + +return 0 ; +} +EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (preserve),"GENERIC_PT_IOMMU" ); + +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_page (virt_to_phys (pts.table_lower)); + ret =pt_descend (&pts, arg, __restore_tables); +if (ret) +return ret; + } + } +return 0 ; +} + +/** +* restore() - Restore page tables and other state of a domain. +* @domain: Domain to preserve +* +* Returns: -ERRNO on failure, on success. +*/ +int DOMAIN_NS (restore )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +struct pt_common *common =common_from_iommu (iommu_table); +struct pt_range range =pt_all_range (common); + +iommu_restore_page (ser->top_table); + +/* Free new table */ +iommu_free_pages (range.top_table); + +/* Set the restored top table */ +pt_top_set (common,phys_to_virt (ser->top_table), ser->top_level); + +/* Restore all pages*/ + range =pt_all_range (common); +return pt_walk_range (&range, __restore_tables,NULL ); +} +EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (restore),"GENERIC_PT_IOMMU" ); +struct pt_unmap_args {struct iommu_pages_list free_list;pt_vaddr_t unmapped;
include/linux/generic_pt/iommu.h +10 -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
@@ -198,6 +199,12 @@ struct pt_iommu_cfg { unsigned long iova, phys_addr_t paddr, \ size_t pgsize, size_t pgcount, \ int prot, gfp_t gfp, size_t *mapped); \ + int pt_iommu_##fmt##_preserve(struct iommu_domain *domain, \ + struct iommu_domain_ser *ser); \ + void pt_iommu_##fmt##_unpreserve(struct iommu_domain *domain, \ + struct iommu_domain_ser *ser); \ + int pt_iommu_##fmt##_restore(struct iommu_domain *domain, \ + struct iommu_domain_ser *ser); \ size_t pt_iommu_##fmt##_unmap_pages( \ struct iommu_domain *domain, unsigned long iova, \ size_t pgsize, size_t pgcount, \
@@ -224,6 +231,9 @@ struct pt_iommu_cfg {#define IOMMU_PT_DOMAIN_OPS (fmt) \ .iova_to_phys = &pt_iommu_##fmt##_iova_to_phys, \ .map_pages = &pt_iommu_##fmt##_map_pages, \ + .preserve = &pt_iommu_##fmt##_preserve, \ + .unpreserve = &pt_iommu_##fmt##_unpreserve, \ + .restore = &pt_iommu_##fmt##_restore, \ .unmap_pages = &pt_iommu_##fmt##_unmap_pages#define IOMMU_PT_DIRTY_OPS (fmt) \ .read_and_clear_dirty = &pt_iommu_##fmt##_read_and_clear_dirty
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 05/14] iommupt: Implement preserve/unpreserve/restore callbacks
Quoted 16 lines · Samiullah Khawaja
Implement the iommu domain ops for presevation, unpresevation and restoration of iommu domains for liveupdate. Use the existing page walker to preserve the ioptdesc of the
top_tableand the lower tables. Preserve thetop_levelalso so it can be restored during boot.Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/generic_pt/iommu_pt.h | 96 +++++++++++++++++++++++++++++ include/linux/generic_pt/iommu.h | 10 +++ 2 files changed, 106 insertions(+)drivers/iommu/generic_pt/iommu_pt.h +0
-0
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 928
Quoted 8 lines
drivers/iommu/generic_pt/iommu_pt.h +5
-0@@ -921,6 +921,102 @@ int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, }EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (map_pages),"GENERIC_PT_IOMMU" ); +/** +* unpreserve() - Unpreserve page tables and other state of a domain. +* @domain: Domain to unpreserve +*/ +void DOMAIN_NS (unpreserve )(struct iommu_domain *domain,struct iommu_domain_ser *ser)
I don't see us using the ser arg in this function? Shall we remove it?
Quoted 11 lines
drivers/iommu/generic_pt/iommu_pt.h +11
-0+{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +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);
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 949
Quoted 10 lines
drivers/iommu/generic_pt/iommu_pt.h +10
-0+ +iommu_unpreserve_pages (&collect.free_list,-1 ); +} +EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (unpreserve),"GENERIC_PT_IOMMU" ); + +/** +* preserve() - Preserve page tables and other state of a domain. +* @domain: Domain to preserve +* +* Returns: -ERRNO on failure, on success.
Nit: 0 on success?
Quoted 28 lines
drivers/iommu/generic_pt/iommu_pt.h +28
-0+*/ +int DOMAIN_NS (preserve )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +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 (&collect.free_list); +if (ret) +return ret; + + ser->top_table =virt_to_phys (range.top_table); + ser->top_level = range.top_level; + +return 0 ; +} +EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (preserve),"GENERIC_PT_IOMMU" ); + +static int __restore_tables (struct pt_range *range,void *arg, +unsigned int level,struct pt_table_p *table)
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 987
Quoted 10 lines
drivers/iommu/generic_pt/iommu_pt.h +10
-0+{ +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_page (virt_to_phys (pts.table_lower)); + ret =pt_descend (&pts, arg, __restore_tables); +if (ret) +return ret;
If pt_descend() returns an error, we immediately return ret. However, we have already successfully called iommu_restore_page() on pts.table_lower and potentially many other tables earlier in the loop or higher up in the tree..
If the domain restore fails halfway through the tree walk, how are these partially restored pages cleaned up? Should we clean them up / free them in an err label or does the caller perform a BUG_ON(ret) ?
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 997
Quoted 10 lines
drivers/iommu/generic_pt/iommu_pt.h +10
-0+ } + } +return 0 ; +} + +/** +* restore() - Restore page tables and other state of a domain. +* @domain: Domain to preserve +* +* Returns: -ERRNO on failure, on success.
Nit: 0 on success?
Quoted 12 lines
drivers/iommu/generic_pt/iommu_pt.h +12
-0+*/ +int DOMAIN_NS (restore )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +struct pt_common *common =common_from_iommu (iommu_table); +struct pt_range range =pt_all_range (common); + +iommu_restore_page (ser->top_table); + +/* Free new table */ +iommu_free_pages (range.top_table);
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 1019
Quoted 10 lines
drivers/iommu/generic_pt/iommu_pt.h +10
-0+ +/* Set the restored top table */ +pt_top_set (common,phys_to_virt (ser->top_table), ser->top_level); + +/* Restore all pages*/ + range =pt_all_range (common); +return pt_walk_range (&range, __restore_tables,NULL ); +} +EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (restore),"GENERIC_PT_IOMMU" ); +
[ ------ >8 Snip >8------]
Thanks,
Praan
Re: [PATCH 05/14] iommupt: Implement preserve/unpreserve/restore callbacks
Quoted 20 lines · Pranjal Shrivastava
Quoted 19 lines · Samiullah Khawaja
Implement the iommu domain ops for presevation, unpresevation and restoration of iommu domains for liveupdate. Use the existing page walker to preserve the ioptdesc of the
top_tableand the lower tables. Preserve thetop_levelalso so it can be restored during boot.Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/generic_pt/iommu_pt.h | 96 +++++++++++++++++++++++++++++ include/linux/generic_pt/iommu.h | 10 +++ 2 files changed, 106 insertions(+)drivers/iommu/generic_pt/iommu_pt.h +0
-0@@ -921,6 +921,102 @@ int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, }EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (map_pages),"GENERIC_PT_IOMMU" );
Quoted 7 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 928
Quoted 5 lines
drivers/iommu/generic_pt/iommu_pt.h +5
-0+/** +* unpreserve() - Unpreserve page tables and other state of a domain. +* @domain: Domain to unpreserve +*/ +void DOMAIN_NS (unpreserve )(struct iommu_domain *domain,struct iommu_domain_ser *ser)I don't see us using the
serarg in this function? Shall we remove it?
Hmm.. this is tricky. Its not needed for GPT, but to keep the unpreserve() callback complete, "ser" should be there as some IOMMU that preserves/unpreserves more state with each domain might need this. I am ok either way, WDYT?
Quoted 16 lines
Quoted 16 lines
drivers/iommu/generic_pt/iommu_pt.h +15
-0+{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +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 (&collect.free_list,-1 ); +} +EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (unpreserve),"GENERIC_PT_IOMMU" );
Quoted 8 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 949
Quoted 6 lines
drivers/iommu/generic_pt/iommu_pt.h +6
-0+ +/** +* preserve() - Preserve page tables and other state of a domain. +* @domain: Domain to preserve +* +* Returns: -ERRNO on failure, on success. Nit: 0 on success?
Agreed. Will update.
Quoted 45 lines
Quoted 29 lines
drivers/iommu/generic_pt/iommu_pt.h +28
-0+*/ +int DOMAIN_NS (preserve )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +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 (&collect.free_list); +if (ret) +return ret; + + ser->top_table =virt_to_phys (range.top_table); + ser->top_level = range.top_level; + +return 0 ; +} +EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (preserve),"GENERIC_PT_IOMMU" ); + +static int __restore_tables (struct pt_range *range,void *arg, +unsigned int level,struct pt_table_p *table)drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 987
Quoted 10 lines
drivers/iommu/generic_pt/iommu_pt.h +10
-0+{ +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_page (virt_to_phys (pts.table_lower)); + ret =pt_descend (&pts, arg, __restore_tables); +if (ret) +return ret;If
pt_descend()returns an error, we immediately return ret. However, we have already successfully callediommu_restore_page()on pts.table_lower and potentially many other tables earlier in the loop or higher up in the tree..
Quoted 3 lines
If the domain restore fails halfway through the tree walk, how are these partially restored pages cleaned up? Should we clean them up / free them in an err label or does the caller perform a
BUG_ON(ret)?
Yes, a failed restore here should be fatal. Looking at pt_descend source, it should not fail here anyways as table_lower is init. I will add a BUG_ON here.
Quoted 13 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 997
Quoted 11 lines
drivers/iommu/generic_pt/iommu_pt.h +10
-0+ } + } +return 0 ; +} + +/** +* restore() - Restore page tables and other state of a domain. +* @domain: Domain to preserve +* +* Returns: -ERRNO on failure, on success. Nit: 0 on success?
Agreed.
Quoted 28 lines
Quoted 13 lines
drivers/iommu/generic_pt/iommu_pt.h +12
-0+*/ +int DOMAIN_NS (restore )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +struct pt_common *common =common_from_iommu (iommu_table); +struct pt_range range =pt_all_range (common); + +iommu_restore_page (ser->top_table); + +/* Free new table */ +iommu_free_pages (range.top_table);drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 1019
Quoted 10 lines
drivers/iommu/generic_pt/iommu_pt.h +10
-0+ +/* Set the restored top table */ +pt_top_set (common,phys_to_virt (ser->top_table), ser->top_level); + +/* Restore all pages*/ + range =pt_all_range (common); +return pt_walk_range (&range, __restore_tables,NULL ); +} +EXPORT_SYMBOL_NS_GPL (DOMAIN_NS (restore),"GENERIC_PT_IOMMU" ); +[ ------ >8 Snip >8------]
Thanks,
Praan
Re: [PATCH 05/14] iommupt: Implement preserve/unpreserve/restore callbacks
In reply to Pranjal ShrivastavaQuoted 9 lines · Pranjal Shrivastava
Quoted 9 lines
drivers/iommu/generic_pt/iommu_pt.h +9
-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_page (virt_to_phys (pts.table_lower));
Quoted 8 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 987
Quoted 3 lines
drivers/iommu/generic_pt/iommu_pt.h +3
-0+ ret =pt_descend (&pts, arg, __restore_tables); +if (ret) +return ret;If
pt_descend()returns an error, we immediately return ret. However, we have already successfully callediommu_restore_page()on pts.table_lower and potentially many other tables earlier in the loop or higher up in the tree..
It doesn't return an error, it just propogates errors from the callbacks which this one never errors. So this is just dead code.
Quoted 10 lines
Quoted 10 lines
drivers/iommu/generic_pt/iommu_pt.h +10
-0+int DOMAIN_NS (restore )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +struct pt_common *common =common_from_iommu (iommu_table); +struct pt_range range =pt_all_range (common); + +iommu_restore_page (ser->top_table); + +/* Free new table */
Quoted 8 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 1016
Quoted 8 lines
drivers/iommu/generic_pt/iommu_pt.h +8
-0+iommu_free_pages (range.top_table); + +/* Set the restored top table */ +pt_top_set (common,phys_to_virt (ser->top_table), ser->top_level); + +/* Restore all pages*/ + range =pt_all_range (common); +return pt_walk_range (&range, __restore_tables,NULL );
This should probably be doing something with the FEAT flags and ias/oas too or do you imagine the calling driver has to deal with that?
Jason
Re: [PATCH 05/14] iommupt: Implement preserve/unpreserve/restore callbacks
Quoted 36 lines · Jason Gunthorpe
Quoted 9 lines · Pranjal Shrivastava
Quoted 9 lines
drivers/iommu/generic_pt/iommu_pt.h +9
-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_page (virt_to_phys (pts.table_lower));Quoted 8 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 987
Quoted 3 lines
drivers/iommu/generic_pt/iommu_pt.h +3
-0+ ret =pt_descend (&pts, arg, __restore_tables); +if (ret) +return ret;If
pt_descend()returns an error, we immediately return ret. However, we have already successfully callediommu_restore_page()on pts.table_lower and potentially many other tables earlier in the loop or higher up in the tree..It doesn't return an error, it just propogates errors from the callbacks which this one never errors. So this is just dead code.
Quoted 14 lines
Quoted 14 lines
drivers/iommu/generic_pt/iommu_pt.h +14
-0+int DOMAIN_NS (restore )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +struct pt_common *common =common_from_iommu (iommu_table); +struct pt_range range =pt_all_range (common); + +iommu_restore_page (ser->top_table); + +/* Free new table */ +iommu_free_pages (range.top_table); + +/* Set the restored top table */ +pt_top_set (common,phys_to_virt (ser->top_table), ser->top_level);
Quoted 8 lines
Quoted 4 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 1016
Quoted 4 lines
drivers/iommu/generic_pt/iommu_pt.h +4
-0+ +/* Restore all pages*/ + range =pt_all_range (common); +return pt_walk_range (&range, __restore_tables,NULL );This should probably be doing something with the FEAT flags and ias/oas too or do you imagine the calling driver has to deal with that?
During boot the iommu_domain is recreated in driver and it sets up the FEAT flags and ias/oas properly. Then this generic callback is used to restore the page tables.
Currently the FEAT flags of a domain are not explicitly preserved, I will preserve them and error out here if there is a mismatch.
Quoted 2 lines
Jason
Re: [PATCH 05/14] iommupt: Implement preserve/unpreserve/restore callbacks
Quoted 46 lines · Samiullah Khawaja
Quoted 36 lines · Jason Gunthorpe
Quoted 9 lines · Pranjal Shrivastava
Quoted 9 lines
drivers/iommu/generic_pt/iommu_pt.h +9
-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_page (virt_to_phys (pts.table_lower));Quoted 8 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 987
Quoted 3 lines
drivers/iommu/generic_pt/iommu_pt.h +3
-0+ ret =pt_descend (&pts, arg, __restore_tables); +if (ret) +return ret;If
pt_descend()returns an error, we immediately return ret. However, we have already successfully callediommu_restore_page()on pts.table_lower and potentially many other tables earlier in the loop or higher up in the tree..It doesn't return an error, it just propogates errors from the callbacks which this one never errors. So this is just dead code.
Quoted 14 lines
Quoted 14 lines
drivers/iommu/generic_pt/iommu_pt.h +14
-0+int DOMAIN_NS (restore )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +struct pt_common *common =common_from_iommu (iommu_table); +struct pt_range range =pt_all_range (common); + +iommu_restore_page (ser->top_table); + +/* Free new table */ +iommu_free_pages (range.top_table); + +/* Set the restored top table */ +pt_top_set (common,phys_to_virt (ser->top_table), ser->top_level);Quoted 8 lines
Quoted 4 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 1016
Quoted 4 lines
drivers/iommu/generic_pt/iommu_pt.h +4
-0+ +/* Restore all pages*/ + range =pt_all_range (common); +return pt_walk_range (&range, __restore_tables,NULL );This should probably be doing something with the FEAT flags and ias/oas too or do you imagine the calling driver has to deal with that?
Quoted 6 lines
During boot the
iommu_domainis recreated in driver and it sets up the FEAT flags and ias/oas properly. Then this generic callback is used to restore the page tables.Currently the FEAT flags of a domain are not explicitly preserved, I will preserve them and error out here if there is a mismatch.
Hrm, that expands the ABI a bit though
If the only operation on the restored table is free then I suppose it can be simplified quite a bit, you just need the minimal things that the collect walker in free touches.
Is that the intention, free only?
If so then the restored iommu_domain should be some special free only domain too.
Jason
Re: [PATCH 05/14] iommupt: Implement preserve/unpreserve/restore callbacks
Quoted 51 lines · Jason Gunthorpe
Quoted 50 lines · Samiullah Khawaja
Quoted 36 lines · Jason Gunthorpe
Quoted 9 lines · Pranjal Shrivastava
Quoted 9 lines
drivers/iommu/generic_pt/iommu_pt.h +9
-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_page (virt_to_phys (pts.table_lower));Quoted 8 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 987
Quoted 3 lines
drivers/iommu/generic_pt/iommu_pt.h +3
-0+ ret =pt_descend (&pts, arg, __restore_tables); +if (ret) +return ret;If
pt_descend()returns an error, we immediately return ret. However, we have already successfully callediommu_restore_page()on pts.table_lower and potentially many other tables earlier in the loop or higher up in the tree..It doesn't return an error, it just propogates errors from the callbacks which this one never errors. So this is just dead code.
Quoted 14 lines
Quoted 14 lines
drivers/iommu/generic_pt/iommu_pt.h +14
-0+int DOMAIN_NS (restore )(struct iommu_domain *domain,struct iommu_domain_ser *ser) +{ +struct pt_iommu *iommu_table = + container_of(domain, struct pt_iommu, domain); +struct pt_common *common =common_from_iommu (iommu_table); +struct pt_range range =pt_all_range (common); + +iommu_restore_page (ser->top_table); + +/* Free new table */ +iommu_free_pages (range.top_table); + +/* Set the restored top table */ +pt_top_set (common,phys_to_virt (ser->top_table), ser->top_level);Quoted 8 lines
Quoted 4 lines
drivers/iommu/generic_pt/iommu_pt.h int DOMAIN_NS(map_pages)(struct iommu_domain *domain, unsigned long iova, · line 1016
Quoted 4 lines
drivers/iommu/generic_pt/iommu_pt.h +4
-0+ +/* Restore all pages*/ + range =pt_all_range (common); +return pt_walk_range (&range, __restore_tables,NULL );This should probably be doing something with the FEAT flags and ias/oas too or do you imagine the calling driver has to deal with that?
During boot the
iommu_domainis recreated in driver and it sets up the FEAT flags and ias/oas properly. Then this generic callback is used to restore the page tables.
Quoted 8 lines
Quoted 2 lines
Currently the FEAT flags of a domain are not explicitly preserved, I will preserve them and error out here if there is a mismatch.
Hrm, that expands the ABI a bit though
If the only operation on the restored table is free then I suppose it can be simplified quite a bit, you just need the minimal things that the collect walker in free touches.
Yes, we use the collect walker during KHO restore of the preserved pages and also during free. But if I understand correctly, the collect walker behaviour changes based on some FEAT_ flags (like SIGN_EXTEND). So we have to be careful if the previous kernel was using different FEAT flags that affect the collect walker. To handle this, we can just preserve the u32 features from struct pt_common and deduce everything using that.
Or are you suggesting not to save u32 features at all?
Thinking about it more, we do preserve the top_level, so that could potentially be used to walk over the page tables of these free-only domains if we just set up the pts->index and pts->end_index properly by initializing the range based on the top_level. Are you thinking of a similar approach to walk these free-only domains?
Quoted 2 lines
Is that the intention, free only?
Yes, the intention is to free only. This domain will be immutable and can only be freed.
Quoted 3 lines
If so then the restored
iommu_domainshould be some special free only domain too.
Agreed.
Quoted 2 lines
Jason
Re: [PATCH 05/14] iommupt: Implement preserve/unpreserve/restore callbacks
Quoted 6 lines · Samiullah Khawaja
Yes, we use the collect walker during KHO restore of the preserved pages and also during free. But if I understand correctly, the collect walker behaviour changes based on some
FEAT_flags (likeSIGN_EXTEND). So we have to be careful if the previous kernel was using different FEAT flags that affect the collect walker. To handle this, we can just preserve the u32 features from structpt_commonand deduce everything using that.
It is agressively not stable, and even new kernels might not support the features old ones were using, so this is a bit dicy.
You probably need some, but I think I'd be very choosey about which some I preserve and which come from the new driver.
Quoted 5 lines
Thinking about it more, we do preserve the
top_level, so that could potentially be used to walk over the page tables of these free-only domains if we just set up thepts->indexandpts->end_indexproperly by initializing the range based on thetop_level. Are you thinking of a similar approach to walk these free-only domains?
Hmm, even the free walker requires computing the end_index, and that requires at least vasz. eg ARM will compute the number of items in the top level based on vasz to support concatenated pages.
I don't think sign_extend is used in the free path though.
So if that is the only purpose then yes you can probably get away with nothing, but you have to be very careful to contain the restored domain to only execute free and nothing else by removing the map/unmap/etc ops.
And add some dedicated self tests (zero pt features and test free) to simulate it.
Jason
Re: [PATCH 05/14] iommupt: Implement preserve/unpreserve/restore callbacks
Quoted 10 lines · Jason Gunthorpe
Quoted 6 lines · Samiullah Khawaja
Yes, we use the collect walker during KHO restore of the preserved pages and also during free. But if I understand correctly, the collect walker behaviour changes based on some
FEAT_flags (likeSIGN_EXTEND). So we have to be careful if the previous kernel was using different FEAT flags that affect the collect walker. To handle this, we can just preserve the u32 features from structpt_commonand deduce everything using that.It is agressively not stable, and even new kernels might not support the features old ones were using, so this is a bit dicy.
Agreed.
Quoted 10 lines
You probably need some, but I think I'd be very choosey about which some I preserve and which come from the new driver.
Quoted 5 lines
Thinking about it more, we do preserve the
top_level, so that could potentially be used to walk over the page tables of these free-only domains if we just set up thepts->indexandpts->end_indexproperly by initializing the range based on thetop_level. Are you thinking of a similar approach to walk these free-only domains?
Quoted 3 lines
Hmm, even the free walker requires computing the
end_index, and that requires at least vasz. eg ARM will compute the number of items in the top level based on vasz to support concatenated pages.
Yes end_index setup using "derived vasz" is exactly what I was referring to earlier. Basically KHO stores the order of the folio, so we can look at the order of the top_table and that should give us an idea of vasz? Even if actual vasz is less than the one drived from the order of the page in next kernel, rest of the page should be filled with zeroes, so iterating through it should be fine?
I think to keep the scope of this series limited and also to do it in the proper way, I can save the vasz. That should give us everything to free and restore in the next kernel without introducing too much complexity and ABI right now. We can put in more functionality with subsequent patch series to handle other usecases. WDYT?
Quoted 2 lines
I don't think
sign_extendis used in the free path though.
I think it is used in the pt_all_range() during free to calculate the range to iterate over. Based on the sign_extend it decides the vasz and that affect last_va and that affects end_index.
Quoted 10 lines
So if that is the only purpose then yes you can probably get away with nothing, but you have to be very careful to contain the restored domain to only execute free and nothing else by removing the map/unmap/etc ops.
And add some dedicated self tests (zero pt features and test free) to simulate it.
Jason
Re: [PATCH 05/14] iommupt: Implement preserve/unpreserve/restore callbacks
Quoted 8 lines · Samiullah Khawaja
Quoted 3 lines
Hmm, even the free walker requires computing the
end_index, and that requires at least vasz. eg ARM will compute the number of items in the top level based on vasz to support concatenated pages.Yes
end_indexsetup using "derived vasz" is exactly what I was referring to earlier. Basically KHO stores the order of the folio, so we can look at the order of thetop_tableand that should give us an idea of vasz?
The top range can be smaller than a full page too.
Quoted 3 lines
Even if actual vasz is less than the one drived from the order of the page in next kernel, rest of the page should be filled with zeroes, so iterating through it should be fine?
I wouldn't do that, it technically breaks the design.
Quoted 2 lines
complexity and ABI right now. We can put in more functionality with subsequent patch series to handle other usecases. WDYT?
Yeah
Quoted 5 lines
Quoted 1 line
I don't think
sign_extendis used in the free path though.I think it is used in the
pt_all_range()during free to calculate the range to iterate over. Based on thesign_extendit decides the vasz and that affectlast_vaand that affectsend_index.
Hmm, Okay it used to not do that, now it does :)
So that will need to be preserved too
Jason
PATCH 6/14 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.
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 need to be cleared.
--- drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 47 ++++++++++- drivers/iommu/intel/iommu.h | 27 +++++++ drivers/iommu/intel/liveupdate.c | 134 +++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 drivers/iommu/intel/liveupdate.c
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 +43 -4
83 lines not on this page · raw message
drivers/iommu/intel/iommu.h +27 -0
@@ -557,6 +557,8 @@ struct root_entry {u64 hi; }; +#define ROOT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(struct root_entry)) +/* * low 64 bits: * 0: present
@@ -1276,6 +1278,31 @@ 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 device_ser *device_ser); +void intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser); +int intel_iommu_preserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser); +#else +static inline int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) +{ +return -EOPNOTSUPP ; +} + +static inline void intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser) +{ +} + +static inline int intel_iommu_preserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser) +{ +return -EOPNOTSUPP ; +} + +static inline void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_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 +134 -0
134 lines not on this page · raw message
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 06/14] iommu/vt-d: Implement device and iommu preserve/unpreserve ops
drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2385
Quoted 13 lines · Samiullah Khawaja
/*
* Take aroot_entryand return the Lower Context Table Pointer (LCTP)
* if marked present.drivers/iommu/intel/iommu.c +4
-2@@ -2378,8 +2379,10 @@ 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); +if (!__maybe_clean_unpreserved_context_entries (iommu)) { +/* Make sure the IOMMUs are switched off */ +iommu_disable_translation (iommu); + }
I think instead of having a "maybe" function where it might or might not clear the context entry, it will be simpler to just make if-else explicit here.
@@ -2375,8 +2375,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);
+ if (iommu->iommu.outgoing_preserved_state)
+ clear_unpreserved_context_entries();
+ else
+ /* Make sure the IOMMUs are switched off */
+ iommu_disable_translation(iommu);
}}
clear_unpreserved_context_entries() will explicitly do what __maybe_clean_unpreserved_context_entries() is doing.
drivers/iommu/intel/iommu.c static const struct iommu_dirty_ops intel_second_stage_dirty_ops = { · line 2909
Quoted 9 lines
drivers/iommu/intel/iommu.c +2
-0} }@@ -2902,6 +2905,38 @@ static const struct iommu_dirty_ops intel_second_stage_dirty_ops = { .set_dirty_tracking = intel_iommu_set_dirty_tracking, }; +#ifdef CONFIG_IOMMU_LIVEUPDATE +static bool __maybe_clean_unpreserved_context_entries (struct intel_iommu *iommu)
Nit: s/clean/clear, this matches domain_context_clear().
Quoted 25 lines
drivers/iommu/intel/iommu.c +24
-0+{ +struct device_domain_info *info; +struct pci_dev *pdev =NULL ; + +if (!iommu->iommu.outgoing_preserved_state) +return false; + +for_each_pci_dev (pdev) { + info =dev_iommu_priv_get (&pdev->dev); +if (!info) +continue ; + +if (info->iommu != iommu) +continue ; + +if (dev_iommu_preserved_state (&pdev->dev)) +continue ; + +domain_context_clear (info); + } + +return true; +} +++ b/drivers/iommu/intel/liveupdate.c
drivers/iommu/intel/iommu.c line 8
Quoted 8 lines
drivers/iommu/intel/iommu.c +8
-0@@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2025, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#define pr_fmt (fmt) "iommu: liveupdate: " fmt
Should this be 'DMAR: liveupdate' to be consistent with other files in intel iommu?
drivers/iommu/intel/liveupdate.c line 19
Quoted 11 lines
drivers/iommu/intel/liveupdate.c new file +11
-0+ +#include <linux/kexec_handover.h> +#include <linux/liveupdate.h> +#include <linux/iommu-lu.h> +#include <linux/module.h> +#include <linux/pci.h> + +#include "iommu.h" +#include "../iommu-pages.h" + +static void unpreserve_iommu_context (struct intel_iommu *iommu,int end)
Nit: Can this be unpreserve_iommu_context_table(), similar to existing free_context_table()? Same for corresponding preserve function
drivers/iommu/intel/liveupdate.c line 25
Quoted 6 lines
drivers/iommu/intel/liveupdate.c new file +6
-0+{ +struct context_entry *context; +int i; + +if (end <0 ) + end =ROOT_ENTRY_NR ;
Can't we just pass ROOT_ENTRY_NR as end value instead of -1?
Re: [PATCH 06/14] iommu/vt-d: Implement device and iommu preserve/unpreserve ops
Quoted 20 lines · Vipin Sharma
drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2385
Quoted 13 lines · Samiullah Khawaja
/*
* Take aroot_entryand return the Lower Context Table Pointer (LCTP)
* if marked present.drivers/iommu/intel/iommu.c +4
-2@@ -2378,8 +2379,10 @@ 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); +if (!__maybe_clean_unpreserved_context_entries (iommu)) { +/* Make sure the IOMMUs are switched off */ +iommu_disable_translation (iommu); + }I think instead of having a "maybe" function where it might or might not clear the context entry, it will be simpler to just make if-else explicit here.
drivers/iommu/intel/iommu.c void intel_iommu_shutdown(void) · line 2378
Quoted 6 lines
drivers/iommu/intel/iommu.c +1
-2@@ -2375,8 +2375,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); +if (iommu->iommu.outgoing_preserved_state)
This is only defined when CONFIG_LIVEUPDATE is enabled and would need the #ifdefs :(.
But I can add a function, iommu_outgoing_preserved_state() or iommu_is_outgoing_preserved() that would return NULL or false respectively when liveupdate is disabled, in the skeleton patch to check this.
I will update this in the next revision.
Quoted 13 lines
drivers/iommu/intel/iommu.c +4
-0+clear_unpreserved_context_entries (); + else +/* Make sure the IOMMUs are switched off */ +iommu_disable_translation (iommu ); } }
clear_unpreserved_context_entries()will explicitly do what__maybe_clean_unpreserved_context_entries()is doing.Quoted 3 lines
(quoted diff) +0
-0} }
Quoted 8 lines
drivers/iommu/intel/iommu.c static const struct iommu_dirty_ops intel_second_stage_dirty_ops = { · line 2909
Quoted 6 lines
drivers/iommu/intel/iommu.c +2
-0@@ -2902,6 +2905,38 @@ static const struct iommu_dirty_ops intel_second_stage_dirty_ops = { .set_dirty_tracking = intel_iommu_set_dirty_tracking, }; +#ifdef CONFIG_IOMMU_LIVEUPDATE +static bool __maybe_clean_unpreserved_context_entries (struct intel_iommu *iommu)Nit: s/clean/clear, this matches
domain_context_clear().
Agreed.
Quoted 29 lines
Quoted 29 lines
drivers/iommu/intel/iommu.c +27
-0+{ +struct device_domain_info *info; +struct pci_dev *pdev =NULL ; + +if (!iommu->iommu.outgoing_preserved_state) +return false; + +for_each_pci_dev (pdev) { + info =dev_iommu_priv_get (&pdev->dev); +if (!info) +continue ; + +if (info->iommu != iommu) +continue ; + +if (dev_iommu_preserved_state (&pdev->dev)) +continue ; + +domain_context_clear (info); + } + +return true; +} +++ b/drivers/iommu/intel/liveupdate.c@@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/*
Quoted 8 lines
drivers/iommu/intel/liveupdate.c line 8
Quoted 5 lines
drivers/iommu/intel/liveupdate.c new file +5
-0+* Copyright (C) 2025, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#define pr_fmt (fmt) "iommu: liveupdate: " fmtShould this be 'DMAR: liveupdate' to be consistent with other files in intel iommu?
Agreed. Will change.
Quoted 15 lines
drivers/iommu/intel/liveupdate.c line 19
Quoted 12 lines
drivers/iommu/intel/liveupdate.c new file +11
-0+ +#include <linux/kexec_handover.h> +#include <linux/liveupdate.h> +#include <linux/iommu-lu.h> +#include <linux/module.h> +#include <linux/pci.h> + +#include "iommu.h" +#include "../iommu-pages.h" + +static void unpreserve_iommu_context (struct intel_iommu *iommu,int end)Nit: Can this be
unpreserve_iommu_context_table(), similar to existingfree_context_table()? Same for corresponding preserve function
Agreed. I will change this.
Quoted 9 lines
drivers/iommu/intel/liveupdate.c line 25
Quoted 7 lines
drivers/iommu/intel/liveupdate.c new file +6
-0+{ +struct context_entry *context; +int i; + +if (end <0 ) + end =ROOT_ENTRY_NR ;Can't we just pass
ROOT_ENTRY_NRas end value instead of -1?
Yes, we can do that. Will update.
Quoted 1 line
Thanks,
Sami
Re: [PATCH 06/14] iommu/vt-d: Implement device and iommu preserve/unpreserve ops
In reply to Samiullah KhawajaQuoted 73 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.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 need to be cleared.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 47 ++++++++++- drivers/iommu/intel/iommu.h | 27 +++++++ drivers/iommu/intel/liveupdate.c | 134 +++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 drivers/iommu/intel/liveupdate.cdrivers/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 +7
-4@@ -16,6 +16,7 @@#include <linux/crash_dump.h> #include <linux/dma-direct.h> #include <linux/dmi.h> +#include <linux/iommu-lu.h> #include <linux/memory.h> #include <linux/pci.h> #include <linux/pci-ats.h> @@ -52,6 +53,8 @@ static int rwbf_quirk;#define rwbf_required (iommu) (rwbf_quirk || cap_rwbf((iommu)->cap)) +static bool __maybe_clean_unpreserved_context_entries (struct intel_iommu *iommu); +/* * set to 1 to panic kernel if can't successfully enable VT-d * (used when kernel is launched w/ TXT) @@ -60,8 +63,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. @@ -2378,8 +2379,10 @@ 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); +if (!__maybe_clean_unpreserved_context_entries (iommu)) { +/* Make sure the IOMMUs are switched off */ +iommu_disable_translation (iommu); + } } }@@ -2902,6 +2905,38 @@ static const struct iommu_dirty_ops intel_second_stage_dirty_ops = { .set_dirty_tracking = intel_iommu_set_dirty_tracking, };
drivers/iommu/intel/iommu.c static const struct iommu_dirty_ops intel_second_stage_dirty_ops = { · line 2917
Quoted 10 lines
drivers/iommu/intel/iommu.c +10
-0+#ifdef CONFIG_IOMMU_LIVEUPDATE +static bool __maybe_clean_unpreserved_context_entries (struct intel_iommu *iommu) +{ +struct device_domain_info *info; +struct pci_dev *pdev =NULL ; + +if (!iommu->iommu.outgoing_preserved_state) +return false; + +for_each_pci_dev (pdev) {
Shouldn't we *also* clean context entries for non-pci devices?
AFAICT, in the current code, we simply turn off translation during shutdowm. However, with this change, because this cleanup loop only runs for_each_pci_dev(), it completely ignores any non-PCI devices (like platform devices) that might be attached to this IOMMU?
Since the IOMMU is left powered on during the handover, skipping non-PCI devices in this cleanup loop means their context entries are left perfectly intact and active in the hardware root table. There could be a chance that the new kernel doesn't reclaim/overwrite these entries in which case it could result in zombie DMAs surviving the KHO.
Quoted 158 lines
drivers/iommu/intel/iommu.c +26
-0+ info =dev_iommu_priv_get (&pdev->dev); +if (!info) +continue ; + +if (info->iommu != iommu) +continue ; + +if (dev_iommu_preserved_state (&pdev->dev)) +continue ; + + domain_context_clear(info); + } + +return true; +} +#else +static bool __maybe_clean_unpreserved_context_entries (struct intel_iommu *iommu) +{ +return false; +} +#endif +static struct iommu_domain *intel_iommu_domain_alloc_second_stage (struct device *dev,struct intel_iommu *iommu,u32 flags)@@ -3925,6 +3960,10 @@ 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, + .preserve_device = intel_iommu_preserve_device, + .unpreserve_device = intel_iommu_unpreserve_device, + .preserve = intel_iommu_preserve, + .unpreserve = intel_iommu_unpreserve, };static void quirk_iommu_igfx (struct pci_dev *dev)drivers/iommu/intel/iommu.h +27
-0@@ -557,6 +557,8 @@ struct root_entry {u64 hi; }; +#define ROOT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(struct root_entry)) +/* * low 64 bits: * 0: present @@ -1276,6 +1278,31 @@ 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 device_ser *device_ser); +void intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser); +int intel_iommu_preserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser); +#else +static inline int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) +{ +return -EOPNOTSUPP ; +} + +static inline void intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser) +{ +} + +static inline int intel_iommu_preserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser) +{ +return -EOPNOTSUPP ; +} + +static inline void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_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 +71
-0@@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2025, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#define pr_fmt (fmt) "iommu: liveupdate: " fmt + +#include <linux/kexec_handover.h> +#include <linux/liveupdate.h> +#include <linux/iommu-lu.h> +#include <linux/module.h> +#include <linux/pci.h> + +#include "iommu.h" +#include "../iommu-pages.h" + +static void unpreserve_iommu_context (struct intel_iommu *iommu,int end) +{ +struct context_entry *context; +int i; + +if (end <0 ) + end =ROOT_ENTRY_NR ; + +for (i =0 ; i < end; i++) { + context =iommu_context_addr (iommu, i,0 ,0 ); +if (context) +iommu_unpreserve_page (context); + +if (!sm_supported (iommu)) +continue ; + + context =iommu_context_addr (iommu, i,0x80 ,0 ); +if (context) +iommu_unpreserve_page (context); + } +} + +static int preserve_iommu_context (struct intel_iommu *iommu) +{ +struct context_entry *context; +int ret; +int i; + +for (i =0 ; i <ROOT_ENTRY_NR ; i++) { + context =iommu_context_addr (iommu, i,0 ,0 ); +if (context) { + ret =iommu_preserve_page (context); +if (ret) + goto error; + } + +if (!sm_supported (iommu)) +continue ; + + context =iommu_context_addr (iommu, i,0x80 ,0 ); +if (context) { + ret =iommu_preserve_page (context); +if (ret) + goto error_sm; + } + } + +return 0 ; + +error_sm: + context =iommu_context_addr (iommu, i,0 ,0 ); +iommu_unpreserve_page (context); +error:
drivers/iommu/intel/liveupdate.c line 81
Quoted 10 lines
drivers/iommu/intel/liveupdate.c new file +10
-0+unpreserve_iommu_context (iommu, i); +return ret; +} + +int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) +{ +struct device_domain_info *info =dev_iommu_priv_get (dev); + +if (!dev_is_pci (dev)) +return -EOPNOTSUPP ;
Maybe also WARN_ON_ONCE ?
drivers/iommu/intel/liveupdate.c line 92
Quoted 11 lines
drivers/iommu/intel/liveupdate.c new file +11
-0+ +if (!info) +return -EINVAL ; + + device_ser->domain_iommu_ser.did =domain_id_iommu (info->domain, info->iommu); +return 0 ; +} + +void intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser) +{ +}
Nit: We can just not populate the unpreserve_device op here, we don't need an empty function
drivers/iommu/intel/liveupdate.c line 101
Quoted 9 lines
drivers/iommu/intel/liveupdate.c new file +9
-0+ +int intel_iommu_preserve (struct iommu_device *iommu_dev,struct iommu_ser *ser) +{ +struct intel_iommu *iommu; +int ret; + + iommu = container_of(iommu_dev, struct intel_iommu, iommu); + +spin_lock (&iommu->lock);
Minor note: We call this with the session->mutex acquired which is a sleepable/preemptable context whereas spin_lock disables preemption and puts us in an atomic context.
While iommu_context_addr() happens to be safe here because it uses GFP_ATOMIC & we pass alloc=0, we are heavily relying on the assumption that iommu_context_addr() or other helpers will never sleep or attempt a GFP_KERNEL allocation under the hood.
Given how easy it would be for a future refactor to accidentally introduce a sleeping lock inside any of the other helpers, I'd recommend adding an explicit comment here warning that this runs in an atomic context (same for unpreserve below).
drivers/iommu/intel/liveupdate.c line 108
Quoted 7 lines
r + ret =
preserve_iommu_context(iommu);drivers/iommu/intel/liveupdate.c new file +6
-0+if (ret) + goto err; + + ret =iommu_preserve_page (iommu->root_entry); +if (ret) { +unpreserve_iommu_context (iommu,-1 );
Nit: The same overloading discussion as Vipin pointed out in the other patch.
Quoted 16 lines
drivers/iommu/intel/liveupdate.c new file +16
-0+ goto err; + } + + 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; +spin_unlock (&iommu->lock); + +return 0 ; +err: +spin_unlock (&iommu->lock); +return ret; +} + +void intel_iommu_unpreserve (struct iommu_device *iommu_dev,struct iommu_ser *iommu_ser)
drivers/iommu/intel/liveupdate.c line 134
Quoted 10 lines
drivers/iommu/intel/liveupdate.c new file +10
-0+{ +struct intel_iommu *iommu; + + iommu = container_of(iommu_dev, struct intel_iommu, iommu); + +spin_lock (&iommu->lock); +unpreserve_iommu_context (iommu,-1 ); +iommu_unpreserve_page (iommu->root_entry); +spin_unlock (&iommu->lock); +}
Thanks,
Praan
Re: [PATCH 06/14] iommu/vt-d: Implement device and iommu preserve/unpreserve ops
Quoted 33 lines · Pranjal Shrivastava
Quoted 32 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.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 need to be cleared.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 47 ++++++++++- drivers/iommu/intel/iommu.h | 27 +++++++ drivers/iommu/intel/liveupdate.c | 134 +++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 drivers/iommu/intel/liveupdate.cdrivers/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 +0
-0@@ -16,6 +16,7 @@#include <linux/crash_dump.h>
Quoted 8 lines
drivers/iommu/intel/iommu.c static int rwbf_quirk; · line 53
Quoted 8 lines
drivers/iommu/intel/iommu.c +1
-0#include <linux/dma-direct.h> #include <linux/dmi.h> +#include <linux/iommu-lu.h> #include <linux/memory.h> #include <linux/pci.h> #include <linux/pci-ats.h> @@ -52,6 +53,8 @@ static int rwbf_quirk;
[ ---- snip >8 ----- ]
Quoted 19 lines
drivers/iommu/intel/iommu.c static int rwbf_quirk;
Quoted 9 lines
drivers/iommu/intel/iommu.c +9
-0+ +int intel_iommu_preserve (struct iommu_device *iommu_dev,struct iommu_ser *ser) +{ +struct intel_iommu *iommu; +int ret; + + iommu = container_of(iommu_dev, struct intel_iommu, iommu); + +spin_lock (&iommu->lock);Minor note: We call this with the
session->mutexacquired which is a sleepable/preemptable context whereasspin_lockdisables preemption and puts us in an atomic context.While
iommu_context_addr()happens to be safe here because it usesGFP_ATOMIC& we pass alloc=0, we are heavily relying on the assumption thatiommu_context_addr()or other helpers will never sleep or attempt aGFP_KERNELallocation under the hood.
Quoted 5 lines
Given how easy it would be for a future refactor to accidentally introduce a sleeping lock inside any of the other helpers, I'd recommend adding an explicit comment here warning that this runs in an atomic context (same for unpreserve below).
Thinking a little more about this, I think we shall instead add a comment in the core API docs, mentioning that these helpers (like iommu_preserve_page etc.) can be called from atomic context and hence should not acquire any mutexes as we can't be certain of the context the drivers call them in.
Thanks,
Praan
Re: [PATCH 06/14] iommu/vt-d: Implement device and iommu preserve/unpreserve ops
In reply to Pranjal ShrivastavaQuoted 92 lines · Pranjal Shrivastava
Quoted 73 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.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 need to be cleared.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/iommu.c | 47 ++++++++++- drivers/iommu/intel/iommu.h | 27 +++++++ drivers/iommu/intel/liveupdate.c | 134 +++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 drivers/iommu/intel/liveupdate.cdrivers/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 +7
-4@@ -16,6 +16,7 @@#include <linux/crash_dump.h> #include <linux/dma-direct.h> #include <linux/dmi.h> +#include <linux/iommu-lu.h> #include <linux/memory.h> #include <linux/pci.h> #include <linux/pci-ats.h> @@ -52,6 +53,8 @@ static int rwbf_quirk;#define rwbf_required (iommu) (rwbf_quirk || cap_rwbf((iommu)->cap)) +static bool __maybe_clean_unpreserved_context_entries (struct intel_iommu *iommu); +/* * set to 1 to panic kernel if can't successfully enable VT-d * (used when kernel is launched w/ TXT) @@ -60,8 +63,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. @@ -2378,8 +2379,10 @@ 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); +if (!__maybe_clean_unpreserved_context_entries (iommu)) { +/* Make sure the IOMMUs are switched off */ +iommu_disable_translation (iommu); + } } }@@ -2902,6 +2905,38 @@ static const struct iommu_dirty_ops intel_second_stage_dirty_ops = { .set_dirty_tracking = intel_iommu_set_dirty_tracking, };drivers/iommu/intel/iommu.c static const struct iommu_dirty_ops intel_second_stage_dirty_ops = { · line 2917
Quoted 10 lines
drivers/iommu/intel/iommu.c +10
-0+#ifdef CONFIG_IOMMU_LIVEUPDATE +static bool __maybe_clean_unpreserved_context_entries (struct intel_iommu *iommu) +{ +struct device_domain_info *info; +struct pci_dev *pdev =NULL ; + +if (!iommu->iommu.outgoing_preserved_state) +return false; + +for_each_pci_dev (pdev) {Shouldn't we *also* clean context entries for non-pci devices?
AFAICT, in the current code, we simply turn off translation during shutdowm. However, with this change, because this cleanup loop only runs
for_each_pci_dev(), it completely ignores any non-PCI devices (like platform devices) that might be attached to this IOMMU?
Quoted 5 lines
Since the IOMMU is left powered on during the handover, skipping non-PCI devices in this cleanup loop means their context entries are left perfectly intact and active in the hardware root table. There could be a chance that the new kernel doesn't reclaim/overwrite these entries in which case it could result in zombie DMAs surviving the KHO.
Agreed. I will update this.
Quoted 163 lines
Quoted 163 lines
drivers/iommu/intel/iommu.c +26
-0+ info =dev_iommu_priv_get (&pdev->dev); +if (!info) +continue ; + +if (info->iommu != iommu) +continue ; + +if (dev_iommu_preserved_state (&pdev->dev)) +continue ; + + domain_context_clear(info); + } + +return true; +} +#else +static bool __maybe_clean_unpreserved_context_entries (struct intel_iommu *iommu) +{ +return false; +} +#endif +static struct iommu_domain *intel_iommu_domain_alloc_second_stage (struct device *dev,struct intel_iommu *iommu,u32 flags)@@ -3925,6 +3960,10 @@ 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, + .preserve_device = intel_iommu_preserve_device, + .unpreserve_device = intel_iommu_unpreserve_device, + .preserve = intel_iommu_preserve, + .unpreserve = intel_iommu_unpreserve, };static void quirk_iommu_igfx (struct pci_dev *dev)drivers/iommu/intel/iommu.h +27
-0@@ -557,6 +557,8 @@ struct root_entry {u64 hi; }; +#define ROOT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(struct root_entry)) +/* * low 64 bits: * 0: present @@ -1276,6 +1278,31 @@ 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 device_ser *device_ser); +void intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser); +int intel_iommu_preserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser); +#else +static inline int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) +{ +return -EOPNOTSUPP ; +} + +static inline void intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser) +{ +} + +static inline int intel_iommu_preserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser) +{ +return -EOPNOTSUPP ; +} + +static inline void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_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 +75
-0@@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2025, Google LLC +* Author: Samiullah Khawaja <[email protected]> +*/ + +#define pr_fmt (fmt) "iommu: liveupdate: " fmt + +#include <linux/kexec_handover.h> +#include <linux/liveupdate.h> +#include <linux/iommu-lu.h> +#include <linux/module.h> +#include <linux/pci.h> + +#include "iommu.h" +#include "../iommu-pages.h" + +static void unpreserve_iommu_context (struct intel_iommu *iommu,int end) +{ +struct context_entry *context; +int i; + +if (end <0 ) + end =ROOT_ENTRY_NR ; + +for (i =0 ; i < end; i++) { + context =iommu_context_addr (iommu, i,0 ,0 ); +if (context) +iommu_unpreserve_page (context); + +if (!sm_supported (iommu)) +continue ; + + context =iommu_context_addr (iommu, i,0x80 ,0 ); +if (context) +iommu_unpreserve_page (context); + } +} + +static int preserve_iommu_context (struct intel_iommu *iommu) +{ +struct context_entry *context; +int ret; +int i; + +for (i =0 ; i <ROOT_ENTRY_NR ; i++) { + context =iommu_context_addr (iommu, i,0 ,0 ); +if (context) { + ret =iommu_preserve_page (context); +if (ret) + goto error; + } + +if (!sm_supported (iommu)) +continue ; + + context =iommu_context_addr (iommu, i,0x80 ,0 ); +if (context) { + ret =iommu_preserve_page (context); +if (ret) + goto error_sm; + } + } + +return 0 ; + +error_sm: + context =iommu_context_addr (iommu, i,0 ,0 ); +iommu_unpreserve_page (context); +error: +unpreserve_iommu_context (iommu, i); +return ret; +} +
Quoted 8 lines
drivers/iommu/intel/liveupdate.c line 81
Quoted 6 lines
drivers/iommu/intel/liveupdate.c new file +6
-0+int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) +{ +struct device_domain_info *info =dev_iommu_priv_get (dev); + +if (!dev_is_pci (dev)) +return -EOPNOTSUPP ;Maybe also
WARN_ON_ONCE?
I think WARN should not be used if the driver supports only PCIe device preservation. Do you think returning NOTSUPP is not enough and an error should be logged? I can add a ratelimited error log.
Quoted 15 lines
drivers/iommu/intel/liveupdate.c line 92
Quoted 12 lines
drivers/iommu/intel/liveupdate.c new file +11
-0+ +if (!info) +return -EINVAL ; + + device_ser->domain_iommu_ser.did =domain_id_iommu (info->domain, info->iommu); +return 0 ; +} + +void intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser) +{ +}Nit: We can just not populate the
unpreserve_deviceop here, we don't need an empty function
Agreed.
Quoted 20 lines
drivers/iommu/intel/liveupdate.c line 101
Quoted 10 lines
drivers/iommu/intel/liveupdate.c new file +9
-0+ +int intel_iommu_preserve (struct iommu_device *iommu_dev,struct iommu_ser *ser) +{ +struct intel_iommu *iommu; +int ret; + + iommu = container_of(iommu_dev, struct intel_iommu, iommu); + +spin_lock (&iommu->lock);Minor note: We call this with the
session->mutexacquired which is a sleepable/preemptable context whereasspin_lockdisables preemption and puts us in an atomic context.While
iommu_context_addr()happens to be safe here because it usesGFP_ATOMIC& we pass alloc=0, we are heavily relying on the assumption thatiommu_context_addr()or other helpers will never sleep or attempt aGFP_KERNELallocation under the hood.
Quoted 4 lines
Given how easy it would be for a future refactor to accidentally introduce a sleeping lock inside any of the other helpers, I'd recommend adding an explicit comment here warning that this runs in an atomic context (same for unpreserve below).
I will add a comment about this as you suggested in the later email.
Quoted 11 lines
drivers/iommu/intel/liveupdate.c line 108
Quoted 8 lines
r + ret =
preserve_iommu_context(iommu);drivers/iommu/intel/liveupdate.c new file +6
-0+if (ret) + goto err; + + ret =iommu_preserve_page (iommu->root_entry); +if (ret) { +unpreserve_iommu_context (iommu,-1 );Nit: The same overloading discussion as Vipin pointed out in the other patch.
Agreed. Will update.
Quoted 22 lines
Quoted 22 lines
drivers/iommu/intel/liveupdate.c new file +21
-0+ goto err; + } + + 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; +spin_unlock (&iommu->lock); + +return 0 ; +err: +spin_unlock (&iommu->lock); +return ret; +} + +void intel_iommu_unpreserve (struct iommu_device *iommu_dev,struct iommu_ser *iommu_ser) +{ +struct intel_iommu *iommu; + + iommu = container_of(iommu_dev, struct intel_iommu, iommu); +
Quoted 8 lines
drivers/iommu/intel/liveupdate.c line 134
Quoted 5 lines
drivers/iommu/intel/liveupdate.c new file +5
-0+spin_lock (&iommu->lock); +unpreserve_iommu_context (iommu,-1 ); +iommu_unpreserve_page (iommu->root_entry); +spin_unlock (&iommu->lock); +}Thanks,
Praan
Thanks,
Sami
PATCH 7/14 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 | 26 +++++++++++++++------ drivers/iommu/intel/iommu.h | 7 ++++++ drivers/iommu/intel/liveupdate.c | 40 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-)
drivers/iommu/intel/iommu.c +19 -7
@@ -222,12 +222,12 @@ static void clear_translation_pre_enabled(struct intel_iommu *iommu) iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED ; } -static void init_translation_status (struct intel_iommu *iommu) +static void init_translation_status (struct intel_iommu *iommu,bool restoring) {u32 gsts; gsts =readl (iommu->reg +DMAR_GSTS_REG ); -if (gsts &DMA_GSTS_TES ) +if (!restoring && (gsts &DMA_GSTS_TES )) iommu->flags |=VTD_FLAG_TRANS_PRE_ENABLED ; }
@@ -670,10 +670,16 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,#endif /* iommu handling */ -static int iommu_alloc_root_entry (struct intel_iommu *iommu) +static int iommu_alloc_root_entry (struct intel_iommu *iommu,struct iommu_ser *restored_state) {struct root_entry *root; +if (restored_state) { +intel_iommu_liveupdate_restore_root_table (iommu, restored_state); +__iommu_flush_cache (iommu, iommu->root_entry,ROOT_SIZE ); +return 0 ; + } + root =iommu_alloc_pages_node_sz (iommu->node,GFP_ATOMIC ,SZ_4K );if (!root) { pr_err("Allocating root entry for %s failed\n",
@@ -1614,6 +1620,7 @@ static int copy_translation_tables(struct intel_iommu *iommu)static int __init init_dmars (void ) { +struct iommu_ser *iommu_ser =NULL ;struct dmar_drhd_unit *drhd;struct intel_iommu *iommu;int ret;
@@ -1636,8 +1643,10 @@ static int __init init_dmars(void) intel_pasid_max_id); } + iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +intel_iommu_init_qi (iommu); -init_translation_status (iommu); +init_translation_status (iommu, !!iommu_ser);if (translation_pre_enabled (iommu) && !is_kdump_kernel ()) {iommu_disable_translation (iommu);
@@ -1651,7 +1660,7 @@ static int __init init_dmars(void)* we could share the same root & context tables * among all IOMMU's. Need to Split it later. */ - ret =iommu_alloc_root_entry (iommu); + ret =iommu_alloc_root_entry (iommu, iommu_ser);if (ret) goto free_iommu;
@@ -2110,15 +2119,18 @@ int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg)static int intel_iommu_add (struct dmar_drhd_unit *dmaru) {struct intel_iommu *iommu = dmaru->iommu; +struct iommu_ser *iommu_ser =NULL ;int ret; + iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +/* * Disable translation if already enabled prior to OS handover. */ -if (iommu->gcmd &DMA_GCMD_TE ) +if (!iommu_ser && iommu->gcmd &DMA_GCMD_TE )iommu_disable_translation (iommu); - ret =iommu_alloc_root_entry (iommu); + ret =iommu_alloc_root_entry (iommu, iommu_ser);if (ret) goto out;
drivers/iommu/intel/iommu.h +7 -0
@@ -1283,6 +1283,8 @@ int intel_iommu_preserve_device(struct device *dev, struct device_ser *device_sevoid intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser);int intel_iommu_preserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser);void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser);#else static inline int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) {
@@ -1301,6 +1303,11 @@ static inline int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_static inline void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser) { } + +static inline void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser) +{ +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM
drivers/iommu/intel/liveupdate.c +40 -0
@@ -73,6 +73,46 @@ static int preserve_iommu_context(struct intel_iommu *iommu)return ret; } +static void restore_iommu_context (struct intel_iommu *iommu) +{ +struct context_entry *context; +int i; + +for (i =0 ; i <ROOT_ENTRY_NR ; i++) { + context =iommu_context_addr (iommu, i,0 ,0 ); +if (context) +BUG_ON (!kho_restore_folio (virt_to_phys (context))); + +if (!sm_supported (iommu)) +continue ; + + context =iommu_context_addr (iommu, i,0x80 ,0 ); +if (context) +BUG_ON (!kho_restore_folio (virt_to_phys (context))); + } +} + +static int __restore_used_domain_ids (struct device_ser *ser,void *arg) +{ +int id = ser->domain_iommu_ser.did; +struct intel_iommu *iommu = arg; + +ida_alloc_range (&iommu->domain_ida, id, id,GFP_ATOMIC ); +return 0 ; +} + +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser) +{ +BUG_ON (!kho_restore_folio (iommu_ser->intel.root_table)); + iommu->root_entry =__va (iommu_ser->intel.root_table); + +restore_iommu_context (iommu); +iommu_for_each_preserved_device (__restore_used_domain_ids, iommu); +pr_info ("Restored IOMMU[0x%llx] Root Table at: 0x%llx\n" , + iommu->reg_phys, iommu_ser->intel.root_table); +} +int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) {struct device_domain_info *info =dev_iommu_priv_get (dev);
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 07/14] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
Quoted 6 lines · Samiullah Khawaja
During boot fetch the preserved state of IOMMU unit and if found then restore the state.
- Reuse the
root_tablethat was preserved in the previous kernel.
- Reclaim the domain ids of the preserved domains for each preserved devices so these are not acquired by another domain.
Can this be two patches? One just restoring root table and second one to reclaim device ids.
drivers/iommu/intel/iommu.c static void clear_translation_pre_enabled(struct intel_iommu *iommu) · line 232
Quoted 10 lines
drivers/iommu/intel/iommu.c +2
-2-static void init_translation_status (struct intel_iommu *iommu) +static void init_translation_status (struct intel_iommu *iommu,bool restoring) {u32 gsts; gsts =readl (iommu->reg +DMAR_GSTS_REG ); -if (gsts &DMA_GSTS_TES ) +if (!restoring && (gsts &DMA_GSTS_TES )) iommu->flags |=VTD_FLAG_TRANS_PRE_ENABLED ; }
I think we can just check in the caller, init_dmars(), and not call this function. We are already modifying that function, so no need to make changes here as well. WDYT?
drivers/iommu/intel/iommu.c void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id, · line 673
Quoted 7 lines
drivers/iommu/intel/iommu.c +1
-1@@ -670,10 +670,16 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,#endif /* iommu handling */ -static int iommu_alloc_root_entry (struct intel_iommu *iommu) +static int iommu_alloc_root_entry (struct intel_iommu *iommu,struct iommu_ser *restored_state)
Nit: Since we are using iommu_ser at other places, I will recommend to keep it same variable name here as well.
drivers/iommu/intel/iommu.c static int __init init_dmars(void) · line 1649
Quoted 9 lines
drivers/iommu/intel/iommu.c +3
-1@@ -1636,8 +1643,10 @@ static int __init init_dmars(void) intel_pasid_max_id); } + iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +intel_iommu_init_qi (iommu); -init_translation_status (iommu); +init_translation_status (iommu, !!iommu_ser);
Just put 'if' here to avoid changes in init_translation_status().
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context(struct intel_iommu *iommu) · line 95
Quoted 1 line
drivers/iommu/intel/liveupdate.c +1
-0+static int __restore_used_domain_ids (struct device_ser *ser,void *arg)
Nit: Just curious, why two __?
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context(struct intel_iommu *iommu) · line 100
Quoted 5 lines
drivers/iommu/intel/liveupdate.c +5
-0+{ +int id = ser->domain_iommu_ser.did; +struct intel_iommu *iommu = arg; + +ida_alloc_range (&iommu->domain_ida, id, id,GFP_ATOMIC );
Should we check if allocation succeeded or not?
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context(struct intel_iommu *iommu) · line 113
Quoted 13 lines
drivers/iommu/intel/liveupdate.c +13
-0+return 0 ; +} + +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser) +{ +BUG_ON (!kho_restore_folio (iommu_ser->intel.root_table)); + iommu->root_entry =__va (iommu_ser->intel.root_table); + +restore_iommu_context (iommu); +iommu_for_each_preserved_device (__restore_used_domain_ids, iommu); +pr_info ("Restored IOMMU[0x%llx] Root Table at: 0x%llx\n" , + iommu->reg_phys, iommu_ser->intel.root_table);
Should raw pointer values be printed like this?
Re: [PATCH 07/14] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
Hi Vipin,
Quoted 10 lines · Vipin Sharma
Quoted 6 lines · Samiullah Khawaja
During boot fetch the preserved state of IOMMU unit and if found then restore the state.
- Reuse the
root_tablethat was preserved in the previous kernel.
- Reclaim the domain ids of the preserved domains for each preserved devices so these are not acquired by another domain.Can this be two patches? One just restoring root table and second one to reclaim device ids.
This patch is restoring the state of the preserved IOMMU. The domain ids are part of the iommu state and used in the context entries that are being restored in this patch, that is why I kept these in one patch. I will reword the subject of this patch and add a note on this in commit message.
Quoted 6 lines
Quoted 6 lines
drivers/iommu/intel/iommu.c +1
-1-static void init_translation_status (struct intel_iommu *iommu) +static void init_translation_status (struct intel_iommu *iommu,bool restoring) {u32 gsts;
Quoted 9 lines
drivers/iommu/intel/iommu.c static void clear_translation_pre_enabled(struct intel_iommu *iommu) · line 232
Quoted 5 lines
drivers/iommu/intel/iommu.c +1
-1gsts =readl (iommu->reg +DMAR_GSTS_REG ); -if (gsts &DMA_GSTS_TES ) +if (!restoring && (gsts &DMA_GSTS_TES )) iommu->flags |=VTD_FLAG_TRANS_PRE_ENABLED ; }I think we can just check in the caller,
init_dmars(), and not call this function. We are already modifying that function, so no need to make changes here as well. WDYT?
Yes, I think we can move the if outside.
Quoted 11 lines
drivers/iommu/intel/iommu.c void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id, · line 673
Quoted 8 lines
drivers/iommu/intel/iommu.c +1
-1@@ -670,10 +670,16 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,#endif /* iommu handling */ -static int iommu_alloc_root_entry (struct intel_iommu *iommu) +static int iommu_alloc_root_entry (struct intel_iommu *iommu,struct iommu_ser *restored_state)Nit: Since we are using
iommu_serat other places, I will recommend to keep it same variable name here as well.
Hmm... that is a good point. I think I will update the other places and change it to restored_state/preserved_state to better reflect the context.
Quoted 5 lines
Quoted 5 lines
drivers/iommu/intel/iommu.c +0
-0@@ -1636,8 +1643,10 @@ static int __init init_dmars(void) intel_pasid_max_id); }
Quoted 7 lines
drivers/iommu/intel/iommu.c static int __init init_dmars(void) · line 1649
Quoted 5 lines
drivers/iommu/intel/iommu.c +3
-1+ iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +intel_iommu_init_qi (iommu); -init_translation_status (iommu); +init_translation_status (iommu, !!iommu_ser);Just put 'if' here to avoid changes in
init_translation_status().
Agreed.
Quoted 4 lines
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context(struct intel_iommu *iommu) · line 95
Quoted 2 lines
drivers/iommu/intel/liveupdate.c +1
-0+static int __restore_used_domain_ids (struct device_ser *ser,void *arg)Nit: Just curious, why two __?
I will remove these.
Quoted 8 lines
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context(struct intel_iommu *iommu) · line 100
Quoted 6 lines
drivers/iommu/intel/liveupdate.c +5
-0+{ +int id = ser->domain_iommu_ser.did; +struct intel_iommu *iommu = arg; + +ida_alloc_range (&iommu->domain_ida, id, id,GFP_ATOMIC );Should we check if allocation succeeded or not?
Good point. This should not fail as we are reserving these ids during init. But I think I will add a WARN here.
Quoted 16 lines
Quoted 4 lines
drivers/iommu/intel/liveupdate.c +3
-0+return 0 ; +} +drivers/iommu/intel/liveupdate.c static int preserve_iommu_context(struct intel_iommu *iommu) · line 113
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +10
-0+void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser) +{ +BUG_ON (!kho_restore_folio (iommu_ser->intel.root_table)); + iommu->root_entry =__va (iommu_ser->intel.root_table); + +restore_iommu_context (iommu); +iommu_for_each_preserved_device (__restore_used_domain_ids, iommu); +pr_info ("Restored IOMMU[0x%llx] Root Table at: 0x%llx\n" , + iommu->reg_phys, iommu_ser->intel.root_table);Should raw pointer values be printed like this?
I will remove this.
Quoted 1 line
Re: [PATCH 07/14] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
In reply to Samiullah KhawajaQuoted 37 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 | 26 +++++++++++++++------ drivers/iommu/intel/iommu.h | 7 ++++++ drivers/iommu/intel/liveupdate.c | 40 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-)drivers/iommu/intel/iommu.c +2
-2@@ -222,12 +222,12 @@ static void clear_translation_pre_enabled(struct intel_iommu *iommu) iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED ; } -static void init_translation_status (struct intel_iommu *iommu) +static void init_translation_status (struct intel_iommu *iommu,bool restoring) {u32 gsts; gsts =readl (iommu->reg +DMAR_GSTS_REG ); -if (gsts &DMA_GSTS_TES ) +if (!restoring && (gsts &DMA_GSTS_TES )) iommu->flags |=VTD_FLAG_TRANS_PRE_ENABLED ; }@@ -670,10 +670,16 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,#endif /* iommu handling */
drivers/iommu/intel/iommu.c void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id, · line 681
Quoted 10 lines
drivers/iommu/intel/iommu.c +6
-1-static int iommu_alloc_root_entry (struct intel_iommu *iommu) +static int iommu_alloc_root_entry (struct intel_iommu *iommu,struct iommu_ser *restored_state) {struct root_entry *root; +if (restored_state) { +intel_iommu_liveupdate_restore_root_table (iommu, restored_state); +__iommu_flush_cache (iommu, iommu->root_entry,ROOT_SIZE ); +return 0 ; + }
Instead of putting this inside the allocator, shouldn't init_dmars and intel_iommu_add check for iommu_ser and call intel_iommu_liveupdate_restore_root_table() directly, bypassing the allocation entirely? This looks like it could be a stand-alone function which has nothing to do with allocation.
Quoted 34 lines
drivers/iommu/intel/iommu.c +6
-2+ root =iommu_alloc_pages_node_sz (iommu->node,GFP_ATOMIC ,SZ_4K );if (!root) { pr_err("Allocating root entry for %s failed\n",@@ -1614,6 +1620,7 @@ static int copy_translation_tables(struct intel_iommu *iommu)static int __init init_dmars (void ) { +struct iommu_ser *iommu_ser =NULL ;struct dmar_drhd_unit *drhd;struct intel_iommu *iommu;int ret;@@ -1636,8 +1643,10 @@ static int __init init_dmars(void) intel_pasid_max_id); } + iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +intel_iommu_init_qi (iommu); -init_translation_status (iommu); +init_translation_status (iommu, !!iommu_ser);if (translation_pre_enabled (iommu) && !is_kdump_kernel ()) {iommu_disable_translation (iommu);@@ -1651,7 +1660,7 @@ static int __init init_dmars(void)* we could share the same root & context tables * among all IOMMU's. Need to Split it later. */ - ret =iommu_alloc_root_entry (iommu); + ret =iommu_alloc_root_entry (iommu, iommu_ser);if (ret) goto free_iommu;
drivers/iommu/intel/iommu.c int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg) · line 2124
Quoted 6 lines
drivers/iommu/intel/iommu.c +1
-0@@ -2110,15 +2119,18 @@ int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg)static int intel_iommu_add (struct dmar_drhd_unit *dmaru) {struct intel_iommu *iommu = dmaru->iommu; +struct iommu_ser *iommu_ser =NULL ;int ret;
Nit: Add: /* Fetch the preserved context using MMIO base as a token */ ?
drivers/iommu/intel/iommu.c int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg) · line 2133
Quoted 11 lines
drivers/iommu/intel/iommu.c +4
-2+ iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +/* * Disable translation if already enabled prior to OS handover. */ -if (iommu->gcmd &DMA_GCMD_TE ) +if (!iommu_ser && iommu->gcmd &DMA_GCMD_TE )iommu_disable_translation (iommu); - ret =iommu_alloc_root_entry (iommu); + ret =iommu_alloc_root_entry (iommu, iommu_ser);
I understand that iommu_get_preserved_data() will eventually return NULL after the flb_finish op has executed (based on the LUO IOCTLs dropping the incoming state), but I'm sensing a potential UAF/double-restore issue here that could happen during the boot window.
I believe we could restore the same context multiple times? I see intel_iommu_add() is called from both dmar_device_add() and dmar_device_remove() paths, and the ACPI probe has the following sequence [1]:
static int acpi_pci_root_add(struct acpi_device *device, ...)
{
// ...
if (hotadd && dmar_device_add(handle)) {
result = -ENXIO;
goto end;
}
// ...
root->bus = pci_acpi_scan_root(root);
if (!root->bus) {
// ...
result = -ENODEV;
goto remove_dmar;
}
// ...remove_dmar:
if (hotadd) dmar_device_remove(handle); end: return result; }
If we successfully restored a domain during dmar_device_add(), but the ACPI probe fails later (e.g., pci_acpi_scan_root fails), we jump to remove_dmar. This tears down the DMAR unit, it unwinds via dmar_device_remove() which eventually calls dmar_iommu_hotplug(false) where we:
disable_dmar_iommu(iommu); free_dmar_iommu(iommu);
At this point, the root table folios are freed back to the allocator.
However, if a re-scan is then triggered before the FLB drops the incoming state, we would call:
dmar_device_add() -> intel_iommu_add() -> iommu_alloc_root_entry() again
Because the KHO state wasn't marked as deleted/consumed, iommu_get_preserved_data() will hand us the exact same iommu_ser pointer?
In which case, we'd call kho_restore_folio(iommu_ser->intel.root_table) on a physical page that might have already been reallocated?
Shouldn't the restored state be explicitly marked as consumed (obj.deleted = 1), and shouldn't the driver properly unpreserve/clean up the KHO tracking during the free_dmar_iommu() teardown path?
Quoted 50 lines
drivers/iommu/intel/iommu.c +0
-0if (ret) goto out;drivers/iommu/intel/iommu.h +7
-0@@ -1283,6 +1283,8 @@ int intel_iommu_preserve_device(struct device *dev, struct device_ser *device_sevoid intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser);int intel_iommu_preserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser);void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser);#else static inline int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) {@@ -1301,6 +1303,11 @@ static inline int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_static inline void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser) { } + +static inline void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser) +{ +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM drivers/iommu/intel/liveupdate.c +14
-0@@ -73,6 +73,46 @@ static int preserve_iommu_context(struct intel_iommu *iommu)return ret; } +static void restore_iommu_context (struct intel_iommu *iommu) +{ +struct context_entry *context; +int i; + +for (i =0 ; i <ROOT_ENTRY_NR ; i++) { + context =iommu_context_addr (iommu, i,0 ,0 ); +if (context) +BUG_ON (!kho_restore_folio (virt_to_phys (context))); + +if (!sm_supported (iommu)) +continue ; + + context =iommu_context_addr (iommu, i,0x80 ,0 );
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context(struct intel_iommu *iommu) · line 99
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +10
-0+if (context) +BUG_ON (!kho_restore_folio (virt_to_phys (context))); + } +} + +static int __restore_used_domain_ids (struct device_ser *ser,void *arg) +{ +int id = ser->domain_iommu_ser.did; +struct intel_iommu *iommu = arg; +
Shouldn't we check if the did actually belongs to the iommu instance? iommu_for_each_preserved_device() iterates over all preserved devices in the system. However, here (__restore_used_domain_ids) we allocate the device's did in the current iommu->domain_ida without checking if that device actually belongs to the current IOMMU?
On multi-IOMMU systems, this will cause every IOMMU's IDA to be cross-polluted with the domain IDs of devices attached to other IOMMUs. We must verify the device belongs to this specific IOMMU first, maybe:
if (ser->domain_iommu_ser.iommu_phys != iommu->reg_phys)
return 0;
Quoted 9 lines
drivers/iommu/intel/liveupdate.c +9
-0+ida_alloc_range (&iommu->domain_ida, id, id,GFP_ATOMIC ); +return 0 ; +} + +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser) +{ +BUG_ON (!kho_restore_folio (iommu_ser->intel.root_table)); + iommu->root_entry =__va (iommu_ser->intel.root_table);
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context(struct intel_iommu *iommu) · line 118
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +7
-0+ +restore_iommu_context (iommu); +iommu_for_each_preserved_device (__restore_used_domain_ids, iommu); +pr_info ("Restored IOMMU[0x%llx] Root Table at: 0x%llx\n" , + iommu->reg_phys, iommu_ser->intel.root_table); +} +int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) {struct device_domain_info *info =dev_iommu_priv_get (dev);
Thanks,
Praan
[1] https://elixir.bootlin.com/linux/v7.0-rc4/source/drivers/acpi/pci_root.c#L728
Re: [PATCH 07/14] iommu/vt-d: Restore IOMMU state and reclaimed domain ids
Quoted 43 lines · Pranjal Shrivastava
Quoted 42 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 | 26 +++++++++++++++------ drivers/iommu/intel/iommu.h | 7 ++++++ drivers/iommu/intel/liveupdate.c | 40 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-)drivers/iommu/intel/iommu.c +3
-3@@ -222,12 +222,12 @@ static void clear_translation_pre_enabled(struct intel_iommu *iommu) iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED ; } -static void init_translation_status (struct intel_iommu *iommu) +static void init_translation_status (struct intel_iommu *iommu,bool restoring) {u32 gsts; gsts =readl (iommu->reg +DMAR_GSTS_REG ); -if (gsts &DMA_GSTS_TES ) +if (!restoring && (gsts &DMA_GSTS_TES )) iommu->flags |=VTD_FLAG_TRANS_PRE_ENABLED ; }@@ -670,10 +670,16 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id,#endif /* iommu handling */ -static int iommu_alloc_root_entry (struct intel_iommu *iommu) +static int iommu_alloc_root_entry (struct intel_iommu *iommu,struct iommu_ser *restored_state) {struct root_entry *root;
Quoted 11 lines
drivers/iommu/intel/iommu.c void dmar_fault_dump_ptes(struct intel_iommu *iommu, u16 source_id, · line 681
Quoted 5 lines
drivers/iommu/intel/iommu.c +5
-0+if (restored_state) { +intel_iommu_liveupdate_restore_root_table (iommu, restored_state); +__iommu_flush_cache (iommu, iommu->root_entry,ROOT_SIZE ); +return 0 ; + }Instead of putting this inside the allocator, shouldn't
init_dmarsandintel_iommu_addcheck foriommu_serand callintel_iommu_liveupdate_restore_root_table()directly, bypassing the allocation entirely? This looks like it could be a stand-alone function which has nothing to do with allocation.
Agreed. Will move the check out into the caller.
Quoted 34 lines
Quoted 34 lines
drivers/iommu/intel/iommu.c +6
-2+ root =iommu_alloc_pages_node_sz (iommu->node,GFP_ATOMIC ,SZ_4K );if (!root) { pr_err("Allocating root entry for %s failed\n",@@ -1614,6 +1620,7 @@ static int copy_translation_tables(struct intel_iommu *iommu)static int __init init_dmars (void ) { +struct iommu_ser *iommu_ser =NULL ;struct dmar_drhd_unit *drhd;struct intel_iommu *iommu;int ret;@@ -1636,8 +1643,10 @@ static int __init init_dmars(void) intel_pasid_max_id); } + iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +intel_iommu_init_qi (iommu); -init_translation_status (iommu); +init_translation_status (iommu, !!iommu_ser);if (translation_pre_enabled (iommu) && !is_kdump_kernel ()) {iommu_disable_translation (iommu);@@ -1651,7 +1660,7 @@ static int __init init_dmars(void)* we could share the same root & context tables * among all IOMMU's. Need to Split it later. */ - ret =iommu_alloc_root_entry (iommu); + ret =iommu_alloc_root_entry (iommu, iommu_ser);if (ret) goto free_iommu;
Quoted 9 lines
drivers/iommu/intel/iommu.c int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg) · line 2124
Quoted 7 lines
drivers/iommu/intel/iommu.c +1
-0@@ -2110,15 +2119,18 @@ int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg)static int intel_iommu_add (struct dmar_drhd_unit *dmaru) {struct intel_iommu *iommu = dmaru->iommu; +struct iommu_ser *iommu_ser =NULL ;int ret;Nit: Add: /* Fetch the preserved context using MMIO base as a token */ ?
Will add.
Quoted 66 lines
drivers/iommu/intel/iommu.c int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg) · line 2133
Quoted 12 lines
drivers/iommu/intel/iommu.c +4
-2+ iommu_ser =iommu_get_preserved_data (iommu->reg_phys,IOMMU_INTEL ); +/* * Disable translation if already enabled prior to OS handover. */ -if (iommu->gcmd &DMA_GCMD_TE ) +if (!iommu_ser && iommu->gcmd &DMA_GCMD_TE )iommu_disable_translation (iommu); - ret =iommu_alloc_root_entry (iommu); + ret =iommu_alloc_root_entry (iommu, iommu_ser);I understand that
iommu_get_preserved_data()will eventually return NULL after theflb_finishop has executed (based on the LUO IOCTLs dropping the incoming state), but I'm sensing a potential UAF/double-restore issue here that could happen during the boot window.I believe we could restore the same context multiple times? I see
intel_iommu_add()is called from bothdmar_device_add()anddmar_device_remove()paths, and the ACPI probe has the following sequence [1]:static int
acpi_pci_root_add(struct acpi_device *device, ...){ // ... if (hotadd && dmar_device_add(handle)) { result = -ENXIO; goto end; } // ... root->bus = pci_acpi_scan_root(root); if (!root->bus) { // ... result = -ENODEV; goto remove_dmar; } // ...
remove_dmar:if (hotadd) dmar_device_remove(handle); end: return result; }If we successfully restored a domain during
dmar_device_add(), but the ACPI probe fails later (e.g.,pci_acpi_scan_rootfails), we jump toremove_dmar. This tears down the DMAR unit, it unwinds viadmar_device_remove()which eventually callsdmar_iommu_hotplug(false)where we:disable_dmar_iommu(iommu); free_dmar_iommu(iommu);At this point, the root table folios are freed back to the allocator.
However, if a re-scan is then triggered before the FLB drops the incoming state, we would call:
dmar_device_add()->intel_iommu_add()->iommu_alloc_root_entry()againBecause the KHO state wasn't marked as deleted/consumed,
iommu_get_preserved_data()will hand us the exact sameiommu_serpointer?
Quoted 6 lines
In which case, we'd call
kho_restore_folio(iommu_ser->intel.root_table)on a physical page that might have already been reallocated?Shouldn't the restored state be explicitly marked as consumed (obj.deleted = 1), and shouldn't the driver properly unpreserve/clean up the KHO tracking during the
free_dmar_iommu()teardown path?
Thats a good point.
I think on disable/free the restored state should not be freed abruptly. The iommu should be able to reuse the same state. We just need to mark it as consumed/restored. I will rework the disable/free code paths to handle this.
Quoted 59 lines
Quoted 59 lines
drivers/iommu/intel/iommu.c +0
-0if (ret) goto out;drivers/iommu/intel/iommu.h +7
-0@@ -1283,6 +1283,8 @@ int intel_iommu_preserve_device(struct device *dev, struct device_ser *device_sevoid intel_iommu_unpreserve_device (struct device *dev,struct device_ser *device_ser);int intel_iommu_preserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser);void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser); +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser);#else static inline int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) {@@ -1301,6 +1303,11 @@ static inline int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_static inline void intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser) { } + +static inline void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser) +{ +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM drivers/iommu/intel/liveupdate.c +22
-0@@ -73,6 +73,46 @@ static int preserve_iommu_context(struct intel_iommu *iommu)return ret; } +static void restore_iommu_context (struct intel_iommu *iommu) +{ +struct context_entry *context; +int i; + +for (i =0 ; i <ROOT_ENTRY_NR ; i++) { + context =iommu_context_addr (iommu, i,0 ,0 ); +if (context) +BUG_ON (!kho_restore_folio (virt_to_phys (context))); + +if (!sm_supported (iommu)) +continue ; + + context =iommu_context_addr (iommu, i,0x80 ,0 ); +if (context) +BUG_ON (!kho_restore_folio (virt_to_phys (context))); + } +} + +static int __restore_used_domain_ids (struct device_ser *ser,void *arg) +{ +int id = ser->domain_iommu_ser.did;
Quoted 8 lines
drivers/iommu/intel/liveupdate.c static int preserve_iommu_context(struct intel_iommu *iommu) · line 99
Quoted 2 lines
drivers/iommu/intel/liveupdate.c +2
-0+struct intel_iommu *iommu = arg; +Shouldn't we check if the did actually belongs to the iommu instance?
iommu_for_each_preserved_device()iterates over all preserved devices in the system. However, here (__restore_used_domain_ids) we allocate the device's did in the currentiommu->domain_idawithout checking if that device actually belongs to the current IOMMU?
Yes, this needs to be checked. I will add this.
Quoted 31 lines
On multi-IOMMU systems, this will cause every IOMMU's IDA to be cross-polluted with the domain IDs of devices attached to other IOMMUs. We must verify the device belongs to this specific IOMMU first, maybe:
if (
ser->domain_iommu_ser.iommu_phys!=iommu->reg_phys)return 0;Quoted 9 lines
drivers/iommu/intel/liveupdate.c +9
-0+ida_alloc_range (&iommu->domain_ida, id, id,GFP_ATOMIC ); +return 0 ; +} + +void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu, +struct iommu_ser *iommu_ser) +{ +BUG_ON (!kho_restore_folio (iommu_ser->intel.root_table)); + iommu->root_entry =__va (iommu_ser->intel.root_table);drivers/iommu/intel/liveupdate.c static int preserve_iommu_context(struct intel_iommu *iommu) · line 118
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +7
-0+ +restore_iommu_context (iommu); +iommu_for_each_preserved_device (__restore_used_domain_ids, iommu); +pr_info ("Restored IOMMU[0x%llx] Root Table at: 0x%llx\n" , + iommu->reg_phys, iommu_ser->intel.root_table); +} +int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) {struct device_domain_info *info =dev_iommu_priv_get (dev);Thanks,
Praan
Thanks,
Sami
PATCH 8/14 iommu: Restore and reattach preserved domains to devices
In reply to Samiullah KhawajaRestore the preserved domains by restoring the page tables using restore IOMMU domain op. Reattach the preserved domain to the device during default domain setup. While attaching, reuse the domain ID that was used in the previous kernel. The context entry setup is not needed as that is preserved during liveupdate.
--- drivers/iommu/intel/iommu.c | 40 ++++++++++++++++++------------ drivers/iommu/intel/iommu.h | 3 ++- drivers/iommu/intel/nested.c | 2 +- drivers/iommu/iommu.c | 47 ++++++++++++++++++++++++++++++++++-- drivers/iommu/liveupdate.c | 31 ++++++++++++++++++++++++ include/linux/iommu-lu.h | 8 ++++++ 6 files changed, 112 insertions(+), 19 deletions(-)
drivers/iommu/intel/iommu.c +25 -15
@@ -1029,7 +1029,8 @@ static bool first_level_by_default(struct intel_iommu *iommu)return true; } -int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) +int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu, +int restore_did) {struct iommu_domain_info *info, *curr;int num, ret = -ENOSPC ;
@@ -1049,8 +1050,11 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)return 0 ; } - num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , -cap_ndoms (iommu->cap) -1 ,GFP_KERNEL ); +if (restore_did >=0 ) + num = restore_did; +else + num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , +cap_ndoms (iommu->cap) -1 ,GFP_KERNEL );if (num <0 ) {pr_err ("%s: No free domain ids\n" , iommu->name); goto err_unlock;
@@ -1321,10 +1325,14 @@ 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 device_ser *device_ser =NULL ;unsigned long flags;int ret; - ret =domain_attach_iommu (domain, iommu); + device_ser =dev_iommu_restored_state (dev); + + ret =domain_attach_iommu (domain, iommu, +dev_iommu_restore_did (dev, &domain->domain));if (ret)return ret;
@@ -1337,16 +1345,18 @@ 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 ; + }if (ret) goto out_block_translation;
@@ -3630,7 +3640,7 @@ domain_add_dev_pasid(struct iommu_domain *domain,if (!dev_pasid)return ERR_PTR (-ENOMEM ); - ret =domain_attach_iommu (dmar_domain, iommu); + ret =domain_attach_iommu (dmar_domain, iommu,-1 );if (ret) goto out_free;
drivers/iommu/intel/iommu.h +2 -1
@@ -1174,7 +1174,8 @@ void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,*/ #define QI_OPT_WAIT_DRAIN BIT(0) -int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu); +int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu, +int restore_did);void domain_detach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu);void device_block_translation (struct device *dev);int paging_domain_compatible (struct iommu_domain *domain,struct device *dev);
drivers/iommu/intel/nested.c +1 -1
@@ -40,7 +40,7 @@ static int intel_nested_attach_dev(struct iommu_domain *domain,return ret; } - ret =domain_attach_iommu (dmar_domain, iommu); + ret =domain_attach_iommu (dmar_domain, iommu,-1 );if (ret) {dev_err_ratelimited (dev,"Failed to attach domain to iommu\n" );return ret;
drivers/iommu/iommu.c +45 -2
90 lines not on this page · raw message
drivers/iommu/liveupdate.c +31 -0
@@ -501,3 +501,34 @@ void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev)iommu_unpreserve_locked (iommu->iommu_dev); } + +struct iommu_domain *iommu_restore_domain (struct device *dev,struct device_ser *ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +struct iommu_domain *domain; +int ret; + + domain_ser =__va (ser->domain_iommu_ser.domain_phys); + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret) +return ERR_PTR (ret); + +guard (mutex)(&flb_obj->lock); +if (domain_ser->restored_domain) +return domain_ser->restored_domain; + + domain_ser->obj.incoming = true; + domain =iommu_paging_domain_alloc (dev); +if (IS_ERR (domain)) +return domain; + + ret = domain->ops->restore (domain, domain_ser); +if (ret) +return ERR_PTR (ret); + + domain->preserved_state = domain_ser; + domain_ser->restored_domain = domain; +return domain; +}
include/linux/iommu-lu.h +8 -0
@@ -65,6 +65,8 @@ static inline int dev_iommu_restore_did(struct device *dev, struct iommu_domainreturn -1 ; } +struct iommu_domain *iommu_restore_domain (struct device *dev, +struct device_ser *ser);int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg);struct device_ser *iommu_get_device_preserved_data (struct device *dev);
@@ -95,6 +97,12 @@ static inline void *iommu_domain_restored_state(struct iommu_domain *domain)return NULL ; } +static inline struct iommu_domain *iommu_restore_domain (struct device *dev, +struct device_ser *ser) +{ +return NULL ; +} +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) {return -EOPNOTSUPP ;
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 08/14] iommu: Restore and reattach preserved domains to devices
Quoted 249 lines · Samiullah Khawaja
Restore the preserved domains by restoring the page tables using restore IOMMU domain op. Reattach the preserved domain to the device during default domain setup. While attaching, reuse the domain ID that was used in the previous kernel. The context entry setup is not needed as that is preserved during liveupdate.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 40 ++++++++++++++++++------------ drivers/iommu/intel/iommu.h | 3 ++- drivers/iommu/intel/nested.c | 2 +- drivers/iommu/iommu.c | 47 ++++++++++++++++++++++++++++++++++-- drivers/iommu/liveupdate.c | 31 ++++++++++++++++++++++++ include/linux/iommu-lu.h | 8 ++++++ 6 files changed, 112 insertions(+), 19 deletions(-)drivers/iommu/intel/iommu.c +25
-15@@ -1029,7 +1029,8 @@ static bool first_level_by_default(struct intel_iommu *iommu)return true; } -int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) +int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu, +int restore_did) {struct iommu_domain_info *info, *curr;int num, ret = -ENOSPC ;@@ -1049,8 +1050,11 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)return 0 ; } - num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , -cap_ndoms (iommu->cap) -1 ,GFP_KERNEL ); +if (restore_did >=0 ) + num = restore_did; +else + num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , +cap_ndoms (iommu->cap) -1 ,GFP_KERNEL );if (num <0 ) {pr_err ("%s: No free domain ids\n" , iommu->name); goto err_unlock;@@ -1321,10 +1325,14 @@ 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 device_ser *device_ser =NULL ;unsigned long flags;int ret; - ret =domain_attach_iommu (domain, iommu); + device_ser =dev_iommu_restored_state (dev); + + ret =domain_attach_iommu (domain, iommu, +dev_iommu_restore_did (dev, &domain->domain));if (ret)return ret;@@ -1337,16 +1345,18 @@ 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 ; + }if (ret) goto out_block_translation;@@ -3630,7 +3640,7 @@ domain_add_dev_pasid(struct iommu_domain *domain,if (!dev_pasid)return ERR_PTR (-ENOMEM ); - ret =domain_attach_iommu (dmar_domain, iommu); + ret =domain_attach_iommu (dmar_domain, iommu,-1 );if (ret) goto out_free;drivers/iommu/intel/iommu.h +2
-1@@ -1174,7 +1174,8 @@ void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,*/ #define QI_OPT_WAIT_DRAIN BIT(0) -int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu); +int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu, +int restore_did);void domain_detach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu);void device_block_translation (struct device *dev);int paging_domain_compatible (struct iommu_domain *domain,struct device *dev);drivers/iommu/intel/nested.c +1
-1@@ -40,7 +40,7 @@ static int intel_nested_attach_dev(struct iommu_domain *domain,return ret; } - ret =domain_attach_iommu (dmar_domain, iommu); + ret =domain_attach_iommu (dmar_domain, iommu,-1 );if (ret) {dev_err_ratelimited (dev,"Failed to attach domain to iommu\n" );return ret;drivers/iommu/iommu.c +45
-2@@ -18,6 +18,7 @@#include <linux/errno.h> #include <linux/host1x_context_bus.h> #include <linux/iommu.h> +#include <linux/iommu-lu.h> #include <linux/iommufd.h> #include <linux/idr.h> #include <linux/err.h> @@ -489,6 +490,10 @@ 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); +#endif + iommu_dev = ops->probe_device (dev);if (IS_ERR (iommu_dev)) { ret =PTR_ERR (iommu_dev);@@ -2149,6 +2154,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 ;@@ -3061,6 +3073,34 @@ int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids) }EXPORT_SYMBOL_GPL (iommu_fwspec_add_ids); +static struct iommu_domain *__iommu_group_maybe_restore_domain (struct iommu_group *group) +{ +struct device_ser *device_ser; +struct iommu_domain *domain; +struct device *dev; + + 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); +if (WARN_ON (IS_ERR (domain))) +return NULL ; + +/* +* The group is owned by the entity (a preserved iommufd) that provided +* this token in the previous kernel. It will be used to reclaim it +* later. +*/ + group->owner = (void *)device_ser->token; + group->owner_cnt =1 ; +return domain; +} +/** * iommu_setup_default_domain - Set the default_domain for the group * @group: Group to change @@ -3075,8 +3115,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;@@ -3120,6 +3160,9 @@ 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) { + restored_domain =__iommu_group_maybe_restore_domain (group); +if (!restored_domain) + 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 @@ -3127,7 +3170,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 +15
-0@@ -501,3 +501,34 @@ void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev)iommu_unpreserve_locked (iommu->iommu_dev); } + +struct iommu_domain *iommu_restore_domain (struct device *dev,struct device_ser *ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +struct iommu_domain *domain; +int ret; + + domain_ser =__va (ser->domain_iommu_ser.domain_phys); + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret) +return ERR_PTR (ret); + +guard (mutex)(&flb_obj->lock);
drivers/iommu/liveupdate.c void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev) · line 528
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+if (domain_ser->restored_domain) +return domain_ser->restored_domain; + + domain_ser->obj.incoming = true; + domain =iommu_paging_domain_alloc (dev); +if (IS_ERR (domain)) +return domain; + + ret = domain->ops->restore (domain, domain_ser); +if (ret)
'domain' will leak here.
Quoted 35 lines
drivers/iommu/liveupdate.c +6
-0+return ERR_PTR (ret); + + domain->preserved_state = domain_ser; + domain_ser->restored_domain = domain; +return domain; +}include/linux/iommu-lu.h +8
-0@@ -65,6 +65,8 @@ static inline int dev_iommu_restore_did(struct device *dev, struct iommu_domainreturn -1 ; } +struct iommu_domain *iommu_restore_domain (struct device *dev, +struct device_ser *ser);int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg);struct device_ser *iommu_get_device_preserved_data (struct device *dev);@@ -95,6 +97,12 @@ static inline void *iommu_domain_restored_state(struct iommu_domain *domain)return NULL ; } +static inline struct iommu_domain *iommu_restore_domain (struct device *dev, +struct device_ser *ser) +{ +return NULL ; +} +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) {return -EOPNOTSUPP ;Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 08/14] iommu: Restore and reattach preserved domains to devices
Quoted 254 lines · Ankit Soni
Quoted 253 lines · Samiullah Khawaja
Restore the preserved domains by restoring the page tables using restore IOMMU domain op. Reattach the preserved domain to the device during default domain setup. While attaching, reuse the domain ID that was used in the previous kernel. The context entry setup is not needed as that is preserved during liveupdate.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 40 ++++++++++++++++++------------ drivers/iommu/intel/iommu.h | 3 ++- drivers/iommu/intel/nested.c | 2 +- drivers/iommu/iommu.c | 47 ++++++++++++++++++++++++++++++++++-- drivers/iommu/liveupdate.c | 31 ++++++++++++++++++++++++ include/linux/iommu-lu.h | 8 ++++++ 6 files changed, 112 insertions(+), 19 deletions(-)drivers/iommu/intel/iommu.c +25
-15@@ -1029,7 +1029,8 @@ static bool first_level_by_default(struct intel_iommu *iommu)return true; } -int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) +int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu, +int restore_did) {struct iommu_domain_info *info, *curr;int num, ret = -ENOSPC ;@@ -1049,8 +1050,11 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)return 0 ; } - num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , -cap_ndoms (iommu->cap) -1 ,GFP_KERNEL ); +if (restore_did >=0 ) + num = restore_did; +else + num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , +cap_ndoms (iommu->cap) -1 ,GFP_KERNEL );if (num <0 ) {pr_err ("%s: No free domain ids\n" , iommu->name); goto err_unlock;@@ -1321,10 +1325,14 @@ 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 device_ser *device_ser =NULL ;unsigned long flags;int ret; - ret =domain_attach_iommu (domain, iommu); + device_ser =dev_iommu_restored_state (dev); + + ret =domain_attach_iommu (domain, iommu, +dev_iommu_restore_did (dev, &domain->domain));if (ret)return ret;@@ -1337,16 +1345,18 @@ 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 ; + }if (ret) goto out_block_translation;@@ -3630,7 +3640,7 @@ domain_add_dev_pasid(struct iommu_domain *domain,if (!dev_pasid)return ERR_PTR (-ENOMEM ); - ret =domain_attach_iommu (dmar_domain, iommu); + ret =domain_attach_iommu (dmar_domain, iommu,-1 );if (ret) goto out_free;drivers/iommu/intel/iommu.h +2
-1@@ -1174,7 +1174,8 @@ void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,*/ #define QI_OPT_WAIT_DRAIN BIT(0) -int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu); +int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu, +int restore_did);void domain_detach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu);void device_block_translation (struct device *dev);int paging_domain_compatible (struct iommu_domain *domain,struct device *dev);drivers/iommu/intel/nested.c +1
-1@@ -40,7 +40,7 @@ static int intel_nested_attach_dev(struct iommu_domain *domain,return ret; } - ret =domain_attach_iommu (dmar_domain, iommu); + ret =domain_attach_iommu (dmar_domain, iommu,-1 );if (ret) {dev_err_ratelimited (dev,"Failed to attach domain to iommu\n" );return ret;drivers/iommu/iommu.c +45
-2@@ -18,6 +18,7 @@#include <linux/errno.h> #include <linux/host1x_context_bus.h> #include <linux/iommu.h> +#include <linux/iommu-lu.h> #include <linux/iommufd.h> #include <linux/idr.h> #include <linux/err.h> @@ -489,6 +490,10 @@ 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); +#endif + iommu_dev = ops->probe_device (dev);if (IS_ERR (iommu_dev)) { ret =PTR_ERR (iommu_dev);@@ -2149,6 +2154,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 ;@@ -3061,6 +3073,34 @@ int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids) }EXPORT_SYMBOL_GPL (iommu_fwspec_add_ids); +static struct iommu_domain *__iommu_group_maybe_restore_domain (struct iommu_group *group) +{ +struct device_ser *device_ser; +struct iommu_domain *domain; +struct device *dev; + + 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); +if (WARN_ON (IS_ERR (domain))) +return NULL ; + +/* +* The group is owned by the entity (a preserved iommufd) that provided +* this token in the previous kernel. It will be used to reclaim it +* later. +*/ + group->owner = (void *)device_ser->token; + group->owner_cnt =1 ; +return domain; +} +/** * iommu_setup_default_domain - Set the default_domain for the group * @group: Group to change @@ -3075,8 +3115,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;@@ -3120,6 +3160,9 @@ 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) { + restored_domain =__iommu_group_maybe_restore_domain (group); +if (!restored_domain) + 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 @@ -3127,7 +3170,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 +19
-0@@ -501,3 +501,34 @@ void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev)iommu_unpreserve_locked (iommu->iommu_dev); } + +struct iommu_domain *iommu_restore_domain (struct device *dev,struct device_ser *ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +struct iommu_domain *domain; +int ret; + + domain_ser =__va (ser->domain_iommu_ser.domain_phys); + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret) +return ERR_PTR (ret); + +guard (mutex)(&flb_obj->lock); +if (domain_ser->restored_domain) +return domain_ser->restored_domain; + + domain_ser->obj.incoming = true;
Quoted 8 lines
drivers/iommu/liveupdate.c void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev) · line 528
Quoted 6 lines
drivers/iommu/liveupdate.c +6
-0+ domain =iommu_paging_domain_alloc (dev); +if (IS_ERR (domain)) +return domain; + + ret = domain->ops->restore (domain, domain_ser); +if (ret)'domain' will leak here.
Agreed. It should be cleaned here if restore fails. I will fix this in the next revision.
Quoted 24 lines
Quoted 24 lines
drivers/iommu/liveupdate.c +6
-0+return ERR_PTR (ret); + + domain->preserved_state = domain_ser; + domain_ser->restored_domain = domain; +return domain; +}include/linux/iommu-lu.h +2
-0@@ -65,6 +65,8 @@ static inline int dev_iommu_restore_did(struct device *dev, struct iommu_domainreturn -1 ; } +struct iommu_domain *iommu_restore_domain (struct device *dev, +struct device_ser *ser);int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg);struct device_ser *iommu_get_device_preserved_data (struct device *dev);@@ -95,6 +97,12 @@ static inline void *iommu_domain_restored_state(struct iommu_domain *domain)return NULL ; }
Quoted 12 lines
Quoted 10 lines
include/linux/iommu-lu.h +6
-1+static inline struct iommu_domain *iommu_restore_domain (struct device *dev, +struct device_ser *ser) +{ +return NULL ; +} +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) {return -EOPNOTSUPP ; --Quoted 2 lines
2.53.0.rc2.204.g2597b5adb4-goog
Sami
Re: [PATCH 08/14] iommu: Restore and reattach preserved domains to devices
In reply to Samiullah KhawajaQuoted 31 lines · Samiullah Khawaja
Restore the preserved domains by restoring the page tables using restore IOMMU domain op. Reattach the preserved domain to the device during default domain setup. While attaching, reuse the domain ID that was used in the previous kernel. The context entry setup is not needed as that is preserved during liveupdate.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 40 ++++++++++++++++++------------ drivers/iommu/intel/iommu.h | 3 ++- drivers/iommu/intel/nested.c | 2 +- drivers/iommu/iommu.c | 47 ++++++++++++++++++++++++++++++++++-- drivers/iommu/liveupdate.c | 31 ++++++++++++++++++++++++ include/linux/iommu-lu.h | 8 ++++++ 6 files changed, 112 insertions(+), 19 deletions(-)drivers/iommu/intel/iommu.c +2
-1@@ -1029,7 +1029,8 @@ static bool first_level_by_default(struct intel_iommu *iommu)return true; } -int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) +int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu, +int restore_did) {struct iommu_domain_info *info, *curr;int num, ret = -ENOSPC ;
drivers/iommu/intel/iommu.c int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) · line 1053
Quoted 6 lines
drivers/iommu/intel/iommu.c +1
-2@@ -1049,8 +1050,11 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)return 0 ; } - num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , -cap_ndoms (iommu->cap) -1 ,GFP_KERNEL ); +if (restore_did >=0 )
I just looked at the code and saw the comment for IDA_START_DID [1] and I realized we probably shouldn't allow domain_ids 0 or 1 here, as they are reserved?
It seems that DID 0 essentially acts as a blocking domain, and DID 1 is used for identity / FLPT_DEFAULT_DID (comparing it to S1DSS in Arm)
If a corrupted KHO image passes restore_did = 0, this check evaluates to true. The driver will bypass the IDA, assign DID 0, and program it into the context entries. Wouldn't we accidentally attach the device to a blocking mode where the VT-d hardware just silently drops/faults all DMA?
Should this check be:
if (restore_did >= IDA_START_DID) ?
drivers/iommu/intel/iommu.c static int dmar_domain_attach_device(struct dmar_domain *domain,
Quoted 24 lines
drivers/iommu/intel/iommu.c +9
-1+ num = restore_did; + else + num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , +cap_ndoms (iommu->cap) -1 ,GFP_KERNEL );if (num <0 ) {pr_err ("%s: No free domain ids\n" , iommu->name); goto err_unlock;@@ -1321,10 +1325,14 @@ 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 device_ser *device_ser =NULL ;unsigned long flags;int ret; - ret =domain_attach_iommu (domain, iommu); + device_ser =dev_iommu_restored_state (dev); + + ret =domain_attach_iommu (domain, iommu, +dev_iommu_restore_did (dev, &domain->domain));if (ret)return ret;@@ -1337,16 +1345,18 @@ static int dmar_domain_attach_device(struct dmar_domain *domain,
[ ------- >8 snip -------- ]
Quoted 55 lines
drivers/iommu/iommu.c +26
-0@@ -18,6 +18,7 @@#include <linux/errno.h> #include <linux/host1x_context_bus.h> #include <linux/iommu.h> +#include <linux/iommu-lu.h> #include <linux/iommufd.h> #include <linux/idr.h> #include <linux/err.h> @@ -489,6 +490,10 @@ 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); +#endif + iommu_dev = ops->probe_device (dev);if (IS_ERR (iommu_dev)) { ret =PTR_ERR (iommu_dev);@@ -2149,6 +2154,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 ;@@ -3061,6 +3073,34 @@ int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids) }EXPORT_SYMBOL_GPL (iommu_fwspec_add_ids); +static struct iommu_domain *__iommu_group_maybe_restore_domain (struct iommu_group *group) +{ +struct device_ser *device_ser; +struct iommu_domain *domain; +struct device *dev; + + 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 ; +
drivers/iommu/iommu.c int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids) · line 3099
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+ domain =iommu_restore_domain (dev, device_ser); +if (WARN_ON (IS_ERR (domain))) +return NULL ; + +/* +* The group is owned by the entity (a preserved iommufd) that provided +* this token in the previous kernel. It will be used to reclaim it +* later. +*/ + group->owner = (void *)device_ser->token;
Umm.. this is a nak as it looks troubled for the following reasons:
1. The group->owner and group->owner_cnt are being updated directly without holding group->mutex. We shouldn't bypass the lock as this violates the group's concurrency protections and can cause APIs like iommu_device_claim_dma_owner[2] to race or fail.
2. Stuff like this shouldn't be open-coded. Presumably, this was open-coded because the standard API (__iommu_take_dma_ownership) forces the IOMMU group into a blocking_domain, which conflicts with the fact that we just restored an active paging domain?
I think we should introduce a proper helper that acquires the lock, safely sets the opaque owner, and manages the domain state without forcing it into a blocking domain? Please note that the helper shouldn't be exposed / exported, we wouldn't want any abusive calls to claim owner without attaching to a blocked domain.
3. Please note that owner is an opaque "pointer" but we set it to a u64. Setting an opaque void *owner directly to a user-controlled u64 token is dangerous. If a malicious or unlucky userspace provides a token value that happens to perfectly match a valid kernel pointer (e.g. another subsystem's cookie), we'll have an aliasing collision.
If that subsystem subsequently calls iommu_device_claim_dma_owner(), the if (group->owner == owner) check will falsely pass, incorrectly granting DMA ownership and breaking device isolation.
I understand we can't restore the original owner pointer because the original iommufd file/context from the previous kernel no longer exists. However, to avoid aliasing could we dynamically allocate a small wrapper (e.g., struct iommu_lu_owner { u64 token; }) and set group->owner to the uniquely allocated pointer?
4. In the full RFC [3], we seem to be "transferring" the ownership but here we seem to set the owner_cnt = 1 what if it was > 1 earlier? The iommu_device_release_dma_owner seems to handle multiple release calls well so this shouldn't lead to crashes but the "owning" entity could use this count for some book keeping.. it'd be nice if we could have a proper restore_owner API / helper here.
Quoted 59 lines
drivers/iommu/iommu.c +9
-2+ group->owner_cnt =1 ; +return domain; +} +/** * iommu_setup_default_domain - Set the default_domain for the group * @group: Group to change @@ -3075,8 +3115,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;@@ -3120,6 +3160,9 @@ 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) { + restored_domain =__iommu_group_maybe_restore_domain (group); +if (!restored_domain) + 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 @@ -3127,7 +3170,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 +15
-0@@ -501,3 +501,34 @@ void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev)iommu_unpreserve_locked (iommu->iommu_dev); } + +struct iommu_domain *iommu_restore_domain (struct device *dev,struct device_ser *ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +struct iommu_domain *domain; +int ret; + + domain_ser =__va (ser->domain_iommu_ser.domain_phys); + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret) +return ERR_PTR (ret); + +guard (mutex)(&flb_obj->lock);
drivers/iommu/liveupdate.c void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev) · line 528
Quoted 10 lines
drivers/iommu/liveupdate.c +10
-0+if (domain_ser->restored_domain) +return domain_ser->restored_domain; + + domain_ser->obj.incoming = true; + domain =iommu_paging_domain_alloc (dev); +if (IS_ERR (domain)) +return domain; + + ret = domain->ops->restore (domain, domain_ser); +if (ret)
did we miss free-ing domain here?
Quoted 22 lines
drivers/iommu/liveupdate.c +6
-0+return ERR_PTR (ret); + + domain->preserved_state = domain_ser; + domain_ser->restored_domain = domain; +return domain; +}include/linux/iommu-lu.h +2
-0@@ -65,6 +65,8 @@ static inline int dev_iommu_restore_did(struct device *dev, struct iommu_domainreturn -1 ; } +struct iommu_domain *iommu_restore_domain (struct device *dev, +struct device_ser *ser);int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg);struct device_ser *iommu_get_device_preserved_data (struct device *dev);@@ -95,6 +97,12 @@ static inline void *iommu_domain_restored_state(struct iommu_domain *domain)return NULL ; }
include/linux/iommu-lu.h static inline void *iommu_domain_restored_state(struct iommu_domain *domain) · line 108
Quoted 10 lines
include/linux/iommu-lu.h +6
-0+static inline struct iommu_domain *iommu_restore_domain (struct device *dev, +struct device_ser *ser) +{ +return NULL ; +} +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) {return -EOPNOTSUPP ;
Thanks,
Praan
[1] https://elixir.bootlin.com/linux/v7.0-rc4/source/drivers/iommu/intel/iommu.h#L795
[2] https://elixir.bootlin.com/linux/v7.0-rc4/source/drivers/iommu/iommu.c#L3367
Re: [PATCH 08/14] iommu: Restore and reattach preserved domains to devices
Quoted 46 lines · Pranjal Shrivastava
Quoted 31 lines · Samiullah Khawaja
Restore the preserved domains by restoring the page tables using restore IOMMU domain op. Reattach the preserved domain to the device during default domain setup. While attaching, reuse the domain ID that was used in the previous kernel. The context entry setup is not needed as that is preserved during liveupdate.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 40 ++++++++++++++++++------------ drivers/iommu/intel/iommu.h | 3 ++- drivers/iommu/intel/nested.c | 2 +- drivers/iommu/iommu.c | 47 ++++++++++++++++++++++++++++++++++-- drivers/iommu/liveupdate.c | 31 ++++++++++++++++++++++++ include/linux/iommu-lu.h | 8 ++++++ 6 files changed, 112 insertions(+), 19 deletions(-)drivers/iommu/intel/iommu.c +2
-1@@ -1029,7 +1029,8 @@ static bool first_level_by_default(struct intel_iommu *iommu)return true; } -int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) +int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu, +int restore_did) {struct iommu_domain_info *info, *curr;int num, ret = -ENOSPC ;drivers/iommu/intel/iommu.c int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) · line 1053
Quoted 6 lines
drivers/iommu/intel/iommu.c +1
-2@@ -1049,8 +1050,11 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)return 0 ; } - num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , -cap_ndoms (iommu->cap) -1 ,GFP_KERNEL ); +if (restore_did >=0 )I just looked at the code and saw the comment for
IDA_START_DID[1] and I realized we probably shouldn't allowdomain_ids0 or 1 here, as they are reserved?It seems that DID 0 essentially acts as a blocking domain, and DID 1 is used for identity /
FLPT_DEFAULT_DID(comparing it to S1DSS in Arm)
Quoted 7 lines
If a corrupted KHO image passes
restore_did= 0, this check evaluates to true. The driver will bypass the IDA, assign DID 0, and program it into the context entries. Wouldn't we accidentally attach the device to a blocking mode where the VT-d hardware just silently drops/faults all DMA?Should this check be:
if (restore_did>=IDA_START_DID) ?
Agreed. Will update.
Quoted 96 lines
drivers/iommu/intel/iommu.c static int dmar_domain_attach_device(struct dmar_domain *domain,
Quoted 25 lines
drivers/iommu/intel/iommu.c +9
-1+ num = restore_did; + else + num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , +cap_ndoms (iommu->cap) -1 ,GFP_KERNEL );if (num <0 ) {pr_err ("%s: No free domain ids\n" , iommu->name); goto err_unlock;@@ -1321,10 +1325,14 @@ 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 device_ser *device_ser =NULL ;unsigned long flags;int ret; - ret =domain_attach_iommu (domain, iommu); + device_ser =dev_iommu_restored_state (dev); + + ret =domain_attach_iommu (domain, iommu, +dev_iommu_restore_did (dev, &domain->domain));if (ret)return ret;@@ -1337,16 +1345,18 @@ static int dmar_domain_attach_device(struct dmar_domain *domain,[ ------- >8 snip -------- ]
Quoted 55 lines
drivers/iommu/iommu.c +26
-0@@ -18,6 +18,7 @@#include <linux/errno.h> #include <linux/host1x_context_bus.h> #include <linux/iommu.h> +#include <linux/iommu-lu.h> #include <linux/iommufd.h> #include <linux/idr.h> #include <linux/err.h> @@ -489,6 +490,10 @@ 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); +#endif + iommu_dev = ops->probe_device (dev);if (IS_ERR (iommu_dev)) { ret =PTR_ERR (iommu_dev);@@ -2149,6 +2154,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 ;@@ -3061,6 +3073,34 @@ int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids) }EXPORT_SYMBOL_GPL (iommu_fwspec_add_ids); +static struct iommu_domain *__iommu_group_maybe_restore_domain (struct iommu_group *group) +{ +struct device_ser *device_ser; +struct iommu_domain *domain; +struct device *dev; + + 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 ; +drivers/iommu/iommu.c int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids) · line 3099
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+ domain =iommu_restore_domain (dev, device_ser); +if (WARN_ON (IS_ERR (domain))) +return NULL ; + +/* +* The group is owned by the entity (a preserved iommufd) that provided +* this token in the previous kernel. It will be used to reclaim it +* later. +*/ + group->owner = (void *)device_ser->token;Umm.. this is a nak as it looks troubled for the following reasons:
Quoted 4 lines
1. The
group->ownerandgroup->owner_cntare being updated directly without holdinggroup->mutex. We shouldn't bypass the lock as this violates the group's concurrency protections and can cause APIs like iommu_device_claim_dma_owner[2] to race or fail.
This function is called with group->mutex held during the setup of default domain during device iommu probe. So this should fine. I will add a lockdep to make it clear.
Quoted 11 lines
2. Stuff like this shouldn't be open-coded. Presumably, this was open-coded because the standard API (
__iommu_take_dma_ownership) forces the IOMMU group into ablocking_domain, which conflicts with the fact that we just restored an active paging domain?I think we should introduce a proper helper that acquires the lock, safely sets the opaque owner, and manages the domain state without forcing it into a blocking domain? Please note that the helper shouldn't be exposed / exported, we wouldn't want any abusive calls to claim owner without attaching to a blocked domain.
The init/restore of owner fields in group should only be done during restore when restoring the iommu domain for the group with mutex already held. Adding a separate helper might allow setting it up at some other place even in the core, so want to avoid that.
Quoted 11 lines
3. Please note that owner is an opaque "pointer" but we set it to a u64. Setting an opaque void *owner directly to a user-controlled u64 token is dangerous. If a malicious or unlucky userspace provides a token value that happens to perfectly match a valid kernel pointer (e.g. another subsystem's cookie), we'll have an aliasing collision.
If that subsystem subsequently calls
iommu_device_claim_dma_owner(), the if (group->owner== owner) check will falsely pass, incorrectly granting DMA ownership and breaking device isolation.
Quoted 5 lines
I understand we can't restore the original owner pointer because the original iommufd file/context from the previous kernel no longer exists. However, to avoid aliasing could we dynamically allocate a small wrapper (e.g., struct
iommu_lu_owner{ u64 token; }) and setgroup->ownerto the uniquely allocated pointer?
Agreed. Will update.
Quoted 7 lines
4. In the full RFC [3], we seem to be "transferring" the ownership but here we seem to set the
owner_cnt= 1 what if it was > 1 earlier? Theiommu_device_release_dma_ownerseems to handle multiple release calls well so this shouldn't lead to crashes but the "owning" entity could use this count for some book keeping.. it'd be nice if we could have a properrestore_ownerAPI / helper here.
This is being done during iommu probe of the device when the default domain is being set on the group, so group will not have any existing owner. I will add a WARN for this. The transfer of ownership will happen after the iommufd is restored. Also note that once initialized here, the device/group will not be owned by any other driver or iommufd until the ownership transfer is done.
Quoted 64 lines
Quoted 64 lines
drivers/iommu/iommu.c +9
-2+ group->owner_cnt =1 ; +return domain; +} +/** * iommu_setup_default_domain - Set the default_domain for the group * @group: Group to change @@ -3075,8 +3115,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;@@ -3120,6 +3160,9 @@ 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) { + restored_domain =__iommu_group_maybe_restore_domain (group); +if (!restored_domain) + 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 @@ -3127,7 +3170,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 +19
-0@@ -501,3 +501,34 @@ void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev)iommu_unpreserve_locked (iommu->iommu_dev); } + +struct iommu_domain *iommu_restore_domain (struct device *dev,struct device_ser *ser) +{ +struct iommu_domain_ser *domain_ser; +struct iommu_lu_flb_obj *flb_obj; +struct iommu_domain *domain; +int ret; + + domain_ser =__va (ser->domain_iommu_ser.domain_phys); + + ret =liveupdate_flb_get_incoming (&iommu_flb, (void **)&flb_obj); +if (ret) +return ERR_PTR (ret); + +guard (mutex)(&flb_obj->lock); +if (domain_ser->restored_domain) +return domain_ser->restored_domain; + + domain_ser->obj.incoming = true;
Quoted 8 lines
drivers/iommu/liveupdate.c void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev) · line 528
Quoted 6 lines
drivers/iommu/liveupdate.c +6
-0+ domain =iommu_paging_domain_alloc (dev); +if (IS_ERR (domain)) +return domain; + + ret = domain->ops->restore (domain, domain_ser); +if (ret)did we miss free-ing domain here?
Yes, Ankit pointed out same. Will update this.
Quoted 37 lines
Quoted 23 lines
drivers/iommu/liveupdate.c +6
-0+return ERR_PTR (ret); + + domain->preserved_state = domain_ser; + domain_ser->restored_domain = domain; +return domain; +}include/linux/iommu-lu.h +2
-0@@ -65,6 +65,8 @@ static inline int dev_iommu_restore_did(struct device *dev, struct iommu_domainreturn -1 ; } +struct iommu_domain *iommu_restore_domain (struct device *dev, +struct device_ser *ser);int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg);struct device_ser *iommu_get_device_preserved_data (struct device *dev);@@ -95,6 +97,12 @@ static inline void *iommu_domain_restored_state(struct iommu_domain *domain)return NULL ; }include/linux/iommu-lu.h static inline void *iommu_domain_restored_state(struct iommu_domain *domain) · line 108
Quoted 10 lines
include/linux/iommu-lu.h +6
-0+static inline struct iommu_domain *iommu_restore_domain (struct device *dev, +struct device_ser *ser) +{ +return NULL ; +} +static inline int iommu_for_each_preserved_device (iommu_preserved_device_iter_fn fn,void *arg) {return -EOPNOTSUPP ;Thanks,
Praan
Quoted 2 lines
[1] https://elixir.bootlin.com/linux/v7.0-rc4/source/drivers/iommu/intel/iommu.h#L795
[2] https://elixir.bootlin.com/linux/v7.0-rc4/source/drivers/iommu/iommu.c#L3367
Thanks,
Sami
PATCH 9/14 iommu/vt-d: preserve PASID table of preserved device
In reply to Samiullah KhawajaIn scalable mode the PASID table is used to fetch the io page tables. Preserve and restore the PASID table of the preserved devices.
--- drivers/iommu/intel/iommu.c | 4 +- drivers/iommu/intel/iommu.h | 5 ++ drivers/iommu/intel/liveupdate.c | 130 +++++++++++++++++++++++++++++++ drivers/iommu/intel/pasid.c | 7 +- drivers/iommu/intel/pasid.h | 9 +++ include/linux/kho/abi/iommu.h | 8 ++ 6 files changed, 160 insertions(+), 3 deletions(-)
drivers/iommu/intel/iommu.c +3 -1
@@ -2944,8 +2944,10 @@ static bool __maybe_clean_unpreserved_context_entries(struct intel_iommu *iommu)if (info->iommu != iommu)continue ; -if (dev_iommu_preserved_state (&pdev->dev)) +if (dev_iommu_preserved_state (&pdev->dev)) { +pasid_cleanup_preserved_table (&pdev->dev);continue ; + }domain_context_clear (info); }
drivers/iommu/intel/iommu.h +5 -0
@@ -1286,6 +1286,7 @@ int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_ser *iommu_servoid intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser);void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu,struct iommu_ser *iommu_ser); +void pasid_cleanup_preserved_table (struct device *dev);#else static inline int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) {
@@ -1309,6 +1310,10 @@ static inline void intel_iommu_liveupdate_restore_root_table(struct intel_iommu struct iommu_ser *iommu_ser) { } + +static inline void pasid_cleanup_preserved_table (struct device *dev) +{ +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM
drivers/iommu/intel/liveupdate.c +130 -0
159 lines not on this page · raw message
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, max_pasid); +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
@@ -287,6 +287,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 +8 -0
@@ -50,6 +50,11 @@ struct device_domain_iommu_ser {u64 iommu_phys; } __packed; +struct device_intel_ser { +u64 pasid_table; +u64 max_pasid; +} __packed; +struct device_ser {struct iommu_obj_ser obj;u64 token;
@@ -57,6 +62,9 @@ struct device_ser {u32 pci_domain;struct device_domain_iommu_ser domain_iommu_ser;enum iommu_lu_type type; +union { +struct device_intel_ser intel; + }; } __packed;struct iommu_intel_ser {
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 09/14] iommu/vt-d: preserve PASID table of preserved device
Quoted 131 lines · Samiullah Khawaja
In scalable mode the PASID table is used to fetch the io page tables. Preserve and restore the PASID table of the preserved devices.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 4 +- drivers/iommu/intel/iommu.h | 5 ++ drivers/iommu/intel/liveupdate.c | 130 +++++++++++++++++++++++++++++++ drivers/iommu/intel/pasid.c | 7 +- drivers/iommu/intel/pasid.h | 9 +++ include/linux/kho/abi/iommu.h | 8 ++ 6 files changed, 160 insertions(+), 3 deletions(-)drivers/iommu/intel/iommu.c +3
-1@@ -2944,8 +2944,10 @@ static bool __maybe_clean_unpreserved_context_entries(struct intel_iommu *iommu)if (info->iommu != iommu)continue ; -if (dev_iommu_preserved_state (&pdev->dev)) +if (dev_iommu_preserved_state (&pdev->dev)) { +pasid_cleanup_preserved_table (&pdev->dev);continue ; + }domain_context_clear (info); }drivers/iommu/intel/iommu.h +5
-0@@ -1286,6 +1286,7 @@ int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_ser *iommu_servoid intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser);void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu,struct iommu_ser *iommu_ser); +void pasid_cleanup_preserved_table (struct device *dev);#else static inline int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) {@@ -1309,6 +1310,10 @@ static inline void intel_iommu_liveupdate_restore_root_table(struct intel_iommu struct iommu_ser *iommu_ser) { } + +static inline void pasid_cleanup_preserved_table (struct device *dev) +{ +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM drivers/iommu/intel/liveupdate.c +64
-0@@ -14,6 +14,7 @@#include <linux/pci.h> #include "iommu.h" +#include "pasid.h" #include "../iommu-pages.h" static void unpreserve_iommu_context (struct intel_iommu *iommu,int end)@@ -113,9 +114,89 @@ void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu, iommu->reg_phys, iommu_ser->intel.root_table); } +enum pasid_lu_op { +PASID_LU_OP_PRESERVE =1 , +PASID_LU_OP_UNPRESERVE , +PASID_LU_OP_RESTORE , +PASID_LU_OP_FREE , +}; + +static int pasid_lu_do_op (void *table,enum pasid_lu_op op) +{ +int ret =0 ; + +switch (op) { +case PASID_LU_OP_PRESERVE : + ret =iommu_preserve_page (table); +break ; +case PASID_LU_OP_UNPRESERVE : +iommu_unpreserve_page (table); +break ; +case PASID_LU_OP_RESTORE : +iommu_restore_page (virt_to_phys (table)); +break ; +case PASID_LU_OP_FREE : +iommu_free_pages (table); +break ; + } + +return ret; +} + +static int pasid_lu_handle_pd (struct pasid_dir_entry *dir,enum pasid_lu_op op) +{ +struct pasid_entry *table; +int ret; + +/* Only preserve first table for NO_PASID. */ + table =get_pasid_table_from_pde (&dir[0 ]); +if (!table) +return -EINVAL ; + + ret =pasid_lu_do_op (table, op); +if (ret) +return ret; + + ret =pasid_lu_do_op (dir, op); +if (ret) + goto err; + +return 0 ; +err: +if (op ==PASID_LU_OP_PRESERVE ) +pasid_lu_do_op (table,PASID_LU_OP_UNPRESERVE ); + +return ret; +} + +void pasid_cleanup_preserved_table (struct device *dev) +{ +struct pasid_table *pasid_table; +struct pasid_dir_entry *dir; +struct pasid_entry *table; + + pasid_table =intel_pasid_get_table (dev); +if (!pasid_table)
drivers/iommu/intel/liveupdate.c void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu, · line 189
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +10
-0+return ; + + dir = pasid_table->table; + table =get_pasid_table_from_pde (&dir[0 ]); +if (!table) +return ; + +/* Cleanup everything except the first entry. */ +memset (&table[1 ],0 ,SZ_4K -sizeof (*table)); +memset (&dir[1 ],0 ,SZ_4K -sizeof (struct pasid_dir_entry ));
(Not too familiar with Intel IOMMU / VT-d)
We seem to hardcode SZ_4K when clearing the directory entries. But in intel_pasid_alloc_table(), the allocation size seems to depend on max_pasid which could be larger than one page (order > 0)?
If the directory is multi-page, won't we leave the trailing pages and full of stale PDE pointers that the HW could still walk?
drivers/iommu/intel/liveupdate.c void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu, · line 194
Quoted 5 lines
drivers/iommu/intel/liveupdate.c +5
-0+ +clflush_cache_range (&table[0 ],SZ_4K ); +clflush_cache_range (&dir[0 ],SZ_4K ); +} +
[ ------ >8 ------ ]
Thanks,
Praan
Re: [PATCH 09/14] iommu/vt-d: preserve PASID table of preserved device
Quoted 148 lines · Pranjal Shrivastava
Quoted 131 lines · Samiullah Khawaja
In scalable mode the PASID table is used to fetch the io page tables. Preserve and restore the PASID table of the preserved devices.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/iommu/intel/iommu.c | 4 +- drivers/iommu/intel/iommu.h | 5 ++ drivers/iommu/intel/liveupdate.c | 130 +++++++++++++++++++++++++++++++ drivers/iommu/intel/pasid.c | 7 +- drivers/iommu/intel/pasid.h | 9 +++ include/linux/kho/abi/iommu.h | 8 ++ 6 files changed, 160 insertions(+), 3 deletions(-)drivers/iommu/intel/iommu.c +3
-1@@ -2944,8 +2944,10 @@ static bool __maybe_clean_unpreserved_context_entries(struct intel_iommu *iommu)if (info->iommu != iommu)continue ; -if (dev_iommu_preserved_state (&pdev->dev)) +if (dev_iommu_preserved_state (&pdev->dev)) { +pasid_cleanup_preserved_table (&pdev->dev);continue ; + }domain_context_clear (info); }drivers/iommu/intel/iommu.h +5
-0@@ -1286,6 +1286,7 @@ int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_ser *iommu_servoid intel_iommu_unpreserve (struct iommu_device *iommu,struct iommu_ser *iommu_ser);void intel_iommu_liveupdate_restore_root_table (struct intel_iommu *iommu,struct iommu_ser *iommu_ser); +void pasid_cleanup_preserved_table (struct device *dev);#else static inline int intel_iommu_preserve_device (struct device *dev,struct device_ser *device_ser) {@@ -1309,6 +1310,10 @@ static inline void intel_iommu_liveupdate_restore_root_table(struct intel_iommu struct iommu_ser *iommu_ser) { } + +static inline void pasid_cleanup_preserved_table (struct device *dev) +{ +}#endif #ifdef CONFIG_INTEL_IOMMU_SVM drivers/iommu/intel/liveupdate.c +64
-0@@ -14,6 +14,7 @@#include <linux/pci.h> #include "iommu.h" +#include "pasid.h" #include "../iommu-pages.h" static void unpreserve_iommu_context (struct intel_iommu *iommu,int end)@@ -113,9 +114,89 @@ void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu, iommu->reg_phys, iommu_ser->intel.root_table); } +enum pasid_lu_op { +PASID_LU_OP_PRESERVE =1 , +PASID_LU_OP_UNPRESERVE , +PASID_LU_OP_RESTORE , +PASID_LU_OP_FREE , +}; + +static int pasid_lu_do_op (void *table,enum pasid_lu_op op) +{ +int ret =0 ; + +switch (op) { +case PASID_LU_OP_PRESERVE : + ret =iommu_preserve_page (table); +break ; +case PASID_LU_OP_UNPRESERVE : +iommu_unpreserve_page (table); +break ; +case PASID_LU_OP_RESTORE : +iommu_restore_page (virt_to_phys (table)); +break ; +case PASID_LU_OP_FREE : +iommu_free_pages (table); +break ; + } + +return ret; +} + +static int pasid_lu_handle_pd (struct pasid_dir_entry *dir,enum pasid_lu_op op) +{ +struct pasid_entry *table; +int ret; + +/* Only preserve first table for NO_PASID. */ + table =get_pasid_table_from_pde (&dir[0 ]); +if (!table) +return -EINVAL ; + + ret =pasid_lu_do_op (table, op); +if (ret) +return ret; + + ret =pasid_lu_do_op (dir, op); +if (ret) + goto err; + +return 0 ; +err: +if (op ==PASID_LU_OP_PRESERVE ) +pasid_lu_do_op (table,PASID_LU_OP_UNPRESERVE ); + +return ret; +} + +void pasid_cleanup_preserved_table (struct device *dev) +{ +struct pasid_table *pasid_table; +struct pasid_dir_entry *dir; +struct pasid_entry *table; + + pasid_table =intel_pasid_get_table (dev); +if (!pasid_table)drivers/iommu/intel/liveupdate.c void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu, · line 189
Quoted 10 lines
drivers/iommu/intel/liveupdate.c +10
-0+return ; + + dir = pasid_table->table; + table =get_pasid_table_from_pde (&dir[0 ]); +if (!table) +return ; + +/* Cleanup everything except the first entry. */ +memset (&table[1 ],0 ,SZ_4K -sizeof (*table)); +memset (&dir[1 ],0 ,SZ_4K -sizeof (struct pasid_dir_entry ));(Not too familiar with Intel IOMMU / VT-d)
We seem to hardcodeSZ_4Kwhen clearing the directory entries. But inintel_pasid_alloc_table(), the allocation size seems to depend onmax_pasidwhich could be larger than one page (order > 0)?
Quoted 2 lines
If the directory is multi-page, won't we leave the trailing pages and full of stale PDE pointers that the HW could still walk?
Agreed. It should cleanup based on max_pasid. Will update.
Quoted 11 lines
drivers/iommu/intel/liveupdate.c void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu, · line 194
Quoted 6 lines
drivers/iommu/intel/liveupdate.c +5
-0+ +clflush_cache_range (&table[0 ],SZ_4K ); +clflush_cache_range (&dir[0 ],SZ_4K ); +} +[ ------ >8 ------ ]
Thanks,
Praan
PATCH 10/14 iommufd-lu: Implement ioctl to let userspace mark an HWPT to be preserved
In reply to Samiullah KhawajaFrom: YiFei Zhu <[email protected]>
Userspace provides a token, which will then be used at restore to identify this HWPT. The restoration logic is not implemented and will be added later.
--- drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/iommufd_private.h | 13 +++++++ drivers/iommu/iommufd/liveupdate.c | 49 +++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 2 + include/uapi/linux/iommufd.h | 19 ++++++++++ 5 files changed, 84 insertions(+) create mode 100644 drivers/iommu/iommufd/liveupdate.c
drivers/iommu/iommufd/Makefile +1 -0
@@ -17,3 +17,4 @@ obj-$(CONFIG_IOMMUFD_DRIVER) += iova_bitmap.o iommufd_driver-y := driver.o obj-$(CONFIG_IOMMUFD_DRIVER_CORE) += iommufd_driver.o +obj-$(CONFIG_IOMMU_LIVEUPDATE) += liveupdate.o
drivers/iommu/iommufd/iommufd_private.h +13 -0
@@ -374,6 +374,10 @@ struct iommufd_hwpt_paging { bool auto_domain : 1; bool enforce_cache_coherency : 1; bool nest_parent : 1; +#ifdef CONFIG_IOMMU_LIVEUPDATE + bool lu_preserve : 1; +u32 lu_token; +#endif /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item;struct iommufd_sw_msi_maps present_sw_msi;
@@ -707,6 +711,15 @@ iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id) struct iommufd_vdevice, obj); } +#ifdef CONFIG_IOMMU_LIVEUPDATE +int iommufd_hwpt_lu_set_preserve (struct iommufd_ucmd *ucmd); +#else +static inline int iommufd_hwpt_lu_set_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 +49 -0
49 lines not on this page · raw message
drivers/iommu/iommufd/main.c +2 -0
@@ -493,6 +493,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_LU_SET_PRESERVE ,iommufd_hwpt_lu_set_preserve , +struct iommu_hwpt_lu_set_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 +19 -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_SET_PRESERVE = 0x95, };/**
@@ -1299,4 +1300,22 @@ struct iommu_hw_queue_alloc { __aligned_u64 length; };#define IOMMU_HW_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HW_QUEUE_ALLOC) + +/** +* struct iommu_hwpt_lu_set_preserve - ioctl(IOMMU_HWPT_LU_SET_PRESERVE) +* @size: sizeof(struct iommu_hwpt_lu_set_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. +* +* The hwpt_token is provided by userspace. If userspace enters a token +* already in use within this iommufd, -EADDRINUSE is returned from this ioctl. +*/ +struct iommu_hwpt_lu_set_preserve { +__u32 size; +__u32 hwpt_id; +__u32 hwpt_token; +}; +#define IOMMU_HWPT_LU_SET_PRESERVE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_LU_SET_PRESERVE)#endif
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH 10/14] iommufd-lu: Implement ioctl to let userspace mark an HWPT to be preserved
drivers/iommu/iommufd/iommufd_private.h struct iommufd_hwpt_paging { · line 379
Quoted 8 lines · Samiullah Khawaja
From: YiFei Zhu <[email protected]>
drivers/iommu/iommufd/iommufd_private.h +3
-0@@ -374,6 +374,10 @@ struct iommufd_hwpt_paging { bool auto_domain : 1; bool enforce_cache_coherency : 1; bool nest_parent : 1; +#ifdef CONFIG_IOMMU_LIVEUPDATE + bool lu_preserve : 1; +u32 lu_token;
Should we use full name i.e. liveupdate here and other places in this series?
drivers/iommu/iommufd/main.c static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { · line 497
Quoted 10 lines
drivers/iommu/iommufd/main.c +2
-0@@ -493,6 +493,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_LU_SET_PRESERVE ,iommufd_hwpt_lu_set_preserve , + struct iommu_hwpt_lu_set_preserve, hwpt_token),
Shouldn't struct iommu_hwpt_lu_set_preserve{} be added to union ucmd_buffer{} above in this file?
Re: [PATCH 10/14] iommufd-lu: Implement ioctl to let userspace mark an HWPT to be preserved
Quoted 12 lines · Vipin Sharma
drivers/iommu/iommufd/iommufd_private.h struct iommufd_hwpt_paging { · line 379
Quoted 8 lines · Samiullah Khawaja
From: YiFei Zhu <[email protected]>
drivers/iommu/iommufd/iommufd_private.h +3
-0@@ -374,6 +374,10 @@ struct iommufd_hwpt_paging { bool auto_domain : 1; bool enforce_cache_coherency : 1; bool nest_parent : 1; +#ifdef CONFIG_IOMMU_LIVEUPDATE + bool lu_preserve : 1; +u32 lu_token;Should we use full name i.e. liveupdate here and other places in this series?
I think using full name liveupdate would be too long in other places in this series. And also there are other examples of "luo" being used as a short form. Please see:
https://lore.kernel.org/all/[email protected]/
Quoted 14 lines
drivers/iommu/iommufd/main.c static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { · line 497
Quoted 11 lines
drivers/iommu/iommufd/main.c +2
-0@@ -493,6 +493,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_LU_SET_PRESERVE ,iommufd_hwpt_lu_set_preserve , + struct iommu_hwpt_lu_set_preserve, hwpt_token),Shouldn't struct iommu_hwpt_lu_set_preserve{} be added to union ucmd_buffer{} above in this file?
Agreed. Will update.
Quoted 1 line
Re: [PATCH 10/14] iommufd-lu: Implement ioctl to let userspace mark an HWPT to be preserved
Quoted 14 lines · Samiullah Khawaja
Quoted 12 lines · Vipin Sharma
drivers/iommu/iommufd/iommufd_private.h struct iommufd_hwpt_paging { · line 379
Quoted 8 lines · Samiullah Khawaja
From: YiFei Zhu <[email protected]>
drivers/iommu/iommufd/iommufd_private.h +3
-0@@ -374,6 +374,10 @@ struct iommufd_hwpt_paging { bool auto_domain : 1; bool enforce_cache_coherency : 1; bool nest_parent : 1; +#ifdef CONFIG_IOMMU_LIVEUPDATE + bool lu_preserve : 1; +u32 lu_token;Should we use full name i.e. liveupdate here and other places in this series?
Quoted 5 lines
I think using full name liveupdate would be too long in other places in this series. And also there are other examples of "luo" being used as a short form. Please see:
That patch is using "luo" short, which I think is also wrong in saying memfd_luo, as there is nothing related to orchestrator (O of LUO) in that patch. It is saving memfd state for liveupdate. But that ship has sailed.
In the current patch, I don't think it will be too long, and it also easier to read code without someone to know what "lu" is. We have worked on it so we know what "lu" stands for and kind of accepted it but I agree what Greg k-h was also suggesting.
https://lore.kernel.org/all/2025093023-frantic-sediment-9849@gregkh/
- "You have more letters, please use them. "lu" is too short."
Not a hard no from me, just a suggestion.
Re: [PATCH 10/14] iommufd-lu: Implement ioctl to let userspace mark an HWPT to be preserved
Quoted 19 lines · Vipin Sharma
Quoted 18 lines · Samiullah Khawaja
Quoted 12 lines · Vipin Sharma
drivers/iommu/iommufd/iommufd_private.h struct iommufd_hwpt_paging { · line 379
Quoted 8 lines · Samiullah Khawaja
From: YiFei Zhu <[email protected]>
drivers/iommu/iommufd/iommufd_private.h +3
-0@@ -374,6 +374,10 @@ struct iommufd_hwpt_paging { bool auto_domain : 1; bool enforce_cache_coherency : 1; bool nest_parent : 1; +#ifdef CONFIG_IOMMU_LIVEUPDATE + bool lu_preserve : 1; +u32 lu_token;Should we use full name i.e. liveupdate here and other places in this series?
I think using full name liveupdate would be too long in other places in this series. And also there are other examples of "luo" being used as a short form. Please see:
Quoted 6 lines
Quoted 1 line
That patch is using "luo" short, which I think is also wrong in saying
memfd_luo, as there is nothing related to orchestrator (O of LUO) in that patch. It is saving memfd state for liveupdate. But that ship has sailed.
Yes, I think that makes sense.
Quoted 5 lines
In the current patch, I don't think it will be too long, and it also easier to read code without someone to know what "lu" is. We have worked on it so we know what "lu" stands for and kind of accepted it but I agree what Greg k-h was also suggesting.
Agreed, in this patch it won't be too long. I think for the public API being used in other subsystems we can use "liveupdate", but internally we can use lu. I will rework this.
Quoted 5 lines
https://lore.kernel.org/all/2025093023-frantic-sediment-9849@gregkh/
- "You have more letters, please use them. "lu" is too short."Not a hard no from me, just a suggestion.
Thanks,
Sami