From nobody Mon Feb 9 02:11:44 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 A4D2D17C9F1 for ; Sat, 18 Jan 2025 00:56:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737161771; cv=none; b=kqpY20g4iAka6mRnZz4TcPMIKnGmLV/qU9cAFrnyk2UJCmmUGW2wsd7vjizPv5k+hkvKkCHjK/qtPzv5/zdHPpzu9vvJWoLN6XK+wj9foYcAwrJ0P/16osfyhZl4ThS2y4cnc+Vjhv74Zay8qzpZ+tcmxzNnuLFd+2Ra6BWWfy4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737161771; c=relaxed/simple; bh=MlMCaN4C4GVV0w3D6Tva9o5mp4aTS3X/nOMX8xQEZzw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JwvytSI8bJ1Tj52edeESEHaAEKYq3S1nDaTO3p3ESD7zs/mnqo+2IOSrDFirBMBmdMt7oCee6lGO07oh9RJUo3ojVlPxG1YYvxQru1Snfbw2e4d2qVENJ3vEc6SGkHLsAlkS7QQq7zHe/7A0q1LRJDyFCLYWNU3XQMOVHIsJV6A= 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=UBVy+9Ps; arc=none smtp.client-ip=209.85.214.201 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="UBVy+9Ps" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2166a1a5cc4so48775255ad.3 for ; Fri, 17 Jan 2025 16:56:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737161769; x=1737766569; 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=Xe76inQQED+VDi+GOM1bZpRo/JAeJ3yY7og1Pn73/dU=; b=UBVy+9Ps2M5R+xEiXeKdfcOMLAarv0KF69bYAKJxCx36PzgudtsDma+uXfrXtRCDDk YQBvouZcn7mqbTxdznfVmHK/gtGTdfhhlvxhhLiAcEtEsI9e1U9m6H7HHGpLdr1HEyKv pCeCcZqIaJObAchCvxlF5qrePz8M2LnAZULXJFsZPcnU/W7yiU4GIPUMlM0y8yu3Hzfl yXcu4JHwFKruTer4f3lkrHKBH8bmvRiIlqZji6ZtHfw19VLxiIQIX0ePqj9zlsfRqq/P e922EgMI0/7C/lqB4l2aO8ABD4iYu0J381zXcnvIakMpvShM9K951DIZfB6qsO3hvU+v bq0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737161769; x=1737766569; 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=Xe76inQQED+VDi+GOM1bZpRo/JAeJ3yY7og1Pn73/dU=; b=EEyeXCsBspWxXWaNzvXV1nQmjeHIQXq7zDNwliy1EibdyT8cgKIG5BSfJOeFIcUZRg 99K9giVm+N7mPGFJSIRrCWbe9Qrg0GBstj1SZq0bO2dd7Q9TEdCmbhGxo4XnVrAWS7ax QEorWxtHGs8Hw0iYwRdxatZDyiUTJq75LJgrLuqZHV956hl7ZJZOjjKQkxH6WGmcv9gv D4+NmSQoxJdiLeTjtiPi0xLtsK70tIQ2L/zOcWoL15JTY6z9Mj46lO+J4GQbTSgqANIc 7pGaTu5OoB0i7qd3hLLTswRHkTSTQ1pXd9FDAp+qhwGlcnt/wCIgH2sKM85Y6OIQGIq2 6xKQ== X-Forwarded-Encrypted: i=1; AJvYcCX6PXRGqA9G1c0RdztBf53TdAHk/UZQ8Z73B/3bCKZMrygAVu3uKjJ1Z00nOhGeueO0k8JbwehVd8xCb7A=@vger.kernel.org X-Gm-Message-State: AOJu0YwRmfDK926nZ3pRHx8KZFoblGj8RPTjwdoGV4LRMse5hZqaxLQC SoLGIo6PATq8bNTEnsODvxKeR/mQPfCo9wvqMk/GCWxX353QJ6zecmXzwKIbS2/TVaBOlvY+xLA xpQ== X-Google-Smtp-Source: AGHT+IEwP7oMktyaCLjiLHcanbI8N9janBfFwvH46GF7ZKFwrHrokmvqDB/j3xfVz2pJ9k+Y3S5lFTl0l78= X-Received: from pjbsf7.prod.google.com ([2002:a17:90b:51c7:b0:2ef:71b9:f22f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:da88:b0:216:69ca:773b with SMTP id d9443c01a7336-21c352c7b99mr77384315ad.5.1737161768921; Fri, 17 Jan 2025 16:56:08 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 17 Jan 2025 16:55:50 -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-9-seanjc@google.com> Subject: [PATCH 08/10] KVM: x86: Remove per-vCPU "cache" of its reference pvclock 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" Remove the per-vCPU "cache" of the reference pvclock and instead cache only the TSC shift+multiplier. All other fields in pvclock are fully recomputed by kvm_guest_time_update(), i.e. aren't actually persisted. In addition to shaving a few bytes, explicitly tracking the TSC shift/mul fields makes it easier to see that those fields are tied to hw_tsc_khz (they exist to avoid having to do expensive math in the common case). And conversely, not tracking the other fields makes it easier to see that things like the version number are pulled from the guest's copy, not from KVM's reference. Signed-off-by: Sean Christopherson Reviewed-by: Paul Durrant --- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/x86.c | 27 +++++++++++++++------------ arch/x86/kvm/xen.c | 8 ++++---- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 5193c3dfbce1..f26105654ec4 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -900,7 +900,8 @@ struct kvm_vcpu_arch { int (*complete_userspace_io)(struct kvm_vcpu *vcpu); =20 gpa_t time; - struct pvclock_vcpu_time_info hv_clock; + u8 pvclock_tsc_shift; + u32 pvclock_tsc_mul; unsigned int hw_tsc_khz; struct gfn_to_pfn_cache pv_time; /* set guest stopped flag in pvclock flags field */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 06d27b3cc207..9eabd70891dd 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3170,6 +3170,7 @@ static void kvm_setup_guest_pvclock(struct pvclock_vc= pu_time_info *ref_hv_clock, =20 static int kvm_guest_time_update(struct kvm_vcpu *v) { + struct pvclock_vcpu_time_info hv_clock =3D {}; unsigned long flags, tgt_tsc_khz; unsigned seq; struct kvm_vcpu_arch *vcpu =3D &v->arch; @@ -3247,20 +3248,22 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) =20 if (unlikely(vcpu->hw_tsc_khz !=3D tgt_tsc_khz)) { kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL, - &vcpu->hv_clock.tsc_shift, - &vcpu->hv_clock.tsc_to_system_mul); + &vcpu->pvclock_tsc_shift, + &vcpu->pvclock_tsc_mul); vcpu->hw_tsc_khz =3D tgt_tsc_khz; kvm_xen_update_tsc_info(v); } =20 - vcpu->hv_clock.tsc_timestamp =3D tsc_timestamp; - vcpu->hv_clock.system_time =3D kernel_ns + v->kvm->arch.kvmclock_offset; + hv_clock.tsc_shift =3D vcpu->pvclock_tsc_shift; + hv_clock.tsc_to_system_mul =3D vcpu->pvclock_tsc_mul; + hv_clock.tsc_timestamp =3D tsc_timestamp; + hv_clock.system_time =3D kernel_ns + v->kvm->arch.kvmclock_offset; vcpu->last_guest_tsc =3D tsc_timestamp; =20 /* If the host uses TSC clocksource, then it is stable */ - vcpu->hv_clock.flags =3D 0; + hv_clock.flags =3D 0; if (use_master_clock) - vcpu->hv_clock.flags |=3D PVCLOCK_TSC_STABLE_BIT; + hv_clock.flags |=3D PVCLOCK_TSC_STABLE_BIT; =20 if (vcpu->pv_time.active) { /* @@ -3269,24 +3272,24 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) * is active/enabled. */ if (vcpu->pvclock_set_guest_stopped_request) { - vcpu->hv_clock.flags |=3D PVCLOCK_GUEST_STOPPED; + hv_clock.flags |=3D PVCLOCK_GUEST_STOPPED; vcpu->pvclock_set_guest_stopped_request =3D false; } - kvm_setup_guest_pvclock(&vcpu->hv_clock, v, &vcpu->pv_time, 0, false); + kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->pv_time, 0, false); =20 - vcpu->hv_clock.flags &=3D ~PVCLOCK_GUEST_STOPPED; + hv_clock.flags &=3D ~PVCLOCK_GUEST_STOPPED; } =20 #ifdef CONFIG_KVM_XEN if (vcpu->xen.vcpu_info_cache.active) - kvm_setup_guest_pvclock(&vcpu->hv_clock, v, &vcpu->xen.vcpu_info_cache, + kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->xen.vcpu_info_cache, offsetof(struct compat_vcpu_info, time), xen_pvclock_tsc_unstable); if (vcpu->xen.vcpu_time_info_cache.active) - kvm_setup_guest_pvclock(&vcpu->hv_clock, v, &vcpu->xen.vcpu_time_info_ca= che, 0, + kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->xen.vcpu_time_info_cache, 0, xen_pvclock_tsc_unstable); #endif - kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); + kvm_hv_setup_tsc_page(v->kvm, &hv_clock); return 0; } =20 diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index b82c28223585..7c6e4172527a 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -177,8 +177,8 @@ static int xen_get_guest_pvclock(struct kvm_vcpu *vcpu, * Sanity check TSC shift+multiplier to verify the guest's view of time * is more or less consistent. */ - if (hv_clock->tsc_shift !=3D vcpu->arch.hv_clock.tsc_shift || - hv_clock->tsc_to_system_mul !=3D vcpu->arch.hv_clock.tsc_to_system_mu= l) + if (hv_clock->tsc_shift !=3D vcpu->arch.pvclock_tsc_shift || + hv_clock->tsc_to_system_mul !=3D vcpu->arch.pvclock_tsc_mul) return -EINVAL; return 0; } @@ -2309,8 +2309,8 @@ void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu) =20 entry =3D kvm_find_cpuid_entry_index(vcpu, function, 1); if (entry) { - entry->ecx =3D vcpu->arch.hv_clock.tsc_to_system_mul; - entry->edx =3D vcpu->arch.hv_clock.tsc_shift; + entry->ecx =3D vcpu->arch.pvclock_tsc_mul; + entry->edx =3D vcpu->arch.pvclock_tsc_shift; } =20 entry =3D kvm_find_cpuid_entry_index(vcpu, function, 2); --=20 2.48.0.rc2.279.g1de40edade-goog