Re: [PATCH RFC 15/39] KVM: x86/xen: handle PV spinlocks slowpath

From: Joao Martins
Date: Thu Feb 10 2022 - 07:18:07 EST


On 2/8/22 12:36, David Woodhouse wrote:
> On Wed, 2019-02-20 at 20:15 +0000, Joao Martins wrote:
>> From: Boris Ostrovsky <boris.ostrovsky@xxxxxxxxxx>
>>
>> Add support for SCHEDOP_poll hypercall.
>>
>> This implementation is optimized for polling for a single channel, which
>> is what Linux does. Polling for multiple channels is not especially
>> efficient (and has not been tested).
>>
>> PV spinlocks slow path uses this hypercall, and explicitly crash if it's
>> not supported.
>>
>> Signed-off-by: Boris Ostrovsky <boris.ostrovsky@xxxxxxxxxx>
>> ---
>
> ...
>
>> +static void kvm_xen_check_poller(struct kvm_vcpu *vcpu, int port)
>> +{
>> + struct kvm_vcpu_xen *vcpu_xen = vcpu_to_xen_vcpu(vcpu);
>> +
>> + if ((vcpu_xen->poll_evtchn == port ||
>> + vcpu_xen->poll_evtchn == -1) &&
>> + test_and_clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.xen.poll_mask))
>> + wake_up(&vcpu_xen->sched_waitq);
>> +}
>
> ...
>
>> + if (sched_poll.nr_ports == 1)
>> + vcpu_xen->poll_evtchn = port;
>> + else
>> + vcpu_xen->poll_evtchn = -1;
>> +
>> + if (!wait_pending_event(vcpu, sched_poll.nr_ports, ports))
>> + wait_event_interruptible_timeout(
>> + vcpu_xen->sched_waitq,
>> + wait_pending_event(vcpu, sched_poll.nr_ports, ports),
>> + sched_poll.timeout ?: KTIME_MAX);
>
> Hm, this doesn't wake on other interrupts, does it?

Hmm, I don't think so? This was specifically polling on event channels,
not sleeping or blocking.

> I think it should.
> Shouldn't it basically be like HLT, with an additional wakeup when the
> listed ports are triggered even when they're masked?
>

I am actually not sure.

Quickly glancing at the xen source, this hypercall doesn't appear to really
block the vcpu, but rather just looking if the evtchn ports are pending and
if a timeout is is specified it sets up a timer. And ofc, wake any evtchn
pollers. But it doesn't appear to actually block the VCPU. It should be
IIRC, the functional equivalent of KVM_HC_VAPIC_POLL_IRQ but for event
channels.


> At https://git.infradead.org/users/dwmw2/linux.git/commitdiff/ddfbdf1af
> I've tried to make it use kvm_vcpu_halt(), and kvm_xen_check_poller()
> sets KVM_REQ_UNBLOCK when an event is delivered to a monitored port.
>
> I haven't quite got it to work yet, but does it seem like a sane
> approach?
>

Joao