Skip to contentSkip to conversation

rtc: Add support for limited alarm timer offsets

PATCH Applied Guenter Roeck 5 participants 21 messages Aug 17, 2023 – Sep 6, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 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 alarm_offset_max 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

v2:
- Rename range_max_offset -> alarm_offset_max
- Use the new variable to validate the limit where possible

----------------------------------------------------------------
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   |  5 +++--
 drivers/rtc/rtc-rzn1.c     |  3 ++-
 drivers/rtc/rtc-tps6586x.c |  1 +
 include/linux/rtc.h        |  1 +
 kernel/time/alarmtimer.c   | 13 +++++++++++++
 7 files changed, 54 insertions(+), 18 deletions(-)
#1 lore raw

PATCH v2 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.

---
v2: Rename range_max_offset -> alarm_offset_max

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

Hi Alexandre,

Quoted 16 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.

Quoted 5 lines

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

I guess it is a bit late to get the series into v6.6, but would it be possible to apply it to a -next branch to get some more test coverage ?

Either case, do you have any additional comments / feedback ?

Thanks,
Guenter

Quoted 18 lines

---
v2: Rename range_max_offset -> alarm_offset_max

 include/linux/rtc.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/rtc.h +1 -0
index 1fd9c6a21ebe..4c0bcbeb1f00 100644
@@ -146,6 +146,7 @@ struct rtc_device {
 
 	time64_t range_min;
 	timeu64_t range_max;
+	timeu64_t alarm_offset_max;
 	time64_t start_secs;
 	time64_t offset_secs;
 	bool set_start_time;
#3 lore raw

Hello,

Quoted 19 lines · Guenter Roeck

Hi Alexandre,

Quoted 16 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.

Quoted 9 lines
Quoted 5 lines

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

I guess it is a bit late to get the series into v6.6, but would it be possible to apply it to a -next branch to get some more test coverage ?

I'm probably going to take 1 and 3-7 for 6.6 once I get a reliable internet access. I can't take 2/7 without a review or ack from the time maintainers.

Quoted 2 lines

Either case, do you have any additional comments / feedback ?

The main issue that remains is that after 2/7, the rtc_device structure is not opaque anymore to its user as alarmtimer_suspend now directly accesses one of the members. But I'd have to find which RTCs have an absolute limit so we can design a proper API. I may also decide that it is good enough to require that the alarm range must cover the registered RTC range.

Quoted 21 lines

Thanks,
Guenter

Quoted 18 lines

---
v2: Rename range_max_offset -> alarm_offset_max

 include/linux/rtc.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/rtc.h +1 -0
index 1fd9c6a21ebe..4c0bcbeb1f00 100644
@@ -146,6 +146,7 @@ struct rtc_device {
 
 	time64_t range_min;
 	timeu64_t range_max;
+	timeu64_t alarm_offset_max;
 	time64_t start_secs;
 	time64_t offset_secs;
 	bool set_start_time;
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw
Quoted 28 lines · Alexandre Belloni

Hello,

Quoted 25 lines · Guenter Roeck

Hi Alexandre,

Quoted 21 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.

Quoted 8 lines
Quoted 3 lines

I guess it is a bit late to get the series into v6.6, but would it be possible to apply it to a -next branch to get some more test coverage ?

I'm probably going to take 1 and 3-7 for 6.6 once I get a reliable internet access. I can't take 2/7 without a review or ack from the time maintainers.

Ok, makes sense.

Quoted 10 lines
Quoted 2 lines

Either case, do you have any additional comments / feedback ?

The main issue that remains is that after 2/7, the rtc_device structure is not opaque anymore to its user as alarmtimer_suspend now directly accesses one of the members. But I'd have to find which RTCs have an absolute limit so we can design a proper API. I may also decide that it is good enough to require that the alarm range must cover the registered RTC range.

What exactly do you have in mind ? For our use case, we need alarmtimer_suspend() to either sleep for the requested time, or as long as possible. Would an API function returning the alarm limit address your concerns ?

Regarding relative/absolute, it is a mixed bag. Below is what I found for the alphabetically first ~40 drivers. Note that "absolute" in the configuration column means that the alarm time is specified as absolute value (e.g, at 1am), not that the limit is absolute or relative to the current time. The limit column gives a hint if the limit is relative or absolute. In many cases the alarm is configured as absolute value but limited to day/week/month/year in the future.

On a side note, struct rtc_device isn't exactly opaque even today. Both alarmtimer.c and ntp.c already access some of its members.

Thanks,
Guenter

---
driver configuration limit comments

rtc-88pm80x.c           relative        24 hours        silently adjusted
rtc-88pm860x.c          relative        32 bit seconds  unchecked
rtc-ab8500.c            absolute        ~2032           unchecked
rtc-ab-b5ze-s3.c        relative        1 month         -EINVAL
rtc-ab-eoz9.c           absolute        1 month         month & year ignored (1 month + 1 day -> fires after 1 day)
rtc-abx80x.c            absolute        1 year          year ignored (1 year + 1 day -> fires after 1 day)
rtc-ac100.c             absolute        2069            -EINVAL
rtc-armada38x.c         absolute        2106            unchecked
rtc-as3722.c            absolute        unlimited       unchecked
rtc-asm9260.c           absolute        unlimited       unchecked
rtc-at91rm9200.c        absolute        1 year          year ignored (1 year + 1 day -> fires after 1 day)
rtc-at91sam9.c          relative        32 bit seconds  unchecked
rtc-bd70528.c           absolute        2099            unchecked
rtc-brcmstb-waketimer.c absolute        2106            unchecked
rtc-cadence.c           absolute        2999            unchecked
rtc-cmos.c              absolute        day/month/year  -EINVAL if out of range
rtc-cpcap.c             absolute        ~2059           in days since 1970, day masked, unchecked (2060 -> 1971)
rtc-cros-ec.c           relative        24h or max      -EINVAL
rtc-da9052.c            absolute        2063            unchecked, year masked
rtc-da9055.c            absolute        2133 ?          unchecked, year masked
rtc-da9063.c            absolute        2063            unchecked, year masked
rtc-digicolor.c         relative        32-bit seconds  unchecked
rtc-ds1286.c            absolute        24 hours        unchecked, month & year ignored
rtc-ds1305.c            absolute        24 hours        -EINVAL
rtc-ds1307.c            absolute        1 year          unchecked, year ignored
rtc-ds1343.c            absolute        1 month         unchecked, month / year ignored
rtc-ds1374.c            relative        ~194 days       unchecked, masked (195 days -> fires after 1 day)
rtc-ds1511.c            absolute        1 month         unchecked, month/year ignored
rtc-ds1553.c            absolute        1 month         unchecked, month/year ignored
rtc-ds1685.c            absolute        1 month         unchecked, month/year ignored
rtc-ds3232.c            absolute        1 month         unchecked, month/year ignored
rtc-efi.c               absolute        not specified   several years
rtc-fm3130.c            absolute        1 year          year ignored
rtc-fsl-ftm-alarm.c     relative        262 seconds     -ERANGE
rtc-goldfish.c          absolute        ~500 years      unchecked
rtc-hym8563.c           absolute        1 month         unchecked, month/year ignored
rtc-imxdi.c             abolute         2106            unchecked (32 bit seconds since 1970)
rtc-imx-sc.c            absolute        unknown         Value passed to SCU which determines if valid
rtc-isl1208.c           absolute        1 year          unchecked, year ignored
rtc-jz4740.c            absolute        2106            unchecked (32 bit seconds since 1970)
#5 lore raw

PATCH v2 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.

---
v2: Rename range_max_offset -> alarm_offset_max

 kernel/time/alarmtimer.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
kernel/time/alarmtimer.c +13 -0
index 8d9f13d847f0..895e3a6d6444 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->alarm_offset_max &&
+	    rtc->alarm_offset_max * MSEC_PER_SEC < ktime_to_ms(min))
+		min = ms_to_ktime(rtc->alarm_offset_max * 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
maintainer
#6 lore raw

Re: [PATCH v2 2/7] rtc: alarmtimer: Use maximum alarm time offset

Quoted 39 lines · 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.

---
v2: Rename range_max_offset -> alarm_offset_max

 kernel/time/alarmtimer.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
kernel/time/alarmtimer.c +2 -0
index 8d9f13d847f0..895e3a6d6444 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);
+
+       /*

kernel/time/alarmtimer.c static int alarmtimer_suspend(struct device *dev) · line 304

Quoted 10 lines
kernel/time/alarmtimer.c +10 -0
+        * 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->alarm_offset_max &&
+           rtc->alarm_offset_max * MSEC_PER_SEC < ktime_to_ms(min))
+               min = ms_to_ktime(rtc->alarm_offset_max * MSEC_PER_SEC);

I don't really have an objection here, but I wonder if this would be better abstracted by a rtc_ function?

ktime_t rtc_bound_ktime_interval(ktime interval)
{
if (!rtc->alarm_offset_max)

      return interval;

return ms_to_ktime(min(rtc->alarm_offset_max, ktime_to_ms(interval)));
}

(simple enough to throw into rtc.h maybe as an inline function?)

Then the above would be tweaked to:
min = rtc_bound_interval(min);

thanks
-john

#7 lore raw

Re: [PATCH v2 2/7] rtc: alarmtimer: Use maximum alarm time offset

Quoted 54 lines · John Stultz
Quoted 39 lines · 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.

---
v2: Rename range_max_offset -> alarm_offset_max

 kernel/time/alarmtimer.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
kernel/time/alarmtimer.c +2 -0
index 8d9f13d847f0..895e3a6d6444 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);
+
+       /*

kernel/time/alarmtimer.c static int alarmtimer_suspend(struct device *dev) · line 304

Quoted 10 lines
kernel/time/alarmtimer.c +10 -0
+        * 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->alarm_offset_max &&
+           rtc->alarm_offset_max * MSEC_PER_SEC < ktime_to_ms(min))
+               min = ms_to_ktime(rtc->alarm_offset_max * MSEC_PER_SEC);

I don't really have an objection here, but I wonder if this would be better abstracted by a rtc_ function?

Quoted 1 line

ktime_t rtc_bound_ktime_interval(ktime interval)

Probably more like like rtc_bound_alarm_interval(), but fine with me.

Quoted 4 lines

{
if (!rtc->alarm_offset_max)

      return interval;

return ms_to_ktime(min(rtc->alarm_offset_max, ktime_to_ms(interval)));

alarm_offset_max is in seconds, so that would need some tweaking.

Guenter

Quoted 9 lines

}

(simple enough to throw into rtc.h maybe as an inline function?)

Then the above would be tweaked to:
min = rtc_bound_interval(min);

thanks
-john

#8 lore raw

Re: [PATCH v2 2/7] rtc: alarmtimer: Use maximum alarm time offset

In reply to John Stultz
Quoted 30 lines · John Stultz
Quoted 29 lines · 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.

---
v2: Rename range_max_offset -> alarm_offset_max

 kernel/time/alarmtimer.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
Quoted 14 lines
Quoted 5 lines
kernel/time/alarmtimer.c +0 -0
index 8d9f13d847f0..895e3a6d6444 100644

kernel/time/alarmtimer.c static int alarmtimer_suspend(struct device *dev) · line 298

Quoted 9 lines
kernel/time/alarmtimer.c +6 -0
@@ -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.

Side note, since someone asked: The behavior is exactly what we see today if a rtc driver silently truncates the requested alarm time. The system is _expected_ to to back to sleep, but what it does depends on its actual configuration. For example, if suspend was requested manually and auto-suspend is disabled, it may well be that the system stays up after the initial timeout. Again, that is exactly the same behavior that is observed today, but with this patch the system doesn't fail the initial suspend request because the requested timeout is not supported by the rtc driver.

Not sure if I should add some text along that line above for clarification. If so please let me know.

Quoted 13 lines

kernel/time/alarmtimer.c static int alarmtimer_suspend(struct device *dev) · line 304

Quoted 6 lines
kernel/time/alarmtimer.c +6 -0
+        * 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->alarm_offset_max &&
+           rtc->alarm_offset_max * MSEC_PER_SEC < ktime_to_ms(min))
+               min = ms_to_ktime(rtc->alarm_offset_max * MSEC_PER_SEC);

I don't really have an objection here, but I wonder if this would be better abstracted by a rtc_ function?

ktime_t rtc_bound_ktime_interval(ktime interval)
{
if (!rtc->alarm_offset_max)

Quoted 9 lines
      return interval;

return ms_to_ktime(min(rtc->alarm_offset_max, ktime_to_ms(interval)));
}

(simple enough to throw into rtc.h maybe as an inline function?)

Then the above would be tweaked to:
min = rtc_bound_interval(min);

How should I proceed ? Would sending two patches on top of patch 1 to introduce the API and use it be ok ?

Thanks,
Guenter

reviewer
#9 lore raw

Re: [PATCH v2 2/7] rtc: alarmtimer: Use maximum alarm time offset

In reply to Guenter Roeck

Quoting Guenter Roeck (2023-08-17 15:55:32)

Quoted 33 lines

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.

---
v2: Rename range_max_offset -> alarm_offset_max

 kernel/time/alarmtimer.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
kernel/time/alarmtimer.c +0 -0
index 8d9f13d847f0..895e3a6d6444 100644

kernel/time/alarmtimer.c static int alarmtimer_suspend(struct device *dev) · line 298

Quoted 9 lines
kernel/time/alarmtimer.c +6 -0
@@ -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.

Does this assume that the kernel is configured for autosuspend (CONFIG_PM_AUTOSLEEP)? Maybe we should only do this when that config is enabled.

If userspace is the one autosuspending, then I don't know what we do, or how the kernel knows it is OK. Maybe we need another alarmtimer clock id that will fail creation if the wakeup time is larger than what the rtc can be programmed for? Or maybe that new clock id can have this fixed behavior to wakeup early with the assumption that userspace will go back to sleep, and outdated userspace can use the original alarmtimer clock id if they don't care about suspend failing?

I see another problem too. What do we do if an alarmtimer is created, the rtc device is unregistered, and then we enter suspend? It looks like alarmtimer_suspend() bails out early with no error, so suspend continues. That looks wrong. Presumably we should fail suspend entirely at that point because we'll never be able to wakeup to run the alarmtimer.

#10 lore raw

Re: [PATCH v2 2/7] rtc: alarmtimer: Use maximum alarm time offset

Quoted 38 lines · Stephen Boyd

Quoting Guenter Roeck (2023-08-17 15:55:32)

Quoted 37 lines

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.

---
v2: Rename range_max_offset -> alarm_offset_max

  kernel/time/alarmtimer.c | 13 +++++++++++++
kernel/time/alarmtimer.c +2 -0
index 8d9f13d847f0..895e3a6d6444 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);
+
+       /*
Quoted 9 lines

kernel/time/alarmtimer.c static int alarmtimer_suspend(struct device *dev) · line 298

Quoted 4 lines
kernel/time/alarmtimer.c +4 -0
+        * 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.

Does this assume that the kernel is configured for autosuspend (CONFIG_PM_AUTOSLEEP)? Maybe we should only do this when that config is enabled.

It doesn't really assume anything. It standardizes behavior if the rtc does not support the requested alarm time. Today that either fails or the rtc silently adjusts the alarm time (sometimes to 1 day + 1 minute -> one minute) depending on the implementation in the rtc driver. With this patch in place, the the rtc driver informing the rtc core about the limit, the alarm would fire at the maximum time supported by the rtc if the requested alarm time is larger than its limit.

I see that as improvement, no matter if CONFIG_PM_AUTOSLEEP is enabled or not.

Quoted 8 lines

If userspace is the one autosuspending, then I don't know what we do, or how the kernel knows it is OK. Maybe we need another alarmtimer clock id that will fail creation if the wakeup time is larger than what the rtc can be programmed for? Or maybe that new clock id can have this fixed behavior to wakeup early with the assumption that userspace will go back to sleep, and outdated userspace can use the original alarmtimer clock id if they don't care about suspend failing?

I don't know how to answer this. Again, I see my suggested patch as improvement over not suspending at all or resuming at a more or less random time, which is what you get today depending on the rtc driver.

Actually, I would argue that the above situation applies even if the rtc supports the requested alarm time. Currently, if userspace is the one autosuspending, the system wakes up after the alarm time expires (assuming the rtc supports it). Then what ? Your above question applies to that situation as well and is really independent of the alarm time limit supported by the rtc.

I would agree that various improvements on how to handle the situation where the requested alarm time is larger than the rtc limit may be possible, but I see those as independent and orthogonal to this patch.

Quoted 6 lines

I see another problem too. What do we do if an alarmtimer is created, the rtc device is unregistered, and then we enter suspend? It looks like alarmtimer_suspend() bails out early with no error, so suspend continues. That looks wrong. Presumably we should fail suspend entirely at that point because we'll never be able to wakeup to run the alarmtimer.

Maybe I am missing something, but I think this is equivalent of not having an rtc in the system, or for CONFIG_RTC_CLASS=n. Currently the system just suspends without waking up in those situations. Changing that would be a substantial functional change since suddenly systems without rtc would simply fail to suspend if there is a pending alarm.

Thanks,
Guenter

reviewer
#11 lore raw

Re: [PATCH v2 2/7] rtc: alarmtimer: Use maximum alarm time offset

Quoting Guenter Roeck (2023-08-30 00:13:09)

Quoted 57 lines · Stephen Boyd
Quoted 38 lines

Quoting Guenter Roeck (2023-08-17 15:55:32)

Quoted 37 lines

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.

---
v2: Rename range_max_offset -> alarm_offset_max

  kernel/time/alarmtimer.c | 13 +++++++++++++
kernel/time/alarmtimer.c +2 -0
index 8d9f13d847f0..895e3a6d6444 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);
+
+       /*
Quoted 9 lines

kernel/time/alarmtimer.c static int alarmtimer_suspend(struct device *dev) · line 298

Quoted 4 lines
kernel/time/alarmtimer.c +4 -0
+        * 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.

Does this assume that the kernel is configured for autosuspend (CONFIG_PM_AUTOSLEEP)? Maybe we should only do this when that config is enabled.

It doesn't really assume anything. It standardizes behavior if the rtc does not support the requested alarm time. Today that either fails or the rtc silently adjusts the alarm time (sometimes to 1 day + 1 minute -> one minute) depending on the implementation in the rtc driver. With this patch in place, the the rtc driver informing the rtc core about the limit, the alarm would fire at the maximum time supported by the rtc if the requested alarm time is larger than its limit.

Quoted 1 line

I see that as improvement, no matter if CONFIG_PM_AUTOSLEEP is enabled or not.

Agreed. It's an improvement.

The ABI of alarmtimers seem to be "Run this timer at time X, and wake up the system from suspend if necessary to run the timer at time X".

Quoted 14 lines
Quoted 9 lines

If userspace is the one autosuspending, then I don't know what we do, or how the kernel knows it is OK. Maybe we need another alarmtimer clock id that will fail creation if the wakeup time is larger than what the rtc can be programmed for? Or maybe that new clock id can have this fixed behavior to wakeup early with the assumption that userspace will go back to sleep, and outdated userspace can use the original alarmtimer clock id if they don't care about suspend failing?

I don't know how to answer this. Again, I see my suggested patch as improvement over not suspending at all or resuming at a more or less random time, which is what you get today depending on the rtc driver.

Quoted 5 lines

Actually, I would argue that the above situation applies even if the rtc supports the requested alarm time. Currently, if userspace is the one autosuspending, the system wakes up after the alarm time expires (assuming the rtc supports it). Then what ? Your above question applies to that situation as well and is really independent of the alarm time limit supported by the rtc.

The comment in the code is causing me confusion. It says

The system will wake up earlier than necessary and is expected to go back to sleep if it has nothing to do.

I'd reword this to not talk about auto-suspend because the ABI of alarmtimers doesn't concern itself with autosuspend.

The system will wake up earlier (possibly much earlier) than when the alarmtimer runs. This is the best the kernel can do because the alarmtimer exceeds the time that the rtc device can be programmed for.

Quoted 4 lines

I would agree that various improvements on how to handle the situation where the requested alarm time is larger than the rtc limit may be possible, but I see those as independent and orthogonal to this patch.

I certainly hope that userspace isn't relying on the existing behavior.

Quoted 8 lines
Quoted 7 lines

I see another problem too. What do we do if an alarmtimer is created, the rtc device is unregistered, and then we enter suspend? It looks like alarmtimer_suspend() bails out early with no error, so suspend continues. That looks wrong. Presumably we should fail suspend entirely at that point because we'll never be able to wakeup to run the alarmtimer.

Quoted 5 lines

Maybe I am missing something, but I think this is equivalent of not having an rtc in the system, or for CONFIG_RTC_CLASS=n. Currently the system just suspends without waking up in those situations. Changing that would be a substantial functional change since suddenly systems without rtc would simply fail to suspend if there is a pending alarm.

We fail alarmtimer creation in the case that CONFIG_RTC_CLASS=n or when there isn't an rtc. See alarmtimer_get_rtcdev() and how it is called. I doubt it ever really happens in practice, but it looks possible to simulate by unbinding the rtc device driver.

#12 lore raw

Re: [PATCH v2 2/7] rtc: alarmtimer: Use maximum alarm time offset

Quoted 90 lines · Stephen Boyd

Quoting Guenter Roeck (2023-08-30 00:13:09)

Quoted 57 lines · Stephen Boyd
Quoted 38 lines

Quoting Guenter Roeck (2023-08-17 15:55:32)

Quoted 37 lines

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.

---
v2: Rename range_max_offset -> alarm_offset_max

   kernel/time/alarmtimer.c | 13 +++++++++++++
kernel/time/alarmtimer.c +2 -0
index 8d9f13d847f0..895e3a6d6444 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);
+
+       /*
Quoted 9 lines

kernel/time/alarmtimer.c static int alarmtimer_suspend(struct device *dev) · line 298

Quoted 4 lines
kernel/time/alarmtimer.c +4 -0
+        * 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.

Does this assume that the kernel is configured for autosuspend (CONFIG_PM_AUTOSLEEP)? Maybe we should only do this when that config is enabled.

It doesn't really assume anything. It standardizes behavior if the rtc does not support the requested alarm time. Today that either fails or the rtc silently adjusts the alarm time (sometimes to 1 day + 1 minute -> one minute) depending on the implementation in the rtc driver. With this patch in place, the the rtc driver informing the rtc core about the limit, the alarm would fire at the maximum time supported by the rtc if the requested alarm time is larger than its limit.

Quoted 1 line

I see that as improvement, no matter if CONFIG_PM_AUTOSLEEP is enabled or not.

Agreed. It's an improvement.

The ABI of alarmtimers seem to be "Run this timer at time X, and wake up the system from suspend if necessary to run the timer at time X".

Quoted 14 lines
Quoted 9 lines

If userspace is the one autosuspending, then I don't know what we do, or how the kernel knows it is OK. Maybe we need another alarmtimer clock id that will fail creation if the wakeup time is larger than what the rtc can be programmed for? Or maybe that new clock id can have this fixed behavior to wakeup early with the assumption that userspace will go back to sleep, and outdated userspace can use the original alarmtimer clock id if they don't care about suspend failing?

I don't know how to answer this. Again, I see my suggested patch as improvement over not suspending at all or resuming at a more or less random time, which is what you get today depending on the rtc driver.

Quoted 5 lines

Actually, I would argue that the above situation applies even if the rtc supports the requested alarm time. Currently, if userspace is the one autosuspending, the system wakes up after the alarm time expires (assuming the rtc supports it). Then what ? Your above question applies to that situation as well and is really independent of the alarm time limit supported by the rtc.

The comment in the code is causing me confusion. It says

The system will wake up earlier than necessary and is expected to go back to sleep if it has nothing to do.

Quoted 7 lines

I'd reword this to not talk about auto-suspend because the ABI of alarmtimers doesn't concern itself with autosuspend.

The system will wake up earlier (possibly much earlier) than when the alarmtimer runs. This is the best the kernel can do because the alarmtimer exceeds the time that the rtc device can be programmed for.

Makes sense, and I agree that this is much better. I changed the comment accordingly.

Quoted 21 lines
Quoted 4 lines

I would agree that various improvements on how to handle the situation where the requested alarm time is larger than the rtc limit may be possible, but I see those as independent and orthogonal to this patch.

I certainly hope that userspace isn't relying on the existing behavior.

Quoted 8 lines
Quoted 7 lines

I see another problem too. What do we do if an alarmtimer is created, the rtc device is unregistered, and then we enter suspend? It looks like alarmtimer_suspend() bails out early with no error, so suspend continues. That looks wrong. Presumably we should fail suspend entirely at that point because we'll never be able to wakeup to run the alarmtimer.

Quoted 5 lines

Maybe I am missing something, but I think this is equivalent of not having an rtc in the system, or for CONFIG_RTC_CLASS=n. Currently the system just suspends without waking up in those situations. Changing that would be a substantial functional change since suddenly systems without rtc would simply fail to suspend if there is a pending alarm.

Quoted 4 lines

We fail alarmtimer creation in the case that CONFIG_RTC_CLASS=n or when there isn't an rtc. See alarmtimer_get_rtcdev() and how it is called. I doubt it ever really happens in practice, but it looks possible to simulate by unbinding the rtc device driver.

Thanks for the clarification. That really makes me wonder what happens if an rtc device is unregistered. The .remove_dev callback of alarmtimer_rtc_interface is not populated, and rtc_dev is never cleared. That means unbinding an rtc device driver should result in a crash. Am I missing something ?

Thanks,
Guenter

reviewer
#13 lore raw

Re: [PATCH v2 2/7] rtc: alarmtimer: Use maximum alarm time offset

Quoting Guenter Roeck (2023-08-30 21:23:54)

Quoted 7 lines · Stephen Boyd
Quoted 5 lines

We fail alarmtimer creation in the case that CONFIG_RTC_CLASS=n or when there isn't an rtc. See alarmtimer_get_rtcdev() and how it is called. I doubt it ever really happens in practice, but it looks possible to simulate by unbinding the rtc device driver.

Quoted 6 lines

Thanks for the clarification. That really makes me wonder what happens if an rtc device is unregistered. The .remove_dev callback of alarmtimer_rtc_interface is not populated, and rtc_dev is never cleared. That means unbinding an rtc device driver should result in a crash. Am I missing something ?

Yeah it looks like a potential problem, but most likely nobody actually removes the rtc device from the system. It would be good to handle this case in a followup patch anyway though.

#14 lore raw

PATCH v2 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.

---
v2: Rename range_max_offset -> alarm_offset_max

 drivers/rtc/rtc-cros-ec.c | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)
drivers/rtc/rtc-cros-ec.c +23 -15
index 998ab8606f0b..0cd397c04ff0 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->alarm_offset_max = 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
#15 lore raw

PATCH v2 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.

---
v2: Rename range_max_offset -> alarm_offset_max

 drivers/rtc/rtc-cmos.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/rtc/rtc-cmos.c +11 -0
index c9416fe8542d..228fb2d11c70 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->alarm_offset_max = SECS_PER_YEAR - 1;
+	else if (cmos_rtc.day_alrm)
+		cmos_rtc.rtc->alarm_offset_max = SECS_PER_MONTH - 1;
+	else
+		cmos_rtc.rtc->alarm_offset_max = SECS_PER_DAY - 1;
+
 	rename_region(ports, dev_name(&cmos_rtc.rtc->dev));
 
 	if (!mc146818_does_rtc_work()) {
Signature · 1 line
2.39.2
#16 lore raw

PATCH v2 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.

---
v2: Rename range_max_offset -> alarm_offset_max

 drivers/rtc/rtc-tps6586x.c | 1 +
 1 file changed, 1 insertion(+)
drivers/rtc/rtc-tps6586x.c +1 -0
index 9f14e2475747..20faf08c254c 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->alarm_offset_max = 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
#17 lore raw

PATCH v2 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, and use the reported limit to validate the requested alarm time when setting it.

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.

---
v2: Rename range_max_offset -> alarm_offset_max
Use the new variable to validate the limit when setting the alarm

 drivers/rtc/rtc-ds1305.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/rtc/rtc-ds1305.c +3 -2
index ed9360486953..d4de401548b4 100644
@@ -336,8 +336,8 @@ static int ds1305_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
 	/* make sure alarm fires within the next 24 hours */
 	if (later <= now)
 		return -EINVAL;
-	if ((later - now) > 24 * 60 * 60)
-		return -EDOM;
+	if ((later - now) > ds1305->rtc->alarm_offset_max)
+		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->alarm_offset_max = 24 * 60 * 60;
 
 	ds1305_nvmem_cfg.priv = ds1305;
 	status = devm_rtc_register_device(ds1305->rtc);
Signature · 1 line
2.39.2
#18 lore raw

PATCH v2 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 and use the reported limit to validate the requested alarm time when setting it.

---
v2: Rename range_max_offset -> alarm_offset_max
Use the new variable to validate the limit when setting the alarm

 drivers/rtc/rtc-rzn1.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/rtc/rtc-rzn1.c +2 -1
index dca736caba85..c0f448bce145 100644
@@ -227,7 +227,7 @@ static int rzn1_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 		return ret;
 
 	/* We cannot set alarms more than one week ahead */
-	farest = rtc_tm_to_time64(&tm_now) + (7 * 86400);
+	farest = rtc_tm_to_time64(&tm_now) + rtc->rtcdev->alarm_offset_max;
 	alarm = rtc_tm_to_time64(tm);
 	if (time_after(alarm, farest))
 		return -ERANGE;
@@ -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->alarm_offset_max = 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 v2 7/7
#19 lore raw

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

Hi Guenter,

[email protected] wrote on Thu, 17 Aug 2023 15:55:37 -0700:

Quoted 6 lines

RZN1 only supports alarms up to one week in the future. Report the limit to the RTC core and use the reported limit to validate the requested alarm time when setting it.

Thanks,
Miquèl

maintainer Applied
#20 lore raw

Re: (subset) [PATCH v2 0/7] rtc: Add support for limited alarm timer offsets

In reply to Guenter Roeck
Quoted 14 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.

Quoted 1 line

[...]

Applied, thanks!

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

      commit: 781589e40ac5f929f58824c15448e1ba49c3ac32
[3/7] rtc: cros-ec: Detect and report supported alarm window size
      commit: 00c3092d881bc9d63dc36eecd140cdb38962c7ec
[4/7] rtc: cmos: Report supported alarm limit to rtc infrastructure
      commit: 2546e7083f2ea96bdd6157961dc2748d65a9e487
[5/7] rtc: tps6586x: Report maximum alarm limit to rtc core
      commit: 3637bbdc8a446b8edb369383d2abc816c96ee864
[6/7] rtc: ds1305: Report maximum alarm limit to rtc core
      commit: 46b79ac0b463e155b098805ff66f1f22ff249b45
[7/7] rtc: rzn1: Report maximum alarm limit to rtc core
      commit: 2b0386d578836b9cd5d2e63cff38b7229c319e4a

Best regards,

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com