Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.30 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060127; b=scqTYJuIrQA2a0VTI85gnai/WX9b8jspy061klWMTmzsiANdtdOTT2NL86x5BH8hxUPi 4thq2IL2IWlTr3qS79LgPH0JHZDZO4LYO/gxuRUDltR6r5p6bMgY5mIePuCB3UNEcpyfH f1+rI4X+SLJlb5aa5UsfM+VNNcnIQ/LXtsK0Q/Q2Y+MbuOcjxKPps5G6m7NjJT2CH2tKg 9gTP/rJEncEXHAINISQIMUuzHOpXUUpPIyKRNXgo99LpOrDyIjOfx79Ui++I5iTAf0/so K+XBwteqPMLx4hIvWbyi26mGWi86byQgnPl9P7nbMuL8z8awEDFY1vXkg16LHxwvx/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060127; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=AOW2VPmGGdscfP0LMhHIqZbTD8FNIQIR4q0ekjUhuAs=; b=EQ5e4VMX+UjNjr2DpPCfAmoy1rqFS0jUwmJyrDAQ+t9doyqzsgi7/Of8APU6m7HcwsiT AXAJk71EwajCHa1jd57cgMA3LUBUYiPa75PcD69Nw9Jwvb+XXelMqxuXwFpzoM2WwdKtd 9sqHCsxiMs1vgko1IGJAEP+tmzfB8G28CtNFzPHOQpW0f0L+IBf0bA+trGnAEpASNGsUH dBBX1R1i5wMIr1rahGRtUaYYnXyaRgCKxOPNiCRTvkcGg7GgO2QGzNpyh6BvpRZbINW0l VerJhzvmyuPypC+UDcOG8zzBzSKRCE33P0NG8XCu/tY7Mvq1P/9+M90gnOmJOV9/WLw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=IOiVt86D; arc=none smtp.remote-ip=91.218.175.30 Received: from mta0.migadu.com (out-30.mta0.migadu.com [91.218.175.30]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7CE96606A681 for ; Mon, 30 Nov 2026 17:35:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7CE96606A681 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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 X-Envelope-To: cgroups@vger.kernel.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: cgroups@vger.kernel.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-Mailing-List: cgroups@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 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