Files
seaweedfs/seaweed-block/cmd/blockvolume/memory_cgroup.go
T
pingqiuandClaude Opus 5.5 7a70a7a1ca blockvolume: an underivable cgroup memory limit starts on the default
With neither --memory-limit-mib nor GOMEMLIMIT, a cgroup v2 limit that
cannot be derived (a membership outside the cgroup2 mount of the holder's
mount namespace, an unreadable or malformed memory.max) is the no-limit
case: the holder keeps the 256 MiB default and starts, instead of exiting
2. applyMemoryLimit, run once logging is configured, logs one level=WARN
line naming the derivation's error and the two ways to choose a limit, and
status reports the reason in Memory.SoftLimitFallback (empty otherwise).
Only an explicit but invalid limit still refuses.

Docs: the flag's help, the operator guide's memory section, the status
schema and the v1.1 notes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 15:46:02 -08:00

129 lines
3.8 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"path"
"strconv"
"strings"
)
// cgroupMemoryLimitMiB reads the current unified cgroup and its ancestors:
// a parent's finite limit also constrains this holder. Zero means no v2 limit.
// Use slash paths even in fixture tests on non-Linux hosts.
func cgroupMemoryLimitMiB(readFile func(string) ([]byte, error)) (int64, error) {
data, err := readFile("/proc/self/cgroup")
if errors.Is(err, os.ErrNotExist) {
return 0, nil
}
if err != nil {
return 0, err
}
group := ""
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, "0::") {
group = strings.TrimPrefix(line, "0::")
break
}
}
if group == "" {
return 0, nil
}
if !strings.HasPrefix(group, "/") {
return 0, fmt.Errorf("invalid unified cgroup path %q", group)
}
for _, part := range strings.Split(group, "/") {
if part == ".." {
return 0, fmt.Errorf("unified cgroup path escapes its mount: %q", group)
}
}
leaf, mount, err := cgroupMemoryDirectory(path.Clean(group), readFile)
if err != nil {
return 0, err
}
var limit int64
for dir := leaf; ; dir = path.Dir(dir) {
name := path.Join(dir, "memory.max")
value, err := readFile(name)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return 0, fmt.Errorf("%s: %w", name, err)
}
if err == nil && strings.TrimSpace(string(value)) != "max" {
n, err := strconv.ParseInt(strings.TrimSpace(string(value)), 10, 64)
if err != nil || n <= 0 {
return 0, fmt.Errorf("invalid %s value %q", name, value)
}
if limit == 0 || n < limit {
limit = n
}
}
if dir == mount {
break
}
}
if limit == 0 {
return 0, nil
}
// Divide before multiplying to avoid overflow near MaxInt64.
mib := (limit/10*9 + limit%10*9/10) >> 20
if mib == 0 {
return 0, fmt.Errorf("cgroup memory limit %d is too small for a MiB soft limit", limit)
}
return mib, nil
}
// mountinfo names both the filesystem subtree root and its visible mountpoint.
// A membership path must resolve inside that view; an unresolved v2 hierarchy
// is an error here, not evidence of unlimited memory: the caller keeps the
// default limit and warns why.
func cgroupMemoryDirectory(group string, readFile func(string) ([]byte, error)) (string, string, error) {
data, err := readFile("/proc/self/mountinfo")
if err != nil {
return "", "", fmt.Errorf("read cgroup mounts: %w", err)
}
unescape := strings.NewReplacer(`\040`, " ", `\011`, "\t", `\012`, "\n", `\134`, `\`)
var leaf, mount, bestRoot string
for _, line := range strings.Split(string(data), "\n") {
left, right, ok := strings.Cut(line, " - ")
if !ok {
continue
}
fs := strings.Fields(right)
if len(fs) == 0 || fs[0] != "cgroup2" {
continue
}
fields := strings.Fields(left)
if len(fields) < 6 {
return "", "", fmt.Errorf("malformed cgroup2 mountinfo")
}
root, point := unescape.Replace(fields[3]), unescape.Replace(fields[4])
if !path.IsAbs(root) || !path.IsAbs(point) {
return "", "", fmt.Errorf("invalid cgroup2 mount paths")
}
unresolved := false
for _, part := range strings.Split(root+"/"+point, "/") {
if part == ".." {
unresolved = true
break
}
}
if unresolved {
continue
} // do not clean an out-of-namespace root into a different view
root, point = path.Clean(root), path.Clean(point)
if group != root && root != "/" && !strings.HasPrefix(group, root+"/") {
continue
}
// Prefer the widest visible ancestry when the hierarchy is mounted
// more than once, so a subtree bind mount does not hide a parent cap.
if mount == "" || len(root) < len(bestRoot) {
leaf, mount, bestRoot = path.Join(point, strings.TrimPrefix(group, root)), point, root
}
}
if mount == "" {
return "", "", fmt.Errorf("no cgroup2 mount resolves membership %q", group)
}
return leaf, mount, nil
}