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 A132B3ECBC6 for ; Mon, 16 Mar 2026 19:36:45 +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=1773689805; cv=none; b=O2Mz/CfiaHpm0H3JVlxIOO/l14N5ualj2pFx+zRFMyAq+30bi38my9gUK6861rXFZhx4J9KCs8V0WQNVrGIswnprZ8bJZVDEtb5OJFzHUhyJuUWjlBBKR70QnObXn3PWwXnUmD0GDkZvd3FZKUzVnDxC31fZA8Z6z3A11etBM/4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773689805; c=relaxed/simple; bh=cKmic1cMbbEi8DQY9GDdhz9wpaY8wDC+WT7TExizR3g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MJdvNIEd80V6V450HRSSTJihJaWHljmb/mh3n3CzHt3JJae3W9jy/5nEH5sbk3ie75gHC9rz98iyCo64Gb7IQojdocmaXBjTYDg/+Qq3tA+sZsiIUieeUIG/lchAcXAqVe9nUXM2tJ3UQukWUkCXvUO/L6fwpgHhSqRlzrYngc8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DHBw4fLq; 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="DHBw4fLq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D2862C19421; Mon, 16 Mar 2026 19:36:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773689805; bh=cKmic1cMbbEi8DQY9GDdhz9wpaY8wDC+WT7TExizR3g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DHBw4fLqzYKMr09LP7hdhIwiUeB/idnaKgnsVwRitU+c3Z+tzrJCT2jWLXXX2LjUo cC6ykVsZxrJQBFbwa7i/B9rsYX8Km0cnaQpVxyyXZRcD2O1JOd0ngBGqHEza6KkLzk PP97uc43Zb+att66KkQ/NIS/jCEWNnD0XExgpV6CzVCv8Qkbyb9Nhc4B1mn1lozdMo CyJgy+dee5m7z47OgO+052ia2FjDwsppVklTLO5kVdyQtJJon520EOWFnJJler9Gcd Jf95L/rSKhlK+SLpg+KCtUYioiROfpJAeRHjkPGQT4NQgAtIAMfy3q8w+3QxDM+UiD GFhOr9SrKg95Q== From: Sasha Levin To: stable@vger.kernel.org Cc: Naveen N Rao , Sean Christopherson , Sasha Levin Subject: [PATCH 6.6.y 1/3] KVM: SVM: Limit AVIC physical max index based on configured max_vcpu_ids Date: Mon, 16 Mar 2026 15:36:41 -0400 Message-ID: <20260316193643.1358734-1-sashal@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <2026031633-ranting-ditto-0e1b@gregkh> References: <2026031633-ranting-ditto-0e1b@gregkh> 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 574ef752d4aea04134bc121294d717f4422c2755 ] KVM allows VMMs to specify the maximum possible APIC ID for a virtual machine through KVM_CAP_MAX_VCPU_ID capability so as to limit data structures related to APIC/x2APIC. Utilize the same to set the AVIC physical max index in the VMCB, similar to VMX. This helps hardware limit the number of entries to be scanned in the physical APIC ID table speeding up IPI broadcasts for virtual machines with smaller number of vCPUs. Unlike VMX, SVM AVIC requires a single page to be allocated for the Physical APIC ID table and the Logical APIC ID table, so retain the existing approach of allocating those during VM init. Signed-off-by: Naveen N Rao (AMD) Link: https://lore.kernel.org/r/adb07ccdb3394cd79cb372ba6bcc69a4e4d4ef54.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 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 6970b11a6b4c6..adbd7b3d2e446 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -85,6 +85,7 @@ struct amd_svm_iommu_ir { static void avic_activate_vmcb(struct vcpu_svm *svm) { struct vmcb *vmcb = svm->vmcb01.ptr; + struct kvm *kvm = svm->vcpu.kvm; vmcb->control.int_ctl &= ~(AVIC_ENABLE_MASK | X2APIC_MODE_MASK); vmcb->control.avic_physical_id &= ~AVIC_PHYSICAL_MAX_INDEX_MASK; @@ -100,7 +101,8 @@ 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 |= X2AVIC_MAX_PHYSICAL_ID; + 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 { @@ -111,7 +113,8 @@ 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 |= AVIC_MAX_PHYSICAL_ID; + 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