Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1022940rwb; Thu, 11 Aug 2022 14:22:36 -0700 (PDT) X-Google-Smtp-Source: AA6agR7OQs4LYYZAN12vix7VJ+NrrnK1XaQahHV1DgCj8GwIhhBr+FTOn/MqvHiYVeii6KfXUdOI X-Received: by 2002:a17:90b:17c8:b0:1f5:31c:4d93 with SMTP id me8-20020a17090b17c800b001f5031c4d93mr10709351pjb.53.1660252956362; Thu, 11 Aug 2022 14:22:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660252956; cv=none; d=google.com; s=arc-20160816; b=nRjeZDCBPxy5xjwB91MoDL8yO1tTIb9dkJ6hrXtYdJAc+q2l/4Uq06xdtshlZDsU0c O8eoeBdV/urQooRQ5KD1X8f9bEcna5nzWfIY7ZbKCnPknotWxbFpyOUoLL6wLEDYbzFN X5k5FsawWa6epM9ZkgDWyy3hjLgCLjMtlrcvxxF/cJgVbpA+GlzTGSjlwJhDq0NxRapc NEJxEBjZnk9p1JOANaNTaxXicOeahYyvBRTmR3x3b5xbYfC5JkKFmq4K2WfxtxsJJEma LNhVP5zHmfihYwtIkUk6uiVqEu3K8lzaxRW/z5FmKrOoUv58QK9wSdkWLfj+myiGZb0r lbvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=e3seP/frNyLsAlU7K9H6qLEBTEwpcXPprkWdZzIof8M=; b=BwkD8nO1EHz09kwTVES0eyN+lp957H7ZIHg2AxxcFlZtboaPDaV8OMZNLGBH4jFLyV hMvjWxHzlCzlM4hNsF7IioXadN8J/iWrndDMp5lpCkEXRiLxAFDsYTZUoUqNR0d2HsBr MK7Rs+SX0bnvki4u4Wu2Rqkne/0hmxBECxQkYXGjwV5qN4PPZ3kmw9VzC3PfMc02DVph 2kYk9wBLR4PzICh4K7wC3Vj9P3KoCn59grf5kiv+AD8OJBvRAtD7OivqAf6DpT6+/LFj gjcOFepjPbgTte+yTIVdB+8hXhDlgCmA323Y0HSmcx1hi/vrDPoEVda5HPVDqabV9Xl3 mILw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QZ7i94ca; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 13-20020a63114d000000b004129a78936fsi441483pgr.1.2022.08.11.14.22.21; Thu, 11 Aug 2022 14:22:36 -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=@redhat.com header.s=mimecast20190719 header.b=QZ7i94ca; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236374AbiHKVGd (ORCPT + 99 others); Thu, 11 Aug 2022 17:06:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236331AbiHKVGO (ORCPT ); Thu, 11 Aug 2022 17:06:14 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F283D89802 for ; Thu, 11 Aug 2022 14:06:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660251971; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e3seP/frNyLsAlU7K9H6qLEBTEwpcXPprkWdZzIof8M=; b=QZ7i94casfweiNk+lOHDNpEaCbYaZ5ASrnvW5H3kIrM/mCe2/Jolwq1Gyy7dXkSVoK/kpo HMDyBur42iPuvpOj2Jz/HtkAdrxlf1M9Wz8vFISUWqNNKazS/7wUSszKaZI9qhmm1gZP7g Z6nX2XG7o7o0USLIOE2N83b95nSKRuQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-19-HKzmCSrrPo6qeeTQL5u-fg-1; Thu, 11 Aug 2022 17:06:08 -0400 X-MC-Unique: HKzmCSrrPo6qeeTQL5u-fg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0ED9D943206; Thu, 11 Aug 2022 21:06:08 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id DCB3C492C3B; Thu, 11 Aug 2022 21:06:07 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, mlevitsk@redhat.com, vkuznets@redhat.com Subject: [PATCH v2 9/9] KVM: x86: never write to memory from kvm_vcpu_check_block Date: Thu, 11 Aug 2022 17:06:05 -0400 Message-Id: <20220811210605.402337-10-pbonzini@redhat.com> In-Reply-To: <20220811210605.402337-1-pbonzini@redhat.com> References: <20220811210605.402337-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 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); + } + if (kvm_apic_accept_events(vcpu) < 0) return 0; switch(vcpu->arch.mp_state) { @@ -10662,9 +10673,6 @@ static inline int vcpu_block(struct kvm_vcpu *vcpu) static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) { - if (is_guest_mode(vcpu)) - kvm_check_nested_events(vcpu); - return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && !vcpu->arch.apf.halted); } -- 2.31.1