epoll: avoid calling ep_call_nested() from ep_poll_safewake()
[platform/kernel/linux-rpi.git] / fs / eventpoll.c
index adbe328..dc15bb0 100644 (file)
@@ -205,7 +205,7 @@ struct eventpoll {
        struct list_head rdllist;
 
        /* RB tree root used to store monitored fd structs */
-       struct rb_root rbr;
+       struct rb_root_cached rbr;
 
        /*
         * This is a single linked list that chains all the "struct epitem" that
@@ -276,9 +276,6 @@ static DEFINE_MUTEX(epmutex);
 /* Used to check for epoll file descriptor inclusion loops */
 static struct nested_calls poll_loop_ncalls;
 
-/* Used for safe wake up implementation */
-static struct nested_calls poll_safewake_ncalls;
-
 /* Used to call file's f_op->poll() under the nested calls boundaries */
 static struct nested_calls poll_readywalk_ncalls;
 
@@ -551,40 +548,21 @@ out_unlock:
  * this special case of epoll.
  */
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
-static inline void ep_wake_up_nested(wait_queue_head_t *wqueue,
-                                    unsigned long events, int subclass)
+
+static struct nested_calls poll_safewake_ncalls;
+
+static int ep_poll_wakeup_proc(void *priv, void *cookie, int call_nests)
 {
        unsigned long flags;
+       wait_queue_head_t *wqueue = (wait_queue_head_t *)cookie;
 
-       spin_lock_irqsave_nested(&wqueue->lock, flags, subclass);
-       wake_up_locked_poll(wqueue, events);
+       spin_lock_irqsave_nested(&wqueue->lock, flags, call_nests + 1);
+       wake_up_locked_poll(wqueue, POLLIN);
        spin_unlock_irqrestore(&wqueue->lock, flags);
-}
-#else
-static inline void ep_wake_up_nested(wait_queue_head_t *wqueue,
-                                    unsigned long events, int subclass)
-{
-       wake_up_poll(wqueue, events);
-}
-#endif
 
-static int ep_poll_wakeup_proc(void *priv, void *cookie, int call_nests)
-{
-       ep_wake_up_nested((wait_queue_head_t *) cookie, POLLIN,
-                         1 + call_nests);
        return 0;
 }
 
-/*
- * Perform a safe wake up of the poll wait list. The problem is that
- * with the new callback'd wake up system, it is possible that the
- * poll callback is reentered from inside the call to wake_up() done
- * on the poll wait queue head. The rule is that we cannot reenter the
- * wake up code from the same task more than EP_MAX_NESTS times,
- * and we cannot reenter the same wait queue head at all. This will
- * enable to have a hierarchy of epoll file descriptor of no more than
- * EP_MAX_NESTS deep.
- */
 static void ep_poll_safewake(wait_queue_head_t *wq)
 {
        int this_cpu = get_cpu();
@@ -595,6 +573,15 @@ static void ep_poll_safewake(wait_queue_head_t *wq)
        put_cpu();
 }
 
+#else
+
+static void ep_poll_safewake(wait_queue_head_t *wq)
+{
+       wake_up_poll(wq, POLLIN);
+}
+
+#endif
+
 static void ep_remove_wait_queue(struct eppoll_entry *pwq)
 {
        wait_queue_head_t *whead;
@@ -796,7 +783,7 @@ static int ep_remove(struct eventpoll *ep, struct epitem *epi)
        list_del_rcu(&epi->fllink);
        spin_unlock(&file->f_lock);
 
-       rb_erase(&epi->rbn, &ep->rbr);
+       rb_erase_cached(&epi->rbn, &ep->rbr);
 
        spin_lock_irqsave(&ep->lock, flags);
        if (ep_is_linked(&epi->rdllink))
@@ -840,7 +827,7 @@ static void ep_free(struct eventpoll *ep)
        /*
         * Walks through the whole tree by unregistering poll callbacks.
         */
-       for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
+       for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) {
                epi = rb_entry(rbp, struct epitem, rbn);
 
                ep_unregister_pollwait(ep, epi);
@@ -856,7 +843,7 @@ static void ep_free(struct eventpoll *ep)
         * a lockdep warning.
         */
        mutex_lock(&ep->mtx);
-       while ((rbp = rb_first(&ep->rbr)) != NULL) {
+       while ((rbp = rb_first_cached(&ep->rbr)) != NULL) {
                epi = rb_entry(rbp, struct epitem, rbn);
                ep_remove(ep, epi);
                cond_resched();
@@ -963,7 +950,7 @@ static void ep_show_fdinfo(struct seq_file *m, struct file *f)
        struct rb_node *rbp;
 
        mutex_lock(&ep->mtx);
-       for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
+       for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) {
                struct epitem *epi = rb_entry(rbp, struct epitem, rbn);
                struct inode *inode = file_inode(epi->ffd.file);
 
@@ -1040,7 +1027,7 @@ static int ep_alloc(struct eventpoll **pep)
        init_waitqueue_head(&ep->wq);
        init_waitqueue_head(&ep->poll_wait);
        INIT_LIST_HEAD(&ep->rdllist);
-       ep->rbr = RB_ROOT;
+       ep->rbr = RB_ROOT_CACHED;
        ep->ovflist = EP_UNACTIVE_PTR;
        ep->user = user;
 
@@ -1066,7 +1053,7 @@ static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
        struct epoll_filefd ffd;
 
        ep_set_ffd(&ffd, file, fd);
-       for (rbp = ep->rbr.rb_node; rbp; ) {
+       for (rbp = ep->rbr.rb_root.rb_node; rbp; ) {
                epi = rb_entry(rbp, struct epitem, rbn);
                kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
                if (kcmp > 0)
@@ -1088,7 +1075,7 @@ static struct epitem *ep_find_tfd(struct eventpoll *ep, int tfd, unsigned long t
        struct rb_node *rbp;
        struct epitem *epi;
 
-       for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
+       for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) {
                epi = rb_entry(rbp, struct epitem, rbn);
                if (epi->ffd.fd == tfd) {
                        if (toff == 0)
@@ -1273,20 +1260,22 @@ static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
 static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
 {
        int kcmp;
-       struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
+       struct rb_node **p = &ep->rbr.rb_root.rb_node, *parent = NULL;
        struct epitem *epic;
+       bool leftmost = true;
 
        while (*p) {
                parent = *p;
                epic = rb_entry(parent, struct epitem, rbn);
                kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
-               if (kcmp > 0)
+               if (kcmp > 0) {
                        p = &parent->rb_right;
-               else
+                       leftmost = false;
+               } else
                        p = &parent->rb_left;
        }
        rb_link_node(&epi->rbn, parent, p);
-       rb_insert_color(&epi->rbn, &ep->rbr);
+       rb_insert_color_cached(&epi->rbn, &ep->rbr, leftmost);
 }
 
 
@@ -1530,7 +1519,7 @@ error_remove_epi:
        list_del_rcu(&epi->fllink);
        spin_unlock(&tfile->f_lock);
 
-       rb_erase(&epi->rbn, &ep->rbr);
+       rb_erase_cached(&epi->rbn, &ep->rbr);
 
 error_unregister:
        ep_unregister_pollwait(ep, epi);
@@ -1878,7 +1867,7 @@ static int ep_loop_check_proc(void *priv, void *cookie, int call_nests)
        mutex_lock_nested(&ep->mtx, call_nests + 1);
        ep->visited = 1;
        list_add(&ep->visited_list_link, &visited_list);
-       for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
+       for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) {
                epi = rb_entry(rbp, struct epitem, rbn);
                if (unlikely(is_file_epoll(epi->ffd.file))) {
                        ep_tovisit = epi->ffd.file->private_data;
@@ -2313,8 +2302,10 @@ static int __init eventpoll_init(void)
         */
        ep_nested_calls_init(&poll_loop_ncalls);
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
        /* Initialize the structure used to perform safe poll wait head wake ups */
        ep_nested_calls_init(&poll_safewake_ncalls);
+#endif
 
        /* Initialize the structure used to perform file's f_op->poll() calls */
        ep_nested_calls_init(&poll_readywalk_ncalls);
@@ -2327,11 +2318,11 @@ 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 | SLAB_PANIC, NULL);
+                       0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
 
        /* Allocates slab cache used to allocate "struct eppoll_entry" */
        pwq_cache = kmem_cache_create("eventpoll_pwq",
-                       sizeof(struct eppoll_entry), 0, SLAB_PANIC, NULL);
+               sizeof(struct eppoll_entry), 0, SLAB_PANIC|SLAB_ACCOUNT, NULL);
 
        return 0;
 }