All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] fsdax: handle short dax_direct_access() ranges
@ 2026-12-01 17:43 Amit Prasad
  2026-12-01 17:43 ` [PATCH 1/2] fsdax: check the mapped length for all dax_iomap_direct_access() callers Amit Prasad
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Amit Prasad @ 2026-12-01 17:43 UTC (permalink / raw)
  To: djbw, brauner, viro
  Cc: jack, willy, djwong, alison.schofield, nvdimm, linux-fsdevel,
	Amit Prasad

dax_direct_access() can map fewer pages than asked for. Earlier changes
caused fsdax's copy-on-write, unshare, and dedupe paths to skip a check
of the returned length, which can result in reads and writes outside
the mapped range.

For example, dm-stripe ends each range at a chunk boundary, so
dax_direct_access() returns a shorter length than requested. On a
filesystem with both DAX and reflinks (e.g. XFS) mounted on the striped
device, fsdax doesn't respect the size of the mapped range, and can
read/write outside of it.

The first patch minimally fixes this and explicitly returns an error.
The second patch makes the above scenario work properly across such
short device boundaries.

Tested against v7.4-rc5 in a VM with two pmem devices, beneath a
dm-stripe target with 64 KiB chunks, XFS with reflink=1 and dax=always:

- unpatched: funshare of a reflinked file corrupts it, and a dedupe of
  two identical files reports them as different.
- patch 1: funshare fails with EINVAL without corrupting the file.
- patches 1-2: funshare succeeds and dedupe returns a valid result.

Reproducer (needs a 1 GiB pmem device, e.g. memmap=1G!4G):

  dmsetup create stripe \
    --table "0 2097152 striped 2 128 /dev/pmem0 0 /dev/pmem0 1048576"
  mkfs.xfs -f -m reflink=1 /dev/mapper/stripe
  mount -o dax=always /dev/mapper/stripe /mnt
  head -c 1M /dev/urandom > /mnt/a
  cp --reflink=always /mnt/a /mnt/b
  xfs_io -c "funshare 0 1m" /mnt/b
  cmp /mnt/a /mnt/b

Creates a dm-stripe across two chunks of a pmem device. Unpatched, cmp
reports differences. With patch 1, funshare fails with EINVAL, and
with both patches cmp shows the files are identical.

The patch was reproduced and fixed with the help of an AI coding
assistant (Claude Opus 5.5).

Amit Prasad (2):
  fsdax: check the mapped length for all dax_iomap_direct_access()
    callers
  fsdax: iterate over short dax_direct_access() ranges

 fs/dax.c | 118 +++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 85 insertions(+), 33 deletions(-)


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.54.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/2] fsdax: check the mapped length for all dax_iomap_direct_access() callers
  2026-12-01 17:43 [PATCH 0/2] fsdax: handle short dax_direct_access() ranges Amit Prasad
@ 2026-12-01 17:43 ` Amit Prasad
  2026-12-01 17:43 ` [PATCH 2/2] fsdax: iterate over short dax_direct_access() ranges Amit Prasad
  2026-12-01 18:33 ` [PATCH 0/2] fsdax: handle " Amit Prasad
  2 siblings, 0 replies; 6+ messages in thread
From: Amit Prasad @ 2026-12-01 17:43 UTC (permalink / raw)
  To: djbw, brauner, viro
  Cc: jack, willy, djwong, alison.schofield, nvdimm, linux-fsdevel,
	Amit Prasad, stable

dax_direct_access() may map fewer pages than asked for, e.g. when a
device-mapper target ends the range early. dax_iomap_direct_access()
checks the length only for callers that also ask for a pfn, so the
dax_iomap_copy_around(), dax_unshare_iter(), and
dax_range_compare_iter() methods may use memory outside the device's
returned range. Additionally, dax_iomap_iter() passes
dax_iomap_copy_around() the remaining length of the write instead of the
length the device mapped.

Thus, unshare, dedupe, and copy-on-write writes which cross these
boundaries can read/write to out-of-range data and corrupt it.

Check the length in dax_iomap_direct_access() for every caller, and
limit the length that dax_iomap_iter() passes to dax_iomap_copy_around()
to the mapped range, so that these out-of-range operations fail with an
error instead.

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

Fixes: e28cd3e50f30 ("fsdax: output address in dax_iomap_pfn() and rename it")
Fixes: ff17b8df224b ("fsdax: introduce dax_iomap_cow_copy()")
Cc: stable@vger.kernel.org
Signed-off-by: Amit Prasad <amit@modal.com>
---
 fs/dax.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/fs/dax.c b/fs/dax.c
index 9d161d7ef3ef..0bb029e6a1eb 100644
--- a/fs/dax.c
+++ b/fs/dax.c
@@ -1281,11 +1281,13 @@ static int dax_iomap_direct_access(const struct iomap *iomap, loff_t pos,
 		rc = length;
 		goto out;
 	}
-	if (!pfnp)
-		goto out_check_addr;
 	rc = -EINVAL;
 	if (PFN_PHYS(length) < size)
 		goto out;
+	if (!pfnp) {
+		rc = 0;
+		goto out_check_addr;
+	}
 	if (*pfnp & (PHYS_PFN(size)-1))
 		goto out;
 
@@ -1688,18 +1690,19 @@ static int dax_iomap_iter(struct iomap_iter *iomi, struct iov_iter *iter)
 			break;
 		}
 
+		map_len = PFN_PHYS(map_len);
+		map_len -= offset;
+		if (map_len > end - pos)
+			map_len = end - pos;
+
 		if (cow) {
-			ret = dax_iomap_copy_around(pos, length, PAGE_SIZE,
+			ret = dax_iomap_copy_around(pos, map_len, PAGE_SIZE,
 						    srcmap, kaddr);
 			if (ret)
 				break;
 		}
 
-		map_len = PFN_PHYS(map_len);
 		kaddr += offset;
-		map_len -= offset;
-		if (map_len > end - pos)
-			map_len = end - pos;
 
 		if (recovery)
 			xfer = dax_recovery_write(dax_dev, pgoff, kaddr,
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] fsdax: iterate over short dax_direct_access() ranges
  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-01 17:43 ` Amit Prasad
  2026-12-01 18:00   ` sashiko-bot
  2026-12-01 18:33 ` [PATCH 0/2] fsdax: handle " Amit Prasad
  2 siblings, 1 reply; 6+ messages in thread
From: Amit Prasad @ 2026-12-01 17:43 UTC (permalink / raw)
  To: djbw, brauner, viro
  Cc: jack, willy, djwong, alison.schofield, nvdimm, linux-fsdevel,
	Amit Prasad

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] fsdax: iterate over short dax_direct_access() ranges
  2026-12-01 17:43 ` [PATCH 2/2] fsdax: iterate over short dax_direct_access() ranges Amit Prasad
@ 2026-12-01 18:00   ` sashiko-bot
  2026-12-01 18:37     ` Amit Prasad
  0 siblings, 1 reply; 6+ messages in thread
From: sashiko-bot @ 2026-12-01 18:00 UTC (permalink / raw)
  To: Amit Prasad; +Cc: nvdimm

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] dax: out-of-bounds read in dax_range_compare_iter() [1]
--

commit 40d736e05a044ff4e08b902dc2515a07d60818ac
Author: Amit Prasad <amit@modal.com>

fsdax: iterate over short dax_direct_access() ranges

This commit adds dax_iomap_kaddr() and dax_iomap_copy_from() so that
copy-on-write, unshare, and range comparison iterate over short
dax_direct_access() mappings instead of failing when a range crosses a
device mapping boundary.

> diff --git a/fs/dax.c b/fs/dax.c
> index 0bb029e6a1ebb..6da37f952e6a6 100644
> --- a/fs/dax.c
> +++ b/fs/dax.c

[ ... ]

> @@ -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);

[Severity: Critical]
This problem wasn't introduced by this patch, but can [1] clamping
len to min(smap->length, dmap->length) instead of the remaining bytes in
each extent cause memcmp() to read past the end of the extent?

When pos1 or pos2 starts partway into an extent (either on the initial
dedupe offset or after a partial advance), the remaining extent lengths
are iomap_length(it_src) and iomap_length(it_dest) (i.e.,
smap->offset + smap->length - pos1).  Using the full smap->length and
dmap->length lets dax_iomap_kaddr() and memcmp() read beyond the extent
boundary, and also triggers the count > iomap_length(iter) warning in
iomap_iter_advance().

[1] https://sashiko.dev/#/bug/linux-39a2e02f-3d99-4256-9571-cb0e67941bdc

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201174324.5991-1-amit@modal.com?part=2

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/2] fsdax: handle short dax_direct_access() ranges
  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-01 17:43 ` [PATCH 2/2] fsdax: iterate over short dax_direct_access() ranges Amit Prasad
@ 2026-12-01 18:33 ` Amit Prasad
  2 siblings, 0 replies; 6+ messages in thread
From: Amit Prasad @ 2026-12-01 18:33 UTC (permalink / raw)
  To: apopple, brauner, viro
  Cc: jack, willy, djwong, alison.schofield, nvdimm, linux-fsdevel

Adding Alistair Popple, I missed this notice, sorry about that:
https://lore.kernel.org/nvdimm/20261110210152.3494345-1-dave.jiang@intel.com/

Full series at:
https://lore.kernel.org/nvdimm/20261201174324.5991-1-amit@modal.com/

Amit

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] fsdax: iterate over short dax_direct_access() ranges
  2026-12-01 18:00   ` sashiko-bot
@ 2026-12-01 18:37     ` Amit Prasad
  0 siblings, 0 replies; 6+ messages in thread
From: Amit Prasad @ 2026-12-01 18:37 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: nvdimm

On Tue, Dec 1, 2026 at 1:00 PM <sashiko-bot@kernel.org> wrote:
> [Severity: Critical]
> This problem wasn't introduced by this patch, but can [1] clamping
> len to min(smap->length, dmap->length) instead of the remaining bytes in
> each extent cause memcmp() to read past the end of the extent?

My understanding is that pos1/2 never starts partway into an extent
as every iomap_iter() call asks the filesystem for a new mapping at the
current position, the offset is block-aligned, and XFS (the only caller)
trims mappings to start at the block that holds pos. In other words,
smap->offset == pos1 and dmap->offset == pos2 on every call.

This is probably best addressed in a separate patch.

Amit

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-01 18:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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-01 17:43 ` [PATCH 2/2] fsdax: iterate over short dax_direct_access() ranges Amit Prasad
2026-12-01 18:00   ` sashiko-bot
2026-12-01 18:37     ` Amit Prasad
2026-12-01 18:33 ` [PATCH 0/2] fsdax: handle " Amit Prasad

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.