timers: add mod_timer_pending()

Impact: new timer API

Based on an idea from Martin Josefsson with the help of
Patrick McHardy and Stephen Hemminger:

introduce the mod_timer_pending() API which is a mod_timer()
offspring that is an invariant on already removed timers.

(regular mod_timer() re-activates non-pending timers.)

This is useful for the networking code in that it can
allow unserialized mod_timer_pending() timer-forwarding
calls, but a single del_timer*() will stop the timer
from being reactivated again.

Also while at it:

- optimize the regular mod_timer() path some more, the
  timer-stat and a debug check was needlessly duplicated
  in __mod_timer().

- make the exports come straight after the function, as
  most other exports in timer.c already did.

- eliminate __mod_timer() as an external API, change the
  users to mod_timer().

The regular mod_timer() code path is not impacted
significantly, due to inlining optimizations and due to
the simplifications.

Based-on-patch-from: Stephen Hemminger <shemminger@vyatta.com>
Acked-by: Stephen Hemminger <shemminger@vyatta.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Patrick McHardy <kaber@trash.net>
Cc: netdev@vger.kernel.org
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
This commit is contained in:
Ingo Molnar 2009-02-18 12:23:29 +01:00
Родитель 5955c7a2cf
Коммит 74019224ac
5 изменённых файлов: 88 добавлений и 70 удалений

Просмотреть файл

@ -508,7 +508,7 @@ static void __spu_add_to_rq(struct spu_context *ctx)
list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]); list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
set_bit(ctx->prio, spu_prio->bitmap); set_bit(ctx->prio, spu_prio->bitmap);
if (!spu_prio->nr_waiting++) if (!spu_prio->nr_waiting++)
__mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK); mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
} }
} }

Просмотреть файл

@ -2715,7 +2715,7 @@ static void ipath_hol_signal_up(struct ipath_devdata *dd)
* to prevent HoL blocking, then start the HoL timer that * to prevent HoL blocking, then start the HoL timer that
* periodically continues, then stop procs, so they can detect * periodically continues, then stop procs, so they can detect
* link down if they want, and do something about it. * link down if they want, and do something about it.
* Timer may already be running, so use __mod_timer, not add_timer. * Timer may already be running, so use mod_timer, not add_timer.
*/ */
void ipath_hol_down(struct ipath_devdata *dd) void ipath_hol_down(struct ipath_devdata *dd)
{ {
@ -2724,7 +2724,7 @@ void ipath_hol_down(struct ipath_devdata *dd)
dd->ipath_hol_next = IPATH_HOL_DOWNCONT; dd->ipath_hol_next = IPATH_HOL_DOWNCONT;
dd->ipath_hol_timer.expires = jiffies + dd->ipath_hol_timer.expires = jiffies +
msecs_to_jiffies(ipath_hol_timeout_ms); msecs_to_jiffies(ipath_hol_timeout_ms);
__mod_timer(&dd->ipath_hol_timer, dd->ipath_hol_timer.expires); mod_timer(&dd->ipath_hol_timer, dd->ipath_hol_timer.expires);
} }
/* /*
@ -2763,7 +2763,7 @@ void ipath_hol_event(unsigned long opaque)
else { else {
dd->ipath_hol_timer.expires = jiffies + dd->ipath_hol_timer.expires = jiffies +
msecs_to_jiffies(ipath_hol_timeout_ms); msecs_to_jiffies(ipath_hol_timeout_ms);
__mod_timer(&dd->ipath_hol_timer, mod_timer(&dd->ipath_hol_timer,
dd->ipath_hol_timer.expires); dd->ipath_hol_timer.expires);
} }
} }

Просмотреть файл

@ -86,8 +86,8 @@ static inline int timer_pending(const struct timer_list * timer)
extern void add_timer_on(struct timer_list *timer, int cpu); extern void add_timer_on(struct timer_list *timer, int cpu);
extern int del_timer(struct timer_list * timer); extern int del_timer(struct timer_list * timer);
extern int __mod_timer(struct timer_list *timer, unsigned long expires);
extern int mod_timer(struct timer_list *timer, unsigned long expires); extern int mod_timer(struct timer_list *timer, unsigned long expires);
extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
/* /*
* The jiffies value which is added to now, when there is no timer * The jiffies value which is added to now, when there is no timer
@ -146,25 +146,7 @@ static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
} }
#endif #endif
/** extern void add_timer(struct timer_list *timer);
* add_timer - start a timer
* @timer: the timer to be added
*
* The kernel will do a ->function(->data) callback from the
* timer interrupt at the ->expires point in the future. The
* current time is 'jiffies'.
*
* The timer's ->expires, ->function (and if the handler uses it, ->data)
* fields must be set prior calling this function.
*
* Timers with an ->expires field in the past will be executed in the next
* timer tick.
*/
static inline void add_timer(struct timer_list *timer)
{
BUG_ON(timer_pending(timer));
__mod_timer(timer, timer->expires);
}
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
extern int try_to_del_timer_sync(struct timer_list *timer); extern int try_to_del_timer_sync(struct timer_list *timer);

Просмотреть файл

@ -750,7 +750,7 @@ size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
* from the scheduler (trying to re-grab * from the scheduler (trying to re-grab
* rq->lock), so defer it. * rq->lock), so defer it.
*/ */
__mod_timer(&buf->timer, jiffies + 1); mod_timer(&buf->timer, jiffies + 1);
} }
old = buf->data; old = buf->data;

Просмотреть файл

@ -589,11 +589,14 @@ static struct tvec_base *lock_timer_base(struct timer_list *timer,
} }
} }
int __mod_timer(struct timer_list *timer, unsigned long expires) static inline int
__mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
{ {
struct tvec_base *base, *new_base; struct tvec_base *base, *new_base;
unsigned long flags; unsigned long flags;
int ret = 0; int ret;
ret = 0;
timer_stats_timer_set_start_info(timer); timer_stats_timer_set_start_info(timer);
BUG_ON(!timer->function); BUG_ON(!timer->function);
@ -603,6 +606,9 @@ int __mod_timer(struct timer_list *timer, unsigned long expires)
if (timer_pending(timer)) { if (timer_pending(timer)) {
detach_timer(timer, 0); detach_timer(timer, 0);
ret = 1; ret = 1;
} else {
if (pending_only)
goto out_unlock;
} }
debug_timer_activate(timer); debug_timer_activate(timer);
@ -629,12 +635,83 @@ int __mod_timer(struct timer_list *timer, unsigned long expires)
timer->expires = expires; timer->expires = expires;
internal_add_timer(base, timer); internal_add_timer(base, timer);
out_unlock:
spin_unlock_irqrestore(&base->lock, flags); spin_unlock_irqrestore(&base->lock, flags);
return ret; return ret;
} }
EXPORT_SYMBOL(__mod_timer); /**
* mod_timer_pending - modify a pending timer's timeout
* @timer: the pending timer to be modified
* @expires: new timeout in jiffies
*
* mod_timer_pending() is the same for pending timers as mod_timer(),
* but will not re-activate and modify already deleted timers.
*
* It is useful for unserialized use of timers.
*/
int mod_timer_pending(struct timer_list *timer, unsigned long expires)
{
return __mod_timer(timer, expires, true);
}
EXPORT_SYMBOL(mod_timer_pending);
/**
* mod_timer - modify a timer's timeout
* @timer: the timer to be modified
* @expires: new timeout in jiffies
*
* mod_timer() is a more efficient way to update the expire field of an
* active timer (if the timer is inactive it will be activated)
*
* mod_timer(timer, expires) is equivalent to:
*
* del_timer(timer); timer->expires = expires; add_timer(timer);
*
* Note that if there are multiple unserialized concurrent users of the
* same timer, then mod_timer() is the only safe way to modify the timeout,
* since add_timer() cannot modify an already running timer.
*
* The function returns whether it has modified a pending timer or not.
* (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
* active timer returns 1.)
*/
int mod_timer(struct timer_list *timer, unsigned long expires)
{
/*
* This is a common optimization triggered by the
* networking code - if the timer is re-modified
* to be the same thing then just return:
*/
if (timer->expires == expires && timer_pending(timer))
return 1;
return __mod_timer(timer, expires, false);
}
EXPORT_SYMBOL(mod_timer);
/**
* add_timer - start a timer
* @timer: the timer to be added
*
* The kernel will do a ->function(->data) callback from the
* timer interrupt at the ->expires point in the future. The
* current time is 'jiffies'.
*
* The timer's ->expires, ->function (and if the handler uses it, ->data)
* fields must be set prior calling this function.
*
* Timers with an ->expires field in the past will be executed in the next
* timer tick.
*/
void add_timer(struct timer_list *timer)
{
BUG_ON(timer_pending(timer));
mod_timer(timer, timer->expires);
}
EXPORT_SYMBOL(add_timer);
/** /**
* add_timer_on - start a timer on a particular CPU * add_timer_on - start a timer on a particular CPU
@ -666,44 +743,6 @@ void add_timer_on(struct timer_list *timer, int cpu)
spin_unlock_irqrestore(&base->lock, flags); spin_unlock_irqrestore(&base->lock, flags);
} }
/**
* mod_timer - modify a timer's timeout
* @timer: the timer to be modified
* @expires: new timeout in jiffies
*
* mod_timer() is a more efficient way to update the expire field of an
* active timer (if the timer is inactive it will be activated)
*
* mod_timer(timer, expires) is equivalent to:
*
* del_timer(timer); timer->expires = expires; add_timer(timer);
*
* Note that if there are multiple unserialized concurrent users of the
* same timer, then mod_timer() is the only safe way to modify the timeout,
* since add_timer() cannot modify an already running timer.
*
* The function returns whether it has modified a pending timer or not.
* (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
* active timer returns 1.)
*/
int mod_timer(struct timer_list *timer, unsigned long expires)
{
BUG_ON(!timer->function);
timer_stats_timer_set_start_info(timer);
/*
* This is a common optimization triggered by the
* networking code - if the timer is re-modified
* to be the same thing then just return:
*/
if (timer->expires == expires && timer_pending(timer))
return 1;
return __mod_timer(timer, expires);
}
EXPORT_SYMBOL(mod_timer);
/** /**
* del_timer - deactive a timer. * del_timer - deactive a timer.
* @timer: the timer to be deactivated * @timer: the timer to be deactivated
@ -733,7 +772,6 @@ int del_timer(struct timer_list *timer)
return ret; return ret;
} }
EXPORT_SYMBOL(del_timer); EXPORT_SYMBOL(del_timer);
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
@ -767,7 +805,6 @@ out:
return ret; return ret;
} }
EXPORT_SYMBOL(try_to_del_timer_sync); EXPORT_SYMBOL(try_to_del_timer_sync);
/** /**
@ -796,7 +833,6 @@ int del_timer_sync(struct timer_list *timer)
cpu_relax(); cpu_relax();
} }
} }
EXPORT_SYMBOL(del_timer_sync); EXPORT_SYMBOL(del_timer_sync);
#endif #endif
@ -1268,7 +1304,7 @@ signed long __sched schedule_timeout(signed long timeout)
expire = timeout + jiffies; expire = timeout + jiffies;
setup_timer_on_stack(&timer, process_timeout, (unsigned long)current); setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
__mod_timer(&timer, expire); __mod_timer(&timer, expire, false);
schedule(); schedule();
del_singleshot_timer_sync(&timer); del_singleshot_timer_sync(&timer);