From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yx1-f54.google.com (mail-yx1-f54.google.com [74.125.224.54]) (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 4386D3BFAFA for ; Fri, 19 Jun 2026 20:31:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781901120; cv=none; b=AFgDY8sBXJcaZhiDD/OoDXA34hvymxmbU8r7p8eG8ipKc/bM6oVxBdlcPd3wmvGQzdJ67g9MZToafobApFwgomaJMxvTTj59ukvdjR2e2JxDYzuN5fADUA09gtJQS8x82YBZEw16ugWFOsGEZZN7fS18PiTCzOOWVgU6EzZv6Gk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781901120; c=relaxed/simple; bh=u3DS0RW8qwKnPXbLqw4zkRsFPGr0SmLyS4ByHV94B/Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CkbFio0WkCtCNpBcR4KGRcD9cBkgDL+2lPLlSNJTvdvTD4cd4iq47zgnpySGSW5F54EBw07N8vYFB86IqRIor68v3SdB/OBzYN09QHi8Gyf16sGKfW70KJJXRU3ajP16ddvk5KEUVU9wB3GSt15n7bcvfp9QDI4lM5D1s46fC9M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IOi3drpz; arc=none smtp.client-ip=74.125.224.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IOi3drpz" Received: by mail-yx1-f54.google.com with SMTP id 956f58d0204a3-662df6a4fe4so3086298d50.2 for ; Fri, 19 Jun 2026 13:31:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781901115; x=1782505915; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bd1mWB6GNyNzgHCKDKsPJpGXztRF54FOFrBz+pWkAI8=; b=IOi3drpziRfoWd/KJT3NdR34UPQHXXrOZkgOkvG3WnQ9MXdrN+coIQyYH+93SERPpt iYTzXqmM+X4eZ7Dywh9fwj53nXqGG6+TNT12Wtamj4FHUns1o36g5ssyr6Gh7KYRwoIw a8HZNY11mj9SAleCt56qNZyL5Cx4qg0Pn9eXSdlyDEbGsDk4eQ3PZ6pCRi3iOLxGWjsJ 4S0BJdstjC5b9cIuCarC3xa+RmMaoRpX3OLtgiVgp4bQt3fIq75R2JXStuV6aIB/pmfU kVtvTByA9A7Ofsk3s1RrpairRXAj0t6MUT2d452g4exk8yheSAeaIjXPz14n6j2cRwl/ xW6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781901115; x=1782505915; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=bd1mWB6GNyNzgHCKDKsPJpGXztRF54FOFrBz+pWkAI8=; b=YVWYD3yepQ5k+ZnJ1YY70g/4zDpsgOQcujTKFcz9FSkVBblU9OuqXPGC4CGsNAqeYp ajiy7MiXvRQ/PUIDav9vcnlGUPwnYfzbl+eeACcTFYUlqQlmkXWe8SnCeT8vnBmIvRuo kc7UYauqnrEbjRHcmFvWsyA9Lr3B54ApiO2V3WQ5cIsHL4KUDj8aw7jRhVyViF1u6D9l y4Uri95OEOSSSuAiKvQcu0kcI3QbR9p1fjwvWraEJUVKbXp6MHuvnRC9UKG4oy5EKCnA d/pj+PcTZfk5tGBxKHBLguZNjdFY6/o95UL7GvtFRODaNDMDNhQKG2xoWMuCcEuoiOgv XmYw== X-Forwarded-Encrypted: i=1; AFNElJ+ADOhZY8S3kUmzGa4Lg9UsoWTIg62YodQ5/lBlAopBAW+o8CsdTcqhe1kL6wf0SwD92LXrMs5KlsT+afU=@vger.kernel.org X-Gm-Message-State: AOJu0YxPuJp/eGLElFziU/b9Deb/e4sC0hMSXWvQWcIEfsMUgMURNpPn kN0ioXQ9rnq33V/zVQIbDQ0FXX1g7Oc9NKPslUYVTvkraHnOUvWt4qoP X-Gm-Gg: AfdE7cm1uQGTByQs+U4oO9zVJ/k6VH1to0esOgqGfIsrrRHMxwExbLh4vvwGqKjfZD0 0f3TJWyuLdxLW/8cKlQ+lgdG/Ot53B5umlt+unLV+C1S4lVpQY1+y2ILzxm9nvZVchPjdPu4YFz K5jXA2Zbf2yu+3aTKtVoxE3mZyHXiR4c1Hhgrt3SaRVyLNFuzXp0KUHX0hUOzynv+P9fYgmNZBb TBHiu64+BV0mibi6AlEOJE7WBVu1mZ+doAyvhqdnT+gGsR9XNO3EZxwMx+k2w1wQWBGo2g+oVdm lQAjhIG9ihn6f6hiddgqCBNH/5IogbkDUQ/pJO1jfXFqYQsUOELpbLPEveSHjRlcipZ5MzQLwTb 2AIMDeQMYTUSfQqVN2iDHzES52n4tMnm1tX889w75xgN3v15Yk3NeCnSVkutlsAnLBYFC0p/QKK 1q24HSDf97YOzY7Rw= X-Received: by 2002:a05:690e:e85:b0:65d:7db3:5587 with SMTP id 956f58d0204a3-66302f041e5mr4221049d50.4.1781901114833; Fri, 19 Jun 2026 13:31:54 -0700 (PDT) Received: from TurinLinux.. ([37.19.212.13]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8df7f015805sm11106986d6.1.2026.06.19.13.31.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jun 2026 13:31:54 -0700 (PDT) From: Nicholas Dudar To: stable@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, gregkh@linuxfoundation.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, 0wn@theori.io, mlevitsk@redhat.com, jmattson@google.com, Nicholas Dudar Subject: [PATCH v2 6.1.y 1/3] KVM: nVMX: Add a helper to get highest pending from Posted Interrupt vector Date: Fri, 19 Jun 2026 16:31:05 -0400 Message-Id: <20260619203107.2752678-2-main.kalliope@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260619203107.2752678-1-main.kalliope@gmail.com> References: <20260619203107.2752678-1-main.kalliope@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sean Christopherson commit d83c36d822be44db4bad0c43bea99c8908f54117 upstream. Add a helper to retrieve the highest pending vector given a Posted Interrupt descriptor. While the actual operation is straightforward, it's surprisingly easy to mess up, e.g. if one tries to reuse lapic.c's find_highest_vector(), which doesn't work with PID.PIR due to the APIC's IRR and ISR component registers being physically discontiguous (they're 4-byte registers aligned at 16-byte intervals). To make PIR handling more consistent with respect to IRR and ISR handling, return -1 to indicate "no interrupt pending". Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20240607172609.3205077-2-seanjc@google.com Signed-off-by: Sean Christopherson [ Nicholas Dudar: backport to 6.1.y. 6.1.y defines struct pi_desc in posted_intr.h and predates the move to , so the helper and the include go in posted_intr.h. ] Signed-off-by: Nicholas Dudar --- arch/x86/kvm/vmx/nested.c | 5 +++-- arch/x86/kvm/vmx/posted_intr.h | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index bdc462944..7d8e18dbe 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -12,6 +12,7 @@ #include "mmu.h" #include "nested.h" #include "pmu.h" +#include "posted_intr.h" #include "sgx.h" #include "trace.h" #include "vmx.h" @@ -3818,8 +3819,8 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) if (!pi_test_and_clear_on(vmx->nested.pi_desc)) return 0; - max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); - if (max_irr != 256) { + max_irr = pi_find_highest_vector(vmx->nested.pi_desc); + if (max_irr > 0) { vapic_page = vmx->nested.virtual_apic_map.hva; if (!vapic_page) goto mmio_needed; diff --git a/arch/x86/kvm/vmx/posted_intr.h b/arch/x86/kvm/vmx/posted_intr.h index 269920765..88cea0dac 100644 --- a/arch/x86/kvm/vmx/posted_intr.h +++ b/arch/x86/kvm/vmx/posted_intr.h @@ -2,6 +2,8 @@ #ifndef __KVM_X86_VMX_POSTED_INTR_H #define __KVM_X86_VMX_POSTED_INTR_H +#include + #define POSTED_INTR_ON 0 #define POSTED_INTR_SN 1 @@ -103,4 +105,12 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, bool set); void vmx_pi_start_assignment(struct kvm *kvm); +static inline int pi_find_highest_vector(struct pi_desc *pi_desc) +{ + int vec; + + vec = find_last_bit((unsigned long *)pi_desc->pir, 256); + return vec < 256 ? vec : -1; +} + #endif /* __KVM_X86_VMX_POSTED_INTR_H */ -- 2.34.1