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 16E11606A325 for ; Mon, 30 Nov 2026 17:35:32 +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=IOiVt86D Received: by kanga.kvack.org (Postfix) id 630816B05CA; Mon, 30 Nov 2026 12:35:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E1616B05CC; Mon, 30 Nov 2026 12:35:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F73C6B05CD; Mon, 30 Nov 2026 12:35:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B83D36B05CA for ; Mon, 30 Nov 2026 12:35:26 -0500 (EST) Received: from smtpin28.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4CAF6A7BC4 for ; Mon, 30 Nov 2026 17:35:26 +0000 (UTC) X-FDA: 85492487052.28.4A8746D Received: from mta1.migadu.com (out-164.mta1.migadu.com [95.215.58.164]) by imf11.hostedemail.com (Postfix) with ESMTP id 28BCD4000A for ; Mon, 30 Nov 2026 17:35:23 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=IOiVt86D; spf=pass (imf11.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.164 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=1796060124; b=XQSXe4vreoYESYWqS24OG5xJQ0KXng/RAZSihO+Ro4OcSjrdE7g+4U/Msk2Rt1cxB3ADmK gUWAkkT2CVYKIaIcc45K0eTiyzqbp+bMLnVqH4jQ+0D6iTrpL1XKboE16vl7eeqqRy5d5s 7BZ0vnSYRfLiv5HFt2zcq+P+GF3Wd64= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796060124; 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=EVrj8tAMpRwbzGDUcIRBOBhuCjaGTt1jpdss2I0JlMF+KpcvdoVA4TvnZkpDplFW0VWTYi S++HT1J15VL2qeDm8eWoa/brp1CWRv0ShrcoS+0ngiFhdvZgIumIhH1nAHZ5LylV5eMplQ Fzyl8Rp6aT7tv2pBz1Fhz3tsgXtVKk0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=IOiVt86D; spf=pass (imf11.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.164 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=1796060122; v=1; x=1796664922; b=IOiVt86Di2H1lqjceoEubTA+fMtzVlav86llyjMD6HNfM2ikrV1X943wwyfZVN30ToaPUq7P 50s2L+xNUddsBqu/jOpujFpKMbVPmUIvMhzU6t0UByQ88iSQlZ7tMixheIQvAzE7I57tohvFi2/ X3bEepbNQuD9KeiNuQKenI/8= X-Envelope-To: linux-mm@kvack.org Received: by smtp.migadu.com with ESMTPS id 06ec0f8c036b55a1; Mon, 30 Nov 2026 17:35:17 +0000 X-Mizu-Trace-ID: 06ec0f8c036b55a1 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 v2 06/11] selftests/cgroup: test cgroup.procs reads across a failed migration Date: Mon, 30 Nov 2026 09:34:20 -0800 Message-ID: <20261130173425.4117363-7-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261130173425.4117363-1-shakeel.butt@linux.dev> References: <20261130173425.4117363-1-shakeel.butt@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 28BCD4000A X-Stat-Signature: rtabbjuxxxeewurjqmxc9gwie45gicgd X-Spam-Status: No, score=-18.8 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: 1796060123-955498 X-HE-Meta: U2FsdGVkX1/Uo5q07PP6qrgjahWMcTBcYkJpWPw2SpNTRqQlYMCxeSkFQ9KHt67h1c6H4dgLyouFVBc4MbdH0PzMh7vxmVUQ88oMRdb2e2UmEtwJ1uxKqiLUPNkf7iSBkpQeMCkqcGSi+JU2Z6YQxxmWNZrJ5VdUJi/qLI8s8d54Z9XuPXIKd1UorRDHxPiS7pDnXIiFAr1kig6OkAk24F1EgfTehWRa7unDOe/rKDNflOiAg9RC+k0BfBwGNJcws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hnVROSr3sBSCARZ2CQSIyGn5P0PMQRhElZD3nminr8dkQU9O86oZIVDg2oMNyWnPrGb34zqqcdHYSZLr6Oi2CH6IGondEHY28vMdW6wv/TS7 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