Commit 168d2f46 authored by Jeremy Fitzhardinge's avatar Jeremy Fitzhardinge Committed by Ingo Molnar

xen: save previous spinlock when blocking

A spinlock can be interrupted while spinning, so make sure we preserve
the previous lock of interest if we're taking a lock from within an
interrupt handler.

We also need to deal with the case where the blocking path gets
interrupted between testing to see if the lock is free and actually
blocking.  If we get interrupted there and end up in the state where
the lock is free but the irq isn't pending, then we'll block
indefinitely in the hypervisor.  This fix is to make sure that any
nested lock-takers will always leave the irq pending if there's any
chance the outer lock became free.
Signed-off-by: default avatarJeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com>
Acked-by: default avatarJan Beulich <jbeulich@novell.com>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 7708ad64
...@@ -47,25 +47,41 @@ static int xen_spin_trylock(struct raw_spinlock *lock) ...@@ -47,25 +47,41 @@ static int xen_spin_trylock(struct raw_spinlock *lock)
static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
static DEFINE_PER_CPU(struct xen_spinlock *, lock_spinners); static DEFINE_PER_CPU(struct xen_spinlock *, lock_spinners);
static inline void spinning_lock(struct xen_spinlock *xl) /*
* Mark a cpu as interested in a lock. Returns the CPU's previous
* lock of interest, in case we got preempted by an interrupt.
*/
static inline struct xen_spinlock *spinning_lock(struct xen_spinlock *xl)
{ {
struct xen_spinlock *prev;
prev = __get_cpu_var(lock_spinners);
__get_cpu_var(lock_spinners) = xl; __get_cpu_var(lock_spinners) = xl;
wmb(); /* set lock of interest before count */ wmb(); /* set lock of interest before count */
asm(LOCK_PREFIX " incw %0" asm(LOCK_PREFIX " incw %0"
: "+m" (xl->spinners) : : "memory"); : "+m" (xl->spinners) : : "memory");
return prev;
} }
static inline void unspinning_lock(struct xen_spinlock *xl) /*
* Mark a cpu as no longer interested in a lock. Restores previous
* lock of interest (NULL for none).
*/
static inline void unspinning_lock(struct xen_spinlock *xl, struct xen_spinlock *prev)
{ {
asm(LOCK_PREFIX " decw %0" asm(LOCK_PREFIX " decw %0"
: "+m" (xl->spinners) : : "memory"); : "+m" (xl->spinners) : : "memory");
wmb(); /* decrement count before clearing lock */ wmb(); /* decrement count before restoring lock */
__get_cpu_var(lock_spinners) = NULL; __get_cpu_var(lock_spinners) = prev;
} }
static noinline int xen_spin_lock_slow(struct raw_spinlock *lock) static noinline int xen_spin_lock_slow(struct raw_spinlock *lock)
{ {
struct xen_spinlock *xl = (struct xen_spinlock *)lock; struct xen_spinlock *xl = (struct xen_spinlock *)lock;
struct xen_spinlock *prev;
int irq = __get_cpu_var(lock_kicker_irq); int irq = __get_cpu_var(lock_kicker_irq);
int ret; int ret;
...@@ -74,23 +90,42 @@ static noinline int xen_spin_lock_slow(struct raw_spinlock *lock) ...@@ -74,23 +90,42 @@ static noinline int xen_spin_lock_slow(struct raw_spinlock *lock)
return 0; return 0;
/* announce we're spinning */ /* announce we're spinning */
spinning_lock(xl); prev = spinning_lock(xl);
/* clear pending */ do {
xen_clear_irq_pending(irq); /* clear pending */
xen_clear_irq_pending(irq);
/* check again make sure it didn't become free while
we weren't looking */
ret = xen_spin_trylock(lock);
if (ret) {
/*
* If we interrupted another spinlock while it
* was blocking, make sure it doesn't block
* without rechecking the lock.
*/
if (prev != NULL)
xen_set_irq_pending(irq);
goto out;
}
/* check again make sure it didn't become free while /*
we weren't looking */ * Block until irq becomes pending. If we're
ret = xen_spin_trylock(lock); * interrupted at this point (after the trylock but
if (ret) * before entering the block), then the nested lock
goto out; * handler guarantees that the irq will be left
* pending if there's any chance the lock became free;
* xen_poll_irq() returns immediately if the irq is
* pending.
*/
xen_poll_irq(irq);
} while (!xen_test_irq_pending(irq)); /* check for spurious wakeups */
/* block until irq becomes pending */
xen_poll_irq(irq);
kstat_this_cpu.irqs[irq]++; kstat_this_cpu.irqs[irq]++;
out: out:
unspinning_lock(xl); unspinning_lock(xl, prev);
return ret; return ret;
} }
......
...@@ -164,6 +164,12 @@ static inline void set_evtchn(int port) ...@@ -164,6 +164,12 @@ static inline void set_evtchn(int port)
sync_set_bit(port, &s->evtchn_pending[0]); sync_set_bit(port, &s->evtchn_pending[0]);
} }
static inline int test_evtchn(int port)
{
struct shared_info *s = HYPERVISOR_shared_info;
return sync_test_bit(port, &s->evtchn_pending[0]);
}
/** /**
* notify_remote_via_irq - send event to remote end of event channel via irq * notify_remote_via_irq - send event to remote end of event channel via irq
...@@ -732,6 +738,25 @@ void xen_clear_irq_pending(int irq) ...@@ -732,6 +738,25 @@ void xen_clear_irq_pending(int irq)
clear_evtchn(evtchn); clear_evtchn(evtchn);
} }
void xen_set_irq_pending(int irq)
{
int evtchn = evtchn_from_irq(irq);
if (VALID_EVTCHN(evtchn))
set_evtchn(evtchn);
}
bool xen_test_irq_pending(int irq)
{
int evtchn = evtchn_from_irq(irq);
bool ret = false;
if (VALID_EVTCHN(evtchn))
ret = test_evtchn(evtchn);
return ret;
}
/* Poll waiting for an irq to become pending. In the usual case, the /* Poll waiting for an irq to become pending. In the usual case, the
irq will be disabled so it won't deliver an interrupt. */ irq will be disabled so it won't deliver an interrupt. */
void xen_poll_irq(int irq) void xen_poll_irq(int irq)
......
...@@ -46,6 +46,8 @@ extern void xen_irq_resume(void); ...@@ -46,6 +46,8 @@ extern void xen_irq_resume(void);
/* Clear an irq's pending state, in preparation for polling on it */ /* Clear an irq's pending state, in preparation for polling on it */
void xen_clear_irq_pending(int irq); void xen_clear_irq_pending(int irq);
void xen_set_irq_pending(int irq);
bool xen_test_irq_pending(int irq);
/* Poll waiting for an irq to become pending. In the usual case, the /* Poll waiting for an irq to become pending. In the usual case, the
irq will be disabled so it won't deliver an interrupt. */ irq will be disabled so it won't deliver an interrupt. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment