drm/i915: Initial selftests for exercising eviction
authorChris Wilson <chris@chris-wilson.co.uk>
Mon, 13 Feb 2017 17:15:52 +0000 (17:15 +0000)
committerChris Wilson <chris@chris-wilson.co.uk>
Mon, 13 Feb 2017 20:46:47 +0000 (20:46 +0000)
Very simple tests to just ask eviction to find some free space in a full
GTT and one with some available space.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Link: http://patchwork.freedesktop.org/patch/msgid/20170213171558.20942-41-chris@chris-wilson.co.uk
drivers/gpu/drm/i915/i915_gem_evict.c
drivers/gpu/drm/i915/selftests/i915_gem_evict.c [new file with mode: 0644]
drivers/gpu/drm/i915/selftests/i915_mock_selftests.h

index 776c508cb9aec5cccbfb3a20b166e2151e21c7f6..ca25b1f7f6d1b89fe5690a015896f2a0bdbff038 100644 (file)
@@ -393,3 +393,7 @@ int i915_gem_evict_vm(struct i915_address_space *vm, bool do_idle)
 
        return 0;
 }
+
+#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
+#include "selftests/i915_gem_evict.c"
+#endif
diff --git a/drivers/gpu/drm/i915/selftests/i915_gem_evict.c b/drivers/gpu/drm/i915/selftests/i915_gem_evict.c
new file mode 100644 (file)
index 0000000..97af353
--- /dev/null
@@ -0,0 +1,260 @@
+/*
+ * Copyright © 2016 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#include "../i915_selftest.h"
+
+#include "mock_gem_device.h"
+
+static int populate_ggtt(struct drm_i915_private *i915)
+{
+       struct drm_i915_gem_object *obj;
+       u64 size;
+
+       for (size = 0;
+            size + I915_GTT_PAGE_SIZE <= i915->ggtt.base.total;
+            size += I915_GTT_PAGE_SIZE) {
+               struct i915_vma *vma;
+
+               obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
+               if (IS_ERR(obj))
+                       return PTR_ERR(obj);
+
+               vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
+               if (IS_ERR(vma))
+                       return PTR_ERR(vma);
+       }
+
+       if (!list_empty(&i915->mm.unbound_list)) {
+               size = 0;
+               list_for_each_entry(obj, &i915->mm.unbound_list, global_link)
+                       size++;
+
+               pr_err("Found %lld objects unbound!\n", size);
+               return -EINVAL;
+       }
+
+       if (list_empty(&i915->ggtt.base.inactive_list)) {
+               pr_err("No objects on the GGTT inactive list!\n");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static void unpin_ggtt(struct drm_i915_private *i915)
+{
+       struct i915_vma *vma;
+
+       list_for_each_entry(vma, &i915->ggtt.base.inactive_list, vm_link)
+               i915_vma_unpin(vma);
+}
+
+static void cleanup_objects(struct drm_i915_private *i915)
+{
+       struct drm_i915_gem_object *obj, *on;
+
+       list_for_each_entry_safe(obj, on, &i915->mm.unbound_list, global_link)
+               i915_gem_object_put(obj);
+
+       list_for_each_entry_safe(obj, on, &i915->mm.bound_list, global_link)
+               i915_gem_object_put(obj);
+
+       mutex_unlock(&i915->drm.struct_mutex);
+
+       i915_gem_drain_freed_objects(i915);
+
+       mutex_lock(&i915->drm.struct_mutex);
+}
+
+static int igt_evict_something(void *arg)
+{
+       struct drm_i915_private *i915 = arg;
+       struct i915_ggtt *ggtt = &i915->ggtt;
+       int err;
+
+       /* Fill the GGTT with pinned objects and try to evict one. */
+
+       err = populate_ggtt(i915);
+       if (err)
+               goto cleanup;
+
+       /* Everything is pinned, nothing should happen */
+       err = i915_gem_evict_something(&ggtt->base,
+                                      I915_GTT_PAGE_SIZE, 0, 0,
+                                      0, U64_MAX,
+                                      0);
+       if (err != -ENOSPC) {
+               pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
+                      err);
+               goto cleanup;
+       }
+
+       unpin_ggtt(i915);
+
+       /* Everything is unpinned, we should be able to evict something */
+       err = i915_gem_evict_something(&ggtt->base,
+                                      I915_GTT_PAGE_SIZE, 0, 0,
+                                      0, U64_MAX,
+                                      0);
+       if (err) {
+               pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
+                      err);
+               goto cleanup;
+       }
+
+cleanup:
+       cleanup_objects(i915);
+       return err;
+}
+
+static int igt_overcommit(void *arg)
+{
+       struct drm_i915_private *i915 = arg;
+       struct drm_i915_gem_object *obj;
+       struct i915_vma *vma;
+       int err;
+
+       /* Fill the GGTT with pinned objects and then try to pin one more.
+        * We expect it to fail.
+        */
+
+       err = populate_ggtt(i915);
+       if (err)
+               goto cleanup;
+
+       obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
+       if (IS_ERR(obj)) {
+               err = PTR_ERR(obj);
+               goto cleanup;
+       }
+
+       list_move(&obj->global_link, &i915->mm.unbound_list);
+
+       vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
+       if (!IS_ERR(vma) || PTR_ERR(vma) != -ENOSPC) {
+               pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR(vma));
+               err = -EINVAL;
+               goto cleanup;
+       }
+
+cleanup:
+       cleanup_objects(i915);
+       return err;
+}
+
+static int igt_evict_for_vma(void *arg)
+{
+       struct drm_i915_private *i915 = arg;
+       struct i915_ggtt *ggtt = &i915->ggtt;
+       struct drm_mm_node target = {
+               .start = 0,
+               .size = 4096,
+       };
+       int err;
+
+       /* Fill the GGTT with pinned objects and try to evict a range. */
+
+       err = populate_ggtt(i915);
+       if (err)
+               goto cleanup;
+
+       /* Everything is pinned, nothing should happen */
+       err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
+       if (err != -ENOSPC) {
+               pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
+                      err);
+               goto cleanup;
+       }
+
+       unpin_ggtt(i915);
+
+       /* Everything is unpinned, we should be able to evict the node */
+       err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
+       if (err) {
+               pr_err("i915_gem_evict_for_node returned err=%d\n",
+                      err);
+               goto cleanup;
+       }
+
+cleanup:
+       cleanup_objects(i915);
+       return err;
+}
+
+static int igt_evict_vm(void *arg)
+{
+       struct drm_i915_private *i915 = arg;
+       struct i915_ggtt *ggtt = &i915->ggtt;
+       int err;
+
+       /* Fill the GGTT with pinned objects and try to evict everything. */
+
+       err = populate_ggtt(i915);
+       if (err)
+               goto cleanup;
+
+       /* Everything is pinned, nothing should happen */
+       err = i915_gem_evict_vm(&ggtt->base, false);
+       if (err) {
+               pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
+                      err);
+               goto cleanup;
+       }
+
+       unpin_ggtt(i915);
+
+       err = i915_gem_evict_vm(&ggtt->base, false);
+       if (err) {
+               pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
+                      err);
+               goto cleanup;
+       }
+
+cleanup:
+       cleanup_objects(i915);
+       return err;
+}
+
+int i915_gem_evict_mock_selftests(void)
+{
+       static const struct i915_subtest tests[] = {
+               SUBTEST(igt_evict_something),
+               SUBTEST(igt_evict_for_vma),
+               SUBTEST(igt_evict_vm),
+               SUBTEST(igt_overcommit),
+       };
+       struct drm_i915_private *i915;
+       int err;
+
+       i915 = mock_gem_device();
+       if (!i915)
+               return -ENOMEM;
+
+       mutex_lock(&i915->drm.struct_mutex);
+       err = i915_subtests(tests, i915);
+       mutex_unlock(&i915->drm.struct_mutex);
+
+       drm_dev_unref(&i915->drm);
+       return err;
+}
index b450eab7e6e1da137e4037681273f91884977a5f..cfbd3f5486ae74ad12706e24b244731a3bdbabca 100644 (file)
@@ -16,3 +16,4 @@ selftest(requests, i915_gem_request_mock_selftests)
 selftest(objects, i915_gem_object_mock_selftests)
 selftest(dmabuf, i915_gem_dmabuf_mock_selftests)
 selftest(vma, i915_vma_mock_selftests)
+selftest(evict, i915_gem_evict_mock_selftests)