Skip to contentSkip to conversation

swapon01: Test on all filesystems

PATCH In review Petr Vorel 3 participants 15 messages Jan 19, 2024 – Jan 22, 2024

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH 1/1 swapon01: Test on all filesystems

Test on all filesystems to increase coverage.
Skip filesystems which does not support swap (currently bcachefs, btrfs and tmpfs).

Tested on 5.10, 6.6 and 6.7.

---
Hi Li, Cyril,

feel free to postpone this after the release.
If we are for adding this, I'll retest on Monday older filesystems.

man swapon(8) mentions:

Btrfs
Swap files on Btrfs are supported since Linux 5.0 on files with nocow attribute. See the btrfs(5) manual page for more details.

=> Can we pass "nocow" just to btrfs? Or should it be added to single test, which uses just btrfs with nocow?

NFS
Swap over NFS may not work.

=> I guess this is not important for us, as NFS is not part of filesystems in .all_filesystems, but maybe we should still add it to the whitelist?

Kind regards,
Petr

 testcases/kernel/syscalls/swapon/swapon01.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/swapon/swapon01.c +15 -6
index e59fb20a1..ef4ebfdcf 100644
@@ -8,6 +8,7 @@
  * [Description]
  *
  * Checks that swapon() succeds with swapfile.
+ * Testing on all filesystems which support swap file.
  */
 
 #include <unistd.h>
@@ -17,13 +18,14 @@
 #include "lapi/syscalls.h"
 #include "libswap.h"
 
-#define SWAP_FILE "swapfile01"
+#define MNTPOINT	"mntpoint"
+#define SWAP_FILE	MNTPOINT"/swapfile01"
 
 static void verify_swapon(void)
 {
 	TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
 
-	if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
+	if (tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
 		tst_brk(TBROK | TERRNO,
 				"Failed to turn off swapfile, system reboot recommended");
 	}
@@ -31,13 +33,20 @@ static void verify_swapon(void)
 
 static void setup(void)
 {
-	is_swap_supported(SWAP_FILE);
 	make_swapfile(SWAP_FILE, 0);
 }
 
 static struct tst_test test = {
-	.needs_root = 1,
-	.needs_tmpdir = 1,
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.needs_root = 1, /* for swapon() */
+	.all_filesystems = 1,
 	.test_all = verify_swapon,
-	.setup = setup
+	.setup = setup,
+	.skip_filesystems = (const char *[]) {
+		"bcachefs",
+		"btrfs",
+		"tmpfs",
+		NULL
+	},
 };
Signature · 5 lines
2.43.0


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

Hi!

Quoted 19 lines
 testcases/kernel/syscalls/swapon/swapon01.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/swapon/swapon01.c +1 -0
index e59fb20a1..ef4ebfdcf 100644
@@ -8,6 +8,7 @@
  * [Description]
  *
  * Checks that swapon() succeds with swapfile.
+ * Testing on all filesystems which support swap file.
  */
 
 #include <unistd.h>
@@ -17,13 +18,14 @@
 #include "lapi/syscalls.h"
 #include "libswap.h"
 

testcases/kernel/syscalls/swapon/swapon01.c line 28

Quoted 10 lines
testcases/kernel/syscalls/swapon/swapon01.c +3 -2
-#define SWAP_FILE "swapfile01"
+#define MNTPOINT	"mntpoint"
+#define SWAP_FILE	MNTPOINT"/swapfile01"
 
 static void verify_swapon(void)
 {
 	TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
 
-	if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
+	if (tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {

I do not think that this should be removed, if the swapon() call fails we still do not want TBROK here.

Quoted 7 lines
testcases/kernel/syscalls/swapon/swapon01.c +0 -0
 		tst_brk(TBROK | TERRNO,
 				"Failed to turn off swapfile, system reboot recommended");
 	}
@@ -31,13 +33,20 @@ static void verify_swapon(void)
 
 static void setup(void)
 {

testcases/kernel/syscalls/swapon/swapon01.c static void verify_swapon(void) · line 42

Quoted 10 lines
testcases/kernel/syscalls/swapon/swapon01.c +3 -3
-	is_swap_supported(SWAP_FILE);
 	make_swapfile(SWAP_FILE, 0);
 }
 
 static struct tst_test test = {
-	.needs_root = 1,
-	.needs_tmpdir = 1,
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.needs_root = 1, /* for swapon() */

Can we please avoid comments like this?

I'ts pretty clear that if you are working with devices and setting up swapfiles it's priviledged operation...

testcases/kernel/syscalls/swapon/swapon01.c static void verify_swapon(void) · line 49

Quoted 8 lines
testcases/kernel/syscalls/swapon/swapon01.c +6 -1
+	.all_filesystems = 1,
 	.test_all = verify_swapon,
-	.setup = setup
+	.setup = setup,
+	.skip_filesystems = (const char *[]) {
+		"bcachefs",
+		"btrfs",
+		"tmpfs",

So the rest of the filesystems work fine? If so this change looks fine to me.

Quoted 3 lines
testcases/kernel/syscalls/swapon/swapon01.c +2 -0
+		NULL
+	},
 };
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#2 lore raw
Quoted 28 lines

Hi!

Quoted 26 lines
 testcases/kernel/syscalls/swapon/swapon01.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/swapon/swapon01.c +3 -1
index e59fb20a1..ef4ebfdcf 100644
@@ -8,6 +8,7 @@
  * [Description]
  *
  * Checks that swapon() succeds with swapfile.
+ * Testing on all filesystems which support swap file.
  */
 
 #include <unistd.h>
@@ -17,13 +18,14 @@
 #include "lapi/syscalls.h"
 #include "libswap.h"
 
-#define SWAP_FILE "swapfile01"
+#define MNTPOINT	"mntpoint"
+#define SWAP_FILE	MNTPOINT"/swapfile01"
 
 static void verify_swapon(void)
 {
 	TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
Quoted 5 lines

testcases/kernel/syscalls/swapon/swapon01.c line 28

Quoted 2 lines
testcases/kernel/syscalls/swapon/swapon01.c +1 -1
-	if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
+	if (tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {

I do not think that this should be removed, if the swapon() call fails we still do not want TBROK here.

+1, I did it by accident when rebasing the original patch.

Quoted 11 lines
Quoted 10 lines
testcases/kernel/syscalls/swapon/swapon01.c +0 -1
 		tst_brk(TBROK | TERRNO,
 				"Failed to turn off swapfile, system reboot recommended");
 	}
@@ -31,13 +33,20 @@ static void verify_swapon(void)
 
 static void setup(void)
 {
-	is_swap_supported(SWAP_FILE);
 	make_swapfile(SWAP_FILE, 0);
 }
Quoted 8 lines

testcases/kernel/syscalls/swapon/swapon01.c static void verify_swapon(void) · line 42

Quoted 6 lines
testcases/kernel/syscalls/swapon/swapon01.c +3 -2
 static struct tst_test test = {
-	.needs_root = 1,
-	.needs_tmpdir = 1,
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
+	.needs_root = 1, /* for swapon() */

Can we please avoid comments like this?

Sure (again part for the original effort).

Quoted 14 lines

I'ts pretty clear that if you are working with devices and setting up swapfiles it's priviledged operation...

testcases/kernel/syscalls/swapon/swapon01.c static void verify_swapon(void) · line 49

Quoted 8 lines
testcases/kernel/syscalls/swapon/swapon01.c +6 -1
+	.all_filesystems = 1,
 	.test_all = verify_swapon,
-	.setup = setup
+	.setup = setup,
+	.skip_filesystems = (const char *[]) {
+		"bcachefs",
+		"btrfs",
+		"tmpfs",

So the rest of the filesystems work fine? If so this change looks fine to me.

Yes.

Going to send v2.

Kind regards,
Petr

Quoted 3 lines
Quoted 3 lines
testcases/kernel/syscalls/swapon/swapon01.c +2 -0
+		NULL
+	},
 };
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi Petr

Quoted 8 lines · Petr Vorel
Quoted 3 lines

So the rest of the filesystems work fine? If so this change looks fine to me.

Yes.

Going to send v2.

No need, I dared to modify and combine it in the 2/4.
https://lists.linux.it/pipermail/ltp/2024-January/036806.html

Sorry for my arbitrary work, I just to test the patchset on my side easily.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
In reply to Petr Vorel

Hi Petr, Cyril,

Quoted 82 lines · Petr Vorel

Test on all filesystems to increase coverage.
Skip filesystems which does not support swap (currently bcachefs, btrfs and tmpfs).

Tested on 5.10, 6.6 and 6.7.

---
Hi Li, Cyril,

feel free to postpone this after the release.
If we are for adding this, I'll retest on Monday older filesystems.

man swapon(8) mentions:

Btrfs
Swap files on Btrfs are supported since Linux 5.0 on files with
nocow
attribute. See the btrfs(5) manual page for more details.

=> Can we pass "nocow" just to btrfs? Or should it be added to single test, which uses just btrfs with nocow?

NFS
Swap over NFS may not work.

=> I guess this is not important for us, as NFS is not part of filesystems in .all_filesystems, but maybe we should still add it to the whitelist?

Kind regards,
Petr

 testcases/kernel/syscalls/swapon/swapon01.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/swapon/swapon01.c +4 -2
index e59fb20a1..ef4ebfdcf 100644
@@ -8,6 +8,7 @@
  * [Description]
  *
  * Checks that swapon() succeds with swapfile.
+ * Testing on all filesystems which support swap file.
  */
 
 #include <unistd.h>
@@ -17,13 +18,14 @@
 #include "lapi/syscalls.h"
 #include "libswap.h"
 
-#define SWAP_FILE "swapfile01"
+#define MNTPOINT       "mntpoint"
+#define SWAP_FILE      MNTPOINT"/swapfile01"
 
 static void verify_swapon(void)
 {
        TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
 
-       if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
+       if (tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
                tst_brk(TBROK | TERRNO,
                                "Failed to turn off swapfile, system
 reboot recommended");
        }
testcases/kernel/syscalls/swapon/swapon01.c +4 -3
@@ -31,13 +33,20 @@ static void verify_swapon(void)
 
 static void setup(void)
 {
-       is_swap_supported(SWAP_FILE);
        make_swapfile(SWAP_FILE, 0);
 }
 
 static struct tst_test test = {
-       .needs_root = 1,
-       .needs_tmpdir = 1,
+       .mntpoint = MNTPOINT,
+       .mount_device = 1,
+       .needs_root = 1, /* for swapon() */
+       .all_filesystems = 1,

testcases/kernel/syscalls/swapon/swapon01.c static void verify_swapon(void) · line 52

Quoted 10 lines
testcases/kernel/syscalls/swapon/swapon01.c +7 -1
        .test_all = verify_swapon,
-       .setup = setup
+       .setup = setup,
+       .skip_filesystems = (const char *[]) {
+               "bcachefs",
+               "btrfs",
+               "tmpfs",
+               NULL
+       },
 

IMHO, I hate to add the skip_filesystem in this test, because if we do, then we have to add to all of the related swap tests that, it add more tedious work to people.

So, if we decide to postpone this patch to release, we can just refactor the is_swap_supported() function to make things easier. I will send an RFC patch to show something in my mind, what we need to do later is just to finalize the swap_supported_fs[] struct list.

Signature · 5 lines
Regards,
Li Wang

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

PATCH 1/2 libswap: add known swap supported fs check

This introduce an enhancement to the library's is_swap_supported function to check for filesystem compatibility before attempting to create and enable a swap file. A list of supported filesystems is added (ext2, ext3, ext4, xfs, vfat, exfat, ntfs), and a check against this list is performed to ensure that the swap operations are only attempted on known compatible filesystems.

If the make_swapfile function fails, the error handling is now more descriptive: it distinguishes between failures due to the filesystem not supporting swap files and other types of failures. Similarly, when attempting to enable the swap file with swapon, the patch ensures that clearer error messages are provided in cases where the operation is not supported by the filesystem.

---
 libs/libltpswap/libswap.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)
libs/libltpswap/libswap.c +26 -7
index d014325e5..5f9622aca 100644
@@ -12,6 +12,17 @@
 #include "libswap.h"
 #include "lapi/syscalls.h"
 
+static const char *const swap_supported_fs[] = {
+	"ext2",
+	"ext3",
+	"ext4",
+	"xfs",
+	"vfat",
+	"exfat",
+	"ntfs",
+	NULL
+};
+
 /*
  * Make a swap file
  */
@@ -39,23 +50,31 @@ int make_swapfile(const char *swapfile, int safe)
  */
 void is_swap_supported(const char *filename)
 {
+	int i, sw_support = 0;
 	int fibmap = tst_fibmap(filename);
 	long fs_type = tst_fs_type(filename);
 	const char *fstype = tst_fs_type_name(fs_type);
 
-	int ret = make_swapfile(filename, 1);
-	if (ret != 0) {
-		if (fibmap == 1)
-			tst_brk(TCONF, "mkswap on %s not supported", fstype);
-		else
-			tst_brk(TFAIL, "mkswap on %s failed", fstype);
+	for (i = 0; swap_supported_fs[i]; i++) {
+		if (!strcmp(fstype, swap_supported_fs[i])) {
+			sw_support = 1;
+			break;
+		}
 	}
 
+       int ret = make_swapfile(filename, 1);
+       if (ret != 0) {
+               if (fibmap == 1 && sw_support == 0)
+                       tst_brk(TCONF, "mkswap on %s not supported", fstype);
+               else
+                       tst_brk(TFAIL, "mkswap on %s failed", fstype);
+       }
+
 	TEST(tst_syscall(__NR_swapon, filename, 0));
 	if (TST_RET == -1) {
 		if (errno == EPERM)
 			tst_brk(TCONF, "Permission denied for swapon()");
-		else if (fibmap == 1 && errno == EINVAL)
+		else if (fibmap == 1 && errno == EINVAL && sw_support == 0)
 			tst_brk(TCONF, "Swapfile on %s not implemented", fstype);
 		else
 			tst_brk(TFAIL | TTERRNO, "swapon() on %s failed", fstype);
Signature · 5 lines
2.40.1


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

PATCH 2/2 swapon01: Test on all filesystems

From: Petr Vorel <[email protected]>

Test on all filesystems to increase coverage.
Skip filesystems which does not support swap (currently bcachefs, btrfs and tmpfs).

Tested on 5.10, 6.6 and 6.7.

---
 testcases/kernel/syscalls/swapon/swapon01.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/swapon/swapon01.c +6 -2
index e59fb20a1..e1fe50459 100644
@@ -8,6 +8,7 @@
  * [Description]
  *
  * Checks that swapon() succeds with swapfile.
+ * Testing on all filesystems which support swap file.
  */
 
 #include <unistd.h>
@@ -17,7 +18,8 @@
 #include "lapi/syscalls.h"
 #include "libswap.h"
 
-#define SWAP_FILE "swapfile01"
+#define MNTPOINT	"mntpoint"
+#define SWAP_FILE	MNTPOINT"/swapfile01"
 
 static void verify_swapon(void)
 {
@@ -36,8 +38,10 @@ static void setup(void)
 }
 
 static struct tst_test test = {
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
 	.needs_root = 1,
-	.needs_tmpdir = 1,
+	.all_filesystems = 1,
 	.test_all = verify_swapon,
 	.setup = setup
 };
Signature · 5 lines
2.40.1


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

PATCH 1/1 swapon01: Improving test with memory limits and swap reporting

This is target to create a more robust and controlled environment to test the swapon system call. By introducing memory limits through cgroups and filling memory with a known pattern, the test can better assess swapon behavior when the system experiences memory pressure.

Additionally, the reporting of "SwapCached" memory before turning off the swap file provides a clearer understanding of the swap system's state in response to the test conditions.

---
 testcases/kernel/syscalls/swapon/swapon01.c | 9 +++++++++
 1 file changed, 9 insertions(+)
testcases/kernel/syscalls/swapon/swapon01.c +9 -0
index e1fe50459..faf4a8002 100644
@@ -20,6 +20,7 @@
 
 #define MNTPOINT	"mntpoint"
 #define SWAP_FILE	MNTPOINT"/swapfile01"
+#define TESTMEM		(1UL<<30)
 
 static void verify_swapon(void)
 {
@@ -29,12 +30,19 @@ static void verify_swapon(void)
 		tst_brk(TBROK | TERRNO,
 				"Failed to turn off swapfile, system reboot recommended");
 	}
+
+	tst_res(TINFO, "SwapCached: %ld Kb", SAFE_READ_MEMINFO("SwapCached:"));
 }
 
 static void setup(void)
 {
 	is_swap_supported(SWAP_FILE);
 	make_swapfile(SWAP_FILE, 0);
+
+	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
+	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
+
+	tst_pollute_memory(TESTMEM, 0x41);
 }
 
 static struct tst_test test = {
@@ -42,6 +50,7 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.needs_root = 1,
 	.all_filesystems = 1,
+	.needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
 	.test_all = verify_swapon,
 	.setup = setup
 };
Signature · 5 lines
2.40.1


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

Re: [LTP] [PATCH] swapon01: Improving test with memory limits and swap reporting

Quoted 37 lines · Li Wang

This is target to create a more robust and controlled environment to test the swapon system call. By introducing memory limits through cgroups and filling memory with a known pattern, the test can better assess swapon behavior when the system experiences memory pressure.

Additionally, the reporting of "SwapCached" memory before turning off the swap file provides a clearer understanding of the swap system's state in response to the test conditions.

---
 testcases/kernel/syscalls/swapon/swapon01.c | 9 +++++++++
 1 file changed, 9 insertions(+)
testcases/kernel/syscalls/swapon/swapon01.c +3 -0
index e1fe50459..faf4a8002 100644
@@ -20,6 +20,7 @@
 
 #define MNTPOINT       "mntpoint"
 #define SWAP_FILE      MNTPOINT"/swapfile01"
+#define TESTMEM                (1UL<<30)
 
 static void verify_swapon(void)
 {
@@ -29,12 +30,19 @@ static void verify_swapon(void)
                tst_brk(TBROK | TERRNO,
                                "Failed to turn off swapfile, system
 reboot recommended");
        }
+
+       tst_res(TINFO, "SwapCached: %ld Kb",
 SAFE_READ_MEMINFO("SwapCached:"));
 }
 

testcases/kernel/syscalls/swapon/swapon01.c static void verify_swapon(void) · line 48

Quoted 10 lines
testcases/kernel/syscalls/swapon/swapon01.c +5 -0
 static void setup(void)
 {
        is_swap_supported(SWAP_FILE);
        make_swapfile(SWAP_FILE, 0);
+
+       SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
+       SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
+
+       tst_pollute_memory(TESTMEM, 0x41);
 

Oops, this pollute memory should move to the behind of swapon, I forget to submit the last changes when formatting the patch.

testcases/kernel/syscalls/swapon/swapon01.c +2 -0
@@ -26,12 +26,13 @@ static void verify_swapon(void)
 {
        TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
 
+       tst_pollute_memory(TESTMEM, 0x41);
+       tst_res(TINFO, "SwapCached: %ld Kb",

SAFE_READ_MEMINFO("SwapCached:"));
+
if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
tst_brk(TBROK | TERRNO,
"Failed to turn off swapfile, system reboot

recommended");
        }

-
- tst_res(TINFO, "SwapCached: %ld Kb",
SAFE_READ_MEMINFO("SwapCached:"));
}

static void setup(void)
@@ -41,8 +42,6 @@ static void setup(void)

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

-
- tst_pollute_memory(TESTMEM, 0x41);
}

static struct tst_test test = {

Signature · 5 lines
Regards,
Li Wang

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

PATCH 1/1 libswap: add Btrfs noCOW attribute setting for swap files

In reply to Li Wang

The patch aims to ensure swap files on Btrfs filesystems are created with the appropriate FS_NOCOW_FL attribute, which is necessary to disable CoW (Copy-on-Write) for swap files, perthe btrfs(5) manual page. This change is gated behind a kernel version check to ensure compatibility with the system's capabilities.

---

Notes:
Hi Petr,

I haven't gotten a chance to test this patch on any Btrfs platform, but only compile successfully without error on my RHEL8/9(xfs). Can you help test and guarantee it works for you?

 libs/libltpswap/libswap.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
libs/libltpswap/libswap.c +35 -0
index 5f9622aca..8b180f288 100644
@@ -4,6 +4,7 @@
  * Author: Stanislav Kholmanskikh <[email protected]>
  */
 
+#include <linux/fs.h>
 #include <errno.h>
 
 #define TST_NO_DEFAULT_MAIN
@@ -23,11 +24,37 @@ static const char *const swap_supported_fs[] = {
 	NULL
 };
 
+static void set_nocow_attr(const char *filename)
+{
+	int fd;
+	int attrs;
+
+	fd = SAFE_OPEN(filename, O_RDONLY);
+
+	if (ioctl(fd, FS_IOC_GETFLAGS, &attrs) == -1) {
+		tst_res(TFAIL | TERRNO, "Error getting attributes");
+		close(fd);
+		return;
+	}
+
+	attrs |= FS_NOCOW_FL;
+
+	if (ioctl(fd, FS_IOC_SETFLAGS, &attrs) == -1)
+		tst_res(TFAIL | TERRNO, "Error setting FS_NOCOW_FL attribute");
+	else
+		tst_res(TINFO, "FS_NOCOW_FL attribute set on %s\n", filename);
+
+	close(fd);
+}
+
 /*
  * Make a swap file
  */
 int make_swapfile(const char *swapfile, int safe)
 {
+	long fs_type = tst_fs_type(swapfile);
+	const char *fstype = tst_fs_type_name(fs_type);
+
 	if (!tst_fs_has_free(".", sysconf(_SC_PAGESIZE) * 10, TST_BYTES))
 		tst_brk(TBROK, "Insufficient disk space to create swap file");
 
@@ -35,6 +62,14 @@ int make_swapfile(const char *swapfile, int safe)
 	if (tst_fill_file(swapfile, 0, sysconf(_SC_PAGESIZE), 10) != 0)
 		tst_brk(TBROK, "Failed to create swapfile");
 
+	/* Btrfs file need set 'nocow' attribute */
+	if (strcmp(fstype, "btrfs") == 0) {
+		if (tst_kvercmp(5, 0, 0) > 0)
+			set_nocow_attr(swapfile);
+		else
+			tst_brk(TCONF, "Swapfile on %s not implemented", fstype);
+	}
+
 	/* make the file swapfile */
 	const char *argv[2 + 1];
 	argv[0] = "mkswap";
Signature · 5 lines
2.40.1


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

Re: [LTP] [PATCH] libswap: add Btrfs noCOW attribute setting for swap files

Hi Li,

Quoted 7 lines

The patch aims to ensure swap files on Btrfs filesystems are created with the appropriate FS_NOCOW_FL attribute, which is necessary to disable CoW (Copy-on-Write) for swap files, perthe btrfs(5) manual page. This change is gated behind a kernel version check to ensure compatibility with the system's capabilities.

Quoted 8 lines

---

Notes:
Hi Petr,

I haven't gotten a chance to test this patch on any Btrfs platform, but only compile successfully without error on my RHEL8/9(xfs). Can you help test and guarantee it works for you?

I'm not able to apply this to the current master. I also got a bit confused by number of patches for libswap / swapon01. Could you please resend all the patches again (in a series)? Or push branch to your fork and point the location? (whatever is easier to you)

Kind regards,
Petr

Quoted 68 lines
 libs/libltpswap/libswap.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
libs/libltpswap/libswap.c +35 -0
index 5f9622aca..8b180f288 100644
@@ -4,6 +4,7 @@
  * Author: Stanislav Kholmanskikh <[email protected]>
  */
 
+#include <linux/fs.h>
 #include <errno.h>
 
 #define TST_NO_DEFAULT_MAIN
@@ -23,11 +24,37 @@ static const char *const swap_supported_fs[] = {
 	NULL
 };
 
+static void set_nocow_attr(const char *filename)
+{
+	int fd;
+	int attrs;
+
+	fd = SAFE_OPEN(filename, O_RDONLY);
+
+	if (ioctl(fd, FS_IOC_GETFLAGS, &attrs) == -1) {
+		tst_res(TFAIL | TERRNO, "Error getting attributes");
+		close(fd);
+		return;
+	}
+
+	attrs |= FS_NOCOW_FL;
+
+	if (ioctl(fd, FS_IOC_SETFLAGS, &attrs) == -1)
+		tst_res(TFAIL | TERRNO, "Error setting FS_NOCOW_FL attribute");
+	else
+		tst_res(TINFO, "FS_NOCOW_FL attribute set on %s\n", filename);
+
+	close(fd);
+}
+
 /*
  * Make a swap file
  */
 int make_swapfile(const char *swapfile, int safe)
 {
+	long fs_type = tst_fs_type(swapfile);
+	const char *fstype = tst_fs_type_name(fs_type);
+
 	if (!tst_fs_has_free(".", sysconf(_SC_PAGESIZE) * 10, TST_BYTES))
 		tst_brk(TBROK, "Insufficient disk space to create swap file");
 
@@ -35,6 +62,14 @@ int make_swapfile(const char *swapfile, int safe)
 	if (tst_fill_file(swapfile, 0, sysconf(_SC_PAGESIZE), 10) != 0)
 		tst_brk(TBROK, "Failed to create swapfile");
 
+	/* Btrfs file need set 'nocow' attribute */
+	if (strcmp(fstype, "btrfs") == 0) {
+		if (tst_kvercmp(5, 0, 0) > 0)
+			set_nocow_attr(swapfile);
+		else
+			tst_brk(TCONF, "Swapfile on %s not implemented", fstype);
+	}
+
 	/* make the file swapfile */
 	const char *argv[2 + 1];
 	argv[0] = "mkswap";
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

Re: [LTP] [PATCH] libswap: add Btrfs noCOW attribute setting for swap files

Quoted 19 lines · Petr Vorel

Hi Li,

Quoted 4 lines

The patch aims to ensure swap files on Btrfs filesystems are created with the appropriate FS_NOCOW_FL attribute, which is necessary to disable CoW (Copy-on-Write) for swap files, perthe btrfs(5) manual page. This change is gated behind a kernel version check to ensure

compatibility

Quoted 11 lines

with the system's capabilities.

---

Notes:
Hi Petr,

I haven't gotten a chance to test this patch on any Btrfs platform, but only compile successfully without error on my RHEL8/9(xfs). Can you help test and guarantee it works for you?

Quoted 7 lines

I'm not able to apply this to the current master. I also got a bit confused by
number of patches for libswap / swapon01. Could you please resend all the patches again (in a series)? Or push branch to your fork and point the location?
(whatever is easier to you)

Sure, let's format them in a set.

Sorry for the confusion, I was just developing them one by one and sent directly.

Quoted 78 lines

Kind regards,
Petr

Quoted 26 lines
 libs/libltpswap/libswap.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
libs/libltpswap/libswap.c +8 -0
index 5f9622aca..8b180f288 100644
@@ -4,6 +4,7 @@
  * Author: Stanislav Kholmanskikh <[email protected]>
  */
 
+#include <linux/fs.h>
 #include <errno.h>
 
 #define TST_NO_DEFAULT_MAIN
@@ -23,11 +24,37 @@ static const char *const swap_supported_fs[] = {
      NULL
 };
 
+static void set_nocow_attr(const char *filename)
+{
+     int fd;
+     int attrs;
+
+     fd = SAFE_OPEN(filename, O_RDONLY);
+

libs/libltpswap/libswap.c static const char *const swap_supported_fs[] = { · line 43

Quoted 10 lines
libs/libltpswap/libswap.c +10 -0
+     if (ioctl(fd, FS_IOC_GETFLAGS, &attrs) == -1) {
+             tst_res(TFAIL | TERRNO, "Error getting attributes");
+             close(fd);
+             return;
+     }
+
+     attrs |= FS_NOCOW_FL;
+
+     if (ioctl(fd, FS_IOC_SETFLAGS, &attrs) == -1)
+             tst_res(TFAIL | TERRNO, "Error setting FS_NOCOW_FL

attribute");

libs/libltpswap/libswap.c static const char *const swap_supported_fs[] = { · line 45

Quoted 2 lines
libs/libltpswap/libswap.c +2 -0
+     else
+             tst_res(TINFO, "FS_NOCOW_FL attribute set on %s\n",

filename);

Quoted 4 lines
libs/libltpswap/libswap.c +4 -0
+
+     close(fd);
+}
+

libs/libltpswap/libswap.c static const char *const swap_supported_fs[] = {

Quoted 10 lines
libs/libltpswap/libswap.c +3 -0
 /*
  * Make a swap file
  */
 int make_swapfile(const char *swapfile, int safe)
 {
+     long fs_type = tst_fs_type(swapfile);
+     const char *fstype = tst_fs_type_name(fs_type);
+
      if (!tst_fs_has_free(".", sysconf(_SC_PAGESIZE) * 10, TST_BYTES))
              tst_brk(TBROK, "Insufficient disk space to create swap

file");

libs/libltpswap/libswap.c int make_swapfile(const char *swapfile, int safe) · line 70

Quoted 10 lines
libs/libltpswap/libswap.c +6 -0
@@ -35,6 +62,14 @@ int make_swapfile(const char *swapfile, int safe)
      if (tst_fill_file(swapfile, 0, sysconf(_SC_PAGESIZE), 10) != 0)
              tst_brk(TBROK, "Failed to create swapfile");
 
+     /* Btrfs file need set 'nocow' attribute */
+     if (strcmp(fstype, "btrfs") == 0) {
+             if (tst_kvercmp(5, 0, 0) > 0)
+                     set_nocow_attr(swapfile);
+             else
+                     tst_brk(TCONF, "Swapfile on %s not implemented",

fstype);

Quoted 5 lines
libs/libltpswap/libswap.c +2 -0
+     }
+
      /* make the file swapfile */
      const char *argv[2 + 1];
      argv[0] = "mkswap";
Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#12 lore raw
In reply to Li Wang

Hi Li, Cyril,

Quoted 98 lines

Hi Petr, Cyril,

Quoted 81 lines · Petr Vorel

Test on all filesystems to increase coverage.
Skip filesystems which does not support swap (currently bcachefs, btrfs and tmpfs).

Tested on 5.10, 6.6 and 6.7.

---
Hi Li, Cyril,

feel free to postpone this after the release.
If we are for adding this, I'll retest on Monday older filesystems.

man swapon(8) mentions:

Btrfs
Swap files on Btrfs are supported since Linux 5.0 on files with
nocow
attribute. See the btrfs(5) manual page for more details.

=> Can we pass "nocow" just to btrfs? Or should it be added to single test, which uses just btrfs with nocow?

NFS
Swap over NFS may not work.

=> I guess this is not important for us, as NFS is not part of filesystems in .all_filesystems, but maybe we should still add it to the whitelist?

Kind regards,
Petr

 testcases/kernel/syscalls/swapon/swapon01.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/swapon/swapon01.c +4 -2
index e59fb20a1..ef4ebfdcf 100644
@@ -8,6 +8,7 @@
  * [Description]
  *
  * Checks that swapon() succeds with swapfile.
+ * Testing on all filesystems which support swap file.
  */
 
 #include <unistd.h>
@@ -17,13 +18,14 @@
 #include "lapi/syscalls.h"
 #include "libswap.h"
 
-#define SWAP_FILE "swapfile01"
+#define MNTPOINT       "mntpoint"
+#define SWAP_FILE      MNTPOINT"/swapfile01"
 
 static void verify_swapon(void)
 {
        TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
 
-       if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
+       if (tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
                tst_brk(TBROK | TERRNO,
                                "Failed to turn off swapfile, system
 reboot recommended");
        }
testcases/kernel/syscalls/swapon/swapon01.c +3 -3
@@ -31,13 +33,20 @@ static void verify_swapon(void)
 
 static void setup(void)
 {
-       is_swap_supported(SWAP_FILE);
        make_swapfile(SWAP_FILE, 0);
 }
 
 static struct tst_test test = {
-       .needs_root = 1,
-       .needs_tmpdir = 1,
+       .mntpoint = MNTPOINT,
+       .mount_device = 1,
+       .needs_root = 1, /* for swapon() */

testcases/kernel/syscalls/swapon/swapon01.c static void verify_swapon(void) · line 51

Quoted 10 lines
testcases/kernel/syscalls/swapon/swapon01.c +8 -1
+       .all_filesystems = 1,
        .test_all = verify_swapon,
-       .setup = setup
+       .setup = setup,
+       .skip_filesystems = (const char *[]) {
+               "bcachefs",
+               "btrfs",
+               "tmpfs",
+               NULL
+       },
Quoted 3 lines

IMHO, I hate to add the skip_filesystem in this test, because if we do, then we have to add to all of the related swap tests that, it add more tedious work to people.

First I thought it'd be ok to have just single test which uses all_filesystems (better one than none), but sure, having all of them would be better.

Quoted 4 lines

So, if we decide to postpone this patch to release, we can just refactor the is_swap_supported() function to make things easier. I will send an RFC patch to show something in my mind, what we need to do later is just to finalize the swap_supported_fs[] struct list.

Sure, this is not critical, it can wait if you have better solution. Thanks for spending time on it.

Kind regards,
Petr

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

Hi Li, Cyril,

Quoted 32 lines

Hi Petr, Cyril,

Quoted 28 lines · Petr Vorel

Test on all filesystems to increase coverage.
Skip filesystems which does not support swap (currently bcachefs, btrfs and tmpfs).

Tested on 5.10, 6.6 and 6.7.

---
Hi Li, Cyril,

feel free to postpone this after the release.
If we are for adding this, I'll retest on Monday older filesystems.

man swapon(8) mentions:

Btrfs
Swap files on Btrfs are supported since Linux 5.0 on files with
nocow
attribute. See the btrfs(5) manual page for more details.

=> Can we pass "nocow" just to btrfs? Or should it be added to single test, which uses just btrfs with nocow?

NFS
Swap over NFS may not work.

=> I guess this is not important for us, as NFS is not part of filesystems in .all_filesystems, but maybe we should still add it to

the

Quoted 66 lines
Quoted 53 lines

whitelist?

Kind regards,
Petr

 testcases/kernel/syscalls/swapon/swapon01.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/swapon/swapon01.c +4 -2
index e59fb20a1..ef4ebfdcf 100644
@@ -8,6 +8,7 @@
  * [Description]
  *
  * Checks that swapon() succeds with swapfile.
+ * Testing on all filesystems which support swap file.
  */
 
 #include <unistd.h>
@@ -17,13 +18,14 @@
 #include "lapi/syscalls.h"
 #include "libswap.h"
 
-#define SWAP_FILE "swapfile01"
+#define MNTPOINT       "mntpoint"
+#define SWAP_FILE      MNTPOINT"/swapfile01"
 
 static void verify_swapon(void)
 {
        TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
 
-       if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
+       if (tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
                tst_brk(TBROK | TERRNO,
                                "Failed to turn off swapfile, system
 reboot recommended");
        }
testcases/kernel/syscalls/swapon/swapon01.c +3 -3
@@ -31,13 +33,20 @@ static void verify_swapon(void)
 
 static void setup(void)
 {
-       is_swap_supported(SWAP_FILE);
        make_swapfile(SWAP_FILE, 0);
 }
 
 static struct tst_test test = {
-       .needs_root = 1,
-       .needs_tmpdir = 1,
+       .mntpoint = MNTPOINT,
+       .mount_device = 1,
+       .needs_root = 1, /* for swapon() */

testcases/kernel/syscalls/swapon/swapon01.c static void verify_swapon(void) · line 51

Quoted 10 lines
testcases/kernel/syscalls/swapon/swapon01.c +8 -1
+       .all_filesystems = 1,
        .test_all = verify_swapon,
-       .setup = setup
+       .setup = setup,
+       .skip_filesystems = (const char *[]) {
+               "bcachefs",
+               "btrfs",
+               "tmpfs",
+               NULL
+       },
Quoted 3 lines

IMHO, I hate to add the skip_filesystem in this test, because if we do, then we have to add to all of the related swap tests that, it add more tedious work to people.

First I thought it'd be ok to have just single test which uses all_filesystems
(better one than none), but sure, having all of them would be better.

Quoted 9 lines
Quoted 2 lines

So, if we decide to postpone this patch to release, we can just refactor the is_swap_supported() function to make things easier. I will send an

RFC

Quoted 2 lines

patch to show something in my mind, what we need to do later is just to finalize the swap_supported_fs[] struct list.

Sure, this is not critical, it can wait if you have better solution. Thanks for spending time on it.

Yes, but it is ready there 1/4 :).
https://lists.linux.it/pipermail/ltp/2024-January/036804.html

The rest work is just to finalize the swap_supported_fs list. And, I agree that all of these can be postponed. Because so far it won't be a critical/urgent-fix for a new release.

Signature · 5 lines
Regards,
Li Wang

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

Hi Li,

Quoted 121 lines · Petr Vorel
Quoted 110 lines

Hi Li, Cyril,

Quoted 32 lines

Hi Petr, Cyril,

Quoted 28 lines · Petr Vorel

Test on all filesystems to increase coverage.
Skip filesystems which does not support swap (currently bcachefs, btrfs and tmpfs).

Tested on 5.10, 6.6 and 6.7.

---
Hi Li, Cyril,

feel free to postpone this after the release.
If we are for adding this, I'll retest on Monday older filesystems.

man swapon(8) mentions:

Btrfs
Swap files on Btrfs are supported since Linux 5.0 on files with
nocow
attribute. See the btrfs(5) manual page for more details.

=> Can we pass "nocow" just to btrfs? Or should it be added to single test, which uses just btrfs with nocow?

NFS
Swap over NFS may not work.

=> I guess this is not important for us, as NFS is not part of filesystems in .all_filesystems, but maybe we should still add it to

the

Quoted 66 lines
Quoted 53 lines

whitelist?

Kind regards,
Petr

 testcases/kernel/syscalls/swapon/swapon01.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/swapon/swapon01.c +4 -2
index e59fb20a1..ef4ebfdcf 100644
@@ -8,6 +8,7 @@
  * [Description]
  *
  * Checks that swapon() succeds with swapfile.
+ * Testing on all filesystems which support swap file.
  */
 
 #include <unistd.h>
@@ -17,13 +18,14 @@
 #include "lapi/syscalls.h"
 #include "libswap.h"
 
-#define SWAP_FILE "swapfile01"
+#define MNTPOINT       "mntpoint"
+#define SWAP_FILE      MNTPOINT"/swapfile01"
 
 static void verify_swapon(void)
 {
        TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
 
-       if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
+       if (tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
                tst_brk(TBROK | TERRNO,
                                "Failed to turn off swapfile, system
 reboot recommended");
        }
testcases/kernel/syscalls/swapon/swapon01.c +3 -3
@@ -31,13 +33,20 @@ static void verify_swapon(void)
 
 static void setup(void)
 {
-       is_swap_supported(SWAP_FILE);
        make_swapfile(SWAP_FILE, 0);
 }
 
 static struct tst_test test = {
-       .needs_root = 1,
-       .needs_tmpdir = 1,
+       .mntpoint = MNTPOINT,
+       .mount_device = 1,
+       .needs_root = 1, /* for swapon() */

testcases/kernel/syscalls/swapon/swapon01.c static void verify_swapon(void) · line 51

Quoted 10 lines
testcases/kernel/syscalls/swapon/swapon01.c +8 -1
+       .all_filesystems = 1,
        .test_all = verify_swapon,
-       .setup = setup
+       .setup = setup,
+       .skip_filesystems = (const char *[]) {
+               "bcachefs",
+               "btrfs",
+               "tmpfs",
+               NULL
+       },
Quoted 3 lines

IMHO, I hate to add the skip_filesystem in this test, because if we do, then we have to add to all of the related swap tests that, it add more tedious work to people.

First I thought it'd be ok to have just single test which uses all_filesystems
(better one than none), but sure, having all of them would be better.

Quoted 8 lines
Quoted 2 lines

So, if we decide to postpone this patch to release, we can just refactor the is_swap_supported() function to make things easier. I will send an

RFC

Quoted 2 lines

patch to show something in my mind, what we need to do later is just to finalize the swap_supported_fs[] struct list.

Sure, this is not critical, it can wait if you have better solution. Thanks for spending time on it.

Quoted 6 lines

Yes, but it is ready there 1/4 :).
https://lists.linux.it/pipermail/ltp/2024-January/036804.html

The rest work is just to finalize the swap_supported_fs list. And, I agree that all of these can be postponed. Because so far it won't be a critical/urgent-fix for a new release.

OTOH if you prepared this, I'm ok to review and test. I already run various tests on this.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp