From nobody Thu Oct 03 11:44:17 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XK8wK3W2yz5YCWh; Thu, 03 Oct 2024 11:44:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XK8wK3Bbfz4LPn; Thu, 3 Oct 2024 11:44:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727955857; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HiaFu9tVV8KAVeREu2cRjwOkEq5ExERZuthE7WFg0BI=; b=mbx9sz7R7qVOcDwr3+vDeQQLgQlD6MZL5mjd8zt9GroCDuOXPX0jxJ5uXcg2Td6PnTojXU ssrSzr0sXJv4Wzb7PyudJZ/rR6ZdJSkmm2xCLJHhZaUTGcTzf5Iz+YR7Qoi2TPRMROPq9F KuRrueHOeoNwK5jF1lLkSkWi3qkpZwRthEi4Vy2U2MSSDO4wZ+6+hFKE2xRRsLhGcWRm8t dOvI7r+cQphOYKAEobHiitjgdr9vUmgZcqMOlgGrDX2UKCyhqyYtuHXlYd88bifWgg4oX3 HwkG9GshE7snISX/TjWuM6AsPfi6mELiFJOi2Rz9NF81PbI3uCMs/NYCKbfrYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727955857; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HiaFu9tVV8KAVeREu2cRjwOkEq5ExERZuthE7WFg0BI=; b=Gf3A2s/pYL4pAqVzs1RWtuiF96HsgOL2eDz+u/RSO7QKXkBil0EtCC2eKUkGLTII02em9G 9SwMMfRHjurSZRyaZJ755aTyFnQYJSkFEJFd1HAYv7EcO4DkBJiNRNwG9DE3UH4QQJEDRZ PzTXKnvFSxG7BwnzcLjSZLHGO/+mRcdWAE5hQXQJU5yVH4uwN+lw2uzoB2zR2x547quzU9 FxPrNf2nAOH60Lm6gg5Un9CtGvt2TzWPqZe0gdvkl6I5fVgOTSgg4n6Kd3uC6EXB7f6K++ eCAqqAWKT7fpdj97wQSgOy8X5EwrmWwKdXMmhl1bAO3iscHl4LXcLnBfo111lA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1727955857; a=rsa-sha256; cv=none; b=t3l9KzK18CusWnTtOsOIaUXxacxpR0fwRc7AI7bdX5904w+96lffPb7rZK5AzYdAt0dmOo WFfvxwl5pAzyEJgL0n7muFwX/IcXi9DVnGvNqTlNUwXMqOsLljMDL5AcXhN6byMZLowxlZ MX+k3QQXxfaP72kWePIC4t0k1dLyBv63FU0DjxeIVVo0OeZ3Tx8yI1PT2jdLJZAaw3cvQ3 PvrGiX5P9x+FwWck9LrQuVrqcBBL6kpx4Rm1cgDGZT4R8OuGyrd/M2g30dIqeJ+lvJ7ykr ZeSn8vbD3kt4IGJ5JNevqqaJFrp8KovYKcmIOSxhCAk7xVGA+iiRfyFKatvAbQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XK8wK2hSczpgy; Thu, 3 Oct 2024 11:44:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 493BiHix077945; Thu, 3 Oct 2024 11:44:17 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 493BiHuN077942; Thu, 3 Oct 2024 11:44:17 GMT (envelope-from git) Date: Thu, 3 Oct 2024 11:44:17 GMT Message-Id: <202410031144.493BiHuN077942@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Peter Holm Subject: git: 94a294e59d17 - main - stress2: Added test scenarios for bug report List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pho X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 94a294e59d1772f316fef613b83e92f37294954c Auto-Submitted: auto-generated The branch main has been updated by pho: URL: https://cgit.FreeBSD.org/src/commit/?id=94a294e59d1772f316fef613b83e92f37294954c commit 94a294e59d1772f316fef613b83e92f37294954c Author: Peter Holm AuthorDate: 2024-10-03 11:41:33 +0000 Commit: Peter Holm CommitDate: 2024-10-03 11:41:33 +0000 stress2: Added test scenarios for bug report --- tools/test/stress2/misc/mmap44.sh | 2 +- tools/test/stress2/misc/mmap45.sh | 230 ++++++++++++++++++++++++++++++ tools/test/stress2/misc/mmap46.sh | 233 ++++++++++++++++++++++++++++++ tools/test/stress2/misc/mmap47.sh | 237 +++++++++++++++++++++++++++++++ tools/test/stress2/misc/mmap48.sh | 289 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 990 insertions(+), 1 deletion(-) diff --git a/tools/test/stress2/misc/mmap44.sh b/tools/test/stress2/misc/mmap44.sh index 5865491e1dea..f5999ac62536 100755 --- a/tools/test/stress2/misc/mmap44.sh +++ b/tools/test/stress2/misc/mmap44.sh @@ -219,7 +219,7 @@ mount /dev/md$mdstart $mntpoint mp2=${mntpoint}2 mkdir -p $mp2 mount | grep -q "on $mp2 " && umount -f $mp2 -mount -t nfs 127.0.0.1:$mntpoint $mp2; s=$? +mount -t nfs -o retrycnt=3 127.0.0.1:$mntpoint $mp2 || exit 1 sleep .2 here=`pwd` diff --git a/tools/test/stress2/misc/mmap45.sh b/tools/test/stress2/misc/mmap45.sh new file mode 100755 index 000000000000..59450b41bb5e --- /dev/null +++ b/tools/test/stress2/misc/mmap45.sh @@ -0,0 +1,230 @@ +#!/bin/sh + +# +# Copyright (c) 2024 Peter Holm +# +# SPDX-License-Identifier: BSD-2-Clause +# + +# Demonstrate issue described in: +# [Bug 276002] nfscl: data corruption using both copy_file_range and mmap'd I/O + +# Issue seen: +# +# 8994c8994 +# < 0431020 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f +# --- +# > 0431020 10 11 ee 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f +# 256 -rw------- 1 root wheel 262144 Feb 28 19:44 file +# 256 -rw------- 1 root wheel 262144 Feb 28 19:43 file.orig +# 19:44:34, elapsed 0 days, 00:13.59 +# Failed with exit code 2 after 13 loops. + +[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 +. ../default.cfg +set -u +prog=$(basename "$0" .sh) +log=/tmp/$prog.log +serial=/tmp/$prog.serial +grep -q $mntpoint /etc/exports || + { echo "$mntpoint missing from /etc/exports"; exit 0; } +rpcinfo 2>/dev/null | grep -q mountd || exit 0 + +cat > /tmp/$prog.c < +#include + +#include +#include +#include +#include +#include +#include +#include + +static off_t siz; +static pthread_mutex_t write_mutex; +static int fd, go; +static char *cp; + +static void * +memread(void *arg __unused) +{ + int i; + char c; + + while (go == 1) { + i = arc4random() % siz; + c = cp[i]; + if (c != 0x77) /* No unused vars here */ + usleep(arc4random() % 400); + } + return (0); +} + +static void * +memwrite(void *arg __unused) +{ + int i; + char c; + + while (go == 1) { + i = arc4random() % siz; + pthread_mutex_lock(&write_mutex); + c = cp[i]; + cp[i] = 0xee; /* This value seems to linger with NFS */ + cp[i] = c; + pthread_mutex_unlock(&write_mutex); + usleep(arc4random() % 400); + } + return (0); +} + +static void * +wr(void *arg __unused) +{ + off_t pos; + int r, s; + char buf[1024]; + + while (go == 1) { + s = arc4random() % sizeof(buf) + 1; + pos = arc4random() % (siz - s); + pthread_mutex_lock(&write_mutex); + if (lseek(fd, pos, SEEK_SET) == -1) + err(1, "lseek(%d)", (int)pos); + if ((r = read(fd, buf, s)) != s) { + fprintf(stderr, "r = %d, s = %d, pos = %d\n", r, s, (int)pos); + err(1, "read():2"); + } + if (lseek(fd, pos, SEEK_SET) == -1) + err(1, "lseek(%d)", (int)pos); + if (write(fd, buf, s) != s) + err(1, "write()"); + pthread_mutex_unlock(&write_mutex); + usleep(arc4random() % 400); + } + return (0); +} + +static void * +s1(void *arg __unused) +{ + + while (go == 1) { + if (fdatasync(fd) == -1) + err(1, "fdatasync()"); + usleep(arc4random() % 1000); + } + return (0); +} + +static void * +s2(void *arg __unused) +{ + + while (go == 1) { + if (fsync(fd) == -1) + err(1, "fdatasync()"); + usleep(arc4random() % 1000); + } + return (0); +} + +int +main(int argc, char *argv[]) +{ + struct stat st; + pthread_t tp[10]; + int e, i; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + if ((fd = open(argv[1], O_RDWR)) == -1) + err(1, "open(%s)", argv[1]); + if (fstat(fd, &st) == -1) + err(1, "stat(%s)", argv[1]); + siz = st.st_size; + cp = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (cp == MAP_FAILED) + err(1, "mmap()"); + + go = 1; + pthread_mutex_init(&write_mutex, NULL); + if ((e = pthread_create(&tp[0], NULL, memwrite, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[1], NULL, memwrite, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[2], NULL, memread, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[3], NULL, memread, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[4], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[5], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[6], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[7], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[8], NULL, s1, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[9], NULL, s2, NULL)) != 0) + errc(1, e, "pthread_create"); + + sleep(60); + go = 0; + for (i = 0; i < (int)(sizeof(tp) / sizeof(tp[0])); i++) + pthread_join(tp[i], NULL); + if (munmap(cp, siz) == -1) + err(1, "munmap()"); + close(fd); +} +EOF +mycc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c -lpthread || exit 1 + +mycc -o $serial -Wall -Wextra -O2 ../tools/serial.c || exit 1 +mount | grep -q "on $mntpoint " && umount -f $mntpoint +mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart +mdconfig -s 5g -u $mdstart +newfs -n $newfs_flags /dev/md$mdstart > /dev/null +mount /dev/md$mdstart $mntpoint + +mp2=${mntpoint}2 +mkdir -p $mp2 +mount | grep -q "on $mp2 " && umount -f $mp2 +mount -t nfs -o retrycnt=3 127.0.0.1:$mntpoint $mp2 || exit 1 +sleep .2 + +here=`pwd` +cd $mp2 +$here/../testcases/swap/swap -t 5m -i 20 > /dev/null & +sleep 2 + +size=262144 +$serial file $size +cp file file.orig + +s=0 +/tmp/$prog file || s=1 + +while pgrep -q swap; do pkill swap; done +wait +if ! cmp -s file.orig file; then + od -t x1 file.orig > /var/tmp/$prog.file1 + od -t x1 file > /var/tmp/$prog.file2 + diff /var/tmp/$prog.file1 /var/tmp/$prog.file2 > $log + head -20 $log + rm /var/tmp/$prog.file1 /var/tmp/$prog.file2 + ls -ls file.orig file + s=2 +fi + +cd $here +umount $mp2 +umount $mntpoint +mdconfig -d -u $mdstart +rm -f $serial /tmp/$prog /tmp/$prog.c $log +exit $s diff --git a/tools/test/stress2/misc/mmap46.sh b/tools/test/stress2/misc/mmap46.sh new file mode 100755 index 000000000000..a739f36f3f9e --- /dev/null +++ b/tools/test/stress2/misc/mmap46.sh @@ -0,0 +1,233 @@ +#!/bin/sh + +# +# Copyright (c) 2024 Peter Holm +# +# SPDX-License-Identifier: BSD-2-Clause +# + +# Demonstrate issue described in: +# [Bug 276002] nfscl: data corruption using both copy_file_range and mmap'd I/O + +# Issue seen: +# +# 8994c8994 +# < 0431020 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f +# --- +# > 0431020 10 11 ee 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f +# 256 -rw------- 1 root wheel 262144 Feb 28 19:44 file +# 256 -rw------- 1 root wheel 262144 Feb 28 19:43 file.orig +# 19:44:34, elapsed 0 days, 00:13.59 +# Failed with exit code 2 after 13 loops. + +[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 +. ../default.cfg +set -u +prog=$(basename "$0" .sh) +log=/tmp/$prog.log +serial=/tmp/$prog.serial +grep -q $mntpoint /etc/exports || + { echo "$mntpoint missing from /etc/exports"; exit 0; } +rpcinfo 2>/dev/null | grep -q mountd || exit 0 + +cat > /tmp/$prog.c < +#include + +#include +#include +#include +#include +#include +#include +#include + +static off_t siz; +static pthread_mutex_t write_mutex; +static int fd, go; +static char *cp; + +static void * +memread(void *arg __unused) +{ + int i; + char c; + + while (go == -1) + usleep(50); + while (go == 1) { + i = arc4random() % siz; + c = cp[i]; + if (c != 0x77) /* No unused vars here */ + usleep(arc4random() % 200); + } + return (0); +} + +static void * +memwrite(void *arg __unused) +{ + int i; + char c; + + while (go == -1) + usleep(50); + while (go == 1) { + i = arc4random() % siz; + pthread_mutex_lock(&write_mutex); + c = cp[i]; + cp[i] = 0xee; /* This value seems to linger with NFS */ + cp[i] = c; + pthread_mutex_unlock(&write_mutex); + usleep(arc4random() % 200); + } + return (0); +} + +static void * +wr(void *arg __unused) +{ + off_t pos; + int r, s; + char buf[1024]; + + while (go == -1) + usleep(50); + while (go == 1) { + s = arc4random() % sizeof(buf) + 1; + pos = arc4random() % (siz - s); + pthread_mutex_lock(&write_mutex); + if (lseek(fd, pos, SEEK_SET) == -1) + err(1, "lseek(%d)", (int)pos); + if ((r = read(fd, buf, s)) != s) { + fprintf(stderr, "r = %d, s = %d, pos = %d\n", r, s, (int)pos); + err(1, "read():2"); + } + if (lseek(fd, pos, SEEK_SET) == -1) + err(1, "lseek(%d)", (int)pos); + if (write(fd, buf, s) != s) + err(1, "write()"); + pthread_mutex_unlock(&write_mutex); + usleep(arc4random() % 200); + } + return (0); +} + +static void * +tr(void *arg __unused) +{ + while (go == -1) + usleep(50); + while (go == 1) { + if (ftruncate(fd, siz) == -1) /* No size change */ + err(1, "truncate)"); + usleep(arc4random() % 1000); + } + return (0); +} + +int +main(int argc, char *argv[]) +{ + struct stat st; + pthread_t tp[13]; + int e, i; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + if ((fd = open(argv[1], O_RDWR)) == -1) + err(1, "open(%s)", argv[1]); + if (fstat(fd, &st) == -1) + err(1, "stat(%s)", argv[1]); + siz = st.st_size; + cp = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (cp == MAP_FAILED) + err(1, "mmap()"); + + go = -1; + pthread_mutex_init(&write_mutex, NULL); + if ((e = pthread_create(&tp[0], NULL, memwrite, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[1], NULL, memwrite, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[2], NULL, memread, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[3], NULL, memread, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[4], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[5], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[6], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[7], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[8], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[9], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[10], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[11], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + if ((e = pthread_create(&tp[12], NULL, tr, NULL)) != 0) + errc(1, e, "pthread_create"); + + sleep(1); + go = 1; + sleep(60); + go = 0; + for (i = 0; i < (int)(sizeof(tp) / sizeof(tp[0])); i++) + pthread_join(tp[i], NULL); + if (munmap(cp, siz) == -1) + err(1, "munmap()"); + close(fd); +} +EOF +mycc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c -lpthread || exit 1 + +mycc -o $serial -Wall -Wextra -O2 ../tools/serial.c || exit 1 +mount | grep -q "on $mntpoint " && umount -f $mntpoint +mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart +mdconfig -s 5g -u $mdstart +newfs -n $newfs_flags /dev/md$mdstart > /dev/null +mount /dev/md$mdstart $mntpoint + +mp2=${mntpoint}2 +mkdir -p $mp2 +mount | grep -q "on $mp2 " && umount -f $mp2 +mount -t nfs -o retrycnt=3 127.0.0.1:$mntpoint $mp2 || exit 1 +sleep .2 + +here=`pwd` +cd $mp2 +$here/../testcases/swap/swap -t 5m -i 20 > /dev/null & +sleep 2 + +size=262144 +$serial file $size +cp file file.orig + +s=0 +/tmp/$prog file || s=1 + +while pgrep -q swap; do pkill swap; done +wait +if ! cmp -s file.orig file; then + od -t x1 file.orig > /var/tmp/$prog.file1 + od -t x1 file > /var/tmp/$prog.file2 + diff /var/tmp/$prog.file1 /var/tmp/$prog.file2 > $log + head -20 $log + rm /var/tmp/$prog.file1 /var/tmp/$prog.file2 + ls -ls file.orig file + s=2 +fi + +cd $here +umount $mp2 +umount $mntpoint +mdconfig -d -u $mdstart +rm -f $serial /tmp/$prog /tmp/$prog.c $log +exit $s diff --git a/tools/test/stress2/misc/mmap47.sh b/tools/test/stress2/misc/mmap47.sh new file mode 100755 index 000000000000..43778a193202 --- /dev/null +++ b/tools/test/stress2/misc/mmap47.sh @@ -0,0 +1,237 @@ +#!/bin/sh + +# +# Copyright (c) 2024 Peter Holm +# +# SPDX-License-Identifier: BSD-2-Clause +# + +# Demonstrate issue described in: +# [Bug 276002] nfscl: data corruption using both copy_file_range and mmap'd I/O + +# This version only uses mapped read/write, read(2)/write(2) and ftruncate(2) + +# Issue seen: + +# 19:50:53 Start test of mmap47.sh +# 19:51:56, elapsed 0 days, 00:01.03 +# 19:53:01, elapsed 0 days, 00:02.08 +# 19:54:06, elapsed 0 days, 00:03.13 +# 19:55:40, elapsed 0 days, 00:04.47 +# 617c617 +# < 0023200 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f +# --- +# > 0023200 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e ee +# 256 -rw------- 1 root wheel 262144 Mar 1 19:56 file +# 256 -rw------- 1 root wheel 262144 Mar 1 19:55 file.orig +# 19:56:44, elapsed 0 days, 00:05.51 +# Failed with exit code 2 after 5 loops of mmap47.sh. +# 19:56 /usr/src/tools/test/stress2/misc $ + +[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 +. ../default.cfg +set -u +prog=$(basename "$0" .sh) +log=/tmp/$prog.log +serial=/tmp/$prog.serial +grep -q $mntpoint /etc/exports || + { echo "$mntpoint missing from /etc/exports"; exit 0; } +rpcinfo 2>/dev/null | grep -q mountd || exit 0 + +cat > /tmp/$prog.c < +#include + +#include +#include +#include +#include +#include +#include +#include + +static off_t siz; +static pthread_mutex_t write_mutex; +static int fd, go; +static char *cp; + +static void * +memread(void *arg __unused) +{ + int i; + char c; + + while (go == -1) + usleep(50); + while (go == 1) { + i = arc4random() % siz; + c = cp[i]; + if (c != 0x77) /* No unused vars here */ + usleep(arc4random() % 200); + } + return (0); +} + +static void * +memwrite(void *arg __unused) +{ + int i; + char c; + + while (go == -1) + usleep(50); + while (go == 1) { + i = arc4random() % siz; + pthread_mutex_lock(&write_mutex); + c = cp[i]; + cp[i] = 0xee; /* This value seems to linger with NFS */ + cp[i] = c; + pthread_mutex_unlock(&write_mutex); + usleep(arc4random() % 200); + } + return (0); +} + +static void * +wr(void *arg __unused) +{ + off_t pos; + int r, s; + char buf[1024]; + + while (go == -1) + usleep(50); + while (go == 1) { + s = arc4random() % sizeof(buf) + 1; + pos = arc4random() % (siz - s); + pthread_mutex_lock(&write_mutex); + if (lseek(fd, pos, SEEK_SET) == -1) + err(1, "lseek(%d)", (int)pos); + if ((r = read(fd, buf, s)) != s) { + fprintf(stderr, "r = %d, s = %d, pos = %d\n", r, s, (int)pos); + err(1, "read():2"); + } + if (lseek(fd, pos, SEEK_SET) == -1) + err(1, "lseek(%d)", (int)pos); + if (write(fd, buf, s) != s) + err(1, "write()"); + pthread_mutex_unlock(&write_mutex); + usleep(arc4random() % 200); + } + return (0); +} + +/* Both ftruncate() and fdatasync() triggers the problem */ + +static void * +tr(void *arg __unused) +{ + while (go == -1) + usleep(50); + while (go == 1) { +#if 0 + if (ftruncate(fd, siz) == -1) /* No size change */ + err(1, "truncate)"); +#else + if (fdatasync(fd) == -1) + err(1, "fdatasync()"); +#endif + usleep(arc4random() % 1000); + } + return (0); +} + +int +main(int argc, char *argv[]) +{ + struct stat st; + pthread_t tp[31]; + int e, i, idx; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + if ((fd = open(argv[1], O_RDWR)) == -1) + err(1, "open(%s)", argv[1]); + if (fstat(fd, &st) == -1) + err(1, "stat(%s)", argv[1]); + siz = st.st_size; + cp = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (cp == MAP_FAILED) + err(1, "mmap()"); + + go = -1; + pthread_mutex_init(&write_mutex, NULL); + idx = 0; + for (i = 0; i < (int)(arc4random() % 10 + 1); i++) { + if ((e = pthread_create(&tp[idx++], NULL, memread, NULL)) != 0) + errc(1, e, "pthread_create"); + } + for (i = 0; i < (int)(arc4random() % 10 + 1); i++) { + if ((e = pthread_create(&tp[idx++], NULL, memwrite, NULL)) != 0) + errc(1, e, "pthread_create"); + } + for (i = 0; i < (int)(arc4random() % 10 + 1); i++) { + if ((e = pthread_create(&tp[idx++], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + } + if ((e = pthread_create(&tp[idx++], NULL, tr, NULL)) != 0) + errc(1, e, "pthread_create"); + + sleep(1); + go = 1; + sleep(60); + go = 0; + for (i = 0; i < idx; i++) + pthread_join(tp[i], NULL); + if (munmap(cp, siz) == -1) + err(1, "munmap()"); + close(fd); +} +EOF +mycc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c -lpthread || exit 1 + +mycc -o $serial -Wall -Wextra -O2 ../tools/serial.c || exit 1 +mount | grep -q "on $mntpoint " && umount -f $mntpoint +mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart +mdconfig -s 5g -u $mdstart +newfs -n $newfs_flags /dev/md$mdstart > /dev/null +mount /dev/md$mdstart $mntpoint + +mp2=${mntpoint}2 +mkdir -p $mp2 +mount | grep -q "on $mp2 " && umount -f $mp2 +mount -t nfs -o retrycnt=3 127.0.0.1:$mntpoint $mp2 || exit 1 +sleep .2 + +here=`pwd` +cd $mp2 +$here/../testcases/swap/swap -t 5m -i 20 > /dev/null & +sleep 2 + +size=262144 +$serial file $size +cp file file.orig + +s=0 +/tmp/$prog file || s=1 + +while pgrep -q swap; do pkill swap; done +wait +if ! cmp -s file.orig file; then + od -t x1 file.orig > /var/tmp/$prog.file1 + od -t x1 file > /var/tmp/$prog.file2 + diff /var/tmp/$prog.file1 /var/tmp/$prog.file2 > $log + head -20 $log + rm /var/tmp/$prog.file1 /var/tmp/$prog.file2 + ls -ls file.orig file + s=2 +fi + +cd $here +umount $mp2 +umount $mntpoint +mdconfig -d -u $mdstart +rm -f $serial /tmp/$prog /tmp/$prog.c $log +exit $s diff --git a/tools/test/stress2/misc/mmap48.sh b/tools/test/stress2/misc/mmap48.sh new file mode 100755 index 000000000000..35da78a98c26 --- /dev/null +++ b/tools/test/stress2/misc/mmap48.sh @@ -0,0 +1,289 @@ +#!/bin/sh + +# +# Copyright (c) 2024 Peter Holm +# +# SPDX-License-Identifier: BSD-2-Clause +# + +# Demonstrate issue described in: +# [Bug 276002] nfscl: data corruption using both copy_file_range and mmap'd I/O + +# This version only uses mapped read/write, read(2)/write(2), fstat(2) and ftruncate(2) + +# Issue seen: +# 20241003 10:04:24 all: mmap48.sh +# 5257c5257 +# < 0244200 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f +# --- +# > 0244200 80 81 82 83 84 85 86 87 88 ee 8a 8b 8c 8d 8e 8f +# 256 -rw------- 1 root wheel 262144 Oct 3 10:05 file +# 256 -rw------- 1 root wheel 262144 Oct 3 10:04 file.orig +# FAIL mmap48.sh exit code 2 + +[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 +. ../default.cfg +set -u +prog=$(basename "$0" .sh) +log=/tmp/$prog.log +serial=/tmp/$prog.serial +grep -q $mntpoint /etc/exports || + { echo "$mntpoint missing from /etc/exports"; exit 0; } +rpcinfo 2>/dev/null | grep -q mountd || exit 0 + +cat > /tmp/$prog.c < +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static off_t siz; +static pthread_mutex_t write_mutex; +static int fd, go; +static char *cp; + +#define THREADS 100 + +static void * +memread(void *arg __unused) +{ + int i; + char c; + + if (arc4random() % 100 < 10) + return (0); + pthread_set_name_np(pthread_self(), __func__); + while (go == -1) + usleep(50); + while (go == 1) { + i = arc4random() % siz; + c = cp[i]; + if (c != 0x77) /* No unused vars here */ + usleep(arc4random() % 400); + } + return (NULL); +} + +static void * +memwrite(void *arg __unused) +{ + int i; + char c; + + if (arc4random() % 100 < 10) + return (0); + pthread_set_name_np(pthread_self(), __func__); + while (go == -1) + usleep(50); + while (go == 1) { + i = arc4random() % siz; + pthread_mutex_lock(&write_mutex); + c = cp[i]; + cp[i] = 0xee; /* This value seems to linger with NFS */ + cp[i] = c; + pthread_mutex_unlock(&write_mutex); + usleep(arc4random() % 400); + } + return (NULL); +} + +static void * +wr(void *arg __unused) +{ + off_t pos; + int r, s; + char buf[1024]; + + if (arc4random() % 100 < 10) + return (0); + pthread_set_name_np(pthread_self(), __func__); + while (go == -1) + usleep(50); + while (go == 1) { + s = arc4random() % sizeof(buf) + 1; + pos = arc4random() % (siz - s); + pthread_mutex_lock(&write_mutex); + if (lseek(fd, pos, SEEK_SET) == -1) + err(1, "lseek(%d)", (int)pos); + if ((r = read(fd, buf, s)) != s) { + fprintf(stderr, "r = %d, s = %d, pos = %d\n", r, s, (int)pos); + err(1, "read():2"); + } + if (lseek(fd, pos, SEEK_SET) == -1) + err(1, "lseek(%d)", (int)pos); + if (write(fd, buf, s) != s) + err(1, "write()"); + pthread_mutex_unlock(&write_mutex); + usleep(arc4random() % 200); + } + return (NULL); +} + +/* Both ftruncate() and fdatasync() triggers the problem */ + +static void * +sy(void *arg __unused) +{ + + if (arc4random() % 100 < 10) + return (0); + pthread_set_name_np(pthread_self(), __func__); + while (go == -1) + usleep(50); + while (go == 1) { + if (fdatasync(fd) == -1) + err(1, "fdatasync()"); + usleep(arc4random() % 1000); + } + return (NULL); +} + +static void * +tr(void *arg __unused) +{ + + if (arc4random() % 100 < 10) + return (0); + pthread_set_name_np(pthread_self(), __func__); + while (go == -1) + usleep(50); + while (go == 1) { + if (ftruncate(fd, siz) == -1) /* No size change */ + err(1, "truncate)"); + usleep(arc4random() % 1000); + } + return (NULL); +} + +static void * +fs(void *arg __unused) +{ + struct stat st; + + if (arc4random() % 100 < 10) + return (0); + pthread_set_name_np(pthread_self(), __func__); + while (go == -1) + usleep(50); + while (go == 1) { + if (fstat(fd, &st) == -1) + err(1, "stat()"); + usleep(arc4random() % 1000); + } + return (NULL); +} + +int +main(int argc, char *argv[]) +{ + struct stat st; + pthread_t tp[THREADS]; + int e, i, idx; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + if ((fd = open(argv[1], O_RDWR)) == -1) + err(1, "open(%s)", argv[1]); + if (fstat(fd, &st) == -1) + err(1, "stat(%s)", argv[1]); + siz = st.st_size; + cp = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (cp == MAP_FAILED) + err(1, "mmap()"); + + go = -1; + pthread_mutex_init(&write_mutex, NULL); + idx = 0; + for (i = 0; i < (int)(arc4random() % 3 + 1); i++) { + if ((e = pthread_create(&tp[idx++], NULL, memread, NULL)) != 0) + errc(1, e, "pthread_create"); + } + for (i = 0; i < (int)(arc4random() % 3 + 1); i++) { + if ((e = pthread_create(&tp[idx++], NULL, memwrite, NULL)) != 0) + errc(1, e, "pthread_create"); + } + for (i = 0; i < (int)(arc4random() % 3 + 1); i++) { + if ((e = pthread_create(&tp[idx++], NULL, wr, NULL)) != 0) + errc(1, e, "pthread_create"); + } + for (i = 0; i < (int)(arc4random() % 3 + 1); i++) { + if ((e = pthread_create(&tp[idx++], NULL, fs, NULL)) != 0) + errc(1, e, "pthread_create"); + } + for (i = 0; i < (int)(arc4random() % 3 + 1); i++) { + if ((e = pthread_create(&tp[idx++], NULL, tr, NULL)) != 0) + errc(1, e, "pthread_create"); + } + for (i = 0; i < (int)(arc4random() % 3 + 1); i++) { + if ((e = pthread_create(&tp[idx++], NULL, sy, NULL)) != 0) + errc(1, e, "pthread_create"); + } *** 59 LINES SKIPPED ***