Lines Matching refs:ctx
52 static inline bool isalarm(struct timerfd_ctx *ctx) in isalarm() argument
54 return ctx->clockid == CLOCK_REALTIME_ALARM || in isalarm()
55 ctx->clockid == CLOCK_BOOTTIME_ALARM; in isalarm()
63 static void timerfd_triggered(struct timerfd_ctx *ctx) in timerfd_triggered() argument
67 spin_lock_irqsave(&ctx->wqh.lock, flags); in timerfd_triggered()
68 ctx->expired = 1; in timerfd_triggered()
69 ctx->ticks++; in timerfd_triggered()
70 wake_up_locked_poll(&ctx->wqh, EPOLLIN); in timerfd_triggered()
71 spin_unlock_irqrestore(&ctx->wqh.lock, flags); in timerfd_triggered()
76 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, in timerfd_tmrproc() local
78 timerfd_triggered(ctx); in timerfd_tmrproc()
85 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx, in timerfd_alarmproc() local
87 timerfd_triggered(ctx); in timerfd_alarmproc()
100 struct timerfd_ctx *ctx; in timerfd_clock_was_set() local
104 list_for_each_entry_rcu(ctx, &cancel_list, clist) { in timerfd_clock_was_set()
105 if (!ctx->might_cancel) in timerfd_clock_was_set()
107 spin_lock_irqsave(&ctx->wqh.lock, flags); in timerfd_clock_was_set()
108 if (ctx->moffs != moffs) { in timerfd_clock_was_set()
109 ctx->moffs = KTIME_MAX; in timerfd_clock_was_set()
110 ctx->ticks++; in timerfd_clock_was_set()
111 wake_up_locked_poll(&ctx->wqh, EPOLLIN); in timerfd_clock_was_set()
113 spin_unlock_irqrestore(&ctx->wqh.lock, flags); in timerfd_clock_was_set()
134 static void __timerfd_remove_cancel(struct timerfd_ctx *ctx) in __timerfd_remove_cancel() argument
136 if (ctx->might_cancel) { in __timerfd_remove_cancel()
137 ctx->might_cancel = false; in __timerfd_remove_cancel()
139 list_del_rcu(&ctx->clist); in __timerfd_remove_cancel()
144 static void timerfd_remove_cancel(struct timerfd_ctx *ctx) in timerfd_remove_cancel() argument
146 spin_lock(&ctx->cancel_lock); in timerfd_remove_cancel()
147 __timerfd_remove_cancel(ctx); in timerfd_remove_cancel()
148 spin_unlock(&ctx->cancel_lock); in timerfd_remove_cancel()
151 static bool timerfd_canceled(struct timerfd_ctx *ctx) in timerfd_canceled() argument
153 if (!ctx->might_cancel || ctx->moffs != KTIME_MAX) in timerfd_canceled()
155 ctx->moffs = ktime_mono_to_real(0); in timerfd_canceled()
159 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags) in timerfd_setup_cancel() argument
161 spin_lock(&ctx->cancel_lock); in timerfd_setup_cancel()
162 if ((ctx->clockid == CLOCK_REALTIME || in timerfd_setup_cancel()
163 ctx->clockid == CLOCK_REALTIME_ALARM) && in timerfd_setup_cancel()
165 if (!ctx->might_cancel) { in timerfd_setup_cancel()
166 ctx->might_cancel = true; in timerfd_setup_cancel()
168 list_add_rcu(&ctx->clist, &cancel_list); in timerfd_setup_cancel()
172 __timerfd_remove_cancel(ctx); in timerfd_setup_cancel()
174 spin_unlock(&ctx->cancel_lock); in timerfd_setup_cancel()
177 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx) in timerfd_get_remaining() argument
181 if (isalarm(ctx)) in timerfd_get_remaining()
182 remaining = alarm_expires_remaining(&ctx->t.alarm); in timerfd_get_remaining()
184 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr); in timerfd_get_remaining()
189 static int timerfd_setup(struct timerfd_ctx *ctx, int flags, in timerfd_setup() argument
194 int clockid = ctx->clockid; in timerfd_setup()
200 ctx->expired = 0; in timerfd_setup()
201 ctx->ticks = 0; in timerfd_setup()
202 ctx->tintv = timespec64_to_ktime(ktmr->it_interval); in timerfd_setup()
204 if (isalarm(ctx)) { in timerfd_setup()
205 alarm_init(&ctx->t.alarm, in timerfd_setup()
206 ctx->clockid == CLOCK_REALTIME_ALARM ? in timerfd_setup()
210 hrtimer_init(&ctx->t.tmr, clockid, htmode); in timerfd_setup()
211 hrtimer_set_expires(&ctx->t.tmr, texp); in timerfd_setup()
212 ctx->t.tmr.function = timerfd_tmrproc; in timerfd_setup()
218 if (isalarm(ctx)) { in timerfd_setup()
220 alarm_start(&ctx->t.alarm, texp); in timerfd_setup()
222 alarm_start_relative(&ctx->t.alarm, texp); in timerfd_setup()
224 hrtimer_start(&ctx->t.tmr, texp, htmode); in timerfd_setup()
227 if (timerfd_canceled(ctx)) in timerfd_setup()
231 ctx->settime_flags = flags & TFD_SETTIME_FLAGS; in timerfd_setup()
237 struct timerfd_ctx *ctx = file->private_data; in timerfd_release() local
239 timerfd_remove_cancel(ctx); in timerfd_release()
241 if (isalarm(ctx)) in timerfd_release()
242 alarm_cancel(&ctx->t.alarm); in timerfd_release()
244 hrtimer_cancel(&ctx->t.tmr); in timerfd_release()
245 kfree_rcu(ctx, rcu); in timerfd_release()
251 struct timerfd_ctx *ctx = file->private_data; in timerfd_poll() local
255 poll_wait(file, &ctx->wqh, wait); in timerfd_poll()
257 spin_lock_irqsave(&ctx->wqh.lock, flags); in timerfd_poll()
258 if (ctx->ticks) in timerfd_poll()
260 spin_unlock_irqrestore(&ctx->wqh.lock, flags); in timerfd_poll()
268 struct timerfd_ctx *ctx = file->private_data; in timerfd_read() local
274 spin_lock_irq(&ctx->wqh.lock); in timerfd_read()
278 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks); in timerfd_read()
285 if (timerfd_canceled(ctx)) { in timerfd_read()
286 ctx->ticks = 0; in timerfd_read()
287 ctx->expired = 0; in timerfd_read()
291 if (ctx->ticks) { in timerfd_read()
292 ticks = ctx->ticks; in timerfd_read()
294 if (ctx->expired && ctx->tintv) { in timerfd_read()
301 if (isalarm(ctx)) { in timerfd_read()
303 &ctx->t.alarm, ctx->tintv) - 1; in timerfd_read()
304 alarm_restart(&ctx->t.alarm); in timerfd_read()
306 ticks += hrtimer_forward_now(&ctx->t.tmr, in timerfd_read()
307 ctx->tintv) - 1; in timerfd_read()
308 hrtimer_restart(&ctx->t.tmr); in timerfd_read()
311 ctx->expired = 0; in timerfd_read()
312 ctx->ticks = 0; in timerfd_read()
314 spin_unlock_irq(&ctx->wqh.lock); in timerfd_read()
323 struct timerfd_ctx *ctx = file->private_data; in timerfd_show() local
326 spin_lock_irq(&ctx->wqh.lock); in timerfd_show()
327 value = ktime_to_timespec64(timerfd_get_remaining(ctx)); in timerfd_show()
328 interval = ktime_to_timespec64(ctx->tintv); in timerfd_show()
329 spin_unlock_irq(&ctx->wqh.lock); in timerfd_show()
337 ctx->clockid, in timerfd_show()
338 (unsigned long long)ctx->ticks, in timerfd_show()
339 ctx->settime_flags, in timerfd_show()
352 struct timerfd_ctx *ctx = file->private_data; in timerfd_ioctl() local
364 spin_lock_irq(&ctx->wqh.lock); in timerfd_ioctl()
365 if (!timerfd_canceled(ctx)) { in timerfd_ioctl()
366 ctx->ticks = ticks; in timerfd_ioctl()
367 wake_up_locked_poll(&ctx->wqh, EPOLLIN); in timerfd_ioctl()
370 spin_unlock_irq(&ctx->wqh.lock); in timerfd_ioctl()
409 struct timerfd_ctx *ctx; in SYSCALL_DEFINE2() local
428 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); in SYSCALL_DEFINE2()
429 if (!ctx) in SYSCALL_DEFINE2()
432 init_waitqueue_head(&ctx->wqh); in SYSCALL_DEFINE2()
433 spin_lock_init(&ctx->cancel_lock); in SYSCALL_DEFINE2()
434 ctx->clockid = clockid; in SYSCALL_DEFINE2()
436 if (isalarm(ctx)) in SYSCALL_DEFINE2()
437 alarm_init(&ctx->t.alarm, in SYSCALL_DEFINE2()
438 ctx->clockid == CLOCK_REALTIME_ALARM ? in SYSCALL_DEFINE2()
442 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS); in SYSCALL_DEFINE2()
444 ctx->moffs = ktime_mono_to_real(0); in SYSCALL_DEFINE2()
446 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx, in SYSCALL_DEFINE2()
449 kfree(ctx); in SYSCALL_DEFINE2()
459 struct timerfd_ctx *ctx; in do_timerfd_settime() local
469 ctx = f.file->private_data; in do_timerfd_settime()
471 if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) { in do_timerfd_settime()
476 timerfd_setup_cancel(ctx, flags); in do_timerfd_settime()
483 spin_lock_irq(&ctx->wqh.lock); in do_timerfd_settime()
485 if (isalarm(ctx)) { in do_timerfd_settime()
486 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0) in do_timerfd_settime()
489 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0) in do_timerfd_settime()
492 spin_unlock_irq(&ctx->wqh.lock); in do_timerfd_settime()
494 if (isalarm(ctx)) in do_timerfd_settime()
495 hrtimer_cancel_wait_running(&ctx->t.alarm.timer); in do_timerfd_settime()
497 hrtimer_cancel_wait_running(&ctx->t.tmr); in do_timerfd_settime()
506 if (ctx->expired && ctx->tintv) { in do_timerfd_settime()
507 if (isalarm(ctx)) in do_timerfd_settime()
508 alarm_forward_now(&ctx->t.alarm, ctx->tintv); in do_timerfd_settime()
510 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv); in do_timerfd_settime()
513 old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx)); in do_timerfd_settime()
514 old->it_interval = ktime_to_timespec64(ctx->tintv); in do_timerfd_settime()
519 ret = timerfd_setup(ctx, flags, new); in do_timerfd_settime()
521 spin_unlock_irq(&ctx->wqh.lock); in do_timerfd_settime()
529 struct timerfd_ctx *ctx; in do_timerfd_gettime() local
533 ctx = f.file->private_data; in do_timerfd_gettime()
535 spin_lock_irq(&ctx->wqh.lock); in do_timerfd_gettime()
536 if (ctx->expired && ctx->tintv) { in do_timerfd_gettime()
537 ctx->expired = 0; in do_timerfd_gettime()
539 if (isalarm(ctx)) { in do_timerfd_gettime()
540 ctx->ticks += in do_timerfd_gettime()
542 &ctx->t.alarm, ctx->tintv) - 1; in do_timerfd_gettime()
543 alarm_restart(&ctx->t.alarm); in do_timerfd_gettime()
545 ctx->ticks += in do_timerfd_gettime()
546 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv) in do_timerfd_gettime()
548 hrtimer_restart(&ctx->t.tmr); in do_timerfd_gettime()
551 t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx)); in do_timerfd_gettime()
552 t->it_interval = ktime_to_timespec64(ctx->tintv); in do_timerfd_gettime()
553 spin_unlock_irq(&ctx->wqh.lock); in do_timerfd_gettime()