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