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