Re: [PATCH 1/9] timer locking optimization

From: Roman Zippel
Date: Tue Nov 29 2005 - 21:32:08 EST


Hi,

On Tue, 29 Nov 2005, Oleg Nesterov wrote:

> Also, you have wrong value of 'base' after 'goto restart'.

Here is the updated patch, which fixes this.

bye, Roman

Index: linux-2.6/kernel/timer.c
===================================================================
--- linux-2.6.orig/kernel/timer.c 2005-11-29 13:29:35.000000000 +0100
+++ linux-2.6/kernel/timer.c 2005-11-29 14:01:42.000000000 +0100
@@ -178,27 +178,20 @@ 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);
- }
- cpu_relax();
- }
+again:
+ base = timer->base;
+ 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);
+ goto again;
}

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

BUG_ON(!timer->function);

+restart:
base = lock_timer_base(timer, &flags);

if (timer_pending(timer)) {
@@ -231,11 +225,18 @@ 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)) {
+ spin_unlock_irqrestore(&new_base->t_base.lock, flags);
+ 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/