updated low-latency zap_page_range
Subsystems (3)
PATCH 1/1 updated low-latency zap_page_range
Andrew and Linus,
The lock hold time in zap_page_range is horrid. This patch breaks the work up into chunks and relinquishes the lock after each iteration. This drastically lowers latency by creating a preemption point, as well as lowering lock contention.
This patch is updated over the previous: per Linus's suggestion, we now call a new "cond_resched_lock()" function. Per Andrew's suggestion, it checks the preempt_count as to not allow an improper preemption. However, we can now longer allow the conditional reschedule without CONFIG_PREEMPT here (since there is no way to know if it is safe) so it becomes a nop.
This lowers the maximum latency in zap_page_range from 10~20ms (on a dual Athlon - one of the worst latencies recorded) to unmeasurable.
I made a couple other cleanups and optimizations:
- remove unneeded dir variable and call to pgd_offset - nothing uses this anymore as the code was pushed to unmap_page_range
- removed duplicated start variable - it is the same as address
- BUG -> BUG_ON in unmap_page_range
- remove redundant BUG from zap_page_range - the same check is done in unmap_page_range
- better comments
Patch is against 2.5.28, please apply.
Robert Love
include/linux/sched.h +18 -0
@@ -888,6 +888,24 @@__cond_resched (); } +/* +* cond_resched_lock() - if a reschedule is pending, drop the given lock, +* call schedule, and on return reacquire the lock. +* +* Note: this assumes the given lock is the _only_ held lock and otherwise +* you are not atomic. The kernel preemption counter gives us "free" +* checking that this is really the only lock held -- let's use it. +*/ +static inline void cond_resched_lock (spinlock_t * lock) +{ +if (need_resched () &&preempt_count () ==1 ) { +_raw_spin_unlock (lock); +preempt_enable_no_resched (); +__cond_resched (); +spin_lock (lock); + } +} +/* Reevaluate whether the task has signals pending delivery. This is required every time the blocked sigset_t changes. Athread cathreaders should have t->sigmask_lock. */
mm/memory.c +29 -19
70 lines not on this page · raw message
include/linux/sched.h line 907
Quoted 11 lines · Robert Love
...
include/linux/sched.h +9
-0+static inline void cond_resched_lock (spinlock_t * lock) +{ +if (need_resched () &&preempt_count () ==1 ) { +_raw_spin_unlock (lock); +preempt_enable_no_resched (); +__cond_resched (); +spin_lock (lock); + } +}
Maybe I'm being thick. How come a simple spin_unlock() in here won't do the right thing?
And this won't _really_ compile to nothing with CONFIG_PREEMPT=n, will it? It just does nothing because preempt_count() is zero?
-
Quoted 14 lines · Andrew Morton
include/linux/sched.h line 907
Quoted 10 lines · Robert Love
include/linux/sched.h +9
-0+static inline void cond_resched_lock (spinlock_t * lock) +{ +if (need_resched () &&preempt_count () ==1 ) { +_raw_spin_unlock (lock); +preempt_enable_no_resched (); +__cond_resched (); +spin_lock (lock); + } +}Maybe I'm being thick. How come a simple
spin_unlock()in here won't do the right thing?
It will, but we will check need_resched twice. And preempt_count again. My original version just did the "unlock; lock" combo and thus the checking was automatic... but if we want to check before we unlock, we might as well be optimal about it.
Quoted 2 lines
And this won't _really_ compile to nothing with
CONFIG_PREEMPT=n, will it? It just does nothing becausepreempt_count()is zero?
I hope it compiles to nothing! There is a false in an if... oh, wait, to preserve possible side-effects gcc will keep the need_resched() call so I guess we should reorder it as:
if (preempt_count() == 1 && need_resched())
Then we get "if (0 && ..)" which should hopefully be evaluated away. Then the inline is empty and nothing need be done.
Robert Love
Quoted 4 lines · Robert Love
if (preempt_count() == 1 && need_resched())Then we get "if (0 && ..)" which should hopefully be evaluated away.
I think preempt_count() is not unconditionally 0 for non-preemptible kernels, so I don't think this is a compile-time constant.
That may be a bug in preempt_count(), of course.
Linus
Quoted 6 lines · Linus Torvalds
Quoted 1 line
Then we get "if (0 && ..)" which should hopefully be evaluated away.
I think
preempt_count()is not unconditionally 0 for non-preemptible kernels, so I don't think this is a compile-time constant.That may be a bug in
preempt_count(), of course.
Oh, it was until Ingo's IRQ rewrite... we do not want it unconditional anymore. Here is a new version which defines an empty "cond_resched_lock()" for !CONFIG_PREEMPT [1].
Good for you guys?
Robert Love
[1] You may ask, why not just have it drop the lock and reschedule unconditionally if !CONFIG_PREEMPT? The answer is because in this function, as in many others, we do not know the call chain and the locks held. Most callers of zap_page_range() hold no locks on call -- but one does. This is one reason I would prefer to just unconditionally drop the locks and have each kernel do the right thing.
include/linux/sched.h +28 -0
@@ -888,6 +888,34 @@__cond_resched (); } +#ifdef CONFIG_PREEMPT + +/* +* cond_resched_lock() - if a reschedule is pending, drop the given lock, +* call schedule, and on return reacquire the lock. +* +* Note: this assumes the given lock is the _only_ held lock and otherwise +* you are not atomic. The kernel preemption counter gives us "free" +* checking that this is really the only lock held -- let's use it. +*/ +static inline void cond_resched_lock (spinlock_t * lock) +{ +if (need_resched () &&preempt_count () ==1 ) { +_raw_spin_unlock (lock); +preempt_enable_no_resched (); +__cond_resched (); +spin_lock (lock); + } +} + +#else + +static inline void cond_resched_lock (spinlock_t * lock) +{ +} + +#endif +/* Reevaluate whether the task has signals pending delivery. This is required every time the blocked sigset_t changes. Athread cathreaders should have t->sigmask_lock. */
mm/memory.c +29 -19
70 lines not on this page · raw message
Quoted 12 lines · Linus Torvalds
Quoted 4 lines · Robert Love
if (preempt_count() == 1 && need_resched())Then we get "if (0 && ..)" which should hopefully be evaluated away.
I think
preempt_count()is not unconditionally 0 for non-preemptible kernels, so I don't think this is a compile-time constant.That may be a bug in
preempt_count(), of course.
Didn't we just put bh_count and irq_count in the same word???
Signature · 6 lines
George Anzinger [email protected] High-res-timers: http://sourceforge.net/projects/high-res-timers/ Real time sched: http://sourceforge.net/projects/rtsched/ Preemption patch: http://www.kernel.org/pub/linux/kernel/people/rml
Quoted 5 lines · george anzinger
Quoted 3 lines
That may be a bug in
preempt_count(), of course.Didn't we just put
bh_countandirq_countin the same word???
Yes. But that doesn't mean that the "preempt_count()" macro necessarily needs to reflect that.
In particular, we have separate macros for getting the irq bits from that shared word ("irq_count()" etc). Right now they happen to use the "preempt_count()" macro, but that's not really fundamental.
No big deal either way, I suspect.
Linus
Quoted 10 lines · Robert Love
...
I hope it compiles to nothing! There is a false in an if... oh, wait, to preserve possible side-effects gcc will keep theneed_resched()call so I guess we should reorder it as:if (preempt_count() == 1 && need_resched())Then we get "if (0 && ..)" which should hopefully be evaluated away. Then the inline is empty and nothing need be done.
I think someone changed the definition of preempt_count() while you weren't looking.
Plus it's used as an lvalue :(
-