Skip to content

Commit da2fd65

Browse files
committed
Add bounds on max oom_score_adj value for AdjustOOMScore
oom_score_adj must be in the range -1000 to 1000. In AdjustOOMScore if containerd's score is already at the maximum value we should set that value for the shim instead of trying to set 1001 which is invalid. Signed-off-by: Simon Kaegi <[email protected]>
1 parent 0042148 commit da2fd65

5 files changed

Lines changed: 23 additions & 2 deletions

File tree

container_linux_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,10 @@ func TestShimOOMScore(t *testing.T) {
20432043
}
20442044

20452045
expectedScore := containerdScore + 1
2046+
if expectedScore > sys.OOMScoreAdjMax {
2047+
expectedScore = sys.OOMScoreAdjMax
2048+
}
2049+
20462050
// find the shim's pid
20472051
if cgroups.Mode() == cgroups.Unified {
20482052
processes, err := cg2.Procs(false)

runtime/v1/shim/client/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,17 @@ func eaddrinuse(err error) bool {
174174

175175
// setupOOMScore gets containerd's oom score and adds +1 to it
176176
// to ensure a shim has a lower* score than the daemons
177+
// if not already at the maximum OOM Score
177178
func setupOOMScore(shimPid int) error {
178179
pid := os.Getpid()
179180
score, err := sys.GetOOMScoreAdj(pid)
180181
if err != nil {
181182
return errors.Wrap(err, "get daemon OOM score")
182183
}
183184
shimScore := score + 1
185+
if shimScore > sys.OOMScoreAdjMax {
186+
shimScore = sys.OOMScoreAdjMax
187+
}
184188
if err := sys.SetOOMScore(shimPid, shimScore); err != nil {
185189
return errors.Wrap(err, "set shim OOM score")
186190
}

runtime/v2/shim/util_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,17 @@ func SetScore(pid int) error {
5353

5454
// AdjustOOMScore sets the OOM score for the process to the parents OOM score +1
5555
// to ensure that they parent has a lower* score than the shim
56+
// if not already at the maximum OOM Score
5657
func AdjustOOMScore(pid int) error {
5758
parent := os.Getppid()
5859
score, err := sys.GetOOMScoreAdj(parent)
5960
if err != nil {
6061
return errors.Wrap(err, "get parent OOM score")
6162
}
6263
shimScore := score + 1
64+
if shimScore > sys.OOMScoreAdjMax {
65+
shimScore = sys.OOMScoreAdjMax
66+
}
6367
if err := sys.SetOOMScore(pid, shimScore); err != nil {
6468
return errors.Wrap(err, "set shim OOM score")
6569
}

sys/oom_unix.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ import (
2626
"strings"
2727
)
2828

29-
// OOMScoreMaxKillable is the maximum score keeping the process killable by the oom killer
30-
const OOMScoreMaxKillable = -999
29+
const (
30+
// OOMScoreMaxKillable is the maximum score keeping the process killable by the oom killer
31+
OOMScoreMaxKillable = -999
32+
// OOMScoreAdjMax is from OOM_SCORE_ADJ_MAX https://github.com/torvalds/linux/blob/master/include/uapi/linux/oom.h
33+
OOMScoreAdjMax = 1000
34+
)
3135

3236
// SetOOMScore sets the oom score for the provided pid
3337
func SetOOMScore(pid, score int) error {

sys/oom_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package sys
1818

19+
const (
20+
// OOMScoreAdjMax is not implemented on Windows
21+
OOMScoreAdjMax = 0
22+
)
23+
1924
// SetOOMScore sets the oom score for the process
2025
//
2126
// Not implemented on Windows

0 commit comments

Comments
 (0)