Skip to content

Commit 0054c15

Browse files
committed
8253435: Cgroup: 'stomping of _mount_path' crash if manually mounted cpusets exist
Reviewed-by: sgehwolf, bobv
1 parent 8e338f6 commit 0054c15

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/hotspot/os/linux/cgroupSubsystem_linux.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,22 @@ bool CgroupSubsystemFactory::determine_type(CgroupInfo* cg_infos,
302302
cg_infos[MEMORY_IDX]._root_mount_path = os::strdup(tmproot);
303303
cg_infos[MEMORY_IDX]._data_complete = true;
304304
} else if (strcmp(token, "cpuset") == 0) {
305-
assert(cg_infos[CPUSET_IDX]._mount_path == NULL, "stomping of _mount_path");
306-
cg_infos[CPUSET_IDX]._mount_path = os::strdup(tmpmount);
305+
if (cg_infos[CPUSET_IDX]._mount_path != NULL) {
306+
// On some systems duplicate cpuset controllers get mounted in addition to
307+
// the main cgroup controllers most likely under /sys/fs/cgroup. In that
308+
// case pick the one under /sys/fs/cgroup and discard others.
309+
if (strstr(cg_infos[CPUSET_IDX]._mount_path, "/sys/fs/cgroup") != cg_infos[CPUSET_IDX]._mount_path) {
310+
log_warning(os, container)("Duplicate cpuset controllers detected. Picking %s, skipping %s.",
311+
tmpmount, cg_infos[CPUSET_IDX]._mount_path);
312+
os::free(cg_infos[CPUSET_IDX]._mount_path);
313+
cg_infos[CPUSET_IDX]._mount_path = os::strdup(tmpmount);
314+
} else {
315+
log_warning(os, container)("Duplicate cpuset controllers detected. Picking %s, skipping %s.",
316+
cg_infos[CPUSET_IDX]._mount_path, tmpmount);
317+
}
318+
} else {
319+
cg_infos[CPUSET_IDX]._mount_path = os::strdup(tmpmount);
320+
}
307321
cg_infos[CPUSET_IDX]._root_mount_path = os::strdup(tmproot);
308322
cg_infos[CPUSET_IDX]._data_complete = true;
309323
} else if (strcmp(token, "cpu") == 0) {

test/hotspot/jtreg/containers/cgroup/CgroupSubsystemFactory.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public class CgroupSubsystemFactory {
6060
private Path cgroupv1CgInfoNonZeroHierarchy;
6161
private Path cgroupv1MntInfoNonZeroHierarchyOtherOrder;
6262
private Path cgroupv1MntInfoNonZeroHierarchy;
63+
private Path cgroupv1MntInfoDoubleCpuset;
64+
private Path cgroupv1MntInfoDoubleCpuset2;
6365
private String mntInfoEmpty = "";
6466
private Path cgroupV1SelfCgroup;
6567
private Path cgroupV2SelfCgroup;
@@ -102,11 +104,14 @@ public class CgroupSubsystemFactory {
102104
"41 30 0:37 / /sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime shared:13 - cgroup none rw,seclabel,devices\n" +
103105
"42 30 0:38 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:14 - cgroup none rw,seclabel,cpuset\n" +
104106
"43 30 0:39 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime shared:15 - cgroup none rw,seclabel,blkio\n" +
105-
"44 30 0:40 / /sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime shared:16 - cgroup none rw,seclabel,freezer";
107+
"44 30 0:40 / /sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime shared:16 - cgroup none rw,seclabel,freezer\n";
106108
private String mntInfoHybridRest = cgroupv1MountInfoLineMemory + mntInfoHybridStub;
107109
private String mntInfoHybridMissingMemory = mntInfoHybridStub;
108110
private String mntInfoHybrid = cgroupV2LineHybrid + mntInfoHybridRest;
109111
private String mntInfoHybridFlippedOrder = mntInfoHybridRest + cgroupV2LineHybrid;
112+
private String mntInfoCgroupv1MoreCpusetLine = "121 32 0:37 / /cpusets rw,relatime shared:69 - cgroup none rw,cpuset\n";
113+
private String mntInfoCgroupv1DoubleCpuset = mntInfoCgroupv1MoreCpusetLine + mntInfoHybrid;
114+
private String mntInfoCgroupv1DoubleCpuset2 = mntInfoHybrid + mntInfoCgroupv1MoreCpusetLine;
110115
private String cgroupsNonZeroHierarchy =
111116
"#subsys_name hierarchy num_cgroups enabled\n" +
112117
"cpuset 3 1 1\n" +
@@ -157,6 +162,12 @@ private void setup() {
157162

158163
cgroupV2MntInfoMissingCgroupv2 = Paths.get(existingDirectory.toString(), "mnt_info_missing_cgroup2");
159164
Files.writeString(cgroupV2MntInfoMissingCgroupv2, mntInfoHybridStub);
165+
166+
cgroupv1MntInfoDoubleCpuset = Paths.get(existingDirectory.toString(), "mnt_info_cgroupv1_double_cpuset");
167+
Files.writeString(cgroupv1MntInfoDoubleCpuset, mntInfoCgroupv1DoubleCpuset);
168+
169+
cgroupv1MntInfoDoubleCpuset2 = Paths.get(existingDirectory.toString(), "mnt_info_cgroupv1_double_cpuset2");
170+
Files.writeString(cgroupv1MntInfoDoubleCpuset2, mntInfoCgroupv1DoubleCpuset2);
160171
} catch (IOException e) {
161172
throw new RuntimeException(e);
162173
}
@@ -174,6 +185,16 @@ private boolean isValidCgroup(int value) {
174185
return value == CGROUPS_V1 || value == CGROUPS_V2;
175186
}
176187

188+
public void testCgroupv1MultipleCpusetMounts(WhiteBox wb, Path mountInfo) {
189+
String procCgroups = cgroupv1CgInfoNonZeroHierarchy.toString();
190+
String procSelfCgroup = cgroupV1SelfCgroup.toString();
191+
String procSelfMountinfo = mountInfo.toString();
192+
int retval = wb.validateCgroup(procCgroups, procSelfCgroup, procSelfMountinfo);
193+
Asserts.assertEQ(CGROUPS_V1, retval, "Multiple cpuset controllers, but only one in /sys/fs/cgroup");
194+
Asserts.assertTrue(isValidCgroup(retval));
195+
System.out.println("testCgroupv1MultipleCpusetMounts PASSED!");
196+
}
197+
177198
public void testCgroupv1NoMounts(WhiteBox wb) {
178199
String procCgroups = cgroupv1CgInfoZeroHierarchy.toString();
179200
String procSelfCgroup = cgroupV1SelfCgroup.toString();
@@ -246,6 +267,8 @@ public static void main(String[] args) throws Exception {
246267
test.testCgroupV1HybridMntInfoOrder(wb);
247268
test.testCgroupv1MissingMemoryController(wb);
248269
test.testCgroupv2NoCgroup2Fs(wb);
270+
test.testCgroupv1MultipleCpusetMounts(wb, test.cgroupv1MntInfoDoubleCpuset);
271+
test.testCgroupv1MultipleCpusetMounts(wb, test.cgroupv1MntInfoDoubleCpuset2);
249272
} finally {
250273
test.teardown();
251274
}

0 commit comments

Comments
 (0)