IOMMU: intel_iommu support map and unmap notifications
v7 cover IOMMU: intel_iommu support map and unmap notifications
From: "Aviv Ben-David" <[email protected]>
* Advertize Cache Mode capability in iommu cap register.
This capability is controlled by "cache-mode" property of intel-iommu device. To enable this option call QEMU with "-device intel-iommu,cache-mode=true".
* On page cache invalidation in intel vIOMMU, check if the domain belong to registered notifier, and notify accordingly.
Currently this patch still doesn't enabling VFIO devices support with vIOMMU present. Current problems:
* vfio_iommu_map_notify is not aware about memory range belong to specific VFIOGuestIOMMU.
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.
Changes from v1 to v2:
* remove assumption that the cache do not clears
* fix lockup on high load.
Changes from v2 to v3:
* remove debug leftovers
* split to sepearate commits
* change is_write to flags in vtd_do_iommu_translate, add IOMMU_NO_FAIL to suppress error propagating to guest.
Changes from v3 to v4:
* Add property to intel_iommu device to control the CM capability, default to False.
* Use s->iommu_ops.notify_flag_changed to register notifiers.
Changes from v4 to v4 RESEND:
* Fix codding style pointed by checkpatch.pl script.
Changes from v4 to v5:
* Reduce the number of changes in patch 2 and make flags real bitfield.
* Revert deleted debug prints.
* Fix memory leak in patch 3.
Changes from v5 to v6:
* fix prototype of iommu_translate function for more IOMMU types.
* VFIO will be notified only on the difference, without unmap before change to maps.
Changes from v6 to v7:
* Add replay op to iommu_ops, with current behavior as default implementation (Patch 4).
* Add stub to disable VFIO with intel_iommu support (Patch 5).
* Cosmetic changes to other patches.
Aviv Ben-David (5):
IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to
guest
IOMMU: change iommu_op->translate's is_write to flags, add support to
NO_FAIL flag mode
IOMMU: enable intel_iommu map and unmap notifiers
IOMMU: add specific replay function with default implemenation
IOMMU: add specific null implementation of iommu_replay to intel_iommu
exec.c | 3 +- hw/alpha/typhoon.c | 2 +- hw/i386/amd_iommu.c | 4 +- hw/i386/intel_iommu.c | 165 ++++++++++++++++++++++++++++++++++------- hw/i386/intel_iommu_internal.h | 3 + hw/pci-host/apb.c | 2 +- hw/ppc/spapr_iommu.c | 2 +- hw/s390x/s390-pci-bus.c | 2 +- include/exec/memory.h | 10 ++- include/hw/i386/intel_iommu.h | 11 +++ memory.c | 11 ++- 11 files changed, 177 insertions(+), 38 deletions(-)
Signature · 1 line
1.9.1
PATCH v7 1/5 IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to guest
From: "Aviv Ben-David" <[email protected]>
This capability asks the guest to invalidate cache before each map operation. We can use this invalidation to trap map operations in the hypervisor.
--- hw/i386/intel_iommu.c | 5 +++++ hw/i386/intel_iommu_internal.h | 1 + include/hw/i386/intel_iommu.h | 2 ++ 3 files changed, 8 insertions(+)
hw/i386/intel_iommu.c +5 -0
@@ -2017,6 +2017,7 @@ static Property vtd_properties[] = {DEFINE_PROP_ON_OFF_AUTO ("eim" , IntelIOMMUState, intr_eim,ON_OFF_AUTO_AUTO ),DEFINE_PROP_BOOL ("x-buggy-eim" , IntelIOMMUState, buggy_eim, false), +DEFINE_PROP_BOOL ("cache-mode" , IntelIOMMUState, cache_mode_enabled, FALSE), DEFINE_PROP_END_OF_LIST(), };
@@ -2391,6 +2392,10 @@ static void vtd_init(IntelIOMMUState *s)assert (s->intr_eim !=ON_OFF_AUTO_AUTO ); } +if (s->cache_mode_enabled) { + s->cap |=VTD_CAP_CM ; + } +vtd_reset_context_cache (s);vtd_reset_iotlb (s);
hw/i386/intel_iommu_internal.h +1 -0
@@ -201,6 +201,7 @@#define VTD_CAP_MAMV (VTD_MAMV << 48)#define VTD_CAP_PSI (1ULL << 39)#define VTD_CAP_SLLPS ((1ULL << 34) | (1ULL << 35)) +#define VTD_CAP_CM (1ULL << 7)/* Supported Adjusted Guest Address Widths */ #define VTD_CAP_SAGAW_SHIFT 8
include/hw/i386/intel_iommu.h +2 -0
@@ -257,6 +257,8 @@ struct IntelIOMMUState {uint8_t womask[DMAR_REG_SIZE ];/* WO (write only - read returns 0) */ uint32_t version; +bool cache_mode_enabled;/* RO - is cap CM enabled? */ +dma_addr_t root;/* Current root table pointer */ bool root_extended;/* Type of root table (extended or not) */ bool dmar_enabled;/* Set if DMA remapping is enabled */
Signature · 1 line
1.9.1
PATCH v7 2/5 IOMMU: change iommu_op->translate's is_write to flags, add support to NO_FAIL flag mode
In reply to Aviv B.DFrom: "Aviv Ben-David" <[email protected]>
Supports translation trials without reporting error to guest on translation failure.
--- exec.c | 3 ++- hw/alpha/typhoon.c | 2 +- hw/i386/amd_iommu.c | 4 ++-- hw/i386/intel_iommu.c | 59 +++++++++++++++++++++++++++++++------------------ hw/pci-host/apb.c | 2 +- hw/ppc/spapr_iommu.c | 2 +- hw/s390x/s390-pci-bus.c | 2 +- include/exec/memory.h | 6 +++-- memory.c | 3 ++- 9 files changed, 52 insertions(+), 31 deletions(-)
exec.c +2 -1
@@ -466,7 +466,8 @@ MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,break ; } - iotlb = mr->iommu_ops->translate (mr, addr, is_write); + iotlb = mr->iommu_ops->translate (mr, addr, + is_write ?IOMMU_WO :IOMMU_RO ); addr = ((iotlb.translated_addr & ~iotlb.addr_mask) | (addr & iotlb.addr_mask)); *plen =MIN (*plen, (addr | iotlb.addr_mask) - addr +1 );
hw/alpha/typhoon.c +1 -1
@@ -664,7 +664,7 @@ static bool window_translate(TyphoonWindow *win, hwaddr addr,/* TODO: A translation failure here ought to set PCI error codes on the Pchip and generate a machine check interrupt. */ static IOMMUTLBEntry typhoon_translate_iommu (MemoryRegion *iommu,hwaddr addr, -bool is_write) +IOMMUAccessFlags flags) {TyphoonPchip *pchip =container_of (iommu, TyphoonPchip, iommu);IOMMUTLBEntry ret;
hw/i386/amd_iommu.c +2 -2
@@ -988,7 +988,7 @@ static inline bool amdvi_is_interrupt_addr(hwaddr addr) }static IOMMUTLBEntry amdvi_translate (MemoryRegion *iommu,hwaddr addr, -bool is_write) +IOMMUAccessFlags flags) {AMDVIAddressSpace *as =container_of (iommu, AMDVIAddressSpace, iommu);AMDVIState *s = as->iommu_state;
@@ -1017,7 +1017,7 @@ static IOMMUTLBEntry amdvi_translate(MemoryRegion *iommu, hwaddr addr,return ret; } -amdvi_do_translate (as, addr, is_write, &ret); +amdvi_do_translate (as, addr, flags &IOMMU_WO , &ret);trace_amdvi_translation_result (as->bus_num,PCI_SLOT (as->devfn),PCI_FUNC (as->devfn), addr, ret.translated_addr);return ret;
hw/i386/intel_iommu.c +38 -21
133 lines not on this page · raw message
hw/pci-host/apb.c +1 -1
@@ -209,7 +209,7 @@ static AddressSpace *pbm_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)/* Called from RCU critical section */ static IOMMUTLBEntry pbm_translate_iommu (MemoryRegion *iommu,hwaddr addr, -bool is_write) +IOMMUAccessFlags flags) {IOMMUState *is =container_of (iommu, IOMMUState, iommu);hwaddr baseaddr, offset;
hw/ppc/spapr_iommu.c +1 -1
@@ -110,7 +110,7 @@ static void spapr_tce_free_table(uint64_t *table, int fd, uint32_t nb_table)/* Called from RCU critical section */ static IOMMUTLBEntry spapr_tce_translate_iommu (MemoryRegion *iommu,hwaddr addr, -bool is_write) +IOMMUAccessFlags flags) {sPAPRTCETable *tcet =container_of (iommu, sPAPRTCETable, iommu);uint64_t tce;
hw/s390x/s390-pci-bus.c +1 -1
@@ -378,7 +378,7 @@ out: }static IOMMUTLBEntry s390_translate_iommu (MemoryRegion *iommu,hwaddr addr, -bool is_write) +IOMMUAccessFlags flags) {uint64_t pte;uint32_t flags;
include/exec/memory.h +4 -2
@@ -57,6 +57,7 @@ typedef enum {IOMMU_RO =1 ,IOMMU_WO =2 ,IOMMU_RW =3 , +IOMMU_NO_FAIL =4 ,/* may not be present, don't repport error to guest */ } IOMMUAccessFlags; struct IOMMUTLBEntry {
@@ -168,10 +169,11 @@ struct MemoryRegionOps { };typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps ; -struct MemoryRegionIOMMUOps {/* Return a TLB entry that contains a given address. */ -IOMMUTLBEntry (*translate)(MemoryRegion *iommu,hwaddr addr,bool is_write); +IOMMUTLBEntry (*translate)(MemoryRegion *iommu, +hwaddr addr, +IOMMUAccessFlags flags);/* Returns minimum supported page size */ uint64_t (*get_min_page_size)(MemoryRegion *iommu);/* Called when IOMMU Notifier flag changed */
memory.c +2 -1
@@ -1627,7 +1627,8 @@ void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n, granularity =memory_region_iommu_get_min_page_size (mr);for (addr =0 ; addr <memory_region_size (mr); addr += granularity) { - iotlb = mr->iommu_ops->translate (mr, addr, is_write); + iotlb = mr->iommu_ops->translate (mr, addr, + is_write ?IOMMU_WO :IOMMU_RO );if (iotlb.perm !=IOMMU_NONE ) { n->notify (n, &iotlb); }
Signature · 1 line
1.9.1
PATCH v7 3/5 IOMMU: enable intel_iommu map and unmap notifiers
In reply to Aviv B.DFrom: "Aviv Ben-David" <[email protected]>
Adds a list of registered vtd_as's to intel iommu state to save iteration over each PCI device in a search of the corrosponding domain.
--- hw/i386/intel_iommu.c | 94 ++++++++++++++++++++++++++++++++++++++---- hw/i386/intel_iommu_internal.h | 2 + include/hw/i386/intel_iommu.h | 9 ++++ 3 files changed, 98 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +86 -7
@@ -679,7 +679,7 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa, } *reads = (*reads) && (slpte &VTD_SL_R ); *writes = (*writes) && (slpte &VTD_SL_W ); -if (!(slpte & access_right_check)) { +if (!(slpte & access_right_check) && !(flags &IOMMU_NO_FAIL )) {VTD_DPRINTF (GENERAL ,"error: lack of %s permission for " "gpa 0x%" PRIx64" slpte 0x%" PRIx64, (flags &IOMMU_WO ?"write" :"read" ), gpa, slpte);
@@ -978,6 +978,23 @@ static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)return vtd_bus; } +static int vtd_get_did_dev (IntelIOMMUState *s,uint8_t bus_num,uint8_t devfn, +uint16_t *domain_id) +{ +VTDContextEntry ce; +int ret_fr; + +assert (domain_id); + + ret_fr =vtd_dev_to_context_entry (s, bus_num, devfn, &ce); +if (ret_fr) { +return -1 ; + } + + *domain_id =VTD_CONTEXT_ENTRY_DID (ce.hi); +return 0 ; +} +/* Do a context-cache device-selective invalidation. * @func_mask: FM field after shifting */
@@ -1064,6 +1081,45 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) &domain_id); } +static void vtd_iotlb_page_invalidate_notify (IntelIOMMUState *s, +uint16_t domain_id,hwaddr addr, +uint8_t am) +{ +IntelIOMMUNotifierNode *node; + + QLIST_FOREACH(node, &(s->notifiers_list), next) { +VTDAddressSpace *vtd_as = node->vtd_as; +uint16_t vfio_domain_id; +int ret =vtd_get_did_dev (s,pci_bus_num (vtd_as->bus), vtd_as->devfn, + &vfio_domain_id); + +if (!ret && domain_id == vfio_domain_id) { +hwaddr original_addr = addr; + +while (addr < original_addr + (1 << am) *VTD_PAGE_SIZE ) { +IOMMUTLBEntry entry = s->iommu_ops.translate ( + &node->vtd_as->iommu, + addr, +IOMMU_NO_FAIL ); + +if (entry.perm ==IOMMU_NONE && + node->notifier_flag &IOMMU_NOTIFIER_UNMAP ) { + entry.target_as = &address_space_memory; + entry.iova = addr &VTD_PAGE_MASK_4K ; + entry.translated_addr =0 ; + entry.addr_mask = ~VTD_PAGE_MASK (VTD_PAGE_SHIFT ); +memory_region_notify_iommu (&node->vtd_as->iommu, entry); + addr +=VTD_PAGE_SIZE ; + }else if (node->notifier_flag &IOMMU_NOTIFIER_MAP ) { +memory_region_notify_iommu (&node->vtd_as->iommu, entry); + addr += entry.addr_mask +1 ; + } + } + } + } +} +static void vtd_iotlb_page_invalidate (IntelIOMMUState *s,uint16_t domain_id,hwaddr addr,uint8_t am) {
@@ -1074,6 +1130,8 @@ static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id, info.addr = addr; info.mask = ~((1 << am) -1 );g_hash_table_foreach_remove (s->iotlb, vtd_hash_remove_by_page, &info); + +vtd_iotlb_page_invalidate_notify (s, domain_id, addr, am); }/* Flush IOTLB
@@ -1999,15 +2057,37 @@ static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, IOMMUNotifierFlag new) {VTDAddressSpace *vtd_as =container_of (iommu, VTDAddressSpace, iommu); +IntelIOMMUState *s = vtd_as->iommu_state; +IntelIOMMUNotifierNode *node =NULL ; +IntelIOMMUNotifierNode *next_node =NULL ; -if (new &IOMMU_NOTIFIER_MAP ) { -error_report ("Device at bus %s addr %02x.%d requires iommu " -"notifier which is currently not supported by " -"intel-iommu emulation" , - vtd_as->bus->qbus.name,PCI_SLOT (vtd_as->devfn), -PCI_FUNC (vtd_as->devfn)); +if (!s->cache_mode_enabled && new &IOMMU_NOTIFIER_MAP ) { +error_report ("We need to set cache_mode=1 for intel-iommu to enable " +"device assignment with IOMMU protection." );exit (1 ); } + +/* Add new ndoe if no mapping was exising before this call */ +if (old ==IOMMU_NOTIFIER_NONE ) { + node =g_malloc0 (sizeof (*node)); + node->vtd_as = vtd_as; + node->notifier_flag = new; +QLIST_INSERT_HEAD (&s->notifiers_list, node, next); +return ; + } + +/* update notifier node with new flags */ + QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) { +if (node->vtd_as == vtd_as) { +if (new ==IOMMU_NOTIFIER_NONE ) { +QLIST_REMOVE (node, next); +g_free (node); + }else { + node->notifier_flag = new; + } +return ; + } + } }static const VMStateDescription vtd_vmstate = {
hw/i386/intel_iommu_internal.h +2 -0
@@ -381,6 +381,8 @@ typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;#define VTD_PAGE_SHIFT_1G 30#define VTD_PAGE_MASK_1G (~((1ULL << VTD_PAGE_SHIFT_1G) - 1)) +#define VTD_PAGE_MASK (shift) (~((1ULL << (shift)) - 1)) +struct VTDRootEntry {uint64_t val;uint64_t rsvd;
include/hw/i386/intel_iommu.h +9 -0
@@ -63,6 +63,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress ;typedef struct VTDIrq VTDIrq ;typedef struct VTD_MSIMessage VTD_MSIMessage ; +typedef struct IntelIOMMUNotifierNode IntelIOMMUNotifierNode ;/* Context-Entry */ struct VTDContextEntry {
@@ -247,6 +248,12 @@ struct VTD_MSIMessage {/* When IR is enabled, all MSI/MSI-X data bits should be zero */ #define VTD_IR_MSI_DATA (0) +struct IntelIOMMUNotifierNode { +VTDAddressSpace *vtd_as; +IOMMUNotifierFlag notifier_flag; +QLIST_ENTRY (IntelIOMMUNotifierNode) next; +}; +/* The iommu (DMAR) device state struct */ struct IntelIOMMUState {X86IOMMUState x86_iommu;
@@ -284,6 +291,8 @@ struct IntelIOMMUState {MemoryRegionIOMMUOps iommu_ops;GHashTable *vtd_as_by_busptr;/* VTDBus objects indexed by PCIBus* reference */ VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX ];/* VTDBus objects indexed by bus number */ +/* list of registered notifiers */ + QLIST_HEAD(, IntelIOMMUNotifierNode) notifiers_list;/* interrupt remapping */ bool intr_enabled;/* Whether guest enabled IR */
Signature · 1 line
1.9.1
PATCH v7 4/5 IOMMU: add specific replay function with default implemenation
In reply to Aviv B.DFrom: "Aviv Ben-David" <[email protected]>
The default implementation scans the address space and try to find translation for each page, if exists.
This callback enables effiecent implementation for intel_iommu and other subsystems with large address space.
--- include/exec/memory.h | 4 ++++ memory.c | 8 ++++++++ 2 files changed, 12 insertions(+)
include/exec/memory.h +4 -0
@@ -180,6 +180,10 @@ struct MemoryRegionIOMMUOps {void (*notify_flag_changed)(MemoryRegion *iommu,IOMMUNotifierFlag old_flags,IOMMUNotifierFlag new_flags); +/* Called when region has been moved between iommu domains */ +void (*replay)(MemoryRegion *mr, +IOMMUNotifier *n, +bool is_write); };typedef struct CoalescedMemoryRange CoalescedMemoryRange ;
memory.c +8 -0
@@ -1624,6 +1624,14 @@ void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n,hwaddr addr, granularity;IOMMUTLBEntry iotlb; +/* If there is specific implementation, use it */ +assert (memory_region_is_iommu (mr)); +if (mr->iommu_ops && mr->iommu_ops->replay) { +return mr->iommu_ops->replay (mr, n, is_write); + } + +/* No specific implementation for this iommu, fall back to default +* implementation */ granularity =memory_region_iommu_get_min_page_size (mr);for (addr =0 ; addr <memory_region_size (mr); addr += granularity) {
Signature · 1 line
1.9.1
PATCH v7 5/5 IOMMU: add specific null implementation of iommu_replay to intel_iommu
In reply to Aviv B.DFrom: "Aviv Ben-David" <[email protected]>
Currently the implementation preventing VFIO to work together with intel_iommu.
--- hw/i386/intel_iommu.c | 7 +++++++ 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +7 -0
@@ -2453,6 +2453,12 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)return vtd_dev_as; } +static void vtd_iommu_replay (MemoryRegion *mr,IOMMUNotifier *n, +bool is_write){ +error_report ("VFIO use with intel_iommu is currently not supported." ); +exit (1 ); +} +/* Do the initialization. It will also be called when reset, so pay * attention when adding new initialization stuff. */
@@ -2467,6 +2473,7 @@ static void vtd_init(IntelIOMMUState *s) s->iommu_ops.translate = vtd_iommu_translate; s->iommu_ops.notify_flag_changed = vtd_iommu_notify_flag_changed; + s->iommu_ops.replay = vtd_iommu_replay; s->root =0 ; s->root_extended = false; s->dmar_enabled = false;
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH v7 5/5] IOMMU: add specific null implementation of iommu_replay to intel_iommu
Quoted 15 lines · On Mon, 28 Nov 2016 17:51:55 +0200 "Aviv B.D" <[email protected]> wrote:
From: "Aviv Ben-David" <[email protected]>
Currently the implementation preventing VFIO to work together with
intel_iommu.Signed-off-by Aviv Ben-David <[email protected]>--- hw/i386/intel_iommu.c | 7 +++++++ 1 file changed, 7 insertions(+)hw/i386/intel_iommu.c +0
-0
hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn) · line 2458
Quoted 6 lines
hw/i386/intel_iommu.c +3
-0@@ -2453,6 +2453,12 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)return vtd_dev_as; } +static void vtd_iommu_replay (MemoryRegion *mr,IOMMUNotifier *n, +bool is_write){ +error_report ("VFIO use with intel_iommu is currently not supported." );
It doesn't make sense to call out vfio here, this is a deficiency of intel-iommu, not vfio. vfio is just trying to use QEMU's iommu api. vfio is currently the only caller, but that could change and just adds maintenance effort to scrub this error message at a later time. We could make the whole path support an error return to allow vfio to report the error, we could add a name field in the IOMMUNotifier, or we could just make the error more generic. Thanks,
Alex
Quoted 14 lines
hw/i386/intel_iommu.c +4
-0+exit (1 ); +} +/* Do the initialization. It will also be called when reset, so pay * attention when adding new initialization stuff. */ @@ -2467,6 +2473,7 @@ static void vtd_init(IntelIOMMUState *s) s->iommu_ops.translate = vtd_iommu_translate; s->iommu_ops.notify_flag_changed = vtd_iommu_notify_flag_changed; + s->iommu_ops.replay = vtd_iommu_replay; s->root =0 ; s->root_extended = false; s->dmar_enabled = false;
Re: [Qemu-devel] [PATCH v7 5/5] IOMMU: add specific null implementation of iommu_replay to intel_iommu
Quoted 35 lines · Alex Williamson
hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState
Quoted 15 lines · On Mon, 28 Nov 2016 17:51:55 +0200 "Aviv B.D" <[email protected]> wrote:
From: "Aviv Ben-David" <[email protected]>
Currently the implementation preventing VFIO to work together with
intel_iommu.Signed-off-by Aviv Ben-David <[email protected]>--- hw/i386/intel_iommu.c | 7 +++++++ 1 file changed, 7 insertions(+)hw/i386/intel_iommu.c +0
-0@@ -2453,6 +2453,12 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState*s, PCIBus *bus, int devfn)
hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)
Quoted 6 lines
hw/i386/intel_iommu.c +3
-0return vtd_dev_as; } +static void vtd_iommu_replay (MemoryRegion *mr,IOMMUNotifier *n, +bool is_write){ + error_report("VFIO use with intel_iommu is currently notsupported.");
It doesn't make sense to call out vfio here, this is a deficiency of intel-iommu, not vfio. vfio is just trying to use QEMU's iommu api. vfio is currently the only caller, but that could change and just adds maintenance effort to scrub this error message at a later time. We could make the whole path support an error return to allow vfio to report the error, we could add a name field in the IOMMUNotifier, or we could just make the error more generic. Thanks,
Quoted 3 lines
Alex
Sure, I'll change the message to something generic.
Aviv.
Quoted 16 lines
Quoted 14 lines
hw/i386/intel_iommu.c +4
-0+exit (1 ); +} +/* Do the initialization. It will also be called when reset, so pay * attention when adding new initialization stuff. */ @@ -2467,6 +2473,7 @@ static void vtd_init(IntelIOMMUState *s) s->iommu_ops.translate = vtd_iommu_translate; s->iommu_ops.notify_flag_changed = vtd_iommu_notify_flag_changed; + s->iommu_ops.replay = vtd_iommu_replay; s->root =0 ; s->root_extended = false; s->dmar_enabled = false;
Re: [Qemu-devel] [PATCH v7 3/5] IOMMU: enable intel_iommu map and unmap notifiers
In reply to Aviv B.D2016-11-28 23:51 GMT+08:00 Aviv B.D <[email protected]>:
Quoted 114 lines
From: "Aviv Ben-David" <[email protected]>
Adds a list of registered vtd_as's to intel iommu state to save iteration over each PCI device in a search of the corrosponding domain.
Signed-off-by Aviv Ben-David <[email protected]>--- hw/i386/intel_iommu.c | 94 ++++++++++++++++++++++++++++++++++++++---- hw/i386/intel_iommu_internal.h | 2 + include/hw/i386/intel_iommu.h | 9 ++++ 3 files changed, 98 insertions(+), 7 deletions(-)hw/i386/intel_iommu.c +61
-4@@ -679,7 +679,7 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa, } *reads = (*reads) && (slpte &VTD_SL_R ); *writes = (*writes) && (slpte &VTD_SL_W ); -if (!(slpte & access_right_check)) { +if (!(slpte & access_right_check) && !(flags &IOMMU_NO_FAIL )) {VTD_DPRINTF (GENERAL ,"error: lack of %s permission for " "gpa 0x%" PRIx64" slpte 0x%" PRIx64, (flags &IOMMU_WO ?"write" :"read" ), gpa, slpte);@@ -978,6 +978,23 @@ static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)return vtd_bus; } +static int vtd_get_did_dev (IntelIOMMUState *s,uint8_t bus_num,uint8_t devfn, +uint16_t *domain_id) +{ +VTDContextEntry ce; +int ret_fr; + +assert (domain_id); + + ret_fr =vtd_dev_to_context_entry (s, bus_num, devfn, &ce); +if (ret_fr) { +return -1 ; + } + + *domain_id =VTD_CONTEXT_ENTRY_DID (ce.hi); +return 0 ; +} +/* Do a context-cache device-selective invalidation. * @func_mask: FM field after shifting */ @@ -1064,6 +1081,45 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) &domain_id); } +static void vtd_iotlb_page_invalidate_notify (IntelIOMMUState *s, +uint16_t domain_id,hwaddr addr, +uint8_t am) +{ +IntelIOMMUNotifierNode *node; + + QLIST_FOREACH(node, &(s->notifiers_list), next) { +VTDAddressSpace *vtd_as = node->vtd_as; +uint16_t vfio_domain_id; +int ret =vtd_get_did_dev (s,pci_bus_num (vtd_as->bus), vtd_as->devfn, + &vfio_domain_id); + +if (!ret && domain_id == vfio_domain_id) { +hwaddr original_addr = addr; + +while (addr < original_addr + (1 << am) *VTD_PAGE_SIZE ) { +IOMMUTLBEntry entry = s->iommu_ops.translate ( + &node->vtd_as->iommu, + addr, +IOMMU_NO_FAIL ); + +if (entry.perm ==IOMMU_NONE && + node->notifier_flag &IOMMU_NOTIFIER_UNMAP ) { + entry.target_as = &address_space_memory; + entry.iova = addr &VTD_PAGE_MASK_4K ; + entry.translated_addr =0 ; + entry.addr_mask = ~VTD_PAGE_MASK (VTD_PAGE_SHIFT ); +memory_region_notify_iommu (&node->vtd_as->iommu, entry); + addr +=VTD_PAGE_SIZE ; + }else if (node->notifier_flag &IOMMU_NOTIFIER_MAP ) { +memory_region_notify_iommu (&node->vtd_as->iommu, entry); + addr += entry.addr_mask +1 ; + } + } + } + } +} +static void vtd_iotlb_page_invalidate (IntelIOMMUState *s,uint16_t domain_id,hwaddr addr,uint8_t am) {@@ -1074,6 +1130,8 @@ static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id, info.addr = addr; info.mask = ~((1 << am) -1 );g_hash_table_foreach_remove (s->iotlb, vtd_hash_remove_by_page, &info); + +vtd_iotlb_page_invalidate_notify (s, domain_id, addr, am); }/* Flush IOTLB @@ -1999,15 +2057,37 @@ static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, IOMMUNotifierFlag new) {VTDAddressSpace *vtd_as =container_of (iommu, VTDAddressSpace, iommu); +IntelIOMMUState *s = vtd_as->iommu_state; +IntelIOMMUNotifierNode *node =NULL ; +IntelIOMMUNotifierNode *next_node =NULL ; -if (new &IOMMU_NOTIFIER_MAP ) { -error_report ("Device at bus %s addr %02x.%d requires iommu " - "notifier which is currently not supported by "
hw/i386/intel_iommu.c static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, · line 2070
Quoted 10 lines
hw/i386/intel_iommu.c +5
-3-"intel-iommu emulation" , - vtd_as->bus->qbus.name,PCI_SLOT (vtd_as->devfn), - PCI_FUNC(vtd_as->devfn)); +if (!s->cache_mode_enabled && new &IOMMU_NOTIFIER_MAP ) { +error_report ("We need to set cache_mode=1 for intel-iommu to enable " +"device assignment with IOMMU protection." );exit (1 ); } + +/* Add new ndoe if no mapping was exising before this call */
s /ndoe/node /exising/existing
hw/i386/intel_iommu.c static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, · line 2071
Quoted 1 line
hw/i386/intel_iommu.c +1
-0+if (old ==IOMMU_NOTIFIER_NONE ) {
Where is the declaration of "old"?
Quoted 73 lines
hw/i386/intel_iommu.c +19
-0+ node =g_malloc0 (sizeof (*node)); + node->vtd_as = vtd_as; + node->notifier_flag = new; +QLIST_INSERT_HEAD (&s->notifiers_list, node, next); +return ; + } + +/* update notifier node with new flags */ + QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) { +if (node->vtd_as == vtd_as) { +if (new ==IOMMU_NOTIFIER_NONE ) { +QLIST_REMOVE (node, next); +g_free (node); + }else { + node->notifier_flag = new; + } +return ; + } + } }static const VMStateDescription vtd_vmstate = {hw/i386/intel_iommu_internal.h +2
-0@@ -381,6 +381,8 @@ typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;#define VTD_PAGE_SHIFT_1G 30#define VTD_PAGE_MASK_1G (~((1ULL << VTD_PAGE_SHIFT_1G) - 1)) +#define VTD_PAGE_MASK (shift) (~((1ULL << (shift)) - 1)) +struct VTDRootEntry {uint64_t val;uint64_t rsvd;include/hw/i386/intel_iommu.h +9
-1@@ -63,6 +63,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress ;typedef struct VTDIrq VTDIrq ;typedef struct VTD_MSIMessage VTD_MSIMessage ; +typedef struct IntelIOMMUNotifierNode IntelIOMMUNotifierNode ;/* Context-Entry */ struct VTDContextEntry {@@ -247,6 +248,12 @@ struct VTD_MSIMessage {/* When IR is enabled, all MSI/MSI-X data bits should be zero */ #define VTD_IR_MSI_DATA (0) +struct IntelIOMMUNotifierNode { +VTDAddressSpace *vtd_as; +IOMMUNotifierFlag notifier_flag; +QLIST_ENTRY (IntelIOMMUNotifierNode) next; +}; +/* The iommu (DMAR) device state struct */ struct IntelIOMMUState {X86IOMMUState x86_iommu;@@ -284,6 +291,8 @@ struct IntelIOMMUState {MemoryRegionIOMMUOps iommu_ops;GHashTable *vtd_as_by_busptr;/* VTDBus objects indexed by PCIBus* reference */ VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX ];/* VTDBus objects indexed by bus number */ +/* list of registered notifiers */ + QLIST_HEAD(, IntelIOMMUNotifierNode) notifiers_list;/* interrupt remapping */ bool intr_enabled;/* Whether guest enabled IR */ --1.9.1
Re: [Qemu-devel] [PATCH v7 3/5] IOMMU: enable intel_iommu map and unmap notifiers
Quoted 136 lines · 蓝天宇
2016-11-28 23:51 GMT+08:00 Aviv B.D <[email protected]>:
Quoted 8 lines
From: "Aviv Ben-David" <[email protected]>
Adds a list of registered vtd_as's to intel iommu state to save iteration over each PCI device in a search of the corrosponding domain.
Signed-off-by Aviv Ben-David <[email protected]>--- hw/i386/intel_iommu.c | 94++++++++++++++++++++++++++++++++++++++----
hw/i386/intel_iommu.c static int vtd_gpa_to_slpte(VTDContextEntry *ce,
Quoted 9 lines
hw/i386/intel_iommu_internal.h | 2 + include/hw/i386/intel_iommu.h | 9 ++++ 3 files changed, 98 insertions(+), 7 deletions(-)hw/i386/intel_iommu.c +0
-0@@ -679,7 +679,7 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce,
uint64_tgpa,hw/i386/intel_iommu.c static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa,
Quoted 8 lines
hw/i386/intel_iommu.c +1
-1} *reads = (*reads) && (slpte &VTD_SL_R ); *writes = (*writes) && (slpte &VTD_SL_W ); -if (!(slpte & access_right_check)) { +if (!(slpte & access_right_check) && !(flags &IOMMU_NO_FAIL )) { VTD_DPRINTF(GENERAL, "error: lack of %s permission for " "gpa 0x%"PRIx64 " slpte 0x%"PRIx64, (flags & IOMMU_WO ? "write" : "read"), gpa,slpte);
hw/i386/intel_iommu.c static VTDBus
Quoted 1 line
hw/i386/intel_iommu.c +0
-0@@ -978,6 +978,23 @@ static VTDBus*vtd_find_as_from_bus_num(IntelIOMMUState *s,
uint8_tbus_num)hw/i386/intel_iommu.c static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)
Quoted 4 lines
hw/i386/intel_iommu.c +1
-0return vtd_bus; } +static int vtd_get_did_dev(IntelIOMMUState *s, uint8_t bus_num, uint8_tdevfn,
hw/i386/intel_iommu.c static void
Quoted 20 lines
hw/i386/intel_iommu.c +16
-0+ uint16_t *domain_id) +{ +VTDContextEntry ce; +int ret_fr; + +assert (domain_id); + + ret_fr =vtd_dev_to_context_entry (s, bus_num, devfn, &ce); +if (ret_fr) { +return -1 ; + } + + *domain_id =VTD_CONTEXT_ENTRY_DID (ce.hi); +return 0 ; +} +/* Do a context-cache device-selective invalidation. * @func_mask: FM field after shifting */ @@ -1064,6 +1081,45 @@ static void
vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
Quoted 5 lines
hw/i386/intel_iommu.c +2
-0&domain_id); } +static void vtd_iotlb_page_invalidate_notify (IntelIOMMUState *s, + uint16_t domain_id, hwaddraddr,
hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
Quoted 8 lines
hw/i386/intel_iommu.c +8
-0+ uint8_t am) +{ +IntelIOMMUNotifierNode *node; + + QLIST_FOREACH(node, &(s->notifiers_list), next) { +VTDAddressSpace *vtd_as = node->vtd_as; +uint16_t vfio_domain_id; + int ret = vtd_get_did_dev(s, pci_bus_num(vtd_as->bus),
vtd_as->devfn,hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1104
Quoted 8 lines
hw/i386/intel_iommu.c +8
-0+ &vfio_domain_id); + +if (!ret && domain_id == vfio_domain_id) { +hwaddr original_addr = addr; + +while (addr < original_addr + (1 << am) *VTD_PAGE_SIZE ) { + IOMMUTLBEntry entry = s->iommu_ops.translate( +
&node->vtd_as->iommu,hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
Quoted 10 lines
hw/i386/intel_iommu.c +10
-0+ addr, + IOMMU_NO_FAIL); + +if (entry.perm ==IOMMU_NONE && + node->notifier_flag &IOMMU_NOTIFIER_UNMAP ) { + entry.target_as = &address_space_memory; + entry.iova = addr &VTD_PAGE_MASK_4K ; + entry.translated_addr =0 ; + entry.addr_mask = ~VTD_PAGE_MASK (VTD_PAGE_SHIFT ); + memory_region_notify_iommu(&node->vtd_as->iommu,entry);
hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1121
Quoted 3 lines
hw/i386/intel_iommu.c +3
-0+ addr +=VTD_PAGE_SIZE ; + }else if (node->notifier_flag &IOMMU_NOTIFIER_MAP ) { +
memory_region_notify_iommu(&node->vtd_as->iommu, entry);hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
Quoted 8 lines
hw/i386/intel_iommu.c +7
-0+ addr += entry.addr_mask +1 ; + } + } + } + } +} + static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t
domain_id,hw/i386/intel_iommu.c static void
Quoted 3 lines
hw/i386/intel_iommu.c +0
-0hwaddr addr, uint8_t am) {@@ -1074,6 +1130,8 @@ static voidvtd_iotlb_page_invalidate(IntelIOMMUState *s,
uint16_tdomain_id,hw/i386/intel_iommu.c static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
Quoted 3 lines
hw/i386/intel_iommu.c +0
-0info.addr = addr; info.mask = ~((1 << am) -1 ); g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page,&info);
hw/i386/intel_iommu.c static void
Quoted 6 lines
hw/i386/intel_iommu.c +2
-0+ +vtd_iotlb_page_invalidate_notify (s, domain_id, addr, am); }/* Flush IOTLB @@ -1999,15 +2057,37 @@ static voidvtd_iommu_notify_flag_changed(MemoryRegion *iommu,
hw/i386/intel_iommu.c static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu,
Quoted 3 lines
hw/i386/intel_iommu.c +0
-0IOMMUNotifierFlag new) { VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace,iommu);
hw/i386/intel_iommu.c static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu,
Quoted 12 lines
hw/i386/intel_iommu.c +5
-6+IntelIOMMUState *s = vtd_as->iommu_state; +IntelIOMMUNotifierNode *node =NULL ; +IntelIOMMUNotifierNode *next_node =NULL ; -if (new &IOMMU_NOTIFIER_MAP ) { -error_report ("Device at bus %s addr %02x.%d requires iommu " -"notifier which is currently not supported by " -"intel-iommu emulation" , - vtd_as->bus->qbus.name,PCI_SLOT (vtd_as->devfn), -PCI_FUNC (vtd_as->devfn)); +if (!s->cache_mode_enabled && new &IOMMU_NOTIFIER_MAP ) { + error_report("We need to set cache_mode=1 for intel-iommu to
Quoted 9 lines
enable "
hw/i386/intel_iommu.c static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, · line 2070
Quoted 5 lines
hw/i386/intel_iommu.c +3
-0+ "device assignment with IOMMU protection.");exit (1 ); } + +/* Add new ndoe if no mapping was exising before this call */ s /ndoe/node /exising/existing
Sorry, it will be changed.
Quoted 5 lines
hw/i386/intel_iommu.c static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, · line 2071
Quoted 2 lines
hw/i386/intel_iommu.c +1
-0+if (old ==IOMMU_NOTIFIER_NONE ) {Where is the declaration of "old"?
Old is one of the function parameters.
Quoted 67 lines
Review
Quoted 23 lines
hw/i386/intel_iommu.c +19
-0+ node =g_malloc0 (sizeof (*node)); + node->vtd_as = vtd_as; + node->notifier_flag = new; +QLIST_INSERT_HEAD (&s->notifiers_list, node, next); +return ; + } + +/* update notifier node with new flags */ + QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) { +if (node->vtd_as == vtd_as) { +if (new ==IOMMU_NOTIFIER_NONE ) { +QLIST_REMOVE (node, next); +g_free (node); + }else { + node->notifier_flag = new; + } +return ; + } + } }static const VMStateDescription vtd_vmstate = {(quoted diff) +0
-0
b/hw/i386/intel_iommu_internal.hhw/i386/intel_iommu_internal.h typedef struct VTDIOTLBPageInvInfo
Quoted 4 lines
index 00cbe0d..c1b9cfc 100644
hw/i386/intel_iommu_internal.h +0
-0@@ -381,6 +381,8 @@ typedef struct VTDIOTLBPageInvInfoVTDIOTLBPageInvInfo;
Review
Quoted 9 lines
hw/i386/intel_iommu_internal.h +2
-0#define VTD_PAGE_SHIFT_1G 30#define VTD_PAGE_MASK_1G (~((1ULL << VTD_PAGE_SHIFT_1G) - 1)) +#define VTD_PAGE_MASK (shift) (~((1ULL << (shift)) - 1)) +struct VTDRootEntry {uint64_t val;uint64_t rsvd;(quoted diff) +0
-0
b/include/hw/i386/intel_iommu.hQuoted 25 lines
index 749eef9..1a355c1 100644
include/hw/i386/intel_iommu.h +7
-0@@ -63,6 +63,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress ;typedef struct VTDIrq VTDIrq ;typedef struct VTD_MSIMessage VTD_MSIMessage ; +typedef struct IntelIOMMUNotifierNode IntelIOMMUNotifierNode ;/* Context-Entry */ struct VTDContextEntry {@@ -247,6 +248,12 @@ struct VTD_MSIMessage {/* When IR is enabled, all MSI/MSI-X data bits should be zero */ #define VTD_IR_MSI_DATA (0) +struct IntelIOMMUNotifierNode { +VTDAddressSpace *vtd_as; +IOMMUNotifierFlag notifier_flag; +QLIST_ENTRY (IntelIOMMUNotifierNode) next; +}; +/* The iommu (DMAR) device state struct */ struct IntelIOMMUState {X86IOMMUState x86_iommu;include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 292
Quoted 2 lines
include/hw/i386/intel_iommu.h +0
-0@@ -284,6 +291,8 @@ struct IntelIOMMUState {MemoryRegionIOMMUOps iommu_ops;GHashTable *vtd_as_by_busptr;/* VTDBus objects indexed by PCIBus* reference */
Quoted 12 lines
include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 293
Quoted 1 line
include/hw/i386/intel_iommu.h +0
-0VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX ];/* VTDBus objects indexed by bus number */
Quoted 9 lines
include/hw/i386/intel_iommu.h +2
-1+/* list of registered notifiers */ + QLIST_HEAD(, IntelIOMMUNotifierNode) notifiers_list;/* interrupt remapping */ bool intr_enabled;/* Whether guest enabled IR */ --1.9.1
Aviv.
Quoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.
I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Looks like we just cannot live without knowing this domain layer. Because the address space is binded to the domain. If we want to sync the address space (here to setup a correct shadow page table), we need to do it per-domain.
What I can think of as a solution is that we introduce this "domain" layer - like a memory region per domain. When invalidation happens, it's per-domain, not per-device any more (actually I guess that's what current vt-d iommu driver in kernel is doing, we just ignored it - we fetch the devices that matches the domain ID). We can/need to maintain something different, like sid <-> domain mappings (we can do this as long as we are notified when context entries changed), per-domain mappings (just like per-device mappings that we are trying to build in this series, but what we really need is IMHO per domain one), etc. When device switches domain, we switch the IOMMU memory region accordingly.
Does this make any sense? Comments are greatly welcomed (especially from AlexW and DavidG).
Thanks,
-- peterx
Quoted 12 lines
From: Aviv B.D
Sent: Monday, November 28, 2016 11:52 PMFrom: "Aviv Ben-David" <[email protected]>
* Advertize Cache Mode capability in iommu cap register.
This capability is controlled by "cache-mode" property of intel-iommu device. To enable this option call QEMU with "-device intel-iommu,cache-mode=true".* On page cache invalidation in intel vIOMMU, check if the domain belong to registered notifier, and notify accordingly.
Quoted 9 lines
Currently this patch still doesn't enabling VFIO devices support with vIOMMU present. Current problems:
*vfio_iommu_map_notifyis not aware about memory range belong to specific VFIOGuestIOMMU.
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.
Thanks Aviv for your great work! Extending vIOMMU to support VFIO devices is also an important feature for Intel. We'd like to contribute and collaborate with you in this area to make it fully functional. My colleague (Tianyu Lan) will chime in later with his thoughts. Please don't hesitate sharing your TODO list so we can work together to move forward.
My another colleague (Yi Liu) is also working on SVM virtualization in the meantime with draft design already sent out. There'll be more API changes built on top of the notification framework being worked on here, which we can discuss later when your work is stabilized. :-)
Thanks
Kevin
Quoted 16 lines
From: Peter Xu
Sent: Wednesday, November 30, 2016 5:24 PMQuoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
Quoted 11 lines
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Tianyu and I discussed there is a bigger problem: today VFIO assumes only one address space per container, which is fine w/o vIOMMU (all devices in same container share same GPA->HPA translation). However it's not the case when vIOMMU is enabled, because guest Linux implements per-device IOVA space. If a VFIO container includes multiple devices, it means multiple address spaces required per container...
Quoted 24 lines
Looks like we just cannot live without knowing this domain layer. Because the address space is binded to the domain. If we want to sync the address space (here to setup a correct shadow page table), we need to do it per-domain.
What I can think of as a solution is that we introduce this "domain" layer - like a memory region per domain. When invalidation happens, it's per-domain, not per-device any more (actually I guess that's what current vt-d iommu driver in kernel is doing, we just ignored it - we fetch the devices that matches the domain ID). We can/need to maintain something different, like sid <-> domain mappings (we can do this as long as we are notified when context entries changed), per-domain mappings (just like per-device mappings that we are trying to build in this series, but what we really need is IMHO per domain one), etc. When device switches domain, we switch the IOMMU memory region accordingly.
Does this make any sense? Comments are greatly welcomed (especially from AlexW and DavidG).
Thanks,
-- peterx
Re: [Qemu-devel] [PATCH v7 1/5] IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to guest
In reply to Aviv B.DQuoted 7 lines
From: Aviv B.D
Sent: Monday, November 28, 2016 11:52 PMFrom: "Aviv Ben-David" <[email protected]>
This capability asks the guest to invalidate cache before each map operation. We can use this invalidation to trap map operations in the hypervisor.
'in the hypervisor" or "in the guest"?
Quoted 13 lines
Signed-off-by Aviv Ben-David <[email protected]>--- hw/i386/intel_iommu.c | 5 +++++ hw/i386/intel_iommu_internal.h | 1 + include/hw/i386/intel_iommu.h | 2 ++ 3 files changed, 8 insertions(+)hw/i386/intel_iommu.c +0
-0
hw/i386/intel_iommu.c static Property vtd_properties[] = { · line 2021
Quoted 5 lines
hw/i386/intel_iommu.c +1
-0@@ -2017,6 +2017,7 @@ static Property vtd_properties[] = {DEFINE_PROP_ON_OFF_AUTO ("eim" , IntelIOMMUState, intr_eim,ON_OFF_AUTO_AUTO ),DEFINE_PROP_BOOL ("x-buggy-eim" , IntelIOMMUState, buggy_eim, false), +DEFINE_PROP_BOOL ("cache-mode" , IntelIOMMUState, cache_mode_enabled, FALSE),
better align with spec - "caching-mode".
Quoted 42 lines
hw/i386/intel_iommu.c +4
-0DEFINE_PROP_END_OF_LIST(), };@@ -2391,6 +2392,10 @@ static void vtd_init(IntelIOMMUState *s)assert (s->intr_eim !=ON_OFF_AUTO_AUTO ); } +if (s->cache_mode_enabled) { + s->cap |=VTD_CAP_CM ; + } +vtd_reset_context_cache (s);vtd_reset_iotlb (s);hw/i386/intel_iommu_internal.h +1
-0@@ -201,6 +201,7 @@#define VTD_CAP_MAMV (VTD_MAMV << 48)#define VTD_CAP_PSI (1ULL << 39)#define VTD_CAP_SLLPS ((1ULL << 34) | (1ULL << 35)) +#define VTD_CAP_CM (1ULL << 7)/* Supported Adjusted Guest Address Widths */ #define VTD_CAP_SAGAW_SHIFT 8include/hw/i386/intel_iommu.h +2
-1@@ -257,6 +257,8 @@ struct IntelIOMMUState {uint8_t womask[DMAR_REG_SIZE ];/* WO (write only - read returns 0) */ uint32_t version; +bool cache_mode_enabled;/* RO - is cap CM enabled? */ +dma_addr_t root;/* Current root table pointer */ bool root_extended;/* Type of root table (extended or not) */ bool dmar_enabled;/* Set if DMA remapping is enabled */ --1.9.1
Quoted 61 lines · Aviv B.D
From: "Aviv Ben-David" <[email protected]>
* Advertize Cache Mode capability in iommu cap register.
This capability is controlled by "cache-mode" property of intel-iommu device. To enable this option call QEMU with "-device intel-iommu,cache-mode=true".* On page cache invalidation in intel vIOMMU, check if the domain belong to registered notifier, and notify accordingly.
Currently this patch still doesn't enabling VFIO devices support with vIOMMU present. Current problems:
*vfio_iommu_map_notifyis not aware about memory range belong to specific VFIOGuestIOMMU.
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.Changes from v1 to v2:
* remove assumption that the cache do not clears
* fix lockup on high load.Changes from v2 to v3:
* remove debug leftovers
* split to sepearate commits
* changeis_writeto flags invtd_do_iommu_translate, addIOMMU_NO_FAILto suppress error propagating to guest.Changes from v3 to v4:
* Add property tointel_iommudevice to control the CM capability, default to False.
* Uses->iommu_ops.notify_flag_changedto register notifiers.Changes from v4 to v4 RESEND:
* Fix codding style pointed by checkpatch.pl script.Changes from v4 to v5:
* Reduce the number of changes in patch 2 and make flags real bitfield.
* Revert deleted debug prints.
* Fix memory leak in patch 3.Changes from v5 to v6:
* fix prototype ofiommu_translatefunction for more IOMMU types.
* VFIO will be notified only on the difference, without unmap before change to maps.Changes from v6 to v7:
* Add replay op toiommu_ops, with current behavior as default implementation (Patch 4).
* Add stub to disable VFIO withintel_iommusupport (Patch 5).
* Cosmetic changes to other patches.Aviv Ben-David (5):
IOMMU: add option to enableVTD_CAP_CMto vIOMMU capility exposoed to
guest
IOMMU: change iommu_op->translate'sis_writeto flags, add support to
NO_FAILflag mode
IOMMU: enableintel_iommumap and unmap notifiers
IOMMU: add specific replay function with default implemenation
IOMMU: add specific null implementation ofiommu_replaytointel_iommu
Quoted 13 lines
exec.c | 3 +- hw/alpha/typhoon.c | 2 +- hw/i386/amd_iommu.c | 4 +- hw/i386/intel_iommu.c | 165 ++++++++++++++++++++++++++++++++++------- hw/i386/intel_iommu_internal.h | 3 + hw/pci-host/apb.c | 2 +- hw/ppc/spapr_iommu.c | 2 +- hw/s390x/s390-pci-bus.c | 2 +- include/exec/memory.h | 10 ++- include/hw/i386/intel_iommu.h | 11 +++ memory.c | 11 ++- 11 files changed, 177 insertions(+), 38 deletions(-)
Hi:
I think there are still other gaps to enable passthough device with vIOMMU's DMA translation support.
1. Since this patchset is to shadow guest IO page table to pIOMMU(physical IOMMU) vfio_dma_map/umap(), there will be some fault events from pIOMMU if guest os does misconfigurations. We should report these fault events to guest. This means we need to pass the fault event from pIOMMU driver to vIOMMU in Qemu. I suppose a channel in VFIO should be added to connect pIOMMU and vIOMMU.
The task should be divided into three parts
1) pIOMMU driver reports fault events for vIOMMU via new VFIO interface
2) Add new channel in VFIO subsystem to connect pIOMMU driver and vIOMMU in Qemu
3) vIOMMU in Qemu get fault event from VFIO subsystem in Qemu and inject virtual fault event to guest.
Such VFIO channel is also required by device's PRS(Page Request Services) support. This is also a part of SVM(Shared virtual memory) support in VM. Here is SVM design doc link.
http://marc.info/?l=kvm&m=148049586514120&w=2
2. How to restore GPA->HPA mapping when IOVA is disabled by guest. When guest enables IOVA for device, vIOMMU will invalidate all previous GPA->HPA mapping and update IOVA->HPA mapping to pIOMMU via iommu notifier. But if IOVA is disabled, I think we should restore GPA->HPA mapping for the device otherwise the device won't work again in the VM.
Another option to enable passthough device with intel vIOMMU We may prevent guest to enable DMA translation function successfully via gcmd and vIOMMU never set "Translation Enable Status" bit in gsts register when there is a passthough device.
There are kernel parameter "intel_iommu=on" or Kconfig CONFIG_INTEL_IOMMU_DEFAULT_ON to enable DMA translation for intel IOMMU driver. But they aren't set in distribution RHEL, SLES, Oracle and ubuntu by default. Tha
The iommu driver will trigger a kernel panic when fail to enable DMA translation via gcmd with log "DMAR hardware is malfunctioning".
Signature · 2 lines
Best regards Tianyu Lan
Quoted 18 lines · Kevin
Quoted 18 lines
I am thinking about this replay thing recently and now I start to
Quoted 17 lines
doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Quoted 7 lines
Tianyu and I discussed there is a bigger problem: today VFIO assumes only one address space per container, which is fine w/o vIOMMU (all devices in same container share same
GPA->HPAtranslation). However it's not the case when vIOMMU is enabled, because guest Linux implements per-device IOVA space. If a VFIO container includes multiple devices, it means multiple address spaces required per container...
Hi All:
Some updates about relationship about assigned device and container.
If vIOMMU is disabled, all assigned devices will use global address_space_memory as address space (Detail please see pci_device_iommu_address_space()). VFIO creates container according to address space and so all assigned devices will put into one container.
If vIOMMU is enabled, Intel vIOMMU will allocate separate address spaces for each assigned devices and then VFIO will create different container for each assigned device. In other word, it will be one assigned device per container when vIOMMU is enabled. The original concern won't take place. This is my understanding and please correct me if something wrong.
Signature · 2 lines
Best regards Tianyu Lan
Quoted 13 lines · Peter Xu
Quoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
Quoted 11 lines
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Hi Peter:
According VTD spec 6.2.2.1, "Software must ensure that, if multiple context-entries (or extended-context-entries) are programmed with the same Domain-id (DID), such entries must be programmed with same value for the secondlevel page-table pointer (SLPTPTR) field, and same value for the PASID Table Pointer (PASIDTPTR) field.".
So if two assigned device may have different IO page table, they should be put into different domains.
Quoted 25 lines
Looks like we just cannot live without knowing this domain layer. Because the address space is binded to the domain. If we want to sync the address space (here to setup a correct shadow page table), we need to do it per-domain.
What I can think of as a solution is that we introduce this "domain" layer - like a memory region per domain. When invalidation happens, it's per-domain, not per-device any more (actually I guess that's what current vt-d iommu driver in kernel is doing, we just ignored it - we fetch the devices that matches the domain ID). We can/need to maintain something different, like sid <-> domain mappings (we can do this as long as we are notified when context entries changed), per-domain mappings (just like per-device mappings that we are trying to build in this series, but what we really need is IMHO per domain one), etc. When device switches domain, we switch the IOMMU memory region accordingly.
Does this make any sense? Comments are greatly welcomed (especially from AlexW and DavidG).
Thanks,
-- peterx
Signature · 2 lines
Best regards Tianyu Lan
Quoted 42 lines · On Wed, 30 Nov 2016 17:23:59 +0800 Peter Xu <[email protected]> wrote:
Quoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Looks like we just cannot live without knowing this domain layer. Because the address space is binded to the domain. If we want to sync the address space (here to setup a correct shadow page table), we need to do it per-domain.
What I can think of as a solution is that we introduce this "domain" layer - like a memory region per domain. When invalidation happens, it's per-domain, not per-device any more (actually I guess that's what current vt-d iommu driver in kernel is doing, we just ignored it - we fetch the devices that matches the domain ID). We can/need to maintain something different, like sid <-> domain mappings (we can do this as long as we are notified when context entries changed), per-domain mappings (just like per-device mappings that we are trying to build in this series, but what we really need is IMHO per domain one), etc. When device switches domain, we switch the IOMMU memory region accordingly.
Quoted 2 lines
Does this make any sense? Comments are greatly welcomed (especially from AlexW and DavidG).
It's been a bit since I've looked at VT-d emulation, but I certainly remember that it's way more convoluted than I expected. It seems like a domain should create an AddressSpace and any devices assigned to that domain should make use of that single address space, but IIRC VT-d creates an address space per device, ie. per context entry.
Quoted 28 lines · Kevin
Quoted 16 lines
From: Peter Xu
Sent: Wednesday, November 30, 2016 5:24 PMQuoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
Quoted 11 lines
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Quoted 6 lines
Tianyu and I discussed there is a bigger problem: today VFIO assumes only one address space per container, which is fine w/o vIOMMU (all devices in same container share same
GPA->HPAtranslation). However it's not the case when vIOMMU is enabled, because guest Linux implements per-device IOVA space. If a VFIO container includes multiple devices, it means multiple address spaces required per container...
IIUC the vfio container is created in:
vfio_realize
vfio_get_group
vfio_connect_container
Along the way (for vfio_get_group()), we have:
group = vfio_get_group(groupid, pci_device_iommu_address_space(pdev), errp);
Here the address space is per device. If without vIOMMU, they will be pointed to the same system address space. However if with vIOMMU, that address space will be per-device, no?
-- peterx
Quoted 27 lines · Lan Tianyu
Quoted 13 lines · Peter Xu
Quoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
Quoted 11 lines
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Quoted 1 line
Hi Peter:
Hi, Tianyu,
Quoted 8 lines
According VTD spec 6.2.2.1, "Software must ensure that, if multiple context-entries (or extended-context-entries) are programmed with the same Domain-id (DID), such entries must be programmed with same value for the secondlevel page-table pointer (SLPTPTR) field, and same value for the PASID Table Pointer (PASIDTPTR) field.".
So if two assigned device may have different IO page table, they should be put into different domains.
By default, devices will be put into different domains. However it should be legal that we put two assigned devices into the same IOMMU domain (in the guest), right? And we should handle both cases well IMHO.
Actually I just wrote a tool to do it based on vfio-pci:
https://github.com/xzpeter/clibs/blob/master/gpl/userspace/vfio-bind-group/vfio-bind-group.c
If we run this tool in the guest with parameter like:
./vfio-bind-groups 00:03.0 00:04.0
It'll create one single domain, and put PCI device 00:03.0, 00:04.0 into the same IOMMU domain.
Thanks,
-- peterx
Quoted 45 lines · Alex Williamson
Quoted 42 lines · On Wed, 30 Nov 2016 17:23:59 +0800 Peter Xu <[email protected]> wrote:
Quoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Looks like we just cannot live without knowing this domain layer. Because the address space is binded to the domain. If we want to sync the address space (here to setup a correct shadow page table), we need to do it per-domain.
What I can think of as a solution is that we introduce this "domain" layer - like a memory region per domain. When invalidation happens, it's per-domain, not per-device any more (actually I guess that's what current vt-d iommu driver in kernel is doing, we just ignored it - we fetch the devices that matches the domain ID). We can/need to maintain something different, like sid <-> domain mappings (we can do this as long as we are notified when context entries changed), per-domain mappings (just like per-device mappings that we are trying to build in this series, but what we really need is IMHO per domain one), etc. When device switches domain, we switch the IOMMU memory region accordingly.
Quoted 8 lines
Quoted 2 lines
Does this make any sense? Comments are greatly welcomed (especially from AlexW and DavidG).
It's been a bit since I've looked at VT-d emulation, but I certainly remember that it's way more convoluted than I expected. It seems like a domain should create an AddressSpace and any devices assigned to that domain should make use of that single address space, but IIRC VT-d creates an address space per device, ie. per context entry.
Yes, I think this idea (one address space per domain) came from one of your replies in the past, and I just found it more essential than I thought before.
I'll see whether I can clear the way out before moving on to the replay implementations. Because IIUC the replay will depend on this (introducing the domain layer in VT-d IOMMU emulation).
Thanks!
-- peterx
Quoted 45 lines
From: Peter Xu [mailto:[email protected]]
Sent: Friday, December 02, 2016 1:59 PMQuoted 28 lines · Kevin
Quoted 16 lines
From: Peter Xu
Sent: Wednesday, November 30, 2016 5:24 PMQuoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
Quoted 11 lines
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Quoted 6 lines
Tianyu and I discussed there is a bigger problem: today VFIO assumes only one address space per container, which is fine w/o vIOMMU (all devices in same container share same
GPA->HPAtranslation). However it's not the case when vIOMMU is enabled, because guest Linux implements per-device IOVA space. If a VFIO container includes multiple devices, it means multiple address spaces required per container...IIUC the vfio container is created in:
vfio_realize
vfio_get_group
vfio_connect_container
Quoted 8 lines
Along the way (for
vfio_get_group()), we have:group = vfio_get_group(groupid, pci_device_iommu_address_space(pdev), errp);Here the address space is per device. If without vIOMMU, they will be pointed to the same system address space. However if with vIOMMU, that address space will be per-device, no?
yes, I didn't note that fact. Tianyu also pointed it out in his reply. :-)
Thanks
Kevin
On Thu, Dec 01, 2016 at 02:44:14PM +0800, Lan Tianyu wrote:
[...]
Quoted 10 lines
Hi:
I think there are still other gaps to enable passthough device with vIOMMU's DMA translation support.1. Since this patchset is to shadow guest IO page table to pIOMMU(physical IOMMU) vfio_dma_map/umap(), there will be some fault events from pIOMMU if guest os does misconfigurations. We should report these fault events to guest. This means we need to pass the fault event from pIOMMU driver to vIOMMU in Qemu. I suppose a channel in VFIO should be added to connect pIOMMU and vIOMMU.
Thanks for raising this up - IMHO this is a good question.
Quoted 3 lines
The task should be divided into three parts
1) pIOMMU driver reports fault events for vIOMMU via new VFIO interface
Here you mean "how host kernel capture the DMAR fault", right?
IMHO We can have something like notifier/notifiee as well in DMAR fault reporting - people (like vfio driver in kernel) can register to fault reports related to specific device. When DMAR receives faults for those devices, it triggers the notification list, then vfio can be notified.
Quoted 10 lines
2) Add new channel in VFIO subsystem to connect pIOMMU driver and vIOMMU in Qemu
3) vIOMMU in Qemu get fault event from VFIO subsystem in Qemu and inject virtual fault event to guest.Such VFIO channel is also required by device's PRS(Page Request Services) support. This is also a part of SVM(Shared virtual memory) support in VM. Here is SVM design doc link.
http://marc.info/?l=kvm&m=148049586514120&w=2
Quoted 5 lines
2. How to restore
GPA->HPAmapping when IOVA is disabled by guest. When guest enables IOVA for device, vIOMMU will invalidate all previousGPA->HPAmapping and updateIOVA->HPAmapping to pIOMMU via iommu notifier. But if IOVA is disabled, I think we should restoreGPA->HPAmapping for the device otherwise the device won't work again in the VM.
If we can have a workable replay mechanism, this problem will be solved IMHO. A more general issue is we move one device into an existing domain X (no matter this is system address space, or another existing IOMMU domain) - we need to unmap the pages in A and remap the pages in B. But, this is not the best solution IMHO. Issue of this solution is that we will need to maintain some containers that has totally the same shadow page tables. The best solution is we have a domain layer, and we have an address space (container) for each domain. Then:
- when new domain added: we create a new container for this new domain, re-build the shadow page table in that domain if there is any
- when device moves domain (either move into/from system address space, or move into another IOMMU domain): we can just try to put that device (aka corresponding group) into the existing container that the new domain belongs
Thanks,
-- peterx
On Fri, Dec 02, 2016 at 06:23:52AM +0000, Tian, Kevin wrote:
[...]
Quoted 8 lines
Quoted 8 lines
IIUC the vfio container is created in:
vfio_realize
vfio_get_group
vfio_connect_containerAlong the way (for
vfio_get_group()), we have:
Quoted 8 lines
Quoted 6 lines
group = vfio_get_group(groupid, pci_device_iommu_address_space(pdev), errp);Here the address space is per device. If without vIOMMU, they will be pointed to the same system address space. However if with vIOMMU, that address space will be per-device, no?
yes, I didn't note that fact. Tianyu also pointed it out in his reply. :-)
Sorry I didn't see that just now. Thanks for the confirmation. :)
Thanks,
-- peterx
Quoted 42 lines · On Fri, 2 Dec 2016 13:59:25 +0800 Peter Xu <[email protected]> wrote:
Quoted 28 lines · Kevin
Quoted 16 lines
From: Peter Xu
Sent: Wednesday, November 30, 2016 5:24 PMQuoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
Quoted 11 lines
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Quoted 6 lines
Tianyu and I discussed there is a bigger problem: today VFIO assumes only one address space per container, which is fine w/o vIOMMU (all devices in same container share same
GPA->HPAtranslation). However it's not the case when vIOMMU is enabled, because guest Linux implements per-device IOVA space. If a VFIO container includes multiple devices, it means multiple address spaces required per container...IIUC the vfio container is created in:
vfio_realize
vfio_get_group
vfio_connect_container
Quoted 7 lines
Along the way (for
vfio_get_group()), we have:group = vfio_get_group(groupid, pci_device_iommu_address_space(pdev), errp);Here the address space is per device. If without vIOMMU, they will be pointed to the same system address space. However if with vIOMMU, that address space will be per-device, no?
Correct, with VT-d present, there will be a separate AddressSpace per device, so each device will be placed into separate containers. This is currently the only way to provide the flexibility that those separate devices can be attached to different domains in the guest. It also automatically faults on when devices share an iommu group on the host but the guest attempts to use separate AddressSpaces.
Trouble comes when the guest is booted with iommu=pt as each container will need to map the full guest memory, yet each container is accounted separately for locked memory. libvirt doesn't account for $NUM_HOSTDEVS x $VM_MEM_SIZE for locked memory.
Ideally we could be more flexible with dynamic containers, but it's not currently an option to move a group from one container to another w/o first closing all the devices within the group. Thanks,
Alex
Quoted 49 lines · On Fri, 2 Dec 2016 14:08:59 +0800 Peter Xu <[email protected]> wrote:
Quoted 27 lines · Lan Tianyu
Quoted 13 lines · Peter Xu
Quoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
Quoted 11 lines
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Quoted 1 line
Hi Peter:
Hi, Tianyu,
Quoted 8 lines
According VTD spec 6.2.2.1, "Software must ensure that, if multiple context-entries (or extended-context-entries) are programmed with the same Domain-id (DID), such entries must be programmed with same value for the secondlevel page-table pointer (SLPTPTR) field, and same value for the PASID Table Pointer (PASIDTPTR) field.".
So if two assigned device may have different IO page table, they should be put into different domains.
By default, devices will be put into different domains. However it should be legal that we put two assigned devices into the same IOMMU domain (in the guest), right? And we should handle both cases well IMHO.
Actually I just wrote a tool to do it based on vfio-pci:
Quoted 8 lines
https://github.com/xzpeter/clibs/blob/master/gpl/userspace/vfio-bind-group/vfio-bind-group.c
If we run this tool in the guest with parameter like:
./vfio-bind-groups 00:03.0 00:04.0
It'll create one single domain, and put PCI device 00:03.0, 00:04.0 into the same IOMMU domain.
On the host though, I'd expect we still have separate IOMMU domains, one for each device and we do DMA_{UN}MAP ioctls separately per container. Thanks,
Alex
Quoted 56 lines · Alex Williamson
Quoted 53 lines · On Fri, 2 Dec 2016 14:08:59 +0800 Peter Xu <[email protected]> wrote:
Quoted 27 lines · Lan Tianyu
Quoted 13 lines · Peter Xu
Quoted 4 lines · Aviv B.D
* intel_iommu's replay op is not implemented yet (May come in different patch set).
The replay function is required for hotplug vfio device and to move devices between existing domains.I am thinking about this replay thing recently and now I start to doubt whether the whole vt-d vIOMMU framework suites this...
Generally speaking, current work is throwing away the IOMMU "domain" layer here. We maintain the mapping only per device, and we don't care too much about which domain it belongs. This seems problematic.
Quoted 11 lines
A simplest wrong case for this is (let's assume cache-mode is enabled): if we have two assigned devices A and B, both belong to the same domain 1. Meanwhile, in domain 1 assume we have one mapping which is the first page (iova range 0-0xfff). Then, if guest wants to invalidate the page, it'll notify VT-d vIOMMU with an invalidation message. If we do this invalidation per-device, we'll need to UNMAP the region twice - once for A, once for B (if we have more devices, we will unmap more times), and we can never know we have done duplicated work since we don't keep domain info, so we don't know they are using the same address space. The first unmap will work, and then we'll possibly get some errors on the rest of dma unmap failures.
Quoted 1 line
Hi Peter:
Hi, Tianyu,
Quoted 8 lines
According VTD spec 6.2.2.1, "Software must ensure that, if multiple context-entries (or extended-context-entries) are programmed with the same Domain-id (DID), such entries must be programmed with same value for the secondlevel page-table pointer (SLPTPTR) field, and same value for the PASID Table Pointer (PASIDTPTR) field.".
So if two assigned device may have different IO page table, they should be put into different domains.
By default, devices will be put into different domains. However it should be legal that we put two assigned devices into the same IOMMU domain (in the guest), right? And we should handle both cases well IMHO.
Actually I just wrote a tool to do it based on vfio-pci:
https://github.com/xzpeter/clibs/blob/master/gpl/userspace/vfio-bind-group/vfio-bind-group.c
If we run this tool in the guest with parameter like:
Quoted 8 lines
Quoted 4 lines
./vfio-bind-groups 00:03.0 00:04.0
It'll create one single domain, and put PCI device 00:03.0, 00:04.0 into the same IOMMU domain.
On the host though, I'd expect we still have separate IOMMU domains, one for each device and we do DMA_{UN}MAP ioctls separately per container. Thanks,
Agree. Guest may use different IO page tables for multi assigned devices and this requires to put assigned device in different VTD domain on host. I think we can't put assigned devices into the same VTD domain before enabling dynamic containers.
On Fri, Dec 02, 2016 at 10:30:34AM -0700, Alex Williamson wrote:
[...]
Quoted 3 lines
On the host though, I'd expect we still have separate IOMMU domains, one for each device and we do DMA_{UN}MAP ioctls separately per container. Thanks,
I have a RFC version of replay implementation that used the way to do it (each device will have its own address space, even if more than one devices are in the same IOMMU domain). I'll arrange the patches and post soon. Let's see whether that's a valid approach to fix existing issues for vfio enablement on vt-d.
Thanks,
-- peterx
Quoted 15 lines · Peter Xu
On Thu, Dec 01, 2016 at 02:44:14PM +0800, Lan Tianyu wrote:
[...]
Quoted 10 lines
Hi:
I think there are still other gaps to enable passthough device with vIOMMU's DMA translation support.1. Since this patchset is to shadow guest IO page table to pIOMMU(physical IOMMU) vfio_dma_map/umap(), there will be some fault events from pIOMMU if guest os does misconfigurations. We should report these fault events to guest. This means we need to pass the fault event from pIOMMU driver to vIOMMU in Qemu. I suppose a channel in VFIO should be added to connect pIOMMU and vIOMMU.
Quoted 7 lines
Thanks for raising this up - IMHO this is a good question.
Quoted 3 lines
The task should be divided into three parts
1) pIOMMU driver reports fault events for vIOMMU via new VFIO interfaceHere you mean "how host kernel capture the DMAR fault", right?
Yes, VFIO kernel driver should know the fault event when it's triggered.
Quoted 6 lines
IMHO We can have something like notifier/notifiee as well in DMAR fault reporting - people (like vfio driver in kernel) can register to fault reports related to specific device. When DMAR receives faults for those devices, it triggers the notification list, then vfio can be notified.
Yes, something likes this.
Quoted 11 lines
Quoted 11 lines
2) Add new channel in VFIO subsystem to connect pIOMMU driver and vIOMMU in Qemu
3) vIOMMU in Qemu get fault event from VFIO subsystem in Qemu and inject virtual fault event to guest.Such VFIO channel is also required by device's PRS(Page Request Services) support. This is also a part of SVM(Shared virtual memory) support in VM. Here is SVM design doc link.
http://marc.info/?l=kvm&m=148049586514120&w=2
Quoted 8 lines
Quoted 5 lines
2. How to restore
GPA->HPAmapping when IOVA is disabled by guest. When guest enables IOVA for device, vIOMMU will invalidate all previousGPA->HPAmapping and updateIOVA->HPAmapping to pIOMMU via iommu notifier. But if IOVA is disabled, I think we should restoreGPA->HPAmapping for the device otherwise the device won't work again in the VM.If we can have a workable replay mechanism, this problem will be solved IMHO.
Basic idea is to replay related memory regions to restore GPA->HPA mapping when guest disables IOVA.
Signature · 2 lines
Best regards Tianyu Lan
On Tue, Dec 06, 2016 at 02:30:24PM +0800, Lan Tianyu wrote:
[...]
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
2. How to restore
GPA->HPAmapping when IOVA is disabled by guest. When guest enables IOVA for device, vIOMMU will invalidate all previousGPA->HPAmapping and updateIOVA->HPAmapping to pIOMMU via iommu notifier. But if IOVA is disabled, I think we should restoreGPA->HPAmapping for the device otherwise the device won't work again in the VM.If we can have a workable replay mechanism, this problem will be solved IMHO.
Basic idea is to replay related memory regions to restore
GPA->HPAmapping when guest disables IOVA.
Btw, could you help elaborate in what case we will trigger such a condition? Or, can we dynamically enable/disable an IOMMU?
Thanks,
-- peterx
Quoted 10 lines · Peter Xu
On Tue, Dec 06, 2016 at 02:30:24PM +0800, Lan Tianyu wrote:
[...]
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
2. How to restore
GPA->HPAmapping when IOVA is disabled by guest. When guest enables IOVA for device, vIOMMU will invalidate all previousGPA->HPAmapping and updateIOVA->HPAmapping to pIOMMU via iommu notifier. But if IOVA is disabled, I think we should restoreGPA->HPAmapping for the device otherwise the device won't work again in the VM.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
If we can have a workable replay mechanism, this problem will be solved IMHO.
Basic idea is to replay related memory regions to restore
GPA->HPAmapping when guest disables IOVA.Btw, could you help elaborate in what case we will trigger such a condition? Or, can we dynamically enable/disable an IOMMU?
First case I think of is nest virtualization and we assign physical device to l2 guest. User space driver(E.G DPDK) also can enable/disable IOVA for device dynamically.
Signature · 2 lines
Best regards Tianyu Lan
Quoted 14 lines · Lan Tianyu
Quoted 13 lines · Peter Xu
On Tue, Dec 06, 2016 at 02:30:24PM +0800, Lan Tianyu wrote:
[...]
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
2. How to restore
GPA->HPAmapping when IOVA is disabled by guest. When guest enables IOVA for device, vIOMMU will invalidate all previousGPA->HPAmapping and updateIOVA->HPAmapping to pIOMMU via iommu notifier. But if IOVA is disabled, I think we should restoreGPA->HPAmapping for the device otherwise the device won't work again in the VM.If we can have a workable replay mechanism, this problem will be solved IMHO.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Basic idea is to replay related memory regions to restore
GPA->HPAmapping when guest disables IOVA.Btw, could you help elaborate in what case we will trigger such a condition? Or, can we dynamically enable/disable an IOMMU?
First case I think of is nest virtualization and we assign physical device to l2 guest.
If we assign physical device to l2 guest, then it will belongs to an IOMMU domain in either l1 guest and host, right? (assuming we are using vfio-pci to do device assignment)
Quoted 2 lines
User space driver(E.G DPDK) also can enable/disable IOVA for device dynamically.
Could you provide more detailed (or any pointer) on how to do that? I did try to find it myself, I see an VFIO_IOMMU_ENABLE ioctl, but looks like it is for ppc only.
Thanks,
-- peterx
Quoted 21 lines · Peter Xu
Quoted 20 lines · Lan Tianyu
Quoted 18 lines · Peter Xu
On Tue, Dec 06, 2016 at 02:30:24PM +0800, Lan Tianyu wrote:
[...]
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
2. How to restore
GPA->HPAmapping when IOVA is disabled by guest. When guest enables IOVA for device, vIOMMU will invalidate all previousGPA->HPAmapping and updateIOVA->HPAmapping to pIOMMU via iommu notifier. But if IOVA is disabled, I think we should restoreGPA->HPAmapping for the device otherwise the device won't work again in the VM.If we can have a workable replay mechanism, this problem will be solved IMHO.
Basic idea is to replay related memory regions to restore
GPA->HPAmapping when guest disables IOVA.Btw, could you help elaborate in what case we will trigger such a condition? Or, can we dynamically enable/disable an IOMMU?
Quoted 6 lines
Quoted 2 lines
First case I think of is nest virtualization and we assign physical device to l2 guest.
If we assign physical device to l2 guest, then it will belongs to an IOMMU domain in either l1 guest and host, right? (assuming we are using vfio-pci to do device assignment)
Yes.
Quoted 7 lines
Quoted 3 lines
User space driver(E.G DPDK) also can enable/disable IOVA for device dynamically.
Could you provide more detailed (or any pointer) on how to do that? I did try to find it myself, I see an
VFIO_IOMMU_ENABLEioctl, but looks like it is for ppc only.
No, I just give an example that user space may do that but no more research. But since Qemu already can enable device's IOVA, other user application also should can do that with the same VFIO interface, right?
Signature · 2 lines
Best regards Tianyu Lan
On Tue, Dec 06, 2016 at 04:27:39PM +0800, Lan Tianyu wrote:
[...]
Quoted 11 lines
Quoted 7 lines
Quoted 3 lines
User space driver(E.G DPDK) also can enable/disable IOVA for device dynamically.
Could you provide more detailed (or any pointer) on how to do that? I did try to find it myself, I see an
VFIO_IOMMU_ENABLEioctl, but looks like it is for ppc only.No, I just give an example that user space may do that but no more research. But since Qemu already can enable device's IOVA, other user application also should can do that with the same VFIO interface, right?
AFAIU we can't do that at least on x86. We can use vfio interface to bind group into container, but we should not be able to dynamically disable IOMMU protection. IIUC That needs to taint the kernel.
The only way I know is that we probe vfio-pci with no-iommu mode, in that case, we disabled IOMMU, but we can never dynamically enable it as well.
Please correct me if I am wrong.
Thanks,
-- peterx
Quoted 20 lines · On Tue, 6 Dec 2016 18:59:14 +0800 Peter Xu <[email protected]> wrote:
On Tue, Dec 06, 2016 at 04:27:39PM +0800, Lan Tianyu wrote:
[...]
Quoted 11 lines
Quoted 7 lines
Quoted 3 lines
User space driver(E.G DPDK) also can enable/disable IOVA for device dynamically.
Could you provide more detailed (or any pointer) on how to do that? I did try to find it myself, I see an
VFIO_IOMMU_ENABLEioctl, but looks like it is for ppc only.No, I just give an example that user space may do that but no more research. But since Qemu already can enable device's IOVA, other user application also should can do that with the same VFIO interface, right?
AFAIU we can't do that at least on x86. We can use vfio interface to bind group into container, but we should not be able to dynamically disable IOMMU protection. IIUC That needs to taint the kernel.
Quoted 5 lines
The only way I know is that we probe vfio-pci with no-iommu mode, in that case, we disabled IOMMU, but we can never dynamically enable it as well.
Please correct me if I am wrong.
A vfio user such as QEMU enabling or disabling translation in the vIOMMU is "simply" assigning the vfio container to the system AddressSpace (as we do w/o vIOMMU) or the device specific, IOMMU domain, AddressSpace. The device must be protected by the pIOMMU domain (container) at all times. Thanks,
Alex
Quoted 20 lines · Peter Xu
On Tue, Dec 06, 2016 at 04:27:39PM +0800, Lan Tianyu wrote:
[...]
Quoted 11 lines
Quoted 7 lines
Quoted 3 lines
User space driver(E.G DPDK) also can enable/disable IOVA for device dynamically.
Could you provide more detailed (or any pointer) on how to do that? I did try to find it myself, I see an
VFIO_IOMMU_ENABLEioctl, but looks like it is for ppc only.No, I just give an example that user space may do that but no more research. But since Qemu already can enable device's IOVA, other user application also should can do that with the same VFIO interface, right?
AFAIU we can't do that at least on x86. We can use vfio interface to bind group into container, but we should not be able to dynamically disable IOMMU protection. IIUC That needs to taint the kernel.
Quoted 5 lines
The only way I know is that we probe vfio-pci with no-iommu mode, in that case, we disabled IOMMU, but we can never dynamically enable it as well.
Please correct me if I am wrong.
Actually, disabling device's IOVA doesn't require to disable kernel global DMA protect and just clear device's VTD context entry in the context table. Go though IOMMU and VFIO code, find this will happen when call VFIO_GROUP_UNSET_CONTAINER ioctl and it will be called when destroy VM or unplug assigned device in Qemu. Please help to double check.
Call trace:
__vfio_group_unset_container()vfio_iommu_type1_detach_group()iommu_detach_group()dmar_remove_one_dev_info()__dmar_remove_one_dev_info()domain_context_clear()
The legacy KVM device assign code also will call iommu_detach_device() when deassign a device.
Quoted 1 line
From device emulation view, we need to make sure correct register
emulation regardless of guest behavior.
Quoted 5 lines
Thanks,
-- peterx
Signature · 2 lines
Best regards Tianyu Lan
Quoted 43 lines · Lan Tianyu
Quoted 20 lines · Peter Xu
On Tue, Dec 06, 2016 at 04:27:39PM +0800, Lan Tianyu wrote:
[...]
Quoted 11 lines
Quoted 7 lines
Quoted 3 lines
User space driver(E.G DPDK) also can enable/disable IOVA for device dynamically.
Could you provide more detailed (or any pointer) on how to do that? I did try to find it myself, I see an
VFIO_IOMMU_ENABLEioctl, but looks like it is for ppc only.No, I just give an example that user space may do that but no more research. But since Qemu already can enable device's IOVA, other user application also should can do that with the same VFIO interface, right?
AFAIU we can't do that at least on x86. We can use vfio interface to bind group into container, but we should not be able to dynamically disable IOMMU protection. IIUC That needs to taint the kernel.
Quoted 5 lines
The only way I know is that we probe vfio-pci with no-iommu mode, in that case, we disabled IOMMU, but we can never dynamically enable it as well.
Please correct me if I am wrong.
Actually, disabling device's IOVA doesn't require to disable kernel global DMA protect and just clear device's VTD context entry in the context table. Go though IOMMU and VFIO code, find this will happen when call
VFIO_GROUP_UNSET_CONTAINERioctl and it will be called when destroy VM or unplug assigned device in Qemu. Please help to double check.Call trace:
__vfio_group_unset_container()vfio_iommu_type1_detach_group()iommu_detach_group()dmar_remove_one_dev_info()__dmar_remove_one_dev_info()domain_context_clear()
Quoted 5 lines
The legacy KVM device assign code also will call
iommu_detach_device()when deassign a device.From device emulation view, we need to make sure correct register emulation regardless of guest behavior.
Even if the context entry is cleared and invalidated, IMHO it does not mean that we should be using GPA address space, nor do we need to put it into guest physical address space. Instead, it simply means this device cannot do any IO at that time. If IO comes, IOMMU should do fault reporting to guest OS, which should be treated as error.
So I think we are emulating the correct guest behavior here - we don't need to do anything if a device is detached from an existing IOMMU domain in guest. If we do (e.g., we replay the GPA address space on that device when it is detached, so the shadow page table for that device maps the whole guest memory), that is dangerous, because with that the device can DMA to anywhere it wants to guest memory.
Thanks,
-- peterx
Quoted 47 lines · Peter Xu
Quoted 46 lines · Lan Tianyu
Quoted 20 lines · Peter Xu
On Tue, Dec 06, 2016 at 04:27:39PM +0800, Lan Tianyu wrote:
[...]
Quoted 11 lines
Quoted 7 lines
Quoted 3 lines
User space driver(E.G DPDK) also can enable/disable IOVA for device dynamically.
Could you provide more detailed (or any pointer) on how to do that? I did try to find it myself, I see an
VFIO_IOMMU_ENABLEioctl, but looks like it is for ppc only.No, I just give an example that user space may do that but no more research. But since Qemu already can enable device's IOVA, other user application also should can do that with the same VFIO interface, right?
AFAIU we can't do that at least on x86. We can use vfio interface to bind group into container, but we should not be able to dynamically disable IOMMU protection. IIUC That needs to taint the kernel.
Quoted 5 lines
The only way I know is that we probe vfio-pci with no-iommu mode, in that case, we disabled IOMMU, but we can never dynamically enable it as well.
Please correct me if I am wrong.
Actually, disabling device's IOVA doesn't require to disable kernel global DMA protect and just clear device's VTD context entry in the context table. Go though IOMMU and VFIO code, find this will happen when call
VFIO_GROUP_UNSET_CONTAINERioctl and it will be called when destroy VM or unplug assigned device in Qemu. Please help to double check.Call trace:
__vfio_group_unset_container()vfio_iommu_type1_detach_group()iommu_detach_group()dmar_remove_one_dev_info()__dmar_remove_one_dev_info()domain_context_clear()The legacy KVM device assign code also will call
iommu_detach_device()when deassign a device.
Quoted 8 lines
Quoted 2 lines
From device emulation view, we need to make sure correct register emulation regardless of guest behavior.
Even if the context entry is cleared and invalidated, IMHO it does not mean that we should be using GPA address space, nor do we need to put it into guest physical address space. Instead, it simply means this device cannot do any IO at that time. If IO comes, IOMMU should do fault reporting to guest OS, which should be treated as error.
Yes, that looks right and there will be fault event reported by pIOMMU
if context entry is no present for DMA untranslated request. This goes back to the first gap to report pIOMMU fault event to vIOMMU.
For disabling via clearing DMA translation via gcmd.TE bit, assigned device should work after clearing operation and it's still necessary to restore GPA->HPA mapping since we can't assume guest won't clear the bit after enabling DMA translation.
This maybe low priority gap since Linux IOMMU driver don't disable DMA translation frequently or dynamically. But we also should consider the situation.
Quoted 7 lines
So I think we are emulating the correct guest behavior here - we don't need to do anything if a device is detached from an existing IOMMU domain in guest. If we do (e.g., we replay the GPA address space on that device when it is detached, so the shadow page table for that device maps the whole guest memory), that is dangerous, because with that the device can DMA to anywhere it wants to guest memory.
If guest want to disabling DMA translation, this is expected behavior and device model should follow guest configuration. This just likes most distributions don't enable VTD DMA translation by default and it's OS choice.
Signature · 2 lines
Best regards Tianyu Lan
On Wed, Dec 07, 2016 at 10:04:57PM +0800, Lan Tianyu wrote:
[...]
Quoted 9 lines
Quoted 5 lines
Even if the context entry is cleared and invalidated, IMHO it does not mean that we should be using GPA address space, nor do we need to put it into guest physical address space. Instead, it simply means this device cannot do any IO at that time. If IO comes, IOMMU should do fault reporting to guest OS, which should be treated as error.
Yes, that looks right and there will be fault event reported by pIOMMU if context entry is no present for DMA untranslated request. This goes back to the first gap to report pIOMMU fault event to vIOMMU.
Right.
Quoted 9 lines
For disabling via clearing DMA translation via gcmd.TE bit, assigned device should work after clearing operation and it's still necessary to restore
GPA->HPAmapping since we can't assume guest won't clear the bit after enabling DMA translation.This maybe low priority gap since Linux IOMMU driver don't disable DMA translation frequently or dynamically. But we also should consider the situation.
I see. That's something I missed indeed. Yes we should support disabling via gcmd.te. Also, looks like we still don't support passthrough mode translation as well. That's something we need too since that means we don't supporting iommu=pt.
I agree with you that we can postpone these tasks temporarily, just like the error handling you mentioned.
Quoted 9 lines
Quoted 8 lines
So I think we are emulating the correct guest behavior here - we don't need to do anything if a device is detached from an existing IOMMU domain in guest. If we do (e.g., we replay the GPA address space on that device when it is detached, so the shadow page table for that device maps the whole guest memory), that is dangerous, because with that the device can DMA to anywhere it wants to guest memory.
Quoted 4 lines
If guest want to disabling DMA translation, this is expected behavior and device model should follow guest configuration. This just likes most distributions don't enable VTD DMA translation by default and it's OS choice.
My previous comment only suites the case when a device is moved outside an IOMMU domain. I agree with you on the rest.
Btw, do you have time to help review my RFC series for "vt-d replay"? :) I'd like to receive any further review comments besides the issues mentioned above since IMHO they can be seperated from current series, I have noted them down in my pending list.
(I think I need to test iommu=pt a bit more to make sure it works
asap)
Thanks,
-- peterx
Quoted 4 lines · Peter Xu
Btw, do you have time to help review my RFC series for "vt-d replay"? :) I'd like to receive any further review comments besides the issues mentioned above since IMHO they can be seperated from current series, I have noted them down in my pending list.
Sure. I have reviewed some patches. It looks good for me :)
Signature · 2 lines
Best regards Tianyu Lan