]> git-server-git.apps.pok.os.sepia.ceph.com Git - xfstests-dev.git/commitdiff
xfs: add memory failure test for dax mode
authorShiyang Ruan <ruansy.fnst@fujitsu.com>
Wed, 3 Aug 2022 04:51:29 +0000 (04:51 +0000)
committerZorro Lang <zlang@kernel.org>
Fri, 5 Aug 2022 16:59:23 +0000 (00:59 +0800)
Make sure memory failure mechanism works when filesystem is mounted with
dax option.

Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Zorro Lang <zlang@kernel.org>
.gitignore
src/Makefile
src/t_mmap_cow_memory_failure.c [new file with mode: 0644]
tests/xfs/550 [new file with mode: 0755]
tests/xfs/550.out [new file with mode: 0644]

index c1d75b019efd45bb2fdffde19d59f3fac6f1d082..ad9f454373c5d36370d6ebf95f823fdeeec36836 100644 (file)
@@ -147,6 +147,7 @@ tags
 /src/t_holes
 /src/t_immutable
 /src/t_mmap_collision
+/src/t_mmap_cow_memory_failure
 /src/t_mmap_cow_race
 /src/t_mmap_dio
 /src/t_mmap_fallocate
index 665edcf9f17bc18c42971c7a94adc689089b096e..5f565e73cf12649445222c9dc185c937886cf10c 100644 (file)
@@ -18,7 +18,8 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \
        t_ext4_dax_journal_corruption t_ext4_dax_inline_corruption \
        t_ofd_locks t_mmap_collision mmap-write-concurrent \
        t_get_file_time t_create_short_dirs t_create_long_dirs t_enospc \
-       t_mmap_writev_overlap checkpoint_journal mmap-rw-fault allocstale
+       t_mmap_writev_overlap checkpoint_journal mmap-rw-fault allocstale \
+       t_mmap_cow_memory_failure
 
 LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
        preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
diff --git a/src/t_mmap_cow_memory_failure.c b/src/t_mmap_cow_memory_failure.c
new file mode 100644 (file)
index 0000000..bb3fd3f
--- /dev/null
@@ -0,0 +1,157 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Fujitsu Limited.  All Rights Reserved. */
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <semaphore.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+#include <sys/sem.h>
+#include <time.h>
+#include <unistd.h>
+
+sem_t *sem;
+
+void sigbus_handler(int signal)
+{
+       printf("Process is killed by signal: %d\n", signal);
+       sem_post(sem);
+}
+
+void mmap_read_file(char *filename, off_t offset, size_t size)
+{
+       int fd;
+       char *map, *dummy;
+       struct timespec ts;
+
+       fd = open(filename, O_RDWR);
+       map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, offset);
+       dummy = malloc(size);
+
+       /* make sure page fault happens */
+       memcpy(dummy, map, size);
+
+       /* ready */
+       sem_post(sem);
+
+       usleep(200000);
+
+       clock_gettime(CLOCK_REALTIME, &ts);
+       ts.tv_sec += 3;
+       /* wait for injection done */
+       sem_timedwait(sem, &ts);
+
+       free(dummy);
+       munmap(map, size);
+       close(fd);
+}
+
+void mmap_read_file_then_poison(char *filename, off_t offset, size_t size,
+               off_t poisonOffset, size_t poisonSize)
+{
+       int fd, error;
+       char *map, *dummy;
+
+       /* wait for parent preparation done */
+       sem_wait(sem);
+
+       fd = open(filename, O_RDWR);
+       map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, offset);
+       dummy = malloc(size);
+
+       /* make sure page fault happens */
+       memcpy(dummy, map, size);
+
+       printf("Inject poison...\n");
+       error = madvise(map + poisonOffset, poisonSize, MADV_HWPOISON);
+       if (error)
+               printf("madvise() has fault: %d, errno: %d\n", error, errno);
+
+       free(dummy);
+       munmap(map, size);
+       close(fd);
+}
+
+int main(int argc, char *argv[])
+{
+       char *pReadFile = NULL, *pPoisonFile = NULL;
+       size_t mmapSize, poisonSize;
+       off_t mmapOffset = 0, poisonOffset = 0;
+       long pagesize = sysconf(_SC_PAGESIZE);
+       int c;
+       pid_t pid;
+
+       if (pagesize < 1) {
+               fprintf(stderr, "sysconf(_SC_PAGESIZE): failed to get page size\n");
+               abort();
+       }
+
+       /* default mmap / poison size, in unit of System Page Size */
+       mmapSize = poisonSize = pagesize;
+
+       while ((c = getopt(argc, argv, "o::s::O::S::R:P:")) != -1) {
+               switch (c) {
+               /* mmap offset */
+               case 'o':
+                       mmapOffset = atoi(optarg) * pagesize;
+                       break;
+               /* mmap size */
+               case 's':
+                       mmapSize = atoi(optarg) * pagesize;
+                       break;
+               /* madvice offset */
+               case 'O':
+                       poisonOffset = atoi(optarg) * pagesize;
+                       break;
+               /* madvice size */
+               case 'S':
+                       poisonSize = atoi(optarg) * pagesize;
+                       break;
+               /* filename for mmap read */
+               case 'R':
+                       pReadFile = optarg;
+                       break;
+               /* filename for poison read */
+               case 'P':
+                       pPoisonFile = optarg;
+                       break;
+               default:
+                       printf("Unknown option: %c\n", c);
+                       exit(1);
+               }
+       }
+
+       if (!pReadFile || !pPoisonFile) {
+               printf("Usage: \n"
+                      "  %s [-o mmapOffset] [-s mmapSize] [-O mmapOffset] [-S mmapSize] -R readFile -P poisonFile\n"
+                      "  (offset and size are both in unit of System Page Size: %ld)\n",
+                               basename(argv[0]), pagesize);
+               exit(0);
+       }
+       if (poisonSize < mmapSize)
+               mmapSize = poisonSize;
+
+       /* fork and mmap files */
+       pid = fork();
+       if (pid == 0) {
+               /* handle SIGBUS */
+               signal(SIGBUS, sigbus_handler);
+               sem = sem_open("sync", O_CREAT, 0666, 0);
+
+               /* mread & do memory failure on poison file */
+               mmap_read_file_then_poison(pPoisonFile, mmapOffset, mmapSize,
+                               poisonOffset, poisonSize);
+
+               sem_close(sem);
+       } else {
+               sem = sem_open("sync", O_CREAT, 0666, 0);
+
+               /* mread read file, wait for child process to be killed */
+               mmap_read_file(pReadFile, mmapOffset, mmapSize);
+               sem_close(sem);
+       }
+       exit(0);
+}
diff --git a/tests/xfs/550 b/tests/xfs/550
new file mode 100755 (executable)
index 0000000..87ae411
--- /dev/null
@@ -0,0 +1,50 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2022 Fujitsu Limited.  All Rights Reserved.
+#
+# FS QA Test No. 550
+#
+# Test memory failure mechanism when dax enabled
+#
+. ./common/preamble
+_begin_fstest auto quick dax
+
+# Import common functions.
+. ./common/filter
+. ./common/reflink
+
+# real QA test starts here
+_require_check_dmesg
+_require_scratch_reflink
+_require_cp_reflink
+_require_xfs_scratch_rmapbt
+_require_scratch_dax_mountopt "dax"
+_require_test_program "t_mmap_cow_memory_failure"
+
+echo "Format and mount"
+_scratch_mkfs > $seqres.full 2>&1
+_scratch_mount "-o dax" >> $seqres.full 2>&1
+
+testdir=$SCRATCH_MNT/test-$seq
+mkdir $testdir
+
+echo "Create the original files"
+filesize=65536
+_pwrite_byte 0x61 0 $filesize $testdir/testfile >> $seqres.full
+_scratch_cycle_mount "dax"
+
+echo "Inject memory failure (1 page)"
+# create two processes:
+#  process1: mread 1 page to cause page fault, and wait
+#  process2: mread 1 page to cause page fault, then inject poison on this page
+$here/src/t_mmap_cow_memory_failure -s1 -S1 -R $testdir/testfile -P $testdir/testfile
+
+echo "Inject memory failure (2 pages)"
+$here/src/t_mmap_cow_memory_failure -s2 -S2 -R $testdir/testfile -P $testdir/testfile
+
+_check_dmesg_for "Sending SIGBUS to t_mmap_cow_memo" || echo "Memory failure didn't kill the process"
+_check_dmesg_for "recovery action for dax page: Recovered" || echo "Failured page didn't recovered"
+
+# success, all done
+status=0
+exit
diff --git a/tests/xfs/550.out b/tests/xfs/550.out
new file mode 100644 (file)
index 0000000..80e3223
--- /dev/null
@@ -0,0 +1,9 @@
+QA output created by 550
+Format and mount
+Create the original files
+Inject memory failure (1 page)
+Inject poison...
+Process is killed by signal: 7
+Inject memory failure (2 pages)
+Inject poison...
+Process is killed by signal: 7