blob: 1dbedc71a28c8593d504c7b2835d70a047ffa42a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/eventpoll.c ( Efficent event polling implementation )
Davide Libenzi3419b232006-06-25 05:48:14 -07003 * Copyright (C) 2001,...,2006 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 Libenzid47de162007-05-15 01:40:41 -070047 * 2) ep->mtx (mutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * 3) ep->lock (rw_lock)
49 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105struct epoll_filefd {
106 struct file *file;
107 int fd;
108};
109
110/*
111 * Node that is linked into the "wake_task_list" member of the "struct poll_safewake".
112 * It is used to keep track on all tasks that are currently inside the wake_up() code
113 * to 1) short-circuit the one coming from the same task and same wait queue head
Davide Libenzid47de162007-05-15 01:40:41 -0700114 * (loop) 2) allow a maximum number of epoll descriptors inclusion nesting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * 3) let go the ones coming from other tasks.
116 */
117struct wake_task_node {
118 struct list_head llink;
Ingo Molnar36c8b582006-07-03 00:25:41 -0700119 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 wait_queue_head_t *wq;
121};
122
123/*
124 * This is used to implement the safe poll wake up avoiding to reenter
125 * the poll callback from inside wake_up().
126 */
127struct poll_safewake {
128 struct list_head wake_task_list;
129 spinlock_t lock;
130};
131
132/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * Each file descriptor added to the eventpoll interface will
Davide Libenzi6192bd52007-05-08 00:25:41 -0700134 * have an entry of this type linked to the "rbr" RB tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
136struct epitem {
137 /* RB-Tree node used to link this structure to the eventpoll rb-tree */
138 struct rb_node rbn;
139
140 /* List header used to link this structure to the eventpoll ready list */
141 struct list_head rdllink;
142
143 /* The file descriptor information this item refers to */
144 struct epoll_filefd ffd;
145
146 /* Number of active wait queue attached to poll operations */
147 int nwait;
148
149 /* List containing poll wait queues */
150 struct list_head pwqlist;
151
152 /* The "container" of this item */
153 struct eventpoll *ep;
154
155 /* The structure that describe the interested events and the source fd */
156 struct epoll_event event;
157
158 /*
159 * Used to keep track of the usage count of the structure. This avoids
160 * that the structure will desappear from underneath our processing.
161 */
162 atomic_t usecnt;
163
164 /* List header used to link this item to the "struct file" items list */
165 struct list_head fllink;
Davide Libenzid47de162007-05-15 01:40:41 -0700166
167 /*
168 * Works together "struct eventpoll"->ovflist in keeping the
169 * single linked chain of items.
170 */
171 struct epitem *next;
172};
173
174/*
175 * This structure is stored inside the "private_data" member of the file
176 * structure and rapresent the main data sructure for the eventpoll
177 * interface.
178 */
179struct eventpoll {
180 /* Protect the this structure access */
181 rwlock_t lock;
182
183 /*
184 * This mutex is used to ensure that files are not removed
185 * while epoll is using them. This is held during the event
186 * collection loop, the file cleanup path, the epoll file exit
187 * code and the ctl operations.
188 */
189 struct mutex mtx;
190
191 /* Wait queue used by sys_epoll_wait() */
192 wait_queue_head_t wq;
193
194 /* Wait queue used by file->poll() */
195 wait_queue_head_t poll_wait;
196
197 /* List of ready file descriptors */
198 struct list_head rdllist;
199
200 /* RB-Tree root used to store monitored fd structs */
201 struct rb_root rbr;
202
203 /*
204 * This is a single linked list that chains all the "struct epitem" that
205 * happened while transfering ready events to userspace w/out
206 * holding ->lock.
207 */
208 struct epitem *ovflist;
209};
210
211/* Wait structure used by the poll hooks */
212struct eppoll_entry {
213 /* List header used to link this structure to the "struct epitem" */
214 struct list_head llink;
215
216 /* The "base" pointer is set to the container "struct epitem" */
217 void *base;
218
219 /*
220 * Wait queue item that will be linked to the target file wait
221 * queue head.
222 */
223 wait_queue_t wait;
224
225 /* The wait queue head that linked the "wait" wait queue item */
226 wait_queue_head_t *whead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
229/* Wrapper struct used by poll queueing */
230struct ep_pqueue {
231 poll_table pt;
232 struct epitem *epi;
233};
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
Davide Libenzid47de162007-05-15 01:40:41 -0700236 * This mutex is used to serialize ep_free() and eventpoll_release_file().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
Arjan van de Ven144efe32006-03-23 03:00:32 -0800238static struct mutex epmutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240/* Safe wake up implementation */
241static struct poll_safewake psw;
242
243/* Slab cache used to allocate "struct epitem" */
Christoph Lametere18b8902006-12-06 20:33:20 -0800244static struct kmem_cache *epi_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246/* Slab cache used to allocate "struct eppoll_entry" */
Christoph Lametere18b8902006-12-06 20:33:20 -0800247static struct kmem_cache *pwq_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700249
250/* Setup the structure that is used as key for the rb-tree */
251static inline void ep_set_ffd(struct epoll_filefd *ffd,
252 struct file *file, int fd)
253{
254 ffd->file = file;
255 ffd->fd = fd;
256}
257
258/* Compare rb-tree keys */
259static inline int ep_cmp_ffd(struct epoll_filefd *p1,
260 struct epoll_filefd *p2)
261{
262 return (p1->file > p2->file ? +1:
263 (p1->file < p2->file ? -1 : p1->fd - p2->fd));
264}
265
266/* Special initialization for the rb-tree node to detect linkage */
267static inline void ep_rb_initnode(struct rb_node *n)
268{
David Woodhousec5698822006-04-21 13:17:24 +0100269 rb_set_parent(n, n);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700270}
271
272/* Removes a node from the rb-tree and marks it for a fast is-linked check */
273static inline void ep_rb_erase(struct rb_node *n, struct rb_root *r)
274{
275 rb_erase(n, r);
David Woodhousec5698822006-04-21 13:17:24 +0100276 rb_set_parent(n, n);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700277}
278
279/* Fast check to verify that the item is linked to the main rb-tree */
280static inline int ep_rb_linked(struct rb_node *n)
281{
David Woodhousec5698822006-04-21 13:17:24 +0100282 return rb_parent(n) != n;
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700283}
284
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700285/* Tells us if the item is currently linked */
286static inline int ep_is_linked(struct list_head *p)
287{
288 return !list_empty(p);
289}
290
291/* Get the "struct epitem" from a wait queue pointer */
292static inline struct epitem * ep_item_from_wait(wait_queue_t *p)
293{
294 return container_of(p, struct eppoll_entry, wait)->base;
295}
296
297/* Get the "struct epitem" from an epoll queue wrapper */
298static inline struct epitem * ep_item_from_epqueue(poll_table *p)
299{
300 return container_of(p, struct ep_pqueue, pt)->epi;
301}
302
303/* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
Davide Libenzi6192bd52007-05-08 00:25:41 -0700304static inline int ep_op_has_event(int op)
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700305{
306 return op != EPOLL_CTL_DEL;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/* Initialize the poll safe wake up structure */
310static void ep_poll_safewake_init(struct poll_safewake *psw)
311{
312
313 INIT_LIST_HEAD(&psw->wake_task_list);
314 spin_lock_init(&psw->lock);
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
318 * Perform a safe wake up of the poll wait list. The problem is that
319 * with the new callback'd wake up system, it is possible that the
320 * poll callback is reentered from inside the call to wake_up() done
321 * on the poll wait queue head. The rule is that we cannot reenter the
322 * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times,
323 * and we cannot reenter the same wait queue head at all. This will
324 * enable to have a hierarchy of epoll file descriptor of no more than
325 * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock
326 * because this one gets called by the poll callback, that in turn is called
327 * from inside a wake_up(), that might be called from irq context.
328 */
329static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
330{
331 int wake_nests = 0;
332 unsigned long flags;
Ingo Molnar36c8b582006-07-03 00:25:41 -0700333 struct task_struct *this_task = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct list_head *lsthead = &psw->wake_task_list, *lnk;
335 struct wake_task_node *tncur;
336 struct wake_task_node tnode;
337
338 spin_lock_irqsave(&psw->lock, flags);
339
340 /* Try to see if the current task is already inside this wakeup call */
341 list_for_each(lnk, lsthead) {
342 tncur = list_entry(lnk, struct wake_task_node, llink);
343
344 if (tncur->wq == wq ||
345 (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) {
346 /*
347 * Ops ... loop detected or maximum nest level reached.
348 * We abort this wake by breaking the cycle itself.
349 */
350 spin_unlock_irqrestore(&psw->lock, flags);
351 return;
352 }
353 }
354
355 /* Add the current task to the list */
356 tnode.task = this_task;
357 tnode.wq = wq;
358 list_add(&tnode.llink, lsthead);
359
360 spin_unlock_irqrestore(&psw->lock, flags);
361
362 /* Do really wake up now */
363 wake_up(wq);
364
365 /* Remove the current task from the list */
366 spin_lock_irqsave(&psw->lock, flags);
367 list_del(&tnode.llink);
368 spin_unlock_irqrestore(&psw->lock, flags);
369}
370
Davide Libenzi7699acd2007-05-10 22:23:23 -0700371/*
372 * This function unregister poll callbacks from the associated file descriptor.
373 * Since this must be called without holding "ep->lock" the atomic exchange trick
374 * will protect us from multiple unregister.
375 */
376static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
377{
378 int nwait;
379 struct list_head *lsthead = &epi->pwqlist;
380 struct eppoll_entry *pwq;
381
382 /* This is called without locks, so we need the atomic exchange */
383 nwait = xchg(&epi->nwait, 0);
384
385 if (nwait) {
386 while (!list_empty(lsthead)) {
387 pwq = list_first_entry(lsthead, struct eppoll_entry, llink);
388
389 list_del_init(&pwq->llink);
390 remove_wait_queue(pwq->whead, &pwq->wait);
391 kmem_cache_free(pwq_cache, pwq);
392 }
393 }
394}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*
Davide Libenzi7699acd2007-05-10 22:23:23 -0700397 * Unlink the "struct epitem" from all places it might have been hooked up.
398 * This function must be called with write IRQ lock on "ep->lock".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Davide Libenzi7699acd2007-05-10 22:23:23 -0700400static int ep_unlink(struct eventpoll *ep, struct epitem *epi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Davide Libenzi7699acd2007-05-10 22:23:23 -0700402 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /*
Davide Libenzi7699acd2007-05-10 22:23:23 -0700405 * It can happen that this one is called for an item already unlinked.
406 * The check protect us from doing a double unlink ( crash ).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 */
Davide Libenzi7699acd2007-05-10 22:23:23 -0700408 error = -ENOENT;
409 if (!ep_rb_linked(&epi->rbn))
410 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Davide Libenzi7699acd2007-05-10 22:23:23 -0700412 /*
413 * Clear the event mask for the unlinked item. This will avoid item
414 * notifications to be sent after the unlink operation from inside
415 * the kernel->userspace event transfer loop.
416 */
417 epi->event.events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Davide Libenzi7699acd2007-05-10 22:23:23 -0700419 /*
420 * At this point is safe to do the job, unlink the item from our rb-tree.
421 * This operation togheter with the above check closes the door to
422 * double unlinks.
423 */
424 ep_rb_erase(&epi->rbn, &ep->rbr);
425
426 /*
427 * If the item we are going to remove is inside the ready file descriptors
428 * we want to remove it from this list to avoid stale events.
429 */
430 if (ep_is_linked(&epi->rdllink))
431 list_del_init(&epi->rdllink);
432
433 error = 0;
434error_return:
435
436 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n",
437 current, ep, epi->ffd.file, error));
438
439 return error;
440}
441
442/*
443 * Increment the usage count of the "struct epitem" making it sure
444 * that the user will have a valid pointer to reference.
445 */
446static void ep_use_epitem(struct epitem *epi)
447{
448 atomic_inc(&epi->usecnt);
449}
450
451/*
452 * Decrement ( release ) the usage count by signaling that the user
453 * has finished using the structure. It might lead to freeing the
454 * structure itself if the count goes to zero.
455 */
456static void ep_release_epitem(struct epitem *epi)
457{
458 if (atomic_dec_and_test(&epi->usecnt))
459 kmem_cache_free(epi_cache, epi);
460}
461
462/*
463 * Removes a "struct epitem" from the eventpoll RB tree and deallocates
464 * all the associated resources.
465 */
466static int ep_remove(struct eventpoll *ep, struct epitem *epi)
467{
468 int error;
469 unsigned long flags;
470 struct file *file = epi->ffd.file;
471
472 /*
473 * Removes poll wait queue hooks. We _have_ to do this without holding
474 * the "ep->lock" otherwise a deadlock might occur. This because of the
475 * sequence of the lock acquisition. Here we do "ep->lock" then the wait
476 * queue head lock when unregistering the wait queue. The wakeup callback
477 * will run by holding the wait queue head lock and will call our callback
478 * that will try to get "ep->lock".
479 */
480 ep_unregister_pollwait(ep, epi);
481
482 /* Remove the current item from the list of epoll hooks */
483 spin_lock(&file->f_ep_lock);
484 if (ep_is_linked(&epi->fllink))
Davide Libenzi6192bd52007-05-08 00:25:41 -0700485 list_del_init(&epi->fllink);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700486 spin_unlock(&file->f_ep_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Davide Libenzi7699acd2007-05-10 22:23:23 -0700488 /* We need to acquire the write IRQ lock before calling ep_unlink() */
489 write_lock_irqsave(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Davide Libenzi7699acd2007-05-10 22:23:23 -0700491 /* Really unlink the item from the RB tree */
492 error = ep_unlink(ep, epi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Davide Libenzi7699acd2007-05-10 22:23:23 -0700494 write_unlock_irqrestore(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (error)
Davide Libenzi7699acd2007-05-10 22:23:23 -0700497 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Davide Libenzi7699acd2007-05-10 22:23:23 -0700499 /* At this point it is safe to free the eventpoll item */
500 ep_release_epitem(epi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Davide Libenzi7699acd2007-05-10 22:23:23 -0700502 error = 0;
503error_return:
504 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p) = %d\n",
505 current, ep, file, error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 return error;
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static void ep_free(struct eventpoll *ep)
511{
512 struct rb_node *rbp;
513 struct epitem *epi;
514
515 /* We need to release all tasks waiting for these file */
516 if (waitqueue_active(&ep->poll_wait))
517 ep_poll_safewake(&psw, &ep->poll_wait);
518
519 /*
520 * We need to lock this because we could be hit by
521 * eventpoll_release_file() while we're freeing the "struct eventpoll".
Davide Libenzid47de162007-05-15 01:40:41 -0700522 * We do not need to hold "ep->mtx" here because the epoll file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * is on the way to be removed and no one has references to it
524 * anymore. The only hit might come from eventpoll_release_file() but
Arjan van de Ven144efe32006-03-23 03:00:32 -0800525 * holding "epmutex" is sufficent here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
Arjan van de Ven144efe32006-03-23 03:00:32 -0800527 mutex_lock(&epmutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /*
530 * Walks through the whole tree by unregistering poll callbacks.
531 */
532 for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
533 epi = rb_entry(rbp, struct epitem, rbn);
534
535 ep_unregister_pollwait(ep, epi);
536 }
537
538 /*
Davide Libenzi6192bd52007-05-08 00:25:41 -0700539 * Walks through the whole tree by freeing each "struct epitem". At this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * point we are sure no poll callbacks will be lingering around, and also by
Davide Libenzid47de162007-05-15 01:40:41 -0700541 * holding "epmutex" we can be sure that no file cleanup code will hit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * us during this operation. So we can avoid the lock on "ep->lock".
543 */
544 while ((rbp = rb_first(&ep->rbr)) != 0) {
545 epi = rb_entry(rbp, struct epitem, rbn);
546 ep_remove(ep, epi);
547 }
548
Arjan van de Ven144efe32006-03-23 03:00:32 -0800549 mutex_unlock(&epmutex);
Davide Libenzid47de162007-05-15 01:40:41 -0700550
551 mutex_destroy(&ep->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Davide Libenzi7699acd2007-05-10 22:23:23 -0700554static int ep_eventpoll_release(struct inode *inode, struct file *file)
555{
556 struct eventpoll *ep = file->private_data;
557
558 if (ep) {
559 ep_free(ep);
560 kfree(ep);
561 }
562
563 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep));
564 return 0;
565}
566
567static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
568{
569 unsigned int pollflags = 0;
570 unsigned long flags;
571 struct eventpoll *ep = file->private_data;
572
573 /* Insert inside our poll wait queue */
574 poll_wait(file, &ep->poll_wait, wait);
575
576 /* Check our condition */
577 read_lock_irqsave(&ep->lock, flags);
578 if (!list_empty(&ep->rdllist))
579 pollflags = POLLIN | POLLRDNORM;
580 read_unlock_irqrestore(&ep->lock, flags);
581
582 return pollflags;
583}
584
585/* File callbacks that implement the eventpoll file behaviour */
586static const struct file_operations eventpoll_fops = {
587 .release = ep_eventpoll_release,
588 .poll = ep_eventpoll_poll
589};
590
591/* Fast test to see if the file is an evenpoll file */
592static inline int is_file_epoll(struct file *f)
593{
594 return f->f_op == &eventpoll_fops;
595}
596
597/*
598 * This is called from eventpoll_release() to unlink files from the eventpoll
599 * interface. We need to have this facility to cleanup correctly files that are
600 * closed without being removed from the eventpoll interface.
601 */
602void eventpoll_release_file(struct file *file)
603{
604 struct list_head *lsthead = &file->f_ep_links;
605 struct eventpoll *ep;
606 struct epitem *epi;
607
608 /*
609 * We don't want to get "file->f_ep_lock" because it is not
610 * necessary. It is not necessary because we're in the "struct file"
611 * cleanup path, and this means that noone is using this file anymore.
Davide Libenzid47de162007-05-15 01:40:41 -0700612 * The only hit might come from ep_free() but by holding the mutex
Davide Libenzi7699acd2007-05-10 22:23:23 -0700613 * will correctly serialize the operation. We do need to acquire
Davide Libenzid47de162007-05-15 01:40:41 -0700614 * "ep->mtx" after "epmutex" because ep_remove() requires it when called
Davide Libenzi7699acd2007-05-10 22:23:23 -0700615 * from anywhere but ep_free().
616 */
617 mutex_lock(&epmutex);
618
619 while (!list_empty(lsthead)) {
620 epi = list_first_entry(lsthead, struct epitem, fllink);
621
622 ep = epi->ep;
623 list_del_init(&epi->fllink);
Davide Libenzid47de162007-05-15 01:40:41 -0700624 mutex_lock(&ep->mtx);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700625 ep_remove(ep, epi);
Davide Libenzid47de162007-05-15 01:40:41 -0700626 mutex_unlock(&ep->mtx);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700627 }
628
629 mutex_unlock(&epmutex);
630}
631
632static int ep_alloc(struct eventpoll **pep)
633{
634 struct eventpoll *ep = kzalloc(sizeof(*ep), GFP_KERNEL);
635
636 if (!ep)
637 return -ENOMEM;
638
639 rwlock_init(&ep->lock);
Davide Libenzid47de162007-05-15 01:40:41 -0700640 mutex_init(&ep->mtx);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700641 init_waitqueue_head(&ep->wq);
642 init_waitqueue_head(&ep->poll_wait);
643 INIT_LIST_HEAD(&ep->rdllist);
644 ep->rbr = RB_ROOT;
Davide Libenzid47de162007-05-15 01:40:41 -0700645 ep->ovflist = EP_UNACTIVE_PTR;
Davide Libenzi7699acd2007-05-10 22:23:23 -0700646
647 *pep = ep;
648
649 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n",
650 current, ep));
651 return 0;
652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654/*
Davide Libenzi6192bd52007-05-08 00:25:41 -0700655 * Search the file inside the eventpoll tree. It add usage count to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * the returned item, so the caller must call ep_release_epitem()
657 * after finished using the "struct epitem".
658 */
659static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
660{
661 int kcmp;
662 unsigned long flags;
663 struct rb_node *rbp;
664 struct epitem *epi, *epir = NULL;
665 struct epoll_filefd ffd;
666
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700667 ep_set_ffd(&ffd, file, fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 read_lock_irqsave(&ep->lock, flags);
669 for (rbp = ep->rbr.rb_node; rbp; ) {
670 epi = rb_entry(rbp, struct epitem, rbn);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700671 kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (kcmp > 0)
673 rbp = rbp->rb_right;
674 else if (kcmp < 0)
675 rbp = rbp->rb_left;
676 else {
677 ep_use_epitem(epi);
678 epir = epi;
679 break;
680 }
681 }
682 read_unlock_irqrestore(&ep->lock, flags);
683
684 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n",
685 current, file, epir));
686
687 return epir;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/*
Davide Libenzi7699acd2007-05-10 22:23:23 -0700691 * This is the callback that is passed to the wait queue wakeup
692 * machanism. It is called by the stored file descriptors when they
693 * have events to report.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
Davide Libenzi7699acd2007-05-10 22:23:23 -0700695static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Davide Libenzi7699acd2007-05-10 22:23:23 -0700697 int pwake = 0;
698 unsigned long flags;
699 struct epitem *epi = ep_item_from_wait(wait);
700 struct eventpoll *ep = epi->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Davide Libenzi7699acd2007-05-10 22:23:23 -0700702 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
703 current, epi->ffd.file, epi, ep));
704
705 write_lock_irqsave(&ep->lock, flags);
706
707 /*
708 * If the event mask does not contain any poll(2) event, we consider the
709 * descriptor to be disabled. This condition is likely the effect of the
710 * EPOLLONESHOT bit that disables the descriptor when an event is received,
711 * until the next EPOLL_CTL_MOD will be issued.
712 */
713 if (!(epi->event.events & ~EP_PRIVATE_BITS))
Davide Libenzid47de162007-05-15 01:40:41 -0700714 goto out_unlock;
715
716 /*
717 * If we are trasfering events to userspace, we can hold no locks
718 * (because we're accessing user memory, and because of linux f_op->poll()
719 * semantics). All the events that happens during that period of time are
720 * chained in ep->ovflist and requeued later on.
721 */
722 if (unlikely(ep->ovflist != EP_UNACTIVE_PTR)) {
723 if (epi->next == EP_UNACTIVE_PTR) {
724 epi->next = ep->ovflist;
725 ep->ovflist = epi;
726 }
727 goto out_unlock;
728 }
Davide Libenzi7699acd2007-05-10 22:23:23 -0700729
730 /* If this file is already in the ready list we exit soon */
731 if (ep_is_linked(&epi->rdllink))
732 goto is_linked;
733
734 list_add_tail(&epi->rdllink, &ep->rdllist);
735
736is_linked:
737 /*
738 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
739 * wait list.
740 */
741 if (waitqueue_active(&ep->wq))
742 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
743 TASK_INTERRUPTIBLE);
744 if (waitqueue_active(&ep->poll_wait))
745 pwake++;
746
Davide Libenzid47de162007-05-15 01:40:41 -0700747out_unlock:
Davide Libenzi7699acd2007-05-10 22:23:23 -0700748 write_unlock_irqrestore(&ep->lock, flags);
749
750 /* We have to call this outside the lock */
751 if (pwake)
752 ep_poll_safewake(&psw, &ep->poll_wait);
753
754 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*
758 * This is the callback that is used to add our wait queue to the
759 * target file wakeup lists.
760 */
761static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
762 poll_table *pt)
763{
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700764 struct epitem *epi = ep_item_from_epqueue(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct eppoll_entry *pwq;
766
Christoph Lametere94b1762006-12-06 20:33:17 -0800767 if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
769 pwq->whead = whead;
770 pwq->base = epi;
771 add_wait_queue(whead, &pwq->wait);
772 list_add_tail(&pwq->llink, &epi->pwqlist);
773 epi->nwait++;
774 } else {
775 /* We have to signal that an error occurred */
776 epi->nwait = -1;
777 }
778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
781{
782 int kcmp;
783 struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
784 struct epitem *epic;
785
786 while (*p) {
787 parent = *p;
788 epic = rb_entry(parent, struct epitem, rbn);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700789 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (kcmp > 0)
791 p = &parent->rb_right;
792 else
793 p = &parent->rb_left;
794 }
795 rb_link_node(&epi->rbn, parent, p);
796 rb_insert_color(&epi->rbn, &ep->rbr);
797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
800 struct file *tfile, int fd)
801{
802 int error, revents, pwake = 0;
803 unsigned long flags;
804 struct epitem *epi;
805 struct ep_pqueue epq;
806
807 error = -ENOMEM;
Christoph Lametere94b1762006-12-06 20:33:17 -0800808 if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
Davide Libenzi7699acd2007-05-10 22:23:23 -0700809 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* Item initialization follow here ... */
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700812 ep_rb_initnode(&epi->rbn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 INIT_LIST_HEAD(&epi->rdllink);
814 INIT_LIST_HEAD(&epi->fllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 INIT_LIST_HEAD(&epi->pwqlist);
816 epi->ep = ep;
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700817 ep_set_ffd(&epi->ffd, tfile, fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 epi->event = *event;
819 atomic_set(&epi->usecnt, 1);
820 epi->nwait = 0;
Davide Libenzid47de162007-05-15 01:40:41 -0700821 epi->next = EP_UNACTIVE_PTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* Initialize the poll table using the queue callback */
824 epq.epi = epi;
825 init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
826
827 /*
828 * Attach the item to the poll hooks and get current event bits.
829 * We can safely use the file* here because its usage count has
830 * been increased by the caller of this function.
831 */
832 revents = tfile->f_op->poll(tfile, &epq.pt);
833
834 /*
835 * We have to check if something went wrong during the poll wait queue
836 * install process. Namely an allocation for a wait queue failed due
837 * high memory pressure.
838 */
839 if (epi->nwait < 0)
Davide Libenzi7699acd2007-05-10 22:23:23 -0700840 goto error_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* Add the current item to the list of active epoll hook for this file */
843 spin_lock(&tfile->f_ep_lock);
844 list_add_tail(&epi->fllink, &tfile->f_ep_links);
845 spin_unlock(&tfile->f_ep_lock);
846
847 /* We have to drop the new item inside our item list to keep track of it */
848 write_lock_irqsave(&ep->lock, flags);
849
850 /* Add the current item to the rb-tree */
851 ep_rbtree_insert(ep, epi);
852
853 /* If the file is already "ready" we drop it inside the ready list */
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700854 if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 list_add_tail(&epi->rdllink, &ep->rdllist);
856
857 /* Notify waiting tasks that events are available */
858 if (waitqueue_active(&ep->wq))
Davide Libenzi3419b232006-06-25 05:48:14 -0700859 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (waitqueue_active(&ep->poll_wait))
861 pwake++;
862 }
863
864 write_unlock_irqrestore(&ep->lock, flags);
865
866 /* We have to call this outside the lock */
867 if (pwake)
868 ep_poll_safewake(&psw, &ep->poll_wait);
869
870 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n",
871 current, ep, tfile, fd));
872
873 return 0;
874
Davide Libenzi7699acd2007-05-10 22:23:23 -0700875error_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ep_unregister_pollwait(ep, epi);
877
878 /*
879 * We need to do this because an event could have been arrived on some
880 * allocated wait queue.
881 */
882 write_lock_irqsave(&ep->lock, flags);
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700883 if (ep_is_linked(&epi->rdllink))
Davide Libenzi6192bd52007-05-08 00:25:41 -0700884 list_del_init(&epi->rdllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 write_unlock_irqrestore(&ep->lock, flags);
886
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700887 kmem_cache_free(epi_cache, epi);
Davide Libenzi7699acd2007-05-10 22:23:23 -0700888error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return error;
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
893 * Modify the interest event mask by dropping an event if the new mask
894 * has a match in the current file status.
895 */
896static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
897{
898 int pwake = 0;
899 unsigned int revents;
900 unsigned long flags;
901
902 /*
903 * Set the new event interest mask before calling f_op->poll(), otherwise
904 * a potential race might occur. In fact if we do this operation inside
905 * the lock, an event might happen between the f_op->poll() call and the
906 * new event set registering.
907 */
908 epi->event.events = event->events;
909
910 /*
911 * Get current event bits. We can safely use the file* here because
912 * its usage count has been increased by the caller of this function.
913 */
914 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
915
916 write_lock_irqsave(&ep->lock, flags);
917
918 /* Copy the data member from inside the lock */
919 epi->event.data = event->data;
920
921 /*
Davide Libenzi6192bd52007-05-08 00:25:41 -0700922 * If the item is not linked to the RB tree it means that it's on its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * way toward the removal. Do nothing in this case.
924 */
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700925 if (ep_rb_linked(&epi->rbn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
927 * If the item is "hot" and it is not registered inside the ready
928 * list, push it inside. If the item is not "hot" and it is currently
929 * registered inside the ready list, unlink it.
930 */
931 if (revents & event->events) {
Pekka Enbergb030a4d2005-06-23 00:10:03 -0700932 if (!ep_is_linked(&epi->rdllink)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 list_add_tail(&epi->rdllink, &ep->rdllist);
934
935 /* Notify waiting tasks that events are available */
936 if (waitqueue_active(&ep->wq))
Davide Libenzi3419b232006-06-25 05:48:14 -0700937 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
938 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (waitqueue_active(&ep->poll_wait))
940 pwake++;
941 }
942 }
943 }
944
945 write_unlock_irqrestore(&ep->lock, flags);
946
947 /* We have to call this outside the lock */
948 if (pwake)
949 ep_poll_safewake(&psw, &ep->poll_wait);
950
951 return 0;
952}
953
Davide Libenzid47de162007-05-15 01:40:41 -0700954static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *events,
955 int maxevents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Davide Libenzi6192bd52007-05-08 00:25:41 -0700957 int eventcnt, error = -EFAULT, pwake = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 unsigned int revents;
Davide Libenzi6192bd52007-05-08 00:25:41 -0700959 unsigned long flags;
Davide Libenzid47de162007-05-15 01:40:41 -0700960 struct epitem *epi, *nepi;
961 struct list_head txlist;
Davide Libenzi6192bd52007-05-08 00:25:41 -0700962
Davide Libenzid47de162007-05-15 01:40:41 -0700963 INIT_LIST_HEAD(&txlist);
964
965 /*
966 * We need to lock this because we could be hit by
967 * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
968 */
969 mutex_lock(&ep->mtx);
970
971 /*
972 * Steal the ready list, and re-init the original one to the
973 * empty list. Also, set ep->ovflist to NULL so that events
974 * happening while looping w/out locks, are not lost. We cannot
975 * have the poll callback to queue directly on ep->rdllist,
976 * because we are doing it in the loop below, in a lockless way.
977 */
978 write_lock_irqsave(&ep->lock, flags);
979 list_splice(&ep->rdllist, &txlist);
980 INIT_LIST_HEAD(&ep->rdllist);
981 ep->ovflist = NULL;
982 write_unlock_irqrestore(&ep->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 /*
985 * We can loop without lock because this is a task private list.
Davide Libenzi6192bd52007-05-08 00:25:41 -0700986 * We just splice'd out the ep->rdllist in ep_collect_ready_items().
Davide Libenzid47de162007-05-15 01:40:41 -0700987 * Items cannot vanish during the loop because we are holding "mtx".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
Davide Libenzid47de162007-05-15 01:40:41 -0700989 for (eventcnt = 0; !list_empty(&txlist) && eventcnt < maxevents;) {
990 epi = list_first_entry(&txlist, struct epitem, rdllink);
991
992 list_del_init(&epi->rdllink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /*
995 * Get the ready file event set. We can safely use the file
Davide Libenzid47de162007-05-15 01:40:41 -0700996 * because we are holding the "mtx" and this will guarantee
997 * that both the file and the item will not vanish.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
999 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
Davide Libenzi6192bd52007-05-08 00:25:41 -07001000 revents &= epi->event.events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /*
Davide Libenzi6192bd52007-05-08 00:25:41 -07001003 * Is the event mask intersect the caller-requested one,
1004 * deliver the event to userspace. Again, we are holding
Davide Libenzid47de162007-05-15 01:40:41 -07001005 * "mtx", so no operations coming from userspace can change
1006 * the item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
Davide Libenzi6192bd52007-05-08 00:25:41 -07001008 if (revents) {
1009 if (__put_user(revents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 &events[eventcnt].events) ||
1011 __put_user(epi->event.data,
1012 &events[eventcnt].data))
Davide Libenzi6192bd52007-05-08 00:25:41 -07001013 goto errxit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (epi->event.events & EPOLLONESHOT)
1015 epi->event.events &= EP_PRIVATE_BITS;
1016 eventcnt++;
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /*
Davide Libenzid47de162007-05-15 01:40:41 -07001019 * At this point, noone can insert into ep->rdllist besides
1020 * us. The epoll_ctl() callers are locked out by us holding
1021 * "mtx" and the poll callback will queue them in ep->ovflist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
Davide Libenzi6192bd52007-05-08 00:25:41 -07001023 if (!(epi->event.events & EPOLLET) &&
Davide Libenzid47de162007-05-15 01:40:41 -07001024 (revents & epi->event.events))
1025 list_add_tail(&epi->rdllink, &ep->rdllist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Davide Libenzi6192bd52007-05-08 00:25:41 -07001027 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Davide Libenzid47de162007-05-15 01:40:41 -07001029errxit:
1030
1031 write_lock_irqsave(&ep->lock, flags);
1032 /*
1033 * During the time we spent in the loop above, some other events
1034 * might have been queued by the poll callback. We re-insert them
1035 * here (in case they are not already queued, or they're one-shot).
1036 */
1037 for (nepi = ep->ovflist; (epi = nepi) != NULL;
1038 nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
1039 if (!ep_is_linked(&epi->rdllink) &&
1040 (epi->event.events & ~EP_PRIVATE_BITS))
1041 list_add_tail(&epi->rdllink, &ep->rdllist);
1042 }
1043 /*
1044 * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
1045 * releasing the lock, events will be queued in the normal way inside
1046 * ep->rdllist.
1047 */
1048 ep->ovflist = EP_UNACTIVE_PTR;
Davide Libenzi6192bd52007-05-08 00:25:41 -07001049
1050 /*
Davide Libenzid47de162007-05-15 01:40:41 -07001051 * In case of error in the event-send loop, we might still have items
1052 * inside the "txlist". We need to splice them back inside ep->rdllist.
Davide Libenzi6192bd52007-05-08 00:25:41 -07001053 */
Davide Libenzid47de162007-05-15 01:40:41 -07001054 list_splice(&txlist, &ep->rdllist);
Davide Libenzi6192bd52007-05-08 00:25:41 -07001055
Davide Libenzid47de162007-05-15 01:40:41 -07001056 if (!list_empty(&ep->rdllist)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /*
Davide Libenzid47de162007-05-15 01:40:41 -07001058 * Wake up (if active) both the eventpoll wait list and the ->poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 * wait list.
1060 */
1061 if (waitqueue_active(&ep->wq))
Davide Libenzi3419b232006-06-25 05:48:14 -07001062 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
1063 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (waitqueue_active(&ep->poll_wait))
1065 pwake++;
Davide Libenzi6192bd52007-05-08 00:25:41 -07001066 }
Davide Libenzid47de162007-05-15 01:40:41 -07001067 write_unlock_irqrestore(&ep->lock, flags);
1068
1069 mutex_unlock(&ep->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /* We have to call this outside the lock */
1072 if (pwake)
1073 ep_poll_safewake(&psw, &ep->poll_wait);
Davide Libenzi6192bd52007-05-08 00:25:41 -07001074
1075 return eventcnt == 0 ? error: eventcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
1079 int maxevents, long timeout)
1080{
1081 int res, eavail;
1082 unsigned long flags;
1083 long jtimeout;
1084 wait_queue_t wait;
1085
1086 /*
1087 * Calculate the timeout by checking for the "infinite" value ( -1 )
1088 * and the overflow condition. The passed timeout is in milliseconds,
1089 * that why (t * HZ) / 1000.
1090 */
Davide Libenzie3306dd2005-09-27 21:45:33 -07001091 jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ?
1092 MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094retry:
1095 write_lock_irqsave(&ep->lock, flags);
1096
1097 res = 0;
1098 if (list_empty(&ep->rdllist)) {
1099 /*
1100 * We don't have any available event to return to the caller.
1101 * We need to sleep here, and we will be wake up by
1102 * ep_poll_callback() when events will become available.
1103 */
1104 init_waitqueue_entry(&wait, current);
Davide Libenzid47de162007-05-15 01:40:41 -07001105 wait.flags |= WQ_FLAG_EXCLUSIVE;
Davide Libenzi3419b232006-06-25 05:48:14 -07001106 __add_wait_queue(&ep->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 for (;;) {
1109 /*
1110 * We don't want to sleep if the ep_poll_callback() sends us
1111 * a wakeup in between. That's why we set the task state
1112 * to TASK_INTERRUPTIBLE before doing the checks.
1113 */
1114 set_current_state(TASK_INTERRUPTIBLE);
1115 if (!list_empty(&ep->rdllist) || !jtimeout)
1116 break;
1117 if (signal_pending(current)) {
1118 res = -EINTR;
1119 break;
1120 }
1121
1122 write_unlock_irqrestore(&ep->lock, flags);
1123 jtimeout = schedule_timeout(jtimeout);
1124 write_lock_irqsave(&ep->lock, flags);
1125 }
Davide Libenzi3419b232006-06-25 05:48:14 -07001126 __remove_wait_queue(&ep->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 set_current_state(TASK_RUNNING);
1129 }
1130
1131 /* Is it worth to try to dig for events ? */
1132 eavail = !list_empty(&ep->rdllist);
1133
1134 write_unlock_irqrestore(&ep->lock, flags);
1135
1136 /*
1137 * Try to transfer events to user space. In case we get 0 events and
1138 * there's still timeout left over, we go trying again in search of
1139 * more luck.
1140 */
1141 if (!res && eavail &&
Davide Libenzid47de162007-05-15 01:40:41 -07001142 !(res = ep_send_events(ep, events, maxevents)) && jtimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 goto retry;
1144
1145 return res;
1146}
1147
Davide Libenzi7699acd2007-05-10 22:23:23 -07001148/*
1149 * It opens an eventpoll file descriptor by suggesting a storage of "size"
1150 * file descriptors. The size parameter is just an hint about how to size
1151 * data structures. It won't prevent the user to store more than "size"
1152 * file descriptors inside the epoll interface. It is the kernel part of
1153 * the userspace epoll_create(2).
1154 */
1155asmlinkage long sys_epoll_create(int size)
1156{
1157 int error, fd = -1;
1158 struct eventpoll *ep;
1159 struct inode *inode;
1160 struct file *file;
1161
1162 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
1163 current, size));
1164
1165 /*
1166 * Sanity check on the size parameter, and create the internal data
1167 * structure ( "struct eventpoll" ).
1168 */
1169 error = -EINVAL;
1170 if (size <= 0 || (error = ep_alloc(&ep)) != 0)
1171 goto error_return;
1172
1173 /*
1174 * Creates all the items needed to setup an eventpoll file. That is,
1175 * a file structure, and inode and a free file descriptor.
1176 */
1177 error = anon_inode_getfd(&fd, &inode, &file, "[eventpoll]",
1178 &eventpoll_fops, ep);
1179 if (error)
1180 goto error_free;
1181
1182 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
1183 current, size, fd));
1184
1185 return fd;
1186
1187error_free:
1188 ep_free(ep);
1189 kfree(ep);
1190error_return:
1191 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
1192 current, size, error));
1193 return error;
1194}
1195
1196/*
1197 * The following function implements the controller interface for
1198 * the eventpoll file that enables the insertion/removal/change of
1199 * file descriptors inside the interest set. It represents
1200 * the kernel part of the user space epoll_ctl(2).
1201 */
1202asmlinkage long sys_epoll_ctl(int epfd, int op, int fd,
1203 struct epoll_event __user *event)
1204{
1205 int error;
1206 struct file *file, *tfile;
1207 struct eventpoll *ep;
1208 struct epitem *epi;
1209 struct epoll_event epds;
1210
1211 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n",
1212 current, epfd, op, fd, event));
1213
1214 error = -EFAULT;
1215 if (ep_op_has_event(op) &&
1216 copy_from_user(&epds, event, sizeof(struct epoll_event)))
1217 goto error_return;
1218
1219 /* Get the "struct file *" for the eventpoll file */
1220 error = -EBADF;
1221 file = fget(epfd);
1222 if (!file)
1223 goto error_return;
1224
1225 /* Get the "struct file *" for the target file */
1226 tfile = fget(fd);
1227 if (!tfile)
1228 goto error_fput;
1229
1230 /* The target file descriptor must support poll */
1231 error = -EPERM;
1232 if (!tfile->f_op || !tfile->f_op->poll)
1233 goto error_tgt_fput;
1234
1235 /*
1236 * We have to check that the file structure underneath the file descriptor
1237 * the user passed to us _is_ an eventpoll file. And also we do not permit
1238 * adding an epoll file descriptor inside itself.
1239 */
1240 error = -EINVAL;
1241 if (file == tfile || !is_file_epoll(file))
1242 goto error_tgt_fput;
1243
1244 /*
1245 * At this point it is safe to assume that the "private_data" contains
1246 * our own data structure.
1247 */
1248 ep = file->private_data;
1249
Davide Libenzid47de162007-05-15 01:40:41 -07001250 mutex_lock(&ep->mtx);
Davide Libenzi7699acd2007-05-10 22:23:23 -07001251
1252 /* Try to lookup the file inside our RB tree */
1253 epi = ep_find(ep, tfile, fd);
1254
1255 error = -EINVAL;
1256 switch (op) {
1257 case EPOLL_CTL_ADD:
1258 if (!epi) {
1259 epds.events |= POLLERR | POLLHUP;
1260
1261 error = ep_insert(ep, &epds, tfile, fd);
1262 } else
1263 error = -EEXIST;
1264 break;
1265 case EPOLL_CTL_DEL:
1266 if (epi)
1267 error = ep_remove(ep, epi);
1268 else
1269 error = -ENOENT;
1270 break;
1271 case EPOLL_CTL_MOD:
1272 if (epi) {
1273 epds.events |= POLLERR | POLLHUP;
1274 error = ep_modify(ep, epi, &epds);
1275 } else
1276 error = -ENOENT;
1277 break;
1278 }
1279 /*
1280 * The function ep_find() increments the usage count of the structure
1281 * so, if this is not NULL, we need to release it.
1282 */
1283 if (epi)
1284 ep_release_epitem(epi);
Davide Libenzid47de162007-05-15 01:40:41 -07001285 mutex_unlock(&ep->mtx);
Davide Libenzi7699acd2007-05-10 22:23:23 -07001286
1287error_tgt_fput:
1288 fput(tfile);
1289error_fput:
1290 fput(file);
1291error_return:
1292 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n",
1293 current, epfd, op, fd, event, error));
1294
1295 return error;
1296}
1297
1298/*
1299 * Implement the event wait interface for the eventpoll file. It is the kernel
1300 * part of the user space epoll_wait(2).
1301 */
1302asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
1303 int maxevents, int timeout)
1304{
1305 int error;
1306 struct file *file;
1307 struct eventpoll *ep;
1308
1309 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n",
1310 current, epfd, events, maxevents, timeout));
1311
1312 /* The maximum number of event must be greater than zero */
1313 if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)
1314 return -EINVAL;
1315
1316 /* Verify that the area passed by the user is writeable */
1317 if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
1318 error = -EFAULT;
1319 goto error_return;
1320 }
1321
1322 /* Get the "struct file *" for the eventpoll file */
1323 error = -EBADF;
1324 file = fget(epfd);
1325 if (!file)
1326 goto error_return;
1327
1328 /*
1329 * We have to check that the file structure underneath the fd
1330 * the user passed to us _is_ an eventpoll file.
1331 */
1332 error = -EINVAL;
1333 if (!is_file_epoll(file))
1334 goto error_fput;
1335
1336 /*
1337 * At this point it is safe to assume that the "private_data" contains
1338 * our own data structure.
1339 */
1340 ep = file->private_data;
1341
1342 /* Time to fish for events ... */
1343 error = ep_poll(ep, events, maxevents, timeout);
1344
1345error_fput:
1346 fput(file);
1347error_return:
1348 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n",
1349 current, epfd, events, maxevents, timeout, error));
1350
1351 return error;
1352}
1353
1354#ifdef TIF_RESTORE_SIGMASK
1355
1356/*
1357 * Implement the event wait interface for the eventpoll file. It is the kernel
1358 * part of the user space epoll_pwait(2).
1359 */
1360asmlinkage long sys_epoll_pwait(int epfd, struct epoll_event __user *events,
1361 int maxevents, int timeout, const sigset_t __user *sigmask,
1362 size_t sigsetsize)
1363{
1364 int error;
1365 sigset_t ksigmask, sigsaved;
1366
1367 /*
1368 * If the caller wants a certain signal mask to be set during the wait,
1369 * we apply it here.
1370 */
1371 if (sigmask) {
1372 if (sigsetsize != sizeof(sigset_t))
1373 return -EINVAL;
1374 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
1375 return -EFAULT;
1376 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
1377 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1378 }
1379
1380 error = sys_epoll_wait(epfd, events, maxevents, timeout);
1381
1382 /*
1383 * If we changed the signal mask, we need to restore the original one.
1384 * In case we've got a signal while waiting, we do not restore the
1385 * signal mask yet, and we allow do_signal() to deliver the signal on
1386 * the way back to userspace, before the signal mask is restored.
1387 */
1388 if (sigmask) {
1389 if (error == -EINTR) {
1390 memcpy(&current->saved_sigmask, &sigsaved,
1391 sizeof(sigsaved));
1392 set_thread_flag(TIF_RESTORE_SIGMASK);
1393 } else
1394 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1395 }
1396
1397 return error;
1398}
1399
1400#endif /* #ifdef TIF_RESTORE_SIGMASK */
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402static int __init eventpoll_init(void)
1403{
Arjan van de Ven144efe32006-03-23 03:00:32 -08001404 mutex_init(&epmutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /* Initialize the structure used to perform safe poll wait head wake ups */
1407 ep_poll_safewake_init(&psw);
1408
1409 /* Allocates slab cache used to allocate "struct epitem" items */
1410 epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
1411 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC,
1412 NULL, NULL);
1413
1414 /* Allocates slab cache used to allocate "struct eppoll_entry" */
1415 pwq_cache = kmem_cache_create("eventpoll_pwq",
1416 sizeof(struct eppoll_entry), 0,
1417 EPI_SLAB_DEBUG|SLAB_PANIC, NULL, NULL);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
Davide Libenzicea69242007-05-10 22:23:22 -07001421fs_initcall(eventpoll_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422