[PATCH 07/31] drm: kselftest for drm_mm_insert_node()
Chris Wilson
chris at chris-wilson.co.uk
Mon Dec 12 08:41:37 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/Kconfig | 5 +
drivers/gpu/drm/Makefile | 1 +
drivers/gpu/drm/lib/primes.c | 174 ++++++++++++++++++++++
drivers/gpu/drm/lib/primes.h | 10 ++
drivers/gpu/drm/selftests/drm_mm_selftests.h | 1 +
drivers/gpu/drm/selftests/test-drm_mm.c | 211 +++++++++++++++++++++++++++
6 files changed, 402 insertions(+)
create mode 100644 drivers/gpu/drm/lib/primes.c
create mode 100644 drivers/gpu/drm/lib/primes.h
diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
index 04d1d0a32c5c..09f076896844 100644
--- a/drivers/gpu/drm/Kconfig
+++ b/drivers/gpu/drm/Kconfig
@@ -52,11 +52,16 @@ config DRM_LIB_RAND
bool
default n
+config DRM_LIB_PRIMES
+ bool
+ default n
+
config DRM_DEBUG_MM_SELFTEST
tristate "kselftests for DRM range manager (struct drm_mm)"
depends on DRM
depends on DEBUG_KERNEL
select DRM_LIB_RAND
+ select DRM_LIB_PRIMES
default n
help
This option provides a kernel module that can be used to test
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index 13f5ef896934..75f12919431e 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -19,6 +19,7 @@ drm-y := drm_auth.o drm_bufs.o drm_cache.o \
drm_dumb_buffers.o drm_mode_config.o
drm-$(CONFIG_DRM_LIB_RAND) += lib/rand.o
+obj-$(CONFIG_DRM_LIB_PRIMES) += lib/primes.o
obj-$(CONFIG_DRM_DEBUG_MM_SELFTEST) += selftests/test-drm_mm.o
drm-$(CONFIG_COMPAT) += drm_ioc32.o
diff --git a/drivers/gpu/drm/lib/primes.c b/drivers/gpu/drm/lib/primes.c
new file mode 100644
index 000000000000..3a53ff09f1cd
--- /dev/null
+++ b/drivers/gpu/drm/lib/primes.c
@@ -0,0 +1,174 @@
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+
+#include "primes.h"
+
+static DEFINE_MUTEX(lock);
+
+static struct primes {
+ struct rcu_head rcu;
+ unsigned long last, sz;
+ unsigned long primes[];
+} __rcu *primes;
+
+static bool slow_is_prime_number(unsigned long x)
+{
+ unsigned long y = int_sqrt(x) + 1;
+
+ while (y > 1) {
+ if ((x % y) == 0)
+ break;
+ y--;
+ }
+
+ return y == 1;
+}
+
+static unsigned long slow_next_prime_number(unsigned long x)
+{
+ for (;;) {
+ if (slow_is_prime_number(++x))
+ 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 struct primes *expand(unsigned long x)
+{
+ unsigned long sz, y, prev;
+ struct primes *p, *new;
+
+ sz = x * x;
+ if (sz < x)
+ return NULL;
+
+ mutex_lock(&lock);
+ p = rcu_dereference_protected(primes, lockdep_is_held(&lock));
+ if (p && x < p->last)
+ goto unlock;
+
+ sz = round_up(sz, BITS_PER_LONG);
+ new = kmalloc(sizeof(*new) + sz / sizeof(long), GFP_KERNEL);
+ if (!new) {
+ p = NULL;
+ goto unlock;
+ }
+
+ /* Where memory permits, track the primes using the
+ * Sieve of Eratosthenes.
+ */
+ if (p) {
+ prev = p->sz;
+ memcpy(new->primes, p->primes, prev / BITS_PER_LONG);
+ } else {
+ prev = 0;
+ }
+ memset(new->primes + prev / BITS_PER_LONG,
+ 0xff, (sz - prev) / sizeof(long));
+ for (y = 2UL; y < sz; y = find_next_bit(new->primes, sz, y + 1))
+ new->last = mark_multiples(y, new->primes, prev, sz);
+ new->sz = sz;
+
+ rcu_assign_pointer(primes, new);
+ if (p)
+ kfree_rcu(p, rcu);
+ p = new;
+
+unlock:
+ mutex_unlock(&lock);
+ return p;
+}
+
+unsigned long next_prime_number(unsigned long x)
+{
+ struct primes *p;
+
+ if (x < 2)
+ return 2;
+
+ rcu_read_lock();
+ p = rcu_dereference(primes);
+ if (!p || x >= p->last) {
+ rcu_read_unlock();
+
+ p = expand(x);
+ if (!p)
+ return slow_next_prime_number(x);
+
+ rcu_read_lock();
+ }
+
+ x = find_next_bit(p->primes, p->last, x + 1);
+ rcu_read_unlock();
+
+ return x;
+}
+EXPORT_SYMBOL(next_prime_number);
+
+bool is_prime_number(unsigned long x)
+{
+ struct primes *p;
+ bool result;
+
+ switch (x) {
+ case 0:
+ return false;
+ case 1:
+ case 2:
+ case 3:
+ return true;
+ }
+
+ rcu_read_lock();
+ p = rcu_dereference(primes);
+ if (!p || x >= p->last) {
+ rcu_read_unlock();
+
+ p = expand(x);
+ if (!p)
+ return slow_is_prime_number(x);
+
+ rcu_read_lock();
+ }
+
+ result = test_bit(x, p->primes);
+ rcu_read_unlock();
+
+ return result;
+}
+EXPORT_SYMBOL(is_prime_number);
+
+static int __init primes_init(void)
+{
+ return 0;
+}
+
+static void __exit primes_exit(void)
+{
+ kfree(primes);
+}
+
+module_init(primes_init);
+module_exit(primes_exit);
+
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL");
diff --git a/drivers/gpu/drm/lib/primes.h b/drivers/gpu/drm/lib/primes.h
new file mode 100644
index 000000000000..141363e644bf
--- /dev/null
+++ b/drivers/gpu/drm/lib/primes.h
@@ -0,0 +1,10 @@
+#ifndef __PRIMES_H__
+#define __PRIMES_H__
+
+unsigned long next_prime_number(unsigned long x);
+void free_primes(void);
+
+#define for_each_prime(prime, max) \
+ for (prime = 1; prime < (max); prime = next_prime_number(prime))
+
+#endif /* __PRIMES_H__ */
diff --git a/drivers/gpu/drm/selftests/drm_mm_selftests.h b/drivers/gpu/drm/selftests/drm_mm_selftests.h
index 639913a69101..0927695c3fd4 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(insert, igt_insert)
selftest(reserve, igt_reserve)
selftest(debug, igt_debug)
selftest(init, igt_init)
diff --git a/drivers/gpu/drm/selftests/test-drm_mm.c b/drivers/gpu/drm/selftests/test-drm_mm.c
index 32024d152b92..218fb75ae5e6 100644
--- a/drivers/gpu/drm/selftests/test-drm_mm.c
+++ b/drivers/gpu/drm/selftests/test-drm_mm.c
@@ -16,6 +16,8 @@
#define TESTS "drm_mm_selftests.h"
#include "drm_selftest.h"
+#include "../lib/primes.h"
+
static unsigned int random_seed = 0x12345678;
static int igt_sanitycheck(void *ignored)
@@ -236,6 +238,215 @@ static int igt_reserve(void *ignored)
return 0;
}
+static int __igt_insert(int count, u64 size)
+{
+ u32 lcg_state = random_seed;
+ 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, &lcg_state);
+ 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;
+
+ node = &nodes[n];
+ 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, nodes[n].start);
+ ret = err;
+ goto out;
+ }
+
+ if (!drm_mm_node_allocated(node)) {
+ pr_err("inserted node not allocated! step %d, start %llu\n",
+ n, node->start);
+ 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:
+ 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;
+}
+
+static int igt_insert(void *ignored)
+{
+ const unsigned int count = BIT(10);
+ int n, ret;
+
+ for_each_prime(n, 53) {
+ u64 size = BIT_ULL(n);
+
+ ret = __igt_insert(count, size - 1);
+ if (ret)
+ return ret;
+
+ ret = __igt_insert(count, size);
+ if (ret)
+ return ret;
+
+ ret = __igt_insert(count, size + 1);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
#include "drm_selftest.c"
static int __init test_drm_mm_init(void)
--
2.11.0
More information about the Intel-gfx-trybot
mailing list