Skip to content

Commit e0d3953

Browse files
kolyshkinAkihiroSuda
authored andcommitted
runc update: fix updating swap for cgroup v2
This allows to do runc update $ID --memory=-1 --memory-swap=$VAL for cgroup v2, i.e. set memory to unlimited and swap to a specific value. This was not possible because ConvertMemorySwapToCgroupV2Value rejected memory=-1 ("unlimited"). In a hindsight, it was a mistake, because if memory limit is unlimited, we should treat memory+swap limit as just swap limit. Revise the unit test; add description to each case. Fixes: c86be8a ("cgroupv2: fix setting MemorySwap") Signed-off-by: Kir Kolyshkin <[email protected]> (cherry picked from commit 732806e) Signed-off-by: Akihiro Suda <[email protected]>
1 parent 914a8f3 commit e0d3953

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

libcontainer/cgroups/utils.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,11 @@ func ConvertMemorySwapToCgroupV2Value(memorySwap, memory int64) (int64, error) {
427427
case memorySwap == -1, memorySwap == 0:
428428
// Treat -1 ("max") and 0 ("unset") swap as is.
429429
return memorySwap, nil
430-
case memory == 0, memory == -1:
431-
// Unset or unlimited memory, can't calculate swap.
430+
case memory == -1:
431+
// Unlimited memory, so treat swap as is.
432+
return memorySwap, nil
433+
case memory == 0:
434+
// Unset or unknown memory, can't calculate swap.
432435
return 0, errors.New("unable to set swap limit without memory limit")
433436
case memory < 0:
434437
// Does not make sense to subtract a negative value.

libcontainer/cgroups/utils_test.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -554,82 +554,97 @@ func TestConvertCPUSharesToCgroupV2Value(t *testing.T) {
554554

555555
func TestConvertMemorySwapToCgroupV2Value(t *testing.T) {
556556
cases := []struct {
557+
descr string
557558
memswap, memory int64
558559
expected int64
559560
expErr bool
560561
}{
561562
{
563+
descr: "all unset",
562564
memswap: 0,
563565
memory: 0,
564566
expected: 0,
565567
},
566568
{
569+
descr: "unlimited memory+swap, unset memory",
567570
memswap: -1,
568571
memory: 0,
569572
expected: -1,
570573
},
571574
{
575+
descr: "unlimited memory",
576+
memswap: 300,
577+
memory: -1,
578+
expected: 300,
579+
},
580+
{
581+
descr: "all unlimited",
572582
memswap: -1,
573583
memory: -1,
574584
expected: -1,
575585
},
576586
{
587+
descr: "negative memory+swap",
577588
memswap: -2,
578589
memory: 0,
579590
expErr: true,
580591
},
581592
{
593+
descr: "unlimited memory+swap, set memory",
582594
memswap: -1,
583595
memory: 1000,
584596
expected: -1,
585597
},
586598
{
599+
descr: "memory+swap == memory",
587600
memswap: 1000,
588601
memory: 1000,
589602
expected: 0,
590603
},
591604
{
605+
descr: "memory+swap > memory",
592606
memswap: 500,
593607
memory: 200,
594608
expected: 300,
595609
},
596610
{
611+
descr: "memory+swap < memory",
597612
memswap: 300,
598613
memory: 400,
599614
expErr: true,
600615
},
601616
{
617+
descr: "unset memory",
602618
memswap: 300,
603619
memory: 0,
604620
expErr: true,
605621
},
606622
{
623+
descr: "negative memory",
607624
memswap: 300,
608625
memory: -300,
609626
expErr: true,
610627
},
611-
{
612-
memswap: 300,
613-
memory: -1,
614-
expErr: true,
615-
},
616628
}
617629

618630
for _, c := range cases {
619-
swap, err := ConvertMemorySwapToCgroupV2Value(c.memswap, c.memory)
620-
if c.expErr {
621-
if err == nil {
622-
t.Errorf("memswap: %d, memory %d, expected error, got %d, nil", c.memswap, c.memory, swap)
631+
c := c
632+
t.Run(c.descr, func(t *testing.T) {
633+
swap, err := ConvertMemorySwapToCgroupV2Value(c.memswap, c.memory)
634+
if c.expErr {
635+
if err == nil {
636+
t.Errorf("memswap: %d, memory %d, expected error, got %d, nil", c.memswap, c.memory, swap)
637+
}
638+
// No more checks.
639+
return
623640
}
624-
// no more checks
625-
continue
626-
}
627-
if err != nil {
628-
t.Errorf("memswap: %d, memory %d, expected success, got error %s", c.memswap, c.memory, err)
629-
}
630-
if swap != c.expected {
631-
t.Errorf("memswap: %d, memory %d, expected %d, got %d", c.memswap, c.memory, c.expected, swap)
632-
}
641+
if err != nil {
642+
t.Errorf("memswap: %d, memory %d, expected success, got error %s", c.memswap, c.memory, err)
643+
}
644+
if swap != c.expected {
645+
t.Errorf("memswap: %d, memory %d, expected %d, got %d", c.memswap, c.memory, c.expected, swap)
646+
}
647+
})
633648
}
634649
}
635650

0 commit comments

Comments
 (0)