xref: /openbmc/linux/fs/timerfd.c (revision 66f7b0c8)
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 <davidel@xmailserver.org>
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)8211ffa9d6STodd Poynor static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
8311ffa9d6STodd Poynor 	ktime_t now)
8411ffa9d6STodd Poynor {
8511ffa9d6STodd Poynor 	struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
8611ffa9d6STodd Poynor 					       t.alarm);
8711ffa9d6STodd Poynor 	timerfd_triggered(ctx);
8811ffa9d6STodd Poynor 	return ALARMTIMER_NORESTART;
8911ffa9d6STodd Poynor }
9011ffa9d6STodd Poynor 
919ec26907SThomas Gleixner /*
929ec26907SThomas Gleixner  * Called when the clock was set to cancel the timers in the cancel
931123d939SMax Asbock  * list. This will wake up processes waiting on these timers. The
941123d939SMax Asbock  * wake-up requires ctx->ticks to be non zero, therefore we increment
951123d939SMax Asbock  * it before calling wake_up_locked().
969ec26907SThomas Gleixner  */
timerfd_clock_was_set(void)979ec26907SThomas Gleixner void timerfd_clock_was_set(void)
989ec26907SThomas Gleixner {
992456e855SThomas Gleixner 	ktime_t moffs = ktime_mono_to_real(0);
1009ec26907SThomas Gleixner 	struct timerfd_ctx *ctx;
1019ec26907SThomas Gleixner 	unsigned long flags;
1029ec26907SThomas Gleixner 
1039ec26907SThomas Gleixner 	rcu_read_lock();
1049ec26907SThomas Gleixner 	list_for_each_entry_rcu(ctx, &cancel_list, clist) {
1059ec26907SThomas Gleixner 		if (!ctx->might_cancel)
1069ec26907SThomas Gleixner 			continue;
1079ec26907SThomas Gleixner 		spin_lock_irqsave(&ctx->wqh.lock, flags);
1082456e855SThomas Gleixner 		if (ctx->moffs != moffs) {
1092456e855SThomas Gleixner 			ctx->moffs = KTIME_MAX;
1101123d939SMax Asbock 			ctx->ticks++;
1117dda7128SChristoph Hellwig 			wake_up_locked_poll(&ctx->wqh, EPOLLIN);
1129ec26907SThomas Gleixner 		}
1139ec26907SThomas Gleixner 		spin_unlock_irqrestore(&ctx->wqh.lock, flags);
1149ec26907SThomas Gleixner 	}
1159ec26907SThomas Gleixner 	rcu_read_unlock();
1169ec26907SThomas Gleixner }
1179ec26907SThomas Gleixner 
timerfd_resume_work(struct work_struct * work)118*66f7b0c8SThomas Gleixner static void timerfd_resume_work(struct work_struct *work)
119*66f7b0c8SThomas Gleixner {
120*66f7b0c8SThomas Gleixner 	timerfd_clock_was_set();
121*66f7b0c8SThomas Gleixner }
122*66f7b0c8SThomas Gleixner 
123*66f7b0c8SThomas Gleixner static DECLARE_WORK(timerfd_work, timerfd_resume_work);
124*66f7b0c8SThomas Gleixner 
125*66f7b0c8SThomas Gleixner /*
126*66f7b0c8SThomas Gleixner  * Invoked from timekeeping_resume(). Defer the actual update to work so
127*66f7b0c8SThomas Gleixner  * timerfd_clock_was_set() runs in task context.
128*66f7b0c8SThomas Gleixner  */
timerfd_resume(void)129*66f7b0c8SThomas Gleixner void timerfd_resume(void)
130*66f7b0c8SThomas Gleixner {
131*66f7b0c8SThomas Gleixner 	schedule_work(&timerfd_work);
132*66f7b0c8SThomas Gleixner }
133*66f7b0c8SThomas Gleixner 
__timerfd_remove_cancel(struct timerfd_ctx * ctx)1341e38da30SThomas Gleixner static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
1359ec26907SThomas Gleixner {
1369ec26907SThomas Gleixner 	if (ctx->might_cancel) {
1379ec26907SThomas Gleixner 		ctx->might_cancel = false;
1389ec26907SThomas Gleixner 		spin_lock(&cancel_lock);
1399ec26907SThomas Gleixner 		list_del_rcu(&ctx->clist);
1409ec26907SThomas Gleixner 		spin_unlock(&cancel_lock);
1419ec26907SThomas Gleixner 	}
1429ec26907SThomas Gleixner }
1439ec26907SThomas Gleixner 
timerfd_remove_cancel(struct timerfd_ctx * ctx)1441e38da30SThomas Gleixner static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
1451e38da30SThomas Gleixner {
1461e38da30SThomas Gleixner 	spin_lock(&ctx->cancel_lock);
1471e38da30SThomas Gleixner 	__timerfd_remove_cancel(ctx);
1481e38da30SThomas Gleixner 	spin_unlock(&ctx->cancel_lock);
1491e38da30SThomas Gleixner }
1501e38da30SThomas Gleixner 
timerfd_canceled(struct timerfd_ctx * ctx)1519ec26907SThomas Gleixner static bool timerfd_canceled(struct timerfd_ctx *ctx)
1529ec26907SThomas Gleixner {
1532456e855SThomas Gleixner 	if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
1549ec26907SThomas Gleixner 		return false;
1552456e855SThomas Gleixner 	ctx->moffs = ktime_mono_to_real(0);
1569ec26907SThomas Gleixner 	return true;
1579ec26907SThomas Gleixner }
1589ec26907SThomas Gleixner 
timerfd_setup_cancel(struct timerfd_ctx * ctx,int flags)1599ec26907SThomas Gleixner static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
1609ec26907SThomas Gleixner {
1611e38da30SThomas Gleixner 	spin_lock(&ctx->cancel_lock);
16211ffa9d6STodd Poynor 	if ((ctx->clockid == CLOCK_REALTIME ||
16311ffa9d6STodd Poynor 	     ctx->clockid == CLOCK_REALTIME_ALARM) &&
16411ffa9d6STodd Poynor 	    (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
1659ec26907SThomas Gleixner 		if (!ctx->might_cancel) {
1669ec26907SThomas Gleixner 			ctx->might_cancel = true;
1679ec26907SThomas Gleixner 			spin_lock(&cancel_lock);
1689ec26907SThomas Gleixner 			list_add_rcu(&ctx->clist, &cancel_list);
1699ec26907SThomas Gleixner 			spin_unlock(&cancel_lock);
1709ec26907SThomas Gleixner 		}
1711e38da30SThomas Gleixner 	} else {
1721e38da30SThomas Gleixner 		__timerfd_remove_cancel(ctx);
1739ec26907SThomas Gleixner 	}
1741e38da30SThomas Gleixner 	spin_unlock(&ctx->cancel_lock);
1759ec26907SThomas Gleixner }
1769ec26907SThomas Gleixner 
timerfd_get_remaining(struct timerfd_ctx * ctx)1774d672e7aSDavide Libenzi static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
1784d672e7aSDavide Libenzi {
17976369470SArjan van de Ven 	ktime_t remaining;
1804d672e7aSDavide Libenzi 
18111ffa9d6STodd Poynor 	if (isalarm(ctx))
18211ffa9d6STodd Poynor 		remaining = alarm_expires_remaining(&ctx->t.alarm);
18311ffa9d6STodd Poynor 	else
184b62526edSThomas Gleixner 		remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
18511ffa9d6STodd Poynor 
1868b0e1953SThomas Gleixner 	return remaining < 0 ? 0: remaining;
1874d672e7aSDavide Libenzi }
1884d672e7aSDavide Libenzi 
timerfd_setup(struct timerfd_ctx * ctx,int flags,const struct itimerspec64 * ktmr)18999ee5315SThomas Gleixner static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
190bff41203SDeepa Dinamani 			 const struct itimerspec64 *ktmr)
191b215e283SDavide Libenzi {
192b215e283SDavide Libenzi 	enum hrtimer_mode htmode;
193b215e283SDavide Libenzi 	ktime_t texp;
19499ee5315SThomas Gleixner 	int clockid = ctx->clockid;
195b215e283SDavide Libenzi 
196b215e283SDavide Libenzi 	htmode = (flags & TFD_TIMER_ABSTIME) ?
197b215e283SDavide Libenzi 		HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
198b215e283SDavide Libenzi 
199bff41203SDeepa Dinamani 	texp = timespec64_to_ktime(ktmr->it_value);
200b215e283SDavide Libenzi 	ctx->expired = 0;
2014d672e7aSDavide Libenzi 	ctx->ticks = 0;
202bff41203SDeepa Dinamani 	ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
20311ffa9d6STodd Poynor 
20411ffa9d6STodd Poynor 	if (isalarm(ctx)) {
20511ffa9d6STodd Poynor 		alarm_init(&ctx->t.alarm,
20611ffa9d6STodd Poynor 			   ctx->clockid == CLOCK_REALTIME_ALARM ?
20711ffa9d6STodd Poynor 			   ALARM_REALTIME : ALARM_BOOTTIME,
20811ffa9d6STodd Poynor 			   timerfd_alarmproc);
20911ffa9d6STodd Poynor 	} else {
21011ffa9d6STodd Poynor 		hrtimer_init(&ctx->t.tmr, clockid, htmode);
21111ffa9d6STodd Poynor 		hrtimer_set_expires(&ctx->t.tmr, texp);
21211ffa9d6STodd Poynor 		ctx->t.tmr.function = timerfd_tmrproc;
21311ffa9d6STodd Poynor 	}
21411ffa9d6STodd Poynor 
2152456e855SThomas Gleixner 	if (texp != 0) {
2166cd889d4SAndrei Vagin 		if (flags & TFD_TIMER_ABSTIME)
2176cd889d4SAndrei Vagin 			texp = timens_ktime_to_host(clockid, texp);
21811ffa9d6STodd Poynor 		if (isalarm(ctx)) {
21911ffa9d6STodd Poynor 			if (flags & TFD_TIMER_ABSTIME)
22011ffa9d6STodd Poynor 				alarm_start(&ctx->t.alarm, texp);
22111ffa9d6STodd Poynor 			else
22211ffa9d6STodd Poynor 				alarm_start_relative(&ctx->t.alarm, texp);
22311ffa9d6STodd Poynor 		} else {
22411ffa9d6STodd Poynor 			hrtimer_start(&ctx->t.tmr, texp, htmode);
22511ffa9d6STodd Poynor 		}
22611ffa9d6STodd Poynor 
22799ee5315SThomas Gleixner 		if (timerfd_canceled(ctx))
22899ee5315SThomas Gleixner 			return -ECANCELED;
22999ee5315SThomas Gleixner 	}
230af9c4957SCyrill Gorcunov 
231af9c4957SCyrill Gorcunov 	ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
23299ee5315SThomas Gleixner 	return 0;
233b215e283SDavide Libenzi }
234b215e283SDavide Libenzi 
timerfd_release(struct inode * inode,struct file * file)235b215e283SDavide Libenzi static int timerfd_release(struct inode *inode, struct file *file)
236b215e283SDavide Libenzi {
237b215e283SDavide Libenzi 	struct timerfd_ctx *ctx = file->private_data;
238b215e283SDavide Libenzi 
2399ec26907SThomas Gleixner 	timerfd_remove_cancel(ctx);
24011ffa9d6STodd Poynor 
24111ffa9d6STodd Poynor 	if (isalarm(ctx))
24211ffa9d6STodd Poynor 		alarm_cancel(&ctx->t.alarm);
24311ffa9d6STodd Poynor 	else
24411ffa9d6STodd Poynor 		hrtimer_cancel(&ctx->t.tmr);
2459ec26907SThomas Gleixner 	kfree_rcu(ctx, rcu);
246b215e283SDavide Libenzi 	return 0;
247b215e283SDavide Libenzi }
248b215e283SDavide Libenzi 
timerfd_poll(struct file * file,poll_table * wait)249a11e1d43SLinus Torvalds static __poll_t timerfd_poll(struct file *file, poll_table *wait)
250b215e283SDavide Libenzi {
251b215e283SDavide Libenzi 	struct timerfd_ctx *ctx = file->private_data;
252a11e1d43SLinus Torvalds 	__poll_t events = 0;
253a11e1d43SLinus Torvalds 	unsigned long flags;
254b215e283SDavide Libenzi 
255a11e1d43SLinus Torvalds 	poll_wait(file, &ctx->wqh, wait);
256b215e283SDavide Libenzi 
257a11e1d43SLinus Torvalds 	spin_lock_irqsave(&ctx->wqh.lock, flags);
258a11e1d43SLinus Torvalds 	if (ctx->ticks)
259a11e1d43SLinus Torvalds 		events |= EPOLLIN;
260a11e1d43SLinus Torvalds 	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
261b215e283SDavide Libenzi 
262a11e1d43SLinus Torvalds 	return events;
263b215e283SDavide Libenzi }
264b215e283SDavide Libenzi 
timerfd_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)265b215e283SDavide Libenzi static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
266b215e283SDavide Libenzi 			    loff_t *ppos)
267b215e283SDavide Libenzi {
268b215e283SDavide Libenzi 	struct timerfd_ctx *ctx = file->private_data;
269b215e283SDavide Libenzi 	ssize_t res;
27009828402SDavide Libenzi 	u64 ticks = 0;
271b215e283SDavide Libenzi 
272b215e283SDavide Libenzi 	if (count < sizeof(ticks))
273b215e283SDavide Libenzi 		return -EINVAL;
27418963c01SDavide Libenzi 	spin_lock_irq(&ctx->wqh.lock);
2758120a8aaSMichal Nazarewicz 	if (file->f_flags & O_NONBLOCK)
276b215e283SDavide Libenzi 		res = -EAGAIN;
2778120a8aaSMichal Nazarewicz 	else
2788120a8aaSMichal Nazarewicz 		res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
27999ee5315SThomas Gleixner 
28099ee5315SThomas Gleixner 	/*
28199ee5315SThomas Gleixner 	 * If clock has changed, we do not care about the
28299ee5315SThomas Gleixner 	 * ticks and we do not rearm the timer. Userspace must
28399ee5315SThomas Gleixner 	 * reevaluate anyway.
28499ee5315SThomas Gleixner 	 */
28599ee5315SThomas Gleixner 	if (timerfd_canceled(ctx)) {
2869ec26907SThomas Gleixner 		ctx->ticks = 0;
28799ee5315SThomas Gleixner 		ctx->expired = 0;
28899ee5315SThomas Gleixner 		res = -ECANCELED;
28999ee5315SThomas Gleixner 	}
29099ee5315SThomas Gleixner 
2919ec26907SThomas Gleixner 	if (ctx->ticks) {
2929ec26907SThomas Gleixner 		ticks = ctx->ticks;
2939ec26907SThomas Gleixner 
2942456e855SThomas Gleixner 		if (ctx->expired && ctx->tintv) {
295b215e283SDavide Libenzi 			/*
2962456e855SThomas Gleixner 			 * If tintv != 0, this is a periodic timer that
297b215e283SDavide Libenzi 			 * needs to be re-armed. We avoid doing it in the timer
298b215e283SDavide Libenzi 			 * callback to avoid DoS attacks specifying a very
299b215e283SDavide Libenzi 			 * short timer period.
300b215e283SDavide Libenzi 			 */
30111ffa9d6STodd Poynor 			if (isalarm(ctx)) {
30211ffa9d6STodd Poynor 				ticks += alarm_forward_now(
30311ffa9d6STodd Poynor 					&ctx->t.alarm, ctx->tintv) - 1;
30411ffa9d6STodd Poynor 				alarm_restart(&ctx->t.alarm);
30511ffa9d6STodd Poynor 			} else {
30611ffa9d6STodd Poynor 				ticks += hrtimer_forward_now(&ctx->t.tmr,
3074d672e7aSDavide Libenzi 							     ctx->tintv) - 1;
30811ffa9d6STodd Poynor 				hrtimer_restart(&ctx->t.tmr);
30911ffa9d6STodd Poynor 			}
3104d672e7aSDavide Libenzi 		}
3114d672e7aSDavide Libenzi 		ctx->expired = 0;
3124d672e7aSDavide Libenzi 		ctx->ticks = 0;
313b215e283SDavide Libenzi 	}
31418963c01SDavide Libenzi 	spin_unlock_irq(&ctx->wqh.lock);
315b215e283SDavide Libenzi 	if (ticks)
31609828402SDavide Libenzi 		res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
317b215e283SDavide Libenzi 	return res;
318b215e283SDavide Libenzi }
319b215e283SDavide Libenzi 
320af9c4957SCyrill Gorcunov #ifdef CONFIG_PROC_FS
timerfd_show(struct seq_file * m,struct file * file)321a3816ab0SJoe Perches static void timerfd_show(struct seq_file *m, struct file *file)
322af9c4957SCyrill Gorcunov {
323af9c4957SCyrill Gorcunov 	struct timerfd_ctx *ctx = file->private_data;
324bde9e963SArnd Bergmann 	struct timespec64 value, interval;
325af9c4957SCyrill Gorcunov 
326af9c4957SCyrill Gorcunov 	spin_lock_irq(&ctx->wqh.lock);
327bde9e963SArnd Bergmann 	value = ktime_to_timespec64(timerfd_get_remaining(ctx));
328bde9e963SArnd Bergmann 	interval = ktime_to_timespec64(ctx->tintv);
329af9c4957SCyrill Gorcunov 	spin_unlock_irq(&ctx->wqh.lock);
330af9c4957SCyrill Gorcunov 
331a3816ab0SJoe Perches 	seq_printf(m,
332af9c4957SCyrill Gorcunov 		   "clockid: %d\n"
333af9c4957SCyrill Gorcunov 		   "ticks: %llu\n"
334af9c4957SCyrill Gorcunov 		   "settime flags: 0%o\n"
335af9c4957SCyrill Gorcunov 		   "it_value: (%llu, %llu)\n"
336af9c4957SCyrill Gorcunov 		   "it_interval: (%llu, %llu)\n",
337a3816ab0SJoe Perches 		   ctx->clockid,
338a3816ab0SJoe Perches 		   (unsigned long long)ctx->ticks,
339af9c4957SCyrill Gorcunov 		   ctx->settime_flags,
340bde9e963SArnd Bergmann 		   (unsigned long long)value.tv_sec,
341bde9e963SArnd Bergmann 		   (unsigned long long)value.tv_nsec,
342bde9e963SArnd Bergmann 		   (unsigned long long)interval.tv_sec,
343bde9e963SArnd Bergmann 		   (unsigned long long)interval.tv_nsec);
344af9c4957SCyrill Gorcunov }
345af9c4957SCyrill Gorcunov #else
346af9c4957SCyrill Gorcunov #define timerfd_show NULL
347af9c4957SCyrill Gorcunov #endif
348af9c4957SCyrill Gorcunov 
3495442e9fbSCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
timerfd_ioctl(struct file * file,unsigned int cmd,unsigned long arg)3505442e9fbSCyrill Gorcunov static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3515442e9fbSCyrill Gorcunov {
3525442e9fbSCyrill Gorcunov 	struct timerfd_ctx *ctx = file->private_data;
3535442e9fbSCyrill Gorcunov 	int ret = 0;
3545442e9fbSCyrill Gorcunov 
3555442e9fbSCyrill Gorcunov 	switch (cmd) {
3565442e9fbSCyrill Gorcunov 	case TFD_IOC_SET_TICKS: {
3575442e9fbSCyrill Gorcunov 		u64 ticks;
3585442e9fbSCyrill Gorcunov 
3595442e9fbSCyrill Gorcunov 		if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
3605442e9fbSCyrill Gorcunov 			return -EFAULT;
3615442e9fbSCyrill Gorcunov 		if (!ticks)
3625442e9fbSCyrill Gorcunov 			return -EINVAL;
3635442e9fbSCyrill Gorcunov 
3645442e9fbSCyrill Gorcunov 		spin_lock_irq(&ctx->wqh.lock);
3655442e9fbSCyrill Gorcunov 		if (!timerfd_canceled(ctx)) {
3665442e9fbSCyrill Gorcunov 			ctx->ticks = ticks;
3677dda7128SChristoph Hellwig 			wake_up_locked_poll(&ctx->wqh, EPOLLIN);
3685442e9fbSCyrill Gorcunov 		} else
3695442e9fbSCyrill Gorcunov 			ret = -ECANCELED;
3705442e9fbSCyrill Gorcunov 		spin_unlock_irq(&ctx->wqh.lock);
3715442e9fbSCyrill Gorcunov 		break;
3725442e9fbSCyrill Gorcunov 	}
3735442e9fbSCyrill Gorcunov 	default:
3745442e9fbSCyrill Gorcunov 		ret = -ENOTTY;
3755442e9fbSCyrill Gorcunov 		break;
3765442e9fbSCyrill Gorcunov 	}
3775442e9fbSCyrill Gorcunov 
3785442e9fbSCyrill Gorcunov 	return ret;
3795442e9fbSCyrill Gorcunov }
3805442e9fbSCyrill Gorcunov #else
3815442e9fbSCyrill Gorcunov #define timerfd_ioctl NULL
3825442e9fbSCyrill Gorcunov #endif
3835442e9fbSCyrill Gorcunov 
384b215e283SDavide Libenzi static const struct file_operations timerfd_fops = {
385b215e283SDavide Libenzi 	.release	= timerfd_release,
386a11e1d43SLinus Torvalds 	.poll		= timerfd_poll,
387b215e283SDavide Libenzi 	.read		= timerfd_read,
3886038f373SArnd Bergmann 	.llseek		= noop_llseek,
389af9c4957SCyrill Gorcunov 	.show_fdinfo	= timerfd_show,
3905442e9fbSCyrill Gorcunov 	.unlocked_ioctl	= timerfd_ioctl,
391b215e283SDavide Libenzi };
392b215e283SDavide Libenzi 
timerfd_fget(int fd,struct fd * p)3932903ff01SAl Viro static int timerfd_fget(int fd, struct fd *p)
394b215e283SDavide Libenzi {
3952903ff01SAl Viro 	struct fd f = fdget(fd);
3962903ff01SAl Viro 	if (!f.file)
3972903ff01SAl Viro 		return -EBADF;
3982903ff01SAl Viro 	if (f.file->f_op != &timerfd_fops) {
3992903ff01SAl Viro 		fdput(f);
4002903ff01SAl Viro 		return -EINVAL;
4014d672e7aSDavide Libenzi 	}
4022903ff01SAl Viro 	*p = f;
4032903ff01SAl Viro 	return 0;
4044d672e7aSDavide Libenzi }
4054d672e7aSDavide Libenzi 
SYSCALL_DEFINE2(timerfd_create,int,clockid,int,flags)406836f92adSHeiko Carstens SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
4074d672e7aSDavide Libenzi {
4082030a42cSAl Viro 	int ufd;
409b215e283SDavide Libenzi 	struct timerfd_ctx *ctx;
410b215e283SDavide Libenzi 
411e38b36f3SUlrich Drepper 	/* Check the TFD_* constants for consistency.  */
412e38b36f3SUlrich Drepper 	BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
413e38b36f3SUlrich Drepper 	BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
414e38b36f3SUlrich Drepper 
415610d18f4SDavide Libenzi 	if ((flags & ~TFD_CREATE_FLAGS) ||
416610d18f4SDavide Libenzi 	    (clockid != CLOCK_MONOTONIC &&
41711ffa9d6STodd Poynor 	     clockid != CLOCK_REALTIME &&
41811ffa9d6STodd Poynor 	     clockid != CLOCK_REALTIME_ALARM &&
4194a2378a9SGreg Hackmann 	     clockid != CLOCK_BOOTTIME &&
42011ffa9d6STodd Poynor 	     clockid != CLOCK_BOOTTIME_ALARM))
421b215e283SDavide Libenzi 		return -EINVAL;
422b215e283SDavide Libenzi 
42325b68a8fSStephen Smalley 	if ((clockid == CLOCK_REALTIME_ALARM ||
42425b68a8fSStephen Smalley 	     clockid == CLOCK_BOOTTIME_ALARM) &&
42525b68a8fSStephen Smalley 	    !capable(CAP_WAKE_ALARM))
4262895a5e5SEric Caruso 		return -EPERM;
4272895a5e5SEric Caruso 
4284d672e7aSDavide Libenzi 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
429b215e283SDavide Libenzi 	if (!ctx)
430b215e283SDavide Libenzi 		return -ENOMEM;
431b215e283SDavide Libenzi 
432b215e283SDavide Libenzi 	init_waitqueue_head(&ctx->wqh);
4331e38da30SThomas Gleixner 	spin_lock_init(&ctx->cancel_lock);
4344d672e7aSDavide Libenzi 	ctx->clockid = clockid;
43511ffa9d6STodd Poynor 
43611ffa9d6STodd Poynor 	if (isalarm(ctx))
43711ffa9d6STodd Poynor 		alarm_init(&ctx->t.alarm,
43811ffa9d6STodd Poynor 			   ctx->clockid == CLOCK_REALTIME_ALARM ?
43911ffa9d6STodd Poynor 			   ALARM_REALTIME : ALARM_BOOTTIME,
44011ffa9d6STodd Poynor 			   timerfd_alarmproc);
44111ffa9d6STodd Poynor 	else
44211ffa9d6STodd Poynor 		hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
44311ffa9d6STodd Poynor 
4442456e855SThomas Gleixner 	ctx->moffs = ktime_mono_to_real(0);
445b215e283SDavide Libenzi 
44611fcb6c1SUlrich Drepper 	ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
447628ff7c1SRoland Dreier 			       O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
4482030a42cSAl Viro 	if (ufd < 0)
4494d672e7aSDavide Libenzi 		kfree(ctx);
4504d672e7aSDavide Libenzi 
4514d672e7aSDavide Libenzi 	return ufd;
4524d672e7aSDavide Libenzi }
4534d672e7aSDavide Libenzi 
do_timerfd_settime(int ufd,int flags,const struct itimerspec64 * new,struct itimerspec64 * old)4549d94b9e2SAl Viro static int do_timerfd_settime(int ufd, int flags,
455bff41203SDeepa Dinamani 		const struct itimerspec64 *new,
456bff41203SDeepa Dinamani 		struct itimerspec64 *old)
4574d672e7aSDavide Libenzi {
4582903ff01SAl Viro 	struct fd f;
4594d672e7aSDavide Libenzi 	struct timerfd_ctx *ctx;
4602903ff01SAl Viro 	int ret;
4614d672e7aSDavide Libenzi 
462610d18f4SDavide Libenzi 	if ((flags & ~TFD_SETTIME_FLAGS) ||
463bff41203SDeepa Dinamani 		 !itimerspec64_valid(new))
4644d672e7aSDavide Libenzi 		return -EINVAL;
4654d672e7aSDavide Libenzi 
4662903ff01SAl Viro 	ret = timerfd_fget(ufd, &f);
4672903ff01SAl Viro 	if (ret)
4682903ff01SAl Viro 		return ret;
4692903ff01SAl Viro 	ctx = f.file->private_data;
4704d672e7aSDavide Libenzi 
47125b68a8fSStephen Smalley 	if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
4722895a5e5SEric Caruso 		fdput(f);
4732895a5e5SEric Caruso 		return -EPERM;
4742895a5e5SEric Caruso 	}
4752895a5e5SEric Caruso 
4769ec26907SThomas Gleixner 	timerfd_setup_cancel(ctx, flags);
4779ec26907SThomas Gleixner 
478b215e283SDavide Libenzi 	/*
479b215e283SDavide Libenzi 	 * We need to stop the existing timer before reprogramming
480b215e283SDavide Libenzi 	 * it to the new values.
481b215e283SDavide Libenzi 	 */
482b215e283SDavide Libenzi 	for (;;) {
48318963c01SDavide Libenzi 		spin_lock_irq(&ctx->wqh.lock);
48411ffa9d6STodd Poynor 
48511ffa9d6STodd Poynor 		if (isalarm(ctx)) {
48611ffa9d6STodd Poynor 			if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
487b215e283SDavide Libenzi 				break;
48811ffa9d6STodd Poynor 		} else {
48911ffa9d6STodd Poynor 			if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
49011ffa9d6STodd Poynor 				break;
49111ffa9d6STodd Poynor 		}
49218963c01SDavide Libenzi 		spin_unlock_irq(&ctx->wqh.lock);
493a125ecc1SAnna-Maria Gleixner 
494a125ecc1SAnna-Maria Gleixner 		if (isalarm(ctx))
495a125ecc1SAnna-Maria Gleixner 			hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
496a125ecc1SAnna-Maria Gleixner 		else
497a125ecc1SAnna-Maria Gleixner 			hrtimer_cancel_wait_running(&ctx->t.tmr);
498b215e283SDavide Libenzi 	}
4994d672e7aSDavide Libenzi 
5004d672e7aSDavide Libenzi 	/*
5014d672e7aSDavide Libenzi 	 * If the timer is expired and it's periodic, we need to advance it
5024d672e7aSDavide Libenzi 	 * because the caller may want to know the previous expiration time.
5034d672e7aSDavide Libenzi 	 * We do not update "ticks" and "expired" since the timer will be
5044d672e7aSDavide Libenzi 	 * re-programmed again in the following timerfd_setup() call.
5054d672e7aSDavide Libenzi 	 */
5062456e855SThomas Gleixner 	if (ctx->expired && ctx->tintv) {
50711ffa9d6STodd Poynor 		if (isalarm(ctx))
50811ffa9d6STodd Poynor 			alarm_forward_now(&ctx->t.alarm, ctx->tintv);
50911ffa9d6STodd Poynor 		else
51011ffa9d6STodd Poynor 			hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
51111ffa9d6STodd Poynor 	}
5124d672e7aSDavide Libenzi 
513bff41203SDeepa Dinamani 	old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
514bff41203SDeepa Dinamani 	old->it_interval = ktime_to_timespec64(ctx->tintv);
5154d672e7aSDavide Libenzi 
516b215e283SDavide Libenzi 	/*
517b215e283SDavide Libenzi 	 * Re-program the timer to the new value ...
518b215e283SDavide Libenzi 	 */
5199d94b9e2SAl Viro 	ret = timerfd_setup(ctx, flags, new);
520b215e283SDavide Libenzi 
52118963c01SDavide Libenzi 	spin_unlock_irq(&ctx->wqh.lock);
5222903ff01SAl Viro 	fdput(f);
52399ee5315SThomas Gleixner 	return ret;
524b215e283SDavide Libenzi }
525b215e283SDavide Libenzi 
do_timerfd_gettime(int ufd,struct itimerspec64 * t)526bff41203SDeepa Dinamani static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
5274d672e7aSDavide Libenzi {
5282903ff01SAl Viro 	struct fd f;
5294d672e7aSDavide Libenzi 	struct timerfd_ctx *ctx;
5302903ff01SAl Viro 	int ret = timerfd_fget(ufd, &f);
5312903ff01SAl Viro 	if (ret)
5322903ff01SAl Viro 		return ret;
5332903ff01SAl Viro 	ctx = f.file->private_data;
5344d672e7aSDavide Libenzi 
5354d672e7aSDavide Libenzi 	spin_lock_irq(&ctx->wqh.lock);
5362456e855SThomas Gleixner 	if (ctx->expired && ctx->tintv) {
5374d672e7aSDavide Libenzi 		ctx->expired = 0;
53811ffa9d6STodd Poynor 
53911ffa9d6STodd Poynor 		if (isalarm(ctx)) {
5404d672e7aSDavide Libenzi 			ctx->ticks +=
54111ffa9d6STodd Poynor 				alarm_forward_now(
54211ffa9d6STodd Poynor 					&ctx->t.alarm, ctx->tintv) - 1;
54311ffa9d6STodd Poynor 			alarm_restart(&ctx->t.alarm);
54411ffa9d6STodd Poynor 		} else {
54511ffa9d6STodd Poynor 			ctx->ticks +=
54611ffa9d6STodd Poynor 				hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
54711ffa9d6STodd Poynor 				- 1;
54811ffa9d6STodd Poynor 			hrtimer_restart(&ctx->t.tmr);
54911ffa9d6STodd Poynor 		}
5504d672e7aSDavide Libenzi 	}
551bff41203SDeepa Dinamani 	t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
552bff41203SDeepa Dinamani 	t->it_interval = ktime_to_timespec64(ctx->tintv);
5534d672e7aSDavide Libenzi 	spin_unlock_irq(&ctx->wqh.lock);
5542903ff01SAl Viro 	fdput(f);
5559d94b9e2SAl Viro 	return 0;
5569d94b9e2SAl Viro }
5574d672e7aSDavide Libenzi 
SYSCALL_DEFINE4(timerfd_settime,int,ufd,int,flags,const struct __kernel_itimerspec __user *,utmr,struct __kernel_itimerspec __user *,otmr)5589d94b9e2SAl Viro SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
5596ff84735SDeepa Dinamani 		const struct __kernel_itimerspec __user *, utmr,
5606ff84735SDeepa Dinamani 		struct __kernel_itimerspec __user *, otmr)
5619d94b9e2SAl Viro {
562bff41203SDeepa Dinamani 	struct itimerspec64 new, old;
5639d94b9e2SAl Viro 	int ret;
5649d94b9e2SAl Viro 
565bff41203SDeepa Dinamani 	if (get_itimerspec64(&new, utmr))
5669d94b9e2SAl Viro 		return -EFAULT;
5679d94b9e2SAl Viro 	ret = do_timerfd_settime(ufd, flags, &new, &old);
5689d94b9e2SAl Viro 	if (ret)
5699d94b9e2SAl Viro 		return ret;
570bff41203SDeepa Dinamani 	if (otmr && put_itimerspec64(&old, otmr))
5719d94b9e2SAl Viro 		return -EFAULT;
5729d94b9e2SAl Viro 
5739d94b9e2SAl Viro 	return ret;
5749d94b9e2SAl Viro }
5759d94b9e2SAl Viro 
SYSCALL_DEFINE2(timerfd_gettime,int,ufd,struct __kernel_itimerspec __user *,otmr)5766ff84735SDeepa Dinamani SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
5779d94b9e2SAl Viro {
578bff41203SDeepa Dinamani 	struct itimerspec64 kotmr;
5799d94b9e2SAl Viro 	int ret = do_timerfd_gettime(ufd, &kotmr);
5809d94b9e2SAl Viro 	if (ret)
5819d94b9e2SAl Viro 		return ret;
582bff41203SDeepa Dinamani 	return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
583b215e283SDavide Libenzi }
584b215e283SDavide Libenzi 
5856ff84735SDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE4(timerfd_settime32,int,ufd,int,flags,const struct old_itimerspec32 __user *,utmr,struct old_itimerspec32 __user *,otmr)5868dabe724SArnd Bergmann SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
5879afc5eeeSArnd Bergmann 		const struct old_itimerspec32 __user *, utmr,
5889afc5eeeSArnd Bergmann 		struct old_itimerspec32 __user *, otmr)
5899d94b9e2SAl Viro {
590bff41203SDeepa Dinamani 	struct itimerspec64 new, old;
5919d94b9e2SAl Viro 	int ret;
5929d94b9e2SAl Viro 
5939afc5eeeSArnd Bergmann 	if (get_old_itimerspec32(&new, utmr))
5949d94b9e2SAl Viro 		return -EFAULT;
5959d94b9e2SAl Viro 	ret = do_timerfd_settime(ufd, flags, &new, &old);
5969d94b9e2SAl Viro 	if (ret)
5979d94b9e2SAl Viro 		return ret;
5989afc5eeeSArnd Bergmann 	if (otmr && put_old_itimerspec32(&old, otmr))
5999d94b9e2SAl Viro 		return -EFAULT;
6009d94b9e2SAl Viro 	return ret;
6019d94b9e2SAl Viro }
6029d94b9e2SAl Viro 
SYSCALL_DEFINE2(timerfd_gettime32,int,ufd,struct old_itimerspec32 __user *,otmr)6038dabe724SArnd Bergmann SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
6049afc5eeeSArnd Bergmann 		struct old_itimerspec32 __user *, otmr)
6059d94b9e2SAl Viro {
606bff41203SDeepa Dinamani 	struct itimerspec64 kotmr;
6079d94b9e2SAl Viro 	int ret = do_timerfd_gettime(ufd, &kotmr);
6089d94b9e2SAl Viro 	if (ret)
6099d94b9e2SAl Viro 		return ret;
6109afc5eeeSArnd Bergmann 	return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;
6119d94b9e2SAl Viro }
6129d94b9e2SAl Viro #endif
613