Skip to contentSkip to conversation

swapoff01: Create 64MB swapfile instead of 65536 blocks

PATCH In review Wake Liu via ltp 2 participants 3 messages Nov 23

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 swapoff01: Create 64MB swapfile instead of 65536 blocks

Before commit 6249e87b5fcf ("libswap: customize swapfile size"), swapoff01 created a 64MB swapfile (65536 * 1024 bytes). Converting that count directly to filesystem blocks made the swapfile size scale with
f_bsize: 256MB on 4KB filesystems (requiring .dev_min_size = 350), 1GiB on 16KB block filesystems, 2GiB on exfat (32KB clusters on a 350MB loop device), and 4GiB on 64KB block filesystems, exceeding the loop backing device and failing with TCONF.

Use SAFE_MAKE_SWAPFILE_SIZE(SWAP_FILE, 64) to restore a fixed 64MB swapfile independent of filesystem block size, and drop the now unnecessary .dev_min_size = 350 override since 64MB fits comfortably in the default 300MB loop device.

Fixes 6249e87b5fcf ("libswap: customize swapfile size")
---
 testcases/kernel/syscalls/swapoff/swapoff01.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
testcases/kernel/syscalls/swapoff/swapoff01.c +1 -2
index 065b5edbc..781fe75d6 100644
@@ -41,13 +41,12 @@ static void verify_swapoff(void)
 static void setup(void)
 {
 	is_swap_supported(TEST_FILE);
-	SAFE_MAKE_SWAPFILE_BLKS(SWAP_FILE, 65536);
+	SAFE_MAKE_SWAPFILE_SIZE(SWAP_FILE, 64);
 }
 
 static struct tst_test test = {
 	.mntpoint = MNTPOINT,
 	.mount_device = 1,
-	.dev_min_size = 350,
 	.all_filesystems = 1,
 	.needs_root = 1,
 	.test_all = verify_swapoff,
Signature · 5 lines
2.56.1.750.gf70dc682cf-goog


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

PATCH 2/2 tse_swap: Stat target directory instead of "." in make_swapfile()

make_swapfile() calls safe_statvfs() on "." (the LTP temporary directory) before resolving the directory where the swapfile will be created. When the temporary directory resides on a filesystem with a larger block size than the mounted loop filesystem on mntpoint (for example, 16KB f2fs tmpdir vs 4KB ext4 loop filesystem), blk_size is taken from "." instead of the target filesystem. With SWAPFILE_BY_BLKS, this inflates the requested swapfile size (e.g. 4x on 16KB vs 4KB) and can falsely trigger TCONF in tst_fs_has_free().

In addition, extracting mnt_path via sscanf("%[^/]") fails for absolute paths, truncates nested relative paths at the first slash, and returns the non-existent swapfile name when no slash is present.

Resolve the parent directory of swapfile upfront with safe_dirname() on a path copy and pass it to both safe_statvfs() and tst_fs_has_free(). Also widen byte-size multiplications to uint64_t to avoid 32-bit overflow with large block sizes or swapfile sizes.

Fixes 0f5d8c520d4e ("libswap.c: Check free space with correct mnt path")
---
 libs/swap/tse_swap.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
libs/swap/tse_swap.c +10 -7
index ae3c19313..21dce8a64 100644
@@ -144,15 +144,20 @@ int make_swapfile(const char *file, const int lineno,
 	unsigned long blk_size;
 	unsigned int blocks = 0;
 	size_t pg_size = sysconf(_SC_PAGESIZE);
-	char mnt_path[PATH_MAX];
+	char cpy[PATH_MAX];
+	char *mnt_path;
 
-	safe_statvfs(file, lineno, ".", &fs_info);
+	if (snprintf(cpy, sizeof(cpy), "%s", swapfile) >= (int)sizeof(cpy))
+		tst_brk_(file, lineno, TBROK, "Path too long: %s", swapfile);
+
+	mnt_path = safe_dirname(file, lineno, NULL, cpy);
+	safe_statvfs(file, lineno, mnt_path, &fs_info);
 
 	blk_size = fs_info.f_bsize;
 
 	if (method == SWAPFILE_BY_SIZE) {
 		tst_res_(file, lineno, TINFO, "create a swapfile size of %u megabytes (MB)", num);
-		blocks = num * 1024 * 1024 / blk_size;
+		blocks = (uint64_t)num * TST_MB / blk_size;
 	} else if (method == SWAPFILE_BY_BLKS) {
 		blocks = num;
 		tst_res_(file, lineno, TINFO, "create a swapfile with %u block numbers", blocks);
@@ -161,16 +166,14 @@ int make_swapfile(const char *file, const int lineno,
 	}
 
 	/* To guarantee at least one page can be swapped out */
-	if (blk_size * blocks < pg_size) {
+	if ((uint64_t)blk_size * blocks < pg_size) {
 		tst_res_(file, lineno, TWARN, "Swapfile size is less than the system page size. "
 			"Using page size (%lu bytes) instead of block size (%lu bytes).",
 			(unsigned long)pg_size, blk_size);
 		blk_size = pg_size;
 	}
 
-	safe_sscanf(file, lineno, swapfile, "%[^/]", mnt_path);
-
-	if (!tst_fs_has_free(mnt_path, blk_size * blocks, TST_BYTES))
+	if (!tst_fs_has_free(mnt_path, (uint64_t)blk_size * blocks, TST_BYTES))
 		tst_brk_(file, lineno, TCONF, "Insufficient disk space to create swap file");
 
 	/* create file */
Signature · 5 lines
2.56.1.750.gf70dc682cf-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to Wake Liu via ltp

Hi Wake,

Quoted 1 line · Wake Liu

swapoff01: Create 64MB swapfile instead of 65536 blocks

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

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