• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
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 
12 #include <linux/alarmtimer.h>
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>
19 #include <linux/slab.h>
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>
26 #include <linux/syscalls.h>
27 #include <linux/compat.h>
28 #include <linux/rcupdate.h>
29 #include <linux/time_namespace.h>
30 
31 #include <trace/hooks/fs.h>
32 
33 struct timerfd_ctx {
34 	union {
35 		struct hrtimer tmr;
36 		struct alarm alarm;
37 	} t;
38 	ktime_t tintv;
39 	ktime_t moffs;
40 	wait_queue_head_t wqh;
41 	u64 ticks;
42 	int clockid;
43 	short unsigned expired;
44 	short unsigned settime_flags;	/* to show in fdinfo */
45 	struct rcu_head rcu;
46 	struct list_head clist;
47 	spinlock_t cancel_lock;
48 	bool might_cancel;
49 };
50 
51 static LIST_HEAD(cancel_list);
52 static DEFINE_SPINLOCK(cancel_lock);
53 
isalarm(struct timerfd_ctx * ctx)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 
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,
63  * tintv != 0) until the timer is accessed.
64  */
timerfd_triggered(struct timerfd_ctx * ctx)65 static void timerfd_triggered(struct timerfd_ctx *ctx)
66 {
67 	unsigned long flags;
68 
69 	spin_lock_irqsave(&ctx->wqh.lock, flags);
70 	ctx->expired = 1;
71 	ctx->ticks++;
72 	wake_up_locked_poll(&ctx->wqh, EPOLLIN);
73 	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
74 }
75 
timerfd_tmrproc(struct hrtimer * htmr)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);
81 	return HRTIMER_NORESTART;
82 }
83 
timerfd_alarmproc(struct alarm * alarm,ktime_t now)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 
93 /*
94  * Called when the clock was set to cancel the timers in the cancel
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().
98  */
timerfd_clock_was_set(void)99 void timerfd_clock_was_set(void)
100 {
101 	ktime_t moffs = ktime_mono_to_real(0);
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);
110 		if (ctx->moffs != moffs) {
111 			ctx->moffs = KTIME_MAX;
112 			ctx->ticks++;
113 			wake_up_locked_poll(&ctx->wqh, EPOLLIN);
114 		}
115 		spin_unlock_irqrestore(&ctx->wqh.lock, flags);
116 	}
117 	rcu_read_unlock();
118 }
119 
__timerfd_remove_cancel(struct timerfd_ctx * ctx)120 static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
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 
timerfd_remove_cancel(struct timerfd_ctx * ctx)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 
timerfd_canceled(struct timerfd_ctx * ctx)137 static bool timerfd_canceled(struct timerfd_ctx *ctx)
138 {
139 	if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
140 		return false;
141 	ctx->moffs = ktime_mono_to_real(0);
142 	return true;
143 }
144 
timerfd_setup_cancel(struct timerfd_ctx * ctx,int flags)145 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
146 {
147 	spin_lock(&ctx->cancel_lock);
148 	if ((ctx->clockid == CLOCK_REALTIME ||
149 	     ctx->clockid == CLOCK_REALTIME_ALARM) &&
150 	    (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
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 		}
157 	} else {
158 		__timerfd_remove_cancel(ctx);
159 	}
160 	spin_unlock(&ctx->cancel_lock);
161 }
162 
timerfd_get_remaining(struct timerfd_ctx * ctx)163 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
164 {
165 	ktime_t remaining;
166 
167 	if (isalarm(ctx))
168 		remaining = alarm_expires_remaining(&ctx->t.alarm);
169 	else
170 		remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
171 
172 	return remaining < 0 ? 0: remaining;
173 }
174 
timerfd_setup(struct timerfd_ctx * ctx,int flags,const struct itimerspec64 * ktmr)175 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
176 			 const struct itimerspec64 *ktmr)
177 {
178 	enum hrtimer_mode htmode;
179 	ktime_t texp;
180 	int clockid = ctx->clockid;
181 
182 	htmode = (flags & TFD_TIMER_ABSTIME) ?
183 		HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
184 
185 	texp = timespec64_to_ktime(ktmr->it_value);
186 	ctx->expired = 0;
187 	ctx->ticks = 0;
188 	ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
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 
201 	if (texp != 0) {
202 		if (flags & TFD_TIMER_ABSTIME)
203 			texp = timens_ktime_to_host(clockid, texp);
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 
213 		if (timerfd_canceled(ctx))
214 			return -ECANCELED;
215 	}
216 
217 	ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
218 	return 0;
219 }
220 
timerfd_release(struct inode * inode,struct file * file)221 static int timerfd_release(struct inode *inode, struct file *file)
222 {
223 	struct timerfd_ctx *ctx = file->private_data;
224 
225 	timerfd_remove_cancel(ctx);
226 
227 	if (isalarm(ctx))
228 		alarm_cancel(&ctx->t.alarm);
229 	else
230 		hrtimer_cancel(&ctx->t.tmr);
231 	kfree_rcu(ctx, rcu);
232 	return 0;
233 }
234 
timerfd_poll(struct file * file,poll_table * wait)235 static __poll_t timerfd_poll(struct file *file, poll_table *wait)
236 {
237 	struct timerfd_ctx *ctx = file->private_data;
238 	__poll_t events = 0;
239 	unsigned long flags;
240 
241 	poll_wait(file, &ctx->wqh, wait);
242 
243 	spin_lock_irqsave(&ctx->wqh.lock, flags);
244 	if (ctx->ticks)
245 		events |= EPOLLIN;
246 	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
247 
248 	return events;
249 }
250 
timerfd_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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;
256 	u64 ticks = 0;
257 
258 	if (count < sizeof(ticks))
259 		return -EINVAL;
260 	spin_lock_irq(&ctx->wqh.lock);
261 	if (file->f_flags & O_NONBLOCK)
262 		res = -EAGAIN;
263 	else
264 		res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
265 
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 
277 	if (ctx->ticks) {
278 		ticks = ctx->ticks;
279 
280 		if (ctx->expired && ctx->tintv) {
281 			/*
282 			 * If tintv != 0, this is a periodic timer that
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 			 */
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 			}
296 		}
297 		ctx->expired = 0;
298 		ctx->ticks = 0;
299 	}
300 	spin_unlock_irq(&ctx->wqh.lock);
301 	if (ticks)
302 		res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
303 	return res;
304 }
305 
306 #ifdef CONFIG_PROC_FS
timerfd_show(struct seq_file * m,struct file * file)307 static void timerfd_show(struct seq_file *m, struct file *file)
308 {
309 	struct timerfd_ctx *ctx = file->private_data;
310 	struct timespec64 value, interval;
311 
312 	spin_lock_irq(&ctx->wqh.lock);
313 	value = ktime_to_timespec64(timerfd_get_remaining(ctx));
314 	interval = ktime_to_timespec64(ctx->tintv);
315 	spin_unlock_irq(&ctx->wqh.lock);
316 
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,
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);
330 }
331 #else
332 #define timerfd_show NULL
333 #endif
334 
335 #ifdef CONFIG_CHECKPOINT_RESTORE
timerfd_ioctl(struct file * file,unsigned int cmd,unsigned long arg)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;
353 			wake_up_locked_poll(&ctx->wqh, EPOLLIN);
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 
370 static const struct file_operations timerfd_fops = {
371 	.release	= timerfd_release,
372 	.poll		= timerfd_poll,
373 	.read		= timerfd_read,
374 	.llseek		= noop_llseek,
375 	.show_fdinfo	= timerfd_show,
376 	.unlocked_ioctl	= timerfd_ioctl,
377 };
378 
timerfd_fget(int fd,struct fd * p)379 static int timerfd_fget(int fd, struct fd *p)
380 {
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;
387 	}
388 	*p = f;
389 	return 0;
390 }
391 
SYSCALL_DEFINE2(timerfd_create,int,clockid,int,flags)392 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
393 {
394 	int ufd;
395 	struct timerfd_ctx *ctx;
396 	char file_name_buf[32];
397 
398 	/* Check the TFD_* constants for consistency.  */
399 	BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
400 	BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
401 
402 	if ((flags & ~TFD_CREATE_FLAGS) ||
403 	    (clockid != CLOCK_MONOTONIC &&
404 	     clockid != CLOCK_REALTIME &&
405 	     clockid != CLOCK_REALTIME_ALARM &&
406 	     clockid != CLOCK_BOOTTIME &&
407 	     clockid != CLOCK_BOOTTIME_ALARM))
408 		return -EINVAL;
409 
410 	if ((clockid == CLOCK_REALTIME_ALARM ||
411 	     clockid == CLOCK_BOOTTIME_ALARM) &&
412 	    !capable(CAP_WAKE_ALARM))
413 		return -EPERM;
414 
415 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
416 	if (!ctx)
417 		return -ENOMEM;
418 
419 	init_waitqueue_head(&ctx->wqh);
420 	spin_lock_init(&ctx->cancel_lock);
421 	ctx->clockid = clockid;
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 
431 	ctx->moffs = ktime_mono_to_real(0);
432 
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,
436 			       O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
437 	if (ufd < 0)
438 		kfree(ctx);
439 
440 	return ufd;
441 }
442 
do_timerfd_settime(int ufd,int flags,const struct itimerspec64 * new,struct itimerspec64 * old)443 static int do_timerfd_settime(int ufd, int flags,
444 		const struct itimerspec64 *new,
445 		struct itimerspec64 *old)
446 {
447 	struct fd f;
448 	struct timerfd_ctx *ctx;
449 	int ret;
450 
451 	if ((flags & ~TFD_SETTIME_FLAGS) ||
452 		 !itimerspec64_valid(new))
453 		return -EINVAL;
454 
455 	ret = timerfd_fget(ufd, &f);
456 	if (ret)
457 		return ret;
458 	ctx = f.file->private_data;
459 
460 	if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
461 		fdput(f);
462 		return -EPERM;
463 	}
464 
465 	timerfd_setup_cancel(ctx, flags);
466 
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);
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 		}
481 		spin_unlock_irq(&ctx->wqh.lock);
482 
483 		if (isalarm(ctx))
484 			hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
485 		else
486 			hrtimer_cancel_wait_running(&ctx->t.tmr);
487 	}
488 
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 	 */
495 	if (ctx->expired && ctx->tintv) {
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 	}
501 
502 	old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
503 	old->it_interval = ktime_to_timespec64(ctx->tintv);
504 
505 	/*
506 	 * Re-program the timer to the new value ...
507 	 */
508 	ret = timerfd_setup(ctx, flags, new);
509 
510 	spin_unlock_irq(&ctx->wqh.lock);
511 	fdput(f);
512 	return ret;
513 }
514 
do_timerfd_gettime(int ufd,struct itimerspec64 * t)515 static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
516 {
517 	struct fd f;
518 	struct timerfd_ctx *ctx;
519 	int ret = timerfd_fget(ufd, &f);
520 	if (ret)
521 		return ret;
522 	ctx = f.file->private_data;
523 
524 	spin_lock_irq(&ctx->wqh.lock);
525 	if (ctx->expired && ctx->tintv) {
526 		ctx->expired = 0;
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 		}
539 	}
540 	t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
541 	t->it_interval = ktime_to_timespec64(ctx->tintv);
542 	spin_unlock_irq(&ctx->wqh.lock);
543 	fdput(f);
544 	return 0;
545 }
546 
SYSCALL_DEFINE4(timerfd_settime,int,ufd,int,flags,const struct __kernel_itimerspec __user *,utmr,struct __kernel_itimerspec __user *,otmr)547 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
548 		const struct __kernel_itimerspec __user *, utmr,
549 		struct __kernel_itimerspec __user *, otmr)
550 {
551 	struct itimerspec64 new, old;
552 	int ret;
553 
554 	if (get_itimerspec64(&new, utmr))
555 		return -EFAULT;
556 	ret = do_timerfd_settime(ufd, flags, &new, &old);
557 	if (ret)
558 		return ret;
559 	if (otmr && put_itimerspec64(&old, otmr))
560 		return -EFAULT;
561 
562 	return ret;
563 }
564 
SYSCALL_DEFINE2(timerfd_gettime,int,ufd,struct __kernel_itimerspec __user *,otmr)565 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
566 {
567 	struct itimerspec64 kotmr;
568 	int ret = do_timerfd_gettime(ufd, &kotmr);
569 	if (ret)
570 		return ret;
571 	return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
572 }
573 
574 #ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE4(timerfd_settime32,int,ufd,int,flags,const struct old_itimerspec32 __user *,utmr,struct old_itimerspec32 __user *,otmr)575 SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
576 		const struct old_itimerspec32 __user *, utmr,
577 		struct old_itimerspec32 __user *, otmr)
578 {
579 	struct itimerspec64 new, old;
580 	int ret;
581 
582 	if (get_old_itimerspec32(&new, utmr))
583 		return -EFAULT;
584 	ret = do_timerfd_settime(ufd, flags, &new, &old);
585 	if (ret)
586 		return ret;
587 	if (otmr && put_old_itimerspec32(&old, otmr))
588 		return -EFAULT;
589 	return ret;
590 }
591 
SYSCALL_DEFINE2(timerfd_gettime32,int,ufd,struct old_itimerspec32 __user *,otmr)592 SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
593 		struct old_itimerspec32 __user *, otmr)
594 {
595 	struct itimerspec64 kotmr;
596 	int ret = do_timerfd_gettime(ufd, &kotmr);
597 	if (ret)
598 		return ret;
599 	return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;
600 }
601 #endif
602