Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[PATCH v2 1/3] kernel/time: Add new helpers to convert ktime to/from jiffies

1 view
Skip to first unread message

Alexey Perevalov

unread,
Jan 13, 2014, 5:50:02 AM1/13/14
to
From: Anton Vorontsov <an...@scarybugs.org>

Two new functions: jiffies_to_ktime() and ktime_to_jiffies(), we'll use
them for timerfd deferred timers handling.

We fully reuse the logic from timespec implementations, so the functions
are pretty straightforward.

The only tricky part is in headers: we have to include jiffies.h after
we defined ktime_t, this is because ktime.h needs some declarations from
jiffies.h (e.g. TICK_NSEC).

Signed-off-by: Anton Vorontsov <anton.v...@linaro.org>
Signed-off-by: Alexey Perevalov <a.per...@samsung.com>
---
include/linux/jiffies.h | 4 +++-
include/linux/ktime.h | 3 ++-
kernel/time.c | 23 +++++++++++++++++++++++
3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h
index d235e88..1ba02ae 100644
--- a/include/linux/jiffies.h
+++ b/include/linux/jiffies.h
@@ -6,6 +6,7 @@
#include <linux/types.h>
#include <linux/time.h>
#include <linux/timex.h>
+#include <linux/ktime.h>
#include <asm/param.h> /* for HZ */

/*
@@ -302,7 +303,8 @@ extern void jiffies_to_timespec(const unsigned long jiffies,
extern unsigned long timeval_to_jiffies(const struct timeval *value);
extern void jiffies_to_timeval(const unsigned long jiffies,
struct timeval *value);
-
+extern unsigned long ktime_to_jiffies(ktime_t *value);
+extern void jiffies_to_ktime(const unsigned long jiffies, ktime_t *value);
extern clock_t jiffies_to_clock_t(unsigned long x);
static inline clock_t jiffies_delta_to_clock_t(long delta)
{
diff --git a/include/linux/ktime.h b/include/linux/ktime.h
index 31c0cd1..e8ed619 100644
--- a/include/linux/ktime.h
+++ b/include/linux/ktime.h
@@ -22,7 +22,6 @@
#define _LINUX_KTIME_H

#include <linux/time.h>
-#include <linux/jiffies.h>

/*
* ktime_t:
@@ -58,6 +57,8 @@ union ktime {

typedef union ktime ktime_t; /* Kill this */

+#include <linux/jiffies.h>
+
/*
* ktime_t definitions when using the 64-bit scalar representation:
*/
diff --git a/kernel/time.c b/kernel/time.c
index 7c7964c..22580a0 100644
--- a/kernel/time.c
+++ b/kernel/time.c
@@ -29,6 +29,7 @@

#include <linux/export.h>
#include <linux/timex.h>
+#include <linux/ktime.h>
#include <linux/capability.h>
#include <linux/timekeeper_internal.h>
#include <linux/errno.h>
@@ -575,6 +576,28 @@ void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
}
EXPORT_SYMBOL(jiffies_to_timeval);

+unsigned long ktime_to_jiffies(ktime_t *value)
+{
+ struct timespec ts = ktime_to_timespec(*value);
+
+ /*
+ * nsecs_to_jiffies(ktime_to_ns(*ktime)) is unsafe as nsecs_to_jiffies
+ * doesn't handle MAX_JIFFY_OFFSET. So we reuse the logic from the
+ * timespec to jiffies conversion function.
+ */
+ return timespec_to_jiffies(&ts);
+}
+EXPORT_SYMBOL(ktime_to_jiffies);
+
+void jiffies_to_ktime(const unsigned long jiffies, ktime_t *value)
+{
+ struct timespec ts;
+
+ jiffies_to_timespec(jiffies, &ts);
+ *value = timespec_to_ktime(ts);
+}
+EXPORT_SYMBOL(jiffies_to_ktime);
+
/*
* Convert jiffies/jiffies_64 to clock_t and back.
*/
--
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majo...@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Alexey Perevalov

unread,
Jan 13, 2014, 5:50:03 AM1/13/14
to
From: Anton Vorontsov <an...@enomsg.org>

This patch implements a userland-side API for generic deferrable timers,
per linux/timer.h:

* A deferrable timer will work normally when the system is busy, but
* will not cause a CPU to come out of idle just to service it; instead,
* the timer will be serviced when the CPU eventually wakes up with a
* subsequent non-deferrable timer.

These timers are crucial for power saving, i.e. periodic tasks that want
to work in background when the system is under use, but don't want to
cause wakeups themselves.

The deferred timers are somewhat orthogonal to high-res external timers,
since the deferred timer is tied to the system load, not just to some
external decrementer source.

So, currently, the implementation has a HZ precision, and the maximum
interval is jiffies resolution (i.e. with HZ=1000, on 32 bit that would
be around max 49 days). Of course we can implement longer timeouts by
rearming the timer, although it probably wouldn't make much sense in
real world, so we keep it simple and just return E2BIG if we don't like
the interval.

Signed-off-by: Alexey Perevalov <a.per...@samsung.com>
---
fs/timerfd.c | 89 ++++++++++++++++++++++++++++++++++++++++-------
include/linux/timerfd.h | 4 ++-
2 files changed, 80 insertions(+), 13 deletions(-)

diff --git a/fs/timerfd.c b/fs/timerfd.c
index 3561ce7..9677a66 100644
--- a/fs/timerfd.c
+++ b/fs/timerfd.c
@@ -31,6 +31,8 @@ struct timerfd_ctx {
struct hrtimer tmr;
struct alarm alarm;
} t;
+ struct timer_list dtmr;
+ bool deferrable;
ktime_t tintv;
ktime_t moffs;
wait_queue_head_t wqh;
@@ -51,6 +53,11 @@ static inline bool isalarm(struct timerfd_ctx *ctx)
ctx->clockid == CLOCK_BOOTTIME_ALARM;
}

+static inline bool isdeferrable(struct timerfd_ctx *ctx)
+{
+ return ctx->deferrable;
+}
+
/*
* This gets called when the timer event triggers. We set the "expired"
* flag, but we do not re-arm the timer (in case it's necessary,
@@ -84,6 +91,11 @@ static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
return ALARMTIMER_NORESTART;
}

+static void timerfd_dtmrproc(unsigned long data)
+{
+ timerfd_triggered((struct timerfd_ctx *)data);
+}
+
/*
* Called when the clock was set to cancel the timers in the cancel
* list. This will wake up processes waiting on these timers. The
@@ -151,12 +163,40 @@ static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)

if (isalarm(ctx))
remaining = alarm_expires_remaining(&ctx->t.alarm);
- else
+ else if (isdeferrable(ctx)) {
+ ktime_t expires;
+ jiffies_to_ktime(ctx->dtmr.expires, &expires);
+ remaining = ktime_sub(expires, ktime_get());
+ } else
remaining = hrtimer_expires_remaining(&ctx->t.tmr);

return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
}

+static bool timerfd_deferrable_valid(ktime_t intv)
+{
+ ktime_t max;
+
+ jiffies_to_ktime(MAX_JIFFY_OFFSET, &max);
+ if (intv.tv64 > max.tv64)
+ return 0;
+ return 1;
+}
+
+static int timerfd_setup_deferrable(struct timerfd_ctx *ctx)
+{
+ ktime_t rem = timerfd_get_remaining(ctx);
+
+ if (ctx->clockid != CLOCK_MONOTONIC)
+ return -EINVAL;
+ if (!timerfd_deferrable_valid(ctx->tintv) ||
+ !timerfd_deferrable_valid(rem))
+ return -E2BIG;
+
+ mod_timer(&ctx->dtmr, jiffies + ktime_to_jiffies(&rem) + 1);
+ return 0;
+}
+
static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
const struct itimerspec *ktmr)
{
@@ -177,6 +217,9 @@ static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
ctx->clockid == CLOCK_REALTIME_ALARM ?
ALARM_REALTIME : ALARM_BOOTTIME,
timerfd_alarmproc);
+ } else if (isdeferrable(ctx)) {
+ ctx->dtmr.function = timerfd_dtmrproc;
+ ctx->dtmr.data = (unsigned long)ctx;
} else {
hrtimer_init(&ctx->t.tmr, clockid, htmode);
hrtimer_set_expires(&ctx->t.tmr, texp);
@@ -189,6 +232,13 @@ static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
alarm_start(&ctx->t.alarm, texp);
else
alarm_start_relative(&ctx->t.alarm, texp);
+
+ } else if (isdeferrable(ctx)) {
+ int ret;
+
+ ret = timerfd_setup_deferrable(ctx);
+ if (ret)
+ return ret;
} else {
hrtimer_start(&ctx->t.tmr, texp, htmode);
}
@@ -207,8 +257,11 @@ static int timerfd_release(struct inode *inode, struct file *file)

if (isalarm(ctx))
alarm_cancel(&ctx->t.alarm);
- else
+ else {
+ del_timer_sync(&ctx->dtmr);
hrtimer_cancel(&ctx->t.tmr);
+ }
+
kfree_rcu(ctx, rcu);
return 0;
}
@@ -231,12 +284,15 @@ static unsigned int timerfd_poll(struct file *file, poll_table *wait)

static u64 timerfd_rearm(struct timerfd_ctx *ctx)
{
- u64 orun;
+ u64 orun = 0;

if (isalarm(ctx)) {
orun += alarm_forward_now(
&ctx->t.alarm, ctx->tintv) - 1;
alarm_restart(&ctx->t.alarm);
+ } else if (isdeferrable(ctx)) {
+ mod_timer(&ctx->dtmr, jiffies +
+ ktime_to_jiffies(&ctx->tintv) + 1);
} else {
orun += hrtimer_forward_now(&ctx->t.tmr,
ctx->tintv) - 1;
@@ -341,8 +397,11 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
ctx->clockid == CLOCK_REALTIME_ALARM ?
ALARM_REALTIME : ALARM_BOOTTIME,
timerfd_alarmproc);
- else
+ else {
hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
+ /* Create deferrable timer in any none alarm case */
+ init_timer_deferrable(&ctx->dtmr);
+ }

ctx->moffs = ktime_get_monotonic_offset();

@@ -354,7 +413,7 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
return ufd;
}

-static int do_timerfd_settime(int ufd, int flags,
+static int do_timerfd_settime(int ufd, int flags,
const struct itimerspec *new,
struct itimerspec *old)
{
@@ -379,19 +438,25 @@ static int do_timerfd_settime(int ufd, int flags,
* it to the new values.
*/
for (;;) {
+ int canceled;
spin_lock_irq(&ctx->wqh.lock);

- if (isalarm(ctx)) {
- if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
- break;
- } else {
- if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
- break;
- }
+ if (isalarm(ctx))
+ canceled = alarm_try_to_cancel(&ctx->t.alarm);
+ else if (isdeferrable(ctx))
+ canceled = try_to_del_timer_sync(&ctx->dtmr);
+ else
+ canceled = hrtimer_try_to_cancel(&ctx->t.tmr);
+
+ if (canceled >= 0)
+ break;
spin_unlock_irq(&ctx->wqh.lock);
cpu_relax();
}

+ /* Must set a new value after we cancel the previous timer. */
+ ctx->deferrable = flags & TFD_TIMER_DEFERRABLE;
+
/*
* If the timer is expired and it's periodic, we need to advance it
* because the caller may want to know the previous expiration time.
diff --git a/include/linux/timerfd.h b/include/linux/timerfd.h
index d3b57fa..e053105 100644
--- a/include/linux/timerfd.h
+++ b/include/linux/timerfd.h
@@ -20,6 +20,7 @@
*/
#define TFD_TIMER_ABSTIME (1 << 0)
#define TFD_TIMER_CANCEL_ON_SET (1 << 1)
+#define TFD_TIMER_DEFERRABLE (1 << 2)
#define TFD_CLOEXEC O_CLOEXEC
#define TFD_NONBLOCK O_NONBLOCK

@@ -27,6 +28,7 @@
/* Flags for timerfd_create. */
#define TFD_CREATE_FLAGS TFD_SHARED_FCNTL_FLAGS
/* Flags for timerfd_settime. */
-#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)
+#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET | \
+ TFD_TIMER_DEFERRABLE)

#endif /* _LINUX_TIMERFD_H */

Alexey Perevalov

unread,
Jan 13, 2014, 5:50:03 AM1/13/14
to
From: Anton Vorontsov <an...@enomsg.org>

There is nothing hrtimer-specific inside the timerfd_tmrproc(), except
the function prototype. We're about to add other timer types, so factor
out generic timerfd_expire() helper from timerfd_tmrproc().

Signed-off-by: Anton Vorontsov <anton.v...@linaro.org>
Signed-off-by: Alexey Perevalov <a.per...@samsung.com>
---
fs/timerfd.c | 40 +++++++++++++++++++---------------------
1 file changed, 19 insertions(+), 21 deletions(-)

diff --git a/fs/timerfd.c b/fs/timerfd.c
index 9293121..3561ce7 100644
--- a/fs/timerfd.c
+++ b/fs/timerfd.c
@@ -229,6 +229,23 @@ static unsigned int timerfd_poll(struct file *file, poll_table *wait)
return events;
}

+static u64 timerfd_rearm(struct timerfd_ctx *ctx)
+{
+ u64 orun;
+
+ if (isalarm(ctx)) {
+ orun += alarm_forward_now(
+ &ctx->t.alarm, ctx->tintv) - 1;
+ alarm_restart(&ctx->t.alarm);
+ } else {
+ orun += hrtimer_forward_now(&ctx->t.tmr,
+ ctx->tintv) - 1;
+ hrtimer_restart(&ctx->t.tmr);
+ }
+
+ return orun;
+}
+
static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
loff_t *ppos)
{
@@ -265,15 +282,7 @@ static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
* callback to avoid DoS attacks specifying a very
* short timer period.
*/
- if (isalarm(ctx)) {
- ticks += alarm_forward_now(
- &ctx->t.alarm, ctx->tintv) - 1;
- alarm_restart(&ctx->t.alarm);
- } else {
- ticks += hrtimer_forward_now(&ctx->t.tmr,
- ctx->tintv) - 1;
- hrtimer_restart(&ctx->t.tmr);
- }
+ ticks += timerfd_rearm(ctx);
}
ctx->expired = 0;
ctx->ticks = 0;
@@ -421,18 +430,7 @@ static int do_timerfd_gettime(int ufd, struct itimerspec *t)
spin_lock_irq(&ctx->wqh.lock);
if (ctx->expired && ctx->tintv.tv64) {
ctx->expired = 0;
-
- if (isalarm(ctx)) {
- ctx->ticks +=
- alarm_forward_now(
- &ctx->t.alarm, ctx->tintv) - 1;
- alarm_restart(&ctx->t.alarm);
- } else {
- ctx->ticks +=
- hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
- - 1;
- hrtimer_restart(&ctx->t.tmr);
- }
+ ctx->ticks += timerfd_rearm(ctx);
}
t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
t->it_interval = ktime_to_timespec(ctx->tintv);

Chanwoo Choi

unread,
Jan 13, 2014, 7:20:01 PM1/13/14
to
On 01/13/2014 07:43 PM, Alexey Perevalov wrote:
> From: Anton Vorontsov <an...@scarybugs.org>
>
> Two new functions: jiffies_to_ktime() and ktime_to_jiffies(), we'll use
> them for timerfd deferred timers handling.
>
> We fully reuse the logic from timespec implementations, so the functions
> are pretty straightforward.
>
> The only tricky part is in headers: we have to include jiffies.h after
> we defined ktime_t, this is because ktime.h needs some declarations from
> jiffies.h (e.g. TICK_NSEC).
>
> Signed-off-by: Anton Vorontsov <anton.v...@linaro.org>
> Signed-off-by: Alexey Perevalov <a.per...@samsung.com>

Tested-by: Chanwoo Choi <cw00...@samsung.com>

I tested this patchset about operation of deferrable timer on user-space.

Thanks,
Chanwoo Choi
0 new messages