Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1192964lqt; Fri, 7 Jun 2024 10:27:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVpuIzktMzZE4p13QrzVQi9NL7uks45DAYy5xPbYElMbtovZgzMdFnHC9WUUCnwSd2aqKw9+49B7PHMd7N3EeMrxVNpSsy0jPXK4irz2Q== X-Google-Smtp-Source: AGHT+IG+HeibAZa5LbsH8kzKlRv7sIYruwLrFw8g4c+DViqFJ0jOacGTyZ6BB7rGOpoft+fu1pHi X-Received: by 2002:a17:90a:6b43:b0:2c1:dc60:d933 with SMTP id 98e67ed59e1d1-2c299a23347mr9239393a91.19.1717781268683; Fri, 07 Jun 2024 10:27:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717781268; cv=pass; d=google.com; s=arc-20160816; b=l33BhyAj38VgeVfo2N9bIsTYunQhLQgUTUsJd8CYplo/6q9o5EIKhVqk3YHTv6IEPz EmMOF2PpRoB4jFFYzCQ67cQBD5VnWtfRaIIhIXpWKiRfiNFAR35m9IpGmhe5MD89codL mPZRCXFt/Zg/YjPiGGwCwu7MTa+OAOfBbBLo+8bLh8gervuoYbCVmkEjsRkntxHet7fu ux/uiSOqV3pvh5j4c87FO3LsW6IsUAugsF593FKtfh8r7N9XBIxgpyFRVi6R6TtBYfaH ebA65wZjKzjlZQz9Gx7sXN/HlTDSBawqaVuFLuJyTki3TAD4AwFnpApDBZa84K5xMIdL eINA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=uvcmSFh6UDPlnnaoqVbG3z1+eul2qj/DkJROW9dPPPM=; fh=sAXiYWp0V1C+V26R95+tnL6trTvH6lFUmGvJgUDYPHo=; b=w9tGQetQLoiQNjq9T3ou87yVQz0X330DviOZ17hD/L3dhNOZap8uSXsNyfFCXOaRll Y3oeFe+NRCW4Q4Q8ttCWTAPM4r6OleSPbVlxhTRjlvlG0LnWCQvtz1r+jibZr2HnGlyz JE1GWzh4yH5ETAtc0ANr/hTFHxN3sCiPQP5/vtFHmIU9CbsbFnUd1gtsYtV9BaHLaAtf 5OvqgI9u61uhpQGhyFW/v+8pDLjIl2sPE3RjoD889TvKpjjq81ZKMWQiuWOFTNAYsPdp NEcaiu97h6pEcBo91jPRiAYhnBuFvB7tMgCU1ISRdwESg9FekZY+ReXgobo8i2se0dxO BZqQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vcJDljGS; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-206543-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206543-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2c2d113403bsi639487a91.132.2024.06.07.10.27.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 10:27:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206543-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vcJDljGS; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-206543-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206543-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 4F44D288B20 for ; Fri, 7 Jun 2024 17:27:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 24BFA19ADA8; Fri, 7 Jun 2024 17:26:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vcJDljGS" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3D6419CD1F for ; Fri, 7 Jun 2024 17:26:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717781182; cv=none; b=Xorb6ZDsnTCRBkozxCU3/FKuJluxgdPZlJXe/5QB6CqWIZn7gdfeDCGJ9tLBs8XYGcxj+8cmnaz7kWclQml6/OJlH1y9G4IcJFHIzWBhzo7u4Xeq7XCwASoo+9NWK6XxJp40znyekdzNxHOHED10tGslHopb0jY/jceb00tRqyc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717781182; c=relaxed/simple; bh=EkyMwxgguAdShYFUA0GXN+UsQ76+bfUZ2h4ecWNIerk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rsIlohsEQQ77/4XD1Ow0g66wUicapHV4+xxWoBeyz3FiBQFZwU+jEzKIr/+QFot46BP0dhcqGcLDstf3lh2rmbq0fu6KkEjJ2QjDHcI9vO4K3kGYkCNZ4/Tl/4OY3FzZauexWLwIVtj2gjeUNvisDAgFaeCpXibT8KJbZM2ZI3c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vcJDljGS; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-62a248f46aaso37927097b3.0 for ; Fri, 07 Jun 2024 10:26:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717781180; x=1718385980; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=uvcmSFh6UDPlnnaoqVbG3z1+eul2qj/DkJROW9dPPPM=; b=vcJDljGSZJDzjPb8utCkJ4FypfZ29PYbOLV3rfsiWDB3VlWaK/kucDGxt659/RWNmg Al4UKWV6aZMaNMUwLIfOssKXD3QPgT7a6FXjZDJL04wKIve3EzPnPcnJk3S6wlhSXC0r 2oJkTzGX3o6/CZaAIHT9DpvIk2rddXXoBDST+t+trdnLR8ORMD3wBr2JJ6oTcsQJsZ2A +H4cQoOEzZGkBg+K+Wqy0myjDeBEfOhjmRYO1edG1kSgWh+C5V3Kf2ukHJV/0xIHGHQt LDkfdTbz2UxKXtvBVzzWo511jsGk2GUcB+LYDJGzyqUrYe1kdkdEmyeom/4HhKLtysg9 LO5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717781180; x=1718385980; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uvcmSFh6UDPlnnaoqVbG3z1+eul2qj/DkJROW9dPPPM=; b=Q6Q4PdCh6eHXWWsTcv2HMiM8QZRmLGcGNKlSkU6BJJr7KcBx1LqPUS+2orN0BBvokj 4HuSbUgrwFmvdHjEvia1e/u0xkc8MjJEzXuD7EdIUWbsekiNrzlOx8RXF0fcH4s6etY9 kg0iJ9xvVTmxuwdiL168ZmsrTeCYz2KpbK6HnFWMTojD+lmwvVCyPVQaG0JG7fPeTmkM 3ymwVIVnx7RrTiBUtbIlJXvUKkUbpRUw1/T+xn+dlg9oH2+ibvmLl3ejmF3OldzOsY65 g0dSfhsOdCvH5Ndxg39kY5xNdsHIITcv6iQnG/sP2VSOaiCSIKmHlXzWZbcb7g1vc8OA MQLQ== X-Forwarded-Encrypted: i=1; AJvYcCUqQkyCA1QzZh87NEU54+H4Q/iddLTDOPulM70zhxz4gGqULeyel2589iPYXn5xEoN69dY7JEMO19o7omexZ3/fWRSEmjvmWfpZ+/U4 X-Gm-Message-State: AOJu0YwGt2EFT+dGVgt9txuJmt79hVwuDJzne2A40MHwxH6wOoUZU4qi lEHDlN0S/SzHLKtKZckHfGNvvxiYPtxHBIWzaiqq0331hn2PnFuV3FT2lxAMZCnEZJwestyoRc/ XlQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:f12:b0:627:a671:8805 with SMTP id 00721157ae682-62cd558cf7cmr8461517b3.3.1717781179894; Fri, 07 Jun 2024 10:26:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 7 Jun 2024 10:26:07 -0700 In-Reply-To: <20240607172609.3205077-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240607172609.3205077-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240607172609.3205077-5-seanjc@google.com> Subject: [PATCH 4/6] KVM: nVMX: Check for pending posted interrupts when looking for nested events From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Jim Mattson Content-Type: text/plain; charset="UTF-8" Check for pending (and notified!) posted interrupts when checking if L2 has a pending wake event, as fully posted/notified virtual interrupt is a valid wake event for HLT. Note that KVM must check vmx->nested.pi_pending to avoid prematurely waking L2, e.g. even if KVM sees a non-zero PID.PIR and PID.0N=1, the virtual interrupt won't actually be recognized until a notification IRQ is received by the vCPU or the vCPU does (nested) VM-Enter. Fixes: 26844fee6ade ("KVM: x86: never write to memory from kvm_vcpu_check_block()") Cc: stable@vger.kernel.org Cc: Maxim Levitsky Reported-by: Jim Mattson Closes: https://lore.kernel.org/all/20231207010302.2240506-1-jmattson@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 9099c1d0c7cb..3bac65591f20 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4034,8 +4034,40 @@ static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu) static bool vmx_has_nested_events(struct kvm_vcpu *vcpu, bool for_injection) { - return nested_vmx_preemption_timer_pending(vcpu) || - to_vmx(vcpu)->nested.mtf_pending; + struct vcpu_vmx *vmx = to_vmx(vcpu); + void *vapic = vmx->nested.virtual_apic_map.hva; + int max_irr, vppr; + + if (nested_vmx_preemption_timer_pending(vcpu) || + vmx->nested.mtf_pending) + return true; + + /* + * Virtual Interrupt Delivery doesn't require manual injection. Either + * the interrupt is already in GUEST_RVI and will be recognized by CPU + * at VM-Entry, or there is a KVM_REQ_EVENT pending and KVM will move + * the interrupt from the PIR to RVI prior to entering the guest. + */ + if (for_injection) + return false; + + if (!nested_cpu_has_vid(get_vmcs12(vcpu)) || + __vmx_interrupt_blocked(vcpu)) + return false; + + if (!vapic) + return false; + + vppr = *((u32 *)(vapic + APIC_PROCPRI)); + + if (vmx->nested.pi_pending && vmx->nested.pi_desc && + pi_test_on(vmx->nested.pi_desc)) { + max_irr = pi_find_highest_vector(vmx->nested.pi_desc); + if (max_irr > 0 && (max_irr & 0xf0) > (vppr & 0xf0)) + return true; + } + + return false; } /* -- 2.45.2.505.gda0bf45e8d-goog