Skip to contentSkip to conversation

x86/Hyper-V: Fix hv sched clock function return wrong time unit

PATCH Superseded Yubo Xie 5 participants 7 messages Mar 24, 2020 – Mar 25, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yubo Xie

author
#0 lore raw

PATCH 1/1 x86/Hyper-V: Fix hv sched clock function return wrong time unit

sched clock callback should return time with nano second as unit but current hv callback returns time with 100ns. Fix it.

Fixes adb87ff4f96c ("clocksource/drivers/hyperv: Allocate Hyper-V TSC page statically")
---
 drivers/clocksource/hyperv_timer.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/clocksource/hyperv_timer.c +4 -2
index 9d808d595ca8..662ed978fa24 100644
@@ -343,7 +343,8 @@ static u64 notrace read_hv_clock_tsc_cs(struct clocksource *arg)
 
 static u64 read_hv_sched_clock_tsc(void)
 {
-	return read_hv_clock_tsc() - hv_sched_clock_offset;
+	return (read_hv_clock_tsc() - hv_sched_clock_offset)
+		* (NSEC_PER_SEC / HV_CLOCK_HZ);
 }
 
 static void suspend_hv_clock_tsc(struct clocksource *arg)
@@ -398,7 +399,8 @@ static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg)
 
 static u64 read_hv_sched_clock_msr(void)
 {
-	return read_hv_clock_msr() - hv_sched_clock_offset;
+	return (read_hv_clock_msr() - hv_sched_clock_offset)
+		* (NSEC_PER_SEC / HV_CLOCK_HZ);
 }
 
 static struct clocksource hyperv_cs_msr = {
Signature · 1 line
2.14.5
#1 lore raw
Quoted 7 lines · Yubo Xie

sched clock callback should return time with nano second as unit but current hv callback returns time with 100ns. Fix it.

Fixes adb87ff4f96c ("clocksource/drivers/hyperv: Allocate Hyper-V TSC page statically")

I don't think this is the right commit to reference,

commit bd00cd52d5be655a2f217e2ed74b91a71cb2b14f

Author: Tianyu Lan <[email protected]>
Date:   Wed Aug 14 20:32:16 2019 +0800

clocksource/drivers/hyperv: Add Hyper-V specific sched clock function

looks like the one.

Quoted 19 lines
---
 drivers/clocksource/hyperv_timer.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/clocksource/hyperv_timer.c +2 -1
index 9d808d595ca8..662ed978fa24 100644
@@ -343,7 +343,8 @@ static u64 notrace read_hv_clock_tsc_cs(struct clocksource *arg)
 
 static u64 read_hv_sched_clock_tsc(void)
 {
-	return read_hv_clock_tsc() - hv_sched_clock_offset;
+	return (read_hv_clock_tsc() - hv_sched_clock_offset)
+		* (NSEC_PER_SEC / HV_CLOCK_HZ);
 }
 
 static void suspend_hv_clock_tsc(struct clocksource *arg)

drivers/clocksource/hyperv_timer.c static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg) · line 404

Quoted 7 lines
drivers/clocksource/hyperv_timer.c +2 -1
@@ -398,7 +399,8 @@ static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg)
 
 static u64 read_hv_sched_clock_msr(void)
 {
-	return read_hv_clock_msr() - hv_sched_clock_offset;
+	return (read_hv_clock_msr() - hv_sched_clock_offset)
+		* (NSEC_PER_SEC / HV_CLOCK_HZ);
 }

kvmclock seems to have the same (pre-patch) code ...

Quoted 2 lines
drivers/clocksource/hyperv_timer.c +0 -0
 static struct clocksource hyperv_cs_msr = {
Signature · 1 line
Vitaly
maintainer
#4 lore raw
In reply to Yubo Xie

Hi

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag
fixing commit: adb87ff4f96c ("clocksource/drivers/hyperv: Allocate Hyper-V TSC page statically").

The bot has tested the following trees: v5.5.11, v5.4.27.

v5.5.11: Failed to apply! Possible dependencies:
0af3e137c144 ("clocksource/drivers/hyper-v: Untangle stimers and timesync from clocksources") ddc61bbc4501 ("clocksource/drivers/hyper-v: Reserve PAGE_SIZE space for tsc page")

v5.4.27: Failed to apply! Possible dependencies:
0af3e137c144 ("clocksource/drivers/hyper-v: Untangle stimers and timesync from clocksources") ddc61bbc4501 ("clocksource/drivers/hyper-v: Reserve PAGE_SIZE space for tsc page")

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

Signature · 2 lines
Thanks
Sasha
author
#2 lore raw
In reply to Vitaly Kuznetsov

Hi Vitaly:
Thanks for your review.

Quoted 12 lines · Vitaly Kuznetsov
Quoted 7 lines · Yubo Xie

sched clock callback should return time with nano second as unit but current hv callback returns time with 100ns. Fix it.

Fixes adb87ff4f96c ("clocksource/drivers/hyperv: Allocate Hyper-V TSC page statically")

I don't think this is the right commit to reference,

Quoted 7 lines

commit bd00cd52d5be655a2f217e2ed74b91a71cb2b14f

Author: Tianyu Lan <[email protected]>
Date:   Wed Aug 14 20:32:16 2019 +0800

clocksource/drivers/hyperv: Add Hyper-V specific sched clock function

looks like the one.

Sorry. You are right. Will update in the next version.

Quoted 21 lines
Quoted 21 lines
---
  drivers/clocksource/hyperv_timer.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/clocksource/hyperv_timer.c +2 -1
index 9d808d595ca8..662ed978fa24 100644
@@ -343,7 +343,8 @@ static u64 notrace read_hv_clock_tsc_cs(struct clocksource *arg)
 
  static u64 read_hv_sched_clock_tsc(void)
  {
-	return read_hv_clock_tsc() - hv_sched_clock_offset;
+	return (read_hv_clock_tsc() - hv_sched_clock_offset)
+		* (NSEC_PER_SEC / HV_CLOCK_HZ);
  }
 
  static void suspend_hv_clock_tsc(struct clocksource *arg)
@@ -398,7 +399,8 @@ static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg)
 
Quoted 8 lines

drivers/clocksource/hyperv_timer.c static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg) · line 404

Quoted 6 lines
drivers/clocksource/hyperv_timer.c +2 -1
  static u64 read_hv_sched_clock_msr(void)
  {
-	return read_hv_clock_msr() - hv_sched_clock_offset;
+	return (read_hv_clock_msr() - hv_sched_clock_offset)
+		* (NSEC_PER_SEC / HV_CLOCK_HZ);
  }

kvmclock seems to have the same (pre-patch) code ...

kvm sched clock gets time from pvclock_clocksource_read() and the time unit is nanosecond. So there is such issue in KVM code.

Quoted 4 lines
Quoted 3 lines
drivers/clocksource/hyperv_timer.c +0 -0
  static struct clocksource hyperv_cs_msr = {
#3 lore raw
Quoted 12 lines · Tianyu Lan
Quoted 10 lines

drivers/clocksource/hyperv_timer.c static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg) · line 404

Quoted 8 lines
drivers/clocksource/hyperv_timer.c +2 -1
@@ -398,7 +399,8 @@ static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg)
 
  static u64 read_hv_sched_clock_msr(void)
  {
-	return read_hv_clock_msr() - hv_sched_clock_offset;
+	return (read_hv_clock_msr() - hv_sched_clock_offset)
+		* (NSEC_PER_SEC / HV_CLOCK_HZ);
  }

kvmclock seems to have the same (pre-patch) code ...

Quoted 3 lines

kvm sched clock gets time from pvclock_clocksource_read() and the time unit is nanosecond. So there is such issue in KVM code.

Ah, true, kvmclock is always 1Ghz so it's reading 'naturally' converts to ns.

Signature · 1 line
Vitaly
maintainer
#5 lore raw
In reply to Yubo Xie
Quoted 6 lines

From: [email protected] <[email protected]> On Behalf Of Yubo Xie
Sent: Tuesday, March 24, 2020 8:20 AM
...
sched clock callback should return time with nano second as unit but current hv callback returns time with 100ns. Fix it.

Hi Yubo,
I'm curious how you found the bug? :-) Did you notice some kind of symtom?

Thanks,
-- Dexuan

#6 lore raw

I use "time" to run some performance tests in our project under WSL2, but the result looks weird: the value of "real" is far from the sum of "user" + "sys".

-----Original Message-----
From: Dexuan Cui <[email protected]>
Sent: Wednesday, March 25, 2020 11:46 AM
To: Yubo Xie <[email protected]>; KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; Wei Liu <[email protected]>; [email protected]; [email protected]; Michael Kelley <[email protected]>
Cc: Yubo Xie <[email protected]>; [email protected]; [email protected]; vkuznets <[email protected]>; [email protected]; Tianyu Lan <[email protected]>
Subject: RE: [PATCH] x86/Hyper-V: Fix hv sched clock function return wrong time unit

Quoted 6 lines

From: [email protected] <[email protected]> On Behalf Of Yubo Xie
Sent: Tuesday, March 24, 2020 8:20 AM
...
sched clock callback should return time with nano second as unit but current hv callback returns time with 100ns. Fix it.

Hi Yubo,
I'm curious how you found the bug? :-) Did you notice some kind of symtom?

Thanks,
-- Dexuan