diff mbox series

[v1] swapon01.c: Apply a margin to avoid oom

Message ID 20240314013320.5536-1-wegao@suse.com
State Changes Requested
Headers show
Series [v1] swapon01.c: Apply a margin to avoid oom | expand

Commit Message

Wei Gao March 14, 2024, 1:33 a.m. UTC
During our test i find a oom error during test which triggered by
tst_pollute_memory.

kernel: oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=/,mems_allowed=0-1,oom_memcg=/ltp/test-9552,task_memcg=/ltp/test-95520
kernel: Memory cgroup out of memory: Killed process 9575 (swapon01) total-vm:1051484kB, anon-rss:1046572kB, file-rss:1280kB, shmem-rss:0k0

Signed-off-by: Wei Gao <wegao@suse.com>
---
 testcases/kernel/syscalls/swapon/swapon01.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Li Wang March 14, 2024, 6:40 a.m. UTC | #1
Hi Wei,

On Thu, Mar 14, 2024 at 9:33 AM Wei Gao via ltp <ltp@lists.linux.it> wrote:

> During our test i find a oom error during test which triggered by
> tst_pollute_memory.
>
> kernel:
> oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=/,mems_allowed=0-1,oom_memcg=/ltp/test-9552,task_memcg=/ltp/test-95520
> kernel: Memory cgroup out of memory: Killed process 9575 (swapon01)
> total-vm:1051484kB, anon-rss:1046572kB, file-rss:1280kB, shmem-rss:0k0
>
> Signed-off-by: Wei Gao <wegao@suse.com>
> ---
>  testcases/kernel/syscalls/swapon/swapon01.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> b/testcases/kernel/syscalls/swapon/swapon01.c
> index d406e4bd9..357992525 100644
> --- a/testcases/kernel/syscalls/swapon/swapon01.c
> +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> @@ -41,7 +41,9 @@ static void setup(void)
>         make_swapfile(SWAP_FILE, 10, 0);
>
>         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
> -       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> +
> +       /* Apply a margin to avoid oom during tst_pollute_memory */
> +       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM + TESTMEM/100);
>

Even with a bit of margin added there is still not guarantee to avoid OOM
completely.

As the Cgroup V1 and V2 track memory and swap in two ways, if the margin is
too large that might not make the swap happen which is not our expectation.

So other method I can think of is to disable the OOM in Cgoup,
    echo 1 > memory.oom_control
or, set `overcommit_memory` to 2 to prevent the OOM from occurring.
    echo 2 > /proc/sys/vm/overcommit_memory




>  }
>
>  static struct tst_test test = {
> --
> 2.35.3
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>
>
Wei Gao March 15, 2024, 1:30 a.m. UTC | #2
On Thu, Mar 14, 2024 at 02:40:40PM +0800, Li Wang wrote:
> Hi Wei,
> 
> On Thu, Mar 14, 2024 at 9:33 AM Wei Gao via ltp <ltp@lists.linux.it> wrote:
> 
> > During our test i find a oom error during test which triggered by
> > tst_pollute_memory.
> >
> > kernel:
> > oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=/,mems_allowed=0-1,oom_memcg=/ltp/test-9552,task_memcg=/ltp/test-95520
> > kernel: Memory cgroup out of memory: Killed process 9575 (swapon01)
> > total-vm:1051484kB, anon-rss:1046572kB, file-rss:1280kB, shmem-rss:0k0
> >
> > Signed-off-by: Wei Gao <wegao@suse.com>
> > ---
> >  testcases/kernel/syscalls/swapon/swapon01.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> > b/testcases/kernel/syscalls/swapon/swapon01.c
> > index d406e4bd9..357992525 100644
> > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > @@ -41,7 +41,9 @@ static void setup(void)
> >         make_swapfile(SWAP_FILE, 10, 0);
> >
> >         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
> > -       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> > +
> > +       /* Apply a margin to avoid oom during tst_pollute_memory */
> > +       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM + TESTMEM/100);
> >
> 
> Even with a bit of margin added there is still not guarantee to avoid OOM
> completely.
> 
> As the Cgroup V1 and V2 track memory and swap in two ways, if the margin is
> too large that might not make the swap happen which is not our expectation.
> 
> So other method I can think of is to disable the OOM in Cgoup,
>     echo 1 > memory.oom_control
This option not exist in cgroupv2, it seems no direct oom control for cgroupv2
> or, set `overcommit_memory` to 2 to prevent the OOM from occurring.
>     echo 2 > /proc/sys/vm/overcommit_memory
I have tried this option, oom still happen.
> 
> 
> 
> 
> >  }
> >
> >  static struct tst_test test = {
> > --
> > 2.35.3
> >
> >
> > --
> > Mailing list info: https://lists.linux.it/listinfo/ltp
> >
> >
> 
> -- 
> Regards,
> Li Wang
Li Wang March 15, 2024, 3:17 a.m. UTC | #3
On Fri, Mar 15, 2024 at 9:30 AM Wei Gao <wegao@suse.com> wrote:

> On Thu, Mar 14, 2024 at 02:40:40PM +0800, Li Wang wrote:
> > Hi Wei,
> >
> > On Thu, Mar 14, 2024 at 9:33 AM Wei Gao via ltp <ltp@lists.linux.it>
> wrote:
> >
> > > During our test i find a oom error during test which triggered by
> > > tst_pollute_memory.
> > >
> > > kernel:
> > >
> oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=/,mems_allowed=0-1,oom_memcg=/ltp/test-9552,task_memcg=/ltp/test-95520
> > > kernel: Memory cgroup out of memory: Killed process 9575 (swapon01)
> > > total-vm:1051484kB, anon-rss:1046572kB, file-rss:1280kB, shmem-rss:0k0
> > >
> > > Signed-off-by: Wei Gao <wegao@suse.com>
> > > ---
> > >  testcases/kernel/syscalls/swapon/swapon01.c | 4 +++-
> > >  1 file changed, 3 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> > > b/testcases/kernel/syscalls/swapon/swapon01.c
> > > index d406e4bd9..357992525 100644
> > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > > @@ -41,7 +41,9 @@ static void setup(void)
> > >         make_swapfile(SWAP_FILE, 10, 0);
> > >
> > >         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
> > > -       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> > > +
> > > +       /* Apply a margin to avoid oom during tst_pollute_memory */
> > > +       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM +
> TESTMEM/100);
> > >
> >
> > Even with a bit of margin added there is still not guarantee to avoid OOM
> > completely.
> >
> > As the Cgroup V1 and V2 track memory and swap in two ways, if the margin
> is
> > too large that might not make the swap happen which is not our
> expectation.
> >
> > So other method I can think of is to disable the OOM in Cgoup,
> >     echo 1 > memory.oom_control
> This option not exist in cgroupv2, it seems no direct oom control for
> cgroupv2
>

Ok, which version (found the OOM issue) did you test on?
Maybe here we have to split the control work into two parts (V1 and V2).



> > or, set `overcommit_memory` to 2 to prevent the OOM from occurring.
> >     echo 2 > /proc/sys/vm/overcommit_memory
> I have tried this option, oom still happen.
>

Well, it looks like overcommit controlled by overcommit_memory is
allowed for the entire system, not just the Cgroup.
Wei Gao March 15, 2024, 3:44 a.m. UTC | #4
On Fri, Mar 15, 2024 at 11:17:01AM +0800, Li Wang wrote:
> On Fri, Mar 15, 2024 at 9:30 AM Wei Gao <wegao@suse.com> wrote:
> 
> > On Thu, Mar 14, 2024 at 02:40:40PM +0800, Li Wang wrote:
> > > Hi Wei,
> > >
> > > On Thu, Mar 14, 2024 at 9:33 AM Wei Gao via ltp <ltp@lists.linux.it>
> > wrote:
> > >
> > > > During our test i find a oom error during test which triggered by
> > > > tst_pollute_memory.
> > > >
> > > > kernel:
> > > >
> > oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=/,mems_allowed=0-1,oom_memcg=/ltp/test-9552,task_memcg=/ltp/test-95520
> > > > kernel: Memory cgroup out of memory: Killed process 9575 (swapon01)
> > > > total-vm:1051484kB, anon-rss:1046572kB, file-rss:1280kB, shmem-rss:0k0
> > > >
> > > > Signed-off-by: Wei Gao <wegao@suse.com>
> > > > ---
> > > >  testcases/kernel/syscalls/swapon/swapon01.c | 4 +++-
> > > >  1 file changed, 3 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> > > > b/testcases/kernel/syscalls/swapon/swapon01.c
> > > > index d406e4bd9..357992525 100644
> > > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > > > @@ -41,7 +41,9 @@ static void setup(void)
> > > >         make_swapfile(SWAP_FILE, 10, 0);
> > > >
> > > >         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
> > > > -       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> > > > +
> > > > +       /* Apply a margin to avoid oom during tst_pollute_memory */
> > > > +       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM +
> > TESTMEM/100);
> > > >
> > >
> > > Even with a bit of margin added there is still not guarantee to avoid OOM
> > > completely.
> > >
> > > As the Cgroup V1 and V2 track memory and swap in two ways, if the margin
> > is
> > > too large that might not make the swap happen which is not our
> > expectation.
> > >
> > > So other method I can think of is to disable the OOM in Cgoup,
> > >     echo 1 > memory.oom_control
> > This option not exist in cgroupv2, it seems no direct oom control for
> > cgroupv2
> >
> 
> Ok, which version (found the OOM issue) did you test on?
mount | grep cgroup
cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
> Maybe here we have to split the control work into two parts (V1 and V2).
How to disable OOM and also let swap action happen at the same time for cgroup V2?

Thanks for your such quick feedback :)
> 
> 
> 
> > > or, set `overcommit_memory` to 2 to prevent the OOM from occurring.
> > >     echo 2 > /proc/sys/vm/overcommit_memory
> > I have tried this option, oom still happen.
> >
> 
> Well, it looks like overcommit controlled by overcommit_memory is
> allowed for the entire system, not just the Cgroup.
> 
> 
> -- 
> Regards,
> Li Wang
Li Wang March 15, 2024, 4:09 a.m. UTC | #5
On Fri, Mar 15, 2024 at 11:44 AM Wei Gao <wegao@suse.com> wrote:

> On Fri, Mar 15, 2024 at 11:17:01AM +0800, Li Wang wrote:
> > On Fri, Mar 15, 2024 at 9:30 AM Wei Gao <wegao@suse.com> wrote:
> >
> > > On Thu, Mar 14, 2024 at 02:40:40PM +0800, Li Wang wrote:
> > > > Hi Wei,
> > > >
> > > > On Thu, Mar 14, 2024 at 9:33 AM Wei Gao via ltp <ltp@lists.linux.it>
> > > wrote:
> > > >
> > > > > During our test i find a oom error during test which triggered by
> > > > > tst_pollute_memory.
> > > > >
> > > > > kernel:
> > > > >
> > >
> oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=/,mems_allowed=0-1,oom_memcg=/ltp/test-9552,task_memcg=/ltp/test-95520
> > > > > kernel: Memory cgroup out of memory: Killed process 9575 (swapon01)
> > > > > total-vm:1051484kB, anon-rss:1046572kB, file-rss:1280kB,
> shmem-rss:0k0
> > > > >
> > > > > Signed-off-by: Wei Gao <wegao@suse.com>
> > > > > ---
> > > > >  testcases/kernel/syscalls/swapon/swapon01.c | 4 +++-
> > > > >  1 file changed, 3 insertions(+), 1 deletion(-)
> > > > >
> > > > > diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> > > > > b/testcases/kernel/syscalls/swapon/swapon01.c
> > > > > index d406e4bd9..357992525 100644
> > > > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > > > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > > > > @@ -41,7 +41,9 @@ static void setup(void)
> > > > >         make_swapfile(SWAP_FILE, 10, 0);
> > > > >
> > > > >         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
> > > > > -       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> > > > > +
> > > > > +       /* Apply a margin to avoid oom during tst_pollute_memory */
> > > > > +       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM +
> > > TESTMEM/100);
> > > > >
> > > >
> > > > Even with a bit of margin added there is still not guarantee to
> avoid OOM
> > > > completely.
> > > >
> > > > As the Cgroup V1 and V2 track memory and swap in two ways, if the
> margin
> > > is
> > > > too large that might not make the swap happen which is not our
> > > expectation.
> > > >
> > > > So other method I can think of is to disable the OOM in Cgoup,
> > > >     echo 1 > memory.oom_control
> > > This option not exist in cgroupv2, it seems no direct oom control for
> > > cgroupv2
> > >
> >
> > Ok, which version (found the OOM issue) did you test on?
> mount | grep cgroup
> cgroup2 on /sys/fs/cgroup type cgroup2
> (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
> > Maybe here we have to split the control work into two parts (V1 and V2).
>


> How to disable OOM and also let swap action happen at the same time for
> cgroup V2?
>

The Cgroup v2 does not provide a way to disable OOM inside directly.

If we want to avoid OOM happening, just have to manage OOM behavior
in cgroup v2 by setting appropriate values for 'memory.max', 'memory.high',
and using 'memory.oom.group' to determine whether the OOM killer should
target individual processes or the whole Cgroup.

After looking back at this case again, I guess the main reason is that the
swapfile is too small to bear the memory load, one direct method is like
you to enlarge the 'memory.max' value, but that still does not resolve
problems eventually.

One possible solution is crate a bit larger swapfile for the test, and
unlimited
the swap out size. Can you try this patch on your system?

--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -38,10 +38,14 @@ static void verify_swapon(void)
 static void setup(void)
 {
        is_swap_supported(SWAP_FILE);
-       make_swapfile(SWAP_FILE, 10, 0);
+       make_swapfile(SWAP_FILE, 1024, 0);

        SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
        SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
+       if (TST_CG_VER_IS_V1(tst_cg, "memory"))
+               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL);
+       else
+               SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
 }

 static struct tst_test test = {
Li Wang March 15, 2024, 4:19 a.m. UTC | #6
On Fri, Mar 15, 2024 at 12:09 PM Li Wang <liwang@redhat.com> wrote:

>
>
> On Fri, Mar 15, 2024 at 11:44 AM Wei Gao <wegao@suse.com> wrote:
>
>> On Fri, Mar 15, 2024 at 11:17:01AM +0800, Li Wang wrote:
>> > On Fri, Mar 15, 2024 at 9:30 AM Wei Gao <wegao@suse.com> wrote:
>> >
>> > > On Thu, Mar 14, 2024 at 02:40:40PM +0800, Li Wang wrote:
>> > > > Hi Wei,
>> > > >
>> > > > On Thu, Mar 14, 2024 at 9:33 AM Wei Gao via ltp <ltp@lists.linux.it
>> >
>> > > wrote:
>> > > >
>> > > > > During our test i find a oom error during test which triggered by
>> > > > > tst_pollute_memory.
>> > > > >
>> > > > > kernel:
>> > > > >
>> > >
>> oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=/,mems_allowed=0-1,oom_memcg=/ltp/test-9552,task_memcg=/ltp/test-95520
>> > > > > kernel: Memory cgroup out of memory: Killed process 9575
>> (swapon01)
>> > > > > total-vm:1051484kB, anon-rss:1046572kB, file-rss:1280kB,
>> shmem-rss:0k0
>> > > > >
>> > > > > Signed-off-by: Wei Gao <wegao@suse.com>
>> > > > > ---
>> > > > >  testcases/kernel/syscalls/swapon/swapon01.c | 4 +++-
>> > > > >  1 file changed, 3 insertions(+), 1 deletion(-)
>> > > > >
>> > > > > diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
>> > > > > b/testcases/kernel/syscalls/swapon/swapon01.c
>> > > > > index d406e4bd9..357992525 100644
>> > > > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
>> > > > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
>> > > > > @@ -41,7 +41,9 @@ static void setup(void)
>> > > > >         make_swapfile(SWAP_FILE, 10, 0);
>> > > > >
>> > > > >         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
>> > > > > -       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
>> > > > > +
>> > > > > +       /* Apply a margin to avoid oom during tst_pollute_memory
>> */
>> > > > > +       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM +
>> > > TESTMEM/100);
>> > > > >
>> > > >
>> > > > Even with a bit of margin added there is still not guarantee to
>> avoid OOM
>> > > > completely.
>> > > >
>> > > > As the Cgroup V1 and V2 track memory and swap in two ways, if the
>> margin
>> > > is
>> > > > too large that might not make the swap happen which is not our
>> > > expectation.
>> > > >
>> > > > So other method I can think of is to disable the OOM in Cgoup,
>> > > >     echo 1 > memory.oom_control
>> > > This option not exist in cgroupv2, it seems no direct oom control for
>> > > cgroupv2
>> > >
>> >
>> > Ok, which version (found the OOM issue) did you test on?
>> mount | grep cgroup
>> cgroup2 on /sys/fs/cgroup type cgroup2
>> (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
>> > Maybe here we have to split the control work into two parts (V1 and V2).
>>
>
>
>> How to disable OOM and also let swap action happen at the same time for
>> cgroup V2?
>>
>
> The Cgroup v2 does not provide a way to disable OOM inside directly.
>
> If we want to avoid OOM happening, just have to manage OOM behavior
> in cgroup v2 by setting appropriate values for 'memory.max', 'memory.high',
> and using 'memory.oom.group' to determine whether the OOM killer should
> target individual processes or the whole Cgroup.
>
> After looking back at this case again, I guess the main reason is that the
> swapfile is too small to bear the memory load, one direct method is like
> you to enlarge the 'memory.max' value, but that still does not resolve
> problems eventually.
>
> One possible solution is crate a bit larger swapfile for the test, and
> unlimited
> the swap out size. Can you try this patch on your system?
>
> --- a/testcases/kernel/syscalls/swapon/swapon01.c
> +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> @@ -38,10 +38,14 @@ static void verify_swapon(void)
>  static void setup(void)
>  {
>         is_swap_supported(SWAP_FILE);
> -       make_swapfile(SWAP_FILE, 10, 0);
> +       make_swapfile(SWAP_FILE, 1024, 0);
>
>         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
>         SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> +       if (TST_CG_VER_IS_V1(tst_cg, "memory"))
> +               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL);
> +       else
> +               SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
>  }
>
>  static struct tst_test test = {
>

If this still does not work as expected, then we need plus additional
policy to shrink the pollute memory size.

-       tst_pollute_memory(TESTMEM, 0x41);
+       tst_pollute_memory(TESTMEM * 9/10, 0x41);

>
Wei Gao March 15, 2024, 5:24 a.m. UTC | #7
On Fri, Mar 15, 2024 at 12:19:52PM +0800, Li Wang wrote:
> On Fri, Mar 15, 2024 at 12:09 PM Li Wang <liwang@redhat.com> wrote:
> 
> >
> >
> > On Fri, Mar 15, 2024 at 11:44 AM Wei Gao <wegao@suse.com> wrote:
> >
> >> On Fri, Mar 15, 2024 at 11:17:01AM +0800, Li Wang wrote:
> >> > On Fri, Mar 15, 2024 at 9:30 AM Wei Gao <wegao@suse.com> wrote:
> >> >
> >> > > On Thu, Mar 14, 2024 at 02:40:40PM +0800, Li Wang wrote:
> >> > > > Hi Wei,
> >> > > >
> >> > > > On Thu, Mar 14, 2024 at 9:33 AM Wei Gao via ltp <ltp@lists.linux.it
> >> >
> >> > > wrote:
> >> > > >
> >> > > > > During our test i find a oom error during test which triggered by
> >> > > > > tst_pollute_memory.
> >> > > > >
> >> > > > > kernel:
> >> > > > >
> >> > >
> >> oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=/,mems_allowed=0-1,oom_memcg=/ltp/test-9552,task_memcg=/ltp/test-95520
> >> > > > > kernel: Memory cgroup out of memory: Killed process 9575
> >> (swapon01)
> >> > > > > total-vm:1051484kB, anon-rss:1046572kB, file-rss:1280kB,
> >> shmem-rss:0k0
> >> > > > >
> >> > > > > Signed-off-by: Wei Gao <wegao@suse.com>
> >> > > > > ---
> >> > > > >  testcases/kernel/syscalls/swapon/swapon01.c | 4 +++-
> >> > > > >  1 file changed, 3 insertions(+), 1 deletion(-)
> >> > > > >
> >> > > > > diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> >> > > > > b/testcases/kernel/syscalls/swapon/swapon01.c
> >> > > > > index d406e4bd9..357992525 100644
> >> > > > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> >> > > > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> >> > > > > @@ -41,7 +41,9 @@ static void setup(void)
> >> > > > >         make_swapfile(SWAP_FILE, 10, 0);
> >> > > > >
> >> > > > >         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
> >> > > > > -       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> >> > > > > +
> >> > > > > +       /* Apply a margin to avoid oom during tst_pollute_memory
> >> */
> >> > > > > +       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM +
> >> > > TESTMEM/100);
> >> > > > >
> >> > > >
> >> > > > Even with a bit of margin added there is still not guarantee to
> >> avoid OOM
> >> > > > completely.
> >> > > >
> >> > > > As the Cgroup V1 and V2 track memory and swap in two ways, if the
> >> margin
> >> > > is
> >> > > > too large that might not make the swap happen which is not our
> >> > > expectation.
> >> > > >
> >> > > > So other method I can think of is to disable the OOM in Cgoup,
> >> > > >     echo 1 > memory.oom_control
> >> > > This option not exist in cgroupv2, it seems no direct oom control for
> >> > > cgroupv2
> >> > >
> >> >
> >> > Ok, which version (found the OOM issue) did you test on?
> >> mount | grep cgroup
> >> cgroup2 on /sys/fs/cgroup type cgroup2
> >> (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
> >> > Maybe here we have to split the control work into two parts (V1 and V2).
> >>
> >
> >
> >> How to disable OOM and also let swap action happen at the same time for
> >> cgroup V2?
> >>
> >
> > The Cgroup v2 does not provide a way to disable OOM inside directly.
> >
> > If we want to avoid OOM happening, just have to manage OOM behavior
> > in cgroup v2 by setting appropriate values for 'memory.max', 'memory.high',
> > and using 'memory.oom.group' to determine whether the OOM killer should
> > target individual processes or the whole Cgroup.
> >
> > After looking back at this case again, I guess the main reason is that the
> > swapfile is too small to bear the memory load, one direct method is like
> > you to enlarge the 'memory.max' value, but that still does not resolve
> > problems eventually.
> >
> > One possible solution is crate a bit larger swapfile for the test, and
> > unlimited
> > the swap out size. Can you try this patch on your system?
This not work.
> >
> > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > @@ -38,10 +38,14 @@ static void verify_swapon(void)
> >  static void setup(void)
> >  {
> >         is_swap_supported(SWAP_FILE);
> > -       make_swapfile(SWAP_FILE, 10, 0);
> > +       make_swapfile(SWAP_FILE, 1024, 0);
> >
> >         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
> >         SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> > +       if (TST_CG_VER_IS_V1(tst_cg, "memory"))
> > +               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL);
> > +       else
> > +               SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
> >  }
> >
> >  static struct tst_test test = {
> >
> 
> If this still does not work as expected, then we need plus additional
> policy to shrink the pollute memory size.
> 
> -       tst_pollute_memory(TESTMEM, 0x41);
> +       tst_pollute_memory(TESTMEM * 9/10, 0x41);
This works.
> 
> >
> 
> -- 
> Regards,
> Li Wang
Li Wang March 15, 2024, 5:56 a.m. UTC | #8
On Fri, Mar 15, 2024 at 1:24 PM Wei Gao <wegao@suse.com> wrote:


> > >> How to disable OOM and also let swap action happen at the same time
> for
> > >> cgroup V2?
> > >>
> > >
> > > The Cgroup v2 does not provide a way to disable OOM inside directly.
> > >
> > > If we want to avoid OOM happening, just have to manage OOM behavior
> > > in cgroup v2 by setting appropriate values for 'memory.max',
> 'memory.high',
> > > and using 'memory.oom.group' to determine whether the OOM killer should
> > > target individual processes or the whole Cgroup.
> > >
> > > After looking back at this case again, I guess the main reason is that
> the
> > > swapfile is too small to bear the memory load, one direct method is
> like
> > > you to enlarge the 'memory.max' value, but that still does not resolve
> > > problems eventually.
> > >
> > > One possible solution is crate a bit larger swapfile for the test, and
> > > unlimited
> > > the swap out size. Can you try this patch on your system?
> This not work.
>

Interesting, how about resizing the swapfile size to larger?
can you try that.



> > >
> > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > > @@ -38,10 +38,14 @@ static void verify_swapon(void)
> > >  static void setup(void)
> > >  {
> > >         is_swap_supported(SWAP_FILE);
> > > -       make_swapfile(SWAP_FILE, 10, 0);
> > > +       make_swapfile(SWAP_FILE, 1024, 0);
> > >
> > >         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
> > >         SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> > > +       if (TST_CG_VER_IS_V1(tst_cg, "memory"))
> > > +               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL);
> > > +       else
> > > +               SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
> > >  }
> > >
> > >  static struct tst_test test = {
> > >
> >
> > If this still does not work as expected, then we need plus additional
> > policy to shrink the pollute memory size.
> >
> > -       tst_pollute_memory(TESTMEM, 0x41);
> > +       tst_pollute_memory(TESTMEM * 9/10, 0x41);
> This works.
>

Can you post more info about the test output?

One concern I have is that the swap is not being triggered
with shrinking the pollute memory.

And also it'd be great to have `free -h` `cat /proc/meminfo` `lscpu` `uname
-r` info.
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/swapon/swapon01.c b/testcases/kernel/syscalls/swapon/swapon01.c
index d406e4bd9..357992525 100644
--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -41,7 +41,9 @@  static void setup(void)
 	make_swapfile(SWAP_FILE, 10, 0);
 
 	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
-	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
+
+	/* Apply a margin to avoid oom during tst_pollute_memory */
+	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM + TESTMEM/100);
 }
 
 static struct tst_test test = {