Description: Read inherited cgroup v2 cpuset masks from ancestors
 A cgroup v2 leaf without cpuset enabled may not have effective mask
 files. Search its ancestors, up to the mount point, for the nearest
 available masks.
Origin: upstream, https://github.com/open-mpi/hwloc/commit/dc5714344a441be02080ed04cd389f0483a08cf1
Bug: https://github.com/open-mpi/hwloc/pull/841
Author: Guixiong Wei <weiguixiong@bytedance.com>
Last-Update: 2026-10-18

diff --git a/hwloc/topology-linux.c b/hwloc/topology-linux.c
index e0de289c9..6aafe42ca 100644
--- a/hwloc/topology-linux.c
+++ b/hwloc/topology-linux.c
@@ -2610,27 +2610,38 @@ hwloc_admin_disable_set_from_cgroup(int root_fd,
 {
 #define CPUSET_FILENAME_LEN 256
   char cpuset_filename[CPUSET_FILENAME_LEN];
+  const char *prefix = cgtype == HWLOC_LINUX_CPUSET ? "" : "cpuset.";
+  /* Keep using the configured masks for cgroup1; the core removes offline CPUs. */
+  const char *suffix = cgtype == HWLOC_LINUX_CGROUP2 ? ".effective" : "";
+  size_t cgrouplen = strlen(cpuset_name);
   int err;
 
-  switch (cgtype) {
-  case HWLOC_LINUX_CGROUP2:
-    /* try to read the cpuset from cgroup2. use the last "effective" mask to get a AND of parent masks */
-    snprintf(cpuset_filename, CPUSET_FILENAME_LEN, "%s%s/cpuset.%s.effective", mntpnt, cpuset_name, attr_name);
-    hwloc_debug("Trying to read cgroup2 file <%s>\n", cpuset_filename);
-    break;
-  case HWLOC_LINUX_CGROUP1:
-    /* try to read the cpuset from cgroup1. no need to use "effective_cpus/mems" since we'll remove offline CPUs in the core */
-    snprintf(cpuset_filename, CPUSET_FILENAME_LEN, "%s%s/cpuset.%s", mntpnt, cpuset_name, attr_name);
-    hwloc_debug("Trying to read cgroup1 file <%s>\n", cpuset_filename);
-    break;
-  case HWLOC_LINUX_CPUSET:
-    /* try to read the cpuset directly */
-    snprintf(cpuset_filename, CPUSET_FILENAME_LEN, "%s%s/%s", mntpnt, cpuset_name, attr_name);
+  for (;;) {
+    if (cgtype == HWLOC_LINUX_CGROUP2)
+      while (cgrouplen && cpuset_name[cgrouplen-1] == '/')
+        cgrouplen--;
+
+    err = snprintf(cpuset_filename, sizeof(cpuset_filename), "%s%.*s/%s%s%s",
+                   mntpnt, (int) cgrouplen, cpuset_name, prefix, attr_name, suffix);
+    if (err < 0 || (cgtype == HWLOC_LINUX_CGROUP2
+                   && (size_t) err >= sizeof(cpuset_filename))) {
+      err = -1;
+      break;
+    }
     hwloc_debug("Trying to read cpuset file <%s>\n", cpuset_filename);
-    break;
+    errno = 0;
+    err = hwloc__read_path_as_cpulist(cpuset_filename, admin_enabled_set, root_fd);
+    if (err >= 0 || cgtype != HWLOC_LINUX_CGROUP2 || errno != ENOENT || !cgrouplen)
+      break;
+
+    /* A cgroup without cpuset enabled inherits its ancestor's effective
+     * masks, but has no cpuset files of its own. Remove the last path
+     * component to try the parent cgroup. Stop at the mount point.
+     */
+    while (cgrouplen && cpuset_name[cgrouplen-1] != '/')
+      cgrouplen--;
   }
 
-  err = hwloc__read_path_as_cpulist(cpuset_filename, admin_enabled_set, root_fd);
   if (err < 0) {
     hwloc_debug("failed to read cpuset '%s' attribute '%s'\n", cpuset_name, attr_name);
     hwloc_bitmap_fill(admin_enabled_set);
