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: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-kernel@vger.kernel.org, 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, kmehltretter@gmail.com
Subject: [PATCH v2 07/13] selftests/rseq: Test the upper bound of the concurrency IDs
Date: Tue,  1 Dec 2026 19:27:55 -0500	[thread overview]
Message-ID: <2af58501b3ea3eae3dc82162d9ecba60debd1984.1796169827.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1796169827.git.mathieu.desnoyers@efficios.com>

User space sizes its per-CID data by the number of possible CPUs. The
kernel keeps each mm_cid below that number. mm_cid_transition_test
checks this bound with rounds of 1 to 16 threads. On all the CPUs of
a large machine, they never outnumber the CPUs.

The kernel also aims at compact IDs. It tries to keep the IDs below
max_cids:

  opt_cids = min(threads, allowed CPUs)
  max_cids = min(opt_cids + opt_cids / 4, possible CPUs)

Add mm_cid_bound_test. It runs a process on 2 CPUs, on 4 CPUs and on
all allowed CPUs. The process grows from one thread to twice its
CPUs plus two, then shrinks back to one. After each step, every thread
visits every CPU of the process and reads its mm_cid there. The
largest value must stay below the number of possible CPUs.

The test prints the largest value of each step. It notes the steps
where this value reaches max_cids or min(threads, allowed CPUs). These
notes do not make it fail.

The test passes on guests with 2 to 64 CPUs, on tip sched/urgent and
after each kernel patch of this series. The 4-CPU case is skipped on
smaller guests.

With 64 possible CPUs, some steps get a note while the process
shrinks. Two of them, from a run with the four kernel patches:

  #   35 threads on 64 CPUs: largest mm_cid 63 on CPU 62, max_cids 43, min(threads, CPUs) 35, reaches min(threads, CPUs), reaches max_cids
  #   20 threads on 64 CPUs: largest mm_cid 24 on CPU 63, max_cids 25, min(threads, CPUs) 20, reaches min(threads, CPUs)

Their causes differ:
- At 35 threads, the mm is in per CPU mode. Every CPU ran a thread,
  so every CPU owns an ID. The mm leaves that mode below pcpu_thrs:
  32 threads here.
- At 20 threads, the mm is in per task mode. A thread keeps its ID
  while the ID is below max_cids.

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

diff --git a/tools/testing/selftests/rseq/.gitignore b/tools/testing/selftests/rseq/.gitignore
index e8afb37121d7..ff63ada38d5f 100644
--- a/tools/testing/selftests/rseq/.gitignore
+++ b/tools/testing/selftests/rseq/.gitignore
@@ -9,6 +9,7 @@ param_test_compare_twice
 param_test_mm_cid
 param_test_mm_cid_benchmark
 param_test_mm_cid_compare_twice
+mm_cid_bound_test
 mm_cid_execve_test
 mm_cid_transition_test
 syscall_errors_test
diff --git a/tools/testing/selftests/rseq/Makefile b/tools/testing/selftests/rseq/Makefile
index ac32d6af3b99..b643ce9cb585 100644
--- a/tools/testing/selftests/rseq/Makefile
+++ b/tools/testing/selftests/rseq/Makefile
@@ -20,7 +20,8 @@ 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_transition_test
+		 mm_cid_execve_test mm_cid_transition_test \
+		 mm_cid_bound_test
 
 TEST_GEN_PROGS_EXTENDED = librseq.so \
 	param_test \
diff --git a/tools/testing/selftests/rseq/mm_cid_bound_test.c b/tools/testing/selftests/rseq/mm_cid_bound_test.c
new file mode 100644
index 000000000000..4cb011b490f8
--- /dev/null
+++ b/tools/testing/selftests/rseq/mm_cid_bound_test.c
@@ -0,0 +1,355 @@
+// SPDX-License-Identifier: LGPL-2.1
+/*
+ * The upper bound of the concurrency IDs while the thread count changes.
+ *
+ * A process grows to more threads than it has CPUs, then shrinks back to
+ * one thread. After each step every thread visits every allowed CPU and
+ * reads its mm_cid there. The largest value must stay below the number of
+ * possible CPUs. That is the upper bound of an mm_cid, and it sizes the
+ * per-CID data of user space.
+ *
+ * The kernel also aims at compact IDs. Its target is max_cids:
+ *
+ *     opt_cids = min(threads, allowed CPUs)
+ *     max_cids = min(opt_cids + opt_cids / 4, possible CPUs)
+ *
+ * The slack above opt_cids gives the switch between the two ownership
+ * modes some hysteresis. Threads that come and go near the limit then do
+ * not cause a costly mode switch each time.
+ *
+ * The test prints the steps where the largest mm_cid reaches max_cids,
+ * or reaches min(threads, allowed CPUs). These notes do not make it fail.
+ *
+ * The steps cross both ownership modes of the kernel. The tasks own the
+ * IDs at first. The CPUs own them once there are more threads than
+ * max_cids. The tasks own them again once the thread count drops below a
+ * threshold.
+ *
+ * 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.
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "rseq.h"
+#include "kselftest.h"
+
+#define MAX_THREADS	1024
+#define MAX_STEPS	64
+
+/*
+ * Exit status of the process under test: a mask of the bound and of the
+ * targets exceeded.
+ */
+#define OVER_POSSIBLE	1
+#define OVER_MAX_CIDS	2
+#define OVER_STRICT	4
+#define CHILD_ERROR	64
+
+struct worker {
+	pthread_t	thread;
+	int		stop;
+	unsigned int	ack;
+	unsigned int	max_cid;
+	int		max_cpu;
+};
+
+static struct worker workers[MAX_THREADS];
+static unsigned int epoch;
+static cpu_set_t allowed;
+static int nr_allowed;
+static int nr_possible;
+
+/* Visit each allowed CPU and record the largest mm_cid read there. */
+static void tour(struct worker *w)
+{
+	cpu_set_t one;
+	int cpu;
+
+	w->max_cid = 0;
+	w->max_cpu = -1;
+	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
+		unsigned int cid;
+
+		if (!CPU_ISSET(cpu, &allowed))
+			continue;
+		CPU_ZERO(&one);
+		CPU_SET(cpu, &one);
+		if (sched_setaffinity(0, sizeof(one), &one))
+			continue;
+		cid = rseq_current_mm_cid();
+		if (cid >= w->max_cid) {
+			w->max_cid = cid;
+			w->max_cpu = cpu;
+		}
+	}
+	if (sched_setaffinity(0, sizeof(allowed), &allowed)) {
+		fprintf(stderr, "sched_setaffinity: %s\n", strerror(errno));
+		abort();
+	}
+}
+
+static void *worker_fn(void *arg)
+{
+	struct worker *w = arg;
+	unsigned int seen = RSEQ_READ_ONCE(w->ack);
+
+	if (rseq_register_current_thread())
+		abort();
+	while (!RSEQ_READ_ONCE(w->stop)) {
+		unsigned int e = RSEQ_READ_ONCE(epoch);
+
+		if (e == seen) {
+			usleep(200);
+			continue;
+		}
+		tour(w);
+		seen = e;
+		/* Publish max_cid and max_cpu to measure(). */
+		rseq_smp_store_release(&w->ack, e);
+	}
+	rseq_unregister_current_thread();
+	return NULL;
+}
+
+/* Run one tour on every thread. Return the largest mm_cid, and its CPU. */
+static unsigned int measure(int nr_workers, int *cpu)
+{
+	unsigned int e = RSEQ_READ_ONCE(epoch) + 1;
+	unsigned int max_cid;
+	struct worker self;
+	int i;
+
+	RSEQ_WRITE_ONCE(epoch, e);
+	tour(&self);
+	max_cid = self.max_cid;
+	*cpu = self.max_cpu;
+	for (i = 0; i < nr_workers; i++) {
+		while (rseq_smp_load_acquire(&workers[i].ack) != e)
+			usleep(200);
+		if (workers[i].max_cid > max_cid) {
+			max_cid = workers[i].max_cid;
+			*cpu = workers[i].max_cpu;
+		}
+	}
+	return max_cid;
+}
+
+/*
+ * Create or join threads to reach @target. Return 0, or the error number
+ * after reporting it.
+ */
+static int set_workers(int *nr_workers, int target)
+{
+	int err;
+
+	while (*nr_workers < target) {
+		struct worker *w = &workers[*nr_workers];
+
+		w->stop = 0;
+		w->ack = RSEQ_READ_ONCE(epoch);
+		err = pthread_create(&w->thread, NULL, worker_fn, w);
+		if (err) {
+			fprintf(stderr, "pthread_create: %s\n", strerror(err));
+			return err;
+		}
+		(*nr_workers)++;
+	}
+	while (*nr_workers > target) {
+		struct worker *w = &workers[*nr_workers - 1];
+
+		RSEQ_WRITE_ONCE(w->stop, 1);
+		err = pthread_join(w->thread, NULL);
+		if (err) {
+			fprintf(stderr, "pthread_join: %s\n", strerror(err));
+			return err;
+		}
+		(*nr_workers)--;
+	}
+	return 0;
+}
+
+static unsigned int min_u(unsigned int a, unsigned int b)
+{
+	return a < b ? a : b;
+}
+
+/* The process under test. Return a mask of the OVER_* values. */
+static int run_child(const int *steps, int nr_steps)
+{
+	int nr_workers = 0, ret = 0, i;
+
+	if (sched_getaffinity(0, sizeof(allowed), &allowed))
+		return CHILD_ERROR;
+	nr_allowed = CPU_COUNT(&allowed);
+
+	for (i = 0; i < nr_steps; i++) {
+		unsigned int threads = steps[i], opt, max_cids, strict, cid;
+		int cpu, over = 0, err;
+
+		err = set_workers(&nr_workers, threads - 1);
+		if (err) {
+			ret |= CHILD_ERROR;
+			break;
+		}
+		/* The first tour lets every thread schedule on every CPU. */
+		measure(nr_workers, &cpu);
+		cid = measure(nr_workers, &cpu);
+
+		opt = min_u(threads, nr_allowed);
+		strict = opt;
+		max_cids = min_u(opt + opt / 4, nr_possible);
+		if (cid >= (unsigned int)nr_possible)
+			over |= OVER_POSSIBLE;
+		if (cid >= max_cids)
+			over |= OVER_MAX_CIDS;
+		if (cid >= strict)
+			over |= OVER_STRICT;
+		ksft_print_msg(
+			"%4u threads on %d CPUs: largest mm_cid %u on CPU %d, max_cids %u, min(threads, CPUs) %u%s%s%s\n",
+			       threads, nr_allowed, cid, cpu, max_cids, strict,
+			       over & OVER_STRICT ? ", reaches min(threads, CPUs)" : "",
+			       over & OVER_MAX_CIDS ? ", reaches max_cids" : "",
+			       over & OVER_POSSIBLE ? ", at or above the possible CPUs" : "");
+		ret |= over;
+	}
+	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)
+{
+	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;
+}
+
+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;
+}
+
+/* Thread counts from 1 up to twice the CPUs plus two, and back to 1. */
+static int make_steps(int *steps, int cpus)
+{
+	int top = 2 * cpus + 2, stride = 1, n = 0, t;
+
+	if (top > MAX_THREADS)
+		top = MAX_THREADS;
+	/* Every count on a small set of CPUs, a fraction on a large one. */
+	while (2 * (top / stride) + 1 > MAX_STEPS)
+		stride++;
+	for (t = 1; t < top; t += stride)
+		steps[n++] = t;
+	steps[n++] = top;
+	for (t = top - stride; t > 1; t -= stride)
+		steps[n++] = t;
+	steps[n++] = 1;
+	return n;
+}
+
+static void test_ramp(const cpu_set_t *all, int cpus, const char *name)
+{
+	int steps[MAX_STEPS + 2], nr_steps, status = 0;
+	cpu_set_t narrow;
+	pid_t child;
+
+	if (narrow_cpus(&narrow, all, cpus) < cpus) {
+		ksft_test_result_skip("%s: below the possible CPUs\n", name);
+		return;
+	}
+	nr_steps = make_steps(steps, cpus);
+
+	/* Narrowed before the fork(2): the union of allowed CPUs only grows. */
+	if (sched_setaffinity(0, sizeof(narrow), &narrow))
+		ksft_exit_fail_msg("sched_setaffinity: %s\n", strerror(errno));
+	child = fork();
+	if (!child)
+		_exit(run_child(steps, nr_steps));
+	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");
+
+	status = WEXITSTATUS(status);
+	/* The targets are reported. Only the bound decides the result. */
+	if (status & OVER_MAX_CIDS)
+		ksft_print_msg("%s: some steps reach max_cids\n", name);
+	if (status & OVER_STRICT)
+		ksft_print_msg("%s: some steps reach min(threads, CPUs)\n", name);
+	ksft_test_result(!(status & OVER_POSSIBLE),
+			 "%s: below the possible CPUs\n", name);
+}
+
+int main(void)
+{
+	cpu_set_t all;
+	int nr_all;
+
+	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), &all))
+		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");
+	nr_all = CPU_COUNT(&all);
+	ksft_print_msg("%d possible CPUs, %d allowed\n", nr_possible, nr_all);
+
+	test_ramp(&all, 2, "2 CPUs");
+	test_ramp(&all, 4, "4 CPUs");
+	test_ramp(&all, nr_all, "all CPUs");
+	ksft_finished();
+}
-- 
2.47.3


  parent reply	other threads:[~2026-12-02  0:28 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  0:27 [PATCH v2 00/13] sched/mmcid: fixes for execve, mode switches and affinity Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 01/13] sched/mmcid: Move the execve() hooks into exec_mmap() Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 02/13] sched/mmcid: Add the allowed CPUs of each joining task to the mm Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 03/13] sched/mmcid: Serialize joins and exits with affinity changes by pi_lock Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 04/13] sched/mmcid: Give no concurrency ID to user workers Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 05/13] selftests/rseq: Test the concurrency ID after a failed execve Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 06/13] selftests/rseq: Test the concurrency IDs across ownership mode switches Mathieu Desnoyers
2026-12-02  0:27 ` Mathieu Desnoyers [this message]
2026-12-02  0:27 ` [PATCH v2 08/13] selftests/rseq: Test an affinity change by another process during execve Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 09/13] selftests/rseq: Test the concurrency IDs after the affinity is widened Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 10/13] selftests/rseq: Test two race windows of the concurrency ID mode switches Mathieu Desnoyers
2026-12-02  0:27 ` [PATCH v2 11/13] selftests/rseq: Test the last user after an affinity change during execve Mathieu Desnoyers
2026-12-02  0:28 ` [PATCH v2 12/13] selftests/rseq: Test the concurrency IDs with an io_uring SQPOLL thread Mathieu Desnoyers
2026-12-02  0:28 ` [PATCH v2 13/13] selftests/rseq: Test the concurrency IDs after clone3() into a wider cpuset Mathieu Desnoyers
2026-12-02  2:49 ` [PATCH v2 00/13] sched/mmcid: fixes for execve, mode switches and affinity Karl Mehltretter

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=2af58501b3ea3eae3dc82162d9ecba60debd1984.1796169827.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=kmehltretter@gmail.com \
    --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®