From 2510fc85b0920a89e16b9fd19beffa570d6028c2 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Wed, 24 Apr 2024 10:01:24 +0000 Subject: [PATCH] Recursively check cgroups.controllers files for memory controllers --- base/base/cgroupsv2.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/base/base/cgroupsv2.cpp b/base/base/cgroupsv2.cpp index 1686c6bd88c..c8081d986a9 100644 --- a/base/base/cgroupsv2.cpp +++ b/base/base/cgroupsv2.cpp @@ -23,18 +23,19 @@ bool cgroupsV2MemoryControllerEnabled() { #if defined(OS_LINUX) chassert(cgroupsV2Enabled()); - /// According to https://docs.kernel.org/admin-guide/cgroup-v2.html: - /// - file 'cgroup.controllers' defines which controllers *can* be enabled - /// - file 'cgroup.subtree_control' defines which controllers *are* enabled - /// Caveat: nested groups may disable controllers. For simplicity, check only the top-level group. - std::ifstream subtree_control_file(default_cgroups_mount / "cgroup.subtree_control"); - if (!subtree_control_file.is_open()) + /// According to https://docs.kernel.org/admin-guide/cgroup-v2.html, file "cgroup.controllers" defines + /// which controllers are enabled for the current cgroup. Check the bottom-most nested "cgroup.controllers" + /// file. + std::string cgroup = cgroupV2OfProcess(); + auto cgroup_dir = cgroup.empty() ? default_cgroups_mount : (default_cgroups_mount / cgroup); + std::ifstream controllers_file(cgroup_dir / "cgroup.controllers"); + if (!controllers_file.is_open()) return false; std::string controllers; - std::getline(subtree_control_file, controllers); - if (controllers.find("memory") == std::string::npos) - return false; - return true; + std::getline(controllers_file, controllers); + if (controllers.find("memory") != std::string::npos) + return true; + return false; #else return false; #endif