Skip to contentSkip to conversation

hw/ptimer: Assert next_event is newer than last_event

PATCH In review Philippe Mathieu-Daudé 2 participants 5 messages Sep 21, 2019 – Sep 23, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/ptimer: Assert next_event is newer than last_event

If the period is too big, the 'delta * period' product result might overflow, resulting in a negative number, then the next_event ends before the last_event. This is buggy, as there is no forward progress. Assert this can not happen.

---
 hw/core/ptimer.c | 3 +++
 1 file changed, 3 insertions(+)
hw/core/ptimer.c +3 -0
index d58e2dfdb0..88085d4c81 100644
@@ -125,6 +125,9 @@ static void ptimer_reload(ptimer_state *s, int delta_adjust)
 
     s->last_event = s->next_event;
     s->next_event = s->last_event + delta * period;
+    /* Verify forward progress */
+    g_assert(s->next_event > s->last_event);
+
     if (period_frac) {
         s->next_event += ((int64_t)period_frac * delta) >> 32;
     }
Signature · 1 line
2.20.1
#1 lore raw
Quoted 16 lines · Philippe Mathieu-Daudé

If the period is too big, the 'delta * period' product result might overflow, resulting in a negative number, then the next_event ends before the last_event. This is buggy, as there is no forward progress. Assert this can not happen.

---
 hw/core/ptimer.c | 3 +++
 1 file changed, 3 insertions(+)
hw/core/ptimer.c +0 -0
index d58e2dfdb0..88085d4c81 100644

hw/core/ptimer.c static void ptimer_reload(ptimer_state *s, int delta_adjust)

Quoted 10 lines
hw/core/ptimer.c +3 -1
@@ -125,6 +125,9 @@ static void ptimer_reload(ptimer_state *s, int delta_adjust)
 
     s->last_event = s->next_event;
     s->next_event = s->last_event + delta * period;
+    /* Verify forward progress */
+    g_assert(s->next_event > s->last_event);
+
     if (period_frac) {
         s->next_event += ((int64_t)period_frac * delta) >> 32;
     }
--

Can this only happen if a QEMU timer model using the ptimer code has a bug, or is it guest-triggerable for some of our timer models?

thanks
-- PMM

#2 lore raw
Quoted 18 lines · Peter Maydell
Quoted 17 lines · Philippe Mathieu-Daudé

If the period is too big, the 'delta * period' product result might overflow, resulting in a negative number, then the next_event ends before the last_event. This is buggy, as there is no forward progress. Assert this can not happen.

---
 hw/core/ptimer.c | 3 +++
 1 file changed, 3 insertions(+)
hw/core/ptimer.c +0 -0
index d58e2dfdb0..88085d4c81 100644
@@ -125,6 +125,9 @@ static void ptimer_reload(ptimer_state *s, int delta_adjust)
 
Quoted 13 lines

hw/core/ptimer.c static void ptimer_reload(ptimer_state *s, int delta_adjust)

Quoted 9 lines
hw/core/ptimer.c +3 -1
     s->last_event = s->next_event;
     s->next_event = s->last_event + delta * period;
+    /* Verify forward progress */
+    g_assert(s->next_event > s->last_event);
+
     if (period_frac) {
         s->next_event += ((int64_t)period_frac * delta) >> 32;
     }
--

Can this only happen if a QEMU timer model using the ptimer code has a bug, or is it guest-triggerable for some of our timer models?

I hit this running a raspi4 guest, I had incorrectly initialized a clock using the core cpu frequency, while I had to use the APB one (in my case, core_cpu_freq / 2). The guest use a high value to configure a slow timer, which in my buggy case made QEMU hang in hard way to debug.

So yes, it seems guest-triggerable if the implementation is broken. Using assert() is OK for broken implementation, right? Or should we audit all ptimer calls?

Regards,

Phil.

#3 lore raw
Quoted 13 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Peter Maydell
Quoted 5 lines · Philippe Mathieu-Daudé

If the period is too big, the 'delta * period' product result might overflow, resulting in a negative number, then the next_event ends before the last_event. This is buggy, as there is no forward progress. Assert this can not happen.

Can this only happen if a QEMU timer model using the ptimer code has a bug, or is it guest-triggerable for some of our timer models?

Quoted 7 lines

I hit this running a raspi4 guest, I had incorrectly initialized a clock using the core cpu frequency, while I had to use the APB one (in my case, core_cpu_freq / 2). The guest use a high value to configure a slow timer, which in my buggy case made QEMU hang in hard way to debug.

So yes, it seems guest-triggerable if the implementation is broken. Using assert() is OK for broken implementation, right?

Yeah, if this can only happen if QEMU code is broken then an assert is OK. I was just trying to find out what the cause was, since "this is buggy" isn't specific about where the bug is.

Quoted 1 line

Or should we audit all ptimer calls?

I don't think we specifically need an audit. We could perhaps expand the comment by the assert to specifically say that if the calculation of the next event overflowed then this indicates a bug in the QEMU device model using the ptimer API, so if somebody else runs into the assert they have a hint about what to look at. (An overflowed next_event indicates a time incredibly far in the future, given that it's a nanosecond time in an int64_t.)

The other approach I thought of would be to make the ptimer code handle this sort of after-the-end-of-QEMU-universe time by saturating next_event to INT64_MAX rather than letting it overflow and wrap. Unfortunately while this would be fine for the 'timer event' part of the code, it would break ptimer_get_count() which calculates the current counter value by looking at the difference between the current time and the time of the next event (fixable but only with a bunch of messing about to treat a next_event of INT64_MAX as equivalent to the counter being disabled and tracking the counter value in s->delta). So an assert is the best thing I think.

thanks
-- PMM

#4 lore raw
Quoted 29 lines · Peter Maydell
Quoted 13 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Peter Maydell
Quoted 5 lines · Philippe Mathieu-Daudé

If the period is too big, the 'delta * period' product result might overflow, resulting in a negative number, then the next_event ends before the last_event. This is buggy, as there is no forward progress. Assert this can not happen.

Can this only happen if a QEMU timer model using the ptimer code has a bug, or is it guest-triggerable for some of our timer models?

Quoted 7 lines

I hit this running a raspi4 guest, I had incorrectly initialized a clock using the core cpu frequency, while I had to use the APB one (in my case, core_cpu_freq / 2). The guest use a high value to configure a slow timer, which in my buggy case made QEMU hang in hard way to debug.

So yes, it seems guest-triggerable if the implementation is broken. Using assert() is OK for broken implementation, right?

Yeah, if this can only happen if QEMU code is broken then an assert is OK. I was just trying to find out what the cause was, since "this is buggy" isn't specific about where the bug is.

Quoted 1 line

Or should we audit all ptimer calls?

Quoted 8 lines

I don't think we specifically need an audit. We could perhaps expand the comment by the assert to specifically say that if the calculation of the next event overflowed then this indicates a bug in the QEMU device model using the ptimer API, so if somebody else runs into the assert they have a hint about what to look at. (An overflowed next_event indicates a time incredibly far in the future, given that it's a nanosecond time in an int64_t.)

OK I'll improve the comment.

Quoted 4 lines

The other approach I thought of would be to make the ptimer code handle this sort of after-the-end-of-QEMU-universe time by saturating next_event to INT64_MAX rather than letting it overflow and wrap. Unfortunately while this would be fine for

Quoted 8 lines

the 'timer event' part of the code, it would break ptimer_get_count() which calculates the current counter value by looking at the difference between the current time and the time of the next event (fixable but only with a bunch of messing about to treat a next_event of INT64_MAX as equivalent to the counter being disabled and tracking the counter value in s->delta). So an assert is the best thing I think.

Yes :/

Thanks!

Phil.