Skip to content

sys: refactor OOM utilities#5253

Merged
mxpv merged 6 commits intocontainerd:masterfrom
thaJeztah:refactor_oom
Apr 8, 2021
Merged

sys: refactor OOM utilities#5253
mxpv merged 6 commits intocontainerd:masterfrom
thaJeztah:refactor_oom

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

@thaJeztah thaJeztah commented Mar 23, 2021

Noticed these when I was working on #5252. Not attached to all of them, so feel free to shoot 😂

sys: un-export runningPrivileged() / runningUnprivileged()

These are currently only used inside this package, so we might
as well un-export them until we need them elsewhere.

Also updated SetOOMScore() to first check for privileged; check for privileged
looks to be the "faster" path, and checking it first could (in case of non-
privileged) save having to read and parse /proc/self/uid_map.

sys: add missing pre-condition checks in tests

SetOOMScore requires both privileged (root) and non-user namespace
for negative values, so adjust the pre-conditions accordingly.

sys: use assert for error checks in OOM tests

Just slightly easier to read, and we were already using assert in this file

sys: add boundary checks to SetOOMScore()

Produce a more user-friendly error in the odd case we would try to set a
score out of bounds

sys: add AdjustOOMScore() utility

Handle the limits in this function so that consumers don't have to perform the
boundary checks.

runtime/v2/shim: remove unused SetScore() and remove sys.OOMScoreMaxKillable

The shim.SetScore() utility was no longer used since 7dfc605 (#3380).

Checking for uses outside of this repository, I found only one external use of this in gVisor; https://github.com/google/gvisor/blob/a9441aea2780da8c93da1c73da860219f98438de/pkg/shim/service.go#L262-L264

Which was added in google/gvisor-containerd-shim@35db607 (google/gvisor-containerd-shim#13) by @Random-Liu, but looks like it can be easily replaced by either calling sys.SetOOMScore() directly, or replaced with shim.AdjustOOMScore() as is done in this repository now.

@k8s-ci-robot
Copy link
Copy Markdown

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

Comment thread sys/oom_unix_test.go Outdated
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread sys/oom_unix_test.go Outdated
Copy link
Copy Markdown
Member Author

@thaJeztah thaJeztah Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope this is correct then to require !user-namspaces (otherwise I'll drop this commit); I based this on the fact we're suppressing failures in this situation in SetOOMScore, but perhaps that was the wrong assumption 😅

if os.IsPermission(err) && (userns.RunningInUserNS() || RunningUnprivileged()) {

Comment thread sys/env.go Outdated
@thaJeztah thaJeztah marked this pull request as ready for review March 24, 2021 16:47
@thaJeztah
Copy link
Copy Markdown
Member Author

@AkihiroSuda updated; PTAL

@thaJeztah

This comment has been minimized.

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci Bot commented Apr 1, 2021

Build succeeded.

@thaJeztah
Copy link
Copy Markdown
Member Author

Failure looks legit; perhaps I should remove the "add-preconditions commit"; I'll have a look;

--- FAIL: TestSetOOMScoreBoundaries (0.01s)
    oom_linux_test.go:73: assertion failed: 500 (adjustment int) != -1000 (-1000 int)

^^ fixed the test

@AkihiroSuda @kzys PTAL

Copy link
Copy Markdown
Member

@estesp estesp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple pretty minor points about the constants and use of them; otherwise looks good

Comment thread sys/oom_linux.go Outdated
Comment thread sys/oom_linux_test.go Outdated
@thaJeztah
Copy link
Copy Markdown
Member Author

@estesp Updated with your suggestions.

I also pushed one more commit with a change I wanted to make, but forgot to include; last commit removes the runtime/v2/shim.SetScore() utility, which was no longer used since 7dfc605 (#3380). I checked for uses outside of this repository, I found only one external use of this in gVisor; https://github.com/google/gvisor/blob/a9441aea2780da8c93da1c73da860219f98438de/pkg/shim/service.go#L262-L264, which was added in google/gvisor-containerd-shim@35db607 (google/gvisor-containerd-shim#13), but looks like it can be easily replaced by either calling sys.SetOOMScore() directly, or replaced with shim.AdjustOOMScore() as is done in this repository now.

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci Bot commented Apr 2, 2021

Build succeeded.

These are currently only used inside this package, so we might
as well un-export them until we need them elsewhere.

Also updated SetOOMScore() to first check for privileged; check for privileged
looks to be the "faster" path, and checking it first could (in case of non-
privileged) save having to read and parse /proc/self/uid_map.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
SetOOMScore requires both privileged (root) and non-user namespace,
for negative values, so adjust the pre-conditions accordingly.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Slightly easier to read, and we were already using assert in this file

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Produce a more user-friendly error in the odd case we would try to set a
score out of range

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Handle the limits in this function so that consumers don't have to
perform the boundary checks.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
…illable

The shim.SetScore() utility was no longer used since 7dfc605.

Checking for uses outside of this repository, I found only one external use of
this in gVisor; https://github.com/google/gvisor/blob/a9441aea2780da8c93da1c73da860219f98438de/pkg/shim/service.go#L262-L264

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
Copy link
Copy Markdown
Member Author

rebased to trigger CI (which should now be fixed)

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci Bot commented Apr 7, 2021

Build succeeded.

Copy link
Copy Markdown
Member

@estesp estesp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thaJeztah thaJeztah requested a review from AkihiroSuda April 8, 2021 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants