Skip to contentSkip to conversation

sched_clock: Avoid corrupting hrtimer tree during suspend

PATCH New John Stultz 2 participants 2 messages Jul 24, 2014 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_clock: Avoid corrupting hrtimer tree during suspend

From: Stephen Boyd <[email protected]>

During suspend we call sched_clock_poll() to update the epoch and accumulated time and reprogram the sched_clock_timer to fire before the next wrap-around time. Unfortunately, sched_clock_poll() doesn't restart the timer, instead it relies on the hrtimer layer to do that and during suspend we aren't calling that function from the hrtimer layer. Instead, we're reprogramming the expires time while the hrtimer is enqueued, which can cause the hrtimer tree to be corrupted. Furthermore, we restart the timer during suspend but we update the epoch during resume which seems counter-intuitive.

Let's fix this by saving the accumulated state and canceling the timer during suspend. On resume we can update the epoch and restart the timer similar to what we would do if we were starting the clock for the first time.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc stable <[email protected]>
Fixes a08ca5d1089d "sched_clock: Use an hrtimer instead of timer"

---

Ingo/Thomas: Here's a fix for tip/timers/urgent for 3.16 and -stable. Let me know if you have any objections.

thanks
-john

 kernel/time/sched_clock.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/time/sched_clock.c +3 -1
index 445106d..01d2d15 100644
@@ -191,7 +191,8 @@ void __init sched_clock_postinit(void)
 
 static int sched_clock_suspend(void)
 {
-	sched_clock_poll(&sched_clock_timer);
+	update_sched_clock();
+	hrtimer_cancel(&sched_clock_timer);
 	cd.suspended = true;
 	return 0;
 }
@@ -199,6 +200,7 @@ static int sched_clock_suspend(void)
 static void sched_clock_resume(void)
 {
 	cd.epoch_cyc = read_sched_clock();
+	hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
 	cd.suspended = false;
 }
 
Signature · 1 line
1.9.1
#1 lore raw
sched_clock: Avoid corrupting hrtimer tree during suspend
Commit-ID:  f723aa1817dd8f4fe005aab52ba70c8ab0ef9457
Gitweb:     http://git.kernel.org/tip/f723aa1817dd8f4fe005aab52ba70c8ab0ef9457
Author:     Stephen Boyd <[email protected]>
AuthorDate: Wed, 23 Jul 2014 21:03:50 -0700
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 24 Jul 2014 12:02:49 +0200

sched_clock: Avoid corrupting hrtimer tree during suspend

During suspend we call sched_clock_poll() to update the epoch and accumulated time and reprogram the sched_clock_timer to fire before the next wrap-around time. Unfortunately, sched_clock_poll() doesn't restart the timer, instead it relies on the hrtimer layer to do that and during suspend we aren't calling that function from the hrtimer layer. Instead, we're reprogramming the expires time while the hrtimer is enqueued, which can cause the hrtimer tree to be corrupted. Furthermore, we restart the timer during suspend but we update the epoch during resume which seems counter-intuitive.

Let's fix this by saving the accumulated state and canceling the timer during suspend. On resume we can update the epoch and restart the timer similar to what we would do if we were starting the clock for the first time.

Fixes a08ca5d1089d "sched_clock: Use an hrtimer instead of timer"
Cc Ingo Molnar <[email protected]>
Cc stable <[email protected]>
---
 kernel/time/sched_clock.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/time/sched_clock.c +3 -1
index 445106d..01d2d15 100644
@@ -191,7 +191,8 @@ void __init sched_clock_postinit(void)
 
 static int sched_clock_suspend(void)
 {
-	sched_clock_poll(&sched_clock_timer);
+	update_sched_clock();
+	hrtimer_cancel(&sched_clock_timer);
 	cd.suspended = true;
 	return 0;
 }
@@ -199,6 +200,7 @@ static int sched_clock_suspend(void)
 static void sched_clock_resume(void)
 {
 	cd.epoch_cyc = read_sched_clock();
+	hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
 	cd.suspended = false;
 }