[PATCH 1/9] timer locking optimization

From: Roman Zippel
Date: Mon Nov 28 2005 - 20:33:55 EST



This simplifies the work done lock_timer_base() and moves it to the less
common path at __mod_timer(). Instead of setting the base to NULL we
already set it to the new base there. If another __mod_timer() gets the
lock it can only move the base to another cpu, so it's enough to check
that base is still the same.
Only add_timer_on() could add timer on the current cpu, but this is not
legal, so we don't have to recheck that the timer has been reactivated
on the current cpu.

Signed-off-by: Roman Zippel <zippel@xxxxxxxxxxxxxx>

---

kernel/timer.c | 33 +++++++++++++++++----------------
1 file changed, 17 insertions(+), 16 deletions(-)

Index: linux-2.6-mm/kernel/timer.c
===================================================================
--- linux-2.6-mm.orig/kernel/timer.c 2005-11-28 22:32:47.000000000 +0100
+++ linux-2.6-mm/kernel/timer.c 2005-11-28 23:04:46.000000000 +0100
@@ -178,27 +178,22 @@ static inline void detach_timer(struct t
*
* So __run_timers/migrate_timers can safely modify all timers which could
* be found on ->tvX lists.
- *
- * When the timer's base is locked, and the timer removed from list, it is
- * possible to set timer->base = NULL and drop the lock: the timer remains
- * locked.
*/
static timer_base_t *lock_timer_base(struct timer_list *timer,
unsigned long *flags)
{
timer_base_t *base;

- for (;;) {
- base = timer->base;
- if (likely(base != NULL)) {
- spin_lock_irqsave(&base->lock, *flags);
- if (likely(base == timer->base))
- return base;
- /* The timer has migrated to another CPU */
- spin_unlock_irqrestore(&base->lock, *flags);
- }
+ base = timer->base;
+ spin_lock_irqsave(&base->lock, *flags);
+ while (unlikely(base != timer->base)) {
+ /* The timer has migrated to another CPU */
+ spin_unlock(&base->lock);
cpu_relax();
+ base = timer->base;
+ spin_lock(&base->lock);
}
+ return base;
}

int __mod_timer(struct timer_list *timer, unsigned long expires)
@@ -212,6 +207,7 @@ int __mod_timer(struct timer_list *timer

base = lock_timer_base(timer, &flags);

+restart:
if (timer_pending(timer)) {
detach_timer(timer, 0);
ret = 1;
@@ -231,11 +227,16 @@ int __mod_timer(struct timer_list *timer
/* The timer remains on a former base */
new_base = container_of(base, tvec_base_t, t_base);
} else {
- /* See the comment in lock_timer_base() */
- timer->base = NULL;
+ /*
+ * We shortly release the timer and the timer can
+ * migrate to another cpu, so recheck the base after
+ * getting the lock.
+ */
+ timer->base = &new_base->t_base;
spin_unlock(&base->lock);
spin_lock(&new_base->t_base.lock);
- timer->base = &new_base->t_base;
+ if (unlikely(timer->base != &new_base->t_base))
+ goto restart;
}
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/