Check that we add arbitrary blocks to a restrited eviction scanner in
order to find the first minimal hole that matches our request.

Signed-off-by: Chris Wilson <chris at chris-wilson.co.uk>
---
 drivers/gpu/drm/selftests/drm_mm_selftests.h |   1 +
 drivers/gpu/drm/selftests/test-drm_mm.c      | 189 +++++++++++++++++++++++++++
 2 files changed, 190 insertions(+)

diff --git a/drivers/gpu/drm/selftests/drm_mm_selftests.h 
b/drivers/gpu/drm/selftests/drm_mm_selftests.h
index 7aaca10e7029..0b60b1da39a1 100644
--- a/drivers/gpu/drm/selftests/drm_mm_selftests.h
+++ b/drivers/gpu/drm/selftests/drm_mm_selftests.h
@@ -5,6 +5,7 @@
  *
  * Tests are executed in reverse order by igt/drm_mm
  */
+selftest(evict_range, igt_evict_range)
 selftest(evict, igt_evict)
 selftest(align64, igt_align64)
 selftest(align32, igt_align32)
diff --git a/drivers/gpu/drm/selftests/test-drm_mm.c 
b/drivers/gpu/drm/selftests/test-drm_mm.c
index f9d6a25a8006..48e576531ead 100644
--- a/drivers/gpu/drm/selftests/test-drm_mm.c
+++ b/drivers/gpu/drm/selftests/test-drm_mm.c
@@ -1025,6 +1025,195 @@ static int igt_evict(void *ignored)
        return ret;
 }

+static int igt_evict_range(void *ignored)
+{
+       u32 lcg_state = random_seed;
+       const int size = 8192;
+       const int range_size = size / 2;
+       const int range_start = size / 4;
+       const int range_end = range_start + range_size;
+       struct drm_mm mm;
+       struct evict_node {
+               struct drm_mm_node node;
+               struct list_head link;
+       } *nodes;
+       struct drm_mm_node *node, *next;
+       int *order, n, m;
+       int ret;
+
+       ret = -ENOMEM;
+       nodes = vzalloc(size * sizeof(*nodes));
+       if (!nodes)
+               goto err;
+
+       order = drm_random_order(size, &lcg_state);
+       if (!order)
+               goto err_nodes;
+
+       ret = -EINVAL;
+       drm_mm_init(&mm, 0, size);
+       for (n = 0; n < size; n++) {
+               int err;
+
+               err = drm_mm_insert_node(&mm, &nodes[n].node, 1, 0,
+                                        DRM_MM_SEARCH_DEFAULT);
+               if (err) {
+                       pr_err("insert failed, step %d\n", n);
+                       ret = err;
+                       goto out;
+               }
+       }
+
+       for (n = 1; n < size; n <<= 1) {
+               const int nsize = range_size / 2;
+               LIST_HEAD(evict_list);
+               struct evict_node *e, *en;
+               struct drm_mm_node tmp;
+               int err;
+
+               drm_mm_init_scan_with_range(&mm, nsize, n, 0,
+                                           range_start, range_end);
+               drm_random_reorder(order, size, &lcg_state);
+               for (m = 0; m < size; m++) {
+                       e = &nodes[order[m]];
+                       list_add(&e->link, &evict_list);
+                       if (drm_mm_scan_add_block(&e->node))
+                               break;
+               }
+               list_for_each_entry_safe(e, en, &evict_list, link) {
+                       if (!drm_mm_scan_remove_block(&e->node))
+                               list_del(&e->link);
+               }
+               if (list_empty(&evict_list)) {
+                       pr_err("Failed to find eviction: size=%d, align=%d\n",
+                              nsize, n);
+                       goto out;
+               }
+
+               list_for_each_entry(e, &evict_list, link)
+                       drm_mm_remove_node(&e->node);
+
+               memset(&tmp, 0, sizeof(tmp));
+               err = drm_mm_insert_node_in_range(&mm, &tmp, nsize, n,
+                                                 range_start, range_end,
+                                                 DRM_MM_SEARCH_DEFAULT);
+               if (err) {
+                       pr_err("Failed to insert into eviction hole: size=%d, 
align=%d\n",
+                              nsize, n);
+                       show_scan(&mm);
+                       show_holes(&mm, 3);
+                       ret = err;
+                       goto out;
+               }
+
+               if (tmp.start < range_start || tmp.start + tmp.size > 
range_end) {
+                       pr_err("Inserted node did not fit into target range, 
start=%llx+%llx range=[%d, %d]\n",
+                              tmp.start, tmp.size, range_start, range_end);
+                       drm_mm_remove_node(&tmp);
+                       goto out;
+               }
+
+               if ((int)tmp.start % n || tmp.size != nsize || 
tmp.hole_follows) {
+                       pr_err("Inserted did not fill the eviction hole: 
size=%lld [%d], align=%d [rem=%d], start=%llx, hole-follows?=%d\n",
+                              tmp.size, nsize, n, (int)tmp.start % n, 
tmp.start, tmp.hole_follows);
+
+                       drm_mm_remove_node(&tmp);
+                       goto out;
+               }
+
+               drm_mm_remove_node(&tmp);
+               list_for_each_entry(e, &evict_list, link) {
+                       err = drm_mm_reserve_node(&mm, &e->node);
+                       if (err) {
+                               pr_err("Failed to reinsert node after eviction: 
start=%llx\n",
+                                      e->node.start);
+                               ret = err;
+                               goto out;
+                       }
+               }
+       }
+
+       drm_for_each_prime(n, range_size) {
+               LIST_HEAD(evict_list);
+               struct evict_node *e, *en;
+               struct drm_mm_node tmp;
+               int nsize = (range_size - n + 1) / 2;
+               int err;
+
+               drm_mm_init_scan_with_range(&mm, nsize, n, 0,
+                                           range_start, range_end);
+               drm_random_reorder(order, size, &lcg_state);
+               for (m = 0; m < size; m++) {
+                       e = &nodes[order[m]];
+                       list_add(&e->link, &evict_list);
+                       if (drm_mm_scan_add_block(&e->node))
+                               break;
+               }
+               list_for_each_entry_safe(e, en, &evict_list, link) {
+                       if (!drm_mm_scan_remove_block(&e->node))
+                               list_del(&e->link);
+               }
+               if (list_empty(&evict_list)) {
+                       pr_err("Failed to find eviction: size=%d, align=%d 
(prime)\n",
+                              nsize, n);
+                       goto out;
+               }
+
+               list_for_each_entry(e, &evict_list, link)
+                       drm_mm_remove_node(&e->node);
+
+               memset(&tmp, 0, sizeof(tmp));
+               err = drm_mm_insert_node_in_range(&mm, &tmp, nsize, n,
+                                                 range_start, range_end,
+                                                 DRM_MM_SEARCH_DEFAULT);
+               if (err) {
+                       pr_err("Failed to insert into eviction hole: size=%d, 
align=%d (prime)\n",
+                              nsize, n);
+                       show_scan(&mm);
+                       show_holes(&mm, 3);
+                       ret = err;
+                       goto out;
+               }
+
+               if (tmp.start < range_start || tmp.start + tmp.size > 
range_end) {
+                       pr_err("Inserted node did not fit into target range, 
start=%llx+%llx range=[%d, %d]\n",
+                              tmp.start, tmp.size, range_start, range_end);
+                       drm_mm_remove_node(&tmp);
+                       goto out;
+               }
+
+               if ((int)tmp.start % n || tmp.size != nsize || 
tmp.hole_follows) {
+                       pr_err("Inserted did not fill the eviction hole: 
size=%lld [%d], align=%d [rem=%d] (prime), start=%llx, hole-follows?=%d\n",
+                              tmp.size, nsize, n, (int)tmp.start % n, 
tmp.start, tmp.hole_follows);
+
+                       drm_mm_remove_node(&tmp);
+                       goto out;
+               }
+
+               drm_mm_remove_node(&tmp);
+               list_for_each_entry(e, &evict_list, link) {
+                       err = drm_mm_reserve_node(&mm, &e->node);
+                       if (err) {
+                               pr_err("Failed to reinsert node after eviction: 
start=%llx\n",
+                                      e->node.start);
+                               ret = err;
+                               goto out;
+                       }
+               }
+       }
+
+       ret = 0;
+out:
+       drm_mm_for_each_node_safe(node, next, &mm)
+               drm_mm_remove_node(node);
+       drm_mm_takedown(&mm);
+       kfree(order);
+err_nodes:
+       vfree(nodes);
+err:
+       return ret;
+}
+
 #include "drm_selftest.c"

 static int __init test_drm_mm_init(void)
-- 
2.11.0

Reply via email to