Skip to contentSkip to conversation

mm/filemap: split out folio wait and VFS code

RFC PATCH In review Tal Zussman 4 participants 18 messages May 20 – Jun 1

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

cover mm/filemap: split out folio wait and VFS code

mm/filemap.c has accumulated additional infrastructure over the years that is not directly related to the page cache. It is currently nearly 5000 lines long. This series splits out the folio bit-lock and wait queue code into separate files, and moves the VFS-level generic_file_{read,write}_iter() family of files to fs/read_write.c, in order to provide better separation of concerns. This also slims down mm/filemap.c by ~1000 lines.

The folio wait infrastructure is centralized in mm/folio_wait.c and include/linux/folio_wait.h, with functions moved from mm/filemap.c, mm/page-writeback.c, and include/linux/pagemap.h. Afterwards, the code is cleaned up a little, with functions and data types renamed to refer to folios rather than pages.

generic_file_{read,write}_iter() implement the VFS-level read/write path for filesystems, including support for direct I/O. These functions and their helpers are moved to fs/read_write.c, along with other VFS-level read/write functions. dir_pages() is also moved to include/linux/fs.h. i_blocks_per_folio() is not moved from include/linux/pagemap.h, as it requires folio_size(), which is not currently available in include/linux/fs.h.

No functional change is intended.

Note: I have additional cleanups to mm/filemap.c ready to go, foremost among them centralizing on the filemap_*() naming convention and making the exposed page cache API clearer and more consistent, but I've split these patches off from that in order to avoid sending these logically separate patches to ~60 maintainers.

---
Tal Zussman (11):
mm: add folio_wake_writeback() helper
folio_wait: move folio bit-lock and wait implementation to mm/folio_wait.c
folio_wait: move folio bit-lock and wait declarations to include/linux/folio_wait.h
folio_wait: move folio_wait_writeback() family to mm/folio_wait.c
folio_wait: reformat comments and fix alignment
folio_wait: rename wait_page_* infrastructure to wait_folio_*
folio_wait: convert VM_BUG_ON_FOLIO() to VM_WARN_ON_ONCE_FOLIO()
MAINTAINERS: add folio_wait files to MEMORY MANAGEMENT - CORE
fs: move dir_pages() from <linux/pagemap.h> to <linux/fs.h>
fs: move generic_file_read_iter() to fs/read_write.c
fs: move generic_file_write_iter() family to fs/read_write.c

 MAINTAINERS                |   2 +
 fs/read_write.c            | 358 ++++++++++++++++
 include/linux/folio_wait.h | 183 +++++++++
 include/linux/fs.h         |  19 +-
 include/linux/pagemap.h    | 184 +--------
 io_uring/rw.c              |  14 +-
 io_uring/rw.h              |   6 +-
 mm/Makefile                |   2 +-
 mm/filemap.c               | 993 +--------------------------------------------
 mm/folio_wait.c            | 710 ++++++++++++++++++++++++++++++++
 mm/internal.h              |   4 +
 mm/page-writeback.c        |  66 ---
 12 files changed, 1285 insertions(+), 1256 deletions(-)

---
base-commit: e9add7501ad3297dad9b90ce201266830a68ab47
change-id: 20260511-filemap-split-871b5c18e98c

Best regards,

Signature · 1 line
Tal Zussman <[email protected]>
#1 lore raw

RFC PATCH 1/11 mm: add folio_wake_writeback() helper

Add a folio_wake_writeback() wrapper for folio_wake_bit() for use in folio_end_writeback_no_dropbehind(), in preparation for moving the folio bit lock and wait queue code to a separate file.

No functional change.

---
 mm/filemap.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/filemap.c +9 -1
index 5aaba0d3e81d..567742fbaff0 100644
@@ -1209,6 +1209,14 @@ static void folio_wake_bit(struct folio *folio, int bit_nr)
 	spin_unlock_irqrestore(&q->lock, flags);
 }
 
+/*
+ * Wake waiters on PG_writeback for @folio.
+ */
+static void folio_wake_writeback(struct folio *folio)
+{
+	folio_wake_bit(folio, PG_writeback);
+}
+
 /*
  * A choice of three behaviors for folio_wait_bit_common():
  */
@@ -1664,7 +1672,7 @@ void folio_end_writeback_no_dropbehind(struct folio *folio)
 	}
 
 	if (__folio_end_writeback(folio))
-		folio_wake_bit(folio, PG_writeback);
+		folio_wake_writeback(folio);
 
 	acct_reclaim_writeback(folio);
 }
Signature · 1 line
2.39.5
#2 lore raw

RFC PATCH 2/11 folio_wait: move folio bit-lock and wait implementation to mm/folio_wait.c

In reply to Tal Zussman

mm/filemap.c contains ~600 lines of folio bit-lock and wait queue infrastructure that is logically separate from the page cache. Move it into a new file, mm/folio_wait.c.

folio_wake_writeback(), folio_put_wait_locked(), and __folio_lock_async() are made non-static and declared in mm/internal.h, as they are still used in filemap.c.

pagecache_init() is refactored to call folio_wait_init(), which initializes the wait queue table and page_lock_unfairness sysctl. filemap_sysctl_table is renamed to folio_wait_sysctl_table.

---
 mm/Makefile     |   2 +-
 mm/filemap.c    | 640 +-----------------------------------------------------
 mm/folio_wait.c | 662 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mm/internal.h   |   4 +
 4 files changed, 668 insertions(+), 640 deletions(-)
mm/Makefile +1 -1
index 8ad2ab08244e..65ce5afe7692 100644
@@ -52,7 +52,7 @@ obj-y			:= filemap.o mempool.o oom_kill.o fadvise.o \
 			   maccess.o page-writeback.o folio-compat.o \
 			   readahead.o swap.o truncate.o vmscan.o shrinker.o \
 			   shmem.o util.o mmzone.o vmstat.o backing-dev.o \
-			   mm_init.o percpu.o slab_common.o \
+			   mm_init.o percpu.o slab_common.o folio_wait.o \
 			   compaction.o show_mem.o \
 			   interval_tree.o list_lru.o workingset.o \
 			   debug.o gup.o mmap_lock.o vma_init.o $(mmu-y)
mm/filemap.c +1 -639

657 lines not on this page · raw message

mm/folio_wait.c new file +662 -0

662 lines not on this page · raw message

mm/internal.h +4 -0
index 09931b1e535f..a121ca07f75c 100644
@@ -102,6 +102,10 @@ struct pagetable_move_control {
 })
 
 void page_writeback_init(void);
+void folio_wait_init(void);
+void folio_wake_writeback(struct folio *folio);
+int folio_put_wait_locked(struct folio *folio, int state);
+int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait);
 
 /*
  * If a 16GB hugetlb folio were mapped by PTEs of all of its 4kB pages,
Signature · 1 line
2.39.5
#3 lore raw

RFC PATCH 3/11 folio_wait: move folio bit-lock and wait declarations to include/linux/folio_wait.h

In reply to Tal Zussman

Move ~150 lines of folio bit-lock and wait queue infrastructure from pagemap.h to folio_wait.h. pagemap.h includes the new header so existing users don't break.

---
 include/linux/folio_wait.h | 181 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/pagemap.h    | 172 +-----------------------------------------
 mm/folio_wait.c            |   2 +-
 3 files changed, 183 insertions(+), 172 deletions(-)
include/linux/folio_wait.h new file +181 -0
new file mode 100644
index 000000000000..80ddf1ffcae4
@@ -0,0 +1,181 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_FOLIO_WAIT_H
+#define _LINUX_FOLIO_WAIT_H
+
+#include <linux/bitops.h>
+#include <linux/page-flags.h>
+#include <linux/wait.h>
+
+struct wait_page_key {
+	struct folio *folio;
+	int bit_nr;
+	int page_match;
+};
+
+struct wait_page_queue {
+	struct folio *folio;
+	int bit_nr;
+	wait_queue_entry_t wait;
+};
+
+static inline bool wake_page_match(struct wait_page_queue *wait_page,
+				  struct wait_page_key *key)
+{
+	if (wait_page->folio != key->folio)
+	       return false;
+	key->page_match = 1;
+
+	if (wait_page->bit_nr != key->bit_nr)
+		return false;
+
+	return true;
+}
+
+void __folio_lock(struct folio *folio);
+int __folio_lock_killable(struct folio *folio);
+vm_fault_t __folio_lock_or_retry(struct folio *folio, struct vm_fault *vmf);
+void unlock_page(struct page *page);
+void folio_unlock(struct folio *folio);
+
+/**
+ * folio_trylock() - Attempt to lock a folio.
+ * @folio: The folio to attempt to lock.
+ *
+ * Sometimes it is undesirable to wait for a folio to be unlocked (eg
+ * when the locks are being taken in the wrong order, or if making
+ * progress through a batch of folios is more important than processing
+ * them in order).  Usually folio_lock() is the correct function to call.
+ *
+ * Context: Any context.
+ * Return: Whether the lock was successfully acquired.
+ */
+static inline bool folio_trylock(struct folio *folio)
+{
+	return likely(!test_and_set_bit_lock(PG_locked, folio_flags(folio, 0)));
+}
+
+/*
+ * Return true if the page was successfully locked
+ */
+static inline bool trylock_page(struct page *page)
+{
+	return folio_trylock(page_folio(page));
+}
+
+/**
+ * folio_lock() - Lock this folio.
+ * @folio: The folio to lock.
+ *
+ * The folio lock protects against many things, probably more than it
+ * should.  It is primarily held while a folio is being brought uptodate,
+ * either from its backing file or from swap.  It is also held while a
+ * folio is being truncated from its address_space, so holding the lock
+ * is sufficient to keep folio->mapping stable.
+ *
+ * The folio lock is also held while write() is modifying the page to
+ * provide POSIX atomicity guarantees (as long as the write does not
+ * cross a page boundary).  Other modifications to the data in the folio
+ * do not hold the folio lock and can race with writes, eg DMA and stores
+ * to mapped pages.
+ *
+ * Context: May sleep.  If you need to acquire the locks of two or
+ * more folios, they must be in order of ascending index, if they are
+ * in the same address_space.  If they are in different address_spaces,
+ * acquire the lock of the folio which belongs to the address_space which
+ * has the lowest address in memory first.
+ */
+static inline void folio_lock(struct folio *folio)
+{
+	might_sleep();
+	if (!folio_trylock(folio))
+		__folio_lock(folio);
+}
+
+/**
+ * lock_page() - Lock the folio containing this page.
+ * @page: The page to lock.
+ *
+ * See folio_lock() for a description of what the lock protects.
+ * This is a legacy function and new code should probably use folio_lock()
+ * instead.
+ *
+ * Context: May sleep.  Pages in the same folio share a lock, so do not
+ * attempt to lock two pages which share a folio.
+ */
+static inline void lock_page(struct page *page)
+{
+	struct folio *folio;
+	might_sleep();
+
+	folio = page_folio(page);
+	if (!folio_trylock(folio))
+		__folio_lock(folio);
+}
+
+/**
+ * folio_lock_killable() - Lock this folio, interruptible by a fatal signal.
+ * @folio: The folio to lock.
+ *
+ * Attempts to lock the folio, like folio_lock(), except that the sleep
+ * to acquire the lock is interruptible by a fatal signal.
+ *
+ * Context: May sleep; see folio_lock().
+ * Return: 0 if the lock was acquired; -EINTR if a fatal signal was received.
+ */
+static inline int folio_lock_killable(struct folio *folio)
+{
+	might_sleep();
+	if (!folio_trylock(folio))
+		return __folio_lock_killable(folio);
+	return 0;
+}
+
+/*
+ * folio_lock_or_retry - Lock the folio, unless this would block and the
+ * caller indicated that it can handle a retry.
+ *
+ * Return value and mmap_lock implications depend on flags; see
+ * __folio_lock_or_retry().
+ */
+static inline vm_fault_t folio_lock_or_retry(struct folio *folio,
+					     struct vm_fault *vmf)
+{
+	might_sleep();
+	if (!folio_trylock(folio))
+		return __folio_lock_or_retry(folio, vmf);
+	return 0;
+}
+
+/*
+ * This is exported only for folio_wait_locked/folio_wait_writeback, etc.,
+ * and should not be used directly.
+ */
+void folio_wait_bit(struct folio *folio, int bit_nr);
+int folio_wait_bit_killable(struct folio *folio, int bit_nr);
+
+/*
+ * Wait for a folio to be unlocked.
+ *
+ * This must be called with the caller "holding" the folio,
+ * ie with increased folio reference count so that the folio won't
+ * go away during the wait.
+ */
+static inline void folio_wait_locked(struct folio *folio)
+{
+	if (folio_test_locked(folio))
+		folio_wait_bit(folio, PG_locked);
+}
+
+static inline int folio_wait_locked_killable(struct folio *folio)
+{
+	if (!folio_test_locked(folio))
+		return 0;
+	return folio_wait_bit_killable(folio, PG_locked);
+}
+
+void folio_end_read(struct folio *folio, bool success);
+void folio_end_private_2(struct folio *folio);
+void folio_wait_private_2(struct folio *folio);
+int folio_wait_private_2_killable(struct folio *folio);
+
+#endif /* _LINUX_FOLIO_WAIT_H */
include/linux/pagemap.h +1 -171

190 lines not on this page · raw message

mm/folio_wait.c +1 -1
index 18b42488ce37..06156e138c09 100644
@@ -8,7 +8,7 @@
 #include <linux/mm.h>
 #include <linux/swap.h>
 #include <linux/leafops.h>
-#include <linux/pagemap.h>
+#include <linux/folio_wait.h>
 #include <linux/wait.h>
 #include <linux/hash.h>
 #include <linux/sysctl.h>
Signature · 1 line
2.39.5
#4 lore raw

RFC PATCH 4/11 folio_wait: move folio_wait_writeback() family to mm/folio_wait.c

In reply to Tal Zussman

folio_wait_writeback(), folio_wait_writeback_killable() and folio_wait_stable() are thin wrappers around folio_wait_bit() and folio_wait_bit_killable() on PG_writeback. Move them to mm/folio_wait.c, next to the rest of the folio bit-wait infrastructure.

The legacy wait_on_page_writeback() wrapper stays in folio-compat.c, as its days are numbered, and it will be deleted once the remaining callers are converted.

---
 include/linux/folio_wait.h |  4 +++
 include/linux/pagemap.h    |  3 ---
 mm/folio_wait.c            | 67 ++++++++++++++++++++++++++++++++++++++++++++++
 mm/page-writeback.c        | 66 ---------------------------------------------
 4 files changed, 71 insertions(+), 69 deletions(-)
include/linux/folio_wait.h +4 -0
index 80ddf1ffcae4..4a5cb2fcf046 100644
@@ -178,4 +178,8 @@ void folio_end_private_2(struct folio *folio);
 void folio_wait_private_2(struct folio *folio);
 int folio_wait_private_2_killable(struct folio *folio);
 
+void folio_wait_writeback(struct folio *folio);
+int folio_wait_writeback_killable(struct folio *folio);
+void folio_wait_stable(struct folio *folio);
+
 #endif /* _LINUX_FOLIO_WAIT_H */
include/linux/pagemap.h +0 -3
index 7f65c2b0097b..84ccb682cca8 100644
@@ -1074,13 +1074,10 @@ static inline pgoff_t linear_page_index(const struct vm_area_struct *vma,
 }
 
 void wait_on_page_writeback(struct page *page);
-void folio_wait_writeback(struct folio *folio);
-int folio_wait_writeback_killable(struct folio *folio);
 void end_page_writeback(struct page *page);
 void folio_end_writeback(struct folio *folio);
 void folio_end_writeback_no_dropbehind(struct folio *folio);
 void folio_end_dropbehind(struct folio *folio);
-void folio_wait_stable(struct folio *folio);
 void __folio_mark_dirty(struct folio *folio, struct address_space *, int warn);
 void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb);
 void __folio_cancel_dirty(struct folio *folio);
mm/folio_wait.c +67 -0

79 lines not on this page · raw message

mm/page-writeback.c +0 -66

69 lines not on this page · raw message

Signature · 1 line
2.39.5
#5 lore raw

RFC PATCH 5/11 folio_wait: reformat comments and fix alignment

In reply to Tal Zussman

Reflow comments to fill 80 columns and fix indentation issues carried over from the original locations in pagemap.h and filemap.c.

---
 include/linux/folio_wait.h |  62 ++++++++-------
 mm/folio_wait.c            | 185 ++++++++++++++++++++-------------------------
 2 files changed, 113 insertions(+), 134 deletions(-)
include/linux/folio_wait.h +30 -32
index 4a5cb2fcf046..57ccf9ffd243 100644
@@ -19,10 +19,10 @@ struct wait_page_queue {
 };
 
 static inline bool wake_page_match(struct wait_page_queue *wait_page,
-				  struct wait_page_key *key)
+		struct wait_page_key *key)
 {
 	if (wait_page->folio != key->folio)
-	       return false;
+		return false;
 	key->page_match = 1;
 
 	if (wait_page->bit_nr != key->bit_nr)
@@ -41,10 +41,10 @@ void folio_unlock(struct folio *folio);
  * folio_trylock() - Attempt to lock a folio.
  * @folio: The folio to attempt to lock.
  *
- * Sometimes it is undesirable to wait for a folio to be unlocked (eg
- * when the locks are being taken in the wrong order, or if making
- * progress through a batch of folios is more important than processing
- * them in order).  Usually folio_lock() is the correct function to call.
+ * Sometimes it is undesirable to wait for a folio to be unlocked (e.g. when
+ * the locks are being taken in the wrong order, or if making progress through
+ * a batch of folios is more important than processing them in order). Usually
+ * folio_lock() is the correct function to call.
  *
  * Context: Any context.
  * Return: Whether the lock was successfully acquired.
@@ -66,23 +66,22 @@ static inline bool trylock_page(struct page *page)
  * folio_lock() - Lock this folio.
  * @folio: The folio to lock.
  *
- * The folio lock protects against many things, probably more than it
- * should.  It is primarily held while a folio is being brought uptodate,
- * either from its backing file or from swap.  It is also held while a
- * folio is being truncated from its address_space, so holding the lock
- * is sufficient to keep folio->mapping stable.
+ * The folio lock protects against many things, probably more than it should.
+ * It is primarily held while a folio is being brought uptodate, either from
+ * its backing file or from swap. It is also held while a folio is being
+ * truncated from its address_space, so holding the lock is sufficient to keep
+ * folio->mapping stable.
  *
- * The folio lock is also held while write() is modifying the page to
- * provide POSIX atomicity guarantees (as long as the write does not
- * cross a page boundary).  Other modifications to the data in the folio
- * do not hold the folio lock and can race with writes, eg DMA and stores
- * to mapped pages.
+ * The folio lock is also held while write() is modifying the folio to provide
+ * POSIX atomicity guarantees (as long as the write does not cross a page
+ * boundary). Other modifications to the data in the folio do not hold the
+ * folio lock and can race with writes, e.g. DMA and stores to mapped pages.
  *
- * Context: May sleep.  If you need to acquire the locks of two or
- * more folios, they must be in order of ascending index, if they are
- * in the same address_space.  If they are in different address_spaces,
- * acquire the lock of the folio which belongs to the address_space which
- * has the lowest address in memory first.
+ * Context: May sleep. If you need to acquire the locks of two or more folios,
+ * they must be in order of ascending index, if they are in the same
+ * address_space. If they are in different address_spaces, acquire the lock of
+ * the folio which belongs to the address_space which has the lowest address in
+ * memory first.
  */
 static inline void folio_lock(struct folio *folio)
 {
@@ -99,8 +98,8 @@ static inline void folio_lock(struct folio *folio)
  * This is a legacy function and new code should probably use folio_lock()
  * instead.
  *
- * Context: May sleep.  Pages in the same folio share a lock, so do not
- * attempt to lock two pages which share a folio.
+ * Context: May sleep. Pages in the same folio share a lock, so do not attempt
+ * to lock two pages which share a folio.
  */
 static inline void lock_page(struct page *page)
 {
@@ -116,8 +115,8 @@ static inline void lock_page(struct page *page)
  * folio_lock_killable() - Lock this folio, interruptible by a fatal signal.
  * @folio: The folio to lock.
  *
- * Attempts to lock the folio, like folio_lock(), except that the sleep
- * to acquire the lock is interruptible by a fatal signal.
+ * Attempts to lock the folio, like folio_lock(), except that the sleep to
+ * acquire the lock is interruptible by a fatal signal.
  *
  * Context: May sleep; see folio_lock().
  * Return: 0 if the lock was acquired; -EINTR if a fatal signal was received.
@@ -131,8 +130,8 @@ static inline int folio_lock_killable(struct folio *folio)
 }
 
 /*
- * folio_lock_or_retry - Lock the folio, unless this would block and the
- * caller indicated that it can handle a retry.
+ * folio_lock_or_retry - Lock the folio, unless this would block and the caller
+ * indicated that it can handle a retry.
  *
  * Return value and mmap_lock implications depend on flags; see
  * __folio_lock_or_retry().
@@ -147,8 +146,8 @@ static inline vm_fault_t folio_lock_or_retry(struct folio *folio,
 }
 
 /*
- * This is exported only for folio_wait_locked/folio_wait_writeback, etc.,
- * and should not be used directly.
+ * This is exported only for folio_wait_locked/folio_wait_writeback, etc., and
+ * should not be used directly.
  */
 void folio_wait_bit(struct folio *folio, int bit_nr);
 int folio_wait_bit_killable(struct folio *folio, int bit_nr);
@@ -156,9 +155,8 @@ int folio_wait_bit_killable(struct folio *folio, int bit_nr);
 /*
  * Wait for a folio to be unlocked.
  *
- * This must be called with the caller "holding" the folio,
- * ie with increased folio reference count so that the folio won't
- * go away during the wait.
+ * This must be called with the caller "holding" the folio, i.e. with increased
+ * folio reference count so that the folio won't go away during the wait.
  */
 static inline void folio_wait_locked(struct folio *folio)
 {
mm/folio_wait.c +83 -102

319 lines not on this page · raw message

Signature · 1 line
2.39.5
#6 lore raw

RFC PATCH 6/11 folio_wait: rename wait_page_* infrastructure to wait_folio_*

In reply to Tal Zussman

The folio bit-lock wait infrastructure still refers to "page" in the names of its core types and helpers, even though it operates on folios. Rename accordingly:

  struct wait_page_key           -> struct wait_folio_key
  struct wait_page_queue         -> struct wait_folio_queue
  wait_page_key.page_match       -> wait_folio_key.folio_match
  wake_page_match()              -> wake_folio_match()
  wake_page_function()           -> wake_folio_function()
  PAGE_WAIT_TABLE_{BITS,SIZE}    -> FOLIO_WAIT_TABLE_{BITS,SIZE}

Also rename local variables and field names, such as io_uring's wpq -> wfq. Update relevant comments as well.

While at it, update io_uring/rw.h to include folio_wait.h rather than pagemap.h.

---
 include/linux/folio_wait.h | 16 +++++-----
 include/linux/fs.h         |  2 +-
 io_uring/rw.c              | 14 ++++-----
 io_uring/rw.h              |  6 ++--
 mm/folio_wait.c            | 74 +++++++++++++++++++++++-----------------------
 mm/internal.h              |  2 +-
 6 files changed, 57 insertions(+), 57 deletions(-)
include/linux/folio_wait.h +8 -8
index 57ccf9ffd243..1732df23d952 100644
@@ -6,26 +6,26 @@
 #include <linux/page-flags.h>
 #include <linux/wait.h>
 
-struct wait_page_key {
+struct wait_folio_key {
 	struct folio *folio;
 	int bit_nr;
-	int page_match;
+	int folio_match;
 };
 
-struct wait_page_queue {
+struct wait_folio_queue {
 	struct folio *folio;
 	int bit_nr;
 	wait_queue_entry_t wait;
 };
 
-static inline bool wake_page_match(struct wait_page_queue *wait_page,
-		struct wait_page_key *key)
+static inline bool wake_folio_match(struct wait_folio_queue *wait_folio,
+		struct wait_folio_key *key)
 {
-	if (wait_page->folio != key->folio)
+	if (wait_folio->folio != key->folio)
 		return false;
-	key->page_match = 1;
+	key->folio_match = 1;
 
-	if (wait_page->bit_nr != key->bit_nr)
+	if (wait_folio->bit_nr != key->bit_nr)
 		return false;
 
 	return true;
include/linux/fs.h +1 -1
index bb9cc4f7207c..cd5088dfe9a1 100644
@@ -390,7 +390,7 @@ struct kiocb {
 	 * waitqueue associated with completing the read.
 	 * Valid IFF IOCB_WAITQ is set.
 	 */
-	struct wait_page_queue	*ki_waitq;
+	struct wait_folio_queue	*ki_waitq;
 };
 
 static inline bool is_sync_kiocb(struct kiocb *kiocb)
io_uring/rw.c +7 -7
index 0c4834645279..fc87baac1911 100644
@@ -763,14 +763,14 @@ static ssize_t loop_rw_iter(int ddir, struct io_rw *rw, struct iov_iter *iter)
 static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
 			     int sync, void *arg)
 {
-	struct wait_page_queue *wpq;
+	struct wait_folio_queue *wfq;
 	struct io_kiocb *req = wait->private;
 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
-	struct wait_page_key *key = arg;
+	struct wait_folio_key *key = arg;
 
-	wpq = container_of(wait, struct wait_page_queue, wait);
+	wfq = container_of(wait, struct wait_folio_queue, wait);
 
-	if (!wake_page_match(wpq, key))
+	if (!wake_folio_match(wfq, key))
 		return 0;
 
 	rw->kiocb.ki_flags &= ~IOCB_WAITQ;
@@ -783,7 +783,7 @@ static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
  * This controls whether a given IO request should be armed for async page
  * based retry. If we return false here, the request is handed to the async
  * worker threads for retry. If we're doing buffered reads on a regular file,
- * we prepare a private wait_page_queue entry and retry the operation. This
+ * we prepare a private wait_folio_queue entry and retry the operation. This
  * will either succeed because the page is now uptodate and unlocked, or it
  * will register a callback when the page is unlocked at IO completion. Through
  * that callback, io_uring uses task_work to setup a retry of the operation.
@@ -794,7 +794,7 @@ static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
 static bool io_rw_should_retry(struct io_kiocb *req)
 {
 	struct io_async_rw *io = req->async_data;
-	struct wait_page_queue *wait = &io->wpq;
+	struct wait_folio_queue *wait = &io->wfq;
 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
 	struct kiocb *kiocb = &rw->kiocb;
 
@@ -897,7 +897,7 @@ static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type)
 			return -EINVAL;
 
 		/*
-		 * We have a union of meta fields with wpq used for buffered-io
+		 * We have a union of meta fields with wfq used for buffered-io
 		 * in io_async_rw, so fail it here.
 		 */
 		if (!(file->f_flags & O_DIRECT))
io_uring/rw.h +3 -3
index 9bd7fbf70ea9..22e9f77c51d6 100644
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#include <linux/folio_wait.h>
 #include <linux/io_uring_types.h>
-#include <linux/pagemap.h>
 
 struct io_meta_state {
 	u32			seed;
@@ -19,11 +19,11 @@ struct io_async_rw {
 		unsigned			buf_group;
 
 		/*
-		 * wpq is for buffered io, while meta fields are used with
+		 * wfq is for buffered io, while meta fields are used with
 		 * direct io
 		 */
 		union {
-			struct wait_page_queue		wpq;
+			struct wait_folio_queue		wfq;
 			struct {
 				struct uio_meta			meta;
 				struct io_meta_state		meta_state;
mm/folio_wait.c +37 -37

192 lines not on this page · raw message

mm/internal.h +1 -1
index a121ca07f75c..21b0f4ec2478 100644
@@ -105,7 +105,7 @@ void page_writeback_init(void);
 void folio_wait_init(void);
 void folio_wake_writeback(struct folio *folio);
 int folio_put_wait_locked(struct folio *folio, int state);
-int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait);
+int __folio_lock_async(struct folio *folio, struct wait_folio_queue *wait);
 
 /*
  * If a 16GB hugetlb folio were mapped by PTEs of all of its 4kB pages,
Signature · 1 line
2.39.5
#7 lore raw

RFC PATCH 7/11 folio_wait: convert VM_BUG_ON_FOLIO() to VM_WARN_ON_ONCE_FOLIO()

In reply to Tal Zussman

BUG_ON() is deprecated [1]. The VM_BUG_ON_FOLIO() assertions in folio_unlock(), folio_end_read(), and folio_end_private_2() verify folio state invariants and are already debug checks. There is no additional benefit gained by crashing the system.

Convert them to VM_WARN_ON_ONCE_FOLIO(), as is now preferred for such checks.

[1] https://www.kernel.org/doc/html/latest/process/coding-style.html#use-warn-rather-than-bug

---
 mm/folio_wait.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/folio_wait.c +4 -4
index 70f808729f9c..52d336bc7fe0 100644
@@ -465,7 +465,7 @@ void folio_unlock(struct folio *folio)
 	/* Bit 7 allows x86 to check the byte's sign bit */
 	BUILD_BUG_ON(PG_waiters != 7);
 	BUILD_BUG_ON(PG_locked > 7);
-	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
+	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
 	if (folio_xor_flags_has_waiters(folio, 1 << PG_locked))
 		folio_wake_bit(folio, PG_locked);
 }
@@ -490,8 +490,8 @@ void folio_end_read(struct folio *folio, bool success)
 
 	/* Must be in bottom byte for x86 to work */
 	BUILD_BUG_ON(PG_uptodate > 7);
-	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
-	VM_BUG_ON_FOLIO(success && folio_test_uptodate(folio), folio);
+	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
+	VM_WARN_ON_ONCE_FOLIO(success && folio_test_uptodate(folio), folio);
 
 	if (likely(success))
 		mask |= 1 << PG_uptodate;
@@ -513,7 +513,7 @@ EXPORT_SYMBOL(folio_end_read);
  */
 void folio_end_private_2(struct folio *folio)
 {
-	VM_BUG_ON_FOLIO(!folio_test_private_2(folio), folio);
+	VM_WARN_ON_ONCE_FOLIO(!folio_test_private_2(folio), folio);
 	clear_bit_unlock(PG_private_2, folio_flags(folio, 0));
 	folio_wake_bit(folio, PG_private_2);
 	folio_put(folio);
Signature · 1 line
2.39.5
#8 lore raw

RFC PATCH 8/11 MAINTAINERS: add folio_wait files to MEMORY MANAGEMENT - CORE

In reply to Tal Zussman

Add mm/folio_wait.c and include/linux/folio_wait.h after they were split out from mm/filemap.c, mm/page-writeback.c, and include/linux/pagemap.h.

---
 MAINTAINERS | 2 ++
 1 file changed, 2 insertions(+)
MAINTAINERS +2 -0
index 8cf9ba51d981..bfe1488d9030 100644
@@ -16781,6 +16781,7 @@ S:	Maintained
 W:	http://www.linux-mm.org
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
 F:	include/linux/folio_batch.h
+F:	include/linux/folio_wait.h
 F:	include/linux/gfp.h
 F:	include/linux/gfp_types.h
 F:	include/linux/highmem.h
@@ -16802,6 +16803,7 @@ F:	kernel/fork.c
 F:	mm/Kconfig
 F:	mm/debug.c
 F:	mm/folio-compat.c
+F:	mm/folio_wait.c
 F:	mm/highmem.c
 F:	mm/init-mm.c
 F:	mm/internal.h
Signature · 1 line
2.39.5
#9 lore raw

RFC PATCH 9/11 fs: move dir_pages() from <linux/pagemap.h> to <linux/fs.h>

In reply to Tal Zussman

This is an inode-based helper and should live with other inode helpers.

---
 include/linux/fs.h      | 6 ++++++
 include/linux/pagemap.h | 6 ------
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/fs.h +6 -0
index cd5088dfe9a1..776cc82932a7 100644
@@ -1171,6 +1171,12 @@ static inline void i_size_write(struct inode *inode, loff_t i_size)
 #endif
 }
 
+static inline unsigned long dir_pages(const struct inode *inode)
+{
+	return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>
+			       PAGE_SHIFT;
+}
+
 static inline unsigned iminor(const struct inode *inode)
 {
 	return MINOR(inode->i_rdev);
include/linux/pagemap.h +0 -6
index 84ccb682cca8..f86a550ad516 100644
@@ -1356,12 +1356,6 @@ static inline size_t readahead_batch_length(const struct readahead_control *rac)
 	return rac->_batch_count * PAGE_SIZE;
 }
 
-static inline unsigned long dir_pages(const struct inode *inode)
-{
-	return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>
-			       PAGE_SHIFT;
-}
-
 /**
  * folio_mkwrite_check_truncate - check if folio was truncated
  * @folio: the folio to check
Signature · 1 line
2.39.5
#10 lore raw

RFC PATCH 10/11 fs: move generic_file_read_iter() to fs/read_write.c

In reply to Tal Zussman

generic_file_read_iter() and its kiocb_write_and_wait() helper are VFS-level read functions: Their callers are filesystems, and their job is to glue direct I/O or the page cache (filemap_read) to a struct kiocb and iov_iter caller.

Move both to fs/read_write.c, alongside vfs_iter_read. Drop the extern from generic_file_read_iter()'s declaration and reflow the generic_file_read_iter() definition to fit on one line too.

---
 fs/read_write.c         | 82 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h      |  3 +-
 include/linux/pagemap.h |  1 -
 mm/filemap.c            | 82 -------------------------------------------------
 4 files changed, 84 insertions(+), 84 deletions(-)
fs/read_write.c +82 -0
index 50bff7edc91f..59ceea85c163 100644
@@ -989,6 +989,88 @@ ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
 }
 EXPORT_SYMBOL(vfs_iter_write);
 
+int kiocb_write_and_wait(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;
+
+	if (iocb->ki_flags & IOCB_NOWAIT) {
+		if (filemap_range_needs_writeback(mapping, pos, end))
+			return -EAGAIN;
+		return 0;
+	}
+
+	return filemap_write_and_wait_range(mapping, pos, end);
+}
+EXPORT_SYMBOL_GPL(kiocb_write_and_wait);
+
+/**
+ * generic_file_read_iter - generic filesystem read routine
+ * @iocb:	kernel I/O control block
+ * @iter:	destination for the data read
+ *
+ * This is the "read_iter()" routine for all filesystems
+ * that can use the page cache directly.
+ *
+ * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall
+ * be returned when no data can be read without waiting for I/O requests
+ * to complete; it doesn't prevent readahead.
+ *
+ * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O
+ * requests shall be made for the read or for readahead.  When no data
+ * can be read, -EAGAIN shall be returned.  When readahead would be
+ * triggered, a partial, possibly empty read shall be returned.
+ *
+ * Return:
+ * * number of bytes copied, even for partial reads
+ * * negative error code (or 0 if IOCB_NOIO) if nothing was read
+ */
+ssize_t generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
+{
+	size_t count = iov_iter_count(iter);
+	ssize_t retval = 0;
+
+	if (!count)
+		return 0; /* skip atime */
+
+	if (iocb->ki_flags & IOCB_DIRECT) {
+		struct file *file = iocb->ki_filp;
+		struct address_space *mapping = file->f_mapping;
+		struct inode *inode = mapping->host;
+
+		retval = kiocb_write_and_wait(iocb, count);
+		if (retval < 0)
+			return retval;
+		file_accessed(file);
+
+		retval = mapping->a_ops->direct_IO(iocb, iter);
+		if (retval >= 0) {
+			iocb->ki_pos += retval;
+			count -= retval;
+		}
+		if (retval != -EIOCBQUEUED)
+			iov_iter_revert(iter, count - iov_iter_count(iter));
+
+		/*
+		 * Btrfs can have a short DIO read if we encounter
+		 * compressed extents, so if there was an error, or if
+		 * we've already read everything we wanted to, or if
+		 * there was a short read because we hit EOF, go ahead
+		 * and return.  Otherwise fallthrough to buffered io for
+		 * the rest of the read.  Buffered reads will not work for
+		 * DAX files, so don't bother trying.
+		 */
+		if (retval < 0 || !count || IS_DAX(inode))
+			return retval;
+		if (iocb->ki_pos >= i_size_read(inode))
+			return retval;
+	}
+
+	return filemap_read(iocb, iter, retval);
+}
+EXPORT_SYMBOL(generic_file_read_iter);
+
 static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
 			 unsigned long vlen, loff_t *pos, rwf_t flags)
 {
include/linux/fs.h +2 -1
index 776cc82932a7..c0151ced8e7a 100644
@@ -3055,7 +3055,8 @@ extern int generic_write_check_limits(struct file *file, loff_t pos,
 extern int generic_file_rw_checks(struct file *file_in, struct file *file_out);
 ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *to,
 		ssize_t already_read);
-extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
+ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
+int kiocb_write_and_wait(struct kiocb *iocb, size_t count);
 extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
 extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
 extern ssize_t generic_file_direct_write(struct kiocb *, struct iov_iter *);
include/linux/pagemap.h +0 -1
index f86a550ad516..46cefd552a51 100644
@@ -59,7 +59,6 @@ int filemap_fdatawrite_range(struct address_space *mapping,
 		loff_t start, loff_t end);
 int filemap_check_errors(struct address_space *mapping);
 void __filemap_set_wb_err(struct address_space *mapping, int err);
-int kiocb_write_and_wait(struct kiocb *iocb, size_t count);
 
 static inline int filemap_write_and_wait(struct address_space *mapping)
 {
mm/filemap.c +0 -82

94 lines not on this page · raw message

Signature · 1 line
2.39.5
#11 lore raw

RFC PATCH 11/11 fs: move generic_file_write_iter() family to fs/read_write.c

In reply to Tal Zussman

Move the VFS-level generic write path out of mm/filemap.c into fs/read_write.c next to the just-relocated read path:

- generic_file_write_iter()
- __generic_file_write_iter()
- generic_file_direct_write()
- generic_perform_write()
- kiocb_invalidate_pages()
- kiocb_invalidate_post_direct_write()
- dio_warn_stale_pagecache()

The kiocb_invalidate_* prototypes move from <linux/pagemap.h> to <linux/fs.h>, joining kiocb_write_and_wait() and the other generic read/write declarations. Drop extern from the prototypes of all five generic_file_* declarations in <linux/fs.h>. Reflow the generic_file_direct_write() definition to fit on one line.

---
 fs/read_write.c         | 276 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h      |   8 +-
 include/linux/pagemap.h |   2 -
 mm/filemap.c            | 277 ------------------------------------------------
 4 files changed, 281 insertions(+), 282 deletions(-)
fs/read_write.c +276 -0
index 59ceea85c163..cea5f79fdacf 100644
@@ -1071,6 +1071,282 @@ ssize_t generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 }
 EXPORT_SYMBOL(generic_file_read_iter);
 
+int kiocb_invalidate_pages(struct kiocb *iocb, size_t count)
+{
+	struct address_space *mapping = iocb->ki_filp->f_mapping;
+
+	return filemap_invalidate_pages(mapping, iocb->ki_pos,
+					iocb->ki_pos + count - 1,
+					iocb->ki_flags & IOCB_NOWAIT);
+}
+EXPORT_SYMBOL_GPL(kiocb_invalidate_pages);
+
+/*
+ * Warn about a page cache invalidation failure during a direct I/O write.
+ */
+static void dio_warn_stale_pagecache(struct file *filp)
+{
+	static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST);
+	char pathname[128];
+	char *path;
+
+	errseq_set(&filp->f_mapping->wb_err, -EIO);
+	if (__ratelimit(&_rs)) {
+		path = file_path(filp, pathname, sizeof(pathname));
+		if (IS_ERR(path))
+			path = "(unknown)";
+		pr_crit("Page cache invalidation failure on direct I/O.  Possible data corruption due to collision with buffered I/O!\n");
+		pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid,
+			current->comm);
+	}
+}
+
+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 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
+	 * to buffered write.
+	 */
+	written = kiocb_invalidate_pages(iocb, write_len);
+	if (written) {
+		if (written == -EBUSY)
+			return 0;
+		return written;
+	}
+
+	written = mapping->a_ops->direct_IO(iocb, from);
+
+	/*
+	 * Finally, try again to invalidate clean pages which might have been
+	 * cached by non-direct readahead, or faulted in by get_user_pages()
+	 * if the source of the write was an mmap'ed region of the file
+	 * we're writing.  Either one is a pretty crazy thing to do,
+	 * so we don't support it 100%.  If this invalidation
+	 * fails, tough, the write still worked...
+	 *
+	 * Most of the time we do not need this since dio_complete() will do
+	 * the invalidation for us. However there are some file systems that
+	 * do not end up with dio_complete() being called, so let's not break
+	 * them by removing it completely.
+	 *
+	 * Noticeable example is a blkdev_direct_IO().
+	 *
+	 * Skip invalidation for async writes or if mapping has no pages.
+	 */
+	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)) {
+			i_size_write(inode, pos);
+			mark_inode_dirty(inode);
+		}
+		iocb->ki_pos = pos;
+	}
+	if (written != -EIOCBQUEUED)
+		iov_iter_revert(from, write_len - iov_iter_count(from));
+	return written;
+}
+EXPORT_SYMBOL(generic_file_direct_write);
+
+ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)
+{
+	struct file *file = iocb->ki_filp;
+	loff_t pos = iocb->ki_pos;
+	struct address_space *mapping = file->f_mapping;
+	const struct address_space_operations *a_ops = mapping->a_ops;
+	size_t chunk = mapping_max_folio_size(mapping);
+	long status = 0;
+	ssize_t written = 0;
+
+	do {
+		struct folio *folio;
+		size_t offset;		/* Offset into folio */
+		size_t bytes;		/* Bytes to write to folio */
+		size_t copied;		/* Bytes copied from user */
+		void *fsdata = NULL;
+
+		bytes = iov_iter_count(i);
+retry:
+		offset = pos & (chunk - 1);
+		bytes = min(chunk - offset, bytes);
+		balance_dirty_pages_ratelimited(mapping);
+
+		if (fatal_signal_pending(current)) {
+			status = -EINTR;
+			break;
+		}
+
+		status = a_ops->write_begin(iocb, mapping, pos, bytes,
+						&folio, &fsdata);
+		if (unlikely(status < 0))
+			break;
+
+		offset = offset_in_folio(folio, pos);
+		if (bytes > folio_size(folio) - offset)
+			bytes = folio_size(folio) - offset;
+
+		if (mapping_writably_mapped(mapping))
+			flush_dcache_folio(folio);
+
+		/*
+		 * Faults here on mmap()s can recurse into arbitrary
+		 * filesystem code. Lots of locks are held that can
+		 * deadlock. Use an atomic copy to avoid deadlocking
+		 * in page fault handling.
+		 */
+		copied = copy_folio_from_iter_atomic(folio, offset, bytes, i);
+		flush_dcache_folio(folio);
+
+		status = a_ops->write_end(iocb, mapping, pos, bytes, copied,
+						folio, fsdata);
+		if (unlikely(status != copied)) {
+			iov_iter_revert(i, copied - max(status, 0L));
+			if (unlikely(status < 0))
+				break;
+		}
+		cond_resched();
+
+		if (unlikely(status == 0)) {
+			/*
+			 * A short copy made ->write_end() reject the
+			 * thing entirely.  Might be memory poisoning
+			 * halfway through, might be a race with munmap,
+			 * might be severe memory pressure.
+			 */
+			if (chunk > PAGE_SIZE)
+				chunk /= 2;
+			if (copied) {
+				bytes = copied;
+				goto retry;
+			}
+
+			/*
+			 * 'folio' is now unlocked and faults on it can be
+			 * handled. Ensure forward progress by trying to
+			 * fault it in now.
+			 */
+			if (fault_in_iov_iter_readable(i, bytes) == bytes) {
+				status = -EFAULT;
+				break;
+			}
+		} else {
+			pos += status;
+			written += status;
+		}
+	} while (iov_iter_count(i));
+
+	if (!written)
+		return status;
+	iocb->ki_pos += written;
+	return written;
+}
+EXPORT_SYMBOL(generic_perform_write);
+
+/**
+ * __generic_file_write_iter - write data to a file
+ * @iocb:	IO state structure (file, offset, etc.)
+ * @from:	iov_iter with data to write
+ *
+ * This function does all the work needed for actually writing data to a
+ * file. It does all basic checks, removes SUID from the file, updates
+ * modification times and calls proper subroutines depending on whether we
+ * do direct IO or a standard buffered write.
+ *
+ * It expects i_rwsem to be grabbed unless we work on a block device or similar
+ * object which does not need locking at all.
+ *
+ * This function does *not* take care of syncing data in case of O_SYNC write.
+ * A caller has to handle it. This is mainly due to the fact that we want to
+ * avoid syncing under i_rwsem.
+ *
+ * Return:
+ * * number of bytes written, even for truncated writes
+ * * negative error code if no data has been written at all
+ */
+ssize_t __generic_file_write_iter(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;
+	ssize_t ret;
+
+	ret = file_remove_privs(file);
+	if (ret)
+		return ret;
+
+	ret = file_update_time(file);
+	if (ret)
+		return ret;
+
+	if (iocb->ki_flags & IOCB_DIRECT) {
+		ret = generic_file_direct_write(iocb, from);
+		/*
+		 * If the write stopped short of completing, fall back to
+		 * buffered writes.  Some filesystems do this for writes to
+		 * holes, for example.  For DAX files, a buffered write will
+		 * not succeed (even if it did, DAX does not handle dirty
+		 * page-cache pages correctly).
+		 */
+		if (ret < 0 || !iov_iter_count(from) || IS_DAX(inode))
+			return ret;
+		return direct_write_fallback(iocb, from, ret,
+				generic_perform_write(iocb, from));
+	}
+
+	return generic_perform_write(iocb, from);
+}
+EXPORT_SYMBOL(__generic_file_write_iter);
+
+/**
+ * generic_file_write_iter - write data to a file
+ * @iocb:	IO state structure
+ * @from:	iov_iter with data to write
+ *
+ * This is a wrapper around __generic_file_write_iter() to be used by most
+ * filesystems. It takes care of syncing the file in case of O_SYNC file
+ * and acquires i_rwsem as needed.
+ * Return:
+ * * negative error code if no data has been written at all of
+ *   vfs_fsync_range() failed for a synchronous write
+ * * number of bytes written, even for truncated writes
+ */
+ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
+{
+	struct file *file = iocb->ki_filp;
+	struct inode *inode = file->f_mapping->host;
+	ssize_t ret;
+
+	inode_lock(inode);
+	ret = generic_write_checks(iocb, from);
+	if (ret > 0)
+		ret = __generic_file_write_iter(iocb, from);
+	inode_unlock(inode);
+
+	if (ret > 0)
+		ret = generic_write_sync(iocb, ret);
+	return ret;
+}
+EXPORT_SYMBOL(generic_file_write_iter);
+
 static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
 			 unsigned long vlen, loff_t *pos, rwf_t flags)
 {
include/linux/fs.h +5 -3
index c0151ced8e7a..6cfb9e46bc37 100644
@@ -3057,9 +3057,11 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *to,
 		ssize_t already_read);
 ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
 int kiocb_write_and_wait(struct kiocb *iocb, size_t count);
-extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
-extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
-extern ssize_t generic_file_direct_write(struct kiocb *, struct iov_iter *);
+int kiocb_invalidate_pages(struct kiocb *iocb, size_t count);
+void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count);
+ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
+ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
+ssize_t generic_file_direct_write(struct kiocb *, struct iov_iter *);
 ssize_t generic_perform_write(struct kiocb *, struct iov_iter *);
 ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter,
 		ssize_t direct_written, ssize_t buffered_written);
include/linux/pagemap.h +0 -2
index 46cefd552a51..b7c2dc8076ab 100644
@@ -31,8 +31,6 @@ 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);
-void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count);
 int filemap_invalidate_pages(struct address_space *mapping,
 			     loff_t pos, loff_t end, bool nowait);
 
mm/filemap.c +0 -277

289 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer
#12 lore raw
In reply to Tal Zussman
Quoted 22 lines · Tal Zussman

mm/filemap.c has accumulated additional infrastructure over the years that is not directly related to the page cache. It is currently nearly 5000 lines long. This series splits out the folio bit-lock and wait queue code into separate files, and moves the VFS-level generic_file_{read,write}_iter() family of files to fs/read_write.c, in order to provide better separation of concerns. This also slims down mm/filemap.c by ~1000 lines.

The folio wait infrastructure is centralized in mm/folio_wait.c and include/linux/folio_wait.h, with functions moved from mm/filemap.c, mm/page-writeback.c, and include/linux/pagemap.h. Afterwards, the code is cleaned up a little, with functions and data types renamed to refer to folios rather than pages.

generic_file_{read,write}_iter() implement the VFS-level read/write path for filesystems, including support for direct I/O. These functions and their helpers are moved to fs/read_write.c, along with other VFS-level read/write functions. dir_pages() is also moved to include/linux/fs.h. i_blocks_per_folio() is not moved from include/linux/pagemap.h, as it requires folio_size(), which is not currently available in include/linux/fs.h.

Quoted 7 lines

No functional change is intended.

Note: I have additional cleanups to mm/filemap.c ready to go, foremost among them centralizing on the filemap_*() naming convention and making the exposed page cache API clearer and more consistent, but I've split these patches off from that in order to avoid sending these logically separate patches to ~60 maintainers.

Overall this makes sense to me. In particular I agree it makes sense to move the file read/write helpers into fs. Regarding the page waiting bits it makes some sense to me as well although there it's more of "I don't really care" opinion so let's see what Matthew and others think...

Honza

Quoted 36 lines

---
Tal Zussman (11):
mm: add folio_wake_writeback() helper
folio_wait: move folio bit-lock and wait implementation to mm/folio_wait.c
folio_wait: move folio bit-lock and wait declarations to include/linux/folio_wait.h
folio_wait: move folio_wait_writeback() family to mm/folio_wait.c
folio_wait: reformat comments and fix alignment
folio_wait: rename wait_page_* infrastructure to wait_folio_*
folio_wait: convert VM_BUG_ON_FOLIO() to VM_WARN_ON_ONCE_FOLIO()
MAINTAINERS: add folio_wait files to MEMORY MANAGEMENT - CORE
fs: move dir_pages() from <linux/pagemap.h> to <linux/fs.h>
fs: move generic_file_read_iter() to fs/read_write.c
fs: move generic_file_write_iter() family to fs/read_write.c

 MAINTAINERS                |   2 +
 fs/read_write.c            | 358 ++++++++++++++++
 include/linux/folio_wait.h | 183 +++++++++
 include/linux/fs.h         |  19 +-
 include/linux/pagemap.h    | 184 +--------
 io_uring/rw.c              |  14 +-
 io_uring/rw.h              |   6 +-
 mm/Makefile                |   2 +-
 mm/filemap.c               | 993 +--------------------------------------------
 mm/folio_wait.c            | 710 ++++++++++++++++++++++++++++++++
 mm/internal.h              |   4 +
 mm/page-writeback.c        |  66 ---
 12 files changed, 1285 insertions(+), 1256 deletions(-)

---
base-commit: e9add7501ad3297dad9b90ce201266830a68ab47
change-id: 20260511-filemap-split-871b5c18e98c

Best regards,

Signature · 1 line
Tal Zussman <[email protected]>
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#13 lore raw
Quoted 25 lines · Jan Kara
Quoted 24 lines · Tal Zussman

mm/filemap.c has accumulated additional infrastructure over the years that is not directly related to the page cache. It is currently nearly 5000 lines long. This series splits out the folio bit-lock and wait queue code into separate files, and moves the VFS-level generic_file_{read,write}_iter() family of files to fs/read_write.c, in order to provide better separation of concerns. This also slims down mm/filemap.c by ~1000 lines.

The folio wait infrastructure is centralized in mm/folio_wait.c and include/linux/folio_wait.h, with functions moved from mm/filemap.c, mm/page-writeback.c, and include/linux/pagemap.h. Afterwards, the code is cleaned up a little, with functions and data types renamed to refer to folios rather than pages.

generic_file_{read,write}_iter() implement the VFS-level read/write path for filesystems, including support for direct I/O. These functions and their helpers are moved to fs/read_write.c, along with other VFS-level read/write functions. dir_pages() is also moved to include/linux/fs.h. i_blocks_per_folio() is not moved from include/linux/pagemap.h, as it requires folio_size(), which is not currently available in include/linux/fs.h.

No functional change is intended.

Quoted 8 lines
Quoted 5 lines

Note: I have additional cleanups to mm/filemap.c ready to go, foremost among them centralizing on the filemap_*() naming convention and making the exposed page cache API clearer and more consistent, but I've split these patches off from that in order to avoid sending these logically separate patches to ~60 maintainers.

Overall this makes sense to me. In particular I agree it makes sense to move the file read/write helpers into fs. Regarding the page waiting bits

Seconded.

Quoted 2 lines

it makes some sense to me as well although there it's more of "I don't really care" opinion so let's see what Matthew and others think...

Agreed.

#14 lore raw
In reply to Jan Kara
Quoted 2 lines · Jan Kara

Overall this makes sense to me. In particular I agree it makes sense to move the file read/write helpers into fs.

I disagree very strongly. Mixing default implementations with the higher level APIs is a really bad idea and leads to people taking stupid shortcuts and other layering violations.

Splitting up filemap.c makes sense, but I'd rather keep the generic copy into and out of the pagecache code with the MM infrastructure for it, as it is not VFS code, and making that clear to anyone touching the code is important.

#15 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Jan Kara

Overall this makes sense to me. In particular I agree it makes sense to move the file read/write helpers into fs.

I disagree very strongly. Mixing default implementations with the higher level APIs is a really bad idea and leads to people taking stupid shortcuts and other layering violations.

fs/read_write.c already contains some of these "generic" function implementations, including generic_write_checks(), which is called by generic_file_write_iter() in mm/filemap.c. Right now the two files are unnecessarily interdependent. I do think fs/read_write.c is the natural home for these functions.

Quoted 4 lines

Splitting up filemap.c makes sense, but I'd rather keep the generic copy into and out of the pagecache code with the MM infrastructure for it, as it is not VFS code, and making that clear to anyone touching the code is important.

About half the code moved is implementing direct I/O or multiplexing between page cache I/O and direct I/O. It definitely shouldn't be in the page cache, and I do think it is VFS code. The one exception I see is generic_perform_write(), which is analogous to filemap_read() and should stay in filemap.c (and probably be renamed to something like filemap_write()).

#16 lore raw
Quoted 9 lines · Tal Zussman
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Jan Kara

Overall this makes sense to me. In particular I agree it makes sense to move the file read/write helpers into fs.

I disagree very strongly. Mixing default implementations with the higher level APIs is a really bad idea and leads to people taking stupid shortcuts and other layering violations.

Quoted 5 lines

fs/read_write.c already contains some of these "generic" function implementations, including generic_write_checks(), which is called by generic_file_write_iter() in mm/filemap.c. Right now the two files are unnecessarily interdependent. I do think fs/read_write.c is the natural home for these functions.

generic_write_checks is a very different beast. It is a generic helper that every implementation must call. The implementations have to call it with the right locks held, and this it can't be done before calling into the method.

Quoted 7 lines
Quoted 4 lines

Splitting up filemap.c makes sense, but I'd rather keep the generic copy into and out of the pagecache code with the MM infrastructure for it, as it is not VFS code, and making that clear to anyone touching the code is important.

About half the code moved is implementing direct I/O or multiplexing between page cache I/O and direct I/O.

This will hopefully change quite a bit once we move everyone off the legacy direct I/O code and the helpers for it. Another reason not to move the code around for now as it should change a bit.

Quoted 2 lines

It definitely shouldn't be in the page cache,
and I do think it is VFS code.

For the higher level stuff I'd agree. But I'm not sure how much is left after the above. If we have good helpers left something like libfs.c or a new library might be a better place.

Quoted 3 lines

The one exception I see is
generic_perform_write(), which is analogous to filemap_read() and should stay in filemap.c (and probably be renamed to something like filemap_write()).

Agreed on that.

#17 lore raw
In reply to Jan Kara
Quoted 31 lines · Jan Kara
Quoted 22 lines · Tal Zussman

mm/filemap.c has accumulated additional infrastructure over the years that is not directly related to the page cache. It is currently nearly 5000 lines long. This series splits out the folio bit-lock and wait queue code into separate files, and moves the VFS-level generic_file_{read,write}_iter() family of files to fs/read_write.c, in order to provide better separation of concerns. This also slims down mm/filemap.c by ~1000 lines.

The folio wait infrastructure is centralized in mm/folio_wait.c and include/linux/folio_wait.h, with functions moved from mm/filemap.c, mm/page-writeback.c, and include/linux/pagemap.h. Afterwards, the code is cleaned up a little, with functions and data types renamed to refer to folios rather than pages.

generic_file_{read,write}_iter() implement the VFS-level read/write path for filesystems, including support for direct I/O. These functions and their helpers are moved to fs/read_write.c, along with other VFS-level read/write functions. dir_pages() is also moved to include/linux/fs.h. i_blocks_per_folio() is not moved from include/linux/pagemap.h, as it requires folio_size(), which is not currently available in include/linux/fs.h.

Quoted 7 lines

No functional change is intended.

Note: I have additional cleanups to mm/filemap.c ready to go, foremost among them centralizing on the filemap_*() naming convention and making the exposed page cache API clearer and more consistent, but I've split these patches off from that in order to avoid sending these logically separate patches to ~60 maintainers.

Quoted 4 lines

Overall this makes sense to me. In particular I agree it makes sense to move the file read/write helpers into fs. Regarding the page waiting bits it makes some sense to me as well although there it's more of "I don't really care" opinion so let's see what Matthew and others think...

Sounds good, thanks. For the folio wait/lock code, my reasoning was that it's used well beyond the page cache and independent of it, so there's no reason to clog up filemap.c with 700 lines of infrastructure for a more generic interface (and splitting up pagemap.h a little is a nice bonus). But yes, let's see what Matthew thinks.

Thanks,
Tal