Linux block layer
 help / color / mirror / Atom feed
* [PATCH] loop: only write back the backing file when direct I/O is used
@ 2026-11-26 23:37 János Szathmáry
  2026-11-30  8:37 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ messages in thread
From: János Szathmáry @ 2026-11-26 23:37 UTC (permalink / raw)
  To: Jens Axboe
  Cc: linux-block, Ming Lei, Christoph Hellwig,
	János Szathmáry, stable

Commit 86947bdc2889 ("loop: move vfs_fsync() out of loop_update_dio()")
moved the vfs_fsync() of the backing file out of loop_update_dio(), so
that it no longer runs with the queue frozen.  In loop_configure() the
call became unconditional: every loop device setup now writes back all
dirty data of the backing file, also for a device that uses buffered
I/O.

Before that commit the backing file was only written back when the
device started to use direct I/O.  A buffered loop device reads the
backing file through the page cache, where the dirty data is, so it has
no use for the write back.

With the unconditional call, setting up a loop device on a file that
was written just before waits for that file to reach the disk.  In VMs
running 6.18 with a rate limited virtio block device, mounting a
squashfs image through a loop device (LOOP_CONFIGURE, then mount(2))
right after the image was downloaded takes:

  image size   time for the mount
  0.4 GB       0.8 - 1.4 s
  1.1 GB       3.2 - 5.2 s
  2.1 GB       2.2 - 2.7 s  (larger VM with a higher rate limit)
  4.5 GB       6.1 - 11.0 s

The same mount takes 4 - 9 ms for a 0.3 - 0.4 GB image that was written
minutes earlier, and 4 - 7 ms for a 2.1 GB image that is fdatasync()ed
while it downloads.

Call vfs_fsync() after loop_update_dio() and only when the device ends
up with LO_FLAGS_DIRECT_IO set.  loop_update_dio() can only clear the
flag, so it is final at that point.  The device is not bound and the
queue is not frozen there, so the write back stays out of the frozen
section, which is what the commit above was about.

A device that is switched to direct I/O later with LOOP_SET_DIRECT_IO
is still covered: loop_set_dio() writes back the backing file before it
enables direct I/O.

Fixes: 86947bdc2889 ("loop: move vfs_fsync() out of loop_update_dio()")
Cc: stable@vger.kernel.org
Signed-off-by: János Szathmáry <szathmary.janos89@gmail.com>
---
Tested on arm64 under KVM (QEMU 8.2, 4 CPUs, 8 GiB), mainline at
9437ee8b6cf6 with and without this patch.  The backing file is 512 MiB,
written with dd just before on ext4 on a virtio disk whose writes are
throttled to 150 MB/s (QEMU throttling.bps-write), several runs each:

                                  before          after
  losetup (buffered)              3.42 - 3.65 s   0.04 - 0.08 s
  losetup --direct-io=on          3.40 - 3.56 s   3.40 - 3.68 s
  buffered, then --direct-io=on   -               3.53 s at the switch

Dirty in /proc/meminfo goes from 512 MiB to 0 in every case that takes
seconds, and stays at 512 MiB in the buffered case after the patch.  A
squashfs image written just before reads back identical through a
buffered loop device, before and after.

blktests loop group, same result before and after: 001-003, 005,
007-009 and 011 pass; 004 and 012 are not run (no scsi_debug module,
systemd older than 259); 010 and 013 fail in both, because the test VM
has no systemd or udev.

A W=1 build of drivers/block/loop.o is clean.

The times in the changelog are from VMs on 6.18 and cover the whole
mount as the caller sees it; LOOP_CONFIGURE was not timed on its own
there.

The cause was found and this patch was drafted with the help of an AI
coding assistant (Claude), from reading the loop driver at v6.12, v6.18
and mainline and from the timings in the changelog.  The tests above
were run with its help.

 drivers/block/loop.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a3325ce..0c84eb7 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -986,14 +986,16 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
 	if (error)
 		goto out_unlock;
 
+	loop_update_dio(lo);
+
 	/*
-	 * We might switch to direct I/O mode for the loop device, write back
-	 * all dirty data the page cache now that so that the individual I/O
-	 * operations don't have to do that.
+	 * Direct I/O bypasses the page cache, so write back all dirty data of
+	 * the backing file before the device starts to use it.  With buffered
+	 * I/O the device reads that data from the page cache.
 	 */
-	vfs_fsync(file, 0);
+	if (lo->lo_flags & LO_FLAGS_DIRECT_IO)
+		vfs_fsync(file, 0);
 
-	loop_update_dio(lo);
 	loop_sysfs_init(lo);
 
 	size = lo_calculate_size(lo, file);

base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] loop: only write back the backing file when direct I/O is used
  2026-11-26 23:37 [PATCH] loop: only write back the backing file when direct I/O is used János Szathmáry
@ 2026-11-30  8:37 ` Christoph Hellwig
  0 siblings, 0 replies; 2+ messages in thread
From: Christoph Hellwig @ 2026-11-30  8:37 UTC (permalink / raw)
  To: János Szathmáry; +Cc: Jens Axboe, linux-block, Ming Lei, stable

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30  8:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 23:37 [PATCH] loop: only write back the backing file when direct I/O is used János Szathmáry
2026-11-30  8:37 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox