Skip to contentSkip to conversation

Fix alarmtimer suspend failure

PATCH Applied Stephen Boyd 2 participants 9 messages Jan 24, 2020 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

v3 cover Fix alarmtimer suspend failure

We recently ran into a suspend problem where the alarmtimer platform driver's suspend function fails because the RTC that it's trying to program is already suspended. This patch series fixes that problem by making the platform device a child of the RTC.

The last two patches are non-critical changes to how we do the wakeup and some code cleanup.

Changes from v2:
* Picked up review tags from Doug
* Removed extra space between - and 1 on first patch
* Split last patch into two

Changes from v1:
* Dropped first patch that got picked up
* Reworked second patch to autogenerate the device id and use IS_ERR()

Stephen Boyd (4):
alarmtimer: Make alarmtimer platform device child of RTC device
alarmtimer: Use wakeup source from alarmtimer platform device
alarmtimer: Make alarmtimer_get_rtcdev() a stub when
CONFIG_RTC_CLASS=n
alarmtimer: Update alarmtimer_get_rtcdev() docs to reflect reality

 include/linux/alarmtimer.h |  4 ++++
 kernel/time/alarmtimer.c   | 40 +++++++++++++-------------------------
 2 files changed, 17 insertions(+), 27 deletions(-)

base-commit: bc80e6ad8ee12b0ee6c7d05faf1ebd3f2fb8f1e5

Signature · 1 line
Sent by a computer, using git, on the internet
#1 lore raw

PATCH v3 1/4 alarmtimer: Make alarmtimer platform device child of RTC device

The alarmtimer_suspend() function will fail if an RTC device is on a bus such as SPI or i2c and that RTC device registers and probes after alarmtimer_init() registers and probes the 'alarmtimer' platform device. This is because system wide suspend suspends devices in the reverse order of their probe. When alarmtimer_suspend() attempts to program the RTC for a wakeup it will try to program an RTC device on a bus that has already been suspended.

Let's move the alarmtimer device registration to be when the RTC we use for wakeup is registered. Register the 'alarmtimer' platform device as a child of the RTC device too, so that we can be guaranteed that the RTC device won't be suspended when alarmtimer_suspend() is called.

---
 kernel/time/alarmtimer.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
kernel/time/alarmtimer.c +9 -11
index 9dc7a0913190..205365906137 100644
@@ -91,6 +91,7 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 	unsigned long flags;
 	struct rtc_device *rtc = to_rtc_device(dev);
 	struct wakeup_source *__ws;
+	struct platform_device *pdev;
 	int ret = 0;
 
 	if (rtcdev)
@@ -102,9 +103,11 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 		return -1;
 
 	__ws = wakeup_source_register(dev, "alarmtimer");
+	pdev = platform_device_register_data(dev, "alarmtimer",
+					     PLATFORM_DEVID_AUTO, NULL, 0);
 
 	spin_lock_irqsave(&rtcdev_lock, flags);
-	if (!rtcdev) {
+	if (__ws && !IS_ERR(pdev) && !rtcdev) {
 		if (!try_module_get(rtc->owner)) {
 			ret = -1;
 			goto unlock;
@@ -115,10 +118,14 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 		get_device(dev);
 		ws = __ws;
 		__ws = NULL;
+		pdev = NULL;
+	} else {
+		ret = -1;
 	}
 unlock:
 	spin_unlock_irqrestore(&rtcdev_lock, flags);
 
+	platform_device_unregister(pdev);
 	wakeup_source_unregister(__ws);
 
 	return ret;
@@ -905,8 +912,7 @@ static void get_boottime_timespec(struct timespec64 *tp)
  */
 static int __init alarmtimer_init(void)
 {
-	struct platform_device *pdev;
-	int error = 0;
+	int error;
 	int i;
 
 	alarmtimer_rtc_timer_init();
@@ -931,15 +937,7 @@ static int __init alarmtimer_init(void)
 	if (error)
 		goto out_if;
 
-	pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
-	if (IS_ERR(pdev)) {
-		error = PTR_ERR(pdev);
-		goto out_drv;
-	}
 	return 0;
-
-out_drv:
-	platform_driver_unregister(&alarmtimer_driver);
 out_if:
 	alarmtimer_rtc_interface_remove();
 	return error;
Signature · 1 line
Sent by a computer, using git, on the internet
merge bot Applied
#2 lore raw

[tip: timers/core] alarmtimer: Make alarmtimer platform device child of RTC device

The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     c79108bd19a8490315847e0c95ac6526fcd8e770
Gitweb:        https://git.kernel.org/tip/c79108bd19a8490315847e0c95ac6526fcd8e770
Author:        Stephen Boyd <[email protected]>
AuthorDate:    Thu, 23 Jan 2020 21:58:46 -08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 24 Jan 2020 21:00:20 +01:00

alarmtimer: Make alarmtimer platform device child of RTC device

The alarmtimer_suspend() function will fail if an RTC device is on a bus such as SPI or i2c and that RTC device registers and probes after alarmtimer_init() registers and probes the 'alarmtimer' platform device.

This is because system wide suspend suspends devices in the reverse order of their probe. When alarmtimer_suspend() attempts to program the RTC for a wakeup it will try to program an RTC device on a bus that has already been suspended.

Move the alarmtimer device registration to happen when the RTC which is used for wakeup is registered. Register the 'alarmtimer' platform device as a child of the RTC device too, so that it can be guaranteed that the RTC device won't be suspended when alarmtimer_suspend() is called.

---
 kernel/time/alarmtimer.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
kernel/time/alarmtimer.c +9 -11
index 564ff5d..f0469cc 100644
@@ -89,6 +89,7 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 	unsigned long flags;
 	struct rtc_device *rtc = to_rtc_device(dev);
 	struct wakeup_source *__ws;
+	struct platform_device *pdev;
 	int ret = 0;
 
 	if (rtcdev)
@@ -100,9 +101,11 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 		return -1;
 
 	__ws = wakeup_source_register(dev, "alarmtimer");
+	pdev = platform_device_register_data(dev, "alarmtimer",
+					     PLATFORM_DEVID_AUTO, NULL, 0);
 
 	spin_lock_irqsave(&rtcdev_lock, flags);
-	if (!rtcdev) {
+	if (__ws && !IS_ERR(pdev) && !rtcdev) {
 		if (!try_module_get(rtc->owner)) {
 			ret = -1;
 			goto unlock;
@@ -113,10 +116,14 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 		get_device(dev);
 		ws = __ws;
 		__ws = NULL;
+		pdev = NULL;
+	} else {
+		ret = -1;
 	}
 unlock:
 	spin_unlock_irqrestore(&rtcdev_lock, flags);
 
+	platform_device_unregister(pdev);
 	wakeup_source_unregister(__ws);
 
 	return ret;
@@ -903,8 +910,7 @@ static void get_boottime_timespec(struct timespec64 *tp)
  */
 static int __init alarmtimer_init(void)
 {
-	struct platform_device *pdev;
-	int error = 0;
+	int error;
 	int i;
 
 	alarmtimer_rtc_timer_init();
@@ -929,15 +935,7 @@ static int __init alarmtimer_init(void)
 	if (error)
 		goto out_if;
 
-	pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
-	if (IS_ERR(pdev)) {
-		error = PTR_ERR(pdev);
-		goto out_drv;
-	}
 	return 0;
-
-out_drv:
-	platform_driver_unregister(&alarmtimer_driver);
 out_if:
 	alarmtimer_rtc_interface_remove();
 	return error;
#3 lore raw

PATCH v3 2/4 alarmtimer: Use wakeup source from alarmtimer platform device

In reply to Stephen Boyd

Use the wakeup source that can be associated with the 'alarmtimer' platform device instead of registering another one by hand.

---
 kernel/time/alarmtimer.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
kernel/time/alarmtimer.c +5 -10
index 205365906137..395d4a6db1b2 100644
@@ -58,8 +58,6 @@ static DEFINE_SPINLOCK(freezer_delta_lock);
 #endif
 
 #ifdef CONFIG_RTC_CLASS
-static struct wakeup_source *ws;
-
 /* rtc timer and device for setting alarm wakeups at suspend */
 static struct rtc_timer		rtctimer;
 static struct rtc_device	*rtcdev;
@@ -90,7 +88,6 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 {
 	unsigned long flags;
 	struct rtc_device *rtc = to_rtc_device(dev);
-	struct wakeup_source *__ws;
 	struct platform_device *pdev;
 	int ret = 0;
 
@@ -102,12 +99,13 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 	if (!device_may_wakeup(rtc->dev.parent))
 		return -1;
 
-	__ws = wakeup_source_register(dev, "alarmtimer");
 	pdev = platform_device_register_data(dev, "alarmtimer",
 					     PLATFORM_DEVID_AUTO, NULL, 0);
+	if (!IS_ERR(pdev))
+		device_init_wakeup(&pdev->dev, true);
 
 	spin_lock_irqsave(&rtcdev_lock, flags);
-	if (__ws && !IS_ERR(pdev) && !rtcdev) {
+	if (!IS_ERR(pdev) && !rtcdev) {
 		if (!try_module_get(rtc->owner)) {
 			ret = -1;
 			goto unlock;
@@ -116,8 +114,6 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 		rtcdev = rtc;
 		/* hold a reference so it doesn't go away */
 		get_device(dev);
-		ws = __ws;
-		__ws = NULL;
 		pdev = NULL;
 	} else {
 		ret = -1;
@@ -126,7 +122,6 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 	spin_unlock_irqrestore(&rtcdev_lock, flags);
 
 	platform_device_unregister(pdev);
-	wakeup_source_unregister(__ws);
 
 	return ret;
 }
@@ -293,7 +288,7 @@ static int alarmtimer_suspend(struct device *dev)
 		return 0;
 
 	if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
-		__pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
+		pm_wakeup_event(dev, 2 * MSEC_PER_SEC);
 		return -EBUSY;
 	}
 
@@ -308,7 +303,7 @@ static int alarmtimer_suspend(struct device *dev)
 	/* Set alarm, if in the past reject suspend briefly to handle */
 	ret = rtc_timer_start(rtc, &rtctimer, now, 0);
 	if (ret < 0)
-		__pm_wakeup_event(ws, MSEC_PER_SEC);
+		pm_wakeup_event(dev, MSEC_PER_SEC);
 	return ret;
 }
 
Signature · 1 line
Sent by a computer, using git, on the internet
merge bot Applied
#4 lore raw

[tip: timers/core] alarmtimer: Use wakeup source from alarmtimer platform device

The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     7c94caca877b0feeca6f5f7b07d48c508e20d58f
Gitweb:        https://git.kernel.org/tip/7c94caca877b0feeca6f5f7b07d48c508e20d58f
Author:        Stephen Boyd <[email protected]>
AuthorDate:    Thu, 23 Jan 2020 21:58:47 -08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 24 Jan 2020 21:00:21 +01:00

alarmtimer: Use wakeup source from alarmtimer platform device

Use the wakeup source that can be associated with the 'alarmtimer' platform device instead of registering another one by hand.

---
 kernel/time/alarmtimer.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
kernel/time/alarmtimer.c +5 -10
index f0469cc..685ff57 100644
@@ -58,8 +58,6 @@ static DEFINE_SPINLOCK(freezer_delta_lock);
 #endif
 
 #ifdef CONFIG_RTC_CLASS
-static struct wakeup_source *ws;
-
 /* rtc timer and device for setting alarm wakeups at suspend */
 static struct rtc_timer		rtctimer;
 static struct rtc_device	*rtcdev;
@@ -88,7 +86,6 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 {
 	unsigned long flags;
 	struct rtc_device *rtc = to_rtc_device(dev);
-	struct wakeup_source *__ws;
 	struct platform_device *pdev;
 	int ret = 0;
 
@@ -100,12 +97,13 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 	if (!device_may_wakeup(rtc->dev.parent))
 		return -1;
 
-	__ws = wakeup_source_register(dev, "alarmtimer");
 	pdev = platform_device_register_data(dev, "alarmtimer",
 					     PLATFORM_DEVID_AUTO, NULL, 0);
+	if (!IS_ERR(pdev))
+		device_init_wakeup(&pdev->dev, true);
 
 	spin_lock_irqsave(&rtcdev_lock, flags);
-	if (__ws && !IS_ERR(pdev) && !rtcdev) {
+	if (!IS_ERR(pdev) && !rtcdev) {
 		if (!try_module_get(rtc->owner)) {
 			ret = -1;
 			goto unlock;
@@ -114,8 +112,6 @@ static int alarmtimer_rtc_add_device(struct device *dev,
 		rtcdev = rtc;
 		/* hold a reference so it doesn't go away */
 		get_device(dev);
-		ws = __ws;
-		__ws = NULL;
 		pdev = NULL;
 	} else {
 		ret = -1;
@@ -124,7 +120,6 @@ unlock:
 	spin_unlock_irqrestore(&rtcdev_lock, flags);
 
 	platform_device_unregister(pdev);
-	wakeup_source_unregister(__ws);
 
 	return ret;
 }
@@ -291,7 +286,7 @@ static int alarmtimer_suspend(struct device *dev)
 		return 0;
 
 	if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
-		__pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
+		pm_wakeup_event(dev, 2 * MSEC_PER_SEC);
 		return -EBUSY;
 	}
 
@@ -306,7 +301,7 @@ static int alarmtimer_suspend(struct device *dev)
 	/* Set alarm, if in the past reject suspend briefly to handle */
 	ret = rtc_timer_start(rtc, &rtctimer, now, 0);
 	if (ret < 0)
-		__pm_wakeup_event(ws, MSEC_PER_SEC);
+		pm_wakeup_event(dev, MSEC_PER_SEC);
 	return ret;
 }
 
#5 lore raw

PATCH v3 3/4 alarmtimer: Make alarmtimer_get_rtcdev() a stub when CONFIG_RTC_CLASS=n

In reply to Stephen Boyd

The export isn't there for the stubbed version of alarmtimer_get_rtcdev() so this won't work if someone tries to call this function when CONFIG_RTC_CLASS=n. Export a stub function in the header file so that callers don't have to worry about linking against this symbol. And rtcdev isn't used outside of this ifdef so we don't need to redefine it as NULL. Drop that because we're nearby.

---
 include/linux/alarmtimer.h | 4 ++++
 kernel/time/alarmtimer.c   | 5 -----
 2 files changed, 4 insertions(+), 5 deletions(-)
include/linux/alarmtimer.h +4 -0
index 74748e306f4b..05e758b8b894 100644
@@ -60,7 +60,11 @@ u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval);
 u64 alarm_forward_now(struct alarm *alarm, ktime_t interval);
 ktime_t alarm_expires_remaining(const struct alarm *alarm);
 
+#ifdef CONFIG_RTC_CLASS
 /* Provide way to access the rtc device being used by alarmtimers */
 struct rtc_device *alarmtimer_get_rtcdev(void);
+#else
+static inline struct rtc_device *alarmtimer_get_rtcdev(void) { return NULL; }
+#endif
 
 #endif
kernel/time/alarmtimer.c +0 -5
index 395d4a6db1b2..36124aea8d6f 100644
@@ -145,11 +145,6 @@ static void alarmtimer_rtc_interface_remove(void)
 	class_interface_unregister(&alarmtimer_rtc_interface);
 }
 #else
-struct rtc_device *alarmtimer_get_rtcdev(void)
-{
-	return NULL;
-}
-#define rtcdev (NULL)
 static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
 static inline void alarmtimer_rtc_interface_remove(void) { }
 static inline void alarmtimer_rtc_timer_init(void) { }
Signature · 1 line
Sent by a computer, using git, on the internet
merge bot Applied
#6 lore raw

[tip: timers/core] alarmtimer: Make alarmtimer_get_rtcdev() a stub when CONFIG_RTC_CLASS=n

The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     fd928f3e32ba09381b287f8b732418434d932855
Gitweb:        https://git.kernel.org/tip/fd928f3e32ba09381b287f8b732418434d932855
Author:        Stephen Boyd <[email protected]>
AuthorDate:    Thu, 23 Jan 2020 21:58:48 -08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 24 Jan 2020 21:03:53 +01:00

alarmtimer: Make alarmtimer_get_rtcdev() a stub when CONFIG_RTC_CLASS=n

The stubbed version of alarmtimer_get_rtcdev() is not exported. so this won't work if this function is used in a module when CONFIG_RTC_CLASS=n.

Move the stub function to the header file and make it inline so that callers don't have to worry about linking against this symbol.

rtcdev isn't used outside of this ifdef so it's not required to be redefined to NULL. Drop that while touching this area.

---
 include/linux/alarmtimer.h | 4 ++++
 kernel/time/alarmtimer.c   | 5 -----
 2 files changed, 4 insertions(+), 5 deletions(-)
include/linux/alarmtimer.h +4 -0
index 74748e3..05e758b 100644
@@ -60,7 +60,11 @@ u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval);
 u64 alarm_forward_now(struct alarm *alarm, ktime_t interval);
 ktime_t alarm_expires_remaining(const struct alarm *alarm);
 
+#ifdef CONFIG_RTC_CLASS
 /* Provide way to access the rtc device being used by alarmtimers */
 struct rtc_device *alarmtimer_get_rtcdev(void);
+#else
+static inline struct rtc_device *alarmtimer_get_rtcdev(void) { return NULL; }
+#endif
 
 #endif
kernel/time/alarmtimer.c +0 -5
index 685ff57..2ffb466 100644
@@ -143,11 +143,6 @@ static void alarmtimer_rtc_interface_remove(void)
 	class_interface_unregister(&alarmtimer_rtc_interface);
 }
 #else
-struct rtc_device *alarmtimer_get_rtcdev(void)
-{
-	return NULL;
-}
-#define rtcdev (NULL)
 static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
 static inline void alarmtimer_rtc_interface_remove(void) { }
 static inline void alarmtimer_rtc_timer_init(void) { }
#7 lore raw

PATCH v3 4/4 alarmtimer: Update alarmtimer_get_rtcdev() docs to reflect reality

In reply to Stephen Boyd

This function doesn't do anything like this comment says when an RTC device hasn't been chosen. It looks like we used to do something like that before commit 8bc0dafb5cf3 ("alarmtimers: Rework RTC device selection using class interface") but that's long gone now. Remove this sentence to avoid confusing the reader.

---
 kernel/time/alarmtimer.c | 2 --
 1 file changed, 2 deletions(-)
kernel/time/alarmtimer.c +0 -2
index 36124aea8d6f..2ffb466af77e 100644
@@ -67,8 +67,6 @@ static DEFINE_SPINLOCK(rtcdev_lock);
  * alarmtimer_get_rtcdev - Return selected rtcdevice
  *
  * This function returns the rtc device to use for wakealarms.
- * If one has not already been chosen, it checks to see if a
- * functional rtc device is available.
  */
 struct rtc_device *alarmtimer_get_rtcdev(void)
 {
Signature · 1 line
Sent by a computer, using git, on the internet
merge bot Applied
#8 lore raw

[tip: timers/core] alarmtimer: Update alarmtimer_get_rtcdev() docs to reflect reality

The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     6b088cefbeaa87ba48bf838edfc1e19c9ff3976b
Gitweb:        https://git.kernel.org/tip/6b088cefbeaa87ba48bf838edfc1e19c9ff3976b
Author:        Stephen Boyd <[email protected]>
AuthorDate:    Thu, 23 Jan 2020 21:58:49 -08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 24 Jan 2020 21:00:20 +01:00

alarmtimer: Update alarmtimer_get_rtcdev() docs to reflect reality

This function doesn't do anything like this comment says when an RTC device hasn't been chosen. It looks like we used to do something like that before commit 8bc0dafb5cf3 ("alarmtimers: Rework RTC device selection using class interface") but that's long gone now. Remove this sentence to avoid confusing the reader.

---
 kernel/time/alarmtimer.c | 2 --
 1 file changed, 2 deletions(-)
kernel/time/alarmtimer.c +0 -2
index 9dc7a09..564ff5d 100644
@@ -69,8 +69,6 @@ static DEFINE_SPINLOCK(rtcdev_lock);
  * alarmtimer_get_rtcdev - Return selected rtcdevice
  *
  * This function returns the rtc device to use for wakealarms.
- * If one has not already been chosen, it checks to see if a
- * functional rtc device is available.
  */
 struct rtc_device *alarmtimer_get_rtcdev(void)
 {