• Paul Mackerras's avatar
    KVM: PPC: Book3S HV: Snapshot timebase offset on guest entry · 57b8daa7
    Paul Mackerras authored
    Currently, the HV KVM guest entry/exit code adds the timebase offset
    from the vcore struct to the timebase on guest entry, and subtracts
    it on guest exit.  Which is fine, except that it is possible for
    userspace to change the offset using the SET_ONE_REG interface while
    the vcore is running, as there is only one timebase offset per vcore
    but potentially multiple VCPUs in the vcore.  If that were to happen,
    KVM would subtract a different offset on guest exit from that which
    it had added on guest entry, leading to the timebase being out of sync
    between cores in the host, which then leads to bad things happening
    such as hangs and spurious watchdog timeouts.
    
    To fix this, we add a new field 'tb_offset_applied' to the vcore struct
    which stores the offset that is currently applied to the timebase.
    This value is set from the vcore tb_offset field on guest entry, and
    is what is subtracted from the timebase on guest exit.  Since it is
    zero when the timebase offset is not applied, we can simplify the
    logic in kvmhv_start_timing and kvmhv_accumulate_time.
    
    In addition, we had secondary threads reading the timebase while
    running concurrently with code on the primary thread which would
    eventually add or subtract the timebase offset from the timebase.
    This occurred while saving or restoring the DEC register value on
    the secondary threads.  Although no specific incorrect behaviour has
    been observed, this is a race which should be fixed.  To fix it, we
    move the DEC saving code to just before we call kvmhv_commence_exit,
    and the DEC restoring code to after the point where we have waited
    for the primary thread to switch the MMU context and add the timebase
    offset.  That way we are sure that the timebase contains the guest
    timebase value in both cases.
    Signed-off-by: default avatarPaul Mackerras <paulus@ozlabs.org>
    57b8daa7
asm-offsets.c 29.8 KB