iommu/amd: Implement live update state preservation
Subsystems (4)
cover iommu/amd: Implement live update state preservation
This series adds AMD-Vi support for IOMMU state preservation across a kexec-based live update, so a passed-through device keeps its translations and keeps doing DMA while the kernel underneath is replaced.
It applies on top of the IOMMU live update core series v5 [1], currently in review, and reuses the FLB preservation mechanism that series introduces.
This is an RFC. The reclaim path is not included; it depends on the phase 2 iommufd work and will follow.
What is preserved
=================
The Device Table, adopted by the incoming kernel rather than rebuilt, and for each preserved device its domain ID, page-table mode, and GCR3 tree if the device uses PASID.
What is not preserved
=====================
The command buffer, event log, PPR log, GA log, GA tail and cmd_sem, and the interrupt remapping tables. The incoming kernel allocates all of them fresh. Interrupts raised during the window are lost, but the data and the completion records travel by DMA, so a driver finds the completed work when it next reads its queues.
Disabling the PPR log also disables PPR, so a preserved device cannot raise a page request across the window. That is safe only because an HWPT with a fault queue cannot be marked for preservation today; if that is relaxed for SVA, such a request would go unanswered and, with the event log stopped, unlogged. Whoever adds PRI preservation needs to revisit this.
Architectural overview
======================
At preserve time the driver pins the PCI segment's Device Table for KHO and records each preserved device's domain ID, page-table mode and GCR3 tree.
At shutdown translation stays enabled on any unit carrying preserved devices, so their DMA never stops. Everything else on that unit is made safe: DTEs of unpreserved devices are reset to blocked, the interrupt fields of every DTE are cleared because no DTE may point at a table the next kernel can recycle, and the command, event, PPR and GA engines are stopped and polled until idle. That last step matters because the incoming kernel may reuse the pages those buffers occupied, and an engine still writing after the kexec would corrupt them with nothing to attribute the damage to. If an engine does not go idle the driver panics rather than complete the handover.
On the live update boot the driver finds its record by MMIO physical base, adopts the Device Table, and reserves the domain IDs it describes so a new domain cannot alias a preserved one. A unit handed over translating is left translating. When the core probes the preserved devices their domain ID and GCR3 tree are adopted on attach and verified against the live DTE; a mismatch fails the attach. A restored domain is immutable, so a preserved device may only attach to the domain restored for it.
One generic change
==================
Patch 2 moves the LUO handover-tree parse into start_kernel(), immediately before late_time_init().
AMD-Vi needs this because it is the x86 interrupt remapping provider, so amd_iommu_prepare() runs from late_time_init() via enable_IR_x2apic(). It queries preserved state from three sites on that path. LUO parses the tree from an early_initcall inside rest_init(), so those queries run before the data exists and cannot tell "nothing was preserved" from "not parsed yet". The driver then disables a unit the previous kernel left translating.
Note:
ATS and PASID state on the device itself is not adopted. For a preserved device the attach path still runs the normal enable sequence, so an ATS-capable device would have its ATS Control register rewritten while it is doing DMA. Adopting that state is PCI core roadmap item #4 [2], so this series leaves it alone rather than reprogramming the PCI side here. The restored DTE's IOTLB bit is checked against the device's ATS state, which catches a disagreement.
[1] Samiullah Khawaja, "iommu: Add live update state preservation" (v5)
https://lore.kernel.org/linux-iommu/[email protected]/ [2] David Matlack, "RFC: PCI core Live Update Roadmap" https://lore.kernel.org/linux-pci/[email protected]/
Ankit Soni (7):
iommu/amd: defer device attach only on a kdump boot
liveupdate: parse the incoming handover tree before late_time_init() iommu/kho/abi: add AMD IOMMU live-update serialisation structs iommu/amd: preserve IOMMU and device state for live update iommu/amd: clear unpreserved DTEs and quiesce logs at live-update shutdown iommu/amd: restore preserved state on a live-update boot iommu/amd: reattach preserved devices to their restored domains
drivers/iommu/amd/Makefile | 1 + drivers/iommu/amd/amd_iommu.h | 34 ++ drivers/iommu/amd/amd_iommu_types.h | 14 + drivers/iommu/amd/init.c | 253 +++++++++++-- drivers/iommu/amd/iommu.c | 144 +++++++- drivers/iommu/amd/liveupdate.c | 543 ++++++++++++++++++++++++++++ drivers/iommu/amd/nested.c | 8 + include/linux/kho/abi/iommu.h | 70 ++++ include/linux/liveupdate.h | 4 + init/main.c | 2 + kernel/liveupdate/luo_core.c | 5 +- 11 files changed, 1039 insertions(+), 39 deletions(-) create mode 100644 drivers/iommu/amd/liveupdate.c
base-commit: 1d195de0e8caf627e93c0a39af1e84bb19e3cc53
Signature · 1 line
2.43.0
RFC PATCH 1/7 iommu/amd: defer device attach only on a kdump boot
find_dev_data() sets defer_attach whenever the IOMMU came up already translating. Only a kdump boot can ever complete such an attach: the core completes it from iommu_deferred_attach(), behind a static key that is enabled only when is_kdump_kernel().
That was harmless while a pre-enabled unit surviving driver init implied a kdump boot. A live-update handover breaks the implication, because a unit carrying preserved devices is deliberately left translating. The devices behind it that were not preserved had their DTEs blocked at shutdown, so the skipped attach never replaces the blocked entry and they come up unable to do DMA.
Restrict the deferral to kdump, which is the only case that can complete it.
--- drivers/iommu/amd/iommu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +6 -1
@@ -31,6 +31,7 @@#include <linux/irqdomain.h> #include <linux/percpu.h> #include <linux/cc_platform.h> +#include <linux/crash_dump.h> #include <asm/irq_remapping.h> #include <asm/io_apic.h> #include <asm/apic.h>
@@ -500,7 +501,11 @@ static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid)if (!dev_data)return NULL ; -if (translation_pre_enabled (iommu)) +/* +* Only a kdump boot can complete a deferred attach, so only a +* kdump boot may start one. +*/ +if (translation_pre_enabled (iommu) &&is_kdump_kernel ()) dev_data->defer_attach = true; }
Signature · 1 line
2.43.0
RFC PATCH 2/7 liveupdate: parse the incoming handover tree before late_time_init()
In reply to Ankit SoniLUO parses the handover tree from an early_initcall, which runs inside rest_init() -- the last statement of start_kernel(). Anything earlier in start_kernel() cannot see the incoming state, and cannot tell that it cannot see it: luo_flb_retrieve_one() returns -ENODATA both before the parse and when the previous kernel handed nothing over.
On x86 the IOMMU is such a consumer, because it is also the interrupt remapping provider. amd_iommu_prepare() runs from late_time_init() via enable_IR_x2apic() -> irq_remapping_prepare(), and reaches init_iommu_one_late() long before any initcall.
Fix the ordering rather than teaching each consumer to cope. Call the parse directly from start_kernel(), immediately before late_time_init(). Rename it to liveupdate_init_early() at the same time, since liveupdate_early_init() was named after the early_initcall that no longer exists.
--- include/linux/liveupdate.h | 4 ++++ init/main.c | 2 ++ kernel/liveupdate/luo_core.c | 5 +---- 3 files changed, 7 insertions(+), 4 deletions(-)
include/linux/liveupdate.h +4 -0
@@ -229,6 +229,8 @@ struct liveupdate_flb {#ifdef CONFIG_LIVEUPDATE +void liveupdate_init_early (void ); +/* Return true if live update orchestrator is enabled */ bool liveupdate_enabled (void );
@@ -259,6 +261,8 @@ int liveupdate_get_token_outgoing(struct liveupdate_session *s,#else /* CONFIG_LIVEUPDATE */ +static inline void liveupdate_init_early (void ) { } +static inline bool liveupdate_enabled (void ) {return false;
init/main.c +2 -0
@@ -108,6 +108,7 @@#include <linux/time_namespace.h> #include <linux/unaligned.h> #include <linux/vdso_datastore.h> +#include <linux/liveupdate.h> #include <net/net_namespace.h> #include <asm/io.h>
@@ -1145,6 +1146,7 @@ void start_kernel(void)setup_per_cpu_pageset ();numa_policy_init ();acpi_early_init (); +liveupdate_init_early ();if (late_time_init)late_time_init ();sched_clock_init ();
kernel/liveupdate/luo_core.c +1 -4
@@ -135,7 +135,7 @@ static int __init luo_early_startup(void)return err; } -static int __init liveupdate_early_init (void ) +void __init liveupdate_init_early (void ) {int err;
@@ -145,10 +145,7 @@ static int __init liveupdate_early_init(void)luo_restore_fail ("The incoming tree failed to initialize properly [%pe], disabling live update\n" ,ERR_PTR (err)); } - -return err; } -early_initcall (liveupdate_early_init);/* Called during boot to create outgoing LUO state */ static int __init luo_state_setup (void )
Signature · 1 line
2.43.0
RFC PATCH 3/7 iommu/kho/abi: add AMD IOMMU live-update serialisation structs
In reply to Ankit SoniDescribe the AMD IOMMU state that has to cross a live-update kexec: the device table of a PCI segment, and the domain ID, page-table mode and GCR3 tree of a preserved device.
Both structs fit inside the existing unions, so the layout and the version are unchanged. Assert that, since growing either arm past the Intel one would move the array stride.
--- include/linux/kho/abi/iommu.h | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+)
include/linux/kho/abi/iommu.h +70 -0
@@ -8,6 +8,7 @@#ifndef _LINUX_KHO_ABI_IOMMU_H#define _LINUX_KHO_ABI_IOMMU_H +#include <linux/bug.h> #include <linux/mutex_types.h> #include <linux/compiler.h> #include <linux/types.h>
@@ -75,6 +76,7 @@/** * enum iommu_type_ser - Type of the IOMMU being preserved * @IOMMU_INVALID: Invalid type of IOMMU +* @IOMMU_AMD: AMD-Vi, whose per-instance state is struct iommu_amd_ser * * IOMMU type is stored in the IOMMU HW state to differentiate between various * IOMMU HWs.
@@ -82,6 +84,7 @@enum iommu_type_ser {IOMMU_INVALID ,IOMMU_INTEL , +IOMMU_AMD , };#define IOMMU_SER_FLAG_DELETED (1 << 0)
@@ -142,6 +145,29 @@ struct iommu_device_intel_ser {u64 max_pasid; } __packed; +/* +* Page-table mode of the domain a preserved AMD device was attached to. These +* are wire values with their own numbering rather than the kernel's +* enum protection_domain_mode, so reordering that enum cannot silently change +* the handoff format. +*/ +#define IOMMU_AMD_SER_PD_MODE_NONE 0 +#define IOMMU_AMD_SER_PD_MODE_V1 1 +#define IOMMU_AMD_SER_PD_MODE_V2 2 + +/** +* struct iommu_device_amd_ser - AMD specific state of serialized device +* @gcr3_tbl_phys: Physical address of the device's GCR3 table root, or 0 if +* the device has no PASID/GCR3 table. +* @gcr3_glx: Number of GCR3 table levels (0, 1, or 2; see amd_iommu_max_glx_val) +* @pd_mode: Page-table format of the domain, one of IOMMU_AMD_SER_PD_MODE_* +*/ +struct iommu_device_amd_ser { +u64 gcr3_tbl_phys; +u32 gcr3_glx; +u32 pd_mode; +} __packed; +/** * struct iommu_device_ser - Serialized state of a device * @hdr: Common object header
@@ -150,6 +176,8 @@ struct iommu_device_intel_ser {* @dma_owner_token: Token to identify the DMA owner of this device * @domain_iommu_ser: Domain and IOMMU mapping * @intel: Intel specific serialization data +* @amd: AMD-Vi per-device state, valid when the owning IOMMU record is +* of type IOMMU_AMD */ struct iommu_device_ser {struct iommu_hdr_ser hdr;
@@ -159,9 +187,18 @@ struct iommu_device_ser {struct iommu_dev_map_ser domain_iommu_ser;union {struct iommu_device_intel_ser intel; +struct iommu_device_amd_ser amd; }; } __packed; +/* +* The Intel arm is the larger one and so sets the size of the union, and with +* it the stride of the device array. Growing either arm changes that stride and +* needs a version bump. +*/ +static_assert (sizeof (struct iommu_device_amd_ser ) ==16 ); +static_assert (sizeof (struct iommu_device_intel_ser ) ==24 ); +/* There are maximum 256 buses, so maximum 512 context tables */ #define VTD_PRESERVED_BITMAP_LONGS DIV_ROUND_UP(512, BITS_PER_LONG_LONG)
@@ -181,12 +218,36 @@ struct iommu_intel_ser {u64 context_tables_bitmap[VTD_PRESERVED_BITMAP_LONGS ]; }; +/** +* struct iommu_amd_ser - Serialized state of an AMD IOMMU instance +* @restored: Whether IOMMU state is restored. Guards against double-restore. +* @mmio_phys: Physical address of the IOMMU MMIO register base. +* @dev_table_phys: Physical address of this IOMMU's PCI-segment device +* table (struct amd_iommu_pci_seg.dev_table). +* @dev_table_size: Size of the device table, in bytes. +* @pci_seg_id: PCI segment ID that owns the device table. +* @efr: Extended Feature Register bits (struct amd_iommu.features). +* Compared on restore; a mismatch is fatal. +* @efr2: Extended Feature Register 2 bits (struct amd_iommu.features2). +*/ +struct iommu_amd_ser { +u8 restored; +u8 padding[7 ]; +u64 mmio_phys; +u64 dev_table_phys; +u32 dev_table_size; +u32 pci_seg_id; +u64 efr; +u64 efr2; +} __packed; +/** * struct iommu_hw_ser - Serialized state of an IOMMU instance * @hdr: Common object header * @token: Unique token for the IOMMU * @type: IOMMU type serialized state belongs to * @intel: Intel specific serialization data +* @amd: AMD specific serialization data */ struct iommu_hw_ser {struct iommu_hdr_ser hdr;
@@ -194,9 +255,18 @@ struct iommu_hw_ser {u64 type;union {struct iommu_intel_ser intel; +struct iommu_amd_ser amd; }; } __packed; +/* +* The Intel arm is the larger one and so sets the size of the union, and with +* it the stride of the IOMMU array. Growing either arm changes that stride and +* needs a version bump. +*/ +static_assert (sizeof (struct iommu_amd_ser ) ==48 ); +static_assert (sizeof (struct iommu_intel_ser ) ==88 ); +/** * struct iommu_array_hdr_ser - Header for an array of serialized objects * @next_array_phys: Physical address of the next array of objects
Signature · 1 line
2.43.0
RFC PATCH 4/7 iommu/amd: preserve IOMMU and device state for live update
In reply to Ankit SoniImplement the .preserve and .preserve_device callbacks. Pin the PCI segment's device table for KHO and record each device's domain ID, page-table mode and GCR3 tree, so the next kernel can rebuild them.
The device table is shared by every IOMMU in a segment while the callbacks run once per IOMMU, so the pin is reference counted.
--- drivers/iommu/amd/Makefile | 1 + drivers/iommu/amd/amd_iommu.h | 12 ++ drivers/iommu/amd/amd_iommu_types.h | 14 ++ drivers/iommu/amd/iommu.c | 7 + drivers/iommu/amd/liveupdate.c | 281 ++++++++++++++++++++++++++++ 5 files changed, 315 insertions(+) create mode 100644 drivers/iommu/amd/liveupdate.c
drivers/iommu/amd/Makefile +1 -0
@@ -2,3 +2,4 @@ obj-y += iommu.o init.o quirks.o ppr.o pasid.o obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o +obj-$(CONFIG_IOMMU_LIVEUPDATE) += liveupdate.o
drivers/iommu/amd/amd_iommu.h +12 -0
@@ -225,4 +225,16 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entrystruct iommu_domain *amd_iommu_alloc_domain_nested (struct iommufd_viommu *viommu,u32 flags,const struct iommu_user_data *user_data); + +#ifdef CONFIG_IOMMU_LIVEUPDATE +/* LIVE UPDATE (drivers/iommu/amd/liveupdate.c) */ +int amd_iommu_preserve (struct iommu_device *iommu_dev, +struct iommu_hw_ser *ser); +void amd_iommu_unpreserve (struct iommu_device *iommu_dev, +struct iommu_hw_ser *ser); +int amd_iommu_preserve_device (struct device *dev, +struct iommu_device_ser *device_ser); +void amd_iommu_unpreserve_device (struct device *dev, +struct iommu_device_ser *device_ser); +#endif /* CONFIG_IOMMU_LIVEUPDATE */ #endif /* AMD_IOMMU_H */
drivers/iommu/amd/amd_iommu_types.h +14 -0
@@ -596,6 +596,20 @@ struct amd_iommu_pci_seg {*/ struct dev_table_entry *dev_table; +#ifdef CONFIG_IOMMU_LIVEUPDATE +/* +* The device table is shared by every IOMMU in this PCI segment, but +* the live-update .preserve callback runs once per IOMMU, and each of +* those IOMMUs is preserved and unpreserved independently by the core. +* KHO preservation is not refcounted, so the references are counted +* here and the shared pages are pinned on the first and unpinned only +* on the last -- otherwise one IOMMU being unpreserved would drop the +* pin from underneath the devices still preserved behind every other +* IOMMU in the segment. +*/ +unsigned int dev_table_preserve_count; +#endif +/* * The rlookup iommu table is used to find the IOMMU which is * responsible for a specific device. It is indexed by the PCI
drivers/iommu/amd/iommu.c +7 -0
@@ -32,6 +32,7 @@#include <linux/percpu.h> #include <linux/cc_platform.h> #include <linux/crash_dump.h> +#include <linux/iommu-liveupdate.h> #include <asm/irq_remapping.h> #include <asm/io_apic.h> #include <asm/apic.h>
@@ -3221,6 +3222,12 @@ const struct iommu_ops amd_iommu_ops = { .page_response = amd_iommu_page_response, .get_viommu_size = amd_iommufd_get_viommu_size, .viommu_init = amd_iommufd_viommu_init, +#ifdef CONFIG_IOMMU_LIVEUPDATE + .preserve_device = amd_iommu_preserve_device, + .unpreserve_device = amd_iommu_unpreserve_device, + .preserve = amd_iommu_preserve, + .unpreserve = amd_iommu_unpreserve, +#endif };#ifdef CONFIG_IRQ_REMAP
drivers/iommu/amd/liveupdate.c new file +281 -0
281 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH 6/7 iommu/amd: restore preserved state on a live-update boot
In reply to Ankit SoniAdopt the preserved device table for the PCI segment instead of allocating a fresh one, and reserve the domain IDs it already carries so a domain allocated by this kernel cannot be handed an ID the hardware is still tagging cache entries with.
Also stop clearing the enable bit on a unit that was handed over translating. Doing so would drop its devices into untranslated passthrough while their DMA is still in flight.
--- drivers/iommu/amd/amd_iommu.h | 7 ++ drivers/iommu/amd/init.c | 165 ++++++++++++++++++++++++++------- drivers/iommu/amd/liveupdate.c | 33 +++++++ 3 files changed, 174 insertions(+), 31 deletions(-)
drivers/iommu/amd/amd_iommu.h +7 -0
@@ -237,9 +237,16 @@ int amd_iommu_preserve_device(struct device *dev,void amd_iommu_unpreserve_device (struct device *dev,struct iommu_device_ser *device_ser);void amd_iommu_clear_unpreserved_dtes (struct amd_iommu *iommu); +void amd_iommu_restore_dev_table (struct amd_iommu *iommu, +struct iommu_hw_ser *ser);#else static inline void amd_iommu_clear_unpreserved_dtes (struct amd_iommu *iommu) { } + +static inline void amd_iommu_restore_dev_table (struct amd_iommu *iommu, +struct iommu_hw_ser *ser) +{ +}#endif /* CONFIG_IOMMU_LIVEUPDATE */ #endif /* AMD_IOMMU_H */
drivers/iommu/amd/init.c +134 -31
249 lines not on this page · raw message
drivers/iommu/amd/liveupdate.c +33 -0
@@ -362,3 +362,36 @@ void amd_iommu_clear_unpreserved_dtes(struct amd_iommu *iommu)amd_iommu_flush_all_caches (iommu); } + +void amd_iommu_restore_dev_table (struct amd_iommu *iommu, +struct iommu_hw_ser *ser) +{ +struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; + +if (ser->amd.dev_table_size != pci_seg->dev_table_size) +panic ("AMD-Vi: IOMMU:%d preserved device table size mismatch (0x%x vs 0x%x)\n" , + iommu->index, ser->amd.dev_table_size, + pci_seg->dev_table_size); + +if (ser->amd.pci_seg_id != pci_seg->id) +panic ("AMD-Vi: IOMMU:%d preserved PCI segment mismatch (%u vs %u)\n" , + iommu->index, ser->amd.pci_seg_id, pci_seg->id); + +if (ser->amd.efr != iommu->features || + ser->amd.efr2 != iommu->features2) +panic ("AMD-Vi: IOMMU:%d preserved feature registers mismatch (EFR 0x%llx/0x%llx vs 0x%llx/0x%llx)\n" , + iommu->index, ser->amd.efr, ser->amd.efr2, + iommu->features, iommu->features2); + +/* +* Reclaim the folio from KHO the first time this segment's table is +* seen, then adopt it. Later IOMMUs in the same segment reference the +* same physical table and must not restore it again. +*/ +if (!ser->amd.restored) { +iommu_restore_pages (ser->amd.dev_table_phys); + ser->amd.restored =1 ; + } + + pci_seg->old_dev_tbl_cpy =__va (ser->amd.dev_table_phys); +}
Signature · 1 line
2.43.0
RFC PATCH 5/7 iommu/amd: clear unpreserved DTEs and quiesce logs at live-update shutdown
In reply to Ankit SoniKeep translation enabled on the units carrying preserved devices, but block every device that was not preserved, so the next kernel cannot translate through page tables it did not inherit.
Then stop the command, event, PPR and GA engines and wait for each to report itself idle. Those buffers are not preserved, so an engine still running would write into pages the next kernel is free to reuse.
If an engine does not go idle within the timeout, panic rather than continue. Completing the handover would leave a running DMA engine writing into pages the next kernel owns, and that corruption is both silent and impossible to attribute later. Failing the live update is the lesser harm.
--- drivers/iommu/amd/amd_iommu.h | 5 ++ drivers/iommu/amd/init.c | 88 +++++++++++++++++++++++++++++++++- drivers/iommu/amd/liveupdate.c | 83 ++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu.h +5 -0
@@ -236,5 +236,10 @@ int amd_iommu_preserve_device(struct device *dev, struct iommu_device_ser *device_ser);void amd_iommu_unpreserve_device (struct device *dev,struct iommu_device_ser *device_ser); +void amd_iommu_clear_unpreserved_dtes (struct amd_iommu *iommu); +#else +static inline void amd_iommu_clear_unpreserved_dtes (struct amd_iommu *iommu) +{ +}#endif /* CONFIG_IOMMU_LIVEUPDATE */ #endif /* AMD_IOMMU_H */
drivers/iommu/amd/init.c +87 -1
106 lines not on this page · raw message
drivers/iommu/amd/liveupdate.c +83 -0
86 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH 7/7 iommu/amd: reattach preserved devices to their restored domains
In reply to Ankit SoniA device that rode across the kexec is already translating through the DTE this kernel adopted, so adopt its domain ID and GCR3 table instead of programming the DTE again, and verify the adopted state against the live DTE. A mismatch means the kernel and the hardware disagree about how the device's translations are cached, so fail the attach and leave the DTE alone.
Refuse every other attach for such a device, in all four attach ops, because the blocking, identity, nested and freshly built paging domains would each rewrite that DTE under a device that is still doing DMA. The paging path compares the target against the domain recorded for this device, so a second restored domain cannot be swapped in either, and clone_alias() is skipped for the same reason.
On device removal the core skips the release domain for a preserved device, so detach_device() never runs and dev_data->domain stays set. Drop the software state and leave the DTE alone; the restored domain outlives the device.
--- drivers/iommu/amd/amd_iommu.h | 10 +++ drivers/iommu/amd/iommu.c | 130 ++++++++++++++++++++++++++++- drivers/iommu/amd/liveupdate.c | 146 +++++++++++++++++++++++++++++++++ drivers/iommu/amd/nested.c | 8 ++ 4 files changed, 292 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +10 -0
@@ -239,6 +239,9 @@ void amd_iommu_unpreserve_device(struct device *dev,void amd_iommu_clear_unpreserved_dtes (struct amd_iommu *iommu);void amd_iommu_restore_dev_table (struct amd_iommu *iommu,struct iommu_hw_ser *ser); +int amd_iommu_reattach_device (struct device *dev, +struct protection_domain *domain, +struct iommu_device_ser *device_ser);#else static inline void amd_iommu_clear_unpreserved_dtes (struct amd_iommu *iommu) {
@@ -248,5 +251,12 @@ static inline void amd_iommu_restore_dev_table(struct amd_iommu *iommu, struct iommu_hw_ser *ser) { } + +static inline int amd_iommu_reattach_device (struct device *dev, +struct protection_domain *domain, +struct iommu_device_ser *device_ser) +{ +return -EOPNOTSUPP ; +}#endif /* CONFIG_IOMMU_LIVEUPDATE */ #endif /* AMD_IOMMU_H */
drivers/iommu/amd/iommu.c +128 -2
190 lines not on this page · raw message
drivers/iommu/amd/liveupdate.c +146 -0
149 lines not on this page · raw message
drivers/iommu/amd/nested.c +8 -0
@@ -6,6 +6,7 @@#define dev_fmt (fmt) "AMD-Vi: " fmt#include <linux/iommu.h> +#include <linux/iommu-liveupdate.h> #include <linux/refcount.h> #include <uapi/linux/iommufd.h>
@@ -248,6 +249,13 @@ static int nested_attach_device(struct iommu_domain *dom, struct device *dev,if (WARN_ON (dev_data->pasid_enabled))return -EINVAL ; +/* +* A preserved device is still translating through the DTE this kernel +* adopted, and a nested domain is never the domain restored for it. +*/ +if (dev_iommu_restored_state (dev)) +return -EBUSY ; +mutex_lock (&dev_data->mutex);set_dte_nested (iommu, dom, dev_data, &new);
Signature · 1 line
2.43.0
Mike Rapoport
maintainerRe: [RFC PATCH 2/7] liveupdate: parse the incoming handover tree before late_time_init()
In reply to Ankit SoniHi,
Quoted 6 lines · Ankit Soni
LUO parses the handover tree from an
early_initcall, which runs insiderest_init()-- the last statement ofstart_kernel(). Anything earlier instart_kernel()cannot see the incoming state, and cannot tell that it cannot see it:luo_flb_retrieve_one()returns-ENODATAboth before the parse and when the previous kernel handed nothing over.
Quoted 7 lines
On x86 the IOMMU is such a consumer, because it is also the interrupt remapping provider.
amd_iommu_prepare()runs fromlate_time_init()viaenable_IR_x2apic()->irq_remapping_prepare(), and reachesinit_iommu_one_late()long before any initcall.Fix the ordering rather than teaching each consumer to cope. Call the parse directly from
start_kernel(), immediately beforelate_time_init().
Why do you suggest to call it exactly here?
Order of the calls in start_kernel() is quite sensitive and placing a call in a certain place needs more elaborate explanation.
Quoted 3 lines
Rename it to
liveupdate_init_early()at the same time, sinceliveupdate_early_init()was named after theearly_initcallthat no longer exists.
No strong feeling about this, but we do have other _early_init() functions unrelated to initcalls.
Quoted 6 lines
Signed-off-by Ankit Soni <[email protected]>--- include/linux/liveupdate.h | 4 ++++ init/main.c | 2 ++ kernel/liveupdate/luo_core.c | 5 +---- 3 files changed, 7 insertions(+), 4 deletions(-)
Signature · 2 lines
Sincerely yours, Mike.
Re: [RFC PATCH 2/7] liveupdate: parse the incoming handover tree before late_time_init()
Hi Mike
Quoted 14 lines · Mike Rapoport
Hi,
Quoted 11 lines · Ankit Soni
LUO parses the handover tree from an
early_initcall, which runs insiderest_init()-- the last statement ofstart_kernel(). Anything earlier instart_kernel()cannot see the incoming state, and cannot tell that it cannot see it:luo_flb_retrieve_one()returns-ENODATAboth before the parse and when the previous kernel handed nothing over.On x86 the IOMMU is such a consumer, because it is also the interrupt remapping provider.
amd_iommu_prepare()runs fromlate_time_init()viaenable_IR_x2apic()->irq_remapping_prepare(), and reachesinit_iommu_one_late()long before any initcall.
Quoted 7 lines
Quoted 2 lines
Fix the ordering rather than teaching each consumer to cope. Call the parse directly from
start_kernel(), immediately beforelate_time_init().Why do you suggest to call it exactly here?
Order of the calls instart_kernel()is quite sensitive and placing a call in a certain place needs more elaborate explanation.
Fair, the commit message asserts the placement without justifying it elaborately. The slot is bounded on both sides, and within those bounds I took the latest point. I will fold commit message in respin.
Earliest:
luo_early_startup() ends in kho_restore_free(), which goes through kho_restore_folio() and folio_put(), so it needs struct page and a working page allocator. It also reads a preserved page, and kho_memory_init() is what calls kho_mem_retrieve() to take those pages out of circulation; that runs from mm_core_init(). Reading the handover FDT is not a constraint, as kho_populate() records it from setup_arch() via parse_setup_data().
Second bound: luo_restore_fail() is a panic(), so running before console_init() would panic a failed handover with nothing on the console, which is exactly when the message is wanted.
Latest: on x86 the IOMMU is the interrupt remapping provider, so both halves
of its early init run inside late_time_init():
x86_late_time_init() -> apic_intr_mode_init() -> x86_64_probe_apic()
-> enable_IR_x2apic()
-> irq_remapping_prepare() -> amd_iommu_prepare()
-> early_amd_iommu_init()
init_iommu_one_late(), __disable_iommus(true)
-> irq_remapping_enable() -> amd_iommu_enable()
-> early_enable_iommus() -> reuse_device_table()All three sites that ask whether this unit was handed over sit on that path, and all of it is before any initcall.
So the window is between console_init() and late_time_init(). I placed the call at the end of it, on the grounds that it perturbs the least: every existing call in that window keeps running exactly as before, and the only code that can observe the new call is late_time_init() and later. Nothing in the window consumes LUO state, so moving the call earlier would widen the exposure without buying anything.
Anywhere in that window works for the AMD case, so if you would rather see it somewhere else, I am happy to move it.
Quoted 7 lines
Quoted 3 lines
Rename it to
liveupdate_init_early()at the same time, sinceliveupdate_early_init()was named after theearly_initcallthat no longer exists.No strong feeling about this, but we do have other
_early_init()functions unrelated to initcalls.
Will drop this.
Thanks,
Ankit
Quoted 10 lines
Quoted 6 lines
Signed-off-by Ankit Soni <[email protected]>--- include/linux/liveupdate.h | 4 ++++ init/main.c | 2 ++ kernel/liveupdate/luo_core.c | 5 +---- 3 files changed, 7 insertions(+), 4 deletions(-)Signature · 2 lines
Sincerely yours, Mike.