xref: /openbmc/linux/fs/timerfd.c (revision c21b37f6)
1 /*
2  *  fs/timerfd.c
3  *
4  *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
5  *
6  *
7  *  Thanks to Thomas Gleixner for code reviews and useful comments.
8  *
9  */
10 
11 #include <linux/file.h>
12 #include <linux/poll.h>
13 #include <linux/init.h>
14 #include <linux/fs.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/spinlock.h>
19 #include <linux/time.h>
20 #include <linux/hrtimer.h>
21 #include <linux/anon_inodes.h>
22 #include <linux/timerfd.h>
23 
24 struct timerfd_ctx {
25 	struct hrtimer tmr;
26 	ktime_t tintv;
27 	wait_queue_head_t wqh;
28 	int expired;
29 };
30 
31 /*
32  * This gets called when the timer event triggers. We set the "expired"
33  * flag, but we do not re-arm the timer (in case it's necessary,
34  * tintv.tv64 != 0) until the timer is read.
35  */
36 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
37 {
38 	struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
39 	unsigned long flags;
40 
41 	spin_lock_irqsave(&ctx->wqh.lock, flags);
42 	ctx->expired = 1;
43 	wake_up_locked(&ctx->wqh);
44 	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
45 
46 	return HRTIMER_NORESTART;
47 }
48 
49 static void timerfd_setup(struct timerfd_ctx *ctx, int clockid, int flags,
50 			  const struct itimerspec *ktmr)
51 {
52 	enum hrtimer_mode htmode;
53 	ktime_t texp;
54 
55 	htmode = (flags & TFD_TIMER_ABSTIME) ?
56 		HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
57 
58 	texp = timespec_to_ktime(ktmr->it_value);
59 	ctx->expired = 0;
60 	ctx->tintv = timespec_to_ktime(ktmr->it_interval);
61 	hrtimer_init(&ctx->tmr, clockid, htmode);
62 	ctx->tmr.expires = texp;
63 	ctx->tmr.function = timerfd_tmrproc;
64 	if (texp.tv64 != 0)
65 		hrtimer_start(&ctx->tmr, texp, htmode);
66 }
67 
68 static int timerfd_release(struct inode *inode, struct file *file)
69 {
70 	struct timerfd_ctx *ctx = file->private_data;
71 
72 	hrtimer_cancel(&ctx->tmr);
73 	kfree(ctx);
74 	return 0;
75 }
76 
77 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
78 {
79 	struct timerfd_ctx *ctx = file->private_data;
80 	unsigned int events = 0;
81 	unsigned long flags;
82 
83 	poll_wait(file, &ctx->wqh, wait);
84 
85 	spin_lock_irqsave(&ctx->wqh.lock, flags);
86 	if (ctx->expired)
87 		events |= POLLIN;
88 	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
89 
90 	return events;
91 }
92 
93 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
94 			    loff_t *ppos)
95 {
96 	struct timerfd_ctx *ctx = file->private_data;
97 	ssize_t res;
98 	u64 ticks = 0;
99 	DECLARE_WAITQUEUE(wait, current);
100 
101 	if (count < sizeof(ticks))
102 		return -EINVAL;
103 	spin_lock_irq(&ctx->wqh.lock);
104 	res = -EAGAIN;
105 	if (!ctx->expired && !(file->f_flags & O_NONBLOCK)) {
106 		__add_wait_queue(&ctx->wqh, &wait);
107 		for (res = 0;;) {
108 			set_current_state(TASK_INTERRUPTIBLE);
109 			if (ctx->expired) {
110 				res = 0;
111 				break;
112 			}
113 			if (signal_pending(current)) {
114 				res = -ERESTARTSYS;
115 				break;
116 			}
117 			spin_unlock_irq(&ctx->wqh.lock);
118 			schedule();
119 			spin_lock_irq(&ctx->wqh.lock);
120 		}
121 		__remove_wait_queue(&ctx->wqh, &wait);
122 		__set_current_state(TASK_RUNNING);
123 	}
124 	if (ctx->expired) {
125 		ctx->expired = 0;
126 		if (ctx->tintv.tv64 != 0) {
127 			/*
128 			 * If tintv.tv64 != 0, this is a periodic timer that
129 			 * needs to be re-armed. We avoid doing it in the timer
130 			 * callback to avoid DoS attacks specifying a very
131 			 * short timer period.
132 			 */
133 			ticks = (u64)
134 				hrtimer_forward(&ctx->tmr,
135 						hrtimer_cb_get_time(&ctx->tmr),
136 						ctx->tintv);
137 			hrtimer_restart(&ctx->tmr);
138 		} else
139 			ticks = 1;
140 	}
141 	spin_unlock_irq(&ctx->wqh.lock);
142 	if (ticks)
143 		res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
144 	return res;
145 }
146 
147 static const struct file_operations timerfd_fops = {
148 	.release	= timerfd_release,
149 	.poll		= timerfd_poll,
150 	.read		= timerfd_read,
151 };
152 
153 asmlinkage long sys_timerfd(int ufd, int clockid, int flags,
154 			    const struct itimerspec __user *utmr)
155 {
156 	int error;
157 	struct timerfd_ctx *ctx;
158 	struct file *file;
159 	struct inode *inode;
160 	struct itimerspec ktmr;
161 
162 	if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
163 		return -EFAULT;
164 
165 	if (clockid != CLOCK_MONOTONIC &&
166 	    clockid != CLOCK_REALTIME)
167 		return -EINVAL;
168 	if (!timespec_valid(&ktmr.it_value) ||
169 	    !timespec_valid(&ktmr.it_interval))
170 		return -EINVAL;
171 
172 	if (ufd == -1) {
173 		ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
174 		if (!ctx)
175 			return -ENOMEM;
176 
177 		init_waitqueue_head(&ctx->wqh);
178 
179 		timerfd_setup(ctx, clockid, flags, &ktmr);
180 
181 		/*
182 		 * When we call this, the initialization must be complete, since
183 		 * anon_inode_getfd() will install the fd.
184 		 */
185 		error = anon_inode_getfd(&ufd, &inode, &file, "[timerfd]",
186 					 &timerfd_fops, ctx);
187 		if (error)
188 			goto err_tmrcancel;
189 	} else {
190 		file = fget(ufd);
191 		if (!file)
192 			return -EBADF;
193 		ctx = file->private_data;
194 		if (file->f_op != &timerfd_fops) {
195 			fput(file);
196 			return -EINVAL;
197 		}
198 		/*
199 		 * We need to stop the existing timer before reprogramming
200 		 * it to the new values.
201 		 */
202 		for (;;) {
203 			spin_lock_irq(&ctx->wqh.lock);
204 			if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
205 				break;
206 			spin_unlock_irq(&ctx->wqh.lock);
207 			cpu_relax();
208 		}
209 		/*
210 		 * Re-program the timer to the new value ...
211 		 */
212 		timerfd_setup(ctx, clockid, flags, &ktmr);
213 
214 		spin_unlock_irq(&ctx->wqh.lock);
215 		fput(file);
216 	}
217 
218 	return ufd;
219 
220 err_tmrcancel:
221 	hrtimer_cancel(&ctx->tmr);
222 	kfree(ctx);
223 	return error;
224 }
225 
226