From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796426035; b=PisgtKTbIxC+M//FCM7Wi/BxHmOWaPPfz15XalVAR7llC8l7bJTRtXDZkedXYWC8F04/ XpczmNsfP5PdbiUo9NhdW9R4s1+HR25AnGdphc0i4BZdnBAFeI5CV/uYrt9XhBbAL3EXK 4lER7yvUNtzscjwJ9Ov7s1NsdTUcrYhRBjT7zYbIlA73ahea+PjXqPzGt2aWW8+JrELpR /DI7O3cr2SEHFzlaY+OF/QFhnB7s3/pexzUzNWcKWT3eElNpwhfGdE6dt2s0xYHvLb2Kb Xfqua80MY6gQDRBvFl5nyNIuHmoU2/+kv4itkLJ4bb82tFUy4Xsw5IKidAheFKBmnYA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796426035; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=eW9cQOkv6tdDS0Jj/VYXbBR5PDv+IbNXu8Hck/HyZDY=; b=I6mMIWQYh4ja3m8R6Z3icLbY5wc/Nzoh//+Qu1sBu8RaECo+byUsn9YvoOgJIzYEl7F4 VsMIMNJhMVQkb2kpzHBDV+QUE4nMWf8lg8hKZrZuV5uw4YrB0kEhfXkm7DlHUMRyd1J6D 1sSjuvTFVAdAjP2O1q/RuNANGIv6o1ZhC5+VgfBRRrUAUF7oEE40KnWmGyisIEfWVWyhR Wo6jSwzMHzLZ4CZ2zsYnKObDI7dsbbSWXYWS08N4g82lFF0MHzQQ4mosiAHJS5HUus126 A5qRfaN/GUz8jAW4vwuAhjXsdyQmw0aqHA0Gh16TRDeD2dUQCChbnZt5KhHluH2SLLw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=O5RwJ74S; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1D87606A32C; Fri, 4 Dec 2026 23:13:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=O5RwJ74S Received: by smtp.kernel.org (Postfix) with ESMTPSA id E06BB1F00936; Fri, 4 Dec 2026 23:13:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796426035; bh=eW9cQOkv6tdDS0Jj/VYXbBR5PDv+IbNXu8Hck/HyZDY=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=O5RwJ74S0i/+vLf2h3sLMYGIKrEVo42/FQEyEl21pWr79m1WO12067Do8dFeihqom NCfTNVkhnQRG4iwDiEqNsmvJOKb7RHdgFdjLT01VbnSHbiJH/VTipGECfy5PvQzEFI n3Z0yGin6N3FB5sRAixmob3mPzPjR1zN2bLRerJdHcFp+GJXjvHwD4SnhjQs5v+1OS pBtTLR8UbBILrg8cEv7ehjFc/JgIH6uPHRHnSztfXEk23kox5j+tKc0gU+PMtMtU8i 2UAZb8HzA6Y2T6BIdTKb4XjW8R/vw8CFc4gDlvz68ulkc0yFHRzDXMILkYALuE4c+7 HTv4mje4UlKLg== Date: Fri, 4 Dec 2026 23:13:53 +0000 From: Harry Yoo To: David Sterba , Chris Mason , Nick Terrell , Qu Wenruo Cc: linux-btrfs@vger.kernel.org, Boris Burkov , Leo Martins , stable@vger.kernel.org Subject: Re: [PATCH] btrfs: subpage: do not end writeback of blocks owned by async submission Message-ID: References: <20261204-btrfs-subpage-writeback-fix-v1-1-89e375d29668@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261204-btrfs-subpage-writeback-fix-v1-1-89e375d29668@kernel.org> Hi, I wrote this patch with LLM assistance while debugging a problem in production. I should add that I have a limited background on btrfs (I am an MM developer!). However, I invested quite a lot of time to review and refine the patch and make sure it is readable by human. Huge thanks to Leo Martins for providing a second eye and invested time to investigate and discuss the problem. How to reproduce on x86 ======================= It reproduces on an x86 VM when data folios are large, which needs CONFIG_TRANSPARENT_HUGEPAGE=y and either v7.2 or later or CONFIG_BTRFS_EXPERIMENTAL=y. With CONFIG_BTRFS_ASSERT=y the assertion in the changelog fires within seconds. Without it, the truncating task hangs in folio_wait_writeback(). Kernel config: CONFIG_BTRFS_FS=y CONFIG_BTRFS_ASSERT=y CONFIG_TRANSPARENT_HUGEPAGE=y CONFIG_DETECT_HUNG_TASK=y CONFIG_DEFAULT_HUNG_TASK_TIMEOUT=30 With virtme-ng, from a directory that has repro.c and run-repro.sh: $ gcc -O2 -pthread -o repro repro.c $ truncate -s 10G disk.img $ vng -r /arch/x86/boot/bzImage --cpus 16 --memory 8G \ --disk disk.img --user root --exec "./run-repro.sh /dev/vda 120" On another VM, run "./run-repro.sh " as root. It prints "REPRODUCED after N s" and the splat. run-repro.sh: #!/bin/sh # Run inside the VM as root: ./run-repro.sh [seconds] # Expects ./repro (gcc -O2 -pthread -o repro repro.c) in the current directory. dev=${1:?usage: $0 [seconds]} secs=${2:-60} mnt=$(mktemp -d) mkfs.btrfs -q -f -s 4k "$dev" || exit 1 mount -o compress-force=zstd:3 "$dev" $mnt || exit 1 dmesg -C ./repro $mnt 1 "$secs" 0 16 2 & pid=$! start=$(date +%s) while kill -0 $pid 2>/dev/null; do sleep 1 if dmesg | grep -q -E 'assertion failed: folio_test_writeback|INFO: task repro'; then echo "REPRODUCED after $(($(date +%s) - start)) s" dmesg | grep -A 24 -E 'assertion failed|INFO: task' exit 1 fi done echo "not reproduced in $secs s" umount $mnt repro.c: // SPDX-License-Identifier: GPL-2.0 /* * repro: aim writeback at one large folio holding a clean block 0, an async * (compressed) delalloc range and single-block sync delalloc ranges, right * after a truncate-down made all of them >= i_size. * * Usage: repro * [bblocks=5] [folio_kb=64] */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #define BS 4096 static int nfiles, tail, ns, nsy; static int bblocks = 5; static int folio_kb = 64; static atomic_int stop; static struct fstate { int fd; atomic_int trunc; atomic_long cycles; } *fs; static char *mkbuf(void) { char *b = malloc(1 << 20); for (int i = 0; i < (1 << 20); i++) b[i] = (i / 64) & 1 ? 'a' + i % 13 : (char)rand(); return b; } static void *cyc(void *arg) { struct fstate *f = arg; char *buf = mkbuf(); int fd = f->fd; int fb = folio_kb * 1024; while (!stop) { ftruncate(fd, 0); /* One large folio, all dirty. */ pwrite(fd, buf, fb, 0); sync_file_range(fd, 0, fb, SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER); /* * Re-dirty: block 0 stays clean, [1, bblocks] is the async * range, then a gap, then ns single blocks separated by gaps. */ pwrite(fd, buf, bblocks * BS, BS); for (int i = 0; i < ns; i++) pwrite(fd, buf, BS, (long)(bblocks + 2 + 2 * i) * BS); for (int i = 0; i < tail; i++) pwrite(fd, buf, BS, fb + (long)i * 2 * BS); atomic_store(&f->trunc, 1); ftruncate(fd, BS); atomic_store(&f->trunc, 0); atomic_fetch_add(&f->cycles, 1); } return NULL; } static void *syncer(void *arg) { struct fstate *f = arg; struct stat st; int fb = folio_kb * 1024; while (!stop) { if (!atomic_load(&f->trunc)) { sched_yield(); continue; } fstat(f->fd, &st); if (st.st_size > BS) continue; while (atomic_load(&f->trunc)) sync_file_range(f->fd, 0, fb, SYNC_FILE_RANGE_WRITE); } return NULL; } int main(int argc, char **argv) { char path[4096]; pthread_t t; int secs; if (argc < 7) return 1; nfiles = atoi(argv[2]); secs = atoi(argv[3]); tail = atoi(argv[4]); ns = atoi(argv[5]); nsy = atoi(argv[6]); if (argc > 7) bblocks = atoi(argv[7]); if (argc > 8) folio_kb = atoi(argv[8]); fs = calloc(nfiles, sizeof(*fs)); for (int i = 0; i < nfiles; i++) { snprintf(path, sizeof(path), "%s/n%d", argv[1], i); fs[i].fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600); pthread_create(&t, NULL, cyc, &fs[i]); for (int j = 0; j < nsy; j++) pthread_create(&t, NULL, syncer, &fs[i]); } for (int s = 0; s < secs; s += 10) { long c = 0; sleep(10); for (int i = 0; i < nfiles; i++) c += atomic_load(&fs[i].cycles); printf("t=%ds cycles=%ld\n", s + 10, c); fflush(stdout); } stop = 1; return 0; } -- Cheers, Harry / Hyeonggon