Skip to contentSkip to conversation

fuse: fix data loss when writeback races a write-through extending write

PATCH New David Crawshaw 1 participant 3 messages Nov 27

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover fuse: fix data loss when writeback races a write-through extending write

Since v6.17 commit ef7e7cbb323f "fuse: use iomap for writeback", a write-through pwrite() that extends a file inside the folio holding EOF can return success and lose its data. This happens when that folio is dirty and writeback of it starts while the FUSE_WRITE is in flight.

The mechanism is that fuse_perform_write() unlocks the folio before sending the request and only updates i_size after the reply, and iomap_writeback_handle_eof() zeroes the folio beyond the old i_size.

Patch 1 keeps a folio that straddles i_size locked over the request when the write extends the file within it, and updates i_size before the folios are unlocked, like iomap_write_iter() does.

Patch 2 adds a deterministic selftest case.

Reproduced on v6.17 and v7.4-rc4+ (9437ee8b6cf9); not on v6.16.

Data lost in the reproducer:

                                   unpatched        patched
  WRITE delayed 200us/0/2ms        1428/1500        0/6000
  8 mounts in parallel             3229/4000        0/4000
  EOF in a large folio (THP)        811/900         0/900
  new selftest case                 50/50 fail      50/50 pass

(All testing done on x86_64 KVM guests.)

Disclosure: the bisection, and both patches were produced by an AI coding agent (exe.dev's Shelley, using the Opus 5.5 model). I reviewed the work and wrote the bulk of the prose.

---
David Crawshaw (2):
fuse: update i_size before unlocking the EOF folio in write-through writes selftests/fuse: test a write extending the file beside writeback of the EOF page

 fs/fuse/file.c                                     | 21 ++++++-
 .../filesystems/fuse/write_extend_eof_test.c       | 71 ++++++++++++++++++++++
 2 files changed, 91 insertions(+), 1 deletion(-)

---
base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
change-id: 20261127-b4-fuse-wt-eof-7b71469853be

Best regards,

Signature · 1 line
David Crawshaw <[email protected]>
#1 lore raw

PATCH 1/2 fuse: update i_size before unlocking the EOF folio in write-through writes

Since FUSE writeback was converted to iomap, a write-through write that extends the file within the folio holding the current EOF can lose its data if writeback of that folio starts while the FUSE_WRITE request is in flight.

fuse_perform_write() copies the data into the page cache folio, unlocks it in fuse_fill_write_pages(), sends FUSE_WRITE, and only updates i_size after the reply has arrived. If the folio is dirty, writeback can lock it in between, and iomap_writeback_handle_eof() zeroes the folio from the old i_size to its end.

Depending on timing:

- the page cache is left with zeroes where the write's data should be,
- the FUSE_WRITE request itself carries zeroes, as its pages are the page cache folios and are only copied when the server reads the request, so the server stores zeroes,
- a later writeback of the zeroed folio overwrites the server data.

In each case the write returns success. Before iomap, FUSE writeback never modified the folio and cropped the request to i_size at send time, so this window was harmless. iomap, on the other hand, expects i_size to cover the data in a folio before the folio is unlocked, as iomap_write_iter() does for the writeback cache path.

Fix it the same way for write-through writes: keep a folio that straddles i_size locked over the request when the write extends the file within it and update i_size from fuse_send_write_pages() before the folios are unlocked.

Reproducer: a thread repeatedly stores into the EOF page through a shared mapping and calls sync_file_range(SYNC_FILE_RANGE_WRITE) on it, while another thread pwrite()s a few bytes past EOF within that page; with a server that delays FUSE_WRITE by 200us the data was lost in 493/500 runs (the selftest added in the next patch fails reliably), and in 0/3000 runs with this patch.

This patch written with an AI coding assistant (Shelley using Opus 5.5). I reviewed and tested it.

Fixes ef7e7cbb323f ("fuse: use iomap for writeback")
Cc [email protected] # v6.17+
---
 fs/fuse/file.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
fs/fuse/file.c +20 -1
index 993a1d2b315..ee3bdc3cb04 100644
@@ -1287,6 +1287,15 @@ static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
 	if (!err && ia->write.out.size > count)
 		err = -EIO;
 
+	/*
+	 * Extend i_size before unlocking the last folio, so that writeback
+	 * cannot see the newly written data beyond i_size and zero it.  See
+	 * fuse_fill_write_pages().
+	 */
+	if (!err)
+		fuse_write_update_attr(inode, pos + ia->write.out.size,
+				       ia->write.out.size);
+
 	for (i = 0; i < ap->num_folios; i++) {
 		struct folio *folio = ap->folios[i];
 
@@ -1305,6 +1314,7 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 {
 	struct fuse_args_pages *ap = &ia->ap;
 	struct fuse_conn *fc = get_fuse_conn(mapping->host);
+	loff_t isize = i_size_read(mapping->host);
 	size_t count = 0;
 	unsigned int num;
 	int err = 0;
@@ -1366,7 +1376,16 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 		if (tmp == folio_size(folio))
 			iomap_folio_mark_uptodate(folio);
 
-		if (folio_test_uptodate(folio)) {
+		/*
+		 * Keep the folio locked until the reply if it is not uptodate,
+		 * or if it straddles i_size and this write extends the file
+		 * within it: writeback zeroes a folio beyond i_size
+		 * (iomap_writeback_handle_eof()), which would wipe the data
+		 * just copied, both in the page cache and in the request,
+		 * while i_size has not yet been updated.
+		 */
+		if (folio_test_uptodate(folio) &&
+		    !(folio_pos(folio) < isize && pos > isize)) {
 			folio_unlock(folio);
 		} else {
 			ia->write.folio_locked = true;
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 selftests/fuse: test a write extending the file beside writeback of the EOF page

In reply to David Crawshaw

Add a case to write_extend_eof_test: while a pwrite() that extends the file inside its mmap-dirtied EOF page is held by the server, write that page back with sync_file_range(). The written data must be in the page cache afterwards, and on the server after fsync().

Written with an AI coding assistant (Shelley using Opus 5.5).

---
 .../filesystems/fuse/write_extend_eof_test.c       | 71 ++++++++++++++++++++++
 1 file changed, 71 insertions(+)
tools/testing/selftests/filesystems/fuse/write_extend_eof_test.c +71 -0
index d6d5de8dac2..cdefe459359 100644
@@ -57,6 +57,8 @@ struct backing_file {
 struct server {
 	int fd;
 	unsigned int copy_ops;			/* FUSE_COPY_FILE_RANGE* seen */
+	uint64_t hold_off;			/* delay the WRITE at this offset */
+	int held;				/* ... and set this when it came */
 	struct backing_file file[2];		/* FILE_INO, SRC_INO */
 };
 
@@ -179,6 +181,13 @@ static void *server_thread(void *arg)
 			uint64_t off = in->offset;
 			uint32_t size = in->size;
 
+			/* Leave time for writeback while the write is pending. */
+			if (s->hold_off && off == s->hold_off &&
+			    !(in->write_flags & FUSE_WRITE_CACHE)) {
+				__atomic_store_n(&s->held, 1, __ATOMIC_SEQ_CST);
+				usleep(200 * 1000);
+			}
+
 			if (off < BACKING_SIZE) {
 				uint32_t c = size;
 
@@ -472,4 +481,66 @@ TEST_F(fuse, extend_into_eof_page_preserves_data)
 	free(rd);
 }
 
+struct writeback_args {
+	struct server *srv;
+	char *map;
+	int fd;
+	off_t base;
+	long page;
+};
+
+/* Once the extending WRITE is pending, dirty the EOF page and write it back. */
+static void *writeback_thread(void *arg)
+{
+	struct writeback_args *a = arg;
+	int i;
+
+	for (i = 0; i < 5000; i++) {
+		if (__atomic_load_n(&a->srv->held, __ATOMIC_SEQ_CST))
+			break;
+		usleep(1000);
+	}
+	a->map[a->page - 1] = 1;
+	sync_file_range(a->fd, a->base, a->page, SYNC_FILE_RANGE_WRITE);
+	return NULL;
+}
+
+/*
+ * Writeback of the mmap-dirtied EOF page that starts while a write extending
+ * the file within that page is in flight must not zero the written data:
+ * the write has copied it into the page cache, but i_size is only updated
+ * once the server has replied, and writeback zeroes the page past i_size.
+ */
+TEST_F(fuse, extend_beside_writeback)
+{
+	struct writeback_args a;
+	pthread_t t;
+	off_t base, wr;
+	char rd[4];
+
+	pollute_eof_tail(_metadata, self);
+	base = self->eof & ~(self->page - 1);
+	wr = self->eof + 100;			/* in the EOF page, past EOF */
+	ASSERT_LT(wr + 4, base + self->page);
+
+	a.srv = self->srv;
+	a.map = self->map;
+	a.fd = self->fd;
+	a.base = base;
+	a.page = self->page;
+	self->srv->hold_off = wr;
+	ASSERT_EQ(0, pthread_create(&t, NULL, writeback_thread, &a));
+
+	ASSERT_EQ(4, pwrite(self->fd, "data", 4, wr));
+	pthread_join(t, NULL);
+	ASSERT_EQ(1, self->srv->held);
+
+	ASSERT_EQ(4, pread(self->fd, rd, 4, wr));
+	ASSERT_EQ(0, memcmp(rd, "data", 4));
+
+	ASSERT_EQ(0, fsync(self->fd));
+	ASSERT_EQ(wr + 4, self->srv->file[0].size);
+	ASSERT_EQ(0, memcmp(self->srv->file[0].data + wr, "data", 4));
+}
+
 TEST_HARNESS_MAIN
Signature · 1 line
2.43.0