Skip to contentSkip to conversation

ia64: disable preemption in udelay()

PATCH In review [email protected] 8 participants 14 messages Dec 14, 2005 – Dec 23, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ia64: disable preemption in udelay()

Sending this to a wider audience:

The udelay() inline for ia64 uses the ITC. If CONFIG_PREEMPT is enabled and the platform has unsynchronized ITCs and the calling task migrates to another CPU while doing the udelay loop, then the effective delay may be too short or very, very long.

The most simple fix is to disable preemption around the udelay looping. The downside is that this inhibits realtime preemption for cases of long udelays. One datapoint: an SGI realtime engineer reports that if CONFIG_PREEMPT is turned off, that no significant holdoffs are are attributed to udelay().

I am reluctant to propose a much more complicated patch (that disables preemption only for "short" delays, and uses the global RTC as the time base for longer, preemptible delays) unless this patch introduces significant and unacceptable preemption delays.

Index: linux/include/asm-ia64/delay.h
=================================--- linux.orig/include/asm-ia64/delay.h 2005-10-27 17:02:08.000000000 -0700
+++ linux/include/asm-ia64/delay.h 2005-12-14 10:30:55.000000000 -0800
@@ -87,11 +87,17 @@
static __inline__ void
udelay (unsigned long usecs)
{
- unsigned long start = ia64_get_itc();
- unsigned long cycles = usecs*local_cpu_data->cyc_per_usec;
+ unsigned long start;
+ unsigned long cycles;
+
+ preempt_disable();
+ cycles = usecs*local_cpu_data->cyc_per_usec;
+ start = ia64_get_itc();

while (ia64_get_itc() - start < cycles)
cpu_relax();
+
+ preempt_enable();
}

#endif /* _ASM_IA64_DELAY_H */

#1 lore raw
Quoted 13 lines · On Wed, Dec 14, 2005 at 03:25:26PM -0800, [email protected] wrote:

Sending this to a wider audience:

The udelay() inline for ia64 uses the ITC. If CONFIG_PREEMPT is enabled and the platform has unsynchronized ITCs and the calling task migrates to another CPU while doing the udelay loop, then the effective delay may be too short or very, very long.

The most simple fix is to disable preemption around the udelay looping. The downside is that this inhibits realtime preemption for cases of long udelays. One datapoint: an SGI realtime engineer reports that if CONFIG_PREEMPT is turned off, that no significant holdoffs are are attributed to udelay().

Quoted 4 lines

I am reluctant to propose a much more complicated patch (that disables preemption only for "short" delays, and uses the global RTC as the time base for longer, preemptible delays) unless this patch introduces significant and unacceptable preemption delays.

Stuck between a rock and the proverbial hard place.

I think that the more complex patch is needed though. If some crazy driver has a pre-emptible udelay(10000), then you really don't want to spin for that long without allowing preemption.

What is the threshold between a "long" and a "short" delay? Presumably related to whatever your maximum hold-off value is. Also depends on how long it takes to read the global RTC.

If you don't want to get the global RTC involved, then perhaps you can break long delays up inside udelay()? Something like this:

#define SMALLUSECS 250 /* randomly picked, needs some thought! */

static __inline__ void
udelay (unsigned long usecs)

{
	unsigned long start;
	unsigned long cycles;
	unsigned long smallusecs;

	while (usecs > 0) {
		smallusecs = (usecs > SMALLUSECS) ? SMALLUSECS : usecs;
		preempt_disable();
		cycles = smallusecs*local_cpu_data->cyc_per_usec;
		start = ia64_get_itc();

		while (ia64_get_itc() - start < cycles)
			cpu_relax();

		preempt_enable();
		usecs -= smallusecs;
	}
}

This does make the function a bit big for an "inline" though. Does it really need to be inline? Do we care how fast our delay loops are?

-Tony

#2 lore raw
Quoted 3 lines · Tony

This does make the function a bit big for an "inline" though. Does it really need to be inline? Do we care how fast our delay loops are?

Moving the current slim-line udelay() out of line would save 41 Kbytes of text in the generic vmlinux, plus making any modules that use udelay smaller too. Savings run from a 128-160 bytes for drivers with just one call to a max of 9 Kbytes for qla2xxx.ko.

Being out-of-line would reduce accuracy, but this would only be significant when the sleep is for a very small number of microseconds.

So if we need to add more code to udelay(), I think that it should be moved out-of-line too (into arch/ia64/kernel/time.c).

alpha, m68knommu, powerpc and sh64 already have out of line udelay().

-Tony

#3 lore raw
Quoted 5 lines · Tony

Moving the current slim-line udelay() out of line would save 41 Kbytes of text in the generic vmlinux, plus making any modules that use udelay smaller too. Savings run from a 128-160 bytes for drivers with just one call to a max of 9 Kbytes for qla2xxx.ko.

Tony,

we should really take the chance to make the kernel smaller since we don't have to become active to make it bigger. This happens every day by itself. Furthermore I'd guess that for the current and future IA64 implementations we might even win performance by having functions out of line since we have a really fast call mechanism here. Of course any difference heavily depends on the cache utilization so there would be some benchmark needed. At least for the udelay the answer is easy and must be "do it out of line".

Did anyone already run a benchmark for comparison of inline vs. out of line for IA64? Or does anybody want to do it?

Christian

#4 lore raw
In reply to Luck, Tony
Quoted 2 lines · Tony

Being out-of-line would reduce accuracy, but this would only be significant when the sleep is for a very small number of microseconds.

Even then its a predicted branch to code that will probably be in cache so really ought to have no material impact.

#5 lore raw
In reply to Luck, Tony
Quoted 19 lines · Tony
Quoted 13 lines · On Wed, Dec 14, 2005 at 03:25:26PM -0800, [email protected] wrote:

Sending this to a wider audience:

The udelay() inline for ia64 uses the ITC. If CONFIG_PREEMPT is enabled and the platform has unsynchronized ITCs and the calling task migrates to another CPU while doing the udelay loop, then the effective delay may be too short or very, very long.

The most simple fix is to disable preemption around the udelay looping. The downside is that this inhibits realtime preemption for cases of long udelays. One datapoint: an SGI realtime engineer reports that if CONFIG_PREEMPT is turned off, that no significant holdoffs are are attributed to udelay().

Quoted 4 lines

I am reluctant to propose a much more complicated patch (that disables preemption only for "short" delays, and uses the global RTC as the time base for longer, preemptible delays) unless this patch introduces significant and unacceptable preemption delays.

Quoted 5 lines

Stuck between a rock and the proverbial hard place.

I think that the more complex patch is needed though. If some crazy driver has a pre-emptible udelay(10000), then you really don't want to spin for that long without allowing preemption.

If it's a preemptible sleep period it should just use msleep.

#6 lore raw
Quoted 21 lines · Zwane Mwaikambo
Quoted 19 lines · Tony
Quoted 17 lines · On Wed, Dec 14, 2005 at 03:25:26PM -0800, [email protected] wrote:

Sending this to a wider audience:

The udelay() inline for ia64 uses the ITC. If CONFIG_PREEMPT is enabled and the platform has unsynchronized ITCs and the calling task migrates to another CPU while doing the udelay loop, then the effective delay may be too short or very, very long.

The most simple fix is to disable preemption around the udelay looping. The downside is that this inhibits realtime preemption for cases of long udelays. One datapoint: an SGI realtime engineer reports that if CONFIG_PREEMPT is turned off, that no significant holdoffs are are attributed to udelay().

I am reluctant to propose a much more complicated patch (that disables preemption only for "short" delays, and uses the global RTC as the time base for longer, preemptible delays) unless this patch introduces significant and unacceptable preemption delays.

Quoted 7 lines
Quoted 5 lines

Stuck between a rock and the proverbial hard place.

I think that the more complex patch is needed though. If some crazy driver has a pre-emptible udelay(10000), then you really don't want to spin for that long without allowing preemption.

If it's a preemptible sleep period it should just use msleep.

There are 10 drivers that udelay(10000) or more and a TON that udelay(1000). Turning those all into 1ms+ non preemptible sections will be very bad.

Lee

#7 lore raw

From: "Lee Revell" <[email protected]>

Quoted 3 lines

There are 10 drivers that udelay(10000) or more and a TON that udelay(1000). Turning those all into 1ms+ non preemptible sections will be very bad.

What about 100usec non-preemptible sections?
John Hawkes

#8 lore raw
Quoted 7 lines · John Hawkes

From: "Lee Revell" <[email protected]>

Quoted 3 lines

There are 10 drivers that udelay(10000) or more and a TON that udelay(1000). Turning those all into 1ms+ non preemptible sections will be very bad.

What about 100usec non-preemptible sections?
John Hawkes

100usec should be ok as a non-preemptible udelay is this 1 specific driver?

#9 lore raw
In reply to John Hawkes
Quoted 6 lines · John Hawkes

From: "Lee Revell" <[email protected]>

Quoted 3 lines

There are 10 drivers that udelay(10000) or more and a TON that udelay(1000). Turning those all into 1ms+ non preemptible sections will be very bad.

What about 100usec non-preemptible sections?

That will disappear into the noise, in normal usage these happen all the time. 500usec non preemptible regions are rare (~1 hour to show up) and 1ms very rare (24 hours). My tests show that 300 usec or so is a good place to draw the line if you don't want it to show up in latency tests.

Lee

#10 lore raw
Quoted 8 lines · Lee Revell
Quoted 6 lines · John Hawkes

From: "Lee Revell" <[email protected]>

Quoted 3 lines

There are 10 drivers that udelay(10000) or more and a TON that udelay(1000). Turning those all into 1ms+ non preemptible sections will be very bad.

Quoted 7 lines
Quoted 1 line

What about 100usec non-preemptible sections?

That will disappear into the noise, in normal usage these happen all the time. 500usec non preemptible regions are rare (~1 hour to show up) and 1ms very rare (24 hours). My tests show that 300 usec or so is a good place to draw the line if you don't want it to show up in latency tests.

I may be misreading the original post, but the problem is described as one where the TSC is not syncronised and a CPU switch takes place. Would the correct solution be to somehow set CPU affinity temporarily in such a way as to avoid disabling preempt at all?

The preempt doesn't seem to be the root problem, so it's unlikely to be the best solution...

Signature · 3 lines
    -bill davidsen ([email protected])
"The secret to procrastination is to put things off until the
  last possible moment - but no longer"  -me
#11 lore raw
Quoted 17 lines · Bill Davidsen
Quoted 8 lines · Lee Revell
Quoted 6 lines · John Hawkes

From: "Lee Revell" <[email protected]>

Quoted 3 lines

There are 10 drivers that udelay(10000) or more and a TON that udelay(1000). Turning those all into 1ms+ non preemptible sections will be very bad.

Quoted 7 lines
Quoted 1 line

What about 100usec non-preemptible sections?

That will disappear into the noise, in normal usage these happen all the time. 500usec non preemptible regions are rare (~1 hour to show up) and 1ms very rare (24 hours). My tests show that 300 usec or so is a good place to draw the line if you don't want it to show up in latency tests.

Quoted 7 lines

I may be misreading the original post, but the problem is described as one where the TSC is not syncronised and a CPU switch takes place. Would the correct solution be to somehow set CPU affinity temporarily in such a way as to avoid disabling preempt at all?

The preempt doesn't seem to be the root problem, so it's unlikely to be the best solution...

Agreed. See [RFC] Add thread_info flag for "no cpu migration"[1] on lkml. It got no response.

[1] http://marc.theaimsgroup.com/?l=linux-kernel&m3471059115107&w=2

#13 lore raw
In reply to Lee Revell
Quoted 7 lines · Lee Revell
Quoted 2 lines · Zwane Mwaikambo

If it's a preemptible sleep period it should just use msleep.

There are 10 drivers that udelay(10000) or more and a TON that udelay(1000). Turning those all into 1ms+ non preemptible sections will be very bad.

What i said was _msleep_, if the driver is sleeping for 10ms and it can handle being preempted, you can switch it over to _msleep_. The original post was regarding making udelay non-preemptible.