The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Chi Zhiling <chizhiling@163.com>
To: cem@kernel.org
Cc: linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org,
	Chi Zhiling <chizhiling@kylinos.cn>
Subject: [RFC PATCH 2/2] xfs: Enable concurrency when writing within single block
Date: Fri, 25 Apr 2025 18:38:41 +0800	[thread overview]
Message-ID: <20250425103841.3164087-3-chizhiling@163.com> (raw)
In-Reply-To: <20250425103841.3164087-1-chizhiling@163.com>

From: Chi Zhiling <chizhiling@kylinos.cn>

For unextending writes, we will only update the pagecache and extent.
In this case, if our write occurs within a single block, that is,
within a single folio, we don't need an exclusive lock to ensure the
atomicity of the write, because we already have the folio lock.

Signed-off-by: Chi Zhiling <chizhiling@kylinos.cn>
---
 fs/xfs/xfs_file.c | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c
index a6f214f57238..8eaa98464328 100644
--- a/fs/xfs/xfs_file.c
+++ b/fs/xfs/xfs_file.c
@@ -914,6 +914,27 @@ xfs_file_dax_write(
 	return ret;
 }
 
+#define offset_in_block(inode, p) ((unsigned long)(p) & (i_blocksize(inode) - 1))
+
+static inline bool xfs_allow_concurrent(
+	struct kiocb		*iocb,
+	struct iov_iter		*from)
+{
+	struct inode		*inode = iocb->ki_filp->f_mapping->host;
+
+	/* Extending write? */
+	if (iocb->ki_flags & IOCB_APPEND ||
+	    iocb->ki_pos >= i_size_read(inode))
+		return false;
+
+	/* Exceeds a block range? */
+	if (iov_iter_count(from) > i_blocksize(inode) ||
+	    offset_in_block(inode, iocb->ki_pos) + iov_iter_count(from) > i_blocksize(inode))
+		return false;
+
+	return true;
+}
+
 STATIC ssize_t
 xfs_file_buffered_write(
 	struct kiocb		*iocb,
@@ -925,8 +946,12 @@ xfs_file_buffered_write(
 	bool			cleared_space = false;
 	unsigned int		iolock;
 
+	if (xfs_allow_concurrent(iocb, from))
+		iolock = XFS_IOLOCK_SHARED;
+	else
+		iolock = XFS_IOLOCK_EXCL;
+
 write_retry:
-	iolock = XFS_IOLOCK_EXCL;
 	ret = xfs_ilock_iocb_for_write(iocb, &iolock, false);
 	if (ret)
 		return ret;
@@ -935,6 +960,13 @@ xfs_file_buffered_write(
 	if (ret)
 		goto out;
 
+	if (iolock == XFS_IOLOCK_SHARED &&
+	    iocb->ki_pos + iov_iter_count(from) > i_size_read(inode)) {
+		xfs_iunlock(ip, iolock);
+		iolock = XFS_IOLOCK_EXCL;
+		goto write_retry;
+	}
+
 	trace_xfs_file_buffered_write(iocb, from);
 	ret = iomap_file_buffered_write(iocb, from,
 			&xfs_buffered_write_iomap_ops, NULL);
-- 
2.43.0


  parent reply	other threads:[~2025-04-25 10:57 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-25 10:38 [RFC PATCH 0/2] Implement concurrent buffered write with folio lock Chi Zhiling
2025-04-25 10:38 ` [RFC PATCH 1/2] xfs: Add i_direct_mode to indicate the IO mode of inode Chi Zhiling
2025-04-25 15:12   ` Darrick J. Wong
2025-04-26  1:28     ` Chi Zhiling
2025-04-25 10:38 ` Chi Zhiling [this message]
2025-04-25 15:15   ` [RFC PATCH 2/2] xfs: Enable concurrency when writing within single block Darrick J. Wong
2025-04-26  1:34     ` Chi Zhiling
2025-04-30  2:05 ` [RFC PATCH 0/2] Implement concurrent buffered write with folio lock Dave Chinner
2025-04-30  9:03   ` Chi Zhiling
2025-04-30 23:45     ` Dave Chinner
2025-05-01 23:57       ` Chi Zhiling

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250425103841.3164087-3-chizhiling@163.com \
    --to=chizhiling@163.com \
    --cc=cem@kernel.org \
    --cc=chizhiling@kylinos.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox