Skip to contentSkip to conversation

x86/HVM: p2m_ram_ro is incompatible with device pass-through

PATCH In review Jan Beulich 3 participants 7 messages Oct 1, 2019 – Oct 2, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 x86/HVM: p2m_ram_ro is incompatible with device pass-through

The write-discard property of the type can't be represented in IOMMU page table entries. Make sure the respective checks / tracking can't race, by utilizing the domain lock. The other sides of the sharing/ paging/log-dirty exclusion checks should subsequently perhaps also be put under that lock then.

This also fixes an unguarded d->arch.hvm access.

Take the opportunity and also convert neighboring bool_t to bool in struct hvm_domain.

---
v3: Re-base.
v2: Don't set p2m_ram_ro_used when failing the request.

xen/arch/x86/hvm/dm.c +18 -1
@@ -255,16 +255,33 @@ static int set_mem_type(struct domain *d
 
     mem_type = array_index_nospec(data->mem_type, ARRAY_SIZE(memtype));
 
-    if ( mem_type == HVMMEM_ioreq_server )
+    switch ( mem_type )
     {
         unsigned int flags;
 
+    case HVMMEM_ioreq_server:
         if ( !hap_enabled(d) )
             return -EOPNOTSUPP;
 
         /* Do not change to HVMMEM_ioreq_server if no ioreq server mapped. */
         if ( !p2m_get_ioreq_server(d, &flags) )
             return -EINVAL;
+
+        break;
+
+    case HVMMEM_ram_ro:
+        /* p2m_ram_ro can't be represented in IOMMU mappings. */
+        domain_lock(d);
+        if ( has_arch_pdevs(d) )
+            rc = -EXDEV;
+        else
+            d->arch.hvm.p2m_ram_ro_used = true;
+        domain_unlock(d);
+
+        if ( rc )
+            return rc;
+
+        break;
     }
 
     while ( iter < data->nr )
xen/drivers/passthrough/pci.c +23 -4
@@ -1486,15 +1486,33 @@ static int assign_device(struct domain *
     if ( !is_iommu_enabled(d) )
         return 0;
 
-    /* Prevent device assign if mem paging or mem sharing have been 
-     * enabled for this domain */
-    if ( unlikely(d->arch.hvm.mem_sharing_enabled ||
-                  vm_event_check_ring(d->vm_event_paging) ||
+    domain_lock(d);
+
+    /*
+     * Prevent device assignment if any of
+     * - mem paging
+     * - mem sharing
+     * - the p2m_ram_ro type
+     * - global log-dirty mode
+     * are in use by this domain.
+     */
+    if ( unlikely(vm_event_check_ring(d->vm_event_paging) ||
+#ifdef CONFIG_HVM
+                  (is_hvm_domain(d) &&
+                   (d->arch.hvm.mem_sharing_enabled ||
+                    d->arch.hvm.p2m_ram_ro_used)) ||
+#endif
                   p2m_get_hostp2m(d)->global_logdirty) )
+    {
+        domain_unlock(d);
         return -EXDEV;
+    }
 
     if ( !pcidevs_trylock() )
+    {
+        domain_unlock(d);
         return -ERESTART;
+    }
 
     pdev = pci_get_pdev_by_domain(hardware_domain, seg, bus, devfn);
     if ( !pdev )
@@ -1525,6 +1534,7 @@ static int assign_device(struct domain *
 
  done:
     pcidevs_unlock();
+    domain_unlock(d);
 
     return rc;
 }
xen/include/asm-x86/hvm/domain.h +4 -3
@@ -156,9 +156,10 @@ struct hvm_domain {
 
     struct viridian_domain *viridian;
 
-    bool_t                 mem_sharing_enabled;
-    bool_t                 qemu_mapcache_invalidate;
-    bool_t                 is_s3_suspended;
+    bool                   mem_sharing_enabled;
+    bool                   p2m_ram_ro_used;
+    bool                   qemu_mapcache_invalidate;
+    bool                   is_s3_suspended;
 
     /*
      * TSC value that VCPUs use to calculate their tsc_offset value.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw
Quoted 32 lines · Jan Beulich

The write-discard property of the type can't be represented in IOMMU page table entries. Make sure the respective checks / tracking can't race, by utilizing the domain lock. The other sides of the sharing/ paging/log-dirty exclusion checks should subsequently perhaps also be put under that lock then.

This also fixes an unguarded d->arch.hvm access.

Take the opportunity and also convert neighboring bool_t to bool in struct hvm_domain.

---
v3: Re-base.
v2: Don't set p2m_ram_ro_used when failing the request.

xen/arch/x86/hvm/dm.c +2 -1
@@ -255,16 +255,33 @@ static int set_mem_type(struct domain *d
 
     mem_type = array_index_nospec(data->mem_type, ARRAY_SIZE(memtype));
 
-    if ( mem_type == HVMMEM_ioreq_server )
+    switch ( mem_type )
     {
         unsigned int flags;
 
+    case HVMMEM_ioreq_server:
         if ( !hap_enabled(d) )
             return -EOPNOTSUPP;
 

xen/arch/x86/hvm/dm.c static int set_mem_type(struct domain *d · line 275

Quoted 10 lines
xen/arch/x86/hvm/dm.c +7 -0
         /* Do not change to HVMMEM_ioreq_server if no ioreq server mapped. */
         if ( !p2m_get_ioreq_server(d, &flags) )
             return -EINVAL;
+
+        break;
+
+    case HVMMEM_ram_ro:
+        /* p2m_ram_ro can't be represented in IOMMU mappings. */
+        domain_lock(d);
+        if ( has_arch_pdevs(d) )

I would use is_iommu_enabled because I think it's clearer in this context (giving the comment above explicitly refers to having iommu mappings).

xen/arch/x86/hvm/dm.c static int set_mem_type(struct domain *d · line 276

Quoted 1 line
xen/arch/x86/hvm/dm.c +1 -0
+            rc = -EXDEV;

EOPNOTSUPP might be better, since it's possible that future iommus support such page type?

Quoted 28 lines
xen/arch/x86/hvm/dm.c +8 -0
+        else
+            d->arch.hvm.p2m_ram_ro_used = true;
+        domain_unlock(d);
+
+        if ( rc )
+            return rc;
+
+        break;
     }
 
     while ( iter < data->nr )
xen/drivers/passthrough/pci.c +7 -4
@@ -1486,15 +1486,33 @@ static int assign_device(struct domain *
     if ( !is_iommu_enabled(d) )
         return 0;
 
-    /* Prevent device assign if mem paging or mem sharing have been 
-     * enabled for this domain */
-    if ( unlikely(d->arch.hvm.mem_sharing_enabled ||
-                  vm_event_check_ring(d->vm_event_paging) ||
+    domain_lock(d);
+
+    /*
+     * Prevent device assignment if any of
+     * - mem paging
+     * - mem sharing
+     * - the p2m_ram_ro type

xen/drivers/passthrough/pci.c static int assign_device(struct domain * · line 1505

Quoted 10 lines
xen/drivers/passthrough/pci.c +9 -0
+     * - global log-dirty mode
+     * are in use by this domain.
+     */
+    if ( unlikely(vm_event_check_ring(d->vm_event_paging) ||
+#ifdef CONFIG_HVM
+                  (is_hvm_domain(d) &&
+                   (d->arch.hvm.mem_sharing_enabled ||
+                    d->arch.hvm.p2m_ram_ro_used)) ||
+#endif
                   p2m_get_hostp2m(d)->global_logdirty) )

Is such check needed anymore?

With the enabling of the iommu right at domain creation it shouldn't be possible to enable any of the above features at all anymore.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 16 lines · Roger Pau Monné
Quoted 15 lines · Jan Beulich
xen/arch/x86/hvm/dm.c +2 -1
@@ -255,16 +255,33 @@ static int set_mem_type(struct domain *d
 
     mem_type = array_index_nospec(data->mem_type, ARRAY_SIZE(memtype));
 
-    if ( mem_type == HVMMEM_ioreq_server )
+    switch ( mem_type )
     {
         unsigned int flags;
 
+    case HVMMEM_ioreq_server:
         if ( !hap_enabled(d) )
             return -EOPNOTSUPP;
 
Quoted 14 lines

xen/arch/x86/hvm/dm.c static int set_mem_type(struct domain *d · line 275

Quoted 10 lines
xen/arch/x86/hvm/dm.c +7 -0
         /* Do not change to HVMMEM_ioreq_server if no ioreq server mapped. */
         if ( !p2m_get_ioreq_server(d, &flags) )
             return -EINVAL;
+
+        break;
+
+    case HVMMEM_ram_ro:
+        /* p2m_ram_ro can't be represented in IOMMU mappings. */
+        domain_lock(d);
+        if ( has_arch_pdevs(d) )

I would use is_iommu_enabled because I think it's clearer in this context (giving the comment above explicitly refers to having iommu mappings).

But the whole point of the re-basing over Paul's change is that now the operation gets refused only if a device was actually assigned.

Quoted 4 lines

xen/arch/x86/hvm/dm.c static int set_mem_type(struct domain *d · line 276

Quoted 1 line
xen/arch/x86/hvm/dm.c +1 -0
+            rc = -EXDEV;

EOPNOTSUPP might be better, since it's possible that future iommus support such page type?

I don't think future IOMMU behavior affects the choice of error code. I wanted to use something half way reasonable, yet not too common, in order to be able to easily identify the source of the error. If you and others think this isn't a meaningful concern, I'd be okay switching to -EOPNOTSUPP.

Quoted 30 lines
Quoted 17 lines
xen/drivers/passthrough/pci.c +7 -4
@@ -1486,15 +1486,33 @@ static int assign_device(struct domain *
     if ( !is_iommu_enabled(d) )
         return 0;
 
-    /* Prevent device assign if mem paging or mem sharing have been 
-     * enabled for this domain */
-    if ( unlikely(d->arch.hvm.mem_sharing_enabled ||
-                  vm_event_check_ring(d->vm_event_paging) ||
+    domain_lock(d);
+
+    /*
+     * Prevent device assignment if any of
+     * - mem paging
+     * - mem sharing
+     * - the p2m_ram_ro type

xen/drivers/passthrough/pci.c static int assign_device(struct domain * · line 1505

Quoted 10 lines
xen/drivers/passthrough/pci.c +9 -0
+     * - global log-dirty mode
+     * are in use by this domain.
+     */
+    if ( unlikely(vm_event_check_ring(d->vm_event_paging) ||
+#ifdef CONFIG_HVM
+                  (is_hvm_domain(d) &&
+                   (d->arch.hvm.mem_sharing_enabled ||
+                    d->arch.hvm.p2m_ram_ro_used)) ||
+#endif
                   p2m_get_hostp2m(d)->global_logdirty) )

Is such check needed anymore?

Quoted 2 lines

With the enabling of the iommu right at domain creation it shouldn't be possible to enable any of the above features at all anymore.

See above - all such checks should now be / get converted to check whether devices are assigned, not whether IOMMU page tables exist. After all we want to refuse requests only if strictly necessary.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by
#3 lore raw
Quoted 52 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 15 lines · Jan Beulich
xen/arch/x86/hvm/dm.c +2 -1
@@ -255,16 +255,33 @@ static int set_mem_type(struct domain *d
 
     mem_type = array_index_nospec(data->mem_type, ARRAY_SIZE(memtype));
 
-    if ( mem_type == HVMMEM_ioreq_server )
+    switch ( mem_type )
     {
         unsigned int flags;
 
+    case HVMMEM_ioreq_server:
         if ( !hap_enabled(d) )
             return -EOPNOTSUPP;
 
Quoted 14 lines

xen/arch/x86/hvm/dm.c static int set_mem_type(struct domain *d · line 275

Quoted 10 lines
xen/arch/x86/hvm/dm.c +7 -0
         /* Do not change to HVMMEM_ioreq_server if no ioreq server mapped. */
         if ( !p2m_get_ioreq_server(d, &flags) )
             return -EINVAL;
+
+        break;
+
+    case HVMMEM_ram_ro:
+        /* p2m_ram_ro can't be represented in IOMMU mappings. */
+        domain_lock(d);
+        if ( has_arch_pdevs(d) )

I would use is_iommu_enabled because I think it's clearer in this context (giving the comment above explicitly refers to having iommu mappings).

But the whole point of the re-basing over Paul's change is that now the operation gets refused only if a device was actually assigned.

Quoted 4 lines

xen/arch/x86/hvm/dm.c static int set_mem_type(struct domain *d · line 276

Quoted 1 line
xen/arch/x86/hvm/dm.c +1 -0
+            rc = -EXDEV;

EOPNOTSUPP might be better, since it's possible that future iommus support such page type?

I don't think future IOMMU behavior affects the choice of error code. I wanted to use something half way reasonable, yet not too common, in order to be able to easily identify the source of the error. If you and others think this isn't a meaningful concern, I'd be okay switching to -EOPNOTSUPP.

Quoted 6 lines
Quoted 6 lines
xen/drivers/passthrough/pci.c +0 -0
@@ -1486,15 +1486,33 @@ static int assign_device(struct domain *
     if ( !is_iommu_enabled(d) )
         return 0;
 
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
xen/drivers/passthrough/pci.c +3 -4
-    /* Prevent device assign if mem paging or mem sharing have been 
-     * enabled for this domain */
-    if ( unlikely(d->arch.hvm.mem_sharing_enabled ||
-                  vm_event_check_ring(d->vm_event_paging) ||
+    domain_lock(d);
+
+    /*
Quoted 8 lines

xen/drivers/passthrough/pci.c static int assign_device(struct domain * · line 1499

Quoted 8 lines
xen/drivers/passthrough/pci.c +8 -0
+     * Prevent device assignment if any of
+     * - mem paging
+     * - mem sharing
+     * - the p2m_ram_ro type
+     * - global log-dirty mode
+     * are in use by this domain.
+     */
+    if ( unlikely(vm_event_check_ring(d->vm_event_paging) ||

Would be nice to have some syntactic sugar like vm_event_enabled or some such.

Quoted 5 lines
Quoted 5 lines

xen/drivers/passthrough/pci.c static int assign_device(struct domain * · line 1504

Quoted 5 lines
xen/drivers/passthrough/pci.c +5 -0
+#ifdef CONFIG_HVM
+                  (is_hvm_domain(d) &&
+                   (d->arch.hvm.mem_sharing_enabled ||
+                    d->arch.hvm.p2m_ram_ro_used)) ||
+#endif

Do you really need the CONFIG_HVM guards? is_hvm_domain already has a IS_ENABLED(CONFIG_HVM).

Quoted 10 lines
Quoted 6 lines

xen/drivers/passthrough/pci.c static int assign_device(struct domain * · line 1505

Quoted 1 line
xen/drivers/passthrough/pci.c +0 -0
                   p2m_get_hostp2m(d)->global_logdirty) )

Is such check needed anymore?

With the enabling of the iommu right at domain creation it shouldn't be possible to enable any of the above features at all anymore.

See above - all such checks should now be / get converted to check whether devices are assigned, not whether IOMMU page tables exist. After all we want to refuse requests only if strictly necessary.

Oh right, I was missing the whole point then. So we still keep the iommu enabled together with introspection or ram_ro as long as there are no devices assigned.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 19 lines · Roger Pau Monné
Quoted 18 lines · Jan Beulich
Quoted 17 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich
xen/drivers/passthrough/pci.c +6 -4
@@ -1486,15 +1486,33 @@ static int assign_device(struct domain *
     if ( !is_iommu_enabled(d) )
         return 0;
 
-    /* Prevent device assign if mem paging or mem sharing have been 
-     * enabled for this domain */
-    if ( unlikely(d->arch.hvm.mem_sharing_enabled ||
-                  vm_event_check_ring(d->vm_event_paging) ||
+    domain_lock(d);
+
+    /*
+     * Prevent device assignment if any of
+     * - mem paging
+     * - mem sharing
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

xen/drivers/passthrough/pci.c static int assign_device(struct domain * · line 1499

Quoted 5 lines
xen/drivers/passthrough/pci.c +5 -0
+     * - the p2m_ram_ro type
+     * - global log-dirty mode
+     * are in use by this domain.
+     */
+    if ( unlikely(vm_event_check_ring(d->vm_event_paging) ||

Would be nice to have some syntactic sugar like vm_event_enabled or some such.

I'll leave that to the VM event maintainers.

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

xen/drivers/passthrough/pci.c static int assign_device(struct domain * · line 1504

Quoted 5 lines
xen/drivers/passthrough/pci.c +5 -0
+#ifdef CONFIG_HVM
+                  (is_hvm_domain(d) &&
+                   (d->arch.hvm.mem_sharing_enabled ||
+                    d->arch.hvm.p2m_ram_ro_used)) ||
+#endif

Do you really need the CONFIG_HVM guards? is_hvm_domain already has a IS_ENABLED(CONFIG_HVM).

Strictly speaking at this point in time it wouldn't be needed. But eventually I think we will want such, as there's no point having a bigger than necessary struct arch_domain (and struct arch_vcpu) in a !HVM build. Achieving that would likely imply though that the entire d->arch.hvm disappears, and hence without some kind of guards the above would then fail to compile. (I have accumulated quite a bit of related work already, which is probably why I felt like adding the #ifdef-s here.)

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

xen/drivers/passthrough/pci.c static int assign_device(struct domain * · line 1505

Quoted 1 line
xen/drivers/passthrough/pci.c +0 -0
                   p2m_get_hostp2m(d)->global_logdirty) )

Is such check needed anymore?

With the enabling of the iommu right at domain creation it shouldn't be possible to enable any of the above features at all anymore.

See above - all such checks should now be / get converted to check whether devices are assigned, not whether IOMMU page tables exist. After all we want to refuse requests only if strictly necessary.

Quoted 5 lines

Oh right, I was missing the whole point then. So we still keep the iommu enabled together with introspection or ram_ro as long as there are no devices assigned.

Thanks.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
In reply to Jan Beulich
Quoted 6 lines · Jan Beulich

The write-discard property of the type can't be represented in IOMMU page table entries. Make sure the respective checks / tracking can't race, by utilizing the domain lock. The other sides of the sharing/ paging/log-dirty exclusion checks should subsequently perhaps also be put under that lock then.

Quoted 7 lines

This also fixes an unguarded d->arch.hvm access.

Take the opportunity and also convert neighboring bool_t to bool in struct hvm_domain.

Leaving aside the logdirty change which is an automatic no from me in this form,  I can't see how this change avoids regressing the status quo.

Qemu really does set this flag for option ROMs, which will typically be the iPXE image for net booting.  Currently, netbooting an HVM guest with device passed through to it does work, because it is part of XenServers basic testing.

It is entirely possible that this only "works" as long as DMA doesn't touch the option ROM, and while this might not be ideal, it seems to be less bad behaviour than breaking existing VMs using this configuration.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Jan Beulich

The write-discard property of the type can't be represented in IOMMU page table entries. Make sure the respective checks / tracking can't race, by utilizing the domain lock. The other sides of the sharing/ paging/log-dirty exclusion checks should subsequently perhaps also be put under that lock then.

This also fixes an unguarded d->arch.hvm access.

Quoted 8 lines
Quoted 5 lines

Take the opportunity and also convert neighboring bool_t to bool in struct hvm_domain.

Leaving aside the logdirty change which is an automatic no from me in this form,

There's no log-dirty change here, that line is just patch context. All the patch does is also include that case in the comment next to the check.

Quoted 10 lines

  I can't see how this change avoids regressing the status quo.

Qemu really does set this flag for option ROMs, which will typically be the iPXE image for net booting.  Currently, netbooting an HVM guest with device passed through to it does work, because it is part of XenServers basic testing.

It is entirely possible that this only "works" as long as DMA doesn't touch the option ROM, and while this might not be ideal, it seems to be less bad behaviour than breaking existing VMs using this configuration.

Hmm, yes, I have to admit I didn't consider this case, mis-remembering that HVMMEM_ram_ro would be more "special" than it really is. I guess I'll withdraw the patch then.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel