From 926b193e066e12e7d9ca24dade37635781af2368 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Wed, 26 Apr 2023 16:21:21 +0200 Subject: [PATCH] memory: enforce memory.high also on hotplug changes Factor out the calculation into a method to ensure it keeps in sync and then use the newly added parameter of the change_memory_limit PVE::CGroup method, bump the dependency in d/control respectively. Signed-off-by: Thomas Lamprecht --- debian/control | 2 +- src/PVE/LXC.pm | 5 +---- src/PVE/LXC/Config.pm | 24 ++++++++++++++++++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/debian/control b/debian/control index dc5980d..a101664 100644 --- a/debian/control +++ b/debian/control @@ -22,7 +22,7 @@ Depends: binutils, file, libpve-access-control(>= 7.2-5), libpve-cluster-perl, - libpve-common-perl (>= 7.2-8), + libpve-common-perl (>= 7.4-1), libpve-guest-common-perl (>= 4.2-3), libpve-storage-perl (>= 7.2-10), lxc-pve, diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm index e058e17..d138161 100644 --- a/src/PVE/LXC.pm +++ b/src/PVE/LXC.pm @@ -687,11 +687,8 @@ sub update_lxc_config { # cgroup memory usage is limited by the hard 'max' limit (OOM-killer enforced) and the soft # 'high' limit (cgroup processes get throttled and put under heavy reclaim pressure). - my $lxc_mem_max = int($memory * 1024 * 1024); + my ($lxc_mem_max, $lxc_mem_high) = PVE::LXC::Config::calculate_memory_constraints($memory); $raw .= "lxc.cgroup2.memory.max = $lxc_mem_max\n"; - # Set the high to 1016/1024 (~99.2%) of the 'max' hard limit clamped to 128 MiB max, to - # scale it for the lower range while having a decent 2^x based rest for 2^y memory configs. - my $lxc_mem_high = $memory >= 16 * 1024 ? int(($memory - 128) * 1024 * 1024) : int($memory * 1024 * 1016); $raw .= "lxc.cgroup2.memory.high = $lxc_mem_high\n"; my $lxcswap = int($swap*1024*1024); diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm index bf424f9..ac9db94 100644 --- a/src/PVE/LXC/Config.pm +++ b/src/PVE/LXC/Config.pm @@ -1303,6 +1303,22 @@ sub option_exists { } # END JSON config code +# takes a max memory value as KiB and returns an tuple with max and high values +sub calculate_memory_constraints { + my ($memory) = @_; + + return if !defined($memory); + + # cgroup memory usage is limited by the hard 'max' limit (OOM-killer enforced) and the soft + # 'high' limit (cgroup processes get throttled and put under heavy reclaim pressure). + my $memory_max = int($memory * 1024 * 1024); + # Set the high to 1016/1024 (~99.2%) of the 'max' hard limit clamped to 128 MiB max, to scale + # it for the lower range while having a decent 2^x based rest for 2^y memory configs. + my $memory_high = $memory >= 16 * 1024 ? int(($memory - 128) * 1024 * 1024) : int($memory * 1024 * 1016); + + return ($memory_max, $memory_high); +} + my $LXC_FASTPLUG_OPTIONS= { 'description' => 1, 'onboot' => 1, @@ -1339,11 +1355,11 @@ sub vmconfig_hotplug_pending { # memory (iow. memory+swap). This means we have to change them together. my $hotplug_memory_done; my $hotplug_memory = sub { - my ($wanted_memory, $wanted_swap) = @_; + my ($new_memory, $new_swap) = @_; - $wanted_memory = int($wanted_memory * 1024 * 1024) if defined($wanted_memory); - $wanted_swap = int($wanted_swap * 1024 * 1024) if defined($wanted_swap); - $cgroup->change_memory_limit($wanted_memory, $wanted_swap); + ($new_memory, my $new_memory_high) = calculate_memory_constraints($new_memory); + $new_swap = int($new_swap * 1024 * 1024) if defined($new_swap); + $cgroup->change_memory_limit($new_memory, $new_swap, $new_memory_high); $hotplug_memory_done = 1; }; -- 2.39.5