Skip to content

Commit

Permalink
libc/cgroups: mv cgroupv1 code to separate package
Browse files Browse the repository at this point in the history
... and convert its users.

It is now explicitly clear that this functionality is v1-specific and
must not be used from any cgroup v2 code.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed May 18, 2020
1 parent f3c3cab commit 1259ffa
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cgroups
package cgroupv1

import (
"bufio"
Expand All @@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/opencontainers/runc/libcontainer/cgroups"
)

// Code in this source file are specific to cgroup v1,
Expand All @@ -18,6 +20,8 @@ const (
)

var (
IsCgroup2UnifiedMode = cgroups.IsCgroup2UnifiedMode

errUnified = errors.New("not implemented for cgroup v2 unified hierarchy")
)

Expand Down
9 changes: 5 additions & 4 deletions libcontainer/cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/configs"
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
"github.com/pkg/errors"
Expand Down Expand Up @@ -190,7 +191,7 @@ func (m *manager) Apply(pid int) (err error) {
for name, path := range c.Paths {
_, err := d.path(name)
if err != nil {
if cgroups.IsNotFound(err) {
if cgroupv1.IsNotFound(err) {
continue
}
return err
Expand All @@ -205,7 +206,7 @@ func (m *manager) Apply(pid int) (err error) {
if err != nil {
// The non-presence of the devices subsystem is
// considered fatal for security reasons.
if cgroups.IsNotFound(err) && sys.Name() != "devices" {
if cgroupv1.IsNotFound(err) && sys.Name() != "devices" {
continue
}
return err
Expand Down Expand Up @@ -365,7 +366,7 @@ func getCgroupData(c *configs.Cgroup, pid int) (*cgroupData, error) {
}

func (raw *cgroupData) path(subsystem string) (string, error) {
mnt, err := cgroups.FindCgroupMountpoint(raw.root, subsystem)
mnt, err := cgroupv1.FindCgroupMountpoint(raw.root, subsystem)
// If we didn't mount the subsystem, there is no point we make the path.
if err != nil {
return "", err
Expand All @@ -380,7 +381,7 @@ func (raw *cgroupData) path(subsystem string) (string, error) {
// Use GetOwnCgroupPath instead of GetInitCgroupPath, because the creating
// process could in container and shared pid namespace with host, and
// /proc/1/cgroup could point to whole other world of cgroups.
parentPath, err := cgroups.GetOwnCgroupPath(subsystem)
parentPath, err := cgroupv1.GetOwnCgroupPath(subsystem)
if err != nil {
return "", err
}
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)
Expand All @@ -24,7 +25,7 @@ func (s *BlkioGroup) Name() string {

func (s *BlkioGroup) Apply(d *cgroupData) error {
_, err := d.join("blkio")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)
Expand All @@ -24,7 +25,7 @@ func (s *CpuGroup) Apply(d *cgroupData) error {
// We always want to join the cpu group, to allow fair cpu scheduling
// on a container basis
path, err := d.path("cpu")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
return s.ApplyDir(path, d.config, d.pid)
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/cpuacct.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)
Expand Down Expand Up @@ -42,7 +43,7 @@ func (s *CpuacctGroup) Name() string {

func (s *CpuacctGroup) Apply(d *cgroupData) error {
// we just want to join this group even though we don't set anything
if _, err := d.join("cpuacct"); err != nil && !cgroups.IsNotFound(err) {
if _, err := d.join("cpuacct"); err != nil && !cgroupv1.IsNotFound(err) {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/moby/sys/mountinfo"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
Expand All @@ -25,7 +26,7 @@ func (s *CpusetGroup) Name() string {

func (s *CpusetGroup) Apply(d *cgroupData) error {
dir, err := d.path("cpuset")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
return s.ApplyDir(dir, d.config, d.pid)
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
"golang.org/x/sys/unix"
Expand All @@ -24,7 +25,7 @@ func (s *FreezerGroup) Name() string {

func (s *FreezerGroup) Apply(d *cgroupData) error {
_, err := d.join("freezer")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/hugetlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)
Expand All @@ -21,7 +22,7 @@ func (s *HugetlbGroup) Name() string {

func (s *HugetlbGroup) Apply(d *cgroupData) error {
_, err := d.join("hugetlb")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
return nil
Expand Down
5 changes: 3 additions & 2 deletions libcontainer/cgroups/fs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)
Expand All @@ -39,7 +40,7 @@ func (s *MemoryGroup) Name() string {

func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
path, err := d.path("memory")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
} else if path == "" {
return nil
Expand Down Expand Up @@ -67,7 +68,7 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
// We need to join memory cgroup after set memory limits, because
// kmem.limit_in_bytes can only be set when the cgroup is empty.
_, err = d.join("memory")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/net_cls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)
Expand All @@ -19,7 +20,7 @@ func (s *NetClsGroup) Name() string {

func (s *NetClsGroup) Apply(d *cgroupData) error {
_, err := d.join("net_cls")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/net_prio.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package fs

import (
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)
Expand All @@ -17,7 +18,7 @@ func (s *NetPrioGroup) Name() string {

func (s *NetPrioGroup) Apply(d *cgroupData) error {
_, err := d.join("net_prio")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/perf_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package fs

import (
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/configs"
)

Expand All @@ -16,7 +17,7 @@ func (s *PerfEventGroup) Name() string {

func (s *PerfEventGroup) Apply(d *cgroupData) error {
// we just want to join this group even though we don't set anything
if _, err := d.join("perf_event"); err != nil && !cgroups.IsNotFound(err) {
if _, err := d.join("perf_event"); err != nil && !cgroupv1.IsNotFound(err) {
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/fs/pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
)
Expand All @@ -21,7 +22,7 @@ func (s *PidsGroup) Name() string {

func (s *PidsGroup) Apply(d *cgroupData) error {
_, err := d.join("pids")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
return nil
Expand Down
19 changes: 10 additions & 9 deletions libcontainer/cgroups/systemd/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

systemdDbus "github.com/coreos/go-systemd/v22/dbus"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/cgroups/fs"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -143,7 +144,7 @@ func (m *legacyManager) Apply(pid int) error {
_, err := getSubsystemPath(m.cgroups, name)
if err != nil {
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
if cgroups.IsNotFound(err) {
if cgroupv1.IsNotFound(err) {
continue
}
return err
Expand Down Expand Up @@ -214,7 +215,7 @@ func (m *legacyManager) Apply(pid int) error {
subsystemPath, err := getSubsystemPath(m.cgroups, s.Name())
if err != nil {
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
if cgroups.IsNotFound(err) {
if cgroupv1.IsNotFound(err) {
continue
}
return err
Expand Down Expand Up @@ -273,7 +274,7 @@ func joinCgroups(c *configs.Cgroup, pid int) error {
// let systemd handle this
case "cpuset":
path, err := getSubsystemPath(c, name)
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
s := &fs.CpusetGroup{}
Expand All @@ -291,7 +292,7 @@ func joinCgroups(c *configs.Cgroup, pid int) error {
}
// For other subsystems, omit the `not found` error
// because they are optional.
if !cgroups.IsNotFound(err) {
if !cgroupv1.IsNotFound(err) {
return err
}
}
Expand All @@ -302,12 +303,12 @@ func joinCgroups(c *configs.Cgroup, pid int) error {
}

func getSubsystemPath(c *configs.Cgroup, subsystem string) (string, error) {
mountpoint, err := cgroups.FindCgroupMountpoint(c.Path, subsystem)
mountpoint, err := cgroupv1.FindCgroupMountpoint(c.Path, subsystem)
if err != nil {
return "", err
}

initPath, err := cgroups.GetInitCgroup(subsystem)
initPath, err := cgroupv1.GetInitCgroup(subsystem)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -427,7 +428,7 @@ func (m *legacyManager) Set(container *configs.Config) error {
for _, sys := range legacySubsystems {
// Get the subsystem path, but don't error out for not found cgroups.
path, err := getSubsystemPath(container.Cgroups, sys.Name())
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}
if err := sys.Set(path, container.Cgroups); err != nil {
Expand All @@ -445,7 +446,7 @@ func (m *legacyManager) Set(container *configs.Config) error {

func setKernelMemory(c *configs.Cgroup) error {
path, err := getSubsystemPath(c, "memory")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return err
}

Expand Down Expand Up @@ -476,7 +477,7 @@ func (m *legacyManager) GetCgroups() (*configs.Cgroup, error) {

func (m *legacyManager) GetFreezerState() (configs.FreezerState, error) {
path, err := getSubsystemPath(m.cgroups, "freezer")
if err != nil && !cgroups.IsNotFound(err) {
if err != nil && !cgroupv1.IsNotFound(err) {
return configs.Undefined, err
}
freezer, err := legacySubsystems.Get("freezer")
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/intelrdt"
"github.com/opencontainers/runc/libcontainer/system"
Expand Down Expand Up @@ -1458,7 +1459,7 @@ func (c *linuxContainer) criuApplyCgroups(pid int, req *criurpc.CriuReq) error {
// the stuff below is cgroupv1-specific

path := fmt.Sprintf("/proc/%d/cgroup", pid)
cgroupsPaths, err := cgroups.ParseCgroupFile(path)
cgroupsPaths, err := cgroupv1.ParseCgroupFile(path)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions libcontainer/rootfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/moby/sys/mountinfo"
"github.com/mrunalp/fileutils"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/system"
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
Expand Down Expand Up @@ -428,12 +429,12 @@ func mountToRootfs(m *configs.Mount, rootfs, mountLabel string, enableCgroupns b
}

func getCgroupMounts(m *configs.Mount) ([]*configs.Mount, error) {
mounts, err := cgroups.GetCgroupMounts(false)
mounts, err := cgroupv1.GetCgroupMounts(false)
if err != nil {
return nil, err
}

cgroupPaths, err := cgroups.ParseCgroupFile("/proc/self/cgroup")
cgroupPaths, err := cgroupv1.ParseCgroupFile("/proc/self/cgroup")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -965,7 +966,7 @@ func mountNewCgroup(m *configs.Mount) error {
source = m.Source
)
if data == "systemd" {
data = cgroups.CgroupNamePrefix + data
data = cgroupv1.CgroupNamePrefix + data
source = "systemd"
}
if err := unix.Mount(source, m.Destination, m.Device, uintptr(m.Flags), data); err != nil {
Expand Down

0 comments on commit 1259ffa

Please sign in to comment.