]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blobdiff - mm/vmscan.c
mm: vmscan: clean up get_scan_count()
[mirror_ubuntu-zesty-kernel.git] / mm / vmscan.c
index 196709f5ee5862753f5f3731bdeab58c2c45c323..d4f37634194eceae488f836ce901e26d92e2dfc0 100644 (file)
@@ -1638,6 +1638,13 @@ static int vmscan_swappiness(struct scan_control *sc)
        return mem_cgroup_swappiness(sc->target_mem_cgroup);
 }
 
+enum scan_balance {
+       SCAN_EQUAL,
+       SCAN_FRACT,
+       SCAN_ANON,
+       SCAN_FILE,
+};
+
 /*
  * Determine how aggressively the anon and file LRU lists should be
  * scanned.  The relative value of each set of LRU lists is determined
@@ -1650,15 +1657,16 @@ static int vmscan_swappiness(struct scan_control *sc)
 static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
                           unsigned long *nr)
 {
-       unsigned long anon, file, free;
+       struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
+       u64 fraction[2];
+       u64 denominator = 0;    /* gcc */
+       struct zone *zone = lruvec_zone(lruvec);
        unsigned long anon_prio, file_prio;
+       enum scan_balance scan_balance;
+       unsigned long anon, file, free;
+       bool force_scan = false;
        unsigned long ap, fp;
-       struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
-       u64 fraction[2], denominator;
        enum lru_list lru;
-       int noswap = 0;
-       bool force_scan = false;
-       struct zone *zone = lruvec_zone(lruvec);
 
        /*
         * If the zone or memcg is small, nr[l] can be 0.  This
@@ -1677,10 +1685,29 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
 
        /* If we have no swap space, do not bother scanning anon pages. */
        if (!sc->may_swap || (nr_swap_pages <= 0)) {
-               noswap = 1;
-               fraction[0] = 0;
-               fraction[1] = 1;
-               denominator = 1;
+               scan_balance = SCAN_FILE;
+               goto out;
+       }
+
+       /*
+        * Global reclaim will swap to prevent OOM even with no
+        * swappiness, but memcg users want to use this knob to
+        * disable swapping for individual groups completely when
+        * using the memory controller's swap limit feature would be
+        * too expensive.
+        */
+       if (!global_reclaim(sc) && !vmscan_swappiness(sc)) {
+               scan_balance = SCAN_FILE;
+               goto out;
+       }
+
+       /*
+        * Do not apply any pressure balancing cleverness when the
+        * system is close to OOM, scan both anon and file equally
+        * (unless the swappiness setting disagrees with swapping).
+        */
+       if (!sc->priority && vmscan_swappiness(sc)) {
+               scan_balance = SCAN_EQUAL;
                goto out;
        }
 
@@ -1689,29 +1716,31 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
        file  = get_lru_size(lruvec, LRU_ACTIVE_FILE) +
                get_lru_size(lruvec, LRU_INACTIVE_FILE);
 
+       /*
+        * If it's foreseeable that reclaiming the file cache won't be
+        * enough to get the zone back into a desirable shape, we have
+        * to swap.  Better start now and leave the - probably heavily
+        * thrashing - remaining file pages alone.
+        */
        if (global_reclaim(sc)) {
-               free  = zone_page_state(zone, NR_FREE_PAGES);
+               free = zone_page_state(zone, NR_FREE_PAGES);
                if (unlikely(file + free <= high_wmark_pages(zone))) {
-                       /*
-                        * If we have very few page cache pages, force-scan
-                        * anon pages.
-                        */
-                       fraction[0] = 1;
-                       fraction[1] = 0;
-                       denominator = 1;
-                       goto out;
-               } else if (!inactive_file_is_low_global(zone)) {
-                       /*
-                        * There is enough inactive page cache, do not
-                        * reclaim anything from the working set right now.
-                        */
-                       fraction[0] = 0;
-                       fraction[1] = 1;
-                       denominator = 1;
+                       scan_balance = SCAN_ANON;
                        goto out;
                }
        }
 
+       /*
+        * There is enough inactive page cache, do not reclaim
+        * anything from the anonymous working set right now.
+        */
+       if (!inactive_file_is_low(lruvec)) {
+               scan_balance = SCAN_FILE;
+               goto out;
+       }
+
+       scan_balance = SCAN_FRACT;
+
        /*
         * With swappiness at 100, anonymous and file have the same priority.
         * This scanning priority is essentially the inverse of IO cost.
@@ -1759,14 +1788,35 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
 out:
        for_each_evictable_lru(lru) {
                int file = is_file_lru(lru);
+               unsigned long size;
                unsigned long scan;
 
-               scan = get_lru_size(lruvec, lru);
-               if (sc->priority || noswap || !vmscan_swappiness(sc)) {
-                       scan >>= sc->priority;
-                       if (!scan && force_scan)
-                               scan = SWAP_CLUSTER_MAX;
+               size = get_lru_size(lruvec, lru);
+               scan = size >> sc->priority;
+
+               if (!scan && force_scan)
+                       scan = min(size, SWAP_CLUSTER_MAX);
+
+               switch (scan_balance) {
+               case SCAN_EQUAL:
+                       /* Scan lists relative to size */
+                       break;
+               case SCAN_FRACT:
+                       /*
+                        * Scan types proportional to swappiness and
+                        * their relative recent reclaim efficiency.
+                        */
                        scan = div64_u64(scan * fraction[file], denominator);
+                       break;
+               case SCAN_FILE:
+               case SCAN_ANON:
+                       /* Scan one type exclusively */
+                       if ((scan_balance == SCAN_FILE) != file)
+                               scan = 0;
+                       break;
+               default:
+                       /* Look ma, no brain */
+                       BUG();
                }
                nr[lru] = scan;
        }