Skip to contentSkip to conversation

XSA-343 followup patches

PATCH Superseded Juergen Gross 3 participants 13 messages Oct 16, 2020 – Nov 4, 2020

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v3 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.

Changes in V3:
- addressed comments

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    |  76 ++++++++++++++++++++++----
 xen/include/xen/sched.h    |   5 +-
 6 files changed, 145 insertions(+), 85 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH v3 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..2f4e8c54fc 100644
@@ -42,6 +42,14 @@ struct evtchn_fifo_domain {
     unsigned int num_evtchns;
 };
 
+union evtchn_fifo_lastq {
+    uint32_t raw;
+    struct {
+        uint8_t last_priority;
+        uint16_t 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
Reviewed-by on v3 1/2
#2 lore raw

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

Hi Juergen,

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.

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw

PATCH v3 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).

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

---
V3:
- corrected a copy-and-paste error (Jan Beulich)
- corrected unlocking in two cases (Jan Beulich)
- renamed evtchn_read_trylock() (Jan Beulich)
- added some comments and an ASSERT() for evtchn_write_lock()
- set EVENT_WRITE_LOCK_INC to INT_MIN

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    |  76 ++++++++++++++++++++++----
 xen/include/xen/sched.h    |   2 +-
 5 files changed, 125 insertions(+), 77 deletions(-)
xen/arch/x86/irq.c +2 -4
index 93c4fb9a79..8d1f9a9fc6 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_read_trylock(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..b4e83e0778 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_read_trylock(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 +66 -10

96 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
#4 lore raw

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

Quoted 6 lines · Juergen Gross
xen/arch/x86/pv/shim.c +0 -0
@@ -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);

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 668

Quoted 10 lines
xen/arch/x86/pv/shim.c +5 -4
-        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_read_trylock(chn) )
+        {
+            evtchn_port_set_pending(guest, chn->notify_vcpu_id, chn);
+            evtchn_read_unlock(chn);
+        }

Does this want some form of else, e.g. at least a printk_once()?

xen/arch/x86/pv/shim.c static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind) · line 355

Quoted 6 lines
xen/arch/x86/pv/shim.c +1 -1
@@ -360,7 +352,7 @@ static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
     if ( rc )
         goto out;
 
-    flags = double_evtchn_lock(lchn, rchn);
+    double_evtchn_lock(lchn, rchn);

This introduces an unfortunate conflict with my conversion of the per-domain event lock to an rw one: It acquires rd's lock in read mode only, while the requirements here would not allow doing so. (Same in evtchn_close() then.)

xen/arch/x86/pv/shim.c int evtchn_send(struct domain *ld, unsigned int lport) · line 727

Quoted 7 lines
xen/arch/x86/pv/shim.c +2 -1
@@ -736,7 +723,8 @@ int evtchn_send(struct domain *ld, unsigned int lport)
 
     lchn = evtchn_from_port(ld, lport);
 
-    spin_lock_irqsave(&lchn->lock, flags);
+    if ( !evtchn_read_trylock(lchn) )
+        return 0;

With this, the auxiliary call to xsm_evtchn_send() up from here should also go away again (possibly in a separate follow- on, which would then likely be a clean revert).

Quoted 20 lines
xen/arch/x86/pv/shim.c +5 -4
@@ -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_read_trylock(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;
 
     chn = evtchn_from_port(d, port);
-    spin_lock(&chn->lock);

xen/arch/x86/pv/shim.c void send_guest_global_virq(struct domain *d, uint32_t virq) · line 829

Quoted 10 lines
xen/arch/x86/pv/shim.c +5 -2
-    evtchn_port_set_pending(d, chn->notify_vcpu_id, chn);
-    spin_unlock(&chn->lock);
+    if ( evtchn_read_trylock(chn) )
+    {
+        evtchn_port_set_pending(d, chn->notify_vcpu_id, chn);
+        evtchn_read_unlock(chn);
+    }
 
  out:
     spin_unlock_irqrestore(&v->virq_lock, flags);

As said before, I think these lock uses can go away altogether. I shall put together a patch.

And on the whole I'd really prefer if we first convinced ourselves that there's no way to simply get rid of the IRQ-safe locking forms instead, before taking a decision to go with this model with its extra constraints.

Quoted 8 lines
xen/arch/x86/pv/shim.c +0 -1
@@ -1060,15 +1053,16 @@ int evtchn_unmask(unsigned int port)
 {
     struct domain *d = current->domain;
     struct evtchn *evtchn;
-    unsigned long flags;
 
     if ( unlikely(!port_is_valid(d, port)) )
         return -EINVAL;

xen/arch/x86/pv/shim.c int evtchn_unmask(unsigned int port) · line 1065

Quoted 10 lines
xen/arch/x86/pv/shim.c +5 -3
 
     evtchn = evtchn_from_port(d, port);
-    spin_lock_irqsave(&evtchn->lock, flags);
-    evtchn_port_unmask(d, evtchn);
-    spin_unlock_irqrestore(&evtchn->lock, flags);
+    if ( evtchn_read_trylock(evtchn) )
+    {
+        evtchn_port_unmask(d, evtchn);
+        evtchn_read_unlock(evtchn);
+    }

I think this wants mentioning together with send / query in the description.

Quoted 4 lines
xen/include/xen/event.h +0 -0
@@ -105,6 +105,60 @@ void notify_via_xen_event_channel(struct domain *ld, int lport);
 #define bucket_from_port(d, p) \

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

Quoted 10 lines
xen/include/xen/event.h +8 -0
     ((group_from_port(d, p))[((p) % EVTCHNS_PER_GROUP) / EVTCHNS_PER_BUCKET])
 
+#define EVENT_WRITE_LOCK_INC    INT_MIN
+
+/*
+ * Lock an event channel exclusively. This is allowed only with holding
+ * d->event_lock AND when the channel is free or unbound either when taking
+ * or when releasing the lock, as any concurrent operation on the event
+ * channel using evtchn_read_trylock() will just assume the event channel is
+ * free or unbound at the moment.

... when the evtchn_read_trylock() returns false.

Quoted 4 lines
xen/include/xen/event.h +4 -0
+ */
+static inline void evtchn_write_lock(struct evtchn *evtchn)
+{
+    int val;

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

Quoted 10 lines
xen/include/xen/event.h +10 -0
+
+    /*
+     * The lock can't be held by a writer already, as all writers need to
+     * hold d->event_lock.
+     */
+    ASSERT(atomic_read(&evtchn->lock) >= 0);
+
+    /* 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;

The _INC suffix is slightly odd for this 2nd use, but I guess the dual use will make it so for about any name you may pick.

Quoted 8 lines
xen/include/xen/event.h +8 -0
+          val = atomic_read(&evtchn->lock) )
+        cpu_relax();
+}
+
+static inline void evtchn_write_unlock(struct evtchn *evtchn)
+{
+    arch_lock_release_barrier();
+

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

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

atomic_*_return() return the new value, so I think you mean ">" here?

Jan

#5 lore raw

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

Quoted 9 lines · Jan Beulich
Quoted 8 lines · Juergen Gross
xen/arch/x86/pv/shim.c +0 -1
@@ -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;
 
Quoted 10 lines

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 668

Quoted 8 lines
xen/arch/x86/pv/shim.c +5 -3
-        spin_lock_irqsave(&chn->lock, flags);
-        evtchn_port_set_pending(guest, chn->notify_vcpu_id, chn);
-        spin_unlock_irqrestore(&chn->lock, flags);
+        if ( evtchn_read_trylock(chn) )
+        {
+            evtchn_port_set_pending(guest, chn->notify_vcpu_id, chn);
+            evtchn_read_unlock(chn);
+        }

Does this want some form of else, e.g. at least a printk_once()?

No, I don't think so.

This is just a race with the port_is_valid() test above where the port is just being switched to invalid.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/pv/shim.c +0 -0
@@ -360,7 +352,7 @@ static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
      if ( rc )
          goto out;
 
Quoted 7 lines

xen/arch/x86/pv/shim.c static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind) · line 355

Quoted 2 lines
xen/arch/x86/pv/shim.c +1 -1
-    flags = double_evtchn_lock(lchn, rchn);
+    double_evtchn_lock(lchn, rchn);

This introduces an unfortunate conflict with my conversion of the per-domain event lock to an rw one: It acquires rd's lock in read mode only, while the requirements here would not allow doing so. (Same in evtchn_close() then.)

Is it a problem to use write mode for those cases?

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/pv/shim.c +0 -0
@@ -736,7 +723,8 @@ int evtchn_send(struct domain *ld, unsigned int lport)
 
      lchn = evtchn_from_port(ld, lport);
 
Quoted 7 lines

xen/arch/x86/pv/shim.c int evtchn_send(struct domain *ld, unsigned int lport) · line 727

Quoted 3 lines
xen/arch/x86/pv/shim.c +2 -1
-    spin_lock_irqsave(&lchn->lock, flags);
+    if ( !evtchn_read_trylock(lchn) )
+        return 0;

With this, the auxiliary call to xsm_evtchn_send() up from here should also go away again (possibly in a separate follow- on, which would then likely be a clean revert).

Yes.

Quoted 49 lines
Quoted 21 lines
xen/arch/x86/pv/shim.c +5 -4
@@ -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_read_trylock(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;
 
      chn = evtchn_from_port(d, port);
-    spin_lock(&chn->lock);

xen/arch/x86/pv/shim.c void send_guest_global_virq(struct domain *d, uint32_t virq) · line 829

Quoted 10 lines
xen/arch/x86/pv/shim.c +5 -2
-    evtchn_port_set_pending(d, chn->notify_vcpu_id, chn);
-    spin_unlock(&chn->lock);
+    if ( evtchn_read_trylock(chn) )
+    {
+        evtchn_port_set_pending(d, chn->notify_vcpu_id, chn);
+        evtchn_read_unlock(chn);
+    }
 
   out:
      spin_unlock_irqrestore(&v->virq_lock, flags);

As said before, I think these lock uses can go away altogether. I shall put together a patch.

And on the whole I'd really prefer if we first convinced ourselves that there's no way to simply get rid of the IRQ-safe locking forms instead, before taking a decision to go with this model with its extra constraints.

Quoted 9 lines
xen/arch/x86/pv/shim.c +0 -1
@@ -1060,15 +1053,16 @@ int evtchn_unmask(unsigned int port)
  {
      struct domain *d = current->domain;
      struct evtchn *evtchn;
-    unsigned long flags;
 
      if ( unlikely(!port_is_valid(d, port)) )
          return -EINVAL;
 
Quoted 12 lines

xen/arch/x86/pv/shim.c int evtchn_unmask(unsigned int port) · line 1065

Quoted 9 lines
xen/arch/x86/pv/shim.c +5 -3
      evtchn = evtchn_from_port(d, port);
-    spin_lock_irqsave(&evtchn->lock, flags);
-    evtchn_port_unmask(d, evtchn);
-    spin_unlock_irqrestore(&evtchn->lock, flags);
+    if ( evtchn_read_trylock(evtchn) )
+    {
+        evtchn_port_unmask(d, evtchn);
+        evtchn_read_unlock(evtchn);
+    }

I think this wants mentioning together with send / query in the description.

Okay.

Quoted 7 lines
Quoted 7 lines
xen/include/xen/event.h +0 -0
@@ -105,6 +105,60 @@ 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 10 lines

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

Quoted 8 lines
xen/include/xen/event.h +8 -0
+#define EVENT_WRITE_LOCK_INC    INT_MIN
+
+/*
+ * Lock an event channel exclusively. This is allowed only with holding
+ * d->event_lock AND when the channel is free or unbound either when taking
+ * or when releasing the lock, as any concurrent operation on the event
+ * channel using evtchn_read_trylock() will just assume the event channel is
+ * free or unbound at the moment.

... when the evtchn_read_trylock() returns false.

Okay.

Quoted 10 lines
Quoted 10 lines
xen/include/xen/event.h +9 -0
+ */
+static inline void evtchn_write_lock(struct evtchn *evtchn)
+{
+    int val;
+
+    /*
+     * The lock can't be held by a writer already, as all writers need to
+     * hold d->event_lock.
+     */
Quoted 8 lines

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

Quoted 5 lines
xen/include/xen/event.h +5 -0
+    ASSERT(atomic_read(&evtchn->lock) >= 0);
+
+    /* 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;

The _INC suffix is slightly odd for this 2nd use, but I guess the dual use will make it so for about any name you may pick.

I'll switch to a normal rwlock in V4.

Juergen

#6 lore raw

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

Quoted 21 lines · Jürgen Groß
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Juergen Gross
xen/arch/x86/pv/shim.c +0 -1
@@ -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;
 
Quoted 10 lines

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 668

Quoted 8 lines
xen/arch/x86/pv/shim.c +5 -3
-        spin_lock_irqsave(&chn->lock, flags);
-        evtchn_port_set_pending(guest, chn->notify_vcpu_id, chn);
-        spin_unlock_irqrestore(&chn->lock, flags);
+        if ( evtchn_read_trylock(chn) )
+        {
+            evtchn_port_set_pending(guest, chn->notify_vcpu_id, chn);
+            evtchn_read_unlock(chn);
+        }

Does this want some form of else, e.g. at least a printk_once()?

Quoted 4 lines

No, I don't think so.

This is just a race with the port_is_valid() test above where the port is just being switched to invalid.

This may be such a race yes, but why do you think it _will_ be? Any holding of the lock for writing (or in fact, any pending acquire in write mode) will make this fail, which - if it's not such a race - will mean an event which wasn't sent when it should have been, with potentially fatal (to the guest) consequences.

Quoted 12 lines
Quoted 11 lines

xen/arch/x86/pv/shim.c static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind) · line 355

Quoted 6 lines
xen/arch/x86/pv/shim.c +1 -1
@@ -360,7 +352,7 @@ static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
      if ( rc )
          goto out;
 
-    flags = double_evtchn_lock(lchn, rchn);
+    double_evtchn_lock(lchn, rchn);

This introduces an unfortunate conflict with my conversion of the per-domain event lock to an rw one: It acquires rd's lock in read mode only, while the requirements here would not allow doing so. (Same in evtchn_close() then.)

Quoted 1 line

Is it a problem to use write mode for those cases?

"Problem" can have a wide range of meanings - it's not going to be the end of the world, but I view any use of a write lock as a problem when a read lock would suffice. This can still harm parallelism.

Jan

#7 lore raw

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

Quoted 22 lines · Jan Beulich
Quoted 21 lines · Jürgen Groß
Quoted 19 lines · Jan Beulich
Quoted 6 lines · Juergen Gross
xen/arch/x86/pv/shim.c +0 -0
@@ -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);

xen/arch/x86/pv/shim.c void pv_shim_inject_evtchn(unsigned int port) · line 668

Quoted 10 lines
xen/arch/x86/pv/shim.c +5 -4
-        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_read_trylock(chn) )
+        {
+            evtchn_port_set_pending(guest, chn->notify_vcpu_id, chn);
+            evtchn_read_unlock(chn);
+        }

Does this want some form of else, e.g. at least a printk_once()?

Quoted 6 lines
Quoted 4 lines

No, I don't think so.

This is just a race with the port_is_valid() test above where the port is just being switched to invalid.

This may be such a race yes, but why do you think it _will_ be?

According to the outlined lock discipline there is no other possibility (assuming that the lock discipline is honored).

I'll have a look whether I can add some ASSERT()s to catch any lock discipline violation.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

xen/arch/x86/pv/shim.c static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind) · line 355

Quoted 7 lines
xen/arch/x86/pv/shim.c +1 -1
@@ -360,7 +352,7 @@ static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
       if ( rc )
           goto out;
 
-    flags = double_evtchn_lock(lchn, rchn);
+    double_evtchn_lock(lchn, rchn);

This introduces an unfortunate conflict with my conversion of the per-domain event lock to an rw one: It acquires rd's lock in read mode only, while the requirements here would not allow doing so. (Same in evtchn_close() then.)

Quoted 6 lines
Quoted 1 line

Is it a problem to use write mode for those cases?

"Problem" can have a wide range of meanings - it's not going to be the end of the world, but I view any use of a write lock as a problem when a read lock would suffice. This can still harm parallelism.

Both cases are very rare ones in the life time of an event channel. I don't think you'll ever be able to measure any performance impact from switching these case to a write lock for any well behaved guest.

Juergen

#8 lore raw

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

Quoted 18 lines · Jürgen Groß
Quoted 17 lines · Jan Beulich
Quoted 15 lines · Jürgen Groß
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Juergen Gross
xen/common/event_channel.c +0 -0
@@ -360,7 +352,7 @@ static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
       if ( rc )
           goto out;
 
Quoted 7 lines

xen/common/event_channel.c static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind) · line 355

Quoted 2 lines
xen/common/event_channel.c +1 -1
-    flags = double_evtchn_lock(lchn, rchn);
+    double_evtchn_lock(lchn, rchn);

This introduces an unfortunate conflict with my conversion of the per-domain event lock to an rw one: It acquires rd's lock in read mode only, while the requirements here would not allow doing so. (Same in evtchn_close() then.)

Is it a problem to use write mode for those cases?

Quoted 8 lines
Quoted 4 lines

"Problem" can have a wide range of meanings - it's not going to be the end of the world, but I view any use of a write lock as a problem when a read lock would suffice. This can still harm parallelism.

Both cases are very rare ones in the life time of an event channel. I don't think you'll ever be able to measure any performance impact from switching these case to a write lock for any well behaved guest.

I agree as far as the lifetime of an individual port goes, but we're talking about the per-domain lock here. (Perhaps my choice of context in your patch wasn't the best one, as there it is the per-channel lock of which two instances get acquired. I'm sorry if this has lead to any confusion.)

Jan

#9 lore raw

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

Quoted 28 lines · Jan Beulich
Quoted 18 lines · Jürgen Groß
Quoted 17 lines · Jan Beulich
Quoted 15 lines · Jürgen Groß
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Juergen Gross
xen/common/event_channel.c +0 -0
@@ -360,7 +352,7 @@ static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
        if ( rc )
            goto out;
 
Quoted 7 lines

xen/common/event_channel.c static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind) · line 355

Quoted 2 lines
xen/common/event_channel.c +1 -1
-    flags = double_evtchn_lock(lchn, rchn);
+    double_evtchn_lock(lchn, rchn);

This introduces an unfortunate conflict with my conversion of the per-domain event lock to an rw one: It acquires rd's lock in read mode only, while the requirements here would not allow doing so. (Same in evtchn_close() then.)

Is it a problem to use write mode for those cases?

Quoted 8 lines
Quoted 4 lines

"Problem" can have a wide range of meanings - it's not going to be the end of the world, but I view any use of a write lock as a problem when a read lock would suffice. This can still harm parallelism.

Both cases are very rare ones in the life time of an event channel. I don't think you'll ever be able to measure any performance impact from switching these case to a write lock for any well behaved guest.

Quoted 5 lines

I agree as far as the lifetime of an individual port goes, but we're talking about the per-domain lock here. (Perhaps my choice of context in your patch wasn't the best one, as there it is the per-channel lock of which two instances get acquired. I'm sorry if this has lead to any confusion.)

Hmm, with the switch to an ordinary rwlock it should be fine to drop the requirement to hold the domain's event channel lock exclusively for taking the per-channel lock as a writer.

Juergen

#10 lore raw

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

Hi Juergen,

Quoted 35 lines · Jürgen Groß
Quoted 28 lines · Jan Beulich
Quoted 18 lines · Jürgen Groß
Quoted 17 lines · Jan Beulich
Quoted 15 lines · Jürgen Groß
Quoted 12 lines · Jan Beulich

Review static long · line 355

Quoted 6 lines · Juergen Gross
(quoted diff) +1 -1
@@ -360,7 +352,7 @@ static long 
 evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
         if ( rc )
             goto out;
-    flags = double_evtchn_lock(lchn, rchn);
+    double_evtchn_lock(lchn, rchn);

This introduces an unfortunate conflict with my conversion of the per-domain event lock to an rw one: It acquires rd's lock in read mode only, while the requirements here would not allow doing so. (Same in evtchn_close() then.)

Is it a problem to use write mode for those cases?

Quoted 8 lines
Quoted 4 lines

"Problem" can have a wide range of meanings - it's not going to be the end of the world, but I view any use of a write lock as a problem when a read lock would suffice. This can still harm parallelism.

Both cases are very rare ones in the life time of an event channel. I don't think you'll ever be able to measure any performance impact from switching these case to a write lock for any well behaved guest.

Quoted 5 lines

I agree as far as the lifetime of an individual port goes, but we're talking about the per-domain lock here. (Perhaps my choice of context in your patch wasn't the best one, as there it is the per-channel lock of which two instances get acquired. I'm sorry if this has lead to any confusion.)

Quoted 3 lines

Hmm, with the switch to an ordinary rwlock it should be fine to drop the requirement to hold the domain's event channel lock exclusively for taking the per-channel lock as a writer.

I don't think you can drop d->event_lock. It protects us against allocating new ports while evtchn_reset() is called.

Without it, you are going to re-open XSA-343.

Cheers,

Signature · 1 line
Julien Grall
#11 lore raw

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

Quoted 38 lines · Julien Grall

Hi Juergen,

Quoted 35 lines · Jürgen Groß
Quoted 33 lines · Jan Beulich
Quoted 18 lines · Jürgen Groß
Quoted 17 lines · Jan Beulich
Quoted 15 lines · Jürgen Groß
Quoted 12 lines · Jan Beulich

Review static long · line 355

Quoted 6 lines · Juergen Gross
(quoted diff) +1 -1
@@ -360,7 +352,7 @@ static long 
 evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
         if ( rc )
             goto out;
-    flags = double_evtchn_lock(lchn, rchn);
+    double_evtchn_lock(lchn, rchn);

This introduces an unfortunate conflict with my conversion of the per-domain event lock to an rw one: It acquires rd's lock in read mode only, while the requirements here would not allow doing so. (Same in evtchn_close() then.)

Is it a problem to use write mode for those cases?

Quoted 8 lines
Quoted 4 lines

"Problem" can have a wide range of meanings - it's not going to be the end of the world, but I view any use of a write lock as a problem when a read lock would suffice. This can still harm parallelism.

Both cases are very rare ones in the life time of an event channel. I don't think you'll ever be able to measure any performance impact from switching these case to a write lock for any well behaved guest.

I agree as far as the lifetime of an individual port goes, but we're talking about the per-domain lock here. (Perhaps my choice of context in your patch wasn't the best one, as there it is the per-channel lock of which two instances get acquired. I'm sorry if this has lead to any confusion.)

Quoted 5 lines
Quoted 3 lines

Hmm, with the switch to an ordinary rwlock it should be fine to drop the requirement to hold the domain's event channel lock exclusively for taking the per-channel lock as a writer.

I don't think you can drop d->event_lock. It protects us against allocating new ports while evtchn_reset() is called.

I wrote "exclusively", as in case of a switch to a rwlock it should be fine to hold it as a reader in case the reset coding takes it as a writer.

Quoted 1 line

Without it, you are going to re-open XSA-343.

Yes, of course.

Juergen

#12 lore raw

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

Quoted 43 lines · Jürgen Groß
Quoted 42 lines · Julien Grall

Hi Juergen,

Quoted 38 lines · Jürgen Groß
Quoted 28 lines · Jan Beulich
Quoted 18 lines · Jürgen Groß
Quoted 17 lines · Jan Beulich
Quoted 15 lines · Jürgen Groß
Quoted 12 lines · Jan Beulich

Review static long · line 355

Quoted 6 lines · Juergen Gross
(quoted diff) +1 -1
@@ -360,7 +352,7 @@ static long 
 evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
         if ( rc )
             goto out;
-    flags = double_evtchn_lock(lchn, rchn);
+    double_evtchn_lock(lchn, rchn);

This introduces an unfortunate conflict with my conversion of the per-domain event lock to an rw one: It acquires rd's lock in read mode only, while the requirements here would not allow doing so. (Same in evtchn_close() then.)

Is it a problem to use write mode for those cases?

Quoted 8 lines
Quoted 4 lines

"Problem" can have a wide range of meanings - it's not going to be the end of the world, but I view any use of a write lock as a problem when a read lock would suffice. This can still harm parallelism.

Both cases are very rare ones in the life time of an event channel. I don't think you'll ever be able to measure any performance impact from switching these case to a write lock for any well behaved guest.

Quoted 5 lines

I agree as far as the lifetime of an individual port goes, but we're talking about the per-domain lock here. (Perhaps my choice of context in your patch wasn't the best one, as there it is the per-channel lock of which two instances get acquired. I'm sorry if this has lead to any confusion.)

Hmm, with the switch to an ordinary rwlock it should be fine to drop the requirement to hold the domain's event channel lock exclusively for taking the per-channel lock as a writer.

Quoted 5 lines
Quoted 1 line

I don't think you can drop d->event_lock. It protects us against allocating new ports while evtchn_reset() is called.

I wrote "exclusively", as in case of a switch to a rwlock it should be fine to hold it as a reader in case the reset coding takes it as a writer.

Oh I misread your comment. Sorry for the noise.

Cheers,

Signature · 1 line
Julien Grall