Skip to content

Commit 53c733e

Browse files
AkihiroSudadmcgowan
authored andcommitted
cri: fix memory.memsw.limit_in_bytes: no such file or directory
Skip automatic `if swapLimit == 0 { s.Linux.Resources.Memory.Swap = &limit }` when the swap controller is missing. (default on Ubuntu 20.04) Fix issue 7828 (regression in PR 7783 "cri: make swapping disabled with memory limit") Cherry-Pick was not cleaned, required updated cgroups library from v1.0.3 to v1.0.4 Signed-off-by: Akihiro Suda <[email protected]> (cherry picked from commit 4157503) Signed-off-by: Derek McGowan <[email protected]>
1 parent 07b839d commit 53c733e

12 files changed

Lines changed: 113 additions & 29 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/Microsoft/hcsshim v0.9.6
99
github.com/containerd/aufs v1.0.0
1010
github.com/containerd/btrfs v1.0.0
11-
github.com/containerd/cgroups v1.0.3
11+
github.com/containerd/cgroups v1.0.4
1212
github.com/containerd/console v1.0.3
1313
github.com/containerd/continuity v0.3.0
1414
github.com/containerd/fifo v1.0.0

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ github.com/containerd/cgroups v0.0.0-20200710171044-318312a37340/go.mod h1:s5q4S
183183
github.com/containerd/cgroups v0.0.0-20200824123100-0b889c03f102/go.mod h1:s5q4SojHctfxANBDvMeIaIovkq29IP48TKAxnhYRxvo=
184184
github.com/containerd/cgroups v0.0.0-20210114181951-8a68de567b68/go.mod h1:ZJeTFisyysqgcCdecO57Dj79RfL0LNeGiFUqLYQRYLE=
185185
github.com/containerd/cgroups v1.0.1/go.mod h1:0SJrPIenamHDcZhEcJMNBB85rHcUsw4f25ZfBiPYRkU=
186-
github.com/containerd/cgroups v1.0.3 h1:ADZftAkglvCiD44c77s5YmMqaP2pzVCFZvBmAlBdAP4=
187186
github.com/containerd/cgroups v1.0.3/go.mod h1:/ofk34relqNjSGyqPrmEULrO4Sc8LJhvJmWbUCUKqj8=
187+
github.com/containerd/cgroups v1.0.4 h1:jN/mbWBEaz+T1pi5OFtnkQ+8qnmEbAr1Oo1FRm5B0dA=
188+
github.com/containerd/cgroups v1.0.4/go.mod h1:nLNQtsF7Sl2HxNebu77i1R0oDlhiTG+kO4JTrUzo6IA=
188189
github.com/containerd/console v0.0.0-20180822173158-c12b1e7919c1/go.mod h1:Tj/on1eG8kiEhd0+fhSDzsPAFESxzBBvdyEgyryXffw=
189190
github.com/containerd/console v0.0.0-20181022165439-0650fd9eeb50/go.mod h1:Tj/on1eG8kiEhd0+fhSDzsPAFESxzBBvdyEgyryXffw=
190191
github.com/containerd/console v0.0.0-20191206165004-02ecf6a7291e/go.mod h1:8Pf4gM6VEbTNRIT26AyyU7hxdQU3MvAvxVI0sc00XBE=

integration/client/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.15
55
require (
66
github.com/Microsoft/hcsshim v0.9.6
77
github.com/Microsoft/hcsshim/test v0.0.0-20210408205431-da33ecd607e1
8-
github.com/containerd/cgroups v1.0.3
8+
github.com/containerd/cgroups v1.0.4
99
// the actual version of containerd is replaced with the code at the root of this repository
1010
github.com/containerd/containerd v1.6.1
1111
github.com/containerd/go-runc v1.0.0

integration/client/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ github.com/containerd/aufs v1.0.0/go.mod h1:kL5kd6KM5TzQjR79jljyi4olc1Vrx6XBlcyj
121121
github.com/containerd/btrfs v1.0.0/go.mod h1:zMcX3qkXTAi9GI50+0HOeuV8LU2ryCE/V2vG/ZBiTss=
122122
github.com/containerd/cgroups v0.0.0-20200824123100-0b889c03f102/go.mod h1:s5q4SojHctfxANBDvMeIaIovkq29IP48TKAxnhYRxvo=
123123
github.com/containerd/cgroups v1.0.1/go.mod h1:0SJrPIenamHDcZhEcJMNBB85rHcUsw4f25ZfBiPYRkU=
124-
github.com/containerd/cgroups v1.0.3 h1:ADZftAkglvCiD44c77s5YmMqaP2pzVCFZvBmAlBdAP4=
125-
github.com/containerd/cgroups v1.0.3/go.mod h1:/ofk34relqNjSGyqPrmEULrO4Sc8LJhvJmWbUCUKqj8=
124+
github.com/containerd/cgroups v1.0.4 h1:jN/mbWBEaz+T1pi5OFtnkQ+8qnmEbAr1Oo1FRm5B0dA=
125+
github.com/containerd/cgroups v1.0.4/go.mod h1:nLNQtsF7Sl2HxNebu77i1R0oDlhiTG+kO4JTrUzo6IA=
126126
github.com/containerd/console v0.0.0-20191206165004-02ecf6a7291e/go.mod h1:8Pf4gM6VEbTNRIT26AyyU7hxdQU3MvAvxVI0sc00XBE=
127127
github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw=
128128
github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ=

pkg/cri/opts/spec_linux.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"sync"
2929
"syscall"
3030

31+
"github.com/containerd/cgroups"
3132
"github.com/containerd/containerd/containers"
3233
"github.com/containerd/containerd/log"
3334
"github.com/containerd/containerd/mount"
@@ -403,6 +404,36 @@ func WithSelinuxLabels(process, mount string) oci.SpecOpts {
403404
}
404405
}
405406

407+
var (
408+
swapControllerAvailability bool
409+
swapControllerAvailabilityOnce sync.Once
410+
)
411+
412+
func swapControllerAvailable() bool {
413+
swapControllerAvailabilityOnce.Do(func() {
414+
const warn = "Failed to detect the availability of the swap controller, assuming not available"
415+
p := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
416+
if cgroups.Mode() == cgroups.Unified {
417+
// memory.swap.max does not exist in the cgroup root, so we check /sys/fs/cgroup/<SELF>/memory.swap.max
418+
_, unified, err := cgroups.ParseCgroupFileUnified("/proc/self/cgroup")
419+
if err != nil {
420+
err = fmt.Errorf("failed to parse /proc/self/cgroup: %w", err)
421+
logrus.WithError(err).Warn(warn)
422+
return
423+
}
424+
p = filepath.Join("/sys/fs/cgroup", unified, "memory.swap.max")
425+
}
426+
if _, err := os.Stat(p); err != nil {
427+
if !errors.Is(err, os.ErrNotExist) {
428+
logrus.WithError(err).Warn(warn)
429+
}
430+
return
431+
}
432+
swapControllerAvailability = true
433+
})
434+
return swapControllerAvailability
435+
}
436+
406437
// WithResources sets the provided resource restrictions
407438
func WithResources(resources *runtime.LinuxContainerResources, tolerateMissingHugetlbController, disableHugetlbController bool) oci.SpecOpts {
408439
return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) (err error) {
@@ -448,7 +479,7 @@ func WithResources(resources *runtime.LinuxContainerResources, tolerateMissingHu
448479
if limit != 0 {
449480
s.Linux.Resources.Memory.Limit = &limit
450481
// swap/memory limit should be equal to prevent container from swapping by default
451-
if swapLimit == 0 {
482+
if swapLimit == 0 && swapControllerAvailable() {
452483
s.Linux.Resources.Memory.Swap = &limit
453484
}
454485
}

vendor/github.com/containerd/cgroups/README.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/cgroups/Vagrantfile

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

vendor/github.com/containerd/cgroups/utils.go

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

vendor/github.com/containerd/cgroups/v2/manager.go

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

vendor/github.com/containerd/cgroups/v2/memory.go

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

0 commit comments

Comments
 (0)