RE: [PATCH v2] alarmtimer: fix unexpected rtc interrupt when system resume from S3

From: Lee, Zhuo-hao
Date: Sun Nov 08 2015 - 21:26:25 EST

Hi tglx & all,

I wrote a simple program which can always hit this problem.
Please use gcc to compile the following code and then "./a.out 10000 1" and watch the /proc/interrupts,
and you will see that the RTC interrupt will come unexpectedly after system resumed.
This abnormal interrupt should be removed. I tried on the latest linux kernel,
the bug still exists. So, could you please help to review this patch? Or give me some feedback?

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/timerfd.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>

#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)

int main(int argc, char *argv[])
struct itimerspec new_value;
int fd, sec, enable_suspend;
struct timespec now;
uint64_t exp;
ssize_t s;
pid_t pid;

if (argc != 3) {
fprintf(stderr, "%s secs enable_suspend\n",argv[0]);

sec = atoi(argv[1]);
enable_suspend = atoi(argv[2]);

if (clock_gettime(CLOCK_REALTIME, &now) == -1)

/* Create a CLOCK_REALTIME absolute timer with initial
expiration and interval as specified in command line */

new_value.it_value.tv_sec = now.tv_sec + sec;
new_value.it_value.tv_nsec = now.tv_nsec;
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_nsec = 0;

fd = timerfd_create(CLOCK_REALTIME_ALARM, 0);
if (fd == -1)

if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1)

system("cat /proc/driver/rtc | grep rtc_time");
system("cat /proc/interrupts | grep -E 'CPU0|rtc0'");
printf("\n\ntimer started: wait %d seconds\n\n", sec);
pid = fork();
if (pid == -1) {
fprintf(stderr, "fork failed\n");
} else if (pid == 0) {
/* for chromeOS */
// system("echo 0 > /sys/class/rtc/rtc0/wakealarm");
// system("echo +10 > /sys/class/rtc/rtc0/wakealarm");
// system("powerd_dbus_suspend");

/* for Ubuntu */
system("sudo pm-suspend");
} else {
s = read(fd, &exp, sizeof(uint64_t));
if (s != sizeof(uint64_t))

system("cat /proc/driver/rtc |grep rtc_time");
system("cat /proc/interrupts | grep -E 'CPU0|rtc0'");


Lee, Zhuo-hao

-----Original Message-----
From: Lee, Zhuo-hao
Sent: Thursday, November 5, 2015 1:50 PM
To: linux-kernel@xxxxxxxxxxxxxxx
Cc: Thomas Gleixner; zhuohao.lee82@xxxxxxxxx; Lee, Zhuo-hao
Subject: [PATCH v2] alarmtimer: fix unexpected rtc interrupt when system resume from S3

From: zhuo-hao <zhuo-hao.lee@xxxxxxxxx>

Before the system go to suspend (S3), if user create a timer with clockid CLOCK_REALTIME_ALARM/CLOCK_BOOTTIME_ALARM and set a "large" timeout value to this timer. The function alarmtimer_suspend will be called to setup a timeout value to RTC timer to avoid the system sleep over time. However, if the system wakeup early than RTC timeout, the RTC timer will not be cleared.
And this will cause the hpet_rtc_interrupt come unexpectedly until the RTC timeout. To fix this problem, just adding alarmtimer_resume to cancel the RTC timer.

Signed-off-by: Zhuo-hao Lee <zhuo-hao.lee@xxxxxxxxx>
kernel/time/alarmtimer.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c index 7fbba63..e840ed8 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -271,11 +271,27 @@ static int alarmtimer_suspend(struct device *dev)
__pm_wakeup_event(ws, MSEC_PER_SEC);
return ret;
+static int alarmtimer_resume(struct device *dev) {
+ struct rtc_device *rtc;
+ rtc = alarmtimer_get_rtcdev();
+ if (rtc)
+ rtc_timer_cancel(rtc, &rtctimer);
+ return 0;
static int alarmtimer_suspend(struct device *dev) {
return 0;
+static int alarmtimer_resume(struct device *dev) {
+ return 0;

static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type) @@ -800,6 +816,7 @@ out:
/* Suspend hook structures */
static const struct dev_pm_ops alarmtimer_pm_ops = {
.suspend = alarmtimer_suspend,
+ .resume = alarmtimer_resume,

static struct platform_driver alarmtimer_driver = {

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at