From: liubaolin <liubaolin12138@163.com>
To: Hongling Zeng <zenghongling@kylinos.cn>,
linkinjeon@kernel.org, hyc.lee@gmail.com
Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org,
zhongling0719@126.com, stable@vger.kernel.org
Subject: Re: [PATCH 3/6] ntfs: read initialized_size under size_lock in the iomap paths
Date: Wed, 25 Nov 2026 22:07:43 +0800 [thread overview]
Message-ID: <e291353e-7aa2-4c78-97e2-b5c8d2acbb93@163.com> (raw)
In-Reply-To: <20261125073547.154150-4-zenghongling@kylinos.cn>
在 2026/11/25 15:35, Hongling Zeng 写道:
> The iomap read paths read ni->initialized_size without size_lock.
> ntfs_read_iomap_begin_non_resident() reads it twice in the length
> adjustment condition and once more in round_up(), so a concurrent
> update in between lets the condition pass with one value while the
> length is computed from another, which can yield a length matching
> neither boundary.
>
> Read initialized_size once under size_lock in the resident clamp,
> the non-resident unwritten mapping, the length adjustment and
> ntfs_iomap_put_folio_non_resident(), so each decision is made on one
> value.
>
> The buffered write and direct I/O paths read initialized_size
> unlocked too, when deciding whether the head or the tail of a newly
> allocated cluster has to be zeroed first. The mmap write fault path
> can extend initialized_size while holding neither the inode lock nor
> the MFT record lock, so snapshot the field under size_lock there as
> well.
>
> Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> ---
> fs/ntfs/iomap.c | 50 +++++++++++++++++++++++++++++++++++++++----------
> 1 file changed, 40 insertions(+), 10 deletions(-)
>
> diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
> index 8933f432aa68..afc4494822e1 100644
> --- a/fs/ntfs/iomap.c
> +++ b/fs/ntfs/iomap.c
> @@ -17,11 +17,14 @@ static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos,
> {
> struct ntfs_inode *ni = NTFS_I(inode);
> unsigned long sector_size = 1UL << inode->i_blkbits;
> + unsigned long flags;
> loff_t start_down, end_up, init;
>
> start_down = round_down(pos, sector_size);
> end_up = (pos + len - 1) | (sector_size - 1);
> + read_lock_irqsave(&ni->size_lock, flags);
> init = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, flags);
>
> if (init >= start_down && init <= end_up) {
> if (init < pos) {
> @@ -88,6 +91,7 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
> loff_t i_size;
> u32 attr_len;
> int err = 0;
> + unsigned long irqflags;
> char *kattr;
>
> base_ni = ntfs_base_inode(ni);
> @@ -106,8 +110,10 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo
> goto out;
>
> attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
> + read_lock_irqsave(&ni->size_lock, irqflags);
> if (unlikely(attr_len > ni->initialized_size))
> attr_len = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, irqflags);
> i_size = i_size_read(inode);
>
> if (unlikely(attr_len > i_size)) {
> @@ -200,6 +206,7 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
> struct ntfs_volume *vol = ni->vol;
> loff_t vcn_ofs;
> loff_t rl_length;
> + unsigned long irqflags;
>
> vcn = ntfs_bytes_to_cluster(vol, offset);
> vcn_ofs = ntfs_bytes_to_cluster_off(vol, offset);
> @@ -231,7 +238,12 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
> iomap->type = IOMAP_HOLE;
> iomap->addr = IOMAP_NULL_ADDR;
> } else {
> - if (need_unwritten && offset >= ni->initialized_size)
> + loff_t initialized_size;
> +
> + read_lock_irqsave(&ni->size_lock, irqflags);
> + initialized_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, irqflags);
> + if (need_unwritten && offset >= initialized_size)
> iomap->type = IOMAP_UNWRITTEN;
> else
> iomap->type = IOMAP_MAPPED;
> @@ -255,11 +267,17 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset
> up_write(&ni->runlist.lock);
>
> if (!(flags & IOMAP_ZERO) &&
> - iomap->type == IOMAP_MAPPED &&
> - iomap->offset < ni->initialized_size &&
> - iomap->offset + iomap->length > ni->initialized_size) {
> - iomap->length = round_up(ni->initialized_size, 1 << inode->i_blkbits) -
> - iomap->offset;
> + iomap->type == IOMAP_MAPPED) {
> + loff_t initialized_size;
> +
> + read_lock_irqsave(&ni->size_lock, irqflags);
> + initialized_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, irqflags);
> + if (iomap->offset < initialized_size &&
> + iomap->offset + iomap->length > initialized_size) {
> + iomap->length = round_up(initialized_size, 1 << inode->i_blkbits) -
> + iomap->offset;
> + }
> }
> iomap->flags |= IOMAP_F_MERGED;
>
> @@ -383,6 +401,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
> struct runlist_element *rl, *rlc;
> bool is_retry = false;
> int err = 0, map_err = 0;
> + unsigned long flags;
> s64 vcn, lcn;
> s64 max_clu_count =
> ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size));
> @@ -511,9 +530,14 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
> iomap->flags = IOMAP_F_NEW;
> if (lcn <= LCN_HOLE) {
> loff_t end = offset + length;
> + loff_t initialized_size;
> +
> + read_lock_irqsave(&ni->size_lock, flags);
> + initialized_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, flags);
>
> if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
> - end < ni->initialized_size)) {
> + end < initialized_size)) {
> loff_t z_start, z_end;
>
> z_start = vcn << vol->cluster_size_bits;
> @@ -527,7 +551,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_
> if ((!err || err == -EPERM) &&
> max_clu_count > 1 &&
> (iomap->length & vol->cluster_size_mask &&
> - end < ni->initialized_size)) {
> + end < initialized_size)) {
> loff_t z_start, z_end;
>
> z_start = (vcn + max_clu_count - 1) <<
> @@ -581,6 +605,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
> s64 vcn, start_lcn, lcn_count;
> bool balloc = false, update_mp;
> int err;
> + unsigned long irqflags;
> s64 max_clu_count =
> ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size));
>
> @@ -622,16 +647,21 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
> if (flags & IOMAP_DIRECT ||
> ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) {
> loff_t end = offset + length;
> + loff_t initialized_size;
> +
> + read_lock_irqsave(&ni->size_lock, irqflags);
> + initialized_size = ni->initialized_size;
> + read_unlock_irqrestore(&ni->size_lock, irqflags);
>
> if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
> - end < ni->initialized_size))
> + end < initialized_size))
> err = ntfs_dio_zero_range(inode,
> start_lcn <<
> vol->cluster_size_bits,
> vol->cluster_size);
> if (!err && lcn_count > 1 &&
> (iomap->length & vol->cluster_size_mask &&
> - end < ni->initialized_size))
> + end < initialized_size))
> err = ntfs_dio_zero_range(inode,
> (start_lcn + lcn_count - 1) <<
> vol->cluster_size_bits,
Looks good to me.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
next prev parent reply other threads:[~2026-11-25 14:08 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 7:35 [PATCH 0/6] ntfs: synchronize the in-memory size handling Hongling Zeng
2026-11-25 7:35 ` [PATCH 1/6] ntfs: mark the record dirtied by a resident write at iomap begin Hongling Zeng
2026-11-26 8:45 ` Hyunchul Lee
2026-11-26 9:31 ` dd
2026-12-01 4:19 ` Hyunchul Lee
2026-12-01 7:46 ` dd
2026-11-25 7:35 ` [PATCH 2/6] ntfs: roll back the size update when the attribute lookup fails Hongling Zeng
2026-11-25 14:05 ` liubaolin
2026-11-26 6:34 ` dd
2026-11-25 7:35 ` [PATCH 3/6] ntfs: read initialized_size under size_lock in the iomap paths Hongling Zeng
2026-11-25 14:07 ` liubaolin [this message]
2026-11-26 8:45 ` Hyunchul Lee
2026-11-26 9:34 ` dd
2026-11-25 7:35 ` [PATCH 4/6] ntfs: update the in-memory sizes under the size lock Hongling Zeng
2026-11-25 14:08 ` liubaolin
2026-11-25 7:35 ` [PATCH 5/6] ntfs: serialize the mmap write fault initialized size update Hongling Zeng
2026-11-25 14:08 ` liubaolin
2026-11-25 7:35 ` [PATCH 6/6] ntfs: read initialized_size under size_lock in the mmap prepare path Hongling Zeng
2026-11-25 14:08 ` liubaolin
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=e291353e-7aa2-4c78-97e2-b5c8d2acbb93@163.com \
--to=liubaolin12138@163.com \
--cc=hyc.lee@gmail.com \
--cc=linkinjeon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ntfs@lists.linux.dev \
--cc=stable@vger.kernel.org \
--cc=zenghongling@kylinos.cn \
--cc=zhongling0719@126.com \
/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 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.