From: Alireza Asgari Add KUnit tests for memory.high delay arithmetic on top of the 32-bit and u64 overflow fixes without allocating large amounts of memory or sleeping. Factor the caller's memory plus swap sum and final clamp out of __mem_cgroup_handle_over_high() into calculate_total_high_delay() so the test exercises the production clamp rather than duplicating min() locally. Cover two arithmetic cases: - memcg_high_test_delay_clamp: 3x high (usage=300, high=100, nr_pages=1) yields 4 * HZ from calculate_high_delay() and clamps to MEMCG_MAX_HIGH_DELAY_JIFFIES via calculate_total_high_delay() with no swap overage. - memcg_high_test_delay_overflow: usage=2049, high=1, nr_pages=1 yields max_overage = 2^31 via calculate_overage() and saturates at MEMCG_MAX_HIGH_DELAY_JIFFIES inside calculate_high_delay(). Signed-off-by: Alireza Asgari --- This follow-up applies on top of Hui Zhu's v2: https://lore.kernel.org/r/cover.1796367145.git.zhuhui@kylinos.cn Both KUnit cases passed on 32-bit x86 with HZ=250 and on 64-bit x86 with HZ=1000. --- MAINTAINERS | 1 + mm/Kconfig | 13 +++++++++++++ mm/memcontrol.c | 40 +++++++++++++++++++++++++++------------- mm/tests/memcg_high_kunit.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 13 deletions(-) diff --git a/MAINTAINERS b/MAINTAINERS index b2eaee98852f..12ff6b02aba9 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -6876,6 +6876,7 @@ F: mm/memcontrol-v1.c F: mm/memcontrol-v1.h F: mm/memcontrol.c F: mm/page_counter.c +F: mm/tests/memcg_high_kunit.c F: samples/cgroup/* F: tools/testing/selftests/cgroup/memcg_protection.m F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c diff --git a/mm/Kconfig b/mm/Kconfig index afff1b25b543..d674604f664f 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1530,6 +1530,19 @@ config ARCH_HAS_PTDUMP config PTDUMP bool +config MEMCG_HIGH_KUNIT_TEST + bool "KUnit tests for memory.high delay calculation" if !KUNIT_ALL_TESTS + depends on MEMCG && KUNIT=y + default KUNIT_ALL_TESTS + help + This builds KUnit tests for memory.high delay calculation in + memcontrol.c, covering clamping of ordinary overage delays and + u64 overflow saturation. + + For more information on KUnit and unit tests in general, please + refer to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. source "mm/damon/Kconfig" diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 85b6654e254f..9dea5566bc43 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2608,6 +2608,23 @@ static u64 calculate_high_delay(unsigned int nr_pages, return penalty_jiffies * nr_pages / MEMCG_CHARGE_BATCH; } +static unsigned long calculate_total_high_delay(unsigned int nr_pages, + u64 mem_overage, + u64 swap_overage) +{ + u64 penalty_jiffies; + + penalty_jiffies = calculate_high_delay(nr_pages, mem_overage); + penalty_jiffies += calculate_high_delay(nr_pages, swap_overage); + + /* + * Clamp the max delay per usermode return so as to still keep the + * application moving forwards and also permit diagnostics, albeit + * extremely slowly. + */ + return min(penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES); +} + /* * Reclaims memory over the high limit. Called directly from * try_charge() (context permitting), as well as from the userland @@ -2615,7 +2632,8 @@ static u64 calculate_high_delay(unsigned int nr_pages, */ void __mem_cgroup_handle_over_high(gfp_t gfp_mask) { - u64 penalty_jiffies; + u64 mem_overage, swap_overage; + unsigned long penalty_jiffies; unsigned long pflags; unsigned long nr_reclaimed; unsigned int nr_pages = current->memcg_nr_pages_over_high; @@ -2655,18 +2673,10 @@ void __mem_cgroup_handle_over_high(gfp_t gfp_mask) * memory.high is breached and reclaim is unable to keep up. Throttle * allocators proactively to slow down excessive growth. */ - penalty_jiffies = calculate_high_delay(nr_pages, - mem_find_max_overage(memcg)); - - penalty_jiffies += calculate_high_delay(nr_pages, - swap_find_max_overage(memcg)); - - /* - * Clamp the max delay per usermode return so as to still keep the - * application moving forwards and also permit diagnostics, albeit - * extremely slowly. - */ - penalty_jiffies = min(penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES); + mem_overage = mem_find_max_overage(memcg); + swap_overage = swap_find_max_overage(memcg); + penalty_jiffies = calculate_total_high_delay(nr_pages, mem_overage, + swap_overage); /* * Don't sleep if the amount of jiffies this memcg owes us is so low @@ -6515,3 +6525,7 @@ void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg) K(atomic_long_read(&memcg->memory.children_min_usage)), K(atomic_long_read(&memcg->memory.children_low_usage))); } + +#if IS_ENABLED(CONFIG_MEMCG_HIGH_KUNIT_TEST) +#include "tests/memcg_high_kunit.c" +#endif diff --git a/mm/tests/memcg_high_kunit.c b/mm/tests/memcg_high_kunit.c new file mode 100644 index 000000000000..404504471bb2 --- /dev/null +++ b/mm/tests/memcg_high_kunit.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include + +static void memcg_high_test_delay_clamp(struct kunit *test) +{ + u64 overage = calculate_overage(300, 100); + + /* 3x high with 1 page of debt gives 4 * HZ before the final clamp. */ + KUNIT_EXPECT_EQ(test, calculate_high_delay(1, overage), 4ULL * HZ); + KUNIT_EXPECT_EQ(test, calculate_total_high_delay(1, overage, 0), + MEMCG_MAX_HIGH_DELAY_JIFFIES); +} + +static void memcg_high_test_delay_overflow(struct kunit *test) +{ + /* max_overage = 2^31 overflows u64 when squared and scaled by HZ. */ + u64 overage = calculate_overage(2049, 1); + + KUNIT_EXPECT_EQ(test, calculate_high_delay(1, overage), + MEMCG_MAX_HIGH_DELAY_JIFFIES); +} + +static struct kunit_case memcg_high_test_cases[] = { + KUNIT_CASE(memcg_high_test_delay_clamp), + KUNIT_CASE(memcg_high_test_delay_overflow), + {} +}; + +static struct kunit_suite memcg_high_test_suite = { + .name = "memcg_high", + .test_cases = memcg_high_test_cases, +}; + +kunit_test_suite(memcg_high_test_suite); --- base-commit: d48723711bae9b8cdc56719c448ba42fef28cbee change-id: 20261205-memcg-high-delay-kunit-1ed76a12e255 prerequisite-patch-id: c988b28a239bdb0b1d013ff270719205826c3149 prerequisite-patch-id: d341a7ca8f5f037d70dcee7c7b1572d1a6ad118d Best regards, -- Alireza Asgari