From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E011B33A03A for ; Mon, 16 Mar 2026 19:18:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773688727; cv=none; b=a/4zNAIGPICUVfCaFKJfFph4z/A9qdiNIwKuYWTerac0c+rgfYT1/ECCfDqDtw84BX89UgOxQDfaspoPLLmRV3+0WgZYsMs4iytoQrdYCpP+Cuv+fjO49wjSwxl0Wv5taWdUhsyXWXJs4WlPAVGT66HPKwjtWdVO3BbbmsweEho= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773688727; c=relaxed/simple; bh=AVwMcDk7BYNdyFJycydBKGCWdD9S+xXf6sQoIe6KRzQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U5eAi8174IMChxwhDBShEgQqNF1r+G+i4tfFZdKDIXCMG6+flNywbBSslaW8ZAZvho5Pj3vla4Cy9CdaZRolt4PKdiLtXTiVKnS4hVB9sRE/WL4lrvy1pmN06+noP2nwvLjsB55z+vTGrxyACWJr2KIv5g5As1OrmYMidgpvxbQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WpsfTOss; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WpsfTOss" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 51314C2BC87; Mon, 16 Mar 2026 19:18:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773688727; bh=AVwMcDk7BYNdyFJycydBKGCWdD9S+xXf6sQoIe6KRzQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WpsfTOsshyZCbwQBbWheagYhGI8g2Ngd43CzeNj+3IOXZ4j46uy1HIHvg969sHROu 3eeDAFuvAvAlzqbdCwByU/ICHldB9G4UiB+wD45UqFSOuiahp+WDZVugGiyllRLI1U siyc/FBkV+weruQ0BeaQ7VpNF7HyG/BAdSAkaP5r5RW8JyT2MekXA+pn41B6uOBu5z uQkgoMehWUtXES0yIgDsVK/19xltClgI2qf2G/r+KU5KsV4At6bqdt3GG9lwN2E1Gs ZeNC0ePGiAOf9ZxFcIZGIuxv5o78qACdwobjVeLuRQ7AsFCz/5KXHT5gOiNu2trJmh DLgetYHiL9Gfg== From: Sasha Levin To: stable@vger.kernel.org Cc: Naveen N Rao , Sean Christopherson , Sasha Levin Subject: [PATCH 6.12.y 2/3] KVM: SVM: Add a helper to look up the max physical ID for AVIC Date: Mon, 16 Mar 2026 15:18:44 -0400 Message-ID: <20260316191845.1350980-2-sashal@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260316191845.1350980-1-sashal@kernel.org> References: <2026031633-gambling-shock-b6ca@gregkh> <20260316191845.1350980-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Naveen N Rao [ Upstream commit f2f6e67a56dc88fea7e9b10c4e79bb01d97386b7 ] To help with a future change, add a helper to look up the maximum physical ID depending on the vCPU AVIC mode. No functional change intended. Suggested-by: Sean Christopherson Signed-off-by: Naveen N Rao (AMD) Link: https://lore.kernel.org/r/0ab9bf5e20a3463a4aa3a5ea9bbbac66beedf1d1.1757009416.git.naveen@kernel.org Signed-off-by: Sean Christopherson Stable-dep-of: 87d0f901a9bd ("KVM: SVM: Set/clear CR8 write interception when AVIC is (de)activated") Signed-off-by: Sasha Levin --- arch/x86/kvm/svm/avic.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 50e49558a2c99..a34323fb4f3cd 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -82,13 +82,31 @@ struct amd_svm_iommu_ir { void *data; /* Storing pointer to struct amd_ir_data */ }; +static u32 avic_get_max_physical_id(struct kvm_vcpu *vcpu) +{ + u32 arch_max; + + if (x2avic_enabled && apic_x2apic_mode(vcpu->arch.apic)) + arch_max = X2AVIC_MAX_PHYSICAL_ID; + else + arch_max = AVIC_MAX_PHYSICAL_ID; + + /* + * Despite its name, KVM_CAP_MAX_VCPU_ID represents the maximum APIC ID + * plus one, so the max possible APIC ID is one less than that. + */ + return min(vcpu->kvm->arch.max_vcpu_ids - 1, arch_max); +} + static void avic_activate_vmcb(struct vcpu_svm *svm) { struct vmcb *vmcb = svm->vmcb01.ptr; - struct kvm *kvm = svm->vcpu.kvm; + struct kvm_vcpu *vcpu = &svm->vcpu; vmcb->control.int_ctl &= ~(AVIC_ENABLE_MASK | X2APIC_MODE_MASK); + vmcb->control.avic_physical_id &= ~AVIC_PHYSICAL_MAX_INDEX_MASK; + vmcb->control.avic_physical_id |= avic_get_max_physical_id(vcpu); vmcb->control.int_ctl |= AVIC_ENABLE_MASK; @@ -101,8 +119,7 @@ static void avic_activate_vmcb(struct vcpu_svm *svm) */ if (x2avic_enabled && apic_x2apic_mode(svm->vcpu.arch.apic)) { vmcb->control.int_ctl |= X2APIC_MODE_MASK; - vmcb->control.avic_physical_id |= min(kvm->arch.max_vcpu_ids - 1, - X2AVIC_MAX_PHYSICAL_ID); + /* Disabling MSR intercept for x2APIC registers */ svm_set_x2apic_msr_interception(svm, false); } else { @@ -112,9 +129,6 @@ static void avic_activate_vmcb(struct vcpu_svm *svm) */ kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, &svm->vcpu); - /* For xAVIC and hybrid-xAVIC modes */ - vmcb->control.avic_physical_id |= min(kvm->arch.max_vcpu_ids - 1, - AVIC_MAX_PHYSICAL_ID); /* Enabling MSR intercept for x2APIC registers */ svm_set_x2apic_msr_interception(svm, true); } -- 2.51.0