Skip to content
Closed
Show file tree
Hide file tree
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
12 changes: 5 additions & 7 deletions libcontainer/cgroups/fs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@ func setKernelMemory(path string, kernelMemoryLimit uint64) error {
}

func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error {
ulimited := -1

// If the memory update is set to uint64(-1) we should also
// set swap to uint64(-1), it means unlimited memory.
if cgroup.Resources.Memory == uint64(ulimited) {
// If the memory update is set to MemoryUnlimited we should also
// set swap to MemoryUnlimited.
if cgroup.Resources.Memory == configs.MemoryUnlimited {
// Only set swap if it's enbled in kernel
if cgroups.PathExists(filepath.Join(path, cgroupMemorySwapLimit)) {
cgroup.Resources.MemorySwap = uint64(ulimited)
cgroup.Resources.MemorySwap = uint64(configs.MemoryUnlimited)
Copy link
Contributor

Choose a reason for hiding this comment

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

i think you dont need to cast this to uint64 anymore

}
}

Expand All @@ -126,7 +124,7 @@ func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error {
// When update memory limit, we should adapt the write sequence
// for memory and swap memory, so it won't fail because the new
// value and the old value don't fit kernel's validation.
if cgroup.Resources.MemorySwap == uint64(ulimited) || memoryUsage.Limit < cgroup.Resources.MemorySwap {
if cgroup.Resources.MemorySwap == uint64(configs.MemoryUnlimited) || memoryUsage.Limit < cgroup.Resources.MemorySwap {
Copy link
Contributor

Choose a reason for hiding this comment

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

same as the above, casting is not necessary.

if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion libcontainer/configs/cgroup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

package configs

import (
"math"
)

type FreezerState string

const (
Expand Down Expand Up @@ -33,6 +37,8 @@ type Cgroup struct {
*Resources
}

const MemoryUnlimited = math.MaxUint64
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not the value that is actually used by the kernel, which is 9223372036854771712. Some older kernels will set any value larger than this to 0, and then crash, eg RHEL 7.2.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also this value may well be totally incorrect on 32 bit machines, not sure what happens there.


type Resources struct {
// If this is true allow access to any kind of device within the container. If false, allow access only to devices explicitly listed in the allowed_devices list.
// Deprecated
Expand All @@ -50,7 +56,7 @@ type Resources struct {
// Memory reservation or soft_limit (in bytes)
MemoryReservation uint64 `json:"memory_reservation"`

// Total memory usage (memory + swap); set `-1` to enable unlimited swap
// Total memory usage (memory + swap); set MemoryUnlimited to enable unlimited swap
MemorySwap uint64 `json:"memory_swap"`

// Kernel memory limit (in bytes)
Expand Down
5 changes: 3 additions & 2 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"

"github.com/docker/go-units"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -222,10 +223,10 @@ other options are ignored.
if err != nil {
return fmt.Errorf("invalid value for %s: %s", pair.opt, err)
}
*pair.dest = uint64(v)
} else {
v = -1
*pair.dest = configs.MemoryUnlimited
}
*pair.dest = uint64(v)
}
}
}
Expand Down