Skip to contentSkip to conversation

xfs: compute buffer address correctly in xmbuf_map_backing_mem

PATCH Applied Darrick J. Wong 3 participants 4 messages Apr 8, 2025 – Apr 16, 2025

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

PATCH 1/1 xfs: compute buffer address correctly in xmbuf_map_backing_mem

From: Darrick J. Wong <[email protected]>

Prior to commit e614a00117bc2d, xmbuf_map_backing_mem relied on folio_file_page to return the base page for the xmbuf's loff_t in the xfile, and set b_addr to the page_address of that base page.

Now that folio_file_page has been removed from xmbuf_map_backing_mem, we always set b_addr to the folio_address of the folio. This is correct for the situation where the folio size matches the buffer size, but it's totally wrong if tmpfs uses large folios. We need to use offset_in_folio here.

Found via xfs/801, which demonstrated evidence of corruption of an in-memory rmap btree block right after initializing an adjacent block.

Fixes e614a00117bc2d ("xfs: cleanup mapping tmpfs folios into the buffer cache")
---
 fs/xfs/xfs_buf_mem.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/xfs_buf_mem.c +1 -1
index b4ffd80b7cb632..dcbfa274e06dc6 100644
@@ -165,7 +165,7 @@ xmbuf_map_backing_mem(
 	folio_set_dirty(folio);
 	folio_unlock(folio);
 
-	bp->b_addr = folio_address(folio);
+	bp->b_addr = folio_address(folio) + offset_in_folio(folio, pos);
 	return 0;
 }
 
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Prior to commit e614a00117bc2d, xmbuf_map_backing_mem relied on folio_file_page to return the base page for the xmbuf's loff_t in the xfile, and set b_addr to the page_address of that base page.

Quoted 8 lines

Now that folio_file_page has been removed from xmbuf_map_backing_mem, we always set b_addr to the folio_address of the folio. This is correct for the situation where the folio size matches the buffer size, but it's totally wrong if tmpfs uses large folios. We need to use offset_in_folio here.

Found via xfs/801, which demonstrated evidence of corruption of an in-memory rmap btree block right after initializing an adjacent block.

Hmm, I thought we'd never get large folios for our non-standard tmpfs use. I guess I was wrong on that..

The fix looks good:

But a little note below:

fs/xfs/xfs_buf_mem.c xmbuf_map_backing_mem( · line 168

Quoted 1 line
fs/xfs/xfs_buf_mem.c +1 -0
+	bp->b_addr = folio_address(folio) + offset_in_folio(folio, pos);

Given that this is or at least will become a common pattern, do we want a mm layer helper for it?

#2 lore raw
Quoted 13 lines · Christoph Hellwig
Quoted 12 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Prior to commit e614a00117bc2d, xmbuf_map_backing_mem relied on folio_file_page to return the base page for the xmbuf's loff_t in the xfile, and set b_addr to the page_address of that base page.

Now that folio_file_page has been removed from xmbuf_map_backing_mem, we always set b_addr to the folio_address of the folio. This is correct for the situation where the folio size matches the buffer size, but it's totally wrong if tmpfs uses large folios. We need to use offset_in_folio here.

Quoted 5 lines
Quoted 2 lines

Found via xfs/801, which demonstrated evidence of corruption of an in-memory rmap btree block right after initializing an adjacent block.

Hmm, I thought we'd never get large folios for our non-standard tmpfs use. I guess I was wrong on that..

Yeah, you can force THPs for tmpfs. I don't know why you would, the memory usage is gawful on most files that end up in there.

Quoted 10 lines

The fix looks good:

But a little note below:

fs/xfs/xfs_buf_mem.c xmbuf_map_backing_mem( · line 168

Quoted 1 line
fs/xfs/xfs_buf_mem.c +1 -0
+	bp->b_addr = folio_address(folio) + offset_in_folio(folio, pos);

Given that this is or at least will become a common pattern, do we want a mm layer helper for it?

Yeah, we should; this is the third one in XFS. What to name it, though?

void *folio_addr(const struct folio *folio, loff_t pos) ?

I'm surprised there wasn't an equivalent for struct page.

--D

maintainer Applied
#3 lore raw
In reply to Darrick J. Wong
Quoted 11 lines · Darrick J. Wong

Prior to commit e614a00117bc2d, xmbuf_map_backing_mem relied on folio_file_page to return the base page for the xmbuf's loff_t in the xfile, and set b_addr to the page_address of that base page.

Now that folio_file_page has been removed from xmbuf_map_backing_mem, we always set b_addr to the folio_address of the folio. This is correct for the situation where the folio size matches the buffer size, but it's totally wrong if tmpfs uses large folios. We need to use offset_in_folio here.

[...]

Applied to for-next, thanks!

[1/1] xfs: compute buffer address correctly in xmbuf_map_backing_mem

      commit: a37b3b9c3cc595521c7f9d9b2b0b2ad367bf9c98

Best regards,

Signature · 1 line
Carlos Maiolino <[email protected]>