Skip to contentSkip to conversation

KVM: X86: Just one leader to trigger kvmclock sync request

PATCH In review Wanpeng Li 3 participants 3 messages Feb 28, 2020 – Mar 2, 2020

Subsystems (3)
lore
Share & tools
lore

Wanpeng Li

author
#0 lore raw

PATCH v3 1/1 KVM: X86: Just one leader to trigger kvmclock sync request

From: Wanpeng Li <[email protected]>

In the progress of vCPUs creation, it queues a kvmclock sync worker to the global workqueue before each vCPU creation completes. The workqueue subsystem guarantees not to queue the already queued work, however, we can make the logic more clear by make just one leader to trigger this kvmclock sync request and save on cacheline boucing due to test_and_set_bit.

---
v2 -> v3:
* update patch description
v1 -> v2:
* check vcpu->vcpu_idx

 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +3 -2
index fb5d64e..79bc995 100644
@@ -9390,8 +9390,9 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
 	if (!kvmclock_periodic_sync)
 		return;
 
-	schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
-					KVMCLOCK_SYNC_PERIOD);
+	if (vcpu->vcpu_idx == 0)
+		schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
+						KVMCLOCK_SYNC_PERIOD);
 }
 
 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Signature · 1 line
2.7.4
Reviewed-by on v3 1/1
#1 lore raw
Quoted 23 lines · Wanpeng Li

From: Wanpeng Li <[email protected]>

In the progress of vCPUs creation, it queues a kvmclock sync worker to the global workqueue before each vCPU creation completes. The workqueue subsystem guarantees not to queue the already queued work, however, we can make the logic more clear by make just one leader to trigger this kvmclock sync request and save on cacheline boucing due to test_and_set_bit.

---
v2 -> v3:
* update patch description
v1 -> v2:
* check vcpu->vcpu_idx

 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index fb5d64e..79bc995 100644

arch/x86/kvm/x86.c void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) · line 9395

Quoted 8 lines
arch/x86/kvm/x86.c +3 -2
@@ -9390,8 +9390,9 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
 	if (!kvmclock_periodic_sync)
 		return;
 
-	schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
-					KVMCLOCK_SYNC_PERIOD);
+	if (vcpu->vcpu_idx == 0)
+		schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
+						KVMCLOCK_SYNC_PERIOD);

I would've merged this new check with !kvmclock_periodic_sync above making it more obvious when the work is scheduled

arch/x86/kvm/x86.c +3 -5
index 5de200663f51..93550976f991 100644
@@ -9389,11 +9389,9 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
 
        mutex_unlock(&vcpu->mutex);
 
-       if (!kvmclock_periodic_sync)
-               return;
-
-       schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
-                                       KVMCLOCK_SYNC_PERIOD);
+       if (vcpu->vcpu_idx == 0 && kvmclock_periodic_sync)
+               schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
+                                     KVMCLOCK_SYNC_PERIOD);
 }
 

arch/x86/kvm/x86.c void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) · line 9398

Quoted 3 lines
arch/x86/kvm/x86.c +0 -0
 }
 
 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)

With or without the change mentioned above,

Signature · 1 line
Vitaly
maintainer
#2 lore raw
Quoted 16 lines · Vitaly Kuznetsov
arch/x86/kvm/x86.c +3 -5
@@ -9389,11 +9389,9 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
 
        mutex_unlock(&vcpu->mutex);
 
-       if (!kvmclock_periodic_sync)
-               return;
-
-       schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
-                                       KVMCLOCK_SYNC_PERIOD);
+       if (vcpu->vcpu_idx == 0 && kvmclock_periodic_sync)
+               schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
+                                     KVMCLOCK_SYNC_PERIOD);
 }
 
Quoted 1 line

Good idea, I squashed the change.

Paolo