Skip to contentSkip to conversation

x86/altp2m: propagate ept.ad changes to all active altp2ms

PATCH Superseded Razvan Cojocaru 3 participants 5 messages Oct 1, 2018

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/altp2m: propagate ept.ad changes to all active altp2ms

This patch is a pre-requisite for fixing the logdirty VGA issue (display freezes when switching to a new altp2m view early in a domain's lifetime), but sent separately for easier review. The new ept_set_ad_sync() function has been added to update all active altp2ms' ept.ad. New altp2ms will inherit the hostp2m's ept.ad value. ept_set_ad_sync() is now also the code responsible for locking updated p2ms.

---
Changes since V1:
- Removed unnecessary p2m_lock() in p2m_init_altp2m_ept().
- Changed value's type from int to bool.
- Stopped treating the hostp2m interchangeably with d->arch.altp2m_p2m[0] in ept_set_ad_sync().

---
 xen/arch/x86/mm/p2m-ept.c | 54 ++++++++++++++++++++++++++++++++++++++++-------
 xen/arch/x86/mm/p2m.c     |  8 -------
 2 files changed, 46 insertions(+), 16 deletions(-)
xen/arch/x86/mm/p2m-ept.c +46 -8
index d376966..a5abd85 100644
@@ -17,6 +17,7 @@
 
 #include <xen/domain_page.h>
 #include <xen/sched.h>
+#include <asm/altp2m.h>
 #include <asm/current.h>
 #include <asm/paging.h>
 #include <asm/types.h>
@@ -1218,34 +1219,69 @@ static void ept_tlb_flush(struct p2m_domain *p2m)
     ept_sync_domain_mask(p2m, p2m->domain->dirty_cpumask);
 }
 
+static void ept_set_ad_sync(struct p2m_domain *p2m, bool value)
+{
+    struct domain *d = p2m->domain;
+    struct p2m_domain *hostp2m = p2m_get_hostp2m(d);
+
+    p2m_lock(hostp2m);
+
+    hostp2m->ept.ad = value;
+
+    if ( unlikely(altp2m_active(d)) )
+    {
+        unsigned int i;
+
+        for ( i = 0; i < MAX_ALTP2M; i++ )
+        {
+            if ( d->arch.altp2m_eptp[i] == mfn_x(INVALID_MFN) )
+                continue;
+
+            p2m = d->arch.altp2m_p2m[i];
+
+            p2m_lock(p2m);
+            p2m->ept.ad = value;
+            p2m_unlock(p2m);
+        }
+    }
+
+    p2m_unlock(hostp2m);
+}
+
 static void ept_enable_pml(struct p2m_domain *p2m)
 {
+    struct domain *d = p2m->domain;
+
     /* Domain must have been paused */
-    ASSERT(atomic_read(&p2m->domain->pause_count));
+    ASSERT(atomic_read(&d->pause_count));
 
     /*
      * No need to return whether vmx_domain_enable_pml has succeeded, as
      * ept_p2m_type_to_flags will do the check, and write protection will be
      * used if PML is not enabled.
      */
-    if ( vmx_domain_enable_pml(p2m->domain) )
+    if ( vmx_domain_enable_pml(d) )
         return;
 
     /* Enable EPT A/D bit for PML */
-    p2m->ept.ad = 1;
-    vmx_domain_update_eptp(p2m->domain);
+    ept_set_ad_sync(p2m, true);
+
+    vmx_domain_update_eptp(d);
 }
 
 static void ept_disable_pml(struct p2m_domain *p2m)
 {
+    struct domain *d = p2m->domain;
+
     /* Domain must have been paused */
-    ASSERT(atomic_read(&p2m->domain->pause_count));
+    ASSERT(atomic_read(&d->pause_count));
 
-    vmx_domain_disable_pml(p2m->domain);
+    vmx_domain_disable_pml(d);
 
     /* Disable EPT A/D bit */
-    p2m->ept.ad = 0;
-    vmx_domain_update_eptp(p2m->domain);
+    ept_set_ad_sync(p2m, false);
+
+    vmx_domain_update_eptp(d);
 }
 
 static void ept_flush_pml_buffers(struct p2m_domain *p2m)
@@ -1386,8 +1422,10 @@ void setup_ept_dump(void)
 void p2m_init_altp2m_ept(struct domain *d, unsigned int i)
 {
     struct p2m_domain *p2m = d->arch.altp2m_p2m[i];
+    struct p2m_domain *hostp2m = p2m_get_hostp2m(d);
     struct ept_data *ept;
 
+    p2m->ept.ad = hostp2m->ept.ad;
     p2m->min_remapped_gfn = gfn_x(INVALID_GFN);
     p2m->max_remapped_gfn = 0;
     ept = &p2m->ept;
xen/arch/x86/mm/p2m.c +0 -8
index d6a8810..d90c624 100644
@@ -360,11 +360,7 @@ void p2m_enable_hardware_log_dirty(struct domain *d)
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
     if ( p2m->enable_hardware_log_dirty )
-    {
-        p2m_lock(p2m);
         p2m->enable_hardware_log_dirty(p2m);
-        p2m_unlock(p2m);
-    }
 }
 
 void p2m_disable_hardware_log_dirty(struct domain *d)
@@ -372,11 +368,7 @@ void p2m_disable_hardware_log_dirty(struct domain *d)
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
     if ( p2m->disable_hardware_log_dirty )
-    {
-        p2m_lock(p2m);
         p2m->disable_hardware_log_dirty(p2m);
-        p2m_unlock(p2m);
-    }
 }
 
 void p2m_flush_hardware_cached_dirty(struct domain *d)
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 01.10.18 at 11:58, <[email protected]> wrote:

Changes since V1:
- Removed unnecessary p2m_lock() in p2m_init_altp2m_ept().

This was a step in the right direction, but ...

Quoted 33 lines

static void ept_enable_pml(struct p2m_domain *p2m) {

xen/arch/x86/mm/p2m-ept.c +10 -5
+    struct domain *d = p2m->domain;
+
     /* Domain must have been paused */
-    ASSERT(atomic_read(&p2m->domain->pause_count));
+    ASSERT(atomic_read(&d->pause_count));
 
     /*
      * No need to return whether vmx_domain_enable_pml has succeeded, as
      * ept_p2m_type_to_flags will do the check, and write protection will be
      * used if PML is not enabled.
      */
-    if ( vmx_domain_enable_pml(p2m->domain) )
+    if ( vmx_domain_enable_pml(d) )
         return;
 
     /* Enable EPT A/D bit for PML */
-    p2m->ept.ad = 1;
-    vmx_domain_update_eptp(p2m->domain);
+    ept_set_ad_sync(p2m, true);
+
+    vmx_domain_update_eptp(d);
 }
 
 static void ept_disable_pml(struct p2m_domain *p2m)
 {
+    struct domain *d = p2m->domain;
+
     /* Domain must have been paused */
-    ASSERT(atomic_read(&p2m->domain->pause_count));
+    ASSERT(atomic_read(&d->pause_count));
 

xen/arch/x86/mm/p2m-ept.c static void ept_tlb_flush(struct p2m_domain *p2m) · line 1285

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +4 -3
-    vmx_domain_disable_pml(p2m->domain);
+    vmx_domain_disable_pml(d);
 
     /* Disable EPT A/D bit */
-    p2m->ept.ad = 0;
-    vmx_domain_update_eptp(p2m->domain);
+    ept_set_ad_sync(p2m, false);
+
+    vmx_domain_update_eptp(d);
 }

These two functions used to be called with the p2m lock held, while now they aren't anymore. Afaict this introduces a race where the opposite ept_set_ad_sync() may be called before an original one was follow by the respective vmx_domain_update_eptp(), resulting in the A/D enable bit being set the wrong way round in the end.

I realize that George did already point out that this is sort of ugly a situation, but the fixing of the issue here shouldn't introduce a new race. What's wrong with retaining the host p2m lock in p2m_{en,dis}able_hardware_log_dirty()? ept_set_ad_sync() then simply wouldn't acquire/release that one, but just the altp2m ones.

Jan

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

#2 lore raw
Quoted 57 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 01.10.18 at 11:58, <[email protected]> wrote:

Changes since V1:
- Removed unnecessary p2m_lock() in p2m_init_altp2m_ept().

This was a step in the right direction, but ...

Quoted 33 lines

static void ept_enable_pml(struct p2m_domain *p2m) {

xen/arch/x86/mm/p2m-ept.c +10 -5
+    struct domain *d = p2m->domain;
+
     /* Domain must have been paused */
-    ASSERT(atomic_read(&p2m->domain->pause_count));
+    ASSERT(atomic_read(&d->pause_count));
 
     /*
      * No need to return whether vmx_domain_enable_pml has succeeded, as
      * ept_p2m_type_to_flags will do the check, and write protection will be
      * used if PML is not enabled.
      */
-    if ( vmx_domain_enable_pml(p2m->domain) )
+    if ( vmx_domain_enable_pml(d) )
         return;
 
     /* Enable EPT A/D bit for PML */
-    p2m->ept.ad = 1;
-    vmx_domain_update_eptp(p2m->domain);
+    ept_set_ad_sync(p2m, true);
+
+    vmx_domain_update_eptp(d);
 }
 
 static void ept_disable_pml(struct p2m_domain *p2m)
 {
+    struct domain *d = p2m->domain;
+
     /* Domain must have been paused */
-    ASSERT(atomic_read(&p2m->domain->pause_count));
+    ASSERT(atomic_read(&d->pause_count));
 

xen/arch/x86/mm/p2m-ept.c static void ept_tlb_flush(struct p2m_domain *p2m) · line 1285

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +4 -3
-    vmx_domain_disable_pml(p2m->domain);
+    vmx_domain_disable_pml(d);
 
     /* Disable EPT A/D bit */
-    p2m->ept.ad = 0;
-    vmx_domain_update_eptp(p2m->domain);
+    ept_set_ad_sync(p2m, false);
+
+    vmx_domain_update_eptp(d);
 }

These two functions used to be called with the p2m lock held, while now they aren't anymore. Afaict this introduces a race where the opposite ept_set_ad_sync() may be called before an original one was follow by the respective vmx_domain_update_eptp(), resulting in the A/D enable bit being set the wrong way round in the end.

Quoted 6 lines

I realize that George did already point out that this is sort of ugly a situation, but the fixing of the issue here shouldn't introduce a new race. What's wrong with retaining the host p2m lock in p2m_{en,dis}able_hardware_log_dirty()? ept_set_ad_sync() then simply wouldn't acquire/release that one, but just the altp2m ones.

That is fine with be, in fact the whole change has been prompted by George's remark that "there would something a bit funny here about grabbing the main p2m lock in p2m.c, and then grabbing altp2m locks within the function". If, after these comments, he doesn't mind the scenario then I'll do that in V3.

Thanks,
Razvan

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

#3 lore raw
Quoted 67 lines · Razvan Cojocaru
Quoted 57 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 01.10.18 at 11:58, <[email protected]> wrote:

Changes since V1:
- Removed unnecessary p2m_lock() in p2m_init_altp2m_ept().

This was a step in the right direction, but ...

Quoted 33 lines

static void ept_enable_pml(struct p2m_domain *p2m) {

xen/arch/x86/mm/p2m-ept.c +10 -5
+    struct domain *d = p2m->domain;
+
     /* Domain must have been paused */
-    ASSERT(atomic_read(&p2m->domain->pause_count));
+    ASSERT(atomic_read(&d->pause_count));
 
     /*
      * No need to return whether vmx_domain_enable_pml has succeeded, as
      * ept_p2m_type_to_flags will do the check, and write protection will be
      * used if PML is not enabled.
      */
-    if ( vmx_domain_enable_pml(p2m->domain) )
+    if ( vmx_domain_enable_pml(d) )
         return;
 
     /* Enable EPT A/D bit for PML */
-    p2m->ept.ad = 1;
-    vmx_domain_update_eptp(p2m->domain);
+    ept_set_ad_sync(p2m, true);
+
+    vmx_domain_update_eptp(d);
 }
 
 static void ept_disable_pml(struct p2m_domain *p2m)
 {
+    struct domain *d = p2m->domain;
+
     /* Domain must have been paused */
-    ASSERT(atomic_read(&p2m->domain->pause_count));
+    ASSERT(atomic_read(&d->pause_count));
 

xen/arch/x86/mm/p2m-ept.c static void ept_tlb_flush(struct p2m_domain *p2m) · line 1285

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +4 -3
-    vmx_domain_disable_pml(p2m->domain);
+    vmx_domain_disable_pml(d);
 
     /* Disable EPT A/D bit */
-    p2m->ept.ad = 0;
-    vmx_domain_update_eptp(p2m->domain);
+    ept_set_ad_sync(p2m, false);
+
+    vmx_domain_update_eptp(d);
 }

These two functions used to be called with the p2m lock held, while now they aren't anymore. Afaict this introduces a race where the opposite ept_set_ad_sync() may be called before an original one was follow by the respective vmx_domain_update_eptp(), resulting in the A/D enable bit being set the wrong way round in the end.

Quoted 6 lines

I realize that George did already point out that this is sort of ugly a situation, but the fixing of the issue here shouldn't introduce a new race. What's wrong with retaining the host p2m lock in p2m_{en,dis}able_hardware_log_dirty()? ept_set_ad_sync() then simply wouldn't acquire/release that one, but just the altp2m ones.

Quoted 5 lines

That is fine with be, in fact the whole change has been prompted by George's remark that "there would something a bit funny here about grabbing the main p2m lock in p2m.c, and then grabbing altp2m locks within the function". If, after these comments, he doesn't mind the scenario then I'll do that in V3.

I think I would rather grab the main p2m locks in ept_{enable,disable}_pml(). Wouldn't hurt to have an ASSERT(p2m_is_locked_by_me()) in ept_set_ad_sync() as well.

Thanks,
-George

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

#4 lore raw
Quoted 74 lines · George Dunlap
Quoted 67 lines · Razvan Cojocaru
Quoted 57 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 01.10.18 at 11:58, <[email protected]> wrote:

Changes since V1:
- Removed unnecessary p2m_lock() in p2m_init_altp2m_ept().

This was a step in the right direction, but ...

Quoted 33 lines

static void ept_enable_pml(struct p2m_domain *p2m) {

xen/arch/x86/mm/p2m-ept.c +10 -5
+    struct domain *d = p2m->domain;
+
     /* Domain must have been paused */
-    ASSERT(atomic_read(&p2m->domain->pause_count));
+    ASSERT(atomic_read(&d->pause_count));
 
     /*
      * No need to return whether vmx_domain_enable_pml has succeeded, as
      * ept_p2m_type_to_flags will do the check, and write protection will be
      * used if PML is not enabled.
      */
-    if ( vmx_domain_enable_pml(p2m->domain) )
+    if ( vmx_domain_enable_pml(d) )
         return;
 
     /* Enable EPT A/D bit for PML */
-    p2m->ept.ad = 1;
-    vmx_domain_update_eptp(p2m->domain);
+    ept_set_ad_sync(p2m, true);
+
+    vmx_domain_update_eptp(d);
 }
 
 static void ept_disable_pml(struct p2m_domain *p2m)
 {
+    struct domain *d = p2m->domain;
+
     /* Domain must have been paused */
-    ASSERT(atomic_read(&p2m->domain->pause_count));
+    ASSERT(atomic_read(&d->pause_count));
 

xen/arch/x86/mm/p2m-ept.c static void ept_tlb_flush(struct p2m_domain *p2m) · line 1285

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +4 -3
-    vmx_domain_disable_pml(p2m->domain);
+    vmx_domain_disable_pml(d);
 
     /* Disable EPT A/D bit */
-    p2m->ept.ad = 0;
-    vmx_domain_update_eptp(p2m->domain);
+    ept_set_ad_sync(p2m, false);
+
+    vmx_domain_update_eptp(d);
 }

These two functions used to be called with the p2m lock held, while now they aren't anymore. Afaict this introduces a race where the opposite ept_set_ad_sync() may be called before an original one was follow by the respective vmx_domain_update_eptp(), resulting in the A/D enable bit being set the wrong way round in the end.

Quoted 6 lines

I realize that George did already point out that this is sort of ugly a situation, but the fixing of the issue here shouldn't introduce a new race. What's wrong with retaining the host p2m lock in p2m_{en,dis}able_hardware_log_dirty()? ept_set_ad_sync() then simply wouldn't acquire/release that one, but just the altp2m ones.

Quoted 5 lines

That is fine with be, in fact the whole change has been prompted by George's remark that "there would something a bit funny here about grabbing the main p2m lock in p2m.c, and then grabbing altp2m locks within the function". If, after these comments, he doesn't mind the scenario then I'll do that in V3.

Quoted 3 lines

I think I would rather grab the main p2m locks in ept_{enable,disable}_pml(). Wouldn't hurt to have an ASSERT(p2m_is_locked_by_me()) in ept_set_ad_sync() as well.

I'll do that. Thanks!

Razvan

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