Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 248631
b: refs/heads/master
c: 99ee531
h: refs/heads/master
i:
  248629: f20bb54
  248627: c688706
  248623: 0b97740
v: v3
  • Loading branch information
Thomas Gleixner committed May 2, 2011
1 parent d109251 commit e088062
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 9 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: b12a03ce4880bd13786a98db6de494a3e0123129
refs/heads/master: 99ee5315dac6211e972fa3f23bcc9a0343ff58c4
57 changes: 51 additions & 6 deletions trunk/fs/timerfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
struct timerfd_ctx {
struct hrtimer tmr;
ktime_t tintv;
ktime_t moffs;
wait_queue_head_t wqh;
u64 ticks;
int expired;
int clockid;
bool might_cancel;
};

/*
Expand Down Expand Up @@ -59,24 +61,52 @@ static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
}

static void timerfd_setup(struct timerfd_ctx *ctx, int flags,
const struct itimerspec *ktmr)
static bool timerfd_canceled(struct timerfd_ctx *ctx)
{
ktime_t moffs;

if (!ctx->might_cancel)
return false;

moffs = ktime_get_monotonic_offset();

if (moffs.tv64 == ctx->moffs.tv64)
return false;

ctx->moffs = moffs;
return true;
}

static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
const struct itimerspec *ktmr)
{
enum hrtimer_mode htmode;
ktime_t texp;
int clockid = ctx->clockid;

htmode = (flags & TFD_TIMER_ABSTIME) ?
HRTIMER_MODE_ABS: HRTIMER_MODE_REL;

ctx->might_cancel = false;
if (htmode == HRTIMER_MODE_ABS && ctx->clockid == CLOCK_REALTIME &&
(flags & TFD_TIMER_CANCELON_SET)) {
clockid = CLOCK_REALTIME_COS;
ctx->might_cancel = true;
}

texp = timespec_to_ktime(ktmr->it_value);
ctx->expired = 0;
ctx->ticks = 0;
ctx->tintv = timespec_to_ktime(ktmr->it_interval);
hrtimer_init(&ctx->tmr, ctx->clockid, htmode);
hrtimer_init(&ctx->tmr, clockid, htmode);
hrtimer_set_expires(&ctx->tmr, texp);
ctx->tmr.function = timerfd_tmrproc;
if (texp.tv64 != 0)
if (texp.tv64 != 0) {
hrtimer_start(&ctx->tmr, texp, htmode);
if (timerfd_canceled(ctx))
return -ECANCELED;
}
return 0;
}

static int timerfd_release(struct inode *inode, struct file *file)
Expand Down Expand Up @@ -118,8 +148,21 @@ static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
res = -EAGAIN;
else
res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);

if (ctx->ticks) {
ticks = ctx->ticks;

/*
* If clock has changed, we do not care about the
* ticks and we do not rearm the timer. Userspace must
* reevaluate anyway.
*/
if (timerfd_canceled(ctx)) {
ticks = 0;
ctx->expired = 0;
res = -ECANCELED;
}

if (ctx->expired && ctx->tintv.tv64) {
/*
* If tintv.tv64 != 0, this is a periodic timer that
Expand Down Expand Up @@ -183,6 +226,7 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
init_waitqueue_head(&ctx->wqh);
ctx->clockid = clockid;
hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
ctx->moffs = ktime_get_monotonic_offset();

ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Expand All @@ -199,6 +243,7 @@ SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
struct file *file;
struct timerfd_ctx *ctx;
struct itimerspec ktmr, kotmr;
int ret;

if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
return -EFAULT;
Expand Down Expand Up @@ -240,14 +285,14 @@ SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
/*
* Re-program the timer to the new value ...
*/
timerfd_setup(ctx, flags, &ktmr);
ret = timerfd_setup(ctx, flags, &ktmr);

spin_unlock_irq(&ctx->wqh.lock);
fput(file);
if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
return -EFAULT;

return 0;
return ret;
}

SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
Expand Down
2 changes: 2 additions & 0 deletions trunk/include/linux/hrtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ enum hrtimer_base_type {
HRTIMER_BASE_REALTIME,
HRTIMER_BASE_MONOTONIC,
HRTIMER_BASE_BOOTTIME,
HRTIMER_BASE_REALTIME_COS,
HRTIMER_MAX_CLOCK_BASES,
};

Expand Down Expand Up @@ -310,6 +311,7 @@ extern void hrtimers_resume(void);
extern ktime_t ktime_get(void);
extern ktime_t ktime_get_real(void);
extern ktime_t ktime_get_boottime(void);
extern ktime_t ktime_get_monotonic_offset(void);

DECLARE_PER_CPU(struct tick_device, tick_cpu_device);

Expand Down
6 changes: 6 additions & 0 deletions trunk/include/linux/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ struct itimerval {
* The IDs of various hardware clocks:
*/
#define CLOCK_SGI_CYCLE 10

#ifdef __KERNEL__
/* This clock is not exposed to user space */
#define CLOCK_REALTIME_COS 15
#endif

#define MAX_CLOCKS 16
#define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC)
#define CLOCKS_MONO CLOCK_MONOTONIC
Expand Down
3 changes: 2 additions & 1 deletion trunk/include/linux/timerfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
* shared O_* flags.
*/
#define TFD_TIMER_ABSTIME (1 << 0)
#define TFD_TIMER_CANCELON_SET (1 << 1)
#define TFD_CLOEXEC O_CLOEXEC
#define TFD_NONBLOCK O_NONBLOCK

#define TFD_SHARED_FCNTL_FLAGS (TFD_CLOEXEC | TFD_NONBLOCK)
/* Flags for timerfd_create. */
#define TFD_CREATE_FLAGS TFD_SHARED_FCNTL_FLAGS
/* Flags for timerfd_settime. */
#define TFD_SETTIME_FLAGS TFD_TIMER_ABSTIME
#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCELON_SET)

#endif /* _LINUX_TIMERFD_H */
36 changes: 35 additions & 1 deletion trunk/kernel/hrtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,19 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
.get_time = &ktime_get_boottime,
.resolution = KTIME_LOW_RES,
},
{
.index = CLOCK_REALTIME_COS,
.get_time = &ktime_get_real,
.resolution = KTIME_LOW_RES,
},
}
};

static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
[CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
[CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
[CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
[CLOCK_REALTIME_COS] = HRTIMER_BASE_REALTIME_COS,
};

static inline int hrtimer_clockid_to_base(clockid_t clock_id)
Expand All @@ -110,6 +116,7 @@ static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
base->clock_base[HRTIMER_BASE_REALTIME_COS].softirq_time = xtim;
}

/*
Expand Down Expand Up @@ -479,6 +486,8 @@ static inline void debug_deactivate(struct hrtimer *timer)
trace_hrtimer_cancel(timer);
}

static void hrtimer_expire_cancelable(struct hrtimer_cpu_base *cpu_base);

/* High resolution timer related functions */
#ifdef CONFIG_HIGH_RES_TIMERS

Expand Down Expand Up @@ -715,9 +724,14 @@ static void retrigger_next_event(void *arg)
struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
struct timespec realtime_offset, xtim, wtm, sleep;

if (!hrtimer_hres_active())
if (!hrtimer_hres_active()) {
raw_spin_lock(&base->lock);
hrtimer_expire_cancelable(base);
raw_spin_unlock(&base->lock);
return;
}

/* Optimized out for !HIGH_RES */
get_xtime_and_monotonic_and_sleep_offset(&xtim, &wtm, &sleep);
set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);

Expand All @@ -727,6 +741,10 @@ static void retrigger_next_event(void *arg)
timespec_to_ktime(realtime_offset);
base->clock_base[HRTIMER_BASE_BOOTTIME].offset =
timespec_to_ktime(sleep);
base->clock_base[HRTIMER_BASE_REALTIME_COS].offset =
timespec_to_ktime(realtime_offset);

hrtimer_expire_cancelable(base);

hrtimer_force_reprogram(base, 0);
raw_spin_unlock(&base->lock);
Expand Down Expand Up @@ -1222,6 +1240,22 @@ static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
timer->state &= ~HRTIMER_STATE_CALLBACK;
}

static void hrtimer_expire_cancelable(struct hrtimer_cpu_base *cpu_base)
{
struct timerqueue_node *node;
struct hrtimer_clock_base *base;
ktime_t now = ktime_get_real();

base = &cpu_base->clock_base[HRTIMER_BASE_REALTIME_COS];

while ((node = timerqueue_getnext(&base->active))) {
struct hrtimer *timer;

timer = container_of(node, struct hrtimer, node);
__run_hrtimer(timer, &now);
}
}

#ifdef CONFIG_HIGH_RES_TIMERS

/*
Expand Down
15 changes: 15 additions & 0 deletions trunk/kernel/time/timekeeping.c
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,21 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
} while (read_seqretry(&xtime_lock, seq));
}

/**
* ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
*/
ktime_t ktime_get_monotonic_offset(void)
{
unsigned long seq;
struct timespec wtom;

do {
seq = read_seqbegin(&xtime_lock);
wtom = wall_to_monotonic;
} while (read_seqretry(&xtime_lock, seq));
return timespec_to_ktime(wtom);
}

/**
* xtime_update() - advances the timekeeping infrastructure
* @ticks: number of ticks, that have elapsed since the last call.
Expand Down

0 comments on commit e088062

Please sign in to comment.