Skip to contentSkip to conversation

xfs: zero newly allocated btree root space

PATCH In review Yousef Alhouseen 4 participants 10 messages Jun 30 – Jul 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xfs: zero newly allocated btree root space

xfs_broot_realloc() preserves the existing in-inode btree root while growing its allocation, but leaves the added bytes uninitialized. The inode log formatter copies if_broot_bytes bytes into the journal, so those bytes reach the log record and its CRC calculation before every location has necessarily been overwritten by btree updates.

Request __GFP_ZERO for the initial allocation and every subsequent allocation or reallocation, as required by krealloc() semantics. This keeps stale heap contents out of the filesystem log without a separate memset after each growth.

Fixes 6c1c55ac3c05 ("xfs: refactor the inode fork memory allocation functions")
Reported-by [email protected]

---
Changes in v2:
- Use __GFP_ZERO instead of an explicit memset after krealloc().
- Apply __GFP_ZERO consistently across the allocation lifetime.

 fs/xfs/libxfs/xfs_inode_fork.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/xfs/libxfs/xfs_inode_fork.c +5 -3
index 606a36526ce2..dc05540fa85b 100644
@@ -384,7 +384,8 @@ xfs_broot_alloc(
 	ASSERT(ifp->if_broot == NULL);
 
 	ifp->if_broot = kmalloc(new_size,
-				GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
+				GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL |
+				__GFP_ZERO);
 	ifp->if_broot_bytes = new_size;
 	return ifp->if_broot;
 }
@@ -417,7 +418,8 @@ xfs_broot_realloc(
 	if (ifp->if_broot_bytes > 0 && ifp->if_broot_bytes > new_size) {
 		struct xfs_btree_block	*old_broot = ifp->if_broot;
 
-		ifp->if_broot = kmalloc(new_size, GFP_KERNEL | __GFP_NOFAIL);
+		ifp->if_broot = kmalloc(new_size,
+					GFP_KERNEL | __GFP_NOFAIL | __GFP_ZERO);
 		ifp->if_broot_bytes = new_size;
 		memcpy(ifp->if_broot, old_broot, new_size);
 		kfree(old_broot);
@@ -429,7 +431,7 @@ xfs_broot_realloc(
 	 * object.
 	 */
 	ifp->if_broot = krealloc(ifp->if_broot, new_size,
-			GFP_KERNEL | __GFP_NOFAIL);
+			GFP_KERNEL | __GFP_NOFAIL | __GFP_ZERO);
 	ifp->if_broot_bytes = new_size;
 	return ifp->if_broot;
 }
Signature · 1 line
2.54.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Yousef Alhouseen

xfs_broot_realloc() preserves the existing in-inode btree root while growing its allocation, but leaves the added bytes uninitialized. The inode log formatter copies if_broot_bytes bytes into the journal, so those bytes reach the log record and its CRC calculation before every location has necessarily been overwritten by btree updates.

Request __GFP_ZERO for the initial allocation and every subsequent allocation or reallocation, as required by krealloc() semantics. This keeps stale heap contents out of the filesystem log without a separate memset after each growth.

Quoted 4 lines
Fixes 6c1c55ac3c05 ("xfs: refactor the inode fork memory allocation functions")
Reported-by [email protected]

I wonder, did you figure out exactly *which* code path was leaving if_broot partially uninitialized? Somewhere out there, someone will get cranky at the reduced performance that comes from zeroing (especially on krealloc) when most of the codepaths will immediately zero/set the buffer anyway.

Quoted 2 lines

In the long run I'm willing to take a small performance hit of having many layered protections as is reasonably performant to avoid spilling kernel memory contents to disk, though, so

(others may disagree)

--D

Quoted 44 lines

---
Changes in v2:
- Use __GFP_ZERO instead of an explicit memset after krealloc().
- Apply __GFP_ZERO consistently across the allocation lifetime.

 fs/xfs/libxfs/xfs_inode_fork.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/xfs/libxfs/xfs_inode_fork.c +5 -3
index 606a36526ce2..dc05540fa85b 100644
@@ -384,7 +384,8 @@ xfs_broot_alloc(
 	ASSERT(ifp->if_broot == NULL);
 
 	ifp->if_broot = kmalloc(new_size,
-				GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
+				GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL |
+				__GFP_ZERO);
 	ifp->if_broot_bytes = new_size;
 	return ifp->if_broot;
 }
@@ -417,7 +418,8 @@ xfs_broot_realloc(
 	if (ifp->if_broot_bytes > 0 && ifp->if_broot_bytes > new_size) {
 		struct xfs_btree_block	*old_broot = ifp->if_broot;
 
-		ifp->if_broot = kmalloc(new_size, GFP_KERNEL | __GFP_NOFAIL);
+		ifp->if_broot = kmalloc(new_size,
+					GFP_KERNEL | __GFP_NOFAIL | __GFP_ZERO);
 		ifp->if_broot_bytes = new_size;
 		memcpy(ifp->if_broot, old_broot, new_size);
 		kfree(old_broot);
@@ -429,7 +431,7 @@ xfs_broot_realloc(
 	 * object.
 	 */
 	ifp->if_broot = krealloc(ifp->if_broot, new_size,
-			GFP_KERNEL | __GFP_NOFAIL);
+			GFP_KERNEL | __GFP_NOFAIL | __GFP_ZERO);
 	ifp->if_broot_bytes = new_size;
 	return ifp->if_broot;
 }
Signature · 1 line
2.54.0
#2 lore raw

Yes. The KMSAN report traces the allocation through xfs_bmap_extents_to_btree(), which calls xfs_bmap_broot_realloc(..., 1). With no existing root, that reaches krealloc(NULL, new_size). The conversion initializes the block header, first key, and end-anchored pointer, but does not initialize the layout/alignment gap within the full if_broot_bytes allocation. XFS_ILOG_DBROOT later copies that complete byte count into the log.

So the observed bytes are root-layout slack from the extents-to-btree conversion, rather than record storage that a later insertion should have filled.

Thanks for the review.

Quoted 80 lines · Darrick J. Wong
Quoted 11 lines · Yousef Alhouseen

xfs_broot_realloc() preserves the existing in-inode btree root while growing its allocation, but leaves the added bytes uninitialized. The inode log formatter copies if_broot_bytes bytes into the journal, so those bytes reach the log record and its CRC calculation before every location has necessarily been overwritten by btree updates.

Request __GFP_ZERO for the initial allocation and every subsequent allocation or reallocation, as required by krealloc() semantics. This keeps stale heap contents out of the filesystem log without a separate memset after each growth.

Quoted 4 lines
Fixes 6c1c55ac3c05 ("xfs: refactor the inode fork memory allocation functions")
Reported-by [email protected]

I wonder, did you figure out exactly *which* code path was leaving if_broot partially uninitialized? Somewhere out there, someone will get cranky at the reduced performance that comes from zeroing (especially on krealloc) when most of the codepaths will immediately zero/set the buffer anyway.

Quoted 2 lines

In the long run I'm willing to take a small performance hit of having many layered protections as is reasonably performant to avoid spilling kernel memory contents to disk, though, so

(others may disagree)

--D

Quoted 44 lines

---
Changes in v2:
- Use __GFP_ZERO instead of an explicit memset after krealloc().
- Apply __GFP_ZERO consistently across the allocation lifetime.

fs/xfs/libxfs/xfs_inode_fork.c | 8 +++++---

1 file changed, 5 insertions(+), 3 deletions(-)
fs/xfs/libxfs/xfs_inode_fork.c +5 -4
index 606a36526ce2..dc05540fa85b 100644
@@ -384,7 +384,8 @@ xfs_broot_alloc(
 ASSERT(ifp->if_broot == NULL);
 
 ifp->if_broot = kmalloc(new_size,
- GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
+ GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL |
+ __GFP_ZERO);
 ifp->if_broot_bytes = new_size;
 return ifp->if_broot;
 }
@@ -417,7 +418,8 @@ xfs_broot_realloc(
 if (ifp->if_broot_bytes > 0 && ifp->if_broot_bytes > new_size) {
 struct xfs_btree_block *old_broot = ifp->if_broot;
 
- ifp->if_broot = kmalloc(new_size, GFP_KERNEL | __GFP_NOFAIL);
+ ifp->if_broot = kmalloc(new_size,
+ GFP_KERNEL | __GFP_NOFAIL | __GFP_ZERO);
 ifp->if_broot_bytes = new_size;
 memcpy(ifp->if_broot, old_broot, new_size);
 kfree(old_broot);
@@ -429,7 +431,7 @@ xfs_broot_realloc(
 * object.
 */
 ifp->if_broot = krealloc(ifp->if_broot, new_size,
- GFP_KERNEL | __GFP_NOFAIL);
+ GFP_KERNEL | __GFP_NOFAIL | __GFP_ZERO);
 ifp->if_broot_bytes = new_size;
 return ifp->if_broot;
 }
--

2.54.0

#3 lore raw
Quoted 11 lines · Yousef Alhouseen

Yes. The KMSAN report traces the allocation through xfs_bmap_extents_to_btree(), which calls xfs_bmap_broot_realloc(..., 1). With no existing root, that reaches krealloc(NULL, new_size). The conversion initializes the block header, first key, and end-anchored pointer, but does not initialize the layout/alignment gap within the full if_broot_bytes allocation. XFS_ILOG_DBROOT later copies that complete byte count into the log.

So the observed bytes are root-layout slack from the extents-to-btree conversion, rather than record storage that a later insertion should have filled.

This almost sounds like we should explicitly zero this case just for that particular case. Although Darrick was kinda heading in a different direction and I don't want to start a fight here.

#4 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 8 lines · Yousef Alhouseen

Yes. The KMSAN report traces the allocation through xfs_bmap_extents_to_btree(), which calls xfs_bmap_broot_realloc(..., 1). With no existing root, that reaches krealloc(NULL, new_size). The conversion initializes the block header, first key, and end-anchored pointer, but does not initialize the layout/alignment gap within the full if_broot_bytes allocation. XFS_ILOG_DBROOT later copies that complete byte count into the log.

Quoted 7 lines
Quoted 3 lines

So the observed bytes are root-layout slack from the extents-to-btree conversion, rather than record storage that a later insertion should have filled.

This almost sounds like we should explicitly zero this case just for that particular case. Although Darrick was kinda heading in a different direction and I don't want to start a fight here.

I'm fine with either solution; I simply prefer the one that fixes all the current and future "oops we forgot to zero a hidden padding" bugs in one action.

Carlos? :D

--D

#5 lore raw
Quoted 7 lines · Darrick J. Wong
Quoted 3 lines

This almost sounds like we should explicitly zero this case just for that particular case. Although Darrick was kinda heading in a different direction and I don't want to start a fight here.

I'm fine with either solution; I simply prefer the one that fixes all the current and future "oops we forgot to zero a hidden padding" bugs in one action.

Maybe we should avoid creating hidden zero padding to start with? :) But this is a small allocation, and most of it gets overwritten while the cache is still hot, so I can live with the unconditional zeroing. Still feels a bit odd to reach for the big hammer.

#6 lore raw
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Darrick J. Wong
Quoted 3 lines

This almost sounds like we should explicitly zero this case just for that particular case. Although Darrick was kinda heading in a different direction and I don't want to start a fight here.

Quoted 8 lines
Quoted 3 lines

I'm fine with either solution; I simply prefer the one that fixes all the current and future "oops we forgot to zero a hidden padding" bugs in one action.

Maybe we should avoid creating hidden zero padding to start with? :) But this is a small allocation, and most of it gets overwritten while the cache is still hot, so I can live with the unconditional zeroing. Still feels a bit odd to reach for the big hammer.

<shrug> If this is truly the only place where we forget to initialize if_broot fully then I'm ok with just doing that and not going for GFP_ZERO. If we have an army of KASAN syzbotters whacking away at the system, then I could reevaluate my resistance to whack-a-mole.

--D

maintainer
#7 lore raw
Quoted 15 lines · Darrick J. Wong
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Darrick J. Wong
Quoted 3 lines

This almost sounds like we should explicitly zero this case just for that particular case. Although Darrick was kinda heading in a different direction and I don't want to start a fight here.

Quoted 8 lines
Quoted 3 lines

I'm fine with either solution; I simply prefer the one that fixes all the current and future "oops we forgot to zero a hidden padding" bugs in one action.

Maybe we should avoid creating hidden zero padding to start with? :) But this is a small allocation, and most of it gets overwritten while the cache is still hot, so I can live with the unconditional zeroing. Still feels a bit odd to reach for the big hammer.

Quoted 7 lines

<shrug> If this is truly the only place where we forget to initialize if_broot fully then I'm ok with just doing that and not going for GFP_ZERO. If we have an army of KASAN syzbotters whacking away at the system, then I could reevaluate my resistance to whack-a-mole.

--D

Sorry, this conversation fell through the cracks.

I'd say the right approach here would be to measure the performance penalty if we opt to go with GFP_ZERO and document it on the patch description.
But I'd also prefer to just closing this gap and initializing if_broot only here instead of GFP_ZEROing everything.

#8 lore raw
Quoted 24 lines · Carlos Maiolino
Quoted 15 lines · Darrick J. Wong
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Darrick J. Wong
Quoted 3 lines

This almost sounds like we should explicitly zero this case just for that particular case. Although Darrick was kinda heading in a different direction and I don't want to start a fight here.

Quoted 8 lines
Quoted 3 lines

I'm fine with either solution; I simply prefer the one that fixes all the current and future "oops we forgot to zero a hidden padding" bugs in one action.

Maybe we should avoid creating hidden zero padding to start with? :) But this is a small allocation, and most of it gets overwritten while the cache is still hot, so I can live with the unconditional zeroing. Still feels a bit odd to reach for the big hammer.

Quoted 7 lines

<shrug> If this is truly the only place where we forget to initialize if_broot fully then I'm ok with just doing that and not going for GFP_ZERO. If we have an army of KASAN syzbotters whacking away at the system, then I could reevaluate my resistance to whack-a-mole.

--D

Quoted 7 lines

Sorry, this conversation fell through the cracks.

I'd say the right approach here would be to measure the performance penalty if we opt to go with GFP_ZERO and document it on the patch description.
But I'd also prefer to just closing this gap and initializing if_broot only here instead of GFP_ZEROing everything.

Ok, let's do that then. I concede there /are/ far fewer extents_to_btree calls than if_broot reallocations.

--D

#9 lore raw

Understood. I'll rework this as a v3 that zeroes the extents-to-btree allocation/layout gap at the specific conversion site, instead of adding __GFP_ZERO to every if_broot allocation and reallocation.

Thanks,
Yousef

Quoted 37 lines · Darrick J. Wong
Quoted 24 lines · Carlos Maiolino
Quoted 15 lines · Darrick J. Wong
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Darrick J. Wong
Quoted 3 lines

This almost sounds like we should explicitly zero this case just for that particular case. Although Darrick was kinda heading in a different direction and I don't want to start a fight here.

Quoted 8 lines
Quoted 3 lines

I'm fine with either solution; I simply prefer the one that fixes all the current and future "oops we forgot to zero a hidden padding" bugs in one action.

Maybe we should avoid creating hidden zero padding to start with? :) But this is a small allocation, and most of it gets overwritten while the cache is still hot, so I can live with the unconditional zeroing. Still feels a bit odd to reach for the big hammer.

Quoted 7 lines

<shrug> If this is truly the only place where we forget to initialize if_broot fully then I'm ok with just doing that and not going for GFP_ZERO. If we have an army of KASAN syzbotters whacking away at the system, then I could reevaluate my resistance to whack-a-mole.

--D

Quoted 7 lines

Sorry, this conversation fell through the cracks.

I'd say the right approach here would be to measure the performance penalty if we opt to go with GFP_ZERO and document it on the patch description.
But I'd also prefer to just closing this gap and initializing if_broot only here instead of GFP_ZEROing everything.

Ok, let's do that then. I concede there /are/ far fewer extents_to_btree calls than if_broot reallocations.

--D