From nobody Mon Feb 9 23:16:00 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0698B18A93C for ; Sat, 18 Jan 2025 00:56:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737161774; cv=none; b=Dl3sT1uy/WuNAeji80/d3LCz1O4R8u5UQJeK6LIIRR1barUFGOUrnyFF1KgGO/GezDDiNCsEtpZVhrd3RW3nAcGXdeRNiq+21D8i1IgIn+3i58qUImpAoCMnB+51GALfvMc1id0Pqe4KfQiz4ZHaUY+Fnth0TL0TyVSd0Ta3sPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737161774; c=relaxed/simple; bh=oF0sBicAPPhUq72I5vlh3+SWBQ+gF99lBN9gOBit1as=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=m3hKUHyL6x0CD3WEltD0nAqipu/0Zk07zN5yewksa6lgLLVwGKR7Wui+KXYQqG8/lOaKMa0HyTqBNCQpKjJ4PWCkGKyg1/6MpeH+72A2pb7C58xqz6UYXOWdEU5Jd6wybgtZSzAKAMb9hMp6ssHIjHi6KKLMuXdQKxN5m991OeY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=XpzwarFr; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="XpzwarFr" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2f46b7851fcso7424856a91.1 for ; Fri, 17 Jan 2025 16:56:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737161772; x=1737766572; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=sJCtQ5uAUri7iGFBLSXX36fSE63Q0nPcAnEP4m7LVa8=; b=XpzwarFrh+X2MWjTOkiaudMxp05TEx+sjzew/6Wc5Y89tGkq8Raz0jIExKGMbiN0P1 PngG+XR/w6ynQ9vUwPJdHwM3Y30zyhATRjAbQOgHDfFJHZTzIAjGzZpJ/iPggwG10Qvf Ssik1ENGOtj0ehLJ4gunOVB2EEEGA4+MXcKpV3pl8ZxVodN8ipm5IBXkE9mwQnddOejI l/XIkO+W9kugR/xTO+XVMYUb90T8jkrC4YFo9UHnDY2XcEj93dajF8j2bpcY3sqxI7vw zCAMj3hd5Q55aw7vhwJuPwwZI94i59SpChj268V4C/6/yr7ktwT5AqJGa/vdrIHtiS4/ 9VxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737161772; x=1737766572; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=sJCtQ5uAUri7iGFBLSXX36fSE63Q0nPcAnEP4m7LVa8=; b=RrTfyu79RVLG0PeH7fhNclszvGmDu3zl5RTVjA4s09FVKZI5gDNxs91CBr3I6LbOZg zG4EKfATnrkAPE75A+Ku2ukFhGt0FbpDYq0kXHUGF4leiai4T350aHbCT95ee57X7JHa xGvhaTfhEORvZ4ZTru/d7Xy1I1JfkvutYImH4ABoftRjJhOzbq5MCPfJWGxKptrGaE80 jwTqC+SVZgiBqEsg1OaQAxGwTO0+xi8ngWz67PFlv4NIThIYATenLhwWzph45lkeq5WK I1a7IZJlNJt/IsTVnDQlTZ2yqbJ5AiXy5xyTelNzGjE5Boi3t9yU9QIqfxV4m9dxsxZj cMqw== X-Forwarded-Encrypted: i=1; AJvYcCUc4ndy3Bh7KRaU+HdiGQiAzwlhzFYfv7YBubVepar2QqTuP86Ekjn4E4HJzrAFJjSEvUNqb1J0FhU9Sls=@vger.kernel.org X-Gm-Message-State: AOJu0YzSAVwzR1fa8Gls+lnxDisYd+5tzFMtLrigaJExh+5IAh0CRAJK 1iQrHnU9D+SwebJyYT3CIscyh6kddRK5QUsLMSFP10Y9oGTGgy9kuZrhRTw8nxW2g5wsSirElBZ THA== X-Google-Smtp-Source: AGHT+IEdhNCZ24yUQLqPJww/GZu7srCGFAMCewMWioVRnJRojuDg8lwZ2z7ZAQPhZ3+MlNApu7cBlbpgUxU= X-Received: from pja3.prod.google.com ([2002:a17:90b:5483:b0:2ea:5be5:da6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:da8e:b0:2ee:d024:e4fc with SMTP id 98e67ed59e1d1-2f782d860d5mr7587139a91.33.1737161772463; Fri, 17 Jan 2025 16:56:12 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 17 Jan 2025 16:55:52 -0800 In-Reply-To: <20250118005552.2626804-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250118005552.2626804-1-seanjc@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250118005552.2626804-11-seanjc@google.com> Subject: [PATCH 10/10] KVM: x86: Override TSC_STABLE flag for Xen PV clocks in kvm_guest_time_update() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+352e553a86e0d75f5120@syzkaller.appspotmail.com, Paul Durrant , David Woodhouse , Vitaly Kuznetsov Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When updating PV clocks, handle the Xen-specific UNSTABLE_TSC override in the main kvm_guest_time_update() by simply clearing PVCLOCK_TSC_STABLE_BIT in the flags of the reference pvclock structure. Expand the comment to (hopefully) make it obvious that Xen clocks need to be processed after all clocks that care about the TSC_STABLE flag. No functional change intended. Cc: Paul Durrant Cc: David Woodhouse Signed-off-by: Sean Christopherson Reviewed-by: Paul Durrant --- arch/x86/kvm/x86.c | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c68e7f7ba69d..065b349a0218 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3119,8 +3119,7 @@ u64 get_kvmclock_ns(struct kvm *kvm) static void kvm_setup_guest_pvclock(struct pvclock_vcpu_time_info *ref_hv_= clock, struct kvm_vcpu *vcpu, struct gfn_to_pfn_cache *gpc, - unsigned int offset, - bool force_tsc_unstable) + unsigned int offset) { struct pvclock_vcpu_time_info *guest_hv_clock; struct pvclock_vcpu_time_info hv_clock; @@ -3155,9 +3154,6 @@ static void kvm_setup_guest_pvclock(struct pvclock_vc= pu_time_info *ref_hv_clock, =20 memcpy(guest_hv_clock, &hv_clock, sizeof(*guest_hv_clock)); =20 - if (force_tsc_unstable) - guest_hv_clock->flags &=3D ~PVCLOCK_TSC_STABLE_BIT; - smp_wmb(); =20 guest_hv_clock->version =3D ++hv_clock.version; @@ -3178,16 +3174,6 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) s64 kernel_ns; u64 tsc_timestamp, host_tsc; bool use_master_clock; -#ifdef CONFIG_KVM_XEN - /* - * For Xen guests we may need to override PVCLOCK_TSC_STABLE_BIT as unless - * explicitly told to use TSC as its clocksource Xen will not set this bi= t. - * This default behaviour led to bugs in some guest kernels which cause - * problems if they observe PVCLOCK_TSC_STABLE_BIT in the pvclock flags. - */ - bool xen_pvclock_tsc_unstable =3D - ka->xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE; -#endif =20 kernel_ns =3D 0; host_tsc =3D 0; @@ -3275,7 +3261,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) hv_clock.flags |=3D PVCLOCK_GUEST_STOPPED; vcpu->pvclock_set_guest_stopped_request =3D false; } - kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->pv_time, 0, false); + kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->pv_time, 0); =20 hv_clock.flags &=3D ~PVCLOCK_GUEST_STOPPED; } @@ -3283,13 +3269,22 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) kvm_hv_setup_tsc_page(v->kvm, &hv_clock); =20 #ifdef CONFIG_KVM_XEN + /* + * For Xen guests we may need to override PVCLOCK_TSC_STABLE_BIT as unless + * explicitly told to use TSC as its clocksource Xen will not set this bi= t. + * This default behaviour led to bugs in some guest kernels which cause + * problems if they observe PVCLOCK_TSC_STABLE_BIT in the pvclock flags. + * + * Note! Clear TSC_STABLE only for Xen clocks, i.e. the order matters! + */ + if (ka->xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE) + hv_clock.flags &=3D ~PVCLOCK_TSC_STABLE_BIT; + if (vcpu->xen.vcpu_info_cache.active) kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->xen.vcpu_info_cache, - offsetof(struct compat_vcpu_info, time), - xen_pvclock_tsc_unstable); + offsetof(struct compat_vcpu_info, time)); if (vcpu->xen.vcpu_time_info_cache.active) - kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->xen.vcpu_time_info_cache, 0, - xen_pvclock_tsc_unstable); + kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->xen.vcpu_time_info_cache, 0= ); #endif return 0; } --=20 2.48.0.rc2.279.g1de40edade-goog