Skip to content

Commit 29cc0f3

Browse files
Marc Zyngierwilldeacon
authored andcommitted
arm64: Force the use of CNTVCT_EL0 in __delay()
Quentin forwards a report from Hyesoo Yu, describing an interesting problem with the use of WFxT in __delay() when a vcpu is loaded and that KVM is *not* in VHE mode (either nVHE or hVHE). In this case, CNTVOFF_EL2 is set to a non-zero value to reflect the state of the guest virtual counter. At the same time, __delay() is using get_cycles() to read the counter value, which is indirected to reading CNTPCT_EL0. The core of the issue is that WFxT is using the *virtual* counter, while the kernel is using the physical counter, and that the offset introduces a really bad discrepancy between the two. Fix this by forcing the use of CNTVCT_EL0, making __delay() consistent irrespective of the value of CNTVOFF_EL2. Reported-by: Hyesoo Yu <hyesoo.yu@samsung.com> Reported-by: Quentin Perret <qperret@google.com> Reviewed-by: Quentin Perret <qperret@google.com> Fixes: 7d26b05 ("arm64: Use WFxT for __delay() when possible") Signed-off-by: Marc Zyngier <maz@kernel.org> Link: https://lore.kernel.org/r/ktosachvft2cgqd5qkukn275ugmhy6xrhxur4zqpdxlfr3qh5h@o3zrfnsq63od Cc: stable@vger.kernel.org Signed-off-by: Will Deacon <will@kernel.org>
1 parent 36c0de0 commit 29cc0f3

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

arch/arm64/lib/delay.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,20 @@ static inline unsigned long xloops_to_cycles(unsigned long xloops)
2323
return (xloops * loops_per_jiffy * HZ) >> 32;
2424
}
2525

26+
/*
27+
* Force the use of CNTVCT_EL0 in order to have the same base as WFxT.
28+
* This avoids some annoying issues when CNTVOFF_EL2 is not reset 0 on a
29+
* KVM host running at EL1 until we do a vcpu_put() on the vcpu. When
30+
* running at EL2, the effective offset is always 0.
31+
*
32+
* Note that userspace cannot change the offset behind our back either,
33+
* as the vcpu mutex is held as long as KVM_RUN is in progress.
34+
*/
35+
#define __delay_cycles() __arch_counter_get_cntvct_stable()
36+
2637
void __delay(unsigned long cycles)
2738
{
28-
cycles_t start = get_cycles();
39+
cycles_t start = __delay_cycles();
2940

3041
if (alternative_has_cap_unlikely(ARM64_HAS_WFXT)) {
3142
u64 end = start + cycles;
@@ -35,17 +46,17 @@ void __delay(unsigned long cycles)
3546
* early, use a WFET loop to complete the delay.
3647
*/
3748
wfit(end);
38-
while ((get_cycles() - start) < cycles)
49+
while ((__delay_cycles() - start) < cycles)
3950
wfet(end);
4051
} else if (arch_timer_evtstrm_available()) {
4152
const cycles_t timer_evt_period =
4253
USECS_TO_CYCLES(ARCH_TIMER_EVT_STREAM_PERIOD_US);
4354

44-
while ((get_cycles() - start + timer_evt_period) < cycles)
55+
while ((__delay_cycles() - start + timer_evt_period) < cycles)
4556
wfe();
4657
}
4758

48-
while ((get_cycles() - start) < cycles)
59+
while ((__delay_cycles() - start) < cycles)
4960
cpu_relax();
5061
}
5162
EXPORT_SYMBOL(__delay);

0 commit comments

Comments
 (0)