Skip to contentSkip to conversation

x86/viridian: fix calling of viridian_time_domain_{freeze,thaw}()

PATCH In review Roger Pau Monne 2 participants 4 messages Nov 26, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/viridian: fix calling of viridian_time_domain_{freeze,thaw}()

The current hook placement in domain_{,un}pause() calls arch_domain_pause() unconditionally, regardless of whether the domain is already paused or not unpaused (so no state change). However the underlying Viridian unpause function (time_ref_count_thaw()) expects the call to only be done once there's a transition from paused to active.

Adjust the calling of arch_domain_{,un}pause() so it's only done for state changes, to accommodate for the behavior expected by the Viridian hooks. Note there are no other implementations of arch_domain_{,un}pause() apart from the Viridian use cases, and hence the change is non-functional for all other architectures (or for x86 if Viridian is disabled).

Fixes f6a07643e1cc ('x86/viridian: freeze time reference counter when domain is paused')
---
 xen/common/domain.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
xen/common/domain.c +6 -5
index 3b6e9471c413..e0ca71a53bc1 100644
@@ -1547,8 +1547,7 @@ int vcpu_unpause_by_systemcontroller(struct vcpu *v)
 static void _domain_pause(struct domain *d, bool sync)
 {
     struct vcpu *v;
-
-    atomic_inc(&d->pause_count);
+    bool was_paused = atomic_inc_return(&d->pause_count) - 1;
 
     if ( sync )
         for_each_vcpu ( d, v )
@@ -1557,7 +1556,8 @@ static void _domain_pause(struct domain *d, bool sync)
         for_each_vcpu ( d, v )
             vcpu_sleep_nosync(v);
 
-    arch_domain_pause(d);
+    if ( !was_paused )
+        arch_domain_pause(d);
 }
 
 void domain_pause(struct domain *d)
@@ -1575,11 +1575,12 @@ 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.51.0
#1 lore raw

xen/common/domain.c int vcpu_unpause_by_systemcontroller(struct vcpu *v) · line 1553

Quoted 12 lines · Roger Pau Monne
xen/common/domain.c +1 -2
@@ -1547,8 +1547,7 @@ int vcpu_unpause_by_systemcontroller(struct vcpu *v)
 static void _domain_pause(struct domain *d, bool sync)
 {
     struct vcpu *v;
-
-    atomic_inc(&d->pause_count);
+    bool was_paused = atomic_inc_return(&d->pause_count) - 1;
 
     if ( sync )
         for_each_vcpu ( d, v )

Isn't this racy? Another CPU doing the INC above just afterwards (yielding was_paused as false there) might still ...

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

Quoted 7 lines
xen/common/domain.c +2 -1
@@ -1557,7 +1556,8 @@ static void _domain_pause(struct domain *d, bool sync)
         for_each_vcpu ( d, v )
             vcpu_sleep_nosync(v);
 
-    arch_domain_pause(d);
+    if ( !was_paused )
+        arch_domain_pause(d);

... make it here faster, and then the call would occur to late. Whether that's acceptable is a matter of what exactly the arch hook does.

Furthermore, is what the arch hook does for x86 actually correct when "sync" is false? The vCPU-s might then still be running while the Viridian time is already frozen.

Jan

#2 lore raw
Quoted 21 lines · Jan Beulich

xen/common/domain.c int vcpu_unpause_by_systemcontroller(struct vcpu *v) · line 1553

Quoted 12 lines · Roger Pau Monne
xen/common/domain.c +1 -2
@@ -1547,8 +1547,7 @@ int vcpu_unpause_by_systemcontroller(struct vcpu *v)
 static void _domain_pause(struct domain *d, bool sync)
 {
     struct vcpu *v;
-
-    atomic_inc(&d->pause_count);
+    bool was_paused = atomic_inc_return(&d->pause_count) - 1;
 
     if ( sync )
         for_each_vcpu ( d, v )

Isn't this racy? Another CPU doing the INC above just afterwards (yielding was_paused as false there) might still ...

Quoted 4 lines
xen/common/domain.c +0 -0
@@ -1557,7 +1556,8 @@ static void _domain_pause(struct domain *d, bool sync)
         for_each_vcpu ( d, v )
             vcpu_sleep_nosync(v);
 
Quoted 6 lines

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

Quoted 3 lines
xen/common/domain.c +2 -1
-    arch_domain_pause(d);
+    if ( !was_paused )
+        arch_domain_pause(d);

... make it here faster, and then the call would occur to late. Whether that's acceptable is a matter of what exactly the arch hook does.

It's acceptable for what the Viridian code does now, as there are no current callers to domain_pause() that rely on the Viridian timers being paused.

TBH the Viridian timers would better use the vPT logic, as that avoids having to do this manual housekeeping. I suspect vPT wasn't used in the first place because when using SINTx the same SINTx could be used for other purposes apart from the timer signaling.

As a result the current logic to attempt to account for missed ticks is kind of bodged. It doesn't detect guest EOIs, and hence doesn't really know whether the previous interrupt has been processed ahead of injecting a new one.

Quoted 3 lines

Furthermore, is what the arch hook does for x86 actually correct when "sync" is false? The vCPU-s might then still be running while the Viridian time is already frozen.

I've also wondered about that aspect when using the nosync variant. I think it's fine to stop the timer ahead of the vCPU being paused, the only difference would be whether a tick get delivered in that short window ahead of the pause or afterwards, but that likely doesn't much difference for the purpose here.

Maybe it's best to attempt to move the Viridian timers to use vPT logic, and possibly get rid of the arch_domain_{,un}pause() hooks.

Thanks, Roger.

#3 lore raw
Quoted 47 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich

xen/common/domain.c int vcpu_unpause_by_systemcontroller(struct vcpu *v) · line 1553

Quoted 12 lines · Roger Pau Monne
xen/common/domain.c +1 -2
@@ -1547,8 +1547,7 @@ int vcpu_unpause_by_systemcontroller(struct vcpu *v)
 static void _domain_pause(struct domain *d, bool sync)
 {
     struct vcpu *v;
-
-    atomic_inc(&d->pause_count);
+    bool was_paused = atomic_inc_return(&d->pause_count) - 1;
 
     if ( sync )
         for_each_vcpu ( d, v )

Isn't this racy? Another CPU doing the INC above just afterwards (yielding was_paused as false there) might still ...

Quoted 4 lines
xen/common/domain.c +0 -0
@@ -1557,7 +1556,8 @@ static void _domain_pause(struct domain *d, bool sync)
         for_each_vcpu ( d, v )
             vcpu_sleep_nosync(v);
 
Quoted 6 lines

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

Quoted 3 lines
xen/common/domain.c +2 -1
-    arch_domain_pause(d);
+    if ( !was_paused )
+        arch_domain_pause(d);

... make it here faster, and then the call would occur to late. Whether that's acceptable is a matter of what exactly the arch hook does.

It's acceptable for what the Viridian code does now, as there are no current callers to domain_pause() that rely on the Viridian timers being paused.

TBH the Viridian timers would better use the vPT logic, as that avoids having to do this manual housekeeping. I suspect vPT wasn't used in the first place because when using SINTx the same SINTx could be used for other purposes apart from the timer signaling.

As a result the current logic to attempt to account for missed ticks is kind of bodged. It doesn't detect guest EOIs, and hence doesn't really know whether the previous interrupt has been processed ahead of injecting a new one.

Quoted 3 lines

Furthermore, is what the arch hook does for x86 actually correct when "sync" is false? The vCPU-s might then still be running while the Viridian time is already frozen.

Quoted 8 lines

I've also wondered about that aspect when using the nosync variant. I think it's fine to stop the timer ahead of the vCPU being paused, the only difference would be whether a tick get delivered in that short window ahead of the pause or afterwards, but that likely doesn't much difference for the purpose here.

Maybe it's best to attempt to move the Viridian timers to use vPT logic, and possibly get rid of the arch_domain_{,un}pause() hooks.

That may be more intrusive a change. I was kind of hoping to confine the less invasive change here to the Viridian freeze/thaw functions.

Jan