Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Harry Yoo <harry@kernel.org>
To: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
	 Nick Terrell <terrelln@fb.com>, Qu Wenruo <wqu@suse.com>
Cc: linux-btrfs@vger.kernel.org, Boris Burkov <boris@bur.io>,
	 Leo Martins <loemra.dev@gmail.com>,
	stable@vger.kernel.org
Subject: Re: [PATCH] btrfs: subpage: do not end writeback of blocks owned by async submission
Date: Fri, 4 Dec 2026 23:13:53 +0000	[thread overview]
Message-ID: <axNIwlzzwmQye-QE@thinkstation> (raw)
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 <linux>/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 <scratch device>" 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 <scratch-dev> [seconds]
  # Expects ./repro (gcc -O2 -pthread -o repro repro.c) in the current directory.
  dev=${1:?usage: $0 <scratch-dev> [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 <dir> <files> <secs> <tail_folios> <nsync_blocks>
   *              <syncers_per_file> [bblocks=5] [folio_kb=64]
   */
  #define _GNU_SOURCE
  #include <fcntl.h>
  #include <pthread.h>
  #include <sched.h>
  #include <stdatomic.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <sys/stat.h>
  #include <time.h>
  #include <unistd.h>

  #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

  reply	other threads:[~2026-12-04 23:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 23:10 [PATCH] btrfs: subpage: do not end writeback of blocks owned by async submission Harry Yoo (Meta)
2026-12-04 23:13 ` Harry Yoo [this message]
2026-12-04 23:16 ` Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axNIwlzzwmQye-QE@thinkstation \
    --to=harry@kernel.org \
    --cc=boris@bur.io \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=loemra.dev@gmail.com \
    --cc=mason@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=terrelln@fb.com \
    --cc=wqu@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox