Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp1166482rdb; Fri, 20 Oct 2023 10:07:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGdXlh0hv7r+Aa/w9vxanT1R/B16w4VjA7mF1WMbvRDcpKlQgCYmw+nWBypib83QrDXdGP/ X-Received: by 2002:a17:903:268a:b0:1c3:4b24:d89d with SMTP id jf10-20020a170903268a00b001c34b24d89dmr2478871plb.40.1697821677914; Fri, 20 Oct 2023 10:07:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697821677; cv=none; d=google.com; s=arc-20160816; b=MBcFPpguihX3TXnRZj/E5aN0P+mX7Yt5gHsMVXDzWwSXHEDC6h7notALRrdpKZfxac GKu5PgQga9GcBP7wrj0hKp/8ASI9hWH5PISBJFX2x1AprTLxBk4H4omEXcpxRpNTi4iK +N6MZgSgBlOoaNWc/oHh28U3j9asEHCwbNYfvr7XmHhrH3a1zCU6BMwZ+A86myQjcAsw 6IHklJkn/wS2T6kAjRACaGT69mgzvR6G0M0x5w4VIEPY4PAEGDzBk4Lp3/MYbB+QxCFz aROMz5uh2Rfu7sE0r71g+S5ikhXQ1DpVHIKSenezZ4O20H+FsOEHfCd1gpOhRtnBbker E1Vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=x4BWurQpapK412rcDC+pUyHudLjLoftq7UvjlE0wpgY=; fh=PA0Zgye0gHXlK4ZxNnECUg6MLEBoxBIg9EFJqsT0leE=; b=SkTudfBXQUk26v6ExvZqztMfyPGi+pOLbW+zEdMkgQpw0FdtU9Zu+TT2nEjKQTP8Vb NbfoKIFn5Qk5GpUcWpe0ot3iLR1uWhxItVfh4LTnBzppbkzaDre07zcmwNY7v8eBwuBR qEYZS9rL2WVbK0RMernzDjRs0q7bsV6fzn/KxGKCOwjrb/JkMUvStHp97s343fYbMf+l yFmfr7ewhKrvZ9AQRn7p8tfpnJ4tR0fItLcVCzdatpLM2S1I23wGSGOrkkEf3d04UNgM iYZzI2+NygEGH1YQYXUvfL1YLPtpOkmNJlspBY8oc3seMyC8Nbvwm0/eT0hYcvdttLjD dWCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=LKFuLI8Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id a13-20020a170902eccd00b001c9b5d2c406si2207070plh.403.2023.10.20.10.07.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 10:07:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=LKFuLI8Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id C48F983C9F89; Fri, 20 Oct 2023 10:07:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229683AbjJTRHj (ORCPT + 99 others); Fri, 20 Oct 2023 13:07:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229522AbjJTRHi (ORCPT ); Fri, 20 Oct 2023 13:07:38 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED333A3 for ; Fri, 20 Oct 2023 10:07:36 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-27779f49458so916164a91.2 for ; Fri, 20 Oct 2023 10:07:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697821656; x=1698426456; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=x4BWurQpapK412rcDC+pUyHudLjLoftq7UvjlE0wpgY=; b=LKFuLI8QH7jcz8atySd85fQrc6cGMXwxm2hvQ4z2PXbxZITY5blOeTTRAj8tWWxuC4 h0kAzWEvA9lBKXWQsQwri/k+aLffpsuVaIq9+W8aPY+3HbbiSQzJlP9vzWBUSikVWn3U W+dncEOJvoQg2qWYwsXH7KRW62RMhUuxbGd9AMdODPEphCkoE6MgISK9yIM3VuxDpCWV DQWgSCPVkqBwJnxoXLHZ5h66KTGIOW6tIpVu9064tluIINHwExkPMmPHj84ta0XJxlJ8 +gsCUrT4RMJrmPR+EXPfPNrlRYglIrXUbfOg+NOMjCqAMOtRyJEbS8R6dNfuUvF4tgmT ae3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697821656; x=1698426456; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x4BWurQpapK412rcDC+pUyHudLjLoftq7UvjlE0wpgY=; b=hD/0uH/tx2lgUwFWBQrvRzxqp17ri1G3b7U6o+2b/7NZ3Y2z44vYu4BQ63IfifgjcF xd5o5kQ3t6loER4AH1tDPvAwxlTkXUKLTuns0X4MYaHpMAHVqlS2Jcpwc6HWj7owMnk/ ahFc7UUUwUDdkUqdqViVzy5DRODF/VosUEeuHfnvlTCub6y7j9ipCWbmsxpjRlgs5bYq pPycKn72ZcdGGE/88Km6bNVWOAxshEVsvsukMgFL8NF2hKLTbYLMsAxwszGdUFlx7/rT iLKdLN7cI46R7BKTapI0nS6lKvOcecvR0dxT3h46OP+Ziw7QGk214n6eCFEFP6ZEM86L 3nzw== X-Gm-Message-State: AOJu0YxuwCdF0NkznXHJjSDJn9lhH20lR2Np1/CkBQMumZWmXVKIN55c NoAx9zpFFrios8KuVzj+U1nVsEq2iX0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:ab88:b0:277:3135:bc4f with SMTP id n8-20020a17090aab8800b002773135bc4fmr57158pjq.1.1697821656319; Fri, 20 Oct 2023 10:07:36 -0700 (PDT) Date: Fri, 20 Oct 2023 17:07:34 +0000 In-Reply-To: <87wmvh47zb.fsf@redhat.com> Mime-Version: 1.0 References: <20231020151242.1814-1-kirill.shutemov@linux.intel.com> <20231020151242.1814-5-kirill.shutemov@linux.intel.com> <87wmvh47zb.fsf@redhat.com> Message-ID: Subject: Re: [PATCHv2 04/13] x86/kvm: Do not try to disable kvmclock if it was not enabled From: Sean Christopherson To: Vitaly Kuznetsov Cc: "Kirill A. Shutemov" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "Rafael J. Wysocki" , Peter Zijlstra , Adrian Hunter , Kuppuswamy Sathyanarayanan , Elena Reshetova , Jun Nakajima , Rick Edgecombe , Tom Lendacky , Ashish Kalra , Kai Huang , Baoquan He , kexec@lists.infradead.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Paolo Bonzini , Wanpeng Li Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Fri, 20 Oct 2023 10:07:55 -0700 (PDT) On Fri, Oct 20, 2023, Vitaly Kuznetsov wrote: > > --- > > arch/x86/kernel/kvmclock.c | 12 ++++++++---- > > 1 file changed, 8 insertions(+), 4 deletions(-) > > > > diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c > > index fb8f52149be9..f2fff625576d 100644 > > --- a/arch/x86/kernel/kvmclock.c > > +++ b/arch/x86/kernel/kvmclock.c > > @@ -24,8 +24,8 @@ > > > > static int kvmclock __initdata = 1; > > static int kvmclock_vsyscall __initdata = 1; > > -static int msr_kvm_system_time __ro_after_init = MSR_KVM_SYSTEM_TIME; > > -static int msr_kvm_wall_clock __ro_after_init = MSR_KVM_WALL_CLOCK; > > +static int msr_kvm_system_time __ro_after_init; > > +static int msr_kvm_wall_clock __ro_after_init; > > static u64 kvm_sched_clock_offset __ro_after_init; > > > > static int __init parse_no_kvmclock(char *arg) > > @@ -195,7 +195,8 @@ static void kvm_setup_secondary_clock(void) > > > > void kvmclock_disable(void) > > { > > - native_write_msr(msr_kvm_system_time, 0, 0); > > + if (msr_kvm_system_time) > > + native_write_msr(msr_kvm_system_time, 0, 0); > > } > > > > static void __init kvmclock_init_mem(void) > > @@ -294,7 +295,10 @@ void __init kvmclock_init(void) > > if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) { > > msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW; > > msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW; > > - } else if (!kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) { > > + } else if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) { > > + msr_kvm_system_time = MSR_KVM_SYSTEM_TIME; > > + msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK; > > + } else { > > return; > > } > > This should work, so > > Reviewed-by: Vitaly Kuznetsov > > but my personal preference would be to change kvm_guest_cpu_offline() > to check KVM features explicitly instead of checking MSRs against '0' > at least becase it already does so for other features. Completely > untested: > > diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c > index b8ab9ee5896c..1ee49c98e70a 100644 > --- a/arch/x86/kernel/kvm.c > +++ b/arch/x86/kernel/kvm.c > @@ -454,7 +454,9 @@ static void kvm_guest_cpu_offline(bool shutdown) > kvm_pv_disable_apf(); > if (!shutdown) > apf_task_wake_all(); > - kvmclock_disable(); > + if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2) || > + kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) > + kvmclock_disable(); > } That would result in an unnecessray WRMSR in the case where kvmclock is disabled on the command line. It _should_ be benign given how the code is written, but it's not impossible to imagine a scenario where someone disabled kvmclock in the guest because of a hypervisor bug. And the WRMSR would become a bogus write to MSR 0x0 if someone made a "cleanup" to set msr_kvm_system_time if and only if kvmclock is actually used, e.g. if someone made Kirill's change sans the check in kvmclock_disable().