x86/time: avoid early uses of NOW() to return zero

Waiting loops like the one in flush_command_buffer() will degenerate to
infinite ones when used early enough for NOW() to still return constant
zero. Make sure the returned value at least monotonically increases. When
available, use nominal frequency values as initial approximation.

Do this only in get_s_time(), as producing a sane value in
get_s_time_fixed() for non-zero inputs won't be reasonably possible.
Put an assertion there.

Reported-by: Roger Pau Monné <roger.pau@citrix.com>
Signed-off-by: Jan Beulich <jbeulich@suse.com>
Acked-by: Roger Pau Monné <roger@xenproject.org>
This commit is contained in:
Jan Beulich committed 2026-09-02 15:15:46 +02:00
1 parent 11f8d55624
commit e85f3521a4
5 files changed
+76 -2

No files matched your search

+34
View File
@@ -19,6 +19,7 @@
#include <asm/random.h>
#include <asm/setup.h>
#include <asm/shstk.h>
#include <asm/time.h>
#include <asm/xstate.h>
#include <public/sysctl.h>
@@ -444,6 +445,39 @@ void __init early_cpu_init(bool verbose)
if (!(c->vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON)))
park_offline_cpus = opt_mce;
/*
* If nominal freq isn't available, use highest, thus causing NOW()
* output to move more slowly. See preset_tsc_scale().
*/
if (c->cpuid_level >= 0x15) {
cpuid(0x15, &eax, &ebx, &ecx, &edx);
if (ecx && ebx && eax)
preset_tsc_scale(DIV_ROUND_UP(ecx * 1UL * ebx, eax));
else if (c->cpuid_level >= 0x16) {
/* Assume CPU base freq ≈ TSC freq. */
cpuid(0x16, &eax, &ebx, &ecx, &edx);
if (eax)
preset_tsc_scale(eax * 1000000UL);
else if (ebx)
preset_tsc_scale(ebx * 1000000UL);
}
} else if (c->vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON)) {
unsigned int nom_mhz = 0, hi_mhz = 0;
amd_process_freq(c, NULL, &nom_mhz, &hi_mhz);
if (nom_mhz)
preset_tsc_scale(nom_mhz * 1000000UL);
else if (hi_mhz)
preset_tsc_scale(hi_mhz * 1000000UL);
} else if (c->vendor & X86_VENDOR_INTEL) {
unsigned int hi_mhz = 0;
intel_process_freq(c, NULL, &hi_mhz);
if (hi_mhz)
preset_tsc_scale(hi_mhz * 1000000UL);
}
}
void reset_cpuinfo(struct cpuinfo_x86 *c, bool keep_basic)
+2 -2
View File
@@ -476,8 +476,8 @@ static int num_cpu_cores(struct cpuinfo_x86 *c)
return 1;
}
static void intel_process_freq(const struct cpuinfo_x86 *c,
unsigned int *min_mhz, unsigned int *max_mhz)
void intel_process_freq(const struct cpuinfo_x86 *c,
unsigned int *min_mhz, unsigned int *max_mhz)
{
uint64_t msrval;
uint8_t max_ratio, min_ratio;
+3
View File
@@ -417,6 +417,9 @@ static inline uint8_t get_cpu_family(uint32_t raw, uint8_t *model,
return fam;
}
void intel_process_freq(const struct cpuinfo_x86 *c,
unsigned int *min_mhz, unsigned int *max_mhz);
#ifdef CONFIG_INTEL
extern int8_t opt_tsx;
extern bool rtm_disabled;
+1
View File
@@ -23,6 +23,7 @@ mktime (unsigned int year, unsigned int mon,
int time_suspend(void);
int time_resume(void);
void preset_tsc_scale(unsigned long freq);
void init_percpu_time(void);
void time_latch_stamps(void);
+36
View File
@@ -1713,6 +1713,9 @@ s_time_t get_s_time_fixed(uint64_t at_tsc)
const struct cpu_time *t = &this_cpu(cpu_time);
uint64_t tsc, delta;
/* scale_delta() degenerates when the scale wasn't set yet. */
ASSERT(t->tsc_scale.mul_frac);
if ( at_tsc )
tsc = at_tsc;
else
@@ -1728,6 +1731,20 @@ s_time_t get_s_time_fixed(uint64_t at_tsc)
s_time_t get_s_time(void)
{
/*
* Before the TSC scale is set, avoid returning constant 0 (or whatever
* this_cpu(cpu_time).stamp.local_stime is set to). While the returned
* value is in no way representing time, it at least increases
* monotonically, thus avoiding e.g. waiting loops to degenerate to
* entirely infinite ones.
*/
if ( unlikely(!this_cpu(cpu_time).tsc_scale.mul_frac) )
{
static s_time_t counter;
return arch_fetch_and_add(&counter, 1);
}
return get_s_time_fixed(0);
}
@@ -2681,6 +2698,22 @@ int __init init_xen_time(void)
return 0;
}
/* BSP-only function to pre-set an approximate TSC scale. */
void __init preset_tsc_scale(unsigned long freq)
{
struct cpu_time *t = &this_cpu(cpu_time);
/*
* The incoming frequency is only approximate (nominal). Increase it by
* 1% to make NOW() output rather a little too slow than too fast, thus
* avoiding a possible backwards jump once the final scale is set.
*/
freq += DIV_ROUND_UP(freq, 100);
set_time_scale(&t->tsc_scale, freq);
t->stamp.local_tsc = boot_tsc_stamp;
NOW_good = true;
}
/* Early init function. */
void __init early_time_init(void)
@@ -2698,6 +2731,9 @@ void __init early_time_init(void)
"TSC ADJUST set to %lx on boot CPU - clearing\n", tmp);
wrmsrl(MSR_IA32_TSC_ADJUST, 0);
boot_tsc_stamp -= tmp;
if ( t->stamp.local_tsc )
t->stamp.local_tsc -= tmp;
}
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.