All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Jing Liu <jing2.liu@intel.com>
Cc: x86@kernel.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kselftest@vger.kernel.org, tglx@linutronix.de,
	mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
	pbonzini@redhat.com, corbet@lwn.net, shuah@kernel.org,
	jun.nakajima@intel.com, kevin.tian@intel.com,
	jing2.liu@linux.intel.com, guang.zeng@intel.com,
	wei.w.wang@intel.com, yang.zhong@intel.com
Subject: Re: [PATCH v3 19/22] kvm: x86: Get/set expanded xstate buffer
Date: Wed, 29 Dec 2021 00:38:32 +0000	[thread overview]
Message-ID: <YcuuCMCQryzUFoAZ@google.com> (raw)
In-Reply-To: <20211222124052.644626-20-jing2.liu@intel.com>

Shortlog needs to have a verb somewhere.

On Wed, Dec 22, 2021, Jing Liu wrote:
> From: Guang Zeng <guang.zeng@intel.com>
> 
> When AMX is enabled it requires a larger xstate buffer than
> the legacy hardcoded 4KB one. Exising kvm ioctls

Existing

> (KVM_[G|S]ET_XSAVE under KVM_CAP_XSAVE) are not suitable for
> this purpose.

...

> Reuse KVM_SET_XSAVE for both old/new formats by reimplementing it to
> do properly-sized memdup_user() based on the guest fpu container.

I'm confused, the first sentence says KVM_SET_XSAVE isn't suitable, the second
says it can be reused with minimal effort.

> Also, update the api doc with the new KVM_GET_XSAVE2 ioctl.

...

> @@ -5367,7 +5382,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
>  		break;
>  	}
>  	case KVM_SET_XSAVE: {
> -		u.xsave = memdup_user(argp, sizeof(*u.xsave));
> +		int size = vcpu->arch.guest_fpu.uabi_size;

IIUC, reusing KVM_SET_XSAVE works by requiring that userspace use KVM_GET_XSAVE2
if userspace has expanded the guest FPU size by exposing relevant features to
the guest via guest CPUID.  If so, then that needs to be enforced in KVM_GET_XSAVE,
otherwise userspace will get subtle corruption by invoking the wrong ioctl, e.g.

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 2c9606380bca..5d2acbd52df5 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5386,6 +5386,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
                break;
        }
        case KVM_GET_XSAVE: {
+               r -EINVAL;
+               if (vcpu->arch.guest_fpu.uabi_size > sizeof(struct kvm_xsave))
+                       break;
+
                u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
                r = -ENOMEM;
                if (!u.xsave)

> +
> +		u.xsave = memdup_user(argp, size);
>  		if (IS_ERR(u.xsave)) {
>  			r = PTR_ERR(u.xsave);
>  			goto out_nofree;
> @@ -5376,6 +5393,26 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
>  		r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
>  		break;
>  	}
> +
> +	case KVM_GET_XSAVE2: {
> +		int size = vcpu->arch.guest_fpu.uabi_size;
> +
> +		u.xsave = kzalloc(size, GFP_KERNEL_ACCOUNT);
> +		if (!u.xsave) {
> +			r = -ENOMEM;

I hate the odd patterns in this code as much as anyone, but for better or worse
the style throughout is:

		r = -ENOMEM;
		u.xsave = kzalloc(size, GFP_KERNEL_ACCOUNT);
		if (u.xsave)
			break;

> +			break;
> +		}
> +
> +		kvm_vcpu_ioctl_x86_get_xsave2(vcpu, u.buffer, size);
> +
> +		if (copy_to_user(argp, u.xsave, size)) {
> +			r = -EFAULT;
> +			break;

Same style thing here.

> +		}
> +		r = 0;
> +		break;
> +	}
> +
>  	case KVM_GET_XCRS: {
>  		u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
>  		r = -ENOMEM;
> diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
> index 1daa45268de2..9d1c01669560 100644
> --- a/include/uapi/linux/kvm.h
> +++ b/include/uapi/linux/kvm.h
> @@ -1131,6 +1131,7 @@ struct kvm_ppc_resize_hpt {
>  #define KVM_CAP_EXIT_ON_EMULATION_FAILURE 204
>  #define KVM_CAP_ARM_MTE 205
>  #define KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM 206
> +#define KVM_CAP_XSAVE2 207
>  
>  #ifdef KVM_CAP_IRQ_ROUTING
>  
> @@ -1610,6 +1611,9 @@ struct kvm_enc_region {
>  #define KVM_S390_NORMAL_RESET	_IO(KVMIO,   0xc3)
>  #define KVM_S390_CLEAR_RESET	_IO(KVMIO,   0xc4)
>  
> +/* Available with KVM_CAP_XSAVE2 */
> +#define KVM_GET_XSAVE2		  _IOR(KVMIO,  0xcf, struct kvm_xsave)
> +
>  struct kvm_s390_pv_sec_parm {
>  	__u64 origin;
>  	__u64 length;
> -- 
> 2.27.0
> 

  reply	other threads:[~2021-12-29  0:38 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-22 12:40 [PATCH v3 00/22] AMX Support in KVM Jing Liu
2021-12-22 12:40 ` [PATCH v3 01/22] x86/fpu: Extend fpu_xstate_prctl() with guest permissions Jing Liu
2021-12-22 12:40 ` [PATCH v3 02/22] x86/fpu: Prepare guest FPU for dynamically enabled FPU features Jing Liu
2021-12-22 12:40 ` [PATCH v3 03/22] kvm: x86: Fix xstate_required_size() to follow XSTATE alignment rule Jing Liu
2021-12-22 12:40 ` [PATCH v3 04/22] kvm: x86: Exclude unpermitted xfeatures at KVM_GET_SUPPORTED_CPUID Jing Liu
2021-12-22 12:40 ` [PATCH v3 05/22] kvm: x86: Check permitted dynamic xfeatures at KVM_SET_CPUID2 Jing Liu
2021-12-28 23:38   ` Sean Christopherson
2021-12-29  2:18     ` Tian, Kevin
2021-12-22 12:40 ` [PATCH v3 06/22] x86/fpu: Make XFD initialization in __fpstate_reset() a function argument Jing Liu
2021-12-22 12:40 ` [PATCH v3 07/22] x86/fpu: Add guest support to xfd_enable_feature() Jing Liu
2021-12-22 12:40 ` [PATCH v3 08/22] x86/fpu: Provide fpu_update_guest_perm_features() for guest Jing Liu
2021-12-22 12:40 ` [PATCH v3 09/22] kvm: x86: Enable dynamic XSAVE features at KVM_SET_CPUID2 Jing Liu
2021-12-28 23:54   ` Sean Christopherson
2021-12-29  2:23     ` Tian, Kevin
2021-12-22 12:40 ` [PATCH v3 10/22] x86/fpu: Provide fpu_update_guest_xfd() for IA32_XFD emulation Jing Liu
2021-12-22 12:40 ` [PATCH v3 11/22] kvm: x86: Add emulation for IA32_XFD Jing Liu
2021-12-22 12:40 ` [PATCH v3 12/22] x86/fpu: Prepare xfd_err in struct fpu_guest Jing Liu
2021-12-22 12:40 ` [PATCH v3 13/22] kvm: x86: Intercept #NM for saving IA32_XFD_ERR Jing Liu
2021-12-29  0:09   ` Sean Christopherson
2021-12-29  2:52     ` Tian, Kevin
2021-12-29 17:37       ` Sean Christopherson
2021-12-29  6:50     ` Tian, Kevin
2021-12-29  8:13     ` Tian, Kevin
2021-12-22 12:40 ` [PATCH v3 14/22] kvm: x86: Emulate IA32_XFD_ERR for guest Jing Liu
2021-12-22 12:40 ` [PATCH v3 15/22] kvm: x86: Disable RDMSR interception of IA32_XFD_ERR Jing Liu
2021-12-22 12:40 ` [PATCH v3 16/22] kvm: x86: Add XCR0 support for Intel AMX Jing Liu
2021-12-29  0:21   ` Sean Christopherson
2021-12-29  3:01     ` Tian, Kevin
2021-12-22 12:40 ` [PATCH v3 17/22] kvm: x86: Add CPUID " Jing Liu
2021-12-22 12:40 ` [PATCH v3 18/22] x86/fpu: Add uabi_size to guest_fpu Jing Liu
2021-12-22 12:40 ` [PATCH v3 19/22] kvm: x86: Get/set expanded xstate buffer Jing Liu
2021-12-29  0:38   ` Sean Christopherson [this message]
2021-12-29  2:57     ` Wang, Wei W
2021-12-29  6:36       ` Tian, Kevin
2021-12-22 12:40 ` [PATCH v3 20/22] kvm: selftests: Add support for KVM_CAP_XSAVE2 Jing Liu
2021-12-22 12:40 ` [PATCH v3 21/22] x86/fpu: Provide fpu_sync_guest_vmexit_xfd_state() Jing Liu
2021-12-22 12:40 ` [PATCH v3 22/22] kvm: x86: Disable interception for IA32_XFD on demand Jing Liu
2021-12-29  1:04   ` Sean Christopherson
2021-12-29  3:35     ` Tian, Kevin
2021-12-29  7:16     ` Tian, Kevin
2021-12-29 17:26       ` Sean Christopherson
2021-12-30  1:28         ` Tian, Kevin
2021-12-30  7:04         ` Tian, Kevin
2021-12-31  9:42         ` Tian, Kevin
2021-12-29  7:37     ` Tian, Kevin
2022-01-04 18:32     ` Paolo Bonzini
2022-01-04 18:58       ` 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=YcuuCMCQryzUFoAZ@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=guang.zeng@intel.com \
    --cc=jing2.liu@intel.com \
    --cc=jing2.liu@linux.intel.com \
    --cc=jun.nakajima@intel.com \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=shuah@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=wei.w.wang@intel.com \
    --cc=x86@kernel.org \
    --cc=yang.zhong@intel.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.