Skip to contentSkip to conversation

generic: test swapping process pages in and out of a swapfile

PATCH In review Darrick J. Wong 2 participants 3 messages Aug 19, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 generic: test swapping process pages in and out of a swapfile

From: Darrick J. Wong <[email protected]>

This test is intended to perform minimal spot-checking that we can swap pages to a swapfile and recall them into memory without burning peoples' machines to the ground.

---
 .gitignore               |    1 
 common/cgroup2           |    5 ++
 common/config            |    1 
 common/rc                |    4 -
 src/Makefile             |    2 -
 src/usemem_and_swapoff.c |  117 ++++++++++++++++++++++++++++++++++++++
 tests/generic/728        |  141 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/generic/728.out    |    9 +++
 8 files changed, 276 insertions(+), 4 deletions(-)
 create mode 100644 src/usemem_and_swapoff.c
 create mode 100755 tests/generic/728
 create mode 100644 tests/generic/728.out
.gitignore +1 -0
index 2d72b064..23d6f37b 100644
@@ -166,6 +166,7 @@ tags
 /src/unwritten_mmap
 /src/unwritten_sync
 /src/usemem
+/src/usemem_and_swapoff
 /src/writemod
 /src/writev_on_pagefault
 /src/xfsctl
common/cgroup2 +5 -0
index 8833c9c8..df805a16 100644
@@ -17,4 +17,9 @@ _require_cgroup2()
 	fi
 }
 
+_require_memcg()
+{
+	test -e "$CGROUP2_PATH/memory.stat" || _notrun "memcg required for test"
+}
+
 /bin/true
common/config +1 -0
index 9cb4ad71..e2870be0 100644
@@ -272,6 +272,7 @@ export MKFS_REISER4_PROG=$(type -P mkfs.reiser4)
 export E2FSCK_PROG=$(type -P e2fsck)
 export TUNE2FS_PROG=$(type -P tune2fs)
 export FSCK_OVERLAY_PROG=$(type -P fsck.overlay)
+export SYSTEMD_RUN_PROG="$(type -P systemd-run)"
 
 # SELinux adds extra xattrs which can mess up our expected output.
 # So, mount with a context, and they won't be created.
common/rc +1 -3
index a07b5ac3..081ed72f 100644
@@ -2593,10 +2593,8 @@ _format_swapfile() {
 }
 
 _swapon_file() {
-	local fname="$1"
-
 	# Ignore permission complaints on filesystems that don't support perms
-	swapon "$fname" 2> >(grep -v "insecure permissions" >&2)
+	swapon "$@" 2> >(grep -v "insecure permissions" >&2)
 }
 
 # Check that the filesystem supports swapfiles
src/Makefile +1 -1
index 884bd86a..82698279 100644
@@ -31,7 +31,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	dio-invalidate-cache stat_test t_encrypted_d_revalidate \
 	attr_replace_test swapon mkswap t_attr_corruption t_open_tmpfiles \
 	fscrypt-crypt-util bulkstat_null_ocount splice-test chprojid_fail \
-	detached_mounts_propagation
+	detached_mounts_propagation usemem_and_swapoff
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
 	      btrfs_crc32c_forged_name.py
src/usemem_and_swapoff.c new file +117 -0

117 lines not on this page · raw message

tests/generic/728 new file +141 -0

141 lines not on this page · raw message

tests/generic/728.out new file +9 -0
new file mode 100644
index 00000000..26bdae14
@@ -0,0 +1,9 @@
+QA output created by 728
+Allocating 52428800 memory.
+Dirtying memory.
+Now mlocking memory.
+Allocating 52428800 memory.
+Dirtying memory.
+Now activating swapfile2.
+Now deactivating swapfile1.
+Silence is golden
#1 lore raw

src/usemem_and_swapoff.c line 61

Quoted 6 lines · Darrick J. Wong
src/usemem_and_swapoff.c new file +6 -0
+	printf("Dirtying memory.\n");
+	fflush(stdout);
+
+	/* Dirty the memory to force this program to be swapped out. */
+	for (p = pstart; p < pend; p += pagesize)
+		*p = 'X';

What I liked about dhowells' program was that it checked whether the pages brought back in from swap were the same ones that had been written to swap. As a block filesystem person, you only know the misery of having swap go behind your back to the block device. As a network filesystem person, David is acutely aware of the misery of having to remember to use page_file_index() instead of page->index in order to avoid reading a page from the wrong offset in the swap partition.

Yes, our swap code is nasty in many different ways, why do you ask?

#2 lore raw
Quoted 16 lines · Matthew Wilcox

src/usemem_and_swapoff.c line 61

Quoted 6 lines · Darrick J. Wong
src/usemem_and_swapoff.c new file +6 -0
+	printf("Dirtying memory.\n");
+	fflush(stdout);
+
+	/* Dirty the memory to force this program to be swapped out. */
+	for (p = pstart; p < pend; p += pagesize)
+		*p = 'X';

What I liked about dhowells' program was that it checked whether the pages brought back in from swap were the same ones that had been written to swap. As a block filesystem person, you only know the misery of having swap go behind your back to the block device. As a network filesystem person, David is acutely aware of the misery of having to remember to use page_file_index() instead of page->index in order to avoid reading a page from the wrong offset in the swap partition.

Quoted 1 line

Yes, our swap code is nasty in many different ways, why do you ask?

Wheeee, I'll go change it to store per-page data in each page and check it on the way back.

--D