NSM: Move nsm_create()
[safe/jmp/linux-2.6] / fs / eventpoll.c
index 1aad34e..96355d5 100644 (file)
@@ -1,6 +1,6 @@
 /*
- *  fs/eventpoll.c ( Efficent event polling implementation )
- *  Copyright (C) 2001,...,2006         Davide Libenzi
+ *  fs/eventpoll.c (Efficent event polling implementation)
+ *  Copyright (C) 2001,...,2007         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
@@ -26,7 +26,6 @@
 #include <linux/hash.h>
 #include <linux/spinlock.h>
 #include <linux/syscalls.h>
-#include <linux/rwsem.h>
 #include <linux/rbtree.h>
 #include <linux/wait.h>
 #include <linux/eventpoll.h>
 #include <asm/io.h>
 #include <asm/mman.h>
 #include <asm/atomic.h>
-#include <asm/semaphore.h>
 
 /*
  * LOCKING:
  * There are three level of locking required by epoll :
  *
  * 1) epmutex (mutex)
- * 2) ep->sem (rw_semaphore)
- * 3) ep->lock (rw_lock)
+ * 2) ep->mtx (mutex)
+ * 3) ep->lock (spinlock)
  *
  * The acquire order is the one listed above, from 1 to 3.
  * We need a spinlock (ep->lock) because we manipulate objects
  * a spinlock. During the event transfer loop (from kernel to
  * user space) we could end up sleeping due a copy_to_user(), so
  * we need a lock that will allow us to sleep. This lock is a
- * read-write semaphore (ep->sem). It is acquired on read during
- * the event transfer loop and in write during epoll_ctl(EPOLL_CTL_DEL)
- * and during eventpoll_release_file(). Then we also need a global
- * semaphore to serialize eventpoll_release_file() and ep_free().
- * This semaphore is acquired by ep_free() during the epoll file
+ * mutex (ep->mtx). It is acquired during the event transfer loop,
+ * during epoll_ctl(EPOLL_CTL_DEL) and during eventpoll_release_file().
+ * Then we also need a global mutex to serialize eventpoll_release_file()
+ * and ep_free().
+ * This mutex is acquired by ep_free() during the epoll file
  * cleanup path and it is also acquired by eventpoll_release_file()
  * 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 "epmutex" (together with "ep->lock") to have it working,
- * but having "ep->sem" will make the interface more scalable.
+ * It is possible to drop the "ep->mtx" and to use the global
+ * mutex "epmutex" (together with "ep->lock") to have it working,
+ * but having "ep->mtx" will make the interface more scalable.
  * Events that require holding "epmutex" are very rare, while for
- * normal operations the epoll private "ep->sem" will guarantee
- * a greater scalability.
+ * normal operations the epoll private "ep->mtx" will guarantee
+ * a better scalability.
  */
 
 #define DEBUG_EPOLL 0
 
 #define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
 
+#define EP_UNACTIVE_PTR ((void *) -1L)
+
+#define EP_ITEM_COST (sizeof(struct epitem) + sizeof(struct eppoll_entry))
+
 struct epoll_filefd {
        struct file *file;
        int fd;
@@ -111,7 +113,7 @@ struct epoll_filefd {
  * Node that is linked into the "wake_task_list" member of the "struct poll_safewake".
  * It is used to keep track on all tasks that are currently inside the wake_up() code
  * to 1) short-circuit the one coming from the same task and same wait queue head
- * ( loop ) 2) allow a maximum number of epoll descriptors inclusion nesting
+ * (loop) 2) allow a maximum number of epoll descriptors inclusion nesting
  * 3) let go the ones coming from other tasks.
  */
 struct wake_task_node {
@@ -130,21 +132,57 @@ struct poll_safewake {
 };
 
 /*
+ * Each file descriptor added to the eventpoll interface will
+ * 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 */
+       struct rb_node rbn;
+
+       /* List header used to link this structure to the eventpoll ready list */
+       struct list_head rdllink;
+
+       /*
+        * Works together "struct eventpoll"->ovflist in keeping the
+        * single linked chain of items.
+        */
+       struct epitem *next;
+
+       /* The file descriptor information this item refers to */
+       struct epoll_filefd ffd;
+
+       /* Number of active wait queue attached to poll operations */
+       int nwait;
+
+       /* List containing poll wait queues */
+       struct list_head pwqlist;
+
+       /* The "container" of this item */
+       struct eventpoll *ep;
+
+       /* List header used to link this item to the "struct file" items list */
+       struct list_head fllink;
+
+       /* The structure that describe the interested events and the source fd */
+       struct epoll_event event;
+};
+
+/*
  * This structure is stored inside the "private_data" member of the file
  * structure and rapresent the main data sructure for the eventpoll
  * interface.
  */
 struct eventpoll {
        /* Protect the this structure access */
-       rwlock_t lock;
+       spinlock_t lock;
 
        /*
-        * This semaphore is used to ensure that files are not removed
-        * while epoll is using them. This is read-held during the event
-        * collection loop and it is write-held during the file cleanup
-        * path, the epoll file exit code and the ctl operations.
+        * This mutex is used to ensure that files are not removed
+        * while epoll is using them. This is held during the event
+        * collection loop, the file cleanup path, the epoll file exit
+        * code and the ctl operations.
         */
-       struct rw_semaphore sem;
+       struct mutex mtx;
 
        /* Wait queue used by sys_epoll_wait() */
        wait_queue_head_t wq;
@@ -155,8 +193,18 @@ struct eventpoll {
        /* List of ready file descriptors */
        struct list_head rdllist;
 
-       /* RB-Tree root used to store monitored fd structs */
+       /* RB tree root used to store monitored fd structs */
        struct rb_root rbr;
+
+       /*
+        * This is a single linked list that chains all the "struct epitem" that
+        * happened while transfering ready events to userspace w/out
+        * holding ->lock.
+        */
+       struct epitem *ovflist;
+
+       /* The user that created the eventpoll descriptor */
+       struct user_struct *user;
 };
 
 /* Wait structure used by the poll hooks */
@@ -177,42 +225,6 @@ struct eppoll_entry {
        wait_queue_head_t *whead;
 };
 
-/*
- * Each file descriptor added to the eventpoll interface will
- * 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 */
-       struct rb_node rbn;
-
-       /* List header used to link this structure to the eventpoll ready list */
-       struct list_head rdllink;
-
-       /* The file descriptor information this item refers to */
-       struct epoll_filefd ffd;
-
-       /* Number of active wait queue attached to poll operations */
-       int nwait;
-
-       /* List containing poll wait queues */
-       struct list_head pwqlist;
-
-       /* The "container" of this item */
-       struct eventpoll *ep;
-
-       /* The structure that describe the interested events and the source fd */
-       struct epoll_event event;
-
-       /*
-        * Used to keep track of the usage count of the structure. This avoids
-        * that the structure will desappear from underneath our processing.
-        */
-       atomic_t usecnt;
-
-       /* List header used to link this item to the "struct file" items list */
-       struct list_head fllink;
-};
-
 /* Wrapper struct used by poll queueing */
 struct ep_pqueue {
        poll_table pt;
@@ -220,9 +232,17 @@ struct ep_pqueue {
 };
 
 /*
- * This semaphore is used to serialize ep_free() and eventpoll_release_file().
+ * Configuration options available inside /proc/sys/fs/epoll/
+ */
+/* Maximum number of epoll devices, per user */
+static int max_user_instances __read_mostly;
+/* Maximum number of epoll watched descriptors, per user */
+static int max_user_watches __read_mostly;
+
+/*
+ * This mutex is used to serialize ep_free() and eventpoll_release_file().
  */
-static struct mutex epmutex;
+static DEFINE_MUTEX(epmutex);
 
 /* Safe wake up implementation */
 static struct poll_safewake psw;
@@ -233,8 +253,35 @@ static struct kmem_cache *epi_cache __read_mostly;
 /* Slab cache used to allocate "struct eppoll_entry" */
 static struct kmem_cache *pwq_cache __read_mostly;
 
+#ifdef CONFIG_SYSCTL
+
+#include <linux/sysctl.h>
+
+static int zero;
+
+ctl_table epoll_table[] = {
+       {
+               .procname       = "max_user_instances",
+               .data           = &max_user_instances,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = &proc_dointvec_minmax,
+               .extra1         = &zero,
+       },
+       {
+               .procname       = "max_user_watches",
+               .data           = &max_user_watches,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = &proc_dointvec_minmax,
+               .extra1         = &zero,
+       },
+       { .ctl_name = 0 }
+};
+#endif /* CONFIG_SYSCTL */
+
 
-/* Setup the structure that is used as key for the rb-tree */
+/* 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)
 {
@@ -242,7 +289,7 @@ static inline void ep_set_ffd(struct epoll_filefd *ffd,
        ffd->fd = fd;
 }
 
-/* Compare rb-tree keys */
+/* Compare RB tree keys */
 static inline int ep_cmp_ffd(struct epoll_filefd *p1,
                             struct epoll_filefd *p2)
 {
@@ -250,25 +297,6 @@ static inline int ep_cmp_ffd(struct epoll_filefd *p1,
                (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)
 {
@@ -276,13 +304,13 @@ static inline int ep_is_linked(struct list_head *p)
 }
 
 /* Get the "struct epitem" from a wait queue pointer */
-static inline struct epitem * ep_item_from_wait(wait_queue_t *p)
+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)
+static inline struct epitem *ep_item_from_epqueue(poll_table *p)
 {
        return container_of(p, struct ep_pqueue, pt)->epi;
 }
@@ -318,15 +346,14 @@ static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
        int wake_nests = 0;
        unsigned long flags;
        struct task_struct *this_task = current;
-       struct list_head *lsthead = &psw->wake_task_list, *lnk;
+       struct list_head *lsthead = &psw->wake_task_list;
        struct wake_task_node *tncur;
        struct wake_task_node tnode;
 
        spin_lock_irqsave(&psw->lock, flags);
 
        /* Try to see if the current task is already inside this wakeup call */
-       list_for_each(lnk, lsthead) {
-               tncur = list_entry(lnk, struct wake_task_node, llink);
+       list_for_each_entry(tncur, lsthead, llink) {
 
                if (tncur->wq == wq ||
                    (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) {
@@ -347,7 +374,7 @@ static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
        spin_unlock_irqrestore(&psw->lock, flags);
 
        /* Do really wake up now */
-       wake_up(wq);
+       wake_up_nested(wq, 1 + wake_nests);
 
        /* Remove the current task from the list */
        spin_lock_irqsave(&psw->lock, flags);
@@ -381,78 +408,11 @@ static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
 }
 
 /*
- * Unlink the "struct epitem" from all places it might have been hooked up.
- * This function must be called with write IRQ lock on "ep->lock".
- */
-static int ep_unlink(struct eventpoll *ep, struct epitem *epi)
-{
-       int error;
-
-       /*
-        * It can happen that this one is called for an item already unlinked.
-        * The check protect us from doing a double unlink ( crash ).
-        */
-       error = -ENOENT;
-       if (!ep_rb_linked(&epi->rbn))
-               goto error_return;
-
-       /*
-        * Clear the event mask for the unlinked item. This will avoid item
-        * notifications to be sent after the unlink operation from inside
-        * the kernel->userspace event transfer loop.
-        */
-       epi->event.events = 0;
-
-       /*
-        * At this point is safe to do the job, unlink the item from our rb-tree.
-        * This operation togheter with the above check closes the door to
-        * double unlinks.
-        */
-       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))
-               list_del_init(&epi->rdllink);
-
-       error = 0;
-error_return:
-
-       DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n",
-                    current, ep, epi->ffd.file, error));
-
-       return error;
-}
-
-/*
- * Increment the usage count of the "struct epitem" making it sure
- * that the user will have a valid pointer to reference.
- */
-static void ep_use_epitem(struct epitem *epi)
-{
-       atomic_inc(&epi->usecnt);
-}
-
-/*
- * Decrement ( release ) the usage count by signaling that the user
- * has finished using the structure. It might lead to freeing the
- * structure itself if the count goes to zero.
- */
-static void ep_release_epitem(struct epitem *epi)
-{
-       if (atomic_dec_and_test(&epi->usecnt))
-               kmem_cache_free(epi_cache, epi);
-}
-
-/*
  * Removes a "struct epitem" from the eventpoll RB tree and deallocates
- * all the associated resources.
+ * all the associated resources. Must be called with "mtx" held.
  */
 static int ep_remove(struct eventpoll *ep, struct epitem *epi)
 {
-       int error;
        unsigned long flags;
        struct file *file = epi->ffd.file;
 
@@ -472,26 +432,22 @@ static int ep_remove(struct eventpoll *ep, struct epitem *epi)
                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 RB tree */
-       error = ep_unlink(ep, epi);
+       rb_erase(&epi->rbn, &ep->rbr);
 
-       write_unlock_irqrestore(&ep->lock, flags);
-
-       if (error)
-               goto error_return;
+       spin_lock_irqsave(&ep->lock, flags);
+       if (ep_is_linked(&epi->rdllink))
+               list_del_init(&epi->rdllink);
+       spin_unlock_irqrestore(&ep->lock, flags);
 
        /* At this point it is safe to free the eventpoll item */
-       ep_release_epitem(epi);
+       kmem_cache_free(epi_cache, epi);
 
-       error = 0;
-error_return:
-       DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p) = %d\n",
-                    current, ep, file, error));
+       atomic_dec(&ep->user->epoll_watches);
 
-       return error;
+       DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p)\n",
+                    current, ep, file));
+
+       return 0;
 }
 
 static void ep_free(struct eventpoll *ep)
@@ -506,7 +462,7 @@ static void ep_free(struct eventpoll *ep)
        /*
         * We need to lock this because we could be hit by
         * eventpoll_release_file() while we're freeing the "struct eventpoll".
-        * We do not need to hold "ep->sem" here because the epoll file
+        * We do not need to hold "ep->mtx" 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 "epmutex" is sufficent here.
@@ -525,25 +481,27 @@ static void ep_free(struct eventpoll *ep)
        /*
         * 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
+        * holding "epmutex" we can be sure that no file cleanup code will hit
         * us during this operation. So we can avoid the lock on "ep->lock".
         */
-       while ((rbp = rb_first(&ep->rbr)) != 0) {
+       while ((rbp = rb_first(&ep->rbr)) != NULL) {
                epi = rb_entry(rbp, struct epitem, rbn);
                ep_remove(ep, epi);
        }
 
        mutex_unlock(&epmutex);
+       mutex_destroy(&ep->mtx);
+       atomic_dec(&ep->user->epoll_devs);
+       free_uid(ep->user);
+       kfree(ep);
 }
 
 static int ep_eventpoll_release(struct inode *inode, struct file *file)
 {
        struct eventpoll *ep = file->private_data;
 
-       if (ep) {
+       if (ep)
                ep_free(ep);
-               kfree(ep);
-       }
 
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep));
        return 0;
@@ -559,10 +517,10 @@ static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
        poll_wait(file, &ep->poll_wait, wait);
 
        /* Check our condition */
-       read_lock_irqsave(&ep->lock, flags);
+       spin_lock_irqsave(&ep->lock, flags);
        if (!list_empty(&ep->rdllist))
                pollflags = POLLIN | POLLRDNORM;
-       read_unlock_irqrestore(&ep->lock, flags);
+       spin_unlock_irqrestore(&ep->lock, flags);
 
        return pollflags;
 }
@@ -594,9 +552,11 @@ void eventpoll_release_file(struct file *file)
         * We don't want to get "file->f_ep_lock" because it is not
         * necessary. It is not necessary because we're in the "struct 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
+        * So, for example, epoll_ctl() cannot hit here sicne if we reach this
+        * point, the file counter already went to zero and fget() would fail.
+        * The only hit might come from ep_free() but by holding the mutex
         * will correctly serialize the operation. We do need to acquire
-        * "ep->sem" after "epmutex" because ep_remove() requires it when called
+        * "ep->mtx" after "epmutex" because ep_remove() requires it when called
         * from anywhere but ep_free().
         */
        mutex_lock(&epmutex);
@@ -606,9 +566,9 @@ void eventpoll_release_file(struct file *file)
 
                ep = epi->ep;
                list_del_init(&epi->fllink);
-               down_write(&ep->sem);
+               mutex_lock(&ep->mtx);
                ep_remove(ep, epi);
-               up_write(&ep->sem);
+               mutex_unlock(&ep->mtx);
        }
 
        mutex_unlock(&epmutex);
@@ -616,40 +576,53 @@ void eventpoll_release_file(struct file *file)
 
 static int ep_alloc(struct eventpoll **pep)
 {
-       struct eventpoll *ep = kzalloc(sizeof(*ep), GFP_KERNEL);
+       int error;
+       struct user_struct *user;
+       struct eventpoll *ep;
 
-       if (!ep)
-               return -ENOMEM;
+       user = get_current_user();
+       error = -EMFILE;
+       if (unlikely(atomic_read(&user->epoll_devs) >=
+                       max_user_instances))
+               goto free_uid;
+       error = -ENOMEM;
+       ep = kzalloc(sizeof(*ep), GFP_KERNEL);
+       if (unlikely(!ep))
+               goto free_uid;
 
-       rwlock_init(&ep->lock);
-       init_rwsem(&ep->sem);
+       spin_lock_init(&ep->lock);
+       mutex_init(&ep->mtx);
        init_waitqueue_head(&ep->wq);
        init_waitqueue_head(&ep->poll_wait);
        INIT_LIST_HEAD(&ep->rdllist);
        ep->rbr = RB_ROOT;
+       ep->ovflist = EP_UNACTIVE_PTR;
+       ep->user = user;
 
        *pep = ep;
 
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n",
                     current, ep));
        return 0;
+
+free_uid:
+       free_uid(user);
+       return error;
 }
 
 /*
- * 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".
+ * Search the file inside the eventpoll tree. The RB tree operations
+ * are protected by the "mtx" mutex, and ep_find() must be called with
+ * "mtx" held.
  */
 static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
 {
        int kcmp;
-       unsigned long flags;
        struct rb_node *rbp;
        struct epitem *epi, *epir = NULL;
        struct epoll_filefd ffd;
 
        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);
@@ -658,12 +631,10 @@ static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
                else if (kcmp < 0)
                        rbp = rbp->rb_left;
                else {
-                       ep_use_epitem(epi);
                        epir = epi;
                        break;
                }
        }
-       read_unlock_irqrestore(&ep->lock, flags);
 
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n",
                     current, file, epir));
@@ -686,7 +657,7 @@ static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *k
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
                     current, epi->ffd.file, epi, ep));
 
-       write_lock_irqsave(&ep->lock, flags);
+       spin_lock_irqsave(&ep->lock, flags);
 
        /*
         * If the event mask does not contain any poll(2) event, we consider the
@@ -695,7 +666,21 @@ static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *k
         * until the next EPOLL_CTL_MOD will be issued.
         */
        if (!(epi->event.events & ~EP_PRIVATE_BITS))
-               goto is_disabled;
+               goto out_unlock;
+
+       /*
+        * If we are trasfering events to userspace, we can hold no locks
+        * (because we're accessing user memory, and because of linux f_op->poll()
+        * semantics). All the events that happens during that period of time are
+        * chained in ep->ovflist and requeued later on.
+        */
+       if (unlikely(ep->ovflist != EP_UNACTIVE_PTR)) {
+               if (epi->next == EP_UNACTIVE_PTR) {
+                       epi->next = ep->ovflist;
+                       ep->ovflist = epi;
+               }
+               goto out_unlock;
+       }
 
        /* If this file is already in the ready list we exit soon */
        if (ep_is_linked(&epi->rdllink))
@@ -709,13 +694,12 @@ is_linked:
         * wait list.
         */
        if (waitqueue_active(&ep->wq))
-               __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
-                                TASK_INTERRUPTIBLE);
+               wake_up_locked(&ep->wq);
        if (waitqueue_active(&ep->poll_wait))
                pwake++;
 
-is_disabled:
-       write_unlock_irqrestore(&ep->lock, flags);
+out_unlock:
+       spin_unlock_irqrestore(&ep->lock, flags);
 
        /* We have to call this outside the lock */
        if (pwake)
@@ -766,6 +750,9 @@ static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
        rb_insert_color(&epi->rbn, &ep->rbr);
 }
 
+/*
+ * Must be called with "mtx" held.
+ */
 static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
                     struct file *tfile, int fd)
 {
@@ -774,20 +761,21 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
        struct epitem *epi;
        struct ep_pqueue epq;
 
-       error = -ENOMEM;
+       if (unlikely(atomic_read(&ep->user->epoll_watches) >=
+                    max_user_watches))
+               return -ENOSPC;
        if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
-               goto error_return;
+               return -ENOMEM;
 
        /* Item initialization follow here ... */
-       ep_rb_initnode(&epi->rbn);
        INIT_LIST_HEAD(&epi->rdllink);
        INIT_LIST_HEAD(&epi->fllink);
        INIT_LIST_HEAD(&epi->pwqlist);
        epi->ep = ep;
        ep_set_ffd(&epi->ffd, tfile, fd);
        epi->event = *event;
-       atomic_set(&epi->usecnt, 1);
        epi->nwait = 0;
+       epi->next = EP_UNACTIVE_PTR;
 
        /* Initialize the poll table using the queue callback */
        epq.epi = epi;
@@ -796,7 +784,9 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
        /*
         * Attach the item to the poll hooks and get current event bits.
         * We can safely use the file* here because its usage count has
-        * been increased by the caller of this function.
+        * been increased by the caller of this function. Note that after
+        * this operation completes, the poll callback can start hitting
+        * the new item.
         */
        revents = tfile->f_op->poll(tfile, &epq.pt);
 
@@ -805,6 +795,7 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
         * install process. Namely an allocation for a wait queue failed due
         * high memory pressure.
         */
+       error = -ENOMEM;
        if (epi->nwait < 0)
                goto error_unregister;
 
@@ -813,24 +804,29 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
        list_add_tail(&epi->fllink, &tfile->f_ep_links);
        spin_unlock(&tfile->f_ep_lock);
 
-       /* We have to drop the new item inside our item list to keep track of it */
-       write_lock_irqsave(&ep->lock, flags);
-
-       /* Add the current item to the rb-tree */
+       /*
+        * Add the current item to the RB tree. All RB tree operations are
+        * protected by "mtx", and ep_insert() is called with "mtx" held.
+        */
        ep_rbtree_insert(ep, epi);
 
+       /* We have to drop the new item inside our item list to keep track of it */
+       spin_lock_irqsave(&ep->lock, flags);
+
        /* If the file is already "ready" we drop it inside the ready list */
        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_locked(&ep->wq, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE);
+                       wake_up_locked(&ep->wq);
                if (waitqueue_active(&ep->poll_wait))
                        pwake++;
        }
 
-       write_unlock_irqrestore(&ep->lock, flags);
+       spin_unlock_irqrestore(&ep->lock, flags);
+
+       atomic_inc(&ep->user->epoll_watches);
 
        /* We have to call this outside the lock */
        if (pwake)
@@ -846,21 +842,23 @@ error_unregister:
 
        /*
         * We need to do this because an event could have been arrived on some
-        * allocated wait queue.
+        * allocated wait queue. Note that we don't care about the ep->ovflist
+        * list, since that is used/cleaned only inside a section bound by "mtx".
+        * And ep_insert() is called with "mtx" held.
         */
-       write_lock_irqsave(&ep->lock, flags);
+       spin_lock_irqsave(&ep->lock, flags);
        if (ep_is_linked(&epi->rdllink))
                list_del_init(&epi->rdllink);
-       write_unlock_irqrestore(&ep->lock, flags);
+       spin_unlock_irqrestore(&ep->lock, flags);
 
        kmem_cache_free(epi_cache, epi);
-error_return:
+
        return error;
 }
 
 /*
  * Modify the interest event mask by dropping an event if the new mask
- * has a match in the current file status.
+ * has a match in the current file status. Must be called with "mtx" held.
  */
 static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
 {
@@ -882,36 +880,27 @@ static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_even
         */
        revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
 
-       write_lock_irqsave(&ep->lock, flags);
+       spin_lock_irqsave(&ep->lock, flags);
 
        /* Copy the data member from inside the lock */
        epi->event.data = event->data;
 
        /*
-        * 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 the item is "hot" and it is not registered inside the ready
+        * list, push it inside.
         */
-       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)) {
-                               list_add_tail(&epi->rdllink, &ep->rdllist);
-
-                               /* Notify waiting tasks that events are available */
-                               if (waitqueue_active(&ep->wq))
-                                       __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
-                                                        TASK_INTERRUPTIBLE);
-                               if (waitqueue_active(&ep->poll_wait))
-                                       pwake++;
-                       }
+       if (revents & event->events) {
+               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_locked(&ep->wq);
+                       if (waitqueue_active(&ep->poll_wait))
+                               pwake++;
                }
        }
-
-       write_unlock_irqrestore(&ep->lock, flags);
+       spin_unlock_irqrestore(&ep->lock, flags);
 
        /* We have to call this outside the lock */
        if (pwake)
@@ -920,36 +909,50 @@ static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_even
        return 0;
 }
 
-/*
- * 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, int maxevents)
+static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *events,
+                         int maxevents)
 {
        int eventcnt, error = -EFAULT, pwake = 0;
        unsigned int revents;
        unsigned long flags;
-       struct epitem *epi;
-       struct list_head injlist;
+       struct epitem *epi, *nepi;
+       struct list_head txlist;
 
-       INIT_LIST_HEAD(&injlist);
+       INIT_LIST_HEAD(&txlist);
+
+       /*
+        * We need to lock this because we could be hit by
+        * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
+        */
+       mutex_lock(&ep->mtx);
+
+       /*
+        * Steal the ready list, and re-init the original one to the
+        * empty list. Also, set ep->ovflist to NULL so that events
+        * happening while looping w/out locks, are not lost. We cannot
+        * have the poll callback to queue directly on ep->rdllist,
+        * because we are doing it in the loop below, in a lockless way.
+        */
+       spin_lock_irqsave(&ep->lock, flags);
+       list_splice(&ep->rdllist, &txlist);
+       INIT_LIST_HEAD(&ep->rdllist);
+       ep->ovflist = NULL;
+       spin_unlock_irqrestore(&ep->lock, flags);
 
        /*
         * We can loop without lock because this is a task private list.
         * 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.
+        * Items cannot vanish during the loop because we are holding "mtx".
         */
-       for (eventcnt = 0; !list_empty(txlist) && eventcnt < maxevents;) {
-               epi = list_first_entry(txlist, struct epitem, rdllink);
-               prefetch(epi->rdllink.next);
+       for (eventcnt = 0; !list_empty(&txlist) && eventcnt < maxevents;) {
+               epi = list_first_entry(&txlist, struct epitem, rdllink);
+
+               list_del_init(&epi->rdllink);
 
                /*
                 * Get the ready file event set. We can safely use the file
-                * because we are holding the "sem" in read and this will
-                * guarantee that both the file and the item will not vanish.
+                * because we are holding the "mtx" and this will 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;
@@ -957,8 +960,8 @@ static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
                /*
                 * 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.
+                * "mtx", so no operations coming from userspace can change
+                * the item.
                 */
                if (revents) {
                        if (__put_user(revents,
@@ -970,59 +973,62 @@ static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
                                epi->event.events &= EP_PRIVATE_BITS;
                        eventcnt++;
                }
-
                /*
-                * 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().
+                * At this point, noone can insert into ep->rdllist besides
+                * us. The epoll_ctl() callers are locked out by us holding
+                * "mtx" and the poll callback will queue them in ep->ovflist.
                 */
-               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);
-               }
+                   (revents & epi->event.events))
+                       list_add_tail(&epi->rdllink, &ep->rdllist);
        }
        error = 0;
 
-       errxit:
+errxit:
+
+       spin_lock_irqsave(&ep->lock, flags);
+       /*
+        * During the time we spent in the loop above, some other events
+        * might have been queued by the poll callback. We re-insert them
+        * inside the main ready-list here.
+        */
+       for (nepi = ep->ovflist; (epi = nepi) != NULL;
+            nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
+               /*
+                * If the above loop quit with errors, the epoll item might still
+                * be linked to "txlist", and the list_splice() done below will
+                * take care of those cases.
+                */
+               if (!ep_is_linked(&epi->rdllink))
+                       list_add_tail(&epi->rdllink, &ep->rdllist);
+       }
+       /*
+        * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
+        * releasing the lock, events will be queued in the normal way inside
+        * ep->rdllist.
+        */
+       ep->ovflist = EP_UNACTIVE_PTR;
 
        /*
-        * If the re-injection list or the txlist are not empty, re-splice
-        * them to the ready list and do proper wakeups.
+        * In case of error in the event-send loop, or in case the number of
+        * ready events exceeds the userspace limit, we need to splice the
+        * "txlist" back inside ep->rdllist.
         */
-       if (!list_empty(&injlist) || !list_empty(txlist)) {
-               write_lock_irqsave(&ep->lock, flags);
+       list_splice(&txlist, &ep->rdllist);
 
-               list_splice(txlist, &ep->rdllist);
-               list_splice(&injlist, &ep->rdllist);
+       if (!list_empty(&ep->rdllist)) {
                /*
-                * Wake up ( if active ) both the eventpoll wait list and the ->poll()
-                * wait list.
+                * Wake up (if active) both the eventpoll wait list and the ->poll()
+                * wait list (delayed after we release the lock).
                 */
                if (waitqueue_active(&ep->wq))
-                       __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
-                                        TASK_INTERRUPTIBLE);
+                       wake_up_locked(&ep->wq);
                if (waitqueue_active(&ep->poll_wait))
                        pwake++;
-
-               write_unlock_irqrestore(&ep->lock, flags);
        }
+       spin_unlock_irqrestore(&ep->lock, flags);
+
+       mutex_unlock(&ep->mtx);
 
        /* We have to call this outside the lock */
        if (pwake)
@@ -1031,41 +1037,6 @@ static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
        return eventcnt == 0 ? error: eventcnt;
 }
 
-/*
- * Perform the transfer of events to user space.
- */
-static int ep_events_transfer(struct eventpoll *ep,
-                             struct epoll_event __user *events, int maxevents)
-{
-       int eventcnt;
-       unsigned long flags;
-       struct list_head txlist;
-
-       INIT_LIST_HEAD(&txlist);
-
-       /*
-        * We need to lock this because we could be hit by
-        * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
-        */
-       down_read(&ep->sem);
-
-       /*
-        * 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);
-
-       /* Build result set in userspace */
-       eventcnt = ep_send_events(ep, &txlist, events, maxevents);
-
-       up_read(&ep->sem);
-
-       return eventcnt;
-}
-
 static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
                   int maxevents, long timeout)
 {
@@ -1083,7 +1054,7 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
                MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
 
 retry:
-       write_lock_irqsave(&ep->lock, flags);
+       spin_lock_irqsave(&ep->lock, flags);
 
        res = 0;
        if (list_empty(&ep->rdllist)) {
@@ -1093,6 +1064,7 @@ retry:
                 * ep_poll_callback() when events will become available.
                 */
                init_waitqueue_entry(&wait, current);
+               wait.flags |= WQ_FLAG_EXCLUSIVE;
                __add_wait_queue(&ep->wq, &wait);
 
                for (;;) {
@@ -1109,9 +1081,9 @@ retry:
                                break;
                        }
 
-                       write_unlock_irqrestore(&ep->lock, flags);
+                       spin_unlock_irqrestore(&ep->lock, flags);
                        jtimeout = schedule_timeout(jtimeout);
-                       write_lock_irqsave(&ep->lock, flags);
+                       spin_lock_irqsave(&ep->lock, flags);
                }
                __remove_wait_queue(&ep->wq, &wait);
 
@@ -1121,7 +1093,7 @@ retry:
        /* Is it worth to try to dig for events ? */
        eavail = !list_empty(&ep->rdllist);
 
-       write_unlock_irqrestore(&ep->lock, flags);
+       spin_unlock_irqrestore(&ep->lock, flags);
 
        /*
         * Try to transfer events to user space. In case we get 0 events and
@@ -1129,65 +1101,67 @@ retry:
         * more luck.
         */
        if (!res && eavail &&
-           !(res = ep_events_transfer(ep, events, maxevents)) && jtimeout)
+           !(res = ep_send_events(ep, events, maxevents)) && jtimeout)
                goto retry;
 
        return res;
 }
 
 /*
- * It opens an eventpoll file descriptor by suggesting a storage of "size"
- * file descriptors. The size parameter is just an hint about how to size
- * data structures. It won't prevent the user to store more than "size"
- * file descriptors inside the epoll interface. It is the kernel part of
- * the userspace epoll_create(2).
+ * Open an eventpoll file descriptor.
  */
-asmlinkage long sys_epoll_create(int size)
+asmlinkage long sys_epoll_create1(int flags)
 {
        int error, fd = -1;
        struct eventpoll *ep;
-       struct inode *inode;
-       struct file *file;
+
+       /* Check the EPOLL_* constant for consistency.  */
+       BUILD_BUG_ON(EPOLL_CLOEXEC != O_CLOEXEC);
+
+       if (flags & ~EPOLL_CLOEXEC)
+               return -EINVAL;
 
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
-                    current, size));
+                    current, flags));
 
        /*
-        * Sanity check on the size parameter, and create the internal data
-        * structure ( "struct eventpoll" ).
+        * Create the internal data structure ( "struct eventpoll" ).
         */
-       error = -EINVAL;
-       if (size <= 0 || (error = ep_alloc(&ep)) != 0)
+       error = ep_alloc(&ep);
+       if (error < 0) {
+               fd = error;
                goto error_return;
+       }
 
        /*
         * Creates all the items needed to setup an eventpoll file. That is,
-        * a file structure, and inode and a free file descriptor.
+        * a file structure and a free file descriptor.
         */
-       error = anon_inode_getfd(&fd, &inode, &file, "[eventpoll]",
-                                &eventpoll_fops, ep);
-       if (error)
-               goto error_free;
+       fd = anon_inode_getfd("[eventpoll]", &eventpoll_fops, ep,
+                             flags & O_CLOEXEC);
+       if (fd < 0)
+               ep_free(ep);
+       atomic_inc(&ep->user->epoll_devs);
 
+error_return:
        DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
-                    current, size, fd));
+                    current, flags, fd));
 
        return fd;
+}
 
-error_free:
-       ep_free(ep);
-       kfree(ep);
-error_return:
-       DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
-                    current, size, error));
-       return error;
+asmlinkage long sys_epoll_create(int size)
+{
+       if (size < 0)
+               return -EINVAL;
+
+       return sys_epoll_create1(0);
 }
 
 /*
  * The following function implements the controller interface for
  * the eventpoll file that enables the insertion/removal/change of
- * file descriptors inside the interest set.  It represents
- * the kernel part of the user space epoll_ctl(2).
+ * file descriptors inside the interest set.
  */
 asmlinkage long sys_epoll_ctl(int epfd, int op, int fd,
                              struct epoll_event __user *event)
@@ -1237,9 +1211,13 @@ asmlinkage long sys_epoll_ctl(int epfd, int op, int fd,
         */
        ep = file->private_data;
 
-       down_write(&ep->sem);
+       mutex_lock(&ep->mtx);
 
-       /* Try to lookup the file inside our RB tree */
+       /*
+        * Try to lookup the file inside our RB tree, Since we grabbed "mtx"
+        * above, we can be sure to be able to use the item looked up by
+        * ep_find() till we release the mutex.
+        */
        epi = ep_find(ep, tfile, fd);
 
        error = -EINVAL;
@@ -1266,13 +1244,7 @@ asmlinkage long sys_epoll_ctl(int epfd, int op, int fd,
                        error = -ENOENT;
                break;
        }
-       /*
-        * The function ep_find() increments the usage count of the structure
-        * so, if this is not NULL, we need to release it.
-        */
-       if (epi)
-               ep_release_epitem(epi);
-       up_write(&ep->sem);
+       mutex_unlock(&ep->mtx);
 
 error_tgt_fput:
        fput(tfile);
@@ -1341,7 +1313,7 @@ error_return:
        return error;
 }
 
-#ifdef TIF_RESTORE_SIGMASK
+#ifdef HAVE_SET_RESTORE_SIGMASK
 
 /*
  * Implement the event wait interface for the eventpoll file. It is the kernel
@@ -1378,8 +1350,8 @@ asmlinkage long sys_epoll_pwait(int epfd, struct epoll_event __user *events,
        if (sigmask) {
                if (error == -EINTR) {
                        memcpy(&current->saved_sigmask, &sigsaved,
-                               sizeof(sigsaved));
-                       set_thread_flag(TIF_RESTORE_SIGMASK);
+                              sizeof(sigsaved));
+                       set_restore_sigmask();
                } else
                        sigprocmask(SIG_SETMASK, &sigsaved, NULL);
        }
@@ -1387,11 +1359,16 @@ asmlinkage long sys_epoll_pwait(int epfd, struct epoll_event __user *events,
        return error;
 }
 
-#endif /* #ifdef TIF_RESTORE_SIGMASK */
+#endif /* HAVE_SET_RESTORE_SIGMASK */
 
 static int __init eventpoll_init(void)
 {
-       mutex_init(&epmutex);
+       struct sysinfo si;
+
+       si_meminfo(&si);
+       max_user_instances = 128;
+       max_user_watches = (((si.totalram - si.totalhigh) / 32) << PAGE_SHIFT) /
+               EP_ITEM_COST;
 
        /* Initialize the structure used to perform safe poll wait head wake ups */
        ep_poll_safewake_init(&psw);
@@ -1399,14 +1376,13 @@ static int __init eventpoll_init(void)
        /* Allocates slab cache used to allocate "struct epitem" items */
        epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
                        0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC,
-                       NULL, NULL);
+                       NULL);
 
        /* Allocates slab cache used to allocate "struct eppoll_entry" */
        pwq_cache = kmem_cache_create("eventpoll_pwq",
                        sizeof(struct eppoll_entry), 0,
-                       EPI_SLAB_DEBUG|SLAB_PANIC, NULL, NULL);
+                       EPI_SLAB_DEBUG|SLAB_PANIC, NULL);
 
        return 0;
 }
 fs_initcall(eventpoll_init);
-