From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=117.135.210.2 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796010599; b=IU7omeIWVQ6WT4H/hi+HXFtWXOWZCY+WD1BMlv4at7YBDd6V4U14yG2HVbi3Jm6nHJfh KGOjuLMkQe5qiKUAdz6Aw7bph4uj0uFYqbwx80mYbN+REnQzoK75KEoMxiuKBB34vRm+l WHqygeoXJjjECniRHV62GFwQDxJgFLNh9+Mcz+0yXn242eH7nqGuqrazBjfMr/aY9Jkoi IWnUn6qNC87kmjDcPhDUWrbY6v8D55KIJIey03x2CDE+yzYZ78EHoEHJaRA+W81KNMo0u 94ZPdVRnEKywwnGdg6cIO0pKQ0Aq+xMpph1eyfiX1KL3OhMcHDaH6k3wGIGHtYOwxOg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796010599; h=DMARC-Filter:DKIM-Signature:Received:Message-ID:Date:MIME-Version: User-Agent:Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding:X-CM-TRANSID: X-Coremail-Antispam:X-Originating-IP:X-CM-SenderInfo; bh=7ZIpJn7NL/fyQhtTOr/f47bg9EpXKVTtRysLL4OAwG4=; b=I7MNA4H5lqSQ++aTBB775UK1/PqjD9MhTzjDcaSTrCQm04/zmVZOgMMceKL+4CuRtFH7 lUCa2hFVIjpNlYuiD5pYdz28rXllWPP+BFKcp+4qmZ983pdCKHEeOkdeEOZ3hcBG9msBZ YDKlpu74SQ2oQ4BjCA/f6fvI/81GRlMEf6sbNrRpGNzUYHZH/PV+6tpNq7lTB3hBmPMuo GHCjIYKFB06aUmK3z1Xn0LU7uq6h4cGIUi/VywyUDoRLvgX1XGuubdvbLJdXbN7WWAdNx oshydVbVFdgQJaSB6+BpzH5J6OS0vnbe/91IoZfTrTYHjBuxJHH7lUT7pM55ldY7TcA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=a4zcR6Gl; arc=none smtp.remote-ip=117.135.210.2 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F43F606A327; Mon, 30 Nov 2026 03:49:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2F43F606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=a4zcR6Gl DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=Message-ID:Date:MIME-Version:Subject:To:From: Content-Type; bh=7ZIpJn7NL/fyQhtTOr/f47bg9EpXKVTtRysLL4OAwG4=; b=a4zcR6GlQm72Vw0J+F6eYUQIEfANmFjuJ3SsCkCU9zLaOBVAiinsTG+bKA1GGO iqkBTPlC/TNBSOnJi2elmCrEqtS+pMosySpFzHgw+J3XvMxbikge5GtZH+w2U/pI 6DUa1TO9D6OAgwQMjxElwCpzh0rbejI22Ze6cW39SbYTo= Received: from [IPV6:2409:8900:6ae:3295:bdf5:7149:7d26:d2ca] (unknown []) by gzsmtp5 (Coremail) with SMTP id QCgvCgCnSaFJ8gxrHaXKIw--.64276S2; Mon, 30 Nov 2026 11:49:30 +0800 (CST) Message-ID: <732588d6-977b-4c93-925c-56c55e750c05@163.com> Date: Mon, 30 Nov 2026 11:49:29 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 3/6] ntfs: read initialized_size under size_lock in the iomap paths To: Hongling Zeng , linkinjeon@kernel.org, hyc.lee@gmail.com Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20261130030708.59798-1-zenghongling@kylinos.cn> <20261130030708.59798-4-zenghongling@kylinos.cn> Content-Language: en-US From: liubaolin In-Reply-To: <20261130030708.59798-4-zenghongling@kylinos.cn> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:QCgvCgCnSaFJ8gxrHaXKIw--.64276S2 X-Coremail-Antispam: 1Uf129KBjvJXoW3JF4xJw4xCw45tFW7WFWDXFb_yoW3AFyUpr 9F9FZ8GF43Xr92gws5twn8XayF9348Kw1UuryfGw1fA3s3trn2gF1Fka45uFWjqrZrAF1v vF4qqw48ua1DZF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jedgAUUUUU= X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6gqsd2sM8krx0QAA3E 在 2026/11/30 11:07, 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, > ntfs_iomap_put_folio_non_resident() and the bio completion path > ntfs_iomap_read_end_io(), so each decision is made on one value. > In ntfs_read_iomap_begin_non_resident() the unwritten mapping > decision and the length adjustment share that snapshot, so the type > and the length of a mapped extent cannot be decided against different > initialized_size boundaries. > > 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") > Reported-by: Hyunchul Lee > Cc: stable@vger.kernel.org > Signed-off-by: Hongling Zeng > --- > fs/ntfs/aops.c | 3 +++ > fs/ntfs/iomap.c | 39 +++++++++++++++++++++++++++++++-------- > 2 files changed, 34 insertions(+), 8 deletions(-) > > diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c > index d0d1a8402bc4..82d3ebbc0972 100644 > --- a/fs/ntfs/aops.c > +++ b/fs/ntfs/aops.c > @@ -24,10 +24,13 @@ static void ntfs_iomap_read_end_io(struct bio *bio) > bio_for_each_folio_all(iter, bio) { > struct folio *folio = iter.folio; > struct ntfs_inode *ni = NTFS_I(folio->mapping->host); > + unsigned long flags; > s64 init_size; > loff_t pos = folio_pos(folio); > > + read_lock_irqsave(&ni->size_lock, flags); > init_size = ni->initialized_size; > + read_unlock_irqrestore(&ni->size_lock, flags); > if (pos + iter.offset < init_size && > pos + iter.offset + iter.length > init_size) > folio_zero_segment(folio, offset_in_folio(folio, init_size), > diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c > index 8933f432aa68..a9c04fb964ba 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,8 @@ 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; > + loff_t initialized_size; > + unsigned long irqflags; > > vcn = ntfs_bytes_to_cluster(vol, offset); > vcn_ofs = ntfs_bytes_to_cluster_off(vol, offset); > @@ -231,7 +239,10 @@ 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) > + 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; > @@ -256,9 +267,9 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset > > 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 < 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 +394,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 +523,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 +544,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 +598,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 +640,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