Skip to contentSkip to conversation

evtchn/fifo: Don't set PENDING bit if guest misbehaves

PATCH In review Raphael Ning 6 participants 9 messages Mar 16, 2022 – Mar 21, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 evtchn/fifo: Don't set PENDING bit if guest misbehaves

From: Raphael Ning <[email protected]>

Currently, evtchn_fifo_set_pending() will mark the event as PENDING even if it fails to lock the FIFO event queue(s), or if the guest has not initialized the FIFO control block for the target vCPU. A well-behaved guest should never trigger either of these cases.

There is no good reason to set the PENDING bit (the guest should not depend on this behaviour anyway) or check for pollers in such corner cases, so skip that. In fact, both the comment above the for loop and the commit message for

41a822c39263 xen/events: rework fifo queue locking

suggest that the bit should be set after the FIFO queue(s) are locked.

Take the opportunity to rename the was_pending variable (flipping its sense) and switch to the standard bool type.

---
 xen/common/event_fifo.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/common/event_fifo.c +4 -4
index ed4d3beb10f3..6c74ccebebb7 100644
@@ -165,7 +165,7 @@ static void cf_check evtchn_fifo_set_pending(
     unsigned int port;
     event_word_t *word;
     unsigned long flags;
-    bool_t was_pending;
+    bool_t check_pollers = false;
     struct evtchn_fifo_queue *q, *old_q;
     unsigned int try;
     bool linked = true;
@@ -226,8 +226,6 @@ static void cf_check evtchn_fifo_set_pending(
         spin_unlock_irqrestore(&q->lock, flags);
     }
 
-    was_pending = guest_test_and_set_bit(d, EVTCHN_FIFO_PENDING, word);
-
     /* If we didn't get the lock bail out. */
     if ( try == 3 )
     {
@@ -249,6 +247,8 @@ static void cf_check evtchn_fifo_set_pending(
         goto unlock;
     }
 
+    check_pollers = !guest_test_and_set_bit(d, EVTCHN_FIFO_PENDING, word);
+
     /*
      * Link the event if it unmasked and not already linked.
      */
@@ -314,7 +314,7 @@ static void cf_check evtchn_fifo_set_pending(
                                  &v->evtchn_fifo->control_block->ready) )
         vcpu_mark_events_pending(v);
 
-    if ( !was_pending )
+    if ( check_pollers )
         evtchn_check_pollers(d, port);
 }
 
Signature · 1 line
2.32.0
#1 lore raw
Quoted 30 lines · Raphael Ning

From: Raphael Ning <[email protected]>

Currently, evtchn_fifo_set_pending() will mark the event as PENDING even if it fails to lock the FIFO event queue(s), or if the guest has not initialized the FIFO control block for the target vCPU. A well-behaved guest should never trigger either of these cases.

There is no good reason to set the PENDING bit (the guest should not depend on this behaviour anyway) or check for pollers in such corner cases, so skip that. In fact, both the comment above the for loop and the commit message for

41a822c39263 xen/events: rework fifo queue locking

suggest that the bit should be set after the FIFO queue(s) are locked.

Take the opportunity to rename the was_pending variable (flipping its sense) and switch to the standard bool type.

---
 xen/common/event_fifo.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/common/event_fifo.c +0 -0
index ed4d3beb10f3..6c74ccebebb7 100644

xen/common/event_fifo.c static void cf_check evtchn_fifo_set_pending( · line 168

Quoted 5 lines
xen/common/event_fifo.c +1 -1
@@ -165,7 +165,7 @@ static void cf_check evtchn_fifo_set_pending(
     unsigned int port;
     event_word_t *word;
     unsigned long flags;
-    bool_t was_pending;
+    bool_t check_pollers = false;

Considering your commit message, did you intend to change this to bool?

Can be fixed on commit.  Acked-by: Andrew Cooper <[email protected]>

~Andrew

P.S. David - do you want your maintainership back?  None of this code has undergone any major changes since you wrote it.

#2 lore raw
Quoted 38 lines · Andrew Cooper
Quoted 30 lines · Raphael Ning

From: Raphael Ning <[email protected]>

Currently, evtchn_fifo_set_pending() will mark the event as PENDING even if it fails to lock the FIFO event queue(s), or if the guest has not initialized the FIFO control block for the target vCPU. A well-behaved guest should never trigger either of these cases.

There is no good reason to set the PENDING bit (the guest should not depend on this behaviour anyway) or check for pollers in such corner cases, so skip that. In fact, both the comment above the for loop and the commit message for

41a822c39263 xen/events: rework fifo queue locking

suggest that the bit should be set after the FIFO queue(s) are locked.

Take the opportunity to rename the was_pending variable (flipping its sense) and switch to the standard bool type.

---
 xen/common/event_fifo.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/common/event_fifo.c +0 -0
index ed4d3beb10f3..6c74ccebebb7 100644

xen/common/event_fifo.c static void cf_check evtchn_fifo_set_pending( · line 168

Quoted 5 lines
xen/common/event_fifo.c +1 -1
@@ -165,7 +165,7 @@ static void cf_check evtchn_fifo_set_pending(
     unsigned int port;
     event_word_t *word;
     unsigned long flags;
-    bool_t was_pending;
+    bool_t check_pollers = false;

Considering your commit message, did you intend to change this to bool?

Quoted 1 line

Can be fixed on commit.  Acked-by: Andrew Cooper <[email protected]>

My mistake, I missed that when rebasing the patch.

Quoted 5 lines

~Andrew

P.S. David - do you want your maintainership back?  None of this code has undergone any major changes since you wrote it.

Tested-by
#3 lore raw
In reply to Andrew Cooper
Quoted 43 lines · Andrew Cooper
Quoted 30 lines · Raphael Ning

From: Raphael Ning <[email protected]>

Currently, evtchn_fifo_set_pending() will mark the event as PENDING even if it fails to lock the FIFO event queue(s), or if the guest has not initialized the FIFO control block for the target vCPU. A well-behaved guest should never trigger either of these cases.

There is no good reason to set the PENDING bit (the guest should not depend on this behaviour anyway) or check for pollers in such corner cases, so skip that. In fact, both the comment above the for loop and the commit message for

41a822c39263 xen/events: rework fifo queue locking

suggest that the bit should be set after the FIFO queue(s) are locked.

Take the opportunity to rename the was_pending variable (flipping its sense) and switch to the standard bool type.

---
xen/common/event_fifo.c | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)
xen/common/event_fifo.c +0 -0
index ed4d3beb10f3..6c74ccebebb7 100644

xen/common/event_fifo.c static void cf_check evtchn_fifo_set_pending( · line 168

Quoted 5 lines
xen/common/event_fifo.c +1 -1
@@ -165,7 +165,7 @@ static void cf_check evtchn_fifo_set_pending(
    unsigned int port;
    event_word_t *word;
    unsigned long flags;
-    bool_t was_pending;
+    bool_t check_pollers = false;

Considering your commit message, did you intend to change this to bool?

Quoted 2 lines

Can be fixed on commit. Acked-by: Andrew Cooper <[email protected]>

I’ve tested on the ARM side, I’ve started/destroyed few guests from Dom0, connect to the console, run some network communications between guest and Dom0, everything works:

Cheers,
Luca

Quoted 5 lines

~Andrew

P.S. David - do you want your maintainership back? None of this code has undergone any major changes since you wrote it.

#4 lore raw
Quoted 4 lines · Luca Fancellu

I’ve tested on the ARM side, I’ve started/destroyed few guests from Dom0, connect to the console, run some network communications between guest and Dom0, everything works:

Thanks!  I tested on x86 (in a QEMU VM) by launching and destroying an HVM guest; both dom0 and the guest use FIFO event channels.

Quoted 4 lines

Cheers,
Luca

#5 lore raw
In reply to Andrew Cooper

Hi Andrew,

Quoted 13 lines · Andrew Cooper

xen/common/event_fifo.c static void cf_check evtchn_fifo_set_pending( · line 168

Quoted 10 lines
xen/common/event_fifo.c +1 -1
index ed4d3beb10f3..6c74ccebebb7 100644
@@ -165,7 +165,7 @@ static void cf_check evtchn_fifo_set_pending(
      unsigned int port;
      event_word_t *word;
      unsigned long flags;
-    bool_t was_pending;
+    bool_t check_pollers = false;

Considering your commit message, did you intend to change this to bool?

Quoted 1 line

Can be fixed on commit.  Acked-by: Andrew Cooper <[email protected]>

So far, tools like b4 [1] are not able to find your tag on lore.kernel.org:

42sh> b4 am 6b84a20b2753130cc62406a0fd14d2708f6f504b.1647455219.git.raphning@amazon.com

Looking up https://lore.kernel.org/r/6b84a20b2753130cc62406a0fd14d2708f6f504b.1647455219.git.raphning%40amazon.com
Analyzing 8 messages in the thread
Checking attestation on all messages, may take a moment...
---
✓ [PATCH] evtchn/fifo: Don't set PENDING bit if guest misbehaves
+ Reviewed-by: David Vrabel <[email protected]>
+ Tested-by: Luca Fancellu <[email protected]> (✓
DKIM/armh.onmicrosoft.com)
---
✓ Signed: DKIM/gmail.com
---
Total patches: 1
---
Link:
https://lore.kernel.org/r/6b84a20b2753130cc62406a0fd14d2708f6f504b.1647455219.git.raphning@amazon.com
Base: not specified
git am
./20220316_raphning_evtchn_fifo_don_t_set_pending_bit_if_guest_misbehaves.mbx

This is because they are not at the start of the line. In the future, would you mind write the tag on a separate line?

Cheers,

[1] https://people.kernel.org/monsieuricon/introducing-b4-and-patch-attestation

Signature · 1 line
Julien Grall
#6 lore raw
In reply to Raphael Ning
Quoted 6 lines · Raphael Ning

From: Raphael Ning <[email protected]>

Currently, evtchn_fifo_set_pending() will mark the event as PENDING even if it fails to lock the FIFO event queue(s), or if the guest has not initialized the FIFO control block for the target vCPU. A well-behaved guest should never trigger either of these cases.

Is this true even for the resume case e.g. after a migration?

The guests starts on the new host with no FIFO control block for any vcpu registered, so couldn't an event get lost with your patch in case it was sent before the target vcpu's control block gets registered?

Juergen

#7 lore raw
Quoted 8 lines · Juergen Gross
Quoted 6 lines · Raphael Ning

From: Raphael Ning <[email protected]>

Currently, evtchn_fifo_set_pending() will mark the event as PENDING even if it fails to lock the FIFO event queue(s), or if the guest has not initialized the FIFO control block for the target vCPU. A well-behaved guest should never trigger either of these cases.

Quoted 5 lines

Is this true even for the resume case e.g. after a migration?

The guests starts on the new host with no FIFO control block for any vcpu registered, so couldn't an event get lost with your patch in case it was sent before the target vcpu's control block gets registered?

An event that is PENDING but not LINKED is not reachable by the guest so it won't ever see such an event, so the event is lost whether the P bit is set or not.

Guests ensure that event channels are not bound to VCPUs that don't (yet) have FIFO control blocks.

For example, in Linux xen_irq_resume() reinitializes the control blocks (in xen_evtchn_resume()) before restoring any of the event channels.

David

Reviewed-by on v1 1/1
#8 lore raw
In reply to Raphael Ning
Quoted 4 lines · Raphael Ning

Currently, evtchn_fifo_set_pending() will mark the event as PENDING even if it fails to lock the FIFO event queue(s), or if the guest has not initialized the FIFO control block for the target vCPU. A well-behaved guest should never trigger either of these cases.

David