diff mbox series

generic: add swapfile maxpages regression test

Message ID 20210819014326.GC12597@magnolia (mailing list archive)
State New, archived
Headers show
Series generic: add swapfile maxpages regression test | expand

Commit Message

Darrick J. Wong Aug. 19, 2021, 1:43 a.m. UTC
From: Darrick J. Wong <djwong@kernel.org>

Add regression test for "mm/swap: consider max pages in
iomap_swapfile_add_extent".

Cc: Gang Deng <gavin.dg@linux.alibaba.com>
Cc: Xu Yu <xuyu@linux.alibaba.com>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
---
 tests/generic/727     |   62 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/generic/727.out |    2 ++
 2 files changed, 64 insertions(+)
 create mode 100755 tests/generic/727
 create mode 100644 tests/generic/727.out

Comments

Zorro Lang Aug. 19, 2021, 5:14 a.m. UTC | #1
On Wed, Aug 18, 2021 at 06:43:26PM -0700, Darrick J. Wong wrote:
> From: Darrick J. Wong <djwong@kernel.org>
> 
> Add regression test for "mm/swap: consider max pages in
> iomap_swapfile_add_extent".
> 
> Cc: Gang Deng <gavin.dg@linux.alibaba.com>
> Cc: Xu Yu <xuyu@linux.alibaba.com>
> Signed-off-by: Darrick J. Wong <djwong@kernel.org>
> ---

The code logic looks good to me. And [1][2]... so I think this test
is good. But of course, wait for more review points from cc list.

Reviewed-by: Zorro Lang <zlang@redhat.com>

[1]
Test passed on old kernel without this regression (xfs fails):

# ./check generic/727                                                                                                                    
FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 xx-xxxx-xx 4.18.0-xxx.el8.x86_64+debug #1 SMP Wed Jul 14 12:35:49 EDT 2021
MKFS_OPTIONS  -- /dev/mapper/rhel-xx-xxxx-xx-xfscratch
MOUNT_OPTIONS -- -o acl,user_xattr -o context=system_u:object_r:root_t:s0 /dev/mapper/rhel-xx-xxxx-xx-xfscratch /mnt/scratch

generic/727      15s
Ran: generic/727
Passed all 1 tests

[2]
Reproduced on new kernel with this regression:

# ./check generic/727
FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 xxx-xxxx-xx 5.14.0-rc4-xfs #14 SMP Thu Aug 12 00:56:07 CST 2021
MKFS_OPTIONS  -- /dev/mapper/testvg-scratchdev
MOUNT_OPTIONS -- -o acl,user_xattr -o context=system_u:object_r:root_t:s0 /dev/mapper/testvg-scratchdev /mnt/scratch

generic/727     - output mismatch (see /root/git/xfstests-dev/results//generic/727.out.bad)
    --- tests/generic/727.out   2021-08-19 11:20:14.677794743 +0800
    +++ /root/git/xfstests-dev/results//generic/727.out.bad     2021-08-19 11:21:46.654450307 +0800
    @@ -1,2 +1,3 @@
     QA output created by 727
    +swapon added 2044 pages, expected 1020
     Silence is golden
    ...
    (Run 'diff -u /root/git/xfstests-dev/tests/generic/727.out /root/git/xfstests-dev/results//generic/727.out.bad'  to see the entire diff)
Ran: generic/727
Failures: generic/727
Failed 1 of 1 tests

>  tests/generic/727     |   62 +++++++++++++++++++++++++++++++++++++++++++++++++
>  tests/generic/727.out |    2 ++
>  2 files changed, 64 insertions(+)
>  create mode 100755 tests/generic/727
>  create mode 100644 tests/generic/727.out
> 
> diff --git a/tests/generic/727 b/tests/generic/727
> new file mode 100755
> index 00000000..a546ad51
> --- /dev/null
> +++ b/tests/generic/727
> @@ -0,0 +1,62 @@
> +#! /bin/bash
> +# SPDX-License-Identifier: GPL-2.0
> +# Copyright (c) 2021 Oracle.  All Rights Reserved.
> +#
> +# FS QA Test 727
> +#
> +# Regression test for "mm/swap: consider max pages in iomap_swapfile_add_extent"
> +
> +# Xu Yu found that the iomap swapfile activation code failed to constrain
> +# itself to activating however many swap pages that the mm asked us for.  This
> +# is an deviation in behavior from the classic swapfile code.  It also leads to
> +# kernel memory corruption if the swapfile is cleverly constructed.
> +#
> +. ./common/preamble
> +_begin_fstest auto swap
> +
> +# Override the default cleanup function.
> +_cleanup()
> +{
> +	cd /
> +	rm -f $tmp.*
> +	test -n "$swapfile" && swapoff $swapfile &> /dev/null
> +}
> +
> +# real QA test starts here
> +_supported_fs generic
> +_require_scratch_swapfile
> +
> +_scratch_mkfs >> $seqres.full
> +_scratch_mount >> $seqres.full
> +
> +# Assuming we're not borrowing a FAT16 partition from Windows 3.1, we need an
> +# unlikely enough name that we can grep /proc/swaps for this.
> +swapfile=$SCRATCH_MNT/386spart.par
> +_format_swapfile $swapfile 1m >> $seqres.full
> +
> +swapfile_pages() {
> +	local swapfile="$1"
> +
> +	grep "$swapfile" /proc/swaps | awk '{print $3}'
> +}
> +
> +_swapon_file $swapfile
> +before_pages=$(swapfile_pages "$swapfile")
> +swapoff $swapfile
> +
> +# Extend the length of the swapfile but do not rewrite the header.
> +# The subsequent swapon should set up 1MB worth of pages, not 2MB.
> +$XFS_IO_PROG -f -c 'pwrite 1m 1m' $swapfile >> $seqres.full
> +
> +_swapon_file $swapfile
> +after_pages=$(swapfile_pages "$swapfile")
> +swapoff $swapfile
> +
> +# Both swapon attempts should have found the same number of pages.
> +test "$before_pages" -eq "$after_pages" || \
> +	echo "swapon added $after_pages pages, expected $before_pages"
> +
> +# success, all done
> +echo Silence is golden
> +status=0
> +exit
> diff --git a/tests/generic/727.out b/tests/generic/727.out
> new file mode 100644
> index 00000000..2de2b4b2
> --- /dev/null
> +++ b/tests/generic/727.out
> @@ -0,0 +1,2 @@
> +QA output created by 727
> +Silence is golden
>
Christoph Hellwig Aug. 19, 2021, 8:22 a.m. UTC | #2
Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>
diff mbox series

Patch

diff --git a/tests/generic/727 b/tests/generic/727
new file mode 100755
index 00000000..a546ad51
--- /dev/null
+++ b/tests/generic/727
@@ -0,0 +1,62 @@ 
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2021 Oracle.  All Rights Reserved.
+#
+# FS QA Test 727
+#
+# Regression test for "mm/swap: consider max pages in iomap_swapfile_add_extent"
+
+# Xu Yu found that the iomap swapfile activation code failed to constrain
+# itself to activating however many swap pages that the mm asked us for.  This
+# is an deviation in behavior from the classic swapfile code.  It also leads to
+# kernel memory corruption if the swapfile is cleverly constructed.
+#
+. ./common/preamble
+_begin_fstest auto swap
+
+# Override the default cleanup function.
+_cleanup()
+{
+	cd /
+	rm -f $tmp.*
+	test -n "$swapfile" && swapoff $swapfile &> /dev/null
+}
+
+# real QA test starts here
+_supported_fs generic
+_require_scratch_swapfile
+
+_scratch_mkfs >> $seqres.full
+_scratch_mount >> $seqres.full
+
+# Assuming we're not borrowing a FAT16 partition from Windows 3.1, we need an
+# unlikely enough name that we can grep /proc/swaps for this.
+swapfile=$SCRATCH_MNT/386spart.par
+_format_swapfile $swapfile 1m >> $seqres.full
+
+swapfile_pages() {
+	local swapfile="$1"
+
+	grep "$swapfile" /proc/swaps | awk '{print $3}'
+}
+
+_swapon_file $swapfile
+before_pages=$(swapfile_pages "$swapfile")
+swapoff $swapfile
+
+# Extend the length of the swapfile but do not rewrite the header.
+# The subsequent swapon should set up 1MB worth of pages, not 2MB.
+$XFS_IO_PROG -f -c 'pwrite 1m 1m' $swapfile >> $seqres.full
+
+_swapon_file $swapfile
+after_pages=$(swapfile_pages "$swapfile")
+swapoff $swapfile
+
+# Both swapon attempts should have found the same number of pages.
+test "$before_pages" -eq "$after_pages" || \
+	echo "swapon added $after_pages pages, expected $before_pages"
+
+# success, all done
+echo Silence is golden
+status=0
+exit
diff --git a/tests/generic/727.out b/tests/generic/727.out
new file mode 100644
index 00000000..2de2b4b2
--- /dev/null
+++ b/tests/generic/727.out
@@ -0,0 +1,2 @@ 
+QA output created by 727
+Silence is golden