[PATCH] target/riscv: Keep cycle overflow deadlines in virtual nanoseconds

Philippe Mathieu-Daudé posted 1 patch 3 days, 5 hours ago
target/riscv/tcg/pmu.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
[PATCH] target/riscv: Keep cycle overflow deadlines in virtual nanoseconds
Posted by Philippe Mathieu-Daudé 3 days, 5 hours ago
The cycle counter uses icount_get(), which already returns virtual
time in nanoseconds. The PMU overflow helper applies icount_to_ns()
to both cycle and instruction counters, so cycle overflow timers
are delayed by the icount shift.

Pass the counter context to the helper and scale only instruction
counts.

Fixes: b2d7a7c7e4e30 ("target/riscv: Implement privilege mode filtering for cycle/instret")
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
---
 target/riscv/tcg/pmu.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/target/riscv/tcg/pmu.c b/target/riscv/tcg/pmu.c
index 0165faf06b86..cd4de9cde0ab 100644
--- a/target/riscv/tcg/pmu.c
+++ b/target/riscv/tcg/pmu.c
@@ -281,12 +281,15 @@ static gboolean pmu_remove_event_map(gpointer key, gpointer value,
     return (GPOINTER_TO_UINT(value) == GPOINTER_TO_UINT(udata)) ? true : false;
 }
 
-static int64_t pmu_icount_ticks_to_ns(int64_t value)
+static int64_t pmu_icount_ticks_to_ns(CPURISCVState *env, uint32_t ctr_idx,
+                                      int64_t value)
 {
     int64_t ret = 0;
 
     if (icount_enabled()) {
-        ret = icount_to_ns(value);
+        /* The cycle counter counts ns of virtual time, see icount_get() */
+        ret = riscv_pmu_ctr_monitor_instructions(env, ctr_idx) ?
+              icount_to_ns(value) : value;
     } else {
         ret = (NANOSECONDS_PER_SECOND / RISCV_TIMEBASE_FREQ) * value;
     }
@@ -456,8 +459,9 @@ int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx)
 
     if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
         riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) {
-        overflow_ns = pmu_icount_ticks_to_ns((int64_t)overflow_delta);
-        overflow_left = pmu_icount_ticks_to_ns(overflow_left) ;
+        overflow_ns = pmu_icount_ticks_to_ns(env, ctr_idx,
+                                             (int64_t)overflow_delta);
+        overflow_left = pmu_icount_ticks_to_ns(env, ctr_idx, overflow_left);
     } else {
         return -1;
     }
-- 
2.55.0


Re: [PATCH] target/riscv: Keep cycle overflow deadlines in virtual nanoseconds
Posted by TianCheng TANG 3 days, 1 hour ago
在 2026/11/20 06:20, Philippe Mathieu-Daudé 写道:
> The cycle counter uses icount_get(), which already returns virtual
> time in nanoseconds. The PMU overflow helper applies icount_to_ns()
> to both cycle and instruction counters, so cycle overflow timers
> are delayed by the icount shift.
>
> Pass the counter context to the helper and scale only instruction
> counts.
Hi Philippe,

Patch 3 of my PMU series [1] includes this fix. Its aim is to keep
counter accounting and overflow scheduling consistent for each fixed
event.

It introduces riscv_pmu_read_fixed_source() so counter reads and
privilege accounting use the same source. In non-icount mode, this
switches both paths to VM-elapsed ticks, excluding time spent while
the VM is stopped.

Overflow scheduling must respect that source's units. Under icount,
cycles are already virtual nanoseconds, while instructions remain
raw counts. In pmu_ticks_to_ns(), icount_to_ns() therefore converts
only the number of instructions remaining before overflow to virtual
nanoseconds. The cycle value needs no further conversion.

This covers the issue addressed by your patch. Patch 3 also includes
a cycle-overflow regression test with icount shift=3.

Thanks,
Tiancheng

[1] 
https://lore.kernel.org/qemu-devel/20261119-riscv-pmu-correctness-v5-3-ae9bdc2d2f86@linux.alibaba.com/

> Fixes: b2d7a7c7e4e30 ("target/riscv: Implement privilege mode filtering for cycle/instret")
> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> ---
>   target/riscv/tcg/pmu.c | 12 ++++++++----
>   1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/target/riscv/tcg/pmu.c b/target/riscv/tcg/pmu.c
> index 0165faf06b86..cd4de9cde0ab 100644
> --- a/target/riscv/tcg/pmu.c
> +++ b/target/riscv/tcg/pmu.c
> @@ -281,12 +281,15 @@ static gboolean pmu_remove_event_map(gpointer key, gpointer value,
>       return (GPOINTER_TO_UINT(value) == GPOINTER_TO_UINT(udata)) ? true : false;
>   }
>   
> -static int64_t pmu_icount_ticks_to_ns(int64_t value)
> +static int64_t pmu_icount_ticks_to_ns(CPURISCVState *env, uint32_t ctr_idx,
> +                                      int64_t value)
>   {
>       int64_t ret = 0;
>   
>       if (icount_enabled()) {
> -        ret = icount_to_ns(value);
> +        /* The cycle counter counts ns of virtual time, see icount_get() */
> +        ret = riscv_pmu_ctr_monitor_instructions(env, ctr_idx) ?
> +              icount_to_ns(value) : value;
>       } else {
>           ret = (NANOSECONDS_PER_SECOND / RISCV_TIMEBASE_FREQ) * value;
>       }
> @@ -456,8 +459,9 @@ int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx)
>   
>       if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx) ||
>           riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) {
> -        overflow_ns = pmu_icount_ticks_to_ns((int64_t)overflow_delta);
> -        overflow_left = pmu_icount_ticks_to_ns(overflow_left) ;
> +        overflow_ns = pmu_icount_ticks_to_ns(env, ctr_idx,
> +                                             (int64_t)overflow_delta);
> +        overflow_left = pmu_icount_ticks_to_ns(env, ctr_idx, overflow_left);
>       } else {
>           return -1;
>       }