From 5d193188db3b4afc2caa3f23ba0364c50b4f8087 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Fri, 14 May 2021 11:32:03 -0700 Subject: [PATCH] libct/cg/fs2: optimize setFreezer In case configs.Undefined or any wrong value is passed, there is no need to check whether the freezer is supported. Move arguments check to the beginning to avoid an unnecessary call to supportFreezer(). While at it, simplify the "whether to return an error if freezer is not supported" check. Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/fs2/freezer.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libcontainer/cgroups/fs2/freezer.go b/libcontainer/cgroups/fs2/freezer.go index 441531fd7..56ae0cad0 100644 --- a/libcontainer/cgroups/fs2/freezer.go +++ b/libcontainer/cgroups/fs2/freezer.go @@ -14,16 +14,6 @@ import ( ) func setFreezer(dirPath string, state configs.FreezerState) error { - if err := supportsFreezer(dirPath); err != nil { - // We can ignore this request as long as the user didn't ask us to - // freeze the container (since without the freezer cgroup, that's a - // no-op). - if state == configs.Undefined || state == configs.Thawed { - return nil - } - return errors.Wrap(err, "freezer not supported") - } - var stateStr string switch state { case configs.Undefined: @@ -36,6 +26,16 @@ func setFreezer(dirPath string, state configs.FreezerState) error { return errors.Errorf("invalid freezer state %q requested", state) } + if err := supportsFreezer(dirPath); err != nil { + // We can ignore this request as long as the user didn't ask us to + // freeze the container (since without the freezer cgroup, that's a + // no-op). + if state != configs.Frozen { + return nil + } + return errors.Wrap(err, "freezer not supported") + } + if err := fscommon.WriteFile(dirPath, "cgroup.freeze", stateStr); err != nil { return err }