From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=158.69.130.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796171289; b=2w1ntno3GCjUlacylCJPs6cyP7S1OqK+GG0eB/B0T6eGTvgCz5GouLmm0bCS8iMGJPLm +EoI2Ct6O4xAC0YlQk2KTF7X2P/KIYopTczti7m3rBhIr44BRaVGJDAmh+MJRqP2vQTu1 Lyu+yeP5zh+/v13HUIcGFcKPL1Nv3VuYQWDsoIcXL4lkDVGP4Q4Y81E0GXfHBSGkhGOKy HwO/Fonf4vtmCw2FdQDpXUskcaZ4Dm47EKzIKJmuZtLjVlS4mjBCz9/92H3nW2KyO5xwW 4s1onzBBNeao6B6dJHsS7LQw8dCaBypEHhzh+DWR5PWvyn2ObGi9pgvcfP8ZhILFkfQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796171289; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=iUhg7hRPaM2T7lAm0166hHB+fQfCnI6vD9v2KZ0I5so=; b=lg8+PWzHRxHB4S4FFYfgzAvnutYndci4wYatfRfYm063Nl0AuNoC7vv7vPy6uSedLyEB oDocUppfNOdqjaNq5GqoR5RPK9Pwv66J/8YWVfLLimrLsZ5brl+MNeP8ukbo04HpoH/FN 45uZd8gKgakwcv5NUSzDAyEaFSu8U2dsz/54Tv7sGK5EGyhSnMWQBgDdFSmsuUZnYKBrW dqsbF2K9cu+0CN/rXuYFqhPHH4XUTUo9Mti6whG6yCoV1jrHo9G/F0lNgbh9ffoUqyKqU uxjifTC11b1YzM1NK+q05Nh4L2rBXcg7SZrMIxdLHdxtkfAj7yI7Ndk1PPItUMsXEWg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=efficios.com; spf=pass smtp.mailfrom=efficios.com; dkim=pass header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b="h/4o6eq3"; arc=none smtp.remote-ip=158.69.130.18 Received: from smtpout.efficios.com (smtpout.efficios.com [158.69.130.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F591606A6B3; Wed, 2 Dec 2026 00:28:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6F591606A6B3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=efficios.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=h/4o6eq3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796171288; bh=iUhg7hRPaM2T7lAm0166hHB+fQfCnI6vD9v2KZ0I5so=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h/4o6eq3bO/ODIlFKT2f0Suan4HEXfqTiFE9J++KtcMUjdFCPHC7sdDMKQeS3zcyW REh/bq6/8QfdCuelqrirs7GVwyhR3482D5Cq0/V8nSt3TW2mpHS8rAOHID8RZ+PiB+ PEyU31iqf20n6j+FBW6B7BKRpqlxTIPoNBg6bapJpuMQxfC/VomN5N4Om2sNEhGZmf QFyet52NKeqaadcSUjXV5gJySntO7E5B0zW4IRUjSMvJboq8gfXiE3C8UkZqUw0klU Aqif0bEYQQ2DMc050EXV2v3dT+/XCGryLu5U/d6rwGp1JfBl+93kLTYPegDtaDWY4j Fssw32yJEfdrQ== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jfJWX3QfQzYyq; Tue, 01 Dec 2026 19:28:08 -0500 (EST) From: Mathieu Desnoyers To: tglx@kernel.org, peterz@infradead.org Cc: Mathieu Desnoyers , 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 09/13] selftests/rseq: Test the concurrency IDs after the affinity is widened Date: Tue, 1 Dec 2026 19:27:57 -0500 Message-ID: <9f23cffbe62fe3281bc1bfe44891dccf58d26185.1796169827.git.mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Either the tasks or the CPUs own the concurrency IDs of an mm. When an affinity change widens the allowed CPUs of the mm, the IDs go back to the tasks if the thread count is below pcpu_thrs. Commit 7dbc307b6c72 ("sched/mmcid: Fix affinity-triggered switch back to per-task mode") fixed how pcpu_thrs is computed. Before it, pcpu_thrs was never above the thread count, so the CPUs kept the IDs. mm_cid_transition_test widens the affinity, but it only checks the bound. No test checks that the IDs go back to the tasks. Add a case to mm_cid_bound_test. Three threads start on two CPUs, so the CPUs own the IDs. Each thread then widens its own affinity to all CPUs. It visits each CPU and reads its mm_cid there. Once the tasks own the IDs, the kernel aims at an mm_cid below 3. That is a target, not a bound. The case prints when the target is missed. It fails only when an mm_cid reaches the number of possible CPUs. The case needs 8 possible CPUs, all allowed. With fewer possible CPUs, pcpu_thrs is at most 3 and the CPUs keep the IDs. This case is the first to change the allowed CPUs while the workers run. So the workers now read the epoch with acquire semantics: the epoch publishes the new allowed CPUs. The target is met on guests with 8 to 64 CPUs, on tip sched/urgent and after each kernel patch of this series: # 3 threads widened to 64 CPUs: largest mm_cid 2 on CPU 63 A v7.1.8 kernel does not have the commit above. There, the target is missed on a machine with 384 CPUs. The case still passes there: 3 threads widened to 384 CPUs: largest mm_cid 383 on CPU 383, above the thread count target Signed-off-by: Mathieu Desnoyers --- .../selftests/rseq/mm_cid_bound_test.c | 94 ++++++++++++++++++- 1 file changed, 91 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/rseq/mm_cid_bound_test.c b/tools/testing/selftests/rseq/mm_cid_bound_test.c index 4cb011b490f8..9795cc030e7f 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. + * + * The widening case starts three threads 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; @@ -107,7 +116,8 @@ static void *worker_fn(void *arg) if (rseq_register_current_thread()) abort(); while (!RSEQ_READ_ONCE(w->stop)) { - unsigned int e = RSEQ_READ_ONCE(epoch); + /* Read allowed after the epoch that publishes it. */ + unsigned int e = rseq_smp_load_acquire(&epoch); if (e == seen) { usleep(200); @@ -130,7 +140,8 @@ static unsigned int measure(int nr_workers, int *cpu) struct worker self; int i; - RSEQ_WRITE_ONCE(epoch, e); + /* Publish allowed to the workers with the new epoch. */ + rseq_smp_store_release(&epoch, e); tour(&self); max_cid = self.max_cid; *cpu = self.max_cpu; @@ -228,6 +239,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 +380,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 +438,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