sh: __GFP_REPEAT for pte allocations, too.
[safe/jmp/linux-2.6] / fs / eventpoll.c
index 9900e33..b5c7ca5 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  fs/eventpoll.c ( Efficent event polling implementation )
- *  Copyright (C) 2001,...,2003         Davide Libenzi
+ *  Copyright (C) 2001,...,2006         Davide Libenzi
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -22,7 +22,6 @@
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/poll.h>
-#include <linux/smp_lock.h>
 #include <linux/string.h>
 #include <linux/list.h>
 #include <linux/hash.h>
@@ -34,6 +33,7 @@
 #include <linux/eventpoll.h>
 #include <linux/mount.h>
 #include <linux/bitops.h>
+#include <linux/mutex.h>
 #include <asm/uaccess.h>
 #include <asm/system.h>
 #include <asm/io.h>
@@ -46,7 +46,7 @@
  * LOCKING:
  * There are three level of locking required by epoll :
  *
- * 1) epsem (semaphore)
+ * 1) epmutex (mutex)
  * 2) ep->sem (rw_semaphore)
  * 3) ep->lock (rw_lock)
  *
@@ -67,9 +67,9 @@
  * if a file has been pushed inside an epoll set and it is then
  * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL).
  * It is possible to drop the "ep->sem" and to use the global
- * semaphore "epsem" (together with "ep->lock") to have it working,
+ * semaphore "epmutex" (together with "ep->lock") to have it working,
  * but having "ep->sem" will make the interface more scalable.
- * Events that require holding "epsem" are very rare, while for
+ * Events that require holding "epmutex" are very rare, while for
  * normal operations the epoll private "ep->sem" will guarantee
  * a greater scalability.
  */
 /* Maximum number of poll wake up nests we are allowing */
 #define EP_MAX_POLLWAKE_NESTS 4
 
-/* Macro to allocate a "struct epitem" from the slab cache */
-#define EPI_MEM_ALLOC()        (struct epitem *) kmem_cache_alloc(epi_cache, SLAB_KERNEL)
+/* Maximum msec timeout value storeable in a long int */
+#define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ)
 
-/* Macro to free a "struct epitem" to the slab cache */
-#define EPI_MEM_FREE(p) kmem_cache_free(epi_cache, p)
-
-/* Macro to allocate a "struct eppoll_entry" from the slab cache */
-#define PWQ_MEM_ALLOC()        (struct eppoll_entry *) kmem_cache_alloc(pwq_cache, SLAB_KERNEL)
-
-/* Macro to free a "struct eppoll_entry" to the slab cache */
-#define PWQ_MEM_FREE(p) kmem_cache_free(pwq_cache, p)
-
-/* Fast test to see if the file is an evenpoll file */
-#define IS_FILE_EPOLL(f) ((f)->f_op == &eventpoll_fops)
-
-/* Setup the structure that is used as key for the rb-tree */
-#define EP_SET_FFD(p, f, d) do { (p)->file = (f); (p)->fd = (d); } while (0)
-
-/* Compare rb-tree keys */
-#define EP_CMP_FFD(p1, p2) ((p1)->file > (p2)->file ? +1: \
-                           ((p1)->file < (p2)->file ? -1: (p1)->fd - (p2)->fd))
-
-/* Special initialization for the rb-tree node to detect linkage */
-#define EP_RB_INITNODE(n) (n)->rb_parent = (n)
-
-/* Removes a node from the rb-tree and marks it for a fast is-linked check */
-#define EP_RB_ERASE(n, r) do { rb_erase(n, r); (n)->rb_parent = (n); } while (0)
-
-/* Fast check to verify that the item is linked to the main rb-tree */
-#define EP_RB_LINKED(n) ((n)->rb_parent != (n))
-
-/*
- * Remove the item from the list and perform its initialization.
- * This is useful for us because we can test if the item is linked
- * using "EP_IS_LINKED(p)".
- */
-#define EP_LIST_DEL(p) do { list_del(p); INIT_LIST_HEAD(p); } while (0)
-
-/* Tells us if the item is currently linked */
-#define EP_IS_LINKED(p) (!list_empty(p))
-
-/* Get the "struct epitem" from a wait queue pointer */
-#define EP_ITEM_FROM_WAIT(p) ((struct epitem *) container_of(p, struct eppoll_entry, wait)->base)
-
-/* Get the "struct epitem" from an epoll queue wrapper */
-#define EP_ITEM_FROM_EPQUEUE(p) (container_of(p, struct ep_pqueue, pt)->epi)
-
-/* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
-#define EP_OP_HASH_EVENT(op) ((op) != EPOLL_CTL_DEL)
+#define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
 
 
 struct epoll_filefd {
@@ -166,7 +121,7 @@ struct epoll_filefd {
  */
 struct wake_task_node {
        struct list_head llink;
-       task_t *task;
+       struct task_struct *task;
        wait_queue_head_t *wq;
 };
 
@@ -229,7 +184,7 @@ struct eppoll_entry {
 
 /*
  * Each file descriptor added to the eventpoll interface will
- * have an entry of this type linked to the hash.
+ * have an entry of this type linked to the "rbr" RB tree.
  */
 struct epitem {
        /* RB-Tree node used to link this structure to the eventpoll rb-tree */
@@ -261,15 +216,6 @@ struct epitem {
 
        /* List header used to link this item to the "struct file" items list */
        struct list_head fllink;
-
-       /* List header used to link the item to the transfer list */
-       struct list_head txlink;
-
-       /*
-        * This is used during the collection/transfer of events to userspace
-        * to pin items empty events set.
-        */
-       unsigned int revents;
 };
 
 /* Wrapper struct used by poll queueing */
@@ -282,8 +228,9 @@ struct ep_pqueue {
 
 static void ep_poll_safewake_init(struct poll_safewake *psw);
 static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq);
-static int ep_getfd(int *efd, struct inode **einode, struct file **efile);
-static int ep_file_init(struct file *file);
+static int ep_getfd(int *efd, struct inode **einode, struct file **efile,
+                   struct eventpoll *ep);
+static int ep_alloc(struct eventpoll **pep);
 static void ep_free(struct eventpoll *ep);
 static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd);
 static void ep_use_epitem(struct epitem *epi);
@@ -301,11 +248,8 @@ static int ep_remove(struct eventpoll *ep, struct epitem *epi);
 static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key);
 static int ep_eventpoll_close(struct inode *inode, struct file *file);
 static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait);
-static int ep_collect_ready_items(struct eventpoll *ep,
-                                 struct list_head *txlist, int maxevents);
 static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
-                         struct epoll_event __user *events);
-static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist);
+                         struct epoll_event __user *events, int maxevents);
 static int ep_events_transfer(struct eventpoll *ep,
                              struct epoll_event __user *events,
                              int maxevents);
@@ -313,29 +257,29 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
                   int maxevents, long timeout);
 static int eventpollfs_delete_dentry(struct dentry *dentry);
 static struct inode *ep_eventpoll_inode(void);
-static struct super_block *eventpollfs_get_sb(struct file_system_type *fs_type,
-                                             int flags, const char *dev_name,
-                                             void *data);
+static int eventpollfs_get_sb(struct file_system_type *fs_type,
+                             int flags, const char *dev_name,
+                             void *data, struct vfsmount *mnt);
 
 /*
  * This semaphore is used to serialize ep_free() and eventpoll_release_file().
  */
-static struct semaphore epsem;
+static struct mutex epmutex;
 
 /* Safe wake up implementation */
 static struct poll_safewake psw;
 
 /* Slab cache used to allocate "struct epitem" */
-static kmem_cache_t *epi_cache;
+static struct kmem_cache *epi_cache __read_mostly;
 
 /* Slab cache used to allocate "struct eppoll_entry" */
-static kmem_cache_t *pwq_cache;
+static struct kmem_cache *pwq_cache __read_mostly;
 
 /* Virtual fs used to allocate inodes for eventpoll files */
-static struct vfsmount *eventpoll_mnt;
+static struct vfsmount *eventpoll_mnt __read_mostly;
 
 /* File callbacks that implement the eventpoll file behaviour */
-static struct file_operations eventpoll_fops = {
+static const struct file_operations eventpoll_fops = {
        .release        = ep_eventpoll_close,
        .poll           = ep_eventpoll_poll
 };
@@ -357,6 +301,71 @@ static struct dentry_operations eventpollfs_dentry_operations = {
 
 
 
+/* Fast test to see if the file is an evenpoll file */
+static inline int is_file_epoll(struct file *f)
+{
+       return f->f_op == &eventpoll_fops;
+}
+
+/* Setup the structure that is used as key for the rb-tree */
+static inline void ep_set_ffd(struct epoll_filefd *ffd,
+                             struct file *file, int fd)
+{
+       ffd->file = file;
+       ffd->fd = fd;
+}
+
+/* Compare rb-tree keys */
+static inline int ep_cmp_ffd(struct epoll_filefd *p1,
+                            struct epoll_filefd *p2)
+{
+       return (p1->file > p2->file ? +1:
+               (p1->file < p2->file ? -1 : p1->fd - p2->fd));
+}
+
+/* Special initialization for the rb-tree node to detect linkage */
+static inline void ep_rb_initnode(struct rb_node *n)
+{
+       rb_set_parent(n, n);
+}
+
+/* Removes a node from the rb-tree and marks it for a fast is-linked check */
+static inline void ep_rb_erase(struct rb_node *n, struct rb_root *r)
+{
+       rb_erase(n, r);
+       rb_set_parent(n, n);
+}
+
+/* Fast check to verify that the item is linked to the main rb-tree */
+static inline int ep_rb_linked(struct rb_node *n)
+{
+       return rb_parent(n) != n;
+}
+
+/* Tells us if the item is currently linked */
+static inline int ep_is_linked(struct list_head *p)
+{
+       return !list_empty(p);
+}
+
+/* Get the "struct epitem" from a wait queue pointer */
+static inline struct epitem * ep_item_from_wait(wait_queue_t *p)
+{
+       return container_of(p, struct eppoll_entry, wait)->base;
+}
+
+/* Get the "struct epitem" from an epoll queue wrapper */
+static inline struct epitem * ep_item_from_epqueue(poll_table *p)
+{
+       return container_of(p, struct ep_pqueue, pt)->epi;
+}
+
+/* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
+static inline int ep_op_has_event(int op)
+{
+       return op != EPOLL_CTL_DEL;
+}
+
 /* Initialize the poll safe wake up structure */
 static void ep_poll_safewake_init(struct poll_safewake *psw)
 {
@@ -382,7 +391,7 @@ static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
 {
        int wake_nests = 0;
        unsigned long flags;
-       task_t *this_task = current;
+       struct task_struct *this_task = current;
        struct list_head *lsthead = &psw->wake_task_list, *lnk;
        struct wake_task_node *tncur;
        struct wake_task_node tnode;
@@ -421,15 +430,6 @@ static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
 }
 
 
-/* Used to initialize the epoll bits inside the "struct file" */
-void eventpoll_init_file(struct file *file)
-{
-
-       INIT_LIST_HEAD(&file->f_ep_links);
-       spin_lock_init(&file->f_ep_lock);
-}
-
-
 /*
  * This is called from eventpoll_release() to unlink files from the eventpoll
  * interface. We need to have this facility to cleanup correctly files that are
@@ -447,22 +447,22 @@ void eventpoll_release_file(struct file *file)
         * cleanup path, and this means that noone is using this file anymore.
         * The only hit might come from ep_free() but by holding the semaphore
         * will correctly serialize the operation. We do need to acquire
-        * "ep->sem" after "epsem" because ep_remove() requires it when called
+        * "ep->sem" after "epmutex" because ep_remove() requires it when called
         * from anywhere but ep_free().
         */
-       down(&epsem);
+       mutex_lock(&epmutex);
 
        while (!list_empty(lsthead)) {
-               epi = list_entry(lsthead->next, struct epitem, fllink);
+               epi = list_first_entry(lsthead, struct epitem, fllink);
 
                ep = epi->ep;
-               EP_LIST_DEL(&epi->fllink);
+               list_del_init(&epi->fllink);
                down_write(&ep->sem);
                ep_remove(ep, epi);
                up_write(&ep->sem);
        }
 
-       up(&epsem);
+       mutex_unlock(&epmutex);
 }
 
 
@@ -475,39 +475,38 @@ void eventpoll_release_file(struct file *file)
  */
 asmlinkage long sys_epoll_create(int size)
 {
-       int error, fd;
+       int error, fd = -1;
+       struct eventpoll *ep;
        struct inode *inode;
        struct file *file;
 
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
                     current, size));
 
-       /* Sanity check on the size parameter */
+       /*
+        * Sanity check on the size parameter, and create the internal data
+        * structure ( "struct eventpoll" ).
+        */
        error = -EINVAL;
-       if (size <= 0)
+       if (size <= 0 || (error = ep_alloc(&ep)) != 0)
                goto eexit_1;
 
        /*
         * Creates all the items needed to setup an eventpoll file. That is,
         * a file structure, and inode and a free file descriptor.
         */
-       error = ep_getfd(&fd, &inode, &file);
-       if (error)
-               goto eexit_1;
-
-       /* Setup the file internal data structure ( "struct eventpoll" ) */
-       error = ep_file_init(file);
+       error = ep_getfd(&fd, &inode, &file, ep);
        if (error)
                goto eexit_2;
 
-
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
                     current, size, fd));
 
        return fd;
 
 eexit_2:
-       sys_close(fd);
+       ep_free(ep);
+       kfree(ep);
 eexit_1:
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
                     current, size, error));
@@ -534,7 +533,7 @@ sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event)
                     current, epfd, op, fd, event));
 
        error = -EFAULT;
-       if (EP_OP_HASH_EVENT(op) &&
+       if (ep_op_has_event(op) &&
            copy_from_user(&epds, event, sizeof(struct epoll_event)))
                goto eexit_1;
 
@@ -560,7 +559,7 @@ sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event)
         * adding an epoll file descriptor inside itself.
         */
        error = -EINVAL;
-       if (file == tfile || !IS_FILE_EPOLL(file))
+       if (file == tfile || !is_file_epoll(file))
                goto eexit_3;
 
        /*
@@ -571,7 +570,7 @@ sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event)
 
        down_write(&ep->sem);
 
-       /* Try to lookup the file inside our hash table */
+       /* Try to lookup the file inside our RB tree */
        epi = ep_find(ep, tfile, fd);
 
        error = -EINVAL;
@@ -619,7 +618,6 @@ eexit_1:
        return error;
 }
 
-#define MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
 
 /*
  * Implement the event wait interface for the eventpoll file. It is the kernel
@@ -636,7 +634,7 @@ asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
                     current, epfd, events, maxevents, timeout));
 
        /* The maximum number of event must be greater than zero */
-       if (maxevents <= 0 || maxevents > MAX_EVENTS)
+       if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)
                return -EINVAL;
 
        /* Verify that the area passed by the user is writeable */
@@ -656,7 +654,7 @@ asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
         * the user passed to us _is_ an eventpoll file.
         */
        error = -EINVAL;
-       if (!IS_FILE_EPOLL(file))
+       if (!is_file_epoll(file))
                goto eexit_2;
 
        /*
@@ -678,10 +676,60 @@ eexit_1:
 }
 
 
+#ifdef TIF_RESTORE_SIGMASK
+
+/*
+ * Implement the event wait interface for the eventpoll file. It is the kernel
+ * part of the user space epoll_pwait(2).
+ */
+asmlinkage long sys_epoll_pwait(int epfd, struct epoll_event __user *events,
+               int maxevents, int timeout, const sigset_t __user *sigmask,
+               size_t sigsetsize)
+{
+       int error;
+       sigset_t ksigmask, sigsaved;
+
+       /*
+        * If the caller wants a certain signal mask to be set during the wait,
+        * we apply it here.
+        */
+       if (sigmask) {
+               if (sigsetsize != sizeof(sigset_t))
+                       return -EINVAL;
+               if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
+                       return -EFAULT;
+               sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
+               sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
+       }
+
+       error = sys_epoll_wait(epfd, events, maxevents, timeout);
+
+       /*
+        * If we changed the signal mask, we need to restore the original one.
+        * In case we've got a signal while waiting, we do not restore the
+        * signal mask yet, and we allow do_signal() to deliver the signal on
+        * the way back to userspace, before the signal mask is restored.
+        */
+       if (sigmask) {
+               if (error == -EINTR) {
+                       memcpy(&current->saved_sigmask, &sigsaved,
+                               sizeof(sigsaved));
+                       set_thread_flag(TIF_RESTORE_SIGMASK);
+               } else
+                       sigprocmask(SIG_SETMASK, &sigsaved, NULL);
+       }
+
+       return error;
+}
+
+#endif /* #ifdef TIF_RESTORE_SIGMASK */
+
+
 /*
  * Creates the file descriptor to be used by the epoll interface.
  */
-static int ep_getfd(int *efd, struct inode **einode, struct file **efile)
+static int ep_getfd(int *efd, struct inode **einode, struct file **efile,
+                   struct eventpoll *ep)
 {
        struct qstr this;
        char name[32];
@@ -698,9 +746,10 @@ static int ep_getfd(int *efd, struct inode **einode, struct file **efile)
 
        /* Allocates an inode from the eventpoll file system */
        inode = ep_eventpoll_inode();
-       error = PTR_ERR(inode);
-       if (IS_ERR(inode))
+       if (IS_ERR(inode)) {
+               error = PTR_ERR(inode);
                goto eexit_2;
+       }
 
        /* Allocates a free descriptor to plug the file onto */
        error = get_unused_fd();
@@ -722,8 +771,8 @@ static int ep_getfd(int *efd, struct inode **einode, struct file **efile)
                goto eexit_4;
        dentry->d_op = &eventpollfs_dentry_operations;
        d_add(dentry, inode);
-       file->f_vfsmnt = mntget(eventpoll_mnt);
-       file->f_dentry = dentry;
+       file->f_path.mnt = mntget(eventpoll_mnt);
+       file->f_path.dentry = dentry;
        file->f_mapping = inode->i_mapping;
 
        file->f_pos = 0;
@@ -731,7 +780,7 @@ static int ep_getfd(int *efd, struct inode **einode, struct file **efile)
        file->f_op = &eventpoll_fops;
        file->f_mode = FMODE_READ;
        file->f_version = 0;
-       file->private_data = NULL;
+       file->private_data = ep;
 
        /* Install the new setup file into the allocated fd. */
        fd_install(fd, file);
@@ -752,14 +801,13 @@ eexit_1:
 }
 
 
-static int ep_file_init(struct file *file)
+static int ep_alloc(struct eventpoll **pep)
 {
-       struct eventpoll *ep;
+       struct eventpoll *ep = kzalloc(sizeof(*ep), GFP_KERNEL);
 
-       if (!(ep = kmalloc(sizeof(struct eventpoll), GFP_KERNEL)))
+       if (!ep)
                return -ENOMEM;
 
-       memset(ep, 0, sizeof(*ep));
        rwlock_init(&ep->lock);
        init_rwsem(&ep->sem);
        init_waitqueue_head(&ep->wq);
@@ -767,9 +815,9 @@ static int ep_file_init(struct file *file)
        INIT_LIST_HEAD(&ep->rdllist);
        ep->rbr = RB_ROOT;
 
-       file->private_data = ep;
+       *pep = ep;
 
-       DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_file_init() ep=%p\n",
+       DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n",
                     current, ep));
        return 0;
 }
@@ -790,9 +838,9 @@ static void ep_free(struct eventpoll *ep)
         * We do not need to hold "ep->sem" here because the epoll file
         * is on the way to be removed and no one has references to it
         * anymore. The only hit might come from eventpoll_release_file() but
-        * holding "epsem" is sufficent here.
+        * holding "epmutex" is sufficent here.
         */
-       down(&epsem);
+       mutex_lock(&epmutex);
 
        /*
         * Walks through the whole tree by unregistering poll callbacks.
@@ -804,7 +852,7 @@ static void ep_free(struct eventpoll *ep)
        }
 
        /*
-        * Walks through the whole hash by freeing each "struct epitem". At this
+        * Walks through the whole tree by freeing each "struct epitem". At this
         * point we are sure no poll callbacks will be lingering around, and also by
         * write-holding "sem" we can be sure that no file cleanup code will hit
         * us during this operation. So we can avoid the lock on "ep->lock".
@@ -814,12 +862,12 @@ static void ep_free(struct eventpoll *ep)
                ep_remove(ep, epi);
        }
 
-       up(&epsem);
+       mutex_unlock(&epmutex);
 }
 
 
 /*
- * Search the file inside the eventpoll hash. It add usage count to
+ * Search the file inside the eventpoll tree. It add usage count to
  * the returned item, so the caller must call ep_release_epitem()
  * after finished using the "struct epitem".
  */
@@ -831,11 +879,11 @@ static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
        struct epitem *epi, *epir = NULL;
        struct epoll_filefd ffd;
 
-       EP_SET_FFD(&ffd, file, fd);
+       ep_set_ffd(&ffd, file, fd);
        read_lock_irqsave(&ep->lock, flags);
        for (rbp = ep->rbr.rb_node; rbp; ) {
                epi = rb_entry(rbp, struct epitem, rbn);
-               kcmp = EP_CMP_FFD(&ffd, &epi->ffd);
+               kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
                if (kcmp > 0)
                        rbp = rbp->rb_right;
                else if (kcmp < 0)
@@ -875,7 +923,7 @@ static void ep_release_epitem(struct epitem *epi)
 {
 
        if (atomic_dec_and_test(&epi->usecnt))
-               EPI_MEM_FREE(epi);
+               kmem_cache_free(epi_cache, epi);
 }
 
 
@@ -886,10 +934,10 @@ static void ep_release_epitem(struct epitem *epi)
 static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
                                 poll_table *pt)
 {
-       struct epitem *epi = EP_ITEM_FROM_EPQUEUE(pt);
+       struct epitem *epi = ep_item_from_epqueue(pt);
        struct eppoll_entry *pwq;
 
-       if (epi->nwait >= 0 && (pwq = PWQ_MEM_ALLOC())) {
+       if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
                init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
                pwq->whead = whead;
                pwq->base = epi;
@@ -912,7 +960,7 @@ static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
        while (*p) {
                parent = *p;
                epic = rb_entry(parent, struct epitem, rbn);
-               kcmp = EP_CMP_FFD(&epi->ffd, &epic->ffd);
+               kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
                if (kcmp > 0)
                        p = &parent->rb_right;
                else
@@ -932,17 +980,16 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
        struct ep_pqueue epq;
 
        error = -ENOMEM;
-       if (!(epi = EPI_MEM_ALLOC()))
+       if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
                goto eexit_1;
 
        /* Item initialization follow here ... */
-       EP_RB_INITNODE(&epi->rbn);
+       ep_rb_initnode(&epi->rbn);
        INIT_LIST_HEAD(&epi->rdllink);
        INIT_LIST_HEAD(&epi->fllink);
-       INIT_LIST_HEAD(&epi->txlink);
        INIT_LIST_HEAD(&epi->pwqlist);
        epi->ep = ep;
-       EP_SET_FFD(&epi->ffd, tfile, fd);
+       ep_set_ffd(&epi->ffd, tfile, fd);
        epi->event = *event;
        atomic_set(&epi->usecnt, 1);
        epi->nwait = 0;
@@ -978,12 +1025,12 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
        ep_rbtree_insert(ep, epi);
 
        /* If the file is already "ready" we drop it inside the ready list */
-       if ((revents & event->events) && !EP_IS_LINKED(&epi->rdllink)) {
+       if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
                list_add_tail(&epi->rdllink, &ep->rdllist);
 
                /* Notify waiting tasks that events are available */
                if (waitqueue_active(&ep->wq))
-                       wake_up(&ep->wq);
+                       __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE);
                if (waitqueue_active(&ep->poll_wait))
                        pwake++;
        }
@@ -1007,11 +1054,11 @@ eexit_2:
         * allocated wait queue.
         */
        write_lock_irqsave(&ep->lock, flags);
-       if (EP_IS_LINKED(&epi->rdllink))
-               EP_LIST_DEL(&epi->rdllink);
+       if (ep_is_linked(&epi->rdllink))
+               list_del_init(&epi->rdllink);
        write_unlock_irqrestore(&ep->lock, flags);
 
-       EPI_MEM_FREE(epi);
+       kmem_cache_free(epi_cache, epi);
 eexit_1:
        return error;
 }
@@ -1047,22 +1094,23 @@ static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_even
        epi->event.data = event->data;
 
        /*
-        * If the item is not linked to the hash it means that it's on its
+        * If the item is not linked to the RB tree it means that it's on its
         * way toward the removal. Do nothing in this case.
         */
-       if (EP_RB_LINKED(&epi->rbn)) {
+       if (ep_rb_linked(&epi->rbn)) {
                /*
                 * If the item is "hot" and it is not registered inside the ready
                 * list, push it inside. If the item is not "hot" and it is currently
                 * registered inside the ready list, unlink it.
                 */
                if (revents & event->events) {
-                       if (!EP_IS_LINKED(&epi->rdllink)) {
+                       if (!ep_is_linked(&epi->rdllink)) {
                                list_add_tail(&epi->rdllink, &ep->rdllist);
 
                                /* Notify waiting tasks that events are available */
                                if (waitqueue_active(&ep->wq))
-                                       wake_up(&ep->wq);
+                                       __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
+                                                        TASK_INTERRUPTIBLE);
                                if (waitqueue_active(&ep->poll_wait))
                                        pwake++;
                        }
@@ -1095,11 +1143,11 @@ static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
 
        if (nwait) {
                while (!list_empty(lsthead)) {
-                       pwq = list_entry(lsthead->next, struct eppoll_entry, llink);
+                       pwq = list_first_entry(lsthead, struct eppoll_entry, llink);
 
-                       EP_LIST_DEL(&pwq->llink);
+                       list_del_init(&pwq->llink);
                        remove_wait_queue(pwq->whead, &pwq->wait);
-                       PWQ_MEM_FREE(pwq);
+                       kmem_cache_free(pwq_cache, pwq);
                }
        }
 }
@@ -1118,7 +1166,7 @@ static int ep_unlink(struct eventpoll *ep, struct epitem *epi)
         * The check protect us from doing a double unlink ( crash ).
         */
        error = -ENOENT;
-       if (!EP_RB_LINKED(&epi->rbn))
+       if (!ep_rb_linked(&epi->rbn))
                goto eexit_1;
 
        /*
@@ -1133,27 +1181,27 @@ static int ep_unlink(struct eventpoll *ep, struct epitem *epi)
         * This operation togheter with the above check closes the door to
         * double unlinks.
         */
-       EP_RB_ERASE(&epi->rbn, &ep->rbr);
+       ep_rb_erase(&epi->rbn, &ep->rbr);
 
        /*
         * If the item we are going to remove is inside the ready file descriptors
         * we want to remove it from this list to avoid stale events.
         */
-       if (EP_IS_LINKED(&epi->rdllink))
-               EP_LIST_DEL(&epi->rdllink);
+       if (ep_is_linked(&epi->rdllink))
+               list_del_init(&epi->rdllink);
 
        error = 0;
 eexit_1:
 
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n",
-                    current, ep, epi->file, error));
+                    current, ep, epi->ffd.file, error));
 
        return error;
 }
 
 
 /*
- * Removes a "struct epitem" from the eventpoll hash and deallocates
+ * Removes a "struct epitem" from the eventpoll RB tree and deallocates
  * all the associated resources.
  */
 static int ep_remove(struct eventpoll *ep, struct epitem *epi)
@@ -1174,14 +1222,14 @@ static int ep_remove(struct eventpoll *ep, struct epitem *epi)
 
        /* Remove the current item from the list of epoll hooks */
        spin_lock(&file->f_ep_lock);
-       if (EP_IS_LINKED(&epi->fllink))
-               EP_LIST_DEL(&epi->fllink);
+       if (ep_is_linked(&epi->fllink))
+               list_del_init(&epi->fllink);
        spin_unlock(&file->f_ep_lock);
 
        /* We need to acquire the write IRQ lock before calling ep_unlink() */
        write_lock_irqsave(&ep->lock, flags);
 
-       /* Really unlink the item from the hash */
+       /* Really unlink the item from the RB tree */
        error = ep_unlink(ep, epi);
 
        write_unlock_irqrestore(&ep->lock, flags);
@@ -1210,11 +1258,11 @@ static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *k
 {
        int pwake = 0;
        unsigned long flags;
-       struct epitem *epi = EP_ITEM_FROM_WAIT(wait);
+       struct epitem *epi = ep_item_from_wait(wait);
        struct eventpoll *ep = epi->ep;
 
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
-                    current, epi->file, epi, ep));
+                    current, epi->ffd.file, epi, ep));
 
        write_lock_irqsave(&ep->lock, flags);
 
@@ -1228,7 +1276,7 @@ static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *k
                goto is_disabled;
 
        /* If this file is already in the ready list we exit soon */
-       if (EP_IS_LINKED(&epi->rdllink))
+       if (ep_is_linked(&epi->rdllink))
                goto is_linked;
 
        list_add_tail(&epi->rdllink, &ep->rdllist);
@@ -1239,7 +1287,8 @@ is_linked:
         * wait list.
         */
        if (waitqueue_active(&ep->wq))
-               wake_up(&ep->wq);
+               __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
+                                TASK_INTERRUPTIBLE);
        if (waitqueue_active(&ep->poll_wait))
                pwake++;
 
@@ -1288,71 +1337,30 @@ static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
 
 
 /*
- * Since we have to release the lock during the __copy_to_user() operation and
- * during the f_op->poll() call, we try to collect the maximum number of items
- * by reducing the irqlock/irqunlock switching rate.
- */
-static int ep_collect_ready_items(struct eventpoll *ep, struct list_head *txlist, int maxevents)
-{
-       int nepi;
-       unsigned long flags;
-       struct list_head *lsthead = &ep->rdllist, *lnk;
-       struct epitem *epi;
-
-       write_lock_irqsave(&ep->lock, flags);
-
-       for (nepi = 0, lnk = lsthead->next; lnk != lsthead && nepi < maxevents;) {
-               epi = list_entry(lnk, struct epitem, rdllink);
-
-               lnk = lnk->next;
-
-               /* If this file is already in the ready list we exit soon */
-               if (!EP_IS_LINKED(&epi->txlink)) {
-                       /*
-                        * This is initialized in this way so that the default
-                        * behaviour of the reinjecting code will be to push back
-                        * the item inside the ready list.
-                        */
-                       epi->revents = epi->event.events;
-
-                       /* Link the ready item into the transfer list */
-                       list_add(&epi->txlink, txlist);
-                       nepi++;
-
-                       /*
-                        * Unlink the item from the ready list.
-                        */
-                       EP_LIST_DEL(&epi->rdllink);
-               }
-       }
-
-       write_unlock_irqrestore(&ep->lock, flags);
-
-       return nepi;
-}
-
-
-/*
  * This function is called without holding the "ep->lock" since the call to
  * __copy_to_user() might sleep, and also f_op->poll() might reenable the IRQ
  * because of the way poll() is traditionally implemented in Linux.
  */
 static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
-                         struct epoll_event __user *events)
+                         struct epoll_event __user *events, int maxevents)
 {
-       int eventcnt = 0;
+       int eventcnt, error = -EFAULT, pwake = 0;
        unsigned int revents;
-       struct list_head *lnk;
+       unsigned long flags;
        struct epitem *epi;
+       struct list_head injlist;
+
+       INIT_LIST_HEAD(&injlist);
 
        /*
         * We can loop without lock because this is a task private list.
-        * The test done during the collection loop will guarantee us that
-        * another task will not try to collect this file. Also, items
-        * cannot vanish during the loop because we are holding "sem".
+        * We just splice'd out the ep->rdllist in ep_collect_ready_items().
+        * Items cannot vanish during the loop because we are holding "sem" in
+        * read.
         */
-       list_for_each(lnk, txlist) {
-               epi = list_entry(lnk, struct epitem, txlink);
+       for (eventcnt = 0; !list_empty(txlist) && eventcnt < maxevents;) {
+               epi = list_first_entry(txlist, struct epitem, rdllink);
+               prefetch(epi->rdllink.next);
 
                /*
                 * Get the ready file event set. We can safely use the file
@@ -1360,79 +1368,83 @@ static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
                 * guarantee that both the file and the item will not vanish.
                 */
                revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
+               revents &= epi->event.events;
 
                /*
-                * Set the return event set for the current file descriptor.
-                * Note that only the task task was successfully able to link
-                * the item to its "txlist" will write this field.
+                * Is the event mask intersect the caller-requested one,
+                * deliver the event to userspace. Again, we are holding
+                * "sem" in read, so no operations coming from userspace
+                * can change the item.
                 */
-               epi->revents = revents & epi->event.events;
-
-               if (epi->revents) {
-                       if (__put_user(epi->revents,
+               if (revents) {
+                       if (__put_user(revents,
                                       &events[eventcnt].events) ||
                            __put_user(epi->event.data,
                                       &events[eventcnt].data))
-                               return -EFAULT;
+                               goto errxit;
                        if (epi->event.events & EPOLLONESHOT)
                                epi->event.events &= EP_PRIVATE_BITS;
                        eventcnt++;
                }
-       }
-       return eventcnt;
-}
-
-
-/*
- * Walk through the transfer list we collected with ep_collect_ready_items()
- * and, if 1) the item is still "alive" 2) its event set is not empty 3) it's
- * not already linked, links it to the ready list. Same as above, we are holding
- * "sem" so items cannot vanish underneath our nose.
- */
-static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist)
-{
-       int ricnt = 0, pwake = 0;
-       unsigned long flags;
-       struct epitem *epi;
-
-       write_lock_irqsave(&ep->lock, flags);
-
-       while (!list_empty(txlist)) {
-               epi = list_entry(txlist->next, struct epitem, txlink);
-
-               /* Unlink the current item from the transfer list */
-               EP_LIST_DEL(&epi->txlink);
 
                /*
-                * If the item is no more linked to the interest set, we don't
-                * have to push it inside the ready list because the following
-                * ep_release_epitem() is going to drop it. Also, if the current
-                * item is set to have an Edge Triggered behaviour, we don't have
-                * to push it back either.
+                * This is tricky. We are holding the "sem" in read, and this
+                * means that the operations that can change the "linked" status
+                * of the epoll item (epi->rbn and epi->rdllink), cannot touch
+                * them.  Also, since we are "linked" from a epi->rdllink POV
+                * (the item is linked to our transmission list we just
+                * spliced), the ep_poll_callback() cannot touch us either,
+                * because of the check present in there. Another parallel
+                * epoll_wait() will not get the same result set, since we
+                * spliced the ready list before.  Note that list_del() still
+                * shows the item as linked to the test in ep_poll_callback().
                 */
-               if (EP_RB_LINKED(&epi->rbn) && !(epi->event.events & EPOLLET) &&
-                   (epi->revents & epi->event.events) && !EP_IS_LINKED(&epi->rdllink)) {
-                       list_add_tail(&epi->rdllink, &ep->rdllist);
-                       ricnt++;
+               list_del(&epi->rdllink);
+               if (!(epi->event.events & EPOLLET) &&
+                               (revents & epi->event.events))
+                       list_add_tail(&epi->rdllink, &injlist);
+               else {
+                       /*
+                        * Be sure the item is totally detached before re-init
+                        * the list_head. After INIT_LIST_HEAD() is committed,
+                        * the ep_poll_callback() can requeue the item again,
+                        * but we don't care since we are already past it.
+                        */
+                       smp_mb();
+                       INIT_LIST_HEAD(&epi->rdllink);
                }
        }
+       error = 0;
 
-       if (ricnt) {
+       errxit:
+
+       /*
+        * If the re-injection list or the txlist are not empty, re-splice
+        * them to the ready list and do proper wakeups.
+        */
+       if (!list_empty(&injlist) || !list_empty(txlist)) {
+               write_lock_irqsave(&ep->lock, flags);
+
+               list_splice(txlist, &ep->rdllist);
+               list_splice(&injlist, &ep->rdllist);
                /*
                 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
                 * wait list.
                 */
                if (waitqueue_active(&ep->wq))
-                       wake_up(&ep->wq);
+                       __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
+                                        TASK_INTERRUPTIBLE);
                if (waitqueue_active(&ep->poll_wait))
                        pwake++;
-       }
 
-       write_unlock_irqrestore(&ep->lock, flags);
+               write_unlock_irqrestore(&ep->lock, flags);
+       }
 
        /* We have to call this outside the lock */
        if (pwake)
                ep_poll_safewake(&psw, &ep->poll_wait);
+
+       return eventcnt == 0 ? error: eventcnt;
 }
 
 
@@ -1442,7 +1454,8 @@ static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist)
 static int ep_events_transfer(struct eventpoll *ep,
                              struct epoll_event __user *events, int maxevents)
 {
-       int eventcnt = 0;
+       int eventcnt;
+       unsigned long flags;
        struct list_head txlist;
 
        INIT_LIST_HEAD(&txlist);
@@ -1453,14 +1466,17 @@ static int ep_events_transfer(struct eventpoll *ep,
         */
        down_read(&ep->sem);
 
-       /* Collect/extract ready items */
-       if (ep_collect_ready_items(ep, &txlist, maxevents) > 0) {
-               /* Build result set in userspace */
-               eventcnt = ep_send_events(ep, &txlist, events);
+       /*
+        * Steal the ready list, and re-init the original one to the
+        * empty list.
+        */
+       write_lock_irqsave(&ep->lock, flags);
+       list_splice(&ep->rdllist, &txlist);
+       INIT_LIST_HEAD(&ep->rdllist);
+       write_unlock_irqrestore(&ep->lock, flags);
 
-               /* Reinject ready items into the ready list */
-               ep_reinject_items(ep, &txlist);
-       }
+       /* Build result set in userspace */
+       eventcnt = ep_send_events(ep, &txlist, events, maxevents);
 
        up_read(&ep->sem);
 
@@ -1481,8 +1497,8 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
         * and the overflow condition. The passed timeout is in milliseconds,
         * that why (t * HZ) / 1000.
         */
-       jtimeout = timeout == -1 || timeout > (MAX_SCHEDULE_TIMEOUT - 1000) / HZ ?
-               MAX_SCHEDULE_TIMEOUT: (timeout * HZ + 999) / 1000;
+       jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ?
+               MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
 
 retry:
        write_lock_irqsave(&ep->lock, flags);
@@ -1495,7 +1511,7 @@ retry:
                 * ep_poll_callback() when events will become available.
                 */
                init_waitqueue_entry(&wait, current);
-               add_wait_queue(&ep->wq, &wait);
+               __add_wait_queue(&ep->wq, &wait);
 
                for (;;) {
                        /*
@@ -1515,7 +1531,7 @@ retry:
                        jtimeout = schedule_timeout(jtimeout);
                        write_lock_irqsave(&ep->lock, flags);
                }
-               remove_wait_queue(&ep->wq, &wait);
+               __remove_wait_queue(&ep->wq, &wait);
 
                set_current_state(TASK_RUNNING);
        }
@@ -1537,14 +1553,12 @@ retry:
        return res;
 }
 
-
 static int eventpollfs_delete_dentry(struct dentry *dentry)
 {
 
        return 1;
 }
 
-
 static struct inode *ep_eventpoll_inode(void)
 {
        int error = -ENOMEM;
@@ -1566,19 +1580,18 @@ static struct inode *ep_eventpoll_inode(void)
        inode->i_uid = current->fsuid;
        inode->i_gid = current->fsgid;
        inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
-       inode->i_blksize = PAGE_SIZE;
        return inode;
 
 eexit_1:
        return ERR_PTR(error);
 }
 
-
-static struct super_block *
+static int
 eventpollfs_get_sb(struct file_system_type *fs_type, int flags,
-                  const char *dev_name, void *data)
+                  const char *dev_name, void *data, struct vfsmount *mnt)
 {
-       return get_sb_pseudo(fs_type, "eventpoll:", NULL, EVENTPOLLFS_MAGIC);
+       return get_sb_pseudo(fs_type, "eventpoll:", NULL, EVENTPOLLFS_MAGIC,
+                            mnt);
 }
 
 
@@ -1586,7 +1599,7 @@ static int __init eventpoll_init(void)
 {
        int error;
 
-       init_MUTEX(&epsem);
+       mutex_init(&epmutex);
 
        /* Initialize the structure used to perform safe poll wait head wake ups */
        ep_poll_safewake_init(&psw);