Skip to contentSkip to conversation

time: Fix extra sleeptime injection when suspend fails

PATCH Superseded Mukesh Ojha 3 participants 5 messages Jul 16, 2018 – Jul 17, 2018

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 time: Fix extra sleeptime injection when suspend fails

Currently, there exists a corner case assuming when there is only one clocksource e.g RTC, and system failed to go to suspend mode. While resume rtc_resume() injects the sleeptime as timekeeping_rtc_skipresume() returned 'false' (default value of sleeptime_injected) due to which we can see mismatch in timestamps.

This issue can also come in a system where more than one clocksource are present and very first suspend fails.

Fix this by handling the sleeptime_injected flag properly.

Success case:
------------

                                        {sleeptime_injected=false}

rtc_suspend() => timekeeping_suspend() => timekeeping_resume() =>

(sleeptime injected)
rtc_resume()

Failure case:
------------

         {failure in sleep path} {sleeptime_injected=false}

rtc_suspend() => rtc_resume()

sleeptime injected again which was not required as the suspend failed)

---
Changes in V4:
* Changes as suggested by John
- Changed the variable name from sleeptime_injected to suspend_timing_needed
- Changed the boolean logic.
Changes in v3:
* Updated commit subject and description.
* Updated the patch as per the fix given by Thomas Gleixner.

Changes in v2:
* Updated the commit text.
* Removed extra variable and used the earlier static variable 'sleeptime_injected'.

 drivers/rtc/class.c       |  2 +-
 kernel/time/timekeeping.c | 29 ++++++++++++++++++++++-------
 2 files changed, 23 insertions(+), 8 deletions(-)
drivers/rtc/class.c +1 -1
index d37588f..ee455cc 100644
@@ -102,7 +102,7 @@ static int rtc_resume(struct device *dev)
 	struct timespec64	sleep_time;
 	int err;
 
-	if (timekeeping_rtc_skipresume())
+	if (!timekeeping_rtc_skipresume())
 		return 0;
 
 	rtc_hctosys_ret = -ENODEV;
kernel/time/timekeeping.c +22 -7
index 4786df9..b25f771 100644
@@ -1510,8 +1510,20 @@ void __weak read_boot_clock64(struct timespec64 *ts)
 	ts->tv_nsec = 0;
 }
 
-/* Flag for if timekeeping_resume() has injected sleeptime */
-static bool sleeptime_injected;
+/*
+ * Flag reflecting whether timekeeping_resume() has injected sleeptime.
+ *
+ * The flag starts of false and is only set when a suspend reaches
+ * timekeeping_suspend(), timekeeping_resume() sets it to false when the
+ * timekeeper clocksource is not stopping across suspend and has been
+ * used to update sleep time. If the timekeeper clocksource has stopped
+ * then the flag stays true and is used by the RTC resume code to decide
+ * whether sleeptime must be injected and if so the flag gets false then.
+ *
+ * If a suspend fails before reaching timekeeping_resume() then the flag
+ * stays false and prevents erroneous sleeptime injection.
+ */
+static bool suspend_timing_needed;
 
 /* Flag for if there is a persistent clock on this platform */
 static bool persistent_clock_exists;
@@ -1610,7 +1622,7 @@ static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
  */
 bool timekeeping_rtc_skipresume(void)
 {
-	return sleeptime_injected;
+	return suspend_timing_needed;
 }
 
 /**
@@ -1646,6 +1658,8 @@ void timekeeping_inject_sleeptime64(struct timespec64 *delta)
 	raw_spin_lock_irqsave(&timekeeper_lock, flags);
 	write_seqcount_begin(&tk_core.seq);
 
+	suspend_timing_needed = false;
+
 	timekeeping_forward_now(tk);
 
 	__timekeeping_inject_sleeptime(tk, delta);
@@ -1671,7 +1685,6 @@ void timekeeping_resume(void)
 	struct timespec64 ts_new, ts_delta;
 	u64 cycle_now;
 
-	sleeptime_injected = false;
 	read_persistent_clock64(&ts_new);
 
 	clockevents_resume();
@@ -1701,13 +1714,13 @@ void timekeeping_resume(void)
 					      tk->tkr_mono.mask);
 		nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift);
 		ts_delta = ns_to_timespec64(nsec);
-		sleeptime_injected = true;
+		suspend_timing_needed = false;
 	} else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
 		ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
-		sleeptime_injected = true;
+		suspend_timing_needed = false;
 	}
 
-	if (sleeptime_injected)
+	if (!suspend_timing_needed)
 		__timekeeping_inject_sleeptime(tk, &ts_delta);
 
 	/* Re-base the last cycle value */
@@ -1743,6 +1756,8 @@ int timekeeping_suspend(void)
 	if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
 		persistent_clock_exists = true;
 
+	suspend_timing_needed = true;
+
 	raw_spin_lock_irqsave(&timekeeper_lock, flags);
 	write_seqcount_begin(&tk_core.seq);
 	timekeeping_forward_now(tk);
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
#1 lore raw

drivers/rtc/class.c static int rtc_resume(struct device *dev) · line 106

Quoted 7 lines · Mukesh Ojha
drivers/rtc/class.c +1 -1
@@ -102,7 +102,7 @@ static int rtc_resume(struct device *dev)
 	struct timespec64	sleep_time;
 	int err;
 
-	if (timekeeping_rtc_skipresume())
+	if (!timekeeping_rtc_skipresume())
 		return 0;

That does not make any sense at all, really.

kernel/time/timekeeping.c static void __timekeeping_inject_sleeptime(struct timekeeper *tk, · line 1625

Quoted 8 lines
kernel/time/timekeeping.c +1 -1
 /* Flag for if there is a persistent clock on this platform */
 static bool persistent_clock_exists;
@@ -1610,7 +1622,7 @@ static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
  */
 bool timekeeping_rtc_skipresume(void)
 {
-	return sleeptime_injected;
+	return suspend_timing_needed;

Just make this !suspend_timing_needed and the function name and its return value still makes sense.

Quoted 5 lines
kernel/time/timekeeping.c +0 -1
@@ -1701,13 +1714,13 @@ void timekeeping_resume(void)
 					      tk->tkr_mono.mask);
 		nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift);
 		ts_delta = ns_to_timespec64(nsec);
-		sleeptime_injected = true;

kernel/time/timekeeping.c void timekeeping_resume(void) · line 1724

Quoted 10 lines
kernel/time/timekeeping.c +3 -2
+		suspend_timing_needed = false;
 	} else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
 		ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
-		sleeptime_injected = true;
+		suspend_timing_needed = false;
 	}
 
-	if (sleeptime_injected)
+	if (!suspend_timing_needed)
 		__timekeeping_inject_sleeptime(tk, &ts_delta);

This reads odd as well. I'd rather keep a local variable inject_sleeptime or such and set that in the code pathes above.

   	if (...) {
	   	...
		inject_sleeptime = true;
   	} else if (...) {
	   	...
		inject_sleeptime = true;
	}

	if (inject_sleeptime) {
		suspend_timing_needed = false;
		__timekeeping_inject_sleeptime();
	}

Hmm? Just blindly converting everything results in functional, but nonsensical code. Think about what happens when you look at that stuff 6 month from now...

Thanks,

tglx

#2 lore raw
Quoted 39 lines · Thomas Gleixner

drivers/rtc/class.c static int rtc_resume(struct device *dev) · line 106

Quoted 7 lines · Mukesh Ojha
drivers/rtc/class.c +1 -1
@@ -102,7 +102,7 @@ static int rtc_resume(struct device *dev)
  	struct timespec64	sleep_time;
  	int err;
 
-	if (timekeeping_rtc_skipresume())
+	if (!timekeeping_rtc_skipresume())
  		return 0;

That does not make any sense at all, really.

kernel/time/timekeeping.c static void __timekeeping_inject_sleeptime(struct timekeeper *tk, · line 1625

Quoted 8 lines
kernel/time/timekeeping.c +1 -1
  /* Flag for if there is a persistent clock on this platform */
  static bool persistent_clock_exists;
@@ -1610,7 +1622,7 @@ static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
   */
  bool timekeeping_rtc_skipresume(void)
  {
-	return sleeptime_injected;
+	return suspend_timing_needed;

Just make this !suspend_timing_needed and the function name and its return value still makes sense.

Quoted 5 lines
kernel/time/timekeeping.c +0 -1
@@ -1701,13 +1714,13 @@ void timekeeping_resume(void)
  					      tk->tkr_mono.mask);
  		nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift);
  		ts_delta = ns_to_timespec64(nsec);
-		sleeptime_injected = true;

kernel/time/timekeeping.c void timekeeping_resume(void) · line 1724

Quoted 10 lines
kernel/time/timekeeping.c +3 -2
+		suspend_timing_needed = false;
  	} else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
  		ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
-		sleeptime_injected = true;
+		suspend_timing_needed = false;
  	}
 
-	if (sleeptime_injected)
+	if (!suspend_timing_needed)
  		__timekeeping_inject_sleeptime(tk, &ts_delta);

This reads odd as well. I'd rather keep a local variable inject_sleeptime or such and set that in the code pathes above.

Quoted 12 lines
    	if (...) {
	   	...
		inject_sleeptime = true;
    	} else if (...) {
	   	...
		inject_sleeptime = true;
	}

	if (inject_sleeptime) {
		suspend_timing_needed = false;
		__timekeeping_inject_sleeptime();
	}

Will do suggested change and send in v5.

Thanks.

Quoted 9 lines

Hmm? Just blindly converting everything results in functional, but nonsensical code. Think about what happens when you look at that stuff 6 month from now...

Thanks,

tglx

#3 lore raw
In reply to Mukesh Ojha
Quoted 27 lines · Mukesh Ojha

Currently, there exists a corner case assuming when there is only one clocksource e.g RTC, and system failed to go to suspend mode. While resume rtc_resume() injects the sleeptime as timekeeping_rtc_skipresume() returned 'false' (default value of sleeptime_injected) due to which we can see mismatch in timestamps.

This issue can also come in a system where more than one clocksource are present and very first suspend fails.

Fix this by handling the sleeptime_injected flag properly.

Success case:
------------

                                        {sleeptime_injected=false}

rtc_suspend() => timekeeping_suspend() => timekeeping_resume() =>

(sleeptime injected)
rtc_resume()

Failure case:
------------

         {failure in sleep path} {sleeptime_injected=false}

rtc_suspend() => rtc_resume()

sleeptime injected again which was not required as the suspend failed)

Quoted 7 lines

---
Changes in V4:
* Changes as suggested by John
- Changed the variable name from sleeptime_injected to suspend_timing_needed
- Changed the boolean logic.

Thanks so much for reworking and resending this again!

drivers/rtc/class.c static int rtc_resume(struct device *dev) · line 105

Quoted 10 lines
drivers/rtc/class.c +1 -1
index d37588f..ee455cc 100644
@@ -102,7 +102,7 @@ static int rtc_resume(struct device *dev)
        struct timespec64       sleep_time;
        int err;
 
-       if (timekeeping_rtc_skipresume())
+       if (!timekeeping_rtc_skipresume())

Hrm... So I'd have instead inverted the logic *in* timekeeping_rtc_skipresume(), rather then here, but this looks to be close enough and I can fix that bit up.

Can you confirm you've validated this version of the patch resolves the issue you reported?

thanks
-john

#4 lore raw
Quoted 46 lines · John Stultz
Quoted 27 lines · Mukesh Ojha

Currently, there exists a corner case assuming when there is only one clocksource e.g RTC, and system failed to go to suspend mode. While resume rtc_resume() injects the sleeptime as timekeeping_rtc_skipresume() returned 'false' (default value of sleeptime_injected) due to which we can see mismatch in timestamps.

This issue can also come in a system where more than one clocksource are present and very first suspend fails.

Fix this by handling the sleeptime_injected flag properly.

Success case:
------------

                                         {sleeptime_injected=false}

rtc_suspend() => timekeeping_suspend() => timekeeping_resume() =>

(sleeptime injected)
rtc_resume()

Failure case:
------------

          {failure in sleep path} {sleeptime_injected=false}

rtc_suspend() => rtc_resume()

sleeptime injected again which was not required as the suspend failed)

Quoted 7 lines

---
Changes in V4:
* Changes as suggested by John
- Changed the variable name from sleeptime_injected to suspend_timing_needed
- Changed the boolean logic.

Thanks so much for reworking and resending this again!

Quoted 8 lines
drivers/rtc/class.c +0 -0
index d37588f..ee455cc 100644
@@ -102,7 +102,7 @@ static int rtc_resume(struct device *dev)
         struct timespec64       sleep_time;
         int err;
 
Quoted 6 lines

drivers/rtc/class.c static int rtc_resume(struct device *dev) · line 105

Quoted 2 lines
drivers/rtc/class.c +1 -1
-       if (timekeeping_rtc_skipresume())
+       if (!timekeeping_rtc_skipresume())

Hrm... So I'd have instead inverted the logic *in* timekeeping_rtc_skipresume(), rather then here, but this looks to be close enough and I can fix that bit up.

Will take care of yours and Thomas comment in v5.

Quoted 3 lines

Can you confirm you've validated this version of the patch resolves the issue you reported?

Yeah, I validated.

Thanks
Mukesh

Quoted 3 lines

thanks
-john