Skip to content

Commit

Permalink
KVM: nVMX: clean up posted interrupt descriptor try_cmpxchg
Browse files Browse the repository at this point in the history
Rely on try_cmpxchg64 for re-reading the PID on failure, using READ_ONCE
only right before the first iteration.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
Paolo Bonzini committed Jun 24, 2022
1 parent 4b88b1a commit 4de5c54
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions arch/x86/kvm/vmx/posted_intr.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ static inline struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
return &(to_vmx(vcpu)->pi_desc);
}

static int pi_try_set_control(struct pi_desc *pi_desc, u64 old, u64 new)
static int pi_try_set_control(struct pi_desc *pi_desc, u64 *pold, u64 new)
{
/*
* PID.ON can be set at any time by a different vCPU or by hardware,
* e.g. a device. PID.control must be written atomically, and the
* update must be retried with a fresh snapshot an ON change causes
* the cmpxchg to fail.
*/
if (!try_cmpxchg64(&pi_desc->control, &old, new))
if (!try_cmpxchg64(&pi_desc->control, pold, new))
return -EBUSY;

return 0;
Expand Down Expand Up @@ -96,8 +96,9 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
if (!x2apic_mode)
dest = (dest << 8) & 0xFF00;

old.control = READ_ONCE(pi_desc->control);
do {
old.control = new.control = READ_ONCE(pi_desc->control);
new.control = old.control;

/*
* Clear SN (as above) and refresh the destination APIC ID to
Expand All @@ -111,7 +112,7 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
* descriptor was modified on "put" to use the wakeup vector.
*/
new.nv = POSTED_INTR_VECTOR;
} while (pi_try_set_control(pi_desc, old.control, new.control));
} while (pi_try_set_control(pi_desc, &old.control, new.control));

local_irq_restore(flags);

Expand Down Expand Up @@ -156,12 +157,12 @@ static void pi_enable_wakeup_handler(struct kvm_vcpu *vcpu)

WARN(pi_desc->sn, "PI descriptor SN field set before blocking");

old.control = READ_ONCE(pi_desc->control);
do {
old.control = new.control = READ_ONCE(pi_desc->control);

/* set 'NV' to 'wakeup vector' */
new.control = old.control;
new.nv = POSTED_INTR_WAKEUP_VECTOR;
} while (pi_try_set_control(pi_desc, old.control, new.control));
} while (pi_try_set_control(pi_desc, &old.control, new.control));

/*
* Send a wakeup IPI to this CPU if an interrupt may have been posted
Expand Down

0 comments on commit 4de5c54

Please sign in to comment.