linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [PATCH] kvm: fix i8254 counter 0 wraparound
       [not found] <1355571277-6096-1-git-send-email-nickolai@csail.mit.edu>
@ 2012-12-17 18:34 ` Gleb Natapov
  2012-12-17 19:41 ` Marcelo Tosatti
  2012-12-18  9:12 ` Gleb Natapov
  2 siblings, 0 replies; 3+ messages in thread
From: Gleb Natapov @ 2012-12-17 18:34 UTC (permalink / raw)
  To: Nickolai Zeldovich
  Cc: Marcelo Tosatti, Thomas Gleixner, Ingo Molnar, H. Peter Anvin,
	x86, kvm, linux-kernel

On Sat, Dec 15, 2012 at 06:34:37AM -0500, Nickolai Zeldovich wrote:
> The kvm i8254 emulation for counter 0 (but not for counters 1 and 2)
> has at least two bugs in mode 0:
> 
> 1. The OUT bit, computed by pit_get_out(), is never set high.
> 
> 2. The counter value, computed by pit_get_count(), wraps back around to
>    the initial counter value, rather than wrapping back to 0xFFFF
>    (which is the behavior described in the comment in __kpit_elapsed,
>    the behavior implemented by qemu, and the behavior observed on AMD
>    hardware).
> 
> The bug stems from __kpit_elapsed computing the elapsed time mod the
> initial counter value (stored as nanoseconds in ps->period).  This is both
> unnecessary (none of the callers of kpit_elapsed expect the value to be
> at most the initial counter value) and incorrect (it causes pit_get_count
> to appear to wrap around to the initial counter value rather than 0xFFFF).
> Removing this mod from __kpit_elapsed fixes both of the above bugs.
> 
> Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
Looks good to me. Marcelo, you were last touching the code. Can you look
at it too?

> ---
>  arch/x86/kvm/i8254.c |    1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c
> index 11300d2..c1d30b2 100644
> --- a/arch/x86/kvm/i8254.c
> +++ b/arch/x86/kvm/i8254.c
> @@ -122,7 +122,6 @@ static s64 __kpit_elapsed(struct kvm *kvm)
>  	 */
>  	remaining = hrtimer_get_remaining(&ps->timer);
>  	elapsed = ps->period - ktime_to_ns(remaining);
> -	elapsed = mod_64(elapsed, ps->period);
>  
>  	return elapsed;
>  }
> -- 
> 1.7.10.4

--
			Gleb.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kvm: fix i8254 counter 0 wraparound
       [not found] <1355571277-6096-1-git-send-email-nickolai@csail.mit.edu>
  2012-12-17 18:34 ` [PATCH] kvm: fix i8254 counter 0 wraparound Gleb Natapov
@ 2012-12-17 19:41 ` Marcelo Tosatti
  2012-12-18  9:12 ` Gleb Natapov
  2 siblings, 0 replies; 3+ messages in thread
From: Marcelo Tosatti @ 2012-12-17 19:41 UTC (permalink / raw)
  To: Nickolai Zeldovich
  Cc: Gleb Natapov, Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86,
	kvm, linux-kernel

On Sat, Dec 15, 2012 at 06:34:37AM -0500, Nickolai Zeldovich wrote:
> The kvm i8254 emulation for counter 0 (but not for counters 1 and 2)
> has at least two bugs in mode 0:
> 
> 1. The OUT bit, computed by pit_get_out(), is never set high.
> 
> 2. The counter value, computed by pit_get_count(), wraps back around to
>    the initial counter value, rather than wrapping back to 0xFFFF
>    (which is the behavior described in the comment in __kpit_elapsed,
>    the behavior implemented by qemu, and the behavior observed on AMD
>    hardware).
> 
> The bug stems from __kpit_elapsed computing the elapsed time mod the
> initial counter value (stored as nanoseconds in ps->period).  This is both
> unnecessary (none of the callers of kpit_elapsed expect the value to be
> at most the initial counter value) and incorrect (it causes pit_get_count
> to appear to wrap around to the initial counter value rather than 0xFFFF).
> Removing this mod from __kpit_elapsed fixes both of the above bugs.
> 
> Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
> ---
>  arch/x86/kvm/i8254.c |    1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c
> index 11300d2..c1d30b2 100644
> --- a/arch/x86/kvm/i8254.c
> +++ b/arch/x86/kvm/i8254.c
> @@ -122,7 +122,6 @@ static s64 __kpit_elapsed(struct kvm *kvm)
>  	 */
>  	remaining = hrtimer_get_remaining(&ps->timer);
>  	elapsed = ps->period - ktime_to_ns(remaining);
> -	elapsed = mod_64(elapsed, ps->period);
>  
>  	return elapsed;
>  }

Reviewed-by: Marcelo Tosatti <mtosatti@redhat.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kvm: fix i8254 counter 0 wraparound
       [not found] <1355571277-6096-1-git-send-email-nickolai@csail.mit.edu>
  2012-12-17 18:34 ` [PATCH] kvm: fix i8254 counter 0 wraparound Gleb Natapov
  2012-12-17 19:41 ` Marcelo Tosatti
@ 2012-12-18  9:12 ` Gleb Natapov
  2 siblings, 0 replies; 3+ messages in thread
From: Gleb Natapov @ 2012-12-18  9:12 UTC (permalink / raw)
  To: Nickolai Zeldovich
  Cc: Marcelo Tosatti, Thomas Gleixner, Ingo Molnar, H. Peter Anvin,
	x86, kvm, linux-kernel

On Sat, Dec 15, 2012 at 06:34:37AM -0500, Nickolai Zeldovich wrote:
> The kvm i8254 emulation for counter 0 (but not for counters 1 and 2)
> has at least two bugs in mode 0:
> 
> 1. The OUT bit, computed by pit_get_out(), is never set high.
> 
> 2. The counter value, computed by pit_get_count(), wraps back around to
>    the initial counter value, rather than wrapping back to 0xFFFF
>    (which is the behavior described in the comment in __kpit_elapsed,
>    the behavior implemented by qemu, and the behavior observed on AMD
>    hardware).
> 
> The bug stems from __kpit_elapsed computing the elapsed time mod the
> initial counter value (stored as nanoseconds in ps->period).  This is both
> unnecessary (none of the callers of kpit_elapsed expect the value to be
> at most the initial counter value) and incorrect (it causes pit_get_count
> to appear to wrap around to the initial counter value rather than 0xFFFF).
> Removing this mod from __kpit_elapsed fixes both of the above bugs.
> 
> Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
Applied, thanks!

> ---
>  arch/x86/kvm/i8254.c |    1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c
> index 11300d2..c1d30b2 100644
> --- a/arch/x86/kvm/i8254.c
> +++ b/arch/x86/kvm/i8254.c
> @@ -122,7 +122,6 @@ static s64 __kpit_elapsed(struct kvm *kvm)
>  	 */
>  	remaining = hrtimer_get_remaining(&ps->timer);
>  	elapsed = ps->period - ktime_to_ns(remaining);
> -	elapsed = mod_64(elapsed, ps->period);
>  
>  	return elapsed;
>  }
> -- 
> 1.7.10.4

--
			Gleb.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-12-18  9:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1355571277-6096-1-git-send-email-nickolai@csail.mit.edu>
2012-12-17 18:34 ` [PATCH] kvm: fix i8254 counter 0 wraparound Gleb Natapov
2012-12-17 19:41 ` Marcelo Tosatti
2012-12-18  9:12 ` Gleb Natapov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).