[LTP] [PATCH v2 0/5] swapon03: Try to swapon() as many files until it fails

Petr Vorel pvorel@suse.cz
Thu Nov 6 17:34:55 CET 2025


Hi,

Changes v1->v2:
* Test first swapon() call (Avinesh). For simplicity and -i working
  properly is swapon() tested twice.
* Use correct variable in TCONF message (Avinesh)
* Improve doc (Li)

3 new cleanup commits:
  swapon03: Don't create swap file twice
  swapon03: Cleanup
  swapon03: Remove grep dependency

Posting a patchset diff below (might be a bit more readable than my
explanation).

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=*

Kind regards,
Petr

Petr Vorel (5):
  swapon03: Don't create swap file twice
  swapon03: Cleanup
  swapon03: Try to swapon() as many files until it fails
  libswap: Remove now unused tst_max_swapfiles()
  swapon03: Remove grep dependency

 include/libswap.h                           |   7 --
 libs/swap/libswap.c                         |  64 ------------
 testcases/kernel/syscalls/swapon/swapon03.c | 105 ++++++++++++--------
 3 files changed, 62 insertions(+), 114 deletions(-)

diff --git testcases/kernel/syscalls/swapon/swapon03.c testcases/kernel/syscalls/swapon/swapon03.c
index 0027f874be..91d797871f 100644
--- testcases/kernel/syscalls/swapon/swapon03.c
+++ testcases/kernel/syscalls/swapon/swapon03.c
@@ -10,9 +10,8 @@
  * number of swap files are already in use.
  *
  * NOTE: test does not try to calculate MAX_SWAPFILES from the internal
- * kernel implementation (which is currently <23, 29> depending on kernel
- * configuration). Instead test exptect that at least 15 swap files minus
- * currently used swap can be created.
+ * kernel implementation, instead make sure few swaps were created before
+ * maximum was reached.
  */
 
 #include <stdio.h>
@@ -24,11 +23,18 @@
 #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"
+#define TEST_FILE	MNTPOINT "/LTP_" __FILE__ "_testswap"
 
 static int *swapfiles;
+static char *tmpdir;
 
 static void setup_swap(void)
 {
@@ -41,10 +47,8 @@ static void setup_swap(void)
 	used_swapfiles = tst_count_swaps();
 	expected_swapfiles = NUM_SWAP_FILES - used_swapfiles;
 
-	if (expected_swapfiles < 0) {
-		tst_brk(TCONF, "Warning: too many used swap files (%d)",
-			expected_swapfiles);
-	}
+	if (expected_swapfiles < 0)
+		tst_brk(TCONF, "too many used swap files (%d)", used_swapfiles);
 
 	pid = SAFE_FORK();
 	if (pid == 0) {
@@ -53,9 +57,12 @@ static void setup_swap(void)
 			snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, *swapfiles);
 			MAKE_SMALL_SWAPFILE(filename);
 
-			/* Quit on a first swap file over max */
-			if (swapon(filename, 0) == -1)
+			/* 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)++;
 		}
 		exit(0);
@@ -79,12 +86,14 @@ static void setup_swap(void)
  */
 static void check_and_swapoff(const char *filename)
 {
-	char cmd_buffer[256];
+	char buf[256];
+	int foo;
 
-	snprintf(cmd_buffer, sizeof(cmd_buffer), "grep -q '%s.*file' /proc/swaps", filename);
-
-	if (system(cmd_buffer) == 0 && swapoff(filename) != 0)
-		tst_res(TWARN, "Failed to swapoff %s", filename);
+	snprintf(buf, sizeof(buf), "%s/%s %%*s %%*s %%*s %%s", tmpdir, filename);
+	if (!FILE_LINES_SCANF("/proc/swaps", buf, &foo)) {
+		if (swapoff(filename) != 0)
+			tst_res(TWARN | TERRNO, "swapoff(%s) failed", filename);
+	}
 }
 
 /*
@@ -115,6 +124,8 @@ static void setup(void)
 
 	is_swap_supported(TEST_FILE);
 
+	tmpdir = tst_tmpdir_path();
+
 	swapfiles = SAFE_MMAP(NULL, sizeof(*swapfiles), PROT_READ | PROT_WRITE,
 			MAP_SHARED | MAP_ANONYMOUS, -1, 0);
 	*swapfiles = 0;


More information about the ltp mailing list