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