From mboxrd@z Thu Jan 1 00:00:00 1970 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 A073F22F160 for ; Thu, 27 Feb 2025 02:19:48 +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=1740622790; cv=none; b=jfiRaWpRmnFsCs+JyxpdIYm5Ol1rytYXhsB0uGu+FQd8FXKSr+bXzDeK1LFbBfUl5wbKppMHxpUjsT9vl0dSo0tqxeLyEx8EFYo0BXbIQbDs0ng1sen5fWWmfY2150IKybQJpBP1lSDDjivx9WEGjhBU93zKYsvUWayeMv2F500= 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=gb+RaMoa; 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="gb+RaMoa" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fe870bc003so993398a91.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=lists.linux.dev; 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=gb+RaMoavqOJQ/IVz7yqb2VwYoTHkZA8k1gepDGfyCLHCFE3vaLoQmMyUG846MGCgL bqXOx+xHBCGSMhmX1YTGJ7mHjD1hHE/46oY2ZwMbmUpeNnTd9pCo9xgjwbMCWtsHAxWn GZIzjdXCpTC7yXUW+h7HDr7pkkYWPB6yvdUUn5c4kZqQ74VqaGL3sbecAOXzxcU0fHuP O97RoRlhqRAeq+E7REnkUnZnGNKkXIBzxsmux1tYjLiHScfjYZZ1BfyCG3HGAYf8ZPiy 5z2RKgom4CrKoG1meFGKWbbhgEpmUBNEU5LlUd4kucDNg1u595aS8oCTXFQk1bdwDAv2 EOng== 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=LxbEnEJfBSw5ZQ/k7UcO8dw9YcLXsDoRH+U6xsXBQ7LihLIM3oao9SF7akI5s4y8fN Pfh8rLeDADpwSMho8fJIKxeMetOywk7Vr5nDmXB5SUw0CGhtBxAcQD7r/JJHeGdzOP4u lV+6barakOIfxqDMHYdzj0z0Gvj25IeF8KQwp91nWB2M5Aq+14qAzT8FLyTe2HTCZLh1 uDu92bHA3pJ/d2/q98Wz20ZFs1iBGUmM2Qle/v7ll61sePNTtcDPGhKZgWLsaGTHVzdl tlwFDvaigcs0iugnOkMweoTmHVfaSIQyG/bXtkeZEutiP1T1W/gc1blsG/8r7/w4Dqb6 zjyw== X-Forwarded-Encrypted: i=1; AJvYcCWWRtJ1UCYWVlVA/ll1Qc/7IUN+ABHMv6pDC/tCTlruYnisZ3ePHC9xuRYk1e+DkkaktGu1OD+KUB3S@lists.linux.dev X-Gm-Message-State: AOJu0YxIkx98vvnWmSLCbUvwmQ9riqQUe6521T1++FWFyfdZUo0H4Zgz n8ShhZg5lz6ODZi36B9dBG8seGf+nqz14y5LmvC3T08CrnrkF7Tv1Hf46fygCD30Dxfpm1OTTGE X7g== 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-coco@lists.linux.dev 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-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); +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 = true; kvmclock_disable(); } @@ -130,16 +134,19 @@ static void kvm_setup_secondary_clock(void) static void kvm_restore_sched_clock_state(void) { + kvmclock_suspended = false; kvm_register_clock("primary cpu, sched_clock resume"); } static void kvmclock_suspend(struct clocksource *cs) { + kvmclock_suspended = true; kvmclock_disable(); } static void kvmclock_resume(struct clocksource *cs) { + kvmclock_suspended = false; kvm_register_clock("primary cpu, clocksource resume"); } -- 2.48.1.711.g2feabab25a-goog