Skip to contentSkip to conversation

x86/upcall: inject a spurious event after setting upcall vector

PATCH Superseded Roger Pau Monne 2 participants 8 messages Dec 28, 2017 – Jan 4, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/upcall: inject a spurious event after setting upcall vector

In case the vCPU has pending events to inject. This fixes a bug that happened if the guest mapped the vcpu info area using VCPUOP_register_vcpu_info without having setup the event channel upcall, and then setup the upcall vector.

In this scenario the guest would not receive any upcalls, because the call to VCPUOP_register_vcpu_info would have marked the vCPU as having pending events, but the vector could not be injected because it was not yet setup.

This has not caused issues so far because all the consumers first setup the vector callback and then map the vcpu info page, but there's no limitation that prevents doing it in the inverse order.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

---
 xen/arch/x86/hvm/hvm.c | 1 +
 xen/arch/x86/hvm/irq.c | 4 ++++
 2 files changed, 5 insertions(+)
xen/arch/x86/hvm/hvm.c +1 -0
index 28bc7e4252..3dfb3511d9 100644
@@ -4069,6 +4069,7 @@ static int hvmop_set_evtchn_upcall_vector(
     printk(XENLOG_G_INFO "%pv: upcall vector %02x\n", v, op.vector);
 
     v->arch.hvm_vcpu.evtchn_upcall_vector = op.vector;
+    arch_evtchn_inject(v);
     return 0;
 }
 
xen/arch/x86/hvm/irq.c +4 -0
index 0077f68a83..9427e30806 100644
@@ -385,6 +385,7 @@ void hvm_set_callback_via(struct domain *d, uint64_t via)
     struct hvm_irq *hvm_irq = hvm_domain_irq(d);
     unsigned int gsi=0, pdev=0, pintx=0;
     uint8_t via_type;
+    struct vcpu *v;
 
     via_type = (uint8_t)MASK_EXTR(via, HVM_PARAM_CALLBACK_IRQ_TYPE_MASK) + 1;
     if ( ((via_type == HVMIRQ_callback_gsi) && (via == 0)) ||
@@ -447,6 +448,9 @@ void hvm_set_callback_via(struct domain *d, uint64_t via)
 
     spin_unlock(&d->arch.hvm_domain.irq_lock);
 
+    for_each_vcpu(d, v)
+        arch_evtchn_inject(v);
+
 #ifndef NDEBUG
     printk(XENLOG_G_INFO "Dom%u callback via changed to ", d->domain_id);
     switch ( via_type )
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 28.12.17 at 13:57, <[email protected]> wrote:

In case the vCPU has pending events to inject. This fixes a bug that happened if the guest mapped the vcpu info area using VCPUOP_register_vcpu_info without having setup the event channel upcall, and then setup the upcall vector.

Quoted 8 lines

In this scenario the guest would not receive any upcalls, because the call to VCPUOP_register_vcpu_info would have marked the vCPU as having pending events, but the vector could not be injected because it was not yet setup.

This has not caused issues so far because all the consumers first setup the vector callback and then map the vcpu info page, but there's no limitation that prevents doing it in the inverse order.

Hmm, yes, okay, I can see that we may indeed want to do this for symmetry reasons. There is a small theoretical risk of this causing races, though, for not entirely well written guest drivers.

xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 4072

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -4069,6 +4069,7 @@ static int hvmop_set_evtchn_upcall_vector(
     printk(XENLOG_G_INFO "%pv: upcall vector %02x\n", v, op.vector);
 
     v->arch.hvm_vcpu.evtchn_upcall_vector = op.vector;
+    arch_evtchn_inject(v);

Why go through the arch hook instead of calling
hvm_assert_evtchn_irq() directly?

Quoted 11 lines
xen/arch/x86/hvm/irq.c +1 -0
@@ -385,6 +385,7 @@ void hvm_set_callback_via(struct domain *d, uint64_t via)
     struct hvm_irq *hvm_irq = hvm_domain_irq(d);
     unsigned int gsi=0, pdev=0, pintx=0;
     uint8_t via_type;
+    struct vcpu *v;
 
     via_type = (uint8_t)MASK_EXTR(via, HVM_PARAM_CALLBACK_IRQ_TYPE_MASK) + 1;
     if ( ((via_type == HVMIRQ_callback_gsi) && (via == 0)) ||

xen/arch/x86/hvm/irq.c void hvm_set_callback_via(struct domain *d, uint64_t via) · line 452

Quoted 5 lines
xen/arch/x86/hvm/irq.c +2 -0
@@ -447,6 +448,9 @@ void hvm_set_callback_via(struct domain *d, uint64_t via)
 
     spin_unlock(&d->arch.hvm_domain.irq_lock);
 
+    for_each_vcpu(d, v)
+        arch_evtchn_inject(v);

Wouldn't it make sense to limit this to actually active vCPU-s?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 28.12.17 at 13:57, <[email protected]> wrote:

In case the vCPU has pending events to inject. This fixes a bug that happened if the guest mapped the vcpu info area using VCPUOP_register_vcpu_info without having setup the event channel upcall, and then setup the upcall vector.

In this scenario the guest would not receive any upcalls, because the call to VCPUOP_register_vcpu_info would have marked the vCPU as having pending events, but the vector could not be injected because it was not yet setup.

Quoted 7 lines
Quoted 3 lines

This has not caused issues so far because all the consumers first setup the vector callback and then map the vcpu info page, but there's no limitation that prevents doing it in the inverse order.

Hmm, yes, okay, I can see that we may indeed want to do this for symmetry reasons. There is a small theoretical risk of this causing races, though, for not entirely well written guest drivers.

Not exactly. In the scenario described above not injecting this event will cause further events to not be injected also. This is because VCPUOP_register_vcpu_info sets evtchn_upcall_pending and tries to inject an event using arch_evtchn_inject. If the vector is not set at this point, arch_evtchn_inject will do nothing, but evtchn_upcall_pending will be left set.

Further calls to vcpu_mark_events_pending will not call into hvm_assert_evtchn_irq because the pending bit is already set, thus preventing the delivery of any event channel interrupts.

Quoted 23 lines

xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 4072

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -4069,6 +4069,7 @@ static int hvmop_set_evtchn_upcall_vector(
     printk(XENLOG_G_INFO "%pv: upcall vector %02x\n", v, op.vector);
 
     v->arch.hvm_vcpu.evtchn_upcall_vector = op.vector;
+    arch_evtchn_inject(v);

Why go through the arch hook instead of calling
hvm_assert_evtchn_irq() directly?

Quoted 12 lines
xen/arch/x86/hvm/irq.c +1 -0
@@ -385,6 +385,7 @@ void hvm_set_callback_via(struct domain *d, uint64_t via)
     struct hvm_irq *hvm_irq = hvm_domain_irq(d);
     unsigned int gsi=0, pdev=0, pintx=0;
     uint8_t via_type;
+    struct vcpu *v;
 
     via_type = (uint8_t)MASK_EXTR(via, HVM_PARAM_CALLBACK_IRQ_TYPE_MASK) + 1;
     if ( ((via_type == HVMIRQ_callback_gsi) && (via == 0)) ||
@@ -447,6 +448,9 @@ void hvm_set_callback_via(struct domain *d, uint64_t via)
 
Quoted 6 lines

xen/arch/x86/hvm/irq.c void hvm_set_callback_via(struct domain *d, uint64_t via) · line 452

Quoted 4 lines
xen/arch/x86/hvm/irq.c +2 -0
     spin_unlock(&d->arch.hvm_domain.irq_lock);
 
+    for_each_vcpu(d, v)
+        arch_evtchn_inject(v);

Wouldn't it make sense to limit this to actually active vCPU-s?

Since it's not a hot-path I didn't bother, but I guess using is_vcpu_online should be fine?

Will switch to hvm_assert_evtchn_irq for both of the above, since this is x86/hvm code already.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 10:13, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 28.12.17 at 13:57, <[email protected]> wrote:

In case the vCPU has pending events to inject. This fixes a bug that happened if the guest mapped the vcpu info area using VCPUOP_register_vcpu_info without having setup the event channel upcall, and then setup the upcall vector.

In this scenario the guest would not receive any upcalls, because the call to VCPUOP_register_vcpu_info would have marked the vCPU as having pending events, but the vector could not be injected because it was not yet setup.

Quoted 7 lines
Quoted 3 lines

This has not caused issues so far because all the consumers first setup the vector callback and then map the vcpu info page, but there's no limitation that prevents doing it in the inverse order.

Hmm, yes, okay, I can see that we may indeed want to do this for symmetry reasons. There is a small theoretical risk of this causing races, though, for not entirely well written guest drivers.

Not exactly. In the scenario described above not injecting this event will cause further events to not be injected also. This is because VCPUOP_register_vcpu_info sets evtchn_upcall_pending and tries to inject an event using arch_evtchn_inject. If the vector is not set at this point, arch_evtchn_inject will do nothing, but evtchn_upcall_pending will be left set.

Quoted 3 lines

Further calls to vcpu_mark_events_pending will not call into hvm_assert_evtchn_irq because the pending bit is already set, thus preventing the delivery of any event channel interrupts.

I understand that, but I don't understand how this relates to my comment.

In any event, an option to at least consider would be to clear the pending indicator again if the vector was found unset.

Quoted 11 lines
Quoted 8 lines

xen/arch/x86/hvm/irq.c void hvm_set_callback_via(struct domain *d, uint64_t via) · line 452

Quoted 6 lines
xen/arch/x86/hvm/irq.c +2 -0
@@ -447,6 +448,9 @@ void hvm_set_callback_via(struct domain *d, uint64_t via)
 
     spin_unlock(&d->arch.hvm_domain.irq_lock);
 
+    for_each_vcpu(d, v)
+        arch_evtchn_inject(v);

Wouldn't it make sense to limit this to actually active vCPU-s?

Since it's not a hot-path I didn't bother, but I guess using is_vcpu_online should be fine?

I think so, yes. The less work in such for-each-vcpu loops, the better.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 28 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 10:13, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 28.12.17 at 13:57, <[email protected]> wrote:

In case the vCPU has pending events to inject. This fixes a bug that happened if the guest mapped the vcpu info area using VCPUOP_register_vcpu_info without having setup the event channel upcall, and then setup the upcall vector.

In this scenario the guest would not receive any upcalls, because the call to VCPUOP_register_vcpu_info would have marked the vCPU as having pending events, but the vector could not be injected because it was not yet setup.

Quoted 7 lines
Quoted 3 lines

This has not caused issues so far because all the consumers first setup the vector callback and then map the vcpu info page, but there's no limitation that prevents doing it in the inverse order.

Hmm, yes, okay, I can see that we may indeed want to do this for symmetry reasons. There is a small theoretical risk of this causing races, though, for not entirely well written guest drivers.

Not exactly. In the scenario described above not injecting this event will cause further events to not be injected also. This is because VCPUOP_register_vcpu_info sets evtchn_upcall_pending and tries to inject an event using arch_evtchn_inject. If the vector is not set at this point, arch_evtchn_inject will do nothing, but evtchn_upcall_pending will be left set.

Quoted 6 lines
Quoted 3 lines

Further calls to vcpu_mark_events_pending will not call into hvm_assert_evtchn_irq because the pending bit is already set, thus preventing the delivery of any event channel interrupts.

I understand that, but I don't understand how this relates to my comment.

I don't think this applies to "not entirely well written drivers". I think it's perfectly fine for a guest to map the vcpu_info page first and then setup the vector callback. With the current code doing this will result in no interrupts being injected.

Quoted 2 lines

In any event, an option to at least consider would be to clear the pending indicator again if the vector was found unset.

That seems more complicated than the solution proposed here.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

xen/arch/x86/hvm/irq.c void hvm_set_callback_via(struct domain *d, uint64_t via) · line 452

Quoted 6 lines
xen/arch/x86/hvm/irq.c +2 -0
@@ -447,6 +448,9 @@ void hvm_set_callback_via(struct domain *d, uint64_t via)
 
     spin_unlock(&d->arch.hvm_domain.irq_lock);
 
+    for_each_vcpu(d, v)
+        arch_evtchn_inject(v);

Wouldn't it make sense to limit this to actually active vCPU-s?

Quoted 5 lines
Quoted 2 lines

Since it's not a hot-path I didn't bother, but I guess using is_vcpu_online should be fine?

I think so, yes. The less work in such for-each-vcpu loops, the better.

Thanks, I'm going to send a new version with those two issues fixed then.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 12:37, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 10:13, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 28.12.17 at 13:57, <[email protected]> wrote:

In case the vCPU has pending events to inject. This fixes a bug that happened if the guest mapped the vcpu info area using VCPUOP_register_vcpu_info without having setup the event channel upcall, and then setup the upcall vector.

In this scenario the guest would not receive any upcalls, because the call to VCPUOP_register_vcpu_info would have marked the vCPU as having pending events, but the vector could not be injected because it was not yet setup.

Quoted 7 lines
Quoted 3 lines

This has not caused issues so far because all the consumers first setup the vector callback and then map the vcpu info page, but there's no limitation that prevents doing it in the inverse order.

Hmm, yes, okay, I can see that we may indeed want to do this for symmetry reasons. There is a small theoretical risk of this causing races, though, for not entirely well written guest drivers.

Not exactly. In the scenario described above not injecting this event will cause further events to not be injected also. This is because VCPUOP_register_vcpu_info sets evtchn_upcall_pending and tries to inject an event using arch_evtchn_inject. If the vector is not set at this point, arch_evtchn_inject will do nothing, but evtchn_upcall_pending will be left set.

Further calls to vcpu_mark_events_pending will not call into hvm_assert_evtchn_irq because the pending bit is already set, thus preventing the delivery of any event channel interrupts.

Quoted 7 lines
Quoted 2 lines

I understand that, but I don't understand how this relates to my comment.

I don't think this applies to "not entirely well written drivers". I think it's perfectly fine for a guest to map the vcpu_info page first and then setup the vector callback. With the current code doing this will result in no interrupts being injected.

My meaning of "not entirely well written drivers" is unrelated to the scenario you want to fix: When setting the upcall vector, an event would now be received at a time that the drivers may not expect (iirc there's no requirement to relocate the vcpu info out of the shared info page, and even if there was the issue described would still arise if the vector was changed later on).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 42 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 12:37, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 10:13, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 28.12.17 at 13:57, <[email protected]> wrote:

In case the vCPU has pending events to inject. This fixes a bug that happened if the guest mapped the vcpu info area using VCPUOP_register_vcpu_info without having setup the event channel upcall, and then setup the upcall vector.

In this scenario the guest would not receive any upcalls, because the call to VCPUOP_register_vcpu_info would have marked the vCPU as having pending events, but the vector could not be injected because it was not yet setup.

Quoted 7 lines
Quoted 3 lines

This has not caused issues so far because all the consumers first setup the vector callback and then map the vcpu info page, but there's no limitation that prevents doing it in the inverse order.

Hmm, yes, okay, I can see that we may indeed want to do this for symmetry reasons. There is a small theoretical risk of this causing races, though, for not entirely well written guest drivers.

Not exactly. In the scenario described above not injecting this event will cause further events to not be injected also. This is because VCPUOP_register_vcpu_info sets evtchn_upcall_pending and tries to inject an event using arch_evtchn_inject. If the vector is not set at this point, arch_evtchn_inject will do nothing, but evtchn_upcall_pending will be left set.

Further calls to vcpu_mark_events_pending will not call into hvm_assert_evtchn_irq because the pending bit is already set, thus preventing the delivery of any event channel interrupts.

Quoted 7 lines
Quoted 2 lines

I understand that, but I don't understand how this relates to my comment.

I don't think this applies to "not entirely well written drivers". I think it's perfectly fine for a guest to map the vcpu_info page first and then setup the vector callback. With the current code doing this will result in no interrupts being injected.

Quoted 6 lines

My meaning of "not entirely well written drivers" is unrelated to the scenario you want to fix: When setting the upcall vector, an event would now be received at a time that the drivers may not expect (iirc there's no requirement to relocate the vcpu info out of the shared info page, and even if there was the issue described would still arise if the vector was changed later on).

Sorry, I misunderstood the context of that sentence. Regarding your point, yes, that's right, drivers should be able to cope with spurious interrupts. I would be very surprised if an event channel driver is not able to cope with spurious interrupts.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
Quoted 51 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 13:15, <[email protected]> wrote:
Quoted 42 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 12:37, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 10:13, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 28.12.17 at 13:57, <[email protected]> wrote:

In case the vCPU has pending events to inject. This fixes a bug that happened if the guest mapped the vcpu info area using VCPUOP_register_vcpu_info without having setup the event channel upcall, and then setup the upcall vector.

In this scenario the guest would not receive any upcalls, because the call to VCPUOP_register_vcpu_info would have marked the vCPU as having pending events, but the vector could not be injected because it was not yet setup.

Quoted 7 lines
Quoted 3 lines

This has not caused issues so far because all the consumers first setup the vector callback and then map the vcpu info page, but there's no limitation that prevents doing it in the inverse order.

Hmm, yes, okay, I can see that we may indeed want to do this for symmetry reasons. There is a small theoretical risk of this causing races, though, for not entirely well written guest drivers.

Not exactly. In the scenario described above not injecting this event will cause further events to not be injected also. This is because VCPUOP_register_vcpu_info sets evtchn_upcall_pending and tries to inject an event using arch_evtchn_inject. If the vector is not set at this point, arch_evtchn_inject will do nothing, but evtchn_upcall_pending will be left set.

Further calls to vcpu_mark_events_pending will not call into hvm_assert_evtchn_irq because the pending bit is already set, thus preventing the delivery of any event channel interrupts.

Quoted 7 lines
Quoted 2 lines

I understand that, but I don't understand how this relates to my comment.

I don't think this applies to "not entirely well written drivers". I think it's perfectly fine for a guest to map the vcpu_info page first and then setup the vector callback. With the current code doing this will result in no interrupts being injected.

Quoted 6 lines

My meaning of "not entirely well written drivers" is unrelated to the scenario you want to fix: When setting the upcall vector, an event would now be received at a time that the drivers may not expect (iirc there's no requirement to relocate the vcpu info out of the shared info page, and even if there was the issue described would still arise if the vector was changed later on).

Quoted 4 lines

Sorry, I misunderstood the context of that sentence. Regarding your point, yes, that's right, drivers should be able to cope with spurious interrupts. I would be very surprised if an event channel driver is not able to cope with spurious interrupts.

I'm not concerned about spurious interrupts in general (i.e. in the middle of some other operations). What I'm (slightly) worried about is a driver getting the setup order wrong, and receiving an interrupt when its handler isn't fully ready yet.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel