* [LTP] [PATCH v1] swapon04: Verify discarded block delta instead of absolute block count
@ 2026-11-30 7:39 Wei Gao via ltp
2026-11-30 14:30 ` [LTP] " linuxtestproject.agent
0 siblings, 1 reply; 2+ messages in thread
From: Wei Gao via ltp @ 2026-11-30 7:39 UTC (permalink / raw)
To: ltp; +Cc: Dave Chinner
On filesystems with dynamic speculative preallocation (such as XFS),
writing sequentially to a file causes the filesystem to preallocate
blocks beyond EOF (e.g. allocating 32 MiB / 65408 blocks for a 16 MiB
file) to prevent fragmentation. Since Linux 6.12 commit f1204d96450f
("xfs: only free posteof blocks on first close"), XFS only trims
speculative post-EOF blocks on the first close; subsequent closes
preserve them to avoid fragmentation in repeated open/write/close
workloads unless the file was truncated down (tracked by XFS_ITRUNCATED).
During test execution, if background writeback (kworker flush) runs
while tst_fill_file() writes the 16 MiB data, xfs_vm_writepages()
clears XFS_ITRUNCATED before close() is called. As a result,
xfs_file_release() sees XFS_ITRUNCATED is 0, treats the close as a
subsequent close, and skips removing the post-EOF blocks.
When swapon(SWAP_FLAG_DISCARD) is subsequently invoked, the kernel
discards the 16 MiB swap area, but leaves the extra post-EOF blocks
untouched. Checking an absolute remaining block count
(blocks_after <= max_header_blocks) causes the test to fail
sporadically:
swapon04.c:170: TFAIL: blocks_after (32648) > max_header_blocks (16)
Neither POSIX nor Linux guarantees that st_blocks matches the written
file size without speculative preallocations or metadata. Asserting an
absolute remaining block count is an invalid filesystem assumption.
Fix this by verifying that the expected swap area was discarded
(blocks_before - blocks_after >= min_discard_blocks), rather than checking
an absolute block count.
Link: https://lore.kernel.org/all/awtR1hscpbpeydnU@dread/
Signed-off-by: Wei Gao <wegao@suse.com>
---
testcases/kernel/syscalls/swapon/swapon04.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/testcases/kernel/syscalls/swapon/swapon04.c b/testcases/kernel/syscalls/swapon/swapon04.c
index e0d59d042..7d38f2476 100644
--- a/testcases/kernel/syscalls/swapon/swapon04.c
+++ b/testcases/kernel/syscalls/swapon/swapon04.c
@@ -45,7 +45,7 @@ static char loop_dev[PATH_MAX];
static int loop_dev_id = -1;
static int loop_attached;
static int swap_active;
-static size_t max_header_blocks;
+static size_t min_discard_blocks;
static struct tcase {
int flags;
@@ -127,8 +127,8 @@ static void setup(void)
blk_size = st.st_blksize;
alloc_units = page_size > blk_size ? page_size : blk_size;
- /* Minimum 512-byte blocks for 1 page + small tolerance for filesystem metadata */
- max_header_blocks = (alloc_units / 512) * 2;
+ /* Discard everything except the swap header page and metadata tolerance */
+ min_discard_blocks = (SWAP_SIZE_MB * TST_MB - alloc_units * 2) / 512;
}
static void verify_swapon(unsigned int n)
@@ -166,10 +166,13 @@ static void verify_swapon(unsigned int n)
SAFE_STAT(BACKING_FILE, &st);
blocks_after = st.st_blocks;
- if (tc->exp_discard)
- TST_EXP_LE_LU(blocks_after, max_header_blocks);
- else
+ if (tc->exp_discard) {
+ blkcnt_t discarded = blocks_before > blocks_after ?
+ blocks_before - blocks_after : 0;
+ TST_EXP_LE_LU(min_discard_blocks, discarded);
+ } else {
TST_EXP_EQ_LI(blocks_after, blocks_before);
+ }
if (swapoff(loop_dev) != 0)
tst_brk(TBROK | TERRNO, "swapoff(%s) failed", loop_dev);
--
2.55.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 14:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 7:39 [LTP] [PATCH v1] swapon04: Verify discarded block delta instead of absolute block count Wei Gao via ltp
2026-11-30 14:30 ` [LTP] " linuxtestproject.agent
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.