ocfs2: add IO error check in ocfs2_get_sector()
[safe/jmp/linux-2.6] / fs / eventpoll.c
index 1dbedc7..011b9b8 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
@@ -44,8 +44,8 @@
  * There are three level of locking required by epoll :
  *
  * 1) epmutex (mutex)
- * 2) ep->mtx (mutes)
- * 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
 
 #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;
@@ -134,12 +136,18 @@ struct poll_safewake {
  * 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 */
+       /* 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;
 
@@ -152,23 +160,11 @@ struct epitem {
        /* 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;
 
-       /*
-        * Works together "struct eventpoll"->ovflist in keeping the
-        * single linked chain of items.
-        */
-       struct epitem *next;
+       /* The structure that describe the interested events and the source fd */
+       struct epoll_event event;
 };
 
 /*
@@ -178,7 +174,7 @@ struct epitem {
  */
 struct eventpoll {
        /* Protect the this structure access */
-       rwlock_t lock;
+       spinlock_t lock;
 
        /*
         * This mutex is used to ensure that files are not removed
@@ -197,7 +193,7 @@ 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;
 
        /*
@@ -206,6 +202,9 @@ struct eventpoll {
         * holding ->lock.
         */
        struct epitem *ovflist;
+
+       /* The user that created the eventpoll descriptor */
+       struct user_struct *user;
 };
 
 /* Wait structure used by the poll hooks */
@@ -233,9 +232,15 @@ struct ep_pqueue {
 };
 
 /*
+ * Configuration options available inside /proc/sys/fs/epoll/
+ */
+/* 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;
@@ -246,8 +251,27 @@ 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_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)
 {
@@ -255,7 +279,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)
 {
@@ -263,25 +287,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)
 {
@@ -289,13 +294,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;
 }
@@ -331,15 +336,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)) {
@@ -360,7 +364,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);
@@ -394,78 +398,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;
 
@@ -485,26 +422,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)
@@ -541,24 +474,23 @@ static void ep_free(struct eventpoll *ep)
         * 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);
+       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;
@@ -574,10 +506,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;
 }
@@ -609,6 +541,8 @@ 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.
+        * 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->mtx" after "epmutex" because ep_remove() requires it when called
@@ -631,41 +565,49 @@ 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 = -ENOMEM;
+       ep = kzalloc(sizeof(*ep), GFP_KERNEL);
+       if (unlikely(!ep))
+               goto free_uid;
 
-       rwlock_init(&ep->lock);
+       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);
@@ -674,12 +616,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));
@@ -702,7 +642,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
@@ -739,13 +679,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++;
 
 out_unlock:
-       write_unlock_irqrestore(&ep->lock, flags);
+       spin_unlock_irqrestore(&ep->lock, flags);
 
        /* We have to call this outside the lock */
        if (pwake)
@@ -796,6 +735,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)
 {
@@ -804,19 +746,19 @@ 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;
 
@@ -827,7 +769,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);
 
@@ -836,6 +780,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;
 
@@ -844,24 +789,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)
@@ -877,21 +827,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)
 {
@@ -913,36 +865,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)
@@ -975,11 +918,11 @@ static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *event
         * have the poll callback to queue directly on ep->rdllist,
         * because we are doing it in the loop below, in a lockless way.
         */
-       write_lock_irqsave(&ep->lock, flags);
+       spin_lock_irqsave(&ep->lock, flags);
        list_splice(&ep->rdllist, &txlist);
        INIT_LIST_HEAD(&ep->rdllist);
        ep->ovflist = NULL;
-       write_unlock_irqrestore(&ep->lock, flags);
+       spin_unlock_irqrestore(&ep->lock, flags);
 
        /*
         * We can loop without lock because this is a task private list.
@@ -1028,16 +971,20 @@ static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *event
 
 errxit:
 
-       write_lock_irqsave(&ep->lock, flags);
+       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
-        * here (in case they are not already queued, or they're one-shot).
+        * inside the main ready-list here.
         */
        for (nepi = ep->ovflist; (epi = nepi) != NULL;
             nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
-               if (!ep_is_linked(&epi->rdllink) &&
-                   (epi->event.events & ~EP_PRIVATE_BITS))
+               /*
+                * 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);
        }
        /*
@@ -1048,23 +995,23 @@ errxit:
        ep->ovflist = EP_UNACTIVE_PTR;
 
        /*
-        * In case of error in the event-send loop, we might still have items
-        * inside the "txlist". We need to splice them back inside ep->rdllist.
+        * 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.
         */
        list_splice(&txlist, &ep->rdllist);
 
        if (!list_empty(&ep->rdllist)) {
                /*
                 * Wake up (if active) both the eventpoll wait list and the ->poll()
-                * wait list.
+                * 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);
 
@@ -1092,7 +1039,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)) {
@@ -1119,9 +1066,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);
 
@@ -1131,7 +1078,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
@@ -1146,61 +1093,62 @@ retry:
 }
 
 /*
- * 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)
+SYSCALL_DEFINE1(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);
 
+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;
+SYSCALL_DEFINE1(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)
+SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
+               struct epoll_event __user *, event)
 {
        int error;
        struct file *file, *tfile;
@@ -1249,7 +1197,11 @@ asmlinkage long sys_epoll_ctl(int epfd, int op, int fd,
 
        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;
@@ -1276,12 +1228,6 @@ 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);
        mutex_unlock(&ep->mtx);
 
 error_tgt_fput:
@@ -1299,8 +1245,8 @@ error_return:
  * Implement the event wait interface for the eventpoll file. It is the kernel
  * part of the user space epoll_wait(2).
  */
-asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
-                              int maxevents, int timeout)
+SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,
+               int, maxevents, int, timeout)
 {
        int error;
        struct file *file;
@@ -1351,15 +1297,15 @@ 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
  * 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)
+SYSCALL_DEFINE6(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;
@@ -1388,8 +1334,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);
        }
@@ -1397,11 +1343,18 @@ 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);
+       /*
+        * Allows top 4% of lomem to be allocated for epoll watches (per user).
+        */
+       max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) /
+               EP_ITEM_COST;
 
        /* Initialize the structure used to perform safe poll wait head wake ups */
        ep_poll_safewake_init(&psw);
@@ -1409,14 +1362,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);
-