lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251021070353.96705-3-mngyadam@amazon.de>
Date: Tue, 21 Oct 2025 09:03:36 +0200
From: Mahmoud Adam <mngyadam@...zon.de>
To: <stable@...r.kernel.org>
CC: <gregkh@...uxfoundation.org>, <nagy@...aternagy.com>, Christoph Hellwig
	<hch@....de>, Damien Le Moal <dlemoal@...nel.org>, Hannes Reinecke
	<hare@...e.de>, "Darrick J. Wong" <djwong@...nel.org>, Al Viro
	<viro@...iv.linux.org.uk>, Andreas Gruenbacher <agruenba@...hat.com>, "Anna
 Schumaker" <anna@...nel.org>, Chao Yu <chao@...nel.org>, Christian Brauner
	<brauner@...nel.org>, Ilya Dryomov <idryomov@...il.com>, Jaegeuk Kim
	<jaegeuk@...nel.org>, Jens Axboe <axboe@...nel.dk>, Johannes Thumshirn
	<johannes.thumshirn@....com>, Matthew Wilcox <willy@...radead.org>, "Miklos
 Szeredi" <miklos@...redi.hu>, Miklos Szeredi <mszeredi@...hat.com>, "Theodore
 Ts'o" <tytso@....edu>, Trond Myklebust <trond.myklebust@...merspace.com>,
	Xiubo Li <xiubli@...hat.com>, Andrew Morton <akpm@...ux-foundation.org>,
	"Jeff Layton" <jlayton@...nel.org>, Andreas Dilger
	<adilger.kernel@...ger.ca>, Christoph Hellwig <hch@...radead.org>, Ryusuke
 Konishi <konishi.ryusuke@...il.com>, Luis Chamberlain <mcgrof@...nel.org>,
	<linux-block@...r.kernel.org>, <linux-kernel@...r.kernel.org>,
	<ceph-devel@...r.kernel.org>, <linux-fsdevel@...r.kernel.org>,
	<linux-ext4@...r.kernel.org>, <linux-f2fs-devel@...ts.sourceforge.net>,
	<linux-xfs@...r.kernel.org>, <linux-nfs@...r.kernel.org>,
	<linux-nilfs@...r.kernel.org>, <linux-mm@...ck.org>
Subject: [PATCH 6.1 1/8] filemap: add a kiocb_invalidate_pages helper

From: Christoph Hellwig <hch@....de>

commit e003f74afbd2feadbb9ffbf9135e2d2fb5d320a5 upstream.

Factor out a helper that calls filemap_write_and_wait_range and
invalidate_inode_pages2_range for the range covered by a write kiocb or
returns -EAGAIN if the kiocb is marked as nowait and there would be pages
to write or invalidate.

Link: https://lkml.kernel.org/r/20230601145904.1385409-6-hch@lst.de
Signed-off-by: Christoph Hellwig <hch@....de>
Reviewed-by: Damien Le Moal <dlemoal@...nel.org>
Reviewed-by: Hannes Reinecke <hare@...e.de>
Acked-by: Darrick J. Wong <djwong@...nel.org>
Cc: Al Viro <viro@...iv.linux.org.uk>
Cc: Andreas Gruenbacher <agruenba@...hat.com>
Cc: Anna Schumaker <anna@...nel.org>
Cc: Chao Yu <chao@...nel.org>
Cc: Christian Brauner <brauner@...nel.org>
Cc: Ilya Dryomov <idryomov@...il.com>
Cc: Jaegeuk Kim <jaegeuk@...nel.org>
Cc: Jens Axboe <axboe@...nel.dk>
Cc: Johannes Thumshirn <johannes.thumshirn@....com>
Cc: Matthew Wilcox <willy@...radead.org>
Cc: Miklos Szeredi <miklos@...redi.hu>
Cc: Miklos Szeredi <mszeredi@...hat.com>
Cc: Theodore Ts'o <tytso@....edu>
Cc: Trond Myklebust <trond.myklebust@...merspace.com>
Cc: Xiubo Li <xiubli@...hat.com>
Signed-off-by: Andrew Morton <akpm@...ux-foundation.org>
Signed-off-by: Mahmoud Adam <mngyadam@...zon.de>
---
 include/linux/pagemap.h |  1 +
 mm/filemap.c            | 48 ++++++++++++++++++++++++-----------------
 2 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 1be5a1fa6a3a84..bb462e5a91e28d 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -30,6 +30,7 @@ static inline void invalidate_remote_inode(struct inode *inode)
 int invalidate_inode_pages2(struct address_space *mapping);
 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);
 int write_inode_now(struct inode *, int sync);
 int filemap_fdatawrite(struct address_space *);
 int filemap_flush(struct address_space *);
diff --git a/mm/filemap.c b/mm/filemap.c
index 2ae6c6146d848a..0923b8df285886 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2839,6 +2839,33 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
 }
 EXPORT_SYMBOL_GPL(filemap_read);
 
+int kiocb_invalidate_pages(struct kiocb *iocb, size_t count)
+{
+	struct address_space *mapping = iocb->ki_filp->f_mapping;
+	loff_t pos = iocb->ki_pos;
+	loff_t end = pos + count - 1;
+	int ret;
+
+	if (iocb->ki_flags & IOCB_NOWAIT) {
+		/* we could block if there are any pages in the range */
+		if (filemap_range_has_page(mapping, pos, end))
+			return -EAGAIN;
+	} else {
+		ret = filemap_write_and_wait_range(mapping, pos, end);
+		if (ret)
+			return ret;
+	}
+
+	/*
+	 * After a write we want buffered reads to be sure to go to disk to get
+	 * the new data.  We invalidate clean cached page from the region we're
+	 * about to write.  We do this *before* the write so that we can return
+	 * without clobbering -EIOCBQUEUED from ->direct_IO().
+	 */
+	return invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT,
+					     end >> PAGE_SHIFT);
+}
+
 /**
  * generic_file_read_iter - generic filesystem read routine
  * @iocb:	kernel I/O control block
@@ -3737,30 +3764,11 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
 	write_len = iov_iter_count(from);
 	end = (pos + write_len - 1) >> PAGE_SHIFT;
 
-	if (iocb->ki_flags & IOCB_NOWAIT) {
-		/* If there are pages to writeback, return */
-		if (filemap_range_has_page(file->f_mapping, pos,
-					   pos + write_len - 1))
-			return -EAGAIN;
-	} else {
-		written = filemap_write_and_wait_range(mapping, pos,
-							pos + write_len - 1);
-		if (written)
-			goto out;
-	}
-
-	/*
-	 * After a write we want buffered reads to be sure to go to disk to get
-	 * the new data.  We invalidate clean cached page from the region we're
-	 * about to write.  We do this *before* the write so that we can return
-	 * without clobbering -EIOCBQUEUED from ->direct_IO().
-	 */
-	written = invalidate_inode_pages2_range(mapping,
-					pos >> PAGE_SHIFT, end);
 	/*
 	 * If a page can not be invalidated, return 0 to fall back
 	 * to buffered write.
 	 */
+	written = kiocb_invalidate_pages(iocb, write_len);
 	if (written) {
 		if (written == -EBUSY)
 			return 0;
-- 
2.47.3




Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christian Schlaeger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ