1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/timerfd.c
4 *
5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
6 *
7 *
8 * Thanks to Thomas Gleixner for code reviews and useful comments.
9 *
10 */
11
12 #include <linux/alarmtimer.h>
13 #include <linux/file.h>
14 #include <linux/poll.h>
15 #include <linux/init.h>
16 #include <linux/fs.h>
17 #include <linux/sched.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/list.h>
21 #include <linux/spinlock.h>
22 #include <linux/time.h>
23 #include <linux/hrtimer.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/timerfd.h>
26 #include <linux/syscalls.h>
27 #include <linux/compat.h>
28 #include <linux/rcupdate.h>
29 #include <linux/time_namespace.h>
30
31 #include <trace/hooks/fs.h>
32
33 struct timerfd_ctx {
34 union {
35 struct hrtimer tmr;
36 struct alarm alarm;
37 } t;
38 ktime_t tintv;
39 ktime_t moffs;
40 wait_queue_head_t wqh;
41 u64 ticks;
42 int clockid;
43 short unsigned expired;
44 short unsigned settime_flags; /* to show in fdinfo */
45 struct rcu_head rcu;
46 struct list_head clist;
47 spinlock_t cancel_lock;
48 bool might_cancel;
49 };
50
51 static LIST_HEAD(cancel_list);
52 static DEFINE_SPINLOCK(cancel_lock);
53
isalarm(struct timerfd_ctx * ctx)54 static inline bool isalarm(struct timerfd_ctx *ctx)
55 {
56 return ctx->clockid == CLOCK_REALTIME_ALARM ||
57 ctx->clockid == CLOCK_BOOTTIME_ALARM;
58 }
59
60 /*
61 * This gets called when the timer event triggers. We set the "expired"
62 * flag, but we do not re-arm the timer (in case it's necessary,
63 * tintv != 0) until the timer is accessed.
64 */
timerfd_triggered(struct timerfd_ctx * ctx)65 static void timerfd_triggered(struct timerfd_ctx *ctx)
66 {
67 unsigned long flags;
68
69 spin_lock_irqsave(&ctx->wqh.lock, flags);
70 ctx->expired = 1;
71 ctx->ticks++;
72 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
73 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
74 }
75
timerfd_tmrproc(struct hrtimer * htmr)76 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
77 {
78 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
79 t.tmr);
80 timerfd_triggered(ctx);
81 return HRTIMER_NORESTART;
82 }
83
timerfd_alarmproc(struct alarm * alarm,ktime_t now)84 static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
85 ktime_t now)
86 {
87 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
88 t.alarm);
89 timerfd_triggered(ctx);
90 return ALARMTIMER_NORESTART;
91 }
92
93 /*
94 * Called when the clock was set to cancel the timers in the cancel
95 * list. This will wake up processes waiting on these timers. The
96 * wake-up requires ctx->ticks to be non zero, therefore we increment
97 * it before calling wake_up_locked().
98 */
timerfd_clock_was_set(void)99 void timerfd_clock_was_set(void)
100 {
101 ktime_t moffs = ktime_mono_to_real(0);
102 struct timerfd_ctx *ctx;
103 unsigned long flags;
104
105 rcu_read_lock();
106 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
107 if (!ctx->might_cancel)
108 continue;
109 spin_lock_irqsave(&ctx->wqh.lock, flags);
110 if (ctx->moffs != moffs) {
111 ctx->moffs = KTIME_MAX;
112 ctx->ticks++;
113 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
114 }
115 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
116 }
117 rcu_read_unlock();
118 }
119
timerfd_resume_work(struct work_struct * work)120 static void timerfd_resume_work(struct work_struct *work)
121 {
122 timerfd_clock_was_set();
123 }
124
125 static DECLARE_WORK(timerfd_work, timerfd_resume_work);
126
127 /*
128 * Invoked from timekeeping_resume(). Defer the actual update to work so
129 * timerfd_clock_was_set() runs in task context.
130 */
timerfd_resume(void)131 void timerfd_resume(void)
132 {
133 schedule_work(&timerfd_work);
134 }
135
__timerfd_remove_cancel(struct timerfd_ctx * ctx)136 static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
137 {
138 if (ctx->might_cancel) {
139 ctx->might_cancel = false;
140 spin_lock(&cancel_lock);
141 list_del_rcu(&ctx->clist);
142 spin_unlock(&cancel_lock);
143 }
144 }
145
timerfd_remove_cancel(struct timerfd_ctx * ctx)146 static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
147 {
148 spin_lock(&ctx->cancel_lock);
149 __timerfd_remove_cancel(ctx);
150 spin_unlock(&ctx->cancel_lock);
151 }
152
timerfd_canceled(struct timerfd_ctx * ctx)153 static bool timerfd_canceled(struct timerfd_ctx *ctx)
154 {
155 if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
156 return false;
157 ctx->moffs = ktime_mono_to_real(0);
158 return true;
159 }
160
timerfd_setup_cancel(struct timerfd_ctx * ctx,int flags)161 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
162 {
163 spin_lock(&ctx->cancel_lock);
164 if ((ctx->clockid == CLOCK_REALTIME ||
165 ctx->clockid == CLOCK_REALTIME_ALARM) &&
166 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
167 if (!ctx->might_cancel) {
168 ctx->might_cancel = true;
169 spin_lock(&cancel_lock);
170 list_add_rcu(&ctx->clist, &cancel_list);
171 spin_unlock(&cancel_lock);
172 }
173 } else {
174 __timerfd_remove_cancel(ctx);
175 }
176 spin_unlock(&ctx->cancel_lock);
177 }
178
timerfd_get_remaining(struct timerfd_ctx * ctx)179 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
180 {
181 ktime_t remaining;
182
183 if (isalarm(ctx))
184 remaining = alarm_expires_remaining(&ctx->t.alarm);
185 else
186 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
187
188 return remaining < 0 ? 0: remaining;
189 }
190
timerfd_setup(struct timerfd_ctx * ctx,int flags,const struct itimerspec64 * ktmr)191 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
192 const struct itimerspec64 *ktmr)
193 {
194 enum hrtimer_mode htmode;
195 ktime_t texp;
196 int clockid = ctx->clockid;
197
198 htmode = (flags & TFD_TIMER_ABSTIME) ?
199 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
200
201 texp = timespec64_to_ktime(ktmr->it_value);
202 ctx->expired = 0;
203 ctx->ticks = 0;
204 ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
205
206 if (isalarm(ctx)) {
207 alarm_init(&ctx->t.alarm,
208 ctx->clockid == CLOCK_REALTIME_ALARM ?
209 ALARM_REALTIME : ALARM_BOOTTIME,
210 timerfd_alarmproc);
211 } else {
212 hrtimer_init(&ctx->t.tmr, clockid, htmode);
213 hrtimer_set_expires(&ctx->t.tmr, texp);
214 ctx->t.tmr.function = timerfd_tmrproc;
215 }
216
217 if (texp != 0) {
218 if (flags & TFD_TIMER_ABSTIME)
219 texp = timens_ktime_to_host(clockid, texp);
220 if (isalarm(ctx)) {
221 if (flags & TFD_TIMER_ABSTIME)
222 alarm_start(&ctx->t.alarm, texp);
223 else
224 alarm_start_relative(&ctx->t.alarm, texp);
225 } else {
226 hrtimer_start(&ctx->t.tmr, texp, htmode);
227 }
228
229 if (timerfd_canceled(ctx))
230 return -ECANCELED;
231 }
232
233 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
234 return 0;
235 }
236
timerfd_release(struct inode * inode,struct file * file)237 static int timerfd_release(struct inode *inode, struct file *file)
238 {
239 struct timerfd_ctx *ctx = file->private_data;
240
241 timerfd_remove_cancel(ctx);
242
243 if (isalarm(ctx))
244 alarm_cancel(&ctx->t.alarm);
245 else
246 hrtimer_cancel(&ctx->t.tmr);
247 kfree_rcu(ctx, rcu);
248 return 0;
249 }
250
timerfd_poll(struct file * file,poll_table * wait)251 static __poll_t timerfd_poll(struct file *file, poll_table *wait)
252 {
253 struct timerfd_ctx *ctx = file->private_data;
254 __poll_t events = 0;
255 unsigned long flags;
256
257 poll_wait(file, &ctx->wqh, wait);
258
259 spin_lock_irqsave(&ctx->wqh.lock, flags);
260 if (ctx->ticks)
261 events |= EPOLLIN;
262 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
263
264 return events;
265 }
266
timerfd_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)267 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
268 loff_t *ppos)
269 {
270 struct timerfd_ctx *ctx = file->private_data;
271 ssize_t res;
272 u64 ticks = 0;
273
274 if (count < sizeof(ticks))
275 return -EINVAL;
276 spin_lock_irq(&ctx->wqh.lock);
277 if (file->f_flags & O_NONBLOCK)
278 res = -EAGAIN;
279 else
280 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
281
282 /*
283 * If clock has changed, we do not care about the
284 * ticks and we do not rearm the timer. Userspace must
285 * reevaluate anyway.
286 */
287 if (timerfd_canceled(ctx)) {
288 ctx->ticks = 0;
289 ctx->expired = 0;
290 res = -ECANCELED;
291 }
292
293 if (ctx->ticks) {
294 ticks = ctx->ticks;
295
296 if (ctx->expired && ctx->tintv) {
297 /*
298 * If tintv != 0, this is a periodic timer that
299 * needs to be re-armed. We avoid doing it in the timer
300 * callback to avoid DoS attacks specifying a very
301 * short timer period.
302 */
303 if (isalarm(ctx)) {
304 ticks += alarm_forward_now(
305 &ctx->t.alarm, ctx->tintv) - 1;
306 alarm_restart(&ctx->t.alarm);
307 } else {
308 ticks += hrtimer_forward_now(&ctx->t.tmr,
309 ctx->tintv) - 1;
310 hrtimer_restart(&ctx->t.tmr);
311 }
312 }
313 ctx->expired = 0;
314 ctx->ticks = 0;
315 }
316 spin_unlock_irq(&ctx->wqh.lock);
317 if (ticks)
318 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
319 return res;
320 }
321
322 #ifdef CONFIG_PROC_FS
timerfd_show(struct seq_file * m,struct file * file)323 static void timerfd_show(struct seq_file *m, struct file *file)
324 {
325 struct timerfd_ctx *ctx = file->private_data;
326 struct timespec64 value, interval;
327
328 spin_lock_irq(&ctx->wqh.lock);
329 value = ktime_to_timespec64(timerfd_get_remaining(ctx));
330 interval = ktime_to_timespec64(ctx->tintv);
331 spin_unlock_irq(&ctx->wqh.lock);
332
333 seq_printf(m,
334 "clockid: %d\n"
335 "ticks: %llu\n"
336 "settime flags: 0%o\n"
337 "it_value: (%llu, %llu)\n"
338 "it_interval: (%llu, %llu)\n",
339 ctx->clockid,
340 (unsigned long long)ctx->ticks,
341 ctx->settime_flags,
342 (unsigned long long)value.tv_sec,
343 (unsigned long long)value.tv_nsec,
344 (unsigned long long)interval.tv_sec,
345 (unsigned long long)interval.tv_nsec);
346 }
347 #else
348 #define timerfd_show NULL
349 #endif
350
351 #ifdef CONFIG_CHECKPOINT_RESTORE
timerfd_ioctl(struct file * file,unsigned int cmd,unsigned long arg)352 static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
353 {
354 struct timerfd_ctx *ctx = file->private_data;
355 int ret = 0;
356
357 switch (cmd) {
358 case TFD_IOC_SET_TICKS: {
359 u64 ticks;
360
361 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
362 return -EFAULT;
363 if (!ticks)
364 return -EINVAL;
365
366 spin_lock_irq(&ctx->wqh.lock);
367 if (!timerfd_canceled(ctx)) {
368 ctx->ticks = ticks;
369 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
370 } else
371 ret = -ECANCELED;
372 spin_unlock_irq(&ctx->wqh.lock);
373 break;
374 }
375 default:
376 ret = -ENOTTY;
377 break;
378 }
379
380 return ret;
381 }
382 #else
383 #define timerfd_ioctl NULL
384 #endif
385
386 static const struct file_operations timerfd_fops = {
387 .release = timerfd_release,
388 .poll = timerfd_poll,
389 .read = timerfd_read,
390 .llseek = noop_llseek,
391 .show_fdinfo = timerfd_show,
392 .unlocked_ioctl = timerfd_ioctl,
393 };
394
timerfd_fget(int fd,struct fd * p)395 static int timerfd_fget(int fd, struct fd *p)
396 {
397 struct fd f = fdget(fd);
398 if (!f.file)
399 return -EBADF;
400 if (f.file->f_op != &timerfd_fops) {
401 fdput(f);
402 return -EINVAL;
403 }
404 *p = f;
405 return 0;
406 }
407
SYSCALL_DEFINE2(timerfd_create,int,clockid,int,flags)408 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
409 {
410 int ufd;
411 struct timerfd_ctx *ctx;
412 char file_name_buf[32];
413
414 /* Check the TFD_* constants for consistency. */
415 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
416 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
417
418 if ((flags & ~TFD_CREATE_FLAGS) ||
419 (clockid != CLOCK_MONOTONIC &&
420 clockid != CLOCK_REALTIME &&
421 clockid != CLOCK_REALTIME_ALARM &&
422 clockid != CLOCK_BOOTTIME &&
423 clockid != CLOCK_BOOTTIME_ALARM))
424 return -EINVAL;
425
426 if ((clockid == CLOCK_REALTIME_ALARM ||
427 clockid == CLOCK_BOOTTIME_ALARM) &&
428 !capable(CAP_WAKE_ALARM))
429 return -EPERM;
430
431 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
432 if (!ctx)
433 return -ENOMEM;
434
435 init_waitqueue_head(&ctx->wqh);
436 spin_lock_init(&ctx->cancel_lock);
437 ctx->clockid = clockid;
438
439 if (isalarm(ctx))
440 alarm_init(&ctx->t.alarm,
441 ctx->clockid == CLOCK_REALTIME_ALARM ?
442 ALARM_REALTIME : ALARM_BOOTTIME,
443 timerfd_alarmproc);
444 else
445 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
446
447 ctx->moffs = ktime_mono_to_real(0);
448
449 strscpy(file_name_buf, "[timerfd]", sizeof(file_name_buf));
450 trace_android_vh_timerfd_create(file_name_buf, sizeof(file_name_buf));
451 ufd = anon_inode_getfd(file_name_buf, &timerfd_fops, ctx,
452 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
453 if (ufd < 0)
454 kfree(ctx);
455
456 return ufd;
457 }
458
do_timerfd_settime(int ufd,int flags,const struct itimerspec64 * new,struct itimerspec64 * old)459 static int do_timerfd_settime(int ufd, int flags,
460 const struct itimerspec64 *new,
461 struct itimerspec64 *old)
462 {
463 struct fd f;
464 struct timerfd_ctx *ctx;
465 int ret;
466
467 if ((flags & ~TFD_SETTIME_FLAGS) ||
468 !itimerspec64_valid(new))
469 return -EINVAL;
470
471 ret = timerfd_fget(ufd, &f);
472 if (ret)
473 return ret;
474 ctx = f.file->private_data;
475
476 if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
477 fdput(f);
478 return -EPERM;
479 }
480
481 timerfd_setup_cancel(ctx, flags);
482
483 /*
484 * We need to stop the existing timer before reprogramming
485 * it to the new values.
486 */
487 for (;;) {
488 spin_lock_irq(&ctx->wqh.lock);
489
490 if (isalarm(ctx)) {
491 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
492 break;
493 } else {
494 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
495 break;
496 }
497 spin_unlock_irq(&ctx->wqh.lock);
498
499 if (isalarm(ctx))
500 hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
501 else
502 hrtimer_cancel_wait_running(&ctx->t.tmr);
503 }
504
505 /*
506 * If the timer is expired and it's periodic, we need to advance it
507 * because the caller may want to know the previous expiration time.
508 * We do not update "ticks" and "expired" since the timer will be
509 * re-programmed again in the following timerfd_setup() call.
510 */
511 if (ctx->expired && ctx->tintv) {
512 if (isalarm(ctx))
513 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
514 else
515 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
516 }
517
518 old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
519 old->it_interval = ktime_to_timespec64(ctx->tintv);
520
521 /*
522 * Re-program the timer to the new value ...
523 */
524 ret = timerfd_setup(ctx, flags, new);
525
526 spin_unlock_irq(&ctx->wqh.lock);
527 fdput(f);
528 return ret;
529 }
530
do_timerfd_gettime(int ufd,struct itimerspec64 * t)531 static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
532 {
533 struct fd f;
534 struct timerfd_ctx *ctx;
535 int ret = timerfd_fget(ufd, &f);
536 if (ret)
537 return ret;
538 ctx = f.file->private_data;
539
540 spin_lock_irq(&ctx->wqh.lock);
541 if (ctx->expired && ctx->tintv) {
542 ctx->expired = 0;
543
544 if (isalarm(ctx)) {
545 ctx->ticks +=
546 alarm_forward_now(
547 &ctx->t.alarm, ctx->tintv) - 1;
548 alarm_restart(&ctx->t.alarm);
549 } else {
550 ctx->ticks +=
551 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
552 - 1;
553 hrtimer_restart(&ctx->t.tmr);
554 }
555 }
556 t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
557 t->it_interval = ktime_to_timespec64(ctx->tintv);
558 spin_unlock_irq(&ctx->wqh.lock);
559 fdput(f);
560 return 0;
561 }
562
SYSCALL_DEFINE4(timerfd_settime,int,ufd,int,flags,const struct __kernel_itimerspec __user *,utmr,struct __kernel_itimerspec __user *,otmr)563 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
564 const struct __kernel_itimerspec __user *, utmr,
565 struct __kernel_itimerspec __user *, otmr)
566 {
567 struct itimerspec64 new, old;
568 int ret;
569
570 if (get_itimerspec64(&new, utmr))
571 return -EFAULT;
572 ret = do_timerfd_settime(ufd, flags, &new, &old);
573 if (ret)
574 return ret;
575 if (otmr && put_itimerspec64(&old, otmr))
576 return -EFAULT;
577
578 return ret;
579 }
580
SYSCALL_DEFINE2(timerfd_gettime,int,ufd,struct __kernel_itimerspec __user *,otmr)581 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
582 {
583 struct itimerspec64 kotmr;
584 int ret = do_timerfd_gettime(ufd, &kotmr);
585 if (ret)
586 return ret;
587 return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
588 }
589
590 #ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE4(timerfd_settime32,int,ufd,int,flags,const struct old_itimerspec32 __user *,utmr,struct old_itimerspec32 __user *,otmr)591 SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
592 const struct old_itimerspec32 __user *, utmr,
593 struct old_itimerspec32 __user *, otmr)
594 {
595 struct itimerspec64 new, old;
596 int ret;
597
598 if (get_old_itimerspec32(&new, utmr))
599 return -EFAULT;
600 ret = do_timerfd_settime(ufd, flags, &new, &old);
601 if (ret)
602 return ret;
603 if (otmr && put_old_itimerspec32(&old, otmr))
604 return -EFAULT;
605 return ret;
606 }
607
SYSCALL_DEFINE2(timerfd_gettime32,int,ufd,struct old_itimerspec32 __user *,otmr)608 SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
609 struct old_itimerspec32 __user *, otmr)
610 {
611 struct itimerspec64 kotmr;
612 int ret = do_timerfd_gettime(ufd, &kotmr);
613 if (ret)
614 return ret;
615 return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;
616 }
617 #endif
618