From: Filipe Manana Date: Thu, 29 Aug 2024 23:10:21 +0000 (+0100) Subject: generic: test concurrent direct IO writes and fsync using same fd X-Git-Tag: xfs-zoned-2024-21-07~104 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=136c5099d07eae5cd02ae9bfc992e44f3b21ffe7;p=users%2Fhch%2Fxfstests-dev.git generic: test concurrent direct IO writes and fsync using same fd Test that a program that has 2 threads using the same file descriptor and concurrently doing direct IO writes and fsync doesn't trigger any crash or deadlock. This is motivated by a bug found in btrfs fixed by the following patch: "btrfs: fix race between direct IO write and fsync when using same fd" Signed-off-by: Filipe Manana Reviewed-by: Anand Jain Reviewed-by: Zorro Lang Signed-off-by: Zorro Lang --- diff --git a/.gitignore b/.gitignore index 94f6b5640..f16173d90 100644 --- a/.gitignore +++ b/.gitignore @@ -76,6 +76,7 @@ tags /src/dio-buf-fault /src/dio-interleaved /src/dio-invalidate-cache +/src/dio-write-fsync-same-fd /src/dirhash_collide /src/dirperf /src/dirstress diff --git a/src/Makefile b/src/Makefile index 52299b4c0..3097c29ef 100644 --- a/src/Makefile +++ b/src/Makefile @@ -20,7 +20,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ 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_cow_memory_failure fake-dump-rootino dio-buf-fault rewinddir-test \ - readdir-while-renames dio-append-buf-fault + readdir-while-renames dio-append-buf-fault dio-write-fsync-same-fd 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/dio-write-fsync-same-fd.c b/src/dio-write-fsync-same-fd.c new file mode 100644 index 000000000..79472a9e2 --- /dev/null +++ b/src/dio-write-fsync-same-fd.c @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2024 SUSE Linux Products GmbH. All Rights Reserved. + */ + +/* + * Test two threads working with the same file descriptor, one doing direct IO + * writes into the file and the other just doing fsync calls. We want to verify + * that there are no crashes or deadlocks. + * + * This program never finishes, it starts two infinite loops to write and fsync + * the file. It's meant to be called with the 'timeout' program from coreutils. + */ + +/* Get the O_DIRECT definition. */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +static int fd; + +static ssize_t do_write(int fd, const void *buf, size_t count, off_t offset) +{ + while (count > 0) { + ssize_t ret; + + ret = pwrite(fd, buf, count, offset); + if (ret < 0) { + if (errno == EINTR) + continue; + return ret; + } + count -= ret; + buf += ret; + } + return 0; +} + +static void *fsync_loop(void *arg) +{ + while (1) { + int ret; + + ret = fsync(fd); + if (ret != 0) { + perror("Fsync failed"); + exit(6); + } + } +} + +int main(int argc, char *argv[]) +{ + long pagesize; + void *write_buf; + pthread_t fsyncer; + int ret; + + if (argc != 2) { + fprintf(stderr, "Use: %s \n", argv[0]); + return 1; + } + + fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_DIRECT, 0666); + if (fd == -1) { + perror("Failed to open/create file"); + return 1; + } + + pagesize = sysconf(_SC_PAGE_SIZE); + if (pagesize == -1) { + perror("Failed to get page size"); + return 2; + } + + ret = posix_memalign(&write_buf, pagesize, pagesize); + if (ret) { + perror("Failed to allocate buffer"); + return 3; + } + + ret = pthread_create(&fsyncer, NULL, fsync_loop, NULL); + if (ret != 0) { + fprintf(stderr, "Failed to create writer thread: %d\n", ret); + return 4; + } + + while (1) { + ret = do_write(fd, write_buf, pagesize, 0); + if (ret != 0) { + perror("Write failed"); + exit(5); + } + } + + return 0; +} diff --git a/tests/generic/364 b/tests/generic/364 new file mode 100755 index 000000000..340295973 --- /dev/null +++ b/tests/generic/364 @@ -0,0 +1,31 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2024 SUSE Linux Products GmbH. All Rights Reserved. +# +# FS QA Test No. 364 +# +# Test that a program that has 2 threads using the same file descriptor and +# concurrently doing direct IO writes and fsync doesn't trigger any crash or +# deadlock. +# +. ./common/preamble +_begin_fstest auto quick + +_require_test +_require_odirect +_require_test_program dio-write-fsync-same-fd +_require_command "$TIMEOUT_PROG" timeout + +# Triggers very frequently with kernel config CONFIG_BTRFS_ASSERT=y. +[ $FSTYP == "btrfs" ] && \ + _fixed_by_kernel_commit xxxxxxxxxxxx \ + "btrfs: fix race between direct IO write and fsync when using same fd" + +# On error the test program writes messages to stderr, causing a golden output +# mismatch and making the test fail. +$TIMEOUT_PROG 10s $here/src/dio-write-fsync-same-fd $TEST_DIR/dio-write-fsync-same-fd + +# success, all done +echo "Silence is golden" +status=0 +exit diff --git a/tests/generic/364.out b/tests/generic/364.out new file mode 100644 index 000000000..6bc54ae69 --- /dev/null +++ b/tests/generic/364.out @@ -0,0 +1,2 @@ +QA output created by 364 +Silence is golden