* [PATCH v2 0/2] fsdax: handle short dax_direct_access() ranges
@ 2026-12-03 2:21 Amit Prasad
2026-12-03 2:21 ` [PATCH v2 1/2] fsdax: check the mapped length for all dax_iomap_direct_access() callers Amit Prasad
2026-12-03 2:21 ` [PATCH v2 2/2] fsdax: iterate over short dax_direct_access() ranges Amit Prasad
0 siblings, 2 replies; 4+ messages in thread
From: Amit Prasad @ 2026-12-03 2:21 UTC (permalink / raw)
To: apopple, 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 bug was reproduced and fixed with the help of an AI coding
assistant (Claude Opus 5.5).
Changes since v1:
- Patch 1: describe the new SIGBUS condition in the commit message.
- Patch 2: add Alistair's Acked-by.
- No code changes.
v1: https://lore.kernel.org/nvdimm/20261201174324.5991-1-amit@modal.com/
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] 4+ messages in thread
* [PATCH v2 1/2] fsdax: check the mapped length for all dax_iomap_direct_access() callers
2026-12-03 2:21 [PATCH v2 0/2] fsdax: handle short dax_direct_access() ranges Amit Prasad
@ 2026-12-03 2:21 ` Amit Prasad
2026-12-03 2:41 ` sashiko-bot
2026-12-03 2:21 ` [PATCH v2 2/2] fsdax: iterate over short dax_direct_access() ranges Amit Prasad
1 sibling, 1 reply; 4+ messages in thread
From: Amit Prasad @ 2026-12-03 2:21 UTC (permalink / raw)
To: apopple, 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.
A PMD-sized copy-on-write fault with a short source now fails with
SIGBUS instead of silently copying out-of-range data.
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] 4+ messages in thread
* [PATCH v2 2/2] fsdax: iterate over short dax_direct_access() ranges
2026-12-03 2:21 [PATCH v2 0/2] fsdax: handle short dax_direct_access() ranges Amit Prasad
2026-12-03 2:21 ` [PATCH v2 1/2] fsdax: check the mapped length for all dax_iomap_direct_access() callers Amit Prasad
@ 2026-12-03 2:21 ` Amit Prasad
1 sibling, 0 replies; 4+ messages in thread
From: Amit Prasad @ 2026-12-03 2:21 UTC (permalink / raw)
To: apopple, 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.
Acked-by: Alistair Popple <apopple@nvidia.com>
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] 4+ messages in thread
* Re: [PATCH v2 1/2] fsdax: check the mapped length for all dax_iomap_direct_access() callers
2026-12-03 2:21 ` [PATCH v2 1/2] fsdax: check the mapped length for all dax_iomap_direct_access() callers Amit Prasad
@ 2026-12-03 2:41 ` sashiko-bot
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-03 2:41 UTC (permalink / raw)
To: Amit Prasad; +Cc: nvdimm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] fsdax: uninitialized CoW extent committed on
dax_iomap_copy_around() fault error
--
commit 8f94c2f57aa6785f8bab5e4a4dcfe7614a1a1370
Author: Amit Prasad <amit@modal.com>
fsdax: check the mapped length for all dax_iomap_direct_access() callers
This commit checks the mapped length returned by dax_direct_access() in
dax_iomap_direct_access() even when pfnp is NULL, and clamps the length
passed to dax_iomap_copy_around() in dax_iomap_iter() to the mapped
range.
> diff --git a/fs/dax.c b/fs/dax.c
> index 9d161d7ef3ef7..0bb029e6a1ebb 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;
> + }
[Severity: High]
Can a PMD-sized copy-on-write fault with a short source map commit an
uninitialized 2MB CoW extent into the inode data fork when
dax_iomap_copy_around() fails?
In dax_fault_iter(), dax_insert_entry() associates the newly allocated
destination CoW PFN and installs it into mapping->i_pages before calling
dax_iomap_copy_around(). When srcmap maps less than PMD_SIZE (such as
across a device-mapper target boundary), dax_iomap_direct_access() now
returns -EINVAL, causing dax_iomap_copy_around() to fail with -EIO and
dax_fault_iter() to return VM_FAULT_SIGBUS without copying the source
data:
fs/dax.c:dax_fault_iter() {
...
*entry = dax_insert_entry(xas, vmf, iter, *entry, pfn, entry_flags);
if (write && iomap->flags & IOMAP_F_SHARED) {
err = dax_iomap_copy_around(pos, size, size, srcmap, kaddr);
if (err)
return dax_fault_return(err);
}
...
}
Because dax_iomap_pmd_fault() advances the iomap iterator whenever the
return value is not VM_FAULT_FALLBACK, it advances iter by PMD_SIZE on
VM_FAULT_SIGBUS:
fs/dax.c:dax_iomap_pmd_fault() {
...
ret = dax_fault_iter(vmf, &iter, pfnp, &xas, &entry, true);
if (ret != VM_FAULT_FALLBACK)
iter.status = iomap_iter_advance(&iter, PMD_SIZE);
...
}
xfs_dax_write_iomap_end() then receives written == PMD_SIZE and calls
xfs_reflink_end_cow() instead of xfs_reflink_cancel_cow_range(),
remapping the uncopied CoW extent into the file and corrupting 2MB of
file data (while on a failed PTE CoW fault, written is 0 so
xfs_reflink_cancel_cow_range() frees the CoW block that dax_insert_entry()
already installed into mapping->i_pages).
The follow-up commit "fsdax: iterate over short dax_direct_access()
ranges" updates dax_iomap_copy_around() to iterate over short srcmap
mappings, but this commit is tagged for stable and leaves the fault error
path in dax_fault_iter() and dax_iomap_pmd_fault() broken whenever
dax_iomap_copy_around() fails.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203022139.1022927-1-amit@modal.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-03 2:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 2:21 [PATCH v2 0/2] fsdax: handle short dax_direct_access() ranges Amit Prasad
2026-12-03 2:21 ` [PATCH v2 1/2] fsdax: check the mapped length for all dax_iomap_direct_access() callers Amit Prasad
2026-12-03 2:41 ` sashiko-bot
2026-12-03 2:21 ` [PATCH v2 2/2] fsdax: iterate over short dax_direct_access() ranges 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.