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