[LTP] [PATCH] memcontrol03: Account for process size in cgroup allocation

Li Wang liwang@redhat.com
Tue May 6 08:59:21 CEST 2025


On Mon, May 5, 2025 at 6:53 PM Martin Doucha <mdoucha@suse.cz> wrote:

> The first trunk_G allocation has 2MB safety margin to avoid triggering
> OOM killer. However, on systems with 64K pagesize, this may not be enough.
> Account for process size as reported by cgroup memory stats before
> allocating memory in child processes.
>
> Signed-off-by: Martin Doucha <mdoucha@suse.cz>
>

Reviewed-by: Li Wang <liwang@redhat.com>

---
>  .../kernel/controllers/memcg/memcontrol03.c   | 20 +++++++++++++++----
>  1 file changed, 16 insertions(+), 4 deletions(-)
>
> diff --git a/testcases/kernel/controllers/memcg/memcontrol03.c
> b/testcases/kernel/controllers/memcg/memcontrol03.c
> index b5bbb9954..d2e489ad6 100644
> --- a/testcases/kernel/controllers/memcg/memcontrol03.c
> +++ b/testcases/kernel/controllers/memcg/memcontrol03.c
> @@ -94,17 +94,23 @@ static void cleanup_sub_groups(void)
>  }
>
>  static void alloc_anon_in_child(const struct tst_cg_group *const cg,
> -                               const size_t size, const int expect_oom)
> +       size_t size, const int expect_oom)
>  {
>         int status;
>         const pid_t pid = SAFE_FORK();
> +       size_t cgmem;
>
>         if (!pid) {
>                 SAFE_CG_PRINTF(cg, "cgroup.procs", "%d", getpid());
> +               SAFE_CG_SCANF(cg, "memory.current", "%zu", &cgmem);
> +               size = size > cgmem ? size - cgmem : 0;
>
>                 tst_res(TINFO, "Child %d in %s: Allocating anon: %"PRIdPTR,
>                 getpid(), tst_cg_group_name(cg), size);
> -               alloc_anon(size);
> +
> +               if (size)
> +                       alloc_anon(size);
> +
>                 exit(0);
>         }
>
> @@ -128,9 +134,10 @@ static void alloc_anon_in_child(const struct
> tst_cg_group *const cg,
>  }
>
>  static void alloc_pagecache_in_child(const struct tst_cg_group *const cg,
> -                                    const size_t size)
> +       size_t size)
>  {
>         const pid_t pid = SAFE_FORK();
> +       size_t cgmem;
>
>         if (pid) {
>                 TST_CHECKPOINT_WAIT(CHILD_IDLE);
> @@ -138,10 +145,15 @@ static void alloc_pagecache_in_child(const struct
> tst_cg_group *const cg,
>         }
>
>         SAFE_CG_PRINTF(cg, "cgroup.procs", "%d", getpid());
> +       SAFE_CG_SCANF(cg, "memory.current", "%zu", &cgmem);
> +       size = size > cgmem ? size - cgmem : 0;
>
>         tst_res(TINFO, "Child %d in %s: Allocating pagecache: %"PRIdPTR,
>                 getpid(), tst_cg_group_name(cg), size);
> -       alloc_pagecache(fd, size);
> +
> +       if (size)
> +               alloc_pagecache(fd, size);
> +
>         SAFE_FSYNC(fd);
>
>         TST_CHECKPOINT_WAKE(CHILD_IDLE);
> --
> 2.49.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>
>

-- 
Regards,
Li Wang


More information about the ltp mailing list