All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Anton Romanov <romanton@google.com>
Cc: kvm@vger.kernel.org, pbonzini@redhat.com, vkuznets@redhat.com,
	mlevitsk@redhat.com
Subject: Re: [PATCH v4] KVM: x86: Use current rather than snapshotted TSC frequency if it is constant
Date: Wed, 11 May 2022 19:49:43 +0000	[thread overview]
Message-ID: <YnwTV2TEzmEQqVvZ@google.com> (raw)
In-Reply-To: <20220511014226.3099627-1-romanton@google.com>

On Wed, May 11, 2022, Anton Romanov wrote:
> @@ -8646,9 +8663,10 @@ static void tsc_khz_changed(void *data)
>  	struct cpufreq_freqs *freq = data;
>  	unsigned long khz = 0;
>  
> +	WARN_ON_ONCE(boot_cpu_has(X86_FEATURE_CONSTANT_TSC));

Nit, please add a newline to isolate the WARN.

>  	if (data)
>  		khz = freq->new;
> -	else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
> +	else
>  		khz = cpufreq_quick_get(raw_smp_processor_id());
>  	if (!khz)
>  		khz = tsc_khz;
> @@ -8669,8 +8687,10 @@ static void kvm_hyperv_tsc_notifier(void)
>  	hyperv_stop_tsc_emulation();
>  
>  	/* TSC frequency always matches when on Hyper-V */
> -	for_each_present_cpu(cpu)
> -		per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
> +	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
> +		for_each_present_cpu(cpu)
> +			per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
> +	}
>  	kvm_max_guest_tsc_khz = tsc_khz;
>  
>  	list_for_each_entry(kvm, &vm_list, vm_list) {
> @@ -8783,7 +8803,8 @@ static struct notifier_block kvmclock_cpufreq_notifier_block = {
>  
>  static int kvmclock_cpu_online(unsigned int cpu)
>  {
> -	tsc_khz_changed(NULL);
> +	if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
> +		tsc_khz_changed(NULL);


Ah rats, I missed something in v3.  Rather than handle this in the notifier, KVM
can simply not register the notifier in the first place.  The CPUHP_AP_X86_KVM_CLK_ONLINE
hook exists purely to muck with cpu_tsc_khz.  And that makes the WARN_ON_ONCE in
tsc_khz_changed() much less rendunat (having a caller and its callee check the
same thing in quick succession felt silly).

I.e. instead of modifying kvmclock_cpu_online(), do:

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 6567aec84223..e9efb8d00673 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8882,10 +8882,10 @@ static void kvm_timer_init(void)
                }
                cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
                                          CPUFREQ_TRANSITION_NOTIFIER);
-       }
 
-       cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
-                         kvmclock_cpu_online, kvmclock_cpu_down_prep);
+               cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
+                                 kvmclock_cpu_online, kvmclock_cpu_down_prep);
+       }
 }
 
 #ifdef CONFIG_X86_64
@@ -9038,10 +9038,11 @@ void kvm_arch_exit(void)
 #endif
        kvm_lapic_exit();
 
-       if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
+       if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
                cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
                                            CPUFREQ_TRANSITION_NOTIFIER);
-       cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
+               cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
+       }
 #ifdef CONFIG_X86_64
        pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
        irq_work_sync(&pvclock_irq_work);



>  	return 0;
>  }
>  
> -- 
> 2.36.0.550.gb090851708-goog
> 

      reply	other threads:[~2022-05-11 19:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-11  1:42 [PATCH v4] KVM: x86: Use current rather than snapshotted TSC frequency if it is constant Anton Romanov
2022-05-11 19:49 ` Sean Christopherson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YnwTV2TEzmEQqVvZ@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=mlevitsk@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=romanton@google.com \
    --cc=vkuznets@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.