From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 17FFB606A32C for ; Fri, 4 Dec 2026 18:41:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=owgk6bwq Received: by kanga.kvack.org (Postfix) id 8D86F6B05BE; Fri, 4 Dec 2026 13:41:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8613D6B05C0; Fri, 4 Dec 2026 13:41:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74FEF6B05BF; Fri, 4 Dec 2026 13:41:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 4C77A6B05BD for ; Fri, 4 Dec 2026 13:41:18 -0500 (EST) Received: from smtpin12.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CEB08C057A for ; Fri, 4 Dec 2026 18:41:14 +0000 (UTC) X-FDA: 85507168068.12.9D0389D Received: from mta0.migadu.com (out-43.mta0.migadu.com [91.218.175.43]) by imf02.hostedemail.com (Postfix) with ESMTP id D623C80013 for ; Fri, 4 Dec 2026 18:41:12 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=owgk6bwq; spf=pass (imf02.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.43 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796409673; b=3URJRs49Vf/kdarJ99O9ACats/ruvsIYfoS3q6mCiElILlVIJm0Ve4WNOz88YGv6You6k+ SK/6P/N6HrYfwa8Rf53k/lZFH69djKchGwzYrmzHLVwzr/8yvX4ykgkCrV2zGNEHLT++oB S33Qq4eCBaCE6pM98HWhThAtBbCFiGk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796409673; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AOW2VPmGGdscfP0LMhHIqZbTD8FNIQIR4q0ekjUhuAs=; b=cEW4FO0WQyVcH180SAVGLiDfgKSQqdRCAmLO2MA2NnVQNJKqkckqM5KSWN2vBH20gnrkrS UUsc2oBZ4tgWkcqB7JjMDd/db197HYJvLYsXs5w/nsS1czpIWJ9Z/pixi2MFm08FwOc2ig 1YMbSFdrObGRNCm7ulddJTOlg5cJguc= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=owgk6bwq; spf=pass (imf02.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.43 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=9tccXtbcmhXitMZruX0znSW5iqRxMKp9w4rxnQ7nB0A=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796409671; v=1; x=1797014471; b=owgk6bwqco92T0fsECX0MuacbmnJR52gt5gb7IJTRiAnx50dXmlha38Segxr/JdtURR4tprR a6CMYylgMI48+NY62Qn8HBblYki94Bc2nvgA04PGdIY7WGdX1iXVRnlZ1aPrA6e0locBqdZCn5O w2xNwd8lw4fWXf/knUaeaoY8= X-Envelope-To: linux-mm@kvack.org Received: by smtp.migadu.com with ESMTPS id e49ae95eacd513cb; Fri, 04 Dec 2026 18:41:10 +0000 X-Mizu-Trace-ID: e49ae95eacd513cb X-Migadu-Flow: FLOW_OUT From: Shakeel Butt To: Tejun Heo Cc: Andrew Morton , Johannes Weiner , Michal Koutny , Michal Hocko , Roman Gushchin , Christian Brauner , Oleg Nesterov , Sebastian Andrzej Siewior , Suren Baghdasaryan , "T . J . Mercier" , Usama Arif , Meta kernel team , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 07/12] selftests/cgroup: test cgroup.procs reads across a failed migration Date: Fri, 4 Dec 2026 10:40:36 -0800 Message-ID: <20261204184041.1489976-8-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261204184041.1489976-1-shakeel.butt@linux.dev> References: <20261204184041.1489976-1-shakeel.butt@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D623C80013 X-Stat-Signature: i8sz6cdoqwkaznhmwkmnmje7prw8gb4t X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796409672-556322 X-HE-Meta: U2FsdGVkX18qr+IlebX09SnNDZJBCrvcZSnsebtkD+T4i/pgku1qGIaj4N65ewcEwy9nMCFXPUPiYIE5TccYe1TTgQezzO3p1vcSlRycb7YGy8rocg04YCLdSDSYN3cuI2fsAhwRXBOUFBCcObT9KK45RqN+42gPlQ+ZyvuqkJC3ztT6cC9yd96RdrfV0M2j4JpWq72WT0BUxG9P2Vy+ZyKHEzOz8JKlhsc9CBiqOieU9l8m+cZojO5sNWS+CK4ytHw3WO0cUgIczFLMexlKNNMEh4jiLxp3LN3b/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m9/ihqKEZRFvXdAiAAGFuk34yD7S5mvNvJ046c0rpDb6K8brDyKTJYJjWBSyoH Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 cgroup.procs keeps its task iterator between reads. A failed migration used to make the iterator skip the last process of a css_set. Read one byte of cgroup.procs, so that the iterator waits on the last of three processes. Then fail a migration of that process, and check that the rest of the read still lists it. cpuset makes the migration fail: a partition with one CPU gives that CPU to a child partition, which leaves its other child without CPUs. The test is skipped if this can't be set up. The test fails without "cgroup: don't lose a task in css_task_iter when a migration fails". Signed-off-by: Shakeel Butt --- tools/testing/selftests/cgroup/test_core.c | 154 +++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/tools/testing/selftests/cgroup/test_core.c b/tools/testing/selftests/cgroup/test_core.c index 53592bf309fa..01c268672d6d 100644 --- a/tools/testing/selftests/cgroup/test_core.c +++ b/tools/testing/selftests/cgroup/test_core.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -739,6 +740,158 @@ static int test_cgcore_thread_migration(const char *root) return ret; } +/* Return the highest CPU number in a cpu list like "0-3,8-11", or -1. */ +static int cpulist_last(const char *list) +{ + const char *p = list + strlen(list); + + while (p > list && !isdigit(p[-1])) + p--; + while (p > list && isdigit(p[-1])) + p--; + return isdigit(*p) ? atoi(p) : -1; +} + +/* Is @pid on a line of its own in @buf? */ +static bool pid_listed(const char *buf, pid_t pid) +{ + const char *p = buf; + char *end; + + while (*p) { + if (strtol(p, &end, 10) == pid && end != p && *end == '\n') + return true; + p = strchr(p, '\n'); + if (!p) + break; + p++; + } + return false; +} + +/* + * A migration that fails must not make a cgroup.procs reader miss a process. + * + * cgroup.procs keeps its task iterator between reads. Read one byte, so that + * the iterator waits on the last process, then fail a migration of that + * process: cpuset refuses tasks for a cgroup without CPUs. The process never + * left the cgroup, so the rest of the read must list it. + */ +static int test_cgcore_procs_failed_migration(const char *root) +{ + char *parent = NULL, *full = NULL, *empty = NULL, *cg = NULL; + char cpus[256], cpu[16], buf[4096]; + bool cpuset_enabled = false; + pid_t pids[3] = { 0 }; + int ret = KSFT_FAIL; + int fd = -1, i, last; + ssize_t len, n; + + if (cg_test_v1_named) + return KSFT_SKIP; + + parent = cg_name(root, "cg_test_fail"); + full = cg_name(root, "cg_test_fail/full"); + empty = cg_name(root, "cg_test_fail/empty"); + cg = cg_name(root, "cg_test_src"); + if (!parent || !full || !empty || !cg) + goto cleanup; + + /* + * Make "empty" a cgroup without CPUs: "parent" is a partition with one + * CPU, and its child partition "full" takes that CPU. + */ + ret = KSFT_SKIP; + if (cg_read_strstr(root, "cgroup.subtree_control", "cpuset")) { + if (cg_write(root, "cgroup.subtree_control", "+cpuset")) + goto cleanup; + cpuset_enabled = true; + } + if (cg_read(root, "cpuset.cpus.effective", cpus, sizeof(cpus))) + goto cleanup; + last = cpulist_last(cpus); + if (last <= 0) + goto cleanup; + snprintf(cpu, sizeof(cpu), "%d", last); + + if (cg_create(parent) || cg_write(parent, "cpuset.cpus", cpu) || + cg_write(parent, "cpuset.cpus.partition", "root") || + cg_read_strcmp(parent, "cpuset.cpus.partition", "root\n") || + cg_write(parent, "cgroup.subtree_control", "+cpuset") || + cg_create(full) || cg_write(full, "cpuset.cpus", cpu) || + cg_write(full, "cpuset.cpus.partition", "root") || + cg_read_strcmp(full, "cpuset.cpus.partition", "root\n") || + cg_create(empty) || + cg_read_strcmp(empty, "cpuset.cpus.effective", "\n")) + goto cleanup; + + ret = KSFT_FAIL; + if (cg_create(cg)) + goto cleanup; + + /* Move the processes in one by one, so that they are listed in order. */ + for (i = 0; i < 3; i++) { + pids[i] = fork(); + if (pids[i] < 0) + goto cleanup; + if (!pids[i]) { + while (1) + pause(); + } + if (cg_enter(cg, pids[i])) + goto cleanup; + } + + /* Without a failing migration there is nothing to test. */ + if (cg_enter(empty, pids[2]) != -ENOSPC) { + ret = KSFT_SKIP; + goto cleanup; + } + + fd = cg_open(cg, "cgroup.procs", O_RDONLY); + if (fd < 0) + goto cleanup; + if (read(fd, buf, 1) != 1) + goto cleanup; + len = 1; + + if (cg_enter(empty, pids[2]) != -ENOSPC) + goto cleanup; + + while ((n = read(fd, buf + len, sizeof(buf) - 1 - len)) > 0) + len += n; + if (n < 0) + goto cleanup; + buf[len] = '\0'; + + for (i = 0; i < 3; i++) + if (!pid_listed(buf, pids[i])) + goto cleanup; + + ret = KSFT_PASS; + +cleanup: + if (fd >= 0) + close(fd); + for (i = 0; i < 3; i++) { + if (pids[i] > 0) { + kill(pids[i], SIGKILL); + waitpid(pids[i], NULL, 0); + } + } + cg_destroy(cg); + cg_destroy(empty); + cg_destroy(full); + cg_destroy(parent); + if (cpuset_enabled) + cg_write(root, "cgroup.subtree_control", "-cpuset"); + free(cg); + free(empty); + free(full); + free(parent); + return ret; +} + /* * cgroup migration permission check should be performed based on the * credentials at the time of open instead of write. @@ -941,6 +1094,7 @@ struct corecg_test { T(test_cgcore_populated), T(test_cgcore_proc_migration), T(test_cgcore_thread_migration), + T(test_cgcore_procs_failed_migration), T(test_cgcore_destroy), T(test_cgcore_lesser_euid_open), T(test_cgcore_lesser_ns_open), -- 2.53.1-Meta