udelay() & preemption & drifty ITCs
I may be overlooking something, but isnt there a problem with the udelay() function on IA64 platforms with drifty ITCs when preemption is enabled.
The function uses the ITC for timing. If preemption occurs & the process migrates to a different cpu with a much larger ITC value, it seems like the delay may be satisfied too quickly.
What am I missing.....
Signature · 4 lines
Thanks Jack Steiner ([email protected]) 651-683-5302 Principal Engineer SGI - Silicon Graphics, Inc.
Quoted 7 lines · Jack Steiner
I may be overlooking something, but isnt there a problem with the
udelay()function on IA64 platforms with drifty ITCs when preemption is enabled.The function uses the ITC for timing. If preemption occurs & the process migrates to a different cpu with a much larger ITC value, it seems like the delay may be satisfied too quickly.
Yep, it appears so. I guess we need a 'preempt_disable/disable()' pair around the itc access. Of course, callers under a spinlock are already protected, so maybe exposure to this problem isn't that large?
Jesse
Quoted 3 lines · Jesse Barnes
Yep, it appears so. I guess we need a 'preempt_disable/disable()' pair around the itc access. Of course, callers under a spinlock are already protected, so maybe exposure to this problem isn't that large?
Here's the patch. Compiles and boots, but I haven't made a test module that illustrates this bug, so I haven't tested it.
Jesse
include/asm-ia64/delay.h +3 -0
@@ -15,6 +15,7 @@#include <linux/config.h> #include <linux/kernel.h> #include <linux/sched.h> +#include <linux/preempt.h> #include <linux/compiler.h> #include <asm/intrinsics.h>
@@ -32,8 +33,10 @@ {unsigned long result; +preempt_disable (); result =ia64_getreg (_IA64_REG_CR_ITM);ia64_srlz_d (); +preempt_enable ();return result; }
Quoted 7 lines · Jesse Barnes
Quoted 3 lines · Jesse Barnes
Yep, it appears so. I guess we need a 'preempt_disable/disable()' pair around the itc access. Of course, callers under a spinlock are already protected, so maybe exposure to this problem isn't that large?
Here's the patch. Compiles and boots, but I haven't made a test module that illustrates this bug, so I haven't tested it.
Ugg, where did I keep my brown paper bags? Ignore this patch, I'll post another in a minute.
Jesse
Quoted 7 lines · Jesse Barnes
Quoted 3 lines · Jesse Barnes
Yep, it appears so. I guess we need a 'preempt_disable/disable()' pair around the itc access. Of course, callers under a spinlock are already protected, so maybe exposure to this problem isn't that large?
Here's the patch. Compiles and boots, but I haven't made a test module that illustrates this bug, so I haven't tested it.
Here's one that actually patches the affected routine. Same caveat applies.
Jesse
include/asm-ia64/delay.h +6 -2
@@ -15,6 +15,7 @@#include <linux/config.h> #include <linux/kernel.h> #include <linux/sched.h> +#include <linux/preempt.h> #include <linux/compiler.h> #include <asm/intrinsics.h>
@@ -81,11 +82,14 @@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, cycles; +preempt_disable (); + start =ia64_get_itc (); + cycles = usecs*local_cpu_data->cyc_per_usec;while (ia64_get_itc () - start < cycles)/* skip */ ; +preempt_enable (); }#endif /* _ASM_IA64_DELAY_H */
Quoted 11 lines · Jesse Barnes
Quoted 7 lines · Jesse Barnes
Quoted 3 lines · Jesse Barnes
Yep, it appears so. I guess we need a 'preempt_disable/disable()' pair around the itc access. Of course, callers under a spinlock are already protected, so maybe exposure to this problem isn't that large?
Here's the patch. Compiles and boots, but I haven't made a test module that illustrates this bug, so I haven't tested it.
Here's one that actually patches the affected routine. Same caveat applies.
I dislike disabling preemption in udelay(). Although most delays are short, there are some delays that are 100s or 1000s of usec long. Users running soft-realtime really dont want preemption disabled unnecessarily.
On platforms with synchronized clocks, disabling preemption is not required.
Even on platforms with drifty ITCs, preemption in udelay() is ok as long as migration is disabled. AFAIK, there is no standard API for disabling migration aside from saving/changing/restoring the cpus_allowed mask.
Quoted 31 lines
Jesse
diff -Nru
a/include/asm-ia64/delay.hb/include/asm-ia64/delay.hinclude/asm-ia64/delay.h +6
-2@@ -15,6 +15,7 @@#include <linux/config.h> #include <linux/kernel.h> #include <linux/sched.h> +#include <linux/preempt.h> #include <linux/compiler.h> #include <asm/intrinsics.h> @@ -81,11 +82,14 @@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, cycles; +preempt_disable (); + start =ia64_get_itc (); + cycles = usecs*local_cpu_data->cyc_per_usec;while (ia64_get_itc () - start < cycles)/* skip */ ; +preempt_enable (); }#endif /* _ASM_IA64_DELAY_H */
Signature · 4 lines
Thanks Jack Steiner ([email protected]) 651-683-5302 Principal Engineer SGI - Silicon Graphics, Inc.
Quoted 5 lines · Jack Steiner
I dislike disabling preemption in
udelay(). Although most delays are short, there are some delays that are 100s or 1000s of usec long. Users running soft-realtime really dont want preemption disabled unnecessarily.On platforms with synchronized clocks, disabling preemption is not required.
Yeah, I thought about this too, but unless we have a global flag telling us if itcs drift or not, it seems lik we have to unconditionally disable preemption.
Quoted 3 lines
Even on platforms with drifty ITCs, preemption in
udelay()is ok as long as migration is disabled. AFAIK, there is no standard API for disabling migration aside from saving/changing/restoring thecpus_allowedmask.
Then we'd have to check if drifty itcs were present and also check to see if the process was pinned...
If we found enough code that depended on having preemption disabled because of itc drift, it might be worthwhile to add another inline to deal with it. As for CPU pinning, maybe we could check in preempt_disable() whether the cpus_allowed mask only had one bit set--that would prevent us from disabling preemption at all for pinned processes, right?
Jesse