A preparatory patch for adding new features (and their tests). First we
want to add coverage of existing features to kselftest.

Signed-off-by: Chris Wilson <ch...@chris-wilson.co.uk>
---
 lib/Kconfig.debug                           |   9 ++
 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, 152 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 df1182d41f06..4b180aed88b6 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1784,6 +1784,15 @@ config KFENCE_CHECK_DAG
          graphs (DAG), as otherwise the cycles in the graph means that they
          will never be signaled (or the corresponding task executed).
 
+config ASYNC_DOMAIN_SELFTEST
+       tristate "Asynchronous domain self tests"
+       depends on DEBUG_KERNEL
+       default n
+       help
+         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
diff --git a/lib/Makefile b/lib/Makefile
index 943781cfe8d1..5864053cf63e 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
 obj-$(CONFIG_KFENCE_SELFTEST) += test-kfence.o
 lib-$(CONFIG_MMU) += ioremap.o
 lib-$(CONFIG_SMP) += cpumask.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

Reply via email to