]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/eventpoll.c
Linus 2.6.29-rc8
[net-next-2.6.git] / fs / eventpoll.c
CommitLineData
1da177e4 1/*
c7ea7630
DL
2 * fs/eventpoll.c (Efficent event polling implementation)
3 * Copyright (C) 2001,...,2007 Davide Libenzi
1da177e4
LT
4 *
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
1da177e4
LT
14#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>
1da177e4
LT
24#include <linux/string.h>
25#include <linux/list.h>
26#include <linux/hash.h>
27#include <linux/spinlock.h>
28#include <linux/syscalls.h>
1da177e4
LT
29#include <linux/rbtree.h>
30#include <linux/wait.h>
31#include <linux/eventpoll.h>
32#include <linux/mount.h>
33#include <linux/bitops.h>
144efe3e 34#include <linux/mutex.h>
da66f7cb 35#include <linux/anon_inodes.h>
1da177e4
LT
36#include <asm/uaccess.h>
37#include <asm/system.h>
38#include <asm/io.h>
39#include <asm/mman.h>
40#include <asm/atomic.h>
1da177e4 41
1da177e4
LT
42/*
43 * LOCKING:
44 * There are three level of locking required by epoll :
45 *
144efe3e 46 * 1) epmutex (mutex)
c7ea7630
DL
47 * 2) ep->mtx (mutex)
48 * 3) ep->lock (spinlock)
1da177e4
LT
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
d47de16c
DL
58 * 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
1da177e4
LT
63 * 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).
d47de16c
DL
66 * 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.
144efe3e 69 * Events that require holding "epmutex" are very rare, while for
d47de16c
DL
70 * normal operations the epoll private "ep->mtx" will guarantee
71 * a better scalability.
1da177e4
LT
72 */
73
1da177e4
LT
74#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
e3306dd5
DL
98/* 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
b611967d
DL
101#define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
102
d47de16c
DL
103#define EP_UNACTIVE_PTR ((void *) -1L)
104
7ef9964e
DL
105#define EP_ITEM_COST (sizeof(struct epitem) + sizeof(struct eppoll_entry))
106
1da177e4
LT
107struct 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
d47de16c 116 * (loop) 2) allow a maximum number of epoll descriptors inclusion nesting
1da177e4
LT
117 * 3) let go the ones coming from other tasks.
118 */
119struct wake_task_node {
120 struct list_head llink;
36c8b586 121 struct task_struct *task;
1da177e4
LT
122 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
d47de16c
DL
134/*
135 * Each file descriptor added to the eventpoll interface will
136 * have an entry of this type linked to the "rbr" RB tree.
137 */
138struct epitem {
67647d0f 139 /* RB tree node used to link this structure to the eventpoll RB tree */
d47de16c
DL
140 struct rb_node rbn;
141
142 /* List header used to link this structure to the eventpoll ready list */
143 struct list_head rdllink;
144
c7ea7630
DL
145 /*
146 * Works together "struct eventpoll"->ovflist in keeping the
147 * single linked chain of items.
148 */
149 struct epitem *next;
150
d47de16c
DL
151 /* 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
d47de16c
DL
163 /* List header used to link this item to the "struct file" items list */
164 struct list_head fllink;
165
c7ea7630
DL
166 /* The structure that describe the interested events and the source fd */
167 struct epoll_event event;
d47de16c
DL
168};
169
1da177e4
LT
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 */
c7ea7630 177 spinlock_t lock;
1da177e4
LT
178
179 /*
d47de16c
DL
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.
1da177e4 184 */
d47de16c 185 struct mutex mtx;
1da177e4
LT
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
67647d0f 196 /* RB tree root used to store monitored fd structs */
1da177e4 197 struct rb_root rbr;
d47de16c
DL
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;
7ef9964e
DL
205
206 /* The user that created the eventpoll descriptor */
207 struct user_struct *user;
1da177e4
LT
208};
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;
226};
227
1da177e4
LT
228/* Wrapper struct used by poll queueing */
229struct ep_pqueue {
230 poll_table pt;
231 struct epitem *epi;
232};
233
7ef9964e
DL
234/*
235 * Configuration options available inside /proc/sys/fs/epoll/
236 */
7ef9964e
DL
237/* Maximum number of epoll watched descriptors, per user */
238static int max_user_watches __read_mostly;
239
1da177e4 240/*
d47de16c 241 * This mutex is used to serialize ep_free() and eventpoll_release_file().
1da177e4 242 */
7ef9964e 243static DEFINE_MUTEX(epmutex);
1da177e4
LT
244
245/* Safe wake up implementation */
246static struct poll_safewake psw;
247
248/* Slab cache used to allocate "struct epitem" */
e18b890b 249static struct kmem_cache *epi_cache __read_mostly;
1da177e4
LT
250
251/* Slab cache used to allocate "struct eppoll_entry" */
e18b890b 252static struct kmem_cache *pwq_cache __read_mostly;
1da177e4 253
7ef9964e
DL
254#ifdef CONFIG_SYSCTL
255
256#include <linux/sysctl.h>
257
258static int zero;
259
260ctl_table epoll_table[] = {
7ef9964e
DL
261 {
262 .procname = "max_user_watches",
263 .data = &max_user_watches,
264 .maxlen = sizeof(int),
265 .mode = 0644,
266 .proc_handler = &proc_dointvec_minmax,
267 .extra1 = &zero,
268 },
269 { .ctl_name = 0 }
270};
271#endif /* CONFIG_SYSCTL */
272
b030a4dd 273
67647d0f 274/* Setup the structure that is used as key for the RB tree */
b030a4dd
PE
275static inline void ep_set_ffd(struct epoll_filefd *ffd,
276 struct file *file, int fd)
277{
278 ffd->file = file;
279 ffd->fd = fd;
280}
281
67647d0f 282/* Compare RB tree keys */
b030a4dd
PE
283static inline int ep_cmp_ffd(struct epoll_filefd *p1,
284 struct epoll_filefd *p2)
285{
286 return (p1->file > p2->file ? +1:
287 (p1->file < p2->file ? -1 : p1->fd - p2->fd));
288}
289
b030a4dd
PE
290/* Tells us if the item is currently linked */
291static inline int ep_is_linked(struct list_head *p)
292{
293 return !list_empty(p);
294}
295
296/* Get the "struct epitem" from a wait queue pointer */
cdac75e6 297static inline struct epitem *ep_item_from_wait(wait_queue_t *p)
b030a4dd
PE
298{
299 return container_of(p, struct eppoll_entry, wait)->base;
300}
301
302/* Get the "struct epitem" from an epoll queue wrapper */
cdac75e6 303static inline struct epitem *ep_item_from_epqueue(poll_table *p)
b030a4dd
PE
304{
305 return container_of(p, struct ep_pqueue, pt)->epi;
306}
307
308/* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
6192bd53 309static inline int ep_op_has_event(int op)
b030a4dd
PE
310{
311 return op != EPOLL_CTL_DEL;
312}
313
1da177e4
LT
314/* Initialize the poll safe wake up structure */
315static void ep_poll_safewake_init(struct poll_safewake *psw)
316{
317
318 INIT_LIST_HEAD(&psw->wake_task_list);
319 spin_lock_init(&psw->lock);
320}
321
1da177e4
LT
322/*
323 * Perform a safe wake up of the poll wait list. The problem is that
324 * with the new callback'd wake up system, it is possible that the
325 * poll callback is reentered from inside the call to wake_up() done
326 * on the poll wait queue head. The rule is that we cannot reenter the
327 * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times,
328 * and we cannot reenter the same wait queue head at all. This will
329 * enable to have a hierarchy of epoll file descriptor of no more than
330 * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock
331 * because this one gets called by the poll callback, that in turn is called
332 * from inside a wake_up(), that might be called from irq context.
333 */
334static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
335{
336 int wake_nests = 0;
337 unsigned long flags;
36c8b586 338 struct task_struct *this_task = current;
b70c3940 339 struct list_head *lsthead = &psw->wake_task_list;
1da177e4
LT
340 struct wake_task_node *tncur;
341 struct wake_task_node tnode;
342
343 spin_lock_irqsave(&psw->lock, flags);
344
345 /* Try to see if the current task is already inside this wakeup call */
b70c3940 346 list_for_each_entry(tncur, lsthead, llink) {
1da177e4
LT
347
348 if (tncur->wq == wq ||
349 (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) {
350 /*
351 * Ops ... loop detected or maximum nest level reached.
352 * We abort this wake by breaking the cycle itself.
353 */
354 spin_unlock_irqrestore(&psw->lock, flags);
355 return;
356 }
357 }
358
359 /* Add the current task to the list */
360 tnode.task = this_task;
361 tnode.wq = wq;
362 list_add(&tnode.llink, lsthead);
363
364 spin_unlock_irqrestore(&psw->lock, flags);
365
366 /* Do really wake up now */
0ccf831c 367 wake_up_nested(wq, 1 + wake_nests);
1da177e4
LT
368
369 /* Remove the current task from the list */
370 spin_lock_irqsave(&psw->lock, flags);
371 list_del(&tnode.llink);
372 spin_unlock_irqrestore(&psw->lock, flags);
373}
374
1da177e4 375/*
7699acd1
DL
376 * This function unregister poll callbacks from the associated file descriptor.
377 * Since this must be called without holding "ep->lock" the atomic exchange trick
378 * will protect us from multiple unregister.
1da177e4 379 */
7699acd1 380static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
1da177e4 381{
7699acd1
DL
382 int nwait;
383 struct list_head *lsthead = &epi->pwqlist;
384 struct eppoll_entry *pwq;
1da177e4 385
7699acd1
DL
386 /* This is called without locks, so we need the atomic exchange */
387 nwait = xchg(&epi->nwait, 0);
1da177e4 388
7699acd1
DL
389 if (nwait) {
390 while (!list_empty(lsthead)) {
391 pwq = list_first_entry(lsthead, struct eppoll_entry, llink);
1da177e4 392
7699acd1
DL
393 list_del_init(&pwq->llink);
394 remove_wait_queue(pwq->whead, &pwq->wait);
395 kmem_cache_free(pwq_cache, pwq);
396 }
1da177e4 397 }
1da177e4
LT
398}
399
7699acd1
DL
400/*
401 * Removes a "struct epitem" from the eventpoll RB tree and deallocates
c7ea7630 402 * all the associated resources. Must be called with "mtx" held.
7699acd1
DL
403 */
404static int ep_remove(struct eventpoll *ep, struct epitem *epi)
405{
7699acd1
DL
406 unsigned long flags;
407 struct file *file = epi->ffd.file;
1da177e4
LT
408
409 /*
7699acd1
DL
410 * Removes poll wait queue hooks. We _have_ to do this without holding
411 * the "ep->lock" otherwise a deadlock might occur. This because of the
412 * sequence of the lock acquisition. Here we do "ep->lock" then the wait
413 * queue head lock when unregistering the wait queue. The wakeup callback
414 * will run by holding the wait queue head lock and will call our callback
415 * that will try to get "ep->lock".
1da177e4 416 */
7699acd1 417 ep_unregister_pollwait(ep, epi);
1da177e4 418
7699acd1
DL
419 /* Remove the current item from the list of epoll hooks */
420 spin_lock(&file->f_ep_lock);
421 if (ep_is_linked(&epi->fllink))
422 list_del_init(&epi->fllink);
423 spin_unlock(&file->f_ep_lock);
1da177e4 424
cdac75e6 425 rb_erase(&epi->rbn, &ep->rbr);
1da177e4 426
c7ea7630
DL
427 spin_lock_irqsave(&ep->lock, flags);
428 if (ep_is_linked(&epi->rdllink))
429 list_del_init(&epi->rdllink);
430 spin_unlock_irqrestore(&ep->lock, flags);
1da177e4 431
7699acd1 432 /* At this point it is safe to free the eventpoll item */
c7ea7630 433 kmem_cache_free(epi_cache, epi);
1da177e4 434
7ef9964e
DL
435 atomic_dec(&ep->user->epoll_watches);
436
c7ea7630
DL
437 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p)\n",
438 current, ep, file));
1da177e4 439
c7ea7630 440 return 0;
1da177e4
LT
441}
442
7699acd1 443static void ep_free(struct eventpoll *ep)
1da177e4 444{
7699acd1
DL
445 struct rb_node *rbp;
446 struct epitem *epi;
1da177e4 447
7699acd1
DL
448 /* We need to release all tasks waiting for these file */
449 if (waitqueue_active(&ep->poll_wait))
450 ep_poll_safewake(&psw, &ep->poll_wait);
1da177e4 451
7699acd1
DL
452 /*
453 * We need to lock this because we could be hit by
454 * eventpoll_release_file() while we're freeing the "struct eventpoll".
d47de16c 455 * We do not need to hold "ep->mtx" here because the epoll file
7699acd1
DL
456 * is on the way to be removed and no one has references to it
457 * anymore. The only hit might come from eventpoll_release_file() but
458 * holding "epmutex" is sufficent here.
459 */
460 mutex_lock(&epmutex);
1da177e4
LT
461
462 /*
7699acd1 463 * Walks through the whole tree by unregistering poll callbacks.
1da177e4 464 */
7699acd1
DL
465 for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
466 epi = rb_entry(rbp, struct epitem, rbn);
467
468 ep_unregister_pollwait(ep, epi);
469 }
1da177e4
LT
470
471 /*
7699acd1
DL
472 * Walks through the whole tree by freeing each "struct epitem". At this
473 * point we are sure no poll callbacks will be lingering around, and also by
d47de16c 474 * holding "epmutex" we can be sure that no file cleanup code will hit
7699acd1 475 * us during this operation. So we can avoid the lock on "ep->lock".
1da177e4 476 */
c80544dc 477 while ((rbp = rb_first(&ep->rbr)) != NULL) {
7699acd1
DL
478 epi = rb_entry(rbp, struct epitem, rbn);
479 ep_remove(ep, epi);
480 }
1da177e4 481
7699acd1 482 mutex_unlock(&epmutex);
d47de16c 483 mutex_destroy(&ep->mtx);
7ef9964e 484 free_uid(ep->user);
f0ee9aab 485 kfree(ep);
7699acd1 486}
1da177e4 487
7699acd1
DL
488static int ep_eventpoll_release(struct inode *inode, struct file *file)
489{
490 struct eventpoll *ep = file->private_data;
1da177e4 491
f0ee9aab 492 if (ep)
7699acd1 493 ep_free(ep);
7699acd1
DL
494
495 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep));
496 return 0;
1da177e4
LT
497}
498
7699acd1
DL
499static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
500{
501 unsigned int pollflags = 0;
502 unsigned long flags;
503 struct eventpoll *ep = file->private_data;
1da177e4 504
7699acd1
DL
505 /* Insert inside our poll wait queue */
506 poll_wait(file, &ep->poll_wait, wait);
507
508 /* Check our condition */
c7ea7630 509 spin_lock_irqsave(&ep->lock, flags);
7699acd1
DL
510 if (!list_empty(&ep->rdllist))
511 pollflags = POLLIN | POLLRDNORM;
c7ea7630 512 spin_unlock_irqrestore(&ep->lock, flags);
7699acd1
DL
513
514 return pollflags;
515}
516
517/* File callbacks that implement the eventpoll file behaviour */
518static const struct file_operations eventpoll_fops = {
519 .release = ep_eventpoll_release,
520 .poll = ep_eventpoll_poll
521};
522
523/* Fast test to see if the file is an evenpoll file */
524static inline int is_file_epoll(struct file *f)
525{
526 return f->f_op == &eventpoll_fops;
527}
b611967d
DL
528
529/*
7699acd1
DL
530 * This is called from eventpoll_release() to unlink files from the eventpoll
531 * interface. We need to have this facility to cleanup correctly files that are
532 * closed without being removed from the eventpoll interface.
b611967d 533 */
7699acd1 534void eventpoll_release_file(struct file *file)
b611967d 535{
7699acd1
DL
536 struct list_head *lsthead = &file->f_ep_links;
537 struct eventpoll *ep;
538 struct epitem *epi;
b611967d
DL
539
540 /*
7699acd1
DL
541 * We don't want to get "file->f_ep_lock" because it is not
542 * necessary. It is not necessary because we're in the "struct file"
543 * cleanup path, and this means that noone is using this file anymore.
67647d0f
DL
544 * So, for example, epoll_ctl() cannot hit here sicne if we reach this
545 * point, the file counter already went to zero and fget() would fail.
d47de16c 546 * The only hit might come from ep_free() but by holding the mutex
7699acd1 547 * will correctly serialize the operation. We do need to acquire
d47de16c 548 * "ep->mtx" after "epmutex" because ep_remove() requires it when called
7699acd1 549 * from anywhere but ep_free().
b611967d 550 */
7699acd1 551 mutex_lock(&epmutex);
b611967d 552
7699acd1
DL
553 while (!list_empty(lsthead)) {
554 epi = list_first_entry(lsthead, struct epitem, fllink);
b611967d 555
7699acd1
DL
556 ep = epi->ep;
557 list_del_init(&epi->fllink);
d47de16c 558 mutex_lock(&ep->mtx);
7699acd1 559 ep_remove(ep, epi);
d47de16c 560 mutex_unlock(&ep->mtx);
b611967d
DL
561 }
562
7699acd1 563 mutex_unlock(&epmutex);
b611967d
DL
564}
565
53d2be79 566static int ep_alloc(struct eventpoll **pep)
1da177e4 567{
7ef9964e
DL
568 int error;
569 struct user_struct *user;
570 struct eventpoll *ep;
1da177e4 571
7ef9964e 572 user = get_current_user();
7ef9964e
DL
573 error = -ENOMEM;
574 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
575 if (unlikely(!ep))
576 goto free_uid;
1da177e4 577
c7ea7630 578 spin_lock_init(&ep->lock);
d47de16c 579 mutex_init(&ep->mtx);
1da177e4
LT
580 init_waitqueue_head(&ep->wq);
581 init_waitqueue_head(&ep->poll_wait);
582 INIT_LIST_HEAD(&ep->rdllist);
583 ep->rbr = RB_ROOT;
d47de16c 584 ep->ovflist = EP_UNACTIVE_PTR;
7ef9964e 585 ep->user = user;
1da177e4 586
53d2be79 587 *pep = ep;
1da177e4 588
53d2be79 589 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n",
1da177e4
LT
590 current, ep));
591 return 0;
7ef9964e
DL
592
593free_uid:
594 free_uid(user);
595 return error;
1da177e4
LT
596}
597
1da177e4 598/*
c7ea7630
DL
599 * Search the file inside the eventpoll tree. The RB tree operations
600 * are protected by the "mtx" mutex, and ep_find() must be called with
601 * "mtx" held.
1da177e4
LT
602 */
603static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
604{
605 int kcmp;
1da177e4
LT
606 struct rb_node *rbp;
607 struct epitem *epi, *epir = NULL;
608 struct epoll_filefd ffd;
609
b030a4dd 610 ep_set_ffd(&ffd, file, fd);
1da177e4
LT
611 for (rbp = ep->rbr.rb_node; rbp; ) {
612 epi = rb_entry(rbp, struct epitem, rbn);
b030a4dd 613 kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
1da177e4
LT
614 if (kcmp > 0)
615 rbp = rbp->rb_right;
616 else if (kcmp < 0)
617 rbp = rbp->rb_left;
618 else {
1da177e4
LT
619 epir = epi;
620 break;
621 }
622 }
1da177e4
LT
623
624 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n",
625 current, file, epir));
626
627 return epir;
628}
629
1da177e4 630/*
7699acd1
DL
631 * This is the callback that is passed to the wait queue wakeup
632 * machanism. It is called by the stored file descriptors when they
633 * have events to report.
1da177e4 634 */
7699acd1 635static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
1da177e4 636{
7699acd1
DL
637 int pwake = 0;
638 unsigned long flags;
639 struct epitem *epi = ep_item_from_wait(wait);
640 struct eventpoll *ep = epi->ep;
1da177e4 641
7699acd1
DL
642 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
643 current, epi->ffd.file, epi, ep));
1da177e4 644
c7ea7630 645 spin_lock_irqsave(&ep->lock, flags);
1da177e4 646
7699acd1
DL
647 /*
648 * If the event mask does not contain any poll(2) event, we consider the
649 * descriptor to be disabled. This condition is likely the effect of the
650 * EPOLLONESHOT bit that disables the descriptor when an event is received,
651 * until the next EPOLL_CTL_MOD will be issued.
652 */
653 if (!(epi->event.events & ~EP_PRIVATE_BITS))
d47de16c
DL
654 goto out_unlock;
655
656 /*
657 * If we are trasfering events to userspace, we can hold no locks
658 * (because we're accessing user memory, and because of linux f_op->poll()
659 * semantics). All the events that happens during that period of time are
660 * chained in ep->ovflist and requeued later on.
661 */
662 if (unlikely(ep->ovflist != EP_UNACTIVE_PTR)) {
663 if (epi->next == EP_UNACTIVE_PTR) {
664 epi->next = ep->ovflist;
665 ep->ovflist = epi;
666 }
667 goto out_unlock;
668 }
1da177e4 669
7699acd1
DL
670 /* If this file is already in the ready list we exit soon */
671 if (ep_is_linked(&epi->rdllink))
672 goto is_linked;
673
674 list_add_tail(&epi->rdllink, &ep->rdllist);
675
676is_linked:
677 /*
678 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
679 * wait list.
680 */
681 if (waitqueue_active(&ep->wq))
4a6e9e2c 682 wake_up_locked(&ep->wq);
7699acd1
DL
683 if (waitqueue_active(&ep->poll_wait))
684 pwake++;
685
d47de16c 686out_unlock:
c7ea7630 687 spin_unlock_irqrestore(&ep->lock, flags);
1da177e4 688
7699acd1
DL
689 /* We have to call this outside the lock */
690 if (pwake)
691 ep_poll_safewake(&psw, &ep->poll_wait);
692
693 return 1;
694}
1da177e4
LT
695
696/*
697 * This is the callback that is used to add our wait queue to the
698 * target file wakeup lists.
699 */
700static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
701 poll_table *pt)
702{
b030a4dd 703 struct epitem *epi = ep_item_from_epqueue(pt);
1da177e4
LT
704 struct eppoll_entry *pwq;
705
e94b1766 706 if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
1da177e4
LT
707 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
708 pwq->whead = whead;
709 pwq->base = epi;
710 add_wait_queue(whead, &pwq->wait);
711 list_add_tail(&pwq->llink, &epi->pwqlist);
712 epi->nwait++;
713 } else {
714 /* We have to signal that an error occurred */
715 epi->nwait = -1;
716 }
717}
718
1da177e4
LT
719static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
720{
721 int kcmp;
722 struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
723 struct epitem *epic;
724
725 while (*p) {
726 parent = *p;
727 epic = rb_entry(parent, struct epitem, rbn);
b030a4dd 728 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
1da177e4
LT
729 if (kcmp > 0)
730 p = &parent->rb_right;
731 else
732 p = &parent->rb_left;
733 }
734 rb_link_node(&epi->rbn, parent, p);
735 rb_insert_color(&epi->rbn, &ep->rbr);
736}
737
c7ea7630
DL
738/*
739 * Must be called with "mtx" held.
740 */
1da177e4
LT
741static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
742 struct file *tfile, int fd)
743{
744 int error, revents, pwake = 0;
745 unsigned long flags;
746 struct epitem *epi;
747 struct ep_pqueue epq;
748
7ef9964e
DL
749 if (unlikely(atomic_read(&ep->user->epoll_watches) >=
750 max_user_watches))
751 return -ENOSPC;
e94b1766 752 if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
7ef9964e 753 return -ENOMEM;
1da177e4
LT
754
755 /* Item initialization follow here ... */
1da177e4
LT
756 INIT_LIST_HEAD(&epi->rdllink);
757 INIT_LIST_HEAD(&epi->fllink);
1da177e4
LT
758 INIT_LIST_HEAD(&epi->pwqlist);
759 epi->ep = ep;
b030a4dd 760 ep_set_ffd(&epi->ffd, tfile, fd);
1da177e4 761 epi->event = *event;
1da177e4 762 epi->nwait = 0;
d47de16c 763 epi->next = EP_UNACTIVE_PTR;
1da177e4
LT
764
765 /* Initialize the poll table using the queue callback */
766 epq.epi = epi;
767 init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
768
769 /*
770 * Attach the item to the poll hooks and get current event bits.
771 * We can safely use the file* here because its usage count has
c7ea7630
DL
772 * been increased by the caller of this function. Note that after
773 * this operation completes, the poll callback can start hitting
774 * the new item.
1da177e4
LT
775 */
776 revents = tfile->f_op->poll(tfile, &epq.pt);
777
778 /*
779 * We have to check if something went wrong during the poll wait queue
780 * install process. Namely an allocation for a wait queue failed due
781 * high memory pressure.
782 */
7ef9964e 783 error = -ENOMEM;
1da177e4 784 if (epi->nwait < 0)
7699acd1 785 goto error_unregister;
1da177e4
LT
786
787 /* Add the current item to the list of active epoll hook for this file */
788 spin_lock(&tfile->f_ep_lock);
789 list_add_tail(&epi->fllink, &tfile->f_ep_links);
790 spin_unlock(&tfile->f_ep_lock);
791
c7ea7630
DL
792 /*
793 * Add the current item to the RB tree. All RB tree operations are
794 * protected by "mtx", and ep_insert() is called with "mtx" held.
795 */
1da177e4
LT
796 ep_rbtree_insert(ep, epi);
797
c7ea7630
DL
798 /* We have to drop the new item inside our item list to keep track of it */
799 spin_lock_irqsave(&ep->lock, flags);
800
1da177e4 801 /* If the file is already "ready" we drop it inside the ready list */
b030a4dd 802 if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
1da177e4
LT
803 list_add_tail(&epi->rdllink, &ep->rdllist);
804
805 /* Notify waiting tasks that events are available */
806 if (waitqueue_active(&ep->wq))
4a6e9e2c 807 wake_up_locked(&ep->wq);
1da177e4
LT
808 if (waitqueue_active(&ep->poll_wait))
809 pwake++;
810 }
811
c7ea7630 812 spin_unlock_irqrestore(&ep->lock, flags);
1da177e4 813
7ef9964e
DL
814 atomic_inc(&ep->user->epoll_watches);
815
1da177e4
LT
816 /* We have to call this outside the lock */
817 if (pwake)
818 ep_poll_safewake(&psw, &ep->poll_wait);
819
820 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n",
821 current, ep, tfile, fd));
822
823 return 0;
824
7699acd1 825error_unregister:
1da177e4
LT
826 ep_unregister_pollwait(ep, epi);
827
828 /*
829 * We need to do this because an event could have been arrived on some
67647d0f
DL
830 * allocated wait queue. Note that we don't care about the ep->ovflist
831 * list, since that is used/cleaned only inside a section bound by "mtx".
832 * And ep_insert() is called with "mtx" held.
1da177e4 833 */
c7ea7630 834 spin_lock_irqsave(&ep->lock, flags);
b030a4dd 835 if (ep_is_linked(&epi->rdllink))
6192bd53 836 list_del_init(&epi->rdllink);
c7ea7630 837 spin_unlock_irqrestore(&ep->lock, flags);
1da177e4 838
b030a4dd 839 kmem_cache_free(epi_cache, epi);
7ef9964e 840
1da177e4
LT
841 return error;
842}
843
1da177e4
LT
844/*
845 * Modify the interest event mask by dropping an event if the new mask
c7ea7630 846 * has a match in the current file status. Must be called with "mtx" held.
1da177e4
LT
847 */
848static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
849{
850 int pwake = 0;
851 unsigned int revents;
852 unsigned long flags;
853
854 /*
855 * Set the new event interest mask before calling f_op->poll(), otherwise
856 * a potential race might occur. In fact if we do this operation inside
857 * the lock, an event might happen between the f_op->poll() call and the
858 * new event set registering.
859 */
860 epi->event.events = event->events;
861
862 /*
863 * Get current event bits. We can safely use the file* here because
864 * its usage count has been increased by the caller of this function.
865 */
866 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
867
c7ea7630 868 spin_lock_irqsave(&ep->lock, flags);
1da177e4
LT
869
870 /* Copy the data member from inside the lock */
871 epi->event.data = event->data;
872
873 /*
c7ea7630 874 * If the item is "hot" and it is not registered inside the ready
67647d0f 875 * list, push it inside.
1da177e4 876 */
c7ea7630
DL
877 if (revents & event->events) {
878 if (!ep_is_linked(&epi->rdllink)) {
879 list_add_tail(&epi->rdllink, &ep->rdllist);
880
881 /* Notify waiting tasks that events are available */
882 if (waitqueue_active(&ep->wq))
4a6e9e2c 883 wake_up_locked(&ep->wq);
c7ea7630
DL
884 if (waitqueue_active(&ep->poll_wait))
885 pwake++;
7699acd1
DL
886 }
887 }
c7ea7630 888 spin_unlock_irqrestore(&ep->lock, flags);
1da177e4 889
7699acd1
DL
890 /* We have to call this outside the lock */
891 if (pwake)
892 ep_poll_safewake(&psw, &ep->poll_wait);
1da177e4 893
7699acd1 894 return 0;
1da177e4
LT
895}
896
d47de16c
DL
897static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *events,
898 int maxevents)
1da177e4 899{
6192bd53 900 int eventcnt, error = -EFAULT, pwake = 0;
1da177e4 901 unsigned int revents;
6192bd53 902 unsigned long flags;
d47de16c
DL
903 struct epitem *epi, *nepi;
904 struct list_head txlist;
905
906 INIT_LIST_HEAD(&txlist);
6192bd53 907
d47de16c
DL
908 /*
909 * We need to lock this because we could be hit by
910 * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
911 */
912 mutex_lock(&ep->mtx);
913
914 /*
915 * Steal the ready list, and re-init the original one to the
916 * empty list. Also, set ep->ovflist to NULL so that events
917 * happening while looping w/out locks, are not lost. We cannot
918 * have the poll callback to queue directly on ep->rdllist,
919 * because we are doing it in the loop below, in a lockless way.
920 */
c7ea7630 921 spin_lock_irqsave(&ep->lock, flags);
d47de16c
DL
922 list_splice(&ep->rdllist, &txlist);
923 INIT_LIST_HEAD(&ep->rdllist);
924 ep->ovflist = NULL;
c7ea7630 925 spin_unlock_irqrestore(&ep->lock, flags);
1da177e4
LT
926
927 /*
928 * We can loop without lock because this is a task private list.
6192bd53 929 * We just splice'd out the ep->rdllist in ep_collect_ready_items().
d47de16c 930 * Items cannot vanish during the loop because we are holding "mtx".
1da177e4 931 */
d47de16c
DL
932 for (eventcnt = 0; !list_empty(&txlist) && eventcnt < maxevents;) {
933 epi = list_first_entry(&txlist, struct epitem, rdllink);
934
935 list_del_init(&epi->rdllink);
1da177e4
LT
936
937 /*
938 * Get the ready file event set. We can safely use the file
d47de16c
DL
939 * because we are holding the "mtx" and this will guarantee
940 * that both the file and the item will not vanish.
1da177e4
LT
941 */
942 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
6192bd53 943 revents &= epi->event.events;
1da177e4
LT
944
945 /*
6192bd53
DL
946 * Is the event mask intersect the caller-requested one,
947 * deliver the event to userspace. Again, we are holding
d47de16c
DL
948 * "mtx", so no operations coming from userspace can change
949 * the item.
1da177e4 950 */
6192bd53
DL
951 if (revents) {
952 if (__put_user(revents,
1da177e4
LT
953 &events[eventcnt].events) ||
954 __put_user(epi->event.data,
955 &events[eventcnt].data))
6192bd53 956 goto errxit;
1da177e4
LT
957 if (epi->event.events & EPOLLONESHOT)
958 epi->event.events &= EP_PRIVATE_BITS;
959 eventcnt++;
960 }
1da177e4 961 /*
d47de16c
DL
962 * At this point, noone can insert into ep->rdllist besides
963 * us. The epoll_ctl() callers are locked out by us holding
964 * "mtx" and the poll callback will queue them in ep->ovflist.
1da177e4 965 */
6192bd53 966 if (!(epi->event.events & EPOLLET) &&
d47de16c
DL
967 (revents & epi->event.events))
968 list_add_tail(&epi->rdllink, &ep->rdllist);
1da177e4 969 }
6192bd53 970 error = 0;
1da177e4 971
d47de16c 972errxit:
6192bd53 973
c7ea7630 974 spin_lock_irqsave(&ep->lock, flags);
d47de16c
DL
975 /*
976 * During the time we spent in the loop above, some other events
977 * might have been queued by the poll callback. We re-insert them
f337b9c5 978 * inside the main ready-list here.
d47de16c
DL
979 */
980 for (nepi = ep->ovflist; (epi = nepi) != NULL;
9ce209d6
DL
981 nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
982 /*
983 * If the above loop quit with errors, the epoll item might still
984 * be linked to "txlist", and the list_splice() done below will
985 * take care of those cases.
986 */
987 if (!ep_is_linked(&epi->rdllink))
988 list_add_tail(&epi->rdllink, &ep->rdllist);
989 }
6192bd53 990 /*
d47de16c
DL
991 * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
992 * releasing the lock, events will be queued in the normal way inside
993 * ep->rdllist.
6192bd53 994 */
d47de16c 995 ep->ovflist = EP_UNACTIVE_PTR;
6192bd53 996
d47de16c 997 /*
67647d0f
DL
998 * In case of error in the event-send loop, or in case the number of
999 * ready events exceeds the userspace limit, we need to splice the
1000 * "txlist" back inside ep->rdllist.
d47de16c
DL
1001 */
1002 list_splice(&txlist, &ep->rdllist);
1003
1004 if (!list_empty(&ep->rdllist)) {
1da177e4 1005 /*
d47de16c 1006 * Wake up (if active) both the eventpoll wait list and the ->poll()
67647d0f 1007 * wait list (delayed after we release the lock).
1da177e4
LT
1008 */
1009 if (waitqueue_active(&ep->wq))
4a6e9e2c 1010 wake_up_locked(&ep->wq);
1da177e4
LT
1011 if (waitqueue_active(&ep->poll_wait))
1012 pwake++;
6192bd53 1013 }
c7ea7630 1014 spin_unlock_irqrestore(&ep->lock, flags);
d47de16c
DL
1015
1016 mutex_unlock(&ep->mtx);
1da177e4
LT
1017
1018 /* We have to call this outside the lock */
1019 if (pwake)
1020 ep_poll_safewake(&psw, &ep->poll_wait);
6192bd53
DL
1021
1022 return eventcnt == 0 ? error: eventcnt;
1da177e4
LT
1023}
1024
1da177e4
LT
1025static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
1026 int maxevents, long timeout)
1027{
1028 int res, eavail;
1029 unsigned long flags;
1030 long jtimeout;
1031 wait_queue_t wait;
1032
1033 /*
1034 * Calculate the timeout by checking for the "infinite" value ( -1 )
1035 * and the overflow condition. The passed timeout is in milliseconds,
1036 * that why (t * HZ) / 1000.
1037 */
e3306dd5
DL
1038 jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ?
1039 MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
1da177e4
LT
1040
1041retry:
c7ea7630 1042 spin_lock_irqsave(&ep->lock, flags);
1da177e4
LT
1043
1044 res = 0;
1045 if (list_empty(&ep->rdllist)) {
1046 /*
1047 * We don't have any available event to return to the caller.
1048 * We need to sleep here, and we will be wake up by
1049 * ep_poll_callback() when events will become available.
1050 */
1051 init_waitqueue_entry(&wait, current);
d47de16c 1052 wait.flags |= WQ_FLAG_EXCLUSIVE;
3419b23a 1053 __add_wait_queue(&ep->wq, &wait);
1da177e4
LT
1054
1055 for (;;) {
1056 /*
1057 * We don't want to sleep if the ep_poll_callback() sends us
1058 * a wakeup in between. That's why we set the task state
1059 * to TASK_INTERRUPTIBLE before doing the checks.
1060 */
1061 set_current_state(TASK_INTERRUPTIBLE);
1062 if (!list_empty(&ep->rdllist) || !jtimeout)
1063 break;
1064 if (signal_pending(current)) {
1065 res = -EINTR;
1066 break;
1067 }
1068
c7ea7630 1069 spin_unlock_irqrestore(&ep->lock, flags);
1da177e4 1070 jtimeout = schedule_timeout(jtimeout);
c7ea7630 1071 spin_lock_irqsave(&ep->lock, flags);
1da177e4 1072 }
3419b23a 1073 __remove_wait_queue(&ep->wq, &wait);
1da177e4
LT
1074
1075 set_current_state(TASK_RUNNING);
1076 }
1077
1078 /* Is it worth to try to dig for events ? */
1079 eavail = !list_empty(&ep->rdllist);
1080
c7ea7630 1081 spin_unlock_irqrestore(&ep->lock, flags);
1da177e4
LT
1082
1083 /*
1084 * Try to transfer events to user space. In case we get 0 events and
1085 * there's still timeout left over, we go trying again in search of
1086 * more luck.
1087 */
1088 if (!res && eavail &&
d47de16c 1089 !(res = ep_send_events(ep, events, maxevents)) && jtimeout)
1da177e4
LT
1090 goto retry;
1091
1092 return res;
1093}
1094
7699acd1 1095/*
523723bb 1096 * Open an eventpoll file descriptor.
7699acd1 1097 */
5a8a82b1 1098SYSCALL_DEFINE1(epoll_create1, int, flags)
7699acd1
DL
1099{
1100 int error, fd = -1;
1101 struct eventpoll *ep;
7699acd1 1102
e38b36f3
UD
1103 /* Check the EPOLL_* constant for consistency. */
1104 BUILD_BUG_ON(EPOLL_CLOEXEC != O_CLOEXEC);
1105
a0998b50
UD
1106 if (flags & ~EPOLL_CLOEXEC)
1107 return -EINVAL;
1108
7699acd1 1109 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
9fe5ad9c 1110 current, flags));
7699acd1
DL
1111
1112 /*
9fe5ad9c 1113 * Create the internal data structure ( "struct eventpoll" ).
7699acd1 1114 */
9fe5ad9c
UD
1115 error = ep_alloc(&ep);
1116 if (error < 0) {
2030a42c 1117 fd = error;
7699acd1 1118 goto error_return;
2030a42c 1119 }
7699acd1
DL
1120
1121 /*
1122 * Creates all the items needed to setup an eventpoll file. That is,
2030a42c 1123 * a file structure and a free file descriptor.
7699acd1 1124 */
a0998b50
UD
1125 fd = anon_inode_getfd("[eventpoll]", &eventpoll_fops, ep,
1126 flags & O_CLOEXEC);
2030a42c
AV
1127 if (fd < 0)
1128 ep_free(ep);
7699acd1 1129
2030a42c 1130error_return:
7699acd1 1131 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
9fe5ad9c 1132 current, flags, fd));
7699acd1
DL
1133
1134 return fd;
7699acd1
DL
1135}
1136
5a8a82b1 1137SYSCALL_DEFINE1(epoll_create, int, size)
a0998b50 1138{
9fe5ad9c
UD
1139 if (size < 0)
1140 return -EINVAL;
1141
1142 return sys_epoll_create1(0);
a0998b50
UD
1143}
1144
7699acd1
DL
1145/*
1146 * The following function implements the controller interface for
1147 * the eventpoll file that enables the insertion/removal/change of
67647d0f 1148 * file descriptors inside the interest set.
7699acd1 1149 */
5a8a82b1
HC
1150SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
1151 struct epoll_event __user *, event)
7699acd1
DL
1152{
1153 int error;
1154 struct file *file, *tfile;
1155 struct eventpoll *ep;
1156 struct epitem *epi;
1157 struct epoll_event epds;
1158
1159 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n",
1160 current, epfd, op, fd, event));
1161
1162 error = -EFAULT;
1163 if (ep_op_has_event(op) &&
1164 copy_from_user(&epds, event, sizeof(struct epoll_event)))
1165 goto error_return;
1166
1167 /* Get the "struct file *" for the eventpoll file */
1168 error = -EBADF;
1169 file = fget(epfd);
1170 if (!file)
1171 goto error_return;
1172
1173 /* Get the "struct file *" for the target file */
1174 tfile = fget(fd);
1175 if (!tfile)
1176 goto error_fput;
1177
1178 /* The target file descriptor must support poll */
1179 error = -EPERM;
1180 if (!tfile->f_op || !tfile->f_op->poll)
1181 goto error_tgt_fput;
1182
1183 /*
1184 * We have to check that the file structure underneath the file descriptor
1185 * the user passed to us _is_ an eventpoll file. And also we do not permit
1186 * adding an epoll file descriptor inside itself.
1187 */
1188 error = -EINVAL;
1189 if (file == tfile || !is_file_epoll(file))
1190 goto error_tgt_fput;
1191
1192 /*
1193 * At this point it is safe to assume that the "private_data" contains
1194 * our own data structure.
1195 */
1196 ep = file->private_data;
1197
d47de16c 1198 mutex_lock(&ep->mtx);
7699acd1 1199
67647d0f
DL
1200 /*
1201 * Try to lookup the file inside our RB tree, Since we grabbed "mtx"
1202 * above, we can be sure to be able to use the item looked up by
1203 * ep_find() till we release the mutex.
1204 */
7699acd1
DL
1205 epi = ep_find(ep, tfile, fd);
1206
1207 error = -EINVAL;
1208 switch (op) {
1209 case EPOLL_CTL_ADD:
1210 if (!epi) {
1211 epds.events |= POLLERR | POLLHUP;
1212
1213 error = ep_insert(ep, &epds, tfile, fd);
1214 } else
1215 error = -EEXIST;
1216 break;
1217 case EPOLL_CTL_DEL:
1218 if (epi)
1219 error = ep_remove(ep, epi);
1220 else
1221 error = -ENOENT;
1222 break;
1223 case EPOLL_CTL_MOD:
1224 if (epi) {
1225 epds.events |= POLLERR | POLLHUP;
1226 error = ep_modify(ep, epi, &epds);
1227 } else
1228 error = -ENOENT;
1229 break;
1230 }
d47de16c 1231 mutex_unlock(&ep->mtx);
7699acd1
DL
1232
1233error_tgt_fput:
1234 fput(tfile);
1235error_fput:
1236 fput(file);
1237error_return:
1238 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n",
1239 current, epfd, op, fd, event, error));
1240
1241 return error;
1242}
1243
1244/*
1245 * Implement the event wait interface for the eventpoll file. It is the kernel
1246 * part of the user space epoll_wait(2).
1247 */
5a8a82b1
HC
1248SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,
1249 int, maxevents, int, timeout)
7699acd1
DL
1250{
1251 int error;
1252 struct file *file;
1253 struct eventpoll *ep;
1254
1255 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n",
1256 current, epfd, events, maxevents, timeout));
1257
1258 /* The maximum number of event must be greater than zero */
1259 if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)
1260 return -EINVAL;
1261
1262 /* Verify that the area passed by the user is writeable */
1263 if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
1264 error = -EFAULT;
1265 goto error_return;
1266 }
1267
1268 /* Get the "struct file *" for the eventpoll file */
1269 error = -EBADF;
1270 file = fget(epfd);
1271 if (!file)
1272 goto error_return;
1273
1274 /*
1275 * We have to check that the file structure underneath the fd
1276 * the user passed to us _is_ an eventpoll file.
1277 */
1278 error = -EINVAL;
1279 if (!is_file_epoll(file))
1280 goto error_fput;
1281
1282 /*
1283 * At this point it is safe to assume that the "private_data" contains
1284 * our own data structure.
1285 */
1286 ep = file->private_data;
1287
1288 /* Time to fish for events ... */
1289 error = ep_poll(ep, events, maxevents, timeout);
1290
1291error_fput:
1292 fput(file);
1293error_return:
1294 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n",
1295 current, epfd, events, maxevents, timeout, error));
1296
1297 return error;
1298}
1299
f3de272b 1300#ifdef HAVE_SET_RESTORE_SIGMASK
7699acd1
DL
1301
1302/*
1303 * Implement the event wait interface for the eventpoll file. It is the kernel
1304 * part of the user space epoll_pwait(2).
1305 */
5a8a82b1
HC
1306SYSCALL_DEFINE6(epoll_pwait, int, epfd, struct epoll_event __user *, events,
1307 int, maxevents, int, timeout, const sigset_t __user *, sigmask,
1308 size_t, sigsetsize)
7699acd1
DL
1309{
1310 int error;
1311 sigset_t ksigmask, sigsaved;
1312
1313 /*
1314 * If the caller wants a certain signal mask to be set during the wait,
1315 * we apply it here.
1316 */
1317 if (sigmask) {
1318 if (sigsetsize != sizeof(sigset_t))
1319 return -EINVAL;
1320 if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
1321 return -EFAULT;
1322 sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
1323 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1324 }
1325
1326 error = sys_epoll_wait(epfd, events, maxevents, timeout);
1327
1328 /*
1329 * If we changed the signal mask, we need to restore the original one.
1330 * In case we've got a signal while waiting, we do not restore the
1331 * signal mask yet, and we allow do_signal() to deliver the signal on
1332 * the way back to userspace, before the signal mask is restored.
1333 */
1334 if (sigmask) {
1335 if (error == -EINTR) {
1336 memcpy(&current->saved_sigmask, &sigsaved,
c7ea7630 1337 sizeof(sigsaved));
4e4c22c7 1338 set_restore_sigmask();
7699acd1
DL
1339 } else
1340 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1341 }
1342
1343 return error;
1344}
1345
f3de272b 1346#endif /* HAVE_SET_RESTORE_SIGMASK */
7699acd1 1347
1da177e4
LT
1348static int __init eventpoll_init(void)
1349{
7ef9964e
DL
1350 struct sysinfo si;
1351
1352 si_meminfo(&si);
9df04e1f
DL
1353 /*
1354 * Allows top 4% of lomem to be allocated for epoll watches (per user).
1355 */
1356 max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) /
7ef9964e 1357 EP_ITEM_COST;
1da177e4
LT
1358
1359 /* Initialize the structure used to perform safe poll wait head wake ups */
1360 ep_poll_safewake_init(&psw);
1361
1362 /* Allocates slab cache used to allocate "struct epitem" items */
1363 epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
1364 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC,
20c2df83 1365 NULL);
1da177e4
LT
1366
1367 /* Allocates slab cache used to allocate "struct eppoll_entry" */
1368 pwq_cache = kmem_cache_create("eventpoll_pwq",
1369 sizeof(struct eppoll_entry), 0,
20c2df83 1370 EPI_SLAB_DEBUG|SLAB_PANIC, NULL);
1da177e4 1371
1da177e4 1372 return 0;
1da177e4 1373}
cea69241 1374fs_initcall(eventpoll_init);