Skip to contentSkip to conversation

rtc: Add support for limited alarm timer offsets

PATCH Superseded Guenter Roeck 3 participants 20 messages Aug 16, 2023 – Aug 17, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover rtc: Add support for limited alarm timer offsets

Some alarm timers are based on time offsets, not on absolute times. In some situations, the amount of time that can be scheduled in the future is limited. This may result in a refusal to suspend the system, causing substantial battery drain.

This problem was previously observed on a Chromebook using the cros_ec rtc driver. EC variants on some older Chromebooks only support 24 hours of alarm time in the future. To work around the problem on affected Chromebooks, code to limit the maximum alarm time was added to the cros_ec rtc driver with commit f27efee66370 ("rtc: cros-ec: Limit RTC alarm range if needed"). The problem is now seen again on a system using the cmos RTC driver on hardware limited to 24 hours of alarm time, so a more generic solution is needed.

Some RTC drivers remedy the situation by setting the alarm time to the maximum supported time if a request for an out-of-range timeout is made. This is not really desirable since it may result in unexpected early wakeups. It would be even more undesirable to change the behavior of existing widely used drivers such as the cmos RTC driver.

The existing range_max variable in struct rtc_device can not be used to determine the maximum time offset supported by an rtc chip since it describes the maximum absolute time supported by the chip, not the maximum time offset that can be set for alarms.

To reduce the impact of this problem, introduce a new variable rtc_time_offset in struct rtc_device to let RTC drivers report the maximum supported alarm time offset. The code setting alarm timers can then decide if it wants to reject setting alarm timers to a larger value, if it wants to implement recurring alarms until the actually requested alarm time is met, or if it wants to accept the limited alarm time. Use the new variable to limit the alarm timer range.

The series is intended to solve the problem with minimal changes in the rtc core and in affected drivers.

An alternative I had considered was to have the alarmtimer code guess the maximum timeout supported by the rtc hardware. I discarded it as less desirable since it had to retry repeatedly depending on rtc limitations. This often resulted in error messages by the rtc driver. On top of that, it was all but impossible to support rtc chips such as tps6586x which can only support wake alarms up to 16,383 seconds in the future.

The first patch of the series adds support for providing the maximum supported time offset to the rtc core. The second patch uses that value in the alarmtimer code to set the maximum wake-up time from system suspend. Subsequent patches add support for reporting the maximum alarm timer offset to a subset of affected drivers.

Previous discussion:

    https://lore.kernel.org/lkml/Y19AdIntJZGnBh%[email protected]/T/#mc06d206d5bdb77c613712148818934b4f5640de5

----------------------------------------------------------------
Guenter Roeck (7):
rtc: Add support for limited alarm timer offsets
rtc: alarmtimer: Use maximum alarm time offset
rtc: cros-ec: Detect and report supported alarm window size
rtc: cmos: Report supported alarm limit to rtc infrastructure
rtc: tps6586x: Report maximum alarm limit to rtc core
rtc: ds1305: Report maximum alarm limit to rtc core
rtc: rzn1: Report maximum alarm limit to rtc core

 drivers/rtc/rtc-cmos.c     | 11 +++++++++++
 drivers/rtc/rtc-cros-ec.c  | 38 +++++++++++++++++++++++---------------
 drivers/rtc/rtc-ds1305.c   |  3 ++-
 drivers/rtc/rtc-rzn1.c     |  1 +
 drivers/rtc/rtc-tps6586x.c |  1 +
 include/linux/rtc.h        |  1 +
 kernel/time/alarmtimer.c   | 13 +++++++++++++
 7 files changed, 52 insertions(+), 16 deletions(-)
#1 lore raw

PATCH 1/7 rtc: Add support for limited alarm timer offsets

Some alarm timers are based on time offsets, not on absolute times. In some situations, the amount of time that can be scheduled in the future is limited. This may result in a refusal to suspend the system, causing substantial battery drain.

Some RTC alarm drivers remedy the situation by setting the alarm time to the maximum supported time if a request for an out-of-range timeout is made. This is not really desirable since it may result in unexpected early wakeups.

To reduce the impact of this problem, let RTC drivers report the maximum supported alarm timer offset. The code setting alarm timers can then decide if it wants to reject setting alarm timers to a larger value, if it wants to implement recurring alarms until the actually requested alarm time is met, or if it wants to accept the limited alarm time.

Only introduce the necessary variable into struct rtc_device. Code to set and use the variable will follow with subsequent patches.

---
 include/linux/rtc.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/rtc.h +1 -0
index 1fd9c6a21ebe..b6d000ab1e5e 100644
@@ -146,6 +146,7 @@ struct rtc_device {
 
 	time64_t range_min;
 	timeu64_t range_max;
+	timeu64_t range_max_offset;
 	time64_t start_secs;
 	time64_t offset_secs;
 	bool set_start_time;
Signature · 1 line
2.39.2
#2 lore raw
Quoted 30 lines · Guenter Roeck

Some alarm timers are based on time offsets, not on absolute times. In some situations, the amount of time that can be scheduled in the future is limited. This may result in a refusal to suspend the system, causing substantial battery drain.

Some RTC alarm drivers remedy the situation by setting the alarm time to the maximum supported time if a request for an out-of-range timeout is made. This is not really desirable since it may result in unexpected early wakeups.

To reduce the impact of this problem, let RTC drivers report the maximum supported alarm timer offset. The code setting alarm timers can then decide if it wants to reject setting alarm timers to a larger value, if it wants to implement recurring alarms until the actually requested alarm time is met, or if it wants to accept the limited alarm time.

Only introduce the necessary variable into struct rtc_device. Code to set and use the variable will follow with subsequent patches.

---
 include/linux/rtc.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/rtc.h +0 -0
index 1fd9c6a21ebe..b6d000ab1e5e 100644

include/linux/rtc.h struct rtc_device { · line 149

Quoted 4 lines
include/linux/rtc.h +1 -0
@@ -146,6 +146,7 @@ struct rtc_device {
 
 	time64_t range_min;
 	timeu64_t range_max;
+	timeu64_t range_max_offset;

While range_min and range_max are for the wall clock time, I would prefer using a name that would clearly mark this as an alarm related variable.

Quoted 6 lines
include/linux/rtc.h +0 -0
 	time64_t start_secs;
 	time64_t offset_secs;
 	bool set_start_time;
Signature · 1 line
2.39.2
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#3 lore raw
Quoted 32 lines · Alexandre Belloni
Quoted 31 lines · Guenter Roeck

Some alarm timers are based on time offsets, not on absolute times. In some situations, the amount of time that can be scheduled in the future is limited. This may result in a refusal to suspend the system, causing substantial battery drain.

Some RTC alarm drivers remedy the situation by setting the alarm time to the maximum supported time if a request for an out-of-range timeout is made. This is not really desirable since it may result in unexpected early wakeups.

To reduce the impact of this problem, let RTC drivers report the maximum supported alarm timer offset. The code setting alarm timers can then decide if it wants to reject setting alarm timers to a larger value, if it wants to implement recurring alarms until the actually requested alarm time is met, or if it wants to accept the limited alarm time.

Only introduce the necessary variable into struct rtc_device. Code to set and use the variable will follow with subsequent patches.

---
 include/linux/rtc.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/rtc.h +0 -0
index 1fd9c6a21ebe..b6d000ab1e5e 100644
@@ -146,6 +146,7 @@ struct rtc_device {
 
Quoted 7 lines

include/linux/rtc.h struct rtc_device { · line 149

Quoted 3 lines
include/linux/rtc.h +1 -0
 	time64_t range_min;
 	timeu64_t range_max;
+	timeu64_t range_max_offset;

While range_min and range_max are for the wall clock time, I would prefer using a name that would clearly mark this as an alarm related variable.

Sure, no problem. Do you have a suggestion ? alarm_range_max or alarm_range_max_offset, maybe ? I'd also be happy to use some other term for 'offset' if you have a suggestion.

Thanks,
Guenter

#4 lore raw
Quoted 41 lines · Guenter Roeck
Quoted 32 lines · Alexandre Belloni
Quoted 31 lines · Guenter Roeck

Some alarm timers are based on time offsets, not on absolute times. In some situations, the amount of time that can be scheduled in the future is limited. This may result in a refusal to suspend the system, causing substantial battery drain.

Some RTC alarm drivers remedy the situation by setting the alarm time to the maximum supported time if a request for an out-of-range timeout is made. This is not really desirable since it may result in unexpected early wakeups.

To reduce the impact of this problem, let RTC drivers report the maximum supported alarm timer offset. The code setting alarm timers can then decide if it wants to reject setting alarm timers to a larger value, if it wants to implement recurring alarms until the actually requested alarm time is met, or if it wants to accept the limited alarm time.

Only introduce the necessary variable into struct rtc_device. Code to set and use the variable will follow with subsequent patches.

---
 include/linux/rtc.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/rtc.h +0 -0
index 1fd9c6a21ebe..b6d000ab1e5e 100644
@@ -146,6 +146,7 @@ struct rtc_device {
 
Quoted 7 lines

include/linux/rtc.h struct rtc_device { · line 149

Quoted 3 lines
include/linux/rtc.h +1 -0
 	time64_t range_min;
 	timeu64_t range_max;
+	timeu64_t range_max_offset;

While range_min and range_max are for the wall clock time, I would prefer using a name that would clearly mark this as an alarm related variable.

Quoted 3 lines

Sure, no problem. Do you have a suggestion ? alarm_range_max or alarm_range_max_offset, maybe ? I'd also be happy to use some other term for 'offset' if you have a suggestion.

I don't really know, what about alarm_offset_max? This is the maximum value of the offset for the alarm to the current time.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#5 lore raw
Quoted 42 lines · Alexandre Belloni
Quoted 41 lines · Guenter Roeck
Quoted 39 lines · Alexandre Belloni
Quoted 30 lines · Guenter Roeck

Some alarm timers are based on time offsets, not on absolute times. In some situations, the amount of time that can be scheduled in the future is limited. This may result in a refusal to suspend the system, causing substantial battery drain.

Some RTC alarm drivers remedy the situation by setting the alarm time to the maximum supported time if a request for an out-of-range timeout is made. This is not really desirable since it may result in unexpected early wakeups.

To reduce the impact of this problem, let RTC drivers report the maximum supported alarm timer offset. The code setting alarm timers can then decide if it wants to reject setting alarm timers to a larger value, if it wants to implement recurring alarms until the actually requested alarm time is met, or if it wants to accept the limited alarm time.

Only introduce the necessary variable into struct rtc_device. Code to set and use the variable will follow with subsequent patches.

---
 include/linux/rtc.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/rtc.h +0 -0
index 1fd9c6a21ebe..b6d000ab1e5e 100644

include/linux/rtc.h struct rtc_device { · line 149

Quoted 4 lines
include/linux/rtc.h +1 -0
@@ -146,6 +146,7 @@ struct rtc_device {
 
 	time64_t range_min;
 	timeu64_t range_max;
+	timeu64_t range_max_offset;

While range_min and range_max are for the wall clock time, I would prefer using a name that would clearly mark this as an alarm related variable.

Quoted 7 lines
Quoted 3 lines

Sure, no problem. Do you have a suggestion ? alarm_range_max or alarm_range_max_offset, maybe ? I'd also be happy to use some other term for 'offset' if you have a suggestion.

I don't really know, what about alarm_offset_max? This is the maximum value of the offset for the alarm to the current time.

Sounds good to me.

Thanks,
Guenter

Quoted 5 lines
Signature · 4 lines
-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#6 lore raw

PATCH 2/7 rtc: alarmtimer: Use maximum alarm time offset

In reply to Guenter Roeck

Some userspace applications use timerfd_create() to request wakeups after a long period of time. For example, a backup application may request a wakeup once per week. This is perfectly fine as long as the system does not try to suspend. However, if the system tries to suspend and the system's RTC does not support the required alarm timeout, the suspend operation will fail with an error such as

rtc_cmos 00:01: Alarms can be up to one day in the future
PM: dpm_run_callback(): platform_pm_suspend+0x0/0x4a returns -22 alarmtimer alarmtimer.4.auto: platform_pm_suspend+0x0/0x4a returned -22 after 117 usecs
PM: Device alarmtimer.4.auto failed to suspend: error -22

This results in a refusal to suspend the system, causing substantial battery drain on affected systems.

To fix the problem, use the maximum alarm time offset as reported by rtc drivers to set the maximum alarm time. While this will result in brief spurious wakeups from suspend, it is still much better than not suspending at all.

---
 kernel/time/alarmtimer.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
kernel/time/alarmtimer.c +13 -0
index 8d9f13d847f0..cc31998a34b9 100644
@@ -290,6 +290,19 @@ static int alarmtimer_suspend(struct device *dev)
 	rtc_timer_cancel(rtc, &rtctimer);
 	rtc_read_time(rtc, &tm);
 	now = rtc_tm_to_ktime(tm);
+
+	/*
+	 * If the RTC alarm timer only supports a limited time offset, set
+	 * the alarm time to the maximum supported value.
+	 * The system will wake up earlier than necessary and is expected
+	 * to go back to sleep if it has nothing to do.
+	 * It would be desirable to handle such early wakeups without fully
+	 * waking up the system, but it is unknown if this is even possible.
+	 */
+	if (rtc->range_max_offset &&
+	    rtc->range_max_offset * MSEC_PER_SEC < ktime_to_ms(min))
+		min = ms_to_ktime(rtc->range_max_offset * MSEC_PER_SEC);
+
 	now = ktime_add(now, min);
 
 	/* Set alarm, if in the past reject suspend briefly to handle */
Signature · 1 line
2.39.2
#7 lore raw

PATCH 3/7 rtc: cros-ec: Detect and report supported alarm window size

In reply to Guenter Roeck

The RTC on some older Chromebooks can only handle alarms less than 24 hours in the future. The only way to find out is to try to set an alarm further in the future. If that fails, assume that the RTC connected to the EC can only handle less than 24 hours of alarm window, and report that value to the RTC core.

After that change, it is no longer necessary to limit the alarm time when setting it. Report any excessive alarms to the caller instead.

---
 drivers/rtc/rtc-cros-ec.c | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)
drivers/rtc/rtc-cros-ec.c +23 -15
index 998ab8606f0b..a257a24e8223 100644
@@ -182,21 +182,15 @@ static int cros_ec_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 
 	ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, alarm_offset);
 	if (ret < 0) {
-		if (ret == -EINVAL && alarm_offset >= SECS_PER_DAY) {
-			/*
-			 * RTC chips on some older Chromebooks can only handle
-			 * alarms up to 24h in the future. Try to set an alarm
-			 * below that limit to avoid suspend failures.
-			 */
-			ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
-					      SECS_PER_DAY - 1);
-		}
-
-		if (ret < 0) {
-			dev_err(dev, "error setting alarm in %u seconds: %d\n",
-				alarm_offset, ret);
-			return ret;
-		}
+		dev_err(dev, "error setting alarm in %u seconds: %d\n",
+			alarm_offset, ret);
+		/*
+		 * The EC code returns -EINVAL if the alarm time is too
+		 * far in the future. Convert it to the expected error code.
+		 */
+		if (ret == -EINVAL)
+			ret = -ERANGE;
+		return ret;
 	}
 
 	return 0;
@@ -355,6 +349,20 @@ static int cros_ec_rtc_probe(struct platform_device *pdev)
 	cros_ec_rtc->rtc->ops = &cros_ec_rtc_ops;
 	cros_ec_rtc->rtc->range_max = U32_MAX;
 
+	/*
+	 * The RTC on some older Chromebooks can only handle alarms less than
+	 * 24 hours in the future. The only way to find out is to try to set an
+	 * alarm further in the future. If that fails, assume that the RTC
+	 * connected to the EC can only handle less than 24 hours of alarm
+	 * window.
+	 */
+	ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, SECS_PER_DAY * 2);
+	if (ret == -EINVAL)
+		cros_ec_rtc->rtc->range_max_offset = SECS_PER_DAY - 1;
+
+	(void)cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
+			      EC_RTC_ALARM_CLEAR);
+
 	ret = devm_rtc_register_device(cros_ec_rtc->rtc);
 	if (ret)
 		return ret;
Signature · 1 line
2.39.2
#8 lore raw

PATCH 4/7 rtc: cmos: Report supported alarm limit to rtc infrastructure

In reply to Guenter Roeck

The alarm window supported by the cmos RTC depends on the chip and its configuration. Report the limit to the RTC core.

---
 drivers/rtc/rtc-cmos.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/rtc/rtc-cmos.c +11 -0
index c9416fe8542d..c565dc4d5bf5 100644
@@ -913,6 +913,10 @@ static inline void cmos_check_acpi_rtc_status(struct device *dev,
 #define	INITSECTION	__init
 #endif
 
+#define SECS_PER_DAY	(24 * 60 * 60)
+#define SECS_PER_MONTH	(28 * SECS_PER_DAY)
+#define SECS_PER_YEAR	(365 * SECS_PER_DAY)
+
 static int INITSECTION
 cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
 {
@@ -1019,6 +1023,13 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
 		goto cleanup0;
 	}
 
+	if (cmos_rtc.mon_alrm)
+		cmos_rtc.rtc->range_max_offset = SECS_PER_YEAR - 1;
+	else if (cmos_rtc.day_alrm)
+		cmos_rtc.rtc->range_max_offset = SECS_PER_MONTH - 1;
+	else
+		cmos_rtc.rtc->range_max_offset = SECS_PER_DAY - 1;
+
 	rename_region(ports, dev_name(&cmos_rtc.rtc->dev));
 
 	if (!mc146818_does_rtc_work()) {
Signature · 1 line
2.39.2
#9 lore raw

PATCH 5/7 rtc: tps6586x: Report maximum alarm limit to rtc core

In reply to Guenter Roeck

tps6586x only supports alarms up to 16,383 seconds in the future. Report the limit to the RTC core.

---
 drivers/rtc/rtc-tps6586x.c | 1 +
 1 file changed, 1 insertion(+)
drivers/rtc/rtc-tps6586x.c +1 -0
index 9f14e2475747..11f822d1b337 100644
@@ -252,6 +252,7 @@ static int tps6586x_rtc_probe(struct platform_device *pdev)
 
 	rtc->rtc->ops = &tps6586x_rtc_ops;
 	rtc->rtc->range_max = (1ULL << 30) - 1; /* 30-bit seconds */
+	rtc->rtc->range_max_offset = ALM1_VALID_RANGE_IN_SEC;
 	rtc->rtc->start_secs = mktime64(2009, 1, 1, 0, 0, 0);
 	rtc->rtc->set_start_time = true;
 
Signature · 1 line
2.39.2
#10 lore raw

PATCH 6/7 rtc: ds1305: Report maximum alarm limit to rtc core

In reply to Guenter Roeck

DS1305 only supports alarms up to 24 hours in the future. Report the limit to the RTC core.

If the alarm is too large when trying to set an alarm, return -ERANGE instead of -EDOM to align with error codes returned by other rtc drivers.

---
 drivers/rtc/rtc-ds1305.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/rtc/rtc-ds1305.c +2 -1
index ed9360486953..1d9bbf4e06cb 100644
@@ -337,7 +337,7 @@ static int ds1305_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
 	if (later <= now)
 		return -EINVAL;
 	if ((later - now) > 24 * 60 * 60)
-		return -EDOM;
+		return -ERANGE;
 
 	/* disable alarm if needed */
 	if (ds1305->ctrl[0] & DS1305_AEI0) {
@@ -691,6 +691,7 @@ static int ds1305_probe(struct spi_device *spi)
 	ds1305->rtc->ops = &ds1305_ops;
 	ds1305->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
 	ds1305->rtc->range_max = RTC_TIMESTAMP_END_2099;
+	ds1305->rtc->range_max_offset = 24 * 60 * 60;
 
 	ds1305_nvmem_cfg.priv = ds1305;
 	status = devm_rtc_register_device(ds1305->rtc);
Signature · 1 line
2.39.2
#11 lore raw

PATCH 7/7 rtc: rzn1: Report maximum alarm limit to rtc core

In reply to Guenter Roeck

RZN1 only supports alarms up to one week in the future. Report the limit to the RTC core.

---
 drivers/rtc/rtc-rzn1.c | 1 +
 1 file changed, 1 insertion(+)
drivers/rtc/rtc-rzn1.c +1 -0
index dca736caba85..c2cc3774ebb8 100644
@@ -351,6 +351,7 @@ static int rzn1_rtc_probe(struct platform_device *pdev)
 
 	rtc->rtcdev->range_min = RTC_TIMESTAMP_BEGIN_2000;
 	rtc->rtcdev->range_max = RTC_TIMESTAMP_END_2099;
+	rtc->rtcdev->range_max_offset = 7 * 86400;
 	rtc->rtcdev->ops = &rzn1_rtc_ops;
 	set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->rtcdev->features);
 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->rtcdev->features);
Signature · 1 line
2.39.2
maintainer Reviewed-by on v1 7/7
#12 lore raw

Re: [PATCH 7/7] rtc: rzn1: Report maximum alarm limit to rtc core

Hi Guenter,

[email protected] wrote on Wed, 16 Aug 2023 06:39:36 -0700:

Quoted 14 lines

RZN1 only supports alarms up to one week in the future. Report the limit to the RTC core.

---
 drivers/rtc/rtc-rzn1.c | 1 +
 1 file changed, 1 insertion(+)
drivers/rtc/rtc-rzn1.c +0 -0
index dca736caba85..c2cc3774ebb8 100644

drivers/rtc/rtc-rzn1.c static int rzn1_rtc_probe(struct platform_device *pdev) · line 354

Quoted 4 lines
drivers/rtc/rtc-rzn1.c +1 -0
@@ -351,6 +351,7 @@ static int rzn1_rtc_probe(struct platform_device *pdev)
 
 	rtc->rtcdev->range_min = RTC_TIMESTAMP_BEGIN_2000;
 	rtc->rtcdev->range_max = RTC_TIMESTAMP_END_2099;
+	rtc->rtcdev->range_max_offset = 7 * 86400;

When we set the alarm, we use:

	farest = rtc_tm_to_time64(&tm_now) + (7 * 86400);

What about using range_max_offset (whatever its final name) in this calculation as it will be now set in probe? It would further clarify its purpose.

drivers/rtc/rtc-rzn1.c static int rzn1_rtc_probe(struct platform_device *pdev) · line 357

Quoted 3 lines
drivers/rtc/rtc-rzn1.c +0 -0
 	rtc->rtcdev->ops = &rzn1_rtc_ops;
 	set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->rtcdev->features);
 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->rtcdev->features);

With the above change,

Thanks,
Miquèl

#13 lore raw

Re: [PATCH 7/7] rtc: rzn1: Report maximum alarm limit to rtc core

Quoted 25 lines · Miquel Raynal

Hi Guenter,

[email protected] wrote on Wed, 16 Aug 2023 06:39:36 -0700:

Quoted 14 lines

RZN1 only supports alarms up to one week in the future. Report the limit to the RTC core.

---
 drivers/rtc/rtc-rzn1.c | 1 +
 1 file changed, 1 insertion(+)
drivers/rtc/rtc-rzn1.c +0 -0
index dca736caba85..c2cc3774ebb8 100644

drivers/rtc/rtc-rzn1.c static int rzn1_rtc_probe(struct platform_device *pdev) · line 354

Quoted 4 lines
drivers/rtc/rtc-rzn1.c +1 -0
@@ -351,6 +351,7 @@ static int rzn1_rtc_probe(struct platform_device *pdev)
 
 	rtc->rtcdev->range_min = RTC_TIMESTAMP_BEGIN_2000;
 	rtc->rtcdev->range_max = RTC_TIMESTAMP_END_2099;
+	rtc->rtcdev->range_max_offset = 7 * 86400;

When we set the alarm, we use:

Quoted 5 lines
	farest = rtc_tm_to_time64(&tm_now) + (7 * 86400);

What about using range_max_offset (whatever its final name) in this calculation as it will be now set in probe? It would further clarify its purpose.

Excellent idea. I'll make that change.

Thanks,
Guenter

Quoted 11 lines

drivers/rtc/rtc-rzn1.c static int rzn1_rtc_probe(struct platform_device *pdev) · line 357

Quoted 4 lines
drivers/rtc/rtc-rzn1.c +0 -0
 	rtc->rtcdev->ops = &rzn1_rtc_ops;
 	set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->rtcdev->features);
 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->rtcdev->features);

With the above change,

Thanks,
Miquèl

#14 lore raw
In reply to Guenter Roeck

Hello,

Quoted 43 lines · Guenter Roeck

Some alarm timers are based on time offsets, not on absolute times. In some situations, the amount of time that can be scheduled in the future is limited. This may result in a refusal to suspend the system, causing substantial battery drain.

This problem was previously observed on a Chromebook using the cros_ec rtc driver. EC variants on some older Chromebooks only support 24 hours of alarm time in the future. To work around the problem on affected Chromebooks, code to limit the maximum alarm time was added to the cros_ec rtc driver with commit f27efee66370 ("rtc: cros-ec: Limit RTC alarm range if needed"). The problem is now seen again on a system using the cmos RTC driver on hardware limited to 24 hours of alarm time, so a more generic solution is needed.

Some RTC drivers remedy the situation by setting the alarm time to the maximum supported time if a request for an out-of-range timeout is made. This is not really desirable since it may result in unexpected early wakeups. It would be even more undesirable to change the behavior of existing widely used drivers such as the cmos RTC driver.

The existing range_max variable in struct rtc_device can not be used to determine the maximum time offset supported by an rtc chip since it describes the maximum absolute time supported by the chip, not the maximum time offset that can be set for alarms.

To reduce the impact of this problem, introduce a new variable rtc_time_offset in struct rtc_device to let RTC drivers report the maximum supported alarm time offset. The code setting alarm timers can then decide if it wants to reject setting alarm timers to a larger value, if it wants to implement recurring alarms until the actually requested alarm time is met, or if it wants to accept the limited alarm time. Use the new variable to limit the alarm timer range.

The series is intended to solve the problem with minimal changes in the rtc core and in affected drivers.

An alternative I had considered was to have the alarmtimer code guess the maximum timeout supported by the rtc hardware. I discarded it as less desirable since it had to retry repeatedly depending on rtc limitations. This often resulted in error messages by the rtc driver. On top of that, it was all but impossible to support rtc chips such as tps6586x which can only support wake alarms up to 16,383 seconds in the future.

Quoted 9 lines

The first patch of the series adds support for providing the maximum supported time offset to the rtc core. The second patch uses that value in the alarmtimer code to set the maximum wake-up time from system suspend. Subsequent patches add support for reporting the maximum alarm timer offset to a subset of affected drivers.

Previous discussion:

    https://lore.kernel.org/lkml/Y19AdIntJZGnBh%[email protected]/T/#mc06d206d5bdb77c613712148818934b4f5640de5

I'm fine with the series, however, this doesn't solve the issue for RTCs that have an absolute limit on the alarm (as opposed to an offset to the current time/date).

Quoted 18 lines

----------------------------------------------------------------
Guenter Roeck (7):
rtc: Add support for limited alarm timer offsets
rtc: alarmtimer: Use maximum alarm time offset
rtc: cros-ec: Detect and report supported alarm window size
rtc: cmos: Report supported alarm limit to rtc infrastructure
rtc: tps6586x: Report maximum alarm limit to rtc core
rtc: ds1305: Report maximum alarm limit to rtc core
rtc: rzn1: Report maximum alarm limit to rtc core

 drivers/rtc/rtc-cmos.c     | 11 +++++++++++
 drivers/rtc/rtc-cros-ec.c  | 38 +++++++++++++++++++++++---------------
 drivers/rtc/rtc-ds1305.c   |  3 ++-
 drivers/rtc/rtc-rzn1.c     |  1 +
 drivers/rtc/rtc-tps6586x.c |  1 +
 include/linux/rtc.h        |  1 +
 kernel/time/alarmtimer.c   | 13 +++++++++++++
 7 files changed, 52 insertions(+), 16 deletions(-)
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#15 lore raw

Hi Alexandre,

Quoted 52 lines · Alexandre Belloni

Hello,

Quoted 49 lines · Guenter Roeck

Some alarm timers are based on time offsets, not on absolute times. In some situations, the amount of time that can be scheduled in the future is limited. This may result in a refusal to suspend the system, causing substantial battery drain.

This problem was previously observed on a Chromebook using the cros_ec rtc driver. EC variants on some older Chromebooks only support 24 hours of alarm time in the future. To work around the problem on affected Chromebooks, code to limit the maximum alarm time was added to the cros_ec rtc driver with commit f27efee66370 ("rtc: cros-ec: Limit RTC alarm range if needed"). The problem is now seen again on a system using the cmos RTC driver on hardware limited to 24 hours of alarm time, so a more generic solution is needed.

Some RTC drivers remedy the situation by setting the alarm time to the maximum supported time if a request for an out-of-range timeout is made. This is not really desirable since it may result in unexpected early wakeups. It would be even more undesirable to change the behavior of existing widely used drivers such as the cmos RTC driver.

The existing range_max variable in struct rtc_device can not be used to determine the maximum time offset supported by an rtc chip since it describes the maximum absolute time supported by the chip, not the maximum time offset that can be set for alarms.

To reduce the impact of this problem, introduce a new variable rtc_time_offset in struct rtc_device to let RTC drivers report the maximum supported alarm time offset. The code setting alarm timers can then decide if it wants to reject setting alarm timers to a larger value, if it wants to implement recurring alarms until the actually requested alarm time is met, or if it wants to accept the limited alarm time. Use the new variable to limit the alarm timer range.

The series is intended to solve the problem with minimal changes in the rtc core and in affected drivers.

An alternative I had considered was to have the alarmtimer code guess the maximum timeout supported by the rtc hardware. I discarded it as less desirable since it had to retry repeatedly depending on rtc limitations. This often resulted in error messages by the rtc driver. On top of that, it was all but impossible to support rtc chips such as tps6586x which can only support wake alarms up to 16,383 seconds in the future.

The first patch of the series adds support for providing the maximum supported time offset to the rtc core. The second patch uses that value in the alarmtimer code to set the maximum wake-up time from system suspend. Subsequent patches add support for reporting the maximum alarm timer offset to a subset of affected drivers.

Quoted 8 lines
Quoted 3 lines

Previous discussion:

    https://lore.kernel.org/lkml/Y19AdIntJZGnBh%[email protected]/T/#mc06d206d5bdb77c613712148818934b4f5640de5

I'm fine with the series, however, this doesn't solve the issue for RTCs that have an absolute limit on the alarm (as opposed to an offset to the current time/date).

I thought that is checked by rtc_valid_range() in rtc_set_alarm(). Am I missing something ? Of course that assumes that the absolute maximum alarm timeout matches range_max, but I didn't find any drivers where that would not be the case.

Thanks,
Guenter

Quoted 25 lines
Quoted 20 lines

----------------------------------------------------------------
Guenter Roeck (7):
rtc: Add support for limited alarm timer offsets
rtc: alarmtimer: Use maximum alarm time offset
rtc: cros-ec: Detect and report supported alarm window size
rtc: cmos: Report supported alarm limit to rtc infrastructure
rtc: tps6586x: Report maximum alarm limit to rtc core
rtc: ds1305: Report maximum alarm limit to rtc core
rtc: rzn1: Report maximum alarm limit to rtc core

 drivers/rtc/rtc-cmos.c     | 11 +++++++++++
 drivers/rtc/rtc-cros-ec.c  | 38 +++++++++++++++++++++++---------------
 drivers/rtc/rtc-ds1305.c   |  3 ++-
 drivers/rtc/rtc-rzn1.c     |  1 +
 drivers/rtc/rtc-tps6586x.c |  1 +
 include/linux/rtc.h        |  1 +
 kernel/time/alarmtimer.c   | 13 +++++++++++++
 7 files changed, 52 insertions(+), 16 deletions(-)
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#16 lore raw
Quoted 10 lines · Guenter Roeck
Quoted 4 lines

I'm fine with the series, however, this doesn't solve the issue for RTCs that have an absolute limit on the alarm (as opposed to an offset to the current time/date).

I thought that is checked by rtc_valid_range() in rtc_set_alarm(). Am I missing something ? Of course that assumes that the absolute maximum alarm timeout matches range_max, but I didn't find any drivers where that would not be the case.

There are RTCs where this is not the case. When this is far away in the future enough, the usual solution is to clip range_max which works but is not really great intellectually.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#17 lore raw
Quoted 12 lines · Alexandre Belloni
Quoted 10 lines · Guenter Roeck
Quoted 4 lines

I'm fine with the series, however, this doesn't solve the issue for RTCs that have an absolute limit on the alarm (as opposed to an offset to the current time/date).

I thought that is checked by rtc_valid_range() in rtc_set_alarm(). Am I missing something ? Of course that assumes that the absolute maximum alarm timeout matches range_max, but I didn't find any drivers where that would not be the case.

Quoted 4 lines

There are RTCs where this is not the case. When this is far away in the future enough, the usual solution is to clip range_max which works but is not really great intellectually.

Do you have an example, by any chance ?

Thanks,
Guenter

Quoted 4 lines
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#18 lore raw
Quoted 13 lines · Guenter Roeck
Quoted 12 lines · Alexandre Belloni
Quoted 10 lines · Guenter Roeck
Quoted 4 lines

I'm fine with the series, however, this doesn't solve the issue for RTCs that have an absolute limit on the alarm (as opposed to an offset to the current time/date).

I thought that is checked by rtc_valid_range() in rtc_set_alarm(). Am I missing something ? Of course that assumes that the absolute maximum alarm timeout matches range_max, but I didn't find any drivers where that would not be the case.

Quoted 6 lines
Quoted 4 lines

There are RTCs where this is not the case. When this is far away in the future enough, the usual solution is to clip range_max which works but is not really great intellectually.

Do you have an example, by any chance ?

I'm sorry, I've been looking and I couldn't find it anymore. Don't bother with this for now.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#19 lore raw
Quoted 18 lines · Alexandre Belloni
Quoted 17 lines · Guenter Roeck
Quoted 16 lines · Alexandre Belloni
Quoted 10 lines · Guenter Roeck
Quoted 4 lines

I'm fine with the series, however, this doesn't solve the issue for RTCs that have an absolute limit on the alarm (as opposed to an offset to the current time/date).

I thought that is checked by rtc_valid_range() in rtc_set_alarm(). Am I missing something ? Of course that assumes that the absolute maximum alarm timeout matches range_max, but I didn't find any drivers where that would not be the case.

There are RTCs where this is not the case. When this is far away in the future enough, the usual solution is to clip range_max which works but is not really great intellectually.

Quoted 6 lines
Quoted 2 lines

Do you have an example, by any chance ?

I'm sorry, I've been looking and I couldn't find it anymore. Don't bother with this for now.

I have been looking as well and did not find it either. I'll go ahead and send v2 with the suggested changes.

Thanks,
Guenter