Skip to contentSkip to conversation

qemu vl.c

Qemu-devel Andrzej Zaborowski 4 participants 9 messages Dec 16, 2007 – Dec 18, 2007

lore
Share & tools
lore
#1 lore raw
Quoted 9 lines · Andrzej Zaborowski

CVSROOT: /sources/qemu
Module name: qemu
Changes by: Andrzej Zaborowski <balrog> 07/12/16 13:17:13

Modified files:
. : vl.c

Log message:
Redundant timer rearm optimisation by Anders Melchiorsen.

I think this is incorrect.

When a timer is modified, we need to rearm the host timer immediately. We can not wait for the old timer to fire, or for execution to fall back to the main_loop_wait. Consider the case where the guest adds a timer interrupt and immediately enters a tight loop - Quite a common operation when calibrating a software delay loop. The timer won't be rearmed until some other event occurs. It the system is otherwise quiescent and you're not using SDL output it may never occur.

Paul

#2 lore raw
Quoted 5 lines · Paul Brook
Quoted 1 line

Redundant timer rearm optimisation by Anders Melchiorsen.

I think this is incorrect.

When a timer is modified, we need to rearm the host timer immediately. We can not wait for the old timer to fire, or for execution to fall back to the main_loop_wait. Consider the case where the guest adds a timer interrupt and immediately enters a tight loop - Quite a common operation when calibrating a software delay loop. The timer won't be rearmed until some other event occurs. It the system is otherwise quiescent and you're not using SDL output it may never occur.

Hi Paul,

I am certainly open to it not being correct, but currently I fail to see how it changes the scenario that you mention. The rearming is moved away from qemu_run_timers(), not qemu_mod_timer(). Can you elaborate?

There was a previous patch to remove the rearming when timers are modified,

http://cvs.savannah.gnu.org/viewvc/qemu/vl.c?root=qemu&view=log#rev1.345

and to me it seems that this might affect the case that you describe.

Cheers,
Anders.

#3 lore raw
Quoted 13 lines · Anders
Quoted 11 lines · Paul Brook
Quoted 1 line

Redundant timer rearm optimisation by Anders Melchiorsen.

I think this is incorrect.

When a timer is modified, we need to rearm the host timer immediately. We can not wait for the old timer to fire, or for execution to fall back to the main_loop_wait. Consider the case where the guest adds a timer interrupt and immediately enters a tight loop - Quite a common operation when calibrating a software delay loop. The timer won't be rearmed until some other event occurs. It the system is otherwise quiescent and you're not using SDL output it may never occur.

Quoted 5 lines

Hi Paul,

I am certainly open to it not being correct, but currently I fail to see how it changes the scenario that you mention. The rearming is moved away from qemu_run_timers(), not qemu_mod_timer(). Can you elaborate?

It's possible that this code has always been broken. You changes make it much more obviously so.

Quoted 3 lines

There was a previous patch to remove the rearming when timers are modified,

http://cvs.savannah.gnu.org/viewvc/qemu/vl.c?root=qemu&view=log#rev1.345

I think this this is unrealted.

#4 lore raw
Quoted 16 lines · Paul Brook
Quoted 14 lines · Anders
Quoted 12 lines · Paul Brook
Quoted 2 lines

Redundant timer rearm optimisation by Anders Melchiorsen.

I think this is incorrect.

When a timer is modified, we need to rearm the host timer immediately. We can not wait for the old timer to fire, or for execution to fall back to the main_loop_wait. Consider the case where the guest adds a timer interrupt and immediately enters a tight loop - Quite a common operation when calibrating a software delay loop. The timer won't be rearmed until some other event occurs. It the system is otherwise quiescent and you're not using SDL output it may never occur.

Quoted 10 lines
Quoted 6 lines

Hi Paul,

I am certainly open to it not being correct, but currently I fail to see how it changes the scenario that you mention. The rearming is moved away from qemu_run_timers(), not qemu_mod_timer(). Can you elaborate?

It's possible that this code has always been broken. You changes make it much more obviously so.

I'm merging qemu-cvs into the kvm repository now, and with this commit in, kvm will hang after about a minute. Attaching to it with gdb or strace will cause it to resume, so this is very likely a missing signal problem.

(qemu will happily run with this patch; kvm is much more sensitive to missed signals).

Signature · 1 line
Any sufficiently difficult bug is indistinguishable from a feature.
#5 lore raw
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Redundant timer rearm optimisation by Anders Melchiorsen.

I'm merging qemu-cvs into the kvm repository now, and with this commit in, kvm will hang after about a minute. Attaching to it with gdb or strace will cause it to resume, so this is very likely a missing signal problem.

That is clearly not good.

I still cannot see anything wrong with my patch. As fewer signals are delivered now, it is possible that this change highlights some old race. It is certainly also possible that my patch is all wrong, even if I am not able to see it.

The next few days I will be travelling, but when I get back, I was planning to address Pauls concern by adding

  qemu_rearm_alarm_timer(alarm_timer);

to the bottom of qemu_mod_timer().

If you feel like it, you can try that out. I am predicting that I will have a hard time reproducing your hang, as I have only been testing with kvm, and obviously not seen this problem yet.

(What I really plan to add is a rearm only if the new timer is added at the head of the list, but the above should do for a test)

Cheers,
Anders.

#6 lore raw
Quoted 8 lines · Anders
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Redundant timer rearm optimisation by Anders Melchiorsen.

I'm merging qemu-cvs into the kvm repository now, and with this commit in, kvm will hang after about a minute. Attaching to it with gdb or strace will cause it to resume, so this is very likely a missing signal problem.

Quoted 4 lines

That is clearly not good.

I still cannot see anything wrong with my patch. As fewer signals are delivered now, it is possible that this change highlights some old race.

Yes, I don't see anything wrong in this change either but I see how the scenario given by Paul was already buggy before. Paul confirmed on IRC that this is also what he meant.

I suspect it was buggy since "unix" and "rtc" stopped being the only available clock sources.

Quoted 8 lines

It is certainly also possible that my patch is all wrong, even if I am not able to see it.

The next few days I will be travelling, but when I get back, I was planning to address Pauls concern by adding

  qemu_rearm_alarm_timer(alarm_timer);

That would be also my first guess at fixing this, but I have no good test-case (obviously I wouldn't have committed the change if I had such a test-case).

Quoted 9 lines

to the bottom of qemu_mod_timer().

If you feel like it, you can try that out. I am predicting that I will have a hard time reproducing your hang, as I have only been testing with kvm, and obviously not seen this problem yet.

(What I really plan to add is a rearm only if the new timer is added at the head of the list, but the above should do for a test)

Attached is a small patch to do exactly this. The rearming in main_loop_wait() can also be made conditional but I don't think there would be a real gain.

Testing with KVM will be appreciated.

Regards

vl.c +4 -0
index 70950f8..7552fde 100644
@@ -1041,6 +1041,10 @@ void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
     ts->expire_time = expire_time;
     ts->next = *pt;
     *pt = ts;
+
+    /* rearm if the necessary */
+    if (pt == &active_timers[ts->clock->type])
+        qemu_rearm_alarm_timer(alarm_timer);
 }
 
 int qemu_timer_pending(QEMUTimer *ts)
#7 lore raw
Quoted 8 lines · andrzej zaborowski

Attached is a small patch to do exactly this. The rearming in main_loop_wait() can also be made conditional but I don't think there would be a real gain.

Testing with KVM will be appreciated.

Regards

Time in grub is completely off, Linux calibrate_delay() complains, 'sleep 1' doesn't return, but FC6 x86-64 boots.

(this is with your patch on top of the re-applied patch I reverted earlier).

Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw
Quoted 4 lines · Avi Kivity

Time in grub is completely off, Linux calibrate_delay() complains, 'sleep 1' doesn't return, but FC6 x86-64 boots.

(this is with your patch on top of the re-applied patch I reverted earlier).

Now I have updated my kvm checkout, and I immediately got trouble booting the guest. So it seems something has changed since I made my first patch.

What I could find is that the "env" variable in host_alarm_handler() is always NULL now? Then the expiry flag has to be set outside of that conditional, or alarms will never expire.

Also, an optimization of the new rearming in qemu_mod_timer() is that it should not be done if we are currently running the expired timers. It will be done after all the running is over, anyway.

Attached is a reworked patch with these changes, it should apply against your kvm git master (i.e. do not re-apply the reverted one).

Anders.

qemu/vl.c +14 -1
index 28c5df4..000df7e 100644
@@ -850,6 +850,7 @@ struct qemu_alarm_timer {
 };
 
 #define ALARM_FLAG_DYNTICKS  0x1
+#define ALARM_FLAG_EXPIRED   0x2
 
 static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
 {
@@ -1061,6 +1062,12 @@ void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
     ts->expire_time = expire_time;
     ts->next = *pt;
     *pt = ts;
+
+    /* Rearm if necessary  */
+    if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0
+        && pt == &active_timers[ts->clock->type]) {
+        qemu_rearm_alarm_timer(alarm_timer);
+    }
 }
 
 int qemu_timer_pending(QEMUTimer *ts)
@@ -1095,7 +1102,6 @@ static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
         /* run the callback (the timer list can be modified) */
         ts->cb(ts->opaque);
     }
-    qemu_rearm_alarm_timer(alarm_timer);
 }
 
 int64_t qemu_get_clock(QEMUClock *clock)
@@ -1215,6 +1221,8 @@ static void host_alarm_handler(int host_signum)
 #endif
         CPUState *env = next_cpu;
 
+        alarm_timer->flags |= ALARM_FLAG_EXPIRED;
+
         if (env) {
             /* stop the currently executing cpu because a timer occured */
             cpu_interrupt(env, CPU_INTERRUPT_EXIT);
@@ -7726,6 +7734,11 @@ void main_loop_wait(int timeout)
     qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
                     qemu_get_clock(rt_clock));
 
+    if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
+        alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED);
+        qemu_rearm_alarm_timer(alarm_timer);
+    }
+
     /* Check bottom-halves last in case any of the earlier events triggered
        them.  */
     qemu_bh_poll();