Re: [PATCH v3 33/40] KVM: x86: Move kvm_cpu_get_apicid() from kvm_host.h => avic.c

From: Sean Christopherson

Date: Fri Jun 05 2026 - 16:30:09 EST


On Fri, Jun 05, 2026, Kai Huang wrote:
> On Fri, 2026-05-29 at 15:22 -0700, Sean Christopherson wrote:
> > #define KVM_CLOCK_VALID_FLAGS \
> > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c
> > index b7083cd692ad..9264c8ef1fa1 100644
> > --- a/arch/x86/kvm/svm/avic.c
> > +++ b/arch/x86/kvm/svm/avic.c
> > @@ -146,6 +146,11 @@ static void avic_set_x2apic_msr_interception(struct vcpu_svm *svm,
> > svm->x2avic_msrs_intercepted = intercept;
> > }
> >
> > +static int kvm_cpu_get_apicid(int mps_cpu)
> > +{
> > + return default_cpu_present_to_apicid(mps_cpu);
> > +}
> > +
> >
>
> Looks it is only used by AMD (svm/avic.c). I don't know why, but it would be
> nice if you can mention this in changelog, since this function seems pretty
> common to both Intel and AMD.

Huh, right, VMX posted interrupts needs the APIC ID too. Gah, posted_intr.c uses
cpu_physical_id():

#define cpu_physical_id(cpu) per_cpu(x86_cpu_to_apicid, cpu)

which is basically the same thing as default_cpu_present_to_apicid():

u32 default_cpu_present_to_apicid(int mps_cpu)
{
if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu))
return (int)per_cpu(x86_cpu_to_apicid, mps_cpu);
else
return BAD_APICID;
}
EXPORT_SYMBOL_FOR_KVM(default_cpu_present_to_apicid);

Given that AVIC uses kvm_cpu_get_apicid() only when getting the pCPU for a loaded
vCPU, it darn well should be impossible for the CPU to be offline, let alone not
even present.

I'll drop this patch, and instead send a separate patch to have AVIC use
cpu_physical_id(), and kill off both kvm_cpu_get_apicid() and the above EXPORT.

Thanks!