Skip to contentSkip to conversation

libswap: add two methods to create swapfile

PATCH In review Li Wang 2 participants 8 messages Mar 22, 2024 – Apr 16, 2024

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH v3 1/2 libswap: add two methods to create swapfile

This introduces enhanced functionality within the libswap library, adding two versatile methods for creating a swapfile: by size in megabytes (MB) and by the number of file system blocks.

The make_swapfile function now accepts an enum swapfile_method parameter, enabling the selection of the creation method. To facilitate ease of use, two macros are provided for each method:

MAKE_SWAPFILE_SIZE for creating swapfiles by size. MAKE_SWAPFILE_BLKS for creating swapfiles by block count.

Each method also includes a 'safe' macro variant that incorporates additional error handling:

SAFE_MAKE_SWAPFILE_SIZE for safely creating swapfiles by size. SAFE_MAKE_SWAPFILE_BLKS for safely creating swapfiles by block count.

The make_swapfile_ helper function has been updated to incorporate file and line number arguments, which aids in improved error reporting and debugging. This function is now used internally by the macros to streamline the swapfile creation process.

The update to the make_swapfile function and the introduction of these new macros are designed to offer a more user-friendly and error-resistant approach to swapfile creation, catering to a variety of system configurations and user needs.

---
 include/libswap.h                             | 43 ++++++++++++++-
 libs/libltpswap/libswap.c                     | 55 +++++++++++--------
 testcases/kernel/syscalls/swapoff/swapoff01.c |  2 +-
 testcases/kernel/syscalls/swapoff/swapoff02.c |  2 +-
 testcases/kernel/syscalls/swapon/swapon01.c   |  2 +-
 testcases/kernel/syscalls/swapon/swapon02.c   |  4 +-
 testcases/kernel/syscalls/swapon/swapon03.c   |  4 +-
 7 files changed, 81 insertions(+), 31 deletions(-)
include/libswap.h +41 -2
index 8c75e20ae..96e718542 100644
@@ -11,10 +11,49 @@
 #ifndef __LIBSWAP_H__
 #define __LIBSWAP_H__
 
+enum swapfile_method {
+    SWAPFILE_BY_SIZE,
+    SWAPFILE_BY_BLKS
+};
+
 /*
- * Make a swap file
+ * Create a swapfile of a specified size or number of blocks.
+ */
+int make_swapfile_(const char *file, const int lineno,
+			const char *swapfile, unsigned int num,
+			int safe, enum swapfile_method method);
+
+static inline int make_swapfile(const char *swapfile, unsigned int num,
+			int safe, enum swapfile_method method)
+{
+	return make_swapfile_(__FILE__, __LINE__, swapfile, num, safe, method);
+}
+
+/**
+ * Macro to create swapfile size in megabytes (MB).
+ */
+#define MAKE_SWAPFILE_SIZE(swapfile, size) \
+    make_swapfile(swapfile, size, 0, SWAPFILE_BY_SIZE)
+
+/**
+ * Macro to create swapfile size in block numbers.
+ */
+#define MAKE_SWAPFILE_BLKS(swapfile, blocks) \
+    make_swapfile(swapfile, blocks, 0, SWAPFILE_BY_BLKS)
+
+/**
+ * Macro to safely create swapfile size in megabytes (MB).
+ * Includes safety checks to handle potential errors.
+ */
+#define SAFE_MAKE_SWAPFILE_SIZE(swapfile, size) \
+    make_swapfile(swapfile, size, 1, SWAPFILE_BY_SIZE)
+
+/**
+ * Macro to safely create swapfile size in block numbers.
+ * Includes safety checks to handle potential errors.
  */
-int make_swapfile(const char *swapfile, int blocks, int safe);
+#define SAFE_MAKE_SWAPFILE_BLKS(swapfile, blocks) \
+    make_swapfile(swapfile, blocks, 1, SWAPFILE_BY_BLKS)
 
 /*
  * Check swapon/swapoff support status of filesystems or files
libs/libltpswap/libswap.c +33 -22

88 lines not on this page · raw message

testcases/kernel/syscalls/swapoff/swapoff01.c +1 -1
index 2a0b683c1..cf13907e7 100644
@@ -44,7 +44,7 @@ static void setup(void)
 {
 	is_swap_supported(TEST_FILE);
 
-	if (make_swapfile(SWAP_FILE, 65536, 1))
+	if (SAFE_MAKE_SWAPFILE_BLKS(SWAP_FILE, 65536))
 		tst_brk(TBROK, "Failed to create file for swap");
 }
 
testcases/kernel/syscalls/swapoff/swapoff02.c +1 -1
index 52906848f..61536dda4 100644
@@ -88,7 +88,7 @@ static void setup(void)
 
 	is_swap_supported(TEST_FILE);
 
-	if (make_swapfile(SWAP_FILE, 10, 1))
+	if (SAFE_MAKE_SWAPFILE_BLKS(SWAP_FILE, 10))
 		tst_brk(TBROK, "Failed to create file for swap");
 }
 
testcases/kernel/syscalls/swapon/swapon01.c +1 -1
index d406e4bd9..b986d97c4 100644
@@ -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_BLKS(SWAP_FILE, 10);
 
 	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
 	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
testcases/kernel/syscalls/swapon/swapon02.c +2 -2
index 7e876d26a..e5e29b8e7 100644
@@ -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);
+	MAKE_SWAPFILE_BLKS(USED_FILE, 10);
 
 	if (tst_syscall(__NR_swapon, USED_FILE, 0))
 		tst_res(TWARN | TERRNO, "swapon(alreadyused) failed");
testcases/kernel/syscalls/swapon/swapon03.c +2 -2
index 6f47fc01f..5295a6a73 100644
@@ -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);
 
 			/* 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);
 
 	return 0;
 }
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#1 lore raw

PATCH v3 2/2 swapon01: create 128MB swapfile

The modification in the setup function changes the swap file creation approach. Instead of allocating swap space based on a number of blocks (which potentially could be too small for the polluted memory swapout, thus causing an OOM), it now uses a fixed size of 128 units for the swap file creation.

This is likely a conservative figure to prevent the OOM killer from being triggered due to excessive memory allocation for the swap file, helping ensure the test runs successfully on systems with no swap space.

---
 testcases/kernel/syscalls/swapon/swapon01.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/kernel/syscalls/swapon/swapon01.c +1 -1
index b986d97c4..49915c32d 100644
@@ -38,7 +38,7 @@ static void verify_swapon(void)
 static void setup(void)
 {
 	is_swap_supported(SWAP_FILE);
-	MAKE_SWAPFILE_BLKS(SWAP_FILE, 10);
+	MAKE_SWAPFILE_SIZE(SWAP_FILE, 128);
 
 	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
 	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v3 1/2
#2 lore raw
In reply to Li Wang

Hi Li,

...

Quoted 3 lines

---

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

nit: why this increase to 128? Why not PATH_MAX?

libs/libltpswap/libswap.c out: · line 164

Quoted 8 lines
libs/libltpswap/libswap.c +3 -4
 	/* To guarantee at least one page can be swapped out */
-	if (blk_size * blocks < pg_size)
-		bs = pg_size;
-	else
-		bs = blk_size;
+	if (blk_size * blocks < pg_size) {
+		tst_res(TWARN, "Swapfile size is less than the system page size. \
+			Using page size (%lu bytes) instead of block size (%lu bytes).",

libswap.c:163: WARNING: Avoid line continuations in quoted strings

This will fix it:

tst_res(TWARN, "Swapfile size is less than the system page size. "
"Using page size (%lu bytes) instead of block size (%lu bytes).",

Quoted 24 lines
libs/libltpswap/libswap.c +9 -7
+			(unsigned long)pg_size, blk_size);
+		blk_size = pg_size;
+	}
 
 	if (sscanf(swapfile, "%[^/]", mnt_path) != 1)
-		tst_brk(TBROK, "sscanf failed");
+		tst_brk_(file, lineno, TBROK, "sscanf failed");
 
-	if (!tst_fs_has_free(mnt_path, bs * blocks, TST_BYTES))
-		tst_brk(TCONF, "Insufficient disk space to create swap file");
+	if (!tst_fs_has_free(mnt_path, blk_size * blocks, TST_BYTES))
+		tst_brk_(file, lineno, TCONF, "Insufficient disk space to create swap file");
 
 	/* create file */
-	if (prealloc_contiguous_file(swapfile, bs, blocks) != 0)
-		tst_brk(TBROK, "Failed to create swapfile");
+	if (prealloc_contiguous_file(swapfile, blk_size, blocks) != 0)
+		tst_brk_(file, lineno, TBROK, "Failed to create swapfile");
 
 	/* Fill the file if needed (specific to old xfs filesystems) */
 	if (tst_fs_type(swapfile) == TST_XFS_MAGIC) {
-		if (tst_fill_file(swapfile, 0, bs, blocks) != 0)
-			tst_brk(TBROK, "Failed to fill swapfile");
+		if (tst_fill_file(swapfile, 0, blk_size, blocks) != 0)

libs/libltpswap/libswap.c out: · line 186

Quoted 10 lines
libs/libltpswap/libswap.c +2 -5
+			tst_brk_(file, lineno, TBROK, "Failed to fill swapfile");
 	}
 
 	/* make the file swapfile */
-	const char *argv[2 + 1];
-
-	argv[0] = "mkswap";
-	argv[1] = swapfile;
-	argv[2] = NULL;
+	const char *argv[] = {"mkswap", swapfile, NULL};

libswap.c:186: WARNING: char * array declaration might be better as static const

This will fix it:

	const char *const argv[] = {"mkswap", swapfile, NULL};

libs/libltpswap/libswap.c out: · line 189

Quoted 3 lines
libs/libltpswap/libswap.c +1 -1
 	return tst_cmd(argv, "/dev/null", "/dev/null", safe ?
-				   TST_CMD_PASS_RETVAL | TST_CMD_TCONF_ON_MISSING : 0);
+			TST_CMD_PASS_RETVAL | TST_CMD_TCONF_ON_MISSING : 0);

The rest LGTM.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#3 lore raw
Quoted 20 lines · Petr Vorel

Hi Li,

...

Quoted 3 lines

---

...

libs/libltpswap/libswap.c +6 -3
-int make_swapfile(const char *swapfile, int blocks, int safe)
+int make_swapfile_(const char *file, const int lineno,
+                       const char *swapfile, unsigned int num,
+                       int safe, enum swapfile_method method)
 {
        struct statvfs fs_info;
-       unsigned long blk_size, bs;
+       unsigned long blk_size;
+       unsigned int blocks = 0;
        size_t pg_size = sysconf(_SC_PAGESIZE);
-       char mnt_path[100];
+       char mnt_path[128];
Quoted 2 lines

nit: why this increase to 128? Why not PATH_MAX?

No special meaning, I just thought of using 2 raised to the nth power. But you're right, PATCH_MAX will be safer.

Quoted 12 lines

libs/libltpswap/libswap.c out:

Quoted 8 lines
      /* To guarantee at least one page can be swapped out */
libs/libltpswap/libswap.c +2 -4
-     if (blk_size * blocks < pg_size)
-             bs = pg_size;
-     else
-             bs = blk_size;
+     if (blk_size * blocks < pg_size) {
+             tst_res(TWARN, "Swapfile size is less than the system page

size. \

Quoted 1 line

+ Using page size (%lu bytes) instead of block size

(%lu bytes).",

Quoted 9 lines

libswap.c:163: WARNING: Avoid line continuations in quoted strings

This will fix it:

tst_res(TWARN, "Swapfile size is less than the system page
size. "
"Using page size (%lu bytes) instead of block size
(%lu bytes).",

+1

Quoted 38 lines

libs/libltpswap/libswap.c out:

Quoted 10 lines
libs/libltpswap/libswap.c +4 -3
+                     (unsigned long)pg_size, blk_size);
+             blk_size = pg_size;
+     }
 
      if (sscanf(swapfile, "%[^/]", mnt_path) != 1)
-             tst_brk(TBROK, "sscanf failed");
+             tst_brk_(file, lineno, TBROK, "sscanf failed");
 
-     if (!tst_fs_has_free(mnt_path, bs * blocks, TST_BYTES))
-             tst_brk(TCONF, "Insufficient disk space to create swap

file");

Review

Quoted 2 lines
(quoted diff) +2 -0
+     if (!tst_fs_has_free(mnt_path, blk_size * blocks, TST_BYTES))
+             tst_brk_(file, lineno, TCONF, "Insufficient disk space to

create swap file");

libs/libltpswap/libswap.c out:

Quoted 12 lines
      /* create file */
libs/libltpswap/libswap.c +4 -4
-     if (prealloc_contiguous_file(swapfile, bs, blocks) != 0)
-             tst_brk(TBROK, "Failed to create swapfile");
+     if (prealloc_contiguous_file(swapfile, blk_size, blocks) != 0)
+             tst_brk_(file, lineno, TBROK, "Failed to create swapfile");
 
      /* Fill the file if needed (specific to old xfs filesystems) */
      if (tst_fs_type(swapfile) == TST_XFS_MAGIC) {
-             if (tst_fill_file(swapfile, 0, bs, blocks) != 0)
-                     tst_brk(TBROK, "Failed to fill swapfile");
+             if (tst_fill_file(swapfile, 0, blk_size, blocks) != 0)
+                     tst_brk_(file, lineno, TBROK, "Failed to fill

swapfile");

libs/libltpswap/libswap.c out: · line 186

Quoted 9 lines
      }

      /* make the file swapfile */
libs/libltpswap/libswap.c +1 -5
-     const char *argv[2 + 1];
-
-     argv[0] = "mkswap";
-     argv[1] = swapfile;
-     argv[2] = NULL;
+     const char *argv[] = {"mkswap", swapfile, NULL};
Quoted 7 lines

libswap.c:186: WARNING: char * array declaration might be better as static const

This will fix it:

        const char *const argv[] = {"mkswap", swapfile, NULL};

+1

Quoted 8 lines
Quoted 2 lines
      return tst_cmd(argv, "/dev/null", "/dev/null", safe ?

- TST_CMD_PASS_RETVAL |

TST_CMD_TCONF_ON_MISSING : 0);

Quoted 1 line

+ TST_CMD_PASS_RETVAL | TST_CMD_TCONF_ON_MISSING :

0);

The rest LGTM.

Thanks, I would give the patch set more time in case others have comments.

Quoted 6 lines

Kind regards,
Petr

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Hi Li,

Quoted 8 lines
Quoted 4 lines
libs/libltpswap/libswap.c +1 -1
-       char mnt_path[100];
+       char mnt_path[128];

nit: why this increase to 128? Why not PATH_MAX?

No special meaning, I just thought of using 2 raised to the nth power. But you're right, PATCH_MAX will be safer.

+1

...

Quoted 1 line

Thanks, I would give the patch set more time in case others have comments.

Sure. Thanks for a nice work!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#5 lore raw
Quoted 6 lines · Petr Vorel
Quoted 2 lines

Thanks, I would give the patch set more time in case others have

comments.

Sure. Thanks for a nice work!

Patch set applied.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Hi Li,

Quoted 9 lines
Quoted 4 lines · Petr Vorel
Quoted 1 line

Thanks, I would give the patch set more time in case others have

comments.

Sure. Thanks for a nice work!

Patch set applied.

FYI it looks like this patch merge as:

f987ffff5 ("libswap: add two methods to create swapfile")

introduced TWARN on all tests which touched: swapoff0[12], swapon0[1-3] on openSUSE Tumbleweed on ppc64le (e.g. very close to mainline stable kernels: 6.8.x). Have you noticed this on Fedora as well? I hope to manage to have look on it soon, but maybe it's obvious to you already.

libswap.c:163: TWARN: Swapfile size is less than the system page size. Using page size (65536 bytes) instead of block size (4096 bytes).

Example of the warning:

* swapoff01
on all of the filesystems:
tst_test.c:1701: TINFO: === Testing on ext2 ===
tst_test.c:1117: TINFO: Formatting /dev/loop0 with ext2 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1131: TINFO: Mounting /dev/loop0 to /var/tmp/LTP_swaWdvJcZ/mntpoint fstyp=ext2 flags=0
libswap.c:156: TINFO: create a swapfile with 10 block numbers
libswap.c:163: TWARN: Swapfile size is less than the system page size. Using page size (65536 bytes) instead of block size (4096 bytes).

* swapon03
tst_test.c:1701: TINFO: === Testing on ext2 ===
tst_test.c:1117: TINFO: Formatting /dev/loop0 with ext2 opts='' extra opts=''
mke2fs 1.47.0 (5-Feb-2023)
tst_test.c:1131: TINFO: Mounting /dev/loop0 to /var/tmp/LTP_swalq2Qxc/mntpoint fstyp=ext2 flags=0
libswap.c:156: TINFO: create a swapfile with 10 block numbers
libswap.c:163: TWARN: Swapfile size is less than the system page size. Using page size (65536 bytes) instead of block size (4096 bytes). tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
tst_kconfig.c:88: TINFO: Parsing kernel config '/proc/config.gz'
tst_kconfig.c:88: TINFO: Parsing kernel config '/proc/config.gz'
tst_kconfig.c:88: TINFO: Parsing kernel config '/proc/config.gz'
tst_kconfig.c:88: TINFO: Parsing kernel config '/proc/config.gz'
libswap.c:156: TINFO: create a swapfile with 10 block numbers
libswap.c:163: TWARN: Swapfile size is less than the system page size. Using page size (65536 bytes) instead of block size (4096 bytes). libswap.c:156: TINFO: create a swapfile with 10 block numbers
libswap.c:163: TWARN: Swapfile size is less than the system page size. Using page size (65536 bytes) instead of block size (4096 bytes). libswap.c:156: TINFO: create a swapfile with 10 block numbers
libswap.c:163: TWARN: Swapfile size is less than the system page size. Using page size (65536 bytes) instead of block size (4096 bytes).
...

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#7 lore raw

Hi Petr,

Thanks for reporting this.

Quoted 24 lines · Petr Vorel

Hi Li,

Quoted 9 lines
Quoted 4 lines · Petr Vorel
Quoted 1 line

Thanks, I would give the patch set more time in case others have

comments.

Sure. Thanks for a nice work!

Patch set applied.

FYI it looks like this patch merge as:

f987ffff5 ("libswap: add two methods to create swapfile")

introduced TWARN on all tests which touched: swapoff0[12], swapon0[1-3] on openSUSE Tumbleweed
on ppc64le (e.g. very close to mainline stable kernels: 6.8.x). Have you noticed
this on Fedora as well? I hope to manage to have look on it soon, but maybe
it's obvious to you already.

Quoted 3 lines

libswap.c:163: TWARN: Swapfile size is less than the system page size. Using page size (65536 bytes) instead of block size (4096 bytes).

Yes, that is expected on a system with a larger page size, so it could guarantee at least one page can be swapped out.

Do you want to switch to using TINFO for the choice? TBH it doesn't matter to report that WARNING message.

Signature · 5 lines
Regards,
Li Wang

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