All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wake Liu via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Cc: Wake Liu <wakel@google.com>
Subject: [LTP] [PATCH 2/2] tse_swap: Stat target directory instead of "." in make_swapfile()
Date: Mon, 23 Nov 2026 12:36:25 +0000	[thread overview]
Message-ID: <20261123123626.1499705-2-wakel@google.com> (raw)
In-Reply-To: <20261123123626.1499705-1-wakel@google.com>

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")
Signed-off-by: Wake Liu <wakel@google.com>
---
 libs/swap/tse_swap.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/libs/swap/tse_swap.c b/libs/swap/tse_swap.c
index ae3c19313..21dce8a64 100644
--- a/libs/swap/tse_swap.c
+++ b/libs/swap/tse_swap.c
@@ -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 */
-- 
2.56.1.750.gf70dc682cf-goog


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

  reply	other threads:[~2026-11-23 12:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 12:36 [LTP] [PATCH 1/2] swapoff01: Create 64MB swapfile instead of 65536 blocks Wake Liu via ltp
2026-11-23 12:36 ` Wake Liu via ltp [this message]
2026-11-23 20:38 ` [LTP] " linuxtestproject.agent

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123123626.1499705-2-wakel@google.com \
    --to=ltp@lists.linux.it \
    --cc=wakel@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.