diff mbox

[1/2] qemu-timer: Call clock reset notifiers on forward jumps

Message ID 20150612140845.GD2749@TopQuark.net
State New
Headers show

Commit Message

Paul Donohue June 12, 2015, 2:08 p.m. UTC
Commit 691a0c9c introduced a mechanism by which QEMU_CLOCK_HOST can
notify other parts of the emulator when the host clock has jumped 
backward.  This is used to avoid stalling timers that were scheduled
based on the host clock.

However, if the host clock jumps forward, then timers that were
scheduled based on the host clock may fire rapidly and cause other
problems.  For example, the mc146818rtc periodic timer will block
execution of the VM and consume host CPU while firing every interrupt
for the time period that was skipped by the host clock.

To correct that problem, this commit fires the reset notification if the
host clock jumps forward by more than a hard-coded limit.  The limit is
currently set to a value of 60 seconds, which should be small enough to 
prevent excessive timer loops, but large enough to avoid frequent resets 
in idle VMs.

Signed-off-by: Paul Donohue <qemu-git@PaulSD.com>
---
 include/qemu/timer.h | 9 +++++++++
 qemu-timer.c         | 2 +-
 2 files changed, 10 insertions(+), 1 deletion(-)

Comments

Paolo Bonzini June 17, 2015, 2 p.m. UTC | #1
On 12/06/2015 16:08, Paul Donohue wrote:
> +static inline int64_t get_max_clock_jump(void)
> +{
> +    // This should be small enough to prevent excessive interrupts from being
> +    // generated by the RTC on clock jumps, but large enough to avoid frequent
> +    // unnecessary resets in idle VMs.

This is not how comments are layed out in QEMU...
> +    return 60 * get_ticks_per_sec();
> +}
> +
>  /*
>  *   * Low level clock functions
>  *     */

... and it also looks like your editor has mangled this patch.
Nevertheless, I've applied it to my local branch and will send it out
for inclusion after some more testing.

Paolo

> diff --git a/qemu-timer.c b/qemu-timer.c
> index 5741f0d..d27be3d 100644
> --- a/qemu-timer.c
> +++ b/qemu-timer.c
Paul Donohue June 18, 2015, 12:58 p.m. UTC | #2
On Wed, Jun 17, 2015 at 04:00:06PM +0200, Paolo Bonzini wrote:
> On 12/06/2015 16:08, Paul Donohue wrote:
> > +static inline int64_t get_max_clock_jump(void)
> > +{
> > +    // This should be small enough to prevent excessive interrupts from being
> > +    // generated by the RTC on clock jumps, but large enough to avoid frequent
> > +    // unnecessary resets in idle VMs.
> This is not how comments are layed out in QEMU...
Oops.  Sorry, I'm not sure how I missed that.  Force of habit from 
other projects, I guess...

> > +    return 60 * get_ticks_per_sec();
> > +}
> > +
> >  /*
> >  *   * Low level clock functions
> >  *     */
> ... and it also looks like your editor has mangled this patch.
Ugh, yes.  I do remember noticing that, and I thought I fixed it, but 
apparently not.

I will correct and send an updated patch.
Paolo Bonzini June 18, 2015, 1:17 p.m. UTC | #3
On 18/06/2015 14:58, Paul Donohue wrote:
> On Wed, Jun 17, 2015 at 04:00:06PM +0200, Paolo Bonzini wrote:
>> On 12/06/2015 16:08, Paul Donohue wrote:
>>> +static inline int64_t get_max_clock_jump(void)
>>> +{
>>> +    // This should be small enough to prevent excessive interrupts from being
>>> +    // generated by the RTC on clock jumps, but large enough to avoid frequent
>>> +    // unnecessary resets in idle VMs.
>> This is not how comments are layed out in QEMU...
> Oops.  Sorry, I'm not sure how I missed that.  Force of habit from 
> other projects, I guess...
> 
>>> +    return 60 * get_ticks_per_sec();
>>> +}
>>> +
>>>  /*
>>>  *   * Low level clock functions
>>>  *     */
>> ... and it also looks like your editor has mangled this patch.
> Ugh, yes.  I do remember noticing that, and I thought I fixed it, but 
> apparently not.
> 
> I will correct and send an updated patch.

No problem, I fixed this already.

Paolo
diff mbox

Patch

diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index e5bd494..0193036 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -787,6 +787,14 @@  static inline int64_t get_ticks_per_sec(void)
     return 1000000000LL;
 }
 
+static inline int64_t get_max_clock_jump(void)
+{
+    // This should be small enough to prevent excessive interrupts from being
+    // generated by the RTC on clock jumps, but large enough to avoid frequent
+    // unnecessary resets in idle VMs.
+    return 60 * get_ticks_per_sec();
+}
+
 /*
 *   * Low level clock functions
 *     */
diff --git a/qemu-timer.c b/qemu-timer.c
index 5741f0d..d27be3d 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -573,7 +573,7 @@  int64_t qemu_clock_get_ns(QEMUClockType type)
         now = get_clock_realtime();
         last = clock->last;
         clock->last = now;
-        if (now < last) {
+        if (now < last || now > (last + get_max_clock_jump())) {
             notifier_list_notify(&clock->reset_notifiers, &now);
         }
         return now;