Re: [PATCH v3 00/26] KVM: x86: Halt and APICv overhaul

From: Maxim Levitsky
Date: Thu Dec 09 2021 - 11:03:54 EST


On Thu, 2021-12-09 at 15:45 +0000, Sean Christopherson wrote:
> On Thu, Dec 09, 2021, Maxim Levitsky wrote:
> > On Thu, 2021-12-09 at 15:29 +0100, Paolo Bonzini wrote:
> > > On 12/9/21 01:02, Sean Christopherson wrote:
> > > > RDX, a.k.a. ir_data is NULL. This check in svm_ir_list_add()
> > > >
> > > > if (pi->ir_data && (pi->prev_ga_tag != 0)) {
> > > >
> > > > implies pi->ir_data can be NULL, but neither avic_update_iommu_vcpu_affinity()
> > > > nor amd_iommu_update_ga() check ir->data for NULL.
> > > >
> > > > amd_ir_set_vcpu_affinity() returns "success" without clearing pi.is_guest_mode
> > > >
> > > > /* Note:
> > > > * This device has never been set up for guest mode.
> > > > * we should not modify the IRTE
> > > > */
> > > > if (!dev_data || !dev_data->use_vapic)
> > > > return 0;
> > > >
> > > > so it's plausible svm_ir_list_add() could add to the list with a NULL pi->ir_data.
> > > >
> > > > But none of the relevant code has seen any meaningful changes since 5.15, so odds
> > > > are good I broke something :-/
> >
> > Doesn't reproduce here yet even with my iommu changes :-(
> > Oh well.
>
> Hmm, which suggests it could be an existing corner case.

Could very very be!
Next Sunday I'll lean the AMD iommu code a bit closer, and see if I can spot more bugs in it.

Best regards,
Maxim Levitsky

>
> Based on the above, this seems prudent and correct:
>
> @@ -747,7 +754,7 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
> * so we need to check here if it's already been * added
> * to the ir_list.
> */
> - if (pi->ir_data && (pi->prev_ga_tag != 0)) {
> + if (pi->prev_ga_tag != 0) {
> struct kvm *kvm = svm->vcpu.kvm;
> u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
> struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
> @@ -877,7 +884,7 @@ int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
> * we can reference to them directly when we update vcpu
> * scheduling information in IOMMU irte.
> */
> - if (!ret && pi.is_guest_mode)
> + if (!ret && pi.is_guest_mode && pi.ir_data)
> svm_ir_list_add(svm, &pi);
> } else {
> /* Use legacy mode in IRTE */
> @@ -898,7 +905,7 @@ int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
> * was cached. If so, we need to clean up the per-vcpu
> * ir_list.
> */
> - if (!ret && pi.prev_ga_tag) {
> + if (!ret && pi.prev_ga_tag && !WARN_ON(!pi.ir_data)) {
> int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
> struct kvm_vcpu *vcpu;
>
>