[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 2/2] xen/cpu: round up cpu_khz calculations



On 14/04/2026 12:36, Roger Pau Monne wrote:
> All arches truncate the cpu_khz without taking into account the less
> significant digits.  Instead use DIV_ROUND_UP() when scaling from Hz to kHz
> to get as more accurate kHz value.
>
> Signed-off-by: Roger Pau Monné <roger.pau@xxxxxxxxxx>

Couldn't DIV_ROUND be used here instead for a round-to-closest?

> ---
> While the possibly more accurate value is nice, I'm not sure it's actually
> fixing any functional bug, and hence the lack of "Fixes:" tag.
> ---
>   xen/arch/arm/time.c   | 4 ++--
>   xen/arch/riscv/time.c | 2 +-
>   xen/arch/x86/time.c   | 2 +-
>   3 files changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/xen/arch/arm/time.c b/xen/arch/arm/time.c
> index a12912a106a0..9e0c485c77db 100644
> --- a/xen/arch/arm/time.c
> +++ b/xen/arch/arm/time.c
> @@ -118,7 +118,7 @@ static void __init preinit_dt_xen_time(void)
>       res = dt_property_read_u32(timer, "clock-frequency", &rate);
>       if ( res )
>       {
> -        cpu_khz = rate / 1000;
> +        cpu_khz = DIV_ROUND_UP(rate, 1000);
>           validate_timer_frequency();
>           timer_dt_clock_frequency = rate;
>       }
> @@ -136,7 +136,7 @@ void __init preinit_xen_time(void)
>
>       if ( !cpu_khz )
>       {
> -        cpu_khz = (READ_SYSREG(CNTFRQ_EL0) & CNTFRQ_MASK) / 1000;
> +        cpu_khz = DIV_ROUND_UP(READ_SYSREG(CNTFRQ_EL0) & CNTFRQ_MASK, 1000);
>           validate_timer_frequency();
>       }
>
> diff --git a/xen/arch/riscv/time.c b/xen/arch/riscv/time.c
> index 7efa76fdbcb1..faca7b70e13a 100644
> --- a/xen/arch/riscv/time.c
> +++ b/xen/arch/riscv/time.c
> @@ -40,7 +40,7 @@ static void __init preinit_dt_xen_time(void)
>       if ( !dt_property_read_u32(timer, "timebase-frequency", &rate) )
>           panic("Unable to find clock frequency\n");
>
> -    cpu_khz = rate / 1000;
> +    cpu_khz = DIV_ROUND_UP(rate, 1000);
>   }
>
>   int reprogram_timer(s_time_t timeout)
> diff --git a/xen/arch/x86/time.c b/xen/arch/x86/time.c
> index 244277c0a921..b84414f00d05 100644
> --- a/xen/arch/x86/time.c
> +++ b/xen/arch/x86/time.c
> @@ -2642,7 +2642,7 @@ void __init early_time_init(void)
>       set_time_scale(&t->tsc_scale, tmp);
>       t->stamp.local_tsc = boot_tsc_stamp;
>
> -    cpu_khz = tmp / 1000;
> +    cpu_khz = DIV_ROUND_UP(tmp, 1000);
>       printk("Detected %lu.%03lu MHz processor.\n",
>              cpu_khz / 1000, cpu_khz % 1000);
>



--
Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech





 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.