Skip to content

Commit 2cf45c5

Browse files
committed
common/pkg: error check IsCgroup2UnifiedMode and assume true
Signed-off-by: Lokesh Mandvekar <[email protected]>
1 parent 80309d7 commit 2cf45c5

File tree

4 files changed

+11
-54
lines changed

4 files changed

+11
-54
lines changed

common/pkg/cgroups/cgroups_linux.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -541,15 +541,10 @@ func cpusetCopyFileFromParent(dir, file string, cgroupv2 bool) ([]byte, error) {
541541

542542
// SystemCPUUsage returns the system usage for all the cgroups.
543543
func SystemCPUUsage() (uint64, error) {
544-
cgroupv2, err := IsCgroup2UnifiedMode()
544+
_, err := IsCgroup2UnifiedMode()
545545
if err != nil {
546546
return 0, err
547547
}
548-
if !cgroupv2 {
549-
p := filepath.Join(cgroupRoot, CPUAcct, "cpuacct.usage")
550-
return readFileAsUint64(p)
551-
}
552-
553548
files, err := os.ReadDir(cgroupRoot)
554549
if err != nil {
555550
return 0, err
@@ -602,7 +597,7 @@ func UserConnection(uid int) (*systemdDbus.Conn, error) {
602597
func UserOwnsCurrentSystemdCgroup() (bool, error) {
603598
uid := os.Geteuid()
604599

605-
cgroup2, err := IsCgroup2UnifiedMode()
600+
_, err := IsCgroup2UnifiedMode()
606601
if err != nil {
607602
return false, err
608603
}
@@ -624,20 +619,11 @@ func UserOwnsCurrentSystemdCgroup() (bool, error) {
624619

625620
// If we are on a cgroup v2 system and there are cgroup v1 controllers
626621
// mounted, ignore them when the current process is at the root cgroup.
627-
if cgroup2 && parts[1] != "" && parts[2] == "/" {
622+
if parts[1] != "" && parts[2] == "/" {
628623
continue
629624
}
630625

631-
var cgroupPath string
632-
633-
if cgroup2 {
634-
cgroupPath = filepath.Join(cgroupRoot, parts[2])
635-
} else {
636-
if parts[1] != "name=systemd" {
637-
continue
638-
}
639-
cgroupPath = filepath.Join(cgroupRoot, "systemd", parts[2])
640-
}
626+
cgroupPath := filepath.Join(cgroupRoot, parts[2])
641627

642628
st, err := os.Stat(cgroupPath)
643629
if err != nil {

common/pkg/cgroups/utils_linux.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"github.com/opencontainers/cgroups"
1717
"github.com/sirupsen/logrus"
18-
"go.podman.io/storage/pkg/fileutils"
1918
"golang.org/x/sys/unix"
2019
)
2120

@@ -207,7 +206,7 @@ func MoveUnderCgroup(cgroup, subtree string, processes []uint32) error {
207206
}
208207
defer f.Close()
209208

210-
unifiedMode, err := IsCgroup2UnifiedMode()
209+
_, err = IsCgroup2UnifiedMode()
211210
if err != nil {
212211
return err
213212
}
@@ -221,24 +220,12 @@ func MoveUnderCgroup(cgroup, subtree string, processes []uint32) error {
221220
}
222221

223222
// root cgroup, skip it
224-
if parts[2] == "/" && (!unifiedMode || parts[1] != "") {
223+
if parts[2] == "/" && parts[1] != "" {
225224
continue
226225
}
227226

228227
cgroupRoot := "/sys/fs/cgroup"
229-
// Special case the unified mount on hybrid cgroup and named hierarchies.
230-
// This works on Fedora 31, but we should really parse the mounts to see
231-
// where the cgroup hierarchy is mounted.
232-
if parts[1] == "" && !unifiedMode {
233-
// If it is not using unified mode, the cgroup v2 hierarchy is
234-
// usually mounted under /sys/fs/cgroup/unified
235-
cgroupRoot = filepath.Join(cgroupRoot, "unified")
236-
237-
// Ignore the unified mount if it doesn't exist
238-
if err := fileutils.Exists(cgroupRoot); err != nil && os.IsNotExist(err) {
239-
continue
240-
}
241-
} else if parts[1] != "" {
228+
if parts[1] != "" {
242229
// Assume the controller is mounted at /sys/fs/cgroup/$CONTROLLER.
243230
controller := strings.TrimPrefix(parts[1], "name=")
244231
cgroupRoot = filepath.Join(cgroupRoot, controller)
@@ -292,15 +279,11 @@ var (
292279
// it is running in the root cgroup on a system that uses cgroupv2.
293280
func MaybeMoveToSubCgroup() error {
294281
maybeMoveToSubCgroupSync.Do(func() {
295-
unifiedMode, err := IsCgroup2UnifiedMode()
282+
_, err := IsCgroup2UnifiedMode()
296283
if err != nil {
297284
maybeMoveToSubCgroupSyncErr = err
298285
return
299286
}
300-
if !unifiedMode {
301-
maybeMoveToSubCgroupSyncErr = nil
302-
return
303-
}
304287
cgroup, err := GetOwnCgroup()
305288
if err != nil {
306289
maybeMoveToSubCgroupSyncErr = err

common/pkg/sysinfo/sysinfo_linux.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,21 +229,11 @@ func checkCgroupCpusetInfo(cgMounts map[string]string, quiet bool) cgroupCpusetI
229229

230230
// checkCgroupPids reads the pids information from the pids cgroup mount point.
231231
func checkCgroupPids(cgMounts map[string]string, quiet bool) cgroupPids {
232-
cgroup2, err := cgroupv2.IsCgroup2UnifiedMode()
232+
_, err := cgroupv2.IsCgroup2UnifiedMode()
233233
if err != nil {
234234
logrus.Errorf("Failed to check cgroups version: %v", err)
235235
return cgroupPids{}
236236
}
237-
if !cgroup2 {
238-
_, ok := cgMounts["pids"]
239-
if !ok {
240-
if !quiet {
241-
logrus.Warn("Unable to find pids cgroup in mounts")
242-
}
243-
return cgroupPids{}
244-
}
245-
}
246-
247237
return cgroupPids{
248238
PidsLimit: true,
249239
}

common/pkg/systemd/systemd_linux.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,12 @@ func MovePauseProcessToScope(pausePidPath string) {
8888
}
8989

9090
if err != nil {
91-
unified, err2 := cgroups.IsCgroup2UnifiedMode()
91+
_, err2 := cgroups.IsCgroup2UnifiedMode()
9292
if err2 != nil {
9393
logrus.Warnf("Failed to detect if running with cgroup unified: %v", err)
9494
}
95-
if RunsOnSystemd() && unified {
95+
if RunsOnSystemd() {
9696
logrus.Warnf("Failed to add pause process to systemd sandbox cgroup: %v", err)
97-
} else {
98-
logrus.Debugf("Failed to add pause process to systemd sandbox cgroup: %v", err)
9997
}
10098
}
10199
}

0 commit comments

Comments
 (0)