From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A268C32771 for ; Tue, 16 Aug 2022 23:45:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237701AbiHPXpR (ORCPT ); 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-Google-Smtp-Source: AA6agR76/9iHZUvxlYJRUAl4HHUCIQebjkGRgwgIoQ3QmecE2Cr0/B8jZ0or2nfgmiWsyUzwV3wXNQ== 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> 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 :-(