Skip to contentSkip to conversation

Posix interface for RTC (v2)

PATCH In review John Stultz 2 participants 10 messages Nov 3, 2010 – Nov 4, 2010

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

cover Posix interface for RTC (v2)

From: John Stultz <[email protected]>

So I think this patch set is starting to shape up, and it has been functioning pretty well in my testing, so I wanted to send it out again for review.

The first two patches lay groundwork, pulling the timerlist infrastructure out of hrtimers and into common code.

The next two patches rework the RTC code, so the interface is a little more abstracted away from the hardware, allowing us to multiplex timers from different applications ontop of a single RTC device, and cleaning up the now redundant older emulation layer.

This is where the patchset diverges into two paths:

The next two patches provide a posix interface layer ontop of the RTC device. One of the patches, provided by Richard Cochran, allows for dynamic posix clock registration and is likely to be reworked here shortly. The other implements the posix interface. This interface exposes the actual RTC time and allows timers to be set against that time domain, which may not stay in sync with the kernel's system time. It also allows wakeup timers to be triggered in the future when the machine may be suspended.

The last patch takes a different approach. It too provides a posix interface that can be used to trigger wakeup alarms when the machine is suspended, but it uses a bit of a hybrid method, basing timers off the system time clock ids (ie:
CLOCK_REALTIME/MONOTONIC) while the system is running, and then setting the RTC to fire only when we go to suspend. This hybrid approach was inspired by the Android Alarm driver, which uses this same method, but exposes the interface to userland via ioctl device instead of the posix interface.

I wanted to send out both of these methods to get some feedback on which direction (or possibly both) people would prefer to see.

Any other feedback or comments would also be appreciated!

thanks
-john

CC Alessandro Zummo <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Richard Cochran <[email protected]>
CC Arve Hjønnevåg <[email protected]>

John Stultz (6):
[RFC] Introduce timerlist infrastructure.
[RFC] hrtimers: Convert hrtimers to use timerlist infrastructure [RFC] RTC: Rework RTC code to use timerlist for events [RFC] RTC: Remove UIE emulation
[RFC] RTC: Add posix clock/timer interface
[RFC] Introduce Alarm (hybrid) timers

Richard Cochran (1):
[RFC] posix clocks: dynamic clock ids.

 drivers/rtc/Makefile         |    2 +-
 drivers/rtc/class.c          |   15 +
 drivers/rtc/interface.c      |  579 ++++++++++++++++++++++++++----------------
 drivers/rtc/posix.c          |  258 +++++++++++++++++++
 drivers/rtc/rtc-dev.c        |  104 --------
 drivers/rtc/rtc-lib.c        |   26 ++
 drivers/rtc/rtc-sysfs.c      |   10 +
 include/linux/alarmtimer.h   |   29 ++
 include/linux/hrtimer.h      |   32 +--
 include/linux/posix-timers.h |   11 +-
 include/linux/rtc.h          |   52 +++-
 include/linux/time.h         |    4 +
 include/linux/timerlist.h    |   37 +++
 kernel/hrtimer.c             |   86 +++----
 kernel/posix-timers.c        |   41 +++-
 kernel/time/Makefile         |    2 +-
 kernel/time/alarmtimer.c     |  522 +++++++++++++++++++++++++++++++++++++
 kernel/time/timer_list.c     |    8 +-
 lib/Makefile                 |    2 +-
 lib/timerlist.c              |  115 +++++++++
 20 files changed, 1503 insertions(+), 432 deletions(-)
 create mode 100644 drivers/rtc/posix.c
 create mode 100644 include/linux/alarmtimer.h
 create mode 100644 include/linux/timerlist.h
 create mode 100644 kernel/time/alarmtimer.c
 create mode 100644 lib/timerlist.c
Signature · 1 line
1.7.3.2.146.gca209
#1 lore raw

RFC PATCH 1/7 Introduce timerlist infrastructure.

The timerlist infrastructure is a thin layer over the rbtree code that implements a simple list of timers sorted by an expires value, and a getnext function that provides a pointer to the earliest timer.

This infrastructure allows drivers and other kernel infrastructure to easily implement timers without duplicating code.

Suggestions or feedback would be appreciated.

CC Alessandro Zummo <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Richard Cochran <[email protected]>
---
 include/linux/timerlist.h |   37 ++++++++++++++
 lib/Makefile              |    2 +-
 lib/timerlist.c           |  115 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 153 insertions(+), 1 deletions(-)
 create mode 100644 include/linux/timerlist.h
 create mode 100644 lib/timerlist.c
include/linux/timerlist.h new file +37 -0
new file mode 100644
index 0000000..c46b28a
@@ -0,0 +1,37 @@
+#ifndef _LINUX_TIMERLIST_H
+#define _LINUX_TIMERLIST_H
+
+#include <linux/rbtree.h>
+#include <linux/ktime.h>
+
+
+struct timerlist_node {
+	struct rb_node node;
+	ktime_t expires;
+};
+
+struct timerlist_head {
+	struct rb_root head;
+	struct timerlist_node *next;
+};
+
+
+extern void timerlist_add(struct timerlist_head *head,
+				struct timerlist_node *node);
+extern void timerlist_del(struct timerlist_head *head,
+				struct timerlist_node *node);
+extern struct timerlist_node *timerlist_getnext(struct timerlist_head *head);
+extern struct timerlist_node *timerlist_iterate_next(
+						struct timerlist_node *node);
+
+static inline void timerlist_init(struct timerlist_node *node)
+{
+	RB_CLEAR_NODE(&node->node);
+}
+
+static inline void timerlist_init_head(struct timerlist_head *head)
+{
+	head->head = RB_ROOT;
+	head->next = NULL;
+}
+#endif /* _LINUX_TIMERLIST_H */
lib/Makefile +1 -1
index e6a3763..8b475cf 100644
@@ -8,7 +8,7 @@ KBUILD_CFLAGS = $(subst -pg,,$(ORIG_CFLAGS))
 endif
 
 lib-y := ctype.o string.o vsprintf.o cmdline.o \
-	 rbtree.o radix-tree.o dump_stack.o \
+	 rbtree.o radix-tree.o dump_stack.o timerlist.o\
 	 idr.o int_sqrt.o extable.o prio_tree.o \
 	 sha1.o irq_regs.o reciprocal_div.o argv_split.o \
 	 proportions.o prio_heap.o ratelimit.o show_mem.o \
lib/timerlist.c new file +115 -0

115 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209
#2 lore raw

RFC PATCH 2/7 hrtimers: Convert hrtimers to use timerlist infrastructure

In reply to John Stultz

Converts the hrtimer code to use the timerlist infrastructure

Survived some light testing, but there may still be bugs.

CC Alessandro Zummo <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Richard Cochran <[email protected]>
CC Arve Hjønnevåg <[email protected]>
---
 include/linux/hrtimer.h  |   32 ++++++++---------
 kernel/hrtimer.c         |   86 ++++++++++++++++------------------------------
 kernel/time/timer_list.c |    8 ++--
 3 files changed, 49 insertions(+), 77 deletions(-)
include/linux/hrtimer.h +15 -17
index fd0c1b8..019ac70 100644
@@ -22,7 +22,7 @@
 #include <linux/wait.h>
 #include <linux/percpu.h>
 #include <linux/timer.h>
-
+#include <linux/timerlist.h>
 
 struct hrtimer_clock_base;
 struct hrtimer_cpu_base;
@@ -79,8 +79,8 @@ enum hrtimer_restart {
 
 /**
  * struct hrtimer - the basic hrtimer structure
- * @node:	red black tree node for time ordered insertion
- * @_expires:	the absolute expiry time in the hrtimers internal
+ * @node:	timerlist node, which also manages node.expires,
+ *		the absolute expiry time in the hrtimers internal
  *		representation. The time is related to the clock on
  *		which the timer is based. Is setup by adding
  *		slack to the _softexpires value. For non range timers
@@ -101,8 +101,7 @@ enum hrtimer_restart {
  * The hrtimer structure must be initialized by hrtimer_init()
  */
 struct hrtimer {
-	struct rb_node			node;
-	ktime_t				_expires;
+	struct timerlist_node		node;
 	ktime_t				_softexpires;
 	enum hrtimer_restart		(*function)(struct hrtimer *);
 	struct hrtimer_clock_base	*base;
@@ -141,8 +140,7 @@ struct hrtimer_sleeper {
 struct hrtimer_clock_base {
 	struct hrtimer_cpu_base	*cpu_base;
 	clockid_t		index;
-	struct rb_root		active;
-	struct rb_node		*first;
+	struct timerlist_head	active;
 	ktime_t			resolution;
 	ktime_t			(*get_time)(void);
 	ktime_t			softirq_time;
@@ -184,43 +182,43 @@ struct hrtimer_cpu_base {
 
 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
 {
-	timer->_expires = time;
+	timer->node.expires = time;
 	timer->_softexpires = time;
 }
 
 static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
 {
 	timer->_softexpires = time;
-	timer->_expires = ktime_add_safe(time, delta);
+	timer->node.expires = ktime_add_safe(time, delta);
 }
 
 static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta)
 {
 	timer->_softexpires = time;
-	timer->_expires = ktime_add_safe(time, ns_to_ktime(delta));
+	timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
 }
 
 static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
 {
-	timer->_expires.tv64 = tv64;
+	timer->node.expires.tv64 = tv64;
 	timer->_softexpires.tv64 = tv64;
 }
 
 static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
 {
-	timer->_expires = ktime_add_safe(timer->_expires, time);
+	timer->node.expires = ktime_add_safe(timer->node.expires, time);
 	timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
 }
 
 static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
 {
-	timer->_expires = ktime_add_ns(timer->_expires, ns);
+	timer->node.expires = ktime_add_ns(timer->node.expires, ns);
 	timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
 }
 
 static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
 {
-	return timer->_expires;
+	return timer->node.expires;
 }
 
 static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
@@ -230,7 +228,7 @@ static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
 
 static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
 {
-	return timer->_expires.tv64;
+	return timer->node.expires.tv64;
 }
 static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
 {
@@ -239,12 +237,12 @@ static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
 
 static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
 {
-	return ktime_to_ns(timer->_expires);
+	return ktime_to_ns(timer->node.expires);
 }
 
 static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
 {
-    return ktime_sub(timer->_expires, timer->base->get_time());
+	return ktime_sub(timer->node.expires, timer->base->get_time());
 }
 
 #ifdef CONFIG_HIGH_RES_TIMERS
kernel/hrtimer.c +30 -56

182 lines not on this page · raw message

kernel/time/timer_list.c +4 -4
index ab8f5e3..7583b8d 100644
@@ -79,26 +79,26 @@ print_active_timers(struct seq_file *m, struct hrtimer_clock_base *base,
 {
 	struct hrtimer *timer, tmp;
 	unsigned long next = 0, i;
-	struct rb_node *curr;
+	struct timerlist_node *curr;
 	unsigned long flags;
 
 next_one:
 	i = 0;
 	raw_spin_lock_irqsave(&base->cpu_base->lock, flags);
 
-	curr = base->first;
+	curr = timerlist_getnext(&base->active);
 	/*
 	 * Crude but we have to do this O(N*N) thing, because
 	 * we have to unlock the base when printing:
 	 */
 	while (curr && i < next) {
-		curr = rb_next(curr);
+		curr = timerlist_iterate_next(curr);
 		i++;
 	}
 
 	if (curr) {
 
-		timer = rb_entry(curr, struct hrtimer, node);
+		timer = container_of(curr, struct hrtimer, node);
 		tmp = *timer;
 		raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
 
Signature · 1 line
1.7.3.2.146.gca209
#3 lore raw

RFC PATCH 3/7 RTC: Rework RTC code to use timerlist for events

In reply to John Stultz

This patch reworks a large portion of the generic RTC code to in-effect virtualize the rtc interrupt code.

The current RTC interface is very much a raw hardware interface. Via the proc, /dev/, or sysfs interfaces, applciations can set the hardware to trigger interrupts in one of three modes:

AIE: Alarm interrupt
UIE: Update interrupt (ie: once per second)
PIE: Periodic interrupt (sub-second irqs)

The problem with this interface is that it limits the RTC hardware so it can only be used by one application at a time.

The purpose of this patch is to extend the RTC code so that we can multiplex multiple applications event needs onto a single RTC device. This is done by utilizing the timerlist infrastructure to manage a list of events, which cause the RTC hardware to be programmed to fire an interrupt for the next event in the list.

In order to preserve the functionality of the exsting proc,/dev/ and sysfs interfaces, we emulate the different interrupt modes as follows:

AIE: We create a rtc_timer dedicated to AIE mode interrupts. There is only one per device, so we don't change existing interface semantics.

UIE: Again, a dedicated rtc_timer, set for periodic mode, is used to emulate UIE interrupts. Again, only one per device.

PIE: Since PIE mode interrupts fire faster then the RTC's clock read granularity, we emulate PIE mode interrupts using a hrtimer. Again, one per device.

With this patch, the rtctest.c application in Documentation/rtc.txt passes fine on x86 hardware. However, there may very well still be bugs, so greatly I'd appreciate any feedback or testing!

CC Alessandro Zummo <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Richard Cochran <[email protected]>
CC Arve Hjønnevåg <[email protected]>
---
 drivers/rtc/class.c     |   13 +
 drivers/rtc/interface.c |  574 ++++++++++++++++++++++++++++------------------
 drivers/rtc/rtc-lib.c   |   26 +++
 include/linux/rtc.h     |   43 +++-
 4 files changed, 426 insertions(+), 230 deletions(-)
drivers/rtc/class.c +13 -0
index e6539cb..fb59222 100644
@@ -16,6 +16,7 @@
 #include <linux/kdev_t.h>
 #include <linux/idr.h>
 #include <linux/slab.h>
+#include <linux/workqueue.h>
 
 #include "rtc-core.h"
 
@@ -152,6 +153,18 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,
 	spin_lock_init(&rtc->irq_task_lock);
 	init_waitqueue_head(&rtc->irq_queue);
 
+	/* Init timerlist */
+	timerlist_init_head(&rtc->timerlist);
+	INIT_WORK(&rtc->irqwork, rtctimer_do_work);
+	/* Init aie timer */
+	rtctimer_init(&rtc->aie_timer, rtc_aie_update_irq, (void*)rtc);
+	/* Init uie timer */
+	rtctimer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void*)rtc);
+	/* Init pie timer */
+	hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	rtc->pie_timer.function = rtc_pie_update_irq;
+	rtc->pie_enabled = 0;
+
 	strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
 	dev_set_name(&rtc->dev, "rtc%d", id);
 
drivers/rtc/interface.c +349 -225

678 lines not on this page · raw message

drivers/rtc/rtc-lib.c +26 -0
index 773851f..00df788 100644
@@ -117,4 +117,30 @@ int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
 }
 EXPORT_SYMBOL(rtc_tm_to_time);
 
+/*
+ * Convert rtc_time to ktime
+ */
+ktime_t rtc_tm_to_ktime(struct rtc_time tm)
+{
+	time_t time;
+	rtc_tm_to_time(&tm, &time);
+	return ktime_set(time, 0);
+}
+
+/*
+ * Convert ktime to rtc_time
+ */
+struct rtc_time rtc_ktime_to_tm(ktime_t kt)
+{
+	struct timespec ts;
+	struct rtc_time ret;
+
+	ts = ktime_to_timespec(kt);
+	/* Round up any ns */
+	if (ts.tv_nsec)
+		ts.tv_sec++;
+	rtc_time_to_tm(ts.tv_sec, &ret);
+	return ret;
+}
+
 MODULE_LICENSE("GPL");
include/linux/rtc.h +38 -5

78 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209
#4 lore raw

RFC PATCH 4/7 RTC: Remove UIE emulation

In reply to John Stultz

Since we provide UIE interrupts via a rtc_timer, the old emulation code can be removed.

Note: I assume this is ok, but there may be corner cases I'm missing.

CC Alessandro Zummo <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Richard Cochran <[email protected]>
---
 drivers/rtc/rtc-dev.c |  104 -------------------------------------------------
 include/linux/rtc.h   |   12 ------
 2 files changed, 0 insertions(+), 116 deletions(-)
drivers/rtc/rtc-dev.c +0 -104
index 62227cd..212b16e 100644
@@ -46,105 +46,6 @@ static int rtc_dev_open(struct inode *inode, struct file *file)
 	return err;
 }
 
-#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
-/*
- * Routine to poll RTC seconds field for change as often as possible,
- * after first RTC_UIE use timer to reduce polling
- */
-static void rtc_uie_task(struct work_struct *work)
-{
-	struct rtc_device *rtc =
-		container_of(work, struct rtc_device, uie_task);
-	struct rtc_time tm;
-	int num = 0;
-	int err;
-
-	err = rtc_read_time(rtc, &tm);
-
-	spin_lock_irq(&rtc->irq_lock);
-	if (rtc->stop_uie_polling || err) {
-		rtc->uie_task_active = 0;
-	} else if (rtc->oldsecs != tm.tm_sec) {
-		num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
-		rtc->oldsecs = tm.tm_sec;
-		rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
-		rtc->uie_timer_active = 1;
-		rtc->uie_task_active = 0;
-		add_timer(&rtc->uie_timer);
-	} else if (schedule_work(&rtc->uie_task) == 0) {
-		rtc->uie_task_active = 0;
-	}
-	spin_unlock_irq(&rtc->irq_lock);
-	if (num)
-		rtc_update_irq(rtc, num, RTC_UF | RTC_IRQF);
-}
-static void rtc_uie_timer(unsigned long data)
-{
-	struct rtc_device *rtc = (struct rtc_device *)data;
-	unsigned long flags;
-
-	spin_lock_irqsave(&rtc->irq_lock, flags);
-	rtc->uie_timer_active = 0;
-	rtc->uie_task_active = 1;
-	if ((schedule_work(&rtc->uie_task) == 0))
-		rtc->uie_task_active = 0;
-	spin_unlock_irqrestore(&rtc->irq_lock, flags);
-}
-
-static int clear_uie(struct rtc_device *rtc)
-{
-	spin_lock_irq(&rtc->irq_lock);
-	if (rtc->uie_irq_active) {
-		rtc->stop_uie_polling = 1;
-		if (rtc->uie_timer_active) {
-			spin_unlock_irq(&rtc->irq_lock);
-			del_timer_sync(&rtc->uie_timer);
-			spin_lock_irq(&rtc->irq_lock);
-			rtc->uie_timer_active = 0;
-		}
-		if (rtc->uie_task_active) {
-			spin_unlock_irq(&rtc->irq_lock);
-			flush_scheduled_work();
-			spin_lock_irq(&rtc->irq_lock);
-		}
-		rtc->uie_irq_active = 0;
-	}
-	spin_unlock_irq(&rtc->irq_lock);
-	return 0;
-}
-
-static int set_uie(struct rtc_device *rtc)
-{
-	struct rtc_time tm;
-	int err;
-
-	err = rtc_read_time(rtc, &tm);
-	if (err)
-		return err;
-	spin_lock_irq(&rtc->irq_lock);
-	if (!rtc->uie_irq_active) {
-		rtc->uie_irq_active = 1;
-		rtc->stop_uie_polling = 0;
-		rtc->oldsecs = tm.tm_sec;
-		rtc->uie_task_active = 1;
-		if (schedule_work(&rtc->uie_task) == 0)
-			rtc->uie_task_active = 0;
-	}
-	rtc->irq_data = 0;
-	spin_unlock_irq(&rtc->irq_lock);
-	return 0;
-}
-
-int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
-{
-	if (enabled)
-		return set_uie(rtc);
-	else
-		return clear_uie(rtc);
-}
-EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
-
-#endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
 
 static ssize_t
 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
@@ -493,11 +394,6 @@ void rtc_dev_prepare(struct rtc_device *rtc)
 
 	rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
 
-#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
-	INIT_WORK(&rtc->uie_task, rtc_uie_task);
-	setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
-#endif
-
 	cdev_init(&rtc->char_dev, &rtc_dev_fops);
 	rtc->char_dev.owner = rtc->owner;
 }
include/linux/rtc.h +0 -12
index c50fae5..7af34ec 100644
@@ -203,18 +203,6 @@ struct rtc_device
 	struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
 	int pie_enabled;
 	struct work_struct irqwork;
-
-
-#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
-	struct work_struct uie_task;
-	struct timer_list uie_timer;
-	/* Those fields are protected by rtc->irq_lock */
-	unsigned int oldsecs;
-	unsigned int uie_irq_active:1;
-	unsigned int stop_uie_polling:1;
-	unsigned int uie_task_active:1;
-	unsigned int uie_timer_active:1;
-#endif
 };
 #define to_rtc_device(d) container_of(d, struct rtc_device, dev)
 
Signature · 1 line
1.7.3.2.146.gca209
#5 lore raw

RFC PATCH 5/7 posix clocks: dynamic clock ids.

In reply to John Stultz

From: Richard Cochran <[email protected]>

This patch augments the POSIX clock code to offer a dynamic clock creation method. Instead of registering a hard coded clock ID, modules may call create_posix_clock(), which returns a new clock ID.

This patch still has un-addressed lifetime issues pointed out by Alan Cox. More work is needed here, but for now this provides the functionality needed for the following patches.

CC Alessandro Zummo <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Richard Cochran <[email protected]>
CC Arve Hjønnevåg <[email protected]>
---
 include/linux/posix-timers.h |    7 ++++++-
 include/linux/time.h         |    2 ++
 kernel/posix-timers.c        |   41 ++++++++++++++++++++++++++++++++++-------
 3 files changed, 42 insertions(+), 8 deletions(-)
include/linux/posix-timers.h +6 -1
index 3e23844a..a9e601a 100644
@@ -67,6 +67,7 @@ struct k_itimer {
 };
 
 struct k_clock {
+	clockid_t id;
 	int res;		/* in nanoseconds */
 	int (*clock_getres) (const clockid_t which_clock, struct timespec *tp);
 	int (*clock_set) (const clockid_t which_clock, struct timespec * tp);
@@ -84,7 +85,11 @@ struct k_clock {
 			   struct itimerspec * cur_setting);
 };
 
-void register_posix_clock(const clockid_t clock_id, struct k_clock *new_clock);
+/* Regsiter a posix clock with a "well known" clock id. */
+int register_posix_clock(const clockid_t id, struct k_clock *clock);
+
+/* Create a new posix clock with a dynamic clock id. */
+clockid_t create_posix_clock(struct k_clock *clock);
 
 /* error handlers for timer_create, nanosleep and settime */
 int do_posix_clock_nonanosleep(const clockid_t, int flags, struct timespec *,
include/linux/time.h +2 -0
index 9f15ac7..914c48d 100644
@@ -299,6 +299,8 @@ struct itimerval {
 #define CLOCKS_MASK			(CLOCK_REALTIME | CLOCK_MONOTONIC)
 #define CLOCKS_MONO			CLOCK_MONOTONIC
 
+#define CLOCK_INVALID			-1
+
 /*
  * The various flags for setting POSIX.1b interval timers:
  */
kernel/posix-timers.c +34 -7

58 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209
#6 lore raw

RFC PATCH 6/7 RTC: Add posix clock/timer interface

In reply to John Stultz

This patch provides an initial implementation of the posix clock/timer interface to the RTC. Since the current /dev, proc, and sysfs interfaces don't queue events, two applications trying to set a wakeup events in the future might race, causing events to be missed. The posix interface allows multiple applications to be able to set timers against a single RTC device, letting the kernel manage the multiplexing.

One example use would be a backup job that is done at night on a desktop system that suspends after 15 minutes of idle time. A cron-like job manager could set a posix timer against the RTC to ensure the system woke from suspend in order to run the backup.

However, the system might also run a DVR application to record a favorite show. That application could also use the posix interface to set a timer against the RTC without having to worry if it had overwritten the timer set for the backup job.

Since there can be multiple RTCs on a system, we utilize the dynamic posix clock registration code, and export the clockid via /sys/class/rtc/rtcN/posix_clockid.

TODO:
o improve clock_to_rtc() perf
o stress test timer code
o code comments
o probably other stuff too

CC Alessandro Zummo <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Richard Cochran <[email protected]>
---
 drivers/rtc/Makefile         |    2 +-
 drivers/rtc/class.c          |    2 +
 drivers/rtc/interface.c      |    9 +-
 drivers/rtc/posix.c          |  258 ++++++++++++++++++++++++++++++++++++++++++
 drivers/rtc/rtc-sysfs.c      |   10 ++
 include/linux/posix-timers.h |    2 +
 include/linux/rtc.h          |    5 +-
 7 files changed, 283 insertions(+), 5 deletions(-)
 create mode 100644 drivers/rtc/posix.c
drivers/rtc/Makefile +1 -1
index 4c2832d..05e2c90 100644
@@ -9,7 +9,7 @@ endif
 obj-$(CONFIG_RTC_LIB)		+= rtc-lib.o
 obj-$(CONFIG_RTC_HCTOSYS)	+= hctosys.o
 obj-$(CONFIG_RTC_CLASS)		+= rtc-core.o
-rtc-core-y			:= class.o interface.o
+rtc-core-y			:= class.o interface.o posix.o
 
 rtc-core-$(CONFIG_RTC_INTF_DEV)	+= rtc-dev.o
 rtc-core-$(CONFIG_RTC_INTF_PROC) += rtc-proc.o
drivers/rtc/class.c +2 -0
index fb59222..85762ea 100644
@@ -165,6 +165,8 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,
 	rtc->pie_timer.function = rtc_pie_update_irq;
 	rtc->pie_enabled = 0;
 
+	init_rtc_posix_timer(rtc);
+
 	strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
 	dev_set_name(&rtc->dev, "rtc%d", id);
 
drivers/rtc/interface.c +7 -2
index 55894ee..b6f1bc9 100644
@@ -16,6 +16,11 @@
 #include <linux/log2.h>
 #include <linux/workqueue.h>
 
+
+static void rtctimer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
+static void rtctimer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
+
+
 static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
 {
 	int err;
@@ -490,7 +495,7 @@ EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
  *
  * Must hold ops_lock for proper serialization of timerlist
  */
-void rtctimer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
+static void rtctimer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
 {
 	timerlist_add(&rtc->timerlist, &timer->node);
 	if (&timer->node == timerlist_getnext(&rtc->timerlist)) {
@@ -514,7 +519,7 @@ void rtctimer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  *
  * Must hold ops_lock for proper serialization of timerlist
  */
-void rtctimer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
+static void rtctimer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
 {
 	struct timerlist_node *next = timerlist_getnext(&rtc->timerlist);
 	timerlist_del(&rtc->timerlist, &timer->node);
drivers/rtc/posix.c new file +258 -0

258 lines not on this page · raw message

drivers/rtc/rtc-sysfs.c +10 -0
index 380083c..1e8786f 100644
@@ -81,6 +81,15 @@ rtc_sysfs_show_since_epoch(struct device *dev, struct device_attribute *attr,
 }
 
 static ssize_t
+rtc_sysfs_show_posix_clockid(struct device *dev, struct device_attribute *attr,
+		char *buf)
+{
+	ssize_t retval;
+	retval = sprintf(buf, "%lu\n", (long)to_rtc_device(dev)->posix_id);
+	return retval;
+}
+
+static ssize_t
 rtc_sysfs_show_max_user_freq(struct device *dev, struct device_attribute *attr,
 		char *buf)
 {
@@ -121,6 +130,7 @@ static struct device_attribute rtc_attrs[] = {
 	__ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL),
 	__ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL),
 	__ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL),
+	__ATTR(posix_clockid, S_IRUGO, rtc_sysfs_show_posix_clockid, NULL),
 	__ATTR(max_user_freq, S_IRUGO | S_IWUSR, rtc_sysfs_show_max_user_freq,
 			rtc_sysfs_set_max_user_freq),
 	__ATTR(hctosys, S_IRUGO, rtc_sysfs_show_hctosys, NULL),
include/linux/posix-timers.h +2 -0
index a9e601a..e6b46b5 100644
@@ -4,6 +4,7 @@
 #include <linux/spinlock.h>
 #include <linux/list.h>
 #include <linux/sched.h>
+#include <linux/rtc.h>
 
 union cpu_time_count {
 	cputime_t cpu;
@@ -63,6 +64,7 @@ struct k_itimer {
 			unsigned long incr;
 			unsigned long expires;
 		} mmtimer;
+		struct rtc_timer rtctimer;
 	} it;
 };
 
include/linux/rtc.h +3 -2
index 7af34ec..25c96a1 100644
@@ -203,6 +203,8 @@ struct rtc_device
 	struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
 	int pie_enabled;
 	struct work_struct irqwork;
+
+	clockid_t posix_id;
 };
 #define to_rtc_device(d) container_of(d, struct rtc_device, dev)
 
@@ -246,8 +248,7 @@ int rtc_register(rtc_task_t *task);
 int rtc_unregister(rtc_task_t *task);
 int rtc_control(rtc_task_t *t, unsigned int cmd, unsigned long arg);
 
-void rtctimer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
-void rtctimer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
+void init_rtc_posix_timer(struct rtc_device * rtc);
 void rtctimer_init(struct rtc_timer *timer, void (*f)(void* p), void* data);
 int rtctimer_start(struct rtc_device *rtc, struct rtc_timer* timer,
 			ktime_t expires, ktime_t period);
Signature · 1 line
1.7.3.2.146.gca209
#7 lore raw

RFC PATCH 7/7 Introduce Alarm (hybrid) timers

In reply to John Stultz

So after working on my posix interface to the RTC clocks, I came across the Android alarm driver (by Arve Hjønnevåg) in the android kernel tree.

See: http://android.git.kernel.org/?p=kernel/common.git;a=blob;f=drivers/rtc/alarm.c;h=1250edfbdf3302f5e4ea6194847c6ef4bb7beb1c;hb=android-2.6.36

The android alarm driver is different from the RTC posix clock interface in that rather then directly setting timers against the RTC device, it uses a hybrid mode, where timers are set against hrtimers, but when the system suspends, an RTC alarm is set to wake the system at the next timer expiration.

There are some pluses and minuses with this approach. The good:
* No need for apps to deal with a new time domain (rtc) which may drift or just be different from system time.

* Avoids issue of selecting which rtc to use on multi-rtc systems, as the kernel does it for you.

The bad:
* On systems that have multiple rtc devices, we just use the first one, which may or may not be ideal (honestly, having multiple rtcs never made sense to me).

* For apps that actually want to deal with the RTC clock and are interested in setting timers against it as a different time domain, this provides no user interface to do so.

I don't really see the bad list as a problem, but please pipe up if you do.

I had actually discussed this idea with Thomas earlier as being a potential future interface, but seeing it already in-use by Android verifies that it is the more interesting use mode.

The android alarm driver was built on top of direct RTC manipulation, and also implemented its own rbtree timer code, so instead of trying to porting it over to my timerlist and RTC timer code, I just re-implemented the basic functionality roughly following the android in-kernel interface.

Another large distinction is that while the in-kernel interface is pretty similar, the user-space interface for android alarm timers is via ioctls. 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:
RTC and ELAPSED_REALTIME). Instead, if one wants to use a alarm timer, simply create a posix timer against either CLOCK_REALTIME_ALARM or CLOCK_MONOTONIC_ALARM.

There is surely other bits of the android alarm code that I'm sure I didn't fully understand, so some of the simplifications that I've made may not allow Android to use this interface. I'd be very interested if those details could be pointed out, and hopefully we can find a good solution to get this useful functionality upstream.

This patch is a very initial draft. I'm sure there are bugs. I just wanted to get it out for structural comment about the interface.

Also, this patch does not depend on the posix rtc interface layer also included in this patchset. One or both interfaces may be useful, so I'm including both to get a feel for what folks actually like.

Arve: I know you're busy now, so no rush on reviewing this. I just wanted to send this out to get some early feedback if anyone else wants to comment. I look forward to your input when you are a bit less swamped.

CC Arve Hjønnevåg <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Alessandro Zummo <[email protected]>
---
 include/linux/alarmtimer.h   |   29 +++
 include/linux/posix-timers.h |    2 +
 include/linux/time.h         |    2 +
 kernel/time/Makefile         |    2 +-
 kernel/time/alarmtimer.c     |  522 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 556 insertions(+), 1 deletions(-)
 create mode 100644 include/linux/alarmtimer.h
 create mode 100644 kernel/time/alarmtimer.c
include/linux/alarmtimer.h new file +29 -0
new file mode 100644
index 0000000..ca3ab47
@@ -0,0 +1,29 @@
+#ifndef _LINUX_ALARMTIMER_H
+#define _LINUX_ALARMTIMER_H
+
+#include <linux/time.h>
+#include <linux/hrtimer.h>
+#include <linux/timerlist.h>
+#include <linux/rtc.h>
+
+enum alarmtimer_type {
+	ALARM_REALTIME,
+	ALARM_MONOTONIC,
+
+	ALARM_NUMTYPE,
+};
+
+struct alarm {
+	struct timerlist_node node;
+	ktime_t period;
+	void (*function)(struct alarm*);
+	enum alarmtimer_type type;
+	char enabled;
+};
+
+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
include/linux/posix-timers.h +2 -0
index e6b46b5..9d1ace6 100644
@@ -5,6 +5,7 @@
 #include <linux/list.h>
 #include <linux/sched.h>
 #include <linux/rtc.h>
+#include <linux/alarmtimer.h>
 
 union cpu_time_count {
 	cputime_t cpu;
@@ -65,6 +66,7 @@ struct k_itimer {
 			unsigned long expires;
 		} mmtimer;
 		struct rtc_timer rtctimer;
+		struct alarm alarmtimer;
 	} it;
 };
 
include/linux/time.h +2 -0
index 914c48d..4791858 100644
@@ -290,6 +290,8 @@ struct itimerval {
 #define CLOCK_MONOTONIC_RAW		4
 #define CLOCK_REALTIME_COARSE		5
 #define CLOCK_MONOTONIC_COARSE		6
+#define CLOCK_REALTIME_ALARM		7
+#define CLOCK_MONOTONIC_ALARM		8
 
 /*
  * The IDs of various hardware clocks:
kernel/time/Makefile +1 -1
index ee26662..f85145f 100644
@@ -1,4 +1,4 @@
-obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.o
+obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.o alarmtimer.o
 
 obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD)		+= clockevents.o
 obj-$(CONFIG_GENERIC_CLOCKEVENTS)		+= tick-common.o
kernel/time/alarmtimer.c new file +522 -0

522 lines not on this page · raw message

Signature · 1 line
1.7.3.2.146.gca209
#8 lore raw

Re: [PATCH 7/7] [RFC] Introduce Alarm (hybrid) timers

Quoted 9 lines · John Stultz

Another large distinction is that while the in-kernel interface is pretty similar, the user-space interface for android alarm timers is via ioctls. 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:
RTC and ELAPSED_REALTIME). Instead, if one wants to use a alarm timer, simply create a posix timer against either CLOCK_REALTIME_ALARM or CLOCK_MONOTONIC_ALARM.

I have a comment on this, below ...

Quoted 13 lines
include/linux/posix-timers.h +1 -0
index e6b46b5..9d1ace6 100644
@@ -5,6 +5,7 @@
 #include <linux/list.h>
 #include <linux/sched.h>
 #include <linux/rtc.h>
+#include <linux/alarmtimer.h>
 
 union cpu_time_count {
 	cputime_t cpu;

include/linux/posix-timers.h struct k_itimer { · line 71

Quoted 6 lines
include/linux/posix-timers.h +1 -0
@@ -65,6 +66,7 @@ struct k_itimer {
 			unsigned long expires;
 		} mmtimer;
 		struct rtc_timer rtctimer;
+		struct alarm alarmtimer;
 	} it;
 };

I have an initial dynamic clock patch set ready for review and will post it later today if I can. In implementing the timer_ calls, I began to wonder about this 'it' union. If we really allow and implement many kinds of dynamic clocks, then it would seem ugly to me to simply add yet another union member for each new clock. Wouldn't it be better to provide a private void pointer for the underlying driver's use?

include/linux/time.h struct itimerval { · line 294

Quoted 10 lines
include/linux/time.h +2 -0
index 914c48d..4791858 100644
@@ -290,6 +290,8 @@ struct itimerval {
 #define CLOCK_MONOTONIC_RAW		4
 #define CLOCK_REALTIME_COARSE		5
 #define CLOCK_MONOTONIC_COARSE		6
+#define CLOCK_REALTIME_ALARM		7
+#define CLOCK_MONOTONIC_ALARM		8

I have thought about and have taken Alan Cox's anti-SYS5.4/SuS/POSIX enumeration arguments to heart. If you need a really good example of the weaknesses of that way, take a look at clock_getcpuclockid, pthread_getcpuclockid, and their implementation in posix-cpu-timers.c

If we are serious about dynamic clocks, then we will never again touch the CLOCK_ list. I hope that, after reviewing the coming patch set, you will also agree ;)

Richard

#9 lore raw

Re: [PATCH 7/7] [RFC] Introduce Alarm (hybrid) timers

Quoted 34 lines · Richard Cochran
Quoted 9 lines · John Stultz

Another large distinction is that while the in-kernel interface is pretty similar, the user-space interface for android alarm timers is via ioctls. 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:
RTC and ELAPSED_REALTIME). Instead, if one wants to use a alarm timer, simply create a posix timer against either CLOCK_REALTIME_ALARM or CLOCK_MONOTONIC_ALARM.

I have a comment on this, below ...

Quoted 13 lines
include/linux/posix-timers.h +1 -0
index e6b46b5..9d1ace6 100644
@@ -5,6 +5,7 @@
 #include <linux/list.h>
 #include <linux/sched.h>
 #include <linux/rtc.h>
+#include <linux/alarmtimer.h>
 
 union cpu_time_count {
 	cputime_t cpu;

include/linux/posix-timers.h struct k_itimer { · line 71

Quoted 6 lines
include/linux/posix-timers.h +1 -0
@@ -65,6 +66,7 @@ struct k_itimer {
 			unsigned long expires;
 		} mmtimer;
 		struct rtc_timer rtctimer;
+		struct alarm alarmtimer;
 	} it;
 };
Quoted 7 lines

I have an initial dynamic clock patch set ready for review and will post it later today if I can. In implementing the timer_ calls, I began to wonder about this 'it' union. If we really allow and implement many kinds of dynamic clocks, then it would seem ugly to me to simply add yet another union member for each new clock. Wouldn't it be better to provide a private void pointer for the underlying driver's use?

Yea. That union is getting a bit ugly, but it lets us avoid having to manage allocating and freeing a parallel timer structure that points back to the k_itimer. Its not a huge issue, but the code in this case is much simpler, but at the cost of making the k_itimer it union a little gross. So yea, I'm fine changing it if there's a real desire for it to be done.

Quoted 16 lines

include/linux/time.h struct itimerval { · line 294

Quoted 10 lines
include/linux/time.h +2 -0
index 914c48d..4791858 100644
@@ -290,6 +290,8 @@ struct itimerval {
 #define CLOCK_MONOTONIC_RAW		4
 #define CLOCK_REALTIME_COARSE		5
 #define CLOCK_MONOTONIC_COARSE		6
+#define CLOCK_REALTIME_ALARM		7
+#define CLOCK_MONOTONIC_ALARM		8

I have thought about and have taken Alan Cox's anti-SYS5.4/SuS/POSIX enumeration arguments to heart. If you need a really good example of the weaknesses of that way, take a look at clock_getcpuclockid, pthread_getcpuclockid, and their implementation in posix-cpu-timers.c

Quoted 3 lines

If we are serious about dynamic clocks, then we will never again touch the CLOCK_ list. I hope that, after reviewing the coming patch set, you will also agree ;)

Well, I'm eager to see you patch, and I do think dynamic clockids are a very useful concept that we need.

However, I suspect there will still be a need for static clockids for those cases where its a conceptual api that doesn't deal with specific hardware that has lifetime issues, such as the MONOTONIC/REALTIME_ALARM cases above.

I look forward to your patches!

thanks
-john