* [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