Lines Matching refs:ctx

49 static inline bool isalarm(struct timerfd_ctx *ctx)  in isalarm()  argument
51 return ctx->clockid == CLOCK_REALTIME_ALARM || in isalarm()
52 ctx->clockid == CLOCK_BOOTTIME_ALARM; in isalarm()
60 static void timerfd_triggered(struct timerfd_ctx *ctx) in timerfd_triggered() argument
64 spin_lock_irqsave(&ctx->wqh.lock, flags); in timerfd_triggered()
65 ctx->expired = 1; in timerfd_triggered()
66 ctx->ticks++; in timerfd_triggered()
67 wake_up_locked(&ctx->wqh); in timerfd_triggered()
68 spin_unlock_irqrestore(&ctx->wqh.lock, flags); in timerfd_triggered()
73 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, in timerfd_tmrproc() local
75 timerfd_triggered(ctx); in timerfd_tmrproc()
82 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx, in timerfd_alarmproc() local
84 timerfd_triggered(ctx); in timerfd_alarmproc()
97 struct timerfd_ctx *ctx; in timerfd_clock_was_set() local
101 list_for_each_entry_rcu(ctx, &cancel_list, clist) { in timerfd_clock_was_set()
102 if (!ctx->might_cancel) in timerfd_clock_was_set()
104 spin_lock_irqsave(&ctx->wqh.lock, flags); in timerfd_clock_was_set()
105 if (ctx->moffs.tv64 != moffs.tv64) { in timerfd_clock_was_set()
106 ctx->moffs.tv64 = KTIME_MAX; in timerfd_clock_was_set()
107 ctx->ticks++; in timerfd_clock_was_set()
108 wake_up_locked(&ctx->wqh); in timerfd_clock_was_set()
110 spin_unlock_irqrestore(&ctx->wqh.lock, flags); in timerfd_clock_was_set()
115 static void timerfd_remove_cancel(struct timerfd_ctx *ctx) in timerfd_remove_cancel() argument
117 if (ctx->might_cancel) { in timerfd_remove_cancel()
118 ctx->might_cancel = false; in timerfd_remove_cancel()
120 list_del_rcu(&ctx->clist); in timerfd_remove_cancel()
125 static bool timerfd_canceled(struct timerfd_ctx *ctx) in timerfd_canceled() argument
127 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX) in timerfd_canceled()
129 ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 }); in timerfd_canceled()
133 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags) in timerfd_setup_cancel() argument
135 if ((ctx->clockid == CLOCK_REALTIME || in timerfd_setup_cancel()
136 ctx->clockid == CLOCK_REALTIME_ALARM) && in timerfd_setup_cancel()
138 if (!ctx->might_cancel) { in timerfd_setup_cancel()
139 ctx->might_cancel = true; in timerfd_setup_cancel()
141 list_add_rcu(&ctx->clist, &cancel_list); in timerfd_setup_cancel()
144 } else if (ctx->might_cancel) { in timerfd_setup_cancel()
145 timerfd_remove_cancel(ctx); in timerfd_setup_cancel()
149 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx) in timerfd_get_remaining() argument
153 if (isalarm(ctx)) in timerfd_get_remaining()
154 remaining = alarm_expires_remaining(&ctx->t.alarm); in timerfd_get_remaining()
156 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr); in timerfd_get_remaining()
161 static int timerfd_setup(struct timerfd_ctx *ctx, int flags, in timerfd_setup() argument
166 int clockid = ctx->clockid; in timerfd_setup()
172 ctx->expired = 0; in timerfd_setup()
173 ctx->ticks = 0; in timerfd_setup()
174 ctx->tintv = timespec_to_ktime(ktmr->it_interval); in timerfd_setup()
176 if (isalarm(ctx)) { in timerfd_setup()
177 alarm_init(&ctx->t.alarm, in timerfd_setup()
178 ctx->clockid == CLOCK_REALTIME_ALARM ? in timerfd_setup()
182 hrtimer_init(&ctx->t.tmr, clockid, htmode); in timerfd_setup()
183 hrtimer_set_expires(&ctx->t.tmr, texp); in timerfd_setup()
184 ctx->t.tmr.function = timerfd_tmrproc; in timerfd_setup()
188 if (isalarm(ctx)) { in timerfd_setup()
190 alarm_start(&ctx->t.alarm, texp); in timerfd_setup()
192 alarm_start_relative(&ctx->t.alarm, texp); in timerfd_setup()
194 hrtimer_start(&ctx->t.tmr, texp, htmode); in timerfd_setup()
197 if (timerfd_canceled(ctx)) in timerfd_setup()
201 ctx->settime_flags = flags & TFD_SETTIME_FLAGS; in timerfd_setup()
207 struct timerfd_ctx *ctx = file->private_data; in timerfd_release() local
209 timerfd_remove_cancel(ctx); in timerfd_release()
211 if (isalarm(ctx)) in timerfd_release()
212 alarm_cancel(&ctx->t.alarm); in timerfd_release()
214 hrtimer_cancel(&ctx->t.tmr); in timerfd_release()
215 kfree_rcu(ctx, rcu); in timerfd_release()
221 struct timerfd_ctx *ctx = file->private_data; in timerfd_poll() local
225 poll_wait(file, &ctx->wqh, wait); in timerfd_poll()
227 spin_lock_irqsave(&ctx->wqh.lock, flags); in timerfd_poll()
228 if (ctx->ticks) in timerfd_poll()
230 spin_unlock_irqrestore(&ctx->wqh.lock, flags); in timerfd_poll()
238 struct timerfd_ctx *ctx = file->private_data; in timerfd_read() local
244 spin_lock_irq(&ctx->wqh.lock); in timerfd_read()
248 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks); in timerfd_read()
255 if (timerfd_canceled(ctx)) { in timerfd_read()
256 ctx->ticks = 0; in timerfd_read()
257 ctx->expired = 0; in timerfd_read()
261 if (ctx->ticks) { in timerfd_read()
262 ticks = ctx->ticks; in timerfd_read()
264 if (ctx->expired && ctx->tintv.tv64) { in timerfd_read()
271 if (isalarm(ctx)) { in timerfd_read()
273 &ctx->t.alarm, ctx->tintv) - 1; in timerfd_read()
274 alarm_restart(&ctx->t.alarm); in timerfd_read()
276 ticks += hrtimer_forward_now(&ctx->t.tmr, in timerfd_read()
277 ctx->tintv) - 1; in timerfd_read()
278 hrtimer_restart(&ctx->t.tmr); in timerfd_read()
281 ctx->expired = 0; in timerfd_read()
282 ctx->ticks = 0; in timerfd_read()
284 spin_unlock_irq(&ctx->wqh.lock); in timerfd_read()
293 struct timerfd_ctx *ctx = file->private_data; in timerfd_show() local
296 spin_lock_irq(&ctx->wqh.lock); in timerfd_show()
297 t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); in timerfd_show()
298 t.it_interval = ktime_to_timespec(ctx->tintv); in timerfd_show()
299 spin_unlock_irq(&ctx->wqh.lock); in timerfd_show()
307 ctx->clockid, in timerfd_show()
308 (unsigned long long)ctx->ticks, in timerfd_show()
309 ctx->settime_flags, in timerfd_show()
322 struct timerfd_ctx *ctx = file->private_data; in timerfd_ioctl() local
334 spin_lock_irq(&ctx->wqh.lock); in timerfd_ioctl()
335 if (!timerfd_canceled(ctx)) { in timerfd_ioctl()
336 ctx->ticks = ticks; in timerfd_ioctl()
337 wake_up_locked(&ctx->wqh); in timerfd_ioctl()
340 spin_unlock_irq(&ctx->wqh.lock); in timerfd_ioctl()
379 struct timerfd_ctx *ctx; in SYSCALL_DEFINE2() local
393 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); in SYSCALL_DEFINE2()
394 if (!ctx) in SYSCALL_DEFINE2()
397 init_waitqueue_head(&ctx->wqh); in SYSCALL_DEFINE2()
398 ctx->clockid = clockid; in SYSCALL_DEFINE2()
400 if (isalarm(ctx)) in SYSCALL_DEFINE2()
401 alarm_init(&ctx->t.alarm, in SYSCALL_DEFINE2()
402 ctx->clockid == CLOCK_REALTIME_ALARM ? in SYSCALL_DEFINE2()
406 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS); in SYSCALL_DEFINE2()
408 ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 }); in SYSCALL_DEFINE2()
410 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx, in SYSCALL_DEFINE2()
413 kfree(ctx); in SYSCALL_DEFINE2()
423 struct timerfd_ctx *ctx; in do_timerfd_settime() local
434 ctx = f.file->private_data; in do_timerfd_settime()
436 timerfd_setup_cancel(ctx, flags); in do_timerfd_settime()
443 spin_lock_irq(&ctx->wqh.lock); in do_timerfd_settime()
445 if (isalarm(ctx)) { in do_timerfd_settime()
446 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0) in do_timerfd_settime()
449 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0) in do_timerfd_settime()
452 spin_unlock_irq(&ctx->wqh.lock); in do_timerfd_settime()
462 if (ctx->expired && ctx->tintv.tv64) { in do_timerfd_settime()
463 if (isalarm(ctx)) in do_timerfd_settime()
464 alarm_forward_now(&ctx->t.alarm, ctx->tintv); in do_timerfd_settime()
466 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv); in do_timerfd_settime()
469 old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); in do_timerfd_settime()
470 old->it_interval = ktime_to_timespec(ctx->tintv); in do_timerfd_settime()
475 ret = timerfd_setup(ctx, flags, new); in do_timerfd_settime()
477 spin_unlock_irq(&ctx->wqh.lock); in do_timerfd_settime()
485 struct timerfd_ctx *ctx; in do_timerfd_gettime() local
489 ctx = f.file->private_data; in do_timerfd_gettime()
491 spin_lock_irq(&ctx->wqh.lock); in do_timerfd_gettime()
492 if (ctx->expired && ctx->tintv.tv64) { in do_timerfd_gettime()
493 ctx->expired = 0; in do_timerfd_gettime()
495 if (isalarm(ctx)) { in do_timerfd_gettime()
496 ctx->ticks += in do_timerfd_gettime()
498 &ctx->t.alarm, ctx->tintv) - 1; in do_timerfd_gettime()
499 alarm_restart(&ctx->t.alarm); in do_timerfd_gettime()
501 ctx->ticks += in do_timerfd_gettime()
502 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv) in do_timerfd_gettime()
504 hrtimer_restart(&ctx->t.tmr); in do_timerfd_gettime()
507 t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); in do_timerfd_gettime()
508 t->it_interval = ktime_to_timespec(ctx->tintv); in do_timerfd_gettime()
509 spin_unlock_irq(&ctx->wqh.lock); in do_timerfd_gettime()