From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796392299; b=MWEdUltnq/OpRXVxrc07lyNuPLSTtUt6MHZ0B+P1l0nSMcEGpXvUsXZvys1QnuoflJJO GYjOQWhdyBi+AvKxbTRNwhU849fmmX4Z5G6XE1zJH/R5uuWPI+z3FjDj+dEDKk8udDSbU 5652f7DXP52eNYQ8B+68Yj3UHl5YaX+/NbjL+vk/miP+BGPqu9X4Vuxil4xy8qwr0u0Is fT8GDHijQiDgro7N92rLW6hmfDmurlI/uU/9vbYg5+fim8T3kH9o0NJHuqPA9VnNKrhsV Ad1AuK87AgA/IgyG0PxbcgOjFO24zFXtNo+nnPCtJc3zWcLTYYhblLRoUKIYeAS7wuw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796392299; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=9zq0aHseOUuvGU5w4+9c2Cw6dcolDN3aBWD2uyzEwpA=; b=eiJZOg0F875qP4Da3nsKkfMxzy2nsU7/OsUvt5lrr/+us5HtM7WwwndYs8GjWanI8CAB ch7mVTTS5WGIz504Bg+rU1O2Lh73LqnLbGx3erb0NfF9SuELn7xGq5wTvUJG6FFDYHcH4 P6YwtrBZCLFPLeamzMBZrjZd1l8vNOq8pp/tMcjC3tGxJgth0hV+F0nH97OrpVJplRlJ7 CpKwkvh5Eeavcr4bB65eDKIE2WztduzjQUXxqgzaNVA2V/GmRGwdHd+7B+k0aNXYUNsP2 f+sjWDaIT5f7S63thf4OHzC6QMFpVD/iwuOf4D1dV2PLMh44MGHjYYZgf6q7LR5ClGA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=xSAEqb+X; arc=none smtp.remote-ip=74.125.82.43 Received: from mail-dl1-f43.google.com (mail-dl1-f43.google.com [74.125.82.43]) (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 83208606A32C for ; Fri, 4 Dec 2026 13:51:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 83208606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=xSAEqb+X Received: by mail-dl1-f43.google.com with SMTP id a92af1059eb24-1b5d5fdefe2so1038885c88.1 for ; Fri, 04 Dec 2026 05:51:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796392298; x=1796997098; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=9zq0aHseOUuvGU5w4+9c2Cw6dcolDN3aBWD2uyzEwpA=; b=xSAEqb+Xz/MrK0yL/Ny7u92JX11KX9uTv5Go30Qhp2OBrefDJlb1I64zAJBDXY1So0 MkTgnjjXHfYyd4m7m8N8FZ4MYEWsxiEGwnwboCz8wLcSHDxwSx7aiaxaBmqAB/Os9jkg CaG1UXTnc8x8Lx0Dov7BUtDhqnCtdRcI/KNg3rPpqj1GUgdaoX5U9wsGqVcEWsvFmxrO d91B14mlUgJ6b3uRN5T/RLEjZvQMvYyRHhueI7CoOnJ1E7Ur12AJ/KlgIBttAypSqHv7 f2cL8ssHttpkRy/wG/+VeYGvjTIjLq+Ez2ASXJSG9eoDuYnSH4aOCwMKaHkMPbMeorHx Q3jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796392298; x=1796997098; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=9zq0aHseOUuvGU5w4+9c2Cw6dcolDN3aBWD2uyzEwpA=; b=NacJbHXBzzFGerWfEFuCmjiIxsa6G9GywRSBsKZuY0xgZWSEkEX4eCl1LeO1x02QPx V91mzghcmVgmMH+tGWMRN5P4dZD2k7cbEIyVKl+1KdF6WcvG0yi5nn50vmXjPG29edfE c9n0o618nSY98H+vBKXA708HMLXO9qD6KSt3wr/hWtjl48IopquplfqCHbGD5H9VyFO4 xHJuIZcAWifsdZF33sYppaXTPh5q6hfdCImpcViJmi1kAMH37raGJmvJx67EcHddx294 YsLQckFNALmdaZUuCPJZxQQG5sL2zdOuF572u7Jo03HrufvXHnx2Fl1BB3yLGgdtGlLK NWOQ== X-Forwarded-Encrypted: i=1; AL+hErHPkd7JxzAXKdMYw/hTvitcqqHPxXYvvkHUoXCmxVMFhsPTrVGwRRa7RFRGTS70EgRi+jvDxK7zI+75A+4=@vger.kernel.org X-Gm-Message-State: AB4q+uG7dsenth53Ls+h962KBboKNTA2+Wd2090KVQ8i5uoSuFZ/Vs+8 KHDx1S/GSXYlGeb9FFGji3eTBdGP8f3hv52EN9Q/LD4sQjyUIh5OVAoi X-Gm-Gg: AZFdTZdWvyLps4KntjFYFansI31OrQmvoJp3YKfqugYJqrPRmjlmpXVJK1XVApH8obb eN1uSFwJmZvKb9vzvNwPAv5X0hVUlza03PLbj/R8peysmRGY2rXJM/ZSzAS/4ChDEPfwGwFmm78 1Z9glxETBBuL/GgCj4daPhlqEYIS2rq3MB43BVju8c7g5x2Xrs+lB8jSZMXon2Qdd92ivX6fts2 XWa3WIzaEVHXOsN/syNhlmdNDcDT39FqWgdoptDc0rI9LMmQvdmxt88Bmnw7YMBcFN+CiwH70r4 FHutes37U/ukxmH0ShenBP9VYtbyfMMkLrdJ4w2rvre5ETSN0BOxqXo7kEbYvsmUdoEnrwQgUiP jXHB97VfXB5us/uZHArQVW1P7WGUxvHfq5I6D0v5QWzv+CcPIJHDAZneczOEV4GWsU373BS3tsq Oyyycdm/E9UhiXst31fY+/UDJCyjju48dNVrT8VRds/O72EuSyvYcl5C3NlyxL0yIEo5VwybHwU d1i27KArJxP3y6fyQ4FRd5SyMtzlPLRILeihfofPW4M5zZVUJ467Z0nXHJH6A2qxpInxNCd0HrQ E7XWggOkalWtYVU1Uyc/Rm7vD1L8RDhwcNNp9qmemG5+pHKD4zGoKqUUUIiQvkfCYg== X-Received: by 2002:a05:7300:cd9a:b0:386:b526:24d1 with SMTP id 5a478bee46e88-38752139bf8mr2597417eec.9.1796392298330; Fri, 04 Dec 2026 05:51:38 -0800 (PST) Received: from 6610b1a2ef46 ([203.116.140.24]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3875222ff7fsm8972759eec.25.2026.12.04.05.51.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 05:51:37 -0800 (PST) From: Hang Zhengyang To: Waiman Long , Ridong Chen , Tejun Heo Cc: Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Shuah Khan , Christian Brauner , Matthew Ruffell , Caron Wills , cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH cgroup/for-7.4-fixes 2/2] selftests/cgroup: Test CLONE_INTO_CGROUP into a cpuset with other mems Date: Fri, 4 Dec 2026 13:51:26 +0000 Message-ID: <20261204135126.598380-3-hang.zhengyang1010@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261204135126.598380-1-hang.zhengyang1010@gmail.com> References: <20261204135126.598380-1-hang.zhengyang1010@gmail.com> 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 Add two tests that create a child with CLONE_INTO_CGROUP in a cpuset whose mems differ from the parent's: one with a fork-like clone3(), and one with posix_spawn() and a cgroup attribute, which adds CLONE_VM and CLONE_VFORK. Both check that the memory policy and the pages of a parent region bound to the parent's node stay as they are. They need two memory nodes, and the posix_spawn() test needs glibc 2.39 or later; they are skipped otherwise. Signed-off-by: Hang Zhengyang --- tools/testing/selftests/cgroup/test_cpuset.c | 201 +++++++++++++++++++ 1 file changed, 201 insertions(+) diff --git a/tools/testing/selftests/cgroup/test_cpuset.c b/tools/testing/selftests/cgroup/test_cpuset.c index 3dfadd280c1c..9bc9fee37e2e 100644 --- a/tools/testing/selftests/cgroup/test_cpuset.c +++ b/tools/testing/selftests/cgroup/test_cpuset.c @@ -2,11 +2,19 @@ #define _GNU_SOURCE #include +#include #include +#include #include #include #include +#include +#include +#include +#include +#include #include +#include #include #include "kselftest.h" @@ -479,6 +487,197 @@ static int test_cpuset_affinity_on_controller_disable(const char *root) } +/* Find the first two memory nodes in a list like "0-1" or "0,2". */ +static int two_memory_nodes(int *n0, int *n1) +{ + char buf[256], *p; + + if (read_text("/sys/devices/system/node/has_memory", buf, sizeof(buf)) <= 0) + return -1; + *n0 = strtol(buf, &p, 10); + if (p == buf || (*p != '-' && *p != ',')) + return -1; + if (*p == '-') { + *n1 = *n0 + 1; + return 0; + } + *n1 = strtol(p + 1, NULL, 10); + return 0; +} + +static int page_node(void *addr) +{ + int node = -1; + + if (syscall(__NR_get_mempolicy, &node, NULL, 0, addr, + MPOL_F_NODE | MPOL_F_ADDR)) + return -1; + return node; +} + +#define MAX_NODES 1024 /* get_mempolicy() wants room for all nodes */ + +/* Get the policy of the region at @addr: its mode and its first nodes */ +static int region_policy(void *addr, int *mode, unsigned long *nodes) +{ + unsigned long mask[MAX_NODES / (8 * sizeof(unsigned long))] = { 0 }; + size_t i; + + if (syscall(__NR_get_mempolicy, mode, mask, MAX_NODES, addr, + MPOL_F_ADDR)) + return -1; + for (i = 1; i < ARRAY_SIZE(mask); i++) + if (mask[i]) + return -1; + *nodes = mask[0]; + return 0; +} + +/* + * Create a child in the cgroup of @cgfd, as fork() or as posix_spawn() does. + * Return its pid, or 0 if it is gone already. + */ +static pid_t start_child_in_cgroup(int cgfd, bool spawn) +{ + pid_t pid; + + if (spawn) { +#ifdef POSIX_SPAWN_SETCGROUP + static char *const argv[] = { "true", NULL }; + posix_spawnattr_t attr; + int err; + + posix_spawnattr_init(&attr); + posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETCGROUP); + posix_spawnattr_setcgroup_np(&attr, cgfd); + err = posix_spawnp(&pid, "true", NULL, &attr, argv, environ); + posix_spawnattr_destroy(&attr); + /* Without true(1), the child was created and failed to exec */ + if (err == ENOENT) + return 0; + if (err) { + errno = err; + return -1; + } + return pid; +#else + errno = EOPNOTSUPP; + return -1; +#endif + } + + pid = clone_into_cgroup(cgfd); + if (pid == 0) + _exit(0); + return pid; +} + +/* + * A child created with CLONE_INTO_CGROUP in a cpuset with other memory nodes + * must leave the memory of its parent alone, both after a fork, where the two + * share pages, and after a posix_spawn() with a cgroup attribute, which uses + * CLONE_VM | CLONE_VFORK, where they share the mm. + */ +static int clone_into_other_mems(const char *root, bool spawn) +{ + char *parent = NULL, *child_a = NULL, *child_b = NULL; + size_t i, size = 4 << 20, step = sysconf(_SC_PAGESIZE); + char *mem = MAP_FAILED, nodes_a[16], nodes_b[16]; + int ret = KSFT_FAIL, cgfd = -1, mode, n0, n1; + unsigned long nodes, bound; + pid_t pid; + + if (two_memory_nodes(&n0, &n1) || n1 >= 8 * sizeof(bound)) + return KSFT_SKIP; + + parent = cg_name(root, "cpuset_clone_test"); + child_a = cg_name(parent, "A"); + child_b = cg_name(parent, "B"); + if (!parent || !child_a || !child_b) + goto cleanup; + if (cg_create(parent) || cg_write(parent, "cgroup.subtree_control", "+cpuset")) + goto cleanup; + if (cg_create(child_a) || cg_create(child_b)) + goto cleanup; + snprintf(nodes_a, sizeof(nodes_a), "%d", n0); + snprintf(nodes_b, sizeof(nodes_b), "%d", n1); + if (cg_write(child_a, "cpuset.mems", nodes_a) || + cg_write(child_b, "cpuset.mems", nodes_b)) + goto cleanup; + if (cg_enter_current(child_a)) + goto cleanup; + + /* Bind the memory to n0 and fault it in there */ + mem = mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (mem == MAP_FAILED) + goto cleanup; + bound = 1UL << n0; + if (syscall(__NR_mbind, mem, size, MPOL_BIND, &bound, + 8 * sizeof(bound), 0)) + goto cleanup; + memset(mem, 1, size); + if (page_node(mem) != n0) { + ret = KSFT_SKIP; + goto cleanup; + } + + cgfd = open(child_b, O_DIRECTORY | O_RDONLY); + if (cgfd < 0) + goto cleanup; + pid = start_child_in_cgroup(cgfd, spawn); + if (pid < 0) { + if (errno == ENOSYS || errno == EOPNOTSUPP) + ret = KSFT_SKIP; + goto cleanup; + } + if (pid > 0) + waitpid(pid, NULL, 0); + + if (region_policy(mem, &mode, &nodes)) + goto cleanup; + if (mode != MPOL_BIND || nodes != bound) { + ksft_print_msg("FAIL: parent memory policy changed to mode %d nodes %#lx\n", + mode, nodes); + goto cleanup; + } + /* A write to cpuset.mems waits for queued migrations to finish */ + if (cg_write(child_b, "cpuset.mems", nodes_b)) + goto cleanup; + for (i = 0; i < size; i += step) { + if (page_node(mem + i) != n0) { + ksft_print_msg("FAIL: parent memory moved to node %d\n", + page_node(mem + i)); + goto cleanup; + } + } + ret = KSFT_PASS; + +cleanup: + cg_enter_current(root); + if (cgfd >= 0) + close(cgfd); + if (mem != MAP_FAILED) + munmap(mem, size); + cg_destroy(child_b); + free(child_b); + cg_destroy(child_a); + free(child_a); + cg_destroy(parent); + free(parent); + return ret; +} + +static int test_cpuset_fork_into_other_mems(const char *root) +{ + return clone_into_other_mems(root, false); +} + +static int test_cpuset_spawn_into_other_mems(const char *root) +{ + return clone_into_other_mems(root, true); +} + #define T(x) { x, #x } struct cpuset_test { int (*fn)(const char *root); @@ -488,6 +687,8 @@ struct cpuset_test { T(test_cpuset_perms_object_deny), T(test_cpuset_perms_subtree), T(test_cpuset_affinity_on_controller_disable), + T(test_cpuset_fork_into_other_mems), + T(test_cpuset_spawn_into_other_mems), }; #undef T -- 2.53.0