Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3430508imw; Mon, 11 Jul 2022 08:30:26 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vxudLWgjhXeUtK8Lixkq5b4QG5Qy7ODU/Z9vKQE/P0EXFOfRI/2uRxNeRH/Q8AuHa12zKW X-Received: by 2002:a17:90a:5e0b:b0:1f0:5565:ee6e with SMTP id w11-20020a17090a5e0b00b001f05565ee6emr292531pjf.128.1657553426114; Mon, 11 Jul 2022 08:30:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657553426; cv=none; d=google.com; s=arc-20160816; b=VnxJ/T3cNU9K4iHThC8K+jSzirXxDlrJlJMhpGE7drjrNdveg4PtI2+iCNvsouU2Ra Nv+CVmQgPyojpn2XtINPm3bR3RSShHRzI2aEwSLdvOioQKVBxCgsn4WM9BFV7F4kGauc RzpYqLLWfSb1CPQr5+tdWj+4VZ8mJ9hhm8Fso2wNPPiWgTopM0hFBglMy8IfHH70x2Ze cODzEON6tQbC0iSR3l/JDi+t6bNhA0Uc2pD87eXe3WRGoQ4q3pXA+w3EibG2vMVjxMUi v1TfFCDv2a6+KJT2ned3y60wbrRQiUdqXP8Ily9jVn4hKqpDrHZdBGLg6qF9C1Iwfaip M/nQ== 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=rIXkilLQfgOtYP5k9HiwJCc9d8sCaA9fTRl5ayd2YSU=; b=n/10KwhJEMUztL5J78dD7QMNLPI0R/ATM01fPT9hl5S3z+PyC9lbTo81Zz7g5Te3i9 ANkQ/4JmJOi7xHoUqqSzJiRs5XPzjIa2oNwhJ4RIOc+1R4ouGJDj5kmgwuFGzzvShPgf lfTMIXD/zDXyptfNRCPM/7K2D7UrA5TKDYhWuMElmP4yOg2x0kEiHqJurFp48wP0UKe6 UOX/CcDjKHqTrfixl3rPtjwrtUmHIsuiC2ja//0eCK72CGFW5UKDeSXRFJ0u3+TOXEUQ PoiMQPwkdhWpdrjfrNndRlYNjtBRWbV+m9urx71dgQu+HDayihYp82VvMrmF84eNR3ak 5QHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oeRT2pv5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y30-20020a056a001c9e00b005284a18762csi8346225pfw.156.2022.07.11.08.30.13; Mon, 11 Jul 2022 08:30:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oeRT2pv5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S230518AbiGKPXB (ORCPT + 99 others); Mon, 11 Jul 2022 11:23:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229740AbiGKPW6 (ORCPT ); Mon, 11 Jul 2022 11:22:58 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F0875FAC9 for ; Mon, 11 Jul 2022 08:22:57 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id j3so5051315pfb.6 for ; Mon, 11 Jul 2022 08:22:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=rIXkilLQfgOtYP5k9HiwJCc9d8sCaA9fTRl5ayd2YSU=; b=oeRT2pv5AEhh1wGL8y0oDKKzorx5yHv43t0Sgjd9q8R/C7iaG9Y19Jj1kcyIz6Uc87 7zWboOGTmnx4k6fgHqwUheXR12fXtVNHmZQJe4P/zf2hlUgHvu8Y0WbhEBpnO5RBcnp/ CezdnHbdBvVV+A5ZKTnyKUInqz03eCw8xtgfopQOpiYxWFNrl0E2Eh8wYhFLWEEhJba9 jI4dqMNm2jUAZGFM7/Dng++crAgg1uvMRUmmkuEUGAuFiuOk9uk5UxDUB2TNpJSfyyRd YXZyivdkHGCKWvUOWyW/tUrPXMc7uHjrJO39KWSLaJ4BH1Lr0iGHnN1nTaZvfb3CAi+L z2ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=rIXkilLQfgOtYP5k9HiwJCc9d8sCaA9fTRl5ayd2YSU=; b=QFFtb+HEO8BIWDPec3yjWl506jKAmTaY5pN7w7VA62+JajDGgJvRcqqs6T7cWMmRFP Wj0FgDoNQHYUc/ag0fVbIcCliyYllHQ/QwdCJgz5ISVgNF0l3WRtzb+eWkf624LFjnU0 bC236HDSbeqcGfzLEbPDfcE9nrdxFkZ5/xCfoQeV6A9WwmOVL599CdZk9sElfL6wrPx6 GtV0zm3zhH1gTdWLo70CwYZ0jfVXNRVEEmG7tc8fZKgs+bn+c2AOiGSF6wPCP8y8U9wy AM/mvOh9aP6+3im6fuqJEIimJBW6OiIiU93YBxLmdGpKCDhaUvXMW7LP+PpR+untkpJK Ovhw== X-Gm-Message-State: AJIora9Cp/EZi5EurTSzVU9yNm4RGncoEOK5OH4oWlGd5PChLYhfAYVN zB6y3P38/e2LgJ66f5VX1OlNhg== X-Received: by 2002:a05:6a00:1501:b0:525:79a7:aa4 with SMTP id q1-20020a056a00150100b0052579a70aa4mr19083236pfu.44.1657552976720; Mon, 11 Jul 2022 08:22:56 -0700 (PDT) Received: from google.com (123.65.230.35.bc.googleusercontent.com. [35.230.65.123]) by smtp.gmail.com with ESMTPSA id 129-20020a630787000000b00415e89dd738sm3189922pgh.77.2022.07.11.08.22.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Jul 2022 08:22:56 -0700 (PDT) Date: Mon, 11 Jul 2022 15:22:52 +0000 From: Sean Christopherson To: Maxim Levitsky Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Oliver Upton , Peter Shier Subject: Re: [PATCH v2 17/21] KVM: x86: Morph pending exceptions to pending VM-Exits at queue time Message-ID: References: <20220614204730.3359543-1-seanjc@google.com> <20220614204730.3359543-18-seanjc@google.com> <5eaf496d71b2c8fd321c013c9d1787d4c34d1100.camel@redhat.com> <6fad40967afa4a7ed74c0f4158c8e841b1384318.camel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6fad40967afa4a7ed74c0f4158c8e841b1384318.camel@redhat.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Jul 10, 2022, Maxim Levitsky wrote: > On Thu, 2022-07-07 at 01:24 +0000, Sean Christopherson wrote: > > On Wed, Jul 06, 2022, Maxim Levitsky wrote: > > > Other than that, this is a _very_ good idea to add it to KVM, although > > > maybe we should put it in Documentation folder instead? > > > (but I don't have a strong preference on this) > > > > I definitely want a comment in KVM that's relatively close to the code. I'm not > > opposed to also adding something in Documentation, but I'd want that to be an "and" > > not an "or". > > Also makes sense. > > I do think that it is worthwhile to also add a comment about the way KVM > handles exceptions, which means that inject_pending_event is not always called on instruction > boundary. When we have a pending/injected exception we have first to get rid of it, > and only then we will be on instruction boundary. Yeah, though it's not like KVM has much of a choice, e.g. intercepted=>reflected exceptions must be injected during instruction execution. I wouldn't be opposed to renaming inject_pending_event() if someone can come up with a decent alternative that's sufficiently descriptive but not comically verbose. kvm_check_events() to pair with kvm_check_nested_events()? kvm_check_and_inject_events()? > And to be sure that we will inject pending interrupts on the closest instruction > boundary, we actually open an interrupt/smi/nmi window there. > > This is calling out something slightly different. What it's saying is that if > > there was a pending exception, then KVM should _not_ have injected said pending > > exception and instead should have requested an immediate exit. That "immediate > > exit" should have forced a VM-Exit before the CPU could fetch a new instruction, > > and thus before the guest could trigger an exception that would require reinjection. > > > > The "immediate exit" trick works because all events with higher priority than the > > VMX preeemption timer (or IRQ) are guaranteed to exit, e.g. a hardware SMI can't > > cause a fault in the guest. > > Yes it all makes sense now. It really helps thinking in terms of instruction boundary. > > However, that makes me think: Can that actually happen? I don't think KVM can get itself in that state, but I believe userspace could force it by using KVM_SET_VCPU_EVENTS + KVM_SET_NESTED_STATE. > A pending exception can only be generated by KVM itself (nested hypervisor, > and CPU reflected exceptions/interrupts are all injected). > > If VMRUN/VMRESUME has a pending exception, it means that it itself generated it, > in which case we won't be entering the guest, but rather jump to the > exception handler, and thus nested run will not be pending. Notably, SVM handles single-step #DBs on VMRUN in the nested VM-Exit path. That's the only exception that I can think of off the top of my head that can be coincident with a successful VM-Entry (ignoring things like NMI=>#PF). > We can though have pending NMI/SMI/interrupts. > > Also just a note about injected exceptions/interrupts during VMRUN/VMRESUME. > > If nested_run_pending is true, then the injected exception due to the same > reasoning can not come from VMRUN/VMRESUME. It can come from nested hypevisor's EVENTINJ, > but in this case we currently just copy it from vmcb12/vmcs12 to vmcb02/vmcs02, > without touching vcpu->arch.interrupt. > > Luckily this doesn't cause issues because when the nested run is pending > we don't inject anything to the guest. > > If nested_run_pending is false however, the opposite is true. The EVENTINJ > will be already delivered, and we can only have injected exception/interrupt > that come from the cpu itself via exit_int_info/IDT_VECTORING_INFO_FIELD which > we will copy back as injected interrupt/exception to 'vcpu->arch.exception/interrupt'. > and later re-inject, next time we run the same VMRUN instruction.