From 8ed5a2343f95beb77bc1fd35e4decf25bcbbf28c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Date: Wed, 19 Jul 2023 10:29:24 +0200 Subject: [PATCH 1/2] cpu: Take cgroupsv1 into account when reading misc.capacity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We've been only considering cgroupsv2 when trying to read misc.capacity. However, there are still a bunch of systems out there relying on cgroupsv1. Signed-off-by: Fabiano FidĂȘncio --- source/cpu/security_amd64.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/source/cpu/security_amd64.go b/source/cpu/security_amd64.go index 7422739f99..ae2c2f5349 100644 --- a/source/cpu/security_amd64.go +++ b/source/cpu/security_amd64.go @@ -124,13 +124,22 @@ func sevParameterEnabled(parameter string) bool { func getCgroupMiscCapacity(resource string) int64 { var totalResources int64 = -1 + var err error = nil + var f *os.File = nil + + miscCgroupsPaths := []string{"fs/cgroup/misc.capacity", "fs/cgroup/misc/misc.capacity"} + for _, miscCgroupsPath := range miscCgroupsPaths { + miscCgroups := hostpath.SysfsDir.Path(miscCgroupsPath) + f, err = os.Open(miscCgroups) + if err == nil { + defer f.Close() + break + } + } - miscCgroups := hostpath.SysfsDir.Path("fs/cgroup/misc.capacity") - f, err := os.Open(miscCgroups) if err != nil { return totalResources } - defer f.Close() r := bufio.NewReader(f) for { From 7532ac319271be5f1bc65385bbfe6c4a6af87e90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Date: Wed, 19 Jul 2023 11:17:37 +0200 Subject: [PATCH 2/2] cpu: Add retrieveCgroupMiscCapacityValue() for legibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let's refactor part of the getCgroupMiscCapacity() out to its own retrieveCgroupMiscCapacityValue(), for the legibility sake. Signed-off-by: Fabiano FidĂȘncio --- source/cpu/security_amd64.go | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/source/cpu/security_amd64.go b/source/cpu/security_amd64.go index ae2c2f5349..d1a0dce8f5 100644 --- a/source/cpu/security_amd64.go +++ b/source/cpu/security_amd64.go @@ -122,26 +122,10 @@ func sevParameterEnabled(parameter string) bool { return false } -func getCgroupMiscCapacity(resource string) int64 { +func retrieveCgroupMiscCapacityValue(miscCgroupPath *os.File, resource string) int64 { var totalResources int64 = -1 - var err error = nil - var f *os.File = nil - - miscCgroupsPaths := []string{"fs/cgroup/misc.capacity", "fs/cgroup/misc/misc.capacity"} - for _, miscCgroupsPath := range miscCgroupsPaths { - miscCgroups := hostpath.SysfsDir.Path(miscCgroupsPath) - f, err = os.Open(miscCgroups) - if err == nil { - defer f.Close() - break - } - } - if err != nil { - return totalResources - } - - r := bufio.NewReader(f) + r := bufio.NewReader(miscCgroupPath) for { line, _, err := r.ReadLine() if err != nil { @@ -167,3 +151,18 @@ func getCgroupMiscCapacity(resource string) int64 { return totalResources } + +func getCgroupMiscCapacity(resource string) int64 { + miscCgroupsPaths := []string{"fs/cgroup/misc.capacity", "fs/cgroup/misc/misc.capacity"} + for _, miscCgroupsPath := range miscCgroupsPaths { + miscCgroups := hostpath.SysfsDir.Path(miscCgroupsPath) + f, err := os.Open(miscCgroups) + if err == nil { + defer f.Close() + + return retrieveCgroupMiscCapacityValue(f, resource) + } + } + + return -1 +}