diff options
author | Oleg Nesterov <oleg@tv-sign.ru> | 2005-06-23 03:08:56 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-06-23 12:45:16 -0400 |
commit | 55c888d6d09a0df236adfaf8ccf06ff5d0646775 (patch) | |
tree | deb9434abe3cf7c9b714ccb267ef5d943a847dfe /include | |
parent | bdd646a44672115c986593956aa4ef105485a184 (diff) |
[PATCH] timers fixes/improvements
This patch tries to solve following problems:
1. del_timer_sync() is racy. The timer can be fired again after
del_timer_sync have checked all cpus and before it will recheck
timer_pending().
2. It has scalability problems. All cpus are scanned to determine
if the timer is running on that cpu.
With this patch del_timer_sync is O(1) and no slower than plain
del_timer(pending_timer), unless it has to actually wait for
completion of the currently running timer.
The only restriction is that the recurring timer should not use
add_timer_on().
3. The timers are not serialized wrt to itself.
If CPU_0 does mod_timer(jiffies+1) while the timer is currently
running on CPU 1, it is quite possible that local interrupt on
CPU_0 will start that timer before it finished on CPU_1.
4. The timers locking is suboptimal. __mod_timer() takes 3 locks
at once and still requires wmb() in del_timer/run_timers.
The new implementation takes 2 locks sequentially and does not
need memory barriers.
Currently ->base != NULL means that the timer is pending. In that case
->base.lock is used to lock the timer. __mod_timer also takes timer->lock
because ->base can be == NULL.
This patch uses timer->entry.next != NULL as indication that the timer is
pending. So it does __list_del(), entry->next = NULL instead of list_del()
when the timer is deleted.
The ->base field is used for hashed locking only, it is initialized
in init_timer() which sets ->base = per_cpu(tvec_bases). When the
tvec_bases.lock is locked, it means that all timers which are tied
to this base via timer->base are locked, and the base itself is locked
too.
So __run_timers/migrate_timers can safely modify all timers which could
be found on ->tvX lists (pending timers).
When the timer's base is locked, and the timer removed from ->entry list
(which means that _run_timers/migrate_timers can't see this timer), it is
possible to set timer->base = NULL and drop the lock: the timer remains
locked.
This patch adds lock_timer_base() helper, which waits for ->base != NULL,
locks the ->base, and checks it is still the same.
__mod_timer() schedules the timer on the local CPU and changes it's base.
However, it does not lock both old and new bases at once. It locks the
timer via lock_timer_base(), deletes the timer, sets ->base = NULL, and
unlocks old base. Then __mod_timer() locks new_base, sets ->base = new_base,
and adds this timer. This simplifies the code, because AB-BA deadlock is not
possible. __mod_timer() also ensures that the timer's base is not changed
while the timer's handler is running on the old base.
__run_timers(), del_timer() do not change ->base anymore, they only clear
pending flag.
So del_timer_sync() can test timer->base->running_timer == timer to detect
whether it is running or not.
We don't need timer_list->lock anymore, this patch kills it.
We also don't need barriers. del_timer() and __run_timers() used smp_wmb()
before clearing timer's pending flag. It was needed because __mod_timer()
did not lock old_base if the timer is not pending, so __mod_timer()->list_add()
could race with del_timer()->list_del(). With this patch these functions are
serialized through base->lock.
One problem. TIMER_INITIALIZER can't use per_cpu(tvec_bases). So this patch
adds global
struct timer_base_s {
spinlock_t lock;
struct timer_list *running_timer;
} __init_timer_base;
which is used by TIMER_INITIALIZER. The corresponding fields in tvec_t_base_s
struct are replaced by struct timer_base_s t_base.
It is indeed ugly. But this can't have scalability problems. The global
__init_timer_base.lock is used only when __mod_timer() is called for the first
time AND the timer was compile time initialized. After that the timer migrates
to the local CPU.
Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru>
Acked-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Renaud Lienhart <renaud.lienhart@free.fr>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'include')
-rw-r--r-- | include/linux/timer.h | 30 |
1 files changed, 9 insertions, 21 deletions
diff --git a/include/linux/timer.h b/include/linux/timer.h index 90db1cc62ddd..2e78fedfc069 100644 --- a/include/linux/timer.h +++ b/include/linux/timer.h | |||
@@ -6,45 +6,33 @@ | |||
6 | #include <linux/spinlock.h> | 6 | #include <linux/spinlock.h> |
7 | #include <linux/stddef.h> | 7 | #include <linux/stddef.h> |
8 | 8 | ||
9 | struct tvec_t_base_s; | 9 | struct timer_base_s; |
10 | 10 | ||
11 | struct timer_list { | 11 | struct timer_list { |
12 | struct list_head entry; | 12 | struct list_head entry; |
13 | unsigned long expires; | 13 | unsigned long expires; |
14 | 14 | ||
15 | spinlock_t lock; | ||
16 | unsigned long magic; | 15 | unsigned long magic; |
17 | 16 | ||
18 | void (*function)(unsigned long); | 17 | void (*function)(unsigned long); |
19 | unsigned long data; | 18 | unsigned long data; |
20 | 19 | ||
21 | struct tvec_t_base_s *base; | 20 | struct timer_base_s *base; |
22 | }; | 21 | }; |
23 | 22 | ||
24 | #define TIMER_MAGIC 0x4b87ad6e | 23 | #define TIMER_MAGIC 0x4b87ad6e |
25 | 24 | ||
25 | extern struct timer_base_s __init_timer_base; | ||
26 | |||
26 | #define TIMER_INITIALIZER(_function, _expires, _data) { \ | 27 | #define TIMER_INITIALIZER(_function, _expires, _data) { \ |
27 | .function = (_function), \ | 28 | .function = (_function), \ |
28 | .expires = (_expires), \ | 29 | .expires = (_expires), \ |
29 | .data = (_data), \ | 30 | .data = (_data), \ |
30 | .base = NULL, \ | 31 | .base = &__init_timer_base, \ |
31 | .magic = TIMER_MAGIC, \ | 32 | .magic = TIMER_MAGIC, \ |
32 | .lock = SPIN_LOCK_UNLOCKED, \ | ||
33 | } | 33 | } |
34 | 34 | ||
35 | /*** | 35 | void fastcall init_timer(struct timer_list * timer); |
36 | * init_timer - initialize a timer. | ||
37 | * @timer: the timer to be initialized | ||
38 | * | ||
39 | * init_timer() must be done to a timer prior calling *any* of the | ||
40 | * other timer functions. | ||
41 | */ | ||
42 | static inline void init_timer(struct timer_list * timer) | ||
43 | { | ||
44 | timer->base = NULL; | ||
45 | timer->magic = TIMER_MAGIC; | ||
46 | spin_lock_init(&timer->lock); | ||
47 | } | ||
48 | 36 | ||
49 | /*** | 37 | /*** |
50 | * timer_pending - is a timer pending? | 38 | * timer_pending - is a timer pending? |
@@ -58,7 +46,7 @@ static inline void init_timer(struct timer_list * timer) | |||
58 | */ | 46 | */ |
59 | static inline int timer_pending(const struct timer_list * timer) | 47 | static inline int timer_pending(const struct timer_list * timer) |
60 | { | 48 | { |
61 | return timer->base != NULL; | 49 | return timer->entry.next != NULL; |
62 | } | 50 | } |
63 | 51 | ||
64 | extern void add_timer_on(struct timer_list *timer, int cpu); | 52 | extern void add_timer_on(struct timer_list *timer, int cpu); |
@@ -89,12 +77,12 @@ static inline void add_timer(struct timer_list * timer) | |||
89 | 77 | ||
90 | #ifdef CONFIG_SMP | 78 | #ifdef CONFIG_SMP |
91 | extern int del_timer_sync(struct timer_list *timer); | 79 | extern int del_timer_sync(struct timer_list *timer); |
92 | extern int del_singleshot_timer_sync(struct timer_list *timer); | ||
93 | #else | 80 | #else |
94 | # define del_timer_sync(t) del_timer(t) | 81 | # define del_timer_sync(t) del_timer(t) |
95 | # define del_singleshot_timer_sync(t) del_timer(t) | ||
96 | #endif | 82 | #endif |
97 | 83 | ||
84 | #define del_singleshot_timer_sync(t) del_timer_sync(t) | ||
85 | |||
98 | extern void init_timers(void); | 86 | extern void init_timers(void); |
99 | extern void run_local_timers(void); | 87 | extern void run_local_timers(void); |
100 | extern void it_real_fn(unsigned long); | 88 | extern void it_real_fn(unsigned long); |