Skip to contentSkip to conversation

iomap/fuse: add helper to keep uptodate bitmap in sync

PATCH Applied Joanne Koong 4 participants 11 messages Jul 7 – Jul 22

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

v3 cover iomap/fuse: add helper to keep uptodate bitmap in sync

Filesystems that use iomap normally mark folios uptodate through the iomap read/write paths, which keep iomap's internal uptodate bitmap in sync. However, some filesystems need to write data directly into the page cache outside of those paths (eg fuse, which may need to write server-pushed data straight into the page cache for servicing notify stores). These filesystems need a way to mark a folio uptodate that also updates the iomap bitmap so they're kept in sync.

This series adds iomap_folio_mark_uptodate() for filesystems to do that and updates the relevant fuse call paths to use this. This is needed before fuse can enable large folios.

Patch 1 ("fuse: don't clear folio uptodate on writethrough errors") was originally submitted to the fuse tree [1] but patch 3 ("fuse: use iomap helper to mark folio uptodate") has a dependency on it, so to make cross-coordination between trees easier, patch 1 is now part of this series and submitted to the vfs tree. This series is based on top of the vfs-7.3.iomap branch (head commit e1b77fb85836e ("Merge patch series "iomap: add simple dio path...")).

Thanks,
Joanne

[1] https://lore.kernel.org/fuse-devel/[email protected]/

Changelog
---------
v2: https://lore.kernel.org/linux-fsdevel/[email protected]/
* add Reviewed-by tags and rebase onto the vfs-7.3.iomap branch
* include patch 1 from [1], to ensure ordering between patches for bisectability

v1: https://lore.kernel.org/linux-fsdevel/[email protected]/
v1 -> v2:
* drop helper for clearing uptodate state, as per Matthew and Christoph's comments

Joanne Koong (3):
fuse: don't clear folio uptodate on writethrough errors
iomap: add helper to mark folio uptodate
fuse: use iomap helper to mark folio uptodate

 fs/fuse/file.c         | 20 ++------------------
 fs/fuse/notify.c       |  4 +++-
 fs/iomap/buffered-io.c |  6 ++++++
 include/linux/iomap.h  |  1 +
 4 files changed, 12 insertions(+), 19 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v3 1/3 fuse: don't clear folio uptodate on writethrough errors

In the writethrough path (fuse_send_write_pages()), if the write to the server failed or was a short write, the uptodate flag on the folios are cleared.

As explained by Matthew in [1], this is dangerous because the folio may be mapped into userspace. The mm code has the invariant that a non-uptodate folio must never be visible to userspace (to avoid potentially leaking confidental information to userspace) and has checks in place for this that if violated can bring down the whole machine.

Practically speaking, the effect of this change for the fuse writethrough error path is that if an application does a write and then the server fails to persist the data or only services a short write, the page cache folio keeps the data the application wrote instead of being reverted to the server's contents on the next read. The failure is still reported to the application synchronously through the short count / error return of the write() syscall. Folios that were only partially written are unaffected since they were never marked uptodate in the first place (fuse_fill_write_page() only marks a folio as uptodate if the whole folio was written to).

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

---
 fs/fuse/file.c | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)
fs/fuse/file.c +1 -17
index e052a0d44dee..a72959dbcf12 100644
@@ -1227,8 +1227,7 @@ static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
 	struct file *file = iocb->ki_filp;
 	struct fuse_file *ff = file->private_data;
 	struct fuse_mount *fm = ff->fm;
-	unsigned int offset, i;
-	bool short_write;
+	unsigned int i;
 	int err;
 
 	for (i = 0; i < ap->num_folios; i++)
@@ -1243,24 +1242,9 @@ static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
 	if (!err && ia->write.out.size > count)
 		err = -EIO;
 
-	short_write = ia->write.out.size < count;
-	offset = ap->descs[0].offset;
-	count = ia->write.out.size;
 	for (i = 0; i < ap->num_folios; i++) {
 		struct folio *folio = ap->folios[i];
 
-		if (err) {
-			folio_clear_uptodate(folio);
-		} else {
-			if (count >= folio_size(folio) - offset)
-				count -= folio_size(folio) - offset;
-			else {
-				if (short_write)
-					folio_clear_uptodate(folio);
-				count = 0;
-			}
-			offset = 0;
-		}
 		if (ia->write.folio_locked && (i == ap->num_folios - 1))
 			folio_unlock(folio);
 		folio_put(folio);
Signature · 1 line
2.52.0
maintainer Acked-by on v3 1/3
#3 lore raw

Re: [PATCH v3 1/3] fuse: don't clear folio uptodate on writethrough errors

In reply to Joanne Koong
Quoted 22 lines · Joanne Koong

In the writethrough path (fuse_send_write_pages()), if the write to the server failed or was a short write, the uptodate flag on the folios are cleared.

As explained by Matthew in [1], this is dangerous because the folio may be mapped into userspace. The mm code has the invariant that a non-uptodate folio must never be visible to userspace (to avoid potentially leaking confidental information to userspace) and has checks in place for this that if violated can bring down the whole machine.

Practically speaking, the effect of this change for the fuse writethrough error path is that if an application does a write and then the server fails to persist the data or only services a short write, the page cache folio keeps the data the application wrote instead of being reverted to the server's contents on the next read. The failure is still reported to the application synchronously through the short count / error return of the write() syscall. Folios that were only partially written are unaffected since they were never marked uptodate in the first place (fuse_fill_write_page() only marks a folio as uptodate if the whole folio was written to).

Quoted 5 lines

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

#4 lore raw

PATCH v3 2/3 iomap: add helper to mark folio uptodate

In reply to Joanne Koong

Add an exported helper iomap_folio_mark_uptodate() to mark a folio as uptodate and update its uptodate bitmap if the folio has iomap state data attached.

This is needed because there are some filesystems (eg fuse) that have paths outside of conventional iomap calls that need to mark a folio as uptodate (eg writing server-pushed data directly into the page cache) and need the iomap-internal uptodate bitmap to be in sync with the uptodate state of the folio.

---
 fs/iomap/buffered-io.c | 6 ++++++
 include/linux/iomap.h  | 1 +
 2 files changed, 7 insertions(+)
fs/iomap/buffered-io.c +6 -0
index 27bc2455a98d..f6040199d114 100644
@@ -105,6 +105,12 @@ static void iomap_set_range_uptodate(struct folio *folio, size_t off,
 		folio_mark_uptodate(folio);
 }
 
+void iomap_folio_mark_uptodate(struct folio *folio)
+{
+	iomap_set_range_uptodate(folio, 0, folio_size(folio));
+}
+EXPORT_SYMBOL_GPL(iomap_folio_mark_uptodate);
+
 /*
  * Find the next dirty block in the folio. end_blk is inclusive.
  * If no dirty block is found, this will return end_blk + 1.
include/linux/iomap.h +1 -0
index 3582ed1fe236..21e73cb9c51e 100644
@@ -365,6 +365,7 @@ struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len);
 bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags);
 void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len);
 bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio);
+void iomap_folio_mark_uptodate(struct folio *folio);
 int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
 		const struct iomap_ops *ops,
 		const struct iomap_write_ops *write_ops);
Signature · 1 line
2.52.0
#6 lore raw

Re: [PATCH v3 2/3] iomap: add helper to mark folio uptodate

Quoted 7 lines · Miklos Szeredi
Quoted 4 lines · Joanne Koong

Add an exported helper iomap_folio_mark_uptodate() to mark a folio as uptodate and update its uptodate bitmap if the folio has iomap state data attached.

Quoted 5 lines

Sashiko has a non-regression comment about this:

https://sashiko.dev/#/patchset/20260707220450.1200943-1-joannelkoong%40gmail.com

Can you have a look, please?

I think this underflow issue was fixed by Zhang's patch in [1] that he sent out in May but I don't think that has been merged yet. I'll follow up on his series about the status of it.

Thanks,
Joanne

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

#7 lore raw

PATCH v3 3/3 fuse: use iomap helper to mark folio uptodate

In reply to Joanne Koong

When fuse enables large folios, a large folio will be backed by iomap_folio_state that keeps track of uptodate and dirty state in an internal bitmap.

Fuse writethrough and notify store paths currently set folio uptodate state with folio_mark_uptodate(), which touches only the folio-level flag, but on an iomap-backed folio, that leaves the uptodate bitmap out of sync.

Use the iomap_folio_mark_uptodate() helper to update both the folio uptodate state and the iomap uptodate bitmap.

---
 fs/fuse/file.c   | 2 +-
 fs/fuse/notify.c | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
fs/fuse/file.c +1 -1
index a72959dbcf12..ea4a15a7635a 100644
@@ -1319,7 +1319,7 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
 
 		/* If we copied full folio, mark it uptodate */
 		if (tmp == folio_size(folio))
-			folio_mark_uptodate(folio);
+			iomap_folio_mark_uptodate(folio);
 
 		if (folio_test_uptodate(folio)) {
 			folio_unlock(folio);
fs/fuse/notify.c +3 -1
index 29578104ae6c..1ba763705d91 100644
@@ -2,6 +2,8 @@
 
 #include "dev.h"
 #include "fuse_i.h"
+
+#include <linux/iomap.h>
 #include <linux/pagemap.h>
 
 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
@@ -192,7 +194,7 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 		if (!folio_test_uptodate(folio) && !err && folio_offset == 0 &&
 		    (nr_bytes == folio_size(folio) || file_size == end)) {
 			folio_zero_segment(folio, nr_bytes, folio_size(folio));
-			folio_mark_uptodate(folio);
+			iomap_folio_mark_uptodate(folio);
 		}
 		folio_unlock(folio);
 		folio_put(folio);
Signature · 1 line
2.52.0
maintainer Acked-by on v3 3/3
#9 lore raw

Re: [PATCH v3 3/3] fuse: use iomap helper to mark folio uptodate

In reply to Joanne Koong
Quoted 10 lines · Joanne Koong

When fuse enables large folios, a large folio will be backed by iomap_folio_state that keeps track of uptodate and dirty state in an internal bitmap.

Fuse writethrough and notify store paths currently set folio uptodate state with folio_mark_uptodate(), which touches only the folio-level flag, but on an iomap-backed folio, that leaves the uptodate bitmap out of sync.

Quoted 5 lines

Use the iomap_folio_mark_uptodate() helper to update both the folio uptodate state and the iomap uptodate bitmap.

maintainer Applied
#10 lore raw
In reply to Joanne Koong
Quoted 11 lines · Joanne Koong

iomap/fuse: add helper to keep uptodate bitmap in sync

Filesystems that use iomap normally mark folios uptodate through the iomap read/write paths, which keep iomap's internal uptodate bitmap in sync. However, some filesystems need to write data directly into the page cache outside of those paths (eg fuse, which may need to write server-pushed data straight into the page cache for servicing notify stores). These filesystems need a way to mark a folio uptodate that also updates the iomap bitmap so they're kept in sync.

[...]

Applied to the vfs-7.3.iomap branch of the vfs/vfs.git tree. Patches in the vfs-7.3.iomap branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.3.iomap

[1/3] fuse: don't clear folio uptodate on writethrough errors

      https://git.kernel.org/vfs/vfs/c/e4cd50a53ab6
[2/3] iomap: add helper to mark folio uptodate
      https://git.kernel.org/vfs/vfs/c/f2286d1715d0
[3/3] fuse: use iomap helper to mark folio uptodate
      https://git.kernel.org/vfs/vfs/c/01c7fe2396a6