ia64/linux-2.6.18-xen.hg

view fs/eventpoll.c @ 524:7f8b544237bf

netfront: Allow netfront in domain 0.

This is useful if your physical network device is in a utility domain.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Apr 15 15:18:58 2008 +0100 (2008-04-15)
parents 4763065c587c
children
line source
1 /*
2 * fs/eventpoll.c ( Efficent event polling implementation )
3 * Copyright (C) 2001,...,2006 Davide Libenzi
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 */
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/fs.h>
19 #include <linux/file.h>
20 #include <linux/signal.h>
21 #include <linux/errno.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/poll.h>
25 #include <linux/smp_lock.h>
26 #include <linux/string.h>
27 #include <linux/list.h>
28 #include <linux/hash.h>
29 #include <linux/spinlock.h>
30 #include <linux/syscalls.h>
31 #include <linux/rwsem.h>
32 #include <linux/rbtree.h>
33 #include <linux/wait.h>
34 #include <linux/eventpoll.h>
35 #include <linux/mount.h>
36 #include <linux/bitops.h>
37 #include <linux/mutex.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/io.h>
41 #include <asm/mman.h>
42 #include <asm/atomic.h>
43 #include <asm/semaphore.h>
46 /*
47 * LOCKING:
48 * There are three level of locking required by epoll :
49 *
50 * 1) epmutex (mutex)
51 * 2) ep->sem (rw_semaphore)
52 * 3) ep->lock (rw_lock)
53 *
54 * The acquire order is the one listed above, from 1 to 3.
55 * We need a spinlock (ep->lock) because we manipulate objects
56 * from inside the poll callback, that might be triggered from
57 * a wake_up() that in turn might be called from IRQ context.
58 * So we can't sleep inside the poll callback and hence we need
59 * a spinlock. During the event transfer loop (from kernel to
60 * user space) we could end up sleeping due a copy_to_user(), so
61 * we need a lock that will allow us to sleep. This lock is a
62 * read-write semaphore (ep->sem). It is acquired on read during
63 * the event transfer loop and in write during epoll_ctl(EPOLL_CTL_DEL)
64 * and during eventpoll_release_file(). Then we also need a global
65 * semaphore to serialize eventpoll_release_file() and ep_free().
66 * This semaphore is acquired by ep_free() during the epoll file
67 * cleanup path and it is also acquired by eventpoll_release_file()
68 * if a file has been pushed inside an epoll set and it is then
69 * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL).
70 * It is possible to drop the "ep->sem" and to use the global
71 * semaphore "epmutex" (together with "ep->lock") to have it working,
72 * but having "ep->sem" will make the interface more scalable.
73 * Events that require holding "epmutex" are very rare, while for
74 * normal operations the epoll private "ep->sem" will guarantee
75 * a greater scalability.
76 */
79 #define EVENTPOLLFS_MAGIC 0x03111965 /* My birthday should work for this :) */
81 #define DEBUG_EPOLL 0
83 #if DEBUG_EPOLL > 0
84 #define DPRINTK(x) printk x
85 #define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0)
86 #else /* #if DEBUG_EPOLL > 0 */
87 #define DPRINTK(x) (void) 0
88 #define DNPRINTK(n, x) (void) 0
89 #endif /* #if DEBUG_EPOLL > 0 */
91 #define DEBUG_EPI 0
93 #if DEBUG_EPI != 0
94 #define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */)
95 #else /* #if DEBUG_EPI != 0 */
96 #define EPI_SLAB_DEBUG 0
97 #endif /* #if DEBUG_EPI != 0 */
99 /* Epoll private bits inside the event mask */
100 #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET)
102 /* Maximum number of poll wake up nests we are allowing */
103 #define EP_MAX_POLLWAKE_NESTS 4
105 /* Maximum msec timeout value storeable in a long int */
106 #define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ)
109 struct epoll_filefd {
110 struct file *file;
111 int fd;
112 };
114 /*
115 * Node that is linked into the "wake_task_list" member of the "struct poll_safewake".
116 * It is used to keep track on all tasks that are currently inside the wake_up() code
117 * to 1) short-circuit the one coming from the same task and same wait queue head
118 * ( loop ) 2) allow a maximum number of epoll descriptors inclusion nesting
119 * 3) let go the ones coming from other tasks.
120 */
121 struct wake_task_node {
122 struct list_head llink;
123 struct task_struct *task;
124 wait_queue_head_t *wq;
125 };
127 /*
128 * This is used to implement the safe poll wake up avoiding to reenter
129 * the poll callback from inside wake_up().
130 */
131 struct poll_safewake {
132 struct list_head wake_task_list;
133 spinlock_t lock;
134 };
136 /*
137 * This structure is stored inside the "private_data" member of the file
138 * structure and rapresent the main data sructure for the eventpoll
139 * interface.
140 */
141 struct eventpoll {
142 /* Protect the this structure access */
143 rwlock_t lock;
145 /*
146 * This semaphore is used to ensure that files are not removed
147 * while epoll is using them. This is read-held during the event
148 * collection loop and it is write-held during the file cleanup
149 * path, the epoll file exit code and the ctl operations.
150 */
151 struct rw_semaphore sem;
153 /* Wait queue used by sys_epoll_wait() */
154 wait_queue_head_t wq;
156 /* Wait queue used by file->poll() */
157 wait_queue_head_t poll_wait;
159 /* List of ready file descriptors */
160 struct list_head rdllist;
162 /* RB-Tree root used to store monitored fd structs */
163 struct rb_root rbr;
164 };
166 /* Wait structure used by the poll hooks */
167 struct eppoll_entry {
168 /* List header used to link this structure to the "struct epitem" */
169 struct list_head llink;
171 /* The "base" pointer is set to the container "struct epitem" */
172 void *base;
174 /*
175 * Wait queue item that will be linked to the target file wait
176 * queue head.
177 */
178 wait_queue_t wait;
180 /* The wait queue head that linked the "wait" wait queue item */
181 wait_queue_head_t *whead;
182 };
184 /*
185 * Each file descriptor added to the eventpoll interface will
186 * have an entry of this type linked to the hash.
187 */
188 struct epitem {
189 /* RB-Tree node used to link this structure to the eventpoll rb-tree */
190 struct rb_node rbn;
192 /* List header used to link this structure to the eventpoll ready list */
193 struct list_head rdllink;
195 /* The file descriptor information this item refers to */
196 struct epoll_filefd ffd;
198 /* Number of active wait queue attached to poll operations */
199 int nwait;
201 /* List containing poll wait queues */
202 struct list_head pwqlist;
204 /* The "container" of this item */
205 struct eventpoll *ep;
207 /* The structure that describe the interested events and the source fd */
208 struct epoll_event event;
210 /*
211 * Used to keep track of the usage count of the structure. This avoids
212 * that the structure will desappear from underneath our processing.
213 */
214 atomic_t usecnt;
216 /* List header used to link this item to the "struct file" items list */
217 struct list_head fllink;
219 /* List header used to link the item to the transfer list */
220 struct list_head txlink;
222 /*
223 * This is used during the collection/transfer of events to userspace
224 * to pin items empty events set.
225 */
226 unsigned int revents;
227 };
229 /* Wrapper struct used by poll queueing */
230 struct ep_pqueue {
231 poll_table pt;
232 struct epitem *epi;
233 };
237 static void ep_poll_safewake_init(struct poll_safewake *psw);
238 static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq);
239 static int ep_alloc(struct eventpoll **pep);
240 static void ep_free(struct eventpoll *ep);
241 static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd);
242 static void ep_use_epitem(struct epitem *epi);
243 static void ep_release_epitem(struct epitem *epi);
244 static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
245 poll_table *pt);
246 static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi);
247 static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
248 struct file *tfile, int fd);
249 static int ep_modify(struct eventpoll *ep, struct epitem *epi,
250 struct epoll_event *event);
251 static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi);
252 static int ep_unlink(struct eventpoll *ep, struct epitem *epi);
253 static int ep_remove(struct eventpoll *ep, struct epitem *epi);
254 static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key);
255 static int ep_eventpoll_close(struct inode *inode, struct file *file);
256 static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait);
257 static int ep_collect_ready_items(struct eventpoll *ep,
258 struct list_head *txlist, int maxevents);
259 static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
260 struct epoll_event __user *events);
261 static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist);
262 static int ep_events_transfer(struct eventpoll *ep,
263 struct epoll_event __user *events,
264 int maxevents);
265 static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
266 int maxevents, long timeout);
267 static int eventpollfs_delete_dentry(struct dentry *dentry);
268 static struct inode *ep_eventpoll_inode(const struct file_operations *fops);
269 static int eventpollfs_get_sb(struct file_system_type *fs_type,
270 int flags, const char *dev_name,
271 void *data, struct vfsmount *mnt);
273 /*
274 * This semaphore is used to serialize ep_free() and eventpoll_release_file().
275 */
276 static struct mutex epmutex;
278 /* Safe wake up implementation */
279 static struct poll_safewake psw;
281 /* Slab cache used to allocate "struct epitem" */
282 static kmem_cache_t *epi_cache __read_mostly;
284 /* Slab cache used to allocate "struct eppoll_entry" */
285 static kmem_cache_t *pwq_cache __read_mostly;
287 /* Virtual fs used to allocate inodes for eventpoll files */
288 static struct vfsmount *eventpoll_mnt __read_mostly;
290 /* File callbacks that implement the eventpoll file behaviour */
291 static const struct file_operations eventpoll_fops = {
292 .release = ep_eventpoll_close,
293 .poll = ep_eventpoll_poll
294 };
296 /*
297 * This is used to register the virtual file system from where
298 * eventpoll inodes are allocated.
299 */
300 static struct file_system_type eventpoll_fs_type = {
301 .name = "eventpollfs",
302 .get_sb = eventpollfs_get_sb,
303 .kill_sb = kill_anon_super,
304 };
306 /* Very basic directory entry operations for the eventpoll virtual file system */
307 static struct dentry_operations eventpollfs_dentry_operations = {
308 .d_delete = eventpollfs_delete_dentry,
309 };
313 /* Fast test to see if the file is an evenpoll file */
314 static inline int is_file_epoll(struct file *f)
315 {
316 return f->f_op == &eventpoll_fops;
317 }
319 /* Setup the structure that is used as key for the rb-tree */
320 static inline void ep_set_ffd(struct epoll_filefd *ffd,
321 struct file *file, int fd)
322 {
323 ffd->file = file;
324 ffd->fd = fd;
325 }
327 /* Compare rb-tree keys */
328 static inline int ep_cmp_ffd(struct epoll_filefd *p1,
329 struct epoll_filefd *p2)
330 {
331 return (p1->file > p2->file ? +1:
332 (p1->file < p2->file ? -1 : p1->fd - p2->fd));
333 }
335 /* Special initialization for the rb-tree node to detect linkage */
336 static inline void ep_rb_initnode(struct rb_node *n)
337 {
338 rb_set_parent(n, n);
339 }
341 /* Removes a node from the rb-tree and marks it for a fast is-linked check */
342 static inline void ep_rb_erase(struct rb_node *n, struct rb_root *r)
343 {
344 rb_erase(n, r);
345 rb_set_parent(n, n);
346 }
348 /* Fast check to verify that the item is linked to the main rb-tree */
349 static inline int ep_rb_linked(struct rb_node *n)
350 {
351 return rb_parent(n) != n;
352 }
354 /*
355 * Remove the item from the list and perform its initialization.
356 * This is useful for us because we can test if the item is linked
357 * using "ep_is_linked(p)".
358 */
359 static inline void ep_list_del(struct list_head *p)
360 {
361 list_del(p);
362 INIT_LIST_HEAD(p);
363 }
365 /* Tells us if the item is currently linked */
366 static inline int ep_is_linked(struct list_head *p)
367 {
368 return !list_empty(p);
369 }
371 /* Get the "struct epitem" from a wait queue pointer */
372 static inline struct epitem * ep_item_from_wait(wait_queue_t *p)
373 {
374 return container_of(p, struct eppoll_entry, wait)->base;
375 }
377 /* Get the "struct epitem" from an epoll queue wrapper */
378 static inline struct epitem * ep_item_from_epqueue(poll_table *p)
379 {
380 return container_of(p, struct ep_pqueue, pt)->epi;
381 }
383 /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
384 static inline int ep_op_hash_event(int op)
385 {
386 return op != EPOLL_CTL_DEL;
387 }
389 /* Initialize the poll safe wake up structure */
390 static void ep_poll_safewake_init(struct poll_safewake *psw)
391 {
393 INIT_LIST_HEAD(&psw->wake_task_list);
394 spin_lock_init(&psw->lock);
395 }
398 /*
399 * Perform a safe wake up of the poll wait list. The problem is that
400 * with the new callback'd wake up system, it is possible that the
401 * poll callback is reentered from inside the call to wake_up() done
402 * on the poll wait queue head. The rule is that we cannot reenter the
403 * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times,
404 * and we cannot reenter the same wait queue head at all. This will
405 * enable to have a hierarchy of epoll file descriptor of no more than
406 * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock
407 * because this one gets called by the poll callback, that in turn is called
408 * from inside a wake_up(), that might be called from irq context.
409 */
410 static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
411 {
412 int wake_nests = 0;
413 unsigned long flags;
414 struct task_struct *this_task = current;
415 struct list_head *lsthead = &psw->wake_task_list, *lnk;
416 struct wake_task_node *tncur;
417 struct wake_task_node tnode;
419 spin_lock_irqsave(&psw->lock, flags);
421 /* Try to see if the current task is already inside this wakeup call */
422 list_for_each(lnk, lsthead) {
423 tncur = list_entry(lnk, struct wake_task_node, llink);
425 if (tncur->wq == wq ||
426 (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) {
427 /*
428 * Ops ... loop detected or maximum nest level reached.
429 * We abort this wake by breaking the cycle itself.
430 */
431 spin_unlock_irqrestore(&psw->lock, flags);
432 return;
433 }
434 }
436 /* Add the current task to the list */
437 tnode.task = this_task;
438 tnode.wq = wq;
439 list_add(&tnode.llink, lsthead);
441 spin_unlock_irqrestore(&psw->lock, flags);
443 /* Do really wake up now */
444 wake_up(wq);
446 /* Remove the current task from the list */
447 spin_lock_irqsave(&psw->lock, flags);
448 list_del(&tnode.llink);
449 spin_unlock_irqrestore(&psw->lock, flags);
450 }
453 /*
454 * This is called from eventpoll_release() to unlink files from the eventpoll
455 * interface. We need to have this facility to cleanup correctly files that are
456 * closed without being removed from the eventpoll interface.
457 */
458 void eventpoll_release_file(struct file *file)
459 {
460 struct list_head *lsthead = &file->f_ep_links;
461 struct eventpoll *ep;
462 struct epitem *epi;
464 /*
465 * We don't want to get "file->f_ep_lock" because it is not
466 * necessary. It is not necessary because we're in the "struct file"
467 * cleanup path, and this means that noone is using this file anymore.
468 * The only hit might come from ep_free() but by holding the semaphore
469 * will correctly serialize the operation. We do need to acquire
470 * "ep->sem" after "epmutex" because ep_remove() requires it when called
471 * from anywhere but ep_free().
472 */
473 mutex_lock(&epmutex);
475 while (!list_empty(lsthead)) {
476 epi = list_entry(lsthead->next, struct epitem, fllink);
478 ep = epi->ep;
479 ep_list_del(&epi->fllink);
480 down_write(&ep->sem);
481 ep_remove(ep, epi);
482 up_write(&ep->sem);
483 }
485 mutex_unlock(&epmutex);
486 }
489 /*
490 * It opens an eventpoll file descriptor by suggesting a storage of "size"
491 * file descriptors. The size parameter is just an hint about how to size
492 * data structures. It won't prevent the user to store more than "size"
493 * file descriptors inside the epoll interface. It is the kernel part of
494 * the userspace epoll_create(2).
495 */
496 asmlinkage long sys_epoll_create(int size)
497 {
498 int error, fd;
499 struct eventpoll *ep;
500 struct inode *inode;
501 struct file *file;
503 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
504 current, size));
506 /*
507 * Sanity check on the size parameter, and create the internal data
508 * structure ( "struct eventpoll" ).
509 */
510 error = -EINVAL;
511 if (size <= 0 || (error = ep_alloc(&ep)) != 0)
512 goto eexit_1;
514 /*
515 * Creates all the items needed to setup an eventpoll file. That is,
516 * a file structure, and inode and a free file descriptor.
517 */
518 error = ep_getfd(&fd, &inode, &file, ep, &eventpoll_fops);
519 if (error)
520 goto eexit_2;
522 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
523 current, size, fd));
525 return fd;
527 eexit_2:
528 ep_free(ep);
529 kfree(ep);
530 eexit_1:
531 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
532 current, size, error));
533 return error;
534 }
537 /*
538 * The following function implements the controller interface for
539 * the eventpoll file that enables the insertion/removal/change of
540 * file descriptors inside the interest set. It represents
541 * the kernel part of the user space epoll_ctl(2).
542 */
543 asmlinkage long
544 sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event)
545 {
546 int error;
547 struct file *file, *tfile;
548 struct eventpoll *ep;
549 struct epitem *epi;
550 struct epoll_event epds;
552 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n",
553 current, epfd, op, fd, event));
555 error = -EFAULT;
556 if (ep_op_hash_event(op) &&
557 copy_from_user(&epds, event, sizeof(struct epoll_event)))
558 goto eexit_1;
560 /* Get the "struct file *" for the eventpoll file */
561 error = -EBADF;
562 file = fget(epfd);
563 if (!file)
564 goto eexit_1;
566 /* Get the "struct file *" for the target file */
567 tfile = fget(fd);
568 if (!tfile)
569 goto eexit_2;
571 /* The target file descriptor must support poll */
572 error = -EPERM;
573 if (!tfile->f_op || !tfile->f_op->poll)
574 goto eexit_3;
576 /*
577 * We have to check that the file structure underneath the file descriptor
578 * the user passed to us _is_ an eventpoll file. And also we do not permit
579 * adding an epoll file descriptor inside itself.
580 */
581 error = -EINVAL;
582 if (file == tfile || !is_file_epoll(file))
583 goto eexit_3;
585 /*
586 * At this point it is safe to assume that the "private_data" contains
587 * our own data structure.
588 */
589 ep = file->private_data;
591 down_write(&ep->sem);
593 /* Try to lookup the file inside our hash table */
594 epi = ep_find(ep, tfile, fd);
596 error = -EINVAL;
597 switch (op) {
598 case EPOLL_CTL_ADD:
599 if (!epi) {
600 epds.events |= POLLERR | POLLHUP;
602 error = ep_insert(ep, &epds, tfile, fd);
603 } else
604 error = -EEXIST;
605 break;
606 case EPOLL_CTL_DEL:
607 if (epi)
608 error = ep_remove(ep, epi);
609 else
610 error = -ENOENT;
611 break;
612 case EPOLL_CTL_MOD:
613 if (epi) {
614 epds.events |= POLLERR | POLLHUP;
615 error = ep_modify(ep, epi, &epds);
616 } else
617 error = -ENOENT;
618 break;
619 }
621 /*
622 * The function ep_find() increments the usage count of the structure
623 * so, if this is not NULL, we need to release it.
624 */
625 if (epi)
626 ep_release_epitem(epi);
628 up_write(&ep->sem);
630 eexit_3:
631 fput(tfile);
632 eexit_2:
633 fput(file);
634 eexit_1:
635 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n",
636 current, epfd, op, fd, event, error));
638 return error;
639 }
641 #define MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
643 /*
644 * Implement the event wait interface for the eventpoll file. It is the kernel
645 * part of the user space epoll_wait(2).
646 */
647 asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
648 int maxevents, int timeout)
649 {
650 int error;
651 struct file *file;
652 struct eventpoll *ep;
654 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n",
655 current, epfd, events, maxevents, timeout));
657 /* The maximum number of event must be greater than zero */
658 if (maxevents <= 0 || maxevents > MAX_EVENTS)
659 return -EINVAL;
661 /* Verify that the area passed by the user is writeable */
662 if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
663 error = -EFAULT;
664 goto eexit_1;
665 }
667 /* Get the "struct file *" for the eventpoll file */
668 error = -EBADF;
669 file = fget(epfd);
670 if (!file)
671 goto eexit_1;
673 /*
674 * We have to check that the file structure underneath the fd
675 * the user passed to us _is_ an eventpoll file.
676 */
677 error = -EINVAL;
678 if (!is_file_epoll(file))
679 goto eexit_2;
681 /*
682 * At this point it is safe to assume that the "private_data" contains
683 * our own data structure.
684 */
685 ep = file->private_data;
687 /* Time to fish for events ... */
688 error = ep_poll(ep, events, maxevents, timeout);
690 eexit_2:
691 fput(file);
692 eexit_1:
693 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n",
694 current, epfd, events, maxevents, timeout, error));
696 return error;
697 }
700 /*
701 * Creates the file descriptor to be used by the epoll interface.
702 */
703 int ep_getfd(int *efd, struct inode **einode, struct file **efile,
704 struct eventpoll *ep, const struct file_operations *fops)
705 {
706 struct qstr this;
707 char name[32];
708 struct dentry *dentry;
709 struct inode *inode;
710 struct file *file;
711 int error, fd;
713 /* Get an ready to use file */
714 error = -ENFILE;
715 file = get_empty_filp();
716 if (!file)
717 goto eexit_1;
719 /* Allocates an inode from the eventpoll file system */
720 inode = ep_eventpoll_inode(fops);
721 error = PTR_ERR(inode);
722 if (IS_ERR(inode))
723 goto eexit_2;
725 /* Allocates a free descriptor to plug the file onto */
726 error = get_unused_fd();
727 if (error < 0)
728 goto eexit_3;
729 fd = error;
731 /*
732 * Link the inode to a directory entry by creating a unique name
733 * using the inode number.
734 */
735 error = -ENOMEM;
736 sprintf(name, "[%lu]", inode->i_ino);
737 this.name = name;
738 this.len = strlen(name);
739 this.hash = inode->i_ino;
740 dentry = d_alloc(eventpoll_mnt->mnt_sb->s_root, &this);
741 if (!dentry)
742 goto eexit_4;
743 dentry->d_op = &eventpollfs_dentry_operations;
744 d_add(dentry, inode);
745 file->f_vfsmnt = mntget(eventpoll_mnt);
746 file->f_dentry = dentry;
747 file->f_mapping = inode->i_mapping;
749 file->f_pos = 0;
750 file->f_flags = O_RDONLY;
751 file->f_op = fops;
752 file->f_mode = FMODE_READ;
753 file->f_version = 0;
754 file->private_data = ep;
756 /* Install the new setup file into the allocated fd. */
757 fd_install(fd, file);
759 *efd = fd;
760 *einode = inode;
761 *efile = file;
762 return 0;
764 eexit_4:
765 put_unused_fd(fd);
766 eexit_3:
767 iput(inode);
768 eexit_2:
769 put_filp(file);
770 eexit_1:
771 return error;
772 }
775 static int ep_alloc(struct eventpoll **pep)
776 {
777 struct eventpoll *ep = kzalloc(sizeof(*ep), GFP_KERNEL);
779 if (!ep)
780 return -ENOMEM;
782 rwlock_init(&ep->lock);
783 init_rwsem(&ep->sem);
784 init_waitqueue_head(&ep->wq);
785 init_waitqueue_head(&ep->poll_wait);
786 INIT_LIST_HEAD(&ep->rdllist);
787 ep->rbr = RB_ROOT;
789 *pep = ep;
791 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n",
792 current, ep));
793 return 0;
794 }
797 static void ep_free(struct eventpoll *ep)
798 {
799 struct rb_node *rbp;
800 struct epitem *epi;
802 /* We need to release all tasks waiting for these file */
803 if (waitqueue_active(&ep->poll_wait))
804 ep_poll_safewake(&psw, &ep->poll_wait);
806 /*
807 * We need to lock this because we could be hit by
808 * eventpoll_release_file() while we're freeing the "struct eventpoll".
809 * We do not need to hold "ep->sem" here because the epoll file
810 * is on the way to be removed and no one has references to it
811 * anymore. The only hit might come from eventpoll_release_file() but
812 * holding "epmutex" is sufficent here.
813 */
814 mutex_lock(&epmutex);
816 /*
817 * Walks through the whole tree by unregistering poll callbacks.
818 */
819 for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
820 epi = rb_entry(rbp, struct epitem, rbn);
822 ep_unregister_pollwait(ep, epi);
823 }
825 /*
826 * Walks through the whole hash by freeing each "struct epitem". At this
827 * point we are sure no poll callbacks will be lingering around, and also by
828 * write-holding "sem" we can be sure that no file cleanup code will hit
829 * us during this operation. So we can avoid the lock on "ep->lock".
830 */
831 while ((rbp = rb_first(&ep->rbr)) != 0) {
832 epi = rb_entry(rbp, struct epitem, rbn);
833 ep_remove(ep, epi);
834 }
836 mutex_unlock(&epmutex);
837 }
840 /*
841 * Search the file inside the eventpoll hash. It add usage count to
842 * the returned item, so the caller must call ep_release_epitem()
843 * after finished using the "struct epitem".
844 */
845 static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
846 {
847 int kcmp;
848 unsigned long flags;
849 struct rb_node *rbp;
850 struct epitem *epi, *epir = NULL;
851 struct epoll_filefd ffd;
853 ep_set_ffd(&ffd, file, fd);
854 read_lock_irqsave(&ep->lock, flags);
855 for (rbp = ep->rbr.rb_node; rbp; ) {
856 epi = rb_entry(rbp, struct epitem, rbn);
857 kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
858 if (kcmp > 0)
859 rbp = rbp->rb_right;
860 else if (kcmp < 0)
861 rbp = rbp->rb_left;
862 else {
863 ep_use_epitem(epi);
864 epir = epi;
865 break;
866 }
867 }
868 read_unlock_irqrestore(&ep->lock, flags);
870 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n",
871 current, file, epir));
873 return epir;
874 }
877 /*
878 * Increment the usage count of the "struct epitem" making it sure
879 * that the user will have a valid pointer to reference.
880 */
881 static void ep_use_epitem(struct epitem *epi)
882 {
884 atomic_inc(&epi->usecnt);
885 }
888 /*
889 * Decrement ( release ) the usage count by signaling that the user
890 * has finished using the structure. It might lead to freeing the
891 * structure itself if the count goes to zero.
892 */
893 static void ep_release_epitem(struct epitem *epi)
894 {
896 if (atomic_dec_and_test(&epi->usecnt))
897 kmem_cache_free(epi_cache, epi);
898 }
901 /*
902 * This is the callback that is used to add our wait queue to the
903 * target file wakeup lists.
904 */
905 static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
906 poll_table *pt)
907 {
908 struct epitem *epi = ep_item_from_epqueue(pt);
909 struct eppoll_entry *pwq;
911 if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, SLAB_KERNEL))) {
912 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
913 pwq->whead = whead;
914 pwq->base = epi;
915 add_wait_queue(whead, &pwq->wait);
916 list_add_tail(&pwq->llink, &epi->pwqlist);
917 epi->nwait++;
918 } else {
919 /* We have to signal that an error occurred */
920 epi->nwait = -1;
921 }
922 }
925 static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
926 {
927 int kcmp;
928 struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
929 struct epitem *epic;
931 while (*p) {
932 parent = *p;
933 epic = rb_entry(parent, struct epitem, rbn);
934 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
935 if (kcmp > 0)
936 p = &parent->rb_right;
937 else
938 p = &parent->rb_left;
939 }
940 rb_link_node(&epi->rbn, parent, p);
941 rb_insert_color(&epi->rbn, &ep->rbr);
942 }
945 static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
946 struct file *tfile, int fd)
947 {
948 int error, revents, pwake = 0;
949 unsigned long flags;
950 struct epitem *epi;
951 struct ep_pqueue epq;
953 error = -ENOMEM;
954 if (!(epi = kmem_cache_alloc(epi_cache, SLAB_KERNEL)))
955 goto eexit_1;
957 /* Item initialization follow here ... */
958 ep_rb_initnode(&epi->rbn);
959 INIT_LIST_HEAD(&epi->rdllink);
960 INIT_LIST_HEAD(&epi->fllink);
961 INIT_LIST_HEAD(&epi->txlink);
962 INIT_LIST_HEAD(&epi->pwqlist);
963 epi->ep = ep;
964 ep_set_ffd(&epi->ffd, tfile, fd);
965 epi->event = *event;
966 atomic_set(&epi->usecnt, 1);
967 epi->nwait = 0;
969 /* Initialize the poll table using the queue callback */
970 epq.epi = epi;
971 init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
973 /*
974 * Attach the item to the poll hooks and get current event bits.
975 * We can safely use the file* here because its usage count has
976 * been increased by the caller of this function.
977 */
978 revents = tfile->f_op->poll(tfile, &epq.pt);
980 /*
981 * We have to check if something went wrong during the poll wait queue
982 * install process. Namely an allocation for a wait queue failed due
983 * high memory pressure.
984 */
985 if (epi->nwait < 0)
986 goto eexit_2;
988 /* Add the current item to the list of active epoll hook for this file */
989 spin_lock(&tfile->f_ep_lock);
990 list_add_tail(&epi->fllink, &tfile->f_ep_links);
991 spin_unlock(&tfile->f_ep_lock);
993 /* We have to drop the new item inside our item list to keep track of it */
994 write_lock_irqsave(&ep->lock, flags);
996 /* Add the current item to the rb-tree */
997 ep_rbtree_insert(ep, epi);
999 /* If the file is already "ready" we drop it inside the ready list */
1000 if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
1001 list_add_tail(&epi->rdllink, &ep->rdllist);
1003 /* Notify waiting tasks that events are available */
1004 if (waitqueue_active(&ep->wq))
1005 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE);
1006 if (waitqueue_active(&ep->poll_wait))
1007 pwake++;
1010 write_unlock_irqrestore(&ep->lock, flags);
1012 /* We have to call this outside the lock */
1013 if (pwake)
1014 ep_poll_safewake(&psw, &ep->poll_wait);
1016 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n",
1017 current, ep, tfile, fd));
1019 return 0;
1021 eexit_2:
1022 ep_unregister_pollwait(ep, epi);
1024 /*
1025 * We need to do this because an event could have been arrived on some
1026 * allocated wait queue.
1027 */
1028 write_lock_irqsave(&ep->lock, flags);
1029 if (ep_is_linked(&epi->rdllink))
1030 ep_list_del(&epi->rdllink);
1031 write_unlock_irqrestore(&ep->lock, flags);
1033 kmem_cache_free(epi_cache, epi);
1034 eexit_1:
1035 return error;
1039 /*
1040 * Modify the interest event mask by dropping an event if the new mask
1041 * has a match in the current file status.
1042 */
1043 static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
1045 int pwake = 0;
1046 unsigned int revents;
1047 unsigned long flags;
1049 /*
1050 * Set the new event interest mask before calling f_op->poll(), otherwise
1051 * a potential race might occur. In fact if we do this operation inside
1052 * the lock, an event might happen between the f_op->poll() call and the
1053 * new event set registering.
1054 */
1055 epi->event.events = event->events;
1057 /*
1058 * Get current event bits. We can safely use the file* here because
1059 * its usage count has been increased by the caller of this function.
1060 */
1061 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
1063 write_lock_irqsave(&ep->lock, flags);
1065 /* Copy the data member from inside the lock */
1066 epi->event.data = event->data;
1068 /*
1069 * If the item is not linked to the hash it means that it's on its
1070 * way toward the removal. Do nothing in this case.
1071 */
1072 if (ep_rb_linked(&epi->rbn)) {
1073 /*
1074 * If the item is "hot" and it is not registered inside the ready
1075 * list, push it inside. If the item is not "hot" and it is currently
1076 * registered inside the ready list, unlink it.
1077 */
1078 if (revents & event->events) {
1079 if (!ep_is_linked(&epi->rdllink)) {
1080 list_add_tail(&epi->rdllink, &ep->rdllist);
1082 /* Notify waiting tasks that events are available */
1083 if (waitqueue_active(&ep->wq))
1084 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
1085 TASK_INTERRUPTIBLE);
1086 if (waitqueue_active(&ep->poll_wait))
1087 pwake++;
1092 write_unlock_irqrestore(&ep->lock, flags);
1094 /* We have to call this outside the lock */
1095 if (pwake)
1096 ep_poll_safewake(&psw, &ep->poll_wait);
1098 return 0;
1102 /*
1103 * This function unregister poll callbacks from the associated file descriptor.
1104 * Since this must be called without holding "ep->lock" the atomic exchange trick
1105 * will protect us from multiple unregister.
1106 */
1107 static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
1109 int nwait;
1110 struct list_head *lsthead = &epi->pwqlist;
1111 struct eppoll_entry *pwq;
1113 /* This is called without locks, so we need the atomic exchange */
1114 nwait = xchg(&epi->nwait, 0);
1116 if (nwait) {
1117 while (!list_empty(lsthead)) {
1118 pwq = list_entry(lsthead->next, struct eppoll_entry, llink);
1120 ep_list_del(&pwq->llink);
1121 remove_wait_queue(pwq->whead, &pwq->wait);
1122 kmem_cache_free(pwq_cache, pwq);
1128 /*
1129 * Unlink the "struct epitem" from all places it might have been hooked up.
1130 * This function must be called with write IRQ lock on "ep->lock".
1131 */
1132 static int ep_unlink(struct eventpoll *ep, struct epitem *epi)
1134 int error;
1136 /*
1137 * It can happen that this one is called for an item already unlinked.
1138 * The check protect us from doing a double unlink ( crash ).
1139 */
1140 error = -ENOENT;
1141 if (!ep_rb_linked(&epi->rbn))
1142 goto eexit_1;
1144 /*
1145 * Clear the event mask for the unlinked item. This will avoid item
1146 * notifications to be sent after the unlink operation from inside
1147 * the kernel->userspace event transfer loop.
1148 */
1149 epi->event.events = 0;
1151 /*
1152 * At this point is safe to do the job, unlink the item from our rb-tree.
1153 * This operation togheter with the above check closes the door to
1154 * double unlinks.
1155 */
1156 ep_rb_erase(&epi->rbn, &ep->rbr);
1158 /*
1159 * If the item we are going to remove is inside the ready file descriptors
1160 * we want to remove it from this list to avoid stale events.
1161 */
1162 if (ep_is_linked(&epi->rdllink))
1163 ep_list_del(&epi->rdllink);
1165 error = 0;
1166 eexit_1:
1168 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n",
1169 current, ep, epi->ffd.file, error));
1171 return error;
1175 /*
1176 * Removes a "struct epitem" from the eventpoll hash and deallocates
1177 * all the associated resources.
1178 */
1179 static int ep_remove(struct eventpoll *ep, struct epitem *epi)
1181 int error;
1182 unsigned long flags;
1183 struct file *file = epi->ffd.file;
1185 /*
1186 * Removes poll wait queue hooks. We _have_ to do this without holding
1187 * the "ep->lock" otherwise a deadlock might occur. This because of the
1188 * sequence of the lock acquisition. Here we do "ep->lock" then the wait
1189 * queue head lock when unregistering the wait queue. The wakeup callback
1190 * will run by holding the wait queue head lock and will call our callback
1191 * that will try to get "ep->lock".
1192 */
1193 ep_unregister_pollwait(ep, epi);
1195 /* Remove the current item from the list of epoll hooks */
1196 spin_lock(&file->f_ep_lock);
1197 if (ep_is_linked(&epi->fllink))
1198 ep_list_del(&epi->fllink);
1199 spin_unlock(&file->f_ep_lock);
1201 /* We need to acquire the write IRQ lock before calling ep_unlink() */
1202 write_lock_irqsave(&ep->lock, flags);
1204 /* Really unlink the item from the hash */
1205 error = ep_unlink(ep, epi);
1207 write_unlock_irqrestore(&ep->lock, flags);
1209 if (error)
1210 goto eexit_1;
1212 /* At this point it is safe to free the eventpoll item */
1213 ep_release_epitem(epi);
1215 error = 0;
1216 eexit_1:
1217 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p) = %d\n",
1218 current, ep, file, error));
1220 return error;
1224 /*
1225 * This is the callback that is passed to the wait queue wakeup
1226 * machanism. It is called by the stored file descriptors when they
1227 * have events to report.
1228 */
1229 static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
1231 int pwake = 0;
1232 unsigned long flags;
1233 struct epitem *epi = ep_item_from_wait(wait);
1234 struct eventpoll *ep = epi->ep;
1236 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
1237 current, epi->ffd.file, epi, ep));
1239 write_lock_irqsave(&ep->lock, flags);
1241 /*
1242 * If the event mask does not contain any poll(2) event, we consider the
1243 * descriptor to be disabled. This condition is likely the effect of the
1244 * EPOLLONESHOT bit that disables the descriptor when an event is received,
1245 * until the next EPOLL_CTL_MOD will be issued.
1246 */
1247 if (!(epi->event.events & ~EP_PRIVATE_BITS))
1248 goto is_disabled;
1250 /* If this file is already in the ready list we exit soon */
1251 if (ep_is_linked(&epi->rdllink))
1252 goto is_linked;
1254 list_add_tail(&epi->rdllink, &ep->rdllist);
1256 is_linked:
1257 /*
1258 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
1259 * wait list.
1260 */
1261 if (waitqueue_active(&ep->wq))
1262 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
1263 TASK_INTERRUPTIBLE);
1264 if (waitqueue_active(&ep->poll_wait))
1265 pwake++;
1267 is_disabled:
1268 write_unlock_irqrestore(&ep->lock, flags);
1270 /* We have to call this outside the lock */
1271 if (pwake)
1272 ep_poll_safewake(&psw, &ep->poll_wait);
1274 return 1;
1278 static int ep_eventpoll_close(struct inode *inode, struct file *file)
1280 struct eventpoll *ep = file->private_data;
1282 if (ep) {
1283 ep_free(ep);
1284 kfree(ep);
1287 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep));
1288 return 0;
1292 static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
1294 unsigned int pollflags = 0;
1295 unsigned long flags;
1296 struct eventpoll *ep = file->private_data;
1298 /* Insert inside our poll wait queue */
1299 poll_wait(file, &ep->poll_wait, wait);
1301 /* Check our condition */
1302 read_lock_irqsave(&ep->lock, flags);
1303 if (!list_empty(&ep->rdllist))
1304 pollflags = POLLIN | POLLRDNORM;
1305 read_unlock_irqrestore(&ep->lock, flags);
1307 return pollflags;
1311 /*
1312 * Since we have to release the lock during the __copy_to_user() operation and
1313 * during the f_op->poll() call, we try to collect the maximum number of items
1314 * by reducing the irqlock/irqunlock switching rate.
1315 */
1316 static int ep_collect_ready_items(struct eventpoll *ep, struct list_head *txlist, int maxevents)
1318 int nepi;
1319 unsigned long flags;
1320 struct list_head *lsthead = &ep->rdllist, *lnk;
1321 struct epitem *epi;
1323 write_lock_irqsave(&ep->lock, flags);
1325 for (nepi = 0, lnk = lsthead->next; lnk != lsthead && nepi < maxevents;) {
1326 epi = list_entry(lnk, struct epitem, rdllink);
1328 lnk = lnk->next;
1330 /* If this file is already in the ready list we exit soon */
1331 if (!ep_is_linked(&epi->txlink)) {
1332 /*
1333 * This is initialized in this way so that the default
1334 * behaviour of the reinjecting code will be to push back
1335 * the item inside the ready list.
1336 */
1337 epi->revents = epi->event.events;
1339 /* Link the ready item into the transfer list */
1340 list_add(&epi->txlink, txlist);
1341 nepi++;
1343 /*
1344 * Unlink the item from the ready list.
1345 */
1346 ep_list_del(&epi->rdllink);
1350 write_unlock_irqrestore(&ep->lock, flags);
1352 return nepi;
1356 /*
1357 * This function is called without holding the "ep->lock" since the call to
1358 * __copy_to_user() might sleep, and also f_op->poll() might reenable the IRQ
1359 * because of the way poll() is traditionally implemented in Linux.
1360 */
1361 static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
1362 struct epoll_event __user *events)
1364 int eventcnt = 0;
1365 unsigned int revents;
1366 struct list_head *lnk;
1367 struct epitem *epi;
1369 /*
1370 * We can loop without lock because this is a task private list.
1371 * The test done during the collection loop will guarantee us that
1372 * another task will not try to collect this file. Also, items
1373 * cannot vanish during the loop because we are holding "sem".
1374 */
1375 list_for_each(lnk, txlist) {
1376 epi = list_entry(lnk, struct epitem, txlink);
1378 /*
1379 * Get the ready file event set. We can safely use the file
1380 * because we are holding the "sem" in read and this will
1381 * guarantee that both the file and the item will not vanish.
1382 */
1383 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
1385 /*
1386 * Set the return event set for the current file descriptor.
1387 * Note that only the task task was successfully able to link
1388 * the item to its "txlist" will write this field.
1389 */
1390 epi->revents = revents & epi->event.events;
1392 if (epi->revents) {
1393 if (__put_user(epi->revents,
1394 &events[eventcnt].events) ||
1395 __put_user(epi->event.data,
1396 &events[eventcnt].data))
1397 return -EFAULT;
1398 if (epi->event.events & EPOLLONESHOT)
1399 epi->event.events &= EP_PRIVATE_BITS;
1400 eventcnt++;
1403 return eventcnt;
1407 /*
1408 * Walk through the transfer list we collected with ep_collect_ready_items()
1409 * and, if 1) the item is still "alive" 2) its event set is not empty 3) it's
1410 * not already linked, links it to the ready list. Same as above, we are holding
1411 * "sem" so items cannot vanish underneath our nose.
1412 */
1413 static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist)
1415 int ricnt = 0, pwake = 0;
1416 unsigned long flags;
1417 struct epitem *epi;
1419 write_lock_irqsave(&ep->lock, flags);
1421 while (!list_empty(txlist)) {
1422 epi = list_entry(txlist->next, struct epitem, txlink);
1424 /* Unlink the current item from the transfer list */
1425 ep_list_del(&epi->txlink);
1427 /*
1428 * If the item is no more linked to the interest set, we don't
1429 * have to push it inside the ready list because the following
1430 * ep_release_epitem() is going to drop it. Also, if the current
1431 * item is set to have an Edge Triggered behaviour, we don't have
1432 * to push it back either.
1433 */
1434 if (ep_rb_linked(&epi->rbn) && !(epi->event.events & EPOLLET) &&
1435 (epi->revents & epi->event.events) && !ep_is_linked(&epi->rdllink)) {
1436 list_add_tail(&epi->rdllink, &ep->rdllist);
1437 ricnt++;
1441 if (ricnt) {
1442 /*
1443 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
1444 * wait list.
1445 */
1446 if (waitqueue_active(&ep->wq))
1447 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
1448 TASK_INTERRUPTIBLE);
1449 if (waitqueue_active(&ep->poll_wait))
1450 pwake++;
1453 write_unlock_irqrestore(&ep->lock, flags);
1455 /* We have to call this outside the lock */
1456 if (pwake)
1457 ep_poll_safewake(&psw, &ep->poll_wait);
1461 /*
1462 * Perform the transfer of events to user space.
1463 */
1464 static int ep_events_transfer(struct eventpoll *ep,
1465 struct epoll_event __user *events, int maxevents)
1467 int eventcnt = 0;
1468 struct list_head txlist;
1470 INIT_LIST_HEAD(&txlist);
1472 /*
1473 * We need to lock this because we could be hit by
1474 * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
1475 */
1476 down_read(&ep->sem);
1478 /* Collect/extract ready items */
1479 if (ep_collect_ready_items(ep, &txlist, maxevents) > 0) {
1480 /* Build result set in userspace */
1481 eventcnt = ep_send_events(ep, &txlist, events);
1483 /* Reinject ready items into the ready list */
1484 ep_reinject_items(ep, &txlist);
1487 up_read(&ep->sem);
1489 return eventcnt;
1493 static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
1494 int maxevents, long timeout)
1496 int res, eavail;
1497 unsigned long flags;
1498 long jtimeout;
1499 wait_queue_t wait;
1501 /*
1502 * Calculate the timeout by checking for the "infinite" value ( -1 )
1503 * and the overflow condition. The passed timeout is in milliseconds,
1504 * that why (t * HZ) / 1000.
1505 */
1506 jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ?
1507 MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
1509 retry:
1510 write_lock_irqsave(&ep->lock, flags);
1512 res = 0;
1513 if (list_empty(&ep->rdllist)) {
1514 /*
1515 * We don't have any available event to return to the caller.
1516 * We need to sleep here, and we will be wake up by
1517 * ep_poll_callback() when events will become available.
1518 */
1519 init_waitqueue_entry(&wait, current);
1520 __add_wait_queue(&ep->wq, &wait);
1522 for (;;) {
1523 /*
1524 * We don't want to sleep if the ep_poll_callback() sends us
1525 * a wakeup in between. That's why we set the task state
1526 * to TASK_INTERRUPTIBLE before doing the checks.
1527 */
1528 set_current_state(TASK_INTERRUPTIBLE);
1529 if (!list_empty(&ep->rdllist) || !jtimeout)
1530 break;
1531 if (signal_pending(current)) {
1532 res = -EINTR;
1533 break;
1536 write_unlock_irqrestore(&ep->lock, flags);
1537 jtimeout = schedule_timeout(jtimeout);
1538 write_lock_irqsave(&ep->lock, flags);
1540 __remove_wait_queue(&ep->wq, &wait);
1542 set_current_state(TASK_RUNNING);
1545 /* Is it worth to try to dig for events ? */
1546 eavail = !list_empty(&ep->rdllist);
1548 write_unlock_irqrestore(&ep->lock, flags);
1550 /*
1551 * Try to transfer events to user space. In case we get 0 events and
1552 * there's still timeout left over, we go trying again in search of
1553 * more luck.
1554 */
1555 if (!res && eavail &&
1556 !(res = ep_events_transfer(ep, events, maxevents)) && jtimeout)
1557 goto retry;
1559 return res;
1563 static int eventpollfs_delete_dentry(struct dentry *dentry)
1566 return 1;
1570 static struct inode *ep_eventpoll_inode(const struct file_operations *fops)
1572 int error = -ENOMEM;
1573 struct inode *inode = new_inode(eventpoll_mnt->mnt_sb);
1575 if (!inode)
1576 goto eexit_1;
1578 inode->i_fop = fops;
1580 /*
1581 * Mark the inode dirty from the very beginning,
1582 * that way it will never be moved to the dirty
1583 * list because mark_inode_dirty() will think
1584 * that it already _is_ on the dirty list.
1585 */
1586 inode->i_state = I_DIRTY;
1587 inode->i_mode = S_IRUSR | S_IWUSR;
1588 inode->i_uid = current->fsuid;
1589 inode->i_gid = current->fsgid;
1590 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1591 inode->i_blksize = PAGE_SIZE;
1592 return inode;
1594 eexit_1:
1595 return ERR_PTR(error);
1599 static int
1600 eventpollfs_get_sb(struct file_system_type *fs_type, int flags,
1601 const char *dev_name, void *data, struct vfsmount *mnt)
1603 return get_sb_pseudo(fs_type, "eventpoll:", NULL, EVENTPOLLFS_MAGIC,
1604 mnt);
1608 static int __init eventpoll_init(void)
1610 int error;
1612 mutex_init(&epmutex);
1614 /* Initialize the structure used to perform safe poll wait head wake ups */
1615 ep_poll_safewake_init(&psw);
1617 /* Allocates slab cache used to allocate "struct epitem" items */
1618 epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
1619 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC,
1620 NULL, NULL);
1622 /* Allocates slab cache used to allocate "struct eppoll_entry" */
1623 pwq_cache = kmem_cache_create("eventpoll_pwq",
1624 sizeof(struct eppoll_entry), 0,
1625 EPI_SLAB_DEBUG|SLAB_PANIC, NULL, NULL);
1627 /*
1628 * Register the virtual file system that will be the source of inodes
1629 * for the eventpoll files
1630 */
1631 error = register_filesystem(&eventpoll_fs_type);
1632 if (error)
1633 goto epanic;
1635 /* Mount the above commented virtual file system */
1636 eventpoll_mnt = kern_mount(&eventpoll_fs_type);
1637 error = PTR_ERR(eventpoll_mnt);
1638 if (IS_ERR(eventpoll_mnt))
1639 goto epanic;
1641 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: successfully initialized.\n",
1642 current));
1643 return 0;
1645 epanic:
1646 panic("eventpoll_init() failed\n");
1650 static void __exit eventpoll_exit(void)
1652 /* Undo all operations done inside eventpoll_init() */
1653 unregister_filesystem(&eventpoll_fs_type);
1654 mntput(eventpoll_mnt);
1655 kmem_cache_destroy(pwq_cache);
1656 kmem_cache_destroy(epi_cache);
1659 module_init(eventpoll_init);
1660 module_exit(eventpoll_exit);
1662 MODULE_LICENSE("GPL");