Skip to contentSkip to conversation

Xen spinlock questions

PATCH Jan Beulich 3 participants 29 messages Aug 4, 2008 – Aug 18, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Jeremy,

considering to utilize your pv-ops spinlock implementation for our kernels, I'd appreciate your opinion on the following thoughts:

1) While the goal of the per-CPU kicker irq appears to be to avoid all CPUs waiting for a particular lock to get kicked simultaneously, I think this doesn't have the desired effect. This is because Xen doesn't track what event channel you poll for (through SCHEDOP_poll), and rather kicks all CPUs polling for any event channel.

2) While on native not re-enabling interrupts in __raw_spin_lock_flags() may be tolerable (but perhaps questionable), not doing so at least on the slow path here seems suspicious.

3) Introducing yet another per-CPU IRQ for this purpose further tightens scalability. Using a single, IRQF_PER_CPU IRQ should be sufficient here, as long as it gets properly multiplexed onto individual event channels (of which we have far more than IRQs). I have a patch queued for the traditional tree that does just that conversion for the reschedule and call-function IPIs, which I had long planned to get submitted (but so far wasn't able to due to lack of testing done on the migration aspects of it), and once successful was planning on trying to do something similar for the timer IRQ. I am attaching that (2.6.26 based) patch just for reference.

Thanks, Jan

From: [email protected]
Subject: fold IPIs onto a single IRQ each
Patch-mainline: obsolete

If this is reasonable, a similar change should be done for per-CPU VIRQs.

arch/x86/kernel/genapic_xen_64.c +1 -5
Index: head-2008-07-21/arch/x86/kernel/genapic_xen_64.c
===================================================================
@@ -24,13 +24,9 @@
 #include <asm/genapic.h>
 #include <xen/evtchn.h>
 
-DECLARE_PER_CPU(int, ipi_to_irq[NR_IPIS]);
-
 static inline void __send_IPI_one(unsigned int cpu, int vector)
 {
-	int irq = per_cpu(ipi_to_irq, cpu)[vector];
-	BUG_ON(irq < 0);
-	notify_remote_via_irq(irq);
+	notify_remote_via_ipi(vector, cpu);
 }
 
 void xen_send_IPI_shortcut(unsigned int shortcut, int vector)
arch/x86/kernel/ipi-xen.c +3 -12
Index: head-2008-07-21/arch/x86/kernel/ipi-xen.c
===================================================================
@@ -49,15 +49,6 @@ static inline int __prepare_ICR2(unsigne
 }
 #else
 #include <xen/evtchn.h>
-
-DECLARE_PER_CPU(int, ipi_to_irq[NR_IPIS]);
-
-static inline void __send_IPI_one(unsigned int cpu, int vector)
-{
-	int irq = per_cpu(ipi_to_irq, cpu)[vector];
-	BUG_ON(irq < 0);
-	notify_remote_via_irq(irq);
-}
 #endif
 
 void __send_IPI_shortcut(unsigned int shortcut, int vector)
@@ -91,14 +82,14 @@ void __send_IPI_shortcut(unsigned int sh
 
 	switch (shortcut) {
 	case APIC_DEST_SELF:
-		__send_IPI_one(smp_processor_id(), vector);
+		notify_remote_via_ipi(vector, smp_processor_id());
 		break;
 	case APIC_DEST_ALLBUT:
 		for (cpu = 0; cpu < NR_CPUS; ++cpu) {
 			if (cpu == smp_processor_id())
 				continue;
 			if (cpu_isset(cpu, cpu_online_map)) {
-				__send_IPI_one(cpu, vector);
+				notify_remote_via_ipi(vector, cpu);
 			}
 		}
 		break;
@@ -168,7 +159,7 @@ void send_IPI_mask_bitmask(cpumask_t cpu
 #else
 	for (cpu = 0; cpu < NR_CPUS; ++cpu)
 		if (cpu_isset(cpu, cpumask))
-			__send_IPI_one(cpu, vector);
+			notify_remote_via_ipi(vector, cpu);
 #endif
 	local_irq_restore(flags);
 }
drivers/xen/Kconfig +4 -0
Index: head-2008-07-21/drivers/xen/Kconfig
===================================================================
@@ -4,6 +4,7 @@
 
 config XEN
 	bool
+	select IRQ_PER_CPU if SMP
 
 if XEN
 config XEN_INTERFACE_VERSION
@@ -290,6 +291,9 @@ config HAVE_IRQ_IGNORE_UNHANDLED
 config GENERIC_HARDIRQS_NO__DO_IRQ
 	def_bool y
 
+config IRQ_PER_CPU
+	bool
+
 config NO_IDLE_HZ
 	def_bool y
 
drivers/xen/core/evtchn.c +188 -7

333 lines not on this page · raw message

drivers/xen/core/smpboot.c +31 -29

90 lines not on this page · raw message

include/xen/evtchn.h +18 -0
Index: head-2008-07-21/include/xen/evtchn.h
===================================================================
@@ -78,6 +78,8 @@ int bind_virq_to_irqhandler(
 	unsigned long irqflags,
 	const char *devname,
 	void *dev_id);
+#if defined(CONFIG_SMP) && !defined(MODULE)
+#ifndef CONFIG_X86
 int bind_ipi_to_irqhandler(
 	unsigned int ipi,
 	unsigned int cpu,
@@ -85,6 +87,13 @@ int bind_ipi_to_irqhandler(
 	unsigned long irqflags,
 	const char *devname,
 	void *dev_id);
+#else
+int bind_ipi_to_irqaction(
+	unsigned int ipi,
+	unsigned int cpu,
+	struct irqaction *action);
+#endif
+#endif
 
 /*
  * Common unbind function for all event sources. Takes IRQ to unbind from.
@@ -93,6 +102,11 @@ int bind_ipi_to_irqhandler(
  */
 void unbind_from_irqhandler(unsigned int irq, void *dev_id);
 
+#if defined(CONFIG_SMP) && !defined(MODULE) && defined(CONFIG_X86)
+/* Specialized unbind function for per-CPU IRQs. */
+void unbind_from_per_cpu_irq(unsigned int irq, unsigned int cpu);
+#endif
+
 #ifndef CONFIG_XEN
 void irq_resume(void);
 #endif
@@ -152,4 +166,8 @@ int clear_pirq_hw_action(int pirq);
 #define PIRQ_END 5
 #define PIRQ_ACK 6
 
+#if defined(CONFIG_SMP) && !defined(MODULE) && defined(CONFIG_X86)
+void notify_remote_via_ipi(unsigned int ipi, unsigned int cpu);
+#endif
+
 #endif /* __ASM_EVTCHN_H__ */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 5 lines · Jan Beulich

1) While the goal of the per-CPU kicker irq appears to be to avoid all CPUs waiting for a particular lock to get kicked simultaneously, I think this doesn't have the desired effect. This is because Xen doesn't track what event channel you poll for (through SCHEDOP_poll), and rather kicks all CPUs polling for any event channel.

Yes, this is true. We could easily do something better for VCPUs polling a single event channel though, but there hasn't been a need up to now. I suppose it depends how often we have multiple VCPUs stuck waiting for spinlocks. I can sort out a Xen-side patch if someone wanted to measure the benefits from more selective wakeup from poll.

-- Keir

#18 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich

Jeremy,

considering to utilize your pv-ops spinlock implementation for our kernels, I'd appreciate your opinion on the following thoughts:

1) While the goal of the per-CPU kicker irq appears to be to avoid all CPUs waiting for a particular lock to get kicked simultaneously, I think this doesn't have the desired effect. This is because Xen doesn't track what event channel you poll for (through SCHEDOP_poll), and rather kicks all CPUs polling for any event channel.

There's no problem with kicking all cpus waiting for a given lock, but it was intended to avoid kicking cpus waiting for some other lock. I hadn't looked at the poll implementation that closely. I guess using the per-cpu interrupt gives Xen some room to live up to the expectations we have for it ;)

Quoted 4 lines

2) While on native not re-enabling interrupts in __raw_spin_lock_flags() may be tolerable (but perhaps questionable), not doing so at least on the slow path here seems suspicious.

I wasn't sure about that. Is it OK to enable interrupts in the middle of a spinlock? Can it be done unconditionally?

Quoted 9 lines

3) Introducing yet another per-CPU IRQ for this purpose further tightens scalability. Using a single, IRQF_PER_CPU IRQ should be sufficient here, as long as it gets properly multiplexed onto individual event channels (of which we have far more than IRQs). I have a patch queued for the traditional tree that does just that conversion for the reschedule and call-function IPIs, which I had long planned to get submitted (but so far wasn't able to due to lack of testing done on the migration aspects of it), and once successful was planning on trying to do something similar for the timer IRQ.

There's two lines of work I'm hoping to push to mitigate this:

One is the unification of 32 and 64-bit interrupt handling, so that they both have an underlying notion of a vector, which is what we map event channels to. Since vectors can be mapped to a (irq,cpu) tuple, it would allow multiple per-cpu vectors/event channels to be mapped to a single irq, and do so generically for all event channel types. That would mean we'd end up allocating one set of interrupts for time, function calls, spinlocks, etc, rather than percpu.

The other is eliminating NR_IRQ, and making irq allocation completely dynamic.

Quoted 1 line

I am attaching that (2.6.26 based) patch just for reference.

From a quick look, you're thinking along similar lines.

J

#19 lore raw
Quoted 7 lines
Quoted 4 lines

2) While on native not re-enabling interrupts in __raw_spin_lock_flags() may be tolerable (but perhaps questionable), not doing so at least on the slow path here seems suspicious.

I wasn't sure about that. Is it OK to enable interrupts in the middle of a spinlock? Can it be done unconditionally?

That used to be done in the pre-ticket lock implementation, but of course conditional upon the original interrupt flag.

Later yesterday I noticed another issue: The code setting lock_spinners isn't interruption safe - you'll need to return the old value from spinning_lock() and restore it in unspinning_lock().

Also I'm considering doing it ticket-based nevertheless, as "mix(ing) up next cpu selection" won't really help fairness in xen_spin_unlock_slow(). Apart from definitely needing the wakeup to happen for just the target CPU (Keir, I'd want the necessary support in Xen done for that to work regardless of performance measurements with the traditional locking, as it's known that with ticket locks performance suffers from wrong- order CPU kicking), one thing we'd be in even more need for here than old-style spin locks had been would be a directed yield (sub-)hypercall. Has that ever been considered to become a schedop?

Jan

#20 lore raw
Quoted 3 lines · Jan Beulich

one thing we'd be in even more need for here than
old-style spin locks had been would be a directed yield (sub-)hypercall. Has that ever been considered to become a schedop?

It has. Someone will need to implement it and show a convincing performance win with it with a real benchmark.

-- Keir

#21 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 7 lines
Quoted 5 lines

2) While on native not re-enabling interrupts in __raw_spin_lock_flags() may be tolerable (but perhaps questionable), not doing so at least on the slow path here seems suspicious.

I wasn't sure about that. Is it OK to enable interrupts in the middle of a spinlock? Can it be done unconditionally?

That used to be done in the pre-ticket lock implementation, but of course conditional upon the original interrupt flag.

Right, I see. The spin_lock_flags stuff which the current lock implementation just ignores. I'll add a new lock op for it.

Quoted 4 lines

Later yesterday I noticed another issue: The code setting lock_spinners isn't interruption safe - you'll need to return the old value from spinning_lock() and restore it in unspinning_lock().

Good catch.

Quoted 3 lines

Also I'm considering doing it ticket-based nevertheless, as "mix(ing) up next cpu selection" won't really help fairness in xen_spin_unlock_slow().

Why's that? An alternative might be to just wake all cpus waiting for the lock up, and then let them fight it out. It should be unusual that there's a significant number of waiters anyway, since contention is (should be) rare.

The main reason for ticket locks is to break the egregious unfairness that (some) bus protocols implement. That level of fairness shouldn't be necessary here because once the cpus fall to blocking in the hypervisor, it's up to Xen to tie-break.

Quoted 8 lines

Apart from definitely needing the wakeup to happen for just the target CPU (Keir, I'd want the necessary support in Xen done for that to work regardless of performance measurements with the traditional locking, as it's known that with ticket locks performance suffers from wrong- order CPU kicking), one thing we'd be in even more need for here than old-style spin locks had been would be a directed yield (sub-)hypercall. Has that ever been considered to become a schedop?

Considered. But the point of this exercise was to come up with something that would work with an unmodified hypervisor.

J

#22 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line

Jeremy Fitzhardinge <[email protected]> 05.08.08 20:09 >>>

Quoted 4 lines · Jan Beulich

Later yesterday I noticed another issue: The code setting lock_spinners isn't interruption safe - you'll need to return the old value from spinning_lock() and restore it in unspinning_lock().

Good catch.

More on that: You'll really need two per-CPU variables afaics, one for the current non-irq lock being spun upon, and one for the current irqs-disabled one. The latter one might not need saving/restoring as long as you don't re-enable interrupts, but the code might turn out cleaner when doing the save/restore regardless, e.g. for me (doing ticket locking):

int xen_spin_wait(raw_spinlock_t *lock, unsigned int token)

{
	struct spinning *spinning;
	int rc;

	/* if kicker interrupt not initialized yet, just spin */
	if (spinlock_irq < 0)
		return 0;

	/* announce we're spinning */
	spinning = &__get_cpu_var(spinning);
	if (spinning->lock) {
		BUG_ON(!raw_irqs_disabled());
		spinning = &__get_cpu_var(spinning_irq);
		BUG_ON(spinning->lock);
	}
	spinning->ticket = token >> TICKET_SHIFT;
	smp_wmb();
	spinning->lock = lock;

	/* clear pending */
	xen_clear_irq_pending(spinlock_irq);

	/* check again make sure it didn't become free while
	   we weren't looking  */
	rc = __raw_spin_trylock(lock);
	if (!rc) {
		/* block until irq becomes pending */
		xen_poll_irq(spinlock_irq);
		kstat_this_cpu.irqs[spinlock_irq]++;
	}

	/* announce we're done */
	spinning->lock = NULL;

	return rc;
}
Quoted 8 lines
Quoted 3 lines

Also I'm considering doing it ticket-based nevertheless, as "mix(ing) up next cpu selection" won't really help fairness in xen_spin_unlock_slow().

Why's that? An alternative might be to just wake all cpus waiting for the lock up, and then let them fight it out. It should be unusual that there's a significant number of waiters anyway, since contention is (should be) rare.

On an 8-core system I'm seeing between 20,000 (x86-64) and 35,000 (i686) wakeup interrupts per CPU. I'm not certain this still counts as rare. Though that number may go down a little once the hypervisor doesn't needlessly wake all polling vCPU-s anymore.

Quoted 4 lines

The main reason for ticket locks is to break the egregious unfairness that (some) bus protocols implement. That level of fairness shouldn't be necessary here because once the cpus fall to blocking in the hypervisor, it's up to Xen to tie-break.

Why? The hypervisor doing the tie-break makes it possibly even more unfair, whereas with tickets and a way to kick the next owner (and only it) almost the same level of fairness as on native can be achieved. The only heuristic to determine by measurement is the number of spin loops before going into poll mode (which your original patch's description and implementation for some reason disagree about - the description says 2^16 loops, the implementation uses 2^10). Obviously, the optimal numbers may turn out different for byte and ticket locks.

And doing a wake-them-all approach isn't good here, as was (supposedly, I wasn't there) explained in a talk on the last summit.

Jan

#23 lore raw
Quoted 6 lines · Jan Beulich

More on that: You'll really need two per-CPU variables afaics, one for the current non-irq lock being spun upon, and one for the current irqs-disabled one. The latter one might not need saving/restoring as long as you don't re-enable interrupts, but the code might turn out cleaner when doing the save/restore regardless, e.g. for me (doing ticket locking):

Not sure I follow. How do you use the second array at the kick end of the process?

I ended up just storing the previous value locally, and then restoring it. It assumes that locks will strictly nest, of course, but I think that's reasonable.

arch/x86/xen/spinlock.c +0 -0
@@ -135,25 +135,39 @@

static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; 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) {
- __get_cpu_var(lock_spinners) = xl;
- wmb(); /* set lock of interest before count */
+ struct xen_spinlock *prev;
+
+ prev = xchg(&__get_cpu_var(lock_spinners), xl);
+ /* xchg is a barrier */
+

	asm(LOCK_PREFIX " incw %0"
	    : "+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"
	    : "+m" (xl->spinners) : : "memory");

- wmb(); /* decrement count before clearing lock */
- __get_cpu_var(lock_spinners) = NULL;
+ wmb(); /* decrement count before restoring lock */
+ __get_cpu_var(lock_spinners) = prev;
}

static noinline int xen_spin_lock_slow(struct raw_spinlock *lock, bool irq_enable)

{
	struct xen_spinlock *xl = (struct xen_spinlock *)lock;
+	struct xen_spinlock *prev;
	int irq = __get_cpu_var(lock_kicker_irq);
	int ret;
	unsigned long flags;
@@ -163,7 +177,8 @@
		return 0;

	/* announce we're spinning */
-	spinning_lock(xl);
+	prev = spinning_lock(xl);
+
	flags = __raw_local_save_flags();
	if (irq_enable) {
		ADD_STATS(taken_slow_irqenable, 1);

@@ -189,7 +204,7 @@

out:

	raw_local_irq_restore(flags);
-	unspinning_lock(xl);
+	unspinning_lock(xl, prev);
	return ret;
}
Quoted 39 lines

int xen_spin_wait(raw_spinlock_t *lock, unsigned int token)

{
	struct spinning *spinning;
	int rc;

	/* if kicker interrupt not initialized yet, just spin */
	if (spinlock_irq < 0)
		return 0;

	/* announce we're spinning */
	spinning = &__get_cpu_var(spinning);
	if (spinning->lock) {
		BUG_ON(!raw_irqs_disabled());
		spinning = &__get_cpu_var(spinning_irq);
		BUG_ON(spinning->lock);
	}
	spinning->ticket = token >> TICKET_SHIFT;
	smp_wmb();
	spinning->lock = lock;

	/* clear pending */
	xen_clear_irq_pending(spinlock_irq);

	/* check again make sure it didn't become free while
	   we weren't looking  */
	rc = __raw_spin_trylock(lock);
	if (!rc) {
		/* block until irq becomes pending */
		xen_poll_irq(spinlock_irq);
		kstat_this_cpu.irqs[spinlock_irq]++;
	}

	/* announce we're done */
	spinning->lock = NULL;

	return rc;
}
Quoted 5 lines

On an 8-core system I'm seeing between 20,000 (x86-64) and 35,000 (i686) wakeup interrupts per CPU. I'm not certain this still counts as rare. Though that number may go down a little once the hypervisor doesn't needlessly wake all polling vCPU-s anymore.

What workload are you seeing that on? 20-35k interrupts over what time period?

In my tests, I only see it fall into the slow path a couple of thousand times per cpu for a kernbench run.

Quoted 6 lines
Quoted 2 lines

The main reason for ticket locks is to break the egregious unfairness that (some) bus protocols implement. That level of fairness shouldn't be necessary here because once the cpus fall to blocking in the hypervisor, it's up to Xen to tie-break.

Why? The hypervisor doing the tie-break makes it possibly even more unfair, whereas with tickets and a way to kick the next owner (and only it) almost the same level of fairness as on native can be achieved.

I don't think strict fairness is a particularly desirable property; it's certainly not an unambiguous win. The important thing is solves is total starvation, and if the Xen scheduler ends up starving a CPU then that's a scheduler bug we can fix. We can't fix the cache coherence protocols, so we need to use something like a ticket lock.

Quoted 7 lines

The
only heuristic to determine by measurement is the number of spin loops before going into poll mode (which your original patch's description and implementation for some reason disagree about - the description says 2^16 loops, the implementation uses 2^10). Obviously, the optimal numbers may turn out different for byte and ticket locks.

Yes, there's a bit of confusion about loop iterations vs cycles. The original paper said that after 2^16 *cycles* 90% of locks have been taken, which I map (approximately) to 2^10 loop iterations (but originally I had 2^16 iterations). I don't think it's all that critical; it probably depends too much on workload and precise system and VM configuration to really finely tune, and doesn't end up making all that much difference.

That said, I've implemented a pile of debugfs infrastructure for extracting lots of details about lock performance so there's some scope for tuning it (including being able to change the timeout on the fly to see how things change).

Quoted 3 lines

And doing a wake-them-all approach isn't good here, as was (supposedly, I wasn't there) explained in a talk on the last summit.

Well, yes, wake-all is a total disaster for ticket locks, since it just causes scheduler thrashing. But for byte locks it doesn't matter all that much since whoever runs next will take the lock and make progress. The others will wake up and spin for a bit before sleeping; not great, but still better than plain spinning.

It might be worth using a smaller timeout for re-lock attempts after waking up, but that could also lead to starvation as a sleeper will be at a disadvantage against someone who's currently spinning on the lock, and so will tend to lose against new lock takers.

I think we can also mitigate poll's wake-all behaviour by seeing if our particular per-cpu interrupt is pending and drop back into poll immediately if not (ie, detect a spurious wakeup).

J

#24 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Jeremy Fitzhardinge <[email protected]> 06.08.08 10:47 >>>

Quoted 6 lines · Jan Beulich

More on that: You'll really need two per-CPU variables afaics, one for the current non-irq lock being spun upon, and one for the current irqs-disabled one. The latter one might not need saving/restoring as long as you don't re-enable interrupts, but the code might turn out cleaner when doing the save/restore regardless, e.g. for me (doing ticket locking):

Not sure I follow. How do you use the second array at the kick end of the process?

I just look at both stored values.

Quoted 3 lines

I ended up just storing the previous value locally, and then restoring it. It assumes that locks will strictly nest, of course, but I think that's reasonable.

Storing the previous value locally is fine. But I don't think you can do with just one 'currently spinning' pointer because of the kicking side requirements - if an irq-save lock interrupted an non-irq one (with the spinning pointer already set) and a remote CPU releases the lock and wants to kick you, it won't be able to if the irq-save lock already replaced the non-irq one. Nevertheless, if you're past the try-lock, you may end up never getting the wakeup.

Since there can only be one non-irq and one irq-save lock a CPU is currently spinning on (the latter as long as you don't re-enable interrupts), two fields, otoh, are sufficient.

Btw., I also think that using an xchg() (and hence a locked transaction) for updating the pointer isn't necessary.

Quoted 8 lines
Quoted 5 lines

On an 8-core system I'm seeing between 20,000 (x86-64) and 35,000 (i686) wakeup interrupts per CPU. I'm not certain this still counts as rare. Though that number may go down a little once the hypervisor doesn't needlessly wake all polling vCPU-s anymore.

What workload are you seeing that on? 20-35k interrupts over what time period?

Oh, sorry, I meant to say that's for a kernel build (-j12), taking about 400 wall seconds.

Quoted 2 lines

In my tests, I only see it fall into the slow path a couple of thousand times per cpu for a kernbench run.

Hmm, that's different then for me. Actually, I see a significant spike at modpost stage 2, when all the .ko-s get linked. The (spinlock) interrupt rate gets up to between 1,000 and 2,000 per CPU and second.

Quoted 4 lines

That said, I've implemented a pile of debugfs infrastructure for extracting lots of details about lock performance so there's some scope for tuning it (including being able to change the timeout on the fly to see how things change).

Yeah, that's gonna be useful to have.

Quoted 3 lines

I think we can also mitigate poll's wake-all behaviour by seeing if our particular per-cpu interrupt is pending and drop back into poll immediately if not (ie, detect a spurious wakeup).

Oh, of course - I didn't consider this so far.

Jan

#25 lore raw
Quoted 8 lines · Jan Beulich

Storing the previous value locally is fine. But I don't think you can do with just one 'currently spinning' pointer because of the kicking side requirements - if an irq-save lock interrupted an non-irq one (with the spinning pointer already set) and a remote CPU releases the lock and wants to kick you, it won't be able to if the irq-save lock already replaced the non-irq one. Nevertheless, if you're past the try-lock, you may end up never getting the wakeup.

Quoted 4 lines

Since there can only be one non-irq and one irq-save lock a CPU is currently spinning on (the latter as long as you don't re-enable interrupts), two fields, otoh, are sufficient.

No, I think it's mostly OK. The sequence is:

1. mark that we're about to block
2. clear pending state on irq
3. test again with trylock
4. block in poll

The worrisome interval is between 3-4, but it's only a problem if we end up entering the poll with the lock free and the interrupt non-pending. There are a few possibilities for what happens in that interval:

1. the nested spinlock is fastpath only, and takes some other lock -> OK, because we're still marked as interested in this lock, and will be kicked
-> if the nested spinlock takes the same lock as the outer one, it will end up doing a self-kick
2. the nested spinlock is slowpath and is kicked

     -> OK, because it will leave the irq pending
  3. the nested spinlock is slowpath, but picks up the lock on retry
     -> bad, because it won't leave the irq pending.

The fix is to make sure that in case 4, it checks to see if it's interrupting a blocking lock (by checking if prev != NULL), and leave the irq pending if it is.

Updated patch below. Compile tested only.

Quoted 3 lines

Btw., I also think that using an xchg() (and hence a locked transaction) for updating the pointer isn't necessary.

I was concerned about what would happen if an interrupt got between the fetch and store. But thinking about it, I think you're right.

Quoted 13 lines
Quoted 8 lines
Quoted 6 lines

On an 8-core system I'm seeing between 20,000 (x86-64) and 35,000 (i686) wakeup interrupts per CPU. I'm not certain this still counts as rare. Though that number may go down a little once the hypervisor doesn't needlessly wake all polling vCPU-s anymore.

What workload are you seeing that on? 20-35k interrupts over what time period?

Oh, sorry, I meant to say that's for a kernel build (-j12), taking about 400 wall seconds.

Quoted 7 lines
Quoted 2 lines

In my tests, I only see it fall into the slow path a couple of thousand times per cpu for a kernbench run.

Hmm, that's different then for me. Actually, I see a significant spike at modpost stage 2, when all the .ko-s get linked. The (spinlock) interrupt rate gets up to between 1,000 and 2,000 per CPU and second.

defconfig? allmodconfig?

I'll measure again to confirm.

J

arch/x86/xen/spinlock.c +0 -0
diff -r 5b4b80c08799 arch/x86/xen/spinlock.c
@@ -22,6 +22,7 @@
	u64 taken_slow;
	u64 taken_slow_pickup;
	u64 taken_slow_irqenable;

+ u64 taken_slow_spurious;

	u64 released;
	u64 released_slow;

@@ -135,25 +136,41 @@
static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; 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;
+
	wmb();			/* set lock of interest before count */
+
	asm(LOCK_PREFIX " incw %0"
	    : "+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"
	    : "+m" (xl->spinners) : : "memory");

- wmb(); /* decrement count before clearing lock */
- __get_cpu_var(lock_spinners) = NULL;
+ wmb(); /* decrement count before restoring lock */
+ __get_cpu_var(lock_spinners) = prev;
}

static noinline int xen_spin_lock_slow(struct raw_spinlock *lock, bool irq_enable)

{
	struct xen_spinlock *xl = (struct xen_spinlock *)lock;
+	struct xen_spinlock *prev;
	int irq = __get_cpu_var(lock_kicker_irq);
	int ret;
	unsigned long flags;
@@ -163,33 +180,56 @@
		return 0;

	/* announce we're spinning */
-	spinning_lock(xl);
+	prev = spinning_lock(xl);
+
	flags = __raw_local_save_flags();
	if (irq_enable) {
		ADD_STATS(taken_slow_irqenable, 1);
		raw_local_irq_enable();
	}

- /* clear pending */
- xen_clear_irq_pending(irq);
+ ADD_STATS(taken_slow, 1);
+
+ do {
+ /* clear pending */
+ xen_clear_irq_pending(irq);

- ADD_STATS(taken_slow, 1);
+ /* check again make sure it didn't become free while
+ we weren't looking */
+ ret = xen_spin_trylock(lock);
+ if (ret) {
+ ADD_STATS(taken_slow_pickup, 1);
+
+ /*
+ * 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 */
- ret = xen_spin_trylock(lock);
- if (ret) {
- ADD_STATS(taken_slow_pickup, 1);
- goto out;
- }
+ /* + * Block until irq becomes pending. If we're
+ * interrupted at this point (after the trylock but
+ * before entering the block), then the nested lock
+ * 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);
+ kstat_this_cpu.irqs[irq]++;
+ ADD_STATS(taken_slow_spurious, !xen_test_irq_pending(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]++;
+ /* Leave the irq pending so that any interrupted blocker will
+ recheck. */

out:

	raw_local_irq_restore(flags);
-	unspinning_lock(xl);
+	unspinning_lock(xl, prev);
	return ret;
}

@@ -333,6 +373,8 @@

			   &spinlock_stats.taken_slow_pickup);
	debugfs_create_u64("taken_slow_irqenable", 0444, d_spin_debug,
			   &spinlock_stats.taken_slow_irqenable);

+ debugfs_create_u64("taken_slow_spurious", 0444, d_spin_debug,
+ &spinlock_stats.taken_slow_spurious);

	debugfs_create_u64("released", 0444, d_spin_debug, &spinlock_stats.released);
	debugfs_create_u64("released_slow", 0444, d_spin_debug,
drivers/xen/events.c +0 -0
diff -r 5b4b80c08799 drivers/xen/events.c
@@ -162,6 +162,12 @@
{
	struct shared_info *s = HYPERVISOR_shared_info;
	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]);
}

@@ -748,6 +754,25 @@

		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
irq will be disabled so it won't deliver an interrupt. */
void xen_poll_irq(int irq)

include/xen/events.h +0 -0
diff -r 5b4b80c08799 include/xen/events.h
@@ -47,6 +47,8 @@
 

/* Clear an irq's pending state, in preparation for polling on it */ 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
irq will be disabled so it won't deliver an interrupt. */

#26 lore raw
In reply to Jan Beulich
Quoted 7 lines · Jan Beulich
Quoted 2 lines

In my tests, I only see it fall into the slow path a couple of thousand times per cpu for a kernbench run.

Hmm, that's different then for me. Actually, I see a significant spike at modpost stage 2, when all the .ko-s get linked. The (spinlock) interrupt rate gets up to between 1,000 and 2,000 per CPU and second.

BTW, how much contention for CPUs was there?

J

#27 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line

Jeremy Fitzhardinge <[email protected]> 06.08.08 22:21 >>>

Quoted 4 lines · Jan Beulich
Quoted 3 lines

In my tests, I only see it fall into the slow path a couple of thousand times per cpu for a kernbench run.

Quoted 6 lines
Quoted 4 lines

Hmm, that's different then for me. Actually, I see a significant spike at modpost stage 2, when all the .ko-s get linked. The (spinlock) interrupt rate gets up to between 1,000 and 2,000 per CPU and second.

BTW, how much contention for CPUs was there?

In Xen or the kernel? So far these are all Dom0-only measurements, so in Xen there should be no contention.

To answer your other mail's question: It's defconfig builds I'm using.

Jan

#28 lore raw
Quoted 12 lines · Jan Beulich
Quoted 12 lines
Quoted 2 lines
Quoted 2 lines

Jeremy Fitzhardinge <[email protected]> 06.08.08 22:21 >>>

Quoted 8 lines · Jan Beulich
Quoted 3 lines

In my tests, I only see it fall into the slow path a couple of thousand times per cpu for a kernbench run.

Hmm, that's different then for me. Actually, I see a significant spike at modpost stage 2, when all the .ko-s get linked. The (spinlock) interrupt rate gets up to between 1,000 and 2,000 per CPU and second.

Quoted 6 lines
Quoted 2 lines

BTW, how much contention for CPUs was there?

In Xen or the kernel? So far these are all Dom0-only measurements, so in Xen there should be no contention.

OK. The spinlock results are most interesting when there's contention for physical cpus between domains.

Quoted 2 lines

To answer your other mail's question: It's defconfig builds I'm using.

[SuSE defconfigs] Presumably that has a lot enabled as modules.

J

#2 lore raw
In reply to Keir Fraser
Quoted 9 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 04.08.08 12:24 >>>

Quoted 5 lines · Jan Beulich

1) While the goal of the per-CPU kicker irq appears to be to avoid all CPUs waiting for a particular lock to get kicked simultaneously, I think this doesn't have the desired effect. This is because Xen doesn't track what event channel you poll for (through SCHEDOP_poll), and rather kicks all CPUs polling for any event channel.

Quoted 5 lines

Yes, this is true. We could easily do something better for VCPUs polling a single event channel though, but there hasn't been a need up to now. I suppose it depends how often we have multiple VCPUs stuck waiting for spinlocks. I can sort out a Xen-side patch if someone wanted to measure the benefits from more selective wakeup from poll.

Running kernel builds on 8 vCPU-s competing for 4 pCPU-s shows a 10% improvement in performance with the individual wakeup (patch attached
- probably sub-optimal, but I didn't seem to be able to think of a lock-less mechanism to achieve the desired behavior), using ticket locks in the kernel.

Jan

xen/common/domain.c +2 -1
Index: 2008-08-06/xen/common/domain.c
===================================================================
@@ -209,6 +209,7 @@ struct domain *domain_create(
     atomic_set(&d->refcnt, 1);
     spin_lock_init(&d->domain_lock);
     spin_lock_init(&d->page_alloc_lock);
+    spin_lock_init(&d->poll_lock);
     spin_lock_init(&d->shutdown_lock);
     spin_lock_init(&d->hypercall_deadlock_mutex);
     INIT_LIST_HEAD(&d->page_list);
@@ -653,7 +654,7 @@ void vcpu_reset(struct vcpu *v)
 
     v->fpu_initialised = 0;
     v->fpu_dirtied     = 0;
-    v->is_polling      = 0;
+    v->poll_evtchn     = 0;
     v->is_initialised  = 0;
     v->nmi_pending     = 0;
     v->mce_pending     = 0;
xen/common/event_channel.c +22 -4
Index: 2008-08-06/xen/common/event_channel.c
===================================================================
@@ -545,6 +545,7 @@ out:
 static int evtchn_set_pending(struct vcpu *v, int port)
 {
     struct domain *d = v->domain;
+    unsigned long flags;
 
     /*
      * The following bit operations must happen in strict order.
@@ -564,19 +565,36 @@ static int evtchn_set_pending(struct vcp
     }
     
     /* Check if some VCPU might be polling for this event. */
-    if ( unlikely(d->is_polling) )
+    if ( likely(!d->is_polling) )
+        return 0;
+
+    spin_lock_irqsave(&d->poll_lock, flags);
+
+    if ( likely(d->is_polling) )
     {
-        d->is_polling = 0;
+        bool_t is_polling = 0;
+
+        d->is_polling = -1;
         smp_mb(); /* check vcpu poll-flags /after/ clearing domain poll-flag */
         for_each_vcpu ( d, v )
         {
-            if ( !v->is_polling )
+            int poll_evtchn = v->poll_evtchn;
+
+            if ( !poll_evtchn )
+                continue;
+            if ( poll_evtchn > 0 && poll_evtchn != port )
+            {
+                is_polling = 1;
                 continue;
-            v->is_polling = 0;
+            }
+            v->poll_evtchn = 0;
             vcpu_unblock(v);
         }
+        cmpxchg(&d->is_polling, -1, is_polling);
     }
 
+    spin_unlock_irqrestore(&d->poll_lock, flags);
+
     return 0;
 }
 
xen/common/schedule.c +7 -4
Index: 2008-08-06/xen/common/schedule.c
===================================================================
@@ -348,7 +348,7 @@ static long do_poll(struct sched_poll *s
         return -EFAULT;
 
     set_bit(_VPF_blocked, &v->pause_flags);
-    v->is_polling = 1;
+    v->poll_evtchn = -1;
     d->is_polling = 1;
 
     /* Check for events /after/ setting flags: avoids wakeup waiting race. */
@@ -369,6 +369,9 @@ static long do_poll(struct sched_poll *s
             goto out;
     }
 
+    if ( i == 1 )
+        v->poll_evtchn = port;
+
     if ( sched_poll->timeout != 0 )
         set_timer(&v->poll_timer, sched_poll->timeout);
 
@@ -378,7 +381,7 @@ static long do_poll(struct sched_poll *s
     return 0;
 
  out:
-    v->is_polling = 0;
+    v->poll_evtchn = 0;
     clear_bit(_VPF_blocked, &v->pause_flags);
     return rc;
 }
@@ -758,10 +761,10 @@ static void poll_timer_fn(void *data)
 {
     struct vcpu *v = data;
 
-    if ( !v->is_polling )
+    if ( !v->poll_evtchn )
         return;
 
-    v->is_polling = 0;
+    v->poll_evtchn = 0;
     vcpu_unblock(v);
 }
 
xen/include/xen/sched.h +9 -3
Index: 2008-08-06/xen/include/xen/sched.h
===================================================================
@@ -106,8 +106,6 @@ struct vcpu 
     bool_t           fpu_initialised;
     /* Has the FPU been used since it was last saved? */
     bool_t           fpu_dirtied;
-    /* Is this VCPU polling any event channels (SCHEDOP_poll)? */
-    bool_t           is_polling;
     /* Initialization completed for this VCPU? */
     bool_t           is_initialised;
     /* Currently running on a CPU? */
@@ -137,6 +135,11 @@ struct vcpu 
     unsigned long    pause_flags;
     atomic_t         pause_count;
 
+    /* Is this VCPU polling any event channels (SCHEDOP_poll)?
+     * Positive values indicate a single, negative values multiple channels
+     * being polled. */
+    int              poll_evtchn;
+
     /* IRQ-safe virq_lock protects against delivering VIRQ to stale evtchn. */
     u16              virq_to_evtchn[NR_VIRQS];
     spinlock_t       virq_lock;
@@ -210,7 +213,7 @@ struct domain
     /* Is this guest being debugged by dom0? */
     bool_t           debugger_attached;
     /* Are any VCPUs polling event channels (SCHEDOP_poll)? */
-    bool_t           is_polling;
+    signed char      is_polling;
     /* Is this guest dying (i.e., a zombie)? */
     enum { DOMDYING_alive, DOMDYING_dying, DOMDYING_dead } is_dying;
     /* Domain is paused by controller software? */
@@ -218,6 +221,9 @@ struct domain
     /* Domain's VCPUs are pinned 1:1 to physical CPUs? */
     bool_t           is_pinned;
 
+    /* Protects is_polling modification in evtchn_set_pending(). */
+    spinlock_t       poll_lock;
+
     /* Guest has shut down (inc. reason code)? */
     spinlock_t       shutdown_lock;
     bool_t           is_shutting_down; /* in process of shutting down? */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw
Quoted 11 lines · Jan Beulich
Quoted 5 lines

Yes, this is true. We could easily do something better for VCPUs polling a single event channel though, but there hasn't been a need up to now. I suppose it depends how often we have multiple VCPUs stuck waiting for spinlocks. I can sort out a Xen-side patch if someone wanted to measure the benefits from more selective wakeup from poll.

Running kernel builds on 8 vCPU-s competing for 4 pCPU-s shows a 10% improvement in performance with the individual wakeup (patch attached
- probably sub-optimal, but I didn't seem to be able to think of a lock-less mechanism to achieve the desired behavior), using ticket locks in the kernel.

Hardly a real-world benchmark. :-)

-- Keir

#4 lore raw
In reply to Jan Beulich
Quoted 5 lines · Jan Beulich

Running kernel builds on 8 vCPU-s competing for 4 pCPU-s shows a 10% improvement in performance with the individual wakeup (patch attached
- probably sub-optimal, but I didn't seem to be able to think of a lock-less mechanism to achieve the desired behavior), using ticket locks in the kernel.

Could you try with the attached lock-free patch?

-- Keir

xen/common/domain.c +2 -1
diff -r 265d42af434b xen/common/domain.c
@@ -651,9 +651,10 @@ void vcpu_reset(struct vcpu *v)
 
     set_bit(_VPF_down, &v->pause_flags);
 
+    clear_bit(v->vcpu_id, d->poll_mask);
+
     v->fpu_initialised = 0;
     v->fpu_dirtied     = 0;
-    v->is_polling      = 0;
     v->is_initialised  = 0;
     v->nmi_pending     = 0;
     v->mce_pending     = 0;
xen/common/event_channel.c +12 -10
diff -r 265d42af434b xen/common/event_channel.c
@@ -545,6 +545,7 @@ static int evtchn_set_pending(struct vcp
 static int evtchn_set_pending(struct vcpu *v, int port)
 {
     struct domain *d = v->domain;
+    int vcpuid;
 
     /*
      * The following bit operations must happen in strict order.
@@ -564,17 +565,18 @@ static int evtchn_set_pending(struct vcp
     }
     
     /* Check if some VCPU might be polling for this event. */
-    if ( unlikely(d->is_polling) )
-    {
-        d->is_polling = 0;
-        smp_mb(); /* check vcpu poll-flags /after/ clearing domain poll-flag */
-        for_each_vcpu ( d, v )
-        {
-            if ( !v->is_polling )
-                continue;
-            v->is_polling = 0;
+    if ( likely(bitmap_empty(d->poll_mask, MAX_VIRT_CPUS)) )
+        return 0;
+
+    /* Wake any interested (or potentially interested) pollers. */
+    for ( vcpuid = find_first_bit(d->poll_mask, MAX_VIRT_CPUS);
+          vcpuid < MAX_VIRT_CPUS;
+          vcpuid = find_next_bit(d->poll_mask, MAX_VIRT_CPUS, vcpuid+1) )
+    {
+        v = d->vcpu[vcpuid];
+        if ( ((v->poll_evtchn < 0) || (v->poll_evtchn == port)) &&
+             test_and_clear_bit(vcpuid, d->poll_mask) )
             vcpu_unblock(v);
-        }
     }
 
     return 0;
xen/common/schedule.c +9 -9
diff -r 265d42af434b xen/common/schedule.c
@@ -348,11 +348,11 @@ static long do_poll(struct sched_poll *s
         return -EFAULT;
 
     set_bit(_VPF_blocked, &v->pause_flags);
-    v->is_polling = 1;
-    d->is_polling = 1;
+    v->poll_evtchn = -1;
+    set_bit(v->vcpu_id, d->poll_mask);
 
     /* Check for events /after/ setting flags: avoids wakeup waiting race. */
-    smp_wmb();
+    smp_mb();
 
     for ( i = 0; i < sched_poll->nr_ports; i++ )
     {
@@ -369,6 +369,9 @@ static long do_poll(struct sched_poll *s
             goto out;
     }
 
+    if ( sched_poll->nr_ports == 1 )
+        v->poll_evtchn = port;
+
     if ( sched_poll->timeout != 0 )
         set_timer(&v->poll_timer, sched_poll->timeout);
 
@@ -378,7 +381,7 @@ static long do_poll(struct sched_poll *s
     return 0;
 
  out:
-    v->is_polling = 0;
+    clear_bit(v->vcpu_id, d->poll_mask);
     clear_bit(_VPF_blocked, &v->pause_flags);
     return rc;
 }
@@ -758,11 +761,8 @@ static void poll_timer_fn(void *data)
 {
     struct vcpu *v = data;
 
-    if ( !v->is_polling )
-        return;
-
-    v->is_polling = 0;
-    vcpu_unblock(v);
+    if ( test_and_clear_bit(v->vcpu_id, v->domain->poll_mask) )
+        vcpu_unblock(v);
 }
 
 /* Initialise the data structures. */
xen/include/xen/sched.h +10 -4
diff -r 265d42af434b xen/include/xen/sched.h
@@ -106,8 +106,6 @@ struct vcpu
     bool_t           fpu_initialised;
     /* Has the FPU been used since it was last saved? */
     bool_t           fpu_dirtied;
-    /* Is this VCPU polling any event channels (SCHEDOP_poll)? */
-    bool_t           is_polling;
     /* Initialization completed for this VCPU? */
     bool_t           is_initialised;
     /* Currently running on a CPU? */
@@ -136,6 +134,13 @@ struct vcpu
 
     unsigned long    pause_flags;
     atomic_t         pause_count;
+
+    /*
+     * Valid only if this VCPU is specified in d->poll_mask:
+     * Positive values indicate a single port is being polled; -1 indicates
+     * multiple ports may be being polled.
+     */
+    int              poll_evtchn;
 
     /* IRQ-safe virq_lock protects against delivering VIRQ to stale evtchn. */
     u16              virq_to_evtchn[NR_VIRQS];
@@ -209,14 +214,15 @@ struct domain
     struct domain   *target;
     /* Is this guest being debugged by dom0? */
     bool_t           debugger_attached;
-    /* Are any VCPUs polling event channels (SCHEDOP_poll)? */
-    bool_t           is_polling;
     /* Is this guest dying (i.e., a zombie)? */
     enum { DOMDYING_alive, DOMDYING_dying, DOMDYING_dead } is_dying;
     /* Domain is paused by controller software? */
     bool_t           is_paused_by_controller;
     /* Domain's VCPUs are pinned 1:1 to physical CPUs? */
     bool_t           is_pinned;
+
+    /* Are any VCPUs polling event channels (SCHEDOP_poll)? */
+    DECLARE_BITMAP(poll_mask, MAX_VIRT_CPUS);
 
     /* Guest has shut down (inc. reason code)? */
     spinlock_t       shutdown_lock;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#16 lore raw
In reply to Jan Beulich
Quoted 5 lines · Jan Beulich

Running kernel builds on 8 vCPU-s competing for 4 pCPU-s shows a 10% improvement in performance with the individual wakeup (patch attached
- probably sub-optimal, but I didn't seem to be able to think of a lock-less mechanism to achieve the desired behavior), using ticket locks in the kernel.

What exactly are you measuring here? Are you testing your 2.6.2x forward-port with an adaptation of pv spinlocks using ticket locks as the underlying locking mechanism? Does the 10% improvement come compared to straightforward use of sched_poll, or compared to doing spurious wakeup detection?

J

#5 lore raw
In reply to Keir Fraser
Quoted 2 lines · Keir Fraser

Could you try with the attached lock-free patch?

Just to confirm: this will definitely cause SCHED_poll to return if any event is delivered to the polling vcpu, right? It won't restart the poll after event delivery?

Looks good otherwise.

J

#6 lore raw
Quoted 7 lines · Jeremy Fitzhardinge
Quoted 2 lines · Keir Fraser

Could you try with the attached lock-free patch?

Just to confirm: this will definitely cause SCHED_poll to return if any event is delivered to the polling vcpu, right? It won't restart the poll after event delivery?

That's right. The hypercall returns if any event is delivered to the vcpu, or if any port in the poll set is pending. The fact we don't clear the vcpu from the poll_mask if unblocked for event delivery isn't a correctness issue, but it will cause unnecessary extra work in future invocations of evtchn_set_pending(). Perhaps vcpu_unblock() should clear it.

-- Keir

#7 lore raw
Quoted 6 lines · Keir Fraser

That's right. The hypercall returns if any event is delivered to the vcpu, or if any port in the poll set is pending. The fact we don't clear the vcpu from the poll_mask if unblocked for event delivery isn't a correctness issue, but it will cause unnecessary extra work in future invocations of evtchn_set_pending(). Perhaps vcpu_unblock() should clear it.

That seems reasonable. In this use-case, it's quite likely that if the poll is interrupted by event delivery, on return it will find that the spinlock is now free and never re-enter the poll.

J

#8 lore raw
Quoted 11 lines · Jeremy Fitzhardinge
Quoted 6 lines · Keir Fraser

That's right. The hypercall returns if any event is delivered to the vcpu, or if any port in the poll set is pending. The fact we don't clear the vcpu from the poll_mask if unblocked for event delivery isn't a correctness issue, but it will cause unnecessary extra work in future invocations of evtchn_set_pending(). Perhaps vcpu_unblock() should clear it.

That seems reasonable. In this use-case, it's quite likely that if the poll is interrupted by event delivery, on return it will find that the spinlock is now free and never re-enter the poll.

Attached is a new version of the patch which clears the vcpu from poll_mask when it is unblocked for any reason. Jan: please can you give this one a spin if you get time.

-- Keir

#9 lore raw
Quoted 12 lines · Keir Fraser
Quoted 9 lines · Jeremy Fitzhardinge
Quoted 7 lines · Keir Fraser

That's right. The hypercall returns if any event is delivered to the vcpu, or if any port in the poll set is pending. The fact we don't clear the vcpu from the poll_mask if unblocked for event delivery isn't a correctness issue, but it will cause unnecessary extra work in future invocations of evtchn_set_pending(). Perhaps vcpu_unblock() should clear it.

Quoted 9 lines
Quoted 4 lines

That seems reasonable. In this use-case, it's quite likely that if the poll is interrupted by event delivery, on return it will find that the spinlock is now free and never re-enter the poll.

Attached is a new version of the patch which clears the vcpu from poll_mask when it is unblocked for any reason. Jan: please can you give this one a spin if you get time.

Forgot to attach?

J

#10 lore raw
Quoted 11 lines · Jeremy Fitzhardinge
Quoted 9 lines
Quoted 4 lines

That seems reasonable. In this use-case, it's quite likely that if the poll is interrupted by event delivery, on return it will find that the spinlock is now free and never re-enter the poll.

Attached is a new version of the patch which clears the vcpu from poll_mask when it is unblocked for any reason. Jan: please can you give this one a spin if you get time.

Forgot to attach?

Sorry, attached now.

-- Keir

xen/common/domain.c +2 -1
diff -r 8b1ebe5e8fd7 xen/common/domain.c
@@ -651,9 +651,10 @@ void vcpu_reset(struct vcpu *v)
 
     set_bit(_VPF_down, &v->pause_flags);
 
+    clear_bit(v->vcpu_id, d->poll_mask);
+
     v->fpu_initialised = 0;
     v->fpu_dirtied     = 0;
-    v->is_polling      = 0;
     v->is_initialised  = 0;
     v->nmi_pending     = 0;
     v->mce_pending     = 0;
xen/common/event_channel.c +12 -10
diff -r 8b1ebe5e8fd7 xen/common/event_channel.c
@@ -545,6 +545,7 @@ static int evtchn_set_pending(struct vcp
 static int evtchn_set_pending(struct vcpu *v, int port)
 {
     struct domain *d = v->domain;
+    int vcpuid;
 
     /*
      * The following bit operations must happen in strict order.
@@ -564,17 +565,18 @@ static int evtchn_set_pending(struct vcp
     }
     
     /* Check if some VCPU might be polling for this event. */
-    if ( unlikely(d->is_polling) )
-    {
-        d->is_polling = 0;
-        smp_mb(); /* check vcpu poll-flags /after/ clearing domain poll-flag */
-        for_each_vcpu ( d, v )
-        {
-            if ( !v->is_polling )
-                continue;
-            v->is_polling = 0;
+    if ( likely(bitmap_empty(d->poll_mask, MAX_VIRT_CPUS)) )
+        return 0;
+
+    /* Wake any interested (or potentially interested) pollers. */
+    for ( vcpuid = find_first_bit(d->poll_mask, MAX_VIRT_CPUS);
+          vcpuid < MAX_VIRT_CPUS;
+          vcpuid = find_next_bit(d->poll_mask, MAX_VIRT_CPUS, vcpuid+1) )
+    {
+        v = d->vcpu[vcpuid];
+        if ( ((v->poll_evtchn < 0) || (v->poll_evtchn == port)) &&
+             test_and_clear_bit(vcpuid, d->poll_mask) )
             vcpu_unblock(v);
-        }
     }
 
     return 0;
xen/common/schedule.c +33 -9

74 lines not on this page · raw message

xen/include/xen/sched.h +11 -10
diff -r 8b1ebe5e8fd7 xen/include/xen/sched.h
@@ -106,8 +106,6 @@ struct vcpu
     bool_t           fpu_initialised;
     /* Has the FPU been used since it was last saved? */
     bool_t           fpu_dirtied;
-    /* Is this VCPU polling any event channels (SCHEDOP_poll)? */
-    bool_t           is_polling;
     /* Initialization completed for this VCPU? */
     bool_t           is_initialised;
     /* Currently running on a CPU? */
@@ -136,6 +134,13 @@ struct vcpu
 
     unsigned long    pause_flags;
     atomic_t         pause_count;
+
+    /*
+     * Valid only if this VCPU is specified in d->poll_mask:
+     * Positive values indicate a single port is being polled; -1 indicates
+     * multiple ports may be being polled.
+     */
+    int              poll_evtchn;
 
     /* IRQ-safe virq_lock protects against delivering VIRQ to stale evtchn. */
     u16              virq_to_evtchn[NR_VIRQS];
@@ -209,14 +214,15 @@ struct domain
     struct domain   *target;
     /* Is this guest being debugged by dom0? */
     bool_t           debugger_attached;
-    /* Are any VCPUs polling event channels (SCHEDOP_poll)? */
-    bool_t           is_polling;
     /* Is this guest dying (i.e., a zombie)? */
     enum { DOMDYING_alive, DOMDYING_dying, DOMDYING_dead } is_dying;
     /* Domain is paused by controller software? */
     bool_t           is_paused_by_controller;
     /* Domain's VCPUs are pinned 1:1 to physical CPUs? */
     bool_t           is_pinned;
+
+    /* Are any VCPUs polling event channels (SCHEDOP_poll)? */
+    DECLARE_BITMAP(poll_mask, MAX_VIRT_CPUS);
 
     /* Guest has shut down (inc. reason code)? */
     spinlock_t       shutdown_lock;
@@ -507,6 +513,7 @@ static inline int vcpu_runnable(struct v
              atomic_read(&v->domain->pause_count));
 }
 
+void vcpu_unblock(struct vcpu *v);
 void vcpu_pause(struct vcpu *v);
 void vcpu_pause_nosync(struct vcpu *v);
 void domain_pause(struct domain *d);
@@ -523,12 +530,6 @@ void vcpu_unlock_affinity(struct vcpu *v
 
 void vcpu_runstate_get(struct vcpu *v, struct vcpu_runstate_info *runstate);
 
-static inline void vcpu_unblock(struct vcpu *v)
-{
-    if ( test_and_clear_bit(_VPF_blocked, &v->pause_flags) )
-        vcpu_wake(v);
-}
-
 #define IS_PRIV(_d) ((_d)->is_privileged)
 #define IS_PRIV_FOR(_d, _t) (IS_PRIV(_d) || ((_d)->target && (_d)->target == (_t)))
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#17 lore raw
In reply to Jeremy Fitzhardinge
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Jeremy Fitzhardinge <[email protected]> 11.08.08 20:10 >>>

Quoted 5 lines · Jan Beulich

Running kernel builds on 8 vCPU-s competing for 4 pCPU-s shows a 10% improvement in performance with the individual wakeup (patch attached
- probably sub-optimal, but I didn't seem to be able to think of a lock-less mechanism to achieve the desired behavior), using ticket locks in the kernel.

What exactly are you measuring here? Are you testing your 2.6.2x forward-port with an adaptation of pv spinlocks using ticket locks as the underlying locking mechanism? Does the 10% improvement come

Yes.

Quoted 2 lines

compared to straightforward use of sched_poll, or compared to doing spurious wakeup detection?

Compared to spurious wakeup detection done in the kernel, similar to how you had done it in the latest version of your respective patch that I have seen.

Jan

#11 lore raw
In reply to Keir Fraser
Quoted 8 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 12.08.08 19:00 >>>

Quoted 5 lines · Jeremy Fitzhardinge
Quoted 5 lines
Quoted 4 lines

That seems reasonable. In this use-case, it's quite likely that if the poll is interrupted by event delivery, on return it will find that the spinlock is now free and never re-enter the poll.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Attached is a new version of the patch which clears the vcpu from poll_mask when it is unblocked for any reason. Jan: please can you give this one a spin if you get time.

Forgot to attach?

Sorry, attached now.

I can't really explain the results of testing with this version of the patch:
While the number of false wakeups got further reduced by somewhat less than 20%, both time spent in the kernel and total execution time went up (8% and 4% respectively) compared to my original (and from all I can tell worse) version of the patch. Nothing else changed as far as I'm aware.

Jan

#12 lore raw
Quoted 6 lines · Jan Beulich

I can't really explain the results of testing with this version of the patch:
While the number of false wakeups got further reduced by somewhat less than 20%, both time spent in the kernel and total execution time went up (8% and 4% respectively) compared to my original (and from all I can tell worse) version of the patch. Nothing else changed as far as I'm aware.

That is certainly odd. Presumably consistent across a few runs? I can't imagine where extra time would be being spent though...

-- Keir

#13 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 15.08.08 15:01 >>>

Quoted 6 lines · Jan Beulich

I can't really explain the results of testing with this version of the patch:
While the number of false wakeups got further reduced by somewhat less than 20%, both time spent in the kernel and total execution time went up (8% and 4% respectively) compared to my original (and from all I can tell worse) version of the patch. Nothing else changed as far as I'm aware.

Quoted 2 lines

That is certainly odd. Presumably consistent across a few runs? I can't imagine where extra time would be being spent though...

Yes, I did at least five runs in each environment.

Jan

#15 lore raw
In reply to Keir Fraser
Quoted 11 lines · Keir Fraser
Quoted 7 lines · Jan Beulich

I can't really explain the results of testing with this version of the patch:
While the number of false wakeups got further reduced by somewhat less than 20%, both time spent in the kernel and total execution time went up (8% and 4% respectively) compared to my original (and from all I can tell worse) version of the patch. Nothing else changed as far as I'm aware.

Quoted 3 lines

That is certainly odd. Presumably consistent across a few runs? I can't imagine where extra time would be being spent though...

2 locked instructions in vcpu_unblock() when using polling? Doesn't seem like 8%-worth of time though.

J

#14 lore raw
In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 7 lines

I can't really explain the results of testing with this version of the patch:
While the number of false wakeups got further reduced by somewhat less than 20%, both time spent in the kernel and total execution time went up (8% and 4% respectively) compared to my original (and from all I can tell worse) version of the patch. Nothing else changed as far as I'm aware.

Quoted 4 lines
Quoted 2 lines

That is certainly odd. Presumably consistent across a few runs? I can't imagine where extra time would be being spent though...

Yes, I did at least five runs in each environment.

It might be worth retrying with the vcpu_unblock() changes removed. It'll still work, but poll_mask may have bits spuriously left set for arbitrary time periods. However, vcpu_unblock() is the only thing I obviously make more expensive than in your patch.

We could also possibly make the vcpu_unblock() check cheaper by testing v->poll_evtchn for non-zero, and zero it, and clear from poll_mask. Reading a vcpu-local field may be cheaper than getting access to a domain struct cache line.

-- Keir