public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v3 0/2] swapon03: Try to swapon() as many files until it fails
@ 2025-11-18 14:36 Petr Vorel
  2025-11-18 14:36 ` [LTP] [PATCH v3 1/2] swapon03: Remove fork in setup Petr Vorel
  2025-11-18 14:36 ` [LTP] [PATCH v3 2/2] swapon03: Try to swapon() as many files until it fails Petr Vorel
  0 siblings, 2 replies; 9+ messages in thread
From: Petr Vorel @ 2025-11-18 14:36 UTC (permalink / raw)
  To: ltp

Changes v2->v3:
* Remove fork() (Cyril).
* Add -i2 to runtest/syscalls.
* Postpone other cleanup to later to speedup and simplify.

Link to v2:
https://lore.kernel.org/ltp/20251106163500.1063704-1-pvorel@suse.cz/

Link to v1:
https://lore.kernel.org/ltp/20251105154716.995786-1-pvorel@suse.cz/T/#t
https://patchwork.ozlabs.org/project/ltp/list/?series=481055&state=*

Petr Vorel (2):
  swapon03: Remove fork in setup
  swapon03: Try to swapon() as many files until it fails

 runtest/syscalls                            |  2 +-
 testcases/kernel/syscalls/swapon/swapon03.c | 67 +++++++++++----------
 2 files changed, 35 insertions(+), 34 deletions(-)

-- 
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [LTP] [PATCH v3 1/2] swapon03: Remove fork in setup
  2025-11-18 14:36 [LTP] [PATCH v3 0/2] swapon03: Try to swapon() as many files until it fails Petr Vorel
@ 2025-11-18 14:36 ` Petr Vorel
  2025-11-19 14:57   ` Avinesh Kumar
  2025-12-11 10:19   ` Cyril Hrubis
  2025-11-18 14:36 ` [LTP] [PATCH v3 2/2] swapon03: Try to swapon() as many files until it fails Petr Vorel
  1 sibling, 2 replies; 9+ messages in thread
From: Petr Vorel @ 2025-11-18 14:36 UTC (permalink / raw)
  To: ltp

This is probably some leftover from the conversion to the new library.
Not only this is not necessary, it also masks proper results propagation
from the MAKE_SMALL_SWAPFILE() because there is at least one
tst_brk(TCONF, "") in there that will be converted to TBROK here.

Use safe version SAFE_MAKE_SMALL_SWAPFILE() instead of
MAKE_SMALL_SWAPFILE() to quit early during setup.

Because we don't have SAFE_SWAPON(), quit if TST_EXP_PASS_SILENT()
fails.

Also remove SAFE_SETEUID(0) which was probably due fork().

Fixes: fe1782ed66 ("syscalls/swapon03: use tst_max_swapfiles() and GET_USED_SWAPFILES() API")
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
New in v3.

 testcases/kernel/syscalls/swapon/swapon03.c | 33 +++++++--------------
 1 file changed, 11 insertions(+), 22 deletions(-)

diff --git a/testcases/kernel/syscalls/swapon/swapon03.c b/testcases/kernel/syscalls/swapon/swapon03.c
index d6445d5fc4..c014a48912 100644
--- a/testcases/kernel/syscalls/swapon/swapon03.c
+++ b/testcases/kernel/syscalls/swapon/swapon03.c
@@ -1,8 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
+ * Copyright (c) Linux Test Project, 2009-2025
  * Copyright (c) International Business Machines Corp., 2007
  * Created by <rsalveti@linux.vnet.ibm.com>
- *
  */
 
 /*\
@@ -27,13 +27,9 @@ static int swapfiles;
 
 static int setup_swap(void)
 {
-	pid_t pid;
-	int status;
 	int j, max_swapfiles, used_swapfiles;
 	char filename[FILENAME_MAX];
 
-	SAFE_SETEUID(0);
-
 	/* Determine how many more files are to be created */
 	max_swapfiles = tst_max_swapfiles();
 	used_swapfiles = tst_count_swaps();
@@ -41,24 +37,18 @@ static int setup_swap(void)
 	if (swapfiles > max_swapfiles)
 		swapfiles = max_swapfiles;
 
-	pid = SAFE_FORK();
-	if (pid == 0) {
-		/*create and turn on remaining swapfiles */
-		for (j = 0; j < swapfiles; j++) {
-
-			/* Create the swapfile */
-			snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
-			MAKE_SMALL_SWAPFILE(filename);
+	/*create and turn on remaining swapfiles */
+	for (j = 0; j < swapfiles; j++) {
 
-			/* turn on the swap file */
-			TST_EXP_PASS_SILENT(swapon(filename, 0));
-		}
-		exit(0);
-	} else
-		waitpid(pid, &status, 0);
+		/* Create the swapfile */
+		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
+		SAFE_MAKE_SMALL_SWAPFILE(filename);
 
-	if (WEXITSTATUS(status))
-		tst_brk(TFAIL, "Failed to setup swap files");
+		/* turn on the swap file */
+		TST_EXP_PASS_SILENT(swapon(filename, 0));
+		if (!TST_PASS)
+			tst_brk(TFAIL, "Failed to setup swap files");
+	}
 
 	tst_res(TINFO, "Successfully created %d swap files", swapfiles);
 	MAKE_SMALL_SWAPFILE(TEST_FILE);
@@ -127,7 +117,6 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.all_filesystems = 1,
 	.needs_root = 1,
-	.forks_child = 1,
 	.test_all = verify_swapon,
 	.setup = setup,
 	.cleanup = cleanup
-- 
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [LTP] [PATCH v3 2/2] swapon03: Try to swapon() as many files until it fails
  2025-11-18 14:36 [LTP] [PATCH v3 0/2] swapon03: Try to swapon() as many files until it fails Petr Vorel
  2025-11-18 14:36 ` [LTP] [PATCH v3 1/2] swapon03: Remove fork in setup Petr Vorel
@ 2025-11-18 14:36 ` Petr Vorel
  2025-11-18 15:04   ` Petr Vorel
  2025-12-11 11:15   ` Cyril Hrubis
  1 sibling, 2 replies; 9+ messages in thread
From: Petr Vorel @ 2025-11-18 14:36 UTC (permalink / raw)
  To: ltp; +Cc: Michal Hocko

Previously tst_max_swapfiles() had fine tuning for a specific kernel
version which was fragile due various backports in enterprise kernels.

Let's try to create and use as many swap files until swapon() fails.
Then check for expected EPERM.

Also test swapon() 2x, first in the setup() when first failure happen,
then in the test function (easier than propagate errno from fork to the
main function). Therefore add -i2 into runtest/syscalls.

It was required to increase cmd_buffer size to avoid directive output
may be truncated warning.

Suggested-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
Changes v2->v3:
* Add -i2 to runtest/syscalls.
* Slightly simpler code (no mmap() needed) due cleanup in a previous
  commit (removed fork()).

 runtest/syscalls                            |  2 +-
 testcases/kernel/syscalls/swapon/swapon03.c | 54 +++++++++++++--------
 2 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/runtest/syscalls b/runtest/syscalls
index 54d94c0ca2..80dedef749 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1646,7 +1646,7 @@ swapoff02 swapoff02
 
 swapon01 swapon01
 swapon02 swapon02
-swapon03 swapon03
+swapon03 swapon03 -i2
 
 #Exclusive syscall() for POWER6 machines only
 switch01 endian_switch01
diff --git a/testcases/kernel/syscalls/swapon/swapon03.c b/testcases/kernel/syscalls/swapon/swapon03.c
index c014a48912..548a5a522f 100644
--- a/testcases/kernel/syscalls/swapon/swapon03.c
+++ b/testcases/kernel/syscalls/swapon/swapon03.c
@@ -6,9 +6,12 @@
  */
 
 /*\
- * This test case checks whether swapon(2) system call returns:
+ * Test checks whether :man2:`swapon` system call returns EPERM when the maximum
+ * number of swap files are already in use.
  *
- *  - EPERM when there are more than MAX_SWAPFILES already in use.
+ * NOTE: test does not try to calculate MAX_SWAPFILES from the internal
+ * kernel implementation, instead make sure few swaps were created before
+ * maximum was reached.
  */
 
 #include <stdio.h>
@@ -20,6 +23,13 @@
 #include "lapi/syscalls.h"
 #include "libswap.h"
 
+/*
+ * MAX_SWAPFILES from the internal kernel implementation is currently <23, 29>,
+ * depending on kernel configuration (see man swapon(2). Chose small enough
+ * value for future changes.
+ */
+#define NUM_SWAP_FILES 15
+
 #define MNTPOINT	"mntpoint"
 #define TEST_FILE	MNTPOINT"/testswap"
 
@@ -27,31 +37,33 @@ static int swapfiles;
 
 static int setup_swap(void)
 {
-	int j, max_swapfiles, used_swapfiles;
+	int used_swapfiles, expected_swapfiles;
 	char filename[FILENAME_MAX];
 
-	/* Determine how many more files are to be created */
-	max_swapfiles = tst_max_swapfiles();
 	used_swapfiles = tst_count_swaps();
-	swapfiles = max_swapfiles - used_swapfiles;
-	if (swapfiles > max_swapfiles)
-		swapfiles = max_swapfiles;
+	expected_swapfiles = NUM_SWAP_FILES - used_swapfiles;
 
-	/*create and turn on remaining swapfiles */
-	for (j = 0; j < swapfiles; j++) {
+	if (expected_swapfiles < 0)
+		tst_brk(TCONF, "too many used swap files (%d)", used_swapfiles);
 
-		/* Create the swapfile */
-		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
-		SAFE_MAKE_SMALL_SWAPFILE(filename);
+	SAFE_MAKE_SMALL_SWAPFILE(TEST_FILE);
+	swapfiles++;
 
-		/* turn on the swap file */
-		TST_EXP_PASS_SILENT(swapon(filename, 0));
-		if (!TST_PASS)
-			tst_brk(TFAIL, "Failed to setup swap files");
+	while (true) {
+		/* Create the swapfile */
+		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, swapfiles);
+		MAKE_SMALL_SWAPFILE(filename);
+
+		/* Quit on a first swap file over max, check for EPERM */
+		if (swapon(filename, 0) == -1) {
+			if (errno != EPERM)
+				tst_res(TFAIL | TERRNO, "swapon(%s, 0)", filename);
+			break;
+		}
+		swapfiles++;
 	}
 
 	tst_res(TINFO, "Successfully created %d swap files", swapfiles);
-	MAKE_SMALL_SWAPFILE(TEST_FILE);
 
 	return 0;
 }
@@ -61,7 +73,7 @@ static int setup_swap(void)
  */
 static int check_and_swapoff(const char *filename)
 {
-	char cmd_buffer[256];
+	char cmd_buffer[FILENAME_MAX+28];
 	int rc = -1;
 
 	snprintf(cmd_buffer, sizeof(cmd_buffer), "grep -q '%s.*file' /proc/swaps", filename);
@@ -82,8 +94,8 @@ static void clean_swap(void)
 	int j;
 	char filename[FILENAME_MAX];
 
-	for (j = 0; j < swapfiles; j++) {
-		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
+	for (j = 1; j < swapfiles; j++) {
+		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j);
 		check_and_swapoff(filename);
 	}
 
-- 
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH v3 2/2] swapon03: Try to swapon() as many files until it fails
  2025-11-18 14:36 ` [LTP] [PATCH v3 2/2] swapon03: Try to swapon() as many files until it fails Petr Vorel
@ 2025-11-18 15:04   ` Petr Vorel
  2025-12-11 11:15   ` Cyril Hrubis
  1 sibling, 0 replies; 9+ messages in thread
From: Petr Vorel @ 2025-11-18 15:04 UTC (permalink / raw)
  To: ltp

Hi,

> Previously tst_max_swapfiles() had fine tuning for a specific kernel
> version which was fragile due various backports in enterprise kernels.

> Let's try to create and use as many swap files until swapon() fails.
> Then check for expected EPERM.

> Also test swapon() 2x, first in the setup() when first failure happen,
> then in the test function (easier than propagate errno from fork to the
> main function). Therefore add -i2 into runtest/syscalls.

> It was required to increase cmd_buffer size to avoid directive output
> may be truncated warning.

...
> -	/* Determine how many more files are to be created */
> -	max_swapfiles = tst_max_swapfiles();
>  	used_swapfiles = tst_count_swaps();
> -	swapfiles = max_swapfiles - used_swapfiles;
> -	if (swapfiles > max_swapfiles)
> -		swapfiles = max_swapfiles;
> +	expected_swapfiles = NUM_SWAP_FILES - used_swapfiles;

And merging this should be followed with removing tst_max_swapfiles()

https://lore.kernel.org/ltp/20251106163500.1063704-5-pvorel@suse.cz/
https://patchwork.ozlabs.org/project/ltp/patch/20251106163500.1063704-5-pvorel@suse.cz/

I forget to add it to v3, therefore I keep this patch opened in patchwork.
I'm sorry for the noise.

Kind regards,
Petr

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH v3 1/2] swapon03: Remove fork in setup
  2025-11-18 14:36 ` [LTP] [PATCH v3 1/2] swapon03: Remove fork in setup Petr Vorel
@ 2025-11-19 14:57   ` Avinesh Kumar
  2025-12-11 10:19   ` Cyril Hrubis
  1 sibling, 0 replies; 9+ messages in thread
From: Avinesh Kumar @ 2025-11-19 14:57 UTC (permalink / raw)
  To: ltp, Petr Vorel

Hi Petr,

Reviewed-by: Avinesh Kumar <akumar@suse.de>
for both patches.

Regards,
Avinesh



-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH v3 1/2] swapon03: Remove fork in setup
  2025-11-18 14:36 ` [LTP] [PATCH v3 1/2] swapon03: Remove fork in setup Petr Vorel
  2025-11-19 14:57   ` Avinesh Kumar
@ 2025-12-11 10:19   ` Cyril Hrubis
  2025-12-11 11:55     ` Petr Vorel
  1 sibling, 1 reply; 9+ messages in thread
From: Cyril Hrubis @ 2025-12-11 10:19 UTC (permalink / raw)
  To: Petr Vorel; +Cc: ltp

Hi!
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH v3 2/2] swapon03: Try to swapon() as many files until it fails
  2025-11-18 14:36 ` [LTP] [PATCH v3 2/2] swapon03: Try to swapon() as many files until it fails Petr Vorel
  2025-11-18 15:04   ` Petr Vorel
@ 2025-12-11 11:15   ` Cyril Hrubis
  2025-12-19 14:25     ` Petr Vorel
  1 sibling, 1 reply; 9+ messages in thread
From: Cyril Hrubis @ 2025-12-11 11:15 UTC (permalink / raw)
  To: Petr Vorel; +Cc: Michal Hocko, ltp

Hi!
> Suggested-by: Michal Hocko <mhocko@suse.com>
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
> Changes v2->v3:
> * Add -i2 to runtest/syscalls.
> * Slightly simpler code (no mmap() needed) due cleanup in a previous
>   commit (removed fork()).
> 
>  runtest/syscalls                            |  2 +-
>  testcases/kernel/syscalls/swapon/swapon03.c | 54 +++++++++++++--------
>  2 files changed, 34 insertions(+), 22 deletions(-)
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 54d94c0ca2..80dedef749 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -1646,7 +1646,7 @@ swapoff02 swapoff02
>  
>  swapon01 swapon01
>  swapon02 swapon02
> -swapon03 swapon03
> +swapon03 swapon03 -i2
>  
>  #Exclusive syscall() for POWER6 machines only
>  switch01 endian_switch01
> diff --git a/testcases/kernel/syscalls/swapon/swapon03.c b/testcases/kernel/syscalls/swapon/swapon03.c
> index c014a48912..548a5a522f 100644
> --- a/testcases/kernel/syscalls/swapon/swapon03.c
> +++ b/testcases/kernel/syscalls/swapon/swapon03.c
> @@ -6,9 +6,12 @@
>   */
>  
>  /*\
> - * This test case checks whether swapon(2) system call returns:
> + * Test checks whether :man2:`swapon` system call returns EPERM when the maximum
> + * number of swap files are already in use.
>   *
> - *  - EPERM when there are more than MAX_SWAPFILES already in use.
> + * NOTE: test does not try to calculate MAX_SWAPFILES from the internal
> + * kernel implementation, instead make sure few swaps were created before
> + * maximum was reached.
>   */
>  
>  #include <stdio.h>
> @@ -20,6 +23,13 @@
>  #include "lapi/syscalls.h"
>  #include "libswap.h"
>  
> +/*
> + * MAX_SWAPFILES from the internal kernel implementation is currently <23, 29>,
> + * depending on kernel configuration (see man swapon(2). Chose small enough
> + * value for future changes.
> + */
> +#define NUM_SWAP_FILES 15

It would be slightly better if we renamed this to MIN_SWAP_FILES since
this is the minimal number of swapfiles we expect to be able to create.

>  #define MNTPOINT	"mntpoint"
>  #define TEST_FILE	MNTPOINT"/testswap"
>  
> @@ -27,31 +37,33 @@ static int swapfiles;
>  
>  static int setup_swap(void)
>  {
> -	int j, max_swapfiles, used_swapfiles;
> +	int used_swapfiles, expected_swapfiles;

Here as well, s/expected_swapfiles/min_swapfiles/

>  	char filename[FILENAME_MAX];
>  
> -	/* Determine how many more files are to be created */
> -	max_swapfiles = tst_max_swapfiles();
>  	used_swapfiles = tst_count_swaps();
> -	swapfiles = max_swapfiles - used_swapfiles;
> -	if (swapfiles > max_swapfiles)
> -		swapfiles = max_swapfiles;
> +	expected_swapfiles = NUM_SWAP_FILES - used_swapfiles;
>  
> -	/*create and turn on remaining swapfiles */
> -	for (j = 0; j < swapfiles; j++) {
> +	if (expected_swapfiles < 0)
> +		tst_brk(TCONF, "too many used swap files (%d)", used_swapfiles);

Do we really have to quit here? If all swap slots are already used we
will not create any additioinal swaps and just go directly to the test.
If there are some slot left, we will fill them.

The only difference is taht we shouldn't expect a failure until this
number of swaps has been reached.

So we should do:

	if (swapon(filename, 0) == -1) {
		if (errno == EPERM && swapfiles > min_swapfiles)
			break;

		tst_res(TFAIL | TERRNO, "swapon(%s, 0)", filename);
	}

> -		/* Create the swapfile */
> -		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
> -		SAFE_MAKE_SMALL_SWAPFILE(filename);
> +	SAFE_MAKE_SMALL_SWAPFILE(TEST_FILE);
> +	swapfiles++;

Why do we increase the number of swapfiles here? We only created the
file. Isn't the swapfiles counter for swaps that were enabled by
swapon()?

> -		/* turn on the swap file */
> -		TST_EXP_PASS_SILENT(swapon(filename, 0));
> -		if (!TST_PASS)
> -			tst_brk(TFAIL, "Failed to setup swap files");
> +	while (true) {
> +		/* Create the swapfile */
> +		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, swapfiles);
> +		MAKE_SMALL_SWAPFILE(filename);
> +
> +		/* Quit on a first swap file over max, check for EPERM */
> +		if (swapon(filename, 0) == -1) {
> +			if (errno != EPERM)
> +				tst_res(TFAIL | TERRNO, "swapon(%s, 0)", filename);
> +			break;
> +		}
> +		swapfiles++;
>  	}
>  
>  	tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> -	MAKE_SMALL_SWAPFILE(TEST_FILE);
>  
>  	return 0;
>  }
> @@ -61,7 +73,7 @@ static int setup_swap(void)
>   */
>  static int check_and_swapoff(const char *filename)
>  {
> -	char cmd_buffer[256];
> +	char cmd_buffer[FILENAME_MAX+28];
>  	int rc = -1;
>  
>  	snprintf(cmd_buffer, sizeof(cmd_buffer), "grep -q '%s.*file' /proc/swaps", filename);
> @@ -82,8 +94,8 @@ static void clean_swap(void)
>  	int j;
>  	char filename[FILENAME_MAX];
>  
> -	for (j = 0; j < swapfiles; j++) {
> -		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
> +	for (j = 1; j < swapfiles; j++) {
> +		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j);
>  		check_and_swapoff(filename);
>  	}
>  
> -- 
> 2.51.0
> 

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH v3 1/2] swapon03: Remove fork in setup
  2025-12-11 10:19   ` Cyril Hrubis
@ 2025-12-11 11:55     ` Petr Vorel
  0 siblings, 0 replies; 9+ messages in thread
From: Petr Vorel @ 2025-12-11 11:55 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

Hi all,

thanks for your review, I merged this first patch.

Kind regards,
Petr

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [LTP] [PATCH v3 2/2] swapon03: Try to swapon() as many files until it fails
  2025-12-11 11:15   ` Cyril Hrubis
@ 2025-12-19 14:25     ` Petr Vorel
  0 siblings, 0 replies; 9+ messages in thread
From: Petr Vorel @ 2025-12-19 14:25 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

...
> > +/*
> > + * MAX_SWAPFILES from the internal kernel implementation is currently <23, 29>,
> > + * depending on kernel configuration (see man swapon(2). Chose small enough
> > + * value for future changes.
> > + */
> > +#define NUM_SWAP_FILES 15

> It would be slightly better if we renamed this to MIN_SWAP_FILES since
> this is the minimal number of swapfiles we expect to be able to create.

> >  #define MNTPOINT	"mntpoint"
> >  #define TEST_FILE	MNTPOINT"/testswap"

> > @@ -27,31 +37,33 @@ static int swapfiles;

> >  static int setup_swap(void)
> >  {
> > -	int j, max_swapfiles, used_swapfiles;
> > +	int used_swapfiles, expected_swapfiles;

> Here as well, s/expected_swapfiles/min_swapfiles/

> >  	char filename[FILENAME_MAX];

> > -	/* Determine how many more files are to be created */
> > -	max_swapfiles = tst_max_swapfiles();
> >  	used_swapfiles = tst_count_swaps();
> > -	swapfiles = max_swapfiles - used_swapfiles;
> > -	if (swapfiles > max_swapfiles)
> > -		swapfiles = max_swapfiles;
> > +	expected_swapfiles = NUM_SWAP_FILES - used_swapfiles;

> > -	/*create and turn on remaining swapfiles */
> > -	for (j = 0; j < swapfiles; j++) {
> > +	if (expected_swapfiles < 0)
> > +		tst_brk(TCONF, "too many used swap files (%d)", used_swapfiles);

> Do we really have to quit here? If all swap slots are already used we
> will not create any additioinal swaps and just go directly to the test.
> If there are some slot left, we will fill them.

We already have v4, which does not contain it.  But just for a record (I'm sorry
to discuss in the old version):
the original suggestion was to have at least few swapon() runs. This is not
checked in the code you suggested. Of course, the reason why swapon() fails
should be due already used swaps. But we don't check that. We have options:
* count used swaps (i.e. count lines in /proc/swaps)
* ignore the check
* require at least one swap can be created (code in v4).

Kind regards,
Petr

> The only difference is taht we shouldn't expect a failure until this
> number of swaps has been reached.

> So we should do:

> 	if (swapon(filename, 0) == -1) {
> 		if (errno == EPERM && swapfiles > min_swapfiles)
> 			break;

> 		tst_res(TFAIL | TERRNO, "swapon(%s, 0)", filename);
> 	}

> > -		/* Create the swapfile */
> > -		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
> > -		SAFE_MAKE_SMALL_SWAPFILE(filename);
> > +	SAFE_MAKE_SMALL_SWAPFILE(TEST_FILE);
> > +	swapfiles++;

> Why do we increase the number of swapfiles here? We only created the
> file. Isn't the swapfiles counter for swaps that were enabled by
> swapon()?

> > -		/* turn on the swap file */
> > -		TST_EXP_PASS_SILENT(swapon(filename, 0));
> > -		if (!TST_PASS)
> > -			tst_brk(TFAIL, "Failed to setup swap files");
> > +	while (true) {
> > +		/* Create the swapfile */
> > +		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, swapfiles);
> > +		MAKE_SMALL_SWAPFILE(filename);
> > +
> > +		/* Quit on a first swap file over max, check for EPERM */
> > +		if (swapon(filename, 0) == -1) {
> > +			if (errno != EPERM)
> > +				tst_res(TFAIL | TERRNO, "swapon(%s, 0)", filename);
> > +			break;
> > +		}
> > +		swapfiles++;
> >  	}

> >  	tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> > -	MAKE_SMALL_SWAPFILE(TEST_FILE);

> >  	return 0;
> >  }
> > @@ -61,7 +73,7 @@ static int setup_swap(void)
> >   */
> >  static int check_and_swapoff(const char *filename)
> >  {
> > -	char cmd_buffer[256];
> > +	char cmd_buffer[FILENAME_MAX+28];
> >  	int rc = -1;

> >  	snprintf(cmd_buffer, sizeof(cmd_buffer), "grep -q '%s.*file' /proc/swaps", filename);
> > @@ -82,8 +94,8 @@ static void clean_swap(void)
> >  	int j;
> >  	char filename[FILENAME_MAX];

> > -	for (j = 0; j < swapfiles; j++) {
> > -		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
> > +	for (j = 1; j < swapfiles; j++) {
> > +		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j);
> >  		check_and_swapoff(filename);
> >  	}

> > -- 
> > 2.51.0

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-12-19 14:25 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-18 14:36 [LTP] [PATCH v3 0/2] swapon03: Try to swapon() as many files until it fails Petr Vorel
2025-11-18 14:36 ` [LTP] [PATCH v3 1/2] swapon03: Remove fork in setup Petr Vorel
2025-11-19 14:57   ` Avinesh Kumar
2025-12-11 10:19   ` Cyril Hrubis
2025-12-11 11:55     ` Petr Vorel
2025-11-18 14:36 ` [LTP] [PATCH v3 2/2] swapon03: Try to swapon() as many files until it fails Petr Vorel
2025-11-18 15:04   ` Petr Vorel
2025-12-11 11:15   ` Cyril Hrubis
2025-12-19 14:25     ` Petr Vorel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox