Skip to content

Commit c8a459f

Browse files
committed
Fix problem when update memory and swap memory
Currently, if we start a container with: `docker run -ti --name foo --memory 300M --memory-swap 500M busybox sh` Then we want to update it with: `docker update --memory 600M --memory-swap 800M foo` It'll get error because we can't set memory to 600M with the 500M limit of swap memory. Signed-off-by: Qiang Huang <[email protected]>
1 parent 930dbb3 commit c8a459f

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

libcontainer/cgroups/fs/memory.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,20 @@ func (s *MemoryGroup) SetKernelMemory(path string, cgroup *configs.Cgroup) error
6666
}
6767

6868
func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
69-
if cgroup.Resources.Memory != 0 {
70-
if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
69+
// When memory and swap memory are both set, we need to release the limits first,
70+
// it's to avoid problem when we update the configs, the new memory limit could
71+
// be larger than the existed swap memory limit but smaller than the new swap
72+
// memory limit, we should allow such update usage.
73+
if cgroup.Resources.Memory != 0 && cgroup.Resources.MemorySwap != 0 {
74+
if err := writeFile(path, "memory.memsw.limit_in_bytes", "-1"); err != nil {
75+
return err
76+
}
77+
if err := writeFile(path, "memory.limit_in_bytes", "-1"); err != nil {
7178
return err
7279
}
7380
}
74-
if cgroup.Resources.MemoryReservation != 0 {
75-
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
81+
if cgroup.Resources.Memory != 0 {
82+
if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
7683
return err
7784
}
7885
}
@@ -81,6 +88,11 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
8188
return err
8289
}
8390
}
91+
if cgroup.Resources.MemoryReservation != 0 {
92+
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
93+
return err
94+
}
95+
}
8496
if cgroup.Resources.OomKillDisable {
8597
if err := writeFile(path, "memory.oom_control", "1"); err != nil {
8698
return err

0 commit comments

Comments
 (0)