Posix Alarm Timers
cover Posix Alarm Timers
Just wanted to send out my current Posix Alarm Timers queue for comment and review in the hopes of getting it into shape for 2.6.40.
Some background on the patches can be found here:
https://lwn.net/Articles/429925/
New in this series:
* Added timekeeping_inject_sleeptime, which corrects CLOCK_BOOTTIME for arches that don't support read_persistent_clock().
* I've added a CAP_WAKE_ALARM capability to limit random applications from setting alarms in the last patch. This probably needs some careful review as it allows clock_nanosleep and timer_create to return -EPERM, which is new behavior.
thanks
-john
John Stultz (4):
time: Add timekeeping_inject_sleeptime
timers: Add rb_init_node() to allow for stack allocated rb nodes
timers: Introduce in-kernel alarm-timer interface
timers: posix interface for alarm-timers
drivers/rtc/class.c | 23 +- include/linux/alarmtimer.h | 30 ++ include/linux/capability.h | 7 +- include/linux/posix-timers.h | 2 + include/linux/rbtree.h | 8 + include/linux/time.h | 3 + include/linux/timerqueue.h | 2 +- kernel/time/Makefile | 2 +- kernel/time/alarmtimer.c | 705 ++++++++++++++++++++++++++++++++++++++++++ kernel/time/timekeeping.c | 56 ++++- 10 files changed, 818 insertions(+), 20 deletions(-) create mode 100644 include/linux/alarmtimer.h create mode 100644 kernel/time/alarmtimer.c
Signature · 1 line
1.7.3.2.146.gca209
PATCH 1/4 time: Add timekeeping_inject_sleeptime
Some platforms cannot implement read_persistent_clock, as their RTC devices are only accessible when interrupts are enabled. This keeps them from being used by the timekeeping code on resume to measure the time in suspend.
The RTC layer tries to work around this, by calling do_settimeofday on resume after irqs are reenabled to set the time properly. However, this only corrects CLOCK_REALTIME, and does not properly adjust the sleep time value. This causes btime in /proc/stat to be incorrect as well as making the new CLOCK_BOTTTIME inaccurate.
This patch resolves the issue by introducing a new timekeeping hook to allow the RTC layer to inject the sleep time on resume.
The code also checks to make sure that read_persistent_clock is nonfunctional before setting the sleep time, so that should the RTC's HCTOSYS option be configured in on a system that does support read_persistent_clock we will not increase the total_sleep_time twice.
--- drivers/rtc/class.c | 23 +++++++----------- include/linux/time.h | 1 + kernel/time/timekeeping.c | 56 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 17 deletions(-)
drivers/rtc/class.c +9 -14
@@ -41,26 +41,21 @@ static void rtc_device_release(struct device *dev)* system's wall clock; restore it on resume(). */ -static struct timespec delta;static time_t oldtime; +static struct timespec oldts;static int rtc_suspend (struct device *dev,pm_message_t mesg) {struct rtc_device *rtc =to_rtc_device (dev);struct rtc_time tm; -struct timespec ts =current_kernel_time ();if (strcmp (dev_name (&rtc->dev),CONFIG_RTC_HCTOSYS_DEVICE ) !=0 )return 0 ;rtc_read_time (rtc, &tm); +ktime_get_ts (&oldts);rtc_tm_to_time (&tm, &oldtime); -/* RTC precision is 1 second; adjust delta for avg 1/2 sec err */ -set_normalized_timespec (&delta, - ts.tv_sec - oldtime, - ts.tv_nsec - (NSEC_PER_SEC >>1 )); -return 0 ; }
@@ -70,10 +65,12 @@ static int rtc_resume(struct device *dev)struct rtc_time tm;time_t newtime;struct timespec time; +struct timespec newts;if (strcmp (dev_name (&rtc->dev),CONFIG_RTC_HCTOSYS_DEVICE ) !=0 )return 0 ; +ktime_get_ts (&newts);rtc_read_time (rtc, &tm);if (rtc_valid_tm (&tm) !=0 ) {pr_debug ("%s: bogus resume time\n" ,dev_name (&rtc->dev));
@@ -85,15 +82,13 @@ static int rtc_resume(struct device *dev)pr_debug ("%s: time travel!\n" ,dev_name (&rtc->dev));return 0 ; } +/* calculate the RTC time delta */ +set_normalized_timespec (&time, newtime - oldtime,0 ); -/* restore wall clock using delta against this RTC; -* adjust again for avg 1/2 second RTC sampling error -*/ -set_normalized_timespec (&time, - newtime + delta.tv_sec, - (NSEC_PER_SEC >>1 ) + delta.tv_nsec); -do_settimeofday (&time); +/* subtract kernel time between rtc_suspend to rtc_resume */ + time =timespec_sub (time,timespec_sub (newts, oldts)); +timekeeping_inject_sleeptime (&time);return 0 ; }
include/linux/time.h +1 -0
@@ -126,6 +126,7 @@ struct timespec __current_kernel_time(void); /* does not take xtime_lock */struct timespec get_monotonic_coarse (void );void get_xtime_and_monotonic_and_sleep_offset (struct timespec *xtim,struct timespec *wtom,struct timespec *sleep); +void timekeeping_inject_sleeptime (struct timespec *delta);#define CURRENT_TIME (current_kernel_time())#define CURRENT_TIME_SEC ((struct timespec) { get_seconds(), 0 })
kernel/time/timekeeping.c +53 -3
68 lines not on this page · raw message
Signature · 1 line
1.7.3.2.146.gca209
PATCH 2/4 timers: Add rb_init_node() to allow for stack allocated rb nodes
In reply to John StultzIn cases where a timerqueue_node or some structure that utilizes a timerqueue_node is allocated on the stack, gcc would give warnings caused by the timerqueue_init()'s calling RB_CLEAR_NODE, which self-references the nodes uninitialized data.
The solution is to create an rb_init_node() function that zeros the rb_node structure out and then calls RB_CLEAR_NODE(), and then call the new init function from timerqueue_init().
--- include/linux/rbtree.h | 8 ++++++++ include/linux/timerqueue.h | 2 +- 2 files changed, 9 insertions(+), 1 deletions(-)
include/linux/rbtree.h +8 -0
@@ -136,6 +136,14 @@ static inline void rb_set_color(struct rb_node *rb, int color)#define RB_EMPTY_NODE (node) (rb_parent(node) == node)#define RB_CLEAR_NODE (node) (rb_set_parent(node, node)) +static inline void rb_init_node (struct rb_node *rb) +{ + rb->rb_parent_color =0 ; + rb->rb_right =NULL ; + rb->rb_left =NULL ; +RB_CLEAR_NODE (rb); +} +extern void rb_insert_color (struct rb_node *,struct rb_root *);extern void rb_erase (struct rb_node *,struct rb_root *);
include/linux/timerqueue.h +1 -1
@@ -39,7 +39,7 @@ struct timerqueue_node *timerqueue_getnext(struct timerqueue_head *head)static inline void timerqueue_init (struct timerqueue_node *node) { -RB_CLEAR_NODE (&node->node); +rb_init_node (&node->node); }static inline void timerqueue_init_head (struct timerqueue_head *head)
Signature · 1 line
1.7.3.2.146.gca209
PATCH 3/4 timers: Introduce in-kernel alarm-timer interface
In reply to John StultzThis provides the in kernel interface and infrastructure for alarm-timers.
Alarm-timers are a hybrid style timer, similar to hrtimers, but when the system is suspended, the RTC device is set to fire and wake the system for when the soonest alarm-timer expires.
The concept for Alarm-timers was inspired by the Android Alarm driver (by Arve Hjønnevåg) found in the Android kernel tree.
This in-kernel interface should be fairly compatible with the Android alarm driver in-kernel interface, but has the advantage of utilizing the new RTC timerqueue code instead of doing direct RTC manipulation.
--- include/linux/alarmtimer.h | 30 ++++ kernel/time/Makefile | 2 +- kernel/time/alarmtimer.c | 375 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 406 insertions(+), 1 deletions(-) create mode 100644 include/linux/alarmtimer.h create mode 100644 kernel/time/alarmtimer.c
include/linux/alarmtimer.h new file +30 -0
@@ -0,0 +1,30 @@ +#ifndef _LINUX_ALARMTIMER_H +#define _LINUX_ALARMTIMER_H + +#include <linux/time.h> +#include <linux/hrtimer.h> +#include <linux/timerqueue.h> +#include <linux/rtc.h> + +enum alarmtimer_type { +ALARM_REALTIME , +ALARM_BOOTTIME , + +ALARM_NUMTYPE , +}; + +struct alarm { +struct timerqueue_node node; +ktime_t period; +void (*function)(struct alarm *); +enum alarmtimer_type type; +char enabled; +void *data; +}; + +void alarm_init (struct alarm *alarm,enum alarmtimer_type type, +void (*function)(struct alarm *)); +void alarm_start (struct alarm *alarm,ktime_t start,ktime_t period); +void alarm_cancel (struct alarm *alarm); + +#endif
kernel/time/Makefile +1 -1
@@ -1,5 +1,5 @@ obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o -obj-y += timeconv.o posix-clock.o +obj-y += timeconv.o posix-clock.o alarmtimer.o obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o
kernel/time/alarmtimer.c new file +375 -0
375 lines not on this page · raw message
Signature · 1 line
1.7.3.2.146.gca209
PATCH 4/4 timers: posix interface for alarm-timers
In reply to John StultzThis patch exposes alarm-timers to userland via the posix clock and timers interface, using two new clockids: CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM. Both clockids behave identically to CLOCK_REALTIME and CLOCK_BOOTTIME, respectively, but timers set against the _ALARM suffixed clockids will wake the system if it is suspended.
Some background can be found here:
https://lwn.net/Articles/429925/
The concept for Alarm-timers was inspired by the Android Alarm driver (by Arve Hjønnevåg) found in the Android kernel tree.
While the in-kernel interface is pretty similar between alarm-timers and Android alarm driver, the user-space interface for the Android alarm driver is via ioctls to a new char device. As mentioned above, I've instead chosen to export this functionality via the posix interface, as it seemed a little simpler and avoids creating duplicate interfaces to things like CLOCK_REALTIME and CLOCK_MONOTONIC under alternate names (ie:ANDROID_ALARM_RTC and ANDROID_ALARM_SYSTEMTIME).
The semantics of the Android alarm driver are different from what this posix interface provides. For instance, threads other then the thread waiting on the Android alarm driver are able to modify the alarm being waited on. Also this interface does not allow the same wakelock semantics that the Android driver provides (ie: kernel takes a wakelock on RTC alarm-interupt, and holds it through process wakeup, and while the process runs, until the process either closes the char device or calls back in to wait on a new alarm).
One potential way to implement similar semantics may be via the timerfd infrastructure, but this needs more research.
There may also need to be some sort of sysfs system level policy hooks that allow alarm timers to be disabled to keep them from firing at inappropriate times (ie: laptop in a well insulated bag, mid-flight).
--- include/linux/capability.h | 7 +- include/linux/posix-timers.h | 2 + include/linux/time.h | 2 + kernel/time/alarmtimer.c | 330 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 340 insertions(+), 1 deletions(-)
include/linux/capability.h +6 -1
@@ -355,7 +355,12 @@ struct cpu_vfs_cap_data {#define CAP_SYSLOG 34 -#define CAP_LAST_CAP CAP_SYSLOG +/* Allow triggering something that will wake the system */ + +#define CAP_WAKE_ALARM 35 + + +#define CAP_LAST_CAP CAP_WAKE_ALARM#define cap_valid (x) ((x) >= 0 && (x) <= CAP_LAST_CAP)
include/linux/posix-timers.h +2 -0
@@ -5,6 +5,7 @@#include <linux/list.h> #include <linux/sched.h> #include <linux/timex.h> +#include <linux/alarmtimer.h> union cpu_time_count {cputime_t cpu;
@@ -80,6 +81,7 @@ struct k_itimer {unsigned long incr;unsigned long expires; } mmtimer; +struct alarm alarmtimer; } it; };
include/linux/time.h +2 -0
@@ -295,6 +295,8 @@ struct itimerval {#define CLOCK_REALTIME_COARSE 5#define CLOCK_MONOTONIC_COARSE 6#define CLOCK_BOOTTIME 7 +#define CLOCK_REALTIME_ALARM 8 +#define CLOCK_BOOTTIME_ALARM 9/* * The IDs of various hardware clocks:
kernel/time/alarmtimer.c +330 -0
348 lines not on this page · raw message
Signature · 1 line
1.7.3.2.146.gca209
Re: [PATCH 4/4] timers: posix interface for alarm-timers
Quoted 6 lines · John Stultz
This patch exposes alarm-timers to userland via the posix clock and timers interface, using two new clockids:
CLOCK_REALTIME_ALARMandCLOCK_BOOTTIME_ALARM. Both clockids behave identically toCLOCK_REALTIMEandCLOCK_BOOTTIME, respectively, but timers set against the_ALARMsuffixed clockids will wake the system if it is suspended.
So, is there reason these cannot or should not be using the dynamic posix clock interface?
Quoted 2 lines
The semantics of the Android alarm driver are different from what this posix interface provides.
...
Quoted 7 lines
One potential way to implement similar semantics may be via the timerfd infrastructure, but this needs more research.
There may also need to be some sort of sysfs system level policy hooks that allow alarm timers to be disabled to keep them from firing at inappropriate times (ie: laptop in a well insulated bag, mid-flight).
I have not put hardly any thought into these issues, but the dynamic interface gives you a character device at no extra charge.
Richard
Re: [PATCH 4/4] timers: posix interface for alarm-timers
Quoted 10 lines · Richard Cochran
Quoted 6 lines · John Stultz
This patch exposes alarm-timers to userland via the posix clock and timers interface, using two new clockids:
CLOCK_REALTIME_ALARMandCLOCK_BOOTTIME_ALARM. Both clockids behave identically toCLOCK_REALTIMEandCLOCK_BOOTTIME, respectively, but timers set against the_ALARMsuffixed clockids will wake the system if it is suspended.So, is there reason these cannot or should not be using the dynamic posix clock interface?
So in this case, since the clockids are not directly linked to specific dynamic hardware, I'm not sure if that's the best route.
Quoted 6 lines
Quoted 2 lines
The semantics of the Android alarm driver are different from what this posix interface provides.
...
Quoted 3 lines
One potential way to implement similar semantics may be via the timerfd infrastructure, but this needs more research.
Quoted 7 lines
Quoted 4 lines
There may also need to be some sort of sysfs system level policy hooks that allow alarm timers to be disabled to keep them from firing at inappropriate times (ie: laptop in a well insulated bag, mid-flight).
I have not put hardly any thought into these issues, but the dynamic interface gives you a character device at no extra charge.
True, but I'd like to first try to extend the existing interface (timerfd) before we look at adding new ones.
thanks
-john
Quoted 8 lines · On Wednesday 20 April 2011 01:58:26 John Stultz wrote:
Just wanted to send out my current Posix Alarm Timers queue for comment and review in the hopes of getting it into shape for 2.6.40.
Some background on the patches can be found here:
https://lwn.net/Articles/429925/
Quoted 8 lines
New in this series:
* Addedtimekeeping_inject_sleeptime, which correctsCLOCK_BOOTTIMEfor arches that don't supportread_persistent_clock().
* I've added aCAP_WAKE_ALARMcapability to limit random applications from setting alarms in the last patch. This probably needs some careful review as it allowsclock_nanosleepandtimer_createto return-EPERM, which is new behavior.
I haven't looked at these patches much before, so I've done a brief review now. They look very nice, and I can't find anything to complain about.