[PATCH] drm/i915: Recover batch pool caches from shrinker

Chris Wilson chris at chris-wilson.co.uk
Wed Feb 27 14:20:29 UTC 2019


Discard all of our batch pools under mempressure to make their pages
available to the shrinker. We will quickly reacquire them when necessary
for more GPU relocations or for the command parser.

v2: Init the lists for mock_engine
v3: Return a strong ref from i915_gem_batch_pool_get() and convert it
into an active reference to protect ourselves against all allocations
while the object is in play.
v4: Couple shadow batch to active request early.

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=107936
Signed-off-by: Chris Wilson <chris at chris-wilson.co.uk>
Cc: Matthew Auld <matthew.william.auld at gmail.com>
---
 drivers/gpu/drm/i915/i915_gem_batch_pool.c   | 10 +++--
 drivers/gpu/drm/i915/i915_gem_execbuffer.c   | 44 +++++++++++++-------
 drivers/gpu/drm/i915/i915_gem_object.h       |  1 +
 drivers/gpu/drm/i915/i915_gem_shrinker.c     | 11 +++++
 drivers/gpu/drm/i915/selftests/mock_engine.c |  1 +
 5 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_gem_batch_pool.c b/drivers/gpu/drm/i915/i915_gem_batch_pool.c
index f3890b664e3f..defacde21dca 100644
--- a/drivers/gpu/drm/i915/i915_gem_batch_pool.c
+++ b/drivers/gpu/drm/i915/i915_gem_batch_pool.c
@@ -131,10 +131,14 @@ i915_gem_batch_pool_get(struct i915_gem_batch_pool *pool,
 		return obj;
 
 found:
+	GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
+	list_del(&obj->batch_pool_link);
 	ret = i915_gem_object_pin_pages(obj);
-	if (ret)
+	if (ret) {
+		i915_gem_object_put(obj);
 		return ERR_PTR(ret);
+	}
 
-	list_move_tail(&obj->batch_pool_link, list);
-	return obj;
+	list_add_tail(&obj->batch_pool_link, list);
+	return i915_gem_object_get(obj);
 }
diff --git a/drivers/gpu/drm/i915/i915_gem_execbuffer.c b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
index 48b23c6a024e..d404efe335ce 100644
--- a/drivers/gpu/drm/i915/i915_gem_execbuffer.c
+++ b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
@@ -1211,8 +1211,10 @@ static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
 				      I915_MAP_FORCE_WB :
 				      I915_MAP_FORCE_WC);
 	i915_gem_object_unpin_pages(obj);
-	if (IS_ERR(cmd))
-		return PTR_ERR(cmd);
+	if (IS_ERR(cmd)) {
+		err = PTR_ERR(cmd);
+		goto err_put;
+	}
 
 	err = i915_gem_object_set_to_wc_domain(obj, false);
 	if (err)
@@ -1255,6 +1257,7 @@ static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
 
 	rq->batch = batch;
 	i915_vma_unpin(batch);
+	i915_gem_object_set_active_reference(obj);
 
 	cache->rq = rq;
 	cache->rq_cmd = cmd;
@@ -1271,6 +1274,8 @@ static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
 	i915_vma_unpin(batch);
 err_unmap:
 	i915_gem_object_unpin_map(obj);
+err_put:
+	i915_gem_object_put(obj);
 	return err;
 }
 
@@ -2006,14 +2011,22 @@ static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
 	if (IS_ERR(vma))
 		goto out;
 
+	err = i915_vma_move_to_active(vma, eb->request, 0);
+	if (err) {
+		vma = ERR_PTR(err);
+		goto out;
+	}
+
 	eb->vma[eb->buffer_count] = i915_vma_get(vma);
-	eb->flags[eb->buffer_count] =
-		__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
+	eb->flags[eb->buffer_count] = __EXEC_OBJECT_HAS_PIN | EXEC_OBJECT_ASYNC;
 	vma->exec_flags = &eb->flags[eb->buffer_count];
 	eb->buffer_count++;
 
+	i915_gem_object_set_active_reference(shadow_batch_obj);
+
 out:
 	i915_gem_object_unpin_pages(shadow_batch_obj);
+	i915_gem_object_put(shadow_batch_obj);
 	return vma;
 }
 
@@ -2382,12 +2395,23 @@ i915_gem_do_execbuffer(struct drm_device *dev,
 		goto err_vma;
 	}
 
+	/* All GPU relocation batches must be submitted prior to the user rq */
+	GEM_BUG_ON(eb.reloc_cache.rq);
+
+	/* Allocate a request for this batch buffer nice and early. */
+	eb.request = i915_request_alloc(eb.engine, eb.ctx);
+	if (IS_ERR(eb.request)) {
+		err = PTR_ERR(eb.request);
+		goto err_vma;
+	}
+
 	if (eb_use_cmdparser(&eb)) {
 		struct i915_vma *vma;
 
 		vma = eb_parse(&eb, drm_is_current_master(file));
 		if (IS_ERR(vma)) {
 			err = PTR_ERR(vma);
+			i915_request_add(eb.request);
 			goto err_vma;
 		}
 
@@ -2430,22 +2454,13 @@ i915_gem_do_execbuffer(struct drm_device *dev,
 		vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
 		if (IS_ERR(vma)) {
 			err = PTR_ERR(vma);
+			i915_request_add(eb.request);
 			goto err_vma;
 		}
 
 		eb.batch = vma;
 	}
 
-	/* All GPU relocation batches must be submitted prior to the user rq */
-	GEM_BUG_ON(eb.reloc_cache.rq);
-
-	/* Allocate a request for this batch buffer nice and early. */
-	eb.request = i915_request_alloc(eb.engine, eb.ctx);
-	if (IS_ERR(eb.request)) {
-		err = PTR_ERR(eb.request);
-		goto err_batch_unpin;
-	}
-
 	if (in_fence) {
 		err = i915_request_await_dma_fence(eb.request, in_fence);
 		if (err < 0)
@@ -2495,7 +2510,6 @@ i915_gem_do_execbuffer(struct drm_device *dev,
 		}
 	}
 
-err_batch_unpin:
 	if (eb.batch_flags & I915_DISPATCH_SECURE)
 		i915_vma_unpin(eb.batch);
 err_vma:
diff --git a/drivers/gpu/drm/i915/i915_gem_object.h b/drivers/gpu/drm/i915/i915_gem_object.h
index fab040331cdb..bc23f2766d16 100644
--- a/drivers/gpu/drm/i915/i915_gem_object.h
+++ b/drivers/gpu/drm/i915/i915_gem_object.h
@@ -419,6 +419,7 @@ static inline void
 i915_gem_object_set_active_reference(struct drm_i915_gem_object *obj)
 {
 	lockdep_assert_held(&obj->base.dev->struct_mutex);
+	GEM_BUG_ON(test_bit(I915_BO_ACTIVE_REF, &obj->flags));
 	__set_bit(I915_BO_ACTIVE_REF, &obj->flags);
 }
 
diff --git a/drivers/gpu/drm/i915/i915_gem_shrinker.c b/drivers/gpu/drm/i915/i915_gem_shrinker.c
index 6da795c7e62e..771a09468bde 100644
--- a/drivers/gpu/drm/i915/i915_gem_shrinker.c
+++ b/drivers/gpu/drm/i915/i915_gem_shrinker.c
@@ -114,6 +114,15 @@ static bool unsafe_drop_pages(struct drm_i915_gem_object *obj)
 	return !i915_gem_object_has_pages(obj);
 }
 
+static void shrink_caches(struct drm_i915_private *i915)
+{
+	struct intel_engine_cs *engine;
+	enum intel_engine_id id;
+
+	for_each_engine(engine, i915, id)
+		i915_gem_batch_pool_fini(&engine->batch_pool);
+}
+
 /**
  * i915_gem_shrink - Shrink buffer object caches
  * @i915: i915 device
@@ -178,6 +187,8 @@ i915_gem_shrink(struct drm_i915_private *i915,
 	trace_i915_gem_shrink(i915, target, flags);
 	i915_retire_requests(i915);
 
+	shrink_caches(i915);
+
 	/*
 	 * Unbinding of objects will require HW access; Let us not wake the
 	 * device just to recover a little memory. If absolutely necessary,
diff --git a/drivers/gpu/drm/i915/selftests/mock_engine.c b/drivers/gpu/drm/i915/selftests/mock_engine.c
index 6f3fb803c747..ccc2296e131c 100644
--- a/drivers/gpu/drm/i915/selftests/mock_engine.c
+++ b/drivers/gpu/drm/i915/selftests/mock_engine.c
@@ -240,6 +240,7 @@ struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
 	i915_timeline_set_subclass(&engine->base.timeline, TIMELINE_ENGINE);
 
 	intel_engine_init_breadcrumbs(&engine->base);
+	intel_engine_init_batch_pool(&engine->base);
 
 	/* fake hw queue */
 	spin_lock_init(&engine->hw_lock);
-- 
2.20.1



More information about the Intel-gfx-trybot mailing list