drm/i915: wire up shrinkctl->nr_scanned
authorChris Wilson <chris@chris-wilson.co.uk>
Wed, 6 Sep 2017 23:19:30 +0000 (16:19 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 7 Sep 2017 00:27:25 +0000 (17:27 -0700)
shrink_slab() allows us to report back the number of objects we
successfully scanned (out of the target shrinkctl->nr_to_scan).  As
report the number of pages owned by each GEM object as a separate item
to the shrinker, we cannot precisely control the number of shrinker
objects we scan on each pass; and indeed may free more than requested.
If we fail to tell the shrinker about the number of objects we process,
it will continue to hold a grudge against us as any objects left
unscanned are added to the next reclaim -- and so we will keep on
"unfairly" shrinking our own slab in comparison to other slabs.

Link: http://lkml.kernel.org/r/20170822135325.9191-2-chris@chris-wilson.co.uk
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Hillf Danton <hillf.zj@alibaba-inc.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Mel Gorman <mgorman@techsingularity.net>
Cc: Shaohua Li <shli@fb.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Pekka Enberg <penberg@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
drivers/gpu/drm/i915/i915_debugfs.c
drivers/gpu/drm/i915/i915_drv.h
drivers/gpu/drm/i915/i915_gem.c
drivers/gpu/drm/i915/i915_gem_gtt.c
drivers/gpu/drm/i915/i915_gem_shrinker.c

index a36216bd2a84aee3145399c45aa2a4a438a8707f..e4d4b6b41e26157e3f7c86a22bd906445be2e717 100644 (file)
@@ -4308,10 +4308,10 @@ i915_drop_caches_set(void *data, u64 val)
 
        fs_reclaim_acquire(GFP_KERNEL);
        if (val & DROP_BOUND)
-               i915_gem_shrink(dev_priv, LONG_MAX, I915_SHRINK_BOUND);
+               i915_gem_shrink(dev_priv, LONG_MAX, NULL, I915_SHRINK_BOUND);
 
        if (val & DROP_UNBOUND)
-               i915_gem_shrink(dev_priv, LONG_MAX, I915_SHRINK_UNBOUND);
+               i915_gem_shrink(dev_priv, LONG_MAX, NULL, I915_SHRINK_UNBOUND);
 
        if (val & DROP_SHRINK_ALL)
                i915_gem_shrink_all(dev_priv);
index 60267e375e8812374fca2f5113401b85df23eebf..bd74641ab7f6ebb85e0fe68143371a510d77cdc7 100644 (file)
@@ -3742,6 +3742,7 @@ i915_gem_object_create_internal(struct drm_i915_private *dev_priv,
 /* i915_gem_shrinker.c */
 unsigned long i915_gem_shrink(struct drm_i915_private *dev_priv,
                              unsigned long target,
+                             unsigned long *nr_scanned,
                              unsigned flags);
 #define I915_SHRINK_PURGEABLE 0x1
 #define I915_SHRINK_UNBOUND 0x2
index b9e8e0d6e97ba81d0da0338540f8bb1795073a6d..287c6ead95b38b2072b37dfade9c2807ee4b853a 100644 (file)
@@ -2354,7 +2354,7 @@ rebuild_st:
                                goto err_sg;
                        }
 
-                       i915_gem_shrink(dev_priv, 2 * page_count, *s++);
+                       i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
                        cond_resched();
 
                        /* We've tried hard to allocate the memory by reaping
@@ -5015,7 +5015,7 @@ int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
         * the objects as well, see i915_gem_freeze()
         */
 
-       i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
+       i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
        i915_gem_drain_freed_objects(dev_priv);
 
        mutex_lock(&dev_priv->drm.struct_mutex);
index d60f38adc4c43aa49b1db183d2c859bf565d8c4b..6c6b8e8592aa01b7c8a514be87aa524ca29df045 100644 (file)
@@ -2062,7 +2062,7 @@ int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
                 */
                GEM_BUG_ON(obj->mm.pages == pages);
        } while (i915_gem_shrink(to_i915(obj->base.dev),
-                                obj->base.size >> PAGE_SHIFT,
+                                obj->base.size >> PAGE_SHIFT, NULL,
                                 I915_SHRINK_BOUND |
                                 I915_SHRINK_UNBOUND |
                                 I915_SHRINK_ACTIVE));
index 77fb3980813143d2d9e3432c0ebb994a4bcad032..74002b2d1b6f82640c7e1cf54b8791f01a049327 100644 (file)
@@ -136,6 +136,7 @@ static bool unsafe_drop_pages(struct drm_i915_gem_object *obj)
  * i915_gem_shrink - Shrink buffer object caches
  * @dev_priv: i915 device
  * @target: amount of memory to make available, in pages
+ * @nr_scanned: optional output for number of pages scanned (incremental)
  * @flags: control flags for selecting cache types
  *
  * This function is the main interface to the shrinker. It will try to release
@@ -158,7 +159,9 @@ static bool unsafe_drop_pages(struct drm_i915_gem_object *obj)
  */
 unsigned long
 i915_gem_shrink(struct drm_i915_private *dev_priv,
-               unsigned long target, unsigned flags)
+               unsigned long target,
+               unsigned long *nr_scanned,
+               unsigned flags)
 {
        const struct {
                struct list_head *list;
@@ -169,6 +172,7 @@ i915_gem_shrink(struct drm_i915_private *dev_priv,
                { NULL, 0 },
        }, *phase;
        unsigned long count = 0;
+       unsigned long scanned = 0;
        bool unlock;
 
        if (!shrinker_lock(dev_priv, &unlock))
@@ -249,6 +253,7 @@ i915_gem_shrink(struct drm_i915_private *dev_priv,
                                        count += obj->base.size >> PAGE_SHIFT;
                                }
                                mutex_unlock(&obj->mm.lock);
+                               scanned += obj->base.size >> PAGE_SHIFT;
                        }
                }
                list_splice_tail(&still_in_list, phase->list);
@@ -261,6 +266,8 @@ i915_gem_shrink(struct drm_i915_private *dev_priv,
 
        shrinker_unlock(dev_priv, unlock);
 
+       if (nr_scanned)
+               *nr_scanned += scanned;
        return count;
 }
 
@@ -283,7 +290,7 @@ unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv)
        unsigned long freed;
 
        intel_runtime_pm_get(dev_priv);
-       freed = i915_gem_shrink(dev_priv, -1UL,
+       freed = i915_gem_shrink(dev_priv, -1UL, NULL,
                                I915_SHRINK_BOUND |
                                I915_SHRINK_UNBOUND |
                                I915_SHRINK_ACTIVE);
@@ -329,23 +336,28 @@ i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
        unsigned long freed;
        bool unlock;
 
+       sc->nr_scanned = 0;
+
        if (!shrinker_lock(dev_priv, &unlock))
                return SHRINK_STOP;
 
        freed = i915_gem_shrink(dev_priv,
                                sc->nr_to_scan,
+                               &sc->nr_scanned,
                                I915_SHRINK_BOUND |
                                I915_SHRINK_UNBOUND |
                                I915_SHRINK_PURGEABLE);
        if (freed < sc->nr_to_scan)
                freed += i915_gem_shrink(dev_priv,
-                                        sc->nr_to_scan - freed,
+                                        sc->nr_to_scan - sc->nr_scanned,
+                                        &sc->nr_scanned,
                                         I915_SHRINK_BOUND |
                                         I915_SHRINK_UNBOUND);
        if (freed < sc->nr_to_scan && current_is_kswapd()) {
                intel_runtime_pm_get(dev_priv);
                freed += i915_gem_shrink(dev_priv,
-                                        sc->nr_to_scan - freed,
+                                        sc->nr_to_scan - sc->nr_scanned,
+                                        &sc->nr_scanned,
                                         I915_SHRINK_ACTIVE |
                                         I915_SHRINK_BOUND |
                                         I915_SHRINK_UNBOUND);
@@ -354,7 +366,7 @@ i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
 
        shrinker_unlock(dev_priv, unlock);
 
-       return freed;
+       return sc->nr_scanned ? freed : SHRINK_STOP;
 }
 
 static bool
@@ -453,7 +465,7 @@ i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr
                goto out;
 
        intel_runtime_pm_get(dev_priv);
-       freed_pages += i915_gem_shrink(dev_priv, -1UL,
+       freed_pages += i915_gem_shrink(dev_priv, -1UL, NULL,
                                       I915_SHRINK_BOUND |
                                       I915_SHRINK_UNBOUND |
                                       I915_SHRINK_ACTIVE |