Files
Shailend ChandandgVisor bot dde347d9fc Add initial cgroup2 support.
The cgroup v2 fs is initialized early in the boot process, so that every task
in the sandbox resides in at least the root v2 cgroup. A new runsc flag
--mount-cgroup-v2 has been added: this controls whether runsc mounts a
cgroupv2 filesystem at /sys/fs/cgroup automatically (currently it always
mounts a v1 hierarchy at this path). This flag also gates whether userspace
can mount a cgroup2 fs. When this flag is false, which is the default, all
tasks in the sandbox reside forever in the root cgroup.

Like in Linux, the v2 hierarchy is deemed the default owner of a controller,
that is, until the corresponding v1 hierarchy is mounted: then, the v1 tree
steals ownership of that controller and it remains this way till as long as
a v1 mount exists. In other words, hybrid mode is supported, even if such
workloads are rare these days.

The pids controller is the only controller implemented in full: it serves
as validation for the unified hierarchy. The memory controller can be used
to learn the usage stats of a subtree: but like the current v1 implementation,
and unlike Linux, it doesn't achieve this through charging up the hierarchy
during allocation, instead it queries pgalloc for every active controller's
id. And it does no enforcing. The cpu and cpuset controllers are mere stubs,
like in the current v1 implementation.

The following are supported:
* General matters of topology: cgroup.subtree_control, cgroup.controllers,
                               cgroup.procs etc
* CLONE_INTO_CGROUP
* cgroup.kill
* cgroup.events
* A working pids controller and its events files
* A minimal memory controller that just reports mem usage
* Stubbed cpu and cpuset controllers

The following are NOT supported:
* threaded cgroups
* cgroup namespaces
* BPF_PROG_TYPE_CGROUP_DEVICE
* cgroup.freeze

Updates #13007

PiperOrigin-RevId: 943519748
2026-07-06 15:17:54 -07:00

138 lines
3.4 KiB
Go

// Copyright 2021 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bitmap
import (
"fmt"
"strconv"
"strings"
)
// FormatList produces a string representation of b, which lists the indices
// of set bits in the bitmap. Indices are separated by commas and ranges of
// set bits are abbreviated. Example outputs: "0,2,4", "0,3-7,10", "0-10".
//
// Inverse of ParseList.
func FormatList(b *Bitmap) string {
ones := b.ToSlice()
if len(ones) == 0 {
return ""
}
elems := make([]string, 0, len(ones))
runStart := ones[0]
lastVal := ones[0]
inRun := false
for _, v := range ones[1:] {
last := lastVal
lastVal = v
if last+1 == v {
// In a contiguous block of ones.
if !inRun {
runStart = last
inRun = true
}
continue
}
// Non-contiguous bit.
if inRun {
// Render a run
elems = append(elems, fmt.Sprintf("%d-%d", runStart, last))
inRun = false
continue
}
// Lone non-contiguous bit.
elems = append(elems, fmt.Sprintf("%d", last))
}
// Process potential final run
if inRun {
elems = append(elems, fmt.Sprintf("%d-%d", runStart, lastVal))
} else {
elems = append(elems, fmt.Sprintf("%d", lastVal))
}
return strings.Join(elems, ",")
}
func parseToken(token string) (start, end uint32, err error) {
ts := strings.SplitN(token, "-", 2)
switch len(ts) {
case 0:
return 0, 0, fmt.Errorf("invalid token %q", token)
case 1:
val, err := strconv.ParseUint(ts[0], 10, 32)
if err != nil {
return 0, 0, err
}
return uint32(val), uint32(val), nil
case 2:
val1, err := strconv.ParseUint(ts[0], 10, 32)
if err != nil {
return 0, 0, err
}
val2, err := strconv.ParseUint(ts[1], 10, 32)
if err != nil {
return 0, 0, err
}
if val1 >= val2 {
return 0, 0, fmt.Errorf("start (%v) must be less than end (%v)", val1, val2)
}
return uint32(val1), uint32(val2), nil
default:
panic(fmt.Sprintf("Unreachable: got %d substrs", len(ts)))
}
}
// ParseList parses input as a bitmap. input should be a comma separated list
// of indices, and ranges of set bits may be abbreviated. Examples: "0,2,4",
// "0,3-7,10", "0-10". Input after the first newline or null byte is discarded.
//
// sizeHint sets the initial size of the bitmap, which may prevent reallocation
// when growing the bitmap during parsing. Ideally sizeHint should be at least
// as large as the bitmap represented by input, but this is not required.
//
// Inverse of FormatList.
func ParseList(input string, sizeHint uint32) (*Bitmap, error) {
b := New(sizeHint)
if termIdx := strings.IndexAny(input, "\n\000"); termIdx != -1 {
input = input[:termIdx]
}
input = strings.TrimSpace(input)
if len(input) == 0 {
return &b, nil
}
tokens := strings.Split(input, ",")
for _, t := range tokens {
start, end, err := parseToken(strings.TrimSpace(t))
if err != nil {
return nil, err
}
for i := start; i <= end; i++ {
b.Add(i)
}
}
return &b, nil
}