Skip to contentSkip to conversation

Performance regression due to XSA-336

PATCH Superseded Boris Ostrovsky 1 participant 2 messages Mar 24, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Performance regression due to XSA-336

While running performance tests with recent XSAs backports to our product we've discovered significant regression in TPCC performance. With a particular guest kernel the numbers dropped by as much as 40%.

We've narrowed that down to XSA-336 patch, specifically to the pt_migrate rwlock, and even more specifically to this lock being taken in pt_update_irq().

We have quite a large guest (92 VCPUs) doing lots of VMEXITs and the theory is that lock's cnts atomic is starting to cause lots of coherence traffic. As a quick test of this replacing pt_vcpu_lock() in pt_update_irq() with just spin_lock(&v->arch.hvm_vcpu.tm_lock) gets us almost all performance back.

Stephen Brennan came up with new locking algorithm, I just coded it up.

A couple of notes:

* We have only observed the problem and tested this patch for performance on a fairly old Xen version. However, vpt code is almost identical and I expect upstream to suffer from the same issue.

* Stephen provided the following (slightly edited by me) writeup explaining the locking algorithm. I would like to include it somewhere but not sure what the right place would be. Commit message perhaps?

Currently, every periodic_time is protected by locking the vcpu it is on. You can think of the per-vCPU lock (tm_lock) as protecting the fields of every periodic_time which is attached to that vCPU, as well as the list itself, and so it must be held when read or written, or when an object is added or removed to/from the list.

It seems that there are three types of access to the peridic_time objects:

1. Functions which read (maybe write) all periodic_time instances attached to a particular vCPU. These are functions which use pt_vcpu_lock() after the commit, such as pt_restore_timer(), pt_save_timer(), etc.
2. Functions which want to modify a particular periodic_time object. These guys lock whichever vCPU the periodic_time is attached to, but since the vCPU could be modified without holding any lock, they are vulnerable to the bug. Functions in this group use pt_lock(), such as pt_timer_fn() or destroy_periodic_time().
3. Functions which not only want to modify the periodic_time, but also would like to modify the =vcpu= fields. These are create_periodic_time() or pt_adjust_vcpu(). They create the locking imbalance bug for group 2, but we can't simply hold 2 vcpu locks due to the deadlock risk.

My proposed option is to add a per-periodic_time spinlock, which protects only the periodic_time.vcpu field. Whenever reading the vcpu field of a periodic_time struct, you must first take that lock. The critical sections of group 1 (your "fast path" functions) would look like this:

1. lock vcpu
2. do whatever you want with pts currently on the vcpu. It is safe to read or write fields of pt, because the vcpu lock protects those fields. You simply cannot write pt->vcpu, because somebody holding the pt lock may already be spinning waiting for your vcpu lock.
3. unlock vcpu

Note that there is no additional locking in this fast path. For group 2 functions (which are attempting to lock an individual periodic_time), the critical section would look like this:

1. lock pt lock (stabilizing the vcpu field)
2. lock vcpu
3. feel free to modify any field of the periodic_time
4. unlock vcpu (due to the mutual exclusion of the pt lock, we know that we are unlocking the correct vcpu -- we have not been migrated)
5. unlock pt

For functions in group 3, the critical section would be:

1. lock pt (stabilizing the vcpu field)
2. lock current vcpu
3. remove from vcpu list
4. unlock vcpu. At this point, you're guaranteed that the vcpu functions (callers of pt_vcpu_lock()) are not accessing your pt.
5. assign pt->vcpu (we still have mutual exclusion against group 2 functions)
6. lock destination vcpu
7. add to vcpu list
8. unlock destination vcpu
9. unlock pt

If functions from group 2 and 3 are less frequent, then you won't see too much added lock overhead in this situation! Plus, even if group 2 and 3 are somewhat common, the performance overhead of an uncontented fine-grained lock is muuch smaller than the overhead of a heavily contended coarse-grained lock, like the per-domain rw lock.

Boris Ostrovsky (1):
x86/vpt: Replace per-guest pt_migrate lock with per pt lock

 xen/arch/x86/emul-i8254.c     |   2 +
 xen/arch/x86/hvm/hpet.c       |   1 +
 xen/arch/x86/hvm/hvm.c        |   2 -
 xen/arch/x86/hvm/rtc.c        |   1 +
 xen/arch/x86/hvm/vlapic.c     |   1 +
 xen/arch/x86/hvm/vpt.c        | 122 +++++++++++++++++++++++-------------------
 xen/include/asm-x86/hvm/vpt.h |   9 +---
 7 files changed, 74 insertions(+), 64 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/1 x86/vpt: Replace per-guest pt_migrate lock with per pt lock

Commit 8e76aef72820 ("x86/vpt: fix race when migrating timers between vCPUs") addressed XSA-336 by introducing a per-domain rwlock that was intended to protect periodic timer during VCPU migration. Since such migration is an infrequent event no performance impact was expected.

Unfortunately this turned out not to be the case: on a fairly large guest (92 VCPUs) we've observed as much as 40% TPCC performance regression with some guest kernels. Further investigation pointed to pt_migrate read lock taken in pt_update_irq() as the largest contributor to this regression. With large number of VCPUs and large number of VMEXITs (from where pt_update_irq() is always called) the update of an atomic in read_lock() is thought to be the main cause.

Stephen Brennan examined the locking pattern and suggested using a per-timer lock instead. This lock will need to be held whenever there is a chance that pt->vcpu field may change (thus avoiding XSA-336 condition).

---
 xen/arch/x86/emul-i8254.c     |   2 +
 xen/arch/x86/hvm/hpet.c       |   1 +
 xen/arch/x86/hvm/hvm.c        |   2 -
 xen/arch/x86/hvm/rtc.c        |   1 +
 xen/arch/x86/hvm/vlapic.c     |   1 +
 xen/arch/x86/hvm/vpt.c        | 122 +++++++++++++++++++++++-------------------
 xen/include/asm-x86/hvm/vpt.h |   9 +---
 7 files changed, 74 insertions(+), 64 deletions(-)
xen/arch/x86/emul-i8254.c +2 -0
index 73be4188ad41..d83e727ff35e 100644
@@ -478,6 +478,8 @@ void pit_init(struct domain *d, unsigned long cpu_khz)
     if ( !has_vpit(d) )
         return;
 
+    spin_lock_init(&pit->pt0.lock);
+
     spin_lock_init(&pit->lock);
 
     if ( is_hvm_domain(d) )
xen/arch/x86/hvm/hpet.c +1 -0
index ca94e8b4538c..c7f45412164e 100644
@@ -734,6 +734,7 @@ static void hpet_set(HPETState *h)
         h->hpet.timers[i].cmp = ~0ULL;
         h->hpet.comparator64[i] = ~0ULL;
         h->pt[i].source = PTSRC_isa;
+        spin_lock_init(&h->pt[i].lock);
     }
 }
 
xen/arch/x86/hvm/hvm.c +0 -2
index e7bcffebc490..b60549a12a33 100644
@@ -658,8 +658,6 @@ int hvm_domain_initialise(struct domain *d)
     /* need link to containing domain */
     d->arch.hvm.pl_time->domain = d;
 
-    rwlock_init(&d->arch.hvm.pl_time->pt_migrate);
-
     /* Set the default IO Bitmap. */
     if ( is_hardware_domain(d) )
     {
xen/arch/x86/hvm/rtc.c +1 -0
index 3150f5f1479b..6289d972bb67 100644
@@ -842,6 +842,7 @@ void rtc_init(struct domain *d)
     }
 
     spin_lock_init(&s->lock);
+    spin_lock_init(&s->pt.lock);
 
     init_timer(&s->update_timer, rtc_update_timer, s, smp_processor_id());
     init_timer(&s->update_timer2, rtc_update_timer2, s, smp_processor_id());
xen/arch/x86/hvm/vlapic.c +1 -0
index 5e21fb4937d9..8413e41a7a80 100644
@@ -1626,6 +1626,7 @@ int vlapic_init(struct vcpu *v)
     vlapic_reset(vlapic);
 
     spin_lock_init(&vlapic->esr_lock);
+    spin_lock_init(&vlapic->pt.lock);
 
     tasklet_init(&vlapic->init_sipi.tasklet, vlapic_init_sipi_action, v);
 
xen/arch/x86/hvm/vpt.c +67 -55

250 lines not on this page · raw message

xen/include/asm-x86/hvm/vpt.h +2 -7
index 39d26cbda496..b0f4af25828b 100644
@@ -49,6 +49,8 @@ struct periodic_time {
     u64 last_plt_gtime;         /* platform time when last IRQ is injected */
     struct timer timer;         /* ac_timer */
     time_cb *cb;
+    spinlock_t lock;            /* protects vcpu field during PT migration. */
+                                /* Needs to be taken before VCPU's tm_lock. */
     void *priv;                 /* point back to platform time source */
 };
 
@@ -128,13 +130,6 @@ struct pl_time {    /* platform time */
     struct RTCState  vrtc;
     struct HPETState vhpet;
     struct PMTState  vpmt;
-    /*
-     * rwlock to prevent periodic_time vCPU migration. Take the lock in read
-     * mode in order to prevent the vcpu field of periodic_time from changing.
-     * Lock must be taken in write mode when changes to the vcpu field are
-     * performed, as it allows exclusive access to all the timers of a domain.
-     */
-    rwlock_t pt_migrate;
     /* guest_time = Xen sys time + stime_offset */
     int64_t stime_offset;
     /* Ensures monotonicity in appropriate timer modes. */
Signature · 1 line
1.8.3.1