Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.177 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796147041; b=bWCoCOh29a/ZtK0V8+xeIc9wzazLH9PTtExrhYic+qBSjWHRAQuifNcgdpnD+z3Fvgtc kBCF3CuQdScMoYVQQCWeaANeLjsoTuBdjk3Ny8HkpkzdX54Lv36/8vmY9KwEbBdALclhc 5zCTC1PIpGIpEIxo6JqDTodGWjXAqOTlIK9sUiNxN0ceoFVw2md5XkJzZXr668tGlWqst Idki8oe+h3B6Bg3yeezFPeiwEtcmfeoQfZ/ei5jQn9qX4y46yPPe7Mfm4u/EZlb8fFlD3 FWJBwcriubJCad6bXrjLlwHYCEwhh8qnfXijm0OQIMIhR2tVVo/dqxGuOy8SFHiqQHA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796147041; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=uaa2NMQ2eDzs9xulL0hf7B9YwznHuo7jD6SC8e63kKc=; b=2WsjOMKBVTeS/7AxIjvpK1yiG3OjQRqaGEZGECXwlfCRawwJh31SGwBndTjxl6PK0GEr xaSBM/Kl0Kqevu6x+OOvzhtEPCxKEod6tS+l54rzXJtw6jKIFdXjOKRS3wYSwD0B5NB6i F8vEUOE1fDPZ/1ZVjVo3jl9zmBMBTKfKHLmO+798Q2jBY4hcX+S/+3fpgrwqhzp+ybnfw Fn2YUFRx/edzDKGqHfnEjzIvvj4ffMvxJmzCAjZCNdkcVKAhFutfMyE4dTWtYXusb3vdP 1zycsAlnPC7rN4FbRjw3p5sOSDbbTkv3/3kdv35q6AhoWCa7X88KYEGpnkRjs4M8VJA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=modal.com; spf=pass smtp.mailfrom=modal.com; dkim=pass header.d=modal.com header.i=@modal.com header.a=rsa-sha256 header.s=google header.b=ZWs19OQB; arc=none smtp.remote-ip=209.85.222.177 Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 717CD606A32A for ; Tue, 1 Dec 2026 17:44:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 717CD606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=modal.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=modal.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=modal.com header.i=@modal.com header.a=rsa-sha256 header.s=google header.b=ZWs19OQB Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-93f2f2edb19so992513485a.0 for ; Tue, 01 Dec 2026 09:44:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=modal.com; s=google; t=1796147039; x=1796751839; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uaa2NMQ2eDzs9xulL0hf7B9YwznHuo7jD6SC8e63kKc=; b=ZWs19OQBF48YecvzbxC7nThFNitc06jNIm1Qh2SAGz29WLgj8uWEnEOOzzNpj1OaFY AUyKHFBRhmGcBLDq5lEczGjEcTjEr8NDH7lI6+6aMp2TRzy5E0ogmg/XPeSAZSkk91Ue 2u6HI8tIZJ3Ezhhn+i27Bz5VU8q6ua9IkKb3SA+LZ78Cr+0EGeSH3PmM4cf5BTyeONXk McnxDZGHpzlkC1PMwNJkpbkQtHf1tp3Lus3bdk4u1/5SCA6HEwhBnQhxEP2XyNHM5C91 asNLB9WSVmzIk01ziJ+yhrsF43C/wW/Th+ck5ojyjaKTtJZOISCrYMbvYxWtjc4tpmkn 6CeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796147039; x=1796751839; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=uaa2NMQ2eDzs9xulL0hf7B9YwznHuo7jD6SC8e63kKc=; b=PD90OtaQcfJua9zDH2gofOgHDfTH0odSoBW6lqS3HtYL1pnhqghEr2/5NIu5Qoeh0Z 2s3SQmRHv8sETTH39UB7XXrfgWmopYnUdwUdMX7uoin3J6xDYsuadi0iQTuJaLCf8nMj SvJ0oN8L8t7yvzweHpjvKlxxePjo5gsmJtRgggiMxH5L03rWsjHeu4l+HBLOHqofXUFA 4AH+NkqQqJPztMrzdIjOdXlpW2UIS/Jw1B5MXZndjDEdy5m2SWbTku+HfwYs8zJOPe0D P7nsNpldRv9NHfiWPm++QVqQs6D/ZRITe1WRdhCOq43Zas38d+eWQu5hTjv+wfx+v95d 8jtw== X-Forwarded-Encrypted: i=1; AL+hErHmgRjCKugJ8O7J3n2yGdDpwh0xo1QwdI7pmk+ElsUJI45+vUO0/LHoTFkfyhvDEbDFUOeMvVzCo/H/x6Yl@vger.kernel.org X-Gm-Message-State: APuU4Z9oWgMgGd6t1lq2ypBLu+thwLIK4NqFJJb9IeD0Y6scIVvSLIsY /HV71Yt7JgJQL4PqeCPJaEUUZ3qKXq8pF/twJXKvMuK2yRngX6Cl2/9sQQlSbBKsLnZ37eLIOwM Yq39BUPhtMQ== X-Gm-Gg: AZFdTZdTf1EYAC715IT1vFe86TBtzFC71wz55pYaWk0M8QlfMYEYkAfNui07TJEbdt2 618t1RH4kOiyJx+aR8r9Tp22jSqWTq+3r/bGsFwzdLdLA4QUIzuMu6R2ERcf7M8wUKZNYuJaxdc gwD/CqXF7yxb7d7ODSmwGV2UKUUVHMx/rgMC8MCv/WcumkH5k1ZJ9A0Abgsrnf1hz5Nw3deiWzC oicy58kmClzTgb4alOvGlZwh/buK3rh+38bVDa4oDzkBSFoIfmrrWQVtUVKlnzQ3hBAppvPYS2y jRlI6OBMoiFEsPqkO07AN1f15EZS1VugEHQzadsPYp/dr/qAGpQwVFcZdVPBzrkI0JqkOWN7VMG 960SW/QWtqwIcBbOeXJiGh4rJnmjrFRPRjE3eJonkpphI/Rk4WmoL+5NPE1oDvZWePxidtvpaop 7hjK6nuP42B0SiRSjHoN/ggKhMj22p0cYItRM1HJCNFMRTUP1IZV8A4a7P5m4wNAhsni6ow6+WR XkB+P5yvEd68vN06j1R1IgcunTCkJFLCT0PlxHmMuNa0NXtNpukXsXKVa4mgReOVuiquuHSJziy 5getVJVb6vq4YVwoUfiprp3rZoNilaQ2pu8SVLFkqtcfZ3IZhtfnH9KO7Clh0lx0DZJTFa9h9RD nVK4VZrvtpnbndbL7MQ== X-Received: by 2002:a05:620a:371d:b0:93f:3e39:efa1 with SMTP id af79cd13be357-949ba9c6d0fmr7332731385a.23.1796147039085; Tue, 01 Dec 2026 09:43:59 -0800 (PST) Received: from devbox-amit-63ec.ec2.internal (ec2-44-214-141-241.compute-1.amazonaws.com. [44.214.141.241]) by smtp.gmail.com with ESMTPSA id af79cd13be357-94a017cfb46sm1840863585a.28.2026.12.01.09.43.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 09:43:58 -0800 (PST) From: Amit Prasad 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 Subject: [PATCH 2/2] fsdax: iterate over short dax_direct_access() ranges Date: Tue, 1 Dec 2026 17:43:24 +0000 Message-ID: <20261201174324.5991-3-amit@modal.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261201174324.5991-1-amit@modal.com> References: <20261201174324.5991-1-amit@modal.com> X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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