Skip to contentSkip to conversation

xen/domain: fix domain_pause() and domain_unpause()

PATCH Superseded Juergen Gross 2 participants 3 messages Dec 2

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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 actions in domain_pause() can be skipped, too, if the domain was paused already.

Fixes f6a07643e1cc ("x86/viridian: freeze time reference counter when domain is paused")

---
V2:
- always call arch_domain_pause() (Jan Beulich)

---
 xen/common/domain.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
xen/common/domain.c +9 -6
index 0bef436ef3..b2af89fe51 100644
@@ -1625,15 +1625,16 @@ static void _domain_pause(struct domain *d, bool sync)
 {
     struct vcpu *v;
 
-    atomic_inc(&d->pause_count);
-
-    for_each_vcpu ( d, v )
-        vcpu_sleep_nosync(v);
+    if ( atomic_inc_and_test(&d->pause_count) )
+        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);
 
+    /* 2 nosync calls might race with each other, so do below call always. */
     arch_domain_pause(d);
 }
 
@@ -1652,11 +1653,13 @@ void domain_unpause(struct domain *d)
 {
     struct vcpu *v;
 
-    arch_domain_unpause(d);
-
     if ( atomic_dec_and_test(&d->pause_count) )
+    {
+        arch_domain_unpause(d);
+
         for_each_vcpu( d, v )
             vcpu_wake(v);
+    }
 }
 
 static int _domain_pause_by_systemcontroller(struct domain *d, bool sync)
Signature · 1 line
2.56.0
#1 lore raw
Quoted 6 lines · Juergen Gross

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 actions in domain_pause() can be skipped, too, if the domain was paused already.

The description feels somewhat stale with the v2 change. As does the title - domain_pause() is only being improved, but not really fixed in any way.

Quoted 1 line
Fixes f6a07643e1cc ("x86/viridian: freeze time reference counter when domain is paused")

As to the particular issue there: Isn't there then also a race between domain_pause() and domain_unpause()? Viridian's thaw may run while its freeze hasn't finished (possibly not even started) yet.

xen/common/domain.c static void _domain_pause(struct domain *d, bool sync) · line 1628

Quoted 11 lines
xen/common/domain.c +1 -4
@@ -1625,15 +1625,16 @@ static void _domain_pause(struct domain *d, bool sync)
 {
     struct vcpu *v;
 
-    atomic_inc(&d->pause_count);
-
-    for_each_vcpu ( d, v )
-        vcpu_sleep_nosync(v);
+    if ( atomic_inc_and_test(&d->pause_count) )

Doesn't this need to be "if ( atomic_inc_return(&d->pause_count) == 1 )"? Right now you're checking whether ->pause_count is zero _after_ the increment, just like ...

xen/common/domain.c void domain_unpause(struct domain *d) · line 1656

Quoted 7 lines
xen/common/domain.c +0 -2
@@ -1652,11 +1653,13 @@ void domain_unpause(struct domain *d)
 {
     struct vcpu *v;
 
-    arch_domain_unpause(d);
-
     if ( atomic_dec_and_test(&d->pause_count) )

... it's an "after decrement" check here.

As to the comment you add ahead of the arch_domain_pause() call: It doesn't need to be two nosync calls. Two sync calls can equally race, as in one of the two callers regaining control before the arch function has finished running.

Jan

#2 lore raw
Quoted 44 lines · Jan Beulich
Quoted 6 lines · Juergen Gross

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 actions in domain_pause() can be skipped, too, if the domain was paused already.

The description feels somewhat stale with the v2 change. As does the title - domain_pause() is only being improved, but not really fixed in any way.

Quoted 1 line
Fixes f6a07643e1cc ("x86/viridian: freeze time reference counter when domain is paused")

As to the particular issue there: Isn't there then also a race between domain_pause() and domain_unpause()? Viridian's thaw may run while its freeze hasn't finished (possibly not even started) yet.

xen/common/domain.c static void _domain_pause(struct domain *d, bool sync) · line 1628

Quoted 11 lines
xen/common/domain.c +1 -4
@@ -1625,15 +1625,16 @@ static void _domain_pause(struct domain *d, bool sync)
  {
      struct vcpu *v;
 
-    atomic_inc(&d->pause_count);
-
-    for_each_vcpu ( d, v )
-        vcpu_sleep_nosync(v);
+    if ( atomic_inc_and_test(&d->pause_count) )

Doesn't this need to be "if ( atomic_inc_return(&d->pause_count) == 1 )"? Right now you're checking whether ->pause_count is zero _after_ the increment, just like ...

xen/common/domain.c void domain_unpause(struct domain *d) · line 1656

Quoted 7 lines
xen/common/domain.c +0 -2
@@ -1652,11 +1653,13 @@ void domain_unpause(struct domain *d)
  {
      struct vcpu *v;
 
-    arch_domain_unpause(d);
-
      if ( atomic_dec_and_test(&d->pause_count) )

... it's an "after decrement" check here.

Quoted 4 lines

As to the comment you add ahead of the arch_domain_pause() call: It doesn't need to be two nosync calls. Two sync calls can equally race, as in one of the two callers regaining control before the arch function has finished running.

All of that suggests that an atomic counter isn't the correct solution here, a lock would probably be better. As domain_pause isn't a critical path, the potential performance impact should be neglectable.

Juergen