Skip to contentSkip to conversation

KVM: LAPIC: Tune lapic_timer_advance_ns automatically

PATCH Applied Wanpeng Li 3 participants 6 messages Sep 28, 2018 – Oct 9, 2018

Subsystems (3)

v1 of 5 Applied Latest is v5 →

lore
Share & tools
lore

Wanpeng Li

author
#0 lore raw

PATCH 1/1 KVM: LAPIC: Tune lapic_timer_advance_ns automatically

From: Wanpeng Li <[email protected]>

In cloud environment, lapic_timer_advance_ns is needed to be tuned for every CPU generations, and every host kernel versions(the kvm-unit-tests/tscdeadline_latency.flat is 5700 cycles for upstream kernel and 9600 cycles for our 3.10 product kernel, both preemption_timer=N, Skylake server).

This patch adds the capability to automatically tune lapic_timer_advance_ns step by step, the initial value is 1000ns as d0659d946be05 (KVM: x86: add option to advance tscdeadline hrtimer expiration) recommended, it will be reduced when it is too early, and increased when it is too late. The guest_tsc and tsc_deadline are hard to equal, so we assume we are done when the delta is within a small scope e.g. 100 cycles. This patch reduces latency (kvm-unit-tests/tscdeadline_latency, busy waits, preemption_timer enabled) from ~2600 cyles to ~1200 cyles on our Skylake server.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
---
 arch/x86/kvm/lapic.c | 7 +++++++
 arch/x86/kvm/x86.c   | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +7 -0
index fbb0e6d..b756f12 100644
@@ -70,6 +70,8 @@
 #define APIC_BROADCAST			0xFF
 #define X2APIC_BROADCAST		0xFFFFFFFFul
 
+static bool __read_mostly lapic_timer_advance_adjust_done = false;
+
 static inline int apic_test_vector(int vec, void *bitmap)
 {
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1492,6 +1494,11 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
 	if (guest_tsc < tsc_deadline)
 		__delay(min(tsc_deadline - guest_tsc,
 			nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
+	if (!lapic_timer_advance_adjust_done) {
+		lapic_timer_advance_ns += (s64)(guest_tsc - tsc_deadline) / 8;
+		if (abs(guest_tsc - tsc_deadline) < 100)
+			lapic_timer_advance_adjust_done = true;
+	}
 }
 
 static void start_sw_tscdeadline(struct kvm_lapic *apic)
arch/x86/kvm/x86.c +1 -1
index edbf00e..e865d12 100644
@@ -136,7 +136,7 @@ static u32 __read_mostly tsc_tolerance_ppm = 250;
 module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
 
 /* lapic timer advance (tscdeadline mode only) in nanoseconds */
-unsigned int __read_mostly lapic_timer_advance_ns = 0;
+unsigned int lapic_timer_advance_ns = 1000;
 module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
 EXPORT_SYMBOL_GPL(lapic_timer_advance_ns);
 
Signature · 1 line
2.7.4
maintainer Applied
#1 lore raw
Quoted 55 lines · Wanpeng Li

From: Wanpeng Li <[email protected]>

In cloud environment, lapic_timer_advance_ns is needed to be tuned for every CPU generations, and every host kernel versions(the kvm-unit-tests/tscdeadline_latency.flat is 5700 cycles for upstream kernel and 9600 cycles for our 3.10 product kernel, both preemption_timer=N, Skylake server).

This patch adds the capability to automatically tune lapic_timer_advance_ns step by step, the initial value is 1000ns as d0659d946be05 (KVM: x86: add option to advance tscdeadline hrtimer expiration) recommended, it will be reduced when it is too early, and increased when it is too late. The guest_tsc and tsc_deadline are hard to equal, so we assume we are done when the delta is within a small scope e.g. 100 cycles. This patch reduces latency (kvm-unit-tests/tscdeadline_latency, busy waits, preemption_timer enabled) from ~2600 cyles to ~1200 cyles on our Skylake server.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
---
 arch/x86/kvm/lapic.c | 7 +++++++
 arch/x86/kvm/x86.c   | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +7 -0
index fbb0e6d..b756f12 100644
@@ -70,6 +70,8 @@
 #define APIC_BROADCAST			0xFF
 #define X2APIC_BROADCAST		0xFFFFFFFFul
 
+static bool __read_mostly lapic_timer_advance_adjust_done = false;
+
 static inline int apic_test_vector(int vec, void *bitmap)
 {
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1492,6 +1494,11 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
 	if (guest_tsc < tsc_deadline)
 		__delay(min(tsc_deadline - guest_tsc,
 			nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
+	if (!lapic_timer_advance_adjust_done) {
+		lapic_timer_advance_ns += (s64)(guest_tsc - tsc_deadline) / 8;
+		if (abs(guest_tsc - tsc_deadline) < 100)
+			lapic_timer_advance_adjust_done = true;
+	}
 }
 
 static void start_sw_tscdeadline(struct kvm_lapic *apic)
arch/x86/kvm/x86.c +0 -0
index edbf00e..e865d12 100644

arch/x86/kvm/x86.c static u32 __read_mostly tsc_tolerance_ppm = 250; · line 142

Quoted 8 lines
arch/x86/kvm/x86.c +1 -1
@@ -136,7 +136,7 @@ static u32 __read_mostly tsc_tolerance_ppm = 250;
 module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
 
 /* lapic timer advance (tscdeadline mode only) in nanoseconds */
-unsigned int __read_mostly lapic_timer_advance_ns = 0;
+unsigned int lapic_timer_advance_ns = 1000;
 module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
 EXPORT_SYMBOL_GPL(lapic_timer_advance_ns);
 

Queued, thanks.

Paolo

#2 lore raw
In reply to Wanpeng Li
Quoted 42 lines · Wanpeng Li

From: Wanpeng Li <[email protected]>

In cloud environment, lapic_timer_advance_ns is needed to be tuned for every CPU generations, and every host kernel versions(the kvm-unit-tests/tscdeadline_latency.flat is 5700 cycles for upstream kernel and 9600 cycles for our 3.10 product kernel, both preemption_timer=N, Skylake server).

This patch adds the capability to automatically tune lapic_timer_advance_ns step by step, the initial value is 1000ns as d0659d946be05 (KVM: x86: add option to advance tscdeadline hrtimer expiration) recommended, it will be reduced when it is too early, and increased when it is too late. The guest_tsc and tsc_deadline are hard to equal, so we assume we are done when the delta is within a small scope e.g. 100 cycles. This patch reduces latency (kvm-unit-tests/tscdeadline_latency, busy waits, preemption_timer enabled) from ~2600 cyles to ~1200 cyles on our Skylake server.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>

---
arch/x86/kvm/lapic.c | 7 +++++++
arch/x86/kvm/x86.c | 2 +-

2 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +2 -0
index fbb0e6d..b756f12 100644
@@ -70,6 +70,8 @@
 #define APIC_BROADCAST			0xFF
 #define X2APIC_BROADCAST		0xFFFFFFFFul
 
+static bool __read_mostly lapic_timer_advance_adjust_done = false;
+
 static inline int apic_test_vector(int vec, void *bitmap)
 {
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));

arch/x86/kvm/lapic.c void wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1498

Quoted 5 lines
arch/x86/kvm/lapic.c +2 -0
@@ -1492,6 +1494,11 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
 	if (guest_tsc < tsc_deadline)
 		__delay(min(tsc_deadline - guest_tsc,
 			nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
+	if (!lapic_timer_advance_adjust_done) {
+		lapic_timer_advance_ns += (s64)(guest_tsc - tsc_deadline) / 8;

I don’t understand how this “/ 8” converts between guest TSC units to host nanoseconds.

I think that instead you should do something like:
s64 ns = (s64)(guest_tsc - tsc_deadline) * 1000000ULL; do_div(ns, vcpu->arch.virtual_tsc_khz); lapic_timer_advance_ns += ns;

arch/x86/kvm/lapic.c void wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1499

Quoted 1 line
arch/x86/kvm/lapic.c +1 -0
+		if (abs(guest_tsc - tsc_deadline) < 100)

I would put this “100” hard-coded value as some “#define” to make code more clear.

Quoted 21 lines
arch/x86/kvm/lapic.c +2 -0
+			lapic_timer_advance_adjust_done = true;
+	}
 }
 
 static void start_sw_tscdeadline(struct kvm_lapic *apic)
arch/x86/kvm/x86.c +1 -1
index edbf00e..e865d12 100644
@@ -136,7 +136,7 @@ static u32 __read_mostly tsc_tolerance_ppm = 250;
 module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
 
 /* lapic timer advance (tscdeadline mode only) in nanoseconds */
-unsigned int __read_mostly lapic_timer_advance_ns = 0;
+unsigned int lapic_timer_advance_ns = 1000;
 module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
 EXPORT_SYMBOL_GPL(lapic_timer_advance_ns);
 
Signature · 1 line
2.7.4
author
#3 lore raw
Quoted 37 lines · Liran Alon
Quoted 37 lines · Wanpeng Li

From: Wanpeng Li <[email protected]>

In cloud environment, lapic_timer_advance_ns is needed to be tuned for every CPU generations, and every host kernel versions(the kvm-unit-tests/tscdeadline_latency.flat is 5700 cycles for upstream kernel and 9600 cycles for our 3.10 product kernel, both preemption_timer=N, Skylake server).

This patch adds the capability to automatically tune lapic_timer_advance_ns step by step, the initial value is 1000ns as d0659d946be05 (KVM: x86: add option to advance tscdeadline hrtimer expiration) recommended, it will be reduced when it is too early, and increased when it is too late. The guest_tsc and tsc_deadline are hard to equal, so we assume we are done when the delta is within a small scope e.g. 100 cycles. This patch reduces latency (kvm-unit-tests/tscdeadline_latency, busy waits, preemption_timer enabled) from ~2600 cyles to ~1200 cyles on our Skylake server.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>

---
arch/x86/kvm/lapic.c | 7 +++++++
arch/x86/kvm/x86.c | 2 +-

2 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +0 -0
index fbb0e6d..b756f12 100644
@@ -70,6 +70,8 @@
 #define APIC_BROADCAST                        0xFF
 #define X2APIC_BROADCAST              0xFFFFFFFFul
 
Quoted 13 lines

arch/x86/kvm/lapic.c void wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1498

Quoted 11 lines
arch/x86/kvm/lapic.c +4 -0
+static bool __read_mostly lapic_timer_advance_adjust_done = false;
+
 static inline int apic_test_vector(int vec, void *bitmap)
 {
      return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1492,6 +1494,11 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
      if (guest_tsc < tsc_deadline)
              __delay(min(tsc_deadline - guest_tsc,
                      nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
+     if (!lapic_timer_advance_adjust_done) {
+             lapic_timer_advance_ns += (s64)(guest_tsc - tsc_deadline) / 8;

I don’t understand how this “/ 8” converts between guest TSC units to host nanoseconds.

Oh, I miss it. In addition, /8 here I mean adjust lapic_timer_advance_ns step by step. I can observe big fluctuated value between early and late when running real guest os like linux instead of kvm-unit-tests. After more testing, I saw lapic_timer_advance_ns can be overflow since the delta between guest_tsc and tsc_deadline is too huge.

Quoted 9 lines

I think that instead you should do something like:
s64 ns = (s64)(guest_tsc - tsc_deadline) * 1000000ULL; do_div(ns, vcpu->arch.virtual_tsc_khz); lapic_timer_advance_ns += ns;

arch/x86/kvm/lapic.c void wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1499

Quoted 1 line
arch/x86/kvm/lapic.c +1 -0
+             if (abs(guest_tsc - tsc_deadline) < 100)

I would put this “100” hard-coded value as some “#define” to make code more clear.

How about something like below:

arch/x86/kvm/lapic.c +3 -0
index fbb0e6d..354eb13c 100644
@@ -70,6 +70,9 @@
 #define APIC_BROADCAST            0xFF
 #define X2APIC_BROADCAST        0xFFFFFFFFul
 
+static bool __read_mostly lapic_timer_advance_adjust_done = false;
+#define LAPIC_TIMER_ADVANCE_ADJUST_DONE 100
+
 static inline int apic_test_vector(int vec, void *bitmap)
 {
     return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1472,7 +1475,7 @@ static bool lapic_timer_int_injected(struct

kvm_vcpu *vcpu)
void wait_lapic_expire(struct kvm_vcpu *vcpu)

 {
     struct kvm_lapic *apic = vcpu->arch.apic;

- u64 guest_tsc, tsc_deadline;
+ u64 guest_tsc, tsc_deadline, ns;

     if (!lapic_in_kernel(vcpu))
         return;

@@ -1492,6 +1495,19 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
if (guest_tsc < tsc_deadline)
__delay(min(tsc_deadline - guest_tsc,
nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
+ if (!lapic_timer_advance_adjust_done) {
+ if (guest_tsc < tsc_deadline) {

+            ns = (tsc_deadline - guest_tsc) * 1000000ULL;
+            do_div(ns, vcpu->arch.virtual_tsc_khz);
+            lapic_timer_advance_ns -= min((unsigned int)ns,
lapic_timer_advance_ns / 8);
+        } else {
+            ns = (guest_tsc - tsc_deadline) * 1000000ULL;
+            do_div(ns, vcpu->arch.virtual_tsc_khz);
+            lapic_timer_advance_ns += min((unsigned int)ns,

lapic_timer_advance_ns / 8);
+ }
+ if (ns < LAPIC_TIMER_ADVANCE_ADJUST_DONE)
+ lapic_timer_advance_adjust_done = true;
+ }
}

static void start_sw_tscdeadline(struct kvm_lapic *apic)

arch/x86/kvm/x86.c +1 -1
index ca71773..1f3f955 100644
@@ -136,7 +136,7 @@ static u32 __read_mostly tsc_tolerance_ppm = 250;
 module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
 
 /* lapic timer advance (tscdeadline mode only) in nanoseconds */
-unsigned int __read_mostly lapic_timer_advance_ns = 0;
+unsigned int __read_mostly lapic_timer_advance_ns = 1000;
 module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
 EXPORT_SYMBOL_GPL(lapic_timer_advance_ns);
#4 lore raw
Quoted 56 lines · Wanpeng Li
Quoted 37 lines · Liran Alon
Quoted 37 lines · Wanpeng Li

From: Wanpeng Li <[email protected]>

In cloud environment, lapic_timer_advance_ns is needed to be tuned for every CPU generations, and every host kernel versions(the kvm-unit-tests/tscdeadline_latency.flat is 5700 cycles for upstream kernel and 9600 cycles for our 3.10 product kernel, both preemption_timer=N, Skylake server).

This patch adds the capability to automatically tune lapic_timer_advance_ns step by step, the initial value is 1000ns as d0659d946be05 (KVM: x86: add option to advance tscdeadline hrtimer expiration) recommended, it will be reduced when it is too early, and increased when it is too late. The guest_tsc and tsc_deadline are hard to equal, so we assume we are done when the delta is within a small scope e.g. 100 cycles. This patch reduces latency (kvm-unit-tests/tscdeadline_latency, busy waits, preemption_timer enabled) from ~2600 cyles to ~1200 cyles on our Skylake server.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>

---
arch/x86/kvm/lapic.c | 7 +++++++
arch/x86/kvm/x86.c | 2 +-

2 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +0 -0
index fbb0e6d..b756f12 100644
@@ -70,6 +70,8 @@
 #define APIC_BROADCAST                        0xFF
 #define X2APIC_BROADCAST              0xFFFFFFFFul
 
Quoted 13 lines

arch/x86/kvm/lapic.c void wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1498

Quoted 11 lines
arch/x86/kvm/lapic.c +4 -0
+static bool __read_mostly lapic_timer_advance_adjust_done = false;
+
 static inline int apic_test_vector(int vec, void *bitmap)
 {
     return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1492,6 +1494,11 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
     if (guest_tsc < tsc_deadline)
             __delay(min(tsc_deadline - guest_tsc,
                     nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
+     if (!lapic_timer_advance_adjust_done) {
+             lapic_timer_advance_ns += (s64)(guest_tsc - tsc_deadline) / 8;

I don’t understand how this “/ 8” converts between guest TSC units to host nanoseconds.

Quoted 2 lines

Oh, I miss it. In addition, /8 here I mean adjust lapic_timer_advance_ns step by step. I can observe big fluctuated

If that’s the case, I would also put the “8” as a #define to make it more clear of it’s purpose.

Quoted 48 lines

value between early and late when running real guest os like linux instead of kvm-unit-tests. After more testing, I saw lapic_timer_advance_ns can be overflow since the delta between guest_tsc and tsc_deadline is too huge.

Quoted 9 lines

I think that instead you should do something like:
s64 ns = (s64)(guest_tsc - tsc_deadline) * 1000000ULL; do_div(ns, vcpu->arch.virtual_tsc_khz); lapic_timer_advance_ns += ns;

arch/x86/kvm/lapic.c void wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1499

Quoted 1 line
arch/x86/kvm/lapic.c +1 -0
+             if (abs(guest_tsc - tsc_deadline) < 100)

I would put this “100” hard-coded value as some “#define” to make code more clear.

How about something like below:

arch/x86/kvm/lapic.c +4 -1
index fbb0e6d..354eb13c 100644
@@ -70,6 +70,9 @@
 #define APIC_BROADCAST            0xFF
 #define X2APIC_BROADCAST        0xFFFFFFFFul
 
+static bool __read_mostly lapic_timer_advance_adjust_done = false;
+#define LAPIC_TIMER_ADVANCE_ADJUST_DONE 100
+
 static inline int apic_test_vector(int vec, void *bitmap)
 {
    return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1472,7 +1475,7 @@ static bool lapic_timer_int_injected(struct
 kvm_vcpu *vcpu)
 void wait_lapic_expire(struct kvm_vcpu *vcpu)
 {
    struct kvm_lapic *apic = vcpu->arch.apic;
-    u64 guest_tsc, tsc_deadline;
+    u64 guest_tsc, tsc_deadline, ns;
 
    if (!lapic_in_kernel(vcpu))
        return;
arch/x86/kvm/lapic.c +3 -0
@@ -1492,6 +1495,19 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
    if (guest_tsc < tsc_deadline)
        __delay(min(tsc_deadline - guest_tsc,
            nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
+    if (!lapic_timer_advance_adjust_done) {
+        if (guest_tsc < tsc_deadline) {
+            ns = (tsc_deadline - guest_tsc) * 1000000ULL;

arch/x86/kvm/lapic.c void wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1510

Quoted 10 lines
arch/x86/kvm/lapic.c +8 -0
+            do_div(ns, vcpu->arch.virtual_tsc_khz);
+            lapic_timer_advance_ns -= min((unsigned int)ns,
 lapic_timer_advance_ns / 8);
+        } else {
+            ns = (guest_tsc - tsc_deadline) * 1000000ULL;
+            do_div(ns, vcpu->arch.virtual_tsc_khz);
+            lapic_timer_advance_ns += min((unsigned int)ns,
 lapic_timer_advance_ns / 8);
+        }
+        if (ns < LAPIC_TIMER_ADVANCE_ADJUST_DONE)

Didn’t you meant to compare here that abs(guest_tsc - tsc_deadline) < LAPIC_TIMER_ADVANCE_ADJUST_DONE? This is also a good example on why I would also rename LAPIC_TIMER_ADVANCE_ADJUST_DONE to something which indicates it represents a number in guest TSC units.

Quoted 17 lines
arch/x86/kvm/lapic.c +2 -0
+            lapic_timer_advance_adjust_done = true;
+    }
 }

static void start_sw_tscdeadline(struct kvm_lapic *apic)

arch/x86/kvm/x86.c +1 -1
index ca71773..1f3f955 100644
@@ -136,7 +136,7 @@ static u32 __read_mostly tsc_tolerance_ppm = 250;
 module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
 
 /* lapic timer advance (tscdeadline mode only) in nanoseconds */
-unsigned int __read_mostly lapic_timer_advance_ns = 0;
+unsigned int __read_mostly lapic_timer_advance_ns = 1000;
 module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
 EXPORT_SYMBOL_GPL(lapic_timer_advance_ns);
author
#5 lore raw
Quoted 116 lines · Liran Alon
Quoted 57 lines · Wanpeng Li
Quoted 37 lines · Liran Alon
Quoted 37 lines · Wanpeng Li

From: Wanpeng Li <[email protected]>

In cloud environment, lapic_timer_advance_ns is needed to be tuned for every CPU generations, and every host kernel versions(the kvm-unit-tests/tscdeadline_latency.flat is 5700 cycles for upstream kernel and 9600 cycles for our 3.10 product kernel, both preemption_timer=N, Skylake server).

This patch adds the capability to automatically tune lapic_timer_advance_ns step by step, the initial value is 1000ns as d0659d946be05 (KVM: x86: add option to advance tscdeadline hrtimer expiration) recommended, it will be reduced when it is too early, and increased when it is too late. The guest_tsc and tsc_deadline are hard to equal, so we assume we are done when the delta is within a small scope e.g. 100 cycles. This patch reduces latency (kvm-unit-tests/tscdeadline_latency, busy waits, preemption_timer enabled) from ~2600 cyles to ~1200 cyles on our Skylake server.

Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>

---
arch/x86/kvm/lapic.c | 7 +++++++
arch/x86/kvm/x86.c | 2 +-

2 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +0 -0
index fbb0e6d..b756f12 100644
@@ -70,6 +70,8 @@
 #define APIC_BROADCAST                        0xFF
 #define X2APIC_BROADCAST              0xFFFFFFFFul
 
Quoted 13 lines

arch/x86/kvm/lapic.c void wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1498

Quoted 11 lines
arch/x86/kvm/lapic.c +4 -0
+static bool __read_mostly lapic_timer_advance_adjust_done = false;
+
 static inline int apic_test_vector(int vec, void *bitmap)
 {
     return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1492,6 +1494,11 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
     if (guest_tsc < tsc_deadline)
             __delay(min(tsc_deadline - guest_tsc,
                     nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
+     if (!lapic_timer_advance_adjust_done) {
+             lapic_timer_advance_ns += (s64)(guest_tsc - tsc_deadline) / 8;

I don’t understand how this “/ 8” converts between guest TSC units to host nanoseconds.

Quoted 2 lines

Oh, I miss it. In addition, /8 here I mean adjust lapic_timer_advance_ns step by step. I can observe big fluctuated

If that’s the case, I would also put the “8” as a #define to make it more clear of it’s purpose.

Quoted 54 lines

value between early and late when running real guest os like linux instead of kvm-unit-tests. After more testing, I saw lapic_timer_advance_ns can be overflow since the delta between guest_tsc and tsc_deadline is too huge.

Quoted 9 lines

I think that instead you should do something like:
s64 ns = (s64)(guest_tsc - tsc_deadline) * 1000000ULL; do_div(ns, vcpu->arch.virtual_tsc_khz); lapic_timer_advance_ns += ns;

arch/x86/kvm/lapic.c void wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1499

Quoted 1 line
arch/x86/kvm/lapic.c +1 -0
+             if (abs(guest_tsc - tsc_deadline) < 100)

I would put this “100” hard-coded value as some “#define” to make code more clear.

How about something like below:

arch/x86/kvm/lapic.c +4 -1
index fbb0e6d..354eb13c 100644
@@ -70,6 +70,9 @@
 #define APIC_BROADCAST            0xFF
 #define X2APIC_BROADCAST        0xFFFFFFFFul
 
+static bool __read_mostly lapic_timer_advance_adjust_done = false;
+#define LAPIC_TIMER_ADVANCE_ADJUST_DONE 100
+
 static inline int apic_test_vector(int vec, void *bitmap)
 {
    return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1472,7 +1475,7 @@ static bool lapic_timer_int_injected(struct
 kvm_vcpu *vcpu)
 void wait_lapic_expire(struct kvm_vcpu *vcpu)
 {
    struct kvm_lapic *apic = vcpu->arch.apic;
-    u64 guest_tsc, tsc_deadline;
+    u64 guest_tsc, tsc_deadline, ns;
 
    if (!lapic_in_kernel(vcpu))
        return;
arch/x86/kvm/lapic.c +8 -0
@@ -1492,6 +1495,19 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
    if (guest_tsc < tsc_deadline)
        __delay(min(tsc_deadline - guest_tsc,
            nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
+    if (!lapic_timer_advance_adjust_done) {
+        if (guest_tsc < tsc_deadline) {
+            ns = (tsc_deadline - guest_tsc) * 1000000ULL;
+            do_div(ns, vcpu->arch.virtual_tsc_khz);
+            lapic_timer_advance_ns -= min((unsigned int)ns,
 lapic_timer_advance_ns / 8);
+        } else {
+            ns = (guest_tsc - tsc_deadline) * 1000000ULL;
+            do_div(ns, vcpu->arch.virtual_tsc_khz);
Quoted 8 lines

arch/x86/kvm/lapic.c void wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1510

Quoted 4 lines
arch/x86/kvm/lapic.c +3 -0
+            lapic_timer_advance_ns += min((unsigned int)ns,
 lapic_timer_advance_ns / 8);
+        }
+        if (ns < LAPIC_TIMER_ADVANCE_ADJUST_DONE)

Didn’t you meant to compare here that abs(guest_tsc - tsc_deadline) < LAPIC_TIMER_ADVANCE_ADJUST_DONE? This is also a good example on why I would also rename LAPIC_TIMER_ADVANCE_ADJUST_DONE to something which indicates it represents a number in guest TSC units.

Done in v2.

Regards,
Wanpeng Li