From nobody Mon Apr 13 12:02:17 2026 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 D5D58C19F2D for ; Thu, 11 Aug 2022 21:06:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236496AbiHKVGe (ORCPT ); Thu, 11 Aug 2022 17:06:34 -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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Maxim Levitsky --- 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; } =20 + 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) =20 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 =3D=3D KVM_MP_STATE_RUNNABLE && !vcpu->arch.apf.halted); } --=20 2.31.1