drm/panel: support transmit DSI packet
[firefly-linux-kernel-4.4.55.git] / mm / swap.c
index dfd7d71d68418023b592c09a7d18c7a3ac4c5843..39395fb549c013e20b34d5fe0be8afa6eee5afbf 100644 (file)
--- a/mm/swap.c
+++ b/mm/swap.c
 #include <linux/memcontrol.h>
 #include <linux/gfp.h>
 #include <linux/uio.h>
+#include <linux/hugetlb.h>
+#include <linux/page_idle.h>
 
 #include "internal.h"
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/pagemap.h>
+
 /* How many pages do we try to swap or page in/out together? */
 int page_cluster;
 
-static DEFINE_PER_CPU(struct pagevec[NR_LRU_LISTS], lru_add_pvecs);
+static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
 static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
-static DEFINE_PER_CPU(struct pagevec, lru_deactivate_pvecs);
+static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
 
 /*
  * This path almost never happens for VM activity - pages are normally
@@ -54,110 +59,216 @@ static void __page_cache_release(struct page *page)
 
                spin_lock_irqsave(&zone->lru_lock, flags);
                lruvec = mem_cgroup_page_lruvec(page, zone);
-               VM_BUG_ON(!PageLRU(page));
+               VM_BUG_ON_PAGE(!PageLRU(page), page);
                __ClearPageLRU(page);
                del_page_from_lru_list(page, lruvec, page_off_lru(page));
                spin_unlock_irqrestore(&zone->lru_lock, flags);
        }
+       mem_cgroup_uncharge(page);
 }
 
 static void __put_single_page(struct page *page)
 {
        __page_cache_release(page);
-       free_hot_cold_page(page, 0);
+       free_hot_cold_page(page, false);
 }
 
 static void __put_compound_page(struct page *page)
 {
        compound_page_dtor *dtor;
 
-       __page_cache_release(page);
+       /*
+        * __page_cache_release() is supposed to be called for thp, not for
+        * hugetlb. This is because hugetlb page does never have PageLRU set
+        * (it's never listed to any LRU lists) and no memcg routines should
+        * be called for hugetlb (it has a separate hugetlb_cgroup.)
+        */
+       if (!PageHuge(page))
+               __page_cache_release(page);
        dtor = get_compound_page_dtor(page);
        (*dtor)(page);
 }
 
-static void put_compound_page(struct page *page)
+/**
+ * Two special cases here: we could avoid taking compound_lock_irqsave
+ * and could skip the tail refcounting(in _mapcount).
+ *
+ * 1. Hugetlbfs page:
+ *
+ *    PageHeadHuge will remain true until the compound page
+ *    is released and enters the buddy allocator, and it could
+ *    not be split by __split_huge_page_refcount().
+ *
+ *    So if we see PageHeadHuge set, and we have the tail page pin,
+ *    then we could safely put head page.
+ *
+ * 2. Slab THP page:
+ *
+ *    PG_slab is cleared before the slab frees the head page, and
+ *    tail pin cannot be the last reference left on the head page,
+ *    because the slab code is free to reuse the compound page
+ *    after a kfree/kmem_cache_free without having to check if
+ *    there's any tail pin left.  In turn all tail pinsmust be always
+ *    released while the head is still pinned by the slab code
+ *    and so we know PG_slab will be still set too.
+ *
+ *    So if we see PageSlab set, and we have the tail page pin,
+ *    then we could safely put head page.
+ */
+static __always_inline
+void put_unrefcounted_compound_page(struct page *page_head, struct page *page)
 {
-       if (unlikely(PageTail(page))) {
-               /* __split_huge_page_refcount can run under us */
-               struct page *page_head = compound_trans_head(page);
-
-               if (likely(page != page_head &&
-                          get_page_unless_zero(page_head))) {
-                       unsigned long flags;
-
-                       /*
-                        * THP can not break up slab pages so avoid taking
-                        * compound_lock().  Slab performs non-atomic bit ops
-                        * on page->flags for better performance.  In particular
-                        * slab_unlock() in slub used to be a hot path.  It is
-                        * still hot on arches that do not support
-                        * this_cpu_cmpxchg_double().
-                        */
-                       if (PageSlab(page_head)) {
-                               if (PageTail(page)) {
-                                       if (put_page_testzero(page_head))
-                                               VM_BUG_ON(1);
-
-                                       atomic_dec(&page->_mapcount);
-                                       goto skip_lock_tail;
-                               } else
-                                       goto skip_lock;
-                       }
-                       /*
-                        * page_head wasn't a dangling pointer but it
-                        * may not be a head page anymore by the time
-                        * we obtain the lock. That is ok as long as it
-                        * can't be freed from under us.
-                        */
-                       flags = compound_lock_irqsave(page_head);
-                       if (unlikely(!PageTail(page))) {
-                               /* __split_huge_page_refcount run before us */
-                               compound_unlock_irqrestore(page_head, flags);
-skip_lock:
-                               if (put_page_testzero(page_head))
-                                       __put_single_page(page_head);
-out_put_single:
-                               if (put_page_testzero(page))
-                                       __put_single_page(page);
-                               return;
-                       }
-                       VM_BUG_ON(page_head != page->first_page);
+       /*
+        * If @page is a THP tail, we must read the tail page
+        * flags after the head page flags. The
+        * __split_huge_page_refcount side enforces write memory barriers
+        * between clearing PageTail and before the head page
+        * can be freed and reallocated.
+        */
+       smp_rmb();
+       if (likely(PageTail(page))) {
+               /*
+                * __split_huge_page_refcount cannot race
+                * here, see the comment above this function.
+                */
+               VM_BUG_ON_PAGE(!PageHead(page_head), page_head);
+               if (put_page_testzero(page_head)) {
                        /*
-                        * We can release the refcount taken by
-                        * get_page_unless_zero() now that
-                        * __split_huge_page_refcount() is blocked on
-                        * the compound_lock.
+                        * If this is the tail of a slab THP page,
+                        * the tail pin must not be the last reference
+                        * held on the page, because the PG_slab cannot
+                        * be cleared before all tail pins (which skips
+                        * the _mapcount tail refcounting) have been
+                        * released.
+                        *
+                        * If this is the tail of a hugetlbfs page,
+                        * the tail pin may be the last reference on
+                        * the page instead, because PageHeadHuge will
+                        * not go away until the compound page enters
+                        * the buddy allocator.
                         */
-                       if (put_page_testzero(page_head))
-                               VM_BUG_ON(1);
-                       /* __split_huge_page_refcount will wait now */
-                       VM_BUG_ON(page_mapcount(page) <= 0);
-                       atomic_dec(&page->_mapcount);
-                       VM_BUG_ON(atomic_read(&page_head->_count) <= 0);
-                       VM_BUG_ON(atomic_read(&page->_count) != 0);
-                       compound_unlock_irqrestore(page_head, flags);
+                       VM_BUG_ON_PAGE(PageSlab(page_head), page_head);
+                       __put_compound_page(page_head);
+               }
+       } else
+               /*
+                * __split_huge_page_refcount run before us,
+                * @page was a THP tail. The split @page_head
+                * has been freed and reallocated as slab or
+                * hugetlbfs page of smaller order (only
+                * possible if reallocated as slab on x86).
+                */
+               if (put_page_testzero(page))
+                       __put_single_page(page);
+}
 
-skip_lock_tail:
+static __always_inline
+void put_refcounted_compound_page(struct page *page_head, struct page *page)
+{
+       if (likely(page != page_head && get_page_unless_zero(page_head))) {
+               unsigned long flags;
+
+               /*
+                * @page_head wasn't a dangling pointer but it may not
+                * be a head page anymore by the time we obtain the
+                * lock. That is ok as long as it can't be freed from
+                * under us.
+                */
+               flags = compound_lock_irqsave(page_head);
+               if (unlikely(!PageTail(page))) {
+                       /* __split_huge_page_refcount run before us */
+                       compound_unlock_irqrestore(page_head, flags);
                        if (put_page_testzero(page_head)) {
+                               /*
+                                * The @page_head may have been freed
+                                * and reallocated as a compound page
+                                * of smaller order and then freed
+                                * again.  All we know is that it
+                                * cannot have become: a THP page, a
+                                * compound page of higher order, a
+                                * tail page.  That is because we
+                                * still hold the refcount of the
+                                * split THP tail and page_head was
+                                * the THP head before the split.
+                                */
                                if (PageHead(page_head))
                                        __put_compound_page(page_head);
                                else
                                        __put_single_page(page_head);
                        }
-               } else {
-                       /* page_head is a dangling pointer */
-                       VM_BUG_ON(PageTail(page));
-                       goto out_put_single;
+out_put_single:
+                       if (put_page_testzero(page))
+                               __put_single_page(page);
+                       return;
                }
-       } else if (put_page_testzero(page)) {
-               if (PageHead(page))
-                       __put_compound_page(page);
-               else
-                       __put_single_page(page);
+               VM_BUG_ON_PAGE(page_head != compound_head(page), page);
+               /*
+                * We can release the refcount taken by
+                * get_page_unless_zero() now that
+                * __split_huge_page_refcount() is blocked on the
+                * compound_lock.
+                */
+               if (put_page_testzero(page_head))
+                       VM_BUG_ON_PAGE(1, page_head);
+               /* __split_huge_page_refcount will wait now */
+               VM_BUG_ON_PAGE(page_mapcount(page) <= 0, page);
+               atomic_dec(&page->_mapcount);
+               VM_BUG_ON_PAGE(atomic_read(&page_head->_count) <= 0, page_head);
+               VM_BUG_ON_PAGE(atomic_read(&page->_count) != 0, page);
+               compound_unlock_irqrestore(page_head, flags);
+
+               if (put_page_testzero(page_head)) {
+                       if (PageHead(page_head))
+                               __put_compound_page(page_head);
+                       else
+                               __put_single_page(page_head);
+               }
+       } else {
+               /* @page_head is a dangling pointer */
+               VM_BUG_ON_PAGE(PageTail(page), page);
+               goto out_put_single;
        }
 }
 
+static void put_compound_page(struct page *page)
+{
+       struct page *page_head;
+
+       /*
+        * We see the PageCompound set and PageTail not set, so @page maybe:
+        *  1. hugetlbfs head page, or
+        *  2. THP head page.
+        */
+       if (likely(!PageTail(page))) {
+               if (put_page_testzero(page)) {
+                       /*
+                        * By the time all refcounts have been released
+                        * split_huge_page cannot run anymore from under us.
+                        */
+                       if (PageHead(page))
+                               __put_compound_page(page);
+                       else
+                               __put_single_page(page);
+               }
+               return;
+       }
+
+       /*
+        * We see the PageCompound set and PageTail set, so @page maybe:
+        *  1. a tail hugetlbfs page, or
+        *  2. a tail THP page, or
+        *  3. a split THP page.
+        *
+        *  Case 3 is possible, as we may race with
+        *  __split_huge_page_refcount tearing down a THP page.
+        */
+       page_head = compound_head(page);
+       if (!__compound_tail_refcounted(page_head))
+               put_unrefcounted_compound_page(page_head, page);
+       else
+               put_refcounted_compound_page(page_head, page);
+}
+
 void put_page(struct page *page)
 {
        if (unlikely(PageCompound(page)))
@@ -182,22 +293,37 @@ bool __get_page_tail(struct page *page)
         * split_huge_page().
         */
        unsigned long flags;
-       bool got = false;
-       struct page *page_head = compound_trans_head(page);
-
-       if (likely(page != page_head && get_page_unless_zero(page_head))) {
+       bool got;
+       struct page *page_head = compound_head(page);
 
-               /* Ref to put_compound_page() comment. */
-               if (PageSlab(page_head)) {
-                       if (likely(PageTail(page))) {
-                               __get_page_tail_foll(page, false);
-                               return true;
-                       } else {
-                               put_page(page_head);
-                               return false;
-                       }
+       /* Ref to put_compound_page() comment. */
+       if (!__compound_tail_refcounted(page_head)) {
+               smp_rmb();
+               if (likely(PageTail(page))) {
+                       /*
+                        * This is a hugetlbfs page or a slab
+                        * page. __split_huge_page_refcount
+                        * cannot race here.
+                        */
+                       VM_BUG_ON_PAGE(!PageHead(page_head), page_head);
+                       __get_page_tail_foll(page, true);
+                       return true;
+               } else {
+                       /*
+                        * __split_huge_page_refcount run
+                        * before us, "page" was a THP
+                        * tail. The split page_head has been
+                        * freed and reallocated as slab or
+                        * hugetlbfs page of smaller order
+                        * (only possible if reallocated as
+                        * slab on x86).
+                        */
+                       return false;
                }
+       }
 
+       got = false;
+       if (likely(page != page_head && get_page_unless_zero(page_head))) {
                /*
                 * page_head wasn't a dangling pointer but it
                 * may not be a head page anymore by the time
@@ -356,7 +482,7 @@ void rotate_reclaimable_page(struct page *page)
 
                page_cache_get(page);
                local_irq_save(flags);
-               pvec = &__get_cpu_var(lru_rotate_pvecs);
+               pvec = this_cpu_ptr(&lru_rotate_pvecs);
                if (!pagevec_add(pvec, page))
                        pagevec_move_tail(pvec);
                local_irq_restore(flags);
@@ -384,6 +510,7 @@ static void __activate_page(struct page *page, struct lruvec *lruvec,
                SetPageActive(page);
                lru += LRU_ACTIVE;
                add_page_to_lru_list(page, lruvec, lru);
+               trace_mm_lru_activate(page);
 
                __count_vm_event(PGACTIVATE);
                update_page_reclaim_stat(lruvec, file, 1);
@@ -401,6 +528,11 @@ static void activate_page_drain(int cpu)
                pagevec_lru_move_fn(pvec, __activate_page, NULL);
 }
 
+static bool need_activate_page_drain(int cpu)
+{
+       return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
+}
+
 void activate_page(struct page *page)
 {
        if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
@@ -418,6 +550,11 @@ static inline void activate_page_drain(int cpu)
 {
 }
 
+static bool need_activate_page_drain(int cpu)
+{
+       return false;
+}
+
 void activate_page(struct page *page)
 {
        struct zone *zone = page_zone(page);
@@ -428,62 +565,113 @@ void activate_page(struct page *page)
 }
 #endif
 
+static void __lru_cache_activate_page(struct page *page)
+{
+       struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
+       int i;
+
+       /*
+        * Search backwards on the optimistic assumption that the page being
+        * activated has just been added to this pagevec. Note that only
+        * the local pagevec is examined as a !PageLRU page could be in the
+        * process of being released, reclaimed, migrated or on a remote
+        * pagevec that is currently being drained. Furthermore, marking
+        * a remote pagevec's page PageActive potentially hits a race where
+        * a page is marked PageActive just after it is added to the inactive
+        * list causing accounting errors and BUG_ON checks to trigger.
+        */
+       for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
+               struct page *pagevec_page = pvec->pages[i];
+
+               if (pagevec_page == page) {
+                       SetPageActive(page);
+                       break;
+               }
+       }
+
+       put_cpu_var(lru_add_pvec);
+}
+
 /*
  * Mark a page as having seen activity.
  *
  * inactive,unreferenced       ->      inactive,referenced
  * inactive,referenced         ->      active,unreferenced
  * active,unreferenced         ->      active,referenced
+ *
+ * When a newly allocated page is not yet visible, so safe for non-atomic ops,
+ * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
  */
 void mark_page_accessed(struct page *page)
 {
        if (!PageActive(page) && !PageUnevictable(page) &&
-                       PageReferenced(page) && PageLRU(page)) {
-               activate_page(page);
+                       PageReferenced(page)) {
+
+               /*
+                * If the page is on the LRU, queue it for activation via
+                * activate_page_pvecs. Otherwise, assume the page is on a
+                * pagevec, mark it active and it'll be moved to the active
+                * LRU on the next drain.
+                */
+               if (PageLRU(page))
+                       activate_page(page);
+               else
+                       __lru_cache_activate_page(page);
                ClearPageReferenced(page);
+               if (page_is_file_cache(page))
+                       workingset_activation(page);
        } else if (!PageReferenced(page)) {
                SetPageReferenced(page);
        }
+       if (page_is_idle(page))
+               clear_page_idle(page);
 }
 EXPORT_SYMBOL(mark_page_accessed);
 
-/*
- * Order of operations is important: flush the pagevec when it's already
- * full, not when adding the last page, to make sure that last page is
- * not added to the LRU directly when passed to this function. Because
- * mark_page_accessed() (called after this when writing) only activates
- * pages that are on the LRU, linear writes in subpage chunks would see
- * every PAGEVEC_SIZE page activated, which is unexpected.
- */
-void __lru_cache_add(struct page *page, enum lru_list lru)
+static void __lru_cache_add(struct page *page)
 {
-       struct pagevec *pvec = &get_cpu_var(lru_add_pvecs)[lru];
+       struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
 
        page_cache_get(page);
        if (!pagevec_space(pvec))
-               __pagevec_lru_add(pvec, lru);
+               __pagevec_lru_add(pvec);
        pagevec_add(pvec, page);
-       put_cpu_var(lru_add_pvecs);
+       put_cpu_var(lru_add_pvec);
 }
-EXPORT_SYMBOL(__lru_cache_add);
 
 /**
- * lru_cache_add_lru - add a page to a page list
- * @page: the page to be added to the LRU.
- * @lru: the LRU list to which the page is added.
+ * lru_cache_add: add a page to the page lists
+ * @page: the page to add
  */
-void lru_cache_add_lru(struct page *page, enum lru_list lru)
+void lru_cache_add_anon(struct page *page)
 {
-       if (PageActive(page)) {
-               VM_BUG_ON(PageUnevictable(page));
+       if (PageActive(page))
                ClearPageActive(page);
-       } else if (PageUnevictable(page)) {
-               VM_BUG_ON(PageActive(page));
-               ClearPageUnevictable(page);
-       }
+       __lru_cache_add(page);
+}
+
+void lru_cache_add_file(struct page *page)
+{
+       if (PageActive(page))
+               ClearPageActive(page);
+       __lru_cache_add(page);
+}
+EXPORT_SYMBOL(lru_cache_add_file);
 
-       VM_BUG_ON(PageLRU(page) || PageActive(page) || PageUnevictable(page));
-       __lru_cache_add(page, lru);
+/**
+ * lru_cache_add - add a page to a page list
+ * @page: the page to be added to the LRU.
+ *
+ * Queue the page for addition to the LRU via pagevec. The decision on whether
+ * to add the page to the [in]active [file|anon] list is deferred until the
+ * pagevec is drained. This gives a chance for the caller of lru_cache_add()
+ * have the page added to the active list using mark_page_accessed().
+ */
+void lru_cache_add(struct page *page)
+{
+       VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
+       VM_BUG_ON_PAGE(PageLRU(page), page);
+       __lru_cache_add(page);
 }
 
 /**
@@ -503,12 +691,47 @@ void add_page_to_unevictable_list(struct page *page)
 
        spin_lock_irq(&zone->lru_lock);
        lruvec = mem_cgroup_page_lruvec(page, zone);
+       ClearPageActive(page);
        SetPageUnevictable(page);
        SetPageLRU(page);
        add_page_to_lru_list(page, lruvec, LRU_UNEVICTABLE);
        spin_unlock_irq(&zone->lru_lock);
 }
 
+/**
+ * lru_cache_add_active_or_unevictable
+ * @page:  the page to be added to LRU
+ * @vma:   vma in which page is mapped for determining reclaimability
+ *
+ * Place @page on the active or unevictable LRU list, depending on its
+ * evictability.  Note that if the page is not evictable, it goes
+ * directly back onto it's zone's unevictable list, it does NOT use a
+ * per cpu pagevec.
+ */
+void lru_cache_add_active_or_unevictable(struct page *page,
+                                        struct vm_area_struct *vma)
+{
+       VM_BUG_ON_PAGE(PageLRU(page), page);
+
+       if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED)) {
+               SetPageActive(page);
+               lru_cache_add(page);
+               return;
+       }
+
+       if (!TestSetPageMlocked(page)) {
+               /*
+                * We use the irq-unsafe __mod_zone_page_stat because this
+                * counter is not modified from interrupt context, and the pte
+                * lock is held(spinlock), which implies preemption disabled.
+                */
+               __mod_zone_page_state(page_zone(page), NR_MLOCK,
+                                   hpage_nr_pages(page));
+               count_vm_event(UNEVICTABLE_PGMLOCKED);
+       }
+       add_page_to_unevictable_list(page);
+}
+
 /*
  * If the page can not be invalidated, it is moved to the
  * inactive list to speed up its reclaim.  It is moved to the
@@ -530,7 +753,7 @@ void add_page_to_unevictable_list(struct page *page)
  * be write it out by flusher threads as this is much more effective
  * than the single-page writeout from reclaim.
  */
-static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
+static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
                              void *arg)
 {
        int lru, file;
@@ -583,15 +806,10 @@ static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
  */
 void lru_add_drain_cpu(int cpu)
 {
-       struct pagevec *pvecs = per_cpu(lru_add_pvecs, cpu);
-       struct pagevec *pvec;
-       int lru;
+       struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
 
-       for_each_lru(lru) {
-               pvec = &pvecs[lru - LRU_BASE];
-               if (pagevec_count(pvec))
-                       __pagevec_lru_add(pvec, lru);
-       }
+       if (pagevec_count(pvec))
+               __pagevec_lru_add(pvec);
 
        pvec = &per_cpu(lru_rotate_pvecs, cpu);
        if (pagevec_count(pvec)) {
@@ -603,36 +821,36 @@ void lru_add_drain_cpu(int cpu)
                local_irq_restore(flags);
        }
 
-       pvec = &per_cpu(lru_deactivate_pvecs, cpu);
+       pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
        if (pagevec_count(pvec))
-               pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
+               pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
 
        activate_page_drain(cpu);
 }
 
 /**
- * deactivate_page - forcefully deactivate a page
+ * deactivate_file_page - forcefully deactivate a file page
  * @page: page to deactivate
  *
  * This function hints the VM that @page is a good reclaim candidate,
  * for example if its invalidation fails due to the page being dirty
  * or under writeback.
  */
-void deactivate_page(struct page *page)
+void deactivate_file_page(struct page *page)
 {
        /*
-        * In a workload with many unevictable page such as mprotect, unevictable
-        * page deactivation for accelerating reclaim is pointless.
+        * In a workload with many unevictable page such as mprotect,
+        * unevictable page deactivation for accelerating reclaim is pointless.
         */
        if (PageUnevictable(page))
                return;
 
        if (likely(get_page_unless_zero(page))) {
-               struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
+               struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
 
                if (!pagevec_add(pvec, page))
-                       pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
-               put_cpu_var(lru_deactivate_pvecs);
+                       pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
+               put_cpu_var(lru_deactivate_file_pvecs);
        }
 }
 
@@ -647,34 +865,55 @@ static void lru_add_drain_per_cpu(struct work_struct *dummy)
        lru_add_drain();
 }
 
-/*
- * Returns 0 for success
- */
-int lru_add_drain_all(void)
+static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
+
+void lru_add_drain_all(void)
 {
-       return schedule_on_each_cpu(lru_add_drain_per_cpu);
+       static DEFINE_MUTEX(lock);
+       static struct cpumask has_work;
+       int cpu;
+
+       mutex_lock(&lock);
+       get_online_cpus();
+       cpumask_clear(&has_work);
+
+       for_each_online_cpu(cpu) {
+               struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
+
+               if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
+                   pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
+                   pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
+                   need_activate_page_drain(cpu)) {
+                       INIT_WORK(work, lru_add_drain_per_cpu);
+                       schedule_work_on(cpu, work);
+                       cpumask_set_cpu(cpu, &has_work);
+               }
+       }
+
+       for_each_cpu(cpu, &has_work)
+               flush_work(&per_cpu(lru_add_drain_work, cpu));
+
+       put_online_cpus();
+       mutex_unlock(&lock);
 }
 
-/*
- * Batched page_cache_release().  Decrement the reference count on all the
- * passed pages.  If it fell to zero then remove the page from the LRU and
- * free it.
- *
- * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
- * for the remainder of the operation.
+/**
+ * release_pages - batched page_cache_release()
+ * @pages: array of pages to release
+ * @nr: number of pages
+ * @cold: whether the pages are cache cold
  *
- * The locking in this function is against shrink_inactive_list(): we recheck
- * the page count inside the lock to see whether shrink_inactive_list()
- * grabbed the page via the LRU.  If it did, give up: shrink_inactive_list()
- * will free it.
+ * Decrement the reference count on all the pages in @pages.  If it
+ * fell to zero, remove the page from the LRU and free it.
  */
-void release_pages(struct page **pages, int nr, int cold)
+void release_pages(struct page **pages, int nr, bool cold)
 {
        int i;
        LIST_HEAD(pages_to_free);
        struct zone *zone = NULL;
        struct lruvec *lruvec;
        unsigned long uninitialized_var(flags);
+       unsigned int uninitialized_var(lock_batch);
 
        for (i = 0; i < nr; i++) {
                struct page *page = pages[i];
@@ -688,6 +927,16 @@ void release_pages(struct page **pages, int nr, int cold)
                        continue;
                }
 
+               /*
+                * Make sure the IRQ-safe lock-holding time does not get
+                * excessive with a continuous string of pages from the
+                * same zone. The lock is held only if zone != NULL.
+                */
+               if (zone && ++lock_batch == SWAP_CLUSTER_MAX) {
+                       spin_unlock_irqrestore(&zone->lru_lock, flags);
+                       zone = NULL;
+               }
+
                if (!put_page_testzero(page))
                        continue;
 
@@ -698,21 +947,26 @@ void release_pages(struct page **pages, int nr, int cold)
                                if (zone)
                                        spin_unlock_irqrestore(&zone->lru_lock,
                                                                        flags);
+                               lock_batch = 0;
                                zone = pagezone;
                                spin_lock_irqsave(&zone->lru_lock, flags);
                        }
 
                        lruvec = mem_cgroup_page_lruvec(page, zone);
-                       VM_BUG_ON(!PageLRU(page));
+                       VM_BUG_ON_PAGE(!PageLRU(page), page);
                        __ClearPageLRU(page);
                        del_page_from_lru_list(page, lruvec, page_off_lru(page));
                }
 
+               /* Clear Active bit in case of parallel mark_page_accessed */
+               __ClearPageActive(page);
+
                list_add(&page->lru, &pages_to_free);
        }
        if (zone)
                spin_unlock_irqrestore(&zone->lru_lock, flags);
 
+       mem_cgroup_uncharge_list(&pages_to_free);
        free_hot_cold_page_list(&pages_to_free, cold);
 }
 EXPORT_SYMBOL(release_pages);
@@ -740,33 +994,17 @@ EXPORT_SYMBOL(__pagevec_release);
 void lru_add_page_tail(struct page *page, struct page *page_tail,
                       struct lruvec *lruvec, struct list_head *list)
 {
-       int uninitialized_var(active);
-       enum lru_list lru;
        const int file = 0;
 
-       VM_BUG_ON(!PageHead(page));
-       VM_BUG_ON(PageCompound(page_tail));
-       VM_BUG_ON(PageLRU(page_tail));
+       VM_BUG_ON_PAGE(!PageHead(page), page);
+       VM_BUG_ON_PAGE(PageCompound(page_tail), page);
+       VM_BUG_ON_PAGE(PageLRU(page_tail), page);
        VM_BUG_ON(NR_CPUS != 1 &&
                  !spin_is_locked(&lruvec_zone(lruvec)->lru_lock));
 
        if (!list)
                SetPageLRU(page_tail);
 
-       if (page_evictable(page_tail)) {
-               if (PageActive(page)) {
-                       SetPageActive(page_tail);
-                       active = 1;
-                       lru = LRU_ACTIVE_ANON;
-               } else {
-                       active = 0;
-                       lru = LRU_INACTIVE_ANON;
-               }
-       } else {
-               SetPageUnevictable(page_tail);
-               lru = LRU_UNEVICTABLE;
-       }
-
        if (likely(PageLRU(page)))
                list_add_tail(&page_tail->lru, &page->lru);
        else if (list) {
@@ -782,46 +1020,92 @@ void lru_add_page_tail(struct page *page, struct page *page_tail,
                 * Use the standard add function to put page_tail on the list,
                 * but then correct its position so they all end up in order.
                 */
-               add_page_to_lru_list(page_tail, lruvec, lru);
+               add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
                list_head = page_tail->lru.prev;
                list_move_tail(&page_tail->lru, list_head);
        }
 
        if (!PageUnevictable(page))
-               update_page_reclaim_stat(lruvec, file, active);
+               update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
 }
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
 static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
                                 void *arg)
 {
-       enum lru_list lru = (enum lru_list)arg;
-       int file = is_file_lru(lru);
-       int active = is_active_lru(lru);
+       int file = page_is_file_cache(page);
+       int active = PageActive(page);
+       enum lru_list lru = page_lru(page);
 
-       VM_BUG_ON(PageActive(page));
-       VM_BUG_ON(PageUnevictable(page));
-       VM_BUG_ON(PageLRU(page));
+       VM_BUG_ON_PAGE(PageLRU(page), page);
 
        SetPageLRU(page);
-       if (active)
-               SetPageActive(page);
        add_page_to_lru_list(page, lruvec, lru);
        update_page_reclaim_stat(lruvec, file, active);
+       trace_mm_lru_insertion(page, lru);
 }
 
 /*
  * Add the passed pages to the LRU, then drop the caller's refcount
  * on them.  Reinitialises the caller's pagevec.
  */
-void __pagevec_lru_add(struct pagevec *pvec, enum lru_list lru)
+void __pagevec_lru_add(struct pagevec *pvec)
 {
-       VM_BUG_ON(is_unevictable_lru(lru));
-
-       pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, (void *)lru);
+       pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
 }
 EXPORT_SYMBOL(__pagevec_lru_add);
 
+/**
+ * pagevec_lookup_entries - gang pagecache lookup
+ * @pvec:      Where the resulting entries are placed
+ * @mapping:   The address_space to search
+ * @start:     The starting entry index
+ * @nr_entries:        The maximum number of entries
+ * @indices:   The cache indices corresponding to the entries in @pvec
+ *
+ * pagevec_lookup_entries() will search for and return a group of up
+ * to @nr_entries pages and shadow entries in the mapping.  All
+ * entries are placed in @pvec.  pagevec_lookup_entries() takes a
+ * reference against actual pages in @pvec.
+ *
+ * The search returns a group of mapping-contiguous entries with
+ * ascending indexes.  There may be holes in the indices due to
+ * not-present entries.
+ *
+ * pagevec_lookup_entries() returns the number of entries which were
+ * found.
+ */
+unsigned pagevec_lookup_entries(struct pagevec *pvec,
+                               struct address_space *mapping,
+                               pgoff_t start, unsigned nr_pages,
+                               pgoff_t *indices)
+{
+       pvec->nr = find_get_entries(mapping, start, nr_pages,
+                                   pvec->pages, indices);
+       return pagevec_count(pvec);
+}
+
+/**
+ * pagevec_remove_exceptionals - pagevec exceptionals pruning
+ * @pvec:      The pagevec to prune
+ *
+ * pagevec_lookup_entries() fills both pages and exceptional radix
+ * tree entries into the pagevec.  This function prunes all
+ * exceptionals from @pvec without leaving holes, so that it can be
+ * passed on to page-only pagevec operations.
+ */
+void pagevec_remove_exceptionals(struct pagevec *pvec)
+{
+       int i, j;
+
+       for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
+               struct page *page = pvec->pages[i];
+               if (!radix_tree_exceptional_entry(page))
+                       pvec->pages[j++] = page;
+       }
+       pvec->nr = j;
+}
+
 /**
  * pagevec_lookup - gang pagecache lookup
  * @pvec:      Where the resulting pages are placed
@@ -864,11 +1148,8 @@ void __init swap_setup(void)
 #ifdef CONFIG_SWAP
        int i;
 
-       bdi_init(swapper_spaces[0].backing_dev_info);
-       for (i = 0; i < MAX_SWAPFILES; i++) {
+       for (i = 0; i < MAX_SWAPFILES; i++)
                spin_lock_init(&swapper_spaces[i].tree_lock);
-               INIT_LIST_HEAD(&swapper_spaces[i].i_mmap_nonlinear);
-       }
 #endif
 
        /* Use a smaller cluster for small-memory machines */