From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (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 63651442138 for ; Mon, 10 Aug 2026 22:55:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.199 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402518; cv=none; b=uunEnF986rHnyhalSiLK3VGn5KItUOOq+z5BJ9u0pMOCao8kxb08PqMUkEuKDPDhSiKb/GhhLAaK+e2d0KIxdwO/uqscotrsuWJuJwSCWyHxjuYEL1KYG2Pld5GKSpZmjpDUiOOVNIhUb2ZYNVgt1HDd/bM15YXQBCkq/r88um0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402518; c=relaxed/simple; bh=CVnhoSX+qbZJfZo5pFMAK6JiunEH6jHp8TBgm6WOvd4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BQqpHYtAYb0KD8KU8g1DHOXHfb1PXcGM8a7x+sQtvt9cx9f9OkrhDAwhxqB+rq8unKdbwWUVsxRrd9ZyLgzfJZpUpufp6jV0EWyCAP0YL+SWtJgqz0YVo++S75mRPcd+9ffFqZRlxJ9F+QycaCrpRQOUG2IGXTWnOhnUYkXv5L8= 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=MdkUwrYq; arc=none smtp.client-ip=209.85.210.199 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="MdkUwrYq" Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-84e01a62d99so181068b3a.0 for ; Mon, 10 Aug 2026 15:55:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402516; x=1787007316; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=yh0HMLDRPnv1g8EvjLjzH3sEAJckmpo3zknzdaSQGA0=; b=MdkUwrYqwbEjvaa2hHVkhcr5GcewoRf6eeL+KaP6g6GCI80+rWxQFkvmnVv5WH2i7e gcuLGK3mq2eQIbreTPAxg0m9AKXpSpEzmQqv95WAn+W8oPiplCWQYDgkTCpuU4DpZEnC AaYYfdl/3uDQxCm/Iw06JA77L9hS7lFezbUO9oeQIs1nUsSHT11aPJAt+O4GGoY1JI34 IaLovgm+FwYlE8giABRP7pXx5gwvKNeznoLIcJrleVwgXlNBlfglrCfzfeDXtu+WOwqH Rjbf36Oe+LesMUnKt11mKCu/CgLnc01or1CKZzIW2Oo376N+Xh4rjRtro2j4ZmU5YTAU zZzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402516; x=1787007316; h=content-type: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:content-type; bh=yh0HMLDRPnv1g8EvjLjzH3sEAJckmpo3zknzdaSQGA0=; b=bI5ejcNc7Aolk+xJrSge9U8vk6HbL+qw4WNxkF/qCNVD/eWRuXGmnrFR4Xjp7dOTpB mzpPTbnd68eVsUoi0tmicyqAa0ykvr77w77eZODyllQ/6OdBMjqNtD+ocNJDFdeyPBig LJrUIETzAW/bj9ADwizr8AU0HG3tyGbPdvrtkk9+ze47ZCYvDYAXs6e0SEIN8UyGq6gA I7PV77y37vGedWNF+jNvQu/lrnXDO0hFHRE5ugZkW22PG6rZLcevPK4t0cgSJqLzMu9U OOtI/fv9hRlEZWdEc/17F/ZOe/Vff9AyYUzvIIbSJEwshO8ZINE22av+E2x6Ub7lfZoN AVpQ== X-Forwarded-Encrypted: i=1; AHgh+Ro+u/ACPQUlR07wlCjbtdsRli8SNOyjb63T+q/BepxUxKKjRMrQOpVWjKPGt+seJmulfbljrkmVeFWv7Jg=@vger.kernel.org X-Gm-Message-State: AOJu0YwWyOp3n4TQnWEPjsPSumBzFE8HPzeHY12z8ZgSbDsB63DCFryZ R3k/YJD+8dSLFSFKylrTxcSQG5RzYwPrNLLzSPalfA5gTlS1mxV5+6u92Bo0+NXOSEIOOFoVBiy y5XdFWA== X-Received: from pfbc4.prod.google.com ([2002:a05:6a00:ad04:b0:848:478d:6efb]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:94e7:b0:848:3f91:da82 with SMTP id d2e1a72fcca58-84f2e12c1fcmr51167957b3a.32.1786402515490; Mon, 10 Aug 2026 15:55:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:48 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-11-seanjc@google.com> Subject: [PATCH v9 10/21] KVM: x86: Fall back to non-master-clock if clockread fails in get_kvmclock() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Type: text/plain; charset="UTF-8" From: David Woodhouse When computing kvmclock and it's currently in master-clock mode, fall back to the non-master-clock path if the clock read fails, e.g. if the kernel's clocksource is transitioning away from TSC but ka->use_master_clock hasn't been updated yet. The rdtsc() fallback was added (well, kept) in commit c68dc1b577ea ("KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK") purely to avoid uninitialized variables and compilation problems on 32-bit kernels (already addressed). In hindsight, keeping the rdtsc() was a hack and a mistake. Link: https://lore.kernel.org/all/CAOQ_QsgVqS_PuJo8F10Gg5Xw+tKt+5gDx+kJf1j3CiPO4MAOqg@mail.gmail.com Signed-off-by: David Woodhouse [sean: isolate from refactoring changes, write changelog] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 304e078cb899..33ba840d3267 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1674,14 +1674,13 @@ static bool __get_kvmclock_master_clock(struct kvm *kvm, if (!tsc_hz) return false; - if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { - data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; - data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; - } else { - data->host_tsc = rdtsc(); - } + if (!kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) + return false; + + data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; + data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC | + KVM_CLOCK_TSC_STABLE; - data->flags |= KVM_CLOCK_TSC_STABLE; hv_clock.tsc_timestamp = ka->master_cycle_now; hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; kvm_get_time_scale(NSEC_PER_SEC, tsc_hz, -- 2.55.0.679.g6767b8d81c-goog