linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: paulmck@kernel.org
To: linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com,
	kernel-team@fb.com, mingo@kernel.org
Cc: elver@google.com, andreyknvl@google.com, glider@google.com,
	dvyukov@google.com, cai@lca.pw, boqun.feng@gmail.com,
	"Paul E . McKenney" <paulmck@kernel.org>
Subject: [PATCH kcsan 3/4] kcsan: Switch to KUNIT_CASE_PARAM for parameterized tests
Date: Wed,  3 Mar 2021 16:40:39 -0800	[thread overview]
Message-ID: <20210304004040.25074-3-paulmck@kernel.org> (raw)
In-Reply-To: <20210304003750.GA24696@paulmck-ThinkPad-P72>

From: Marco Elver <elver@google.com>

Since KUnit now support parameterized tests via KUNIT_CASE_PARAM, update
KCSAN's test to switch to it for parameterized tests. This simplifies
parameterized tests and gets rid of the "parameters in case name"
workaround (hack).

At the same time, we can increase the maximum number of threads used,
because on systems with too few CPUs, KUnit allows us to now stop at the
maximum useful threads and not unnecessarily execute redundant test
cases with (the same) limited threads as had been the case before.

Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Marco Elver <elver@google.com>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
---
 kernel/kcsan/kcsan_test.c | 116 +++++++++++++++++++++-------------------------
 1 file changed, 54 insertions(+), 62 deletions(-)

diff --git a/kernel/kcsan/kcsan_test.c b/kernel/kcsan/kcsan_test.c
index f16f632..b71751f 100644
--- a/kernel/kcsan/kcsan_test.c
+++ b/kernel/kcsan/kcsan_test.c
@@ -13,6 +13,8 @@
  * Author: Marco Elver <elver@google.com>
  */
 
+#define pr_fmt(fmt) "kcsan_test: " fmt
+
 #include <kunit/test.h>
 #include <linux/jiffies.h>
 #include <linux/kcsan-checks.h>
@@ -951,22 +953,53 @@ static void test_atomic_builtins(struct kunit *test)
 }
 
 /*
- * Each test case is run with different numbers of threads. Until KUnit supports
- * passing arguments for each test case, we encode #threads in the test case
- * name (read by get_num_threads()). [The '-' was chosen as a stylistic
- * preference to separate test name and #threads.]
+ * Generate thread counts for all test cases. Values generated are in interval
+ * [2, 5] followed by exponentially increasing thread counts from 8 to 32.
  *
  * The thread counts are chosen to cover potentially interesting boundaries and
- * corner cases (range 2-5), and then stress the system with larger counts.
+ * corner cases (2 to 5), and then stress the system with larger counts.
  */
-#define KCSAN_KUNIT_CASE(test_name)                                            \
-	{ .run_case = test_name, .name = #test_name "-02" },                   \
-	{ .run_case = test_name, .name = #test_name "-03" },                   \
-	{ .run_case = test_name, .name = #test_name "-04" },                   \
-	{ .run_case = test_name, .name = #test_name "-05" },                   \
-	{ .run_case = test_name, .name = #test_name "-08" },                   \
-	{ .run_case = test_name, .name = #test_name "-16" }
+static const void *nthreads_gen_params(const void *prev, char *desc)
+{
+	long nthreads = (long)prev;
+
+	if (nthreads < 0 || nthreads >= 32)
+		nthreads = 0; /* stop */
+	else if (!nthreads)
+		nthreads = 2; /* initial value */
+	else if (nthreads < 5)
+		nthreads++;
+	else if (nthreads == 5)
+		nthreads = 8;
+	else
+		nthreads *= 2;
 
+	if (!IS_ENABLED(CONFIG_PREEMPT) || !IS_ENABLED(CONFIG_KCSAN_INTERRUPT_WATCHER)) {
+		/*
+		 * Without any preemption, keep 2 CPUs free for other tasks, one
+		 * of which is the main test case function checking for
+		 * completion or failure.
+		 */
+		const long min_unused_cpus = IS_ENABLED(CONFIG_PREEMPT_NONE) ? 2 : 0;
+		const long min_required_cpus = 2 + min_unused_cpus;
+
+		if (num_online_cpus() < min_required_cpus) {
+			pr_err_once("Too few online CPUs (%u < %d) for test\n",
+				    num_online_cpus(), min_required_cpus);
+			nthreads = 0;
+		} else if (nthreads >= num_online_cpus() - min_unused_cpus) {
+			/* Use negative value to indicate last param. */
+			nthreads = -(num_online_cpus() - min_unused_cpus);
+			pr_warn_once("Limiting number of threads to %ld (only %d online CPUs)\n",
+				     -nthreads, num_online_cpus());
+		}
+	}
+
+	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "threads=%ld", abs(nthreads));
+	return (void *)nthreads;
+}
+
+#define KCSAN_KUNIT_CASE(test_name) KUNIT_CASE_PARAM(test_name, nthreads_gen_params)
 static struct kunit_case kcsan_test_cases[] = {
 	KCSAN_KUNIT_CASE(test_basic),
 	KCSAN_KUNIT_CASE(test_concurrent_races),
@@ -996,24 +1029,6 @@ static struct kunit_case kcsan_test_cases[] = {
 
 /* ===== End test cases ===== */
 
-/* Get number of threads encoded in test name. */
-static bool __no_kcsan
-get_num_threads(const char *test, int *nthreads)
-{
-	int len = strlen(test);
-
-	if (WARN_ON(len < 3))
-		return false;
-
-	*nthreads = test[len - 1] - '0';
-	*nthreads += (test[len - 2] - '0') * 10;
-
-	if (WARN_ON(*nthreads < 0))
-		return false;
-
-	return true;
-}
-
 /* Concurrent accesses from interrupts. */
 __no_kcsan
 static void access_thread_timer(struct timer_list *timer)
@@ -1076,9 +1091,6 @@ static int test_init(struct kunit *test)
 	if (!torture_init_begin((char *)test->name, 1))
 		return -EBUSY;
 
-	if (!get_num_threads(test->name, &nthreads))
-		goto err;
-
 	if (WARN_ON(threads))
 		goto err;
 
@@ -1087,38 +1099,18 @@ static int test_init(struct kunit *test)
 			goto err;
 	}
 
-	if (!IS_ENABLED(CONFIG_PREEMPT) || !IS_ENABLED(CONFIG_KCSAN_INTERRUPT_WATCHER)) {
-		/*
-		 * Without any preemption, keep 2 CPUs free for other tasks, one
-		 * of which is the main test case function checking for
-		 * completion or failure.
-		 */
-		const int min_unused_cpus = IS_ENABLED(CONFIG_PREEMPT_NONE) ? 2 : 0;
-		const int min_required_cpus = 2 + min_unused_cpus;
+	nthreads = abs((long)test->param_value);
+	if (WARN_ON(!nthreads))
+		goto err;
 
-		if (num_online_cpus() < min_required_cpus) {
-			pr_err("%s: too few online CPUs (%u < %d) for test",
-			       test->name, num_online_cpus(), min_required_cpus);
-			goto err;
-		} else if (nthreads > num_online_cpus() - min_unused_cpus) {
-			nthreads = num_online_cpus() - min_unused_cpus;
-			pr_warn("%s: limiting number of threads to %d\n",
-				test->name, nthreads);
-		}
-	}
+	threads = kcalloc(nthreads + 1, sizeof(struct task_struct *), GFP_KERNEL);
+	if (WARN_ON(!threads))
+		goto err;
 
-	if (nthreads) {
-		threads = kcalloc(nthreads + 1, sizeof(struct task_struct *),
-				  GFP_KERNEL);
-		if (WARN_ON(!threads))
+	threads[nthreads] = NULL;
+	for (i = 0; i < nthreads; ++i) {
+		if (torture_create_kthread(access_thread, NULL, threads[i]))
 			goto err;
-
-		threads[nthreads] = NULL;
-		for (i = 0; i < nthreads; ++i) {
-			if (torture_create_kthread(access_thread, NULL,
-						   threads[i]))
-				goto err;
-		}
 	}
 
 	torture_init_end();
-- 
2.9.5


  parent reply	other threads:[~2021-03-04  1:09 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-04  0:37 [PATCH tip/core/rcu 0/28] Torture-test scripting updates for v5.13 Paul E. McKenney
2021-03-04  0:37 ` [PATCH tip/core/rcu 01/28] torturescript: Don't rerun failed rcutorture builds paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 02/28] torture: Allow 1G of memory for torture.sh kvfree testing paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 03/28] torture: Provide bare-metal modprobe-based advice paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 04/28] torture: Improve readability of the testid.txt file paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 05/28] rcuscale: Disable verbose torture-test output paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 06/28] refscale: " paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 07/28] torture: Move build/run synchronization files into scenario directories paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 08/28] torture: Use file-based protocol to mark batch's runs complete paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 09/28] torture: Use "jittering" file to control jitter.sh execution paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 10/28] torture: Eliminate jitter_pids file paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 11/28] torture: Reverse jittering and duration parameters for jitter.sh paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 12/28] torture: Abstract jitter.sh start/stop into scripts paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 13/28] torture: Record TORTURE_KCONFIG_GDB_ARG in qemu-cmd paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 14/28] torture: Extract kvm-test-1-run-qemu.sh from kvm-test-1-run.sh paulmck
2021-03-04  0:37 ` [PATCH tip/core/rcu 15/28] torture: Record jitter start/stop commands paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 16/28] torture: Record kvm-test-1-run.sh and kvm-test-1-run-qemu.sh PIDs paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 17/28] torture: Remove no-mpstat error message paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 18/28] torture: Rename SRCU-t and SRCU-u to avoid lowercase characters paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 19/28] torture: Make upper-case-only no-dot no-slash scenario names official paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 20/28] torture: De-capitalize TORTURE_SUITE paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 21/28] torture: Create a "batches" file for build reuse paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 22/28] torture: Add kvm-again.sh to rerun a previous torture-test paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 23/28] torture: Add --duration argument to kvm-again.sh paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 24/28] torture: Make kvm-transform.sh update jitter commands paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 25/28] torture: Make TORTURE_TRUST_MAKE available in kvm-again.sh environment paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 26/28] torture: Print proper vmlinux path for kvm-again.sh runs paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 27/28] torture: Consolidate qemu-cmd duration editing into kvm-transform.sh paulmck
2021-03-04  0:38 ` [PATCH tip/core/rcu 28/28] torture: Fix kvm.sh --datestamp regex check paulmck
2021-03-04  0:40 ` [PATCH kcsan 1/4] kcsan, debugfs: Move debugfs file creation out of early init paulmck
2021-03-04  0:40 ` [PATCH kcsan 2/4] kcsan: Make test follow KUnit style recommendations paulmck
2021-03-04  0:40 ` paulmck [this message]
2021-03-04  0:40 ` [PATCH kcsan 4/4] kcsan: Add missing license and copyright headers paulmck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210304004040.25074-3-paulmck@kernel.org \
    --to=paulmck@kernel.org \
    --cc=andreyknvl@google.com \
    --cc=boqun.feng@gmail.com \
    --cc=cai@lca.pw \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=glider@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).