1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b215e283SDavide Libenzi /*
3b215e283SDavide Libenzi * fs/timerfd.c
4b215e283SDavide Libenzi *
5b215e283SDavide Libenzi * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
6b215e283SDavide Libenzi *
7b215e283SDavide Libenzi *
8b215e283SDavide Libenzi * Thanks to Thomas Gleixner for code reviews and useful comments.
9b215e283SDavide Libenzi *
10b215e283SDavide Libenzi */
11b215e283SDavide Libenzi
1211ffa9d6STodd Poynor #include <linux/alarmtimer.h>
13b215e283SDavide Libenzi #include <linux/file.h>
14b215e283SDavide Libenzi #include <linux/poll.h>
15b215e283SDavide Libenzi #include <linux/init.h>
16b215e283SDavide Libenzi #include <linux/fs.h>
17b215e283SDavide Libenzi #include <linux/sched.h>
18b215e283SDavide Libenzi #include <linux/kernel.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20b215e283SDavide Libenzi #include <linux/list.h>
21b215e283SDavide Libenzi #include <linux/spinlock.h>
22b215e283SDavide Libenzi #include <linux/time.h>
23b215e283SDavide Libenzi #include <linux/hrtimer.h>
24b215e283SDavide Libenzi #include <linux/anon_inodes.h>
25b215e283SDavide Libenzi #include <linux/timerfd.h>
2645cc2b96SAdrian Bunk #include <linux/syscalls.h>
279d94b9e2SAl Viro #include <linux/compat.h>
289ec26907SThomas Gleixner #include <linux/rcupdate.h>
296cd889d4SAndrei Vagin #include <linux/time_namespace.h>
30b215e283SDavide Libenzi
31b215e283SDavide Libenzi struct timerfd_ctx {
3211ffa9d6STodd Poynor union {
33b215e283SDavide Libenzi struct hrtimer tmr;
3411ffa9d6STodd Poynor struct alarm alarm;
3511ffa9d6STodd Poynor } t;
36b215e283SDavide Libenzi ktime_t tintv;
3799ee5315SThomas Gleixner ktime_t moffs;
38b215e283SDavide Libenzi wait_queue_head_t wqh;
394d672e7aSDavide Libenzi u64 ticks;
404d672e7aSDavide Libenzi int clockid;
41af9c4957SCyrill Gorcunov short unsigned expired;
42af9c4957SCyrill Gorcunov short unsigned settime_flags; /* to show in fdinfo */
439ec26907SThomas Gleixner struct rcu_head rcu;
449ec26907SThomas Gleixner struct list_head clist;
451e38da30SThomas Gleixner spinlock_t cancel_lock;
4699ee5315SThomas Gleixner bool might_cancel;
47b215e283SDavide Libenzi };
48b215e283SDavide Libenzi
499ec26907SThomas Gleixner static LIST_HEAD(cancel_list);
509ec26907SThomas Gleixner static DEFINE_SPINLOCK(cancel_lock);
519ec26907SThomas Gleixner
isalarm(struct timerfd_ctx * ctx)5211ffa9d6STodd Poynor static inline bool isalarm(struct timerfd_ctx *ctx)
5311ffa9d6STodd Poynor {
5411ffa9d6STodd Poynor return ctx->clockid == CLOCK_REALTIME_ALARM ||
5511ffa9d6STodd Poynor ctx->clockid == CLOCK_BOOTTIME_ALARM;
5611ffa9d6STodd Poynor }
5711ffa9d6STodd Poynor
58b215e283SDavide Libenzi /*
59b215e283SDavide Libenzi * This gets called when the timer event triggers. We set the "expired"
60b215e283SDavide Libenzi * flag, but we do not re-arm the timer (in case it's necessary,
612456e855SThomas Gleixner * tintv != 0) until the timer is accessed.
62b215e283SDavide Libenzi */
timerfd_triggered(struct timerfd_ctx * ctx)6311ffa9d6STodd Poynor static void timerfd_triggered(struct timerfd_ctx *ctx)
64b215e283SDavide Libenzi {
65b215e283SDavide Libenzi unsigned long flags;
66b215e283SDavide Libenzi
6718963c01SDavide Libenzi spin_lock_irqsave(&ctx->wqh.lock, flags);
68b215e283SDavide Libenzi ctx->expired = 1;
694d672e7aSDavide Libenzi ctx->ticks++;
707dda7128SChristoph Hellwig wake_up_locked_poll(&ctx->wqh, EPOLLIN);
7118963c01SDavide Libenzi spin_unlock_irqrestore(&ctx->wqh.lock, flags);
7211ffa9d6STodd Poynor }
73b215e283SDavide Libenzi
timerfd_tmrproc(struct hrtimer * htmr)7411ffa9d6STodd Poynor static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
7511ffa9d6STodd Poynor {
7611ffa9d6STodd Poynor struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
7711ffa9d6STodd Poynor t.tmr);
7811ffa9d6STodd Poynor timerfd_triggered(ctx);
79b215e283SDavide Libenzi return HRTIMER_NORESTART;
80b215e283SDavide Libenzi }
81b215e283SDavide Libenzi
timerfd_alarmproc(struct alarm * alarm,ktime_t now)822634303fSThomas Gleixner static void timerfd_alarmproc(struct alarm *alarm, ktime_t now)
8311ffa9d6STodd Poynor {
8411ffa9d6STodd Poynor struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
8511ffa9d6STodd Poynor t.alarm);
8611ffa9d6STodd Poynor timerfd_triggered(ctx);
8711ffa9d6STodd Poynor }
8811ffa9d6STodd Poynor
899ec26907SThomas Gleixner /*
909ec26907SThomas Gleixner * Called when the clock was set to cancel the timers in the cancel
911123d939SMax Asbock * list. This will wake up processes waiting on these timers. The
921123d939SMax Asbock * wake-up requires ctx->ticks to be non zero, therefore we increment
931123d939SMax Asbock * it before calling wake_up_locked().
949ec26907SThomas Gleixner */
timerfd_clock_was_set(void)959ec26907SThomas Gleixner void timerfd_clock_was_set(void)
969ec26907SThomas Gleixner {
972456e855SThomas Gleixner ktime_t moffs = ktime_mono_to_real(0);
989ec26907SThomas Gleixner struct timerfd_ctx *ctx;
999ec26907SThomas Gleixner unsigned long flags;
1009ec26907SThomas Gleixner
1019ec26907SThomas Gleixner rcu_read_lock();
1029ec26907SThomas Gleixner list_for_each_entry_rcu(ctx, &cancel_list, clist) {
1039ec26907SThomas Gleixner if (!ctx->might_cancel)
1049ec26907SThomas Gleixner continue;
1059ec26907SThomas Gleixner spin_lock_irqsave(&ctx->wqh.lock, flags);
1062456e855SThomas Gleixner if (ctx->moffs != moffs) {
1072456e855SThomas Gleixner ctx->moffs = KTIME_MAX;
1081123d939SMax Asbock ctx->ticks++;
1097dda7128SChristoph Hellwig wake_up_locked_poll(&ctx->wqh, EPOLLIN);
1109ec26907SThomas Gleixner }
1119ec26907SThomas Gleixner spin_unlock_irqrestore(&ctx->wqh.lock, flags);
1129ec26907SThomas Gleixner }
1139ec26907SThomas Gleixner rcu_read_unlock();
1149ec26907SThomas Gleixner }
1159ec26907SThomas Gleixner
timerfd_resume_work(struct work_struct * work)11666f7b0c8SThomas Gleixner static void timerfd_resume_work(struct work_struct *work)
11766f7b0c8SThomas Gleixner {
11866f7b0c8SThomas Gleixner timerfd_clock_was_set();
11966f7b0c8SThomas Gleixner }
12066f7b0c8SThomas Gleixner
12166f7b0c8SThomas Gleixner static DECLARE_WORK(timerfd_work, timerfd_resume_work);
12266f7b0c8SThomas Gleixner
12366f7b0c8SThomas Gleixner /*
12466f7b0c8SThomas Gleixner * Invoked from timekeeping_resume(). Defer the actual update to work so
12566f7b0c8SThomas Gleixner * timerfd_clock_was_set() runs in task context.
12666f7b0c8SThomas Gleixner */
timerfd_resume(void)12766f7b0c8SThomas Gleixner void timerfd_resume(void)
12866f7b0c8SThomas Gleixner {
12966f7b0c8SThomas Gleixner schedule_work(&timerfd_work);
13066f7b0c8SThomas Gleixner }
13166f7b0c8SThomas Gleixner
__timerfd_remove_cancel(struct timerfd_ctx * ctx)1321e38da30SThomas Gleixner static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
1339ec26907SThomas Gleixner {
1349ec26907SThomas Gleixner if (ctx->might_cancel) {
1359ec26907SThomas Gleixner ctx->might_cancel = false;
1369ec26907SThomas Gleixner spin_lock(&cancel_lock);
1379ec26907SThomas Gleixner list_del_rcu(&ctx->clist);
1389ec26907SThomas Gleixner spin_unlock(&cancel_lock);
1399ec26907SThomas Gleixner }
1409ec26907SThomas Gleixner }
1419ec26907SThomas Gleixner
timerfd_remove_cancel(struct timerfd_ctx * ctx)1421e38da30SThomas Gleixner static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
1431e38da30SThomas Gleixner {
1441e38da30SThomas Gleixner spin_lock(&ctx->cancel_lock);
1451e38da30SThomas Gleixner __timerfd_remove_cancel(ctx);
1461e38da30SThomas Gleixner spin_unlock(&ctx->cancel_lock);
1471e38da30SThomas Gleixner }
1481e38da30SThomas Gleixner
timerfd_canceled(struct timerfd_ctx * ctx)1499ec26907SThomas Gleixner static bool timerfd_canceled(struct timerfd_ctx *ctx)
1509ec26907SThomas Gleixner {
1512456e855SThomas Gleixner if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
1529ec26907SThomas Gleixner return false;
1532456e855SThomas Gleixner ctx->moffs = ktime_mono_to_real(0);
1549ec26907SThomas Gleixner return true;
1559ec26907SThomas Gleixner }
1569ec26907SThomas Gleixner
timerfd_setup_cancel(struct timerfd_ctx * ctx,int flags)1579ec26907SThomas Gleixner static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
1589ec26907SThomas Gleixner {
1591e38da30SThomas Gleixner spin_lock(&ctx->cancel_lock);
16011ffa9d6STodd Poynor if ((ctx->clockid == CLOCK_REALTIME ||
16111ffa9d6STodd Poynor ctx->clockid == CLOCK_REALTIME_ALARM) &&
16211ffa9d6STodd Poynor (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
1639ec26907SThomas Gleixner if (!ctx->might_cancel) {
1649ec26907SThomas Gleixner ctx->might_cancel = true;
1659ec26907SThomas Gleixner spin_lock(&cancel_lock);
1669ec26907SThomas Gleixner list_add_rcu(&ctx->clist, &cancel_list);
1679ec26907SThomas Gleixner spin_unlock(&cancel_lock);
1689ec26907SThomas Gleixner }
1691e38da30SThomas Gleixner } else {
1701e38da30SThomas Gleixner __timerfd_remove_cancel(ctx);
1719ec26907SThomas Gleixner }
1721e38da30SThomas Gleixner spin_unlock(&ctx->cancel_lock);
1739ec26907SThomas Gleixner }
1749ec26907SThomas Gleixner
timerfd_get_remaining(struct timerfd_ctx * ctx)1754d672e7aSDavide Libenzi static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
1764d672e7aSDavide Libenzi {
17776369470SArjan van de Ven ktime_t remaining;
1784d672e7aSDavide Libenzi
17911ffa9d6STodd Poynor if (isalarm(ctx))
18011ffa9d6STodd Poynor remaining = alarm_expires_remaining(&ctx->t.alarm);
18111ffa9d6STodd Poynor else
182b62526edSThomas Gleixner remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
18311ffa9d6STodd Poynor
1848b0e1953SThomas Gleixner return remaining < 0 ? 0: remaining;
1854d672e7aSDavide Libenzi }
1864d672e7aSDavide Libenzi
timerfd_setup(struct timerfd_ctx * ctx,int flags,const struct itimerspec64 * ktmr)18799ee5315SThomas Gleixner static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
188bff41203SDeepa Dinamani const struct itimerspec64 *ktmr)
189b215e283SDavide Libenzi {
190b215e283SDavide Libenzi enum hrtimer_mode htmode;
191b215e283SDavide Libenzi ktime_t texp;
19299ee5315SThomas Gleixner int clockid = ctx->clockid;
193b215e283SDavide Libenzi
194b215e283SDavide Libenzi htmode = (flags & TFD_TIMER_ABSTIME) ?
195b215e283SDavide Libenzi HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
196b215e283SDavide Libenzi
197bff41203SDeepa Dinamani texp = timespec64_to_ktime(ktmr->it_value);
198b215e283SDavide Libenzi ctx->expired = 0;
1994d672e7aSDavide Libenzi ctx->ticks = 0;
200bff41203SDeepa Dinamani ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
20111ffa9d6STodd Poynor
20211ffa9d6STodd Poynor if (isalarm(ctx)) {
20311ffa9d6STodd Poynor alarm_init(&ctx->t.alarm,
20411ffa9d6STodd Poynor ctx->clockid == CLOCK_REALTIME_ALARM ?
20511ffa9d6STodd Poynor ALARM_REALTIME : ALARM_BOOTTIME,
20611ffa9d6STodd Poynor timerfd_alarmproc);
20711ffa9d6STodd Poynor } else {
2089eeb54b4SNam Cao hrtimer_setup(&ctx->t.tmr, timerfd_tmrproc, clockid, htmode);
20911ffa9d6STodd Poynor hrtimer_set_expires(&ctx->t.tmr, texp);
21011ffa9d6STodd Poynor }
21111ffa9d6STodd Poynor
2122456e855SThomas Gleixner if (texp != 0) {
2136cd889d4SAndrei Vagin if (flags & TFD_TIMER_ABSTIME)
2146cd889d4SAndrei Vagin texp = timens_ktime_to_host(clockid, texp);
21511ffa9d6STodd Poynor if (isalarm(ctx)) {
21611ffa9d6STodd Poynor if (flags & TFD_TIMER_ABSTIME)
21711ffa9d6STodd Poynor alarm_start(&ctx->t.alarm, texp);
21811ffa9d6STodd Poynor else
21911ffa9d6STodd Poynor alarm_start_relative(&ctx->t.alarm, texp);
22011ffa9d6STodd Poynor } else {
22111ffa9d6STodd Poynor hrtimer_start(&ctx->t.tmr, texp, htmode);
22211ffa9d6STodd Poynor }
22311ffa9d6STodd Poynor
22499ee5315SThomas Gleixner if (timerfd_canceled(ctx))
22599ee5315SThomas Gleixner return -ECANCELED;
22699ee5315SThomas Gleixner }
227af9c4957SCyrill Gorcunov
228af9c4957SCyrill Gorcunov ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
22999ee5315SThomas Gleixner return 0;
230b215e283SDavide Libenzi }
231b215e283SDavide Libenzi
timerfd_release(struct inode * inode,struct file * file)232b215e283SDavide Libenzi static int timerfd_release(struct inode *inode, struct file *file)
233b215e283SDavide Libenzi {
234b215e283SDavide Libenzi struct timerfd_ctx *ctx = file->private_data;
235b215e283SDavide Libenzi
2369ec26907SThomas Gleixner timerfd_remove_cancel(ctx);
23711ffa9d6STodd Poynor
23811ffa9d6STodd Poynor if (isalarm(ctx))
23911ffa9d6STodd Poynor alarm_cancel(&ctx->t.alarm);
24011ffa9d6STodd Poynor else
24111ffa9d6STodd Poynor hrtimer_cancel(&ctx->t.tmr);
2429ec26907SThomas Gleixner kfree_rcu(ctx, rcu);
243b215e283SDavide Libenzi return 0;
244b215e283SDavide Libenzi }
245b215e283SDavide Libenzi
timerfd_poll(struct file * file,poll_table * wait)246a11e1d43SLinus Torvalds static __poll_t timerfd_poll(struct file *file, poll_table *wait)
247b215e283SDavide Libenzi {
248b215e283SDavide Libenzi struct timerfd_ctx *ctx = file->private_data;
249a11e1d43SLinus Torvalds __poll_t events = 0;
250a11e1d43SLinus Torvalds unsigned long flags;
251b215e283SDavide Libenzi
252a11e1d43SLinus Torvalds poll_wait(file, &ctx->wqh, wait);
253b215e283SDavide Libenzi
254a11e1d43SLinus Torvalds spin_lock_irqsave(&ctx->wqh.lock, flags);
255a11e1d43SLinus Torvalds if (ctx->ticks)
256a11e1d43SLinus Torvalds events |= EPOLLIN;
257a11e1d43SLinus Torvalds spin_unlock_irqrestore(&ctx->wqh.lock, flags);
258b215e283SDavide Libenzi
259a11e1d43SLinus Torvalds return events;
260b215e283SDavide Libenzi }
261b215e283SDavide Libenzi
timerfd_read_iter(struct kiocb * iocb,struct iov_iter * to)262d9497990SJens Axboe static ssize_t timerfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
263b215e283SDavide Libenzi {
264d9497990SJens Axboe struct file *file = iocb->ki_filp;
265b215e283SDavide Libenzi struct timerfd_ctx *ctx = file->private_data;
266b215e283SDavide Libenzi ssize_t res;
26709828402SDavide Libenzi u64 ticks = 0;
268b215e283SDavide Libenzi
269d9497990SJens Axboe if (iov_iter_count(to) < sizeof(ticks))
270b215e283SDavide Libenzi return -EINVAL;
271d9497990SJens Axboe
27218963c01SDavide Libenzi spin_lock_irq(&ctx->wqh.lock);
273d9497990SJens Axboe if (file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT)
274b215e283SDavide Libenzi res = -EAGAIN;
2758120a8aaSMichal Nazarewicz else
2768120a8aaSMichal Nazarewicz res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
27799ee5315SThomas Gleixner
27899ee5315SThomas Gleixner /*
27999ee5315SThomas Gleixner * If clock has changed, we do not care about the
28099ee5315SThomas Gleixner * ticks and we do not rearm the timer. Userspace must
28199ee5315SThomas Gleixner * reevaluate anyway.
28299ee5315SThomas Gleixner */
28399ee5315SThomas Gleixner if (timerfd_canceled(ctx)) {
2849ec26907SThomas Gleixner ctx->ticks = 0;
28599ee5315SThomas Gleixner ctx->expired = 0;
28699ee5315SThomas Gleixner res = -ECANCELED;
28799ee5315SThomas Gleixner }
28899ee5315SThomas Gleixner
2899ec26907SThomas Gleixner if (ctx->ticks) {
2909ec26907SThomas Gleixner ticks = ctx->ticks;
2919ec26907SThomas Gleixner
2922456e855SThomas Gleixner if (ctx->expired && ctx->tintv) {
293b215e283SDavide Libenzi /*
2942456e855SThomas Gleixner * If tintv != 0, this is a periodic timer that
295b215e283SDavide Libenzi * needs to be re-armed. We avoid doing it in the timer
296b215e283SDavide Libenzi * callback to avoid DoS attacks specifying a very
297b215e283SDavide Libenzi * short timer period.
298b215e283SDavide Libenzi */
29911ffa9d6STodd Poynor if (isalarm(ctx)) {
30011ffa9d6STodd Poynor ticks += alarm_forward_now(
30111ffa9d6STodd Poynor &ctx->t.alarm, ctx->tintv) - 1;
30211ffa9d6STodd Poynor alarm_restart(&ctx->t.alarm);
30311ffa9d6STodd Poynor } else {
30411ffa9d6STodd Poynor ticks += hrtimer_forward_now(&ctx->t.tmr,
3054d672e7aSDavide Libenzi ctx->tintv) - 1;
30611ffa9d6STodd Poynor hrtimer_restart(&ctx->t.tmr);
30711ffa9d6STodd Poynor }
3084d672e7aSDavide Libenzi }
3094d672e7aSDavide Libenzi ctx->expired = 0;
3104d672e7aSDavide Libenzi ctx->ticks = 0;
311b215e283SDavide Libenzi }
31218963c01SDavide Libenzi spin_unlock_irq(&ctx->wqh.lock);
313d9497990SJens Axboe if (ticks) {
314d9497990SJens Axboe res = copy_to_iter(&ticks, sizeof(ticks), to);
315d9497990SJens Axboe if (!res)
316d9497990SJens Axboe res = -EFAULT;
317d9497990SJens Axboe }
318b215e283SDavide Libenzi return res;
319b215e283SDavide Libenzi }
320b215e283SDavide Libenzi
321af9c4957SCyrill Gorcunov #ifdef CONFIG_PROC_FS
timerfd_show(struct seq_file * m,struct file * file)322a3816ab0SJoe Perches static void timerfd_show(struct seq_file *m, struct file *file)
323af9c4957SCyrill Gorcunov {
324af9c4957SCyrill Gorcunov struct timerfd_ctx *ctx = file->private_data;
325bde9e963SArnd Bergmann struct timespec64 value, interval;
326af9c4957SCyrill Gorcunov
327af9c4957SCyrill Gorcunov spin_lock_irq(&ctx->wqh.lock);
328bde9e963SArnd Bergmann value = ktime_to_timespec64(timerfd_get_remaining(ctx));
329bde9e963SArnd Bergmann interval = ktime_to_timespec64(ctx->tintv);
330af9c4957SCyrill Gorcunov spin_unlock_irq(&ctx->wqh.lock);
331af9c4957SCyrill Gorcunov
332a3816ab0SJoe Perches seq_printf(m,
333af9c4957SCyrill Gorcunov "clockid: %d\n"
334af9c4957SCyrill Gorcunov "ticks: %llu\n"
335af9c4957SCyrill Gorcunov "settime flags: 0%o\n"
336af9c4957SCyrill Gorcunov "it_value: (%llu, %llu)\n"
337af9c4957SCyrill Gorcunov "it_interval: (%llu, %llu)\n",
338a3816ab0SJoe Perches ctx->clockid,
339a3816ab0SJoe Perches (unsigned long long)ctx->ticks,
340af9c4957SCyrill Gorcunov ctx->settime_flags,
341bde9e963SArnd Bergmann (unsigned long long)value.tv_sec,
342bde9e963SArnd Bergmann (unsigned long long)value.tv_nsec,
343bde9e963SArnd Bergmann (unsigned long long)interval.tv_sec,
344bde9e963SArnd Bergmann (unsigned long long)interval.tv_nsec);
345af9c4957SCyrill Gorcunov }
346af9c4957SCyrill Gorcunov #else
347af9c4957SCyrill Gorcunov #define timerfd_show NULL
348af9c4957SCyrill Gorcunov #endif
349af9c4957SCyrill Gorcunov
3505442e9fbSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
timerfd_ioctl(struct file * file,unsigned int cmd,unsigned long arg)3515442e9fbSCyrill Gorcunov static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3525442e9fbSCyrill Gorcunov {
3535442e9fbSCyrill Gorcunov struct timerfd_ctx *ctx = file->private_data;
3545442e9fbSCyrill Gorcunov int ret = 0;
3555442e9fbSCyrill Gorcunov
3565442e9fbSCyrill Gorcunov switch (cmd) {
3575442e9fbSCyrill Gorcunov case TFD_IOC_SET_TICKS: {
3585442e9fbSCyrill Gorcunov u64 ticks;
3595442e9fbSCyrill Gorcunov
3605442e9fbSCyrill Gorcunov if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
3615442e9fbSCyrill Gorcunov return -EFAULT;
3625442e9fbSCyrill Gorcunov if (!ticks)
3635442e9fbSCyrill Gorcunov return -EINVAL;
3645442e9fbSCyrill Gorcunov
3655442e9fbSCyrill Gorcunov spin_lock_irq(&ctx->wqh.lock);
3665442e9fbSCyrill Gorcunov if (!timerfd_canceled(ctx)) {
3675442e9fbSCyrill Gorcunov ctx->ticks = ticks;
3687dda7128SChristoph Hellwig wake_up_locked_poll(&ctx->wqh, EPOLLIN);
3695442e9fbSCyrill Gorcunov } else
3705442e9fbSCyrill Gorcunov ret = -ECANCELED;
3715442e9fbSCyrill Gorcunov spin_unlock_irq(&ctx->wqh.lock);
3725442e9fbSCyrill Gorcunov break;
3735442e9fbSCyrill Gorcunov }
3745442e9fbSCyrill Gorcunov default:
3755442e9fbSCyrill Gorcunov ret = -ENOTTY;
3765442e9fbSCyrill Gorcunov break;
3775442e9fbSCyrill Gorcunov }
3785442e9fbSCyrill Gorcunov
3795442e9fbSCyrill Gorcunov return ret;
3805442e9fbSCyrill Gorcunov }
3815442e9fbSCyrill Gorcunov #else
3825442e9fbSCyrill Gorcunov #define timerfd_ioctl NULL
3835442e9fbSCyrill Gorcunov #endif
3845442e9fbSCyrill Gorcunov
385b215e283SDavide Libenzi static const struct file_operations timerfd_fops = {
386b215e283SDavide Libenzi .release = timerfd_release,
387a11e1d43SLinus Torvalds .poll = timerfd_poll,
388d9497990SJens Axboe .read_iter = timerfd_read_iter,
3896038f373SArnd Bergmann .llseek = noop_llseek,
390af9c4957SCyrill Gorcunov .show_fdinfo = timerfd_show,
3915442e9fbSCyrill Gorcunov .unlocked_ioctl = timerfd_ioctl,
392b215e283SDavide Libenzi };
393b215e283SDavide Libenzi
SYSCALL_DEFINE2(timerfd_create,int,clockid,int,flags)394836f92adSHeiko Carstens SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
3954d672e7aSDavide Libenzi {
39614010faaSChristian Brauner struct timerfd_ctx *ctx __free(kfree) = NULL;
39714010faaSChristian Brauner int ret;
398b215e283SDavide Libenzi
399e38b36f3SUlrich Drepper /* Check the TFD_* constants for consistency. */
400e38b36f3SUlrich Drepper BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
401e38b36f3SUlrich Drepper BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
402e38b36f3SUlrich Drepper
403610d18f4SDavide Libenzi if ((flags & ~TFD_CREATE_FLAGS) ||
404610d18f4SDavide Libenzi (clockid != CLOCK_MONOTONIC &&
40511ffa9d6STodd Poynor clockid != CLOCK_REALTIME &&
40611ffa9d6STodd Poynor clockid != CLOCK_REALTIME_ALARM &&
4074a2378a9SGreg Hackmann clockid != CLOCK_BOOTTIME &&
40811ffa9d6STodd Poynor clockid != CLOCK_BOOTTIME_ALARM))
409b215e283SDavide Libenzi return -EINVAL;
410b215e283SDavide Libenzi
41125b68a8fSStephen Smalley if ((clockid == CLOCK_REALTIME_ALARM ||
41225b68a8fSStephen Smalley clockid == CLOCK_BOOTTIME_ALARM) &&
41325b68a8fSStephen Smalley !capable(CAP_WAKE_ALARM))
4142895a5e5SEric Caruso return -EPERM;
4152895a5e5SEric Caruso
416*bf4afc53SLinus Torvalds ctx = kzalloc_obj(*ctx);
417b215e283SDavide Libenzi if (!ctx)
418b215e283SDavide Libenzi return -ENOMEM;
419b215e283SDavide Libenzi
420b215e283SDavide Libenzi init_waitqueue_head(&ctx->wqh);
4211e38da30SThomas Gleixner spin_lock_init(&ctx->cancel_lock);
4224d672e7aSDavide Libenzi ctx->clockid = clockid;
42311ffa9d6STodd Poynor
42411ffa9d6STodd Poynor if (isalarm(ctx))
42511ffa9d6STodd Poynor alarm_init(&ctx->t.alarm,
42611ffa9d6STodd Poynor ctx->clockid == CLOCK_REALTIME_ALARM ?
42711ffa9d6STodd Poynor ALARM_REALTIME : ALARM_BOOTTIME,
42811ffa9d6STodd Poynor timerfd_alarmproc);
42911ffa9d6STodd Poynor else
4309eeb54b4SNam Cao hrtimer_setup(&ctx->t.tmr, timerfd_tmrproc, clockid, HRTIMER_MODE_ABS);
43111ffa9d6STodd Poynor
4322456e855SThomas Gleixner ctx->moffs = ktime_mono_to_real(0);
433b215e283SDavide Libenzi
43414010faaSChristian Brauner ret = FD_ADD(flags & TFD_SHARED_FCNTL_FLAGS,
43514010faaSChristian Brauner anon_inode_getfile_fmode("[timerfd]", &timerfd_fops, ctx,
436f9835fa1SAl Viro O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS),
43714010faaSChristian Brauner FMODE_NOWAIT));
43814010faaSChristian Brauner if (ret >= 0)
43914010faaSChristian Brauner retain_and_null_ptr(ctx);
44014010faaSChristian Brauner return ret;
4414d672e7aSDavide Libenzi }
4424d672e7aSDavide Libenzi
do_timerfd_settime(int ufd,int flags,const struct itimerspec64 * new,struct itimerspec64 * old)4439d94b9e2SAl Viro static int do_timerfd_settime(int ufd, int flags,
444bff41203SDeepa Dinamani const struct itimerspec64 *new,
445bff41203SDeepa Dinamani struct itimerspec64 *old)
4464d672e7aSDavide Libenzi {
4474d672e7aSDavide Libenzi struct timerfd_ctx *ctx;
4482903ff01SAl Viro int ret;
4494d672e7aSDavide Libenzi
450610d18f4SDavide Libenzi if ((flags & ~TFD_SETTIME_FLAGS) ||
451bff41203SDeepa Dinamani !itimerspec64_valid(new))
4524d672e7aSDavide Libenzi return -EINVAL;
4534d672e7aSDavide Libenzi
454919a7a1aSAl Viro CLASS(fd, f)(ufd);
455919a7a1aSAl Viro if (fd_empty(f))
456919a7a1aSAl Viro return -EBADF;
457919a7a1aSAl Viro
458919a7a1aSAl Viro if (fd_file(f)->f_op != &timerfd_fops)
459919a7a1aSAl Viro return -EINVAL;
460919a7a1aSAl Viro
4611da91ea8SAl Viro ctx = fd_file(f)->private_data;
4624d672e7aSDavide Libenzi
463919a7a1aSAl Viro if (isalarm(ctx) && !capable(CAP_WAKE_ALARM))
4642895a5e5SEric Caruso return -EPERM;
4652895a5e5SEric Caruso
4669ec26907SThomas Gleixner timerfd_setup_cancel(ctx, flags);
4679ec26907SThomas Gleixner
468b215e283SDavide Libenzi /*
469b215e283SDavide Libenzi * We need to stop the existing timer before reprogramming
470b215e283SDavide Libenzi * it to the new values.
471b215e283SDavide Libenzi */
472b215e283SDavide Libenzi for (;;) {
47318963c01SDavide Libenzi spin_lock_irq(&ctx->wqh.lock);
47411ffa9d6STodd Poynor
47511ffa9d6STodd Poynor if (isalarm(ctx)) {
47611ffa9d6STodd Poynor if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
477b215e283SDavide Libenzi break;
47811ffa9d6STodd Poynor } else {
47911ffa9d6STodd Poynor if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
48011ffa9d6STodd Poynor break;
48111ffa9d6STodd Poynor }
48218963c01SDavide Libenzi spin_unlock_irq(&ctx->wqh.lock);
483a125ecc1SAnna-Maria Gleixner
484a125ecc1SAnna-Maria Gleixner if (isalarm(ctx))
485a125ecc1SAnna-Maria Gleixner hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
486a125ecc1SAnna-Maria Gleixner else
487a125ecc1SAnna-Maria Gleixner hrtimer_cancel_wait_running(&ctx->t.tmr);
488b215e283SDavide Libenzi }
4894d672e7aSDavide Libenzi
4904d672e7aSDavide Libenzi /*
4914d672e7aSDavide Libenzi * If the timer is expired and it's periodic, we need to advance it
4924d672e7aSDavide Libenzi * because the caller may want to know the previous expiration time.
4934d672e7aSDavide Libenzi * We do not update "ticks" and "expired" since the timer will be
4944d672e7aSDavide Libenzi * re-programmed again in the following timerfd_setup() call.
4954d672e7aSDavide Libenzi */
4962456e855SThomas Gleixner if (ctx->expired && ctx->tintv) {
49711ffa9d6STodd Poynor if (isalarm(ctx))
49811ffa9d6STodd Poynor alarm_forward_now(&ctx->t.alarm, ctx->tintv);
49911ffa9d6STodd Poynor else
50011ffa9d6STodd Poynor hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
50111ffa9d6STodd Poynor }
5024d672e7aSDavide Libenzi
503bff41203SDeepa Dinamani old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
504bff41203SDeepa Dinamani old->it_interval = ktime_to_timespec64(ctx->tintv);
5054d672e7aSDavide Libenzi
506b215e283SDavide Libenzi /*
507b215e283SDavide Libenzi * Re-program the timer to the new value ...
508b215e283SDavide Libenzi */
5099d94b9e2SAl Viro ret = timerfd_setup(ctx, flags, new);
510b215e283SDavide Libenzi
51118963c01SDavide Libenzi spin_unlock_irq(&ctx->wqh.lock);
51299ee5315SThomas Gleixner return ret;
513b215e283SDavide Libenzi }
514b215e283SDavide Libenzi
do_timerfd_gettime(int ufd,struct itimerspec64 * t)515bff41203SDeepa Dinamani static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
5164d672e7aSDavide Libenzi {
5174d672e7aSDavide Libenzi struct timerfd_ctx *ctx;
518919a7a1aSAl Viro CLASS(fd, f)(ufd);
519919a7a1aSAl Viro
520919a7a1aSAl Viro if (fd_empty(f))
521919a7a1aSAl Viro return -EBADF;
522919a7a1aSAl Viro if (fd_file(f)->f_op != &timerfd_fops)
523919a7a1aSAl Viro return -EINVAL;
5241da91ea8SAl Viro ctx = fd_file(f)->private_data;
5254d672e7aSDavide Libenzi
5264d672e7aSDavide Libenzi spin_lock_irq(&ctx->wqh.lock);
5272456e855SThomas Gleixner if (ctx->expired && ctx->tintv) {
5284d672e7aSDavide Libenzi ctx->expired = 0;
52911ffa9d6STodd Poynor
53011ffa9d6STodd Poynor if (isalarm(ctx)) {
5314d672e7aSDavide Libenzi ctx->ticks +=
53211ffa9d6STodd Poynor alarm_forward_now(
53311ffa9d6STodd Poynor &ctx->t.alarm, ctx->tintv) - 1;
53411ffa9d6STodd Poynor alarm_restart(&ctx->t.alarm);
53511ffa9d6STodd Poynor } else {
53611ffa9d6STodd Poynor ctx->ticks +=
53711ffa9d6STodd Poynor hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
53811ffa9d6STodd Poynor - 1;
53911ffa9d6STodd Poynor hrtimer_restart(&ctx->t.tmr);
54011ffa9d6STodd Poynor }
5414d672e7aSDavide Libenzi }
542bff41203SDeepa Dinamani t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
543bff41203SDeepa Dinamani t->it_interval = ktime_to_timespec64(ctx->tintv);
5444d672e7aSDavide Libenzi spin_unlock_irq(&ctx->wqh.lock);
5459d94b9e2SAl Viro return 0;
5469d94b9e2SAl Viro }
5474d672e7aSDavide Libenzi
SYSCALL_DEFINE4(timerfd_settime,int,ufd,int,flags,const struct __kernel_itimerspec __user *,utmr,struct __kernel_itimerspec __user *,otmr)5489d94b9e2SAl Viro SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
5496ff84735SDeepa Dinamani const struct __kernel_itimerspec __user *, utmr,
5506ff84735SDeepa Dinamani struct __kernel_itimerspec __user *, otmr)
5519d94b9e2SAl Viro {
552bff41203SDeepa Dinamani struct itimerspec64 new, old;
5539d94b9e2SAl Viro int ret;
5549d94b9e2SAl Viro
555bff41203SDeepa Dinamani if (get_itimerspec64(&new, utmr))
5569d94b9e2SAl Viro return -EFAULT;
5579d94b9e2SAl Viro ret = do_timerfd_settime(ufd, flags, &new, &old);
5589d94b9e2SAl Viro if (ret)
5599d94b9e2SAl Viro return ret;
560bff41203SDeepa Dinamani if (otmr && put_itimerspec64(&old, otmr))
5619d94b9e2SAl Viro return -EFAULT;
5629d94b9e2SAl Viro
5639d94b9e2SAl Viro return ret;
5649d94b9e2SAl Viro }
5659d94b9e2SAl Viro
SYSCALL_DEFINE2(timerfd_gettime,int,ufd,struct __kernel_itimerspec __user *,otmr)5666ff84735SDeepa Dinamani SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
5679d94b9e2SAl Viro {
568bff41203SDeepa Dinamani struct itimerspec64 kotmr;
5699d94b9e2SAl Viro int ret = do_timerfd_gettime(ufd, &kotmr);
5709d94b9e2SAl Viro if (ret)
5719d94b9e2SAl Viro return ret;
572bff41203SDeepa Dinamani return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
573b215e283SDavide Libenzi }
574b215e283SDavide Libenzi
5756ff84735SDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE4(timerfd_settime32,int,ufd,int,flags,const struct old_itimerspec32 __user *,utmr,struct old_itimerspec32 __user *,otmr)5768dabe724SArnd Bergmann SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
5779afc5eeeSArnd Bergmann const struct old_itimerspec32 __user *, utmr,
5789afc5eeeSArnd Bergmann struct old_itimerspec32 __user *, otmr)
5799d94b9e2SAl Viro {
580bff41203SDeepa Dinamani struct itimerspec64 new, old;
5819d94b9e2SAl Viro int ret;
5829d94b9e2SAl Viro
5839afc5eeeSArnd Bergmann if (get_old_itimerspec32(&new, utmr))
5849d94b9e2SAl Viro return -EFAULT;
5859d94b9e2SAl Viro ret = do_timerfd_settime(ufd, flags, &new, &old);
5869d94b9e2SAl Viro if (ret)
5879d94b9e2SAl Viro return ret;
5889afc5eeeSArnd Bergmann if (otmr && put_old_itimerspec32(&old, otmr))
5899d94b9e2SAl Viro return -EFAULT;
5909d94b9e2SAl Viro return ret;
5919d94b9e2SAl Viro }
5929d94b9e2SAl Viro
SYSCALL_DEFINE2(timerfd_gettime32,int,ufd,struct old_itimerspec32 __user *,otmr)5938dabe724SArnd Bergmann SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
5949afc5eeeSArnd Bergmann struct old_itimerspec32 __user *, otmr)
5959d94b9e2SAl Viro {
596bff41203SDeepa Dinamani struct itimerspec64 kotmr;
5979d94b9e2SAl Viro int ret = do_timerfd_gettime(ufd, &kotmr);
5989d94b9e2SAl Viro if (ret)
5999d94b9e2SAl Viro return ret;
6009afc5eeeSArnd Bergmann return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;
6019d94b9e2SAl Viro }
6029d94b9e2SAl Viro #endif
603