From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: tglx@kernel.org, peterz@infradead.org
Cc: linux-kernel@vger.kernel.org,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
mingo@redhat.com, juri.lelli@redhat.com,
vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
vschneid@redhat.com, kprateek.nayak@amd.com, paulmck@kernel.org,
boqun@kernel.org, shuah@kernel.org, kees@kernel.org,
viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz,
oleg@redhat.com, bestswngs@gmail.com, xmei5@asu.edu,
xujiakai24@mails.ucas.ac.cn, linux-mm@kvack.org,
linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: [PATCH 14/17] selftests/rseq: Test the concurrency IDs after the affinity is widened
Date: Tue, 17 Nov 2026 11:17:14 -0500 [thread overview]
Message-ID: <05781fc46cb63b548588e58b356ab08b10619aac.1794932121.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1794932121.git.mathieu.desnoyers@efficios.com>
When the allowed CPUs of an mm grow, the kernel can give the
concurrency IDs back to the tasks. Commit 7dbc307b6c72 ("sched/mmcid:
Fix affinity-triggered switch back to per-task mode") fixed a threshold
that kept the CPUs as owners. No test shows it: the tests that widen
the affinity only check that the IDs are unique and in range.
Add a case to mm_cid_bound_test. Three threads start on two CPUs, so
the CPUs own the IDs. The threads are then allowed on all CPUs and
visit each of them. The kernel aims at an mm_cid below 3 for them. That
is a target, not a bound. The case prints when the target is missed. It
fails only when an mm_cid is not below the possible CPUs. The case needs
8 CPUs: with fewer, the kernel keeps the CPUs as owners.
The target is met on guests with 8, 16 and 64 CPUs:
3 threads widened to 64 CPUs: largest mm_cid 2 on CPU 63
A v7.1.8 kernel with 384 CPUs, which does not have the commit above,
misses it:
3 threads widened to 384 CPUs: largest mm_cid 383 on CPU 383, above the thread count target
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
.../selftests/rseq/mm_cid_bound_test.c | 88 ++++++++++++++++++-
1 file changed, 87 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/rseq/mm_cid_bound_test.c b/tools/testing/selftests/rseq/mm_cid_bound_test.c
index 11332b9ff24b..a751d8929138 100644
--- a/tools/testing/selftests/rseq/mm_cid_bound_test.c
+++ b/tools/testing/selftests/rseq/mm_cid_bound_test.c
@@ -28,6 +28,11 @@
* The number of IDs follows the union of the allowed CPUs of the threads,
* which only grows. The affinity is therefore narrowed before the fork(2)
* that creates the process under test.
+ *
+ * A last case widens the affinity. Three threads start on two CPUs, so the
+ * CPUs own the IDs. The threads are then allowed on all CPUs. The kernel
+ * aims at giving the IDs back to the tasks, so that three threads read an
+ * mm_cid below 3. That is a target too: the case prints when it is missed.
*/
#define _GNU_SOURCE
@@ -46,6 +51,9 @@
#define MAX_THREADS 1024
#define MAX_STEPS 64
+#define WIDEN_THREADS 3
+#define WIDEN_MIN_CPUS 8
+#define WIDEN_SETTLE_US 20000
/*
* Exit status of the process under test: a mask of the bound and of the
@@ -67,6 +75,7 @@ struct worker {
static struct worker workers[MAX_THREADS];
static unsigned int epoch;
static cpu_set_t allowed;
+static cpu_set_t all_cpus;
static int nr_allowed;
static int nr_possible;
@@ -228,6 +237,49 @@ static int run_child(const int *steps, int nr_steps)
return ret;
}
+/*
+ * The process under test of the widening case. It starts on two CPUs.
+ * Return a mask of the OVER_* values.
+ */
+static int run_child_widen(void)
+{
+ int nr_workers = 0, ret = 0, cpu, err;
+ unsigned int cid;
+
+ if (sched_getaffinity(0, sizeof(allowed), &allowed))
+ return CHILD_ERROR;
+ nr_allowed = CPU_COUNT(&allowed);
+
+ err = set_workers(&nr_workers, WIDEN_THREADS - 1);
+ if (err)
+ return CHILD_ERROR;
+ measure(nr_workers, &cpu);
+ cid = measure(nr_workers, &cpu);
+ ksft_print_msg("%4d threads on %d CPUs: largest mm_cid %u on CPU %d\n",
+ WIDEN_THREADS, nr_allowed, cid, cpu);
+
+ /* Each thread widens its own affinity at the end of its next tour. */
+ allowed = all_cpus;
+ nr_allowed = CPU_COUNT(&allowed);
+ if (sched_setaffinity(0, sizeof(allowed), &allowed))
+ ret |= CHILD_ERROR;
+ measure(nr_workers, &cpu);
+ /* The kernel changes the owners of the IDs from a work item. */
+ usleep(WIDEN_SETTLE_US);
+ measure(nr_workers, &cpu);
+ cid = measure(nr_workers, &cpu);
+ if (cid >= (unsigned int)nr_possible)
+ ret |= OVER_POSSIBLE;
+ if (cid >= WIDEN_THREADS)
+ ret |= OVER_STRICT;
+ ksft_print_msg("%4d threads widened to %d CPUs: largest mm_cid %u on CPU %d%s\n",
+ WIDEN_THREADS, nr_allowed, cid, cpu,
+ ret & OVER_STRICT ? ", above the thread count target" : "");
+ if (set_workers(&nr_workers, 0))
+ ret |= CHILD_ERROR;
+ return ret;
+}
+
/* Keep the first @nr CPUs of @src in @dst. */
static int narrow_cpus(cpu_set_t *dst, const cpu_set_t *src, int nr)
{
@@ -326,13 +378,46 @@ static void test_ramp(const cpu_set_t *all, int cpus, const char *name)
"%s: below the possible CPUs\n", name);
}
+static void test_widen(const cpu_set_t *all)
+{
+ const char *name = "widened from 2 CPUs: below the possible CPUs";
+ cpu_set_t narrow;
+ int status = 0;
+ pid_t child;
+
+ /* With fewer CPUs the kernel keeps the CPUs as owners of the IDs. */
+ if (CPU_COUNT(all) < WIDEN_MIN_CPUS || nr_possible < WIDEN_MIN_CPUS) {
+ ksft_test_result_skip("%s: fewer than %d CPUs\n", name, WIDEN_MIN_CPUS);
+ return;
+ }
+ narrow_cpus(&narrow, all, 2);
+ all_cpus = *all;
+ if (sched_setaffinity(0, sizeof(narrow), &narrow))
+ ksft_exit_fail_msg("sched_setaffinity: %s\n", strerror(errno));
+ child = fork();
+ if (!child)
+ _exit(run_child_widen());
+ if (child < 0)
+ ksft_exit_fail_msg("fork: %s\n", strerror(errno));
+ if (sched_setaffinity(0, sizeof(*all), all))
+ ksft_exit_fail_msg("sched_setaffinity: %s\n", strerror(errno));
+ if (waitpid(child, &status, 0) != child)
+ ksft_exit_fail_msg("waitpid: %s\n", strerror(errno));
+ if (!WIFEXITED(status) || (WEXITSTATUS(status) & CHILD_ERROR))
+ ksft_exit_fail_msg("the process under test failed to run\n");
+ /* The thread count is a target. Only the bound decides the result. */
+ if (WEXITSTATUS(status) & OVER_STRICT)
+ ksft_print_msg("widened from 2 CPUs: above the thread count target\n");
+ ksft_test_result(!(WEXITSTATUS(status) & OVER_POSSIBLE), "%s\n", name);
+}
+
int main(void)
{
cpu_set_t all;
int nr_all;
ksft_print_header();
- ksft_set_plan(3);
+ ksft_set_plan(4);
if (!rseq_available())
ksft_exit_skip("rseq is unavailable\n");
@@ -351,5 +436,6 @@ int main(void)
test_ramp(&all, 2, "2 CPUs");
test_ramp(&all, 4, "4 CPUs");
test_ramp(&all, nr_all, "all CPUs");
+ test_widen(&all);
ksft_finished();
}
--
2.47.3
next prev parent reply other threads:[~2026-11-17 16:17 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 16:17 [PATCH 00/17] sched/mmcid: fixes for execve, mode switches and affinity Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 01/17] sched/mmcid: Give a CID to a task that joins its mm while running Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 02/17] sched/mmcid: Give no concurrency ID to user workers Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 03/17] sched/mmcid: Reset stale state when the first user joins an mm Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 04/17] sched/mmcid: Let a running joiner take its CID after the fixups Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 05/17] sched/mmcid: Take the allowed CPUs of the first user when it joins Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 06/17] sched/mmcid: Synchronize MM teardown with affinity updates Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 07/17] selftests/rseq: Test the concurrency ID after a failed execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 08/17] selftests/rseq: Test the concurrency ID after the last user fails execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 09/17] selftests/rseq: Test the concurrency IDs across ownership mode switches Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 10/17] selftests/rseq: Test the upper bound of the concurrency IDs Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 11/17] selftests/rseq: Test affinity changes made by another process Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 12/17] selftests/rseq: Test an affinity change by another process during execve Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 13/17] selftests/rseq: Test concurrent thread creation and forced migration Mathieu Desnoyers
2026-11-17 16:17 ` Mathieu Desnoyers [this message]
2026-11-17 16:17 ` [PATCH 15/17] selftests/rseq: Test two race windows of the concurrency ID mode switches Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 16/17] selftests/rseq: Test affinity changes while a task replaces or drops its mm Mathieu Desnoyers
2026-11-17 16:17 ` [PATCH 17/17] selftests/rseq: Test the last user after an affinity change during execve Mathieu Desnoyers
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=05781fc46cb63b548588e58b356ab08b10619aac.1794932121.git.mathieu.desnoyers@efficios.com \
--to=mathieu.desnoyers@efficios.com \
--cc=bestswngs@gmail.com \
--cc=boqun@kernel.org \
--cc=brauner@kernel.org \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=jack@suse.cz \
--cc=juri.lelli@redhat.com \
--cc=kees@kernel.org \
--cc=kprateek.nayak@amd.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=oleg@redhat.com \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=shuah@kernel.org \
--cc=tglx@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=viro@zeniv.linux.org.uk \
--cc=vschneid@redhat.com \
--cc=xmei5@asu.edu \
--cc=xujiakai24@mails.ucas.ac.cn \
/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
all inboxes | Powered by JetHome®