From nobody Mon Feb 9 09:33:09 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 C385822F16C for ; Thu, 27 Feb 2025 02:19:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740622790; cv=none; b=soCGWU/wkXS/BDpn8lIUr0WeyYyQiE8XQ/1uKDZACAyrS/ZAkv5DDrDJjxXYP/KoR0pLQLJVjMTwwLn6G0nEba5o6OgvlbskGCVnFVzJETchPhZIfuR/J81Yu5NSEjSC85ySQpkqEXrNII3Xe1lIU70zomdlnjjkMCC3PbWHlP8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740622790; c=relaxed/simple; bh=R5OwNlXhbkGyFpfs6H/9FfbWD/QK2xYWDQUAUET1omM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qxXbA1E2sVz/0ht9C0CU2QK7bYjjaecC6jYBz16ijN+IWLH+Ja3atIqNuG/IKxYnINsrZTuPrw8VHemX2uaCCkUEoxE2LNPkyOtQbFPRuWjejxE/v2trSLxp3uJPynx0lGDIfeTfd7AasSYTj54I4AW0D9fZx6ScUKYiddUkJJc= 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=zuWy5JVs; arc=none smtp.client-ip=209.85.216.74 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="zuWy5JVs" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2fe870bc003so993397a91.1 for ; Wed, 26 Feb 2025 18:19:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740622788; x=1741227588; 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=8l5U+hO6xQEeVCf+kI9O/UrrnIVeF1W04fkwqmR0RAk=; b=zuWy5JVsiYI1ZfzrJuCUUiLvMOgDT8++J5uxoTwFEt3DrW7QBR6I5wtKR4Wpn5IyhV v6U0951GdBh626qmBE1x4/B882Ni0Nke9dmrBN3Gwv2bLR3C7V5DRm/dT1R9SIb0ynaC IAAWN8DMFEYbfLZWOOZie/g9pC9T4C5IE7CSR/zTE2ycLlHcHedGGlRkeADw/a/UOGDx xRmEnOwzkthLbknuF/2X376Qw5fnPXBWviZTZ6e665FrWPHBJm6q3h4KE9yeqCZNJcht 7tlu2/+002Yf6s0tFnBtuHbUJ1zqGiWuANRsnq0UdceT1gZBO4E/cq0Z+G+2yi4hpvhZ VJvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740622788; x=1741227588; 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=8l5U+hO6xQEeVCf+kI9O/UrrnIVeF1W04fkwqmR0RAk=; b=anOblBeyx7rNSkkzTwd7WRSlD4zwPfrD8I5BbRdrQ/2IwOdE+3nM2d4zQ61if++zbH 1KuCSPc9h+P2CVHqC0gIW27SFHFqOw84LuXZQimw2MP1Iv3qtPuDjwGDt13nGELWHUdp 4N/T0tCU3petWyJTqxturlYpLnwKlcGTjhK0bW0i1MSwlwdB75Hby/ZbYVDl+hwcxR5Q rUljI95NfxlHiHYukrFxQwNlVse8IpC8CfIQLRGcog6YX2Go+aCRSCy/OvyaA6n5bFhL 8K2XgIy7EcRhkgCBUlZAdPK2qGCMhYVIfn3FbpLOlCbGLWcFx7tfWBcBVl9bghgggyoG +kwQ== X-Gm-Message-State: AOJu0YzbzZ7LTqpoYv1vIN0PgOy/nQA9jlSLjwPg/pG5dLVqVoUGzDRT xwgrBWJeM3MJKdSEM188J9DoJX5ZRgG0l2feFrbix2j2VJR5JFR/C5kBln3FRkLjaS/eD5+ZyfO Lfg== X-Google-Smtp-Source: AGHT+IGsGVabM6lqtL4epZ9oGORwajTYA23ku4RspaNu+4NGjN39FOibtOjpJfF6SNul6q4RPaHxkAISArM= X-Received: from pjbph15.prod.google.com ([2002:a17:90b:3bcf:b0:2ef:d136:17fc]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3901:b0:2fa:2268:1af4 with SMTP id 98e67ed59e1d1-2fea1299b06mr2509653a91.7.1740622788182; Wed, 26 Feb 2025 18:19:48 -0800 (PST) Reply-To: Sean Christopherson Date: Wed, 26 Feb 2025 18:18:42 -0800 In-Reply-To: <20250227021855.3257188-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: <20250227021855.3257188-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250227021855.3257188-27-seanjc@google.com> Subject: [PATCH v2 26/38] x86/kvmclock: WARN if wall clock is read while kvmclock is suspended From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "Kirill A. Shutemov" , Paolo Bonzini , Sean Christopherson , Juergen Gross , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Ajay Kaher , Jan Kiszka , Andy Lutomirski , Peter Zijlstra , Daniel Lezcano , John Stultz Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, virtualization@lists.linux.dev, linux-hyperv@vger.kernel.org, xen-devel@lists.xenproject.org, Tom Lendacky , Nikunj A Dadhania Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" WARN if kvmclock is still suspended when its wallclock is read, i.e. when the kernel reads its persistent clock. The wallclock subtly depends on the BSP's kvmclock being enabled, and returns garbage if kvmclock is disabled. Signed-off-by: Sean Christopherson --- arch/x86/kernel/kvmclock.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index 319f8b2d0702..0ce23f862cbd 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -52,6 +52,8 @@ static struct pvclock_vsyscall_time_info *hvclock_mem; DEFINE_PER_CPU(struct pvclock_vsyscall_time_info *, hv_clock_per_cpu); EXPORT_PER_CPU_SYMBOL_GPL(hv_clock_per_cpu); =20 +static bool kvmclock_suspended; + /* * The wallclock is the time of day when we booted. Since then, some time = may * have elapsed since the hypervisor wrote the data. So we try to account = for @@ -59,6 +61,7 @@ EXPORT_PER_CPU_SYMBOL_GPL(hv_clock_per_cpu); */ static void kvm_get_wallclock(struct timespec64 *now) { + WARN_ON_ONCE(kvmclock_suspended); wrmsrl(msr_kvm_wall_clock, slow_virt_to_phys(&wall_clock)); preempt_disable(); pvclock_read_wallclock(&wall_clock, this_cpu_pvti(), now); @@ -118,6 +121,7 @@ static void kvm_save_sched_clock_state(void) * to the old address prior to reconfiguring kvmclock would clobber * random memory. */ + kvmclock_suspended =3D true; kvmclock_disable(); } =20 @@ -130,16 +134,19 @@ static void kvm_setup_secondary_clock(void) =20 static void kvm_restore_sched_clock_state(void) { + kvmclock_suspended =3D false; kvm_register_clock("primary cpu, sched_clock resume"); } =20 static void kvmclock_suspend(struct clocksource *cs) { + kvmclock_suspended =3D true; kvmclock_disable(); } =20 static void kvmclock_resume(struct clocksource *cs) { + kvmclock_suspended =3D false; kvm_register_clock("primary cpu, clocksource resume"); } =20 --=20 2.48.1.711.g2feabab25a-goog