Linux filesystem development
 help / color / mirror / Atom feed
From: Amit Prasad <amit@modal.com>
To: djbw@kernel.org, brauner@kernel.org, viro@zeniv.linux.org.uk
Cc: jack@suse.cz, willy@infradead.org, djwong@kernel.org,
	alison.schofield@intel.com, nvdimm@lists.linux.dev,
	linux-fsdevel@vger.kernel.org, Amit Prasad <amit@modal.com>
Subject: [PATCH 2/2] fsdax: iterate over short dax_direct_access() ranges
Date: Tue,  1 Dec 2026 17:43:24 +0000	[thread overview]
Message-ID: <20261201174324.5991-3-amit@modal.com> (raw)
In-Reply-To: <20261201174324.5991-1-amit@modal.com>

After the previous patch, unshare, copy-on-write writes and dedupe fail
with an error when their range crosses a point where the device ends a
mapped range. Handle such ranges iteratively in pieces instead.

Add dax_iomap_kaddr(), which maps a byte range of an iomap and returns
the number of bytes mapped. Use it in a new helper,
dax_iomap_copy_from(), which copies the copy-on-write source for
dax_iomap_copy_around() one mapped range at a time. In each step,
unshare and compare now handle only as many bytes as both the source and
the destination map; the iomap iterator continues with the rest.

Authored with the use of an AI coding assistant (Claude Opus 5.5).

Signed-off-by: Amit Prasad <amit@modal.com>
---
 fs/dax.c | 101 +++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 75 insertions(+), 26 deletions(-)

diff --git a/fs/dax.c b/fs/dax.c
index 0bb029e6a1eb..6da37f952e6a 100644
--- a/fs/dax.c
+++ b/fs/dax.c
@@ -1303,6 +1303,57 @@ static int dax_iomap_direct_access(const struct iomap *iomap, loff_t pos,
 	return rc;
 }
 
+/*
+ * Map @size bytes at @pos of @iomap at *@kaddr, under dax_read_lock().  Return
+ * how many of those bytes are mapped, which may be fewer than @size.
+ */
+static long dax_iomap_kaddr(const struct iomap *iomap, loff_t pos, size_t size,
+		void **kaddr)
+{
+	size_t off = offset_in_page(pos);
+	long nr = dax_direct_access(iomap->dax_dev, dax_iomap_pgoff(iomap, pos),
+			PHYS_PFN(ALIGN(off + size, PAGE_SIZE)), DAX_ACCESS,
+			kaddr, NULL);
+
+	if (nr < 0)
+		return nr;
+	*kaddr += off;
+	return min_t(size_t, PFN_PHYS(nr) - off, size);
+}
+
+/*
+ * Copy @len bytes at @pos of @srcmap to @dst.
+ *
+ * One dax_direct_access() call can map fewer bytes than asked for, because
+ * the device may end the range early.  So copy in as many steps as it takes to
+ * cover @len.
+ */
+static int dax_iomap_copy_from(const struct iomap *srcmap, loff_t pos,
+		void *dst, size_t len)
+{
+	int id = dax_read_lock();
+	int ret = 0;
+
+	while (len) {
+		void *saddr;
+		long n = dax_iomap_kaddr(srcmap, pos, len, &saddr);
+
+		if (n < 0) {
+			ret = dax_mem2blk_err(n);
+			break;
+		}
+		if (copy_mc_to_kernel(dst, saddr, n)) {
+			ret = -EIO;
+			break;
+		}
+		pos += n;
+		dst += n;
+		len -= n;
+	}
+	dax_read_unlock(id);
+	return ret;
+}
+
 /**
  * dax_iomap_copy_around - Prepare for an unaligned write to a shared/cow page
  * by copying the data before and after the range to be written.
@@ -1332,20 +1383,13 @@ static int dax_iomap_copy_around(loff_t pos, uint64_t length, size_t align_size,
 	/* zero the edges if srcmap is a HOLE or IOMAP_UNWRITTEN */
 	bool zero_edge = srcmap->flags & IOMAP_F_SHARED ||
 			 srcmap->type == IOMAP_UNWRITTEN;
-	void *saddr = NULL;
 	int ret = 0;
 
-	if (!zero_edge) {
-		ret = dax_iomap_direct_access(srcmap, pos, size, &saddr, NULL);
-		if (ret)
-			return dax_mem2blk_err(ret);
-	}
-
 	if (copy_all) {
 		if (zero_edge)
 			memset(daddr, 0, size);
 		else
-			ret = copy_mc_to_kernel(daddr, saddr, length);
+			ret = dax_iomap_copy_from(srcmap, pos, daddr, length);
 		goto out;
 	}
 
@@ -1354,9 +1398,10 @@ static int dax_iomap_copy_around(loff_t pos, uint64_t length, size_t align_size,
 		if (zero_edge)
 			memset(daddr, 0, head_off);
 		else {
-			ret = copy_mc_to_kernel(daddr, saddr, head_off);
+			ret = dax_iomap_copy_from(srcmap, pos - head_off, daddr,
+						  head_off);
 			if (ret)
-				return -EIO;
+				return ret;
 		}
 	}
 
@@ -1368,16 +1413,16 @@ static int dax_iomap_copy_around(loff_t pos, uint64_t length, size_t align_size,
 		if (zero_edge)
 			memset(daddr + tail_off, 0, tail_len);
 		else {
-			ret = copy_mc_to_kernel(daddr + tail_off,
-						saddr + tail_off, tail_len);
+			ret = dax_iomap_copy_from(srcmap, end, daddr + tail_off,
+						  tail_len);
 			if (ret)
-				return -EIO;
+				return ret;
 		}
 	}
 out:
 	if (zero_edge)
 		dax_flush(srcmap->dax_dev, daddr, size);
-	return ret ? -EIO : 0;
+	return ret;
 }
 
 /*
@@ -1443,6 +1488,7 @@ static int dax_unshare_iter(struct iomap_iter *iter)
 	u32 mod;
 	int id = 0;
 	s64 ret;
+	u64 len;
 	void *daddr = NULL, *saddr = NULL;
 
 	if (!iomap_want_unshare_iter(iter))
@@ -1468,21 +1514,25 @@ static int dax_unshare_iter(struct iomap_iter *iter)
 				      (copy_pos + copy_len - 1) >> PAGE_SHIFT);
 
 	id = dax_read_lock();
-	ret = dax_iomap_direct_access(iomap, copy_pos, copy_len, &daddr, NULL);
+	ret = dax_iomap_kaddr(iomap, copy_pos, copy_len, &daddr);
 	if (ret < 0)
 		goto out_unlock;
 
-	ret = dax_iomap_direct_access(srcmap, copy_pos, copy_len, &saddr, NULL);
+	/* Copy what both devices map; the iterator maps the rest. */
+	ret = dax_iomap_kaddr(srcmap, copy_pos, ret, &saddr);
 	if (ret < 0)
 		goto out_unlock;
 
-	if (copy_mc_to_kernel(daddr, saddr, copy_len) != 0)
+	len = ret;
+	if (copy_mc_to_kernel(daddr, saddr, len) != 0)
 		ret = -EIO;
 
 out_unlock:
 	dax_read_unlock(id);
 	if (ret < 0)
 		return dax_mem2blk_err(ret);
+	if (len < copy_len)
+		return iomap_iter_advance(iter, copy_pos + len - iter->pos);
 	return iomap_iter_advance_full(iter);
 }
 
@@ -2194,7 +2244,7 @@ static int dax_range_compare_iter(struct iomap_iter *it_src,
 	const struct iomap *dmap = &it_dest->iomap;
 	loff_t pos1 = it_src->pos, pos2 = it_dest->pos;
 	void *saddr, *daddr;
-	size_t size1, size2;
+	long n;
 	int id, ret;
 
 	len = min(len, min(smap->length, dmap->length));
@@ -2210,18 +2260,17 @@ static int dax_range_compare_iter(struct iomap_iter *it_src,
 	}
 
 	id = dax_read_lock();
-	size1 = ALIGN(len + offset_in_page(pos1), PAGE_SIZE);
-	ret = dax_iomap_direct_access(smap, pos1, size1, &saddr, NULL);
-	if (ret < 0)
+	n = dax_iomap_kaddr(smap, pos1, len, &saddr);
+	if (n < 0)
 		goto out_unlock;
 
-	size2 = ALIGN(len + offset_in_page(pos2), PAGE_SIZE);
-	ret = dax_iomap_direct_access(dmap, pos2, size2, &daddr, NULL);
-	if (ret < 0)
+	/* Compare what both devices map; the iterators map the rest. */
+	n = dax_iomap_kaddr(dmap, pos2, n, &daddr);
+	if (n < 0)
 		goto out_unlock;
 
-	*same = !memcmp(saddr + offset_in_page(pos1),
-			daddr + offset_in_page(pos2), len);
+	len = n;
+	*same = !memcmp(saddr, daddr, len);
 	if (!*same)
 		len = 0;
 	dax_read_unlock(id);
-- 
2.54.0


  parent reply	other threads:[~2026-12-01 17:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 17:43 [PATCH 0/2] fsdax: handle short dax_direct_access() ranges Amit Prasad
2026-12-01 17:43 ` [PATCH 1/2] fsdax: check the mapped length for all dax_iomap_direct_access() callers Amit Prasad
2026-12-03  0:43   ` Alistair Popple
2026-12-01 17:43 ` Amit Prasad [this message]
2026-12-03  0:49   ` [PATCH 2/2] fsdax: iterate over short dax_direct_access() ranges Alistair Popple
2026-12-01 18:33 ` [PATCH 0/2] fsdax: handle " Amit Prasad

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=20261201174324.5991-3-amit@modal.com \
    --to=amit@modal.com \
    --cc=alison.schofield@intel.com \
    --cc=brauner@kernel.org \
    --cc=djbw@kernel.org \
    --cc=djwong@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.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