Skip to content

Commit ff9f916

Browse files
skaegiJohnathan Dell
authored andcommitted
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]> (cherry picked from commit da2fd65)
1 parent 1e683ff commit ff9f916

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
@@ -1845,6 +1845,10 @@ func TestShimOOMScore(t *testing.T) {
18451845
t.Fatal(err)
18461846
}
18471847
expectedScore := containerdScore + 1
1848+
if expectedScore > sys.OOMScoreAdjMax {
1849+
expectedScore = sys.OOMScoreAdjMax
1850+
}
1851+
18481852
// find the shim's pid
18491853
for _, p := range processes {
18501854
score, err := sys.GetOOMScoreAdj(p.Pid)

runtime/v1/shim/client/client.go

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

171171
// setupOOMScore gets containerd's oom score and adds +1 to it
172172
// to ensure a shim has a lower* score than the daemons
173+
// if not already at the maximum OOM Score
173174
func setupOOMScore(shimPid int) error {
174175
pid := os.Getpid()
175176
score, err := sys.GetOOMScoreAdj(pid)
176177
if err != nil {
177178
return errors.Wrap(err, "get daemon OOM score")
178179
}
179180
shimScore := score + 1
181+
if shimScore > sys.OOMScoreAdjMax {
182+
shimScore = sys.OOMScoreAdjMax
183+
}
180184
if err := sys.SetOOMScore(shimPid, shimScore); err != nil {
181185
return errors.Wrap(err, "set shim OOM score")
182186
}

runtime/v2/shim/util_unix.go

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

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

sys/oom_unix.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ import (
2828
"github.com/opencontainers/runc/libcontainer/system"
2929
)
3030

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

3438
// SetOOMScore sets the oom score for the provided pid
3539
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)