lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <aHXnO6KapsNLjocd@google.com>
Date: Tue, 15 Jul 2025 05:29:31 +0000
From: Tzung-Bi Shih <tzungbi@...nel.org>
To: Suleiman Souhlal <suleiman@...gle.com>
Cc: Paolo Bonzini <pbonzini@...hat.com>,
	Sean Christopherson <seanjc@...gle.com>,
	Thomas Gleixner <tglx@...utronix.de>,
	Ingo Molnar <mingo@...hat.com>, Borislav Petkov <bp@...en8.de>,
	Dave Hansen <dave.hansen@...ux.intel.com>, x86@...nel.org,
	"H. Peter Anvin" <hpa@...or.com>, Chao Gao <chao.gao@...el.com>,
	David Woodhouse <dwmw2@...radead.org>,
	Sergey Senozhatsky <senozhatsky@...omium.org>,
	Konrad Rzeszutek Wilk <konrad.wilk@...cle.com>,
	John Stultz <jstultz@...gle.com>, kvm@...r.kernel.org,
	linux-kernel@...r.kernel.org, ssouhlal@...ebsd.org
Subject: Re: [PATCH v7 1/3] KVM: x86: Advance guest TSC after deep suspend.

On Mon, Jul 14, 2025 at 12:36:47PM +0900, Suleiman Souhlal wrote:
> Try to advance guest TSC to current time after suspend when the host
> TSCs went backwards.
> 
> This makes the behavior consistent between suspends where host TSC
> resets and suspends where it doesn't, such as suspend-to-idle, where
> in the former case if the host TSC resets, the guests' would
> previously be "frozen" due to KVM's backwards TSC prevention, while
> in the latter case they would advance.
> 
> Suggested-by: Sean Christopherson <seanjc@...gle.com>
> Signed-off-by: Suleiman Souhlal <suleiman@...gle.com>

Tested again with comparing `date` before and after suspend-to-RAM:
  echo deep >/sys/power/mem_sleep
  echo $(date '+%s' -d '+3 minutes') >/sys/class/rtc/rtc0/wakealarm
  echo mem >/sys/power/state

Without the patch, the guest's `date` is slower (~3 mins) than the host's
after resuming.

Tested-by: Tzung-Bi Shih <tzungbi@...nel.org>

> @@ -5035,7 +5035,36 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
>  
>  	/* Apply any externally detected TSC adjustments (due to suspend) */
>  	if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
> +#ifdef CONFIG_X86_64
> +		unsigned long flags;
> +		struct kvm *kvm;
> +		bool advance;
> +		u64 kernel_ns, l1_tsc, offset, tsc_now;
> +
> +		kvm = vcpu->kvm;
> +		advance = kvm_get_time_and_clockread(&kernel_ns, &tsc_now);
> +		raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
> +		/*
> +		 * Advance the guest's TSC to current time instead of only
> +		 * preventing it from going backwards, while making sure
> +		 * all the vCPUs use the same offset.
> +		 */
> +		if (kvm->arch.host_was_suspended && advance) {
> +			l1_tsc = nsec_to_cycles(vcpu,
> +						kvm->arch.kvmclock_offset + kernel_ns);
> +			offset = kvm_compute_l1_tsc_offset(vcpu, l1_tsc);
> +			kvm->arch.cur_tsc_offset = offset;
> +			kvm_vcpu_write_tsc_offset(vcpu, offset);
> +		} else if (advance) {
> +			kvm_vcpu_write_tsc_offset(vcpu, kvm->arch.cur_tsc_offset);
> +		} else {
> +			adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
> +		}
> +		kvm->arch.host_was_suspended = false;
> +		raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
> +#else
>  		adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
> +#endif /* CONFIG_X86_64 */

Wondering if it needs to acquire the `tsc_write_lock`, given that:
- The original code adjust_tsc_offset_host() doesn't acquire.  Note:
  adjust_tsc_offset_host() eventually calls kvm_vcpu_write_tsc_offset() too.
- Documentation/virt/kvm/locking.rst [1].

[1] https://elixir.bootlin.com/linux/v6.15/source/Documentation/virt/kvm/locking.rst#L264

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ