blob: 96355d505347414fc67dd1a7cd21503bdffa3c91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Davide Libenzic7ea7632007-05-15 01:40:47 -07002 * fs/eventpoll.c (Efficent event polling implementation)
3 * Copyright (C) 2001,...,2007 Davide Libenzi
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * Davide Libenzi <davidel@xmailserver.org>
11 *
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/sched.h>
17#include <linux/fs.h>
18#include <linux/file.h>
19#include <linux/signal.h>
20#include <linux/errno.h>
21#include <linux/mm.h>
22#include <linux/slab.h>
23#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/string.h>
25#include <linux/list.h>
26#include <linux/hash.h>
27#include <linux/spinlock.h>
28#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/rbtree.h>
30#include <linux/wait.h>
31#include <linux/eventpoll.h>
32#include <linux/mount.h>
33#include <linux/bitops.h>
Arjan van de Ven144efe32006-03-23 03:00:32 -080034#include <linux/mutex.h>
Davide Libenzida66f7c2007-05-10 22:23:21 -070035#include <linux/anon_inodes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37#include <asm/system.h>
38#include <asm/io.h>
39#include <asm/mman.h>
40#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
43 * LOCKING:
44 * There are three level of locking required by epoll :
45 *
Arjan van de Ven144efe32006-03-23 03:00:32 -080046 * 1) epmutex (mutex)
Davide Libenzic7ea7632007-05-15 01:40:47 -070047 * 2) ep->mtx (mutex)
48 * 3) ep->lock (spinlock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 *
50 * The acquire order is the one listed above, from 1 to 3.
51 * We need a spinlock (ep->lock) because we manipulate objects
52 * from inside the poll callback, that might be triggered from
53 * a wake_up() that in turn might be called from IRQ context.
54 * So we can't sleep inside the poll callback and hence we need
55 * a spinlock. During the event transfer loop (from kernel to
56 * user space) we could end up sleeping due a copy_to_user(), so
57 * we need a lock that will allow us to sleep. This lock is a
Davide Libenzid47de162007-05-15 01:40:41 -070058 * mutex (ep->mtx). It is acquired during the event transfer loop,
59 * during epoll_ctl(EPOLL_CTL_DEL) and during eventpoll_release_file().
60 * Then we also need a global mutex to serialize eventpoll_release_file()
61 * and ep_free().
62 * This mutex is acquired by ep_free() during the epoll file
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * cleanup path and it is also acquired by eventpoll_release_file()
64 * if a file has been pushed inside an epoll set and it is then
65 * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL).
Davide Libenzid47de162007-05-15 01:40:41 -070066 * It is possible to drop the "ep->mtx" and to use the global
67 * mutex "epmutex" (together with "ep->lock") to have it working,
68 * but having "ep->mtx" will make the interface more scalable.
Arjan van de Ven144efe32006-03-23 03:00:32 -080069 * Events that require holding "epmutex" are very rare, while for
Davide Libenzid47de162007-05-15 01:40:41 -070070 * normal operations the epoll private "ep->mtx" will guarantee
71 * a better scalability.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define DEBUG_EPOLL 0
75
76#if DEBUG_EPOLL > 0
77#define DPRINTK(x) printk x
78#define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0)
79#else /* #if DEBUG_EPOLL > 0 */
80#define DPRINTK(x) (void) 0
81#define DNPRINTK(n, x) (void) 0
82#endif /* #if DEBUG_EPOLL > 0 */
83
84#define DEBUG_EPI 0
85
86#if DEBUG_EPI != 0
87#define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */)
88#else /* #if DEBUG_EPI != 0 */
89#define EPI_SLAB_DEBUG 0
90#endif /* #if DEBUG_EPI != 0 */
91
92/* Epoll private bits inside the event mask */
93#define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET)
94
95/* Maximum number of poll wake up nests we are allowing */
96#define EP_MAX_POLLWAKE_NESTS 4
97
Davide Libenzie3306dd2005-09-27 21:45:33 -070098/* Maximum msec timeout value storeable in a long int */
99#define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ)
100
Davide Libenzib6119672006-10-11 01:21:44 -0700101#define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
102
Davide Libenzid47de162007-05-15 01:40:41 -0700103#define EP_UNACTIVE_PTR ((void *) -1L)
104
Davide Libenzi7ef99642008-12-01 13:13:55 -0800105#define EP_ITEM_COST (sizeof(struct epitem) + sizeof(struct eppoll_entry))
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107struct epoll_filefd {
108 struct file *file;
109 int fd;
110};
111
112/*
113 * Node that is linked into the "wake_task_list" member of the "struct poll_safewake".
114 * It is used to keep track on all tasks that are currently inside the wake_up() code
115 * to 1) short-circuit the one coming from the same task and same wait queue head
Davide Libenzid47de162007-05-15 01:40:41 -0700116 * (loop) 2) allow a maximum number of epoll descriptors inclusion nesting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * 3) let go the ones coming from other tasks.
118 */
119struct wake_task_node {
120 struct list_head llink;
Ingo Molnar36c8b582006-07-03 00:25:41 -0700121 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 wait_queue_head_t *wq;
123};
124
125/*
126 * This is used to implement the safe poll wake up avoiding to reenter
127 * the poll callback from inside wake_up().
128 */
129struct poll_safewake {
130 struct list_head wake_task_list;
131 spinlock_t lock;
132};
133
134/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * Each file descriptor added to the eventpoll interface will
Davide Libenzi6192bd52007-05-08 00:25:41 -0700136 * have an entry of this type linked to the "rbr" RB tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
138struct epitem {
Davide Libenzi67647d02007-05-15 01:40:52 -0700139 /* RB tree node used to link this structure to the eventpoll RB tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct rb_node rbn;
141
142 /* List header used to link this structure to the eventpoll ready list */
143 struct list_head rdllink;
144
Davide Libenzic7ea7632007-05-15 01:40:47 -0700145 /*
146 * Works together "struct eventpoll"->ovflist in keeping the
147 * single linked chain of items.
148 */
149 struct epitem *next;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* The file descriptor information this item refers to */
152 struct epoll_filefd ffd;
153
154 /* Number of active wait queue attached to poll operations */
155 int nwait;
156
157 /* List containing poll wait queues */
158 struct list_head pwqlist;
159
160 /* The "container" of this item */
161 struct eventpoll *ep;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* List header used to link this item to the "struct file" items list */
164 struct list_head fllink;
Davide Libenzid47de162007-05-15 01:40:41 -0700165
Davide Libenzic7ea7632007-05-15 01:40:47 -0700166 /* The structure that describe the interested events and the source fd */
167 struct epoll_event event;
Davide Libenzid47de162007-05-15 01:40:41 -0700168};
169
170/*
171 * This structure is stored inside the "private_data" member of the file
172 * structure and rapresent the main data sructure for the eventpoll
173 * interface.
174 */
175struct eventpoll {
176 /* Protect the this structure access */
Davide Libenzic7ea7632007-05-15 01:40:47 -0700177 spinlock_t lock;
Davide Libenzid47de162007-05-15 01:40:41 -0700178
179 /*
180 * This mutex is used to ensure that files are not removed
181 * while epoll is using them. This is held during the event
182 * collection loop, the file cleanup path, the epoll file exit
183 * code and the ctl operations.
184 */
185 struct mutex mtx;
186
187 /* Wait queue used by sys_epoll_wait() */
188 wait_queue_head_t wq;
189
190 /* Wait queue used by file->poll() */
191 wait_queue_head_t poll_wait;
192
193 /* List of ready file descriptors */
194 struct list_head rdllist;
195
Davide Libenzi67647d02007-05-15 01:40:52 -0700196 /* RB tree root used to store monitored fd structs */
Davide Libenzid47de162007-05-15 01:40:41 -0700197 struct rb_root rbr;
198
199 /*
200 * This is a single linked list that chains all the "struct epitem" that
201 * happened while transfering ready events to userspace w/out
202 * holding ->lock.
203 */
204 struct epitem *ovflist;
Davide Libenzi7ef99642008-12-01 13:13:55 -0800205
206 /* The user that created the eventpoll descriptor */
207 struct user_struct *user;
Davide Libenzid47de162007-05-15 01:40:41 -0700208};
209
210/* Wait structure used by the poll hooks */
211struct eppoll_entry {
212 /* List header used to link this structure to the "struct epitem" */
213 struct list_head llink;
214
215 /* The "base" pointer is set to the container "struct epitem" */
216 void *base;
217
218 /*
219 * Wait queue item that will be linked to the target file wait
220 * queue head.
221 */
222 wait_queue_t wait;
223
224 /* The wait queue head that linked the "wait" wait queue item */
225 wait_queue_head_t *whead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
228/* Wrapper struct used by poll queueing */
229struct ep_pqueue {
230 poll_table pt;
231 struct epitem *epi;
232};
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*
Davide Libenzi7ef99642008-12-01 13:13:55 -0800235 * Configuration options available inside /proc/sys/fs/epoll/
236 */
237/* Maximum number of epoll devices, per user */
238static int max_user_instances __read_mostly;
239/* Maximum number of epoll watched descriptors, per user */
240static int max_user_watches __read_mostly;
241
242/*
Davide Libenzid47de162007-05-15 01:40:41 -0700243 * This mutex is used to serialize ep_free() and eventpoll_release_file().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Davide Libenzi7ef99642008-12-01 13:13:55 -0800245static DEFINE_MUTEX(epmutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247/* Safe wake up implementation */
248static struct poll_safewake psw;
249
250/* Slab cache used to allocate "struct epitem" */
Christoph Lametere18b8902006-12-06 20:33:20 -0800251static struct kmem_cache *epi_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253/* Slab cache used to allocate "struct eppoll_entry" */
Christoph Lametere18b8902006-12-06 20:33:20 -0800254static struct kmem_cache *pwq_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Davide Libenzi7ef99642008-12-01 13:13:55 -0800256#ifdef CONFIG_SYSCTL
257
258#include <linux/sysctl.h>
259
260static int zero;
261
262ctl_table epoll_table[] = {
263 {
264 .procname = "max_user_instances",
265 .data = &max_user_instances,
266 .maxlen = sizeof(int),
267 .mode = 0644,
268 .proc_handler = &proc_dointvec_minmax,
269 .extra1 = &zero,
270 },
271 {
272 .procname = "max_user_watches",
273 .data = &max_user_watches,
274 .maxlen = sizeof(int),
275 .mode = 0644,
276 .proc_handler = &proc_dointvec_minmax,
277 .extra1 = &zero,
278 },
279 { .ctl_name = 0 }
280};
281#endif /* CONFIG_SYSCTL */
282
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700283
Davide Libenzi67647d02007-05-15 01:40:52 -0700284/* Setup the structure that is used as key for the RB tree */
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700285static inline void ep_set_ffd(struct epoll_filefd *ffd,
286 struct file *file, int fd)
287{
288 ffd->file = file;
289 ffd->fd = fd;
290}
291
Davide Libenzi67647d02007-05-15 01:40:52 -0700292/* Compare RB tree keys */
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700293static inline int ep_cmp_ffd(struct epoll_filefd *p1,
294 struct epoll_filefd *p2)
295{
296 return (p1->file > p2->file ? +1:
297 (p1->file < p2->file ? -1 : p1->fd - p2->fd));
298}
299
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700300/* Tells us if the item is currently linked */
301static inline int ep_is_linked(struct list_head *p)
302{
303 return !list_empty(p);
304}
305
306/* Get the "struct epitem" from a wait queue pointer */
Davide Libenzicdac75e2008-04-29 00:58:34 -0700307static inline struct epitem *ep_item_from_wait(wait_queue_t *p)
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700308{
309 return container_of(p, struct eppoll_entry, wait)->base;
310}
311
312/* Get the "struct epitem" from an epoll queue wrapper */
Davide Libenzicdac75e2008-04-29 00:58:34 -0700313static inline struct epitem *ep_item_from_epqueue(poll_table *p)
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700314{
315 return container_of(p, struct ep_pqueue, pt)->epi;
316}
317
318/* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
Davide Libenzi6192bd52007-05-08 00:25:41 -0700319static inline int ep_op_has_event(int op)
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700320{
321 return op != EPOLL_CTL_DEL;
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/* Initialize the poll safe wake up structure */
325static void ep_poll_safewake_init(struct poll_safewake *psw)
326{
327
328 INIT_LIST_HEAD(&psw->wake_task_list);
329 spin_lock_init(&psw->lock);
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
333 * Perform a safe wake up of the poll wait list. The problem is that
334 * with the new callback'd wake up system, it is possible that the
335 * poll callback is reentered from inside the call to wake_up() done
336 * on the poll wait queue head. The rule is that we cannot reenter the
337 * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times,
338 * and we cannot reenter the same wait queue head at all. This will
339 * enable to have a hierarchy of epoll file descriptor of no more than
340 * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock
341 * because this one gets called by the poll callback, that in turn is called
342 * from inside a wake_up(), that might be called from irq context.
343 */
344static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
345{
346 int wake_nests = 0;
347 unsigned long flags;
Ingo Molnar36c8b582006-07-03 00:25:41 -0700348 struct task_struct *this_task = current;
Matthias Kaehlckeb70c3942007-10-18 23:39:56 -0700349 struct list_head *lsthead = &psw->wake_task_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct wake_task_node *tncur;
351 struct wake_task_node tnode;
352
353 spin_lock_irqsave(&psw->lock, flags);
354
355 /* Try to see if the current task is already inside this wakeup call */
Matthias Kaehlckeb70c3942007-10-18 23:39:56 -0700356 list_for_each_entry(tncur, lsthead, llink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (tncur->wq == wq ||
359 (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) {
360 /*
361 * Ops ... loop detected or maximum nest level reached.
362 * We abort this wake by breaking the cycle itself.
363 */
364 spin_unlock_irqrestore(&psw->lock, flags);
365 return;
366 }
367 }
368
369 /* Add the current task to the list */
370 tnode.task = this_task;
371 tnode.wq = wq;
372 list_add(&tnode.llink, lsthead);
373
374 spin_unlock_irqrestore(&psw->lock, flags);
375
376 /* Do really wake up now */
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800377 wake_up_nested(wq, 1 + wake_nests);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* Remove the current task from the list */
380 spin_lock_irqsave(&psw->lock, flags);
381 list_del(&tnode.llink);
382 spin_unlock_irqrestore(&psw->lock, flags);
383}
384
Davide Libenzi7699acd2007-05-10 22:23:23 -0700385/*
386 * This function unregister poll callbacks from the associated file descriptor.
387 * Since this must be called without holding "ep->lock" the atomic exchange trick
388 * will protect us from multiple unregister.
389 */
390static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
391{
392 int nwait;
393 struct list_head *lsthead = &epi->pwqlist;
394 struct eppoll_entry *pwq;
395
396 /* This is called without locks, so we need the atomic exchange */
397 nwait = xchg(&epi->nwait, 0);
398
399 if (nwait) {
400 while (!list_empty(lsthead)) {
401 pwq = list_first_entry(lsthead, struct eppoll_entry, llink);
402
403 list_del_init(&pwq->llink);
404 remove_wait_queue(pwq->whead, &pwq->wait);
405 kmem_cache_free(pwq_cache, pwq);
406 }
407 }
408}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/*
Davide Libenzi7699acd2007-05-10 22:23:23 -0700411 * Removes a "struct epitem" from the eventpoll RB tree and deallocates
Davide Libenzic7ea7632007-05-15 01:40:47 -0700412 * all the associated resources. Must be called with "mtx" held.
Davide Libenzi7699acd2007-05-10 22:23:23 -0700413 */
414static int ep_remove(struct eventpoll *ep, struct epitem *epi)
415{
Davide Libenzi7699acd2007-05-10 22:23:23 -0700416 unsigned long flags;
417 struct file *file = epi->ffd.file;
418
419 /*
420 * Removes poll wait queue hooks. We _have_ to do this without holding
421 * the "ep->lock" otherwise a deadlock might occur. This because of the
422 * sequence of the lock acquisition. Here we do "ep->lock" then the wait
423 * queue head lock when unregistering the wait queue. The wakeup callback
424 * will run by holding the wait queue head lock and will call our callback
425 * that will try to get "ep->lock".
426 */
427 ep_unregister_pollwait(ep, epi);
428
429 /* Remove the current item from the list of epoll hooks */
430 spin_lock(&file->f_ep_lock);
431 if (ep_is_linked(&epi->fllink))
Davide Libenzi6192bd52007-05-08 00:25:41 -0700432 list_del_init(&epi->fllink);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700433 spin_unlock(&file->f_ep_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Davide Libenzicdac75e2008-04-29 00:58:34 -0700435 rb_erase(&epi->rbn, &ep->rbr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Davide Libenzic7ea7632007-05-15 01:40:47 -0700437 spin_lock_irqsave(&ep->lock, flags);
438 if (ep_is_linked(&epi->rdllink))
439 list_del_init(&epi->rdllink);
440 spin_unlock_irqrestore(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Davide Libenzi7699acd2007-05-10 22:23:23 -0700442 /* At this point it is safe to free the eventpoll item */
Davide Libenzic7ea7632007-05-15 01:40:47 -0700443 kmem_cache_free(epi_cache, epi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Davide Libenzi7ef99642008-12-01 13:13:55 -0800445 atomic_dec(&ep->user->epoll_watches);
446
Davide Libenzic7ea7632007-05-15 01:40:47 -0700447 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p)\n",
448 current, ep, file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Davide Libenzic7ea7632007-05-15 01:40:47 -0700450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static void ep_free(struct eventpoll *ep)
454{
455 struct rb_node *rbp;
456 struct epitem *epi;
457
458 /* We need to release all tasks waiting for these file */
459 if (waitqueue_active(&ep->poll_wait))
460 ep_poll_safewake(&psw, &ep->poll_wait);
461
462 /*
463 * We need to lock this because we could be hit by
464 * eventpoll_release_file() while we're freeing the "struct eventpoll".
Davide Libenzid47de162007-05-15 01:40:41 -0700465 * We do not need to hold "ep->mtx" here because the epoll file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * is on the way to be removed and no one has references to it
467 * anymore. The only hit might come from eventpoll_release_file() but
Arjan van de Ven144efe32006-03-23 03:00:32 -0800468 * holding "epmutex" is sufficent here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Arjan van de Ven144efe32006-03-23 03:00:32 -0800470 mutex_lock(&epmutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /*
473 * Walks through the whole tree by unregistering poll callbacks.
474 */
475 for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
476 epi = rb_entry(rbp, struct epitem, rbn);
477
478 ep_unregister_pollwait(ep, epi);
479 }
480
481 /*
Davide Libenzi6192bd52007-05-08 00:25:41 -0700482 * Walks through the whole tree by freeing each "struct epitem". At this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * point we are sure no poll callbacks will be lingering around, and also by
Davide Libenzid47de162007-05-15 01:40:41 -0700484 * holding "epmutex" we can be sure that no file cleanup code will hit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * us during this operation. So we can avoid the lock on "ep->lock".
486 */
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700487 while ((rbp = rb_first(&ep->rbr)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 epi = rb_entry(rbp, struct epitem, rbn);
489 ep_remove(ep, epi);
490 }
491
Arjan van de Ven144efe32006-03-23 03:00:32 -0800492 mutex_unlock(&epmutex);
Davide Libenzid47de162007-05-15 01:40:41 -0700493 mutex_destroy(&ep->mtx);
Davide Libenzi7ef99642008-12-01 13:13:55 -0800494 atomic_dec(&ep->user->epoll_devs);
495 free_uid(ep->user);
Davide Libenzif0ee9aa2007-05-15 01:40:57 -0700496 kfree(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Davide Libenzi7699acd2007-05-10 22:23:23 -0700499static int ep_eventpoll_release(struct inode *inode, struct file *file)
500{
501 struct eventpoll *ep = file->private_data;
502
Davide Libenzif0ee9aa2007-05-15 01:40:57 -0700503 if (ep)
Davide Libenzi7699acd2007-05-10 22:23:23 -0700504 ep_free(ep);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700505
506 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep));
507 return 0;
508}
509
510static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
511{
512 unsigned int pollflags = 0;
513 unsigned long flags;
514 struct eventpoll *ep = file->private_data;
515
516 /* Insert inside our poll wait queue */
517 poll_wait(file, &ep->poll_wait, wait);
518
519 /* Check our condition */
Davide Libenzic7ea7632007-05-15 01:40:47 -0700520 spin_lock_irqsave(&ep->lock, flags);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700521 if (!list_empty(&ep->rdllist))
522 pollflags = POLLIN | POLLRDNORM;
Davide Libenzic7ea7632007-05-15 01:40:47 -0700523 spin_unlock_irqrestore(&ep->lock, flags);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700524
525 return pollflags;
526}
527
528/* File callbacks that implement the eventpoll file behaviour */
529static const struct file_operations eventpoll_fops = {
530 .release = ep_eventpoll_release,
531 .poll = ep_eventpoll_poll
532};
533
534/* Fast test to see if the file is an evenpoll file */
535static inline int is_file_epoll(struct file *f)
536{
537 return f->f_op == &eventpoll_fops;
538}
539
540/*
541 * This is called from eventpoll_release() to unlink files from the eventpoll
542 * interface. We need to have this facility to cleanup correctly files that are
543 * closed without being removed from the eventpoll interface.
544 */
545void eventpoll_release_file(struct file *file)
546{
547 struct list_head *lsthead = &file->f_ep_links;
548 struct eventpoll *ep;
549 struct epitem *epi;
550
551 /*
552 * We don't want to get "file->f_ep_lock" because it is not
553 * necessary. It is not necessary because we're in the "struct file"
554 * cleanup path, and this means that noone is using this file anymore.
Davide Libenzi67647d02007-05-15 01:40:52 -0700555 * So, for example, epoll_ctl() cannot hit here sicne if we reach this
556 * point, the file counter already went to zero and fget() would fail.
Davide Libenzid47de162007-05-15 01:40:41 -0700557 * The only hit might come from ep_free() but by holding the mutex
Davide Libenzi7699acd2007-05-10 22:23:23 -0700558 * will correctly serialize the operation. We do need to acquire
Davide Libenzid47de162007-05-15 01:40:41 -0700559 * "ep->mtx" after "epmutex" because ep_remove() requires it when called
Davide Libenzi7699acd2007-05-10 22:23:23 -0700560 * from anywhere but ep_free().
561 */
562 mutex_lock(&epmutex);
563
564 while (!list_empty(lsthead)) {
565 epi = list_first_entry(lsthead, struct epitem, fllink);
566
567 ep = epi->ep;
568 list_del_init(&epi->fllink);
Davide Libenzid47de162007-05-15 01:40:41 -0700569 mutex_lock(&ep->mtx);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700570 ep_remove(ep, epi);
Davide Libenzid47de162007-05-15 01:40:41 -0700571 mutex_unlock(&ep->mtx);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700572 }
573
574 mutex_unlock(&epmutex);
575}
576
577static int ep_alloc(struct eventpoll **pep)
578{
Davide Libenzi7ef99642008-12-01 13:13:55 -0800579 int error;
580 struct user_struct *user;
581 struct eventpoll *ep;
Davide Libenzi7699acd2007-05-10 22:23:23 -0700582
Davide Libenzi7ef99642008-12-01 13:13:55 -0800583 user = get_current_user();
584 error = -EMFILE;
585 if (unlikely(atomic_read(&user->epoll_devs) >=
586 max_user_instances))
587 goto free_uid;
588 error = -ENOMEM;
589 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
590 if (unlikely(!ep))
591 goto free_uid;
Davide Libenzi7699acd2007-05-10 22:23:23 -0700592
Davide Libenzic7ea7632007-05-15 01:40:47 -0700593 spin_lock_init(&ep->lock);
Davide Libenzid47de162007-05-15 01:40:41 -0700594 mutex_init(&ep->mtx);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700595 init_waitqueue_head(&ep->wq);
596 init_waitqueue_head(&ep->poll_wait);
597 INIT_LIST_HEAD(&ep->rdllist);
598 ep->rbr = RB_ROOT;
Davide Libenzid47de162007-05-15 01:40:41 -0700599 ep->ovflist = EP_UNACTIVE_PTR;
Davide Libenzi7ef99642008-12-01 13:13:55 -0800600 ep->user = user;
Davide Libenzi7699acd2007-05-10 22:23:23 -0700601
602 *pep = ep;
603
604 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n",
605 current, ep));
606 return 0;
Davide Libenzi7ef99642008-12-01 13:13:55 -0800607
608free_uid:
609 free_uid(user);
610 return error;
Davide Libenzi7699acd2007-05-10 22:23:23 -0700611}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613/*
Davide Libenzic7ea7632007-05-15 01:40:47 -0700614 * Search the file inside the eventpoll tree. The RB tree operations
615 * are protected by the "mtx" mutex, and ep_find() must be called with
616 * "mtx" held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
618static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
619{
620 int kcmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct rb_node *rbp;
622 struct epitem *epi, *epir = NULL;
623 struct epoll_filefd ffd;
624
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700625 ep_set_ffd(&ffd, file, fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 for (rbp = ep->rbr.rb_node; rbp; ) {
627 epi = rb_entry(rbp, struct epitem, rbn);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700628 kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (kcmp > 0)
630 rbp = rbp->rb_right;
631 else if (kcmp < 0)
632 rbp = rbp->rb_left;
633 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 epir = epi;
635 break;
636 }
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n",
640 current, file, epir));
641
642 return epir;
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645/*
Davide Libenzi7699acd2007-05-10 22:23:23 -0700646 * This is the callback that is passed to the wait queue wakeup
647 * machanism. It is called by the stored file descriptors when they
648 * have events to report.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 */
Davide Libenzi7699acd2007-05-10 22:23:23 -0700650static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Davide Libenzi7699acd2007-05-10 22:23:23 -0700652 int pwake = 0;
653 unsigned long flags;
654 struct epitem *epi = ep_item_from_wait(wait);
655 struct eventpoll *ep = epi->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Davide Libenzi7699acd2007-05-10 22:23:23 -0700657 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
658 current, epi->ffd.file, epi, ep));
659
Davide Libenzic7ea7632007-05-15 01:40:47 -0700660 spin_lock_irqsave(&ep->lock, flags);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700661
662 /*
663 * If the event mask does not contain any poll(2) event, we consider the
664 * descriptor to be disabled. This condition is likely the effect of the
665 * EPOLLONESHOT bit that disables the descriptor when an event is received,
666 * until the next EPOLL_CTL_MOD will be issued.
667 */
668 if (!(epi->event.events & ~EP_PRIVATE_BITS))
Davide Libenzid47de162007-05-15 01:40:41 -0700669 goto out_unlock;
670
671 /*
672 * If we are trasfering events to userspace, we can hold no locks
673 * (because we're accessing user memory, and because of linux f_op->poll()
674 * semantics). All the events that happens during that period of time are
675 * chained in ep->ovflist and requeued later on.
676 */
677 if (unlikely(ep->ovflist != EP_UNACTIVE_PTR)) {
678 if (epi->next == EP_UNACTIVE_PTR) {
679 epi->next = ep->ovflist;
680 ep->ovflist = epi;
681 }
682 goto out_unlock;
683 }
Davide Libenzi7699acd2007-05-10 22:23:23 -0700684
685 /* If this file is already in the ready list we exit soon */
686 if (ep_is_linked(&epi->rdllink))
687 goto is_linked;
688
689 list_add_tail(&epi->rdllink, &ep->rdllist);
690
691is_linked:
692 /*
693 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
694 * wait list.
695 */
696 if (waitqueue_active(&ep->wq))
Matthew Wilcox4a6e9e2c2007-08-30 16:10:22 -0400697 wake_up_locked(&ep->wq);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700698 if (waitqueue_active(&ep->poll_wait))
699 pwake++;
700
Davide Libenzid47de162007-05-15 01:40:41 -0700701out_unlock:
Davide Libenzic7ea7632007-05-15 01:40:47 -0700702 spin_unlock_irqrestore(&ep->lock, flags);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700703
704 /* We have to call this outside the lock */
705 if (pwake)
706 ep_poll_safewake(&psw, &ep->poll_wait);
707
708 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/*
712 * This is the callback that is used to add our wait queue to the
713 * target file wakeup lists.
714 */
715static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
716 poll_table *pt)
717{
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700718 struct epitem *epi = ep_item_from_epqueue(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 struct eppoll_entry *pwq;
720
Christoph Lametere94b1762006-12-06 20:33:17 -0800721 if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
723 pwq->whead = whead;
724 pwq->base = epi;
725 add_wait_queue(whead, &pwq->wait);
726 list_add_tail(&pwq->llink, &epi->pwqlist);
727 epi->nwait++;
728 } else {
729 /* We have to signal that an error occurred */
730 epi->nwait = -1;
731 }
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
735{
736 int kcmp;
737 struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
738 struct epitem *epic;
739
740 while (*p) {
741 parent = *p;
742 epic = rb_entry(parent, struct epitem, rbn);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700743 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (kcmp > 0)
745 p = &parent->rb_right;
746 else
747 p = &parent->rb_left;
748 }
749 rb_link_node(&epi->rbn, parent, p);
750 rb_insert_color(&epi->rbn, &ep->rbr);
751}
752
Davide Libenzic7ea7632007-05-15 01:40:47 -0700753/*
754 * Must be called with "mtx" held.
755 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
757 struct file *tfile, int fd)
758{
759 int error, revents, pwake = 0;
760 unsigned long flags;
761 struct epitem *epi;
762 struct ep_pqueue epq;
763
Davide Libenzi7ef99642008-12-01 13:13:55 -0800764 if (unlikely(atomic_read(&ep->user->epoll_watches) >=
765 max_user_watches))
766 return -ENOSPC;
Christoph Lametere94b1762006-12-06 20:33:17 -0800767 if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
Davide Libenzi7ef99642008-12-01 13:13:55 -0800768 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* Item initialization follow here ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 INIT_LIST_HEAD(&epi->rdllink);
772 INIT_LIST_HEAD(&epi->fllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 INIT_LIST_HEAD(&epi->pwqlist);
774 epi->ep = ep;
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700775 ep_set_ffd(&epi->ffd, tfile, fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 epi->event = *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 epi->nwait = 0;
Davide Libenzid47de162007-05-15 01:40:41 -0700778 epi->next = EP_UNACTIVE_PTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* Initialize the poll table using the queue callback */
781 epq.epi = epi;
782 init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
783
784 /*
785 * Attach the item to the poll hooks and get current event bits.
786 * We can safely use the file* here because its usage count has
Davide Libenzic7ea7632007-05-15 01:40:47 -0700787 * been increased by the caller of this function. Note that after
788 * this operation completes, the poll callback can start hitting
789 * the new item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
791 revents = tfile->f_op->poll(tfile, &epq.pt);
792
793 /*
794 * We have to check if something went wrong during the poll wait queue
795 * install process. Namely an allocation for a wait queue failed due
796 * high memory pressure.
797 */
Davide Libenzi7ef99642008-12-01 13:13:55 -0800798 error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (epi->nwait < 0)
Davide Libenzi7699acd2007-05-10 22:23:23 -0700800 goto error_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* Add the current item to the list of active epoll hook for this file */
803 spin_lock(&tfile->f_ep_lock);
804 list_add_tail(&epi->fllink, &tfile->f_ep_links);
805 spin_unlock(&tfile->f_ep_lock);
806
Davide Libenzic7ea7632007-05-15 01:40:47 -0700807 /*
808 * Add the current item to the RB tree. All RB tree operations are
809 * protected by "mtx", and ep_insert() is called with "mtx" held.
810 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 ep_rbtree_insert(ep, epi);
812
Davide Libenzic7ea7632007-05-15 01:40:47 -0700813 /* We have to drop the new item inside our item list to keep track of it */
814 spin_lock_irqsave(&ep->lock, flags);
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* If the file is already "ready" we drop it inside the ready list */
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700817 if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 list_add_tail(&epi->rdllink, &ep->rdllist);
819
820 /* Notify waiting tasks that events are available */
821 if (waitqueue_active(&ep->wq))
Matthew Wilcox4a6e9e2c2007-08-30 16:10:22 -0400822 wake_up_locked(&ep->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (waitqueue_active(&ep->poll_wait))
824 pwake++;
825 }
826
Davide Libenzic7ea7632007-05-15 01:40:47 -0700827 spin_unlock_irqrestore(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Davide Libenzi7ef99642008-12-01 13:13:55 -0800829 atomic_inc(&ep->user->epoll_watches);
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* We have to call this outside the lock */
832 if (pwake)
833 ep_poll_safewake(&psw, &ep->poll_wait);
834
835 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n",
836 current, ep, tfile, fd));
837
838 return 0;
839
Davide Libenzi7699acd2007-05-10 22:23:23 -0700840error_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ep_unregister_pollwait(ep, epi);
842
843 /*
844 * We need to do this because an event could have been arrived on some
Davide Libenzi67647d02007-05-15 01:40:52 -0700845 * allocated wait queue. Note that we don't care about the ep->ovflist
846 * list, since that is used/cleaned only inside a section bound by "mtx".
847 * And ep_insert() is called with "mtx" held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
Davide Libenzic7ea7632007-05-15 01:40:47 -0700849 spin_lock_irqsave(&ep->lock, flags);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700850 if (ep_is_linked(&epi->rdllink))
Davide Libenzi6192bd52007-05-08 00:25:41 -0700851 list_del_init(&epi->rdllink);
Davide Libenzic7ea7632007-05-15 01:40:47 -0700852 spin_unlock_irqrestore(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700854 kmem_cache_free(epi_cache, epi);
Davide Libenzi7ef99642008-12-01 13:13:55 -0800855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return error;
857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859/*
860 * Modify the interest event mask by dropping an event if the new mask
Davide Libenzic7ea7632007-05-15 01:40:47 -0700861 * has a match in the current file status. Must be called with "mtx" held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
863static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
864{
865 int pwake = 0;
866 unsigned int revents;
867 unsigned long flags;
868
869 /*
870 * Set the new event interest mask before calling f_op->poll(), otherwise
871 * a potential race might occur. In fact if we do this operation inside
872 * the lock, an event might happen between the f_op->poll() call and the
873 * new event set registering.
874 */
875 epi->event.events = event->events;
876
877 /*
878 * Get current event bits. We can safely use the file* here because
879 * its usage count has been increased by the caller of this function.
880 */
881 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
882
Davide Libenzic7ea7632007-05-15 01:40:47 -0700883 spin_lock_irqsave(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /* Copy the data member from inside the lock */
886 epi->event.data = event->data;
887
888 /*
Davide Libenzic7ea7632007-05-15 01:40:47 -0700889 * If the item is "hot" and it is not registered inside the ready
Davide Libenzi67647d02007-05-15 01:40:52 -0700890 * list, push it inside.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
Davide Libenzic7ea7632007-05-15 01:40:47 -0700892 if (revents & event->events) {
893 if (!ep_is_linked(&epi->rdllink)) {
894 list_add_tail(&epi->rdllink, &ep->rdllist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Davide Libenzic7ea7632007-05-15 01:40:47 -0700896 /* Notify waiting tasks that events are available */
897 if (waitqueue_active(&ep->wq))
Matthew Wilcox4a6e9e2c2007-08-30 16:10:22 -0400898 wake_up_locked(&ep->wq);
Davide Libenzic7ea7632007-05-15 01:40:47 -0700899 if (waitqueue_active(&ep->poll_wait))
900 pwake++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 }
Davide Libenzic7ea7632007-05-15 01:40:47 -0700903 spin_unlock_irqrestore(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* We have to call this outside the lock */
906 if (pwake)
907 ep_poll_safewake(&psw, &ep->poll_wait);
908
909 return 0;
910}
911
Davide Libenzid47de162007-05-15 01:40:41 -0700912static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *events,
913 int maxevents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Davide Libenzi6192bd52007-05-08 00:25:41 -0700915 int eventcnt, error = -EFAULT, pwake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 unsigned int revents;
Davide Libenzi6192bd52007-05-08 00:25:41 -0700917 unsigned long flags;
Davide Libenzid47de162007-05-15 01:40:41 -0700918 struct epitem *epi, *nepi;
919 struct list_head txlist;
Davide Libenzi6192bd52007-05-08 00:25:41 -0700920
Davide Libenzid47de162007-05-15 01:40:41 -0700921 INIT_LIST_HEAD(&txlist);
922
923 /*
924 * We need to lock this because we could be hit by
925 * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
926 */
927 mutex_lock(&ep->mtx);
928
929 /*
930 * Steal the ready list, and re-init the original one to the
931 * empty list. Also, set ep->ovflist to NULL so that events
932 * happening while looping w/out locks, are not lost. We cannot
933 * have the poll callback to queue directly on ep->rdllist,
934 * because we are doing it in the loop below, in a lockless way.
935 */
Davide Libenzic7ea7632007-05-15 01:40:47 -0700936 spin_lock_irqsave(&ep->lock, flags);
Davide Libenzid47de162007-05-15 01:40:41 -0700937 list_splice(&ep->rdllist, &txlist);
938 INIT_LIST_HEAD(&ep->rdllist);
939 ep->ovflist = NULL;
Davide Libenzic7ea7632007-05-15 01:40:47 -0700940 spin_unlock_irqrestore(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /*
943 * We can loop without lock because this is a task private list.
Davide Libenzi6192bd52007-05-08 00:25:41 -0700944 * We just splice'd out the ep->rdllist in ep_collect_ready_items().
Davide Libenzid47de162007-05-15 01:40:41 -0700945 * Items cannot vanish during the loop because we are holding "mtx".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
Davide Libenzid47de162007-05-15 01:40:41 -0700947 for (eventcnt = 0; !list_empty(&txlist) && eventcnt < maxevents;) {
948 epi = list_first_entry(&txlist, struct epitem, rdllink);
949
950 list_del_init(&epi->rdllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /*
953 * Get the ready file event set. We can safely use the file
Davide Libenzid47de162007-05-15 01:40:41 -0700954 * because we are holding the "mtx" and this will guarantee
955 * that both the file and the item will not vanish.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 */
957 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
Davide Libenzi6192bd52007-05-08 00:25:41 -0700958 revents &= epi->event.events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /*
Davide Libenzi6192bd52007-05-08 00:25:41 -0700961 * Is the event mask intersect the caller-requested one,
962 * deliver the event to userspace. Again, we are holding
Davide Libenzid47de162007-05-15 01:40:41 -0700963 * "mtx", so no operations coming from userspace can change
964 * the item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Davide Libenzi6192bd52007-05-08 00:25:41 -0700966 if (revents) {
967 if (__put_user(revents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 &events[eventcnt].events) ||
969 __put_user(epi->event.data,
970 &events[eventcnt].data))
Davide Libenzi6192bd52007-05-08 00:25:41 -0700971 goto errxit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (epi->event.events & EPOLLONESHOT)
973 epi->event.events &= EP_PRIVATE_BITS;
974 eventcnt++;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /*
Davide Libenzid47de162007-05-15 01:40:41 -0700977 * At this point, noone can insert into ep->rdllist besides
978 * us. The epoll_ctl() callers are locked out by us holding
979 * "mtx" and the poll callback will queue them in ep->ovflist.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
Davide Libenzi6192bd52007-05-08 00:25:41 -0700981 if (!(epi->event.events & EPOLLET) &&
Davide Libenzid47de162007-05-15 01:40:41 -0700982 (revents & epi->event.events))
983 list_add_tail(&epi->rdllink, &ep->rdllist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
Davide Libenzi6192bd52007-05-08 00:25:41 -0700985 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Davide Libenzid47de162007-05-15 01:40:41 -0700987errxit:
988
Davide Libenzic7ea7632007-05-15 01:40:47 -0700989 spin_lock_irqsave(&ep->lock, flags);
Davide Libenzid47de162007-05-15 01:40:41 -0700990 /*
991 * During the time we spent in the loop above, some other events
992 * might have been queued by the poll callback. We re-insert them
Davide Libenzif337b9c2008-10-15 22:01:56 -0700993 * inside the main ready-list here.
Davide Libenzid47de162007-05-15 01:40:41 -0700994 */
995 for (nepi = ep->ovflist; (epi = nepi) != NULL;
Davide Libenzi9ce209d2008-10-17 16:17:40 -0700996 nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
997 /*
998 * If the above loop quit with errors, the epoll item might still
999 * be linked to "txlist", and the list_splice() done below will
1000 * take care of those cases.
1001 */
1002 if (!ep_is_linked(&epi->rdllink))
1003 list_add_tail(&epi->rdllink, &ep->rdllist);
1004 }
Davide Libenzid47de162007-05-15 01:40:41 -07001005 /*
1006 * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
1007 * releasing the lock, events will be queued in the normal way inside
1008 * ep->rdllist.
1009 */
1010 ep->ovflist = EP_UNACTIVE_PTR;
Davide Libenzi6192bd52007-05-08 00:25:41 -07001011
1012 /*
Davide Libenzi67647d02007-05-15 01:40:52 -07001013 * In case of error in the event-send loop, or in case the number of
1014 * ready events exceeds the userspace limit, we need to splice the
1015 * "txlist" back inside ep->rdllist.
Davide Libenzi6192bd52007-05-08 00:25:41 -07001016 */
Davide Libenzid47de162007-05-15 01:40:41 -07001017 list_splice(&txlist, &ep->rdllist);
Davide Libenzi6192bd52007-05-08 00:25:41 -07001018
Davide Libenzid47de162007-05-15 01:40:41 -07001019 if (!list_empty(&ep->rdllist)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /*
Davide Libenzid47de162007-05-15 01:40:41 -07001021 * Wake up (if active) both the eventpoll wait list and the ->poll()
Davide Libenzi67647d02007-05-15 01:40:52 -07001022 * wait list (delayed after we release the lock).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
1024 if (waitqueue_active(&ep->wq))
Matthew Wilcox4a6e9e2c2007-08-30 16:10:22 -04001025 wake_up_locked(&ep->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (waitqueue_active(&ep->poll_wait))
1027 pwake++;
Davide Libenzi6192bd52007-05-08 00:25:41 -07001028 }
Davide Libenzic7ea7632007-05-15 01:40:47 -07001029 spin_unlock_irqrestore(&ep->lock, flags);
Davide Libenzid47de162007-05-15 01:40:41 -07001030
1031 mutex_unlock(&ep->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 /* We have to call this outside the lock */
1034 if (pwake)
1035 ep_poll_safewake(&psw, &ep->poll_wait);
Davide Libenzi6192bd52007-05-08 00:25:41 -07001036
1037 return eventcnt == 0 ? error: eventcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
1041 int maxevents, long timeout)
1042{
1043 int res, eavail;
1044 unsigned long flags;
1045 long jtimeout;
1046 wait_queue_t wait;
1047
1048 /*
1049 * Calculate the timeout by checking for the "infinite" value ( -1 )
1050 * and the overflow condition. The passed timeout is in milliseconds,
1051 * that why (t * HZ) / 1000.
1052 */
Davide Libenzie3306dd2005-09-27 21:45:33 -07001053 jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ?
1054 MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056retry:
Davide Libenzic7ea7632007-05-15 01:40:47 -07001057 spin_lock_irqsave(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 res = 0;
1060 if (list_empty(&ep->rdllist)) {
1061 /*
1062 * We don't have any available event to return to the caller.
1063 * We need to sleep here, and we will be wake up by
1064 * ep_poll_callback() when events will become available.
1065 */
1066 init_waitqueue_entry(&wait, current);
Davide Libenzid47de162007-05-15 01:40:41 -07001067 wait.flags |= WQ_FLAG_EXCLUSIVE;
Davide Libenzi3419b232006-06-25 05:48:14 -07001068 __add_wait_queue(&ep->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 for (;;) {
1071 /*
1072 * We don't want to sleep if the ep_poll_callback() sends us
1073 * a wakeup in between. That's why we set the task state
1074 * to TASK_INTERRUPTIBLE before doing the checks.
1075 */
1076 set_current_state(TASK_INTERRUPTIBLE);
1077 if (!list_empty(&ep->rdllist) || !jtimeout)
1078 break;
1079 if (signal_pending(current)) {
1080 res = -EINTR;
1081 break;
1082 }
1083
Davide Libenzic7ea7632007-05-15 01:40:47 -07001084 spin_unlock_irqrestore(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 jtimeout = schedule_timeout(jtimeout);
Davide Libenzic7ea7632007-05-15 01:40:47 -07001086 spin_lock_irqsave(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Davide Libenzi3419b232006-06-25 05:48:14 -07001088 __remove_wait_queue(&ep->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 set_current_state(TASK_RUNNING);
1091 }
1092
1093 /* Is it worth to try to dig for events ? */
1094 eavail = !list_empty(&ep->rdllist);
1095
Davide Libenzic7ea7632007-05-15 01:40:47 -07001096 spin_unlock_irqrestore(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 /*
1099 * Try to transfer events to user space. In case we get 0 events and
1100 * there's still timeout left over, we go trying again in search of
1101 * more luck.
1102 */
1103 if (!res && eavail &&
Davide Libenzid47de162007-05-15 01:40:41 -07001104 !(res = ep_send_events(ep, events, maxevents)) && jtimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 goto retry;
1106
1107 return res;
1108}
1109
Davide Libenzi7699acd2007-05-10 22:23:23 -07001110/*
Andrew Morton523723b2008-08-12 15:09:01 -07001111 * Open an eventpoll file descriptor.
Davide Libenzi7699acd2007-05-10 22:23:23 -07001112 */
Ulrich Drepper9fe5ad92008-07-23 21:29:43 -07001113asmlinkage long sys_epoll_create1(int flags)
Davide Libenzi7699acd2007-05-10 22:23:23 -07001114{
1115 int error, fd = -1;
1116 struct eventpoll *ep;
Davide Libenzi7699acd2007-05-10 22:23:23 -07001117
Ulrich Dreppere38b36f2008-07-23 21:29:42 -07001118 /* Check the EPOLL_* constant for consistency. */
1119 BUILD_BUG_ON(EPOLL_CLOEXEC != O_CLOEXEC);
1120
Ulrich Dreppera0998b52008-07-23 21:29:27 -07001121 if (flags & ~EPOLL_CLOEXEC)
1122 return -EINVAL;
1123
Davide Libenzi7699acd2007-05-10 22:23:23 -07001124 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
Ulrich Drepper9fe5ad92008-07-23 21:29:43 -07001125 current, flags));
Davide Libenzi7699acd2007-05-10 22:23:23 -07001126
1127 /*
Ulrich Drepper9fe5ad92008-07-23 21:29:43 -07001128 * Create the internal data structure ( "struct eventpoll" ).
Davide Libenzi7699acd2007-05-10 22:23:23 -07001129 */
Ulrich Drepper9fe5ad92008-07-23 21:29:43 -07001130 error = ep_alloc(&ep);
1131 if (error < 0) {
Al Viro2030a422008-02-23 06:46:49 -05001132 fd = error;
Davide Libenzi7699acd2007-05-10 22:23:23 -07001133 goto error_return;
Al Viro2030a422008-02-23 06:46:49 -05001134 }
Davide Libenzi7699acd2007-05-10 22:23:23 -07001135
1136 /*
1137 * Creates all the items needed to setup an eventpoll file. That is,
Al Viro2030a422008-02-23 06:46:49 -05001138 * a file structure and a free file descriptor.
Davide Libenzi7699acd2007-05-10 22:23:23 -07001139 */
Ulrich Dreppera0998b52008-07-23 21:29:27 -07001140 fd = anon_inode_getfd("[eventpoll]", &eventpoll_fops, ep,
1141 flags & O_CLOEXEC);
Al Viro2030a422008-02-23 06:46:49 -05001142 if (fd < 0)
1143 ep_free(ep);
Davide Libenzi7ef99642008-12-01 13:13:55 -08001144 atomic_inc(&ep->user->epoll_devs);
Davide Libenzi7699acd2007-05-10 22:23:23 -07001145
Al Viro2030a422008-02-23 06:46:49 -05001146error_return:
Davide Libenzi7699acd2007-05-10 22:23:23 -07001147 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
Ulrich Drepper9fe5ad92008-07-23 21:29:43 -07001148 current, flags, fd));
Davide Libenzi7699acd2007-05-10 22:23:23 -07001149
1150 return fd;
Davide Libenzi7699acd2007-05-10 22:23:23 -07001151}
1152
Ulrich Dreppera0998b52008-07-23 21:29:27 -07001153asmlinkage long sys_epoll_create(int size)
1154{
Ulrich Drepper9fe5ad92008-07-23 21:29:43 -07001155 if (size < 0)
1156 return -EINVAL;
1157
1158 return sys_epoll_create1(0);
Ulrich Dreppera0998b52008-07-23 21:29:27 -07001159}
1160
Davide Libenzi7699acd2007-05-10 22:23:23 -07001161/*
1162 * The following function implements the controller interface for
1163 * the eventpoll file that enables the insertion/removal/change of
Davide Libenzi67647d02007-05-15 01:40:52 -07001164 * file descriptors inside the interest set.
Davide Libenzi7699acd2007-05-10 22:23:23 -07001165 */
1166asmlinkage long sys_epoll_ctl(int epfd, int op, int fd,
1167 struct epoll_event __user *event)
1168{
1169 int error;
1170 struct file *file, *tfile;
1171 struct eventpoll *ep;
1172 struct epitem *epi;
1173 struct epoll_event epds;
1174
1175 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n",
1176 current, epfd, op, fd, event));
1177
1178 error = -EFAULT;
1179 if (ep_op_has_event(op) &&
1180 copy_from_user(&epds, event, sizeof(struct epoll_event)))
1181 goto error_return;
1182
1183 /* Get the "struct file *" for the eventpoll file */
1184 error = -EBADF;
1185 file = fget(epfd);
1186 if (!file)
1187 goto error_return;
1188
1189 /* Get the "struct file *" for the target file */
1190 tfile = fget(fd);
1191 if (!tfile)
1192 goto error_fput;
1193
1194 /* The target file descriptor must support poll */
1195 error = -EPERM;
1196 if (!tfile->f_op || !tfile->f_op->poll)
1197 goto error_tgt_fput;
1198
1199 /*
1200 * We have to check that the file structure underneath the file descriptor
1201 * the user passed to us _is_ an eventpoll file. And also we do not permit
1202 * adding an epoll file descriptor inside itself.
1203 */
1204 error = -EINVAL;
1205 if (file == tfile || !is_file_epoll(file))
1206 goto error_tgt_fput;
1207
1208 /*
1209 * At this point it is safe to assume that the "private_data" contains
1210 * our own data structure.
1211 */
1212 ep = file->private_data;
1213
Davide Libenzid47de162007-05-15 01:40:41 -07001214 mutex_lock(&ep->mtx);
Davide Libenzi7699acd2007-05-10 22:23:23 -07001215
Davide Libenzi67647d02007-05-15 01:40:52 -07001216 /*
1217 * Try to lookup the file inside our RB tree, Since we grabbed "mtx"
1218 * above, we can be sure to be able to use the item looked up by
1219 * ep_find() till we release the mutex.
1220 */
Davide Libenzi7699acd2007-05-10 22:23:23 -07001221 epi = ep_find(ep, tfile, fd);
1222
1223 error = -EINVAL;
1224 switch (op) {
1225 case EPOLL_CTL_ADD:
1226 if (!epi) {
1227 epds.events |= POLLERR | POLLHUP;
1228
1229 error = ep_insert(ep, &epds, tfile, fd);
1230 } else
1231 error = -EEXIST;
1232 break;
1233 case EPOLL_CTL_DEL:
1234 if (epi)
1235 error = ep_remove(ep, epi);
1236 else
1237 error = -ENOENT;
1238 break;
1239 case EPOLL_CTL_MOD:
1240 if (epi) {
1241 epds.events |= POLLERR | POLLHUP;
1242 error = ep_modify(ep, epi, &epds);
1243 } else
1244 error = -ENOENT;
1245 break;
1246 }
Davide Libenzid47de162007-05-15 01:40:41 -07001247 mutex_unlock(&ep->mtx);
Davide Libenzi7699acd2007-05-10 22:23:23 -07001248
1249error_tgt_fput:
1250 fput(tfile);
1251error_fput:
1252 fput(file);
1253error_return:
1254 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n",
1255 current, epfd, op, fd, event, error));
1256
1257 return error;
1258}
1259
1260/*
1261 * Implement the event wait interface for the eventpoll file. It is the kernel
1262 * part of the user space epoll_wait(2).
1263 */
1264asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
1265 int maxevents, int timeout)
1266{
1267 int error;
1268 struct file *file;
1269 struct eventpoll *ep;
1270
1271 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n",
1272 current, epfd, events, maxevents, timeout));
1273
1274 /* The maximum number of event must be greater than zero */
1275 if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)
1276 return -EINVAL;
1277
1278 /* Verify that the area passed by the user is writeable */
1279 if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
1280 error = -EFAULT;
1281 goto error_return;
1282 }
1283
1284 /* Get the "struct file *" for the eventpoll file */
1285 error = -EBADF;
1286 file = fget(epfd);
1287 if (!file)
1288 goto error_return;
1289
1290 /*
1291 * We have to check that the file structure underneath the fd
1292 * the user passed to us _is_ an eventpoll file.
1293 */
1294 error = -EINVAL;
1295 if (!is_file_epoll(file))
1296 goto error_fput;
1297
1298 /*
1299 * At this point it is safe to assume that the "private_data" contains
1300 * our own data structure.
1301 */
1302 ep = file->private_data;
1303
1304 /* Time to fish for events ... */
1305 error = ep_poll(ep, events, maxevents, timeout);
1306
1307error_fput:
1308 fput(file);
1309error_return:
1310 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n",
1311 current, epfd, events, maxevents, timeout, error));
1312
1313 return error;
1314}
1315
Roland McGrathf3de2722008-04-30 00:53:09 -07001316#ifdef HAVE_SET_RESTORE_SIGMASK
Davide Libenzi7699acd2007-05-10 22:23:23 -07001317
1318/*
1319 * Implement the event wait interface for the eventpoll file. It is the kernel
1320 * part of the user space epoll_pwait(2).
1321 */
1322asmlinkage long sys_epoll_pwait(int epfd, struct epoll_event __user *events,
1323 int maxevents, int timeout, const sigset_t __user *sigmask,
1324 size_t sigsetsize)
1325{
1326 int error;
1327 sigset_t ksigmask, sigsaved;
1328
1329 /*
1330 * If the caller wants a certain signal mask to be set during the wait,
1331 * we apply it here.
1332 */
1333 if (sigmask) {
1334 if (sigsetsize != sizeof(sigset_t))
1335 return -EINVAL;
1336 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
1337 return -EFAULT;
1338 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
1339 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1340 }
1341
1342 error = sys_epoll_wait(epfd, events, maxevents, timeout);
1343
1344 /*
1345 * If we changed the signal mask, we need to restore the original one.
1346 * In case we've got a signal while waiting, we do not restore the
1347 * signal mask yet, and we allow do_signal() to deliver the signal on
1348 * the way back to userspace, before the signal mask is restored.
1349 */
1350 if (sigmask) {
1351 if (error == -EINTR) {
1352 memcpy(&current->saved_sigmask, &sigsaved,
Davide Libenzic7ea7632007-05-15 01:40:47 -07001353 sizeof(sigsaved));
Roland McGrath4e4c22c2008-04-30 00:53:06 -07001354 set_restore_sigmask();
Davide Libenzi7699acd2007-05-10 22:23:23 -07001355 } else
1356 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1357 }
1358
1359 return error;
1360}
1361
Roland McGrathf3de2722008-04-30 00:53:09 -07001362#endif /* HAVE_SET_RESTORE_SIGMASK */
Davide Libenzi7699acd2007-05-10 22:23:23 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364static int __init eventpoll_init(void)
1365{
Davide Libenzi7ef99642008-12-01 13:13:55 -08001366 struct sysinfo si;
1367
1368 si_meminfo(&si);
1369 max_user_instances = 128;
1370 max_user_watches = (((si.totalram - si.totalhigh) / 32) << PAGE_SHIFT) /
1371 EP_ITEM_COST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /* Initialize the structure used to perform safe poll wait head wake ups */
1374 ep_poll_safewake_init(&psw);
1375
1376 /* Allocates slab cache used to allocate "struct epitem" items */
1377 epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
1378 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09001379 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 /* Allocates slab cache used to allocate "struct eppoll_entry" */
1382 pwq_cache = kmem_cache_create("eventpoll_pwq",
1383 sizeof(struct eppoll_entry), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001384 EPI_SLAB_DEBUG|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
Davide Libenzicea69242007-05-10 22:23:22 -07001388fs_initcall(eventpoll_init);