Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp477079pxv; Wed, 30 Jun 2021 09:49:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy0QU6vHDmIECKdc4FzoVsm1QwPHNfUpUz5vxNF2THYb2ooOAzKd5KooiV/ZUQvXnzT18kF X-Received: by 2002:a05:6638:299:: with SMTP id c25mr3241729jaq.59.1625071785981; Wed, 30 Jun 2021 09:49:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625071785; cv=none; d=google.com; s=arc-20160816; b=uRqpRq9QvYsKBi0Wmc7S4E5DCKuOtyvyXvjlR7M5OHUGagHUVuUPPo7fNZP3Pds+1u LATSEZpC5wH2l4y+mnD6zCZaNes5RKupZsxVl94pBuzo3hx823y5WDGA+iApt1KzHMAE NjEJTMb3jGk8NCPld09VfTouG/savnJtuUL9btAKMM39h6omg7UWTneHCiQvKtdcXZ0Q WmEpGzcVR+yb7DoCauYLYAkAorLGEmVmGOd+6p4u06jr7kCu5gkBGdyJD9JUDAJ1rKlc lKb0z4vdX/Q1stUVu8JvLmZs4lbL6UgucA1MxoXAtR6FHN1+ZnjhuPVkErCWBpUcSN64 /ASg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=hx4sHPmpea1ssyo//7YojpBEY/8Dcb5c5IPS9PMJhIk=; b=FLJVogFU4jmIcLJ95qC3uRIA1KGfLvRQjuCC/P34N9QI4ksvZspLzKVD5s6fX0oGMD nn1Fkl/61ns8F+nJKesxD3ZImM872TlwRjuSaquJKOSgvHJm6NQWRTPuKRpIW86uSPzJ dXWT+O2r5XuolbDgtRaNgUxiX/HJt30+MASeMy/g+OHoz1f1A+aOtDK1bsQZB5hHjKyq K6vxFqKT9DedctcLtTk/8KNlzGNt8be5+lO9GBMzrltwQXD5XfgxSK5c0NT4IdZVswZg H+tSGjjnbXgZyfo1fBfqvCjCCmymUhF+M6EwwLBRjJZeyE++E8n35Rlwf445z2GN18pq lTtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=jw4C41Uc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s13si11601711jaj.54.2021.06.30.09.49.33; Wed, 30 Jun 2021 09:49:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=jw4C41Uc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229963AbhF3QvS (ORCPT + 99 others); Wed, 30 Jun 2021 12:51:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229510AbhF3QvR (ORCPT ); Wed, 30 Jun 2021 12:51:17 -0400 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FAB4C0617A8 for ; Wed, 30 Jun 2021 09:48:47 -0700 (PDT) Received: by mail-pj1-x102c.google.com with SMTP id z3-20020a17090a3983b029016bc232e40bso1773224pjb.4 for ; Wed, 30 Jun 2021 09:48:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=hx4sHPmpea1ssyo//7YojpBEY/8Dcb5c5IPS9PMJhIk=; b=jw4C41UcaJLjRGuy4z9pSqd4r5Z5SnfuXfelNN0qNxjZCfFm4y61lAm2gS/JbmuC+y pTkSg/zoQc+ojr+tDHflc4aTT33Y3V7sq0sGWNZTKokoJ5v9gKSllQJKRrC7xN3lh6JU W+8sx3juiAIraD3gqInA/NklMtZezqx8B2Y/IiLA1wZ2Wu5YMQHLJMAcfhxBs+3QMpw1 sb3S1UBBzyXkKo1gMYg/bcAJh1hVJKrfeejbIcSMcoroznlfrf0slh32djIVKpx6b2ed Sz05nHV6NSS90mMWwNUWAaU4hS9LCxycbbmGB9JqSw/SbSOnVgcO5L9mQEFyxDwjecHT 4c+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=hx4sHPmpea1ssyo//7YojpBEY/8Dcb5c5IPS9PMJhIk=; b=P5YBTTCc/KNVQrKFwX8PeXhPcxgo4p2C7P2Gxn87uMOk7ZubHPH9lYOKJBBPkKmC8T F4b4rjGlfSTPVajjf75zO0NhozPZUe2R2YzS/0Qg3zlbPtFw4rRR8ZzngeIUbD5fNBs2 92BzAo9ExdY31jhxYVIbJHohXiDZxZw9aVZllEm2+1t7VN2Bdka8m50dAK8rxbTGgWS1 qj+eqaiGcy19rcg1TLNjUJuWoKvVEZB/D5/i8nEUy/fytNf9wl+BQCLj5zfTUo4fq5w4 5en3TM41U9PjNK1LIFzHroNmifmucTjCKDWwUg7Y4TlgowDugh1r3nJRR1YilH8n4CDM BCeg== X-Gm-Message-State: AOAM531mQEYkV13zNWzop6Or/40debYZ0Qd+ghRQ0bymXKmtDtO1rcq3 ZL7mnVLBWbFZJgjcdkEiSjvtHQ== X-Received: by 2002:a17:902:fe10:b029:127:6549:fe98 with SMTP id g16-20020a170902fe10b02901276549fe98mr32880513plj.25.1625071726557; Wed, 30 Jun 2021 09:48:46 -0700 (PDT) Received: from google.com (254.80.82.34.bc.googleusercontent.com. [34.82.80.254]) by smtp.gmail.com with ESMTPSA id v3sm22917472pfb.126.2021.06.30.09.48.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 09:48:45 -0700 (PDT) Date: Wed, 30 Jun 2021 16:48:42 +0000 From: David Matlack To: David Edmondson Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Thomas Gleixner , Borislav Petkov , Vitaly Kuznetsov , Joerg Roedel , Ingo Molnar , Sean Christopherson , Wanpeng Li , Jim Mattson , "H. Peter Anvin" , Paolo Bonzini , x86@kernel.org, Joao Martins Subject: Re: [PATCH 2/2] KVM: x86: On emulation failure, convey the exit reason to userspace Message-ID: References: <20210628173152.2062988-1-david.edmondson@oracle.com> <20210628173152.2062988-3-david.edmondson@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210628173152.2062988-3-david.edmondson@oracle.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jun 28, 2021 at 06:31:52PM +0100, David Edmondson wrote: > To aid in debugging. Please add more context to the commit message. > > Suggested-by: Joao Martins > Signed-off-by: David Edmondson > --- > arch/x86/kvm/x86.c | 23 +++++++++++++++++------ > include/uapi/linux/kvm.h | 2 ++ > 2 files changed, 19 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 8166ad113fb2..48ef0dc68faf 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -7455,7 +7455,7 @@ void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) > } > EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt); > > -static void prepare_emulation_failure_exit(struct kvm_vcpu *vcpu) > +static void prepare_emulation_failure_exit(struct kvm_vcpu *vcpu, uint64_t flags) > { > struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; > u32 insn_size = ctxt->fetch.end - ctxt->fetch.data; > @@ -7466,7 +7466,8 @@ static void prepare_emulation_failure_exit(struct kvm_vcpu *vcpu) > run->emulation_failure.ndata = 0; > run->emulation_failure.flags = 0; > > - if (insn_size) { > + if (insn_size && > + (flags & KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES)) { > run->emulation_failure.ndata = 3; > run->emulation_failure.flags |= > KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES; > @@ -7476,6 +7477,14 @@ static void prepare_emulation_failure_exit(struct kvm_vcpu *vcpu) > memcpy(run->emulation_failure.insn_bytes, > ctxt->fetch.data, insn_size); > } > + > + if (flags & KVM_INTERNAL_ERROR_EMULATION_FLAG_EXIT_REASON) { This flag is always passed so this check if superfluous. Perhaps change `int flags` to `bool instruction_bytes` and have it control only whether the instruction bytes are populated. > + run->emulation_failure.ndata = 4; > + run->emulation_failure.flags |= > + KVM_INTERNAL_ERROR_EMULATION_FLAG_EXIT_REASON; > + run->emulation_failure.exit_reason = > + static_call(kvm_x86_get_exit_reason)(vcpu); > + } > } > > static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) > @@ -7492,16 +7501,18 @@ static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) > > if (kvm->arch.exit_on_emulation_error || > (emulation_type & EMULTYPE_SKIP)) { > - prepare_emulation_failure_exit(vcpu); > + prepare_emulation_failure_exit( > + vcpu, > + KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES | > + KVM_INTERNAL_ERROR_EMULATION_FLAG_EXIT_REASON); > return 0; > } > > kvm_queue_exception(vcpu, UD_VECTOR); > > if (!is_guest_mode(vcpu) && static_call(kvm_x86_get_cpl)(vcpu) == 0) { > - vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; > - vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; > - vcpu->run->internal.ndata = 0; > + prepare_emulation_failure_exit( > + vcpu, KVM_INTERNAL_ERROR_EMULATION_FLAG_EXIT_REASON); Should kvm_task_switch and kvm_handle_memory_failure also be updated like this? > return 0; > } > > diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h > index 68c9e6d8bbda..3e4126652a67 100644 > --- a/include/uapi/linux/kvm.h > +++ b/include/uapi/linux/kvm.h > @@ -282,6 +282,7 @@ struct kvm_xen_exit { > > /* Flags that describe what fields in emulation_failure hold valid data. */ > #define KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES (1ULL << 0) > +#define KVM_INTERNAL_ERROR_EMULATION_FLAG_EXIT_REASON (1ULL << 1) > > /* for KVM_RUN, returned by mmap(vcpu_fd, offset=0) */ > struct kvm_run { > @@ -404,6 +405,7 @@ struct kvm_run { > __u64 flags; > __u8 insn_size; > __u8 insn_bytes[15]; > + __u64 exit_reason; Please document what this field contains, especially since its contents depend on AMD versus Intel. > } emulation_failure; > /* KVM_EXIT_OSI */ > struct { > -- > 2.30.2 >