Merge branch 'xarray' of git://git.infradead.org/users/willy/linux-dax
[sfrench/cifs-2.6.git] / mm / workingset.c
index 5cfb29ec3fd92b4621abe7426d7e695d68279cbb..d46f8c92aa2fe736a1248ae30e0fb73a17b58a3e 100644 (file)
  * the only thing eating into inactive list space is active pages.
  *
  *
- *             Activating refaulting pages
+ *             Refaulting inactive pages
  *
  * All that is known about the active list is that the pages have been
  * accessed more than once in the past.  This means that at any given
  * used less frequently than the refaulting page - or even not used at
  * all anymore.
  *
+ * That means if inactive cache is refaulting with a suitable refault
+ * distance, we assume the cache workingset is transitioning and put
+ * pressure on the current active list.
+ *
  * If this is wrong and demotion kicks in, the pages which are truly
  * used more frequently will be reactivated while the less frequently
  * used once will be evicted from memory.
  * But if this is right, the stale pages will be pushed out of memory
  * and the used pages get to stay in cache.
  *
+ *             Refaulting active pages
+ *
+ * If on the other hand the refaulting pages have recently been
+ * deactivated, it means that the active list is no longer protecting
+ * actively used cache from reclaim. The cache is NOT transitioning to
+ * a different workingset; the existing workingset is thrashing in the
+ * space allocated to the page cache.
+ *
  *
  *             Implementation
  *
  */
 
 #define EVICTION_SHIFT ((BITS_PER_LONG - BITS_PER_XA_VALUE) +  \
-                        NODES_SHIFT +                          \
-                        MEM_CGROUP_ID_SHIFT)
+                        1 + NODES_SHIFT + MEM_CGROUP_ID_SHIFT)
 #define EVICTION_MASK  (~0UL >> EVICTION_SHIFT)
 
 /*
  */
 static unsigned int bucket_order __read_mostly;
 
-static void *pack_shadow(int memcgid, pg_data_t *pgdat, unsigned long eviction)
+static void *pack_shadow(int memcgid, pg_data_t *pgdat, unsigned long eviction,
+                        bool workingset)
 {
        eviction >>= bucket_order;
        eviction &= EVICTION_MASK;
        eviction = (eviction << MEM_CGROUP_ID_SHIFT) | memcgid;
        eviction = (eviction << NODES_SHIFT) | pgdat->node_id;
+       eviction = (eviction << 1) | workingset;
 
        return xa_mk_value(eviction);
 }
 
 static void unpack_shadow(void *shadow, int *memcgidp, pg_data_t **pgdat,
-                         unsigned long *evictionp)
+                         unsigned long *evictionp, bool *workingsetp)
 {
        unsigned long entry = xa_to_value(shadow);
        int memcgid, nid;
+       bool workingset;
 
+       workingset = entry & 1;
+       entry >>= 1;
        nid = entry & ((1UL << NODES_SHIFT) - 1);
        entry >>= NODES_SHIFT;
        memcgid = entry & ((1UL << MEM_CGROUP_ID_SHIFT) - 1);
@@ -194,6 +210,7 @@ static void unpack_shadow(void *shadow, int *memcgidp, pg_data_t **pgdat,
        *memcgidp = memcgid;
        *pgdat = NODE_DATA(nid);
        *evictionp = entry << bucket_order;
+       *workingsetp = workingset;
 }
 
 /**
@@ -206,8 +223,8 @@ static void unpack_shadow(void *shadow, int *memcgidp, pg_data_t **pgdat,
  */
 void *workingset_eviction(struct address_space *mapping, struct page *page)
 {
-       struct mem_cgroup *memcg = page_memcg(page);
        struct pglist_data *pgdat = page_pgdat(page);
+       struct mem_cgroup *memcg = page_memcg(page);
        int memcgid = mem_cgroup_id(memcg);
        unsigned long eviction;
        struct lruvec *lruvec;
@@ -219,30 +236,30 @@ void *workingset_eviction(struct address_space *mapping, struct page *page)
 
        lruvec = mem_cgroup_lruvec(pgdat, memcg);
        eviction = atomic_long_inc_return(&lruvec->inactive_age);
-       return pack_shadow(memcgid, pgdat, eviction);
+       return pack_shadow(memcgid, pgdat, eviction, PageWorkingset(page));
 }
 
 /**
  * workingset_refault - evaluate the refault of a previously evicted page
+ * @page: the freshly allocated replacement page
  * @shadow: shadow entry of the evicted page
  *
  * Calculates and evaluates the refault distance of the previously
  * evicted page in the context of the node it was allocated in.
- *
- * Returns %true if the page should be activated, %false otherwise.
  */
-bool workingset_refault(void *shadow)
+void workingset_refault(struct page *page, void *shadow)
 {
        unsigned long refault_distance;
+       struct pglist_data *pgdat;
        unsigned long active_file;
        struct mem_cgroup *memcg;
        unsigned long eviction;
        struct lruvec *lruvec;
        unsigned long refault;
-       struct pglist_data *pgdat;
+       bool workingset;
        int memcgid;
 
-       unpack_shadow(shadow, &memcgid, &pgdat, &eviction);
+       unpack_shadow(shadow, &memcgid, &pgdat, &eviction, &workingset);
 
        rcu_read_lock();
        /*
@@ -262,41 +279,51 @@ bool workingset_refault(void *shadow)
         * configurations instead.
         */
        memcg = mem_cgroup_from_id(memcgid);
-       if (!mem_cgroup_disabled() && !memcg) {
-               rcu_read_unlock();
-               return false;
-       }
+       if (!mem_cgroup_disabled() && !memcg)
+               goto out;
        lruvec = mem_cgroup_lruvec(pgdat, memcg);
        refault = atomic_long_read(&lruvec->inactive_age);
        active_file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES);
 
        /*
-        * The unsigned subtraction here gives an accurate distance
-        * across inactive_age overflows in most cases.
+        * Calculate the refault distance
         *
-        * There is a special case: usually, shadow entries have a
-        * short lifetime and are either refaulted or reclaimed along
-        * with the inode before they get too old.  But it is not
-        * impossible for the inactive_age to lap a shadow entry in
-        * the field, which can then can result in a false small
-        * refault distance, leading to a false activation should this
-        * old entry actually refault again.  However, earlier kernels
-        * used to deactivate unconditionally with *every* reclaim
-        * invocation for the longest time, so the occasional
-        * inappropriate activation leading to pressure on the active
-        * list is not a problem.
+        * The unsigned subtraction here gives an accurate distance
+        * across inactive_age overflows in most cases. There is a
+        * special case: usually, shadow entries have a short lifetime
+        * and are either refaulted or reclaimed along with the inode
+        * before they get too old.  But it is not impossible for the
+        * inactive_age to lap a shadow entry in the field, which can
+        * then result in a false small refault distance, leading to a
+        * false activation should this old entry actually refault
+        * again.  However, earlier kernels used to deactivate
+        * unconditionally with *every* reclaim invocation for the
+        * longest time, so the occasional inappropriate activation
+        * leading to pressure on the active list is not a problem.
         */
        refault_distance = (refault - eviction) & EVICTION_MASK;
 
        inc_lruvec_state(lruvec, WORKINGSET_REFAULT);
 
-       if (refault_distance <= active_file) {
-               inc_lruvec_state(lruvec, WORKINGSET_ACTIVATE);
-               rcu_read_unlock();
-               return true;
+       /*
+        * Compare the distance to the existing workingset size. We
+        * don't act on pages that couldn't stay resident even if all
+        * the memory was available to the page cache.
+        */
+       if (refault_distance > active_file)
+               goto out;
+
+       SetPageActive(page);
+       atomic_long_inc(&lruvec->inactive_age);
+       inc_lruvec_state(lruvec, WORKINGSET_ACTIVATE);
+
+       /* Page was active prior to eviction */
+       if (workingset) {
+               SetPageWorkingset(page);
+               inc_lruvec_state(lruvec, WORKINGSET_RESTORE);
        }
+out:
        rcu_read_unlock();
-       return false;
 }
 
 /**
@@ -349,12 +376,20 @@ void workingset_update_node(struct xa_node *node)
         * already where they should be. The list_empty() test is safe
         * as node->private_list is protected by the i_pages lock.
         */
+       VM_WARN_ON_ONCE(!irqs_disabled());  /* For __inc_lruvec_page_state */
+
        if (node->count && node->count == node->nr_values) {
-               if (list_empty(&node->private_list))
+               if (list_empty(&node->private_list)) {
                        list_lru_add(&shadow_nodes, &node->private_list);
+                       __inc_lruvec_page_state(virt_to_page(node),
+                                               WORKINGSET_NODES);
+               }
        } else {
-               if (!list_empty(&node->private_list))
+               if (!list_empty(&node->private_list)) {
                        list_lru_del(&shadow_nodes, &node->private_list);
+                       __dec_lruvec_page_state(virt_to_page(node),
+                                               WORKINGSET_NODES);
+               }
        }
 }
 
@@ -363,7 +398,7 @@ static unsigned long count_shadow_nodes(struct shrinker *shrinker,
 {
        unsigned long max_nodes;
        unsigned long nodes;
-       unsigned long cache;
+       unsigned long pages;
 
        nodes = list_lru_shrink_count(&shadow_nodes, sc);
 
@@ -389,14 +424,20 @@ static unsigned long count_shadow_nodes(struct shrinker *shrinker,
         *
         * PAGE_SIZE / xa_nodes / node_entries * 8 / PAGE_SIZE
         */
+#ifdef CONFIG_MEMCG
        if (sc->memcg) {
-               cache = mem_cgroup_node_nr_lru_pages(sc->memcg, sc->nid,
-                                                    LRU_ALL_FILE);
-       } else {
-               cache = node_page_state(NODE_DATA(sc->nid), NR_ACTIVE_FILE) +
-                       node_page_state(NODE_DATA(sc->nid), NR_INACTIVE_FILE);
-       }
-       max_nodes = cache >> (XA_CHUNK_SHIFT - 3);
+               struct lruvec *lruvec;
+
+               pages = mem_cgroup_node_nr_lru_pages(sc->memcg, sc->nid,
+                                                    LRU_ALL);
+               lruvec = mem_cgroup_lruvec(NODE_DATA(sc->nid), sc->memcg);
+               pages += lruvec_page_state(lruvec, NR_SLAB_RECLAIMABLE);
+               pages += lruvec_page_state(lruvec, NR_SLAB_UNRECLAIMABLE);
+       } else
+#endif
+               pages = node_present_pages(sc->nid);
+
+       max_nodes = pages >> (XA_CHUNK_SHIFT - 3);
 
        if (!nodes)
                return SHRINK_EMPTY;
@@ -438,6 +479,8 @@ static enum lru_status shadow_lru_isolate(struct list_head *item,
        }
 
        list_lru_isolate(lru, item);
+       __dec_lruvec_page_state(virt_to_page(node), WORKINGSET_NODES);
+
        spin_unlock(lru_lock);
 
        /*
@@ -459,7 +502,7 @@ static enum lru_status shadow_lru_isolate(struct list_head *item,
         * shadow entries we were tracking ...
         */
        xas_store(&xas, NULL);
-       inc_lruvec_page_state(virt_to_page(node), WORKINGSET_NODERECLAIM);
+       __inc_lruvec_page_state(virt_to_page(node), WORKINGSET_NODERECLAIM);
 
 out_invalid:
        xa_unlock_irq(&mapping->i_pages);
@@ -481,7 +524,7 @@ static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
 static struct shrinker workingset_shadow_shrinker = {
        .count_objects = count_shadow_nodes,
        .scan_objects = scan_shadow_nodes,
-       .seeks = DEFAULT_SEEKS,
+       .seeks = 0, /* ->count reports only fully expendable nodes */
        .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE,
 };