[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3 1/2] xen/domain: fix domain_pause() and domain_unpause()



Calls of domain_pause() can be nested. So there should be no action
other than handling the paused counter if a nested call is detected.

Especially calling arch_domain_unpause() unconditionally in
domain_unpause() is wrong, but the arch_domain_pause() call in
domain_pause() can be skipped, too, if the domain was paused already.

This requires to drop the scheme of the pause counter being modified
just via atomic updates, as the calling of the arch_domain_[un]pause()
functions needs to be guarded against races, too.

Introduce a pause_lock instead, which is being held across the update
of the pause counter AND the potential call of arch_domain_[un]pause().
While there might still be races with concurrent pause/unpause calls,
the vcpu_sleep() and vcpu_wake() calls can happen outside the lock, as
especially vcpu_wake() is looking at the pause count, too.

Note that a race of domain_pause() seeing an initial count of 0 and
domain_unpause() concurrently setting it back to 0 can't happen, as the
domain_unpause() can be valid only, if the initial count was 1 already.
This means, that there is no risk of the count being 0 at the end AND
vcpus having been put to sleep by an unpause action.

Fixes: f6a07643e1cc ("x86/viridian: freeze time reference counter when domain 
is paused")
Signed-off-by: Juergen Gross <jgross@xxxxxxxx>
---
V3:
- switch to lock
---
 xen/common/domain.c     | 30 ++++++++++++++++++++++++------
 xen/include/xen/sched.h |  1 +
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/xen/common/domain.c b/xen/common/domain.c
index 0bef436ef3..5f183a0d83 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -943,6 +943,7 @@ struct domain *domain_create(domid_t domid,
     rspin_lock_init_prof(d, domain_lock);
     rspin_lock_init_prof(d, page_alloc_lock);
     spin_lock_init(&d->hypercall_deadlock_mutex);
+    spin_lock_init(&d->pause_lock);
     rwlock_init(&d->caps_lock);
     INIT_PAGE_LIST_HEAD(&d->page_list);
     INIT_PAGE_LIST_HEAD(&d->extra_page_list);
@@ -1624,17 +1625,25 @@ int vcpu_unpause_by_systemcontroller(struct vcpu *v)
 static void _domain_pause(struct domain *d, bool sync)
 {
     struct vcpu *v;
+    bool pause;
 
+    spin_lock(&d->pause_lock);
+
+    pause = !atomic_read(&d->pause_count);
     atomic_inc(&d->pause_count);
+    if ( pause )
+        arch_domain_pause(d);
 
-    for_each_vcpu ( d, v )
-        vcpu_sleep_nosync(v);
+    spin_unlock(&d->pause_lock);
+
+    if ( pause )
+        for_each_vcpu ( d, v )
+            vcpu_sleep_nosync(v);
 
+    /* Wait might be needed even if already paused, could have been nosync. */
     if ( sync )
         for_each_vcpu ( d, v )
             vcpu_wait_sleep(v);
-
-    arch_domain_pause(d);
 }
 
 void domain_pause(struct domain *d)
@@ -1651,10 +1660,19 @@ void domain_pause_nosync(struct domain *d)
 void domain_unpause(struct domain *d)
 {
     struct vcpu *v;
+    bool unpause;
+
+    spin_lock(&d->pause_lock);
+
+    ASSERT(atomic_read(&d->pause_count));
+    atomic_dec(&d->pause_count);
+    unpause = !atomic_read(&d->pause_count);
+    if ( unpause )
+        arch_domain_unpause(d);
 
-    arch_domain_unpause(d);
+    spin_unlock(&d->pause_lock);
 
-    if ( atomic_dec_and_test(&d->pause_count) )
+    if ( unpause )
         for_each_vcpu( d, v )
             vcpu_wake(v);
 }
diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h
index 29ecc97c11..628521b9ab 100644
--- a/xen/include/xen/sched.h
+++ b/xen/include/xen/sched.h
@@ -573,6 +573,7 @@ struct domain
      * raising DOM_EXC */
     evtchn_port_t    suspend_evtchn;
 
+    spinlock_t       pause_lock;
     atomic_t         pause_count;
     atomic_t         refcnt;
 
-- 
2.56.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.