exfat: fix iomap mapping vs truncate race
Subsystems (2)
v2 cover exfat: fix iomap mapping vs truncate race
From: Youling Tang <[email protected]>
This series fixes two related problems in the exfat iomap path.
Patch 1 fixes a race between iomap mapping and truncate: i_size was read twice, and a shrink in between could make the mapping length negative, which underflowed the cluster count.
Patch 2 fixes the handling of the EXFAT_EOF_CLUSTER sentinel. The iomap code used to convert it into a physical address; now exfat_map_cluster() maps only the valid prefix when a range straddles the end of the chain, and iomap reports the remaining range as a hole, matching the old buffer_head code.
Patch 2 relies on the non-negative length guaranteed by patch 1 for the hole mapping.
Changes since v1:
- Move the valid-prefix clamp into exfat_map_cluster().
v1: https://lore.kernel.org/exfat/[email protected]/T/#t
Youling Tang (2):
exfat: fix iomap mapping when writeback races with truncate
exfat: don't map EXFAT_EOF_CLUSTER as a physical cluster
fs/exfat/inode.c | 15 ++++++++++++--- fs/exfat/iomap.c | 21 +++++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/2 exfat: fix iomap mapping when writeback races with truncate
From: Youling Tang <[email protected]>
__exfat_iomap_begin() reads i_size twice: once to detect a range that starts beyond EOF, and once to clamp the mapping to EOF. A concurrent truncate can shrink i_size between the two reads, so the clamp can produce a negative length. The unsigned cluster index subtraction then underflows, making exfat_map_cluster() believe it has to allocate a huge number of clusters and report a hole.
Fix this by reading i_size once for both the EOF check and the block-aligned clamp.
--- fs/exfat/iomap.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
fs/exfat/iomap.c +5 -4
@@ -51,8 +51,10 @@ static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t lengthbool balloc = false;if (!may_alloc) { +loff_t isize =i_size_read (inode); +/* Completely beyond EOF. Treat as hole */ -if (i_size_read (inode) <= offset) { +if (offset >= isize) { iomap->type =IOMAP_HOLE ; iomap->addr =IOMAP_NULL_ADDR ; iomap->offset = offset;
@@ -61,9 +63,8 @@ static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length }/* Clamp length if the requested range goes beyond i_size */ -if (offset + length >i_size_read (inode)) - length =round_up (i_size_read (inode), -i_blocksize (inode)) - offset; +if (offset + length > isize) + length =round_up (isize,i_blocksize (inode)) - offset; } num_clusters = exfat_bytes_to_cluster_round_up(sbi,
Signature · 1 line
2.43.0
PATCH v2 2/2 exfat: don't map EXFAT_EOF_CLUSTER as a physical cluster
In reply to Youling TangFrom: Youling Tang <[email protected]>
exfat_map_cluster() reports a hole by returning success with *clu == EXFAT_EOF_CLUSTER for !create. __exfat_iomap_begin() does not check that sentinel and converts it into a physical address far beyond the device. A writeback that races with the i_size/i_blocks window in exfat_cont_expand() can hit this: the writeback submits a bio past the end of the device, and fsync() or msync() returns -EIO. If the resulting iomap length is zero, iomap_add_to_ioend() can loop on a zero-length bio and hang a concurrent truncate.
When a range starts before the end of the cluster chain but extends past it, exfat_map_cluster() returns the sentinel for the whole range. Make it map the valid prefix in that case, and return the sentinel only if the whole range is beyond the chain:
offset chain end offset+len
|--- prefix ----|--- tail ---|
|== allocated ==|
MAPPED HOLEReporting the whole straddling range as a hole instead would skip valid dirty data.
Report the remaining range as a hole instead of turning the sentinel into an address, matching the old exfat_get_block() behavior.
---
v2: move the clamp into exfat_map_cluster().
fs/exfat/inode.c | 15 ++++++++++++--- fs/exfat/iomap.c | 12 ++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-)
fs/exfat/inode.c +12 -3
@@ -134,6 +134,9 @@ void exfat_sync_inode(struct inode *inode)* Input: inode, (logical) clu_offset, target allocation area * Output: errcode, cluster number * *clu = (~0), if it's unable to allocate a new cluster +* +* For !create, *count is limited to the part of the range backed by the +* cluster chain; the caller reports the remainder as a hole. */ int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, unsigned int *clu, unsigned int *count, int create,
@@ -153,9 +156,15 @@ int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, *count > num_clusters - clu_offset) num_to_be_allocated = clu_offset + *count - num_clusters; -if (!create && (num_to_be_allocated >0 )) { - *clu =EXFAT_EOF_CLUSTER ; -return 0 ; +if (!create && num_to_be_allocated >0 ) { +if (clu_offset >= num_clusters) { + *clu =EXFAT_EOF_CLUSTER ; +return 0 ; + } + +/* Range straddles the chain end: map the valid prefix only. */ + *count = num_clusters - clu_offset; + num_to_be_allocated =0 ; } *clu = last_clu = ei->start_clu;
fs/exfat/iomap.c +12 -0
@@ -90,6 +90,18 @@ static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t lengthif (err) goto out; +/* +* exfat_map_cluster() can return EXFAT_EOF_CLUSTER when racing with +* exfat_cont_expand(). Report it as a hole. +*/ +if (!may_alloc && cluster ==EXFAT_EOF_CLUSTER ) { + iomap->type =IOMAP_HOLE ; + iomap->addr =IOMAP_NULL_ADDR ; + iomap->offset = offset; + iomap->length = length; + goto out; + } + cluster_offset =exfat_cluster_offset (sbi, offset); cluster_length =exfat_cluster_to_bytes (sbi, num_clusters);
Signature · 1 line
2.43.0
Re: [PATCH v2 2/2] exfat: don't map EXFAT_EOF_CLUSTER as a physical cluster
Quoted 24 lines · Youling Tang
From: Youling Tang <[email protected]>
exfat_map_cluster()reports a hole by returning success with *clu ==EXFAT_EOF_CLUSTERfor !create.__exfat_iomap_begin()does not check that sentinel and converts it into a physical address far beyond the device. A writeback that races with the i_size/i_blocks window inexfat_cont_expand()can hit this: the writeback submits a bio past the end of the device, andfsync()ormsync()returns-EIO. If the resulting iomap length is zero,iomap_add_to_ioend()can loop on a zero-length bio and hang a concurrent truncate.When a range starts before the end of the cluster chain but extends past it,
exfat_map_cluster()returns the sentinel for the whole range. Make it map the valid prefix in that case, and return the sentinel only if the whole range is beyond the chain:offset chain end offset+len |--- prefix ----|--- tail ---| |== allocated ==| MAPPED HOLEReporting the whole straddling range as a hole instead would skip valid dirty data.
Quoted 6 lines
Report the remaining range as a hole instead of turning the sentinel into an address, matching the old
exfat_get_block()behavior.Fixes 82a81a7352bc ("exfat: add iomap buffered I/O support")Signed-off-by Youling Tang <[email protected]>
Looks good to me:
Quoted 63 lines
---
v2: move the clamp intoexfat_map_cluster().fs/exfat/inode.c | 15 ++++++++++++--- fs/exfat/iomap.c | 12 ++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-)fs/exfat/inode.c +12
-3@@ -134,6 +134,9 @@ void exfat_sync_inode(struct inode *inode)* Input: inode, (logical) clu_offset, target allocation area * Output: errcode, cluster number * *clu = (~0), if it's unable to allocate a new cluster +* +* For !create, *count is limited to the part of the range backed by the +* cluster chain; the caller reports the remainder as a hole. */ int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, unsigned int *clu, unsigned int *count, int create,@@ -153,9 +156,15 @@ int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, *count > num_clusters - clu_offset) num_to_be_allocated = clu_offset + *count - num_clusters; -if (!create && (num_to_be_allocated >0 )) { - *clu =EXFAT_EOF_CLUSTER ; -return 0 ; +if (!create && num_to_be_allocated >0 ) { +if (clu_offset >= num_clusters) { + *clu =EXFAT_EOF_CLUSTER ; +return 0 ; + } + +/* Range straddles the chain end: map the valid prefix only. */ + *count = num_clusters - clu_offset; + num_to_be_allocated =0 ; } *clu = last_clu = ei->start_clu;fs/exfat/iomap.c +12
-0@@ -90,6 +90,18 @@ static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t lengthif (err) goto out; +/* +* exfat_map_cluster() can return EXFAT_EOF_CLUSTER when racing with +* exfat_cont_expand(). Report it as a hole. +*/ +if (!may_alloc && cluster ==EXFAT_EOF_CLUSTER ) { + iomap->type =IOMAP_HOLE ; + iomap->addr =IOMAP_NULL_ADDR ; + iomap->offset = offset; + iomap->length = length; + goto out; + } + cluster_offset =exfat_cluster_offset (sbi, offset); cluster_length =exfat_cluster_to_bytes (sbi, num_clusters);
Quoted 18 lines · Youling Tang
From: Youling Tang <[email protected]>
This series fixes two related problems in the exfat iomap path.
Patch 1 fixes a race between iomap mapping and truncate:
i_sizewas read twice, and a shrink in between could make the mapping length negative, which underflowed the cluster count.Patch 2 fixes the handling of the
EXFAT_EOF_CLUSTERsentinel. The iomap code used to convert it into a physical address; nowexfat_map_cluster()maps only the valid prefix when a range straddles the end of the chain, and iomap reports the remaining range as a hole, matching the oldbuffer_headcode.Patch 2 relies on the non-negative length guaranteed by patch 1 for the hole mapping.
Quoted 8 lines
Changes since v1:
- Move the valid-prefix clamp intoexfat_map_cluster().v1: https://lore.kernel.org/exfat/[email protected]/T/#t
Youling Tang (2):
exfat: fix iomap mapping when writeback races with truncate
exfat: don't mapEXFAT_EOF_CLUSTERas a physical cluster
Applied them to #dev.
Thanks!