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