generic: test small swapfile without page-aligned contiguous blocks
PATCH v5 1/1 generic: test small swapfile without page-aligned contiguous blocks
If a swapfile doesn't contain even a single page-aligned contiguous range of blocks, it's an invalid swapfile, and might cause kernel issue. This case covered commit 5808fecc5723 ("iomap: Fix negative assignment to unsigned sis->pages in iomap_swapfile_activate").
---
Hi,
V5 rebase this case on the latest xfstests, to match the "group info" change:
https://lore.kernel.org/fstests/[email protected]/
Thanks,
Zorro
tests/generic/640 | 77 +++++++++++++++++++++++++++++++++++++++++++ tests/generic/640.out | 2 ++ 2 files changed, 79 insertions(+) create mode 100755 tests/generic/640 create mode 100644 tests/generic/640.out
tests/generic/640 new file +77 -0
@@ -0,0 +1,77 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2021 Red Hat Inc. All Rights Reserved. +# +# FS QA Test 640 +# +# Test small swapfile which doesn't contain even a single page-aligned contiguous +# range of blocks. This case covered commit 5808fecc5723 ("iomap: Fix negative +# assignment to unsigned sis->pages in iomap_swapfile_activate"). +# +. ./common/preamble +_begin_fstest auto quick swap + +# Import common functions +. ./common/filter + +# real QA test starts here +_supported_fs generic +_require_scratch +_require_scratch_swapfile +_require_test_program mkswap +_require_test_program swapon +_require_xfs_io_command fcollapse + +make_unaligned_swapfile() +{ + local fname=$1 + local n=$((psize / bsize - 1)) + + # Make sure the swapfile doesn't contain even a single page-aligned + # contiguous range of blocks. This's necessary to cover the bug + $XFS_IO_PROG -f -t -c "pwrite 0 $(((psize + bsize) * n))" $fname >> $seqres.full 2>&1 + for((i=1; i<=n; i++));do + $XFS_IO_PROG -c "fcollapse $(((psize - bsize) * i)) $bsize" $fname + done + chmod 0600 $fname + $CHATTR_PROG +C $fname > /dev/null 2>&1 + $here/src/mkswap $fname +} + +_scratch_mkfs >> $seqres.full 2>&1 +_scratch_mount +psize=`get_page_size` +bsize=`_get_file_block_size $SCRATCH_MNT` +# Due to we need page-unaligned blocks, so blocksize < pagesize is necessary. +# If not, try to make a smaller enough block size +if [ $bsize -ge $psize ];then + _scratch_unmount + _scratch_mkfs_blocksized 1024 >> $seqres.full 2>&1 + if [ $? -ne 0 ];then + _notrun "Can't make filesystem block size < page size." + fi + _scratch_mount + bsize=`_get_file_block_size $SCRATCH_MNT` + if [ $bsize -ne 1024 ];then + _notrun "Can't force 1024-byte file block size." + fi +fi +swapfile=$SCRATCH_MNT/$seq.swapfile +make_unaligned_swapfile $swapfile +# Expect EINVAL from swapon. If not, might miss 5808fecc5723 ("iomap: Fix +# negative assignment to unsigned sis->pages in iomap_swapfile_activate") +$here/src/swapon $swapfile +# Further testing, check if swap size <= swapfile size, if swapon passed +if [ $? -eq 0 ];then + swapsize=$(awk -v fname="$swapfile" '{if ($1~fname) print $3}' /proc/swaps) + swapsize=$((swapsize * 1024)) + filesize=$(_get_filesize $swapfile) + if [ $swapsize -gt $filesize ]; then + echo "Allocated swap size($swapsize) shouldn't be greater than swapfile size($filesize)" + fi +fi +swapoff $swapfile 2>/dev/null + +# success, all done +status=0 +exit
tests/generic/640.out new file +2 -0
@@ -0,0 +1,2 @@ +QA output created by 640 +swapon: Invalid argument
Signature · 1 line
2.31.1
Quoted 92 lines · Zorro Lang
If a swapfile doesn't contain even a single page-aligned contiguous range of blocks, it's an invalid swapfile, and might cause kernel issue. This case covered commit
5808fecc5723("iomap: Fix negative assignment to unsignedsis->pagesiniomap_swapfile_activate").Reviewed-by Ritesh Harjani <[email protected]>Signed-off-by Zorro Lang <[email protected]>---
Hi,
V5 rebase this case on the latest xfstests, to match the "group info" change:
https://lore.kernel.org/fstests/[email protected]/Thanks,
Zorrotests/generic/640 | 77 +++++++++++++++++++++++++++++++++++++++++++ tests/generic/640.out | 2 ++ 2 files changed, 79 insertions(+) create mode 100755 tests/generic/640 create mode 100644 tests/generic/640.outtests/generic/640 new file +63
-0@@ -0,0 +1,77 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2021 Red Hat Inc. All Rights Reserved. +# +# FS QA Test 640 +# +# Test small swapfile which doesn't contain even a single page-aligned contiguous +# range of blocks. This case covered commit 5808fecc5723 ("iomap: Fix negative +# assignment to unsigned sis->pages in iomap_swapfile_activate"). +# +. ./common/preamble +_begin_fstest auto quick swap + +# Import common functions +. ./common/filter + +# real QA test starts here +_supported_fs generic +_require_scratch +_require_scratch_swapfile +_require_test_program mkswap +_require_test_program swapon +_require_xfs_io_command fcollapse + +make_unaligned_swapfile() +{ + local fname=$1 + local n=$((psize / bsize - 1)) + + # Make sure the swapfile doesn't contain even a single page-aligned + # contiguous range of blocks. This's necessary to cover the bug + $XFS_IO_PROG -f -t -c "pwrite 0 $(((psize + bsize) * n))" $fname >> $seqres.full 2>&1 + for((i=1; i<=n; i++));do + $XFS_IO_PROG -c "fcollapse $(((psize - bsize) * i)) $bsize" $fname + done + chmod 0600 $fname + $CHATTR_PROG +C $fname > /dev/null 2>&1 + $here/src/mkswap $fname +} + +_scratch_mkfs >> $seqres.full 2>&1 +_scratch_mount +psize=`get_page_size` +bsize=`_get_file_block_size $SCRATCH_MNT` +# Due to we need page-unaligned blocks, so blocksize < pagesize is necessary. +# If not, try to make a smaller enough block size +if [ $bsize -ge $psize ];then + _scratch_unmount + _scratch_mkfs_blocksized 1024 >> $seqres.full 2>&1 + if [ $? -ne 0 ];then + _notrun "Can't make filesystem block size < page size." + fi + _scratch_mount + bsize=`_get_file_block_size $SCRATCH_MNT` + if [ $bsize -ne 1024 ];then + _notrun "Can't force 1024-byte file block size." + fi +fi +swapfile=$SCRATCH_MNT/$seq.swapfile +make_unaligned_swapfile $swapfile +# Expect EINVAL from swapon. If not, might miss 5808fecc5723 ("iomap: Fix +# negative assignment to unsigned sis->pages in iomap_swapfile_activate") +$here/src/swapon $swapfile
tests/generic/640 line 73
Quoted 10 lines
tests/generic/640 new file +10
-0+# Further testing, check if swap size <= swapfile size, if swapon passed +if [ $? -eq 0 ];then + swapsize=$(awk -v fname="$swapfile" '{if ($1~fname) print $3}' /proc/swaps) + swapsize=$((swapsize * 1024)) + filesize=$(_get_filesize $swapfile) + if [ $swapsize -gt $filesize ]; then + echo "Allocated swap size($swapsize) shouldn't be greater than swapfile size($filesize)" + fi +fi +swapoff $swapfile 2>/dev/null
Do we actually need swapoff if swapon failed? Or are you merely being cautious?
--D
Quoted 15 lines
tests/generic/640 new file +4
-0+ +# success, all done +status=0 +exittests/generic/640.out new file +2
-0@@ -0,0 +1,2 @@ +QA output created by 640 +swapon: Invalid argumentSignature · 1 line
2.31.1
Quoted 98 lines · Darrick J. Wong
Quoted 97 lines · Zorro Lang
If a swapfile doesn't contain even a single page-aligned contiguous range of blocks, it's an invalid swapfile, and might cause kernel issue. This case covered commit
5808fecc5723("iomap: Fix negative assignment to unsignedsis->pagesiniomap_swapfile_activate").Reviewed-by Ritesh Harjani <[email protected]>Signed-off-by Zorro Lang <[email protected]>---
Hi,
V5 rebase this case on the latest xfstests, to match the "group info" change:
https://lore.kernel.org/fstests/[email protected]/Thanks,
Zorrotests/generic/640 | 77 +++++++++++++++++++++++++++++++++++++++++++ tests/generic/640.out | 2 ++ 2 files changed, 79 insertions(+) create mode 100755 tests/generic/640 create mode 100644 tests/generic/640.outtests/generic/640 new file +68
-0@@ -0,0 +1,77 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2021 Red Hat Inc. All Rights Reserved. +# +# FS QA Test 640 +# +# Test small swapfile which doesn't contain even a single page-aligned contiguous +# range of blocks. This case covered commit 5808fecc5723 ("iomap: Fix negative +# assignment to unsigned sis->pages in iomap_swapfile_activate"). +# +. ./common/preamble +_begin_fstest auto quick swap + +# Import common functions +. ./common/filter + +# real QA test starts here +_supported_fs generic +_require_scratch +_require_scratch_swapfile +_require_test_program mkswap +_require_test_program swapon +_require_xfs_io_command fcollapse + +make_unaligned_swapfile() +{ + local fname=$1 + local n=$((psize / bsize - 1)) + + # Make sure the swapfile doesn't contain even a single page-aligned + # contiguous range of blocks. This's necessary to cover the bug + $XFS_IO_PROG -f -t -c "pwrite 0 $(((psize + bsize) * n))" $fname >> $seqres.full 2>&1 + for((i=1; i<=n; i++));do + $XFS_IO_PROG -c "fcollapse $(((psize - bsize) * i)) $bsize" $fname + done + chmod 0600 $fname + $CHATTR_PROG +C $fname > /dev/null 2>&1 + $here/src/mkswap $fname +} + +_scratch_mkfs >> $seqres.full 2>&1 +_scratch_mount +psize=`get_page_size` +bsize=`_get_file_block_size $SCRATCH_MNT` +# Due to we need page-unaligned blocks, so blocksize < pagesize is necessary. +# If not, try to make a smaller enough block size +if [ $bsize -ge $psize ];then + _scratch_unmount + _scratch_mkfs_blocksized 1024 >> $seqres.full 2>&1 + if [ $? -ne 0 ];then + _notrun "Can't make filesystem block size < page size." + fi + _scratch_mount + bsize=`_get_file_block_size $SCRATCH_MNT` + if [ $bsize -ne 1024 ];then + _notrun "Can't force 1024-byte file block size." + fi +fi +swapfile=$SCRATCH_MNT/$seq.swapfile +make_unaligned_swapfile $swapfile +# Expect EINVAL from swapon. If not, might miss 5808fecc5723 ("iomap: Fix +# negative assignment to unsigned sis->pages in iomap_swapfile_activate") +$here/src/swapon $swapfile +# Further testing, check if swap size <= swapfile size, if swapon passed +if [ $? -eq 0 ];then + swapsize=$(awk -v fname="$swapfile" '{if ($1~fname) print $3}' /proc/swaps) + swapsize=$((swapsize * 1024)) + filesize=$(_get_filesize $swapfile)
Quoted 8 lines
tests/generic/640 line 73
Quoted 5 lines
tests/generic/640 new file +5
-0+ if [ $swapsize -gt $filesize ]; then + echo "Allocated swap size($swapsize) shouldn't be greater than swapfile size($filesize)" + fi +fi +swapoff $swapfile 2>/dev/nullDo we actually need swapoff if swapon failed? Or are you merely being cautious?
Maybe cautious :) I thought it won't affect this testing even if swapon failed, so moving it into above "if [ $? -eq 0 ] ... fi" or just keep it at the end all good to me. Is there anything wrong I missed ?
Thanks,
Zorro
Quoted 19 lines
--D
Quoted 15 lines
tests/generic/640 new file +4
-0+ +# success, all done +status=0 +exittests/generic/640.out new file +2
-0@@ -0,0 +1,2 @@ +QA output created by 640 +swapon: Invalid argumentSignature · 1 line
2.31.1
Darrick J. Wong
Reviewed-byQuoted 108 lines · Zorro Lang
Quoted 98 lines · Darrick J. Wong
Quoted 97 lines · Zorro Lang
If a swapfile doesn't contain even a single page-aligned contiguous range of blocks, it's an invalid swapfile, and might cause kernel issue. This case covered commit
5808fecc5723("iomap: Fix negative assignment to unsignedsis->pagesiniomap_swapfile_activate").Reviewed-by Ritesh Harjani <[email protected]>Signed-off-by Zorro Lang <[email protected]>---
Hi,
V5 rebase this case on the latest xfstests, to match the "group info" change:
https://lore.kernel.org/fstests/[email protected]/Thanks,
Zorrotests/generic/640 | 77 +++++++++++++++++++++++++++++++++++++++++++ tests/generic/640.out | 2 ++ 2 files changed, 79 insertions(+) create mode 100755 tests/generic/640 create mode 100644 tests/generic/640.outtests/generic/640 new file +68
-0@@ -0,0 +1,77 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2021 Red Hat Inc. All Rights Reserved. +# +# FS QA Test 640 +# +# Test small swapfile which doesn't contain even a single page-aligned contiguous +# range of blocks. This case covered commit 5808fecc5723 ("iomap: Fix negative +# assignment to unsigned sis->pages in iomap_swapfile_activate"). +# +. ./common/preamble +_begin_fstest auto quick swap + +# Import common functions +. ./common/filter + +# real QA test starts here +_supported_fs generic +_require_scratch +_require_scratch_swapfile +_require_test_program mkswap +_require_test_program swapon +_require_xfs_io_command fcollapse + +make_unaligned_swapfile() +{ + local fname=$1 + local n=$((psize / bsize - 1)) + + # Make sure the swapfile doesn't contain even a single page-aligned + # contiguous range of blocks. This's necessary to cover the bug + $XFS_IO_PROG -f -t -c "pwrite 0 $(((psize + bsize) * n))" $fname >> $seqres.full 2>&1 + for((i=1; i<=n; i++));do + $XFS_IO_PROG -c "fcollapse $(((psize - bsize) * i)) $bsize" $fname + done + chmod 0600 $fname + $CHATTR_PROG +C $fname > /dev/null 2>&1 + $here/src/mkswap $fname +} + +_scratch_mkfs >> $seqres.full 2>&1 +_scratch_mount +psize=`get_page_size` +bsize=`_get_file_block_size $SCRATCH_MNT` +# Due to we need page-unaligned blocks, so blocksize < pagesize is necessary. +# If not, try to make a smaller enough block size +if [ $bsize -ge $psize ];then + _scratch_unmount + _scratch_mkfs_blocksized 1024 >> $seqres.full 2>&1 + if [ $? -ne 0 ];then + _notrun "Can't make filesystem block size < page size." + fi + _scratch_mount + bsize=`_get_file_block_size $SCRATCH_MNT` + if [ $bsize -ne 1024 ];then + _notrun "Can't force 1024-byte file block size." + fi +fi +swapfile=$SCRATCH_MNT/$seq.swapfile +make_unaligned_swapfile $swapfile +# Expect EINVAL from swapon. If not, might miss 5808fecc5723 ("iomap: Fix +# negative assignment to unsigned sis->pages in iomap_swapfile_activate") +$here/src/swapon $swapfile +# Further testing, check if swap size <= swapfile size, if swapon passed +if [ $? -eq 0 ];then + swapsize=$(awk -v fname="$swapfile" '{if ($1~fname) print $3}' /proc/swaps) + swapsize=$((swapsize * 1024)) + filesize=$(_get_filesize $swapfile)Quoted 8 lines
tests/generic/640 line 73
Quoted 5 lines
tests/generic/640 new file +5
-0+ if [ $swapsize -gt $filesize ]; then + echo "Allocated swap size($swapsize) shouldn't be greater than swapfile size($filesize)" + fi +fi +swapoff $swapfile 2>/dev/nullDo we actually need swapoff if swapon failed? Or are you merely being cautious?
Quoted 3 lines
Maybe cautious :) I thought it won't affect this testing even if swapon failed, so moving it into above "if [ $? -eq 0 ] ... fi" or just keep it at the end all good to me. Is there anything wrong I missed ?
Nope, just curious.
--D
Quoted 23 lines
Thanks,
ZorroQuoted 19 lines
--D
Quoted 15 lines
tests/generic/640 new file +4
-0+ +# success, all done +status=0 +exittests/generic/640.out new file +2
-0@@ -0,0 +1,2 @@ +QA output created by 640 +swapon: Invalid argumentSignature · 1 line
2.31.1