]>
Commit | Line | Data |
---|---|---|
e1ad7468 DL |
1 | /* |
2 | * fs/eventfd.c | |
3 | * | |
4 | * Copyright (C) 2007 Davide Libenzi <[email protected]> | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <linux/file.h> | |
9 | #include <linux/poll.h> | |
10 | #include <linux/init.h> | |
11 | #include <linux/fs.h> | |
12 | #include <linux/sched.h> | |
13 | #include <linux/kernel.h> | |
14 | #include <linux/list.h> | |
15 | #include <linux/spinlock.h> | |
16 | #include <linux/anon_inodes.h> | |
17 | #include <linux/eventfd.h> | |
7747cdb2 | 18 | #include <linux/syscalls.h> |
e1ad7468 DL |
19 | |
20 | struct eventfd_ctx { | |
e1ad7468 DL |
21 | wait_queue_head_t wqh; |
22 | /* | |
23 | * Every time that a write(2) is performed on an eventfd, the | |
24 | * value of the __u64 being written is added to "count" and a | |
25 | * wakeup is performed on "wqh". A read(2) will return the "count" | |
26 | * value to userspace, and will reset "count" to zero. The kernel | |
27 | * size eventfd_signal() also, adds to the "count" counter and | |
28 | * issue a wakeup. | |
29 | */ | |
30 | __u64 count; | |
31 | }; | |
32 | ||
33 | /* | |
34 | * Adds "n" to the eventfd counter "count". Returns "n" in case of | |
35 | * success, or a value lower then "n" in case of coutner overflow. | |
36 | * This function is supposed to be called by the kernel in paths | |
37 | * that do not allow sleeping. In this function we allow the counter | |
38 | * to reach the ULLONG_MAX value, and we signal this as overflow | |
39 | * condition by returining a POLLERR to poll(2). | |
40 | */ | |
41 | int eventfd_signal(struct file *file, int n) | |
42 | { | |
43 | struct eventfd_ctx *ctx = file->private_data; | |
44 | unsigned long flags; | |
45 | ||
46 | if (n < 0) | |
47 | return -EINVAL; | |
d48eb233 | 48 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
e1ad7468 DL |
49 | if (ULLONG_MAX - ctx->count < n) |
50 | n = (int) (ULLONG_MAX - ctx->count); | |
51 | ctx->count += n; | |
52 | if (waitqueue_active(&ctx->wqh)) | |
53 | wake_up_locked(&ctx->wqh); | |
d48eb233 | 54 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
e1ad7468 DL |
55 | |
56 | return n; | |
57 | } | |
58 | ||
59 | static int eventfd_release(struct inode *inode, struct file *file) | |
60 | { | |
61 | kfree(file->private_data); | |
62 | return 0; | |
63 | } | |
64 | ||
65 | static unsigned int eventfd_poll(struct file *file, poll_table *wait) | |
66 | { | |
67 | struct eventfd_ctx *ctx = file->private_data; | |
68 | unsigned int events = 0; | |
69 | unsigned long flags; | |
70 | ||
71 | poll_wait(file, &ctx->wqh, wait); | |
72 | ||
d48eb233 | 73 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
e1ad7468 DL |
74 | if (ctx->count > 0) |
75 | events |= POLLIN; | |
76 | if (ctx->count == ULLONG_MAX) | |
77 | events |= POLLERR; | |
78 | if (ULLONG_MAX - 1 > ctx->count) | |
79 | events |= POLLOUT; | |
d48eb233 | 80 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
e1ad7468 DL |
81 | |
82 | return events; | |
83 | } | |
84 | ||
85 | static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count, | |
86 | loff_t *ppos) | |
87 | { | |
88 | struct eventfd_ctx *ctx = file->private_data; | |
89 | ssize_t res; | |
90 | __u64 ucnt; | |
91 | DECLARE_WAITQUEUE(wait, current); | |
92 | ||
93 | if (count < sizeof(ucnt)) | |
94 | return -EINVAL; | |
d48eb233 | 95 | spin_lock_irq(&ctx->wqh.lock); |
e1ad7468 DL |
96 | res = -EAGAIN; |
97 | ucnt = ctx->count; | |
98 | if (ucnt > 0) | |
99 | res = sizeof(ucnt); | |
100 | else if (!(file->f_flags & O_NONBLOCK)) { | |
101 | __add_wait_queue(&ctx->wqh, &wait); | |
102 | for (res = 0;;) { | |
103 | set_current_state(TASK_INTERRUPTIBLE); | |
104 | if (ctx->count > 0) { | |
105 | ucnt = ctx->count; | |
106 | res = sizeof(ucnt); | |
107 | break; | |
108 | } | |
109 | if (signal_pending(current)) { | |
110 | res = -ERESTARTSYS; | |
111 | break; | |
112 | } | |
d48eb233 | 113 | spin_unlock_irq(&ctx->wqh.lock); |
e1ad7468 | 114 | schedule(); |
d48eb233 | 115 | spin_lock_irq(&ctx->wqh.lock); |
e1ad7468 DL |
116 | } |
117 | __remove_wait_queue(&ctx->wqh, &wait); | |
118 | __set_current_state(TASK_RUNNING); | |
119 | } | |
120 | if (res > 0) { | |
121 | ctx->count = 0; | |
122 | if (waitqueue_active(&ctx->wqh)) | |
123 | wake_up_locked(&ctx->wqh); | |
124 | } | |
d48eb233 | 125 | spin_unlock_irq(&ctx->wqh.lock); |
e1ad7468 DL |
126 | if (res > 0 && put_user(ucnt, (__u64 __user *) buf)) |
127 | return -EFAULT; | |
128 | ||
129 | return res; | |
130 | } | |
131 | ||
132 | static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count, | |
133 | loff_t *ppos) | |
134 | { | |
135 | struct eventfd_ctx *ctx = file->private_data; | |
136 | ssize_t res; | |
137 | __u64 ucnt; | |
138 | DECLARE_WAITQUEUE(wait, current); | |
139 | ||
140 | if (count < sizeof(ucnt)) | |
141 | return -EINVAL; | |
142 | if (copy_from_user(&ucnt, buf, sizeof(ucnt))) | |
143 | return -EFAULT; | |
144 | if (ucnt == ULLONG_MAX) | |
145 | return -EINVAL; | |
d48eb233 | 146 | spin_lock_irq(&ctx->wqh.lock); |
e1ad7468 DL |
147 | res = -EAGAIN; |
148 | if (ULLONG_MAX - ctx->count > ucnt) | |
149 | res = sizeof(ucnt); | |
150 | else if (!(file->f_flags & O_NONBLOCK)) { | |
151 | __add_wait_queue(&ctx->wqh, &wait); | |
152 | for (res = 0;;) { | |
153 | set_current_state(TASK_INTERRUPTIBLE); | |
154 | if (ULLONG_MAX - ctx->count > ucnt) { | |
155 | res = sizeof(ucnt); | |
156 | break; | |
157 | } | |
158 | if (signal_pending(current)) { | |
159 | res = -ERESTARTSYS; | |
160 | break; | |
161 | } | |
d48eb233 | 162 | spin_unlock_irq(&ctx->wqh.lock); |
e1ad7468 | 163 | schedule(); |
d48eb233 | 164 | spin_lock_irq(&ctx->wqh.lock); |
e1ad7468 DL |
165 | } |
166 | __remove_wait_queue(&ctx->wqh, &wait); | |
167 | __set_current_state(TASK_RUNNING); | |
168 | } | |
169 | if (res > 0) { | |
170 | ctx->count += ucnt; | |
171 | if (waitqueue_active(&ctx->wqh)) | |
172 | wake_up_locked(&ctx->wqh); | |
173 | } | |
d48eb233 | 174 | spin_unlock_irq(&ctx->wqh.lock); |
e1ad7468 DL |
175 | |
176 | return res; | |
177 | } | |
178 | ||
179 | static const struct file_operations eventfd_fops = { | |
180 | .release = eventfd_release, | |
181 | .poll = eventfd_poll, | |
182 | .read = eventfd_read, | |
183 | .write = eventfd_write, | |
184 | }; | |
185 | ||
186 | struct file *eventfd_fget(int fd) | |
187 | { | |
188 | struct file *file; | |
189 | ||
190 | file = fget(fd); | |
191 | if (!file) | |
192 | return ERR_PTR(-EBADF); | |
193 | if (file->f_op != &eventfd_fops) { | |
194 | fput(file); | |
195 | return ERR_PTR(-EINVAL); | |
196 | } | |
197 | ||
198 | return file; | |
199 | } | |
200 | ||
b087498e | 201 | asmlinkage long sys_eventfd2(unsigned int count, int flags) |
e1ad7468 | 202 | { |
2030a42c | 203 | int fd; |
e1ad7468 | 204 | struct eventfd_ctx *ctx; |
e1ad7468 | 205 | |
e38b36f3 UD |
206 | /* Check the EFD_* constants for consistency. */ |
207 | BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC); | |
208 | BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK); | |
209 | ||
e7d476df | 210 | if (flags & ~(EFD_CLOEXEC | EFD_NONBLOCK)) |
b087498e UD |
211 | return -EINVAL; |
212 | ||
e1ad7468 DL |
213 | ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); |
214 | if (!ctx) | |
215 | return -ENOMEM; | |
216 | ||
217 | init_waitqueue_head(&ctx->wqh); | |
e1ad7468 DL |
218 | ctx->count = count; |
219 | ||
220 | /* | |
221 | * When we call this, the initialization must be complete, since | |
222 | * anon_inode_getfd() will install the fd. | |
223 | */ | |
b087498e | 224 | fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx, |
e7d476df | 225 | flags & (O_CLOEXEC | O_NONBLOCK)); |
2030a42c AV |
226 | if (fd < 0) |
227 | kfree(ctx); | |
228 | return fd; | |
e1ad7468 DL |
229 | } |
230 | ||
b087498e UD |
231 | asmlinkage long sys_eventfd(unsigned int count) |
232 | { | |
233 | return sys_eventfd2(count, 0); | |
234 | } | |
235 |