Skip to contentSkip to conversation

iomap: add helpers to keep uptodate bitmap in sync

PATCH Superseded Joanne Koong 3 participants 6 messages Jun 23 – Jun 24

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover iomap: add helpers to keep uptodate bitmap in sync

This series is on top of the main linux tree [1] because it depends on some fuse changes in Miklos's tree that were merged into 7.2 but not yet pulled into Christian's vfs tree.

The first patch in this series adds two helpers to iomap that update the uptodate bitmap whenever a folio's uptodate status needs to be set or cleared.

This is so that external callers who need to update the uptodate folio state from a non-iomap path can do so and keep the bitmap in sync.

The second patch adds the fuse caller side of it. fuse uses these two helpers in the notify store path for storing server-pushed data into the page cache pages and in the writethrough path. This series is needed before fuse can start using large folios.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/

Joanne Koong (2):
iomap: add helpers to mark and clear folio uptodate state
fuse: use iomap helpers to update folio uptodate state

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

PATCH 1/2 iomap: add helpers to mark and clear folio uptodate state

Add two exported helpers, iomap_folio_mark_uptodate() and iomap_folio_clear_uptodate(). These set or clear the uptodate state of a folio and adjust the uptodate bitmap if there is iomap state attached to the folio.

This is needed because there are some filesystems (eg fuse) that have paths outside of conventional iomap calls that need to mark or clear 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 | 27 +++++++++++++++++++++++++++
 include/linux/iomap.h  |  2 ++
 2 files changed, 29 insertions(+)
fs/iomap/buffered-io.c +27 -0
index 8d4806dc46d4..317fa7af98bd 100644
@@ -105,6 +105,33 @@ 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);
+
+void iomap_folio_clear_uptodate(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+	unsigned long flags;
+
+	if (!folio_test_uptodate(folio))
+		return;
+
+	if (ifs) {
+		struct inode *inode = folio->mapping->host;
+		unsigned int nr_blks = i_blocks_per_folio(inode, folio);
+
+		spin_lock_irqsave(&ifs->state_lock, flags);
+		bitmap_clear(ifs->state, 0, nr_blks);
+		spin_unlock_irqrestore(&ifs->state_lock, flags);
+	}
+
+	folio_clear_uptodate(folio);
+}
+EXPORT_SYMBOL_GPL(iomap_folio_clear_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 +2 -0
index 3582ed1fe236..f13ae425f552 100644
@@ -365,6 +365,8 @@ 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);
+void iomap_folio_clear_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
#2 lore raw

PATCH 2/2 fuse: use iomap helpers to update folio uptodate state

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 or clear uptodate state with folio_mark_uptodate() / folio_clear_uptodate(), which touch only the folio-level flag, but on an iomap-backed folio, that leaves the uptodate bitmap out of sync.

Use iomap helpers to update folio uptodate state, which will make the corresponding updates to the iomap bitmap as well.

---
 fs/fuse/file.c   | 6 +++---
 fs/fuse/notify.c | 4 +++-
 2 files changed, 6 insertions(+), 4 deletions(-)
fs/fuse/file.c +3 -3
index e052a0d44dee..12a2aa605f6b 100644
@@ -1250,13 +1250,13 @@ static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
 		struct folio *folio = ap->folios[i];
 
 		if (err) {
-			folio_clear_uptodate(folio);
+			iomap_folio_clear_uptodate(folio);
 		} else {
 			if (count >= folio_size(folio) - offset)
 				count -= folio_size(folio) - offset;
 			else {
 				if (short_write)
-					folio_clear_uptodate(folio);
+					iomap_folio_clear_uptodate(folio);
 				count = 0;
 			}
 			offset = 0;
@@ -1335,7 +1335,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
#3 lore raw

Re: [PATCH v1 2/2] fuse: use iomap helpers to update folio uptodate state

Quoted 5 lines · Joanne Koong

+++ b/fs/fuse/file.c

fs/fuse/file.c +0 -0
@@ -1250,13 +1250,13 @@ static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
 		struct folio *folio = ap->folios[i];
 
 		if (err) {

fs/fuse/file.c static ssize_t fuse_send_write_pages(struct fuse_io_args *ia, · line 1260

Quoted 10 lines
fs/fuse/file.c +2 -2
-			folio_clear_uptodate(folio);
+			iomap_folio_clear_uptodate(folio);
 		} else {
 			if (count >= folio_size(folio) - offset)
 				count -= folio_size(folio) - offset;
 			else {
 				if (short_write)
-					folio_clear_uptodate(folio);
+					iomap_folio_clear_uptodate(folio);
 				count = 0;

I believe this to be a bug. I talked about this a bit as LSFMM, but the fundamental problem is that the folio uptodate flag DOES NOT BELONG TO THE FILESYSTEM (unlike the buffer_head uptodate flag, which definitely does).

The MM has a number of sanity checks that make sure we haven't done something awful like mapping a !uptodate folio into userspace. That would potentially leak confidential information to userspace. So when a filesystem clears the uptodate flag, the MM can come along and bring down the whole machine because the mapped folio is now !uptodate.

I know we have a terrible writeback error handling story. But I think the right thing to do is just take out the calls to clear uptodate. In all filesystems. Darrick took it out of iomap in e9c3a8e820ed but it's time to purge it everywhere.

#4 lore raw

Re: [PATCH v1 2/2] fuse: use iomap helpers to update folio uptodate state

Quoted 10 lines · Matthew Wilcox

I believe this to be a bug. I talked about this a bit as LSFMM, but the fundamental problem is that the folio uptodate flag DOES NOT BELONG TO THE FILESYSTEM (unlike the buffer_head uptodate flag, which definitely does).

The MM has a number of sanity checks that make sure we haven't done something awful like mapping a !uptodate folio into userspace. That would potentially leak confidential information to userspace. So when a filesystem clears the uptodate flag, the MM can come along and bring down the whole machine because the mapped folio is now !uptodate.

Quoted 4 lines

I know we have a terrible writeback error handling story. But I think the right thing to do is just take out the calls to clear uptodate. In all filesystems. Darrick took it out of iomap in e9c3a8e820ed but it's time to purge it everywhere.

Yeah. We're not going to fix everything at once, but we should slowly move to stop clearing the folio uptodate bit. And I suspect the same is trye for the sub-folio uptodate bits.

#5 lore raw

Re: [PATCH v1 2/2] fuse: use iomap helpers to update folio uptodate state

Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Matthew Wilcox

I believe this to be a bug. I talked about this a bit as LSFMM, but the fundamental problem is that the folio uptodate flag DOES NOT BELONG TO THE FILESYSTEM (unlike the buffer_head uptodate flag, which definitely does).

The MM has a number of sanity checks that make sure we haven't done something awful like mapping a !uptodate folio into userspace. That would potentially leak confidential information to userspace. So when a filesystem clears the uptodate flag, the MM can come along and bring down the whole machine because the mapped folio is now !uptodate.

Quoted 4 lines
Quoted 4 lines

I know we have a terrible writeback error handling story. But I think the right thing to do is just take out the calls to clear uptodate. In all filesystems. Darrick took it out of iomap in e9c3a8e820ed but it's time to purge it everywhere.

Sounds good, thanks for looking at this. I'll send a patch to remove this in fuse.

Thanks,
Joanne