mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Re: [PATCH 1/9] timer locking optimization
@ 2005-11-29 12:57 Oleg Nesterov
  2005-11-29 12:17 ` Roman Zippel
  2005-11-30  2:32 ` Roman Zippel
  0 siblings, 2 replies; 9+ messages in thread
From: Oleg Nesterov @ 2005-11-29 12:57 UTC (permalink / raw)
  To: Roman Zippel; +Cc: linux-kernel

Roman Zippel wrote:
>
> +	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);

This spins with interrupts disabled, not good, imho.

>  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;

This is not right.

This way you can delete the timer (ret == 1), notice that timer's base
was changed after re-locking, goto restart, and get ret == 0.

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

Oleg.

^ permalink raw reply	[flat|nested] 9+ messages in thread
* [PATCH 1/9] timer locking optimization
@ 2005-11-29  1:34 Roman Zippel
  0 siblings, 0 replies; 9+ messages in thread
From: Roman Zippel @ 2005-11-29  1:34 UTC (permalink / raw)
  To: linux-kernel


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@linux-m68k.org>

---

 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;
 		}
 	}
 

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-11-30 17:02 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-29 12:57 [PATCH 1/9] timer locking optimization Oleg Nesterov
2005-11-29 12:17 ` Roman Zippel
2005-11-30  2:32 ` Roman Zippel
2005-11-30  9:59   ` Oleg Nesterov
2005-11-30 15:07     ` Roman Zippel
2005-11-30 18:17       ` Oleg Nesterov
2005-11-30 11:23   ` Oleg Nesterov
2005-11-30 11:06     ` Roman Zippel
  -- strict thread matches above, loose matches on Subject: below --
2005-11-29  1:34 Roman Zippel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®