mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 09/17] selftests/rseq: Test the concurrency IDs across ownership mode switches
Date: Tue, 17 Nov 2026 11:17:09 -0500	[thread overview]
Message-ID: <ec371dca311a8d1b059f2ee99c7b41dc049bfac6.1794932121.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1794932121.git.mathieu.desnoyers@efficios.com>

Either the tasks or the CPUs own the concurrency IDs of an mm. The
kernel switches between both when the thread count crosses a threshold.
It also does when an affinity change widens the allowed CPUs. No test
changes the thread count while the IDs are in use.

Add mm_cid_transition_test. Each round creates 1 to 16 threads and
joins them, so the thread count crosses both thresholds. A round has
three kinds of threads:
- Most threads count in an array indexed by their mm_cid, in an rseq
  critical section with no lock. Two threads that run with the same
  mm_cid at the same time lose counts.
- One thread fails execve() in a loop, so it leaves the mm and rejoins
  it.
- One thread calls vfork() in a loop. Its child is a user of the mm,
  and can join while the thread above is out.

A last case starts on two CPUs and widens the affinity of a thread
while three others count.

The test fails when counts are lost, or when an mm_cid is not below the
number of possible CPUs.

It passes with this series on guests with 2 to 64 CPUs. On a v7.1.8
kernel with 384 CPUs, it reports the ID a failed execve() gets in per
CPU mode:

  thread count changes: mm_cid 2147483648 (0x80000000), 384 possible CPUs
  not ok 1 thread count changes on at most 4 CPUs

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
 tools/testing/selftests/rseq/.gitignore       |   1 +
 tools/testing/selftests/rseq/Makefile         |   2 +-
 .../selftests/rseq/mm_cid_transition_test.c   | 375 ++++++++++++++++++
 3 files changed, 377 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/rseq/mm_cid_transition_test.c

diff --git a/tools/testing/selftests/rseq/.gitignore b/tools/testing/selftests/rseq/.gitignore
index 1d94aaf15f4e..e8afb37121d7 100644
--- a/tools/testing/selftests/rseq/.gitignore
+++ b/tools/testing/selftests/rseq/.gitignore
@@ -10,6 +10,7 @@ param_test_mm_cid
 param_test_mm_cid_benchmark
 param_test_mm_cid_compare_twice
 mm_cid_execve_test
+mm_cid_transition_test
 syscall_errors_test
 slice_test
 legacy_check
diff --git a/tools/testing/selftests/rseq/Makefile b/tools/testing/selftests/rseq/Makefile
index ee40f80c079c..ac32d6af3b99 100644
--- a/tools/testing/selftests/rseq/Makefile
+++ b/tools/testing/selftests/rseq/Makefile
@@ -20,7 +20,7 @@ OVERRIDE_TARGETS = 1
 
 TEST_GEN_PROGS = basic_test basic_percpu_ops_test basic_percpu_ops_mm_cid_test \
 		 param_test_benchmark param_test_mm_cid_benchmark \
-		 mm_cid_execve_test
+		 mm_cid_execve_test mm_cid_transition_test
 
 TEST_GEN_PROGS_EXTENDED = librseq.so \
 	param_test \
diff --git a/tools/testing/selftests/rseq/mm_cid_transition_test.c b/tools/testing/selftests/rseq/mm_cid_transition_test.c
new file mode 100644
index 000000000000..7c80c222bd96
--- /dev/null
+++ b/tools/testing/selftests/rseq/mm_cid_transition_test.c
@@ -0,0 +1,375 @@
+// SPDX-License-Identifier: LGPL-2.1
+/*
+ * The concurrency IDs while the kernel switches their ownership mode.
+ *
+ * The kernel lets the tasks own the IDs of an mm while there are few
+ * threads. The CPUs own them once there are more threads than IDs. The
+ * kernel switches mode when the thread count crosses a threshold. It also
+ * does when an affinity change widens the allowed CPUs.
+ *
+ * Each round creates a few threads and joins them, so the thread count
+ * crosses both thresholds. A round has three kinds of threads:
+ * - Most threads count in an array indexed by their mm_cid, with an rseq
+ *   critical section and no lock. Two threads that run with the same
+ *   mm_cid at the same time lose counts.
+ * - One thread fails execve(2) in a loop. It leaves the mm and rejoins
+ *   it.
+ * - One thread calls vfork(2) in a loop. The child is a user of the mm
+ *   until it exits. It can join while the thread above is out.
+ *
+ * A second case starts on two CPUs and widens the affinity of a thread
+ * while the others count.
+ *
+ * The test fails when counts are lost, or when an mm_cid is not below the
+ * number of possible CPUs. A kernel that loops looking for a free ID
+ * shows as a timeout.
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <limits.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "rseq.h"
+#include "kselftest.h"
+
+#define MAX_CIDS	CPU_SETSIZE
+#define MAX_WORKERS	16
+#define ROUNDS		200
+#define WIDEN_RUNS	100
+#define ROUND_US	500
+#define MAX_TRIES	1000000
+
+#define CHILD_PASS	0
+#define CHILD_FAIL	1
+#define CHILD_ERROR	2
+
+struct worker {
+	pthread_t	thread;
+	intptr_t	counted;
+	unsigned int	bad_cid;
+	bool		execs;
+	bool		vforks;
+};
+
+static intptr_t counts[MAX_CIDS];
+static struct worker workers[MAX_WORKERS];
+static int stop;
+static int nr_possible;
+static cpu_set_t all_cpus;
+static char exec_path[PATH_MAX];
+
+/* Add one to the count of the current mm_cid. */
+static void count_one(struct worker *w)
+{
+	for (;;) {
+		unsigned int cid = rseq_current_mm_cid();
+
+		if (cid >= (unsigned int)nr_possible || cid >= MAX_CIDS) {
+			w->bad_cid = cid;
+			return;
+		}
+		if (!rseq_addv(RSEQ_MO_RELAXED, RSEQ_PERCPU_MM_CID, &counts[cid], 1, cid)) {
+			w->counted++;
+			return;
+		}
+	}
+}
+
+static int join_failed;
+
+/* Join @t. Report an error and set join_failed if that fails. */
+static void join_thread(pthread_t t)
+{
+	int err = pthread_join(t, NULL);
+
+	if (err) {
+		fprintf(stderr, "pthread_join: %s\n", strerror(err));
+		RSEQ_WRITE_ONCE(join_failed, 1);
+	}
+}
+
+/* Remove @path. Return 0, or -1 after reporting the error. */
+static int remove_file(const char *path)
+{
+	if (unlink(path)) {
+		fprintf(stderr, "unlink %s: %s\n", path, strerror(errno));
+		return -1;
+	}
+	return 0;
+}
+
+static void *worker_fn(void *arg)
+{
+	struct worker *w = arg;
+	char *const av[] = { exec_path, NULL };
+	int i;
+
+	if (rseq_register_current_thread())
+		abort();
+	while (!RSEQ_READ_ONCE(stop) && w->bad_cid == UINT_MAX) {
+		for (i = 0; i < 100; i++)
+			count_one(w);
+		if (w->execs && execve(exec_path, av, NULL) == -1 && errno != ENOEXEC) {
+			fprintf(stderr, "execve: %s, expected ENOEXEC\n", strerror(errno));
+			abort();
+		}
+		if (w->vforks) {
+			pid_t pid = vfork();
+
+			if (!pid)
+				_exit(0);
+			if (pid < 0 || waitpid(pid, NULL, 0) != pid) {
+				fprintf(stderr, "vfork: %s\n", strerror(errno));
+				abort();
+			}
+		}
+	}
+	rseq_unregister_current_thread();
+	return NULL;
+}
+
+/* Run @nr workers for a while, one of them failing execve(2). */
+static int run_round(int nr, int widen_us)
+{
+	int i, started = 0, widened = 1;
+
+	RSEQ_WRITE_ONCE(stop, 0);
+	for (i = 0; i < nr; i++) {
+		int err, tries = 0;
+
+		workers[i].execs = i == 0 && exec_path[0];
+		workers[i].vforks = i == 1;
+		/* clone(CLONE_FS) fails with EAGAIN while a thread is in execve. */
+		do {
+			err = pthread_create(&workers[i].thread, NULL, worker_fn, &workers[i]);
+			if (err == EAGAIN)
+				sched_yield();
+		} while (err == EAGAIN && ++tries < MAX_TRIES);
+		if (err)
+			break;
+		started++;
+	}
+	if (widen_us >= 0) {
+		usleep(widen_us);
+		/* The union of allowed CPUs of the mm grows. */
+		if (sched_setaffinity(0, sizeof(all_cpus), &all_cpus)) {
+			fprintf(stderr, "sched_setaffinity: %s\n", strerror(errno));
+			widened = 0;
+		}
+	}
+	usleep(ROUND_US);
+	RSEQ_WRITE_ONCE(stop, 1);
+	for (i = 0; i < started; i++)
+		join_thread(workers[i].thread);
+	return started == nr && widened && !RSEQ_READ_ONCE(join_failed) ? 0 : -1;
+}
+
+/* Compare the counts of the array with the counts of the workers. */
+static int check_counts(const char *what)
+{
+	intptr_t counted = 0, total = 0;
+	int i, ret = CHILD_PASS;
+
+	for (i = 0; i < MAX_WORKERS; i++) {
+		counted += workers[i].counted;
+		if (workers[i].bad_cid != UINT_MAX) {
+			fprintf(stderr, "%s: mm_cid %u (0x%x), %d possible CPUs\n",
+				what, workers[i].bad_cid, workers[i].bad_cid, nr_possible);
+			ret = CHILD_FAIL;
+		}
+	}
+	for (i = 0; i < MAX_CIDS; i++)
+		total += counts[i];
+	if (total != counted) {
+		fprintf(stderr, "%s: %ld counts lost out of %ld: two threads ran with one mm_cid\n",
+			what, (long)(counted - total), (long)counted);
+		ret = CHILD_FAIL;
+	}
+	return ret;
+}
+
+static void init_workers(void)
+{
+	int i;
+
+	for (i = 0; i < MAX_WORKERS; i++)
+		workers[i].bad_cid = UINT_MAX;
+}
+
+/*
+ * A file no binfmt handler recognises, in @dir. execve(2) on it fails with
+ * ENOEXEC after sched_mm_cid_before_execve() and before the other threads
+ * are killed.
+ */
+static int make_unknown_format_file(const char *dir)
+{
+	static const char junk[] = "not a binary of any known format\n";
+	char *const av[] = { exec_path, NULL };
+	bool written;
+	int fd;
+
+	snprintf(exec_path, sizeof(exec_path), "%s/mm_cid_transition_test.XXXXXX", dir);
+	fd = mkstemp(exec_path);
+	if (fd < 0)
+		goto fail;
+	written = write(fd, junk, sizeof(junk) - 1) == (ssize_t)sizeof(junk) - 1 &&
+		  !fchmod(fd, 0755);
+	if (close(fd) || !written)
+		goto unlink;
+	if (execve(exec_path, av, NULL) == -1 && errno == ENOEXEC)
+		return 0;
+unlink:
+	remove_file(exec_path);
+fail:
+	exec_path[0] = '\0';
+	return -1;
+}
+
+/* Rounds of 1 to MAX_WORKERS threads: the count crosses both thresholds. */
+static int run_child_rounds(int unused)
+{
+	int round, ret;
+
+	init_workers();
+	/* Without the file the rounds run with no execve. */
+	if (make_unknown_format_file("/tmp") && make_unknown_format_file("."))
+		fprintf(stderr, "no unknown format file: the rounds run without execve\n");
+	for (round = 0; round < ROUNDS; round++) {
+		if (run_round(1 + (round * 7) % MAX_WORKERS, -1)) {
+			ret = CHILD_ERROR;
+			goto out;
+		}
+	}
+	ret = check_counts("thread count changes");
+out:
+	if (exec_path[0] && remove_file(exec_path) && ret == CHILD_PASS)
+		ret = CHILD_ERROR;
+	return ret;
+}
+
+/* Run one round that widens the allowed CPUs @delay_us after its start. */
+static int run_child_widen(int delay_us)
+{
+	init_workers();
+	if (run_round(3, delay_us))
+		return CHILD_ERROR;
+	return check_counts("affinity change");
+}
+
+static int read_nr_possible(void)
+{
+	int first, last, total = 0;
+	char sep;
+	FILE *f;
+
+	f = fopen("/sys/devices/system/cpu/possible", "r");
+	if (!f)
+		return -1;
+	/* A list of ranges: "0-63" or "0,2-5". */
+	while (fscanf(f, "%d", &first) == 1) {
+		last = first;
+		sep = '\n';
+		if (fscanf(f, "%c", &sep) == 1 && sep == '-') {
+			if (fscanf(f, "%d", &last) != 1)
+				break;
+			if (fscanf(f, "%c", &sep) != 1)
+				sep = '\n';
+		}
+		total += last - first + 1;
+		if (sep != ',')
+			break;
+	}
+	fclose(f);
+	return total;
+}
+
+/* Keep the first @nr CPUs of @src in @dst. */
+static int narrow_cpus(cpu_set_t *dst, const cpu_set_t *src, int nr)
+{
+	int cpu, taken = 0;
+
+	CPU_ZERO(dst);
+	for (cpu = 0; cpu < CPU_SETSIZE && taken < nr; cpu++) {
+		if (!CPU_ISSET(cpu, src))
+			continue;
+		CPU_SET(cpu, dst);
+		taken++;
+	}
+	return taken;
+}
+
+/*
+ * Run @fn(@arg) in a new process and return its exit status. The affinity
+ * is @narrow when its mm is created. The union of allowed CPUs of an mm
+ * starts there and only grows.
+ */
+static int run_under_test(int (*fn)(int), int arg, const cpu_set_t *narrow)
+{
+	int status = 0;
+	pid_t child;
+
+	if (sched_setaffinity(0, sizeof(*narrow), narrow))
+		ksft_exit_skip("sched_setaffinity: %s\n", strerror(errno));
+	child = fork();
+	if (!child)
+		_exit(fn(arg));
+	if (child < 0)
+		ksft_exit_fail_msg("fork: %s\n", strerror(errno));
+	if (sched_setaffinity(0, sizeof(all_cpus), &all_cpus))
+		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))
+		ksft_exit_fail_msg("the process under test was killed by signal %d\n",
+				   WTERMSIG(status));
+	if (WEXITSTATUS(status) > CHILD_FAIL)
+		ksft_exit_fail_msg("the process under test failed to run\n");
+	return WEXITSTATUS(status);
+}
+
+int main(void)
+{
+	cpu_set_t narrow;
+	int status, i;
+
+	ksft_print_header();
+	ksft_set_plan(3);
+
+	if (!rseq_available())
+		ksft_exit_skip("rseq is unavailable\n");
+	if (rseq_register_current_thread())
+		ksft_exit_fail_msg("rseq_register_current_thread: %s\n", strerror(errno));
+	if (!rseq_mm_cid_available())
+		ksft_exit_skip("the rseq mm_cid field is unavailable\n");
+	if (sched_getaffinity(0, sizeof(all_cpus), &all_cpus))
+		ksft_exit_fail_msg("sched_getaffinity: %s\n", strerror(errno));
+	nr_possible = read_nr_possible();
+	if (nr_possible <= 0)
+		ksft_exit_skip("cannot read the possible CPUs\n");
+
+	narrow_cpus(&narrow, &all_cpus, 4);
+	status = run_under_test(run_child_rounds, 0, &narrow);
+	ksft_test_result(status == CHILD_PASS, "thread count changes on at most 4 CPUs\n");
+
+	status = run_under_test(run_child_rounds, 0, &all_cpus);
+	ksft_test_result(status == CHILD_PASS, "thread count changes on all CPUs\n");
+
+	narrow_cpus(&narrow, &all_cpus, 2);
+	for (i = 0; i < WIDEN_RUNS; i++) {
+		status = run_under_test(run_child_widen, (i * 13) % ROUND_US, &narrow);
+		if (status != CHILD_PASS)
+			break;
+	}
+	ksft_test_result(status == CHILD_PASS, "affinity change widens the allowed CPUs\n");
+	ksft_finished();
+}
-- 
2.47.3


  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 ` Mathieu Desnoyers [this message]
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 ` [PATCH 14/17] selftests/rseq: Test the concurrency IDs after the affinity is widened Mathieu Desnoyers
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=ec371dca311a8d1b059f2ee99c7b41dc049bfac6.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®