Skip to contentSkip to conversation

fuse: invalidate page cache pages before direct write

RFC PATCH In review Hao Xu 2 participants 6 messages May 9, 2023 – Jun 29, 2023

Subsystems (2)
lore
Share & tools
lore

Hao Xu

author
#0 lore raw

RFC PATCH 1/1 fuse: invalidate page cache pages before direct write

From: Hao Xu <[email protected]>

In FOPEN_DIRECT_IO, page cache may still be there for a file, direct write should respect that and invalidate the corresponding pages so that page cache readers don't get stale data. Another thing this patch does is flush related pages to avoid its loss.

---

Reference:
https://lore.kernel.org/linux-fsdevel/[email protected]/#:~:text=I%20think%20this%20problem%20exists%20before%20this%20patchset

 fs/fuse/file.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
fs/fuse/file.c +13 -1
index 89d97f6188e0..edc84c1dfc5c 100644
@@ -1490,7 +1490,8 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
 	int write = flags & FUSE_DIO_WRITE;
 	int cuse = flags & FUSE_DIO_CUSE;
 	struct file *file = io->iocb->ki_filp;
-	struct inode *inode = file->f_mapping->host;
+	struct address_space *mapping = file->f_mapping;
+	struct inode *inode = mapping->host;
 	struct fuse_file *ff = file->private_data;
 	struct fuse_conn *fc = ff->fm->fc;
 	size_t nmax = write ? fc->max_write : fc->max_read;
@@ -1516,6 +1517,17 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
 			inode_unlock(inode);
 	}
 
+	res = filemap_write_and_wait_range(mapping, pos, pos + count - 1);
+	if (res)
+		return res;
+
+	if (write) {
+		if (invalidate_inode_pages2_range(mapping,
+				idx_from, idx_to)) {
+			return -ENOTBLK;
+		}
+	}
+
 	io->should_dirty = !write && user_backed_iter(iter);
 	while (count) {
 		ssize_t nres;
Signature · 1 line
2.25.1
author
#1 lore raw
Quoted 31 lines · Hao Xu

From: Hao Xu <[email protected]>

In FOPEN_DIRECT_IO, page cache may still be there for a file, direct write should respect that and invalidate the corresponding pages so that page cache readers don't get stale data. Another thing this patch does is flush related pages to avoid its loss.

---

Reference:
https://lore.kernel.org/linux-fsdevel/[email protected]/#:~:text=I%20think%20this%20problem%20exists%20before%20this%20patchset

  fs/fuse/file.c | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
fs/fuse/file.c +2 -1
index 89d97f6188e0..edc84c1dfc5c 100644
@@ -1490,7 +1490,8 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
  	int write = flags & FUSE_DIO_WRITE;
  	int cuse = flags & FUSE_DIO_CUSE;
  	struct file *file = io->iocb->ki_filp;
-	struct inode *inode = file->f_mapping->host;
+	struct address_space *mapping = file->f_mapping;
+	struct inode *inode = mapping->host;
  	struct fuse_file *ff = file->private_data;
  	struct fuse_conn *fc = ff->fm->fc;
  	size_t nmax = write ? fc->max_write : fc->max_read;

fs/fuse/file.c ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, · line 1520

Quoted 4 lines
fs/fuse/file.c +1 -0
@@ -1516,6 +1517,17 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
  			inode_unlock(inode);
  	}
 
+	res = filemap_write_and_wait_range(mapping, pos, pos + count - 1);

Seems We don't need to flush dirty page if the page is only private mmaped, because the pages are always clean. I'll fix this in v2.

Quoted 13 lines
fs/fuse/file.c +10 -0
+	if (res)
+		return res;
+
+	if (write) {
+		if (invalidate_inode_pages2_range(mapping,
+				idx_from, idx_to)) {
+			return -ENOTBLK;
+		}
+	}
+
  	io->should_dirty = !write && user_backed_iter(iter);
  	while (count) {
  		ssize_t nres;
author
#2 lore raw
In reply to Hao Xu

Ping...

Quoted 48 lines · Hao Xu

From: Hao Xu <[email protected]>

In FOPEN_DIRECT_IO, page cache may still be there for a file, direct write should respect that and invalidate the corresponding pages so that page cache readers don't get stale data. Another thing this patch does is flush related pages to avoid its loss.

---

Reference:
https://lore.kernel.org/linux-fsdevel/[email protected]/#:~:text=I%20think%20this%20problem%20exists%20before%20this%20patchset

  fs/fuse/file.c | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
fs/fuse/file.c +13 -1
index 89d97f6188e0..edc84c1dfc5c 100644
@@ -1490,7 +1490,8 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
  	int write = flags & FUSE_DIO_WRITE;
  	int cuse = flags & FUSE_DIO_CUSE;
  	struct file *file = io->iocb->ki_filp;
-	struct inode *inode = file->f_mapping->host;
+	struct address_space *mapping = file->f_mapping;
+	struct inode *inode = mapping->host;
  	struct fuse_file *ff = file->private_data;
  	struct fuse_conn *fc = ff->fm->fc;
  	size_t nmax = write ? fc->max_write : fc->max_read;
@@ -1516,6 +1517,17 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
  			inode_unlock(inode);
  	}
 
+	res = filemap_write_and_wait_range(mapping, pos, pos + count - 1);
+	if (res)
+		return res;
+
+	if (write) {
+		if (invalidate_inode_pages2_range(mapping,
+				idx_from, idx_to)) {
+			return -ENOTBLK;
+		}
+	}
+
  	io->should_dirty = !write && user_backed_iter(iter);
  	while (count) {
  		ssize_t nres;
#3 lore raw
Quoted 44 lines · Hao Xu

Ping...

Quoted 41 lines · Hao Xu

From: Hao Xu <[email protected]>

In FOPEN_DIRECT_IO, page cache may still be there for a file, direct write should respect that and invalidate the corresponding pages so that page cache readers don't get stale data. Another thing this patch does is flush related pages to avoid its loss.

---

Reference:
https://lore.kernel.org/linux-fsdevel/[email protected]/#:~:text=I%20think%20this%20problem%20exists%20before%20this%20patchset

  fs/fuse/file.c | 14 +++++++++++++-

  1 file changed, 13 insertions(+), 1 deletion(-)
fs/fuse/file.c +2 -1
index 89d97f6188e0..edc84c1dfc5c 100644
@@ -1490,7 +1490,8 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, 
 struct iov_iter *iter,
       int write = flags & FUSE_DIO_WRITE;
       int cuse = flags & FUSE_DIO_CUSE;
       struct file *file = io->iocb->ki_filp;
-    struct inode *inode = file->f_mapping->host;
+    struct address_space *mapping = file->f_mapping;
+    struct inode *inode = mapping->host;
       struct fuse_file *ff = file->private_data;
       struct fuse_conn *fc = ff->fm->fc;

      size_t nmax = write ? fc->max_write : fc->max_read;

fs/fuse/file.c +6 -0
@@ -1516,6 +1517,17 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, 
 struct iov_iter *iter,
               inode_unlock(inode);
       }
+    res = filemap_write_and_wait_range(mapping, pos, pos + count - 1);
+    if (res)
+        return res;
+
+    if (write) {
+        if (invalidate_inode_pages2_range(mapping,
Quoted 9 lines

fs/fuse/file.c ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, · line 1533

Quoted 8 lines
fs/fuse/file.c +5 -0
+                idx_from, idx_to)) {
+            return -ENOTBLK;
+        }
+    }
+
       io->should_dirty = !write && user_backed_iter(iter);
       while (count) {
           ssize_t nres;

Is this part not working?

	if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
		if (!write)
			inode_lock(inode);
		fuse_sync_writes(inode);
		if (!write)
			inode_unlock(inode);
	}

Thanks,
Bernd

author
#4 lore raw

Hi Bernd,

Quoted 59 lines · Bernd Schubert
Quoted 44 lines · Hao Xu

Ping...

Quoted 41 lines · Hao Xu

From: Hao Xu <[email protected]>

In FOPEN_DIRECT_IO, page cache may still be there for a file, direct write should respect that and invalidate the corresponding pages so that page cache readers don't get stale data. Another thing this patch does is flush related pages to avoid its loss.

---

Reference:
https://lore.kernel.org/linux-fsdevel/[email protected]/#:~:text=I%20think%20this%20problem%20exists%20before%20this%20patchset

  fs/fuse/file.c | 14 +++++++++++++-

  1 file changed, 13 insertions(+), 1 deletion(-)
fs/fuse/file.c +2 -1
index 89d97f6188e0..edc84c1dfc5c 100644
@@ -1490,7 +1490,8 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, 
 struct iov_iter *iter,
       int write = flags & FUSE_DIO_WRITE;
       int cuse = flags & FUSE_DIO_CUSE;
       struct file *file = io->iocb->ki_filp;
-    struct inode *inode = file->f_mapping->host;
+    struct address_space *mapping = file->f_mapping;
+    struct inode *inode = mapping->host;
       struct fuse_file *ff = file->private_data;
       struct fuse_conn *fc = ff->fm->fc;

      size_t nmax = write ? fc->max_write : fc->max_read;

fs/fuse/file.c +6 -0
@@ -1516,6 +1517,17 @@ ssize_t fuse_direct_io(struct fuse_io_priv 
 *io, struct iov_iter *iter,
               inode_unlock(inode);
       }
+    res = filemap_write_and_wait_range(mapping, pos, pos + count - 1);
+    if (res)
+        return res;
+
+    if (write) {
+        if (invalidate_inode_pages2_range(mapping,
Quoted 9 lines

fs/fuse/file.c ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, · line 1533

Quoted 8 lines
fs/fuse/file.c +5 -0
+                idx_from, idx_to)) {
+            return -ENOTBLK;
+        }
+    }
+
       io->should_dirty = !write && user_backed_iter(iter);
       while (count) {
           ssize_t nres;

Is this part not working?

Quoted 9 lines

    if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) { if (!write)
            inode_lock(inode);
        fuse_sync_writes(inode);
        if (!write)
            inode_unlock(inode);
    }

This code seems to be waiting for already triggered page cache writeback requests, it's not related with the issue this patch tries to address. The issue here is we should invalidate related page cache page before we do direct write.

Regards,
Hao

Quoted 3 lines

Thanks,
Bernd

#5 lore raw

Hi Hao,

Quoted 73 lines · Hao Xu

Hi Bernd,

Quoted 59 lines · Bernd Schubert
Quoted 44 lines · Hao Xu

Ping...

Quoted 41 lines · Hao Xu

From: Hao Xu <[email protected]>

In FOPEN_DIRECT_IO, page cache may still be there for a file, direct write should respect that and invalidate the corresponding pages so that page cache readers don't get stale data. Another thing this patch does is flush related pages to avoid its loss.

---

Reference:
https://lore.kernel.org/linux-fsdevel/[email protected]/#:~:text=I%20think%20this%20problem%20exists%20before%20this%20patchset

  fs/fuse/file.c | 14 +++++++++++++-

  1 file changed, 13 insertions(+), 1 deletion(-)
fs/fuse/file.c +2 -1
index 89d97f6188e0..edc84c1dfc5c 100644
@@ -1490,7 +1490,8 @@ ssize_t fuse_direct_io(struct fuse_io_priv 
 *io, struct iov_iter *iter,
       int write = flags & FUSE_DIO_WRITE;
       int cuse = flags & FUSE_DIO_CUSE;
       struct file *file = io->iocb->ki_filp;
-    struct inode *inode = file->f_mapping->host;
+    struct address_space *mapping = file->f_mapping;
+    struct inode *inode = mapping->host;
       struct fuse_file *ff = file->private_data;
       struct fuse_conn *fc = ff->fm->fc;

      size_t nmax = write ? fc->max_write : fc->max_read;

fs/fuse/file.c +6 -0
@@ -1516,6 +1517,17 @@ ssize_t fuse_direct_io(struct fuse_io_priv 
 *io, struct iov_iter *iter,
               inode_unlock(inode);
       }
+    res = filemap_write_and_wait_range(mapping, pos, pos + count - 1);
+    if (res)
+        return res;
+
+    if (write) {
+        if (invalidate_inode_pages2_range(mapping,
Quoted 9 lines

fs/fuse/file.c ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, · line 1533

Quoted 8 lines
fs/fuse/file.c +5 -0
+                idx_from, idx_to)) {
+            return -ENOTBLK;
+        }
+    }
+
       io->should_dirty = !write && user_backed_iter(iter);
       while (count) {
           ssize_t nres;

Is this part not working?

Quoted 9 lines

     if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) { if (!write)
             inode_lock(inode);
         fuse_sync_writes(inode);
         if (!write)
             inode_unlock(inode);
     }

Quoted 4 lines

This code seems to be waiting for already triggered page cache writeback requests, it's not related with the issue this patch tries to address. The issue here is we should invalidate related page cache page before we do direct write.

oh, right, I just see it. I think you should move your filemap_write_and_wait_range() call above that piece in order to ensure it is send to the daemon/server side.

Thanks,
Bernd