Skip to contentSkip to conversation

hrtimer: cleanups and fixes

PATCH In review Peter Zijlstra 2 participants 8 messages Jan 5, 2009

lore
Share & tools
lore
#0 lore raw

cover hrtimer: cleanups and fixes

Please consider for .29.

#1 lore raw

PATCH 1/5 hrtimer: splitout peek ahead functionality

From: Thomas Gleixner <[email protected]>

Provide a peek ahead function that assumes irqs disabled, allows for micro optimizations.

---
 kernel/hrtimer.c |   24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
kernel/hrtimer.c +17 -7
Index: linux-2.6-tip/kernel/hrtimer.c
===================================================================
@@ -1244,6 +1244,22 @@ void hrtimer_interrupt(struct clock_even
 	}
 }
 
+/*
+ * local version of hrtimer_peek_ahead_timers() called with interrupts
+ * disabled.
+ */
+static void __hrtimer_peek_ahead_timers(void)
+{
+	struct tick_device *td;
+
+	if (!hrtimer_hres_active())
+		return;
+
+	td = &__get_cpu_var(tick_cpu_device);
+	if (td && td->evtdev)
+		hrtimer_interrupt(td->evtdev);
+}
+
 /**
  * hrtimer_peek_ahead_timers -- run soft-expired timers now
  *
@@ -1255,16 +1271,10 @@ void hrtimer_interrupt(struct clock_even
  */
 void hrtimer_peek_ahead_timers(void)
 {
-	struct tick_device *td;
 	unsigned long flags;
 
-	if (!hrtimer_hres_active())
-		return;
-
 	local_irq_save(flags);
-	td = &__get_cpu_var(tick_cpu_device);
-	if (td && td->evtdev)
-		hrtimer_interrupt(td->evtdev);
+	__hrtimer_peek_ahead_timers();
 	local_irq_restore(flags);
 }
 
#2 lore raw

PATCH 2/5 hrtimer: fix HOTPLUG_CPU=n compile warning

In reply to Peter Zijlstra

From: Thomas Gleixner <[email protected]>

kernel/hrtimer.c: In function 'hrtimer_cpu_notify':
kernel/hrtimer.c:1574: warning: unused variable 'dcpu'

Introduced by commit 37810659ea7d9572c5ac284ade272f806ef8f788 ("hrtimer: removing all ur callback modes, fix hotplug") from the timers. dcpu is only used if CONFIG_HOTPLUG_CPU is set.

---
 kernel/hrtimer.c |   19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
kernel/hrtimer.c +8 -11
Index: linux-2.6/kernel/hrtimer.c
===================================================================
@@ -1532,6 +1532,11 @@ static void __cpuinit init_hrtimers_cpu(
 
 #ifdef CONFIG_HOTPLUG_CPU
 
+static void tickle_timers(void *arg)
+{
+	hrtimer_peek_ahead_timers();
+}
+
 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
 				struct hrtimer_clock_base *new_base)
 {
@@ -1567,7 +1572,7 @@ static void migrate_hrtimer_list(struct 
 	}
 }
 
-static int migrate_hrtimers(int scpu)
+static void migrate_hrtimers(int scpu)
 {
 	struct hrtimer_cpu_base *old_base, *new_base;
 	int dcpu, i;
@@ -1595,12 +1600,7 @@ static int migrate_hrtimers(int scpu)
 	spin_unlock_irq(&new_base->lock);
 	put_cpu_var(hrtimer_bases);
 
-	return dcpu;
-}
-
-static void tickle_timers(void *arg)
-{
-	hrtimer_peek_ahead_timers();
+	smp_call_function_single(dcpu, tickle_timers, NULL, 0);
 }
 
 #endif /* CONFIG_HOTPLUG_CPU */
@@ -1625,11 +1625,8 @@ static int __cpuinit hrtimer_cpu_notify(
 	case CPU_DEAD:
 	case CPU_DEAD_FROZEN:
 	{
-		int dcpu;
-
 		clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
-		dcpu = migrate_hrtimers(scpu);
-		smp_call_function_single(dcpu, tickle_timers, NULL, 0);
+		migrate_hrtimers(scpu);
 		break;
 	}
 #endif
#3 lore raw

PATCH 3/5 hrtimer: simplify hotplug migration

In reply to Peter Zijlstra

From: Thomas Gleixner <[email protected]>

No need for a smp function call, which is likely to run on the same CPU anyway. We can just call hrtimers_peek_ahead() in the interrupts disabled section of migrate_hrtimers().

---
 kernel/hrtimer.c |   25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)
kernel/hrtimer.c +10 -15
Index: linux-2.6-tip/kernel/hrtimer.c
===================================================================
@@ -1505,11 +1505,6 @@ static void __cpuinit init_hrtimers_cpu(
 
 #ifdef CONFIG_HOTPLUG_CPU
 
-static void tickle_timers(void *arg)
-{
-	hrtimer_peek_ahead_timers();
-}
-
 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
 				struct hrtimer_clock_base *new_base)
 {
@@ -1548,20 +1543,19 @@ static void migrate_hrtimer_list(struct 
 static void migrate_hrtimers(int scpu)
 {
 	struct hrtimer_cpu_base *old_base, *new_base;
-	int dcpu, i;
+	int i;
 
 	BUG_ON(cpu_online(scpu));
-	old_base = &per_cpu(hrtimer_bases, scpu);
-	new_base = &get_cpu_var(hrtimer_bases);
-
-	dcpu = smp_processor_id();
-
 	tick_cancel_sched_timer(scpu);
+
+	local_irq_disable();
+	old_base = &per_cpu(hrtimer_bases, scpu);
+	new_base = &__get_cpu_var(hrtimer_bases);
 	/*
 	 * The caller is globally serialized and nobody else
 	 * takes two locks at once, deadlock is not possible.
 	 */
-	spin_lock_irq(&new_base->lock);
+	spin_lock(&new_base->lock);
 	spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
 
 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
@@ -1570,10 +1564,11 @@ static void migrate_hrtimers(int scpu)
 	}
 
 	spin_unlock(&old_base->lock);
-	spin_unlock_irq(&new_base->lock);
-	put_cpu_var(hrtimer_bases);
+	spin_unlock(&new_base->lock);
 
-	smp_call_function_single(dcpu, tickle_timers, NULL, 0);
+	/* Check, if we got expired work to do */
+	__hrtimer_peek_ahead_timers();
+	local_irq_enable();
 }
 
 #endif /* CONFIG_HOTPLUG_CPU */
#4 lore raw

PATCH 4/5 hrtimer: fix recursion deadlock by re-introducing the softirq

In reply to Peter Zijlstra

There are a few sites that do:

spin_lock_irq(&foo)
hrtimer_start(&bar)

    __run_hrtimer(&bar)
      func()
        spin_lock(&foo)

which obviously deadlocks. In order to avoid this, never call __run_hrtimer() from hrtimer_start*() context, but instead defer this to softirq context.

---
 include/linux/interrupt.h |    3 ++
 kernel/hrtimer.c          |   60 ++++++++++++++++++++--------------------------
 2 files changed, 30 insertions(+), 33 deletions(-)
include/linux/interrupt.h +3 -0
Index: linux-2.6/include/linux/interrupt.h
===================================================================
@@ -253,6 +253,9 @@ enum
 	BLOCK_SOFTIRQ,
 	TASKLET_SOFTIRQ,
 	SCHED_SOFTIRQ,
+#ifdef CONFIG_HIGH_RES_TIMERS
+	HRTIMER_SOFTIRQ,
+#endif
 	RCU_SOFTIRQ, 	/* Preferable RCU should always be the last softirq */
 
 	NR_SOFTIRQS
kernel/hrtimer.c +27 -33

138 lines not on this page · raw message

#5 lore raw

PATCH 5/5 hrtimer: fixup comments

In reply to Peter Zijlstra

From: Thomas Gleixner <[email protected]>

Clean up the comments

---
 kernel/hrtimer.c |   20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
kernel/hrtimer.c +9 -11
Index: linux-2.6/kernel/hrtimer.c
===================================================================
@@ -1141,9 +1141,9 @@ static void __run_hrtimer(struct hrtimer
 	spin_lock(&cpu_base->lock);
 
 	/*
-	 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
-	 * reprogramming of the event hardware. This happens at the end of this
-	 * function anyway.
+	 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
+	 * we do not reprogramm the event hardware. Happens either in
+	 * hrtimer_start_range_ns() or in hrtimer_interrupt()
 	 */
 	if (restart != HRTIMER_NORESTART) {
 		BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
@@ -1540,14 +1540,12 @@ static void migrate_hrtimer_list(struct 
 		__remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
 		timer->base = new_base;
 		/*
-		 * Enqueue the timers on the new cpu, but do not reprogram 
-		 * the timer as that would enable a deadlock between
-		 * hrtimer_enqueue_reprogramm() running the timer and us still
-		 * holding a nested base lock.
-		 *
-		 * Instead we tickle the hrtimer interrupt after the migration
-		 * is done, which will run all expired timers and re-programm
-		 * the timer device.
+		 * Enqueue the timers on the new cpu. This does not
+		 * reprogram the event device in case the timer
+		 * expires before the earliest on this CPU, but we run
+		 * hrtimer_interrupt after we migrated everything to
+		 * sort out already expired timers and reprogram the
+		 * event device.
 		 */
 		enqueue_hrtimer(timer, new_base);
 
#6 lore raw
In reply to Peter Zijlstra
Quoted 1 line · Peter Zijlstra

Please consider for .29.

picked them up into tip/timers/urgent:

e3f1d88: hrtimer: fixup comments
a6037b6: hrtimer: fix recursion deadlock by re-introducing the softirq 731a55b: hrtimer: simplify hotplug migration
d5fd43c: hrtimer: fix HOTPLUG_CPU=n compile warning 8bdec95: hrtimer: splitout peek ahead functionality

thanks Peter!

Ingo

#7 lore raw

PATCH 1/1 hrtimer: splitout peek ahead functionality, fix

Quoted 5 lines · Ingo Molnar
Quoted 1 line · Peter Zijlstra

Please consider for .29.

Quoted 7 lines

picked them up into tip/timers/urgent:

e3f1d88: hrtimer: fixup comments
a6037b6: hrtimer: fix recursion deadlock by re-introducing the softirq 731a55b: hrtimer: simplify hotplug migration
d5fd43c: hrtimer: fix HOTPLUG_CPU=n compile warning 8bdec95: hrtimer: splitout peek ahead functionality

i needed the small fixlet below, for !HIGH_RES_TIMERS.

Ingo

-------------->

Quoted 1 line

From 82c5b7b527ccc4b5d3cf832437e842f9d2920a79 Mon Sep 17 00:00:00 2001

From: Ingo Molnar <[email protected]>
Date: Mon, 5 Jan 2009 14:11:10 +0100
Subject: [PATCH] hrtimer: splitout peek ahead functionality, fix

Impact: build fix on !CONFIG_HIGH_RES_TIMERS

Fix:

kernel/hrtimer.c:1586: error: implicit declaration of function '__hrtimer_peek_ahead_timers'

---
 kernel/hrtimer.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
kernel/hrtimer.c +5 -1
index aa024f2..1455b76 100644
@@ -1268,7 +1268,11 @@ static void run_hrtimer_softirq(struct softirq_action *h)
 	hrtimer_peek_ahead_timers();
 }
 
-#endif	/* CONFIG_HIGH_RES_TIMERS */
+#else /* CONFIG_HIGH_RES_TIMERS */
+
+static inline void __hrtimer_peek_ahead_timers(void) { }
+
+#endif	/* !CONFIG_HIGH_RES_TIMERS */
 
 /*
  * Called from timer softirq every jiffy, expire hrtimers: