[PATCH 1/9] lib: Add kselftests for async-domains

From: Chris Wilson
Date: Fri Jun 24 2016 - 05:14:17 EST


---
lib/Kconfig.debug | 12 +++
lib/Makefile | 1 +
lib/test-async-domain.c | 131 ++++++++++++++++++++++++++++
tools/testing/selftests/lib/Makefile | 2 +-
tools/testing/selftests/lib/async-domain.sh | 10 +++
5 files changed, 155 insertions(+), 1 deletion(-)
create mode 100644 lib/test-async-domain.c
create mode 100755 tools/testing/selftests/lib/async-domain.sh

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index b9cfdbfae9aa..f7b17daf8e4d 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1763,6 +1763,18 @@ config KPROBES_SANITY_TEST

Say N if you are unsure.

+config ASYNC_DOMAIN_SELFTEST
+ tristate "Asynchronous domain self tests"
+ depends on DEBUG_KERNEL
+ default n
+ help
+ This option provides a kernel modules that can be used to test
+ the asynchronous task execution. This option is not useful for
+ distributions or general kernels, but only for kernel developers
+ working on the async_domain facility.
+
+ Say N if you are unsure.
+
config BACKTRACE_SELF_TEST
tristate "Self test for the backtrace code"
depends on DEBUG_KERNEL
diff --git a/lib/Makefile b/lib/Makefile
index ff6a7a6c6395..23cdb9885e45 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -28,6 +28,7 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \
earlycpio.o seq_buf.o nmi_backtrace.o nodemask.o

obj-$(CONFIG_ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS) += usercopy.o
+obj-$(CONFIG_ASYNC_DOMAIN_SELFTEST) += test-async-domain.o
lib-$(CONFIG_MMU) += ioremap.o
lib-$(CONFIG_SMP) += cpumask.o
lib-$(CONFIG_HAS_DMA) += dma-noop.o
diff --git a/lib/test-async-domain.c b/lib/test-async-domain.c
new file mode 100644
index 000000000000..558a71414fb6
--- /dev/null
+++ b/lib/test-async-domain.c
@@ -0,0 +1,131 @@
+/*
+ * Test cases for async-domain facility.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/async.h>
+#include <linux/module.h>
+#include <linux/delay.h>
+
+static void task_A(void *data, async_cookie_t cookie)
+{
+ long *result = data;
+ smp_store_mb(*result, 'A');
+}
+
+static void task_B(void *data, async_cookie_t cookie)
+{
+ long *result = data;
+ usleep_range(100, 200);
+ smp_store_mb(*result, 'B');
+}
+
+static int __init test_implicit(struct async_domain *domain)
+{
+ const long expected = 'B';
+ long result = 0;
+
+ if (!async_schedule_domain(task_B, &result, domain))
+ return -ENOMEM;
+
+ async_synchronize_full_domain(domain);
+
+ if (READ_ONCE(result) != expected) {
+ pr_warn("%s expected %c [%ld], got %ld\n",
+ __func__, (char)expected, expected, result);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int __init test_registered(struct async_domain *domain)
+{
+ const long expected = 'B';
+ long result = 0;
+
+ if (!async_schedule_domain(task_B, &result, domain))
+ return -ENOMEM;
+
+ async_synchronize_full();
+
+ if (READ_ONCE(result) != expected) {
+ pr_warn("%s expected %c [%ld], got %ld\n",
+ __func__, (char)expected, expected, result);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static void task_nop(void *data, async_cookie_t cookie)
+{
+ async_cookie_t *result = data;
+ smp_store_mb(*result, cookie);
+}
+
+static int __init perf_nop(int batch, long timeout_us)
+{
+ ktime_t start;
+ async_cookie_t nop, last;
+ long count, delay;
+
+ count = 0;
+ nop = last = 0;
+ start = ktime_get();
+ do {
+ ktime_t delta;
+ int n;
+
+ for (n = 0; n < batch; n++)
+ last = async_schedule(task_nop, &nop);
+ async_synchronize_full();
+ delta = ktime_sub(ktime_get(), start);
+ delay = ktime_to_ns(delta) >> 10;
+ count += batch;
+ } while (delay < timeout_us);
+
+ pr_info("%ld nop tasks (batches of %d) completed in %ldus; last queued %lld, saw %lld last\n",
+ count, batch, delay,
+ (long long)last, (long long)READ_ONCE(nop));
+ return 0;
+}
+
+static int __init test_async_domain_init(void)
+{
+ ASYNC_DOMAIN(domain);
+ int ret;
+
+ pr_info("Testing async-domains\n");
+
+ ret = test_implicit(&domain);
+ if (ret)
+ return ret;
+
+ ret = test_registered(&domain);
+ if (ret)
+ return ret;
+
+ ret = perf_nop(1, 100);
+ if (ret)
+ return ret;
+
+ ret = perf_nop(128, 1000);
+ if (ret)
+ return ret;
+
+ async_unregister_domain(&domain);
+ return 0;
+}
+
+static void __exit test_async_domain_cleanup(void)
+{
+ async_synchronize_full();
+}
+
+module_init(test_async_domain_init);
+module_exit(test_async_domain_cleanup);
+
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL");
diff --git a/tools/testing/selftests/lib/Makefile b/tools/testing/selftests/lib/Makefile
index 08360060ab14..46a77ac5b4c6 100644
--- a/tools/testing/selftests/lib/Makefile
+++ b/tools/testing/selftests/lib/Makefile
@@ -3,6 +3,6 @@
# No binaries, but make sure arg-less "make" doesn't trigger "run_tests"
all:

-TEST_PROGS := printf.sh bitmap.sh
+TEST_PROGS := printf.sh bitmap.sh async-domain.sh

include ../lib.mk
diff --git a/tools/testing/selftests/lib/async-domain.sh b/tools/testing/selftests/lib/async-domain.sh
new file mode 100755
index 000000000000..22c270051de7
--- /dev/null
+++ b/tools/testing/selftests/lib/async-domain.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+# Runs infrastructure tests using test-async-domain kernel module
+
+if /sbin/modprobe -q test-async-domain; then
+ /sbin/modprobe -q -r test-async-domain
+ echo "async-domain: ok"
+else
+ echo "async-domain: [FAIL]"
+ exit 1
+fi
--
2.8.1