Skip to contentSkip to conversation

XSA-343 followup patches

PATCH Superseded Juergen Gross 4 participants 26 messages Oct 12, 2020 – Oct 20, 2020

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v2 cover XSA-343 followup patches

The patches for XSA-343 produced some fallout, especially the event channel locking has shown to be problematic.

Patch 1 is targeting fifo event channels for avoiding any races for the case that the fifo queue has been changed for a specific event channel.

The second patch is modifying the per event channel locking scheme in order to avoid deadlocks and problems due to the event channel lock having been changed to require IRQs off by the XSA-343 patches.

Juergen Gross (2):
xen/events: access last_priority and last_vcpu_id together xen/evtchn: rework per event channel lock

 xen/arch/x86/irq.c         |   6 +-
 xen/arch/x86/pv/shim.c     |   9 +--
 xen/common/event_channel.c | 109 +++++++++++++++++--------------------
 xen/common/event_fifo.c    |  25 +++++++--
 xen/include/xen/event.h    |  56 ++++++++++++++++---
 xen/include/xen/sched.h    |   5 +-
 6 files changed, 126 insertions(+), 84 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 1/2 xen/events: access last_priority and last_vcpu_id together

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

---
 xen/common/event_fifo.c | 25 +++++++++++++++++++------
 xen/include/xen/sched.h |  3 +--
 2 files changed, 20 insertions(+), 8 deletions(-)
xen/common/event_fifo.c +19 -6
index fc189152e1..fffbd409c8 100644
@@ -42,6 +42,14 @@ struct evtchn_fifo_domain {
     unsigned int num_evtchns;
 };
 
+union evtchn_fifo_lastq {
+    u32 raw;
+    struct {
+        u8 last_priority;
+        u16 last_vcpu_id;
+    };
+};
+
 static inline event_word_t *evtchn_fifo_word_from_port(const struct domain *d,
                                                        unsigned int port)
 {
@@ -86,16 +94,18 @@ static struct evtchn_fifo_queue *lock_old_queue(const struct domain *d,
     struct vcpu *v;
     struct evtchn_fifo_queue *q, *old_q;
     unsigned int try;
+    union evtchn_fifo_lastq lastq;
 
     for ( try = 0; try < 3; try++ )
     {
-        v = d->vcpu[evtchn->last_vcpu_id];
-        old_q = &v->evtchn_fifo->queue[evtchn->last_priority];
+        lastq.raw = read_atomic(&evtchn->fifo_lastq);
+        v = d->vcpu[lastq.last_vcpu_id];
+        old_q = &v->evtchn_fifo->queue[lastq.last_priority];
 
         spin_lock_irqsave(&old_q->lock, *flags);
 
-        v = d->vcpu[evtchn->last_vcpu_id];
-        q = &v->evtchn_fifo->queue[evtchn->last_priority];
+        v = d->vcpu[lastq.last_vcpu_id];
+        q = &v->evtchn_fifo->queue[lastq.last_priority];
 
         if ( old_q == q )
             return old_q;
@@ -246,8 +256,11 @@ static void evtchn_fifo_set_pending(struct vcpu *v, struct evtchn *evtchn)
         /* Moved to a different queue? */
         if ( old_q != q )
         {
-            evtchn->last_vcpu_id = v->vcpu_id;
-            evtchn->last_priority = q->priority;
+            union evtchn_fifo_lastq lastq;
+
+            lastq.last_vcpu_id = v->vcpu_id;
+            lastq.last_priority = q->priority;
+            write_atomic(&evtchn->fifo_lastq, lastq.raw);
 
             spin_unlock_irqrestore(&old_q->lock, flags);
             spin_lock_irqsave(&q->lock, flags);
xen/include/xen/sched.h +1 -2
index d8ed83f869..a298ff4df8 100644
@@ -114,8 +114,7 @@ struct evtchn
         u16 virq;      /* state == ECS_VIRQ */
     } u;
     u8 priority;
-    u8 last_priority;
-    u16 last_vcpu_id;
+    u32 fifo_lastq;    /* Data for fifo events identifying last queue. */
 #ifdef CONFIG_XSM
     union {
 #ifdef XSM_NEED_GENERIC_EVTCHN_SSID
Signature · 1 line
2.26.2
#2 lore raw

RE: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 30 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Juergen Gross
Sent: 12 October 2020 10:28
To: [email protected]
Cc: Juergen Gross <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Jan Beulich <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

---
 xen/common/event_fifo.c | 25 +++++++++++++++++++------
 xen/include/xen/sched.h |  3 +--
 2 files changed, 20 insertions(+), 8 deletions(-)
xen/common/event_fifo.c +0 -0
index fc189152e1..fffbd409c8 100644

xen/common/event_fifo.c struct evtchn_fifo_domain { · line 51

Quoted 10 lines
xen/common/event_fifo.c +7 -0
@@ -42,6 +42,14 @@ struct evtchn_fifo_domain {
     unsigned int num_evtchns;
 };
 
+union evtchn_fifo_lastq {
+    u32 raw;
+    struct {
+        u8 last_priority;
+        u16 last_vcpu_id;
+    };
+};

I guess you want to s/u32/uint32_t, etc. above.

Quoted 29 lines
xen/common/event_fifo.c +7 -4
+
 static inline event_word_t *evtchn_fifo_word_from_port(const struct domain *d,
                                                        unsigned int port)
 {
@@ -86,16 +94,18 @@ static struct evtchn_fifo_queue *lock_old_queue(const struct domain *d,
     struct vcpu *v;
     struct evtchn_fifo_queue *q, *old_q;
     unsigned int try;
+    union evtchn_fifo_lastq lastq;
 
     for ( try = 0; try < 3; try++ )
     {
-        v = d->vcpu[evtchn->last_vcpu_id];
-        old_q = &v->evtchn_fifo->queue[evtchn->last_priority];
+        lastq.raw = read_atomic(&evtchn->fifo_lastq);
+        v = d->vcpu[lastq.last_vcpu_id];
+        old_q = &v->evtchn_fifo->queue[lastq.last_priority];
 
         spin_lock_irqsave(&old_q->lock, *flags);
 
-        v = d->vcpu[evtchn->last_vcpu_id];
-        q = &v->evtchn_fifo->queue[evtchn->last_priority];
+        v = d->vcpu[lastq.last_vcpu_id];
+        q = &v->evtchn_fifo->queue[lastq.last_priority];
 
         if ( old_q == q )
             return old_q;
@@ -246,8 +256,11 @@ static void evtchn_fifo_set_pending(struct vcpu *v, struct evtchn *evtchn)
         /* Moved to a different queue? */

xen/common/event_fifo.c static void evtchn_fifo_set_pending(struct vcpu *v, struct evtchn *evtchn) · line 264

Quoted 10 lines
xen/common/event_fifo.c +5 -2
         if ( old_q != q )
         {
-            evtchn->last_vcpu_id = v->vcpu_id;
-            evtchn->last_priority = q->priority;
+            union evtchn_fifo_lastq lastq;
+
+            lastq.last_vcpu_id = v->vcpu_id;
+            lastq.last_priority = q->priority;
+            write_atomic(&evtchn->fifo_lastq, lastq.raw);
 

You're going to leak some stack here I think. Perhaps add a 'pad' field between 'last_priority' and 'last_vcpu_id' and zero it?

Paul

Quoted 19 lines
xen/common/event_fifo.c +0 -0
             spin_unlock_irqrestore(&old_q->lock, flags);
             spin_lock_irqsave(&q->lock, flags);
xen/include/xen/sched.h +1 -3
index d8ed83f869..a298ff4df8 100644
@@ -114,8 +114,7 @@ struct evtchn
         u16 virq;      /* state == ECS_VIRQ */
     } u;
     u8 priority;
-    u8 last_priority;
-    u16 last_vcpu_id;
+    u32 fifo_lastq;    /* Data for fifo events identifying last queue. */
 #ifdef CONFIG_XSM
     union {
 #ifdef XSM_NEED_GENERIC_EVTCHN_SSID
--

2.26.2

#3 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 33 lines · Paul Durrant
Quoted 33 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Juergen Gross
Sent: 12 October 2020 10:28
To: [email protected]
Cc: Juergen Gross <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Jan Beulich <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

---
  xen/common/event_fifo.c | 25 +++++++++++++++++++------
  xen/include/xen/sched.h |  3 +--
  2 files changed, 20 insertions(+), 8 deletions(-)
xen/common/event_fifo.c +0 -0
index fc189152e1..fffbd409c8 100644
@@ -42,6 +42,14 @@ struct evtchn_fifo_domain {
      unsigned int num_evtchns;
  };
 
Quoted 9 lines

xen/common/event_fifo.c struct evtchn_fifo_domain { · line 51

Quoted 7 lines
xen/common/event_fifo.c +7 -0
+union evtchn_fifo_lastq {
+    u32 raw;
+    struct {
+        u8 last_priority;
+        u16 last_vcpu_id;
+    };
+};

I guess you want to s/u32/uint32_t, etc. above.

Hmm, yes, probably.

Quoted 26 lines
Quoted 26 lines
xen/common/event_fifo.c +7 -4
+
  static inline event_word_t *evtchn_fifo_word_from_port(const struct domain *d,
                                                         unsigned int port)
  {
@@ -86,16 +94,18 @@ static struct evtchn_fifo_queue *lock_old_queue(const struct domain *d,
      struct vcpu *v;
      struct evtchn_fifo_queue *q, *old_q;
      unsigned int try;
+    union evtchn_fifo_lastq lastq;
 
      for ( try = 0; try < 3; try++ )
      {
-        v = d->vcpu[evtchn->last_vcpu_id];
-        old_q = &v->evtchn_fifo->queue[evtchn->last_priority];
+        lastq.raw = read_atomic(&evtchn->fifo_lastq);
+        v = d->vcpu[lastq.last_vcpu_id];
+        old_q = &v->evtchn_fifo->queue[lastq.last_priority];
 
          spin_lock_irqsave(&old_q->lock, *flags);
 
-        v = d->vcpu[evtchn->last_vcpu_id];
-        q = &v->evtchn_fifo->queue[evtchn->last_priority];
+        v = d->vcpu[lastq.last_vcpu_id];
+        q = &v->evtchn_fifo->queue[lastq.last_priority];
 
Quoted 16 lines
Quoted 4 lines
xen/common/event_fifo.c +0 -0
          if ( old_q == q )
              return old_q;
@@ -246,8 +256,11 @@ static void evtchn_fifo_set_pending(struct vcpu *v, struct evtchn *evtchn)
          /* Moved to a different queue? */

xen/common/event_fifo.c static void evtchn_fifo_set_pending(struct vcpu *v, struct evtchn *evtchn) · line 264

Quoted 10 lines
xen/common/event_fifo.c +5 -2
          if ( old_q != q )
          {
-            evtchn->last_vcpu_id = v->vcpu_id;
-            evtchn->last_priority = q->priority;
+            union evtchn_fifo_lastq lastq;
+
+            lastq.last_vcpu_id = v->vcpu_id;
+            lastq.last_priority = q->priority;
+            write_atomic(&evtchn->fifo_lastq, lastq.raw);
 

You're going to leak some stack here I think. Perhaps add a 'pad' field between 'last_priority' and 'last_vcpu_id' and zero it?

I can do that, but why? This is nothing a guest is supposed to see at any time.

Juergen

#4 lore raw

RE: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 98 lines

-----Original Message-----
From: Jürgen Groß <[email protected]>
Sent: 12 October 2020 10:56
To: [email protected]; [email protected]
Cc: 'Andrew Cooper' <[email protected]>; 'George Dunlap' <[email protected]>; 'Ian Jackson' <[email protected]>; 'Jan Beulich' <[email protected]>; 'Julien Grall' <[email protected]>; 'Stefano Stabellini' <[email protected]>; 'Wei Liu' <[email protected]>
Subject: Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 6 lines · Paul Durrant
Quoted 6 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Juergen Gross
Sent: 12 October 2020 10:28
To: [email protected]
Cc: Juergen Gross <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Jan Beulich <[email protected]>;

Julien

Quoted 27 lines
Quoted 27 lines

Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

---
  xen/common/event_fifo.c | 25 +++++++++++++++++++------
  xen/include/xen/sched.h |  3 +--
  2 files changed, 20 insertions(+), 8 deletions(-)
xen/common/event_fifo.c +0 -0
index fc189152e1..fffbd409c8 100644
@@ -42,6 +42,14 @@ struct evtchn_fifo_domain {
      unsigned int num_evtchns;
  };
 
Quoted 9 lines

xen/common/event_fifo.c struct evtchn_fifo_domain { · line 51

Quoted 7 lines
xen/common/event_fifo.c +7 -0
+union evtchn_fifo_lastq {
+    u32 raw;
+    struct {
+        u8 last_priority;
+        u16 last_vcpu_id;
+    };
+};

I guess you want to s/u32/uint32_t, etc. above.

Hmm, yes, probably.

Quoted 26 lines
Quoted 26 lines
xen/common/event_fifo.c +7 -4
+
  static inline event_word_t *evtchn_fifo_word_from_port(const struct domain *d,
                                                         unsigned int port)
  {
@@ -86,16 +94,18 @@ static struct evtchn_fifo_queue *lock_old_queue(const struct domain *d,
      struct vcpu *v;
      struct evtchn_fifo_queue *q, *old_q;
      unsigned int try;
+    union evtchn_fifo_lastq lastq;
 
      for ( try = 0; try < 3; try++ )
      {
-        v = d->vcpu[evtchn->last_vcpu_id];
-        old_q = &v->evtchn_fifo->queue[evtchn->last_priority];
+        lastq.raw = read_atomic(&evtchn->fifo_lastq);
+        v = d->vcpu[lastq.last_vcpu_id];
+        old_q = &v->evtchn_fifo->queue[lastq.last_priority];
 
          spin_lock_irqsave(&old_q->lock, *flags);
 
-        v = d->vcpu[evtchn->last_vcpu_id];
-        q = &v->evtchn_fifo->queue[evtchn->last_priority];
+        v = d->vcpu[lastq.last_vcpu_id];
+        q = &v->evtchn_fifo->queue[lastq.last_priority];
 
Quoted 16 lines
Quoted 4 lines
xen/common/event_fifo.c +0 -0
          if ( old_q == q )
              return old_q;
@@ -246,8 +256,11 @@ static void evtchn_fifo_set_pending(struct vcpu *v, struct evtchn *evtchn)
          /* Moved to a different queue? */

xen/common/event_fifo.c static void evtchn_fifo_set_pending(struct vcpu *v, struct evtchn *evtchn) · line 264

Quoted 10 lines
xen/common/event_fifo.c +5 -2
          if ( old_q != q )
          {
-            evtchn->last_vcpu_id = v->vcpu_id;
-            evtchn->last_priority = q->priority;
+            union evtchn_fifo_lastq lastq;
+
+            lastq.last_vcpu_id = v->vcpu_id;
+            lastq.last_priority = q->priority;
+            write_atomic(&evtchn->fifo_lastq, lastq.raw);
 

You're going to leak some stack here I think. Perhaps add a 'pad' field between 'last_priority' and

Quoted 4 lines

'last_vcpu_id' and zero it?

I can do that, but why? This is nothing a guest is supposed to see at any time.

True, but it would also be nice if the value of 'raw' was at least predictable. I guest just adding '= {}' to the declaration would actually be easiest.

Paul

Quoted 3 lines

Juergen

#5 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

In reply to Juergen Gross
Quoted 11 lines · Juergen Gross

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

I seem to vaguely recall that at the time this seemingly racy access was done on purpose by David. Did you go look at the old commits to understand whether there really is a race which can't be tolerated within the spec?

xen/include/xen/sched.h struct evtchn · line 117

Quoted 9 lines
xen/include/xen/sched.h +1 -2
@@ -114,8 +114,7 @@ struct evtchn
         u16 virq;      /* state == ECS_VIRQ */
     } u;
     u8 priority;
-    u8 last_priority;
-    u16 last_vcpu_id;
+    u32 fifo_lastq;    /* Data for fifo events identifying last queue. */

This grows struct evtchn's size on at least 32-bit Arm. I'd like to suggest including "priority" in the union, and call the new field simply "fifo" or some such.

Jan

#6 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 11 lines · Jan Beulich
Quoted 10 lines · Juergen Gross

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

Quoted 6 lines
Quoted 1 line

I seem to vaguely recall that at the time this seemingly racy access was done on purpose by David. Did you go look at the old commits to understand whether there really is a race which can't be tolerated within the spec?

At least the comments in the code tell us that the race regarding the writing of priority (not last_priority) is acceptable.

Especially Julien was rather worried by the current situation. In case you can convince him the current handling is fine, we can easily drop this patch.

Quoted 14 lines

xen/include/xen/sched.h struct evtchn · line 117

Quoted 10 lines
xen/include/xen/sched.h +1 -2
@@ -114,8 +114,7 @@ struct evtchn
          u16 virq;      /* state == ECS_VIRQ */
      } u;
      u8 priority;
-    u8 last_priority;
-    u16 last_vcpu_id;
+    u32 fifo_lastq;    /* Data for fifo events identifying last queue. */

This grows struct evtchn's size on at least 32-bit Arm. I'd like to suggest including "priority" in the union, and call the new field simply "fifo" or some such.

This will add quite some complexity as suddenly all writes to the union will need to be made through a cmpxchg() scheme.

Regarding the growth: struct evtchn is aligned to 64 bytes. So there is no growth at all, as the size will not be larger than those 64 bytes.

Juergen

#7 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 14 lines · Jürgen Groß
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Juergen Gross

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

Quoted 7 lines
Quoted 4 lines

I seem to vaguely recall that at the time this seemingly racy access was done on purpose by David. Did you go look at the old commits to understand whether there really is a race which can't be tolerated within the spec?

At least the comments in the code tell us that the race regarding the writing of priority (not last_priority) is acceptable.

Ah, then it was comments. I knew I read something to this effect somewhere, recently.

Quoted 3 lines

Especially Julien was rather worried by the current situation. In case you can convince him the current handling is fine, we can easily drop this patch.

Julien, in the light of the above - can you clarify the specific concerns you (still) have?

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
xen/include/xen/sched.h +0 -0
@@ -114,8 +114,7 @@ struct evtchn
          u16 virq;      /* state == ECS_VIRQ */
      } u;
Quoted 8 lines

xen/include/xen/sched.h struct evtchn · line 117

Quoted 4 lines
xen/include/xen/sched.h +1 -2
      u8 priority;
-    u8 last_priority;
-    u16 last_vcpu_id;
+    u32 fifo_lastq;    /* Data for fifo events identifying last queue. */

This grows struct evtchn's size on at least 32-bit Arm. I'd like to suggest including "priority" in the union, and call the new field simply "fifo" or some such.

Quoted 6 lines

This will add quite some complexity as suddenly all writes to the union will need to be made through a cmpxchg() scheme.

Regarding the growth: struct evtchn is aligned to 64 bytes. So there is no growth at all, as the size will not be larger than those 64 bytes.

Oh, I didn't spot this attribute, which I consider at least suspicious. Without XSM I'm getting the impression that on 32-bit Arm the structure's size would be 32 bytes or less without it, so it looks as if it shouldn't be there unconditionally.

Jan

#8 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Hi Jan,

Quoted 26 lines · Jan Beulich
Quoted 14 lines · Jürgen Groß
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Juergen Gross

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

Quoted 7 lines
Quoted 4 lines

I seem to vaguely recall that at the time this seemingly racy access was done on purpose by David. Did you go look at the old commits to understand whether there really is a race which can't be tolerated within the spec?

At least the comments in the code tell us that the race regarding the writing of priority (not last_priority) is acceptable.

Ah, then it was comments. I knew I read something to this effect somewhere, recently.

Quoted 6 lines
Quoted 3 lines

Especially Julien was rather worried by the current situation. In case you can convince him the current handling is fine, we can easily drop this patch.

Julien, in the light of the above - can you clarify the specific concerns you (still) have?

Let me start with that the assumption if evtchn->lock is not held when evtchn_fifo_set_pending() is called. If it is held, then my comment is moot.

From my understanding, the goal of lock_old_queue() is to return the old queue used.

last_priority and last_vcpu_id may be updated separately and I could not convince myself that it would not be possible to return a queue that is neither the current one nor the old one.

The following could happen if evtchn->priority and evtchn->notify_vcpu_id keeps changing between calls.

pCPU0 | pCPU1

				|
evtchn_fifo_set_pending(v0,...)	|
				| evtchn_fifo_set_pending(v1, ...)
 [...]				|
 /* Queue has changed */	|
 evtchn->last_vcpu_id = v0 	|
				| -> evtchn_old_queue()
				| v = d->vcpu[evtchn->last_vcpu_id];
  				| old_q = ...
				| spin_lock(old_q->...)
				| v = ...
				| q = ...
				| /* q and old_q would be the same */
				|

evtchn->las_priority = priority|

If my diagram is correct, then pCPU1 would return a queue that is neither the current nor old one.

In which case, I think it would at least be possible to corrupt the queue. From evtchn_fifo_set_pending():

        /*
         * If this event was a tail, the old queue is now empty and
         * its tail must be invalidated to prevent adding an event to
         * the old queue from corrupting the new queue.
         */
        if ( old_q->tail == port )
            old_q->tail = 0;

Did I miss anything?

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 33 lines · Julien Grall

Hi Jan,

Quoted 30 lines · Jan Beulich
Quoted 14 lines · Jürgen Groß
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Juergen Gross

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

Quoted 7 lines
Quoted 4 lines

I seem to vaguely recall that at the time this seemingly racy access was done on purpose by David. Did you go look at the old commits to understand whether there really is a race which can't be tolerated within the spec?

At least the comments in the code tell us that the race regarding the writing of priority (not last_priority) is acceptable.

Ah, then it was comments. I knew I read something to this effect somewhere, recently.

Quoted 3 lines

Especially Julien was rather worried by the current situation. In case you can convince him the current handling is fine, we can easily drop this patch.

Quoted 5 lines
Quoted 2 lines

Julien, in the light of the above - can you clarify the specific concerns you (still) have?

Let me start with that the assumption if evtchn->lock is not held when evtchn_fifo_set_pending() is called. If it is held, then my comment is moot.

But this isn't interesting - we know there are paths where it is held, and ones (interdomain sending) where it's the remote port's lock instead which is held. What's important here is that a _consistent_ lock be held (but it doesn't need to be evtchn's).

Quoted 25 lines

From my understanding, the goal of lock_old_queue() is to return the
old queue used.

last_priority and last_vcpu_id may be updated separately and I could not convince myself that it would not be possible to return a queue that is neither the current one nor the old one.

The following could happen if evtchn->priority and evtchn->notify_vcpu_id keeps changing between calls.

pCPU0 | pCPU1

				|
evtchn_fifo_set_pending(v0,...)	|
				| evtchn_fifo_set_pending(v1, ...)
  [...]				|
  /* Queue has changed */	|
  evtchn->last_vcpu_id = v0 	|
				| -> evtchn_old_queue()
				| v = d->vcpu[evtchn->last_vcpu_id];
   				| old_q = ...
				| spin_lock(old_q->...)
				| v = ...
				| q = ...
				| /* q and old_q would be the same */
				|
Quoted 4 lines

evtchn->las_priority = priority|

If my diagram is correct, then pCPU1 would return a queue that is neither the current nor old one.

I think I agree.

Quoted 11 lines

In which case, I think it would at least be possible to corrupt the queue. From evtchn_fifo_set_pending():

         /*
          * If this event was a tail, the old queue is now empty and
          * its tail must be invalidated to prevent adding an event to
          * the old queue from corrupting the new queue.
          */
         if ( old_q->tail == port )
             old_q->tail = 0;
Quoted 1 line

Did I miss anything?

I don't think you did. The important point though is that a consistent lock is being held whenever we come here, so two racing set_pending() aren't possible for one and the same evtchn. As a result I don't think the patch here is actually needed.

If I take this further, then I think I can reason why it wasn't necessary to add further locking to send_guest_{global,vcpu}_virq():
The virq_lock is the "consistent lock" protecting ECS_VIRQ ports. The spin_barrier() while closing the port guards that side against the port changing to a different ECS_* behind the sending functions' backs. And binding such ports sets ->virq_to_evtchn[] last, with a suitable barrier (the unlock).

Which leaves send_guest_pirq() before we can drop the IRQ-safe locking again. I guess we would need to work towards using the underlying irq_desc's lock as consistent lock here, but this certainly isn't the case just yet, and I'm not really certain this can be achieved.

Jan

#10 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 88 lines · Jan Beulich
Quoted 33 lines · Julien Grall

Hi Jan,

Quoted 30 lines · Jan Beulich
Quoted 14 lines · Jürgen Groß
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Juergen Gross

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

Quoted 7 lines
Quoted 4 lines

I seem to vaguely recall that at the time this seemingly racy access was done on purpose by David. Did you go look at the old commits to understand whether there really is a race which can't be tolerated within the spec?

At least the comments in the code tell us that the race regarding the writing of priority (not last_priority) is acceptable.

Ah, then it was comments. I knew I read something to this effect somewhere, recently.

Quoted 3 lines

Especially Julien was rather worried by the current situation. In case you can convince him the current handling is fine, we can easily drop this patch.

Quoted 5 lines
Quoted 2 lines

Julien, in the light of the above - can you clarify the specific concerns you (still) have?

Let me start with that the assumption if evtchn->lock is not held when evtchn_fifo_set_pending() is called. If it is held, then my comment is moot.

But this isn't interesting - we know there are paths where it is held, and ones (interdomain sending) where it's the remote port's lock instead which is held. What's important here is that a _consistent_ lock be held (but it doesn't need to be evtchn's).

Quoted 25 lines

From my understanding, the goal of lock_old_queue() is to return the
old queue used.

last_priority and last_vcpu_id may be updated separately and I could not convince myself that it would not be possible to return a queue that is neither the current one nor the old one.

The following could happen if evtchn->priority and evtchn->notify_vcpu_id keeps changing between calls.

pCPU0 | pCPU1

				|
evtchn_fifo_set_pending(v0,...)	|
				| evtchn_fifo_set_pending(v1, ...)
   [...]				|
   /* Queue has changed */	|
   evtchn->last_vcpu_id = v0 	|
				| -> evtchn_old_queue()
				| v = d->vcpu[evtchn->last_vcpu_id];
    				| old_q = ...
				| spin_lock(old_q->...)
				| v = ...
				| q = ...
				| /* q and old_q would be the same */
				|
Quoted 4 lines

evtchn->las_priority = priority|

If my diagram is correct, then pCPU1 would return a queue that is neither the current nor old one.

I think I agree.

Quoted 11 lines

In which case, I think it would at least be possible to corrupt the queue. From evtchn_fifo_set_pending():

          /*
           * If this event was a tail, the old queue is now empty and
           * its tail must be invalidated to prevent adding an event to
           * the old queue from corrupting the new queue.
           */
          if ( old_q->tail == port )
              old_q->tail = 0;
Quoted 6 lines
Quoted 1 line

Did I miss anything?

I don't think you did. The important point though is that a consistent lock is being held whenever we come here, so two racing set_pending() aren't possible for one and the same evtchn. As a result I don't think the patch here is actually needed.

Julien, do you agree?

Can i drop this patch?

Juergen

#11 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

In reply to Jan Beulich
Quoted 37 lines · Jan Beulich
Quoted 36 lines · Julien Grall

Hi Jan,

Quoted 32 lines · Jan Beulich
Quoted 14 lines · Jürgen Groß
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Juergen Gross

The queue for a fifo event is depending on the vcpu_id and the priority of the event. When sending an event it might happen the event needs to change queues and the old queue needs to be kept for keeping the links between queue elements intact. For this purpose the event channel contains last_priority and last_vcpu_id values elements for being able to identify the old queue.

In order to avoid races always access last_priority and last_vcpu_id with a single atomic operation avoiding any inconsistencies.

Quoted 7 lines
Quoted 4 lines

I seem to vaguely recall that at the time this seemingly racy access was done on purpose by David. Did you go look at the old commits to understand whether there really is a race which can't be tolerated within the spec?

At least the comments in the code tell us that the race regarding the writing of priority (not last_priority) is acceptable.

Ah, then it was comments. I knew I read something to this effect somewhere, recently.

Quoted 3 lines

Especially Julien was rather worried by the current situation. In case you can convince him the current handling is fine, we can easily drop this patch.

Julien, in the light of the above - can you clarify the specific concerns you (still) have?

Quoted 7 lines
Quoted 2 lines

Let me start with that the assumption if evtchn->lock is not held when evtchn_fifo_set_pending() is called. If it is held, then my comment is moot.

But this isn't interesting - we know there are paths where it is held, and ones (interdomain sending) where it's the remote port's lock instead which is held. What's important here is that a _consistent_ lock be held (but it doesn't need to be evtchn's).

Yes, a _consistent_ lock *should* be sufficient. But it is better to use the same lock everywhere so it is easier to reason (see more below).

Quoted 44 lines
Quoted 26 lines

From my understanding, the goal of lock_old_queue() is to return the
old queue used.

last_priority and last_vcpu_id may be updated separately and I could not convince myself that it would not be possible to return a queue that is neither the current one nor the old one.

The following could happen if evtchn->priority and evtchn->notify_vcpu_id keeps changing between calls.

pCPU0 | pCPU1

				|
evtchn_fifo_set_pending(v0,...)	|
				| evtchn_fifo_set_pending(v1, ...)
   [...]				|
   /* Queue has changed */	|
   evtchn->last_vcpu_id = v0 	|
				| -> evtchn_old_queue()
				| v = d->vcpu[evtchn->last_vcpu_id];
    				| old_q = ...
				| spin_lock(old_q->...)
				| v = ...
				| q = ...
				| /* q and old_q would be the same */
				|
Quoted 4 lines

evtchn->las_priority = priority|

If my diagram is correct, then pCPU1 would return a queue that is neither the current nor old one.

I think I agree.

Quoted 11 lines

In which case, I think it would at least be possible to corrupt the queue. From evtchn_fifo_set_pending():

          /*
           * If this event was a tail, the old queue is now empty and
           * its tail must be invalidated to prevent adding an event to
           * the old queue from corrupting the new queue.
           */
          if ( old_q->tail == port )
              old_q->tail = 0;
Quoted 6 lines
Quoted 1 line

Did I miss anything?

I don't think you did. The important point though is that a consistent lock is being held whenever we come here, so two racing set_pending() aren't possible for one and the same evtchn. As a result I don't think the patch here is actually needed.

I haven't yet read in full details the rest of the patches to say whether this is necessary or not. However, at a first glance, I think this is not a sane to rely on different lock to protect us. And don't get me started on the lack of documentation...

Furthermore, the implementation of old_lock_queue() suggests that the code was planned to be lockless. Why would you need the loop otherwise?

Therefore, regardless the rest of the discussion, I think this patch would be useful to have for our peace of mind.

Quoted 8 lines

If I take this further, then I think I can reason why it wasn't necessary to add further locking to send_guest_{global,vcpu}_virq():
The virq_lock is the "consistent lock" protecting ECS_VIRQ ports. The spin_barrier() while closing the port guards that side against the port changing to a different ECS_* behind the sending functions' backs. And binding such ports sets ->virq_to_evtchn[] last, with a suitable barrier (the unlock).

This makes sense.

Quoted 5 lines

Which leaves send_guest_pirq() before we can drop the IRQ-safe locking again. I guess we would need to work towards using the underlying irq_desc's lock as consistent lock here, but this certainly isn't the case just yet, and I'm not really certain this can be achieved.

I can't comment on the PIRQ code but I think this is a risky approach (see more above).

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 14 lines · Julien Grall
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Julien Grall
Quoted 7 lines · Jan Beulich
Quoted 3 lines · Jürgen Groß

Especially Julien was rather worried by the current situation. In case you can convince him the current handling is fine, we can easily drop this patch.

Julien, in the light of the above - can you clarify the specific concerns you (still) have?

Let me start with that the assumption if evtchn->lock is not held when evtchn_fifo_set_pending() is called. If it is held, then my comment is moot.

Quoted 7 lines
Quoted 4 lines

But this isn't interesting - we know there are paths where it is held, and ones (interdomain sending) where it's the remote port's lock instead which is held. What's important here is that a _consistent_ lock be held (but it doesn't need to be evtchn's).

Yes, a _consistent_ lock *should* be sufficient. But it is better to use the same lock everywhere so it is easier to reason (see more below).

But that's already not the case, due to the way interdomain channels have events sent. You did suggest acquiring both locks, but as indicated at the time I think this goes too far. As far as the doc aspect - we can improve the situation. Iirc it was you who made me add the respective comment ahead of struct evtchn_port_ops.

Quoted 50 lines
Quoted 43 lines
Quoted 25 lines

From my understanding, the goal of lock_old_queue() is to return the
old queue used.

last_priority and last_vcpu_id may be updated separately and I could not convince myself that it would not be possible to return a queue that is neither the current one nor the old one.

The following could happen if evtchn->priority and evtchn->notify_vcpu_id keeps changing between calls.

pCPU0 | pCPU1

				|
evtchn_fifo_set_pending(v0,...)	|
				| evtchn_fifo_set_pending(v1, ...)
   [...]				|
   /* Queue has changed */	|
   evtchn->last_vcpu_id = v0 	|
				| -> evtchn_old_queue()
				| v = d->vcpu[evtchn->last_vcpu_id];
    				| old_q = ...
				| spin_lock(old_q->...)
				| v = ...
				| q = ...
				| /* q and old_q would be the same */
				|
Quoted 4 lines

evtchn->las_priority = priority|

If my diagram is correct, then pCPU1 would return a queue that is neither the current nor old one.

I think I agree.

Quoted 11 lines

In which case, I think it would at least be possible to corrupt the queue. From evtchn_fifo_set_pending():

          /*
           * If this event was a tail, the old queue is now empty and
           * its tail must be invalidated to prevent adding an event to
           * the old queue from corrupting the new queue.
           */
          if ( old_q->tail == port )
              old_q->tail = 0;
Quoted 6 lines
Quoted 1 line

Did I miss anything?

I don't think you did. The important point though is that a consistent lock is being held whenever we come here, so two racing set_pending() aren't possible for one and the same evtchn. As a result I don't think the patch here is actually needed.

Quoted 7 lines

I haven't yet read in full details the rest of the patches to say whether this is necessary or not. However, at a first glance, I think this is not a sane to rely on different lock to protect us. And don't get me started on the lack of documentation...

Furthermore, the implementation of old_lock_queue() suggests that the code was planned to be lockless. Why would you need the loop otherwise?

The lock-less aspect of this affects multiple accesses to e.g. the same queue, I think. I'm unconvinced it was really considered whether racing sending on the same channel is also safe this way.

Quoted 2 lines

Therefore, regardless the rest of the discussion, I think this patch would be useful to have for our peace of mind.

That's a fair position to take. My counterargument is mainly that readability (and hence maintainability) suffers with those changes.

Quoted 11 lines
Quoted 7 lines

If I take this further, then I think I can reason why it wasn't necessary to add further locking to send_guest_{global,vcpu}_virq():
The virq_lock is the "consistent lock" protecting ECS_VIRQ ports. The spin_barrier() while closing the port guards that side against the port changing to a different ECS_* behind the sending functions' backs. And binding such ports sets ->virq_to_evtchn[] last, with a suitable barrier (the unlock).

This makes sense.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

Which leaves send_guest_pirq() before we can drop the IRQ-safe locking again. I guess we would need to work towards using the underlying irq_desc's lock as consistent lock here, but this certainly isn't the case just yet, and I'm not really certain this can be achieved.

I can't comment on the PIRQ code but I think this is a risky approach (see more above).

It may be; one would only know how risky it is once it is being tried. For the moment, with your apparent agreement above, I'll see whether I can put together a relaxation patch for the vIRQ sending. Main question is going to be whether in the process I wouldn't find a reason why this isn't a safe thing to do.

Jan

#13 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Hi Jan,

Quoted 84 lines · Jan Beulich
Quoted 14 lines · Julien Grall
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Julien Grall
Quoted 7 lines · Jan Beulich
Quoted 3 lines · Jürgen Groß

Especially Julien was rather worried by the current situation. In case you can convince him the current handling is fine, we can easily drop this patch.

Julien, in the light of the above - can you clarify the specific concerns you (still) have?

Let me start with that the assumption if evtchn->lock is not held when evtchn_fifo_set_pending() is called. If it is held, then my comment is moot.

Quoted 7 lines
Quoted 4 lines

But this isn't interesting - we know there are paths where it is held, and ones (interdomain sending) where it's the remote port's lock instead which is held. What's important here is that a _consistent_ lock be held (but it doesn't need to be evtchn's).

Yes, a _consistent_ lock *should* be sufficient. But it is better to use the same lock everywhere so it is easier to reason (see more below).

But that's already not the case, due to the way interdomain channels have events sent. You did suggest acquiring both locks, but as indicated at the time I think this goes too far. As far as the doc aspect - we can improve the situation. Iirc it was you who made me add the respective comment ahead of struct evtchn_port_ops.

Quoted 55 lines
Quoted 43 lines
Quoted 27 lines

From my understanding, the goal of lock_old_queue() is to return the
old queue used.

last_priority and last_vcpu_id may be updated separately and I could not convince myself that it would not be possible to return a queue that is neither the current one nor the old one.

The following could happen if evtchn->priority and evtchn->notify_vcpu_id keeps changing between calls.

pCPU0 | pCPU1

				|
evtchn_fifo_set_pending(v0,...)	|
				| evtchn_fifo_set_pending(v1, ...)
    [...]				|
    /* Queue has changed */	|
    evtchn->last_vcpu_id = v0 	|
				| -> evtchn_old_queue()
				| v = d->vcpu[evtchn->last_vcpu_id];
     				| old_q = ...
				| spin_lock(old_q->...)
				| v = ...
				| q = ...
				| /* q and old_q would be the same */
				|
    evtchn->las_priority = priority|
Quoted 2 lines

If my diagram is correct, then pCPU1 would return a queue that is neither the current nor old one.

I think I agree.

Quoted 11 lines

In which case, I think it would at least be possible to corrupt the queue. From evtchn_fifo_set_pending():

           /*
            * If this event was a tail, the old queue is now empty and
            * its tail must be invalidated to prevent adding an event to
            * the old queue from corrupting the new queue.
            */
           if ( old_q->tail == port )
               old_q->tail = 0;
Quoted 6 lines
Quoted 1 line

Did I miss anything?

I don't think you did. The important point though is that a consistent lock is being held whenever we come here, so two racing set_pending() aren't possible for one and the same evtchn. As a result I don't think the patch here is actually needed.

I haven't yet read in full details the rest of the patches to say whether this is necessary or not. However, at a first glance, I think this is not a sane to rely on different lock to protect us. And don't get me started on the lack of documentation...

Quoted 5 lines
Quoted 2 lines

Furthermore, the implementation of old_lock_queue() suggests that the code was planned to be lockless. Why would you need the loop otherwise?

The lock-less aspect of this affects multiple accesses to e.g. the same queue, I think.

I don't think we are talking about the same thing. What I was referring to is the following code:

static struct evtchn_fifo_queue *lock_old_queue(const struct domain *d,
struct evtchn *evtchn, unsigned long *flags)

{
    struct vcpu *v;
    struct evtchn_fifo_queue *q, *old_q;
    unsigned int try;

    for ( try = 0; try < 3; try++ )
    {
        v = d->vcpu[evtchn->last_vcpu_id];
        old_q = &v->evtchn_fifo->queue[evtchn->last_priority];

        spin_lock_irqsave(&old_q->lock, *flags);

        v = d->vcpu[evtchn->last_vcpu_id];
        q = &v->evtchn_fifo->queue[evtchn->last_priority];

        if ( old_q == q )
            return old_q;

        spin_unlock_irqrestore(&old_q->lock, *flags);
    }

    gprintk(XENLOG_WARNING,
            "dom%d port %d lost event (too many queue changes)\n",
            d->domain_id, evtchn->port);
    return NULL;
}

Given that evtchn->last_vcpu_id and evtchn->last_priority can only be modified in evtchn_fifo_set_pending(), this suggests that it is expected for the function to multiple called concurrently on the same event channel.

Quoted 2 lines

I'm unconvinced it was really considered
whether racing sending on the same channel is also safe this way.

How would you explain the 3 try in lock_old_queue then?

Quoted 7 lines
Quoted 3 lines

Therefore, regardless the rest of the discussion, I think this patch would be useful to have for our peace of mind.

That's a fair position to take. My counterargument is mainly that readability (and hence maintainability) suffers with those changes.

We surely have different opinion... I don't particularly care about the approach as long as it is *properly* documented.

Cheers,

Signature · 1 line
Julien Grall
#14 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 125 lines · Julien Grall

Hi Jan,

Quoted 84 lines · Jan Beulich
Quoted 14 lines · Julien Grall
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Julien Grall
Quoted 7 lines · Jan Beulich
Quoted 3 lines · Jürgen Groß

Especially Julien was rather worried by the current situation. In case you can convince him the current handling is fine, we can easily drop this patch.

Julien, in the light of the above - can you clarify the specific concerns you (still) have?

Let me start with that the assumption if evtchn->lock is not held when evtchn_fifo_set_pending() is called. If it is held, then my comment is moot.

Quoted 7 lines
Quoted 4 lines

But this isn't interesting - we know there are paths where it is held, and ones (interdomain sending) where it's the remote port's lock instead which is held. What's important here is that a _consistent_ lock be held (but it doesn't need to be evtchn's).

Yes, a _consistent_ lock *should* be sufficient. But it is better to use the same lock everywhere so it is easier to reason (see more below).

But that's already not the case, due to the way interdomain channels have events sent. You did suggest acquiring both locks, but as indicated at the time I think this goes too far. As far as the doc aspect - we can improve the situation. Iirc it was you who made me add the respective comment ahead of struct evtchn_port_ops.

Quoted 55 lines
Quoted 43 lines
Quoted 27 lines

From my understanding, the goal of lock_old_queue() is to return the
old queue used.

last_priority and last_vcpu_id may be updated separately and I could not convince myself that it would not be possible to return a queue that is neither the current one nor the old one.

The following could happen if evtchn->priority and evtchn->notify_vcpu_id keeps changing between calls.

pCPU0 | pCPU1

				|
evtchn_fifo_set_pending(v0,...)	|
				| evtchn_fifo_set_pending(v1, ...)
    [...]				|
    /* Queue has changed */	|
    evtchn->last_vcpu_id = v0 	|
				| -> evtchn_old_queue()
				| v = d->vcpu[evtchn->last_vcpu_id];
     				| old_q = ...
				| spin_lock(old_q->...)
				| v = ...
				| q = ...
				| /* q and old_q would be the same */
				|
    evtchn->las_priority = priority|
Quoted 2 lines

If my diagram is correct, then pCPU1 would return a queue that is neither the current nor old one.

I think I agree.

Quoted 11 lines

In which case, I think it would at least be possible to corrupt the queue. From evtchn_fifo_set_pending():

           /*
            * If this event was a tail, the old queue is now empty and
            * its tail must be invalidated to prevent adding an event to
            * the old queue from corrupting the new queue.
            */
           if ( old_q->tail == port )
               old_q->tail = 0;
Quoted 6 lines
Quoted 1 line

Did I miss anything?

I don't think you did. The important point though is that a consistent lock is being held whenever we come here, so two racing set_pending() aren't possible for one and the same evtchn. As a result I don't think the patch here is actually needed.

I haven't yet read in full details the rest of the patches to say whether this is necessary or not. However, at a first glance, I think this is not a sane to rely on different lock to protect us. And don't get me started on the lack of documentation...

Quoted 5 lines
Quoted 2 lines

Furthermore, the implementation of old_lock_queue() suggests that the code was planned to be lockless. Why would you need the loop otherwise?

The lock-less aspect of this affects multiple accesses to e.g. the same queue, I think.

I don't think we are talking about the same thing. What I was referring to is the following code:

static struct evtchn_fifo_queue *lock_old_queue(const struct domain *d,
struct evtchn *evtchn, unsigned long *flags)

{
     struct vcpu *v;
     struct evtchn_fifo_queue *q, *old_q;
     unsigned int try;

     for ( try = 0; try < 3; try++ )
     {
         v = d->vcpu[evtchn->last_vcpu_id];
         old_q = &v->evtchn_fifo->queue[evtchn->last_priority];

         spin_lock_irqsave(&old_q->lock, *flags);

         v = d->vcpu[evtchn->last_vcpu_id];
         q = &v->evtchn_fifo->queue[evtchn->last_priority];

         if ( old_q == q )
             return old_q;

         spin_unlock_irqrestore(&old_q->lock, *flags);
     }

     gprintk(XENLOG_WARNING,
             "dom%d port %d lost event (too many queue changes)\n",
             d->domain_id, evtchn->port);
     return NULL;
}
Quoted 8 lines

Given that evtchn->last_vcpu_id and evtchn->last_priority can only be modified in evtchn_fifo_set_pending(), this suggests that it is expected for the function to multiple called concurrently on the same event channel.

Quoted 2 lines

I'm unconvinced it was really considered
whether racing sending on the same channel is also safe this way.

How would you explain the 3 try in lock_old_queue then?

Queue changes (as said by the gprintk()) can't result from sending alone, but require re-binding to a different vCPU or altering the priority. I'm simply unconvinced that the code indeed fully reflects the original intentions. IOW I'm unsure whether we talk about the same thing ...

Jan

#15 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 5 lines · Jan Beulich
Quoted 4 lines · Julien Grall

Given that evtchn->last_vcpu_id and evtchn->last_priority can only be modified in evtchn_fifo_set_pending(), this suggests that it is expected for the function to multiple called concurrently on the same event channel.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I'm unconvinced it was really considered
whether racing sending on the same channel is also safe this way.

How would you explain the 3 try in lock_old_queue then?

Queue changes (as said by the gprintk()) can't result from sending alone, but require re-binding to a different vCPU or altering the priority.

I agree with that. However, this doesn't change the fact that update to evtchn->last_priority and evtchn->last_vcpu can only happen when calling evtchn_fifo_set_pending().

If evtchn_fifo_set_pending() cannot be called concurrently for the same event, then there is *no* way for evtchn->last_{priority, vcpu} to be updated concurrently.

Quoted 2 lines

I'm simply unconvinced that the code indeed fully reflects the original intentions.

Do you mind (re-)sharing what was the original intentions?

Cheers,

Signature · 1 line
Julien Grall
#16 lore raw

Re: [PATCH v2 1/2] xen/events: access last_priority and last_vcpu_id together

Quoted 21 lines · Julien Grall
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Julien Grall

Given that evtchn->last_vcpu_id and evtchn->last_priority can only be modified in evtchn_fifo_set_pending(), this suggests that it is expected for the function to multiple called concurrently on the same event channel.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I'm unconvinced it was really considered
whether racing sending on the same channel is also safe this way.

How would you explain the 3 try in lock_old_queue then?

Queue changes (as said by the gprintk()) can't result from sending alone, but require re-binding to a different vCPU or altering the priority.

I agree with that. However, this doesn't change the fact that update to evtchn->last_priority and evtchn->last_vcpu can only happen when calling evtchn_fifo_set_pending().

Quoted 8 lines

If evtchn_fifo_set_pending() cannot be called concurrently for the same event, then there is *no* way for evtchn->last_{priority, vcpu} to be updated concurrently.

Quoted 2 lines

I'm simply unconvinced that the code indeed fully reflects the original intentions.

Do you mind (re-)sharing what was the original intentions?

If only I knew, I would.

Jan

#17 lore raw

PATCH v2 2/2 xen/evtchn: rework per event channel lock

In reply to Juergen Gross

Currently the lock for a single event channel needs to be taken with interrupts off, which causes deadlocks in some cases.

Rework the per event channel lock to be non-blocking for the case of sending an event and removing the need for disabling interrupts for taking the lock.

The lock is needed for avoiding races between sending an event or querying the channel's state against removal of the event channel.

Use a locking scheme similar to a rwlock, but with some modifications:

- sending an event or querying the event channel's state uses an operation similar to read_trylock(), in case of not obtaining the lock the sending is omitted or a default state is returned

- closing an event channel is similar to write_lock(), but without real fairness regarding multiple writers (this saves some space in the event channel structure and multiple writers are impossible as closing an event channel requires the domain's event_lock to be held).

With this locking scheme it is mandatory that a writer will always either start with an unbound or free event channel or will end with an unbound or free event channel, as otherwise the reaction of a reader not getting the lock would be wrong.

Fixes e045199c7c9c54 ("evtchn: address races with evtchn_reset()")

---
V2:
- added needed barriers

---
 xen/arch/x86/irq.c         |   6 +-
 xen/arch/x86/pv/shim.c     |   9 +--
 xen/common/event_channel.c | 109 +++++++++++++++++--------------------
 xen/include/xen/event.h    |  56 ++++++++++++++++---
 xen/include/xen/sched.h    |   2 +-
 5 files changed, 106 insertions(+), 76 deletions(-)
xen/arch/x86/irq.c +2 -4
index 93c4fb9a79..77290032f5 100644
@@ -2495,14 +2495,12 @@ static void dump_irqs(unsigned char key)
                 pirq = domain_irq_to_pirq(d, irq);
                 info = pirq_info(d, pirq);
                 evtchn = evtchn_from_port(d, info->evtchn);
-                local_irq_disable();
-                if ( spin_trylock(&evtchn->lock) )
+                if ( evtchn_tryread_lock(evtchn) )
                 {
                     pending = evtchn_is_pending(d, evtchn);
                     masked = evtchn_is_masked(d, evtchn);
-                    spin_unlock(&evtchn->lock);
+                    evtchn_read_unlock(evtchn);
                 }
-                local_irq_enable();
                 printk("d%d:%3d(%c%c%c)%c",
                        d->domain_id, pirq, "-P?"[pending],
                        "-M?"[masked], info->masked ? 'M' : '-',
xen/arch/x86/pv/shim.c +5 -4
index 9aef7a860a..3734250bf7 100644
@@ -660,11 +660,12 @@ void pv_shim_inject_evtchn(unsigned int port)
     if ( port_is_valid(guest, port) )
     {
         struct evtchn *chn = evtchn_from_port(guest, port);
-        unsigned long flags;
 
-        spin_lock_irqsave(&chn->lock, flags);
-        evtchn_port_set_pending(guest, chn->notify_vcpu_id, chn);
-        spin_unlock_irqrestore(&chn->lock, flags);
+        if ( evtchn_tryread_lock(chn) )
+        {
+            evtchn_port_set_pending(guest, chn->notify_vcpu_id, chn);
+            evtchn_read_unlock(chn);
+        }
     }
 }
 
xen/common/event_channel.c +51 -58

336 lines not on this page · raw message

xen/include/xen/event.h +47 -9

77 lines not on this page · raw message

xen/include/xen/sched.h +1 -1
index a298ff4df8..096e0ec6af 100644
@@ -85,7 +85,7 @@ extern domid_t hardware_domid;
 
 struct evtchn
 {
-    spinlock_t lock;
+    atomic_t lock;         /* kind of rwlock, use evtchn_*_[un]lock()        */
 #define ECS_FREE         0 /* Channel is available for use.                  */
 #define ECS_RESERVED     1 /* Channel is reserved.                           */
 #define ECS_UNBOUND      2 /* Channel is waiting to bind to a remote domain. */
Signature · 1 line
2.26.2
#18 lore raw

Re: [PATCH v2 2/2] xen/evtchn: rework per event channel lock

Quoted 7 lines · Juergen Gross

Currently the lock for a single event channel needs to be taken with interrupts off, which causes deadlocks in some cases.

Rework the per event channel lock to be non-blocking for the case of sending an event and removing the need for disabling interrupts for taking the lock.

Quoted 8 lines

The lock is needed for avoiding races between sending an event or querying the channel's state against removal of the event channel.

Use a locking scheme similar to a rwlock, but with some modifications:

- sending an event or querying the event channel's state uses an operation similar to read_trylock(), in case of not obtaining the lock the sending is omitted or a default state is returned

And how come omitting the send or returning default state is valid?

Jan

#19 lore raw

Re: [PATCH v2 2/2] xen/evtchn: rework per event channel lock

Quoted 11 lines · Jan Beulich
Quoted 10 lines · Juergen Gross

Currently the lock for a single event channel needs to be taken with interrupts off, which causes deadlocks in some cases.

Rework the per event channel lock to be non-blocking for the case of sending an event and removing the need for disabling interrupts for taking the lock.

The lock is needed for avoiding races between sending an event or querying the channel's state against removal of the event channel.

Quoted 7 lines
Quoted 5 lines

Use a locking scheme similar to a rwlock, but with some modifications:

- sending an event or querying the event channel's state uses an operation similar to read_trylock(), in case of not obtaining the lock the sending is omitted or a default state is returned

And how come omitting the send or returning default state is valid?

This is explained in the part of the commit message you didn't cite:

With this locking scheme it is mandatory that a writer will always either start with an unbound or free event channel or will end with an unbound or free event channel, as otherwise the reaction of a reader not getting the lock would be wrong.

Juergen

#20 lore raw

Re: [PATCH v2 2/2] xen/evtchn: rework per event channel lock

Quoted 18 lines · Jürgen Groß
Quoted 17 lines · Jan Beulich
Quoted 15 lines · Juergen Gross

Currently the lock for a single event channel needs to be taken with interrupts off, which causes deadlocks in some cases.

Rework the per event channel lock to be non-blocking for the case of sending an event and removing the need for disabling interrupts for taking the lock.

The lock is needed for avoiding races between sending an event or querying the channel's state against removal of the event channel.

Use a locking scheme similar to a rwlock, but with some modifications:

- sending an event or querying the event channel's state uses an operation similar to read_trylock(), in case of not obtaining the lock the sending is omitted or a default state is returned

Quoted 8 lines
Quoted 1 line

And how come omitting the send or returning default state is valid?

This is explained in the part of the commit message you didn't cite:

With this locking scheme it is mandatory that a writer will always either start with an unbound or free event channel or will end with an unbound or free event channel, as otherwise the reaction of a reader not getting the lock would be wrong.

Oh, I did read this latter part as something extra to be aware of, not as this being the correctness guarantee. Could you make the connection more clear?

Jan

#21 lore raw

Re: [PATCH v2 2/2] xen/evtchn: rework per event channel lock

In reply to Juergen Gross
Quoted 16 lines · Juergen Gross
xen/common/event_channel.c +5 -3
@@ -798,9 +786,11 @@ void send_guest_vcpu_virq(struct vcpu *v, uint32_t virq)
 
     d = v->domain;
     chn = evtchn_from_port(d, port);
-    spin_lock(&chn->lock);
-    evtchn_port_set_pending(d, v->vcpu_id, chn);
-    spin_unlock(&chn->lock);
+    if ( evtchn_tryread_lock(chn) )
+    {
+        evtchn_port_set_pending(d, v->vcpu_id, chn);
+        evtchn_read_unlock(chn);
+    }
 
  out:
     spin_unlock_irqrestore(&v->virq_lock, flags);

xen/common/event_channel.c void send_guest_global_virq(struct domain *d, uint32_t virq) · line 824

Quoted 9 lines
xen/common/event_channel.c +3 -3
@@ -829,9 +819,11 @@ void send_guest_global_virq(struct domain *d, uint32_t virq)
         goto out;
 
     chn = evtchn_from_port(d, port);
-    spin_lock(&chn->lock);
-    evtchn_port_set_pending(d, chn->notify_vcpu_id, chn);
-    spin_unlock(&chn->lock);
+    if ( evtchn_tryread_lock(chn) )
+    {
+        evtchn_port_set_pending(d, v->vcpu_id, chn);

Is this simply a copy-and-paste mistake (re-using the code from send_guest_vcpu_virq()), or is there a reason you switch from where to obtain the vCPU to send to (in which case this ought to be mentioned in the description, and in which case you could use literal zero)?

xen/include/xen/event.h void notify_via_xen_event_channel(struct domain *ld, int lport); · line 108

Quoted 7 lines
xen/include/xen/event.h +1 -0
@@ -105,6 +105,45 @@ void notify_via_xen_event_channel(struct domain *ld, int lport);
 #define bucket_from_port(d, p) \
     ((group_from_port(d, p))[((p) % EVTCHNS_PER_GROUP) / EVTCHNS_PER_BUCKET])
 
+#define EVENT_WRITE_LOCK_INC    MAX_VIRT_CPUS

Isn't the ceiling on simultaneous readers the number of pCPU-s, and the value here then needs to be NR_CPUS + 1 to accommodate the maximum number of readers? Furthermore, with you dropping the disabling of interrupts, one pCPU can acquire a read lock now more than once, when interrupting a locked region.

Quoted 9 lines
xen/include/xen/event.h +9 -0
+static inline void evtchn_write_lock(struct evtchn *evtchn)
+{
+    int val;
+
+    /* No barrier needed, atomic_add_return() is full barrier. */
+    for ( val = atomic_add_return(EVENT_WRITE_LOCK_INC, &evtchn->lock);
+          val != EVENT_WRITE_LOCK_INC;
+          val = atomic_read(&evtchn->lock) )
+        cpu_relax();

xen/include/xen/event.h void notify_via_xen_event_channel(struct domain *ld, int lport); · line 127

Quoted 10 lines
xen/include/xen/event.h +10 -0
+}
+
+static inline void evtchn_write_unlock(struct evtchn *evtchn)
+{
+    arch_lock_release_barrier();
+
+    atomic_sub(EVENT_WRITE_LOCK_INC, &evtchn->lock);
+}
+
+static inline bool evtchn_tryread_lock(struct evtchn *evtchn)

The corresponding "generic" function is read_trylock() - I'd suggest to use the same base name, with the evtchn_ prefix.

Quoted 5 lines
xen/include/xen/event.h +0 -1
@@ -274,12 +312,12 @@ static inline int evtchn_port_poll(struct domain *d, evtchn_port_t port)
     if ( port_is_valid(d, port) )
     {
         struct evtchn *evtchn = evtchn_from_port(d, port);
-        unsigned long flags;

xen/include/xen/event.h static inline int evtchn_port_poll(struct domain *d, evtchn_port_t port) · line 321

Quoted 10 lines
xen/include/xen/event.h +4 -3
 
-        spin_lock_irqsave(&evtchn->lock, flags);
-        if ( evtchn_usable(evtchn) )
+        if ( evtchn_tryread_lock(evtchn) && evtchn_usable(evtchn) )
+        {
             rc = evtchn_is_pending(d, evtchn);
-        spin_unlock_irqrestore(&evtchn->lock, flags);
+            evtchn_read_unlock(evtchn);
+        }
     }

This needs to be two nested if()-s, as you need to drop the lock even when evtchn_usable() returns false.

Jan

#22 lore raw

Re: [PATCH v2 2/2] xen/evtchn: rework per event channel lock

Quoted 19 lines · Jan Beulich
Quoted 18 lines · Juergen Gross
xen/common/event_channel.c +5 -3
@@ -798,9 +786,11 @@ void send_guest_vcpu_virq(struct vcpu *v, uint32_t virq)
 
      d = v->domain;
      chn = evtchn_from_port(d, port);
-    spin_lock(&chn->lock);
-    evtchn_port_set_pending(d, v->vcpu_id, chn);
-    spin_unlock(&chn->lock);
+    if ( evtchn_tryread_lock(chn) )
+    {
+        evtchn_port_set_pending(d, v->vcpu_id, chn);
+        evtchn_read_unlock(chn);
+    }
 
   out:
      spin_unlock_irqrestore(&v->virq_lock, flags);
@@ -829,9 +819,11 @@ void send_guest_global_virq(struct domain *d, uint32_t virq)
          goto out;
 
Quoted 13 lines

xen/common/event_channel.c void send_guest_global_virq(struct domain *d, uint32_t virq) · line 824

Quoted 7 lines
xen/common/event_channel.c +3 -3
      chn = evtchn_from_port(d, port);
-    spin_lock(&chn->lock);
-    evtchn_port_set_pending(d, chn->notify_vcpu_id, chn);
-    spin_unlock(&chn->lock);
+    if ( evtchn_tryread_lock(chn) )
+    {
+        evtchn_port_set_pending(d, v->vcpu_id, chn);

Is this simply a copy-and-paste mistake (re-using the code from send_guest_vcpu_virq()), or is there a reason you switch from where to obtain the vCPU to send to (in which case this ought to be mentioned in the description, and in which case you could use literal zero)?

Thanks for spotting! Its a copy-and-paste mistake.

Quoted 7 lines
Quoted 7 lines
xen/include/xen/event.h +0 -0
@@ -105,6 +105,45 @@ void notify_via_xen_event_channel(struct domain *ld, int lport);
  #define bucket_from_port(d, p) \
      ((group_from_port(d, p))[((p) % EVTCHNS_PER_GROUP) / EVTCHNS_PER_BUCKET])
 
Quoted 7 lines

xen/include/xen/event.h void notify_via_xen_event_channel(struct domain *ld, int lport); · line 108

Quoted 1 line
xen/include/xen/event.h +1 -0
+#define EVENT_WRITE_LOCK_INC    MAX_VIRT_CPUS

Isn't the ceiling on simultaneous readers the number of pCPU-s, and the value here then needs to be NR_CPUS + 1 to accommodate the maximum number of readers? Furthermore, with you dropping the disabling of interrupts, one pCPU can acquire a read lock now more than once, when interrupting a locked region.

Yes, I think you are right.

So at least 2 * (NR-CPUS + 1), or even 3 * (NR_CPUS + 1) for covering NMIs, too?

Quoted 15 lines
Quoted 15 lines
xen/include/xen/event.h +14 -0
+static inline void evtchn_write_lock(struct evtchn *evtchn)
+{
+    int val;
+
+    /* No barrier needed, atomic_add_return() is full barrier. */
+    for ( val = atomic_add_return(EVENT_WRITE_LOCK_INC, &evtchn->lock);
+          val != EVENT_WRITE_LOCK_INC;
+          val = atomic_read(&evtchn->lock) )
+        cpu_relax();
+}
+
+static inline void evtchn_write_unlock(struct evtchn *evtchn)
+{
+    arch_lock_release_barrier();
Quoted 8 lines

xen/include/xen/event.h void notify_via_xen_event_channel(struct domain *ld, int lport); · line 127

Quoted 5 lines
xen/include/xen/event.h +5 -0
+
+    atomic_sub(EVENT_WRITE_LOCK_INC, &evtchn->lock);
+}
+
+static inline bool evtchn_tryread_lock(struct evtchn *evtchn)

The corresponding "generic" function is read_trylock() - I'd suggest to use the same base name, with the evtchn_ prefix.

Okay.

Quoted 7 lines
Quoted 7 lines
xen/include/xen/event.h +0 -1
@@ -274,12 +312,12 @@ static inline int evtchn_port_poll(struct domain *d, evtchn_port_t port)
      if ( port_is_valid(d, port) )
      {
          struct evtchn *evtchn = evtchn_from_port(d, port);
-        unsigned long flags;
 
Quoted 12 lines

xen/include/xen/event.h static inline int evtchn_port_poll(struct domain *d, evtchn_port_t port) · line 321

Quoted 9 lines
xen/include/xen/event.h +4 -3
-        spin_lock_irqsave(&evtchn->lock, flags);
-        if ( evtchn_usable(evtchn) )
+        if ( evtchn_tryread_lock(evtchn) && evtchn_usable(evtchn) )
+        {
              rc = evtchn_is_pending(d, evtchn);
-        spin_unlock_irqrestore(&evtchn->lock, flags);
+            evtchn_read_unlock(evtchn);
+        }
      }

This needs to be two nested if()-s, as you need to drop the lock even when evtchn_usable() returns false.

Oh, yes.

Juergen

#23 lore raw

Re: [PATCH v2 2/2] xen/evtchn: rework per event channel lock

Quoted 16 lines · Jürgen Groß
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Juergen Gross
xen/include/xen/event.h +0 -0
@@ -105,6 +105,45 @@ void notify_via_xen_event_channel(struct domain *ld, int lport);
  #define bucket_from_port(d, p) \
      ((group_from_port(d, p))[((p) % EVTCHNS_PER_GROUP) / EVTCHNS_PER_BUCKET])
 
Quoted 7 lines

xen/include/xen/event.h void notify_via_xen_event_channel(struct domain *ld, int lport); · line 108

Quoted 1 line
xen/include/xen/event.h +1 -0
+#define EVENT_WRITE_LOCK_INC    MAX_VIRT_CPUS

Isn't the ceiling on simultaneous readers the number of pCPU-s, and the value here then needs to be NR_CPUS + 1 to accommodate the maximum number of readers? Furthermore, with you dropping the disabling of interrupts, one pCPU can acquire a read lock now more than once, when interrupting a locked region.

Quoted 4 lines

Yes, I think you are right.

So at least 2 * (NR-CPUS + 1), or even 3 * (NR_CPUS + 1) for covering NMIs, too?

Hard to say: Even interrupts can in principle nest. I'd go further and use e.g. INT_MAX / 4, albeit no matter what value we choose there'll remain a theoretical risk. I'm therefore not fully convinced of the concept, irrespective of it providing an elegant solution to the problem at hand. I'd be curious what others think.

Jan

#24 lore raw

Re: [PATCH v2 2/2] xen/evtchn: rework per event channel lock

Quoted 19 lines · Jan Beulich
Quoted 18 lines · Jürgen Groß
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Juergen Gross
xen/include/xen/event.h +0 -0
@@ -105,6 +105,45 @@ void notify_via_xen_event_channel(struct domain *ld, int lport);
   #define bucket_from_port(d, p) \
       ((group_from_port(d, p))[((p) % EVTCHNS_PER_GROUP) / EVTCHNS_PER_BUCKET])
 
Quoted 7 lines

xen/include/xen/event.h void notify_via_xen_event_channel(struct domain *ld, int lport); · line 108

Quoted 1 line
xen/include/xen/event.h +1 -0
+#define EVENT_WRITE_LOCK_INC    MAX_VIRT_CPUS

Isn't the ceiling on simultaneous readers the number of pCPU-s, and the value here then needs to be NR_CPUS + 1 to accommodate the maximum number of readers? Furthermore, with you dropping the disabling of interrupts, one pCPU can acquire a read lock now more than once, when interrupting a locked region.

Yes, I think you are right.

Quoted 8 lines
Quoted 2 lines

So at least 2 * (NR-CPUS + 1), or even 3 * (NR_CPUS + 1) for covering NMIs, too?

Hard to say: Even interrupts can in principle nest. I'd go further and use e.g. INT_MAX / 4, albeit no matter what value we choose there'll remain a theoretical risk. I'm therefore not fully convinced of the concept, irrespective of it providing an elegant solution to the problem at hand. I'd be curious what others think.

I just realized I should add a sanity test in evtchn_write_lock() to exclude the case of multiple writers (this should never happen due to all writers locking d->event_lock).

This in turn means we can set EVENT_WRITE_LOCK_INC to INT_MIN and use negative lock values for a write-locked event channel.

Hitting this limit seems to require quite high values of NR_CPUS, even with interrupts nesting (I'm quite sure we'll run out of stack space way before this limit can be hit even with 16 million cpus).

Juergen

#25 lore raw

Re: [PATCH v2 2/2] xen/evtchn: rework per event channel lock

In reply to Juergen Gross

Hi Juergen,

Quoted 22 lines · Juergen Gross

Currently the lock for a single event channel needs to be taken with interrupts off, which causes deadlocks in some cases.

Rework the per event channel lock to be non-blocking for the case of sending an event and removing the need for disabling interrupts for taking the lock.

The lock is needed for avoiding races between sending an event or querying the channel's state against removal of the event channel.

Use a locking scheme similar to a rwlock, but with some modifications:

- sending an event or querying the event channel's state uses an operation similar to read_trylock(), in case of not obtaining the lock the sending is omitted or a default state is returned

- closing an event channel is similar to write_lock(), but without real fairness regarding multiple writers (this saves some space in the event channel structure and multiple writers are impossible as closing an event channel requires the domain's event_lock to be held).

Quoted 7 lines

With this locking scheme it is mandatory that a writer will always either start with an unbound or free event channel or will end with an unbound or free event channel, as otherwise the reaction of a reader not getting the lock would be wrong.

Fixes e045199c7c9c54 ("evtchn: address races with evtchn_reset()")

The approach looks ok to me. I have a couple of remarks below.

[...]

xen/include/xen/event.h void notify_via_xen_event_channel(struct domain *ld, int lport); · line 109

Quoted 10 lines
xen/include/xen/event.h +2 -0
index 509d3ae861..39a93f7556 100644
@@ -105,6 +105,45 @@ void notify_via_xen_event_channel(struct domain *ld, int lport);
  #define bucket_from_port(d, p) \
      ((group_from_port(d, p))[((p) % EVTCHNS_PER_GROUP) / EVTCHNS_PER_BUCKET])
 
+#define EVENT_WRITE_LOCK_INC    MAX_VIRT_CPUS
+static inline void evtchn_write_lock(struct evtchn *evtchn)

I think it would be good to describe the locking expectation in-code.

Quoted 17 lines
xen/include/xen/event.h +17 -0
+{
+    int val;
+
+    /* No barrier needed, atomic_add_return() is full barrier. */
+    for ( val = atomic_add_return(EVENT_WRITE_LOCK_INC, &evtchn->lock);
+          val != EVENT_WRITE_LOCK_INC;
+          val = atomic_read(&evtchn->lock) )
+        cpu_relax();
+}
+
+static inline void evtchn_write_unlock(struct evtchn *evtchn)
+{
+    arch_lock_release_barrier();
+
+    atomic_sub(EVENT_WRITE_LOCK_INC, &evtchn->lock);
+}
+

xen/include/xen/event.h void notify_via_xen_event_channel(struct domain *ld, int lport); · line 136

Quoted 10 lines
xen/include/xen/event.h +10 -0
+static inline bool evtchn_tryread_lock(struct evtchn *evtchn)
+{
+    if ( atomic_read(&evtchn->lock) >= EVENT_WRITE_LOCK_INC )
+        return false;
+
+    /* No barrier needed, atomic_inc_return() is full barrier. */
+    if ( atomic_inc_return(&evtchn->lock) < EVENT_WRITE_LOCK_INC )
+        return true;
+
+    atomic_dec(&evtchn->lock);

NIT: Can you add a newline here?

xen/include/xen/event.h void notify_via_xen_event_channel(struct domain *ld, int lport); · line 139

Quoted 3 lines
xen/include/xen/event.h +3 -0
+    return false;
+}
+

Cheers,

Signature · 1 line
Julien Grall