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