Skip to contentSkip to conversation

hw/ptimer: Don't wrap around counter for expired timer that uses tick handler

PATCH In review Dmitry Osipenko 3 participants 13 messages Jun 25, 2016 – Jul 7, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hw/ptimer: Don't wrap around counter for expired timer that uses tick handler

Software should see timer counter wrap around only after IRQ being triggered. Change returned counter value to "1" for the expired timer and avoid returning wrapped around counter value in periodic mode for the timer that has bottom-half handler setup, assuming it drives timer IRQ.

This fixes regression introduced by the commit 5a50307 ("hw/ptimer: Perform counter wrap around if timer already expired") on SPARC emulated machine as reported by Mark Cave-Ayland.

---
 hw/core/ptimer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/core/ptimer.c +5 -3
index 05b0c27..8006442 100644
@@ -93,10 +93,10 @@ uint64_t ptimer_get_count(ptimer_state *s)
         bool oneshot = (s->enabled == 2);
 
         /* Figure out the current counter value.  */
-        if (s->period == 0 || (expired && (oneshot || use_icount))) {
+        if (expired && (oneshot || use_icount || s->bh != NULL)) {
             /* Prevent timer underflowing if it should already have
                triggered.  */
-            counter = 0;
+            counter = 1;
         } else {
             uint64_t rem;
             uint64_t div;
@@ -143,7 +143,9 @@ uint64_t ptimer_get_count(ptimer_state *s)
 
             if (expired && counter != 0) {
                 /* Wrap around periodic counter.  */
-                counter = s->limit - (counter - 1) % s->limit;
+                counter = s->delta = s->limit - (counter - 1) % s->limit;
+                /* Re-arm timer according to the wrapped around value.  */
+                ptimer_reload(s);
             }
         }
     } else {
Signature · 1 line
2.9.0
#1 lore raw

V2: Patch applies cleanly to the QEMU master branch.

Signature · 1 line
Dmitry
Tested-by on v2 1/1
#2 lore raw
In reply to Dmitry Osipenko
Quoted 33 lines · Dmitry Osipenko

Software should see timer counter wrap around only after IRQ being triggered. Change returned counter value to "1" for the expired timer and avoid returning wrapped around counter value in periodic mode for the timer that has bottom-half handler setup, assuming it drives timer IRQ.

This fixes regression introduced by the commit 5a50307 ("hw/ptimer: Perform counter wrap around if timer already expired") on SPARC emulated machine as reported by Mark Cave-Ayland.

---
 hw/core/ptimer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/core/ptimer.c +2 -2
index 05b0c27..8006442 100644
@@ -93,10 +93,10 @@ uint64_t ptimer_get_count(ptimer_state *s)
         bool oneshot = (s->enabled == 2);
 
         /* Figure out the current counter value.  */
-        if (s->period == 0 || (expired && (oneshot || use_icount))) {
+        if (expired && (oneshot || use_icount || s->bh != NULL)) {
             /* Prevent timer underflowing if it should already have
                triggered.  */
-            counter = 0;
+            counter = 1;
         } else {
             uint64_t rem;
             uint64_t div;

hw/core/ptimer.c uint64_t ptimer_get_count(ptimer_state *s) · line 151

Quoted 10 lines
hw/core/ptimer.c +3 -1
@@ -143,7 +143,9 @@ uint64_t ptimer_get_count(ptimer_state *s)
 
             if (expired && counter != 0) {
                 /* Wrap around periodic counter.  */
-                counter = s->limit - (counter - 1) % s->limit;
+                counter = s->delta = s->limit - (counter - 1) % s->limit;
+                /* Re-arm timer according to the wrapped around value.  */
+                ptimer_reload(s);
             }
         }
     } else {

Hi Dmitry,

I ran through all of my OpenBIOS test images for qemu-system-sparc and AFAICT this fixes the issue without introducing any further regressions so:

Many thanks,

Mark.

#3 lore raw
Quoted 51 lines · Mark Cave-Ayland
Quoted 33 lines · Dmitry Osipenko

Software should see timer counter wrap around only after IRQ being triggered. Change returned counter value to "1" for the expired timer and avoid returning wrapped around counter value in periodic mode for the timer that has bottom-half handler setup, assuming it drives timer IRQ.

This fixes regression introduced by the commit 5a50307 ("hw/ptimer: Perform counter wrap around if timer already expired") on SPARC emulated machine as reported by Mark Cave-Ayland.

---
 hw/core/ptimer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/core/ptimer.c +2 -2
index 05b0c27..8006442 100644
@@ -93,10 +93,10 @@ uint64_t ptimer_get_count(ptimer_state *s)
         bool oneshot = (s->enabled == 2);
 
         /* Figure out the current counter value.  */
-        if (s->period == 0 || (expired && (oneshot || use_icount))) {
+        if (expired && (oneshot || use_icount || s->bh != NULL)) {
             /* Prevent timer underflowing if it should already have
                triggered.  */
-            counter = 0;
+            counter = 1;
         } else {
             uint64_t rem;
             uint64_t div;

hw/core/ptimer.c uint64_t ptimer_get_count(ptimer_state *s) · line 151

Quoted 10 lines
hw/core/ptimer.c +3 -1
@@ -143,7 +143,9 @@ uint64_t ptimer_get_count(ptimer_state *s)
 
             if (expired && counter != 0) {
                 /* Wrap around periodic counter.  */
-                counter = s->limit - (counter - 1) % s->limit;
+                counter = s->delta = s->limit - (counter - 1) % s->limit;
+                /* Re-arm timer according to the wrapped around value.  */
+                ptimer_reload(s);
             }
         }
     } else {

Hi Dmitry,

I ran through all of my OpenBIOS test images for qemu-system-sparc and AFAICT this fixes the issue without introducing any further regressions so:

Quoted 7 lines

Many thanks,

Mark.

Great! Thanks for testing it.

Signature · 1 line
Dmitry
#4 lore raw
In reply to Dmitry Osipenko
Quoted 21 lines · Dmitry Osipenko

Software should see timer counter wrap around only after IRQ being triggered. Change returned counter value to "1" for the expired timer and avoid returning wrapped around counter value in periodic mode for the timer that has bottom-half handler setup, assuming it drives timer IRQ.

This fixes regression introduced by the commit 5a50307 ("hw/ptimer: Perform counter wrap around if timer already expired") on SPARC emulated machine as reported by Mark Cave-Ayland.

---
 hw/core/ptimer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/core/ptimer.c +0 -0
index 05b0c27..8006442 100644
@@ -93,10 +93,10 @@ uint64_t ptimer_get_count(ptimer_state *s)
         bool oneshot = (s->enabled == 2);
 

hw/core/ptimer.c uint64_t ptimer_get_count(ptimer_state *s) · line 102

Quoted 10 lines
hw/core/ptimer.c +2 -2
         /* Figure out the current counter value.  */
-        if (s->period == 0 || (expired && (oneshot || use_icount))) {
+        if (expired && (oneshot || use_icount || s->bh != NULL)) {
             /* Prevent timer underflowing if it should already have
                triggered.  */
-            counter = 0;
+            counter = 1;
         } else {
             uint64_t rem;
             uint64_t div;

I guess this fixes a regression, but it looks really weird. Why should the timer behaviour change if there happens to be a bottom half present? That should be an internal implementation detail. It's also a bit odd that use_icount is in the check:
that shouldn't generally affect device emulation behaviour...

thanks
-- PMM

#5 lore raw
Quoted 5 lines · Peter Maydell

I guess this fixes a regression, but it looks really weird. Why should the timer behaviour change if there happens to be a bottom half present? That should be an internal implementation detail. It's also a bit odd that use_icount is in the check:
that shouldn't generally affect device emulation behaviour...

In case of a polled timer that doesn't have ptimer trigger bottom half callback setup, we are free to wrap around counter since timer behaviour isn't changed from ptimer user perspective, as it won't be able to change it's state in the handler.

I just decided to keep that wraparound feature for a case of a polled free running timer, this should result in a better distribution of the polled value. The potential users of that feature are "imx_epit" and "digic" timer device models. I should have mentioned it in the commit message to avoid confusion, sorry.

It is still an internal implementation detail, not sure what you are meaning. Could you elaborate, please?

"use_icount" is redundant now and should be omitted, good point.

This patch is supposed to fix IRQ set -> timer expire/counter wraparound ordering. I'm wondering whether we have same ordering issue with the scheduled ptimer trigger callback. I can imagine the following scenario:

1) ptimer_tick() -> periodic counter reloaded or set to "0" in oneshot mode and ptimer_trigger() schedules trigger callback

2) device uses ptimer_get_count() but the scheduled callback still pending execution.

That should happen in the same QEMU "cycle" to cause a potential issue. Is this possible? I'm not familiar with how AIO scheduler works.

The potential fix could be: to directly call trigger callback from ptimer_tick() after changing the delta value.

Signature · 1 line
Dmitry
#6 lore raw
Quoted 12 lines · Dmitry Osipenko
Quoted 5 lines · Peter Maydell

I guess this fixes a regression, but it looks really weird. Why should the timer behaviour change if there happens to be a bottom half present? That should be an internal implementation detail. It's also a bit odd that use_icount is in the check:
that shouldn't generally affect device emulation behaviour...

In case of a polled timer that doesn't have ptimer trigger bottom half callback setup, we are free to wrap around counter since timer behaviour isn't changed from ptimer user perspective, as it won't be able to change it's state in the handler.

Quoted 7 lines

I just decided to keep that wraparound feature for a case of a polled free running timer, this should result in a better distribution of the polled value. The potential users of that feature are "imx_epit" and "digic" timer device models. I should have mentioned it in the commit message to avoid confusion, sorry.

It is still an internal implementation detail, not sure what you are meaning. Could you elaborate, please?

What I meant was: ptimer_get_count() is typically called to generate a value to return from a register. That's a separate thing, conceptually, from whether the device happens to also trigger an interrupt on timer expiry by passing a bh to ptimer_init(). So it's very odd for a detail of interrupt-on-timer-expiry (that there is a bottom half) to affect the value returned when you read the timer count register.

thanks
-- PMM

#7 lore raw
Quoted 21 lines · Peter Maydell
Quoted 12 lines · Dmitry Osipenko
Quoted 5 lines · Peter Maydell

I guess this fixes a regression, but it looks really weird. Why should the timer behaviour change if there happens to be a bottom half present? That should be an internal implementation detail. It's also a bit odd that use_icount is in the check:
that shouldn't generally affect device emulation behaviour...

In case of a polled timer that doesn't have ptimer trigger bottom half callback setup, we are free to wrap around counter since timer behaviour isn't changed from ptimer user perspective, as it won't be able to change it's state in the handler.

Quoted 7 lines

I just decided to keep that wraparound feature for a case of a polled free running timer, this should result in a better distribution of the polled value. The potential users of that feature are "imx_epit" and "digic" timer device models. I should have mentioned it in the commit message to avoid confusion, sorry.

It is still an internal implementation detail, not sure what you are meaning. Could you elaborate, please?

Quoted 7 lines

What I meant was: ptimer_get_count() is typically called to generate a value to return from a register. That's a separate thing, conceptually, from whether the device happens to also trigger an interrupt on timer expiry by passing a bh to ptimer_init(). So it's very odd for a detail of interrupt-on-timer-expiry (that there is a bottom half) to affect the value returned when you read the timer count register.

In order to handle wraparound correctly, software needs to track the moment of the wraparound - the interrupt. If software reads wrapped around counter value before IRQ triggered (ptimer expired), then it would assume that no wraparound happened and won't perform counter value correction, resulting in periodic counter "jumping" backwards.

Anything wrong with it? Am I missing something?

Signature · 1 line
Dmitry
#8 lore raw
Quoted 8 lines · Dmitry Osipenko
Quoted 6 lines · Peter Maydell

What I meant was: ptimer_get_count() is typically called to generate a value to return from a register. That's a separate thing, conceptually, from whether the device happens to also trigger an interrupt on timer expiry by passing a bh to ptimer_init(). So it's very odd for a detail of interrupt-on-timer-expiry (that there is a bottom half) to affect the value returned when you read the timer count register.

Quoted 5 lines

In order to handle wraparound correctly, software needs to track the moment of the wraparound - the interrupt. If software reads wrapped around counter value before IRQ triggered (ptimer expired), then it would assume that no wraparound happened and won't perform counter value correction, resulting in periodic counter "jumping" backwards.

That just says you need particular behaviour between counter reads and IRQ triggers; it doesn't say that you need the behaviour to be different if the ptimer code doesn't know about the IRQ trigger.

thanks
-- PMM

#9 lore raw
Quoted 15 lines · Peter Maydell
Quoted 8 lines · Dmitry Osipenko
Quoted 6 lines · Peter Maydell

What I meant was: ptimer_get_count() is typically called to generate a value to return from a register. That's a separate thing, conceptually, from whether the device happens to also trigger an interrupt on timer expiry by passing a bh to ptimer_init(). So it's very odd for a detail of interrupt-on-timer-expiry (that there is a bottom half) to affect the value returned when you read the timer count register.

Quoted 5 lines

In order to handle wraparound correctly, software needs to track the moment of the wraparound - the interrupt. If software reads wrapped around counter value before IRQ triggered (ptimer expired), then it would assume that no wraparound happened and won't perform counter value correction, resulting in periodic counter "jumping" backwards.

Quoted 4 lines

That just says you need particular behaviour between counter reads and IRQ triggers; it doesn't say that you need the behaviour to be different if the ptimer code doesn't know about the IRQ trigger.

Okay, I already explained the reason for having two different behaviours - to make polled counter value more distributed when possible. If I understand you correctly, you don't like it because it is "odd" and I agree that it's a bit clumsy.

So, what we are going to do now? Would you just revert the offending commit or you have some other suggestions?

I think we still need to change the returned counter value to "1" in case of the expired timer, since it would result in the deterministic behaviour across of all of the timers. However, it definitely feels like it should go into the standalone patch and I can include it into the next iteration of the ptimer patches.

Signature · 1 line
Dmitry
#10 lore raw
Quoted 21 lines · Dmitry Osipenko
Quoted 15 lines · Peter Maydell
Quoted 8 lines · Dmitry Osipenko
Quoted 6 lines · Peter Maydell

What I meant was: ptimer_get_count() is typically called to generate a value to return from a register. That's a separate thing, conceptually, from whether the device happens to also trigger an interrupt on timer expiry by passing a bh to ptimer_init(). So it's very odd for a detail of interrupt-on-timer-expiry (that there is a bottom half) to affect the value returned when you read the timer count register.

Quoted 5 lines

In order to handle wraparound correctly, software needs to track the moment of the wraparound - the interrupt. If software reads wrapped around counter value before IRQ triggered (ptimer expired), then it would assume that no wraparound happened and won't perform counter value correction, resulting in periodic counter "jumping" backwards.

Quoted 4 lines

That just says you need particular behaviour between counter reads and IRQ triggers; it doesn't say that you need the behaviour to be different if the ptimer code doesn't know about the IRQ trigger.

Quoted 6 lines

Okay, I already explained the reason for having two different behaviours - to make polled counter value more distributed when possible. If I understand you correctly, you don't like it because it is "odd" and I agree that it's a bit clumsy.

So, what we are going to do now? Would you just revert the offending commit or you have some other suggestions?

Well, we need to fix the regression, but basically I'm kind of confused at the moment. I haven't invested a lot of time in trying to understand the timer code, so all I can really do is say "this does not look like the right thing" and ask you to come up with a different fix for it.

thanks
-- PMM

#11 lore raw
Quoted 26 lines · Peter Maydell
Quoted 24 lines · Dmitry Osipenko
Quoted 15 lines · Peter Maydell
Quoted 8 lines · Dmitry Osipenko
Quoted 6 lines · Peter Maydell

What I meant was: ptimer_get_count() is typically called to generate a value to return from a register. That's a separate thing, conceptually, from whether the device happens to also trigger an interrupt on timer expiry by passing a bh to ptimer_init(). So it's very odd for a detail of interrupt-on-timer-expiry (that there is a bottom half) to affect the value returned when you read the timer count register.

Quoted 5 lines

In order to handle wraparound correctly, software needs to track the moment of the wraparound - the interrupt. If software reads wrapped around counter value before IRQ triggered (ptimer expired), then it would assume that no wraparound happened and won't perform counter value correction, resulting in periodic counter "jumping" backwards.

Quoted 4 lines

That just says you need particular behaviour between counter reads and IRQ triggers; it doesn't say that you need the behaviour to be different if the ptimer code doesn't know about the IRQ trigger.

Okay, I already explained the reason for having two different behaviours - to make polled counter value more distributed when possible. If I understand you correctly, you don't like it because it is "odd" and I agree that it's a bit clumsy.

Quoted 8 lines
Quoted 2 lines

So, what we are going to do now? Would you just revert the offending commit or you have some other suggestions?

Well, we need to fix the regression, but basically I'm kind of confused at the moment. I haven't invested a lot of time in trying to understand the timer code, so all I can really do is say "this does not look like the right thing" and ask you to come up with a different fix for it.

My current best guess is that this condition should simply be "if (expired) {".

thanks
-- PMM

#12 lore raw
Quoted 27 lines · Peter Maydell
Quoted 26 lines · Peter Maydell
Quoted 24 lines · Dmitry Osipenko
Quoted 15 lines · Peter Maydell
Quoted 8 lines · Dmitry Osipenko
Quoted 6 lines · Peter Maydell

What I meant was: ptimer_get_count() is typically called to generate a value to return from a register. That's a separate thing, conceptually, from whether the device happens to also trigger an interrupt on timer expiry by passing a bh to ptimer_init(). So it's very odd for a detail of interrupt-on-timer-expiry (that there is a bottom half) to affect the value returned when you read the timer count register.

Quoted 5 lines

In order to handle wraparound correctly, software needs to track the moment of the wraparound - the interrupt. If software reads wrapped around counter value before IRQ triggered (ptimer expired), then it would assume that no wraparound happened and won't perform counter value correction, resulting in periodic counter "jumping" backwards.

Quoted 4 lines

That just says you need particular behaviour between counter reads and IRQ triggers; it doesn't say that you need the behaviour to be different if the ptimer code doesn't know about the IRQ trigger.

Okay, I already explained the reason for having two different behaviours - to make polled counter value more distributed when possible. If I understand you correctly, you don't like it because it is "odd" and I agree that it's a bit clumsy.

Quoted 9 lines
Quoted 8 lines
Quoted 2 lines

So, what we are going to do now? Would you just revert the offending commit or you have some other suggestions?

Well, we need to fix the regression, but basically I'm kind of confused at the moment. I haven't invested a lot of time in trying to understand the timer code, so all I can really do is say "this does not look like the right thing" and ask you to come up with a different fix for it.

I'm currently leaning to the revert solution. Unfortunately, I haven't had a chance to look at it yet, will do it today and send patch after.

Quoted 3 lines

My current best guess is that this condition should simply be "if (expired) {".

Since you are insisted that wraparound isn't a needed feature - yes, that's nearly the same what the original code did :)

Signature · 1 line
Dmitry