Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 810190c

Browse files
authored
Merge pull request #2 from tiborvass/fix_memory_uint64_to_int64
[17.06] Fix memory uint64 to int64
2 parents 2d41c04 + 2a13a01 commit 810190c

5 files changed

Lines changed: 36 additions & 38 deletions

File tree

libcontainer/cgroups/fs/memory.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,22 @@ func EnableKernelMemoryAccounting(path string) error {
7171
// until a limit is set on the cgroup and limit cannot be set once the
7272
// cgroup has children, or if there are already tasks in the cgroup.
7373
for _, i := range []int64{1, -1} {
74-
if err := setKernelMemory(path, uint64(i)); err != nil {
74+
if err := setKernelMemory(path, i); err != nil {
7575
return err
7676
}
7777
}
7878
return nil
7979
}
8080

81-
func setKernelMemory(path string, kernelMemoryLimit uint64) error {
81+
func setKernelMemory(path string, kernelMemoryLimit int64) error {
8282
if path == "" {
8383
return fmt.Errorf("no such directory for %s", cgroupKernelMemoryLimit)
8484
}
8585
if !cgroups.PathExists(filepath.Join(path, cgroupKernelMemoryLimit)) {
8686
// kernel memory is not enabled on the system so we should do nothing
8787
return nil
8888
}
89-
if err := ioutil.WriteFile(filepath.Join(path, cgroupKernelMemoryLimit), []byte(strconv.FormatUint(kernelMemoryLimit, 10)), 0700); err != nil {
89+
if err := ioutil.WriteFile(filepath.Join(path, cgroupKernelMemoryLimit), []byte(strconv.FormatInt(kernelMemoryLimit, 10)), 0700); err != nil {
9090
// Check if the error number returned by the syscall is "EBUSY"
9191
// The EBUSY signal is returned on attempts to write to the
9292
// memory.kmem.limit_in_bytes file if the cgroup has children or
@@ -104,14 +104,12 @@ func setKernelMemory(path string, kernelMemoryLimit uint64) error {
104104
}
105105

106106
func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error {
107-
ulimited := -1
108-
109-
// If the memory update is set to uint64(-1) we should also
110-
// set swap to uint64(-1), it means unlimited memory.
111-
if cgroup.Resources.Memory == uint64(ulimited) {
112-
// Only set swap if it's enbled in kernel
107+
// If the memory update is set to -1 we should also
108+
// set swap to -1, it means unlimited memory.
109+
if cgroup.Resources.Memory == -1 {
110+
// Only set swap if it's enabled in kernel
113111
if cgroups.PathExists(filepath.Join(path, cgroupMemorySwapLimit)) {
114-
cgroup.Resources.MemorySwap = uint64(ulimited)
112+
cgroup.Resources.MemorySwap = -1
115113
}
116114
}
117115

@@ -126,29 +124,29 @@ func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error {
126124
// When update memory limit, we should adapt the write sequence
127125
// for memory and swap memory, so it won't fail because the new
128126
// value and the old value don't fit kernel's validation.
129-
if cgroup.Resources.MemorySwap == uint64(ulimited) || memoryUsage.Limit < cgroup.Resources.MemorySwap {
130-
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
127+
if cgroup.Resources.MemorySwap == -1 || memoryUsage.Limit < uint64(cgroup.Resources.MemorySwap) {
128+
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
131129
return err
132130
}
133-
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
131+
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
134132
return err
135133
}
136134
} else {
137-
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
135+
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
138136
return err
139137
}
140-
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
138+
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
141139
return err
142140
}
143141
}
144142
} else {
145143
if cgroup.Resources.Memory != 0 {
146-
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatUint(cgroup.Resources.Memory, 10)); err != nil {
144+
if err := writeFile(path, cgroupMemoryLimit, strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
147145
return err
148146
}
149147
}
150148
if cgroup.Resources.MemorySwap != 0 {
151-
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatUint(cgroup.Resources.MemorySwap, 10)); err != nil {
149+
if err := writeFile(path, cgroupMemorySwapLimit, strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
152150
return err
153151
}
154152
}
@@ -169,13 +167,13 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
169167
}
170168

171169
if cgroup.Resources.MemoryReservation != 0 {
172-
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatUint(cgroup.Resources.MemoryReservation, 10)); err != nil {
170+
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
173171
return err
174172
}
175173
}
176174

177175
if cgroup.Resources.KernelMemoryTCP != 0 {
178-
if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatUint(cgroup.Resources.KernelMemoryTCP, 10)); err != nil {
176+
if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatInt(cgroup.Resources.KernelMemoryTCP, 10)); err != nil {
179177
return err
180178
}
181179
}

libcontainer/configs/cgroup_unix.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ type Resources struct {
4545
Devices []*Device `json:"devices"`
4646

4747
// Memory limit (in bytes)
48-
Memory uint64 `json:"memory"`
48+
Memory int64 `json:"memory"`
4949

5050
// Memory reservation or soft_limit (in bytes)
51-
MemoryReservation uint64 `json:"memory_reservation"`
51+
MemoryReservation int64 `json:"memory_reservation"`
5252

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

5656
// Kernel memory limit (in bytes)
57-
KernelMemory uint64 `json:"kernel_memory"`
57+
KernelMemory int64 `json:"kernel_memory"`
5858

5959
// Kernel memory limit for TCP use (in bytes)
60-
KernelMemoryTCP uint64 `json:"kernel_memory_tcp"`
60+
KernelMemoryTCP int64 `json:"kernel_memory_tcp"`
6161

6262
// CPU shares (relative weight vs. other containers)
6363
CpuShares uint64 `json:"cpu_shares"`

update.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ other options are ignored.
124124

125125
r := specs.LinuxResources{
126126
Memory: &specs.LinuxMemory{
127-
Limit: u64Ptr(0),
128-
Reservation: u64Ptr(0),
129-
Swap: u64Ptr(0),
130-
Kernel: u64Ptr(0),
131-
KernelTCP: u64Ptr(0),
127+
Limit: i64Ptr(0),
128+
Reservation: i64Ptr(0),
129+
Swap: i64Ptr(0),
130+
Kernel: i64Ptr(0),
131+
KernelTCP: i64Ptr(0),
132132
},
133133
CPU: &specs.LinuxCPU{
134134
Shares: u64Ptr(0),
@@ -213,7 +213,7 @@ other options are ignored.
213213
}
214214
for _, pair := range []struct {
215215
opt string
216-
dest *uint64
216+
dest *int64
217217
}{
218218
{"memory", r.Memory.Limit},
219219
{"memory-swap", r.Memory.Swap},
@@ -232,7 +232,7 @@ other options are ignored.
232232
} else {
233233
v = -1
234234
}
235-
*pair.dest = uint64(v)
235+
*pair.dest = v
236236
}
237237
}
238238
r.Pids.Limit = int64(context.Int("pids-limit"))

vendor.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# OCI runtime-spec. When updating this, make sure you use a version tag rather
22
# than a commit ID so it's much more obvious what version of the spec we are
33
# using.
4-
github.com/opencontainers/runtime-spec v1.0.0-rc5
4+
github.com/opencontainers/runtime-spec a45ba0989fc26c695fe166a49c45bb8b7618ab36 https://github.com/docker/runtime-spec
55
# Core libcontainer functionality.
66
github.com/mrunalp/fileutils ed869b029674c0e9ce4c0dfa781405c2d9946d08
77
github.com/opencontainers/selinux v1.0.0-rc1

vendor/github.com/opencontainers/runtime-spec/specs-go/config.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)