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