Skip to content

Commit 3016408

Browse files
authored
Merge pull request #4501 from AkihiroSuda/cherrypick-4357
[1.2] runc update: fix updating swap for cgroup v2
2 parents fe36d38 + e0d3953 commit 3016408

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

libcontainer/cgroups/utils.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -415,26 +415,29 @@ func ConvertCPUSharesToCgroupV2Value(cpuShares uint64) uint64 {
415415

416416
// ConvertMemorySwapToCgroupV2Value converts MemorySwap value from OCI spec
417417
// for use by cgroup v2 drivers. A conversion is needed since Resources.MemorySwap
418-
// is defined as memory+swap combined, while in cgroup v2 swap is a separate value.
418+
// is defined as memory+swap combined, while in cgroup v2 swap is a separate value,
419+
// so we need to subtract memory from it where it makes sense.
419420
func ConvertMemorySwapToCgroupV2Value(memorySwap, memory int64) (int64, error) {
420-
// for compatibility with cgroup1 controller, set swap to unlimited in
421-
// case the memory is set to unlimited, and swap is not explicitly set,
422-
// treating the request as "set both memory and swap to unlimited".
423-
if memory == -1 && memorySwap == 0 {
421+
switch {
422+
case memory == -1 && memorySwap == 0:
423+
// For compatibility with cgroup1 controller, set swap to unlimited in
424+
// case the memory is set to unlimited and the swap is not explicitly set,
425+
// treating the request as "set both memory and swap to unlimited".
424426
return -1, nil
425-
}
426-
if memorySwap == -1 || memorySwap == 0 {
427-
// -1 is "max", 0 is "unset", so treat as is
427+
case memorySwap == -1, memorySwap == 0:
428+
// Treat -1 ("max") and 0 ("unset") swap as is.
428429
return memorySwap, nil
429-
}
430-
// sanity checks
431-
if memory == 0 || memory == -1 {
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")
433-
}
434-
if memory < 0 {
436+
case memory < 0:
437+
// Does not make sense to subtract a negative value.
435438
return 0, fmt.Errorf("invalid memory value: %d", memory)
436-
}
437-
if memorySwap < memory {
439+
case memorySwap < memory:
440+
// Sanity check.
438441
return 0, errors.New("memory+swap limit should be >= memory limit")
439442
}
440443

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)