diff mbox series

swapon01: swapon01: prevent OOM happening in swap process

Message ID 20240315062448.3181177-1-liwang@redhat.com
State Superseded
Headers show
Series swapon01: swapon01: prevent OOM happening in swap process | expand

Commit Message

Li Wang March 15, 2024, 6:24 a.m. UTC
Here we take many ways to prevent the OOM happening with
run the swapon test:

  * shrink the pollute memory size
  * increase swapfile size
  * setting swap max for Cgroup
  * setting test oom_score_adj to -1000

Reproted-by: Wei Gao <wegao@suse.com>
Signed-off-by: Li Wang <liwang@redhat.com>
---

Notes:
    Hi Wei,
    
       Could you please test this patch and post the output messages?
       Thanks!!

 testcases/kernel/syscalls/swapon/swapon01.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

Comments

Wei Gao March 15, 2024, 7:44 a.m. UTC | #1
On Fri, Mar 15, 2024 at 02:24:48PM +0800, Li Wang wrote:
> Here we take many ways to prevent the OOM happening with
> run the swapon test:
> 
>   * shrink the pollute memory size
>   * increase swapfile size
>   * setting swap max for Cgroup
>   * setting test oom_score_adj to -1000
> 
> Reproted-by: Wei Gao <wegao@suse.com>
> Signed-off-by: Li Wang <liwang@redhat.com>
> ---
> 
> Notes:
>     Hi Wei,
>     
>        Could you please test this patch and post the output messages?
tst_device.c:97: TINFO: Found free device 1 '/dev/loop1'
tst_test.c:1741: TINFO: LTP version: 20240129-94-g8f14b06e0
tst_test.c:1627: TINFO: Timeout per run is 0h 00m 30s
tst_supported_fs_types.c:97: TINFO: Kernel supports ext2
tst_supported_fs_types.c:62: TINFO: mkfs.ext2 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports ext3
tst_supported_fs_types.c:62: TINFO: mkfs.ext3 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports ext4
tst_supported_fs_types.c:62: TINFO: mkfs.ext4 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports xfs
tst_supported_fs_types.c:62: TINFO: mkfs.xfs does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports btrfs
tst_supported_fs_types.c:62: TINFO: mkfs.btrfs does exist
tst_supported_fs_types.c:105: TINFO: Skipping bcachefs because of FUSE blacklist
tst_supported_fs_types.c:97: TINFO: Kernel supports vfat
tst_supported_fs_types.c:62: TINFO: mkfs.vfat does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports exfat
tst_supported_fs_types.c:58: TINFO: mkfs.exfat does not exist
tst_supported_fs_types.c:128: TINFO: Filesystem ntfs is not supported
tst_supported_fs_types.c:97: TINFO: Kernel supports tmpfs
tst_supported_fs_types.c:49: TINFO: mkfs is not needed for tmpfs
tst_test.c:1701: TINFO: === Testing on ext2 ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext2 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint fstyp=ext2 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:30: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on ext3 ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext3 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint fstyp=ext3 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:30: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on ext4 ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext4 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint fstyp=ext4 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:30: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on xfs ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with xfs opts='' extra opts=''
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint fstyp=xfs flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:30: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on btrfs ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with btrfs opts='' extra opts=''
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint fstyp=btrfs flags=0
libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
libswap.c:125: TINFO: File 'mntpoint/swapfile01' is not contiguous
libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:30: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on vfat ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with vfat opts='' extra opts=''
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint fstyp=vfat flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:30: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on tmpfs ===
tst_test.c:1118: TINFO: Skipping mkfs for TMPFS filesystem
tst_test.c:1098: TINFO: Limiting tmpfs size to 32MB
tst_test.c:1132: TINFO: Mounting ltp-tmpfs to /tmp/LTP_swafHGLPe/mntpoint fstyp=tmpfs flags=0
tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
libswap.c:214: TCONF: Swapfile on tmpfs not implemented

Summary:
passed   6
failed   0
broken   0
skipped  1
warnings 0


>        Thanks!!
> 
>  testcases/kernel/syscalls/swapon/swapon01.c | 17 ++++++++++++++---
>  1 file changed, 14 insertions(+), 3 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/swapon/swapon01.c b/testcases/kernel/syscalls/swapon/swapon01.c
> index d406e4bd9..b4fc02d6e 100644
> --- a/testcases/kernel/syscalls/swapon/swapon01.c
> +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> @@ -26,7 +26,7 @@ static void verify_swapon(void)
>  {
>  	TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
>  
> -	tst_pollute_memory(TESTMEM, 0x41);
> +	tst_pollute_memory(TESTMEM * 9/10, 0x41);
>  	tst_res(TINFO, "SwapCached: %ld Kb", SAFE_READ_MEMINFO("SwapCached:"));
>  
>  	if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
> @@ -37,11 +37,21 @@ static void verify_swapon(void)
>  
>  static void setup(void)
>  {
> +	tst_enable_oom_protection(0);
>  	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 void cleanup(void)
> +{
> +	tst_disable_oom_protection(0);
>  }
>  
>  static struct tst_test test = {
> @@ -51,5 +61,6 @@ static struct tst_test test = {
>  	.all_filesystems = 1,
>  	.needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
>  	.test_all = verify_swapon,
> -	.setup = setup
> +	.setup = setup,
> +	.cleanup = cleanup
>  };
> -- 
> 2.40.1
>
Li Wang March 15, 2024, 8:36 a.m. UTC | #2
On Fri, Mar 15, 2024 at 3:44 PM Wei Gao <wegao@suse.com> wrote:

> On Fri, Mar 15, 2024 at 02:24:48PM +0800, Li Wang wrote:
> > Here we take many ways to prevent the OOM happening with
> > run the swapon test:
> >
> >   * shrink the pollute memory size
> >   * increase swapfile size
> >   * setting swap max for Cgroup
> >   * setting test oom_score_adj to -1000
> >
> > Reproted-by: Wei Gao <wegao@suse.com>
> > Signed-off-by: Li Wang <liwang@redhat.com>
> > ---
> >
> > Notes:
> >     Hi Wei,
> >
> >        Could you please test this patch and post the output messages?
> tst_device.c:97: TINFO: Found free device 1 '/dev/loop1'
> tst_test.c:1741: TINFO: LTP version: 20240129-94-g8f14b06e0
> tst_test.c:1627: TINFO: Timeout per run is 0h 00m 30s
> tst_supported_fs_types.c:97: TINFO: Kernel supports ext2
> tst_supported_fs_types.c:62: TINFO: mkfs.ext2 does exist
> tst_supported_fs_types.c:97: TINFO: Kernel supports ext3
> tst_supported_fs_types.c:62: TINFO: mkfs.ext3 does exist
> tst_supported_fs_types.c:97: TINFO: Kernel supports ext4
> tst_supported_fs_types.c:62: TINFO: mkfs.ext4 does exist
> tst_supported_fs_types.c:97: TINFO: Kernel supports xfs
> tst_supported_fs_types.c:62: TINFO: mkfs.xfs does exist
> tst_supported_fs_types.c:97: TINFO: Kernel supports btrfs
> tst_supported_fs_types.c:62: TINFO: mkfs.btrfs does exist
> tst_supported_fs_types.c:105: TINFO: Skipping bcachefs because of FUSE
> blacklist
> tst_supported_fs_types.c:97: TINFO: Kernel supports vfat
> tst_supported_fs_types.c:62: TINFO: mkfs.vfat does exist
> tst_supported_fs_types.c:97: TINFO: Kernel supports exfat
> tst_supported_fs_types.c:58: TINFO: mkfs.exfat does not exist
> tst_supported_fs_types.c:128: TINFO: Filesystem ntfs is not supported
> tst_supported_fs_types.c:97: TINFO: Kernel supports tmpfs
> tst_supported_fs_types.c:49: TINFO: mkfs is not needed for tmpfs
> tst_test.c:1701: TINFO: === Testing on ext2 ===
> tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext2 opts='' extra
> opts=''
> mke2fs 1.47.0 (5-Feb-2023)
> tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> fstyp=ext2 flags=0
> tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> swapon01.c:30: TINFO: SwapCached: 0 Kb
> tst_test.c:1701: TINFO: === Testing on ext3 ===
> tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext3 opts='' extra
> opts=''
> mke2fs 1.47.0 (5-Feb-2023)
> tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> fstyp=ext3 flags=0
> tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> swapon01.c:30: TINFO: SwapCached: 0 Kb
> tst_test.c:1701: TINFO: === Testing on ext4 ===
> tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext4 opts='' extra
> opts=''
> mke2fs 1.47.0 (5-Feb-2023)
> tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> fstyp=ext4 flags=0
> tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> swapon01.c:30: TINFO: SwapCached: 0 Kb
> tst_test.c:1701: TINFO: === Testing on xfs ===
> tst_test.c:1118: TINFO: Formatting /dev/loop1 with xfs opts='' extra
> opts=''
> tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> fstyp=xfs flags=0
> tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> swapon01.c:30: TINFO: SwapCached: 0 Kb
> tst_test.c:1701: TINFO: === Testing on btrfs ===
> tst_test.c:1118: TINFO: Formatting /dev/loop1 with btrfs opts='' extra
> opts=''
> tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> fstyp=btrfs flags=0
> libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
> tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
> libswap.c:125: TINFO: File 'mntpoint/swapfile01' is not contiguous
> libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
> swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> swapon01.c:30: TINFO: SwapCached: 0 Kb
> tst_test.c:1701: TINFO: === Testing on vfat ===
> tst_test.c:1118: TINFO: Formatting /dev/loop1 with vfat opts='' extra
> opts=''
> tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> fstyp=vfat flags=0
> tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
>


> swapon01.c:30: TINFO: SwapCached: 0 Kb
>

That's my concern here, with shrinking the polluted memory size,
swapping is not triggered, this is not what we expected.

And could you `cat /proc/sys/vm/swappiness` before doing the test?

An improved V2 (with setting swappiness to 60 percent):

--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -26,7 +26,7 @@ static void verify_swapon(void)
 {
        TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));

-       tst_pollute_memory(TESTMEM, 0x41);
+       tst_pollute_memory(TESTMEM * 9/10, 0x41);
        tst_res(TINFO, "SwapCached: %ld Kb",
SAFE_READ_MEMINFO("SwapCached:"));

        if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
@@ -37,11 +37,25 @@ static void verify_swapon(void)

 static void setup(void)
 {
+       tst_enable_oom_protection(0);
        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 (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
+               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%ld", TESTMEM *
2);
+
+       if (SAFE_CG_HAS(tst_cg, "memory.swappiness"))
+               SAFE_CG_PRINT(tst_cg, "memory.swappiness", "60");
+       else
+               FILE_PRINTF("/proc/sys/vm/swappiness", "%d", 60);
+}
+
+static void cleanup(void)
+{
+       tst_disable_oom_protection(0);
 }

 static struct tst_test test = {
@@ -50,6 +64,12 @@ static struct tst_test test = {
        .needs_root = 1,
        .all_filesystems = 1,
        .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
+       .save_restore = (const struct tst_path_val[]) {
+               {"/proc/sys/vm/swappiness", NULL,
+                       TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
+               {}
+       },
        .test_all = verify_swapon,
-       .setup = setup
+       .setup = setup,
+       .cleanup = cleanup
 };




> tst_test.c:1701: TINFO: === Testing on tmpfs ===
> tst_test.c:1118: TINFO: Skipping mkfs for TMPFS filesystem
> tst_test.c:1098: TINFO: Limiting tmpfs size to 32MB
> tst_test.c:1132: TINFO: Mounting ltp-tmpfs to /tmp/LTP_swafHGLPe/mntpoint
> fstyp=tmpfs flags=0
> tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
> libswap.c:214: TCONF: Swapfile on tmpfs not implemented
>
> Summary:
> passed   6
> failed   0
> broken   0
> skipped  1
> warnings 0
>
>
> >        Thanks!!
> >
> >  testcases/kernel/syscalls/swapon/swapon01.c | 17 ++++++++++++++---
> >  1 file changed, 14 insertions(+), 3 deletions(-)
> >
> > diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> b/testcases/kernel/syscalls/swapon/swapon01.c
> > index d406e4bd9..b4fc02d6e 100644
> > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > @@ -26,7 +26,7 @@ static void verify_swapon(void)
> >  {
> >       TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
> >
> > -     tst_pollute_memory(TESTMEM, 0x41);
> > +     tst_pollute_memory(TESTMEM * 9/10, 0x41);
> >       tst_res(TINFO, "SwapCached: %ld Kb",
> SAFE_READ_MEMINFO("SwapCached:"));
> >
> >       if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
> > @@ -37,11 +37,21 @@ static void verify_swapon(void)
> >
> >  static void setup(void)
> >  {
> > +     tst_enable_oom_protection(0);
> >       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 void cleanup(void)
> > +{
> > +     tst_disable_oom_protection(0);
> >  }
> >
> >  static struct tst_test test = {
> > @@ -51,5 +61,6 @@ static struct tst_test test = {
> >       .all_filesystems = 1,
> >       .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
> >       .test_all = verify_swapon,
> > -     .setup = setup
> > +     .setup = setup,
> > +     .cleanup = cleanup
> >  };
> > --
> > 2.40.1
> >
>
>
Wei Gao March 15, 2024, 10:52 a.m. UTC | #3
On Fri, Mar 15, 2024 at 04:36:43PM +0800, Li Wang wrote:
> On Fri, Mar 15, 2024 at 3:44 PM Wei Gao <wegao@suse.com> wrote:
> 
> > On Fri, Mar 15, 2024 at 02:24:48PM +0800, Li Wang wrote:
> > > Here we take many ways to prevent the OOM happening with
> > > run the swapon test:
> > >
> > >   * shrink the pollute memory size
> > >   * increase swapfile size
> > >   * setting swap max for Cgroup
> > >   * setting test oom_score_adj to -1000
> > >
> > > Reproted-by: Wei Gao <wegao@suse.com>
> > > Signed-off-by: Li Wang <liwang@redhat.com>
> > > ---
> > >
> > > Notes:
> > >     Hi Wei,
> > >
> > >        Could you please test this patch and post the output messages?
> > tst_device.c:97: TINFO: Found free device 1 '/dev/loop1'
> > tst_test.c:1741: TINFO: LTP version: 20240129-94-g8f14b06e0
> > tst_test.c:1627: TINFO: Timeout per run is 0h 00m 30s
> > tst_supported_fs_types.c:97: TINFO: Kernel supports ext2
> > tst_supported_fs_types.c:62: TINFO: mkfs.ext2 does exist
> > tst_supported_fs_types.c:97: TINFO: Kernel supports ext3
> > tst_supported_fs_types.c:62: TINFO: mkfs.ext3 does exist
> > tst_supported_fs_types.c:97: TINFO: Kernel supports ext4
> > tst_supported_fs_types.c:62: TINFO: mkfs.ext4 does exist
> > tst_supported_fs_types.c:97: TINFO: Kernel supports xfs
> > tst_supported_fs_types.c:62: TINFO: mkfs.xfs does exist
> > tst_supported_fs_types.c:97: TINFO: Kernel supports btrfs
> > tst_supported_fs_types.c:62: TINFO: mkfs.btrfs does exist
> > tst_supported_fs_types.c:105: TINFO: Skipping bcachefs because of FUSE
> > blacklist
> > tst_supported_fs_types.c:97: TINFO: Kernel supports vfat
> > tst_supported_fs_types.c:62: TINFO: mkfs.vfat does exist
> > tst_supported_fs_types.c:97: TINFO: Kernel supports exfat
> > tst_supported_fs_types.c:58: TINFO: mkfs.exfat does not exist
> > tst_supported_fs_types.c:128: TINFO: Filesystem ntfs is not supported
> > tst_supported_fs_types.c:97: TINFO: Kernel supports tmpfs
> > tst_supported_fs_types.c:49: TINFO: mkfs is not needed for tmpfs
> > tst_test.c:1701: TINFO: === Testing on ext2 ===
> > tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext2 opts='' extra
> > opts=''
> > mke2fs 1.47.0 (5-Feb-2023)
> > tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> > fstyp=ext2 flags=0
> > tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > tst_test.c:1701: TINFO: === Testing on ext3 ===
> > tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext3 opts='' extra
> > opts=''
> > mke2fs 1.47.0 (5-Feb-2023)
> > tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> > fstyp=ext3 flags=0
> > tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > tst_test.c:1701: TINFO: === Testing on ext4 ===
> > tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext4 opts='' extra
> > opts=''
> > mke2fs 1.47.0 (5-Feb-2023)
> > tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> > fstyp=ext4 flags=0
> > tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > tst_test.c:1701: TINFO: === Testing on xfs ===
> > tst_test.c:1118: TINFO: Formatting /dev/loop1 with xfs opts='' extra
> > opts=''
> > tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> > fstyp=xfs flags=0
> > tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > tst_test.c:1701: TINFO: === Testing on btrfs ===
> > tst_test.c:1118: TINFO: Formatting /dev/loop1 with btrfs opts='' extra
> > opts=''
> > tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> > fstyp=btrfs flags=0
> > libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
> > tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
> > libswap.c:125: TINFO: File 'mntpoint/swapfile01' is not contiguous
> > libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
> > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > tst_test.c:1701: TINFO: === Testing on vfat ===
> > tst_test.c:1118: TINFO: Formatting /dev/loop1 with vfat opts='' extra
> > opts=''
> > tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swafHGLPe/mntpoint
> > fstyp=vfat flags=0
> > tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> >
> 
> 
> > swapon01.c:30: TINFO: SwapCached: 0 Kb
> >
> 
> That's my concern here, with shrinking the polluted memory size,
> swapping is not triggered, this is not what we expected.
> 
> And could you `cat /proc/sys/vm/swappiness` before doing the test?
before the test the swappiness is 60
cat /proc/sys/vm/swappiness
60

I have tried use swappiness 60 or 100 the result is same(SwapCached: 0 Kb)
tst_test.c:1701: TINFO: === Testing on ext2 ===
tst_test.c:1118: TINFO: Formatting /dev/loop4 with ext2 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop4 to /tmp/LTP_swajCvvix/mntpoint fstyp=ext2 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb <<<<<<<<<

> 
> An improved V2 (with setting swappiness to 60 percent):
> 
> --- a/testcases/kernel/syscalls/swapon/swapon01.c
> +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> @@ -26,7 +26,7 @@ static void verify_swapon(void)
>  {
>         TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
> 
> -       tst_pollute_memory(TESTMEM, 0x41);
> +       tst_pollute_memory(TESTMEM * 9/10, 0x41);
>         tst_res(TINFO, "SwapCached: %ld Kb",
> SAFE_READ_MEMINFO("SwapCached:"));
> 
>         if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
> @@ -37,11 +37,25 @@ static void verify_swapon(void)
> 
>  static void setup(void)
>  {
> +       tst_enable_oom_protection(0);
>         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 (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
> +               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%ld", TESTMEM *
> 2);
> +
> +       if (SAFE_CG_HAS(tst_cg, "memory.swappiness"))
> +               SAFE_CG_PRINT(tst_cg, "memory.swappiness", "60");
> +       else
> +               FILE_PRINTF("/proc/sys/vm/swappiness", "%d", 60);
> +}
> +
> +static void cleanup(void)
> +{
> +       tst_disable_oom_protection(0);
>  }
> 
>  static struct tst_test test = {
> @@ -50,6 +64,12 @@ static struct tst_test test = {
>         .needs_root = 1,
>         .all_filesystems = 1,
>         .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
> +       .save_restore = (const struct tst_path_val[]) {
> +               {"/proc/sys/vm/swappiness", NULL,
> +                       TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
> +               {}
> +       },
>         .test_all = verify_swapon,
> -       .setup = setup
> +       .setup = setup,
> +       .cleanup = cleanup
>  };
> 
> 
> 
> 
> > tst_test.c:1701: TINFO: === Testing on tmpfs ===
> > tst_test.c:1118: TINFO: Skipping mkfs for TMPFS filesystem
> > tst_test.c:1098: TINFO: Limiting tmpfs size to 32MB
> > tst_test.c:1132: TINFO: Mounting ltp-tmpfs to /tmp/LTP_swafHGLPe/mntpoint
> > fstyp=tmpfs flags=0
> > tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
> > libswap.c:214: TCONF: Swapfile on tmpfs not implemented
> >
> > Summary:
> > passed   6
> > failed   0
> > broken   0
> > skipped  1
> > warnings 0
> >
> >
> > >        Thanks!!
> > >
> > >  testcases/kernel/syscalls/swapon/swapon01.c | 17 ++++++++++++++---
> > >  1 file changed, 14 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> > b/testcases/kernel/syscalls/swapon/swapon01.c
> > > index d406e4bd9..b4fc02d6e 100644
> > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > > @@ -26,7 +26,7 @@ static void verify_swapon(void)
> > >  {
> > >       TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
> > >
> > > -     tst_pollute_memory(TESTMEM, 0x41);
> > > +     tst_pollute_memory(TESTMEM * 9/10, 0x41);
> > >       tst_res(TINFO, "SwapCached: %ld Kb",
> > SAFE_READ_MEMINFO("SwapCached:"));
> > >
> > >       if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
> > > @@ -37,11 +37,21 @@ static void verify_swapon(void)
> > >
> > >  static void setup(void)
> > >  {
> > > +     tst_enable_oom_protection(0);
> > >       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 void cleanup(void)
> > > +{
> > > +     tst_disable_oom_protection(0);
> > >  }
> > >
> > >  static struct tst_test test = {
> > > @@ -51,5 +61,6 @@ static struct tst_test test = {
> > >       .all_filesystems = 1,
> > >       .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
> > >       .test_all = verify_swapon,
> > > -     .setup = setup
> > > +     .setup = setup,
> > > +     .cleanup = cleanup
> > >  };
> > > --
> > > 2.40.1
> > >
> >
> >
> 
> -- 
> Regards,
> Li Wang
Li Wang March 17, 2024, 9:52 a.m. UTC | #4
Hi Wei,

Can you try this one and post the test log here?

And again, it'd be helpful to know the config of your SUT.
e.g. `free -h`  `lscpu`  `uname -r`  infoformation


--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -37,11 +37,20 @@ static void verify_swapon(void)

 static void setup(void)
 {
+       tst_enable_oom_protection(0);
        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 (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
+               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%ld", TESTMEM *
2);
+}
+
+static void cleanup(void)
+{
+       tst_disable_oom_protection(0);
 }

 static struct tst_test test = {
@@ -51,5 +60,6 @@ static struct tst_test test = {
        .all_filesystems = 1,
        .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
        .test_all = verify_swapon,
-       .setup = setup
+       .setup = setup,
+       .cleanup = cleanup
 };


On Fri, Mar 15, 2024 at 6:52 PM Wei Gao <wegao@suse.com> wrote:

> On Fri, Mar 15, 2024 at 04:36:43PM +0800, Li Wang wrote:
> > On Fri, Mar 15, 2024 at 3:44 PM Wei Gao <wegao@suse.com> wrote:
> >
> > > On Fri, Mar 15, 2024 at 02:24:48PM +0800, Li Wang wrote:
> > > > Here we take many ways to prevent the OOM happening with
> > > > run the swapon test:
> > > >
> > > >   * shrink the pollute memory size
> > > >   * increase swapfile size
> > > >   * setting swap max for Cgroup
> > > >   * setting test oom_score_adj to -1000
> > > >
> > > > Reproted-by: Wei Gao <wegao@suse.com>
> > > > Signed-off-by: Li Wang <liwang@redhat.com>
> > > > ---
> > > >
> > > > Notes:
> > > >     Hi Wei,
> > > >
> > > >        Could you please test this patch and post the output messages?
> > > tst_device.c:97: TINFO: Found free device 1 '/dev/loop1'
> > > tst_test.c:1741: TINFO: LTP version: 20240129-94-g8f14b06e0
> > > tst_test.c:1627: TINFO: Timeout per run is 0h 00m 30s
> > > tst_supported_fs_types.c:97: TINFO: Kernel supports ext2
> > > tst_supported_fs_types.c:62: TINFO: mkfs.ext2 does exist
> > > tst_supported_fs_types.c:97: TINFO: Kernel supports ext3
> > > tst_supported_fs_types.c:62: TINFO: mkfs.ext3 does exist
> > > tst_supported_fs_types.c:97: TINFO: Kernel supports ext4
> > > tst_supported_fs_types.c:62: TINFO: mkfs.ext4 does exist
> > > tst_supported_fs_types.c:97: TINFO: Kernel supports xfs
> > > tst_supported_fs_types.c:62: TINFO: mkfs.xfs does exist
> > > tst_supported_fs_types.c:97: TINFO: Kernel supports btrfs
> > > tst_supported_fs_types.c:62: TINFO: mkfs.btrfs does exist
> > > tst_supported_fs_types.c:105: TINFO: Skipping bcachefs because of FUSE
> > > blacklist
> > > tst_supported_fs_types.c:97: TINFO: Kernel supports vfat
> > > tst_supported_fs_types.c:62: TINFO: mkfs.vfat does exist
> > > tst_supported_fs_types.c:97: TINFO: Kernel supports exfat
> > > tst_supported_fs_types.c:58: TINFO: mkfs.exfat does not exist
> > > tst_supported_fs_types.c:128: TINFO: Filesystem ntfs is not supported
> > > tst_supported_fs_types.c:97: TINFO: Kernel supports tmpfs
> > > tst_supported_fs_types.c:49: TINFO: mkfs is not needed for tmpfs
> > > tst_test.c:1701: TINFO: === Testing on ext2 ===
> > > tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext2 opts='' extra
> > > opts=''
> > > mke2fs 1.47.0 (5-Feb-2023)
> > > tst_test.c:1132: TINFO: Mounting /dev/loop1 to
> /tmp/LTP_swafHGLPe/mntpoint
> > > fstyp=ext2 flags=0
> > > tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> > > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > > tst_test.c:1701: TINFO: === Testing on ext3 ===
> > > tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext3 opts='' extra
> > > opts=''
> > > mke2fs 1.47.0 (5-Feb-2023)
> > > tst_test.c:1132: TINFO: Mounting /dev/loop1 to
> /tmp/LTP_swafHGLPe/mntpoint
> > > fstyp=ext3 flags=0
> > > tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> > > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > > tst_test.c:1701: TINFO: === Testing on ext4 ===
> > > tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext4 opts='' extra
> > > opts=''
> > > mke2fs 1.47.0 (5-Feb-2023)
> > > tst_test.c:1132: TINFO: Mounting /dev/loop1 to
> /tmp/LTP_swafHGLPe/mntpoint
> > > fstyp=ext4 flags=0
> > > tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> > > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > > tst_test.c:1701: TINFO: === Testing on xfs ===
> > > tst_test.c:1118: TINFO: Formatting /dev/loop1 with xfs opts='' extra
> > > opts=''
> > > tst_test.c:1132: TINFO: Mounting /dev/loop1 to
> /tmp/LTP_swafHGLPe/mntpoint
> > > fstyp=xfs flags=0
> > > tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> > > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > > tst_test.c:1701: TINFO: === Testing on btrfs ===
> > > tst_test.c:1118: TINFO: Formatting /dev/loop1 with btrfs opts='' extra
> > > opts=''
> > > tst_test.c:1132: TINFO: Mounting /dev/loop1 to
> /tmp/LTP_swafHGLPe/mntpoint
> > > fstyp=btrfs flags=0
> > > libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
> > > tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
> > > libswap.c:125: TINFO: File 'mntpoint/swapfile01' is not contiguous
> > > libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
> > > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > > tst_test.c:1701: TINFO: === Testing on vfat ===
> > > tst_test.c:1118: TINFO: Formatting /dev/loop1 with vfat opts='' extra
> > > opts=''
> > > tst_test.c:1132: TINFO: Mounting /dev/loop1 to
> /tmp/LTP_swafHGLPe/mntpoint
> > > fstyp=vfat flags=0
> > > tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> > > swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> > >
> >
> >
> > > swapon01.c:30: TINFO: SwapCached: 0 Kb
> > >
> >
> > That's my concern here, with shrinking the polluted memory size,
> > swapping is not triggered, this is not what we expected.
> >
> > And could you `cat /proc/sys/vm/swappiness` before doing the test?
> before the test the swappiness is 60
> cat /proc/sys/vm/swappiness
> 60
>
> I have tried use swappiness 60 or 100 the result is same(SwapCached: 0 Kb)
> tst_test.c:1701: TINFO: === Testing on ext2 ===
> tst_test.c:1118: TINFO: Formatting /dev/loop4 with ext2 opts='' extra
> opts=''
> mke2fs 1.47.0 (5-Feb-2023)
> tst_test.c:1132: TINFO: Mounting /dev/loop4 to /tmp/LTP_swajCvvix/mntpoint
> fstyp=ext2 flags=0
> tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
> swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
> swapon01.c:31: TINFO: SwapCached: 0 Kb <<<<<<<<<
>
> >
> > An improved V2 (with setting swappiness to 60 percent):
> >
> > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > @@ -26,7 +26,7 @@ static void verify_swapon(void)
> >  {
> >         TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
> >
> > -       tst_pollute_memory(TESTMEM, 0x41);
> > +       tst_pollute_memory(TESTMEM * 9/10, 0x41);
> >         tst_res(TINFO, "SwapCached: %ld Kb",
> > SAFE_READ_MEMINFO("SwapCached:"));
> >
> >         if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
> > @@ -37,11 +37,25 @@ static void verify_swapon(void)
> >
> >  static void setup(void)
> >  {
> > +       tst_enable_oom_protection(0);
> >         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 (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
> > +               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%ld", TESTMEM
> *
> > 2);
> > +
> > +       if (SAFE_CG_HAS(tst_cg, "memory.swappiness"))
> > +               SAFE_CG_PRINT(tst_cg, "memory.swappiness", "60");
> > +       else
> > +               FILE_PRINTF("/proc/sys/vm/swappiness", "%d", 60);
> > +}
> > +
> > +static void cleanup(void)
> > +{
> > +       tst_disable_oom_protection(0);
> >  }
> >
> >  static struct tst_test test = {
> > @@ -50,6 +64,12 @@ static struct tst_test test = {
> >         .needs_root = 1,
> >         .all_filesystems = 1,
> >         .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
> > +       .save_restore = (const struct tst_path_val[]) {
> > +               {"/proc/sys/vm/swappiness", NULL,
> > +                       TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
> > +               {}
> > +       },
> >         .test_all = verify_swapon,
> > -       .setup = setup
> > +       .setup = setup,
> > +       .cleanup = cleanup
> >  };
> >
> >
> >
> >
> > > tst_test.c:1701: TINFO: === Testing on tmpfs ===
> > > tst_test.c:1118: TINFO: Skipping mkfs for TMPFS filesystem
> > > tst_test.c:1098: TINFO: Limiting tmpfs size to 32MB
> > > tst_test.c:1132: TINFO: Mounting ltp-tmpfs to
> /tmp/LTP_swafHGLPe/mntpoint
> > > fstyp=tmpfs flags=0
> > > tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
> > > libswap.c:214: TCONF: Swapfile on tmpfs not implemented
> > >
> > > Summary:
> > > passed   6
> > > failed   0
> > > broken   0
> > > skipped  1
> > > warnings 0
> > >
> > >
> > > >        Thanks!!
> > > >
> > > >  testcases/kernel/syscalls/swapon/swapon01.c | 17 ++++++++++++++---
> > > >  1 file changed, 14 insertions(+), 3 deletions(-)
> > > >
> > > > diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> > > b/testcases/kernel/syscalls/swapon/swapon01.c
> > > > index d406e4bd9..b4fc02d6e 100644
> > > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > > > @@ -26,7 +26,7 @@ static void verify_swapon(void)
> > > >  {
> > > >       TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
> > > >
> > > > -     tst_pollute_memory(TESTMEM, 0x41);
> > > > +     tst_pollute_memory(TESTMEM * 9/10, 0x41);
> > > >       tst_res(TINFO, "SwapCached: %ld Kb",
> > > SAFE_READ_MEMINFO("SwapCached:"));
> > > >
> > > >       if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
> > > > @@ -37,11 +37,21 @@ static void verify_swapon(void)
> > > >
> > > >  static void setup(void)
> > > >  {
> > > > +     tst_enable_oom_protection(0);
> > > >       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 void cleanup(void)
> > > > +{
> > > > +     tst_disable_oom_protection(0);
> > > >  }
> > > >
> > > >  static struct tst_test test = {
> > > > @@ -51,5 +61,6 @@ static struct tst_test test = {
> > > >       .all_filesystems = 1,
> > > >       .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
> > > >       .test_all = verify_swapon,
> > > > -     .setup = setup
> > > > +     .setup = setup,
> > > > +     .cleanup = cleanup
> > > >  };
> > > > --
> > > > 2.40.1
> > > >
> > >
> > >
> >
> > --
> > Regards,
> > Li Wang
>
>
Wei Gao March 18, 2024, 3:20 a.m. UTC | #5
On Sun, Mar 17, 2024 at 05:52:01PM +0800, Li Wang wrote:
> Hi Wei,
> 
> Can you try this one and post the test log here?
> 
> And again, it'd be helpful to know the config of your SUT.
> e.g. `free -h`  `lscpu`  `uname -r`  infoformation
> 
> 
> --- a/testcases/kernel/syscalls/swapon/swapon01.c
> +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> @@ -37,11 +37,20 @@ static void verify_swapon(void)
> 
>  static void setup(void)
>  {
> +       tst_enable_oom_protection(0);
>         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 (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
> +               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%ld", TESTMEM *
> 2);
> +}
> +
> +static void cleanup(void)
> +{
> +       tst_disable_oom_protection(0);
>  }
> 
>  static struct tst_test test = {
> @@ -51,5 +60,6 @@ static struct tst_test test = {
>         .all_filesystems = 1,
>         .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
>         .test_all = verify_swapon,
> -       .setup = setup
> +       .setup = setup,
> +       .cleanup = cleanup
>  };
> 
> 
localhost:~ # free -h <<<< before running case
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       478Mi       3.3Gi       9.0Mi       346Mi       3.4Gi
Swap:             0B          0B          0B
localhost:~ # lscpu
Architecture:            x86_64
  CPU op-mode(s):        32-bit, 64-bit
  Address sizes:         46 bits physical, 48 bits virtual
  Byte Order:            Little Endian
CPU(s):                  2
  On-line CPU(s) list:   0,1
Vendor ID:               GenuineIntel
  BIOS Vendor ID:        QEMU
  Model name:            Intel(R) Xeon(R) Gold 5218R CPU @ 2.10GHz
    BIOS Model name:     pc-i440fx-7.1  CPU @ 2.0GHz
    BIOS CPU family:     1
    CPU family:          6
    Model:               85
    Thread(s) per core:  1
    Core(s) per socket:  1
    Socket(s):           2
    Stepping:            7
    BogoMIPS:            4190.15
    Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1
                         gb rdtscp lm constant_tsc arch_perfmon rep_good nopl xtopology cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16
                         pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnow
                         prefetch cpuid_fault ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust b
                         mi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512
                         vl xsaveopt xsavec xgetbv1 xsaves arat vnmi umip pku ospke avx512_vnni md_clear arch_capabilities
Virtualization features:
  Virtualization:        VT-x
  Hypervisor vendor:     KVM
  Virtualization type:   full
Caches (sum of all):
  L1d:                   64 KiB (2 instances)
  L1i:                   64 KiB (2 instances)
  L2:                    8 MiB (2 instances)
  L3:                    32 MiB (2 instances)
NUMA:
  NUMA node(s):          2
  NUMA node0 CPU(s):     0
  NUMA node1 CPU(s):     1
Vulnerabilities:
  Gather data sampling:  Unknown: Dependent on hypervisor status
  Itlb multihit:         Not affected
  L1tf:                  Not affected
  Mds:                   Not affected
  Meltdown:              Not affected
  Mmio stale data:       Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
  Retbleed:              Mitigation; Enhanced IBRS
  Spec rstack overflow:  Not affected
  Spec store bypass:     Mitigation; Speculative Store Bypass disabled via prctl
  Spectre v1:            Mitigation; usercopy/swapgs barriers and __user pointer sanitization
  Spectre v2:            Mitigation; Enhanced / Automatic IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
  Srbds:                 Not affected
  Tsx async abort:       Mitigation; Clear CPU buffers; SMT Host state unknown
localhost:~ # uname -r
6.4.0-9-default

swapon01                                                                                                                                      100%  851KB 386.5KB/s   00:02
tst_device.c:97: TINFO: Found free device 0 '/dev/loop0'
tst_test.c:1741: TINFO: LTP version: 20240129-94-g8f14b06e0
tst_test.c:1627: TINFO: Timeout per run is 0h 00m 30s
tst_supported_fs_types.c:97: TINFO: Kernel supports ext2
tst_supported_fs_types.c:62: TINFO: mkfs.ext2 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports ext3
tst_supported_fs_types.c:62: TINFO: mkfs.ext3 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports ext4
tst_supported_fs_types.c:62: TINFO: mkfs.ext4 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports xfs
tst_supported_fs_types.c:62: TINFO: mkfs.xfs does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports btrfs
tst_supported_fs_types.c:62: TINFO: mkfs.btrfs does exist
tst_supported_fs_types.c:105: TINFO: Skipping bcachefs because of FUSE blacklist
tst_supported_fs_types.c:97: TINFO: Kernel supports vfat
tst_supported_fs_types.c:62: TINFO: mkfs.vfat does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports exfat
tst_supported_fs_types.c:58: TINFO: mkfs.exfat does not exist
tst_supported_fs_types.c:128: TINFO: Filesystem ntfs is not supported
tst_supported_fs_types.c:97: TINFO: Kernel supports tmpfs
tst_supported_fs_types.c:49: TINFO: mkfs is not needed for tmpfs
tst_test.c:1701: TINFO: === Testing on ext2 ===
tst_test.c:1118: TINFO: Formatting /dev/loop0 with ext2 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop0 to /tmp/LTP_swakWRRNY/mntpoint fstyp=ext2 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
Test timeouted, sending SIGKILL!
tst_test.c:1680: TINFO: If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1
tst_test.c:1681: TBROK: Test killed! (timeout?)

Summary:
passed   1
failed   0
broken   1
skipped  0
warnings 0
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try  1...
tst_device.c:413: TINFO: Likely gvfsd-trash is probing newly mounted fs, kill it to speed up tests.
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try  2...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try  3...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try  4...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try  5...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try  6...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try  7...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try  8...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try  9...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 10...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 11...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 12...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 13...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 14...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 15...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 16...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 17...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 18...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 19...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 20...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 21...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 22...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 23...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 24...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 25...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 26...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 27...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 28...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 29...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 30...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 31...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 32...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 33...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 34...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 35...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 36...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 37...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 38...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 39...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 40...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 41...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 42...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 43...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 44...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 45...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 46...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 47...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 48...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 49...
tst_device.c:409: TINFO: umount('mntpoint') failed with EBUSY, try 50...
tst_device.c:419: TWARN: Failed to umount('mntpoint') after 50 retries
tst_device.c:264: TWARN: ioctl(/dev/loop0, LOOP_CLR_FD, 0) no ENXIO for too long
tst_tmpdir.c:343: TWARN: tst_rmdir: rmobj(/tmp/LTP_swakWRRNY) failed: unlink(/tmp/LTP_swakWRRNY/mntpoint/swapfile01) failed; errno=1: EPERM


localhost:~ # free -h <<<< after running case
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       480Mi       3.3Gi        32Mi       395Mi       3.4Gi
Swap:          3.0Mi          0B       3.0Mi
Li Wang March 18, 2024, 6:32 a.m. UTC | #6
On Mon, Mar 18, 2024 at 11:20 AM Wei Gao <wegao@suse.com> wrote:

> On Sun, Mar 17, 2024 at 05:52:01PM +0800, Li Wang wrote:
> > Hi Wei,
> >
> > Can you try this one and post the test log here?
> >
> > And again, it'd be helpful to know the config of your SUT.
> > e.g. `free -h`  `lscpu`  `uname -r`  infoformation
> >
> >
> > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > @@ -37,11 +37,20 @@ static void verify_swapon(void)
> >
> >  static void setup(void)
> >  {
> > +       tst_enable_oom_protection(0);
> >         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 (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
> > +               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%ld", TESTMEM
> *
> > 2);
> > +}
> > +
> > +static void cleanup(void)
> > +{
> > +       tst_disable_oom_protection(0);
> >  }
> >
> >  static struct tst_test test = {
> > @@ -51,5 +60,6 @@ static struct tst_test test = {
> >         .all_filesystems = 1,
> >         .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
> >         .test_all = verify_swapon,
> > -       .setup = setup
> > +       .setup = setup,
> > +       .cleanup = cleanup
> >  };
> >
> >



>
> localhost:~ # free -h <<<< before running case
>                total        used        free      shared  buff/cache
>  available
> Mem:           3.8Gi       478Mi       3.3Gi       9.0Mi       346Mi
>  3.4Gi
> Swap:             0B          0B          0B
>

I see, it is very likely that your system has no swap space
so that the TESTMEM(1GB) can not be swapped out, then
OOM killer has to be called out.

I can reproduce your problem by disabling all swap files on my system.

So the possible fix way is to reduce the TESTMEM size and increase
the tested swapfile.

I guess this patch can work for you, have a try?

--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -20,7 +20,7 @@

 #define MNTPOINT       "mntpoint"
 #define SWAP_FILE      MNTPOINT"/swapfile01"
-#define TESTMEM                (1UL<<30)
+#define TESTMEM                (1UL<<24)  /* 16MB */

 static void verify_swapon(void)
 {
@@ -38,7 +38,7 @@ static void verify_swapon(void)
 static void setup(void)
 {
        is_swap_supported(SWAP_FILE);
-       make_swapfile(SWAP_FILE, 10, 0);
+       make_swapfile(SWAP_FILE, 4096, 0);

        SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
        SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
Wei Gao March 18, 2024, 7:02 a.m. UTC | #7
On Mon, Mar 18, 2024 at 02:32:41PM +0800, Li Wang wrote:
> On Mon, Mar 18, 2024 at 11:20 AM Wei Gao <wegao@suse.com> wrote:
> 
> > On Sun, Mar 17, 2024 at 05:52:01PM +0800, Li Wang wrote:
> > > Hi Wei,
> > >
> > > Can you try this one and post the test log here?
> > >
> > > And again, it'd be helpful to know the config of your SUT.
> > > e.g. `free -h`  `lscpu`  `uname -r`  infoformation
> > >
> > >
> > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > > @@ -37,11 +37,20 @@ static void verify_swapon(void)
> > >
> > >  static void setup(void)
> > >  {
> > > +       tst_enable_oom_protection(0);
> > >         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 (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
> > > +               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%ld", TESTMEM
> > *
> > > 2);
> > > +}
> > > +
> > > +static void cleanup(void)
> > > +{
> > > +       tst_disable_oom_protection(0);
> > >  }
> > >
> > >  static struct tst_test test = {
> > > @@ -51,5 +60,6 @@ static struct tst_test test = {
> > >         .all_filesystems = 1,
> > >         .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
> > >         .test_all = verify_swapon,
> > > -       .setup = setup
> > > +       .setup = setup,
> > > +       .cleanup = cleanup
> > >  };
> > >
> > >
> 
> 
> 
> >
> > localhost:~ # free -h <<<< before running case
> >                total        used        free      shared  buff/cache
> >  available
> > Mem:           3.8Gi       478Mi       3.3Gi       9.0Mi       346Mi
> >  3.4Gi
> > Swap:             0B          0B          0B
> >
> 
> I see, it is very likely that your system has no swap space
> so that the TESTMEM(1GB) can not be swapped out, then
> OOM killer has to be called out.
> 
> I can reproduce your problem by disabling all swap files on my system.
> 
> So the possible fix way is to reduce the TESTMEM size and increase
> the tested swapfile.
> 
> I guess this patch can work for you, have a try?

No oom, but SwapCached size is unstable, most of time is still 0

wegao:/home/ltp # ./scp.sh
rm -f -f -r swapon01 swapon02 swapon03  *.o *.pyc .cache.mk *.dwo .*.dwo
make -C "/home/ltp/lib" -f "/home/ltp/lib/Makefile" all
make[1]: Entering directory '/home/ltp/lib'
GEN ltp-version.h
make[2]: Nothing to be done for 'all'.
make[2]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/ltp/lib'
BUILD libltpswap.a
make[1]: Nothing to be done for 'all'.
CC testcases/kernel/syscalls/swapon/swapon01
CC testcases/kernel/syscalls/swapon/swapon02
CC testcases/kernel/syscalls/swapon/swapon03
swapon01                                                                                                    100%  851KB 373.6KB/s   00:02
tst_device.c:97: TINFO: Found free device 1 '/dev/loop1'
tst_test.c:1741: TINFO: LTP version: 20240129-92-g9fa305fe3
tst_test.c:1627: TINFO: Timeout per run is 0h 00m 30s
tst_supported_fs_types.c:97: TINFO: Kernel supports ext2
tst_supported_fs_types.c:62: TINFO: mkfs.ext2 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports ext3
tst_supported_fs_types.c:62: TINFO: mkfs.ext3 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports ext4
tst_supported_fs_types.c:62: TINFO: mkfs.ext4 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports xfs
tst_supported_fs_types.c:62: TINFO: mkfs.xfs does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports btrfs
tst_supported_fs_types.c:62: TINFO: mkfs.btrfs does exist
tst_supported_fs_types.c:105: TINFO: Skipping bcachefs because of FUSE blacklist
tst_supported_fs_types.c:97: TINFO: Kernel supports vfat
tst_supported_fs_types.c:62: TINFO: mkfs.vfat does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports exfat
tst_supported_fs_types.c:58: TINFO: mkfs.exfat does not exist
tst_supported_fs_types.c:128: TINFO: Filesystem ntfs is not supported
tst_supported_fs_types.c:97: TINFO: Kernel supports tmpfs
tst_supported_fs_types.c:49: TINFO: mkfs is not needed for tmpfs
tst_test.c:1701: TINFO: === Testing on ext2 ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext2 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaDbjDqs/mntpoint fstyp=ext2 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 56 Kb <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
tst_test.c:1701: TINFO: === Testing on ext3 ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext3 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaDbjDqs/mntpoint fstyp=ext3 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb <<<<<<<<<<<<<<<<<<<<<<<<<<
tst_test.c:1701: TINFO: === Testing on ext4 ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext4 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaDbjDqs/mntpoint fstyp=ext4 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on xfs ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with xfs opts='' extra opts=''
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaDbjDqs/mntpoint fstyp=xfs flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on btrfs ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with btrfs opts='' extra opts=''
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaDbjDqs/mntpoint fstyp=btrfs flags=0
libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
libswap.c:125: TINFO: File 'mntpoint/swapfile01' is not contiguous
libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on vfat ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with vfat opts='' extra opts=''
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaDbjDqs/mntpoint fstyp=vfat flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on tmpfs ===
tst_test.c:1118: TINFO: Skipping mkfs for TMPFS filesystem
tst_test.c:1098: TINFO: Limiting tmpfs size to 32MB
tst_test.c:1132: TINFO: Mounting ltp-tmpfs to /tmp/LTP_swaDbjDqs/mntpoint fstyp=tmpfs flags=0
tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
libswap.c:214: TCONF: Swapfile on tmpfs not implemented

Summary:
passed   6
failed   0
broken   0
skipped  1
warnings 0
wegao:/home/ltp # ./scp.sh
rm -f -f -r swapon01 swapon02 swapon03  *.o *.pyc .cache.mk *.dwo .*.dwo
make -C "/home/ltp/lib" -f "/home/ltp/lib/Makefile" all
make[1]: Entering directory '/home/ltp/lib'
GEN ltp-version.h
make[2]: Nothing to be done for 'all'.
make[2]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/ltp/lib'
BUILD libltpswap.a
make[1]: Nothing to be done for 'all'.
CC testcases/kernel/syscalls/swapon/swapon01
CC testcases/kernel/syscalls/swapon/swapon02
CC testcases/kernel/syscalls/swapon/swapon03
swapon01                                                                                                    100%  851KB  96.5KB/s   00:08
tst_device.c:97: TINFO: Found free device 1 '/dev/loop1'
tst_test.c:1741: TINFO: LTP version: 20240129-92-g9fa305fe3
tst_test.c:1627: TINFO: Timeout per run is 0h 00m 30s
tst_supported_fs_types.c:97: TINFO: Kernel supports ext2
tst_supported_fs_types.c:62: TINFO: mkfs.ext2 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports ext3
tst_supported_fs_types.c:62: TINFO: mkfs.ext3 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports ext4
tst_supported_fs_types.c:62: TINFO: mkfs.ext4 does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports xfs
tst_supported_fs_types.c:62: TINFO: mkfs.xfs does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports btrfs
tst_supported_fs_types.c:62: TINFO: mkfs.btrfs does exist
tst_supported_fs_types.c:105: TINFO: Skipping bcachefs because of FUSE blacklist
tst_supported_fs_types.c:97: TINFO: Kernel supports vfat
tst_supported_fs_types.c:62: TINFO: mkfs.vfat does exist
tst_supported_fs_types.c:97: TINFO: Kernel supports exfat
tst_supported_fs_types.c:58: TINFO: mkfs.exfat does not exist
tst_supported_fs_types.c:128: TINFO: Filesystem ntfs is not supported
tst_supported_fs_types.c:97: TINFO: Kernel supports tmpfs
tst_supported_fs_types.c:49: TINFO: mkfs is not needed for tmpfs
tst_test.c:1701: TINFO: === Testing on ext2 ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext2 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaJSeWym/mntpoint fstyp=ext2 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
tst_test.c:1701: TINFO: === Testing on ext3 ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext3 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaJSeWym/mntpoint fstyp=ext3 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on ext4 ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with ext4 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaJSeWym/mntpoint fstyp=ext4 flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on xfs ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with xfs opts='' extra opts=''
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaJSeWym/mntpoint fstyp=xfs flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on btrfs ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with btrfs opts='' extra opts=''
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaJSeWym/mntpoint fstyp=btrfs flags=0
libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
libswap.c:125: TINFO: File 'mntpoint/swapfile01' is not contiguous
libswap.c:42: TINFO: FS_NOCOW_FL attribute set on mntpoint/swapfile01
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on vfat ===
tst_test.c:1118: TINFO: Formatting /dev/loop1 with vfat opts='' extra opts=''
tst_test.c:1132: TINFO: Mounting /dev/loop1 to /tmp/LTP_swaJSeWym/mntpoint fstyp=vfat flags=0
tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:28: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed
swapon01.c:31: TINFO: SwapCached: 0 Kb
tst_test.c:1701: TINFO: === Testing on tmpfs ===
tst_test.c:1118: TINFO: Skipping mkfs for TMPFS filesystem
tst_test.c:1098: TINFO: Limiting tmpfs size to 32MB
tst_test.c:1132: TINFO: Mounting ltp-tmpfs to /tmp/LTP_swaJSeWym/mntpoint fstyp=tmpfs flags=0
tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
libswap.c:214: TCONF: Swapfile on tmpfs not implemented

Summary:
passed   6
failed   0
broken   0
skipped  1
warnings 0


> 
> --- a/testcases/kernel/syscalls/swapon/swapon01.c
> +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> @@ -20,7 +20,7 @@
> 
>  #define MNTPOINT       "mntpoint"
>  #define SWAP_FILE      MNTPOINT"/swapfile01"
> -#define TESTMEM                (1UL<<30)
> +#define TESTMEM                (1UL<<24)  /* 16MB */
> 
>  static void verify_swapon(void)
>  {
> @@ -38,7 +38,7 @@ static void verify_swapon(void)
>  static void setup(void)
>  {
>         is_swap_supported(SWAP_FILE);
> -       make_swapfile(SWAP_FILE, 10, 0);
> +       make_swapfile(SWAP_FILE, 4096, 0);
> 
>         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
>         SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> 
> 
> -- 
> Regards,
> Li Wang
Li Wang March 18, 2024, 7:26 a.m. UTC | #8
On Mon, Mar 18, 2024 at 3:02 PM Wei Gao <wegao@suse.com> wrote:

> On Mon, Mar 18, 2024 at 02:32:41PM +0800, Li Wang wrote:
> > On Mon, Mar 18, 2024 at 11:20 AM Wei Gao <wegao@suse.com> wrote:
> >
> > > On Sun, Mar 17, 2024 at 05:52:01PM +0800, Li Wang wrote:
> > > > Hi Wei,
> > > >
> > > > Can you try this one and post the test log here?
> > > >
> > > > And again, it'd be helpful to know the config of your SUT.
> > > > e.g. `free -h`  `lscpu`  `uname -r`  infoformation
> > > >
> > > >
> > > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > > > @@ -37,11 +37,20 @@ static void verify_swapon(void)
> > > >
> > > >  static void setup(void)
> > > >  {
> > > > +       tst_enable_oom_protection(0);
> > > >         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 (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
> > > > +               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%ld",
> TESTMEM
> > > *
> > > > 2);
> > > > +}
> > > > +
> > > > +static void cleanup(void)
> > > > +{
> > > > +       tst_disable_oom_protection(0);
> > > >  }
> > > >
> > > >  static struct tst_test test = {
> > > > @@ -51,5 +60,6 @@ static struct tst_test test = {
> > > >         .all_filesystems = 1,
> > > >         .needs_cgroup_ctrls = (const char *const []){ "memory", NULL
> },
> > > >         .test_all = verify_swapon,
> > > > -       .setup = setup
> > > > +       .setup = setup,
> > > > +       .cleanup = cleanup
> > > >  };
> > > >
> > > >
> >
> >
> >
> > >
> > > localhost:~ # free -h <<<< before running case
> > >                total        used        free      shared  buff/cache
> > >  available
> > > Mem:           3.8Gi       478Mi       3.3Gi       9.0Mi       346Mi
> > >  3.4Gi
> > > Swap:             0B          0B          0B
> > >
> >
> > I see, it is very likely that your system has no swap space
> > so that the TESTMEM(1GB) can not be swapped out, then
> > OOM killer has to be called out.
> >
> > I can reproduce your problem by disabling all swap files on my system.
> >
> > So the possible fix way is to reduce the TESTMEM size and increase
> > the tested swapfile.
> >
> > I guess this patch can work for you, have a try?
>
> No oom, but SwapCached size is unstable, most of time is still 0
>


That's because the available swapfile on your SUT is too small,
you can adjust it (then retest it) by yourself to find a proper size.

This is fine as long as the swapfile size is less than 300MB,
otherwise we need to set .dev_min_size like what we did
for swapoff01.c.

And, on the other side, we can't guarantee the system SwapCached
happened every time, it depends on the system's configuration.
Wei Gao March 18, 2024, 12:39 p.m. UTC | #9
On Mon, Mar 18, 2024 at 03:26:00PM +0800, Li Wang wrote:
> On Mon, Mar 18, 2024 at 3:02 PM Wei Gao <wegao@suse.com> wrote:
> 
> > On Mon, Mar 18, 2024 at 02:32:41PM +0800, Li Wang wrote:
> > > On Mon, Mar 18, 2024 at 11:20 AM Wei Gao <wegao@suse.com> wrote:
> > >
> > > > On Sun, Mar 17, 2024 at 05:52:01PM +0800, Li Wang wrote:
> > > > > Hi Wei,
> > > > >
> > > > > Can you try this one and post the test log here?
> > > > >
> > > > > And again, it'd be helpful to know the config of your SUT.
> > > > > e.g. `free -h`  `lscpu`  `uname -r`  infoformation
> > > > >
> > > > >
> > > > > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > > > > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > > > > @@ -37,11 +37,20 @@ static void verify_swapon(void)
> > > > >
> > > > >  static void setup(void)
> > > > >  {
> > > > > +       tst_enable_oom_protection(0);
> > > > >         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 (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
> > > > > +               SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%ld",
> > TESTMEM
> > > > *
> > > > > 2);
> > > > > +}
> > > > > +
> > > > > +static void cleanup(void)
> > > > > +{
> > > > > +       tst_disable_oom_protection(0);
> > > > >  }
> > > > >
> > > > >  static struct tst_test test = {
> > > > > @@ -51,5 +60,6 @@ static struct tst_test test = {
> > > > >         .all_filesystems = 1,
> > > > >         .needs_cgroup_ctrls = (const char *const []){ "memory", NULL
> > },
> > > > >         .test_all = verify_swapon,
> > > > > -       .setup = setup
> > > > > +       .setup = setup,
> > > > > +       .cleanup = cleanup
> > > > >  };
> > > > >
> > > > >
> > >
> > >
> > >
> > > >
> > > > localhost:~ # free -h <<<< before running case
> > > >                total        used        free      shared  buff/cache
> > > >  available
> > > > Mem:           3.8Gi       478Mi       3.3Gi       9.0Mi       346Mi
> > > >  3.4Gi
> > > > Swap:             0B          0B          0B
> > > >
> > >
> > > I see, it is very likely that your system has no swap space
> > > so that the TESTMEM(1GB) can not be swapped out, then
> > > OOM killer has to be called out.
> > >
> > > I can reproduce your problem by disabling all swap files on my system.
> > >
> > > So the possible fix way is to reduce the TESTMEM size and increase
> > > the tested swapfile.
> > >
> > > I guess this patch can work for you, have a try?
> >
> > No oom, but SwapCached size is unstable, most of time is still 0
> >
> 
> 
> That's because the available swapfile on your SUT is too small,
> you can adjust it (then retest it) by yourself to find a proper size.
> 
> This is fine as long as the swapfile size is less than 300MB,
> otherwise we need to set .dev_min_size like what we did
> for swapoff01.c.
> 
> And, on the other side, we can't guarantee the system SwapCached
> happened every time, it depends on the system's configuration.
> 

100M is good enough for current system, could you help check following patch?

--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -14,13 +14,15 @@
 #include <unistd.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <sys/statvfs.h>
 #include "tst_test.h"
 #include "lapi/syscalls.h"
 #include "libswap.h"

 #define MNTPOINT       "mntpoint"
 #define SWAP_FILE      MNTPOINT"/swapfile01"
 #define TESTMEM                (1UL<<30)
+#define SWAP_SIZE         100 * 1024 * 1024  /* 100MB */

 static void verify_swapon(void)
 {
@@ -37,8 +39,16 @@ static void verify_swapon(void)

 static void setup(void)
 {
+       struct statvfs fs_info;
+       unsigned long blk_size;
+
+       if (statvfs(".", &fs_info) == -1)
+               tst_brk(TBROK | TERRNO, "Failed to call statvfs");
+
+       blk_size = fs_info.f_bsize;
+
        is_swap_supported(SWAP_FILE);
-       make_swapfile(SWAP_FILE, 10, 0);
+       make_swapfile(SWAP_FILE, SWAP_SIZE / blk_size , 0);

        SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
        SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);

> 
> -- 
> Regards,
> Li Wang
Li Wang March 19, 2024, 3:28 a.m. UTC | #10
On Mon, Mar 18, 2024 at 8:40 PM Wei Gao <wegao@suse.com> wrote:



>
> > That's because the available swapfile on your SUT is too small,
> > you can adjust it (then retest it) by yourself to find a proper size.
> >
> > This is fine as long as the swapfile size is less than 300MB,
> > otherwise we need to set .dev_min_size like what we did
> > for swapoff01.c.
> >
> > And, on the other side, we can't guarantee the system SwapCached
> > happened every time, it depends on the system's configuration.
> >
>
> 100M is good enough for current system, could you help check following
> patch?
>

Can we rewrite the make_swapfile() API to support passing MB size for
making the swapfile?
Petr Vorel March 19, 2024, 5:03 a.m. UTC | #11
> On Mon, Mar 18, 2024 at 8:40 PM Wei Gao <wegao@suse.com> wrote:




> > > That's because the available swapfile on your SUT is too small,
> > > you can adjust it (then retest it) by yourself to find a proper size.

> > > This is fine as long as the swapfile size is less than 300MB,
> > > otherwise we need to set .dev_min_size like what we did
> > > for swapoff01.c.

> > > And, on the other side, we can't guarantee the system SwapCached
> > > happened every time, it depends on the system's configuration.


> > 100M is good enough for current system, could you help check following
> > patch?


> Can we rewrite the make_swapfile() API to support passing MB size for
> making the swapfile?

I guess it would be desirable (but keep also possible to pass that 10 blocks,
therefore maybe use flag to distinguish between MB and blocks?).

Kind regards,
Petr
Li Wang March 19, 2024, 5:43 a.m. UTC | #12
On Tue, Mar 19, 2024 at 1:04 PM Petr Vorel <pvorel@suse.cz> wrote:

> > On Mon, Mar 18, 2024 at 8:40 PM Wei Gao <wegao@suse.com> wrote:
>
>
>
>
> > > > That's because the available swapfile on your SUT is too small,
> > > > you can adjust it (then retest it) by yourself to find a proper size.
>
> > > > This is fine as long as the swapfile size is less than 300MB,
> > > > otherwise we need to set .dev_min_size like what we did
> > > > for swapoff01.c.
>
> > > > And, on the other side, we can't guarantee the system SwapCached
> > > > happened every time, it depends on the system's configuration.
>
>
> > > 100M is good enough for current system, could you help check following
> > > patch?
>
>
> > Can we rewrite the make_swapfile() API to support passing MB size for
> > making the swapfile?
>
> I guess it would be desirable (but keep also possible to pass that 10
> blocks,
> therefore maybe use flag to distinguish between MB and blocks?).
>

That's fine but a bit complex for users to distinguish flags.

Or, what about making the function use the flag as static, and
export two additional functions with MB and blocks?

enum swapfile_method {
    SWAPFILE_BY_SIZE,
    SWAPFILE_BY_BLOCKS
};

static int make_swapfile(const char *swapfile, unsigned int para, int safe,
enum swapfile_method method) {
    // The main logic to achieve the swapfile-making process
    // ...
}

int make_swapfile_size(const char *swapfile, unsigned int size, int safe) {
    return make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE);
}

int make_swapfile_blks(const char *swapfile, unsigned int blocks, int safe)
{
    return make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS);
}
Li Wang March 19, 2024, 5:51 a.m. UTC | #13
On Tue, Mar 19, 2024 at 1:43 PM Li Wang <liwang@redhat.com> wrote:

>
>
> On Tue, Mar 19, 2024 at 1:04 PM Petr Vorel <pvorel@suse.cz> wrote:
>
>> > On Mon, Mar 18, 2024 at 8:40 PM Wei Gao <wegao@suse.com> wrote:
>>
>>
>>
>>
>> > > > That's because the available swapfile on your SUT is too small,
>> > > > you can adjust it (then retest it) by yourself to find a proper
>> size.
>>
>> > > > This is fine as long as the swapfile size is less than 300MB,
>> > > > otherwise we need to set .dev_min_size like what we did
>> > > > for swapoff01.c.
>>
>> > > > And, on the other side, we can't guarantee the system SwapCached
>> > > > happened every time, it depends on the system's configuration.
>>
>>
>> > > 100M is good enough for current system, could you help check following
>> > > patch?
>>
>>
>> > Can we rewrite the make_swapfile() API to support passing MB size for
>> > making the swapfile?
>>
>> I guess it would be desirable (but keep also possible to pass that 10
>> blocks,
>> therefore maybe use flag to distinguish between MB and blocks?).
>>
>
> That's fine but a bit complex for users to distinguish flags.
>
> Or, what about making the function use the flag as static, and
> export two additional functions with MB and blocks?
>
> enum swapfile_method {
>     SWAPFILE_BY_SIZE,
>     SWAPFILE_BY_BLOCKS
> };
>
> static int make_swapfile(const char *swapfile, unsigned int para, int
> safe, enum swapfile_method method) {
>     // The main logic to achieve the swapfile-making process
>     // ...
> }
>
> int make_swapfile_size(const char *swapfile, unsigned int size, int safe) {
>     return make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE);
> }
>
> int make_swapfile_blks(const char *swapfile, unsigned int blocks, int
> safe) {
>     return make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS);
> }
>

Even simpler to define two macros with one make_swapfile():

#define MAKE_SWAPFILE_SIZE(swapfile, size, safe) \
    make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE)

#define MAKE_SWAPFILE_BLKS(swapfile, blocks, safe) \
    make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS)
Wei Gao March 19, 2024, 7:29 a.m. UTC | #14
On Tue, Mar 19, 2024 at 01:51:10PM +0800, Li Wang wrote:
> On Tue, Mar 19, 2024 at 1:43 PM Li Wang <liwang@redhat.com> wrote:
> 
> >
> >
> > On Tue, Mar 19, 2024 at 1:04 PM Petr Vorel <pvorel@suse.cz> wrote:
> >
> >> > On Mon, Mar 18, 2024 at 8:40 PM Wei Gao <wegao@suse.com> wrote:
> >>
> >>
> >>
> >>
> >> > > > That's because the available swapfile on your SUT is too small,
> >> > > > you can adjust it (then retest it) by yourself to find a proper
> >> size.
> >>
> >> > > > This is fine as long as the swapfile size is less than 300MB,
> >> > > > otherwise we need to set .dev_min_size like what we did
> >> > > > for swapoff01.c.
> >>
> >> > > > And, on the other side, we can't guarantee the system SwapCached
> >> > > > happened every time, it depends on the system's configuration.
> >>
> >>
> >> > > 100M is good enough for current system, could you help check following
> >> > > patch?
> >>
> >>
> >> > Can we rewrite the make_swapfile() API to support passing MB size for
> >> > making the swapfile?
> >>
> >> I guess it would be desirable (but keep also possible to pass that 10
> >> blocks,
> >> therefore maybe use flag to distinguish between MB and blocks?).
> >>
> >
> > That's fine but a bit complex for users to distinguish flags.
> >
> > Or, what about making the function use the flag as static, and
> > export two additional functions with MB and blocks?
> >
> > enum swapfile_method {
> >     SWAPFILE_BY_SIZE,
> >     SWAPFILE_BY_BLOCKS
> > };
> >
> > static int make_swapfile(const char *swapfile, unsigned int para, int
> > safe, enum swapfile_method method) {
> >     // The main logic to achieve the swapfile-making process
> >     // ...
> > }
> >
> > int make_swapfile_size(const char *swapfile, unsigned int size, int safe) {
> >     return make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE);
> > }
> >
> > int make_swapfile_blks(const char *swapfile, unsigned int blocks, int
> > safe) {
> >     return make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS);
> > }
> >
> 
> Even simpler to define two macros with one make_swapfile():
> 
> #define MAKE_SWAPFILE_SIZE(swapfile, size, safe) \
>     make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE)
> 
> #define MAKE_SWAPFILE_BLKS(swapfile, blocks, safe) \
>     make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS)
> 
> 
Make a quick below patch:

diff --git a/include/libswap.h b/include/libswap.h
index 8c75e20ae..c15930615 100644
--- a/include/libswap.h
+++ b/include/libswap.h
@@ -14,7 +14,18 @@
 /*
  * Make a swap file
  */
-int make_swapfile(const char *swapfile, int blocks, int safe);
+enum swapfile_method {
+    SWAPFILE_BY_SIZE,
+    SWAPFILE_BY_BLOCKS
+};
+
+int make_swapfile(const char *swapfile, unsigned int para, int safe, enum swapfile_method method);
+
+#define MAKE_SWAPFILE_SIZE(swapfile, size, safe) \
+    make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE)
+
+#define MAKE_SWAPFILE_BLKS(swapfile, blocks, safe) \
+    make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS)

 /*
  * Check swapon/swapoff support status of filesystems or files
diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index a26ea25e4..88376be05 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -133,18 +133,24 @@ out:
        return contiguous;
 }

-int make_swapfile(const char *swapfile, int blocks, int safe)
+int make_swapfile(const char *swapfile, unsigned int para, int safe, enum swapfile_method method)
 {
        struct statvfs fs_info;
        unsigned long blk_size, bs;
        size_t pg_size = sysconf(_SC_PAGESIZE);
        char mnt_path[100];
+       unsigned int blocks;

        if (statvfs(".", &fs_info) == -1)
                return -1;

        blk_size = fs_info.f_bsize;

+       if (method == SWAPFILE_BY_SIZE)
+               blocks = para * 1024 * 1024 / blk_size;
+       else
+               blocks = para;
+
        /* To guarantee at least one page can be swapped out */
        if (blk_size * blocks < pg_size)
                bs = pg_size;
@@ -181,7 +187,7 @@ int make_swapfile(const char *swapfile, int blocks, int safe)
 bool is_swap_supported(const char *filename)
 {
        int i, sw_support = 0;
-       int ret = make_swapfile(filename, 10, 1);
+       int ret = make_swapfile(filename, 10, 1, SWAPFILE_BY_BLOCKS);
        int fi_contiguous = file_is_contiguous(filename);
        long fs_type = tst_fs_type(filename);
        const char *fstype = tst_fs_type_name(fs_type);
diff --git a/testcases/kernel/syscalls/swapon/swapon01.c b/testcases/kernel/syscalls/swapon/swapon01.c
index d406e4bd9..823813714 100644
--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -38,7 +38,7 @@ static void verify_swapon(void)
 static void setup(void)
 {
        is_swap_supported(SWAP_FILE);
-       make_swapfile(SWAP_FILE, 10, 0);
+       MAKE_SWAPFILE_SIZE(SWAP_FILE, 100, 0);

        SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
        SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
diff --git a/testcases/kernel/syscalls/swapon/swapon02.c b/testcases/kernel/syscalls/swapon/swapon02.c
index 7e876d26a..f76bb28cf 100644
--- a/testcases/kernel/syscalls/swapon/swapon02.c
+++ b/testcases/kernel/syscalls/swapon/swapon02.c
@@ -50,8 +50,8 @@ static void setup(void)
        is_swap_supported(TEST_FILE);

        SAFE_TOUCH(NOTSWAP_FILE, 0777, NULL);
-       make_swapfile(SWAP_FILE, 10, 0);
-       make_swapfile(USED_FILE, 10, 0);
+       MAKE_SWAPFILE_BLKS(SWAP_FILE, 10, 0);
+       MAKE_SWAPFILE_BLKS(USED_FILE, 10, 0);

        if (tst_syscall(__NR_swapon, USED_FILE, 0))
                tst_res(TWARN | TERRNO, "swapon(alreadyused) failed");
diff --git a/testcases/kernel/syscalls/swapon/swapon03.c b/testcases/kernel/syscalls/swapon/swapon03.c
index 6f47fc01f..aaaedfa11 100644
--- a/testcases/kernel/syscalls/swapon/swapon03.c
+++ b/testcases/kernel/syscalls/swapon/swapon03.c
@@ -49,7 +49,7 @@ static int setup_swap(void)

                        /* Create the swapfile */
                        snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
-                       make_swapfile(filename, 10, 0);
+                       MAKE_SWAPFILE_BLKS(filename, 10, 0);

                        /* turn on the swap file */
                        TST_EXP_PASS_SILENT(swapon(filename, 0));
@@ -62,7 +62,7 @@ static int setup_swap(void)
                tst_brk(TFAIL, "Failed to setup swap files");

        tst_res(TINFO, "Successfully created %d swap files", swapfiles);
-       make_swapfile(TEST_FILE, 10, 0);
+       MAKE_SWAPFILE_BLKS(TEST_FILE, 10, 0);

        return 0;
 }

> 
> -- 
> Regards,
> Li Wang
Li Wang March 19, 2024, 8:18 a.m. UTC | #15
On Tue, Mar 19, 2024 at 3:29 PM Wei Gao <wegao@suse.com> wrote:

> On Tue, Mar 19, 2024 at 01:51:10PM +0800, Li Wang wrote:
> > On Tue, Mar 19, 2024 at 1:43 PM Li Wang <liwang@redhat.com> wrote:
> >
> > >
> > >
> > > On Tue, Mar 19, 2024 at 1:04 PM Petr Vorel <pvorel@suse.cz> wrote:
> > >
> > >> > On Mon, Mar 18, 2024 at 8:40 PM Wei Gao <wegao@suse.com> wrote:
> > >>
> > >>
> > >>
> > >>
> > >> > > > That's because the available swapfile on your SUT is too small,
> > >> > > > you can adjust it (then retest it) by yourself to find a proper
> > >> size.
> > >>
> > >> > > > This is fine as long as the swapfile size is less than 300MB,
> > >> > > > otherwise we need to set .dev_min_size like what we did
> > >> > > > for swapoff01.c.
> > >>
> > >> > > > And, on the other side, we can't guarantee the system SwapCached
> > >> > > > happened every time, it depends on the system's configuration.
> > >>
> > >>
> > >> > > 100M is good enough for current system, could you help check
> following
> > >> > > patch?
> > >>
> > >>
> > >> > Can we rewrite the make_swapfile() API to support passing MB size
> for
> > >> > making the swapfile?
> > >>
> > >> I guess it would be desirable (but keep also possible to pass that 10
> > >> blocks,
> > >> therefore maybe use flag to distinguish between MB and blocks?).
> > >>
> > >
> > > That's fine but a bit complex for users to distinguish flags.
> > >
> > > Or, what about making the function use the flag as static, and
> > > export two additional functions with MB and blocks?
> > >
> > > enum swapfile_method {
> > >     SWAPFILE_BY_SIZE,
> > >     SWAPFILE_BY_BLOCKS
> > > };
> > >
> > > static int make_swapfile(const char *swapfile, unsigned int para, int
> > > safe, enum swapfile_method method) {
> > >     // The main logic to achieve the swapfile-making process
> > >     // ...
> > > }
> > >
> > > int make_swapfile_size(const char *swapfile, unsigned int size, int
> safe) {
> > >     return make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE);
> > > }
> > >
> > > int make_swapfile_blks(const char *swapfile, unsigned int blocks, int
> > > safe) {
> > >     return make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS);
> > > }
> > >
> >
> > Even simpler to define two macros with one make_swapfile():
> >
> > #define MAKE_SWAPFILE_SIZE(swapfile, size, safe) \
> >     make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE)
> >
> > #define MAKE_SWAPFILE_BLKS(swapfile, blocks, safe) \
> >     make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS)
> >
> >
> Make a quick below patch:
>

Thanks for your work Wei! (but seems duplicated with my patchset[1])
[1] https://lists.linux.it/pipermail/ltp/2024-March/037627.html

Sorry, I didn't mean to take the credit, I just tested it and found that
the changes are quite simple, so just sent it out...

Would you mind helping out with comments and reviews there?
And I can help to add your SBT tag later.



>
> diff --git a/include/libswap.h b/include/libswap.h
> index 8c75e20ae..c15930615 100644
> --- a/include/libswap.h
> +++ b/include/libswap.h
> @@ -14,7 +14,18 @@
>  /*
>   * Make a swap file
>   */
> -int make_swapfile(const char *swapfile, int blocks, int safe);
> +enum swapfile_method {
> +    SWAPFILE_BY_SIZE,
> +    SWAPFILE_BY_BLOCKS
> +};
> +
> +int make_swapfile(const char *swapfile, unsigned int para, int safe, enum
> swapfile_method method);
> +
> +#define MAKE_SWAPFILE_SIZE(swapfile, size, safe) \
> +    make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE)
> +
> +#define MAKE_SWAPFILE_BLKS(swapfile, blocks, safe) \
> +    make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS)
>
>  /*
>   * Check swapon/swapoff support status of filesystems or files
> diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
> index a26ea25e4..88376be05 100644
> --- a/libs/libltpswap/libswap.c
> +++ b/libs/libltpswap/libswap.c
> @@ -133,18 +133,24 @@ out:
>         return contiguous;
>  }
>
> -int make_swapfile(const char *swapfile, int blocks, int safe)
> +int make_swapfile(const char *swapfile, unsigned int para, int safe, enum
> swapfile_method method)
>  {
>         struct statvfs fs_info;
>         unsigned long blk_size, bs;
>         size_t pg_size = sysconf(_SC_PAGESIZE);
>         char mnt_path[100];
> +       unsigned int blocks;
>
>         if (statvfs(".", &fs_info) == -1)
>                 return -1;
>
>         blk_size = fs_info.f_bsize;
>
> +       if (method == SWAPFILE_BY_SIZE)
> +               blocks = para * 1024 * 1024 / blk_size;
> +       else
> +               blocks = para;
> +
>         /* To guarantee at least one page can be swapped out */
>         if (blk_size * blocks < pg_size)
>                 bs = pg_size;
> @@ -181,7 +187,7 @@ int make_swapfile(const char *swapfile, int blocks,
> int safe)
>  bool is_swap_supported(const char *filename)
>  {
>         int i, sw_support = 0;
> -       int ret = make_swapfile(filename, 10, 1);
> +       int ret = make_swapfile(filename, 10, 1, SWAPFILE_BY_BLOCKS);
>         int fi_contiguous = file_is_contiguous(filename);
>         long fs_type = tst_fs_type(filename);
>         const char *fstype = tst_fs_type_name(fs_type);
> diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> b/testcases/kernel/syscalls/swapon/swapon01.c
> index d406e4bd9..823813714 100644
> --- a/testcases/kernel/syscalls/swapon/swapon01.c
> +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> @@ -38,7 +38,7 @@ static void verify_swapon(void)
>  static void setup(void)
>  {
>         is_swap_supported(SWAP_FILE);
> -       make_swapfile(SWAP_FILE, 10, 0);
> +       MAKE_SWAPFILE_SIZE(SWAP_FILE, 100, 0);
>
>         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
>         SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> diff --git a/testcases/kernel/syscalls/swapon/swapon02.c
> b/testcases/kernel/syscalls/swapon/swapon02.c
> index 7e876d26a..f76bb28cf 100644
> --- a/testcases/kernel/syscalls/swapon/swapon02.c
> +++ b/testcases/kernel/syscalls/swapon/swapon02.c
> @@ -50,8 +50,8 @@ static void setup(void)
>         is_swap_supported(TEST_FILE);
>
>         SAFE_TOUCH(NOTSWAP_FILE, 0777, NULL);
> -       make_swapfile(SWAP_FILE, 10, 0);
> -       make_swapfile(USED_FILE, 10, 0);
> +       MAKE_SWAPFILE_BLKS(SWAP_FILE, 10, 0);
> +       MAKE_SWAPFILE_BLKS(USED_FILE, 10, 0);
>
>         if (tst_syscall(__NR_swapon, USED_FILE, 0))
>                 tst_res(TWARN | TERRNO, "swapon(alreadyused) failed");
> diff --git a/testcases/kernel/syscalls/swapon/swapon03.c
> b/testcases/kernel/syscalls/swapon/swapon03.c
> index 6f47fc01f..aaaedfa11 100644
> --- a/testcases/kernel/syscalls/swapon/swapon03.c
> +++ b/testcases/kernel/syscalls/swapon/swapon03.c
> @@ -49,7 +49,7 @@ static int setup_swap(void)
>
>                         /* Create the swapfile */
>                         snprintf(filename, sizeof(filename), "%s%02d",
> TEST_FILE, j + 2);
> -                       make_swapfile(filename, 10, 0);
> +                       MAKE_SWAPFILE_BLKS(filename, 10, 0);
>
>                         /* turn on the swap file */
>                         TST_EXP_PASS_SILENT(swapon(filename, 0));
> @@ -62,7 +62,7 @@ static int setup_swap(void)
>                 tst_brk(TFAIL, "Failed to setup swap files");
>
>         tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> -       make_swapfile(TEST_FILE, 10, 0);
> +       MAKE_SWAPFILE_BLKS(TEST_FILE, 10, 0);
>
>         return 0;
>  }
>
> >
> > --
> > Regards,
> > Li Wang
>
>
Wei Gao March 19, 2024, 9:20 a.m. UTC | #16
On Tue, Mar 19, 2024 at 04:18:35PM +0800, Li Wang wrote:
> On Tue, Mar 19, 2024 at 3:29 PM Wei Gao <wegao@suse.com> wrote:
> 
> > On Tue, Mar 19, 2024 at 01:51:10PM +0800, Li Wang wrote:
> > > On Tue, Mar 19, 2024 at 1:43 PM Li Wang <liwang@redhat.com> wrote:
> > >
> > > >
> > > >
> > > > On Tue, Mar 19, 2024 at 1:04 PM Petr Vorel <pvorel@suse.cz> wrote:
> > > >
> > > >> > On Mon, Mar 18, 2024 at 8:40 PM Wei Gao <wegao@suse.com> wrote:
> > > >>
> > > >>
> > > >>
> > > >>
> > > >> > > > That's because the available swapfile on your SUT is too small,
> > > >> > > > you can adjust it (then retest it) by yourself to find a proper
> > > >> size.
> > > >>
> > > >> > > > This is fine as long as the swapfile size is less than 300MB,
> > > >> > > > otherwise we need to set .dev_min_size like what we did
> > > >> > > > for swapoff01.c.
> > > >>
> > > >> > > > And, on the other side, we can't guarantee the system SwapCached
> > > >> > > > happened every time, it depends on the system's configuration.
> > > >>
> > > >>
> > > >> > > 100M is good enough for current system, could you help check
> > following
> > > >> > > patch?
> > > >>
> > > >>
> > > >> > Can we rewrite the make_swapfile() API to support passing MB size
> > for
> > > >> > making the swapfile?
> > > >>
> > > >> I guess it would be desirable (but keep also possible to pass that 10
> > > >> blocks,
> > > >> therefore maybe use flag to distinguish between MB and blocks?).
> > > >>
> > > >
> > > > That's fine but a bit complex for users to distinguish flags.
> > > >
> > > > Or, what about making the function use the flag as static, and
> > > > export two additional functions with MB and blocks?
> > > >
> > > > enum swapfile_method {
> > > >     SWAPFILE_BY_SIZE,
> > > >     SWAPFILE_BY_BLOCKS
> > > > };
> > > >
> > > > static int make_swapfile(const char *swapfile, unsigned int para, int
> > > > safe, enum swapfile_method method) {
> > > >     // The main logic to achieve the swapfile-making process
> > > >     // ...
> > > > }
> > > >
> > > > int make_swapfile_size(const char *swapfile, unsigned int size, int
> > safe) {
> > > >     return make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE);
> > > > }
> > > >
> > > > int make_swapfile_blks(const char *swapfile, unsigned int blocks, int
> > > > safe) {
> > > >     return make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS);
> > > > }
> > > >
> > >
> > > Even simpler to define two macros with one make_swapfile():
> > >
> > > #define MAKE_SWAPFILE_SIZE(swapfile, size, safe) \
> > >     make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE)
> > >
> > > #define MAKE_SWAPFILE_BLKS(swapfile, blocks, safe) \
> > >     make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS)
> > >
> > >
> > Make a quick below patch:
> >
> 
> Thanks for your work Wei! (but seems duplicated with my patchset[1])
> [1] https://lists.linux.it/pipermail/ltp/2024-March/037627.html
> 
> Sorry, I didn't mean to take the credit, I just tested it and found that
> the changes are quite simple, so just sent it out...
> 
> Would you mind helping out with comments and reviews there?
> And I can help to add your SBT tag later.
> 
No problem, i have checked your patch and looks good to me, no more comments.
I suppose we can merge this soon, thanks for your patch :)
> 
> 
> >
> > diff --git a/include/libswap.h b/include/libswap.h
> > index 8c75e20ae..c15930615 100644
> > --- a/include/libswap.h
> > +++ b/include/libswap.h
> > @@ -14,7 +14,18 @@
> >  /*
> >   * Make a swap file
> >   */
> > -int make_swapfile(const char *swapfile, int blocks, int safe);
> > +enum swapfile_method {
> > +    SWAPFILE_BY_SIZE,
> > +    SWAPFILE_BY_BLOCKS
> > +};
> > +
> > +int make_swapfile(const char *swapfile, unsigned int para, int safe, enum
> > swapfile_method method);
> > +
> > +#define MAKE_SWAPFILE_SIZE(swapfile, size, safe) \
> > +    make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE)
> > +
> > +#define MAKE_SWAPFILE_BLKS(swapfile, blocks, safe) \
> > +    make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS)
> >
> >  /*
> >   * Check swapon/swapoff support status of filesystems or files
> > diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
> > index a26ea25e4..88376be05 100644
> > --- a/libs/libltpswap/libswap.c
> > +++ b/libs/libltpswap/libswap.c
> > @@ -133,18 +133,24 @@ out:
> >         return contiguous;
> >  }
> >
> > -int make_swapfile(const char *swapfile, int blocks, int safe)
> > +int make_swapfile(const char *swapfile, unsigned int para, int safe, enum
> > swapfile_method method)
> >  {
> >         struct statvfs fs_info;
> >         unsigned long blk_size, bs;
> >         size_t pg_size = sysconf(_SC_PAGESIZE);
> >         char mnt_path[100];
> > +       unsigned int blocks;
> >
> >         if (statvfs(".", &fs_info) == -1)
> >                 return -1;
> >
> >         blk_size = fs_info.f_bsize;
> >
> > +       if (method == SWAPFILE_BY_SIZE)
> > +               blocks = para * 1024 * 1024 / blk_size;
> > +       else
> > +               blocks = para;
> > +
> >         /* To guarantee at least one page can be swapped out */
> >         if (blk_size * blocks < pg_size)
> >                 bs = pg_size;
> > @@ -181,7 +187,7 @@ int make_swapfile(const char *swapfile, int blocks,
> > int safe)
> >  bool is_swap_supported(const char *filename)
> >  {
> >         int i, sw_support = 0;
> > -       int ret = make_swapfile(filename, 10, 1);
> > +       int ret = make_swapfile(filename, 10, 1, SWAPFILE_BY_BLOCKS);
> >         int fi_contiguous = file_is_contiguous(filename);
> >         long fs_type = tst_fs_type(filename);
> >         const char *fstype = tst_fs_type_name(fs_type);
> > diff --git a/testcases/kernel/syscalls/swapon/swapon01.c
> > b/testcases/kernel/syscalls/swapon/swapon01.c
> > index d406e4bd9..823813714 100644
> > --- a/testcases/kernel/syscalls/swapon/swapon01.c
> > +++ b/testcases/kernel/syscalls/swapon/swapon01.c
> > @@ -38,7 +38,7 @@ static void verify_swapon(void)
> >  static void setup(void)
> >  {
> >         is_swap_supported(SWAP_FILE);
> > -       make_swapfile(SWAP_FILE, 10, 0);
> > +       MAKE_SWAPFILE_SIZE(SWAP_FILE, 100, 0);
> >
> >         SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
> >         SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
> > diff --git a/testcases/kernel/syscalls/swapon/swapon02.c
> > b/testcases/kernel/syscalls/swapon/swapon02.c
> > index 7e876d26a..f76bb28cf 100644
> > --- a/testcases/kernel/syscalls/swapon/swapon02.c
> > +++ b/testcases/kernel/syscalls/swapon/swapon02.c
> > @@ -50,8 +50,8 @@ static void setup(void)
> >         is_swap_supported(TEST_FILE);
> >
> >         SAFE_TOUCH(NOTSWAP_FILE, 0777, NULL);
> > -       make_swapfile(SWAP_FILE, 10, 0);
> > -       make_swapfile(USED_FILE, 10, 0);
> > +       MAKE_SWAPFILE_BLKS(SWAP_FILE, 10, 0);
> > +       MAKE_SWAPFILE_BLKS(USED_FILE, 10, 0);
> >
> >         if (tst_syscall(__NR_swapon, USED_FILE, 0))
> >                 tst_res(TWARN | TERRNO, "swapon(alreadyused) failed");
> > diff --git a/testcases/kernel/syscalls/swapon/swapon03.c
> > b/testcases/kernel/syscalls/swapon/swapon03.c
> > index 6f47fc01f..aaaedfa11 100644
> > --- a/testcases/kernel/syscalls/swapon/swapon03.c
> > +++ b/testcases/kernel/syscalls/swapon/swapon03.c
> > @@ -49,7 +49,7 @@ static int setup_swap(void)
> >
> >                         /* Create the swapfile */
> >                         snprintf(filename, sizeof(filename), "%s%02d",
> > TEST_FILE, j + 2);
> > -                       make_swapfile(filename, 10, 0);
> > +                       MAKE_SWAPFILE_BLKS(filename, 10, 0);
> >
> >                         /* turn on the swap file */
> >                         TST_EXP_PASS_SILENT(swapon(filename, 0));
> > @@ -62,7 +62,7 @@ static int setup_swap(void)
> >                 tst_brk(TFAIL, "Failed to setup swap files");
> >
> >         tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> > -       make_swapfile(TEST_FILE, 10, 0);
> > +       MAKE_SWAPFILE_BLKS(TEST_FILE, 10, 0);
> >
> >         return 0;
> >  }
> >
> > >
> > > --
> > > Regards,
> > > Li Wang
> >
> >
> 
> -- 
> Regards,
> Li Wang
Petr Vorel March 19, 2024, 7:23 p.m. UTC | #17
> On Tue, Mar 19, 2024 at 1:04 PM Petr Vorel <pvorel@suse.cz> wrote:

> > > On Mon, Mar 18, 2024 at 8:40 PM Wei Gao <wegao@suse.com> wrote:




> > > > > That's because the available swapfile on your SUT is too small,
> > > > > you can adjust it (then retest it) by yourself to find a proper size.

> > > > > This is fine as long as the swapfile size is less than 300MB,
> > > > > otherwise we need to set .dev_min_size like what we did
> > > > > for swapoff01.c.

> > > > > And, on the other side, we can't guarantee the system SwapCached
> > > > > happened every time, it depends on the system's configuration.


> > > > 100M is good enough for current system, could you help check following
> > > > patch?


> > > Can we rewrite the make_swapfile() API to support passing MB size for
> > > making the swapfile?

> > I guess it would be desirable (but keep also possible to pass that 10
> > blocks,
> > therefore maybe use flag to distinguish between MB and blocks?).


> That's fine but a bit complex for users to distinguish flags.

> Or, what about making the function use the flag as static, and
> export two additional functions with MB and blocks?

Yes, I meant something like that (2 wrappers which would pass the flag
to the 3rd function which actually implements it.

> enum swapfile_method {
>     SWAPFILE_BY_SIZE,
>     SWAPFILE_BY_BLOCKS
> };

> static int make_swapfile(const char *swapfile, unsigned int para, int safe,
> enum swapfile_method method) {
>     // The main logic to achieve the swapfile-making process
>     // ...
> }

> int make_swapfile_size(const char *swapfile, unsigned int size, int safe) {
>     return make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE);
> }

> int make_swapfile_blks(const char *swapfile, unsigned int blocks, int safe)
> {
>     return make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS);
> }

+1

Kind regards,
Petr
Petr Vorel March 19, 2024, 7:24 p.m. UTC | #18
> On Tue, Mar 19, 2024 at 1:43 PM Li Wang <liwang@redhat.com> wrote:



> > On Tue, Mar 19, 2024 at 1:04 PM Petr Vorel <pvorel@suse.cz> wrote:

> >> > On Mon, Mar 18, 2024 at 8:40 PM Wei Gao <wegao@suse.com> wrote:




> >> > > > That's because the available swapfile on your SUT is too small,
> >> > > > you can adjust it (then retest it) by yourself to find a proper
> >> size.

> >> > > > This is fine as long as the swapfile size is less than 300MB,
> >> > > > otherwise we need to set .dev_min_size like what we did
> >> > > > for swapoff01.c.

> >> > > > And, on the other side, we can't guarantee the system SwapCached
> >> > > > happened every time, it depends on the system's configuration.


> >> > > 100M is good enough for current system, could you help check following
> >> > > patch?


> >> > Can we rewrite the make_swapfile() API to support passing MB size for
> >> > making the swapfile?

> >> I guess it would be desirable (but keep also possible to pass that 10
> >> blocks,
> >> therefore maybe use flag to distinguish between MB and blocks?).


> > That's fine but a bit complex for users to distinguish flags.

> > Or, what about making the function use the flag as static, and
> > export two additional functions with MB and blocks?

> > enum swapfile_method {
> >     SWAPFILE_BY_SIZE,
> >     SWAPFILE_BY_BLOCKS
> > };

> > static int make_swapfile(const char *swapfile, unsigned int para, int
> > safe, enum swapfile_method method) {
> >     // The main logic to achieve the swapfile-making process
> >     // ...
> > }

> > int make_swapfile_size(const char *swapfile, unsigned int size, int safe) {
> >     return make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE);
> > }

> > int make_swapfile_blks(const char *swapfile, unsigned int blocks, int
> > safe) {
> >     return make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS);
> > }


> Even simpler to define two macros with one make_swapfile():

> #define MAKE_SWAPFILE_SIZE(swapfile, size, safe) \
>     make_swapfile(swapfile, size, safe, SWAPFILE_BY_SIZE)

> #define MAKE_SWAPFILE_BLKS(swapfile, blocks, safe) \
>     make_swapfile(swapfile, blocks, safe, SWAPFILE_BY_BLOCKS)

+1

Kind regards,
Petr
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/swapon/swapon01.c b/testcases/kernel/syscalls/swapon/swapon01.c
index d406e4bd9..b4fc02d6e 100644
--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -26,7 +26,7 @@  static void verify_swapon(void)
 {
 	TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
 
-	tst_pollute_memory(TESTMEM, 0x41);
+	tst_pollute_memory(TESTMEM * 9/10, 0x41);
 	tst_res(TINFO, "SwapCached: %ld Kb", SAFE_READ_MEMINFO("SwapCached:"));
 
 	if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
@@ -37,11 +37,21 @@  static void verify_swapon(void)
 
 static void setup(void)
 {
+	tst_enable_oom_protection(0);
 	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 void cleanup(void)
+{
+	tst_disable_oom_protection(0);
 }
 
 static struct tst_test test = {
@@ -51,5 +61,6 @@  static struct tst_test test = {
 	.all_filesystems = 1,
 	.needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
 	.test_all = verify_swapon,
-	.setup = setup
+	.setup = setup,
+	.cleanup = cleanup
 };