sys_timerfd.c revision 1.2 1 /* $NetBSD: sys_timerfd.c,v 1.2 2021/09/19 15:51:27 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sys_timerfd.c,v 1.2 2021/09/19 15:51:27 thorpej Exp $");
34
35 /*
36 * timerfd
37 *
38 * Timerfd objects are similar to POSIX timers, except they are associated
39 * with a file descriptor rather than a process. Timerfd objects are
40 * created with the timerfd_create(2) system call, similar to timer_create(2).
41 * The timerfd analogues for timer_gettime(2) and timer_settime(2) are
42 * timerfd_gettime(2) and timerfd_settime(2), respectively.
43 *
44 * When a timerfd object's timer fires, an internal counter is incremented.
45 * When this counter is non-zero, the descriptor associated with the timerfd
46 * object is "readable". Note that this is slightly different than the
47 * POSIX timer "overrun" counter, which only increments if the timer fires
48 * again while the notification signal is already pending. Thus, we are
49 * responsible for incrementing the "overrun" counter each time the timerfd
50 * timer fires.
51 *
52 * This implementation is API compatible with the Linux timerfd interface.
53 */
54
55 #include <sys/types.h>
56 #include <sys/condvar.h>
57 #include <sys/file.h>
58 #include <sys/filedesc.h>
59 #include <sys/kauth.h>
60 #include <sys/mutex.h>
61 #include <sys/poll.h>
62 #include <sys/proc.h>
63 #include <sys/select.h>
64 #include <sys/stat.h>
65 #include <sys/syscallargs.h>
66 #include <sys/timerfd.h>
67 #include <sys/uio.h>
68
69 /* N.B. all timerfd state is protected by itimer_lock() */
70 struct timerfd {
71 struct itimer tfd_itimer;
72 kcondvar_t tfd_read_wait;
73 kcondvar_t tfd_restart_wait;
74 struct selinfo tfd_read_sel;
75 int64_t tfd_nwaiters;
76 bool tfd_cancel_on_set;
77 bool tfd_cancelled;
78 bool tfd_restarting;
79
80 /*
81 * Information kept for stat(2).
82 */
83 struct timespec tfd_btime; /* time created */
84 struct timespec tfd_mtime; /* last timerfd_settime() */
85 struct timespec tfd_atime; /* last read */
86 };
87
88 static void timerfd_wake(struct timerfd *);
89
90 static inline uint64_t
91 timerfd_fire_count(const struct timerfd * const tfd)
92 {
93 return (unsigned int)tfd->tfd_itimer.it_overruns;
94 }
95
96 static inline bool
97 timerfd_is_readable(const struct timerfd * const tfd)
98 {
99 return tfd->tfd_itimer.it_overruns != 0 || tfd->tfd_cancelled;
100 }
101
102 /*
103 * timerfd_fire:
104 *
105 * Called when the timerfd's timer fires.
106 *
107 * Called from a callout with itimer lock held.
108 */
109 static void
110 timerfd_fire(struct itimer * const it)
111 {
112 struct timerfd * const tfd =
113 container_of(it, struct timerfd, tfd_itimer);
114
115 it->it_overruns++;
116 timerfd_wake(tfd);
117 }
118
119 /*
120 * timerfd_realtime_changed:
121 *
122 * Called when CLOCK_REALTIME is changed with clock_settime()
123 * or settimeofday().
124 *
125 * Called with itimer lock held.
126 */
127 static void
128 timerfd_realtime_changed(struct itimer * const it)
129 {
130 struct timerfd * const tfd =
131 container_of(it, struct timerfd, tfd_itimer);
132
133 /* Should only be called when timer is armed. */
134 KASSERT(timespecisset(&it->it_time.it_value));
135
136 if (tfd->tfd_cancel_on_set) {
137 tfd->tfd_cancelled = true;
138 timerfd_wake(tfd);
139 }
140 }
141
142 static const struct itimer_ops timerfd_itimer_monotonic_ops = {
143 .ito_fire = timerfd_fire,
144 };
145
146 static const struct itimer_ops timerfd_itimer_realtime_ops = {
147 .ito_fire = timerfd_fire,
148 .ito_realtime_changed = timerfd_realtime_changed,
149 };
150
151 /*
152 * timerfd_create:
153 *
154 * Create a timerfd object.
155 */
156 static struct timerfd *
157 timerfd_create(clockid_t const clock_id, int const flags)
158 {
159 struct timerfd * const tfd = kmem_zalloc(sizeof(*tfd), KM_SLEEP);
160
161 KASSERT(clock_id == CLOCK_REALTIME || clock_id == CLOCK_MONOTONIC);
162
163 cv_init(&tfd->tfd_read_wait, "tfdread");
164 cv_init(&tfd->tfd_restart_wait, "tfdrstrt");
165 selinit(&tfd->tfd_read_sel);
166 getnanotime(&tfd->tfd_btime);
167
168 /* Caller deals with TFD_CLOEXEC and TFD_NONBLOCK. */
169
170 itimer_lock();
171 itimer_init(&tfd->tfd_itimer,
172 clock_id == CLOCK_REALTIME ? &timerfd_itimer_realtime_ops
173 : &timerfd_itimer_monotonic_ops,
174 clock_id, NULL);
175 itimer_unlock();
176
177 return tfd;
178 }
179
180 /*
181 * timerfd_destroy:
182 *
183 * Destroy a timerfd object.
184 */
185 static void
186 timerfd_destroy(struct timerfd * const tfd)
187 {
188
189 KASSERT(tfd->tfd_nwaiters == 0);
190 KASSERT(tfd->tfd_restarting == false);
191
192 itimer_lock();
193 itimer_poison(&tfd->tfd_itimer);
194 itimer_fini(&tfd->tfd_itimer); /* drops itimer lock */
195
196 cv_destroy(&tfd->tfd_read_wait);
197 cv_destroy(&tfd->tfd_restart_wait);
198
199 seldestroy(&tfd->tfd_read_sel);
200
201 kmem_free(tfd, sizeof(*tfd));
202 }
203
204 /*
205 * timerfd_wait:
206 *
207 * Block on a timerfd. Handles non-blocking, as well as
208 * the restart cases.
209 */
210 static int
211 timerfd_wait(struct timerfd * const tfd, int const fflag)
212 {
213 extern kmutex_t itimer_mutex; /* XXX */
214 int error;
215
216 if (fflag & FNONBLOCK) {
217 return EAGAIN;
218 }
219
220 /*
221 * We're going to block. If there is a restart in-progress,
222 * wait for that to complete first.
223 */
224 while (tfd->tfd_restarting) {
225 cv_wait(&tfd->tfd_restart_wait, &itimer_mutex);
226 }
227
228 tfd->tfd_nwaiters++;
229 KASSERT(tfd->tfd_nwaiters > 0);
230 error = cv_wait_sig(&tfd->tfd_read_wait, &itimer_mutex);
231 tfd->tfd_nwaiters--;
232 KASSERT(tfd->tfd_nwaiters >= 0);
233
234 /*
235 * If a restart was triggered while we were asleep, we need
236 * to return ERESTART if no other error was returned. If we
237 * are the last waiter coming out of the restart drain, clear
238 * the condition.
239 */
240 if (tfd->tfd_restarting) {
241 if (error == 0) {
242 error = ERESTART;
243 }
244 if (tfd->tfd_nwaiters == 0) {
245 tfd->tfd_restarting = false;
246 cv_broadcast(&tfd->tfd_restart_wait);
247 }
248 }
249
250 return error;
251 }
252
253 /*
254 * timerfd_wake:
255 *
256 * Wake LWPs blocked on a timerfd.
257 */
258 static void
259 timerfd_wake(struct timerfd * const tfd)
260 {
261
262 if (tfd->tfd_nwaiters) {
263 cv_broadcast(&tfd->tfd_read_wait);
264 }
265 selnotify(&tfd->tfd_read_sel, POLLIN | POLLRDNORM, NOTE_SUBMIT);
266 }
267
268 /*
269 * timerfd file operations
270 */
271
272 static int
273 timerfd_fop_read(file_t * const fp, off_t * const offset,
274 struct uio * const uio, kauth_cred_t const cred, int const flags)
275 {
276 struct timerfd * const tfd = fp->f_timerfd;
277 struct itimer * const it = &tfd->tfd_itimer;
278 int const fflag = fp->f_flag;
279 uint64_t return_value;
280 int error;
281
282 if (uio->uio_resid < sizeof(uint64_t)) {
283 return EINVAL;
284 }
285
286 itimer_lock();
287
288 while (!timerfd_is_readable(tfd)) {
289 if ((error = timerfd_wait(tfd, fflag)) != 0) {
290 itimer_unlock();
291 return error;
292 }
293 }
294
295 if (tfd->tfd_cancelled) {
296 itimer_unlock();
297 return ECANCELED;
298 }
299
300 return_value = timerfd_fire_count(tfd);
301 it->it_overruns = 0;
302
303 getnanotime(&tfd->tfd_atime);
304
305 itimer_unlock();
306
307 error = uiomove(&return_value, sizeof(return_value), uio);
308
309 return error;
310 }
311
312 static int
313 timerfd_fop_ioctl(file_t * const fp, unsigned long const cmd, void * const data)
314 {
315 struct timerfd * const tfd = fp->f_timerfd;
316 int error = 0;
317
318 switch (cmd) {
319 case TFD_IOC_SET_TICKS: {
320 const uint64_t * const new_ticksp = data;
321 if (*new_ticksp > INT_MAX) {
322 return EINVAL;
323 }
324 itimer_lock();
325 tfd->tfd_itimer.it_overruns = (int)*new_ticksp;
326 itimer_unlock();
327 break;
328 }
329
330 default:
331 error = EPASSTHROUGH;
332 }
333
334 return error;
335 }
336
337 static int
338 timerfd_fop_poll(file_t * const fp, int const events)
339 {
340 struct timerfd * const tfd = fp->f_timerfd;
341 int revents = events & (POLLOUT | POLLWRNORM);
342
343 if (events & (POLLIN | POLLRDNORM)) {
344 itimer_lock();
345 if (timerfd_is_readable(tfd)) {
346 revents |= events & (POLLIN | POLLRDNORM);
347 } else {
348 selrecord(curlwp, &tfd->tfd_read_sel);
349 }
350 itimer_unlock();
351 }
352
353 return revents;
354 }
355
356 static int
357 timerfd_fop_stat(file_t * const fp, struct stat * const st)
358 {
359 struct timerfd * const tfd = fp->f_timerfd;
360
361 memset(st, 0, sizeof(*st));
362
363 itimer_lock();
364 st->st_size = (off_t)timerfd_fire_count(tfd);
365 st->st_atimespec = tfd->tfd_atime;
366 st->st_mtimespec = tfd->tfd_mtime;
367 itimer_unlock();
368
369 st->st_blksize = sizeof(uint64_t);
370 st->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
371 st->st_blocks = 1;
372 st->st_birthtimespec = tfd->tfd_btime;
373 st->st_ctimespec = st->st_mtimespec;
374 st->st_uid = kauth_cred_geteuid(fp->f_cred);
375 st->st_gid = kauth_cred_getegid(fp->f_cred);
376
377 return 0;
378 }
379
380 static int
381 timerfd_fop_close(file_t * const fp)
382 {
383 struct timerfd * const tfd = fp->f_timerfd;
384
385 fp->f_timerfd = NULL;
386 timerfd_destroy(tfd);
387
388 return 0;
389 }
390
391 static void
392 timerfd_filt_read_detach(struct knote * const kn)
393 {
394 struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
395
396 itimer_lock();
397 KASSERT(kn->kn_hook == tfd);
398 selremove_knote(&tfd->tfd_read_sel, kn);
399 itimer_unlock();
400 }
401
402 static int
403 timerfd_filt_read(struct knote * const kn, long const hint)
404 {
405 struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
406
407 if (hint & NOTE_SUBMIT) {
408 KASSERT(itimer_lock_held());
409 } else {
410 itimer_lock();
411 }
412
413 kn->kn_data = (int64_t)timerfd_fire_count(tfd);
414
415 if ((hint & NOTE_SUBMIT) == 0) {
416 itimer_unlock();
417 }
418
419 return kn->kn_data != 0;
420 }
421
422 static const struct filterops timerfd_read_filterops = {
423 .f_isfd = 1,
424 .f_detach = timerfd_filt_read_detach,
425 .f_event = timerfd_filt_read,
426 };
427
428 static int
429 timerfd_fop_kqfilter(file_t * const fp, struct knote * const kn)
430 {
431 struct timerfd * const tfd = ((file_t *)kn->kn_obj)->f_timerfd;
432 struct selinfo *sel;
433
434 switch (kn->kn_filter) {
435 case EVFILT_READ:
436 sel = &tfd->tfd_read_sel;
437 kn->kn_fop = &timerfd_read_filterops;
438 break;
439
440 default:
441 return EINVAL;
442 }
443
444 kn->kn_hook = tfd;
445
446 itimer_lock();
447 selrecord_knote(sel, kn);
448 itimer_unlock();
449
450 return 0;
451 }
452
453 static void
454 timerfd_fop_restart(file_t * const fp)
455 {
456 struct timerfd * const tfd = fp->f_timerfd;
457
458 /*
459 * Unblock blocked reads in order to allow close() to complete.
460 * System calls return ERESTART so that the fd is revalidated.
461 */
462
463 itimer_lock();
464
465 if (tfd->tfd_nwaiters != 0) {
466 tfd->tfd_restarting = true;
467 cv_broadcast(&tfd->tfd_read_wait);
468 }
469
470 itimer_unlock();
471 }
472
473 static const struct fileops timerfd_fileops = {
474 .fo_name = "timerfd",
475 .fo_read = timerfd_fop_read,
476 .fo_write = fbadop_write,
477 .fo_ioctl = timerfd_fop_ioctl,
478 .fo_fcntl = fnullop_fcntl,
479 .fo_poll = timerfd_fop_poll,
480 .fo_stat = timerfd_fop_stat,
481 .fo_close = timerfd_fop_close,
482 .fo_kqfilter = timerfd_fop_kqfilter,
483 .fo_restart = timerfd_fop_restart,
484 };
485
486 /*
487 * timerfd_create(2) system call
488 */
489 int
490 do_timerfd_create(struct lwp * const l, clockid_t const clock_id,
491 int const flags, register_t *retval)
492 {
493 file_t *fp;
494 int fd, error;
495
496 if (flags & ~(TFD_CLOEXEC | TFD_NONBLOCK)) {
497 return EINVAL;
498 }
499
500 switch (clock_id) {
501 case CLOCK_REALTIME:
502 case CLOCK_MONOTONIC:
503 /* allowed */
504 break;
505
506 default:
507 return EINVAL;
508 }
509
510 if ((error = fd_allocfile(&fp, &fd)) != 0) {
511 return error;
512 }
513
514 fp->f_flag = FREAD;
515 if (flags & TFD_NONBLOCK) {
516 fp->f_flag |= FNONBLOCK;
517 }
518 fp->f_type = DTYPE_TIMERFD;
519 fp->f_ops = &timerfd_fileops;
520 fp->f_timerfd = timerfd_create(clock_id, flags);
521 fd_set_exclose(l, fd, !!(flags & TFD_CLOEXEC));
522 fd_affix(curproc, fp, fd);
523
524 *retval = fd;
525 return 0;
526 }
527
528 int
529 sys_timerfd_create(struct lwp *l, const struct sys_timerfd_create_args *uap,
530 register_t *retval)
531 {
532 /* {
533 syscallarg(clockid_t) clock_id;
534 syscallarg(int) flags;
535 } */
536
537 return do_timerfd_create(l, SCARG(uap, clock_id), SCARG(uap, flags),
538 retval);
539 }
540
541 /*
542 * timerfd_gettime(2) system call.
543 */
544 int
545 do_timerfd_gettime(struct lwp *l, int fd, struct itimerspec *curr_value,
546 register_t *retval)
547 {
548 file_t *fp;
549
550 if ((fp = fd_getfile(fd)) == NULL) {
551 return EBADF;
552 }
553
554 if (fp->f_ops != &timerfd_fileops) {
555 fd_putfile(fd);
556 return EINVAL;
557 }
558
559 struct timerfd * const tfd = fp->f_timerfd;
560 itimer_lock();
561 itimer_gettime(&tfd->tfd_itimer, curr_value);
562 itimer_unlock();
563
564 fd_putfile(fd);
565 return 0;
566 }
567
568 int
569 sys_timerfd_gettime(struct lwp *l, const struct sys_timerfd_gettime_args *uap,
570 register_t *retval)
571 {
572 /* {
573 syscallarg(int) fd;
574 syscallarg(struct itimerspec *) curr_value;
575 } */
576
577 struct itimerspec oits;
578 int error;
579
580 error = do_timerfd_gettime(l, SCARG(uap, fd), &oits, retval);
581 if (error == 0) {
582 error = copyout(&oits, SCARG(uap, curr_value), sizeof(oits));
583 }
584 return error;
585 }
586
587 /*
588 * timerfd_settime(2) system call.
589 */
590 int
591 do_timerfd_settime(struct lwp *l, int fd, int flags,
592 const struct itimerspec *new_value, struct itimerspec *old_value,
593 register_t *retval)
594 {
595 file_t *fp;
596 int error;
597
598 if (flags & ~(TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)) {
599 return EINVAL;
600 }
601
602 if ((fp = fd_getfile(fd)) == NULL) {
603 return EBADF;
604 }
605
606 if (fp->f_ops != &timerfd_fileops) {
607 fd_putfile(fd);
608 return EINVAL;
609 }
610
611 struct timerfd * const tfd = fp->f_timerfd;
612 struct itimer * const it = &tfd->tfd_itimer;
613
614 itimer_lock();
615
616 restart:
617 if (old_value != NULL) {
618 *old_value = it->it_time;
619 }
620 it->it_time = *new_value;
621
622 /*
623 * If we've been passed a relative value, convert it to an
624 * absolute, as that's what the itimer facility expects for
625 * non-virtual timers. Also ensure that this doesn't set it
626 * to zero or lets it go negative.
627 * XXXJRT re-factor.
628 */
629 if (timespecisset(&it->it_time.it_value) &&
630 (flags & TFD_TIMER_ABSTIME) == 0) {
631 struct timespec now;
632 if (it->it_clockid == CLOCK_REALTIME) {
633 getnanotime(&now);
634 } else { /* CLOCK_MONOTONIC */
635 getnanouptime(&now);
636 }
637 timespecadd(&it->it_time.it_value, &now,
638 &it->it_time.it_value);
639 }
640
641 error = itimer_settime(it);
642 if (error == ERESTART) {
643 goto restart;
644 }
645 KASSERT(error == 0);
646
647 /* Reset the expirations counter. */
648 it->it_overruns = 0;
649
650 if (it->it_clockid == CLOCK_REALTIME) {
651 tfd->tfd_cancelled = false;
652 tfd->tfd_cancel_on_set = !!(flags & TFD_TIMER_CANCEL_ON_SET);
653 }
654
655 getnanotime(&tfd->tfd_mtime);
656 itimer_unlock();
657
658 fd_putfile(fd);
659 return error;
660 }
661
662 int
663 sys_timerfd_settime(struct lwp *l, const struct sys_timerfd_settime_args *uap,
664 register_t *retval)
665 {
666 /* {
667 syscallarg(int) fd;
668 syscallarg(int) flags;
669 syscallarg(const struct itimerspec *) new_value;
670 syscallarg(struct itimerspec *) old_value;
671 } */
672
673 struct itimerspec nits, oits, *oitsp = NULL;
674 int error;
675
676 error = copyin(SCARG(uap, new_value), &nits, sizeof(nits));
677 if (error) {
678 return error;
679 }
680
681 if (SCARG(uap, old_value) != NULL) {
682 oitsp = &oits;
683 }
684
685 error = do_timerfd_settime(l, SCARG(uap, fd), SCARG(uap, flags),
686 &nits, oitsp, retval);
687 if (error == 0 && oitsp != NULL) {
688 error = copyout(oitsp, SCARG(uap, old_value), sizeof(*oitsp));
689 }
690 return error;
691 }
692