mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-08 21:04:35 +00:00
cgroup: fix the issue of set mem.limit and mem.swap
When update memory limit, we should adapt the write sequence
for memory and swap memory, so it won't fail because
the new value and the old value don't fit kernel's
validation.
Fixes: #1917
Signed-off-by: fupan.lfp <fupan.lfp@antgroup.com>
(cherry picked from commit 30f4834c5b
)
Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
This commit is contained in:
parent
a05e137710
commit
915fea7b1f
@ -349,14 +349,34 @@ fn set_memory_resources(cg: &cgroups::Cgroup, memory: &LinuxMemory, update: bool
|
|||||||
mem_controller.set_kmem_limit(-1)?;
|
mem_controller.set_kmem_limit(-1)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
set_resource!(mem_controller, set_limit, memory, limit);
|
// If the memory update is set to -1 we should also
|
||||||
set_resource!(mem_controller, set_soft_limit, memory, reservation);
|
// set swap to -1, it means unlimited memory.
|
||||||
set_resource!(mem_controller, set_kmem_limit, memory, kernel);
|
let mut swap = memory.swap.unwrap_or(0);
|
||||||
set_resource!(mem_controller, set_tcp_limit, memory, kernel_tcp);
|
if memory.limit == Some(-1) {
|
||||||
|
swap = -1;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(swap) = memory.swap {
|
if memory.limit.is_some() && swap != 0 {
|
||||||
// set memory swap
|
let memstat = get_memory_stats(cg)
|
||||||
let swap = if cg.v2() {
|
.into_option()
|
||||||
|
.ok_or_else(|| anyhow!("failed to get the cgroup memory stats"))?;
|
||||||
|
let memusage = memstat.get_usage();
|
||||||
|
|
||||||
|
// When update memory limit, the kernel would check the current memory limit
|
||||||
|
// set against the new swap setting, if the current memory limit is large than
|
||||||
|
// the new swap, then set limit first, otherwise the kernel would complain and
|
||||||
|
// refused to set; on the other hand, if the current memory limit is smaller than
|
||||||
|
// the new swap, then we should set the swap first and then set the memor limit.
|
||||||
|
if swap == -1 || memusage.get_limit() < swap as u64 {
|
||||||
|
mem_controller.set_memswap_limit(swap)?;
|
||||||
|
set_resource!(mem_controller, set_limit, memory, limit);
|
||||||
|
} else {
|
||||||
|
set_resource!(mem_controller, set_limit, memory, limit);
|
||||||
|
mem_controller.set_memswap_limit(swap)?;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
set_resource!(mem_controller, set_limit, memory, limit);
|
||||||
|
swap = if cg.v2() {
|
||||||
convert_memory_swap_to_v2_value(swap, memory.limit.unwrap_or(0))?
|
convert_memory_swap_to_v2_value(swap, memory.limit.unwrap_or(0))?
|
||||||
} else {
|
} else {
|
||||||
swap
|
swap
|
||||||
@ -366,6 +386,10 @@ fn set_memory_resources(cg: &cgroups::Cgroup, memory: &LinuxMemory, update: bool
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set_resource!(mem_controller, set_soft_limit, memory, reservation);
|
||||||
|
set_resource!(mem_controller, set_kmem_limit, memory, kernel);
|
||||||
|
set_resource!(mem_controller, set_tcp_limit, memory, kernel_tcp);
|
||||||
|
|
||||||
if let Some(swappiness) = memory.swappiness {
|
if let Some(swappiness) = memory.swappiness {
|
||||||
if (0..=100).contains(&swappiness) {
|
if (0..=100).contains(&swappiness) {
|
||||||
mem_controller.set_swappiness(swappiness as u64)?;
|
mem_controller.set_swappiness(swappiness as u64)?;
|
||||||
|
Loading…
Reference in New Issue
Block a user