Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1192324lqt; Fri, 7 Jun 2024 10:26:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU999XODyDnaNSr9iptCW9UNSuy6RMIMEmNXru2wCdjJAyh6RIFY4qF197B/vSAeB5Fy08A+ljBfqgYdGOuSFKLQi0ZzQkoLhPbkyOdjA== X-Google-Smtp-Source: AGHT+IFnBTTpxCSSNhj6b22Yyp3LawIStC2SqAvwKEcng3jcDH03YOjSIzXZh16upiaohOxC5w1/ X-Received: by 2002:a05:622a:1310:b0:43d:fa59:6461 with SMTP id d75a77b69052e-44041c874b1mr38587521cf.39.1717781201514; Fri, 07 Jun 2024 10:26:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717781201; cv=pass; d=google.com; s=arc-20160816; b=iCuKjxzgy6oFMe1idc48SVLaXD2gHCu5AjzLqFvH6UI0DD5zVjk0klP4cOGKlaxJsk AV+sH5u8umVxEywAbtfmuXWy6jr++F66x3ynXZI3cNGhug9b374Cq8o2uvRZSreg9dgM Ag0El2KCYv1AgWD6P1vQuEtOW5ZaYwFhwQ/V593SqJX9rvLzR2fP7CNtbcMfQNPONssT GOW64XV1Du8yUn7yhLnGtj47p7Xs/d3S08pgaAW6W46k4uI5LSHZpxunRzurq04SKY8X FeMbXaw1OS0G/FDgpN3jzVpV0Bnksdlt1zYS6P6Slwm3GU+iB7c7b0/L9dEoEORZiNJK JeVA== 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=nvAxTcZIvC1OdGrlu2eJxWT3B4hHYurQ+uIblxla4PA=; fh=SITWWUOoj45kQNZQP8eXFoSqhMZj3zwJxh2qFr97yGA=; b=W51OXr62GbKMBTTDOwNrOuG0L5kZSaCoTv3S+61O0clhAv0UPU1sGDwzmUUJo3mr/5 0DrCQAaPIMKaExTyt4TgytmjEyeLuPomksse/xnRljgIWWg6tHJdcuagtPjV15/u5VgG 802O04luqsaGI3CS+apIK5AEs5dEPlJ2AdCsiHnOqr3M/yfc/Dkw9/vDqowuOo30ohTa hjCJY0bZMFBQ2y+yzjPloMN8G3FwzpZd7oROuPsXvthLtaZpqnkeZdIgnYu+zrzcP8A4 Hci2+1AxHFzrcr4b2zXE8IlXCN5esJPmuRfJpAizIMboP1aK9aX2LUydDBGNN5hpFa9d kNRA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zGbTp4o1; 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-206540-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206540-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-44038b63a71si44310531cf.598.2024.06.07.10.26.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 10:26:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206540-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zGbTp4o1; 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-206540-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206540-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 302F51C21702 for ; Fri, 7 Jun 2024 17:26:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0F99D19B597; Fri, 7 Jun 2024 17:26:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zGbTp4o1" Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 E1D4519ADB1 for ; Fri, 7 Jun 2024 17:26:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717781176; cv=none; b=VRsTciN3HJZa8DrQr8dkTWmRLe8vU1vsteusNvoGSLWkI7bq663l1ezhcl6COcDSzqH3fXqXUra4vKeCwwomvDvKHYcKca0gJBcd7mKK3e5odacG3mFNHNB9FhmJZ8BcIzd8MLc/xjWx9ftUrKJDqqqMgdt8dntR8uYmuwehMYE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717781176; c=relaxed/simple; bh=CmeU6X6XuRUCMZgWNmWWbwkO7e45T52s6O6uFlVMD4w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uiJzuPOI2naGYExHB/OZlH7oJ5dZu6dXKlSsajv+U8i9PoTctWPQwwh2gd6ZCYd40T8WZOWao9Fl8IHM6I7kfAYXcG/Q8LtC3a//jMUfavGqK3g1/meAiZS5Wbu4C7IUJt2Zar4HKMppZMDppneUAg20j4XAGTkSrOOoeazcLDc= 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=zGbTp4o1; arc=none smtp.client-ip=209.85.216.73 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-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2c1e9cbab00so1817552a91.0 for ; Fri, 07 Jun 2024 10:26:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717781174; x=1718385974; 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=nvAxTcZIvC1OdGrlu2eJxWT3B4hHYurQ+uIblxla4PA=; b=zGbTp4o1gvvfKg1SufjTOQJSja6C0ucpU/ZpqFqcCWgzMOv72MqE5X2BV88zu5ea3O /pnTM3mGshd++JyXUtmnJX0Rbbk287AqoffV7nwYurLHsPTKRnoi1/w8Mr0o723trkyj W9EnBTsZD3tfepoGOTCNinaa8PktrY/rv2DixHDdAyxIrzKQYTV/TN3pnfmn8CyhZ3Wv K+3GyJiHtpV/FrRACu/2SewvHl8EOAsj7slHyEXCQnj2EIJMX9Ae2z+5A5Xz2jaZ4ClV 2UXxeRFDXh70ANO5w+Xq1dOJrkzfZiP8sX3vYTNzC0FAl4Whv+m2KmEWR5OCaMMnjbKF Icig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717781174; x=1718385974; 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=nvAxTcZIvC1OdGrlu2eJxWT3B4hHYurQ+uIblxla4PA=; b=c84j9zwUGNmaRs9ZwJ5TSuVkN+ClIiqBV6BLz1eRhsIDUrK6C7jkGdZ0mrzDTge44K ixxDHDa1+EwMQWhWkETEywiOXAe0SNUL8jqWnmOxiMtqKJ+KLPDjaifpkX+kf3x1mbvJ ZNSDeLnpaVwzgAoUWyoHxNLOG2ytAsLL/SphgDkYskseYi8+tjMWNog2kIyb12Oz2LH3 RHqhajNBczO2aOD2qr+gVpxbaTla/r/9wrKPWwZ6moYx3UBNIvcMSF9aDqVNZoofF01e zP3/8UYrYts+1HNyEQCooEeVaZgLZoxOIV04SeBUyMJ27abkZWQru/C0MjMpd5tdMF28 aaYA== X-Forwarded-Encrypted: i=1; AJvYcCUy0KGvbSgJuOpQ1+u8EmP6eQk5q8sQovUloxNnqc2lz78wsn/g2gjQGfrdkOMnW2OMErurd8W6QAi6VJjuMsr8h3T1gUiHuHjzLwn/ X-Gm-Message-State: AOJu0Yz8Bo0IRbjtetOHs5Wx5yLiL9ViwJ9l1EtbOEn61ZwRuD1X0L4/ FTAidNUip2ueax8Che7ZWGvrTrxxarG5abhTqG4u1PT93J/YbTHJJ+oxZJsjWD+nF3z/4yr907Q wng== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:5a82:b0:2c2:d12e:c344 with SMTP id 98e67ed59e1d1-2c2d12ed108mr3554a91.2.1717781174029; Fri, 07 Jun 2024 10:26:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 7 Jun 2024 10:26:04 -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-2-seanjc@google.com> Subject: [PATCH 1/6] KVM: nVMX: Add a helper to get highest pending from Posted Interrupt vector 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" 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 Signed-off-by: Sean Christopherson --- 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 75b4f41d9926..0710486d42cc 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" @@ -3899,8 +3900,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 6b2a0226257e..1715d2ab07be 100644 --- a/arch/x86/kvm/vmx/posted_intr.h +++ b/arch/x86/kvm/vmx/posted_intr.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0 */ #ifndef __KVM_X86_VMX_POSTED_INTR_H #define __KVM_X86_VMX_POSTED_INTR_H + +#include #include void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu); @@ -12,4 +14,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.45.2.505.gda0bf45e8d-goog