mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Binbin Wu <binbin.wu@linux.intel.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Jon Kohler <jon@nutanix.com>
Subject: Re: [PATCH 3/4] KVM: x86: Load guest/host XCR0 and XSS outside of the fastpath run loop
Date: Wed, 5 Nov 2025 18:42:04 +0800	[thread overview]
Message-ID: <88404ae2-fa4b-4357-918b-fd949dd2521a@linux.intel.com> (raw)
In-Reply-To: <20251030224246.3456492-4-seanjc@google.com>



On 10/31/2025 6:42 AM, Sean Christopherson wrote:
[...]
>   
> -void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
> +static void kvm_load_guest_xfeatures(struct kvm_vcpu *vcpu)
>   {
>   	if (vcpu->arch.guest_state_protected)
>   		return;
>   
>   	if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
> -
>   		if (vcpu->arch.xcr0 != kvm_host.xcr0)
>   			xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
>   
> @@ -1217,6 +1216,27 @@ void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
>   		    vcpu->arch.ia32_xss != kvm_host.xss)
>   			wrmsrq(MSR_IA32_XSS, vcpu->arch.ia32_xss);
>   	}
> +}
> +
> +static void kvm_load_host_xfeatures(struct kvm_vcpu *vcpu)
> +{
> +	if (vcpu->arch.guest_state_protected)
> +		return;
> +
> +	if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
> +		if (vcpu->arch.xcr0 != kvm_host.xcr0)
> +			xsetbv(XCR_XFEATURE_ENABLED_MASK, kvm_host.xcr0);
> +
> +		if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
> +		    vcpu->arch.ia32_xss != kvm_host.xss)
> +			wrmsrq(MSR_IA32_XSS, kvm_host.xss);
> +	}
> +}

kvm_load_guest_xfeatures() and kvm_load_host_xfeatures() are almost the same
except for the guest values VS. host values to set.
I am wondering if it is worth adding a helper to dedup the code, like:

static void kvm_load_xfeatures(struct kvm_vcpu *vcpu, u64 xcr0, u64 xss)
{
         if (vcpu->arch.guest_state_protected)
                 return;

         if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
                 if (vcpu->arch.xcr0 != kvm_host.xcr0)
                         xsetbv(XCR_XFEATURE_ENABLED_MASK, xcr0);

                 if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
                     vcpu->arch.ia32_xss != kvm_host.xss)
                         wrmsrq(MSR_IA32_XSS, xss);
         }
}



  reply	other threads:[~2025-11-05 10:42 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-30 22:42 [PATCH 0/4] KVM: x86: Cleanup #MC and XCR0/XSS/PKRU handling Sean Christopherson
2025-10-30 22:42 ` [PATCH 1/4] KVM: SVM: Handle #MCs in guest outside of fastpath Sean Christopherson
2025-10-30 22:42 ` [PATCH 2/4] KVM: VMX: Handle #MCs on VM-Enter/TD-Enter outside of the fastpath Sean Christopherson
2025-11-17 12:38   ` Tony Lindgren
2025-11-17 15:47     ` Sean Christopherson
2025-11-18  5:33       ` Tony Lindgren
2025-11-17 16:30     ` Edgecombe, Rick P
2025-10-30 22:42 ` [PATCH 3/4] KVM: x86: Load guest/host XCR0 and XSS outside of the fastpath run loop Sean Christopherson
2025-11-05 10:42   ` Binbin Wu [this message]
2025-11-05 14:43     ` Sean Christopherson
2025-11-06  1:55       ` Binbin Wu
2025-10-30 22:42 ` [PATCH 4/4] KVM: x86: Load guest/host PKRU " Sean Christopherson
2025-10-31 17:58   ` Jon Kohler
2025-10-31 20:52     ` Sean Christopherson
2025-11-03 15:32       ` Jon Kohler
2025-10-31 17:58 ` [PATCH 0/4] KVM: x86: Cleanup #MC and XCR0/XSS/PKRU handling Jon Kohler
2025-10-31 23:35 ` Edgecombe, Rick P
2025-11-10 15:37 ` Sean Christopherson
2025-11-17 18:35   ` Sean Christopherson

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=88404ae2-fa4b-4357-918b-fd949dd2521a@linux.intel.com \
    --to=binbin.wu@linux.intel.com \
    --cc=jon@nutanix.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®