Skip to contentSkip to conversation

timer: patchset focus on del_timer_sync()

RFC PATCH In review Yong Zhang 2 participants 6 messages Aug 25, 2010 High-resolution timers, timer wheel, clockevents

lore
Share & tools
lore

Yong Zhang

author
#0 lore raw

v2 cover timer: patchset focus on del_timer_sync()

From: Yong Zhang <[email protected]>

This is inspired by http://lkml.org/lkml/2010/8/16/291
which catch a lockdep false positive on fake_timer_lock. When I go into del_timer_sync(), but don't find anything which prevent del_timer_sync() from using in softirq context, and indeed it's been used in softirq for some timer(such as __dst_free()).

Thus, tell others it can't be used in softirq context, and teach lockdep about that. It's realized by patch-0002.

Your comments are very appreciated.

Thanks,
Yong

V2 <- V1
Update patch 0001, the new version make try_to_del_timer_sync() safe on both hardirq context and UP. Thanks Oleg.

---
Yong Zhang (3):
timer: make try_to_del_timer_sync() safe on both hardirq context and UP
timer: del_timer_sync() can be used in softirq context
timer: warn when del_timer_sync() used in hardirq context

 include/linux/timer.h |    4 ++--
 kernel/timer.c        |   20 +++++++++-----------
 2 files changed, 11 insertions(+), 13 deletions(-)
author
#1 lore raw

RFC PATCH v2 1/3 timer: make try_to_del_timer_sync() safe on both hardirq context and UP

From: Yong Zhang <[email protected]>

In commit fd450b7318b75343fd76b3d95416853e34e72c95, it was saying try_to_del_timer_sync() can be used in interrupt context.

But because base->running_timer is SMP special, this lead to unsymmetry try_to_del_timer_sync() on UP and SMP.

We can make running_timer live on everywhere, then try_to_del_timer_sync() is also UP safe and can be used in hardirq context.

Cc Thomas Gleixner <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Oleg Nesterov <[email protected]>

---
Hi Oleg,

How do you think about this one?

Yong

 include/linux/timer.h |    4 ++--
 kernel/timer.c        |    6 +-----
 2 files changed, 3 insertions(+), 7 deletions(-)
include/linux/timer.h +2 -2
index 38cf093..225c4ce 100644
@@ -248,11 +248,11 @@ static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
 
 extern void add_timer(struct timer_list *timer);
 
+extern int try_to_del_timer_sync(struct timer_list *timer);
+
 #ifdef CONFIG_SMP
-  extern int try_to_del_timer_sync(struct timer_list *timer);
   extern int del_timer_sync(struct timer_list *timer);
 #else
-# define try_to_del_timer_sync(t)	del_timer(t)
 # define del_timer_sync(t)		del_timer(t)
 #endif
 
kernel/timer.c +1 -5
index 97bf05b..e031eb4 100644
@@ -347,9 +347,7 @@ EXPORT_SYMBOL_GPL(set_timer_slack);
 static inline void set_running_timer(struct tvec_base *base,
 					struct timer_list *timer)
 {
-#ifdef CONFIG_SMP
 	base->running_timer = timer;
-#endif
 }
 
 static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
@@ -936,15 +934,12 @@ int del_timer(struct timer_list *timer)
 }
 EXPORT_SYMBOL(del_timer);
 
-#ifdef CONFIG_SMP
 /**
  * try_to_del_timer_sync - Try to deactivate a timer
  * @timer: timer do del
  *
  * This function tries to deactivate a timer. Upon successful (ret >= 0)
  * exit the timer is not queued and the handler is not running on any CPU.
- *
- * It must not be called from interrupt contexts.
  */
 int try_to_del_timer_sync(struct timer_list *timer)
 {
@@ -973,6 +968,7 @@ out:
 }
 EXPORT_SYMBOL(try_to_del_timer_sync);
 
+#ifdef CONFIG_SMP
 /**
  * del_timer_sync - deactivate a timer and wait for the handler to finish.
  * @timer: the timer to be deactivated
Signature · 1 line
1.7.0.4
Acked-by on v2 1/3
#2 lore raw

Re: [RFC V2 PATCH 1/3] timer: make try_to_del_timer_sync() safe on both hardirq context and UP

Quoted 11 lines · Yong Zhang

From: Yong Zhang <[email protected]>

In commit fd450b7318b75343fd76b3d95416853e34e72c95, it was saying try_to_del_timer_sync() can be used in interrupt context.

But because base->running_timer is SMP special, this lead to unsymmetry try_to_del_timer_sync() on UP and SMP.

We can make running_timer live on everywhere, then try_to_del_timer_sync() is also UP safe and can be used in hardirq context.

Quoted 75 lines
Cc Thomas Gleixner <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Oleg Nesterov <[email protected]>

---
Hi Oleg,

How do you think about this one?

Yong

 include/linux/timer.h |    4 ++--
 kernel/timer.c        |    6 +-----
 2 files changed, 3 insertions(+), 7 deletions(-)
include/linux/timer.h +2 -2
index 38cf093..225c4ce 100644
@@ -248,11 +248,11 @@ static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
 
 extern void add_timer(struct timer_list *timer);
 
+extern int try_to_del_timer_sync(struct timer_list *timer);
+
 #ifdef CONFIG_SMP
-  extern int try_to_del_timer_sync(struct timer_list *timer);
   extern int del_timer_sync(struct timer_list *timer);
 #else
-# define try_to_del_timer_sync(t)	del_timer(t)
 # define del_timer_sync(t)		del_timer(t)
 #endif
 
kernel/timer.c +1 -5
index 97bf05b..e031eb4 100644
@@ -347,9 +347,7 @@ EXPORT_SYMBOL_GPL(set_timer_slack);
 static inline void set_running_timer(struct tvec_base *base,
 					struct timer_list *timer)
 {
-#ifdef CONFIG_SMP
 	base->running_timer = timer;
-#endif
 }
 
 static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
@@ -936,15 +934,12 @@ int del_timer(struct timer_list *timer)
 }
 EXPORT_SYMBOL(del_timer);
 
-#ifdef CONFIG_SMP
 /**
  * try_to_del_timer_sync - Try to deactivate a timer
  * @timer: timer do del
  *
  * This function tries to deactivate a timer. Upon successful (ret >= 0)
  * exit the timer is not queued and the handler is not running on any CPU.
- *
- * It must not be called from interrupt contexts.
  */
 int try_to_del_timer_sync(struct timer_list *timer)
 {
@@ -973,6 +968,7 @@ out:
 }
 EXPORT_SYMBOL(try_to_del_timer_sync);
 
+#ifdef CONFIG_SMP
 /**
  * del_timer_sync - deactivate a timer and wait for the handler to finish.
  * @timer: the timer to be deactivated
Signature · 1 line
1.7.0.4
author
#3 lore raw

RFC PATCH v2 2/3 timer: del_timer_sync() can be used in softirq context

In reply to Yong Zhang

From: Yong Zhang <[email protected]>

Actually we have used del_timer_sync() in softirq context for a long time, such as: __dst_free()::cancel_delayed_work() and maybe the DEBUG_OBJECTS_TIMERS fundamental code.

So change the comments of it to warn on hardirq context only, and make lockdep know about this change.

Cc Thomas Gleixner <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/timer.c |    8 +++-----
 1 files changed, 3 insertions(+), 5 deletions(-)
kernel/timer.c +3 -5
index 55b6a2d..08c9559 100644
@@ -981,7 +981,7 @@ EXPORT_SYMBOL(try_to_del_timer_sync);
  *
  * Synchronization rules: Callers must prevent restarting of the timer,
  * otherwise this function is meaningless. It must not be called from
- * interrupt contexts. The caller must not hold locks which would prevent
+ * hardirq contexts. The caller must not hold locks which would prevent
  * completion of the timer's handler. The timer's handler must not call
  * add_timer_on(). Upon exit the timer is not queued and the handler is
  * not running on any CPU.
@@ -991,12 +991,10 @@ EXPORT_SYMBOL(try_to_del_timer_sync);
 int del_timer_sync(struct timer_list *timer)
 {
 #ifdef CONFIG_LOCKDEP
-	unsigned long flags;
-
-	local_irq_save(flags);
+	local_bh_disable();
 	lock_map_acquire(&timer->lockdep_map);
 	lock_map_release(&timer->lockdep_map);
-	local_irq_restore(flags);
+	local_bh_enable();
 #endif
 
 	for (;;) {
Signature · 1 line
1.7.0.4
author
#4 lore raw

RFC PATCH v2 3/3 timer: warn when del_timer_sync() used in hardirq context

In reply to Yong Zhang

From: Yong Zhang <[email protected]>

Add explict warning to prevent del_timer_sync() from using in hardirq context.

Cc Thomas Gleixner <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 kernel/timer.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
kernel/timer.c +5 -1
index 08c9559..1bc4b4a 100644
@@ -996,7 +996,11 @@ int del_timer_sync(struct timer_list *timer)
 	lock_map_release(&timer->lockdep_map);
 	local_bh_enable();
 #endif
-
+	/*
+	 * don't use it in hardirq context, because it
+	 * could lead to deadlock.
+	 */
+	WARN_ON(in_irq());
 	for (;;) {
 		int ret = try_to_del_timer_sync(timer);
 		if (ret >= 0)
Signature · 1 line
1.7.0.4
#5 lore raw

Re: [RFC V2 PATCH 3/3] timer: warn when del_timer_sync() used in hardirq context

Quoted 5 lines · Yong Zhang

From: Yong Zhang <[email protected]>

Add explict warning to prevent del_timer_sync() from using in hardirq context.

Agreed, del_timer_sync() from softirq should be OK.

Quoted 28 lines
Cc Thomas Gleixner <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 kernel/timer.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
kernel/timer.c +5 -1
index 08c9559..1bc4b4a 100644
@@ -996,7 +996,11 @@ int del_timer_sync(struct timer_list *timer)
 	lock_map_release(&timer->lockdep_map);
 	local_bh_enable();
 #endif
-
+	/*
+	 * don't use it in hardirq context, because it
+	 * could lead to deadlock.
+	 */
+	WARN_ON(in_irq());
 	for (;;) {
 		int ret = try_to_del_timer_sync(timer);
 		if (ret >= 0)
Signature · 1 line
1.7.0.4