Skip to content

Commit 8a4629f

Browse files
committed
cgroups: nokmem: error out on explicitly-set kmemcg limits
When built with nokmem we explicitly are disabling support for kmemcg, but it is a strict specification requirement that if we cannot fulfil an aspect of the container configuration that we error out. Completely ignoring explicitly-requested kmemcg limits with nokmem would undoubtably lead to problems. Fixes: 6a2c155 ("libcontainer: ability to compile without kmem") Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 4932620 commit 8a4629f

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

libcontainer/cgroups/fs/kmem.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package fs
44

55
import (
6+
"errors"
67
"fmt"
78
"io/ioutil"
89
"os"
@@ -17,7 +18,12 @@ import (
1718
const cgroupKernelMemoryLimit = "memory.kmem.limit_in_bytes"
1819

1920
func EnableKernelMemoryAccounting(path string) error {
20-
// Check if kernel memory is enabled
21+
// Ensure that kernel memory is available in this kernel build. If it
22+
// isn't, we just ignore it because EnableKernelMemoryAccounting is
23+
// automatically called for all memory limits.
24+
if !cgroups.PathExists(filepath.Join(path, cgroupKernelMemoryLimit)) {
25+
return nil
26+
}
2127
// We have to limit the kernel memory here as it won't be accounted at all
2228
// until a limit is set on the cgroup and limit cannot be set once the
2329
// cgroup has children, or if there are already tasks in the cgroup.
@@ -34,8 +40,9 @@ func setKernelMemory(path string, kernelMemoryLimit int64) error {
3440
return fmt.Errorf("no such directory for %s", cgroupKernelMemoryLimit)
3541
}
3642
if !cgroups.PathExists(filepath.Join(path, cgroupKernelMemoryLimit)) {
37-
// kernel memory is not enabled on the system so we should do nothing
38-
return nil
43+
// We have specifically been asked to set a kmem limit. If the kernel
44+
// doesn't support it we *must* error out.
45+
return errors.New("kernel memory accounting not supported by this kernel")
3946
}
4047
if err := ioutil.WriteFile(filepath.Join(path, cgroupKernelMemoryLimit), []byte(strconv.FormatInt(kernelMemoryLimit, 10)), 0700); err != nil {
4148
// Check if the error number returned by the syscall is "EBUSY"

libcontainer/cgroups/fs/kmem_disabled.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
package fs
44

5+
import (
6+
"errors"
7+
)
8+
59
func EnableKernelMemoryAccounting(path string) error {
610
return nil
711
}
812

913
func setKernelMemory(path string, kernelMemoryLimit int64) error {
10-
return nil
14+
return errors.New("kernel memory accounting disabled in this runc build")
1115
}

0 commit comments

Comments
 (0)