Skip to contentSkip to conversation

alarmtimer: Fix bug where relative alarm timers were treated as absolute

PATCH In review John Stultz 3 participants 4 messages Jul 7, 2014 – Jul 8, 2014

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

PATCH 1/1 alarmtimer: Fix bug where relative alarm timers were treated as absolute

Sharvil noticed with the posix timer_settime interface, using the CLOCK_REALTIME_ALARM or CLOCK_BOOTTIME_ALARM clockid, if the users tried to specify a relative time timer, it would incorrectly be treated as absolute regardless of the state of the flags argument.

This patch corrects this, properly checking the absolute/relative flag, as well as adds further error checking that no invalid flag bits are set.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Sharvil Nanavati <[email protected]>
Cc stable <[email protected]> #3.0+

---

Thomas, Ingo: Please consider for tip/timers/urgent for 3.16

 kernel/time/alarmtimer.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
kernel/time/alarmtimer.c +18 -2
index 88c9c65..fe75444 100644
@@ -585,9 +585,14 @@ static int alarm_timer_set(struct k_itimer *timr, int flags,
 				struct itimerspec *new_setting,
 				struct itimerspec *old_setting)
 {
+	ktime_t exp;
+
 	if (!rtcdev)
 		return -ENOTSUPP;
 
+	if (flags & ~TIMER_ABSTIME)
+		return -EINVAL;
+
 	if (old_setting)
 		alarm_timer_get(timr, old_setting);
 
@@ -597,8 +602,16 @@ static int alarm_timer_set(struct k_itimer *timr, int flags,
 
 	/* start the timer */
 	timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
-	alarm_start(&timr->it.alarm.alarmtimer,
-			timespec_to_ktime(new_setting->it_value));
+	exp = timespec_to_ktime(new_setting->it_value);
+	/* Convert (if necessary) to absolute time */
+	if (flags != TIMER_ABSTIME) {
+		ktime_t now;
+
+		now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
+		exp = ktime_add(now, exp);
+	}
+
+	alarm_start(&timr->it.alarm.alarmtimer, exp);
 	return 0;
 }
 
@@ -730,6 +743,9 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
 	if (!alarmtimer_get_rtcdev())
 		return -ENOTSUPP;
 
+	if (flags & ~TIMER_ABSTIME)
+		return -EINVAL;
+
 	if (!capable(CAP_WAKE_ALARM))
 		return -EPERM;
 
Signature · 1 line
1.9.1
#1 lore raw
Quoted 6 lines · John Stultz
kernel/time/alarmtimer.c +0 -2
@@ -597,8 +602,16 @@ static int alarm_timer_set(struct k_itimer *timr, int flags,
 
 	/* start the timer */
 	timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
-	alarm_start(&timr->it.alarm.alarmtimer,
-			timespec_to_ktime(new_setting->it_value));

kernel/time/alarmtimer.c static int alarm_timer_set(struct k_itimer *timr, int flags, · line 614

Quoted 10 lines
kernel/time/alarmtimer.c +10 -0
+	exp = timespec_to_ktime(new_setting->it_value);
+	/* Convert (if necessary) to absolute time */
+	if (flags != TIMER_ABSTIME) {
+		ktime_t now;
+
+		now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
+		exp = ktime_add(now, exp);
+	}
+
+	alarm_start(&timr->it.alarm.alarmtimer, exp);

Nothing protects 'exp' from becoming invalid before queuing the alarm, if the time base is reset on another cpu. Or would that be harmless here?

kernel/time/alarmtimer.c static int alarm_timer_set(struct k_itimer *timr, int flags, · line 616

Quoted 2 lines
kernel/time/alarmtimer.c +0 -0
 	return 0;
 }

Thanks,
Richard

#2 lore raw
Quoted 13 lines · Richard Cochran
Quoted 12 lines · John Stultz
kernel/time/alarmtimer.c +6 -2
@@ -597,8 +602,16 @@ static int alarm_timer_set(struct k_itimer *timr, int flags,
 
      /* start the timer */
      timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
-     alarm_start(&timr->it.alarm.alarmtimer,
-                     timespec_to_ktime(new_setting->it_value));
+     exp = timespec_to_ktime(new_setting->it_value);
+     /* Convert (if necessary) to absolute time */
+     if (flags != TIMER_ABSTIME) {
+             ktime_t now;
+
+             now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
Quoted 8 lines

kernel/time/alarmtimer.c static int alarm_timer_set(struct k_itimer *timr, int flags, · line 614

Quoted 4 lines
kernel/time/alarmtimer.c +4 -0
+             exp = ktime_add(now, exp);
+     }
+
+     alarm_start(&timr->it.alarm.alarmtimer, exp);

Nothing protects 'exp' from becoming invalid before queuing the alarm, if the time base is reset on another cpu. Or would that be harmless here?

Hrmn.. So that's a separate question, but a good one to validate as well.

common_timer_set() has a similar behavior where the return value of hrtimer_start_expires() isn't passed up. This is because the userspace behavior is that if the time has already passed, the timer should fire immediately.

If you look in __hrtimer_start_range_ns(), you'll see the chunk of logic at the bottom which (if I'm reading it right) raises the softirq (to fire the timer) if our timer is the earliest and enqueue_reprogram fails (due to the clockevent logic returning ETIME due to the time being in the past).

So its definitely subtle but looks like its ok. But I'll add a validation test to be sure I'm not missing anything there as well.

thanks
-john

#3 lore raw
In reply to John Stultz
alarmtimer: Fix bug where relative alarm timers were treated as absolute
Commit-ID:  16927776ae757d0d132bdbfabbfe2c498342bd59
Gitweb:     http://git.kernel.org/tip/16927776ae757d0d132bdbfabbfe2c498342bd59
Author:     John Stultz <[email protected]>
AuthorDate: Mon, 7 Jul 2014 14:06:11 -0700
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 8 Jul 2014 10:49:36 +0200

alarmtimer: Fix bug where relative alarm timers were treated as absolute

Sharvil noticed with the posix timer_settime interface, using the CLOCK_REALTIME_ALARM or CLOCK_BOOTTIME_ALARM clockid, if the users tried to specify a relative time timer, it would incorrectly be treated as absolute regardless of the state of the flags argument.

This patch corrects this, properly checking the absolute/relative flag, as well as adds further error checking that no invalid flag bits are set.

---
 kernel/time/alarmtimer.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
kernel/time/alarmtimer.c +18 -2
index 88c9c65..fe75444 100644
@@ -585,9 +585,14 @@ static int alarm_timer_set(struct k_itimer *timr, int flags,
 				struct itimerspec *new_setting,
 				struct itimerspec *old_setting)
 {
+	ktime_t exp;
+
 	if (!rtcdev)
 		return -ENOTSUPP;
 
+	if (flags & ~TIMER_ABSTIME)
+		return -EINVAL;
+
 	if (old_setting)
 		alarm_timer_get(timr, old_setting);
 
@@ -597,8 +602,16 @@ static int alarm_timer_set(struct k_itimer *timr, int flags,
 
 	/* start the timer */
 	timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
-	alarm_start(&timr->it.alarm.alarmtimer,
-			timespec_to_ktime(new_setting->it_value));
+	exp = timespec_to_ktime(new_setting->it_value);
+	/* Convert (if necessary) to absolute time */
+	if (flags != TIMER_ABSTIME) {
+		ktime_t now;
+
+		now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
+		exp = ktime_add(now, exp);
+	}
+
+	alarm_start(&timr->it.alarm.alarmtimer, exp);
 	return 0;
 }
 
@@ -730,6 +743,9 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
 	if (!alarmtimer_get_rtcdev())
 		return -ENOTSUPP;
 
+	if (flags & ~TIMER_ABSTIME)
+		return -EINVAL;
+
 	if (!capable(CAP_WAKE_ALARM))
 		return -EPERM;