This is a note to let you know that I've just added the patch titled

    filemap: add a kiocb_invalidate_post_direct_write helper

to the 6.1-stable tree which can be found at:
    
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     filemap-add-a-kiocb_invalidate_post_direct_write-helper.patch
and it can be found in the queue-6.1 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <[email protected]> know about it.


>From [email protected] Tue Oct 21 16:13:23 
>2025
From: Mahmoud Adam <[email protected]>
Date: Tue, 21 Oct 2025 09:03:37 +0200
Subject: filemap: add a kiocb_invalidate_post_direct_write helper
To: <[email protected]>
Cc: <[email protected]>, <[email protected]>, Christoph Hellwig 
<[email protected]>, Damien Le Moal <[email protected]>, Hannes Reinecke 
<[email protected]>, "Darrick J. Wong" <[email protected]>, Al Viro 
<[email protected]>, Andreas Gruenbacher <[email protected]>, "Anna 
Schumaker" <[email protected]>, Chao Yu <[email protected]>, Christian Brauner 
<[email protected]>, Ilya Dryomov <[email protected]>, Jaegeuk Kim 
<[email protected]>, Jens Axboe <[email protected]>, Johannes Thumshirn 
<[email protected]>, Matthew Wilcox <[email protected]>, "Miklos 
Szeredi" <[email protected]>, Miklos Szeredi <[email protected]>, "Theodore 
Ts'o" <[email protected]>, Trond Myklebust <[email protected]>, Xiubo 
Li <[email protected]>, Andrew Morton <[email protected]>, "Jeff 
Layton" <[email protected]>, Andreas Dilger <[email protected]>, 
Christoph Hellwig <[email protected]>, Ryusuke Konishi 
<[email protected]>, Luis Chamberlain <[email protected]>,
  <[email protected]>, <[email protected]>, 
<[email protected]>, <[email protected]>, 
<[email protected]>, <[email protected]>, 
<[email protected]>, <[email protected]>, 
<[email protected]>, <[email protected]>
Message-ID: <[email protected]>

From: Christoph Hellwig <[email protected]>

commit c402a9a9430b670926decbb284b756ee6f47c1ec upstream.

Add a helper to invalidate page cache after a dio write.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Christoph Hellwig <[email protected]>
Reviewed-by: Damien Le Moal <[email protected]>
Reviewed-by: Hannes Reinecke <[email protected]>
Acked-by: Darrick J. Wong <[email protected]>
Cc: Al Viro <[email protected]>
Cc: Andreas Gruenbacher <[email protected]>
Cc: Anna Schumaker <[email protected]>
Cc: Chao Yu <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Ilya Dryomov <[email protected]>
Cc: Jaegeuk Kim <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Johannes Thumshirn <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Miklos Szeredi <[email protected]>
Cc: Miklos Szeredi <[email protected]>
Cc: Theodore Ts'o <[email protected]>
Cc: Trond Myklebust <[email protected]>
Cc: Xiubo Li <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Mahmoud Adam <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
---
 fs/direct-io.c          |   10 ++--------
 fs/iomap/direct-io.c    |   12 ++----------
 include/linux/fs.h      |    5 -----
 include/linux/pagemap.h |    1 +
 mm/filemap.c            |   37 ++++++++++++++++++++-----------------
 5 files changed, 25 insertions(+), 40 deletions(-)

--- a/fs/direct-io.c
+++ b/fs/direct-io.c
@@ -286,14 +286,8 @@ static ssize_t dio_complete(struct dio *
         * zeros from unwritten extents.
         */
        if (flags & DIO_COMPLETE_INVALIDATE &&
-           ret > 0 && dio_op == REQ_OP_WRITE &&
-           dio->inode->i_mapping->nrpages) {
-               err = invalidate_inode_pages2_range(dio->inode->i_mapping,
-                                       offset >> PAGE_SHIFT,
-                                       (offset + ret - 1) >> PAGE_SHIFT);
-               if (err)
-                       dio_warn_stale_pagecache(dio->iocb->ki_filp);
-       }
+           ret > 0 && dio_op == REQ_OP_WRITE)
+               kiocb_invalidate_post_direct_write(dio->iocb, ret);
 
        inode_dio_end(dio->inode);
 
--- a/fs/iomap/direct-io.c
+++ b/fs/iomap/direct-io.c
@@ -81,7 +81,6 @@ ssize_t iomap_dio_complete(struct iomap_
 {
        const struct iomap_dio_ops *dops = dio->dops;
        struct kiocb *iocb = dio->iocb;
-       struct inode *inode = file_inode(iocb->ki_filp);
        loff_t offset = iocb->ki_pos;
        ssize_t ret = dio->error;
 
@@ -108,15 +107,8 @@ ssize_t iomap_dio_complete(struct iomap_
         * ->end_io() when necessary, otherwise a racing buffer read would cache
         * zeros from unwritten extents.
         */
-       if (!dio->error && dio->size &&
-           (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
-               int err;
-               err = invalidate_inode_pages2_range(inode->i_mapping,
-                               offset >> PAGE_SHIFT,
-                               (offset + dio->size - 1) >> PAGE_SHIFT);
-               if (err)
-                       dio_warn_stale_pagecache(iocb->ki_filp);
-       }
+       if (!dio->error && dio->size && (dio->flags & IOMAP_DIO_WRITE))
+               kiocb_invalidate_post_direct_write(iocb, dio->size);
 
        inode_dio_end(file_inode(iocb->ki_filp));
        if (ret > 0) {
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3371,11 +3371,6 @@ static inline void inode_dio_end(struct
                wake_up_bit(&inode->i_state, __I_DIO_WAKEUP);
 }
 
-/*
- * Warn about a page cache invalidation failure diring a direct I/O write.
- */
-void dio_warn_stale_pagecache(struct file *filp);
-
 extern void inode_set_flags(struct inode *inode, unsigned int flags,
                            unsigned int mask);
 
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -31,6 +31,7 @@ int invalidate_inode_pages2(struct addre
 int invalidate_inode_pages2_range(struct address_space *mapping,
                pgoff_t start, pgoff_t end);
 int kiocb_invalidate_pages(struct kiocb *iocb, size_t count);
+void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count);
 int write_inode_now(struct inode *, int sync);
 int filemap_fdatawrite(struct address_space *);
 int filemap_flush(struct address_space *);
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3733,7 +3733,7 @@ EXPORT_SYMBOL(read_cache_page_gfp);
 /*
  * Warn about a page cache invalidation failure during a direct I/O write.
  */
-void dio_warn_stale_pagecache(struct file *filp)
+static void dio_warn_stale_pagecache(struct file *filp)
 {
        static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST);
        char pathname[128];
@@ -3750,19 +3750,23 @@ void dio_warn_stale_pagecache(struct fil
        }
 }
 
+void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count)
+{
+       struct address_space *mapping = iocb->ki_filp->f_mapping;
+
+       if (mapping->nrpages &&
+           invalidate_inode_pages2_range(mapping,
+                       iocb->ki_pos >> PAGE_SHIFT,
+                       (iocb->ki_pos + count - 1) >> PAGE_SHIFT))
+               dio_warn_stale_pagecache(iocb->ki_filp);
+}
+
 ssize_t
 generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
 {
-       struct file     *file = iocb->ki_filp;
-       struct address_space *mapping = file->f_mapping;
-       struct inode    *inode = mapping->host;
-       loff_t          pos = iocb->ki_pos;
-       ssize_t         written;
-       size_t          write_len;
-       pgoff_t         end;
-
-       write_len = iov_iter_count(from);
-       end = (pos + write_len - 1) >> PAGE_SHIFT;
+       struct address_space *mapping = iocb->ki_filp->f_mapping;
+       size_t write_len = iov_iter_count(from);
+       ssize_t written;
 
        /*
         * If a page can not be invalidated, return 0 to fall back
@@ -3772,7 +3776,7 @@ generic_file_direct_write(struct kiocb *
        if (written) {
                if (written == -EBUSY)
                        return 0;
-               goto out;
+               return written;
        }
 
        written = mapping->a_ops->direct_IO(iocb, from);
@@ -3794,11 +3798,11 @@ generic_file_direct_write(struct kiocb *
         *
         * Skip invalidation for async writes or if mapping has no pages.
         */
-       if (written > 0 && mapping->nrpages &&
-           invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, end))
-               dio_warn_stale_pagecache(file);
-
        if (written > 0) {
+               struct inode *inode = mapping->host;
+               loff_t pos = iocb->ki_pos;
+
+               kiocb_invalidate_post_direct_write(iocb, written);
                pos += written;
                write_len -= written;
                if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
@@ -3809,7 +3813,6 @@ generic_file_direct_write(struct kiocb *
        }
        if (written != -EIOCBQUEUED)
                iov_iter_revert(from, write_len - iov_iter_count(from));
-out:
        return written;
 }
 EXPORT_SYMBOL(generic_file_direct_write);


Patches currently in stable-queue which might be from [email protected] are

queue-6.1/block-fix-race-between-set_blocksize-and-read-paths.patch
queue-6.1/filemap-add-a-kiocb_invalidate_pages-helper.patch
queue-6.1/fs-factor-out-a-direct_write_fallback-helper.patch
queue-6.1/direct_write_fallback-on-error-revert-the-ki_pos-update-from-buffered-write.patch
queue-6.1/filemap-update-ki_pos-in-generic_perform_write.patch
queue-6.1/filemap-add-a-kiocb_invalidate_post_direct_write-helper.patch
queue-6.1/nilfs2-fix-deadlock-warnings-caused-by-lock-dependency-in-init_nilfs.patch
queue-6.1/block-open-code-__generic_file_write_iter-for-blkdev-writes.patch


_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Reply via email to