xref: /linux-6.15/fs/timerfd.c (revision f9835fa1)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b215e283SDavide Libenzi /*
3b215e283SDavide Libenzi  *  fs/timerfd.c
4b215e283SDavide Libenzi  *
5b215e283SDavide Libenzi  *  Copyright (C) 2007  Davide Libenzi <[email protected]>
6b215e283SDavide Libenzi  *
7b215e283SDavide Libenzi  *
8b215e283SDavide Libenzi  *  Thanks to Thomas Gleixner for code reviews and useful comments.
9b215e283SDavide Libenzi  *
10b215e283SDavide Libenzi  */
11b215e283SDavide Libenzi 
1211ffa9d6STodd Poynor #include <linux/alarmtimer.h>
13b215e283SDavide Libenzi #include <linux/file.h>
14b215e283SDavide Libenzi #include <linux/poll.h>
15b215e283SDavide Libenzi #include <linux/init.h>
16b215e283SDavide Libenzi #include <linux/fs.h>
17b215e283SDavide Libenzi #include <linux/sched.h>
18b215e283SDavide Libenzi #include <linux/kernel.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20b215e283SDavide Libenzi #include <linux/list.h>
21b215e283SDavide Libenzi #include <linux/spinlock.h>
22b215e283SDavide Libenzi #include <linux/time.h>
23b215e283SDavide Libenzi #include <linux/hrtimer.h>
24b215e283SDavide Libenzi #include <linux/anon_inodes.h>
25b215e283SDavide Libenzi #include <linux/timerfd.h>
2645cc2b96SAdrian Bunk #include <linux/syscalls.h>
279d94b9e2SAl Viro #include <linux/compat.h>
289ec26907SThomas Gleixner #include <linux/rcupdate.h>
296cd889d4SAndrei Vagin #include <linux/time_namespace.h>
30b215e283SDavide Libenzi 
31b215e283SDavide Libenzi struct timerfd_ctx {
3211ffa9d6STodd Poynor 	union {
33b215e283SDavide Libenzi 		struct hrtimer tmr;
3411ffa9d6STodd Poynor 		struct alarm alarm;
3511ffa9d6STodd Poynor 	} t;
36b215e283SDavide Libenzi 	ktime_t tintv;
3799ee5315SThomas Gleixner 	ktime_t moffs;
38b215e283SDavide Libenzi 	wait_queue_head_t wqh;
394d672e7aSDavide Libenzi 	u64 ticks;
404d672e7aSDavide Libenzi 	int clockid;
41af9c4957SCyrill Gorcunov 	short unsigned expired;
42af9c4957SCyrill Gorcunov 	short unsigned settime_flags;	/* to show in fdinfo */
439ec26907SThomas Gleixner 	struct rcu_head rcu;
449ec26907SThomas Gleixner 	struct list_head clist;
451e38da30SThomas Gleixner 	spinlock_t cancel_lock;
4699ee5315SThomas Gleixner 	bool might_cancel;
47b215e283SDavide Libenzi };
48b215e283SDavide Libenzi 
499ec26907SThomas Gleixner static LIST_HEAD(cancel_list);
509ec26907SThomas Gleixner static DEFINE_SPINLOCK(cancel_lock);
519ec26907SThomas Gleixner 
isalarm(struct timerfd_ctx * ctx)5211ffa9d6STodd Poynor static inline bool isalarm(struct timerfd_ctx *ctx)
5311ffa9d6STodd Poynor {
5411ffa9d6STodd Poynor 	return ctx->clockid == CLOCK_REALTIME_ALARM ||
5511ffa9d6STodd Poynor 		ctx->clockid == CLOCK_BOOTTIME_ALARM;
5611ffa9d6STodd Poynor }
5711ffa9d6STodd Poynor 
58b215e283SDavide Libenzi /*
59b215e283SDavide Libenzi  * This gets called when the timer event triggers. We set the "expired"
60b215e283SDavide Libenzi  * flag, but we do not re-arm the timer (in case it's necessary,
612456e855SThomas Gleixner  * tintv != 0) until the timer is accessed.
62b215e283SDavide Libenzi  */
timerfd_triggered(struct timerfd_ctx * ctx)6311ffa9d6STodd Poynor static void timerfd_triggered(struct timerfd_ctx *ctx)
64b215e283SDavide Libenzi {
65b215e283SDavide Libenzi 	unsigned long flags;
66b215e283SDavide Libenzi 
6718963c01SDavide Libenzi 	spin_lock_irqsave(&ctx->wqh.lock, flags);
68b215e283SDavide Libenzi 	ctx->expired = 1;
694d672e7aSDavide Libenzi 	ctx->ticks++;
707dda7128SChristoph Hellwig 	wake_up_locked_poll(&ctx->wqh, EPOLLIN);
7118963c01SDavide Libenzi 	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
7211ffa9d6STodd Poynor }
73b215e283SDavide Libenzi 
timerfd_tmrproc(struct hrtimer * htmr)7411ffa9d6STodd Poynor static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
7511ffa9d6STodd Poynor {
7611ffa9d6STodd Poynor 	struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
7711ffa9d6STodd Poynor 					       t.tmr);
7811ffa9d6STodd Poynor 	timerfd_triggered(ctx);
79b215e283SDavide Libenzi 	return HRTIMER_NORESTART;
80b215e283SDavide Libenzi }
81b215e283SDavide Libenzi 
timerfd_alarmproc(struct alarm * alarm,ktime_t now)822634303fSThomas Gleixner static void timerfd_alarmproc(struct alarm *alarm, ktime_t now)
8311ffa9d6STodd Poynor {
8411ffa9d6STodd Poynor 	struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
8511ffa9d6STodd Poynor 					       t.alarm);
8611ffa9d6STodd Poynor 	timerfd_triggered(ctx);
8711ffa9d6STodd Poynor }
8811ffa9d6STodd Poynor 
899ec26907SThomas Gleixner /*
909ec26907SThomas Gleixner  * Called when the clock was set to cancel the timers in the cancel
911123d939SMax Asbock  * list. This will wake up processes waiting on these timers. The
921123d939SMax Asbock  * wake-up requires ctx->ticks to be non zero, therefore we increment
931123d939SMax Asbock  * it before calling wake_up_locked().
949ec26907SThomas Gleixner  */
timerfd_clock_was_set(void)959ec26907SThomas Gleixner void timerfd_clock_was_set(void)
969ec26907SThomas Gleixner {
972456e855SThomas Gleixner 	ktime_t moffs = ktime_mono_to_real(0);
989ec26907SThomas Gleixner 	struct timerfd_ctx *ctx;
999ec26907SThomas Gleixner 	unsigned long flags;
1009ec26907SThomas Gleixner 
1019ec26907SThomas Gleixner 	rcu_read_lock();
1029ec26907SThomas Gleixner 	list_for_each_entry_rcu(ctx, &cancel_list, clist) {
1039ec26907SThomas Gleixner 		if (!ctx->might_cancel)
1049ec26907SThomas Gleixner 			continue;
1059ec26907SThomas Gleixner 		spin_lock_irqsave(&ctx->wqh.lock, flags);
1062456e855SThomas Gleixner 		if (ctx->moffs != moffs) {
1072456e855SThomas Gleixner 			ctx->moffs = KTIME_MAX;
1081123d939SMax Asbock 			ctx->ticks++;
1097dda7128SChristoph Hellwig 			wake_up_locked_poll(&ctx->wqh, EPOLLIN);
1109ec26907SThomas Gleixner 		}
1119ec26907SThomas Gleixner 		spin_unlock_irqrestore(&ctx->wqh.lock, flags);
1129ec26907SThomas Gleixner 	}
1139ec26907SThomas Gleixner 	rcu_read_unlock();
1149ec26907SThomas Gleixner }
1159ec26907SThomas Gleixner 
timerfd_resume_work(struct work_struct * work)11666f7b0c8SThomas Gleixner static void timerfd_resume_work(struct work_struct *work)
11766f7b0c8SThomas Gleixner {
11866f7b0c8SThomas Gleixner 	timerfd_clock_was_set();
11966f7b0c8SThomas Gleixner }
12066f7b0c8SThomas Gleixner 
12166f7b0c8SThomas Gleixner static DECLARE_WORK(timerfd_work, timerfd_resume_work);
12266f7b0c8SThomas Gleixner 
12366f7b0c8SThomas Gleixner /*
12466f7b0c8SThomas Gleixner  * Invoked from timekeeping_resume(). Defer the actual update to work so
12566f7b0c8SThomas Gleixner  * timerfd_clock_was_set() runs in task context.
12666f7b0c8SThomas Gleixner  */
timerfd_resume(void)12766f7b0c8SThomas Gleixner void timerfd_resume(void)
12866f7b0c8SThomas Gleixner {
12966f7b0c8SThomas Gleixner 	schedule_work(&timerfd_work);
13066f7b0c8SThomas Gleixner }
13166f7b0c8SThomas Gleixner 
__timerfd_remove_cancel(struct timerfd_ctx * ctx)1321e38da30SThomas Gleixner static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
1339ec26907SThomas Gleixner {
1349ec26907SThomas Gleixner 	if (ctx->might_cancel) {
1359ec26907SThomas Gleixner 		ctx->might_cancel = false;
1369ec26907SThomas Gleixner 		spin_lock(&cancel_lock);
1379ec26907SThomas Gleixner 		list_del_rcu(&ctx->clist);
1389ec26907SThomas Gleixner 		spin_unlock(&cancel_lock);
1399ec26907SThomas Gleixner 	}
1409ec26907SThomas Gleixner }
1419ec26907SThomas Gleixner 
timerfd_remove_cancel(struct timerfd_ctx * ctx)1421e38da30SThomas Gleixner static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
1431e38da30SThomas Gleixner {
1441e38da30SThomas Gleixner 	spin_lock(&ctx->cancel_lock);
1451e38da30SThomas Gleixner 	__timerfd_remove_cancel(ctx);
1461e38da30SThomas Gleixner 	spin_unlock(&ctx->cancel_lock);
1471e38da30SThomas Gleixner }
1481e38da30SThomas Gleixner 
timerfd_canceled(struct timerfd_ctx * ctx)1499ec26907SThomas Gleixner static bool timerfd_canceled(struct timerfd_ctx *ctx)
1509ec26907SThomas Gleixner {
1512456e855SThomas Gleixner 	if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
1529ec26907SThomas Gleixner 		return false;
1532456e855SThomas Gleixner 	ctx->moffs = ktime_mono_to_real(0);
1549ec26907SThomas Gleixner 	return true;
1559ec26907SThomas Gleixner }
1569ec26907SThomas Gleixner 
timerfd_setup_cancel(struct timerfd_ctx * ctx,int flags)1579ec26907SThomas Gleixner static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
1589ec26907SThomas Gleixner {
1591e38da30SThomas Gleixner 	spin_lock(&ctx->cancel_lock);
16011ffa9d6STodd Poynor 	if ((ctx->clockid == CLOCK_REALTIME ||
16111ffa9d6STodd Poynor 	     ctx->clockid == CLOCK_REALTIME_ALARM) &&
16211ffa9d6STodd Poynor 	    (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
1639ec26907SThomas Gleixner 		if (!ctx->might_cancel) {
1649ec26907SThomas Gleixner 			ctx->might_cancel = true;
1659ec26907SThomas Gleixner 			spin_lock(&cancel_lock);
1669ec26907SThomas Gleixner 			list_add_rcu(&ctx->clist, &cancel_list);
1679ec26907SThomas Gleixner 			spin_unlock(&cancel_lock);
1689ec26907SThomas Gleixner 		}
1691e38da30SThomas Gleixner 	} else {
1701e38da30SThomas Gleixner 		__timerfd_remove_cancel(ctx);
1719ec26907SThomas Gleixner 	}
1721e38da30SThomas Gleixner 	spin_unlock(&ctx->cancel_lock);
1739ec26907SThomas Gleixner }
1749ec26907SThomas Gleixner 
timerfd_get_remaining(struct timerfd_ctx * ctx)1754d672e7aSDavide Libenzi static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
1764d672e7aSDavide Libenzi {
17776369470SArjan van de Ven 	ktime_t remaining;
1784d672e7aSDavide Libenzi 
17911ffa9d6STodd Poynor 	if (isalarm(ctx))
18011ffa9d6STodd Poynor 		remaining = alarm_expires_remaining(&ctx->t.alarm);
18111ffa9d6STodd Poynor 	else
182b62526edSThomas Gleixner 		remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
18311ffa9d6STodd Poynor 
1848b0e1953SThomas Gleixner 	return remaining < 0 ? 0: remaining;
1854d672e7aSDavide Libenzi }
1864d672e7aSDavide Libenzi 
timerfd_setup(struct timerfd_ctx * ctx,int flags,const struct itimerspec64 * ktmr)18799ee5315SThomas Gleixner static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
188bff41203SDeepa Dinamani 			 const struct itimerspec64 *ktmr)
189b215e283SDavide Libenzi {
190b215e283SDavide Libenzi 	enum hrtimer_mode htmode;
191b215e283SDavide Libenzi 	ktime_t texp;
19299ee5315SThomas Gleixner 	int clockid = ctx->clockid;
193b215e283SDavide Libenzi 
194b215e283SDavide Libenzi 	htmode = (flags & TFD_TIMER_ABSTIME) ?
195b215e283SDavide Libenzi 		HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
196b215e283SDavide Libenzi 
197bff41203SDeepa Dinamani 	texp = timespec64_to_ktime(ktmr->it_value);
198b215e283SDavide Libenzi 	ctx->expired = 0;
1994d672e7aSDavide Libenzi 	ctx->ticks = 0;
200bff41203SDeepa Dinamani 	ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
20111ffa9d6STodd Poynor 
20211ffa9d6STodd Poynor 	if (isalarm(ctx)) {
20311ffa9d6STodd Poynor 		alarm_init(&ctx->t.alarm,
20411ffa9d6STodd Poynor 			   ctx->clockid == CLOCK_REALTIME_ALARM ?
20511ffa9d6STodd Poynor 			   ALARM_REALTIME : ALARM_BOOTTIME,
20611ffa9d6STodd Poynor 			   timerfd_alarmproc);
20711ffa9d6STodd Poynor 	} else {
20811ffa9d6STodd Poynor 		hrtimer_setup(&ctx->t.tmr, timerfd_tmrproc, clockid, htmode);
20911ffa9d6STodd Poynor 		hrtimer_set_expires(&ctx->t.tmr, texp);
21011ffa9d6STodd Poynor 	}
21111ffa9d6STodd Poynor 
21211ffa9d6STodd Poynor 	if (texp != 0) {
2132456e855SThomas Gleixner 		if (flags & TFD_TIMER_ABSTIME)
2146cd889d4SAndrei Vagin 			texp = timens_ktime_to_host(clockid, texp);
2156cd889d4SAndrei Vagin 		if (isalarm(ctx)) {
21611ffa9d6STodd Poynor 			if (flags & TFD_TIMER_ABSTIME)
21711ffa9d6STodd Poynor 				alarm_start(&ctx->t.alarm, texp);
21811ffa9d6STodd Poynor 			else
21911ffa9d6STodd Poynor 				alarm_start_relative(&ctx->t.alarm, texp);
22011ffa9d6STodd Poynor 		} else {
22111ffa9d6STodd Poynor 			hrtimer_start(&ctx->t.tmr, texp, htmode);
22211ffa9d6STodd Poynor 		}
22311ffa9d6STodd Poynor 
22411ffa9d6STodd Poynor 		if (timerfd_canceled(ctx))
22599ee5315SThomas Gleixner 			return -ECANCELED;
22699ee5315SThomas Gleixner 	}
22799ee5315SThomas Gleixner 
228af9c4957SCyrill Gorcunov 	ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
229af9c4957SCyrill Gorcunov 	return 0;
23099ee5315SThomas Gleixner }
231b215e283SDavide Libenzi 
timerfd_release(struct inode * inode,struct file * file)232b215e283SDavide Libenzi static int timerfd_release(struct inode *inode, struct file *file)
233b215e283SDavide Libenzi {
234b215e283SDavide Libenzi 	struct timerfd_ctx *ctx = file->private_data;
235b215e283SDavide Libenzi 
236b215e283SDavide Libenzi 	timerfd_remove_cancel(ctx);
2379ec26907SThomas Gleixner 
23811ffa9d6STodd Poynor 	if (isalarm(ctx))
23911ffa9d6STodd Poynor 		alarm_cancel(&ctx->t.alarm);
24011ffa9d6STodd Poynor 	else
24111ffa9d6STodd Poynor 		hrtimer_cancel(&ctx->t.tmr);
24211ffa9d6STodd Poynor 	kfree_rcu(ctx, rcu);
2439ec26907SThomas Gleixner 	return 0;
244b215e283SDavide Libenzi }
245b215e283SDavide Libenzi 
timerfd_poll(struct file * file,poll_table * wait)246b215e283SDavide Libenzi static __poll_t timerfd_poll(struct file *file, poll_table *wait)
247a11e1d43SLinus Torvalds {
248b215e283SDavide Libenzi 	struct timerfd_ctx *ctx = file->private_data;
249b215e283SDavide Libenzi 	__poll_t events = 0;
250a11e1d43SLinus Torvalds 	unsigned long flags;
251a11e1d43SLinus Torvalds 
252b215e283SDavide Libenzi 	poll_wait(file, &ctx->wqh, wait);
253a11e1d43SLinus Torvalds 
254b215e283SDavide Libenzi 	spin_lock_irqsave(&ctx->wqh.lock, flags);
255a11e1d43SLinus Torvalds 	if (ctx->ticks)
256a11e1d43SLinus Torvalds 		events |= EPOLLIN;
257a11e1d43SLinus Torvalds 	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
258a11e1d43SLinus Torvalds 
259b215e283SDavide Libenzi 	return events;
260a11e1d43SLinus Torvalds }
261b215e283SDavide Libenzi 
timerfd_read_iter(struct kiocb * iocb,struct iov_iter * to)262b215e283SDavide Libenzi static ssize_t timerfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
263d9497990SJens Axboe {
264b215e283SDavide Libenzi 	struct file *file = iocb->ki_filp;
265d9497990SJens Axboe 	struct timerfd_ctx *ctx = file->private_data;
266b215e283SDavide Libenzi 	ssize_t res;
267b215e283SDavide Libenzi 	u64 ticks = 0;
26809828402SDavide Libenzi 
269b215e283SDavide Libenzi 	if (iov_iter_count(to) < sizeof(ticks))
270d9497990SJens Axboe 		return -EINVAL;
271b215e283SDavide Libenzi 
272d9497990SJens Axboe 	spin_lock_irq(&ctx->wqh.lock);
27318963c01SDavide Libenzi 	if (file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT)
274d9497990SJens Axboe 		res = -EAGAIN;
275b215e283SDavide Libenzi 	else
2768120a8aaSMichal Nazarewicz 		res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
2778120a8aaSMichal Nazarewicz 
27899ee5315SThomas Gleixner 	/*
27999ee5315SThomas Gleixner 	 * If clock has changed, we do not care about the
28099ee5315SThomas Gleixner 	 * ticks and we do not rearm the timer. Userspace must
28199ee5315SThomas Gleixner 	 * reevaluate anyway.
28299ee5315SThomas Gleixner 	 */
28399ee5315SThomas Gleixner 	if (timerfd_canceled(ctx)) {
28499ee5315SThomas Gleixner 		ctx->ticks = 0;
2859ec26907SThomas Gleixner 		ctx->expired = 0;
28699ee5315SThomas Gleixner 		res = -ECANCELED;
28799ee5315SThomas Gleixner 	}
28899ee5315SThomas Gleixner 
28999ee5315SThomas Gleixner 	if (ctx->ticks) {
2909ec26907SThomas Gleixner 		ticks = ctx->ticks;
2919ec26907SThomas Gleixner 
2929ec26907SThomas Gleixner 		if (ctx->expired && ctx->tintv) {
2932456e855SThomas Gleixner 			/*
294b215e283SDavide Libenzi 			 * If tintv != 0, this is a periodic timer that
2952456e855SThomas Gleixner 			 * needs to be re-armed. We avoid doing it in the timer
296b215e283SDavide Libenzi 			 * callback to avoid DoS attacks specifying a very
297b215e283SDavide Libenzi 			 * short timer period.
298b215e283SDavide Libenzi 			 */
299b215e283SDavide Libenzi 			if (isalarm(ctx)) {
30011ffa9d6STodd Poynor 				ticks += alarm_forward_now(
30111ffa9d6STodd Poynor 					&ctx->t.alarm, ctx->tintv) - 1;
30211ffa9d6STodd Poynor 				alarm_restart(&ctx->t.alarm);
30311ffa9d6STodd Poynor 			} else {
30411ffa9d6STodd Poynor 				ticks += hrtimer_forward_now(&ctx->t.tmr,
30511ffa9d6STodd Poynor 							     ctx->tintv) - 1;
3064d672e7aSDavide Libenzi 				hrtimer_restart(&ctx->t.tmr);
30711ffa9d6STodd Poynor 			}
30811ffa9d6STodd Poynor 		}
3094d672e7aSDavide Libenzi 		ctx->expired = 0;
3104d672e7aSDavide Libenzi 		ctx->ticks = 0;
3114d672e7aSDavide Libenzi 	}
312b215e283SDavide Libenzi 	spin_unlock_irq(&ctx->wqh.lock);
31318963c01SDavide Libenzi 	if (ticks) {
314d9497990SJens Axboe 		res = copy_to_iter(&ticks, sizeof(ticks), to);
315d9497990SJens Axboe 		if (!res)
316d9497990SJens Axboe 			res = -EFAULT;
317d9497990SJens Axboe 	}
318d9497990SJens Axboe 	return res;
319b215e283SDavide Libenzi }
320b215e283SDavide Libenzi 
321b215e283SDavide Libenzi #ifdef CONFIG_PROC_FS
timerfd_show(struct seq_file * m,struct file * file)322af9c4957SCyrill Gorcunov static void timerfd_show(struct seq_file *m, struct file *file)
323a3816ab0SJoe Perches {
324af9c4957SCyrill Gorcunov 	struct timerfd_ctx *ctx = file->private_data;
325af9c4957SCyrill Gorcunov 	struct timespec64 value, interval;
326bde9e963SArnd Bergmann 
327af9c4957SCyrill Gorcunov 	spin_lock_irq(&ctx->wqh.lock);
328af9c4957SCyrill Gorcunov 	value = ktime_to_timespec64(timerfd_get_remaining(ctx));
329bde9e963SArnd Bergmann 	interval = ktime_to_timespec64(ctx->tintv);
330bde9e963SArnd Bergmann 	spin_unlock_irq(&ctx->wqh.lock);
331af9c4957SCyrill Gorcunov 
332af9c4957SCyrill Gorcunov 	seq_printf(m,
333a3816ab0SJoe Perches 		   "clockid: %d\n"
334af9c4957SCyrill Gorcunov 		   "ticks: %llu\n"
335af9c4957SCyrill Gorcunov 		   "settime flags: 0%o\n"
336af9c4957SCyrill Gorcunov 		   "it_value: (%llu, %llu)\n"
337af9c4957SCyrill Gorcunov 		   "it_interval: (%llu, %llu)\n",
338af9c4957SCyrill Gorcunov 		   ctx->clockid,
339a3816ab0SJoe Perches 		   (unsigned long long)ctx->ticks,
340a3816ab0SJoe Perches 		   ctx->settime_flags,
341af9c4957SCyrill Gorcunov 		   (unsigned long long)value.tv_sec,
342bde9e963SArnd Bergmann 		   (unsigned long long)value.tv_nsec,
343bde9e963SArnd Bergmann 		   (unsigned long long)interval.tv_sec,
344bde9e963SArnd Bergmann 		   (unsigned long long)interval.tv_nsec);
345bde9e963SArnd Bergmann }
346af9c4957SCyrill Gorcunov #else
347af9c4957SCyrill Gorcunov #define timerfd_show NULL
348af9c4957SCyrill Gorcunov #endif
349af9c4957SCyrill Gorcunov 
350af9c4957SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
timerfd_ioctl(struct file * file,unsigned int cmd,unsigned long arg)3515442e9fbSCyrill Gorcunov static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3525442e9fbSCyrill Gorcunov {
3535442e9fbSCyrill Gorcunov 	struct timerfd_ctx *ctx = file->private_data;
3545442e9fbSCyrill Gorcunov 	int ret = 0;
3555442e9fbSCyrill Gorcunov 
3565442e9fbSCyrill Gorcunov 	switch (cmd) {
3575442e9fbSCyrill Gorcunov 	case TFD_IOC_SET_TICKS: {
3585442e9fbSCyrill Gorcunov 		u64 ticks;
3595442e9fbSCyrill Gorcunov 
3605442e9fbSCyrill Gorcunov 		if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
3615442e9fbSCyrill Gorcunov 			return -EFAULT;
3625442e9fbSCyrill Gorcunov 		if (!ticks)
3635442e9fbSCyrill Gorcunov 			return -EINVAL;
3645442e9fbSCyrill Gorcunov 
3655442e9fbSCyrill Gorcunov 		spin_lock_irq(&ctx->wqh.lock);
3665442e9fbSCyrill Gorcunov 		if (!timerfd_canceled(ctx)) {
3675442e9fbSCyrill Gorcunov 			ctx->ticks = ticks;
3685442e9fbSCyrill Gorcunov 			wake_up_locked_poll(&ctx->wqh, EPOLLIN);
3697dda7128SChristoph Hellwig 		} else
3705442e9fbSCyrill Gorcunov 			ret = -ECANCELED;
3715442e9fbSCyrill Gorcunov 		spin_unlock_irq(&ctx->wqh.lock);
3725442e9fbSCyrill Gorcunov 		break;
3735442e9fbSCyrill Gorcunov 	}
3745442e9fbSCyrill Gorcunov 	default:
3755442e9fbSCyrill Gorcunov 		ret = -ENOTTY;
3765442e9fbSCyrill Gorcunov 		break;
3775442e9fbSCyrill Gorcunov 	}
3785442e9fbSCyrill Gorcunov 
3795442e9fbSCyrill Gorcunov 	return ret;
3805442e9fbSCyrill Gorcunov }
3815442e9fbSCyrill Gorcunov #else
3825442e9fbSCyrill Gorcunov #define timerfd_ioctl NULL
3835442e9fbSCyrill Gorcunov #endif
3845442e9fbSCyrill Gorcunov 
3855442e9fbSCyrill Gorcunov static const struct file_operations timerfd_fops = {
386b215e283SDavide Libenzi 	.release	= timerfd_release,
387b215e283SDavide Libenzi 	.poll		= timerfd_poll,
388a11e1d43SLinus Torvalds 	.read_iter	= timerfd_read_iter,
389d9497990SJens Axboe 	.llseek		= noop_llseek,
3906038f373SArnd Bergmann 	.show_fdinfo	= timerfd_show,
391af9c4957SCyrill Gorcunov 	.unlocked_ioctl	= timerfd_ioctl,
3925442e9fbSCyrill Gorcunov };
393b215e283SDavide Libenzi 
SYSCALL_DEFINE2(timerfd_create,int,clockid,int,flags)394b215e283SDavide Libenzi SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
395836f92adSHeiko Carstens {
3964d672e7aSDavide Libenzi 	int ufd;
3972030a42cSAl Viro 	struct timerfd_ctx *ctx;
398b215e283SDavide Libenzi 	struct file *file;
399d9497990SJens Axboe 
400b215e283SDavide Libenzi 	/* Check the TFD_* constants for consistency.  */
401e38b36f3SUlrich Drepper 	BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
402e38b36f3SUlrich Drepper 	BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
403e38b36f3SUlrich Drepper 
404e38b36f3SUlrich Drepper 	if ((flags & ~TFD_CREATE_FLAGS) ||
405610d18f4SDavide Libenzi 	    (clockid != CLOCK_MONOTONIC &&
406610d18f4SDavide Libenzi 	     clockid != CLOCK_REALTIME &&
40711ffa9d6STodd Poynor 	     clockid != CLOCK_REALTIME_ALARM &&
40811ffa9d6STodd Poynor 	     clockid != CLOCK_BOOTTIME &&
4094a2378a9SGreg Hackmann 	     clockid != CLOCK_BOOTTIME_ALARM))
41011ffa9d6STodd Poynor 		return -EINVAL;
411b215e283SDavide Libenzi 
412b215e283SDavide Libenzi 	if ((clockid == CLOCK_REALTIME_ALARM ||
41325b68a8fSStephen Smalley 	     clockid == CLOCK_BOOTTIME_ALARM) &&
41425b68a8fSStephen Smalley 	    !capable(CAP_WAKE_ALARM))
41525b68a8fSStephen Smalley 		return -EPERM;
4162895a5e5SEric Caruso 
4172895a5e5SEric Caruso 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
4184d672e7aSDavide Libenzi 	if (!ctx)
419b215e283SDavide Libenzi 		return -ENOMEM;
420b215e283SDavide Libenzi 
421b215e283SDavide Libenzi 	init_waitqueue_head(&ctx->wqh);
422b215e283SDavide Libenzi 	spin_lock_init(&ctx->cancel_lock);
4231e38da30SThomas Gleixner 	ctx->clockid = clockid;
4244d672e7aSDavide Libenzi 
42511ffa9d6STodd Poynor 	if (isalarm(ctx))
42611ffa9d6STodd Poynor 		alarm_init(&ctx->t.alarm,
42711ffa9d6STodd Poynor 			   ctx->clockid == CLOCK_REALTIME_ALARM ?
42811ffa9d6STodd Poynor 			   ALARM_REALTIME : ALARM_BOOTTIME,
42911ffa9d6STodd Poynor 			   timerfd_alarmproc);
43011ffa9d6STodd Poynor 	else
43111ffa9d6STodd Poynor 		hrtimer_setup(&ctx->t.tmr, timerfd_tmrproc, clockid, HRTIMER_MODE_ABS);
43211ffa9d6STodd Poynor 
43311ffa9d6STodd Poynor 	ctx->moffs = ktime_mono_to_real(0);
4342456e855SThomas Gleixner 
435b215e283SDavide Libenzi 	ufd = get_unused_fd_flags(flags & TFD_SHARED_FCNTL_FLAGS);
436d9497990SJens Axboe 	if (ufd < 0) {
437d9497990SJens Axboe 		kfree(ctx);
4384d672e7aSDavide Libenzi 		return ufd;
439d9497990SJens Axboe 	}
440d9497990SJens Axboe 
4414d672e7aSDavide Libenzi 	file = anon_inode_getfile_fmode("[timerfd]", &timerfd_fops, ctx,
442*f9835fa1SAl Viro 			    O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS),
443*f9835fa1SAl Viro 			    FMODE_NOWAIT);
444*f9835fa1SAl Viro 	if (IS_ERR(file)) {
445d9497990SJens Axboe 		put_unused_fd(ufd);
446d9497990SJens Axboe 		kfree(ctx);
447d9497990SJens Axboe 		return PTR_ERR(file);
448d9497990SJens Axboe 	}
449d9497990SJens Axboe 
450d9497990SJens Axboe 	fd_install(ufd, file);
451d9497990SJens Axboe 	return ufd;
4524d672e7aSDavide Libenzi }
4534d672e7aSDavide Libenzi 
do_timerfd_settime(int ufd,int flags,const struct itimerspec64 * new,struct itimerspec64 * old)4544d672e7aSDavide Libenzi static int do_timerfd_settime(int ufd, int flags,
4559d94b9e2SAl Viro 		const struct itimerspec64 *new,
456bff41203SDeepa Dinamani 		struct itimerspec64 *old)
457bff41203SDeepa Dinamani {
4584d672e7aSDavide Libenzi 	struct timerfd_ctx *ctx;
4594d672e7aSDavide Libenzi 	int ret;
4602903ff01SAl Viro 
4614d672e7aSDavide Libenzi 	if ((flags & ~TFD_SETTIME_FLAGS) ||
462610d18f4SDavide Libenzi 		 !itimerspec64_valid(new))
463bff41203SDeepa Dinamani 		return -EINVAL;
4644d672e7aSDavide Libenzi 
4654d672e7aSDavide Libenzi 	CLASS(fd, f)(ufd);
466919a7a1aSAl Viro 	if (fd_empty(f))
467919a7a1aSAl Viro 		return -EBADF;
468919a7a1aSAl Viro 
469919a7a1aSAl Viro 	if (fd_file(f)->f_op != &timerfd_fops)
470919a7a1aSAl Viro 		return -EINVAL;
471919a7a1aSAl Viro 
472919a7a1aSAl Viro 	ctx = fd_file(f)->private_data;
4731da91ea8SAl Viro 
4744d672e7aSDavide Libenzi 	if (isalarm(ctx) && !capable(CAP_WAKE_ALARM))
475919a7a1aSAl Viro 		return -EPERM;
4762895a5e5SEric Caruso 
4772895a5e5SEric Caruso 	timerfd_setup_cancel(ctx, flags);
4789ec26907SThomas Gleixner 
4799ec26907SThomas Gleixner 	/*
480b215e283SDavide Libenzi 	 * We need to stop the existing timer before reprogramming
481b215e283SDavide Libenzi 	 * it to the new values.
482b215e283SDavide Libenzi 	 */
483b215e283SDavide Libenzi 	for (;;) {
484b215e283SDavide Libenzi 		spin_lock_irq(&ctx->wqh.lock);
48518963c01SDavide Libenzi 
48611ffa9d6STodd Poynor 		if (isalarm(ctx)) {
48711ffa9d6STodd Poynor 			if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
48811ffa9d6STodd Poynor 				break;
489b215e283SDavide Libenzi 		} else {
49011ffa9d6STodd Poynor 			if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
49111ffa9d6STodd Poynor 				break;
49211ffa9d6STodd Poynor 		}
49311ffa9d6STodd Poynor 		spin_unlock_irq(&ctx->wqh.lock);
49418963c01SDavide Libenzi 
495a125ecc1SAnna-Maria Gleixner 		if (isalarm(ctx))
496a125ecc1SAnna-Maria Gleixner 			hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
497a125ecc1SAnna-Maria Gleixner 		else
498a125ecc1SAnna-Maria Gleixner 			hrtimer_cancel_wait_running(&ctx->t.tmr);
499a125ecc1SAnna-Maria Gleixner 	}
500b215e283SDavide Libenzi 
5014d672e7aSDavide Libenzi 	/*
5024d672e7aSDavide Libenzi 	 * If the timer is expired and it's periodic, we need to advance it
5034d672e7aSDavide Libenzi 	 * because the caller may want to know the previous expiration time.
5044d672e7aSDavide Libenzi 	 * We do not update "ticks" and "expired" since the timer will be
5054d672e7aSDavide Libenzi 	 * re-programmed again in the following timerfd_setup() call.
5064d672e7aSDavide Libenzi 	 */
5074d672e7aSDavide Libenzi 	if (ctx->expired && ctx->tintv) {
5082456e855SThomas Gleixner 		if (isalarm(ctx))
50911ffa9d6STodd Poynor 			alarm_forward_now(&ctx->t.alarm, ctx->tintv);
51011ffa9d6STodd Poynor 		else
51111ffa9d6STodd Poynor 			hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
51211ffa9d6STodd Poynor 	}
51311ffa9d6STodd Poynor 
5144d672e7aSDavide Libenzi 	old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
515bff41203SDeepa Dinamani 	old->it_interval = ktime_to_timespec64(ctx->tintv);
516bff41203SDeepa Dinamani 
5174d672e7aSDavide Libenzi 	/*
518b215e283SDavide Libenzi 	 * Re-program the timer to the new value ...
519b215e283SDavide Libenzi 	 */
520b215e283SDavide Libenzi 	ret = timerfd_setup(ctx, flags, new);
5219d94b9e2SAl Viro 
522b215e283SDavide Libenzi 	spin_unlock_irq(&ctx->wqh.lock);
52318963c01SDavide Libenzi 	return ret;
52499ee5315SThomas Gleixner }
525b215e283SDavide Libenzi 
do_timerfd_gettime(int ufd,struct itimerspec64 * t)526b215e283SDavide Libenzi static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
527bff41203SDeepa Dinamani {
5284d672e7aSDavide Libenzi 	struct timerfd_ctx *ctx;
5294d672e7aSDavide Libenzi 	CLASS(fd, f)(ufd);
530919a7a1aSAl Viro 
531919a7a1aSAl Viro 	if (fd_empty(f))
532919a7a1aSAl Viro 		return -EBADF;
533919a7a1aSAl Viro 	if (fd_file(f)->f_op != &timerfd_fops)
534919a7a1aSAl Viro 		return -EINVAL;
535919a7a1aSAl Viro 	ctx = fd_file(f)->private_data;
5361da91ea8SAl Viro 
5374d672e7aSDavide Libenzi 	spin_lock_irq(&ctx->wqh.lock);
5384d672e7aSDavide Libenzi 	if (ctx->expired && ctx->tintv) {
5392456e855SThomas Gleixner 		ctx->expired = 0;
5404d672e7aSDavide Libenzi 
54111ffa9d6STodd Poynor 		if (isalarm(ctx)) {
54211ffa9d6STodd Poynor 			ctx->ticks +=
5434d672e7aSDavide Libenzi 				alarm_forward_now(
54411ffa9d6STodd Poynor 					&ctx->t.alarm, ctx->tintv) - 1;
54511ffa9d6STodd Poynor 			alarm_restart(&ctx->t.alarm);
54611ffa9d6STodd Poynor 		} else {
54711ffa9d6STodd Poynor 			ctx->ticks +=
54811ffa9d6STodd Poynor 				hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
54911ffa9d6STodd Poynor 				- 1;
55011ffa9d6STodd Poynor 			hrtimer_restart(&ctx->t.tmr);
55111ffa9d6STodd Poynor 		}
55211ffa9d6STodd Poynor 	}
5534d672e7aSDavide Libenzi 	t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
554bff41203SDeepa Dinamani 	t->it_interval = ktime_to_timespec64(ctx->tintv);
555bff41203SDeepa Dinamani 	spin_unlock_irq(&ctx->wqh.lock);
5564d672e7aSDavide Libenzi 	return 0;
5579d94b9e2SAl Viro }
5589d94b9e2SAl Viro 
SYSCALL_DEFINE4(timerfd_settime,int,ufd,int,flags,const struct __kernel_itimerspec __user *,utmr,struct __kernel_itimerspec __user *,otmr)5594d672e7aSDavide Libenzi SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
5609d94b9e2SAl Viro 		const struct __kernel_itimerspec __user *, utmr,
5616ff84735SDeepa Dinamani 		struct __kernel_itimerspec __user *, otmr)
5626ff84735SDeepa Dinamani {
5639d94b9e2SAl Viro 	struct itimerspec64 new, old;
564bff41203SDeepa Dinamani 	int ret;
5659d94b9e2SAl Viro 
5669d94b9e2SAl Viro 	if (get_itimerspec64(&new, utmr))
567bff41203SDeepa Dinamani 		return -EFAULT;
5689d94b9e2SAl Viro 	ret = do_timerfd_settime(ufd, flags, &new, &old);
5699d94b9e2SAl Viro 	if (ret)
5709d94b9e2SAl Viro 		return ret;
5719d94b9e2SAl Viro 	if (otmr && put_itimerspec64(&old, otmr))
572bff41203SDeepa Dinamani 		return -EFAULT;
5739d94b9e2SAl Viro 
5749d94b9e2SAl Viro 	return ret;
5759d94b9e2SAl Viro }
5769d94b9e2SAl Viro 
SYSCALL_DEFINE2(timerfd_gettime,int,ufd,struct __kernel_itimerspec __user *,otmr)5779d94b9e2SAl Viro SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
5786ff84735SDeepa Dinamani {
5799d94b9e2SAl Viro 	struct itimerspec64 kotmr;
580bff41203SDeepa Dinamani 	int ret = do_timerfd_gettime(ufd, &kotmr);
5819d94b9e2SAl Viro 	if (ret)
5829d94b9e2SAl Viro 		return ret;
5839d94b9e2SAl Viro 	return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
584bff41203SDeepa Dinamani }
585b215e283SDavide Libenzi 
586b215e283SDavide Libenzi #ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE4(timerfd_settime32,int,ufd,int,flags,const struct old_itimerspec32 __user *,utmr,struct old_itimerspec32 __user *,otmr)5876ff84735SDeepa Dinamani SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
5888dabe724SArnd Bergmann 		const struct old_itimerspec32 __user *, utmr,
5899afc5eeeSArnd Bergmann 		struct old_itimerspec32 __user *, otmr)
5909afc5eeeSArnd Bergmann {
5919d94b9e2SAl Viro 	struct itimerspec64 new, old;
592bff41203SDeepa Dinamani 	int ret;
5939d94b9e2SAl Viro 
5949d94b9e2SAl Viro 	if (get_old_itimerspec32(&new, utmr))
5959afc5eeeSArnd Bergmann 		return -EFAULT;
5969d94b9e2SAl Viro 	ret = do_timerfd_settime(ufd, flags, &new, &old);
5979d94b9e2SAl Viro 	if (ret)
5989d94b9e2SAl Viro 		return ret;
5999d94b9e2SAl Viro 	if (otmr && put_old_itimerspec32(&old, otmr))
6009afc5eeeSArnd Bergmann 		return -EFAULT;
6019d94b9e2SAl Viro 	return ret;
6029d94b9e2SAl Viro }
6039d94b9e2SAl Viro 
SYSCALL_DEFINE2(timerfd_gettime32,int,ufd,struct old_itimerspec32 __user *,otmr)6049d94b9e2SAl Viro SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
6058dabe724SArnd Bergmann 		struct old_itimerspec32 __user *, otmr)
6069afc5eeeSArnd Bergmann {
6079d94b9e2SAl Viro 	struct itimerspec64 kotmr;
608bff41203SDeepa Dinamani 	int ret = do_timerfd_gettime(ufd, &kotmr);
6099d94b9e2SAl Viro 	if (ret)
6109d94b9e2SAl Viro 		return ret;
6119d94b9e2SAl Viro 	return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;
6129afc5eeeSArnd Bergmann }
6139d94b9e2SAl Viro #endif
6149d94b9e2SAl Viro