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

[xen master] x86/time: deal with negative deltas in get_s_time_fixed()



commit 7b3e1b4e848d34c9a5b6634009959a7b9dd42104
Author:     Jan Beulich <jbeulich@xxxxxxxx>
AuthorDate: Tue Jan 20 09:03:17 2026 +0100
Commit:     Jan Beulich <jbeulich@xxxxxxxx>
CommitDate: Tue Jan 20 09:03:17 2026 +0100

    x86/time: deal with negative deltas in get_s_time_fixed()
    
    Callers may pass in TSC values from before the local TSC stamp was last
    updated (this would in particular be the case when the TSC was latched, a
    time rendezvous occurs, and the latched value is used only afterwards).
    scale_delta(), otoh, deals with unsigned values, and hence would treat
    negative incoming deltas as huge positive values, yielding entirely bogus
    return values.
    
    Fixes: 88e64cb785c1 ("x86/HVM: use fixed TSC value when saving or restoring 
domain")
    Reported-by: Ð?нÑ?он Ð?аÑ?ков <akmarkov45@xxxxxxxxx>
    Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
    Reviewed-by: Roger Pau Monné <roger.pau@xxxxxxxxxx>
---
 xen/arch/x86/time.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/xen/arch/x86/time.c b/xen/arch/x86/time.c
index 362c76df0b..87b4e83250 100644
--- a/xen/arch/x86/time.c
+++ b/xen/arch/x86/time.c
@@ -1651,8 +1651,13 @@ s_time_t get_s_time_fixed(u64 at_tsc)
         tsc = at_tsc;
     else
         tsc = rdtsc_ordered();
-    delta = tsc - t->stamp.local_tsc;
-    return t->stamp.local_stime + scale_delta(delta, &t->tsc_scale);
+
+    if ( likely(tsc >= t->stamp.local_tsc) )
+        delta = scale_delta(tsc - t->stamp.local_tsc, &t->tsc_scale);
+    else
+        delta = -scale_delta(t->stamp.local_tsc - tsc, &t->tsc_scale);
+
+    return t->stamp.local_stime + delta;
 }
 
 s_time_t get_s_time(void)
--
generated by git-patchbot for /home/xen/git/xen.git#master



 


Rackspace

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