Re: [PATCH v15 19/20] KVM: SEV: Provide support for SNP_EXTENDED_GUEST_REQUEST NAE event
From: Sean Christopherson
Date: Mon May 13 2024 - 19:48:39 EST
On Wed, May 01, 2024, Michael Roth wrote:
> Version 2 of GHCB specification added support for the SNP Extended Guest
> Request Message NAE event. This event serves a nearly identical purpose
> to the previously-added SNP_GUEST_REQUEST event, but allows for
> additional certificate data to be supplied via an additional
> guest-supplied buffer to be used mainly for verifying the signature of
> an attestation report as returned by firmware.
>
> This certificate data is supplied by userspace, so unlike with
> SNP_GUEST_REQUEST events, SNP_EXTENDED_GUEST_REQUEST events are first
> forwarded to userspace via a KVM_EXIT_VMGEXIT exit structure, and then
> the firmware request is made after the certificate data has been fetched
> from userspace.
>
> Since there is a potential for race conditions where the
> userspace-supplied certificate data may be out-of-sync relative to the
> reported TCB or VLEK that firmware will use when signing attestation
> reports, a hook is also provided so that userspace can be informed once
> the attestation request is actually completed. See the updates to
> Documentation/ for more details on these aspects.
>
> Signed-off-by: Michael Roth <michael.roth@xxxxxxx>
> ---
> Documentation/virt/kvm/api.rst | 87 ++++++++++++++++++++++++++++++++++
> arch/x86/kvm/svm/sev.c | 86 +++++++++++++++++++++++++++++++++
> arch/x86/kvm/svm/svm.h | 3 ++
> include/uapi/linux/kvm.h | 23 +++++++++
> 4 files changed, 199 insertions(+)
>
> diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
> index f0b76ff5030d..f3780ac98d56 100644
> --- a/Documentation/virt/kvm/api.rst
> +++ b/Documentation/virt/kvm/api.rst
> @@ -7060,6 +7060,93 @@ Please note that the kernel is allowed to use the kvm_run structure as the
> primary storage for certain register types. Therefore, the kernel may use the
> values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set.
>
> +::
> +
> + /* KVM_EXIT_VMGEXIT */
> + struct kvm_user_vmgexit {
LOL, it looks dumb, but maybe kvm_vmgexit_exit to avoid confusing about whether
the struct refers to host userspace vs. guest userspace?
Actually, I vote to punt on naming until more exits need to be kicked to userspace,
and just do (see below for details on how I got here):
/* KVM_EXIT_VMGEXIT */
struct {
__u64 exit_code;
union {
struct {
__u64 data_gpa;
__u64 data_npages;
__u64 ret;
} req_certs;
};
} vmgexit;
> + #define KVM_USER_VMGEXIT_REQ_CERTS 1
> + __u32 type; /* KVM_USER_VMGEXIT_* type */
Regardless of whether or not requesting a certificate is vendor specific enough
to justify its own exit reason, I don't think KVM should have a #VMGEXIT that
adds its own layer. Structuring the user exit this way will make it weird and/or
difficult to handle #VMGEXITs that _do_ fit a generic pattern, e.g. a user might
wonder why PSC #VMGEXITs don't show up here.
And defining an exit reason that is, for all intents and purposes, a regurgitation
of the raw #VMGEXIT reason, but with a different value, is also confusing. E.g.
it wouldn't be unreasonable for a reader to expect that "type" matches the value
defined in the GHCB (or whever the values are defined).
Ah, you copied what KVM does for Hyper-V and Xen emulation. Hrm. But only
partially.
Assuming it's impractical to have a generic user exit for this, and we think
there is a high likelihood of needing to punt more #VMGEXITs to userspace, then
we should more closely (perhaps even exactly) follow the Hyper-V and Xen models.
I.e. for all values and whanot that are controlled/defined by a third party
(Hyper-V, Xen, the GHCB, etc.) #define those values in a header that is clearly
"owned" by the third party.
E.g. IIRC, include/xen/interface/xen.h is copied verbatim from Xen documentation
(source?). And include/asm-generic/hyperv-tlfs.h is the kernel's copy of the
TLFS, which dictates all of the Hyper-V hypercalls.
If we do that, then my concerns/objections largely go away, e.g. KVM isn't
defining magic values, there's less chance for confusion about what "type" holds,
etc.
Oh, and if we go that route, the sizes for all fields should follow the GHCB,
e.g. I believe the "type" should be a __u64.
> + union {
> + struct {
> + __u64 data_gpa;
> + __u64 data_npages;
> + #define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_INVALID_LEN 1
> + #define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_BUSY 2
> + #define KVM_USER_VMGEXIT_REQ_CERTS_ERROR_GENERIC (1 << 31)
Hopefully it won't matter, but are BUSY and GENERIC actually defined somewhere?
I don't see them in GHCB 2.0.
In a perfect world, it would be nice for KVM to not have to care about the error
codes. But KVM disallows KVM_{G,S}ET_REGS for guest with protected state, which
means it's not feasible for userspace to set registers, at least not in any sane
way.
Heh, we could abuse KVM_SYNC_X86_REGS to let userspace specify RBX, but (a) that's
gross, and (b) KVM_SYNC_X86_REGS and KVM_SYNC_X86_SREGS really ought to be rejected
if guest state is protected.
> + __u32 ret;
> + #define KVM_USER_VMGEXIT_REQ_CERTS_FLAGS_NOTIFY_DONE BIT(0)
This has no business being buried in a VMGEXIT_REQ_CERTS flags. Notifying
userspace that KVM completed its portion of a userspace exit is completely generic.
And aside from where the notification flag lives, _if_ we add a notification
mechanism, it belongs in a separate patch, because it's purely a performance
optimization. Userspace can use immediate_exit to force KVM to re-exit after
completing an exit.
Actually, I take that back, this isn't even an optimization, it's literally a
non-generic implementation of kvm_run.immediate_exit.
If this were an optimization, i.e. KVM truly notified userspace without exiting,
then it would need to be a lot more robust, e.g. to ensure userspace actually
received the notification before KVM moved on.
> + __u8 flags;
> + #define KVM_USER_VMGEXIT_REQ_CERTS_STATUS_PENDING 0
> + #define KVM_USER_VMGEXIT_REQ_CERTS_STATUS_DONE 1
This is also a weird reimplementation of generic functionality. KVM nullifies
vcpu->arch.complete_userspace_io _before_ invoking the callback. So if a callback
needs to run again on the next KVM_RUN, it can simply set complete_userspace_io
again. In other words, literally doing nothing will get you what you want :-)
> + __u8 status;
> + } req_certs;
> + };
> + };