Skip to contentSkip to conversation

x86/PoD: defer nested P2M flushes

PATCH Superseded Jan Beulich 2 participants 12 messages Oct 11, 2021 – Oct 19, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/PoD: defer nested P2M flushes

With NPT or shadow in use, the p2m_set_entry() -> p2m_pt_set_entry() -> write_p2m_entry() -> p2m_flush_nestedp2m() call sequence triggers a lock order violation when the PoD lock is held around it. Hence such flushing needs to be deferred. Steal the approach from p2m_change_type_range().

Similarly for EPT I think ept_set_entry() -> ept_sync_domain() -> ept_sync_domain_prepare() -> p2m_flush_nestedp2m() is affected.

xen/arch/x86/mm/p2m-pod.c +22 -6
@@ -24,6 +24,7 @@
 #include <xen/mm.h>
 #include <xen/sched.h>
 #include <xen/trace.h>
+#include <asm/hvm/nestedhvm.h>
 #include <asm/page.h>
 #include <asm/paging.h>
 #include <asm/p2m.h>
@@ -494,6 +495,13 @@ p2m_pod_offline_or_broken_replace(struct
 static int
 p2m_pod_zero_check_superpage(struct p2m_domain *p2m, gfn_t gfn);
 
+static void pod_unlock_and_flush(struct p2m_domain *p2m)
+{
+    pod_unlock(p2m);
+    p2m->defer_nested_flush = false;
+    if ( nestedhvm_enabled(p2m->domain) )
+        p2m_flush_nestedp2m(p2m->domain);
+}
 
 /*
  * This function is needed for two reasons:
@@ -514,6 +522,7 @@ p2m_pod_decrease_reservation(struct doma
 
     gfn_lock(p2m, gfn, order);
     pod_lock(p2m);
+    p2m->defer_nested_flush = true;
 
     /*
      * If we don't have any outstanding PoD entries, let things take their
@@ -665,7 +674,7 @@ out_entry_check:
     }
 
 out_unlock:
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     gfn_unlock(p2m, gfn, order);
     return ret;
 }
@@ -1144,8 +1153,10 @@ p2m_pod_demand_populate(struct p2m_domai
      * won't start until we're done.
      */
     if ( unlikely(d->is_dying) )
-        goto out_fail;
-
+    {
+        pod_unlock(p2m);
+        return false;
+    }
 
     /*
      * Because PoD does not have cache list for 1GB pages, it has to remap
@@ -1167,6 +1178,8 @@ p2m_pod_demand_populate(struct p2m_domai
                               p2m_populate_on_demand, p2m->default_access);
     }
 
+    p2m->defer_nested_flush = true;
+
     /* Only reclaim if we're in actual need of more cache. */
     if ( p2m->pod.entry_count > p2m->pod.count )
         pod_eager_reclaim(p2m);
@@ -1229,8 +1242,9 @@ p2m_pod_demand_populate(struct p2m_domai
         __trace_var(TRC_MEM_POD_POPULATE, 0, sizeof(t), &t);
     }
 
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     return true;
+
 out_of_memory:
     pod_unlock(p2m);
 
@@ -1239,12 +1253,14 @@ out_of_memory:
            p2m->pod.entry_count, current->domain->domain_id);
     domain_crash(d);
     return false;
+
 out_fail:
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     return false;
+
 remap_and_retry:
     BUG_ON(order != PAGE_ORDER_2M);
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
 
     /*
      * Remap this 2-meg region in singleton chunks. See the comment on the
#1 lore raw

Ping: [PATCH] x86/PoD: defer nested P2M flushes

Quoted 10 lines · Jan Beulich

With NPT or shadow in use, the p2m_set_entry() -> p2m_pt_set_entry() -> write_p2m_entry() -> p2m_flush_nestedp2m() call sequence triggers a lock order violation when the PoD lock is held around it. Hence such flushing needs to be deferred. Steal the approach from p2m_change_type_range().

Similarly for EPT I think ept_set_entry() -> ept_sync_domain() -> ept_sync_domain_prepare() -> p2m_flush_nestedp2m() is affected.

Thoughts?

Thanks, Jan

Quoted 93 lines
xen/arch/x86/mm/p2m-pod.c +22 -6
@@ -24,6 +24,7 @@
 #include <xen/mm.h>
 #include <xen/sched.h>
 #include <xen/trace.h>
+#include <asm/hvm/nestedhvm.h>
 #include <asm/page.h>
 #include <asm/paging.h>
 #include <asm/p2m.h>
@@ -494,6 +495,13 @@ p2m_pod_offline_or_broken_replace(struct
 static int
 p2m_pod_zero_check_superpage(struct p2m_domain *p2m, gfn_t gfn);
 
+static void pod_unlock_and_flush(struct p2m_domain *p2m)
+{
+    pod_unlock(p2m);
+    p2m->defer_nested_flush = false;
+    if ( nestedhvm_enabled(p2m->domain) )
+        p2m_flush_nestedp2m(p2m->domain);
+}
 
 /*
  * This function is needed for two reasons:
@@ -514,6 +522,7 @@ p2m_pod_decrease_reservation(struct doma
 
     gfn_lock(p2m, gfn, order);
     pod_lock(p2m);
+    p2m->defer_nested_flush = true;
 
     /*
      * If we don't have any outstanding PoD entries, let things take their
@@ -665,7 +674,7 @@ out_entry_check:
     }
 
 out_unlock:
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     gfn_unlock(p2m, gfn, order);
     return ret;
 }
@@ -1144,8 +1153,10 @@ p2m_pod_demand_populate(struct p2m_domai
      * won't start until we're done.
      */
     if ( unlikely(d->is_dying) )
-        goto out_fail;
-
+    {
+        pod_unlock(p2m);
+        return false;
+    }
 
     /*
      * Because PoD does not have cache list for 1GB pages, it has to remap
@@ -1167,6 +1178,8 @@ p2m_pod_demand_populate(struct p2m_domai
                               p2m_populate_on_demand, p2m->default_access);
     }
 
+    p2m->defer_nested_flush = true;
+
     /* Only reclaim if we're in actual need of more cache. */
     if ( p2m->pod.entry_count > p2m->pod.count )
         pod_eager_reclaim(p2m);
@@ -1229,8 +1242,9 @@ p2m_pod_demand_populate(struct p2m_domai
         __trace_var(TRC_MEM_POD_POPULATE, 0, sizeof(t), &t);
     }
 
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     return true;
+
 out_of_memory:
     pod_unlock(p2m);
 
@@ -1239,12 +1253,14 @@ out_of_memory:
            p2m->pod.entry_count, current->domain->domain_id);
     domain_crash(d);
     return false;
+
 out_fail:
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     return false;
+
 remap_and_retry:
     BUG_ON(order != PAGE_ORDER_2M);
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
 
     /*
      * Remap this 2-meg region in singleton chunks. See the comment on the
#2 lore raw
In reply to Jan Beulich
Quoted 75 lines · Jan Beulich

With NPT or shadow in use, the p2m_set_entry() -> p2m_pt_set_entry() -> write_p2m_entry() -> p2m_flush_nestedp2m() call sequence triggers a lock order violation when the PoD lock is held around it. Hence such flushing needs to be deferred. Steal the approach from p2m_change_type_range().

Similarly for EPT I think ept_set_entry() -> ept_sync_domain() -> ept_sync_domain_prepare() -> p2m_flush_nestedp2m() is affected.

xen/arch/x86/mm/p2m-pod.c +16 -3
@@ -24,6 +24,7 @@
 #include <xen/mm.h>
 #include <xen/sched.h>
 #include <xen/trace.h>
+#include <asm/hvm/nestedhvm.h>
 #include <asm/page.h>
 #include <asm/paging.h>
 #include <asm/p2m.h>
@@ -494,6 +495,13 @@ p2m_pod_offline_or_broken_replace(struct
 static int
 p2m_pod_zero_check_superpage(struct p2m_domain *p2m, gfn_t gfn);
 
+static void pod_unlock_and_flush(struct p2m_domain *p2m)
+{
+    pod_unlock(p2m);
+    p2m->defer_nested_flush = false;
+    if ( nestedhvm_enabled(p2m->domain) )
+        p2m_flush_nestedp2m(p2m->domain);
+}
 
 /*
  * This function is needed for two reasons:
@@ -514,6 +522,7 @@ p2m_pod_decrease_reservation(struct doma
 
     gfn_lock(p2m, gfn, order);
     pod_lock(p2m);
+    p2m->defer_nested_flush = true;
 
     /*
      * If we don't have any outstanding PoD entries, let things take their
@@ -665,7 +674,7 @@ out_entry_check:
     }
 
 out_unlock:
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     gfn_unlock(p2m, gfn, order);
     return ret;
 }
@@ -1144,8 +1153,10 @@ p2m_pod_demand_populate(struct p2m_domai
      * won't start until we're done.
      */
     if ( unlikely(d->is_dying) )
-        goto out_fail;
-
+    {
+        pod_unlock(p2m);
+        return false;
+    }
 
     /*
      * Because PoD does not have cache list for 1GB pages, it has to remap
@@ -1167,6 +1178,8 @@ p2m_pod_demand_populate(struct p2m_domai
                               p2m_populate_on_demand, p2m->default_access);
     }
 
+    p2m->defer_nested_flush = true;
+
     /* Only reclaim if we're in actual need of more cache. */
     if ( p2m->pod.entry_count > p2m->pod.count )
         pod_eager_reclaim(p2m);

xen/arch/x86/mm/p2m-pod.c p2m_pod_demand_populate(struct p2m_domai · line 1249

Quoted 9 lines
xen/arch/x86/mm/p2m-pod.c +2 -1
@@ -1229,8 +1242,9 @@ p2m_pod_demand_populate(struct p2m_domai
         __trace_var(TRC_MEM_POD_POPULATE, 0, sizeof(t), &t);
     }
 
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     return true;
+
 out_of_memory:
     pod_unlock(p2m);

Don't you need to set defer_nested_flush = false in the out_of_memory label? (as you don't call pod_unlock_and_flush that would do it)

Thanks, Roger.

#3 lore raw
Quoted 5 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich
xen/arch/x86/mm/p2m-pod.c +0 -0
@@ -1229,8 +1242,9 @@ p2m_pod_demand_populate(struct p2m_domai
         __trace_var(TRC_MEM_POD_POPULATE, 0, sizeof(t), &t);
     }
 
Quoted 9 lines

xen/arch/x86/mm/p2m-pod.c p2m_pod_demand_populate(struct p2m_domai · line 1249

Quoted 6 lines
xen/arch/x86/mm/p2m-pod.c +2 -1
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     return true;
+
 out_of_memory:
     pod_unlock(p2m);

Don't you need to set defer_nested_flush = false in the out_of_memory label? (as you don't call pod_unlock_and_flush that would do it)

Yes of course - thanks for spotting. I had pod_unlock_and_flush() here too initially, and when switching back I forgot to convert rather than just delete that.

Jan

#4 lore raw
Quoted 16 lines · Jan Beulich
Quoted 5 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich
xen/arch/x86/mm/p2m-pod.c +0 -0
@@ -1229,8 +1242,9 @@ p2m_pod_demand_populate(struct p2m_domai
         __trace_var(TRC_MEM_POD_POPULATE, 0, sizeof(t), &t);
     }
 
Quoted 9 lines

xen/arch/x86/mm/p2m-pod.c p2m_pod_demand_populate(struct p2m_domai · line 1249

Quoted 6 lines
xen/arch/x86/mm/p2m-pod.c +2 -1
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     return true;
+
 out_of_memory:
     pod_unlock(p2m);

Don't you need to set defer_nested_flush = false in the out_of_memory label? (as you don't call pod_unlock_and_flush that would do it)

Quoted 3 lines

Yes of course - thanks for spotting. I had pod_unlock_and_flush() here too initially, and when switching back I forgot to convert rather than just delete that.

Oh, wait, that was on purpose: There's no point clearing the flag when the next thing we do is invoke domain_crash(). If it wasn't that way, I don't think I could avoid using pod_unlock_and_flush() here as well.

Jan

#5 lore raw
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Jan Beulich
Quoted 14 lines · Roger Pau Monné

xen/arch/x86/mm/p2m-pod.c p2m_pod_demand_populate(struct p2m_domai · line 1249

Quoted 10 lines · Jan Beulich
xen/arch/x86/mm/p2m-pod.c +2 -1
@@ -1229,8 +1242,9 @@ p2m_pod_demand_populate(struct p2m_domai
         __trace_var(TRC_MEM_POD_POPULATE, 0, sizeof(t), &t);
     }
 
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     return true;
+
 out_of_memory:
     pod_unlock(p2m);

Don't you need to set defer_nested_flush = false in the out_of_memory label? (as you don't call pod_unlock_and_flush that would do it)

Quoted 8 lines
Quoted 3 lines

Yes of course - thanks for spotting. I had pod_unlock_and_flush() here too initially, and when switching back I forgot to convert rather than just delete that.

Oh, wait, that was on purpose: There's no point clearing the flag when the next thing we do is invoke domain_crash(). If it wasn't that way, I don't think I could avoid using pod_unlock_and_flush() here as well.

Oh, I see. We would need to be careful if that domain crash is ever removed.

Thanks, Roger.

#6 lore raw
Quoted 22 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 19 lines · Jan Beulich
Quoted 5 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich
xen/arch/x86/mm/p2m-pod.c +0 -0
@@ -1229,8 +1242,9 @@ p2m_pod_demand_populate(struct p2m_domai
         __trace_var(TRC_MEM_POD_POPULATE, 0, sizeof(t), &t);
     }
 
Quoted 9 lines

xen/arch/x86/mm/p2m-pod.c p2m_pod_demand_populate(struct p2m_domai · line 1249

Quoted 6 lines
xen/arch/x86/mm/p2m-pod.c +2 -1
-    pod_unlock(p2m);
+    pod_unlock_and_flush(p2m);
     return true;
+
 out_of_memory:
     pod_unlock(p2m);

Don't you need to set defer_nested_flush = false in the out_of_memory label? (as you don't call pod_unlock_and_flush that would do it)

Yes of course - thanks for spotting. I had pod_unlock_and_flush() here too initially, and when switching back I forgot to convert rather than just delete that.

Quoted 7 lines
Quoted 4 lines

Oh, wait, that was on purpose: There's no point clearing the flag when the next thing we do is invoke domain_crash(). If it wasn't that way, I don't think I could avoid using pod_unlock_and_flush() here as well.

Oh, I see. We would need to be careful if that domain crash is ever removed.

Well, I can change the call there as well. Doing so would seem preferable over adding a respective comment there. I didn't do so originally because as it stands this would be meaningless code churn. Let me know what you think.

Jan

#7 lore raw
In reply to Jan Beulich
Quoted 7 lines · Jan Beulich

With NPT or shadow in use, the p2m_set_entry() -> p2m_pt_set_entry() -> write_p2m_entry() -> p2m_flush_nestedp2m() call sequence triggers a lock order violation when the PoD lock is held around it. Hence such flushing needs to be deferred. Steal the approach from p2m_change_type_range().

Similarly for EPT I think ept_set_entry() -> ept_sync_domain() -> ept_sync_domain_prepare() -> p2m_flush_nestedp2m() is affected.

I'm slightly worried by this path because it doesn't seem to acknowledge defer_nested_flush. Maybe the flag should be checked by p2m_flush_nestedp2m instead of leaving it to the callers?

Thanks, Roger.

#8 lore raw
Quoted 11 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich

With NPT or shadow in use, the p2m_set_entry() -> p2m_pt_set_entry() -> write_p2m_entry() -> p2m_flush_nestedp2m() call sequence triggers a lock order violation when the PoD lock is held around it. Hence such flushing needs to be deferred. Steal the approach from p2m_change_type_range().

Similarly for EPT I think ept_set_entry() -> ept_sync_domain() -> ept_sync_domain_prepare() -> p2m_flush_nestedp2m() is affected.

I'm slightly worried by this path because it doesn't seem to acknowledge defer_nested_flush.

Oh, yes. Iirc I did look at that logic, write down the remark, and then forgot to add the conditional to ept_sync_domain_prepare(). The interactions with the real (host) flush are kind of ugly there, though - we then further depend on the ->defer_flush / ->need_flush logic, which is EPT-only. But I think I've convinced myself that this ought to work out.

Quoted 2 lines

Maybe the flag should be checked by p2m_flush_nestedp2m instead of leaving it to the callers?

I'm not sure this would be a good idea, as there are more callers.

Jan

#9 lore raw
Quoted 20 lines · Jan Beulich
Quoted 11 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich

With NPT or shadow in use, the p2m_set_entry() -> p2m_pt_set_entry() -> write_p2m_entry() -> p2m_flush_nestedp2m() call sequence triggers a lock order violation when the PoD lock is held around it. Hence such flushing needs to be deferred. Steal the approach from p2m_change_type_range().

Similarly for EPT I think ept_set_entry() -> ept_sync_domain() -> ept_sync_domain_prepare() -> p2m_flush_nestedp2m() is affected.

I'm slightly worried by this path because it doesn't seem to acknowledge defer_nested_flush.

Oh, yes. Iirc I did look at that logic, write down the remark, and then forgot to add the conditional to ept_sync_domain_prepare(). The interactions with the real (host) flush are kind of ugly there, though - we then further depend on the ->defer_flush / ->need_flush logic, which is EPT-only. But I think I've convinced myself that this ought to work out.

Quoted 4 lines
Quoted 2 lines

Maybe the flag should be checked by p2m_flush_nestedp2m instead of leaving it to the callers?

I'm not sure this would be a good idea, as there are more callers.

We should maybe add an ASSERT to p2m_flush_nestedp2m to make sure it's not called with defer_nested_flush being set then, or else it's possible for new callers of p2m_flush_nestedp2m to forget checking defer_nested_flush.

Thanks, Roger.

#10 lore raw
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 11 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich

With NPT or shadow in use, the p2m_set_entry() -> p2m_pt_set_entry() -> write_p2m_entry() -> p2m_flush_nestedp2m() call sequence triggers a lock order violation when the PoD lock is held around it. Hence such flushing needs to be deferred. Steal the approach from p2m_change_type_range().

Similarly for EPT I think ept_set_entry() -> ept_sync_domain() -> ept_sync_domain_prepare() -> p2m_flush_nestedp2m() is affected.

I'm slightly worried by this path because it doesn't seem to acknowledge defer_nested_flush.

Oh, yes. Iirc I did look at that logic, write down the remark, and then forgot to add the conditional to ept_sync_domain_prepare(). The interactions with the real (host) flush are kind of ugly there, though - we then further depend on the ->defer_flush / ->need_flush logic, which is EPT-only. But I think I've convinced myself that this ought to work out.

Quoted 2 lines

Maybe the flag should be checked by p2m_flush_nestedp2m instead of leaving it to the callers?

Quoted 6 lines
Quoted 1 line

I'm not sure this would be a good idea, as there are more callers.

We should maybe add an ASSERT to p2m_flush_nestedp2m to make sure it's not called with defer_nested_flush being set then, or else it's possible for new callers of p2m_flush_nestedp2m to forget checking defer_nested_flush.

I'm afraid we can't do that, or at least not this easily: The flag assumes the p2m lock to be held when it gets set. Hence callers not holding the lock (hvm_set_efer(), nvmx_handle_invept()) may trigger such an assert just because another CPU set the flag.

Jan

#11 lore raw
Quoted 32 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 11 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich

With NPT or shadow in use, the p2m_set_entry() -> p2m_pt_set_entry() -> write_p2m_entry() -> p2m_flush_nestedp2m() call sequence triggers a lock order violation when the PoD lock is held around it. Hence such flushing needs to be deferred. Steal the approach from p2m_change_type_range().

Similarly for EPT I think ept_set_entry() -> ept_sync_domain() -> ept_sync_domain_prepare() -> p2m_flush_nestedp2m() is affected.

I'm slightly worried by this path because it doesn't seem to acknowledge defer_nested_flush.

Oh, yes. Iirc I did look at that logic, write down the remark, and then forgot to add the conditional to ept_sync_domain_prepare(). The interactions with the real (host) flush are kind of ugly there, though - we then further depend on the ->defer_flush / ->need_flush logic, which is EPT-only. But I think I've convinced myself that this ought to work out.

Quoted 2 lines

Maybe the flag should be checked by p2m_flush_nestedp2m instead of leaving it to the callers?

Quoted 6 lines
Quoted 1 line

I'm not sure this would be a good idea, as there are more callers.

We should maybe add an ASSERT to p2m_flush_nestedp2m to make sure it's not called with defer_nested_flush being set then, or else it's possible for new callers of p2m_flush_nestedp2m to forget checking defer_nested_flush.

Quoted 4 lines

I'm afraid we can't do that, or at least not this easily: The flag assumes the p2m lock to be held when it gets set. Hence callers not holding the lock (hvm_set_efer(), nvmx_handle_invept()) may trigger such an assert just because another CPU set the flag.

Hm, indeed. Forcing those to take the p2m lock might be too much overhead for the sake of correctness.

Thanks, Roger.