Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix read "max" value for cgroup v2 #2837

Merged
merged 2 commits into from
Mar 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions container/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package common
import (
"fmt"
"io/ioutil"
"math"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -50,25 +50,6 @@ func DebugInfo(watches map[string][]string) map[string][]string {
return out
}

// findFileInAncestorDir returns the path to the parent directory that contains the specified file.
// "" is returned if the lookup reaches the limit.
func findFileInAncestorDir(current, file, limit string) (string, error) {
for {
fpath := path.Join(current, file)
_, err := os.Stat(fpath)
if err == nil {
return current, nil
}
if !os.IsNotExist(err) {
return "", err
}
if current == limit {
return "", nil
}
current = filepath.Dir(current)
}
}

var bootTime = func() time.Time {
now := time.Now()
var sysinfo unix.Sysinfo_t
Expand Down Expand Up @@ -165,11 +146,7 @@ func GetSpec(cgroupPaths map[string]string, machineInfoFactory info.MachineInfoF
spec.Memory.Reservation = readUInt64(memoryRoot, "memory.soft_limit_in_bytes")
}
} else {
memoryRoot, err := findFileInAncestorDir(memoryRoot, "memory.max", "/sys/fs/cgroup")
if err != nil {
return spec, err
}
if memoryRoot != "" {
if utils.FileExists(path.Join(memoryRoot, "memory.max")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

spec.HasMemory = true
spec.Memory.Reservation = readUInt64(memoryRoot, "memory.high")
spec.Memory.Limit = readUInt64(memoryRoot, "memory.max")
Expand Down Expand Up @@ -226,7 +203,10 @@ func readString(dirpath string, file string) string {

func readUInt64(dirpath string, file string) uint64 {
out := readString(dirpath, file)
if out == "" || out == "max" {
if out == "max" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have you decided to change behaviour for out == ""?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for out == "" I've left the same behavior there was before 2ccad4b

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, my bad!

return math.MaxUint64
Copy link
Contributor

@odinuge odinuge Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The max value for cgroup v1 is 9223372036854771712 (max signed integer rounded to nearest page, at least on x86). So to be correct it should be something like this I guess:

Suggested change
return math.MaxUint64
return math.MaxInt64^0xFFF

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with changing it, but would it be a problem with cgroup v1? "max" is used only on cgroup v2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember where this value is used, but I just thought it was nice with the same value for both cgroup v2. If you try to write MaxInt64^0xFFF + 0x1 you get max on cgroup v2, and 9223372036854771712 on v1. 9223372036854771712 is also default on v1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: This is used for more than the memory limits, and my above comments only apply to those. Hmm

}
if out == "" {
return 0
}

Expand Down