return 0;
 }
 
-static void avic_ring_doorbell(struct kvm_vcpu *vcpu)
+void avic_ring_doorbell(struct kvm_vcpu *vcpu)
 {
        /*
         * Note, the vCPU could get migrated to a different pCPU at any point,
        kvm_for_each_vcpu(i, vcpu, kvm) {
                if (kvm_apic_match_dest(vcpu, source, icrl & APIC_SHORT_MASK,
                                        GET_APIC_DEST_FIELD(icrh),
-                                       icrl & APIC_DEST_MASK))
-                       kvm_vcpu_wake_up(vcpu);
+                                       icrl & APIC_DEST_MASK)) {
+                       vcpu->arch.apic->irr_pending = true;
+                       svm_complete_interrupt_delivery(vcpu,
+                                                       icrl & APIC_MODE_MASK,
+                                                       icrl & APIC_INT_LEVELTRIG,
+                                                       icrl & APIC_VECTOR_MASK);
+               }
        }
 }
 
        return;
 }
 
-int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
-{
-       if (!vcpu->arch.apicv_active)
-               return -1;
-
-       /*
-        * Pairs with the smp_mb_*() after setting vcpu->guest_mode in
-        * vcpu_enter_guest() to ensure the write to the vIRR is ordered before
-        * the read of guest_mode, which guarantees that either VMRUN will see
-        * and process the new vIRR entry, or that the below code will signal
-        * the doorbell if the vCPU is already running in the guest.
-        */
-       smp_mb__after_atomic();
-
-       /*
-        * Signal the doorbell to tell hardware to inject the IRQ if the vCPU
-        * is in the guest.  If the vCPU is not in the guest, hardware will
-        * automatically process AVIC interrupts at VMRUN.
-        */
-       if (vcpu->mode == IN_GUEST_MODE) {
-               /*
-                * Signal the doorbell to tell hardware to inject the IRQ.  If
-                * the vCPU exits the guest before the doorbell chimes, hardware
-                * will automatically process AVIC interrupts at the next VMRUN.
-                */
-               avic_ring_doorbell(vcpu);
-       } else {
-               /*
-                * Wake the vCPU if it was blocking.  KVM will then detect the
-                * pending IRQ when checking if the vCPU has a wake event.
-                */
-               kvm_vcpu_wake_up(vcpu);
-       }
-
-       return 0;
-}
-
 bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
 {
        return false;
 
                SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
 }
 
-static void svm_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,
-                                 int trig_mode, int vector)
+void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
+                                    int trig_mode, int vector)
 {
-       struct kvm_vcpu *vcpu = apic->vcpu;
+       /*
+        * vcpu->arch.apicv_active must be read after vcpu->mode.
+        * Pairs with smp_store_release in vcpu_enter_guest.
+        */
+       bool in_guest_mode = (smp_load_acquire(&vcpu->mode) == IN_GUEST_MODE);
 
-       kvm_lapic_set_irr(vector, apic);
-       if (svm_deliver_avic_intr(vcpu, vector)) {
+       if (!READ_ONCE(vcpu->arch.apicv_active)) {
+               /* Process the interrupt via inject_pending_event */
                kvm_make_request(KVM_REQ_EVENT, vcpu);
                kvm_vcpu_kick(vcpu);
+               return;
+       }
+
+       trace_kvm_apicv_accept_irq(vcpu->vcpu_id, delivery_mode, trig_mode, vector);
+       if (in_guest_mode) {
+               /*
+                * Signal the doorbell to tell hardware to inject the IRQ.  If
+                * the vCPU exits the guest before the doorbell chimes, hardware
+                * will automatically process AVIC interrupts at the next VMRUN.
+                */
+               avic_ring_doorbell(vcpu);
        } else {
-               trace_kvm_apicv_accept_irq(vcpu->vcpu_id, delivery_mode,
-                                          trig_mode, vector);
+               /*
+                * Wake the vCPU if it was blocking.  KVM will then detect the
+                * pending IRQ when checking if the vCPU has a wake event.
+                */
+               kvm_vcpu_wake_up(vcpu);
        }
 }
 
+static void svm_deliver_interrupt(struct kvm_lapic *apic,  int delivery_mode,
+                                 int trig_mode, int vector)
+{
+       kvm_lapic_set_irr(vector, apic);
+
+       /*
+        * Pairs with the smp_mb_*() after setting vcpu->guest_mode in
+        * vcpu_enter_guest() to ensure the write to the vIRR is ordered before
+        * the read of guest_mode.  This guarantees that either VMRUN will see
+        * and process the new vIRR entry, or that svm_complete_interrupt_delivery
+        * will signal the doorbell if the CPU has already entered the guest.
+        */
+       smp_mb__after_atomic();
+       svm_complete_interrupt_delivery(apic->vcpu, delivery_mode, trig_mode, vector);
+}
+
 static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
 
 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code);
 void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
                          int read, int write);
+void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
+                                    int trig_mode, int vec);
 
 /* nested.c */
 
 void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
 void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr);
 void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr);
-int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec);
 bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu);
 int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
                       uint32_t guest_irq, bool set);
 void avic_vcpu_blocking(struct kvm_vcpu *vcpu);
 void avic_vcpu_unblocking(struct kvm_vcpu *vcpu);
+void avic_ring_doorbell(struct kvm_vcpu *vcpu);
 
 /* sev.c */
 
 
         * result in virtual interrupt delivery.
         */
        local_irq_disable();
-       vcpu->mode = IN_GUEST_MODE;
+
+       /* Store vcpu->apicv_active before vcpu->mode.  */
+       smp_store_release(&vcpu->mode, IN_GUEST_MODE);
 
        srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);