diff mbox series

[6/7] btrfs: Move prefaulting out of hot write path

Message ID 20250129181800.ADD26AB5@davehans-spike.ostc.intel.com (mailing list archive)
State New
Headers show
Series Move prefaulting into write slow paths | expand

Commit Message

Dave Hansen Jan. 29, 2025, 6:18 p.m. UTC
From: Dave Hansen <dave.hansen@linux.intel.com>

Prefaulting the write source buffer incurs an extra userspace access
in the common fast path. Make btrfs_buffered_write() consistent with
generic_perform_write(): only touch userspace an extra time when
copy_folio_from_iter_atomic() has failed to make progress.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Chris Mason <clm@fb.com>
Cc: Josef Bacik <josef@toxicpanda.com>
Cc: David Sterba <dsterba@suse.com>
Cc: linux-btrfs@vger.kernel.org
---

 b/fs/btrfs/file.c |   20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff -puN fs/btrfs/file.c~btrfs-postfault fs/btrfs/file.c
--- a/fs/btrfs/file.c~btrfs-postfault	2025-01-29 09:03:36.927756510 -0800
+++ b/fs/btrfs/file.c	2025-01-29 09:03:36.935757176 -0800
@@ -1164,15 +1164,6 @@  ssize_t btrfs_buffered_write(struct kioc
 		int extents_locked;
 		bool force_page_uptodate = false;
 
-		/*
-		 * Fault pages before locking them in prepare_one_folio()
-		 * to avoid recursive lock
-		 */
-		if (unlikely(fault_in_iov_iter_readable(i, write_bytes))) {
-			ret = -EFAULT;
-			break;
-		}
-
 		only_release_metadata = false;
 		sector_offset = pos & (fs_info->sectorsize - 1);
 
@@ -1314,6 +1305,17 @@  again:
 
 		cond_resched();
 
+		/*
+		 * Fault pages in a slow path after dropping folio
+		 * lock. This avoids the chance of deadlocking in
+		 * the fault handler.
+		 */
+		if (unlikely(copied == 0) &&
+		    (fault_in_iov_iter_readable(i, write_bytes) == write_bytes)) {
+			ret = -EFAULT;
+			break;
+		}
+
 		pos += copied;
 		num_written += copied;
 	}