Skip to contentSkip to conversation

x86/hpet: Fix return value of hpet_setup()

PATCH In review Andrew Cooper 2 participants 2 messages Dec 30, 2020 – Dec 31, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hpet: Fix return value of hpet_setup()

hpet_setup() is idempotent if the rate has already been calculated, and returns the cached value. However, this only works correctly when the return statements are identical.

Use a sensibly named local variable, rather than a dead one with a bad name.

Fixes a60bb68219 ("x86/time: reduce rounding errors in calculations")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/hpet.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
xen/arch/x86/hpet.c +5 -3
index a55e68e6f7..e6fab8acd8 100644
@@ -759,7 +759,7 @@ u64 __init hpet_setup(void)
 {
     static u64 __initdata hpet_rate;
     u32 hpet_id, hpet_period;
-    unsigned int last;
+    unsigned int last, rem;
 
     if ( hpet_rate )
         return hpet_rate;
@@ -789,9 +789,11 @@ u64 __init hpet_setup(void)
     hpet_resume(hpet_boot_cfg);
 
     hpet_rate = 1000000000000000ULL; /* 10^15 */
-    last = do_div(hpet_rate, hpet_period);
+    rem = do_div(hpet_rate, hpet_period);
+    if ( (rem * 2) > hpet_period )
+        hpet_rate++;
 
-    return hpet_rate + (last * 2 > hpet_period);
+    return hpet_rate;
 }
 
 void hpet_resume(u32 *boot_cfg)
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Andrew Cooper

hpet_setup() is idempotent if the rate has already been calculated, and returns the cached value. However, this only works correctly when the return statements are identical.

Use a sensibly named local variable, rather than a dead one with a bad name.

Fixes a60bb68219 ("x86/time: reduce rounding errors in calculations")

Thanks, Roger.