2020-05-04 20:46:48 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package fs2
|
|
|
|
|
|
|
|
import (
|
2021-04-14 18:11:13 +00:00
|
|
|
"fmt"
|
2020-05-04 20:46:48 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
2021-04-14 18:11:13 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
2020-05-04 20:46:48 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
type manager struct {
|
|
|
|
config *configs.Cgroup
|
|
|
|
// dirPath is like "/sys/fs/cgroup/user.slice/user-1001.slice/session-1.scope"
|
|
|
|
dirPath string
|
|
|
|
// controllers is content of "cgroup.controllers" file.
|
|
|
|
// excludes pseudo-controllers ("devices" and "freezer").
|
|
|
|
controllers map[string]struct{}
|
|
|
|
rootless bool
|
|
|
|
}
|
|
|
|
|
2020-05-04 20:46:48 +00:00
|
|
|
// NewManager creates a manager for cgroup v2 unified hierarchy.
|
|
|
|
// dirPath is like "/sys/fs/cgroup/user.slice/user-1001.slice/session-1.scope".
|
|
|
|
// If dirPath is empty, it is automatically set using config.
|
|
|
|
func NewManager(config *configs.Cgroup, dirPath string, rootless bool) (cgroups.Manager, error) {
|
|
|
|
if config == nil {
|
|
|
|
config = &configs.Cgroup{}
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
if dirPath == "" {
|
2020-05-04 20:46:48 +00:00
|
|
|
var err error
|
|
|
|
dirPath, err = defaultDirPath(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &manager{
|
2020-08-10 17:43:49 +00:00
|
|
|
config: config,
|
|
|
|
dirPath: dirPath,
|
|
|
|
rootless: rootless,
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (m *manager) getControllers() error {
|
|
|
|
if m.controllers != nil {
|
|
|
|
return nil
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
data, err := cgroups.ReadFile(m.dirPath, "cgroup.controllers")
|
2020-05-04 20:46:48 +00:00
|
|
|
if err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
if m.rootless && m.config.Path == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
2021-04-14 18:11:13 +00:00
|
|
|
fields := strings.Fields(data)
|
2020-08-10 17:43:49 +00:00
|
|
|
m.controllers = make(map[string]struct{}, len(fields))
|
|
|
|
for _, c := range fields {
|
|
|
|
m.controllers[c] = struct{}{}
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
return nil
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) Apply(pid int) error {
|
2020-08-10 17:43:49 +00:00
|
|
|
if err := CreateCgroupPath(m.dirPath, m.config); err != nil {
|
|
|
|
// Related tests:
|
|
|
|
// - "runc create (no limits + no cgrouppath + no permission) succeeds"
|
|
|
|
// - "runc create (rootless + no limits + cgrouppath + no permission) fails with permission error"
|
|
|
|
// - "runc create (rootless + limits + no cgrouppath + no permission) fails with informative error"
|
|
|
|
if m.rootless {
|
|
|
|
if m.config.Path == "" {
|
2021-06-18 20:46:09 +00:00
|
|
|
if blNeed, nErr := needAnyControllers(m.config.Resources); nErr == nil && !blNeed {
|
2020-08-10 17:43:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "rootless needs no limits + no cgrouppath when no permission is granted for cgroups")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := cgroups.WriteCgroupProc(m.dirPath, pid); err != nil {
|
2020-05-04 20:46:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) GetPids() ([]int, error) {
|
|
|
|
return cgroups.GetPids(m.dirPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) GetAllPids() ([]int, error) {
|
|
|
|
return cgroups.GetAllPids(m.dirPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) GetStats() (*cgroups.Stats, error) {
|
2021-07-02 08:43:15 +00:00
|
|
|
var errs []error
|
2020-08-10 17:43:49 +00:00
|
|
|
|
|
|
|
st := cgroups.NewStats()
|
|
|
|
|
2020-05-04 20:46:48 +00:00
|
|
|
// pids (since kernel 4.5)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := statPids(m.dirPath, st); err != nil {
|
|
|
|
errs = append(errs, err)
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
// memory (since kernel 4.5)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := statMemory(m.dirPath, st); err != nil && !os.IsNotExist(err) {
|
|
|
|
errs = append(errs, err)
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
// io (since kernel 4.5)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := statIo(m.dirPath, st); err != nil && !os.IsNotExist(err) {
|
|
|
|
errs = append(errs, err)
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
// cpu (since kernel 4.15)
|
2021-06-18 20:46:09 +00:00
|
|
|
// Note cpu.stat is available even if the controller is not enabled.
|
|
|
|
if err := statCpu(m.dirPath, st); err != nil && !os.IsNotExist(err) {
|
|
|
|
errs = append(errs, err)
|
2020-08-10 17:43:49 +00:00
|
|
|
}
|
|
|
|
// hugetlb (since kernel 5.6)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := statHugeTlb(m.dirPath, st); err != nil && !os.IsNotExist(err) {
|
|
|
|
errs = append(errs, err)
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
if len(errs) > 0 && !m.rootless {
|
2020-08-10 17:43:49 +00:00
|
|
|
return st, errors.Errorf("error while statting cgroup v2: %+v", errs)
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
return st, nil
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) Freeze(state configs.FreezerState) error {
|
|
|
|
if err := setFreezer(m.dirPath, state); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.config.Resources.Freezer = state
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (m *manager) Destroy() error {
|
2021-04-14 18:11:13 +00:00
|
|
|
return cgroups.RemovePath(m.dirPath)
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (m *manager) Path(_ string) string {
|
|
|
|
return m.dirPath
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 20:46:09 +00:00
|
|
|
func (m *manager) Set(r *configs.Resources) error {
|
2020-08-10 17:43:49 +00:00
|
|
|
if err := m.getControllers(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-04 20:46:48 +00:00
|
|
|
// pids (since kernel 4.5)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := setPids(m.dirPath, r); err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
return err
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
// memory (since kernel 4.5)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := setMemory(m.dirPath, r); err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
return err
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
// io (since kernel 4.5)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := setIo(m.dirPath, r); err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
return err
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
// cpu (since kernel 4.15)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := setCpu(m.dirPath, r); err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
return err
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
// devices (since kernel 4.15, pseudo-controller)
|
2020-08-10 17:43:49 +00:00
|
|
|
//
|
2021-06-18 20:46:09 +00:00
|
|
|
// When m.rootless is true, errors from the device subsystem are ignored because it is really not expected to work.
|
2020-08-10 17:43:49 +00:00
|
|
|
// However, errors from other subsystems are not ignored.
|
|
|
|
// see @test "runc create (rootless + limits + no cgrouppath + no permission) fails with informative error"
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := setDevices(m.dirPath, r); err != nil && !m.rootless {
|
2020-08-10 17:43:49 +00:00
|
|
|
return err
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
// cpuset (since kernel 5.0)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := setCpuset(m.dirPath, r); err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// hugetlb (since kernel 5.6)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := setHugeTlb(m.dirPath, r); err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
return err
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
// freezer (since kernel 5.2, pseudo-controller)
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := setFreezer(m.dirPath, r.Freezer); err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
return err
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
2021-06-18 20:46:09 +00:00
|
|
|
if err := m.setUnified(r.Unified); err != nil {
|
2021-04-14 18:11:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-06-18 20:46:09 +00:00
|
|
|
m.config.Resources = r
|
2020-05-04 20:46:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:11:13 +00:00
|
|
|
func (m *manager) setUnified(res map[string]string) error {
|
|
|
|
for k, v := range res {
|
|
|
|
if strings.Contains(k, "/") {
|
|
|
|
return fmt.Errorf("unified resource %q must be a file name (no slashes)", k)
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
if err := cgroups.WriteFile(m.dirPath, k, v); err != nil {
|
2021-04-14 18:11:13 +00:00
|
|
|
errC := errors.Cause(err)
|
|
|
|
// Check for both EPERM and ENOENT since O_CREAT is used by WriteFile.
|
|
|
|
if errors.Is(errC, os.ErrPermission) || errors.Is(errC, os.ErrNotExist) {
|
|
|
|
// Check if a controller is available,
|
|
|
|
// to give more specific error if not.
|
|
|
|
sk := strings.SplitN(k, ".", 2)
|
|
|
|
if len(sk) != 2 {
|
|
|
|
return fmt.Errorf("unified resource %q must be in the form CONTROLLER.PARAMETER", k)
|
|
|
|
}
|
|
|
|
c := sk[0]
|
|
|
|
if _, ok := m.controllers[c]; !ok && c != "cgroup" {
|
|
|
|
return fmt.Errorf("unified resource %q can't be set: controller %q not available", k, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.Wrapf(err, "can't set unified resource %q", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (m *manager) GetPaths() map[string]string {
|
|
|
|
paths := make(map[string]string, 1)
|
|
|
|
paths[""] = m.dirPath
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
2020-05-04 20:46:48 +00:00
|
|
|
func (m *manager) GetCgroups() (*configs.Cgroup, error) {
|
|
|
|
return m.config, nil
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
|
|
|
|
func (m *manager) GetFreezerState() (configs.FreezerState, error) {
|
|
|
|
return getFreezer(m.dirPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) Exists() bool {
|
|
|
|
return cgroups.PathExists(m.dirPath)
|
|
|
|
}
|
2021-04-14 18:11:13 +00:00
|
|
|
|
|
|
|
func OOMKillCount(path string) (uint64, error) {
|
|
|
|
return fscommon.GetValueByKey(path, "memory.events", "oom_kill")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) OOMKillCount() (uint64, error) {
|
2021-06-18 20:46:09 +00:00
|
|
|
c, err := OOMKillCount(m.dirPath)
|
|
|
|
if err != nil && m.rootless && os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, err
|
2021-04-14 18:11:13 +00:00
|
|
|
}
|