[PATCH 3/9] drm: kselftest for drm_mm_insert_node()

Chris Wilson chris at chris-wilson.co.uk
Fri Dec 9 13:08:19 UTC 2016


Exercise drm_mm_insert_node(), check that we can't overfill a range and
that the lists are correct after reserving/removing.

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

diff --git a/drivers/gpu/drm/drm_mm_selftests.h b/drivers/gpu/drm/drm_mm_selftests.h
index 204200227b3c..97690bf5add3 100644
--- a/drivers/gpu/drm/drm_mm_selftests.h
+++ b/drivers/gpu/drm/drm_mm_selftests.h
@@ -5,6 +5,7 @@
  *
  * Tests are executed in reverse order by igt/drm_mm
  */
+selftest(insert, igt_insert)
 selftest(reserve, igt_reserve)
 selftest(init, igt_init)
 selftest(sanitycheck, igt_sanitycheck) /* keep last */
diff --git a/drivers/gpu/drm/test-drm_mm.c b/drivers/gpu/drm/test-drm_mm.c
index d7ab054678a8..679f7e703218 100644
--- a/drivers/gpu/drm/test-drm_mm.c
+++ b/drivers/gpu/drm/test-drm_mm.c
@@ -14,6 +14,78 @@
 #define TESTS "drm_mm_selftests.h"
 #include "drm_selftest.h"
 
+static unsigned long *primes;
+static unsigned long prime_last, prime_sz;
+
+static unsigned long slow_next_prime_number(unsigned long x)
+{
+	for (;;) {
+		unsigned long y = int_sqrt(++x) + 1;
+		while (y > 1) {
+			if ((x % y) == 0)
+				break;
+			y--;
+		}
+		if (y == 1)
+			return x;
+	}
+}
+
+static unsigned long mark_multiples(unsigned long x,
+				    unsigned long *p,
+				    unsigned long start,
+				    unsigned long end)
+{
+	unsigned long m;
+
+	m = 2*x;
+	if (m < start)
+		m = (start / x + 1) * x;
+
+	while (m < end) {
+		__clear_bit(m, p);
+		m += x;
+	}
+
+	return x;
+}
+
+static unsigned long next_prime_number(unsigned long x)
+{
+	if (x == 1)
+		return 2;
+
+	if (x >= prime_last) {
+		unsigned long sz, y;
+		unsigned long *nprimes;
+
+		sz = x*x;
+		if (sz < x)
+			return slow_next_prime_number(x);
+
+		sz = round_up(sz, BITS_PER_LONG);
+		nprimes = krealloc(primes, sz / sizeof(long), GFP_KERNEL);
+		if (!nprimes)
+			return slow_next_prime_number(x);
+
+		/* Where memory permits, track the primes using the
+		 * Sieve of Eratosthenes.
+		 */
+		memset(nprimes + prime_sz / BITS_PER_LONG,
+		       0xff, (sz - prime_sz) / sizeof(long));
+		for (y = 2UL; y < sz; y = find_next_bit(nprimes, sz, y + 1))
+			prime_last = mark_multiples(y, nprimes, prime_sz, sz);
+
+		primes = nprimes;
+		prime_sz = sz;
+	}
+
+	return find_next_bit(primes, prime_last, x + 1);
+}
+
+#define for_each_prime(prime, max) \
+	for (prime = 1;	prime < (max); prime = next_prime_number(prime))
+
 static int igt_sanitycheck(void *ignored)
 {
 	pr_info("%s - ok!\n", __func__);
@@ -204,6 +276,204 @@ static int igt_reserve(void *ignored)
 	return 0;
 }
 
+static int __igt_insert(int count, u64 size)
+{
+	struct drm_mm mm;
+	struct drm_mm_node *nodes, *node, *next;
+	int *order, n, o = 0;
+	int ret;
+
+	/* Fill a range with lots of nodes, check it doesn't fail too early */
+
+	ret = -ENOMEM;
+	nodes = vzalloc(count * sizeof(*nodes));
+	if (!nodes)
+		goto err;
+
+	order = random_order(count);
+	if (!order)
+		goto err_nodes;
+
+	ret = -EINVAL;
+	drm_mm_init(&mm, 0, count * size);
+	if (!drm_mm_clean(&mm)) {
+		pr_err("mm not empty on creation\n");
+		goto out;
+	}
+
+	for (n = 0; n < count; n++) {
+		int err;
+
+		err = drm_mm_insert_node(&mm, &nodes[n], size, 0,
+					 DRM_MM_SEARCH_DEFAULT);
+		if (err) {
+			pr_err("insert failed, step %d, start %llu\n",
+			       n, nodes[n].start);
+			ret = err;
+			goto out;
+		}
+	}
+
+	/* Repeated use should then fail */
+	if (1) {
+		struct drm_mm_node tmp;
+
+		memset(&tmp, 0, sizeof(tmp));
+		if (!drm_mm_insert_node(&mm, &tmp, size, 0,
+					DRM_MM_SEARCH_DEFAULT)) {
+			drm_mm_remove_node(&tmp);
+			pr_err("impossible insert succeeded, step %d, start %llu\n",
+			       n, tmp.start);
+			goto out;
+		}
+	}
+
+	n = 0;
+	drm_mm_for_each_node(node, &mm) {
+		if (node->start != n * size) {
+			pr_err("node %d out of order, expected start %llx, found %llx\n",
+			       n, n * size, node->start);
+			goto out;
+		}
+
+		if (node->size != size) {
+			pr_err("node %d has wrong size, expected size %llx, found %llx\n",
+			       n, size, node->size);
+			goto out;
+		}
+
+		if (node->hole_follows) {
+			pr_err("node %d is followed by a hole!\n", n);
+			goto out;
+		}
+
+		n++;
+	}
+
+	for (n = 0; n < count; n++) {
+		drm_mm_for_each_node_in_range(node, &mm, n * size, (n + 1) * size) {
+			if (node->start != n * size) {
+				pr_err("lookup node %d out of order, expected start %llx, found %llx\n",
+				       n, n * size, node->start);
+				goto out;
+			}
+		}
+	}
+
+	/* Remove one and reinsert, as the only hole it should refill itself */
+	for (n = 0; n < count; n++) {
+		int err;
+
+		drm_mm_remove_node(&nodes[n]);
+		err = drm_mm_insert_node(&mm, &nodes[n], size, 0,
+					 DRM_MM_SEARCH_DEFAULT);
+		if (err) {
+			pr_err("reinsert failed, step %d\n", n);
+			ret = err;
+			goto out;
+		}
+
+		if (nodes[n].start != n * size) {
+			pr_err("reinsert node moved, step %d, expected %llx, found %llx\n",
+			       n, n * size, nodes[n].start);
+			goto out;
+		}
+	}
+
+	/* Remove several, reinsert, check full */
+	for_each_prime(n, min(128, count)) {
+		int m;
+
+		for (m = 0; m < n; m++) {
+			node = &nodes[order[(o + m) % count]];
+			drm_mm_remove_node(node);
+		}
+
+		for (m = 0; m < n; m++) {
+			int err;
+
+			node = &nodes[order[(o + m) % count]];
+			err = drm_mm_insert_node(&mm, node, size, 0,
+						 DRM_MM_SEARCH_DEFAULT);
+			if (err) {
+				pr_err("insert failed, step %d, start %llu\n",
+				       n, node->start);
+				ret = err;
+				goto out;
+			}
+		}
+
+		o += n;
+
+		if (1) {
+			struct drm_mm_node tmp;
+
+			memset(&tmp, 0, sizeof(tmp));
+			if (!drm_mm_insert_node(&mm, &tmp, size, 0,
+						DRM_MM_SEARCH_DEFAULT)) {
+				drm_mm_remove_node(&tmp);
+				pr_err("impossible insert succeeded, start %llu\n",
+				       tmp.start);
+				goto out;
+			}
+		}
+
+		m = 0;
+		drm_mm_for_each_node(node, &mm) {
+			if (node->start != m * size) {
+				pr_err("node %d out of order, expected start %llx, found %llx\n",
+				       m, m * size, node->start);
+				goto out;
+			}
+
+			if (node->size != size) {
+				pr_err("node %d has wrong size, expected size %llx, found %llx\n",
+				       m, size, node->size);
+				goto out;
+			}
+
+			if (node->hole_follows) {
+				pr_err("node %d is followed by a hole!\n", m);
+				goto out;
+			}
+
+			m++;
+		}
+	}
+
+	ret = 0;
+out:
+	list_for_each_entry_safe(node, next, &mm.head_node.node_list, node_list)
+		drm_mm_remove_node(node);
+	drm_mm_takedown(&mm);
+	kfree(order);
+err_nodes:
+	vfree(nodes);
+err:
+	return ret;
+}
+
+static int igt_insert(void *ignored)
+{
+	int n, ret;
+
+	for (n = 1; n < 50; n++) {
+		ret = __igt_insert(8192, (1ull << n) - 1);
+		if (ret)
+			return ret;
+
+		ret = __igt_insert(8192, 1ull << n);
+		if (ret)
+			return ret;
+
+		ret = __igt_insert(8192, (1ull << n) + 1);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 #include "drm_selftest.c"
 
 static int __init test_drm_mm_init(void)
@@ -218,6 +488,7 @@ static int __init test_drm_mm_init(void)
 
 static void __exit test_drm_mm_exit(void)
 {
+	kfree(primes);
 }
 
 module_init(test_drm_mm_init);
-- 
2.11.0



More information about the dri-devel mailing list