From 860f05f307f4f0a777fd84a21f493167384953a3 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 25 Apr 2024 15:40:49 -0700 Subject: [PATCH] libct/cg/fs: fix setting rt_period vs rt_runtime The issue is the same as in commit 1b2adcf but for RT scheduler; the fix is also the same. Test case by ls-ggg. Co-authored-by: ls-ggg <335814617@qq.com> Signed-off-by: Kir Kolyshkin (cherry picked from commit b60079e2e59670b8babd653002d8f469064fb244) Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/fs/cpu.go | 20 ++++++++++++++++++-- tests/integration/update.bats | 11 +++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/libcontainer/cgroups/fs/cpu.go b/libcontainer/cgroups/fs/cpu.go index 6c79f899b48..72c9cd70b50 100644 --- a/libcontainer/cgroups/fs/cpu.go +++ b/libcontainer/cgroups/fs/cpu.go @@ -35,15 +35,31 @@ func (s *CpuGroup) Apply(path string, r *configs.Resources, pid int) error { } func (s *CpuGroup) SetRtSched(path string, r *configs.Resources) error { + var period string if r.CpuRtPeriod != 0 { - if err := cgroups.WriteFile(path, "cpu.rt_period_us", strconv.FormatUint(r.CpuRtPeriod, 10)); err != nil { - return err + period = strconv.FormatUint(r.CpuRtPeriod, 10) + if err := cgroups.WriteFile(path, "cpu.rt_period_us", period); err != nil { + // The values of cpu.rt_period_us and cpu.rt_runtime_us + // are inter-dependent and need to be set in a proper order. + // If the kernel rejects the new period value with EINVAL + // and the new runtime value is also being set, let's + // ignore the error for now and retry later. + if !errors.Is(err, unix.EINVAL) || r.CpuRtRuntime == 0 { + return err + } + } else { + period = "" } } if r.CpuRtRuntime != 0 { if err := cgroups.WriteFile(path, "cpu.rt_runtime_us", strconv.FormatInt(r.CpuRtRuntime, 10)); err != nil { return err } + if period != "" { + if err := cgroups.WriteFile(path, "cpu.rt_period_us", period); err != nil { + return err + } + } } return nil } diff --git a/tests/integration/update.bats b/tests/integration/update.bats index b7a44cb72ce..b1accd5be7a 100644 --- a/tests/integration/update.bats +++ b/tests/integration/update.bats @@ -653,6 +653,17 @@ EOF check_cgroup_value "cpu.rt_period_us" 900001 check_cgroup_value "cpu.rt_runtime_us" 600001 + + # https://github.com/opencontainers/runc/issues/4094 + runc update test_update_rt --cpu-rt-period 10000 --cpu-rt-runtime 3000 + [ "$status" -eq 0 ] + check_cgroup_value "cpu.rt_period_us" 10000 + check_cgroup_value "cpu.rt_runtime_us" 3000 + + runc update test_update_rt --cpu-rt-period 100000 --cpu-rt-runtime 20000 + [ "$status" -eq 0 ] + check_cgroup_value "cpu.rt_period_us" 100000 + check_cgroup_value "cpu.rt_runtime_us" 20000 } @test "update devices [minimal transition rules]" {