Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp4201851rwb; Tue, 16 Aug 2022 16:59:38 -0700 (PDT) X-Google-Smtp-Source: AA6agR7XvBeKTmDJb3WLq3+48+lbOAD27LCw+3xIs5BT1r/gQuhvNwSXq7CHUimdJDZZH66S3z1S X-Received: by 2002:aa7:dbd7:0:b0:43e:8c3b:f444 with SMTP id v23-20020aa7dbd7000000b0043e8c3bf444mr20335795edt.83.1660694378325; Tue, 16 Aug 2022 16:59:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660694378; cv=none; d=google.com; s=arc-20160816; b=CtgiVMOn3CyW5laE/iYh4dB6f0aGjOQ3mWYsKy5g6BFysIejgJj10AFGYj7nIWsyih whsyYzIVsUdRMemXIr6ul72a/NpsIBQ6+8S303KedAHeTFTvJQTmlbTZwh6DBvd7ZC+r ru8jc/bUWlfC8sWYX6yLZ5Plw+YigFP7uX/lbhiBaUOUa5k1frJD2sEZsknbk+X7wURe kyN5RThSnih4SzXk8NRctek1f1pKDf6c4NQ+aDpy5txHA1xICSR75d3kmfs6fSHONSaK 37V18Kc4r+IFszmVLHNabx9WxN667KRpsk1+foZOx3xK6W4XVptLm3BgfpohsGd2OZoJ r8jw== 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=T8Wd5ecB9QDTm6Zwnt9WBgEsFx0O5umnw625jWHrX/M=; b=PKL3j2XooL3OKo1Z6xE/FPjvpCA9mCg14qn+0RP4wpuUX8pdUgRWutr+lqAuPOgdwr 0haROEGPLw0sfLZt5MFklk0KSbVMc+nnP7Krp2SUDXbj4yzO74V8WnhC/lcwglWRPBGn 4Due9nJyPNXdwx6LZqiNDg4on962pbYtvFlP5kydF0n8uvUKRiK9b4IAIFeasF3+qL8H 6WCM3cM8kHM1h/TgiIxeyj3OO7bv9LVEheIbmS0r3KEDOU5VdgI/9fmcrrnF0t0pGc5f FCsn4eesMGr6C/IbVHQlj/DLBMqsJElvtsBrAcjVNZB36i8zT3BZ0v6SMBiUB1IQUw06 Hi7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jumeSEYx; 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 ho39-20020a1709070ea700b007307bd9033esi13793925ejc.972.2022.08.16.16.59.11; Tue, 16 Aug 2022 16:59:38 -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=jumeSEYx; 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 S237698AbiHPXpR (ORCPT + 99 others); Tue, 16 Aug 2022 19:45:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229675AbiHPXpP (ORCPT ); Tue, 16 Aug 2022 19:45:15 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00279915FC for ; Tue, 16 Aug 2022 16:45:14 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id ch17-20020a17090af41100b001fa74771f61so2440499pjb.0 for ; Tue, 16 Aug 2022 16:45:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc; bh=T8Wd5ecB9QDTm6Zwnt9WBgEsFx0O5umnw625jWHrX/M=; b=jumeSEYxVa1d9kq7i5FFS4qyMGOZkNP0043rIjIqRE3Vugi8nADSvyvpmv3f1SvV5Y eIWNRT4/oHQZ8V9HHSEjEBd/Sui40yeXHtlb5JS5tB2nCSB2F6r+pVMiXXXXHulkDarn Z4rsOg7u4h/dG6fFXX9RtjtI40Slm6UulsbB/8Z6IYqv2gNOez8PhJ4G10/WdyrK+7yK 25juHiVzZNSBYfzjdikFcVDPhGAaQs/KVum4Rco1DRXvx2x1vj09a1TQfRl6WIMNbznT FrkKJpxO8r+ywD5UC4+KWiYIoqJhc8i1SaN1burCu2r/nCF0IoEq366ud58eY3AueX7l 3cug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc; bh=T8Wd5ecB9QDTm6Zwnt9WBgEsFx0O5umnw625jWHrX/M=; b=370JwZuN3GnikI4g+9ZaDIXOmT6mQ2hoMc3PmCVCOGmQoRxpCNdgWVfce+cJahBzjC yhghiHZHrjQxqun/SxOSpRX1Ihdy654s39e0/Huc3PKdkCz8nf/7ri8qZl9LQHmuVWP+ 7KvZK9nzI2UnYUY+kjqMhjSevdXaR85djX6tJ359qpuYiLE+f27HVVAff5UknCBs/WHA XJFh/ztop71k7f5DLkzVTp/5qdqVR/NiQCXMOlJwAv8if8nsDnuWrEqwubZRH4dJfIHk ybk2IOuMsOZOZ3wcuGdwCUCqM4ezoMuEedsa+3pSQFq2ZnFJH66K+MqVtDffBMYpH/OF OHVw== X-Gm-Message-State: ACgBeo0UeJYnu6Gi8Wr8/TFuqcHj9gyj21IxcL5jY51aU7b+MGwXyhro poGIp9M7aKAr2EJdZZ/bjLV7Vw== X-Received: by 2002:a17:902:f68f:b0:171:55f0:9062 with SMTP id l15-20020a170902f68f00b0017155f09062mr23600649plg.18.1660693512929; Tue, 16 Aug 2022 16:45:12 -0700 (PDT) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id x29-20020aa7941d000000b00525343b5047sm8971175pfo.76.2022.08.16.16.45.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Aug 2022 16:45:12 -0700 (PDT) Date: Tue, 16 Aug 2022 23:45:08 +0000 From: Sean Christopherson To: Paolo Bonzini Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, mlevitsk@redhat.com, vkuznets@redhat.com Subject: Re: [PATCH v2 9/9] KVM: x86: never write to memory from kvm_vcpu_check_block Message-ID: References: <20220811210605.402337-1-pbonzini@redhat.com> <20220811210605.402337-10-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220811210605.402337-10-pbonzini@redhat.com> X-Spam-Status: No, score=-14.4 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,FSL_HELO_FAKE,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=no 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 Thu, Aug 11, 2022, Paolo Bonzini wrote: > kvm_vcpu_check_block() is called while not in TASK_RUNNING, and therefore > it cannot sleep. Writing to guest memory is therefore forbidden, but it > can happen on AMD processors if kvm_check_nested_events() causes a vmexit. > > Fortunately, all events that are caught by kvm_check_nested_events() are > also recognized by kvm_vcpu_has_events() through vendor callbacks such as > kvm_x86_interrupt_allowed() or kvm_x86_ops.nested_ops->has_events(), so > remove the call and postpone the actual processing to vcpu_block(). > > Signed-off-by: Paolo Bonzini > --- > arch/x86/kvm/x86.c | 14 +++++++++++--- > 1 file changed, 11 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 5e9358ea112b..9226fd536783 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -10639,6 +10639,17 @@ static inline int vcpu_block(struct kvm_vcpu *vcpu) > return 1; > } > > + if (is_guest_mode(vcpu)) { > + /* > + * Evaluate nested events before exiting the halted state. > + * This allows the halt state to be recorded properly in > + * the VMCS12's activity state field (AMD does not have > + * a similar field and a vmexit always causes a spurious > + * wakeup from HLT). > + */ > + kvm_check_nested_events(vcpu); Formatting nit, I'd prefer the block comment go above the if-statement, that way we avoiding debating whether or not the technically-unnecessary braces align with kernel/KVM style, and it doesn't have to wrap as aggressively. And s/vmexit/VM-Exit while I'm nitpicking. /* * Evaluate nested events before exiting the halted state. This allows * the halt state to be recorded properly in the VMCS12's activity * state field (AMD does not have a similar field and a VM-Exit always * causes a spurious wakeup from HLT). */ if (is_guest_mode(vcpu)) kvm_check_nested_events(vcpu); Side topic, the AMD behavior is a bug report waiting to happen. I know of at least one customer failure that was root caused to a KVM bug where KVM caused a spurious wakeup. To be fair, the guest workload was being stupid (execute HLT on vCPU and then effectively unmap its code by doing kexec), but it's still an unpleasant gap :-(