timers: enable irqs in __mod_timer()
On top of "[PATCH 5/5] timers: cleanup, kill __get_base()", see
http://marc.theaimsgroup.com/?l=linux-kernel&m=111125359121372
If the timer is currently running on another CPU, __mod_timer() spins with interrupts disabled and timer->lock held. I think it is better to spin_unlock_irqrestore(&timer->lock) in __mod_timer's retry path.
This patch is unneccessary long. It is because it tries to cleanup the code a bit. I do not like the fact that lock+test+unlock pattern is duplicated in the code.
If you think that this patch uglifies the code or does not match kernel's coding style - just say nack :)
kernel/timer.c renamed from kernel/timer.c~6_LOCK +54 -58
@@ -174,64 +174,60 @@ int __mod_timer(struct timer_list *timer {tvec_base_t *old_base, *new_base;unsigned long flags; -int ret =0 ; - -BUG_ON (!timer->function); - -check_timer (timer); - -spin_lock_irqsave (&timer->lock, flags); - new_base = &__get_cpu_var (tvec_bases); -repeat: - old_base =timer_base (timer); - -/* -* Prevent deadlocks via ordering by old_base < new_base. -*/ -if (old_base && (new_base != old_base)) { -if (old_base < new_base) { -spin_lock (&new_base->lock); -spin_lock (&old_base->lock); - }else { -spin_lock (&old_base->lock); -spin_lock (&new_base->lock); - } -/* -* The timer base might have been cancelled while we were -* trying to take the lock(s), or can still be running on -* old_base's CPU. -*/ -if (timer_base (timer) != old_base - || old_base->running_timer == timer) { -spin_unlock (&new_base->lock); -spin_unlock (&old_base->lock); - goto repeat; - } - }else { -spin_lock (&new_base->lock); -if (timer_base (timer) != old_base) { -spin_unlock (&new_base->lock); - goto repeat; - } - } - -/* -* Delete the previous timeout (if there was any). -* We hold timer->lock, no need to check old_base != 0. -*/ -if (timer_pending (timer)) { -list_del (&timer->entry); - ret =1 ; - } - - timer->expires = expires; -internal_add_timer (new_base, timer); -__set_base (timer, new_base,1 ); - -if (old_base && (new_base != old_base)) -spin_unlock (&old_base->lock); -spin_unlock (&new_base->lock); -spin_unlock_irqrestore (&timer->lock, flags); +int new_lock, ret; + +BUG_ON (!timer->function); +check_timer (timer); + +for (ret =-1 ; ret <0 ; ) { +spin_lock_irqsave (&timer->lock, flags); + new_base = &__get_cpu_var (tvec_bases); + old_base =timer_base (timer); + +/* Prevent AB-BA deadlocks. */ + new_lock = old_base < new_base; +if (new_lock) +spin_lock (&new_base->lock); + +/* Note: +* (old_base == NULL) => (new_lock == 1) +* (old_base == new_base) => (new_lock == 0) +*/ +if (old_base) { +spin_lock (&old_base->lock); + +if (timer_base (timer) != old_base) + goto unlock; + +if (old_base != new_base) { +/* Ensure the timer is serialized. */ +if (old_base->running_timer == timer) + goto unlock; + +if (!new_lock) { +spin_lock (&new_base->lock); + new_lock =1 ; + } + } + } + + ret =0 ; +/* We hold timer->lock, no need to check old_base != 0. */ +if (timer_pending (timer)) { +list_del (&timer->entry); + ret =1 ; + } + + timer->expires = expires; +internal_add_timer (new_base, timer); +__set_base (timer, new_base,1 ); +unlock: +if (old_base) +spin_unlock (&old_base->lock); +if (new_lock) +spin_unlock (&new_base->lock); +spin_unlock_irqrestore (&timer->lock, flags); + }return ret; }
Quoted 6 lines · Oleg Nesterov
If the timer is currently running on another CPU,
__mod_timer()
spins with interrupts disabled andtimer->lockheld. I think it is better tospin_unlock_irqrestore(&timer->lock)in __mod_timer's retry path.
Quoted 6 lines
This patch is unneccessary long. It is because it tries to cleanup the code a bit. I do not like the fact that lock+test+unlock pattern is duplicated in the code.
If you think that this patch uglifies the code or does not match kernel's coding style - just say nack :)
I've seen worse ;)
I think this makes it a bit more kernel-like?
kernel/timer.c renamed from kernel/timer.c~timers-enable-irqs-in-__mod_timer-tidy +4 -3
@@ -174,12 +174,13 @@ int __mod_timer(struct timer_list *timer {tvec_base_t *old_base, *new_base;unsigned long flags; -int new_lock, ret; +int new_lock; +int ret =-1 ;BUG_ON (!timer->function);check_timer (timer); -for (ret =-1 ; ret <0 ; ) { + do { spin_lock_irqsave(&timer->lock, flags); new_base = &__get_cpu_var(tvec_bases); old_base = timer_base(timer);
@@ -227,7 +228,7 @@ unlock:if (new_lock)spin_unlock (&new_base->lock);spin_unlock_irqrestore (&timer->lock, flags); - } + }while (ret ==-1 );return ret; }
_
RE: [PATCH 0/5] timers: description
In reply to Oleg NesterovOleg Nesterov wrote on Monday, March 21, 2005 6:19 AM
Quoted 2 lines
These patches are updated version of '
del_timer_sync: proof of concept' 2 patches.
Looks good performance wise. Took a quick micro benchmark measurement on a 16-node numa box (32-way). Results are pretty nice (to the expectation).
Time to execute del_timer_sync, averaged over 10,000 call:
Vanilla 2.6.11 19,313 ns
2.6.12-rc1-mm1 81 nsdel_singleshot_timer_sync 74ns
Took another measurement on a 4-way smp box:
Vanilla 2.6.11 648 ns
2.6.12-rc1-mm1 55 nsdel_singleshot 41 ns
And Andrew always asks what are the impact on real-world bench, we did not see performance regression on db transaction processing benchmark with these set of timer patches versus using del_singleshot_timer_sync. (del_singleshot_timer_sync is slightly faster, though 14 ns difference falls well into noise range)
Note: I've only stress tested deleting an un-expired timer.
RE: [PATCH 0/5] timers: description
In reply to Oleg NesterovOleg Nesterov wrote on March 19, 2005 17:28:48
Quoted 2 lines
These patches are updated version of '
del_timer_sync: proof of concept' 2 patches.
I changed schedule_timeout() to call the new del_timer_sync instead of currently del_singleshot_timer_sync in attempt to stress these set of patches a bit more and I just observed a kernel hang.
The symptom starts with lost network connectivity. It looks like the entire ethernet connections were gone, followed by blank screen on the console. I'm not sure whether it is a hard or soft hang, but system is inaccessible (blank screen and no network connection). I'm forced to do a reboot when that happens.
Re: [PATCH 0/5] timers: description
Quoted 9 lines · Kenneth W
Oleg Nesterov wrote on March 19, 2005 17:28:48
Quoted 2 lines
These patches are updated version of '
del_timer_sync: proof of concept' 2 patches.I changed
schedule_timeout()to call the newdel_timer_syncinstead of currentlydel_singleshot_timer_syncin attempt to stress these set of patches a bit more and I just observed a kernel hang.
Quoted 5 lines
The symptom starts with lost network connectivity. It looks like the entire ethernet connections were gone, followed by blank screen on the console. I'm not sure whether it is a hard or soft hang, but system is inaccessible (blank screen and no network connection). I'm forced to do a reboot when that happens.
Very strange. I am running 2.6.11 + timer patches +
#define del_singleshot_timer_sync(t) del_timer_sync(t)
without any problems.
This timer is private to schedule_timeout(), it can't change base, so del_timer_sync() should be "obviously correct" in that case.
What kernel version? Could you try this stupid patch?
Oleg.
kernel/timer.c renamed from kernel/timer.c~ +22 -3
@@ -352,27 +352,46 @@ EXPORT_SYMBOL(del_timer);*/ int del_timer_sync (struct timer_list *timer) { +unsigned long tout; +int running =0 , migrated =0 , done =0 ;int ret;check_timer (timer); +preempt_disable (); + tout = jiffies +10 ; + ret =0 ;for (;;) {unsigned long flags;tvec_base_t *base; base =timer_base (timer); -if (!base) +if (!base) { +preempt_enable ();return ret; + } +if (time_after (jiffies, tout)) { +preempt_enable (); +printk (KERN_ERR "del_timer_sync hang: %d %d %d %d\n" , + running, migrated, done, ret); +dump_stack (); +return 0 ; + }spin_lock_irqsave (&base->lock, flags); -if (base->running_timer == timer) +if (base->running_timer == timer) { + ++running; goto unlock; + } -if (timer_base (timer) != base) +if (timer_base (timer) != base) { + ++migrated; goto unlock; + } + ++done;if (timer_pending (timer)) {list_del (&timer->entry); ret =1 ;
RE: [PATCH 0/5] timers: description
In reply to Chen, Kenneth WQuoted 8 lines · Kenneth W
Oleg Nesterov wrote on March 19, 2005 17:28:48
Quoted 2 lines
These patches are updated version of '
del_timer_sync: proof of concept' 2 patches.I changed
schedule_timeout()to call the newdel_timer_syncinstead of currentlydel_singleshot_timer_syncin attempt to stress these set of patches a bit more and I just observed a kernel hang.
Quoted 5 lines
The symptom starts with lost network connectivity. It looks like the entire ethernet connections were gone, followed by blank screen on the console. I'm not sure whether it is a hard or soft hang, but system is inaccessible (blank screen and no network connection). I'm forced to do a reboot when that happens.
Same problems here with occasional hangs w/o changes to schedule_timeout.
Re: [PATCH 0/5] timers: description
Quoted 7 lines · Christoph Lameter
Quoted 4 lines · Kenneth W
I changed
schedule_timeout()to call the newdel_timer_syncinstead of currentlydel_singleshot_timer_syncin attempt to stress these set of patches a bit more and I just observed a kernel hang.
Quoted 7 lines
Quoted 5 lines
The symptom starts with lost network connectivity. It looks like the entire ethernet connections were gone, followed by blank screen on the console. I'm not sure whether it is a hard or soft hang, but system is inaccessible (blank screen and no network connection). I'm forced to do a reboot when that happens.
Same problems here with occasional hangs w/o changes to
schedule_timeout.
Bad. You are runnning 2.6.12-rc1-mm1 ?
Oleg.
Re: [PATCH 0/5] timers: description
Quoted 3 lines · Oleg Nesterov
Quoted 1 line
Same problems here with occasional hangs w/o changes to
schedule_timeout.Bad. You are runnning 2.6.12-rc1-mm1 ?
Not sure if this is really related to your patches. Its 2.6.11 with your patches extracted from mm.