XSA-343 followup patches
v4 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 V4:
- switched to real rwlock
Changes in V3:
- addressed comments
*** BLURB HERE ***
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 | 120 ++++++++++++++++++------------------- xen/common/event_fifo.c | 25 ++++++-- xen/include/xen/event.h | 55 +++++++++++++---- xen/include/xen/sched.h | 6 +- 6 files changed, 131 insertions(+), 90 deletions(-)
Signature · 1 line
2.26.2
PATCH v4 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
@@ -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
@@ -114,8 +114,7 @@ struct evtchnu16 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
PATCH v4 2/2 xen/evtchn: rework per event channel lock
In reply to Juergen GrossCurrently 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 event channel state changes (creation, closing, binding) against normal operations (set pending, [un]masking, priority changes).
Use a rwlock, but with some restrictions:
- normal operations use read_trylock(), in case of not obtaining the lock the operation is omitted or a default state is returned
- closing an event channel is using write_lock(), with ASSERT()ing that the lock is taken as writer only when the state of the event channel is either before or after the locked region appropriate (either free or unbound).
---
V4:
- switch to rwlock
- add ASSERT() to verify correct write_lock() usage
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 | 120 ++++++++++++++++++------------------- xen/include/xen/event.h | 55 +++++++++++++---- xen/include/xen/sched.h | 3 +- 5 files changed, 111 insertions(+), 82 deletions(-)
xen/arch/x86/irq.c +2 -4
@@ -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
@@ -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 +57 -63
358 lines not on this page · raw message
xen/include/xen/event.h +45 -10
75 lines not on this page · raw message
xen/include/xen/sched.h +2 -1
@@ -85,7 +85,7 @@ extern domid_t hardware_domid;struct evtchn { -spinlock_t lock; +rwlock_t 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. */
@@ -114,6 +114,7 @@ struct evtchnu16 virq;/* state == ECS_VIRQ */ } u;u8 priority; +u8 old_state;/* State when taking lock in write mode. */ u32 fifo_lastq;/* Data for fifo events identifying last queue. */ #ifdef CONFIG_XSM union {
Signature · 1 line
2.26.2