Skip to contentSkip to conversation

BUG in pv_clock when overflow condition is detected

PATCH In review Igor Mammedov 3 participants 9 messages Feb 13, 2012 – Feb 21, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 BUG in pv_clock when overflow condition is detected

BUG when overflow occurs at pvclock.c:pvclock_get_nsec_offset

    u64 delta = native_read_tsc() - shadow->tsc_timestamp;

this might happen at an attempt to read an uninitialized yet clock. It won't prevent stalls and hangs but at least it won't do it silently.

---
 arch/x86/kernel/pvclock.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
arch/x86/kernel/pvclock.c +4 -1
index 42eb330..35a6190 100644
@@ -43,7 +43,10 @@ void pvclock_set_flags(u8 flags)
 
 static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time *shadow)
 {
-	u64 delta = native_read_tsc() - shadow->tsc_timestamp;
+	u64 delta;
+	u64 tsc = native_read_tsc();
+	BUG_ON(tsc < shadow->tsc_timestamp);
+	delta = tsc - shadow->tsc_timestamp;
 	return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
 				   shadow->tsc_shift);
 }
Signature · 1 line
1.7.7.6
#1 lore raw
Quoted 17 lines · Igor Mammedov

BUG when overflow occurs at pvclock.c:pvclock_get_nsec_offset

    u64 delta = native_read_tsc() - shadow->tsc_timestamp;

this might happen at an attempt to read an uninitialized yet clock. It won't prevent stalls and hangs but at least it won't do it silently.

---
 arch/x86/kernel/pvclock.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
arch/x86/kernel/pvclock.c +0 -0
index 42eb330..35a6190 100644

arch/x86/kernel/pvclock.c void pvclock_set_flags(u8 flags) · line 51

Quoted 10 lines
arch/x86/kernel/pvclock.c +4 -1
@@ -43,7 +43,10 @@ void pvclock_set_flags(u8 flags)
 
 static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time *shadow)
 {
-	u64 delta = native_read_tsc() - shadow->tsc_timestamp;
+	u64 delta;
+	u64 tsc = native_read_tsc();
+	BUG_ON(tsc < shadow->tsc_timestamp);
+	delta = tsc - shadow->tsc_timestamp;
 	return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
 				   shadow->tsc_shift);

Maybe a WARN_ON_ONCE()? Otherwise a relatively minor hypervisor bug can kill the guest.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 19 lines · Avi Kivity
Quoted 18 lines · Igor Mammedov

BUG when overflow occurs at pvclock.c:pvclock_get_nsec_offset

     u64 delta = native_read_tsc() - shadow->tsc_timestamp;

this might happen at an attempt to read an uninitialized yet clock. It won't prevent stalls and hangs but at least it won't do it silently.

---
  arch/x86/kernel/pvclock.c |    5 ++++-
  1 files changed, 4 insertions(+), 1 deletions(-)
arch/x86/kernel/pvclock.c +0 -0
index 42eb330..35a6190 100644
@@ -43,7 +43,10 @@ void pvclock_set_flags(u8 flags)
 
Quoted 12 lines

arch/x86/kernel/pvclock.c void pvclock_set_flags(u8 flags) · line 51

Quoted 9 lines
arch/x86/kernel/pvclock.c +4 -1
  static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time *shadow)
  {
-	u64 delta = native_read_tsc() - shadow->tsc_timestamp;
+	u64 delta;
+	u64 tsc = native_read_tsc();
+	BUG_ON(tsc<  shadow->tsc_timestamp);
+	delta = tsc - shadow->tsc_timestamp;
  	return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
  				   shadow->tsc_shift);

Maybe a WARN_ON_ONCE()? Otherwise a relatively minor hypervisor bug can kill the guest.

An attempt to print from this place is not perfect since it often leads to recursive calling to this very function and it hang there anyway. But if you insist I'll re-post it with WARN_ON_ONCE,
It won't make much difference because guest will hang/stall due overflow anyway.

If there is an intention to keep guest functional after the event then maybe this patch is a way to go
http://www.spinics.net/lists/kvm/msg68463.html
this way clock will be re-silent to this kind of errors, like bare-metal one is.

Signature · 2 lines
Thanks,
 Igor
#3 lore raw
Quoted 15 lines · Igor Mammedov
Quoted 11 lines

arch/x86/kernel/pvclock.c void pvclock_set_flags(u8 flags) · line 51

Quoted 10 lines

static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time
*shadow)
{

arch/x86/kernel/pvclock.c +4 -1
-    u64 delta = native_read_tsc() - shadow->tsc_timestamp;
+    u64 delta;
+    u64 tsc = native_read_tsc();
+    BUG_ON(tsc<  shadow->tsc_timestamp);
+    delta = tsc - shadow->tsc_timestamp;
      return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
                     shadow->tsc_shift);
Quoted 2 lines

Maybe a WARN_ON_ONCE()? Otherwise a relatively minor hypervisor bug can kill the guest.

Quoted 5 lines

An attempt to print from this place is not perfect since it often leads to recursive calling to this very function and it hang there anyway. But if you insist I'll re-post it with WARN_ON_ONCE,
It won't make much difference because guest will hang/stall due overflow anyway.

Won't a BUG_ON() also result in a printk?

Quoted 6 lines

If there is an intention to keep guest functional after the event then maybe this patch is a way to go
http://www.spinics.net/lists/kvm/msg68463.html
this way clock will be re-silent to this kind of errors, like bare-metal one is.

It's the same patch... do you mean something that detects the overflow and uses the last value?

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw

----- Original Message -----

Quoted 33 lines

From: "Avi Kivity" <[email protected]>
To: "Igor Mammedov" <[email protected]>
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], "amit shah" <[email protected]>, [email protected]
Sent: Wednesday, February 15, 2012 3:02:04 PM
Subject: Re: [PATCH] BUG in pv_clock when overflow condition is detected

Quoted 16 lines · Igor Mammedov
Quoted 11 lines

arch/x86/kernel/pvclock.c void pvclock_set_flags(u8 flags) · line 51

Quoted 10 lines

static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time
*shadow)
{

arch/x86/kernel/pvclock.c +4 -1
-    u64 delta = native_read_tsc() - shadow->tsc_timestamp;
+    u64 delta;
+    u64 tsc = native_read_tsc();
+    BUG_ON(tsc<  shadow->tsc_timestamp);
+    delta = tsc - shadow->tsc_timestamp;
      return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
                     shadow->tsc_shift);
Quoted 3 lines

Maybe a WARN_ON_ONCE()? Otherwise a relatively minor hypervisor bug can
kill the guest.

Quoted 8 lines

An attempt to print from this place is not perfect since it often leads
to recursive calling to this very function and it hang there anyway.
But if you insist I'll re-post it with WARN_ON_ONCE,
It won't make much difference because guest will hang/stall due overflow
anyway.

Quoted 1 line

Won't a BUG_ON() also result in a printk?

Yes, it will. But stack will still keep failure point and poking with crash/gdb at core will always show where it's BUGged.

In case it manages to print dump somehow (saw it couple times from ~ 30 test cycles), logs from console or from kernel message buffer (again poking with gdb) will show where it was called from.

If WARN* is used, it will still totaly screwup clock and "last value" and system will become unusable, requiring looking with gdb/crash at the core any way.

So I've just used more stable failure point that will leave trace everywhere it manages (maybe in console log, but for sure in stack) in case of WARN it might leave trace on console or not and probably won't reflect failure point in stack either leaving only kernel message buffer for clue.

Quoted 10 lines
Quoted 9 lines

If there is an intention to keep guest functional after the event then
maybe this patch is a way to go
http://www.spinics.net/lists/kvm/msg68463.html
this way clock will be re-silent to this kind of errors, like bare-metal
one is.

Quoted 3 lines

It's the same patch... do you mean something that detects the overflow
and uses the last value?

I'm sorry, pasted wrong link
here it goes:
"pvclock: Make pv_clock more robust and fixup it if overflow happens"

     http://www.spinics.net/lists/kvm/msg68440.html
Quoted 9 lines

--
error compiling committee.c: too many arguments to function

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 38 lines · Igor Mammedov
Quoted 26 lines · Igor Mammedov
Quoted 16 lines
Quoted 11 lines

arch/x86/kernel/pvclock.c void pvclock_set_flags(u8 flags) · line 51

Quoted 10 lines

static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time
*shadow)
{

arch/x86/kernel/pvclock.c +4 -1
-    u64 delta = native_read_tsc() - shadow->tsc_timestamp;
+    u64 delta;
+    u64 tsc = native_read_tsc();
+    BUG_ON(tsc<  shadow->tsc_timestamp);
+    delta = tsc - shadow->tsc_timestamp;
      return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
                     shadow->tsc_shift);
Quoted 3 lines

Maybe a WARN_ON_ONCE()? Otherwise a relatively minor hypervisor bug can
kill the guest.

Quoted 8 lines

An attempt to print from this place is not perfect since it often leads
to recursive calling to this very function and it hang there anyway.
But if you insist I'll re-post it with WARN_ON_ONCE,
It won't make much difference because guest will hang/stall due overflow
anyway.

Quoted 1 line

Won't a BUG_ON() also result in a printk?

Yes, it will. But stack will still keep failure point and poking with crash/gdb at core will always show where it's BUGged.

In case it manages to print dump somehow (saw it couple times from ~ 30 test cycles), logs from console or from kernel message buffer (again poking with gdb) will show where it was called from.

If WARN* is used, it will still totaly screwup clock and "last value" and system will become unusable, requiring looking with gdb/crash at the core any way.

Quoted 6 lines

So I've just used more stable failure point that will leave trace everywhere it manages (maybe in console log, but for sure in stack) in case of WARN it might leave trace on console or not and probably won't reflect failure point in stack either leaving only kernel message buffer for clue.

Makes sense. But do get an ack from the Xen people to ensure this doesn't break for them.

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw
Quoted 46 lines · Avi Kivity
Quoted 38 lines · Igor Mammedov
Quoted 26 lines · Igor Mammedov
Quoted 16 lines
Quoted 11 lines

arch/x86/kernel/pvclock.c void pvclock_set_flags(u8 flags) · line 51

Quoted 10 lines

static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time
*shadow)
{

arch/x86/kernel/pvclock.c +4 -1
-    u64 delta = native_read_tsc() - shadow->tsc_timestamp;
+    u64 delta;
+    u64 tsc = native_read_tsc();
+    BUG_ON(tsc<   shadow->tsc_timestamp);
+    delta = tsc - shadow->tsc_timestamp;
       return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
                      shadow->tsc_shift);
Quoted 3 lines

Maybe a WARN_ON_ONCE()? Otherwise a relatively minor hypervisor bug can
kill the guest.

Quoted 8 lines

An attempt to print from this place is not perfect since it often leads
to recursive calling to this very function and it hang there anyway.
But if you insist I'll re-post it with WARN_ON_ONCE,
It won't make much difference because guest will hang/stall due overflow
anyway.

Quoted 1 line

Won't a BUG_ON() also result in a printk?

Yes, it will. But stack will still keep failure point and poking with crash/gdb at core will always show where it's BUGged.

In case it manages to print dump somehow (saw it couple times from ~ 30 test cycles), logs from console or from kernel message buffer (again poking with gdb) will show where it was called from.

If WARN* is used, it will still totaly screwup clock and "last value" and system will become unusable, requiring looking with gdb/crash at the core any way.

Quoted 6 lines

So I've just used more stable failure point that will leave trace everywhere it manages (maybe in console log, but for sure in stack) in case of WARN it might leave trace on console or not and probably won't reflect failure point in stack either leaving only kernel message buffer for clue.

Quoted 3 lines

Makes sense. But do get an ack from the Xen people to ensure this doesn't break for them.

Konrad, Ian

Could you please review patch form point of view of xen? Whole thread could be found here https://lkml.org/lkml/2012/2/13/286

Signature · 2 lines
Thanks,
 Igor
#7 lore raw
Quoted 47 lines · Igor Mammedov
Quoted 46 lines · Avi Kivity
Quoted 44 lines · Igor Mammedov
Quoted 26 lines · Igor Mammedov
Quoted 16 lines
Quoted 11 lines

Review

Quoted 10 lines

static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time
*shadow)
{

(quoted diff) +4 -1
-    u64 delta = native_read_tsc() - shadow->tsc_timestamp;
+    u64 delta;
+    u64 tsc = native_read_tsc();
+    BUG_ON(tsc<   shadow->tsc_timestamp);
+    delta = tsc - shadow->tsc_timestamp;
      return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
                     shadow->tsc_shift);
Quoted 3 lines

Maybe a WARN_ON_ONCE()? Otherwise a relatively minor hypervisor bug can
kill the guest.

Quoted 8 lines

An attempt to print from this place is not perfect since it often leads
to recursive calling to this very function and it hang there anyway.
But if you insist I'll re-post it with WARN_ON_ONCE,
It won't make much difference because guest will hang/stall due overflow
anyway.

Quoted 1 line

Won't a BUG_ON() also result in a printk?

Yes, it will. But stack will still keep failure point and poking with crash/gdb at core will always show where it's BUGged.

In case it manages to print dump somehow (saw it couple times from ~ 30 test cycles), logs from console or from kernel message buffer (again poking with gdb) will show where it was called from.

If WARN* is used, it will still totaly screwup clock and "last value" and system will become unusable, requiring looking with gdb/crash at the core any way.

So I've just used more stable failure point that will leave trace everywhere it manages (maybe in console log, but for sure in stack) in case of WARN it might leave trace on console or not and probably won't reflect failure point in stack either leaving only kernel message buffer for clue.

Quoted 7 lines
Quoted 3 lines

Makes sense. But do get an ack from the Xen people to ensure this doesn't break for them.

Konrad, Ian

Could you please review patch form point of view of xen? Whole thread could be found here https://lkml.org/lkml/2012/2/13/286

What are the conditions under which this happens? You should probably include that in the git description as well? Is this something that happens often? If there is an overflow can you synthesize a value instead of crashing the guest?

Hm, so are you asking for review for this patch or for
http://www.spinics.net/lists/kvm/msg68440.html ?

(which would also entail a early_percpu_clock_init implementation in the Xen code naturally).

#8 lore raw
Quoted 51 lines · Konrad Rzeszutek Wilk
Quoted 50 lines · Igor Mammedov
Quoted 49 lines · Avi Kivity
Quoted 38 lines · Igor Mammedov
Quoted 26 lines · Igor Mammedov
Quoted 16 lines
Quoted 11 lines

Review

Quoted 10 lines

static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time
*shadow)
{

(quoted diff) +4 -1
-    u64 delta = native_read_tsc() - shadow->tsc_timestamp;
+    u64 delta;
+    u64 tsc = native_read_tsc();
+    BUG_ON(tsc<    shadow->tsc_timestamp);
+    delta = tsc - shadow->tsc_timestamp;
       return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
                      shadow->tsc_shift);
Quoted 3 lines

Maybe a WARN_ON_ONCE()? Otherwise a relatively minor hypervisor bug can
kill the guest.

Quoted 8 lines

An attempt to print from this place is not perfect since it often leads
to recursive calling to this very function and it hang there anyway.
But if you insist I'll re-post it with WARN_ON_ONCE,
It won't make much difference because guest will hang/stall due overflow
anyway.

Quoted 1 line

Won't a BUG_ON() also result in a printk?

Yes, it will. But stack will still keep failure point and poking with crash/gdb at core will always show where it's BUGged.

In case it manages to print dump somehow (saw it couple times from ~ 30 test cycles), logs from console or from kernel message buffer (again poking with gdb) will show where it was called from.

If WARN* is used, it will still totaly screwup clock and "last value" and system will become unusable, requiring looking with gdb/crash at the core any way.

Quoted 6 lines

So I've just used more stable failure point that will leave trace everywhere it manages (maybe in console log, but for sure in stack) in case of WARN it might leave trace on console or not and probably won't reflect failure point in stack either leaving only kernel message buffer for clue.

Makes sense. But do get an ack from the Xen people to ensure this doesn't break for them.

Quoted 7 lines
Quoted 4 lines

Konrad, Ian

Could you please review patch form point of view of xen? Whole thread could be found here https://lkml.org/lkml/2012/2/13/286

What are the conditions under which this happens?
You should probably include that in the git description as well?

This happens on cpu hot-plug in kvm guest:
https://lkml.org/lkml/2012/2/7/222

It probably doesn't affect xen pv guest but issue might affect hvm one. I'm certainly not xen expert to say it for sure after a cursory look at the code. If you can confirm that it affects xen hvm I will write early_percpu_clock_init patch for it as well.

Quoted 1 line

Is this something that happens often?

Very seldom and unlikely.

Quoted 1 line

Hm, so are you asking for review for this patch

I was asking for review of subj patch
"BUG in pv_clock when overflow condition is detected"
I'll update patch description and re-spin it.

Quoted 3 lines

If there is an overflow can you synthesize a value instead of
crashing the guest?
or for http://www.spinics.net/lists/kvm/msg68440.html ?

Probably could, but there was argument that it is fixing the symptoms and not the root cause. It seems that you've already found patch that proposes this "pvclock: Make pv_clock more robust and fixup it if overflow happens"

Quoted 4 lines

(which would also entail a early_percpu_clock_init implementation in the Xen code naturally).

Signature · 2 lines
Thanks,
 Igor