* [PATCH v6 2/6] ntfs: roll back the size update when the attribute lookup fails
[not found] <20261202054112.1138977-1-zenghongling@kylinos.cn>
@ 2026-12-02 5:41 ` Hongling Zeng
2026-12-03 2:38 ` kernel test robot
2026-12-05 13:38 ` kernel test robot
2026-12-02 5:41 ` [PATCH v6 3/6] ntfs: read initialized_size under size_lock in the iomap paths Hongling Zeng
` (2 subsequent siblings)
3 siblings, 2 replies; 6+ messages in thread
From: Hongling Zeng @ 2026-12-02 5:41 UTC (permalink / raw)
To: linkinjeon, hyc.lee, liubaolin12138
Cc: ntfs, linux-kernel, Hongling Zeng, stable, Baolin Liu
ntfs_enlarge_attribute() truncates the attribute to the end of the
write before looking it up again, because the truncation can move the
attribute to a different mft record. If the lookup fails, the write
fails but the attribute stays extended.
Undo the truncation before returning the error. A resident resize
shrinks the value in place without sleeping on a folio, so it is
safe under mrec_lock. A non-resident shrink has to drop the folios
beyond the old EOF, which cannot happen under mrec_lock: writeback
locks a folio and waits for mrec_lock in
ntfs_write_iomap_begin_non_resident(). Defer it to
ntfs_inode_attr_pwrite(), which releases mrec_lock and follows
ntfs_setattr_size(): truncate_setsize() outside the lock, then the
size update under it, after re-checking that no concurrent write
took over the extension. Roll the sizes back with
__ntfs_attr_truncate_vfs() and ntfs_attr_set_data_size(): only the
clusters the write allocated are freed, and the allocation that
predates the write is preserved. The old allocation is
snapshotted only for a non-resident attribute, since a resident
allocated_size counts bytes, not clusters. If the rollback fails, mark the
volume and sync i_size with data_size.
Only the named streams can drop the folios in the deferred rollback:
the stream ioctl holds the mapping invalidate lock across the write.
The internal attributes reach it under the base mrec_lock their
callers hold, and the folio locks would invert against it, since the
writeback and the resident readers take mrec_lock with the folio
locked. Their readers and writers serialize on the base mrec_lock,
their mappings stay kernel only, and the failed write dirtied no
folio, so the folios beyond the old EOF are left in place and i_size
is synced by hand.
Recompute the modified flag after the deferred rollback, so a failed
write the rollback restored completely does not update the base file
times of a named stream.
Fixes: af0db57d4293 ("ntfs: update inode operations")
Suggested-by: Hyunchul Lee <hyc.lee@gmail.com>
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Reviewed-by: Hyunchul Lee <hyc.lee@gmail.com>
---
fs/ntfs/inode.c | 109 +++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 104 insertions(+), 5 deletions(-)
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 3aac1d7d310f..4938c5f0c356 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -3849,12 +3849,20 @@ s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf)
}
static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
- struct ntfs_attr_search_ctx *ctx)
+ struct ntfs_attr_search_ctx *ctx,
+ bool *deferred_rollback)
{
struct ntfs_inode *ni = NTFS_I(vi);
struct super_block *sb = vi->i_sb;
+ unsigned long flags;
+ s64 old_data_size;
int ret;
+ /* Snapshot the rollback target under size_lock. */
+ read_lock_irqsave(&ni->size_lock, flags);
+ old_data_size = ni->data_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+
if (pos + count <= ni->initialized_size)
return 0;
@@ -3864,7 +3872,7 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
if (NInoCompressed(ni))
return -EOPNOTSUPP;
- if (pos + count > ni->data_size) {
+ if (pos + count > old_data_size) {
ret = ntfs_attr_truncate(ni, pos + count);
if (ret) {
ntfs_debug("Failed to truncate attribute");
@@ -3876,7 +3884,37 @@ static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count,
CASE_SENSITIVE,
0, NULL, 0, ctx);
if (ret) {
- ntfs_error(sb, "Failed to look up attr %#x", ni->type);
+ int rollback_err;
+
+ /* A resident resize never sleeps on a folio under mrec_lock. */
+ if (NInoNonResident(ni)) {
+ /*
+ * The shrink would drop folios under mrec_lock:
+ * let the caller roll back once the lock is out.
+ */
+ *deferred_rollback = true;
+ ntfs_error(sb, "Failed to look up attr %#x",
+ ni->type);
+ return ret;
+ }
+ rollback_err = ntfs_attr_truncate(ni, old_data_size);
+ if (rollback_err) {
+ s64 current_data_size;
+
+ /* The sizes may be inconsistent now. */
+ read_lock_irqsave(&ni->size_lock, flags);
+ current_data_size = ni->data_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+
+ i_size_write(vi, current_data_size);
+ NVolSetErrors(ni->vol);
+ ntfs_error(sb,
+ "Failed to look up attr %#x and roll back: %d",
+ ni->type, rollback_err);
+ } else {
+ ntfs_error(sb, "Failed to look up attr %#x",
+ ni->type);
+ }
return ret;
}
}
@@ -4096,6 +4134,54 @@ static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi,
return ret ? ret : written;
}
+/* Undo the deferred size update like ntfs_write_rollback() does. */
+static void ntfs_inode_rollback_enlarge(struct inode *vi,
+ struct ntfs_inode *mrec_ni, s64 new_data_size,
+ s64 old_data_size, s64 old_alloc_size)
+{
+ struct ntfs_inode *ni = NTFS_I(vi);
+ bool stream = ntfs_inode_is_named_stream(ni);
+ unsigned long flags;
+ s64 current_data_size;
+ int rollback_err = 0;
+
+ if (stream)
+ truncate_setsize(vi, old_data_size);
+
+ mutex_lock(&mrec_ni->mrec_lock);
+ read_lock_irqsave(&ni->size_lock, flags);
+ current_data_size = ni->data_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+ if (current_data_size == new_data_size) {
+ /* Free only what the write allocated. */
+ s64 keep = max(old_data_size, old_alloc_size);
+
+ if (ni->allocated_size > keep)
+ rollback_err = __ntfs_attr_truncate_vfs(ni, keep,
+ keep);
+ if (!rollback_err && ni->data_size != old_data_size)
+ rollback_err = ntfs_attr_set_data_size(ni,
+ old_data_size);
+ }
+ mutex_unlock(&mrec_ni->mrec_lock);
+ if (rollback_err) {
+ /* The sizes may be inconsistent now. */
+ read_lock_irqsave(&ni->size_lock, flags);
+ current_data_size = ni->data_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+ i_size_write(vi, current_data_size);
+ NVolSetErrors(ni->vol);
+ ntfs_error(vi->i_sb, "Failed to roll back the size update: %d",
+ rollback_err);
+ } else if (current_data_size != new_data_size) {
+ /* A concurrent write took over the extension. */
+ i_size_write(vi, current_data_size);
+ } else if (!stream) {
+ /* No folio was dropped. */
+ i_size_write(vi, old_data_size);
+ }
+}
+
s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool sync)
{
struct ntfs_inode *ni = NTFS_I(vi);
@@ -4103,9 +4189,11 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s
struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ?
base_ni : ni;
struct ntfs_attr_search_ctx *ctx;
- loff_t old_data_size, old_init_size;
+ loff_t old_data_size, old_init_size, old_alloc_size;
+ unsigned long flags;
s64 ret;
bool modified = false;
+ bool deferred_rollback = false;
WARN_ON(!NInoAttr(ni));
@@ -4152,7 +4240,9 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s
old_data_size = ni->data_size;
old_init_size = ni->initialized_size;
- ret = ntfs_enlarge_attribute(vi, pos, count, ctx);
+ old_alloc_size = NInoNonResident(ni) ? ni->allocated_size : 0;
+ ret = ntfs_enlarge_attribute(vi, pos, count, ctx,
+ &deferred_rollback);
modified = ni->data_size != old_data_size ||
ni->initialized_size != old_init_size;
if (ret)
@@ -4195,6 +4285,15 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s
ntfs_attr_put_search_ctx(ctx);
out_unlock:
mutex_unlock(&mrec_ni->mrec_lock);
+ if (deferred_rollback) {
+ ntfs_inode_rollback_enlarge(vi, mrec_ni, pos + count,
+ old_data_size, old_alloc_size);
+ /* The rollback may have restored the sizes. */
+ read_lock_irqsave(&ni->size_lock, flags);
+ modified = ni->data_size != old_data_size ||
+ ni->initialized_size != old_init_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+ }
if (modified && ntfs_inode_is_named_stream(ni))
ntfs_stream_update_base_time(base_ni);
return ret;
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v6 3/6] ntfs: read initialized_size under size_lock in the iomap paths
[not found] <20261202054112.1138977-1-zenghongling@kylinos.cn>
2026-12-02 5:41 ` [PATCH v6 2/6] ntfs: roll back the size update when the attribute lookup fails Hongling Zeng
@ 2026-12-02 5:41 ` Hongling Zeng
2026-12-02 5:41 ` [PATCH v6 5/6] ntfs: serialize the mmap write fault initialized size update Hongling Zeng
2026-12-02 5:41 ` [PATCH v6 6/6] ntfs: read initialized_size under size_lock in the mmap prepare path Hongling Zeng
3 siblings, 0 replies; 6+ messages in thread
From: Hongling Zeng @ 2026-12-02 5:41 UTC (permalink / raw)
To: linkinjeon, hyc.lee, liubaolin12138
Cc: ntfs, linux-kernel, Hongling Zeng, stable, Baolin Liu
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 <hyc.lee@gmail.com>
Link: https://lore.kernel.org/r/awfxvg7w_i49JxBv@hyunchul-PC02
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Reviewed-by: Hyunchul Lee <hyc.lee@gmail.com>
---
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 cbc02715aab5..5b531fe39b90 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,
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v6 5/6] ntfs: serialize the mmap write fault initialized size update
[not found] <20261202054112.1138977-1-zenghongling@kylinos.cn>
2026-12-02 5:41 ` [PATCH v6 2/6] ntfs: roll back the size update when the attribute lookup fails Hongling Zeng
2026-12-02 5:41 ` [PATCH v6 3/6] ntfs: read initialized_size under size_lock in the iomap paths Hongling Zeng
@ 2026-12-02 5:41 ` Hongling Zeng
2026-12-02 5:41 ` [PATCH v6 6/6] ntfs: read initialized_size under size_lock in the mmap prepare path Hongling Zeng
3 siblings, 0 replies; 6+ messages in thread
From: Hongling Zeng @ 2026-12-02 5:41 UTC (permalink / raw)
To: linkinjeon, hyc.lee, liubaolin12138
Cc: ntfs, linux-kernel, Hongling Zeng, stable, Baolin Liu
The mmap write fault path checks whether the faulting range extends
past ni->initialized_size with an unlocked read and then calls
ntfs_attr_set_initialized_size(), which rewrites the field and the
MFT record with its argument unconditionally. The call is made
without the MFT record lock that every other caller of the function
holds, and a buffered write or a second fault extending the
initialized size between the read and the rewrite can make the
rewrite shrink the field again, which exposes initialized data as
uninitialized.
Hold the MFT record lock across the check and the update and read
initialized_size under size_lock inside the critical section, so the
decision is made on the value that is current for the record and the
field can no longer regress through the fault path.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Reviewed-by: Hyunchul Lee <hyc.lee@gmail.com>
---
fs/ntfs/attrib.c | 1 +
fs/ntfs/iomap.c | 15 +++++++++++----
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 77ee1972ef68..27aeaa66da29 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -2431,6 +2431,7 @@ int ntfs_attr_set(struct ntfs_inode *ni, s64 ofs, s64 cnt, const u8 val)
return ret;
}
+/* The caller must hold the base inode's mft record lock. */
int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
{
struct ntfs_attr_search_ctx *ctx;
diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c
index 5b531fe39b90..b5aa30c63bb6 100644
--- a/fs/ntfs/iomap.c
+++ b/fs/ntfs/iomap.c
@@ -669,10 +669,17 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode,
return err;
}
- if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE &&
- iomap->offset + iomap->length > ni->initialized_size) {
- err = ntfs_attr_set_initialized_size(ni,
- iomap->offset + iomap->length);
+ if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) {
+ loff_t initialized_size;
+
+ mutex_lock(&mrec_ni->mrec_lock);
+ read_lock_irqsave(&ni->size_lock, irqflags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, irqflags);
+ if (iomap->offset + iomap->length > initialized_size)
+ err = ntfs_attr_set_initialized_size(ni,
+ iomap->offset + iomap->length);
+ mutex_unlock(&mrec_ni->mrec_lock);
}
return err;
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v6 6/6] ntfs: read initialized_size under size_lock in the mmap prepare path
[not found] <20261202054112.1138977-1-zenghongling@kylinos.cn>
` (2 preceding siblings ...)
2026-12-02 5:41 ` [PATCH v6 5/6] ntfs: serialize the mmap write fault initialized size update Hongling Zeng
@ 2026-12-02 5:41 ` Hongling Zeng
3 siblings, 0 replies; 6+ messages in thread
From: Hongling Zeng @ 2026-12-02 5:41 UTC (permalink / raw)
To: linkinjeon, hyc.lee, liubaolin12138
Cc: ntfs, linux-kernel, Hongling Zeng, stable, Baolin Liu
ntfs_file_mmap_prepare() reads ni->initialized_size without the
size lock when deciding whether a shared writable mapping needs
its initialized size extended before the faults start. The field
is published under size_lock, so the read is a data race on the
protocol the other readers follow: a stale low value causes a
redundant call that __ntfs_extend_initialized_size() re-checks
under the lock, and a stale high value skips the extension and
leaves it to the write faults.
Take the snapshot under size_lock like the other readers.
Fixes: 9c87959601e8 ("ntfs: update file operations")
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
Reviewed-by: Hyunchul Lee <hyc.lee@gmail.com>
---
fs/ntfs/file.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index f5cad4b043f7..ede31de3cc75 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -1232,14 +1232,20 @@ int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
if (vma_desc_test_all(desc, VMA_SHARED_BIT, VMA_MAYWRITE_BIT)) {
struct inode *inode = file_inode(file);
+ struct ntfs_inode *ni = NTFS_I(inode);
loff_t from, to;
+ loff_t initialized_size;
+ unsigned long flags;
int err;
from = ((loff_t)desc->pgoff << PAGE_SHIFT);
to = min_t(loff_t, i_size_read(inode),
from + desc->end - desc->start);
- if (NTFS_I(inode)->initialized_size < to) {
+ read_lock_irqsave(&ni->size_lock, flags);
+ initialized_size = ni->initialized_size;
+ read_unlock_irqrestore(&ni->size_lock, flags);
+ if (initialized_size < to) {
err = ntfs_extend_initialized_size(inode, to, to);
if (err)
return err;
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v6 2/6] ntfs: roll back the size update when the attribute lookup fails
2026-12-02 5:41 ` [PATCH v6 2/6] ntfs: roll back the size update when the attribute lookup fails Hongling Zeng
@ 2026-12-03 2:38 ` kernel test robot
2026-12-05 13:38 ` kernel test robot
1 sibling, 0 replies; 6+ messages in thread
From: kernel test robot @ 2026-12-03 2:38 UTC (permalink / raw)
To: Hongling Zeng, linkinjeon, hyc.lee, liubaolin12138
Cc: llvm, oe-kbuild-all, ntfs, linux-kernel, Hongling Zeng, stable,
Baolin Liu
Hi Hongling,
kernel test robot noticed the following build errors:
[auto build test ERROR on next-20261130]
[also build test ERROR on v7.4-rc5]
[cannot apply to brauner-vfs/vfs.all linus/master v7.4-rc5 v7.4-rc4 v7.4-rc3]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Hongling-Zeng/ntfs-roll-back-the-size-update-when-the-attribute-lookup-fails/20261202-134108
base: next-20261130
patch link: https://lore.kernel.org/r/20261202054112.1138977-3-zenghongling%40kylinos.cn
patch subject: [PATCH v6 2/6] ntfs: roll back the size update when the attribute lookup fails
config: s390-defconfig (https://download.01.org/0day-ci/archive/20261203/20261203102615.WgRQ2XEm-lkp@intel.com/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261203/20261203102615.WgRQ2XEm-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/20261203102615.WgRQ2XEm-lkp@intel.com/
All errors (new ones prefixed by >>):
>> fs/ntfs/inode.c:4173:19: error: call to undeclared function 'ntfs_attr_set_data_size'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
4173 | rollback_err = ntfs_attr_set_data_size(ni,
| ^
1 error generated.
vim +/ntfs_attr_set_data_size +4173 fs/ntfs/inode.c
4146
4147 /* Undo the deferred size update like ntfs_write_rollback() does. */
4148 static void ntfs_inode_rollback_enlarge(struct inode *vi,
4149 struct ntfs_inode *mrec_ni, s64 new_data_size,
4150 s64 old_data_size, s64 old_alloc_size)
4151 {
4152 struct ntfs_inode *ni = NTFS_I(vi);
4153 bool stream = ntfs_inode_is_named_stream(ni);
4154 unsigned long flags;
4155 s64 current_data_size;
4156 int rollback_err = 0;
4157
4158 if (stream)
4159 truncate_setsize(vi, old_data_size);
4160
4161 mutex_lock(&mrec_ni->mrec_lock);
4162 read_lock_irqsave(&ni->size_lock, flags);
4163 current_data_size = ni->data_size;
4164 read_unlock_irqrestore(&ni->size_lock, flags);
4165 if (current_data_size == new_data_size) {
4166 /* Free only what the write allocated. */
4167 s64 keep = max(old_data_size, old_alloc_size);
4168
4169 if (ni->allocated_size > keep)
4170 rollback_err = __ntfs_attr_truncate_vfs(ni, keep,
4171 keep);
4172 if (!rollback_err && ni->data_size != old_data_size)
> 4173 rollback_err = ntfs_attr_set_data_size(ni,
4174 old_data_size);
4175 }
4176 mutex_unlock(&mrec_ni->mrec_lock);
4177 if (rollback_err) {
4178 /* The sizes may be inconsistent now. */
4179 read_lock_irqsave(&ni->size_lock, flags);
4180 current_data_size = ni->data_size;
4181 read_unlock_irqrestore(&ni->size_lock, flags);
4182 i_size_write(vi, current_data_size);
4183 NVolSetErrors(ni->vol);
4184 ntfs_error(vi->i_sb, "Failed to roll back the size update: %d",
4185 rollback_err);
4186 } else if (current_data_size != new_data_size) {
4187 /* A concurrent write took over the extension. */
4188 i_size_write(vi, current_data_size);
4189 } else if (!stream) {
4190 /* No folio was dropped. */
4191 i_size_write(vi, old_data_size);
4192 }
4193 }
4194
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v6 2/6] ntfs: roll back the size update when the attribute lookup fails
2026-12-02 5:41 ` [PATCH v6 2/6] ntfs: roll back the size update when the attribute lookup fails Hongling Zeng
2026-12-03 2:38 ` kernel test robot
@ 2026-12-05 13:38 ` kernel test robot
1 sibling, 0 replies; 6+ messages in thread
From: kernel test robot @ 2026-12-05 13:38 UTC (permalink / raw)
To: Hongling Zeng, linkinjeon, hyc.lee, liubaolin12138
Cc: oe-kbuild-all, ntfs, linux-kernel, Hongling Zeng, stable,
Baolin Liu
Hi Hongling,
kernel test robot noticed the following build errors:
[auto build test ERROR on next-20261130]
[also build test ERROR on v7.4-rc5]
[cannot apply to brauner-vfs/vfs.all linus/master v7.4-rc5 v7.4-rc4 v7.4-rc3]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Hongling-Zeng/ntfs-mark-the-record-dirtied-by-a-resident-write-in-iomap_end/20261202-134107
base: next-20261130
patch link: https://lore.kernel.org/r/20261202054112.1138977-3-zenghongling%40kylinos.cn
patch subject: [PATCH v6 2/6] ntfs: roll back the size update when the attribute lookup fails
config: sh-allyesconfig (https://download.01.org/0day-ci/archive/20261205/20261205213335.QXttRKO7-lkp@intel.com/config)
compiler: sh4-linux-gcc (GCC) 16.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261205/20261205213335.QXttRKO7-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/20261205213335.QXttRKO7-lkp@intel.com/
All errors (new ones prefixed by >>):
fs/ntfs/inode.c: In function 'ntfs_inode_rollback_enlarge':
>> fs/ntfs/inode.c:4173:40: error: implicit declaration of function 'ntfs_attr_set_data_size' [-Wimplicit-function-declaration]
4173 | rollback_err = ntfs_attr_set_data_size(ni,
| ^~~~~~~~~~~~~~~~~~~~~~~
vim +/ntfs_attr_set_data_size +4173 fs/ntfs/inode.c
4146
4147 /* Undo the deferred size update like ntfs_write_rollback() does. */
4148 static void ntfs_inode_rollback_enlarge(struct inode *vi,
4149 struct ntfs_inode *mrec_ni, s64 new_data_size,
4150 s64 old_data_size, s64 old_alloc_size)
4151 {
4152 struct ntfs_inode *ni = NTFS_I(vi);
4153 bool stream = ntfs_inode_is_named_stream(ni);
4154 unsigned long flags;
4155 s64 current_data_size;
4156 int rollback_err = 0;
4157
4158 if (stream)
4159 truncate_setsize(vi, old_data_size);
4160
4161 mutex_lock(&mrec_ni->mrec_lock);
4162 read_lock_irqsave(&ni->size_lock, flags);
4163 current_data_size = ni->data_size;
4164 read_unlock_irqrestore(&ni->size_lock, flags);
4165 if (current_data_size == new_data_size) {
4166 /* Free only what the write allocated. */
4167 s64 keep = max(old_data_size, old_alloc_size);
4168
4169 if (ni->allocated_size > keep)
4170 rollback_err = __ntfs_attr_truncate_vfs(ni, keep,
4171 keep);
4172 if (!rollback_err && ni->data_size != old_data_size)
> 4173 rollback_err = ntfs_attr_set_data_size(ni,
4174 old_data_size);
4175 }
4176 mutex_unlock(&mrec_ni->mrec_lock);
4177 if (rollback_err) {
4178 /* The sizes may be inconsistent now. */
4179 read_lock_irqsave(&ni->size_lock, flags);
4180 current_data_size = ni->data_size;
4181 read_unlock_irqrestore(&ni->size_lock, flags);
4182 i_size_write(vi, current_data_size);
4183 NVolSetErrors(ni->vol);
4184 ntfs_error(vi->i_sb, "Failed to roll back the size update: %d",
4185 rollback_err);
4186 } else if (current_data_size != new_data_size) {
4187 /* A concurrent write took over the extension. */
4188 i_size_write(vi, current_data_size);
4189 } else if (!stream) {
4190 /* No folio was dropped. */
4191 i_size_write(vi, old_data_size);
4192 }
4193 }
4194
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-05 13:38 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261202054112.1138977-1-zenghongling@kylinos.cn>
2026-12-02 5:41 ` [PATCH v6 2/6] ntfs: roll back the size update when the attribute lookup fails Hongling Zeng
2026-12-03 2:38 ` kernel test robot
2026-12-05 13:38 ` kernel test robot
2026-12-02 5:41 ` [PATCH v6 3/6] ntfs: read initialized_size under size_lock in the iomap paths Hongling Zeng
2026-12-02 5:41 ` [PATCH v6 5/6] ntfs: serialize the mmap write fault initialized size update Hongling Zeng
2026-12-02 5:41 ` [PATCH v6 6/6] ntfs: read initialized_size under size_lock in the mmap prepare path Hongling Zeng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox