Linux XFS filesystem development
 help / color / mirror / Atom feed
* [PATCH v2] xfs: fix unaligned access to rmapbt high keys in scrub
@ 2026-11-10 22:39 Stian Halseth
  2026-11-11  5:45 ` Dave Chinner
  0 siblings, 1 reply; 3+ messages in thread
From: Stian Halseth @ 2026-11-10 22:39 UTC (permalink / raw)
  To: Carlos Maiolino, linux-xfs
  Cc: Darrick J . Wong, Dave Chinner, sparclinux, Stian Halseth,
	Jan Engelhardt, stable

An rmapbt node stores each key as a low/high pair of packed 20-byte
struct xfs_rmap_key, so every high key is only 4-byte aligned.
xchk_rmapbt_check_unwritten_in_keyflags() reads rm_offset through
union xfs_btree_key, which is 8-byte aligned, so the compiler may use
an 8-byte load. On sparc64 every high key it checks then takes an
unaligned access trap:

  Kernel unaligned access at TPC[105a54d8] xchk_rmapbt_check_unwritten_in_keyflags+0xb8/0x160 [xfs]

Read both keys through struct xfs_rmap_key and convert rm_offset with
be64_to_cpu(), like the other rmapbt key accessors, instead of
comparing against a byte-swapped flag.

Fixes: 38384569a2a8 ("xfs: detect unwritten bit set in rmapbt node block keys")
Reported-by: Jan Engelhardt <jengelh@inai.de>
Closes: https://github.com/sparclinux/issues/issues/115
Cc: stable@vger.kernel.org # v6.4+
Signed-off-by: Stian Halseth <stian@itx.no>
---
Tested on sparc64 (T4, 7.3.3) by scrubbing the rmapbt of a loop-mounted
image: alignment faults per scrub went from about 4000 to 0.

v2: read both keys through struct xfs_rmap_key with be64_to_cpu() and drop
the byte-swapped badflag (Dave). be64_to_cpu() on the union alone still
traps, as it is a no-op on big-endian.
v1: https://lore.kernel.org/all/20261109134853.1659991-1-stian@itx.no/

 fs/xfs/scrub/rmap.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/fs/xfs/scrub/rmap.c b/fs/xfs/scrub/rmap.c
index 68e2847c962bb..29609c5967fe3 100644
--- a/fs/xfs/scrub/rmap.c
+++ b/fs/xfs/scrub/rmap.c
@@ -149,8 +149,7 @@ xchk_rmapbt_check_unwritten_in_keyflags(
 	struct xfs_scrub	*sc = bs->sc;
 	struct xfs_btree_cur	*cur = bs->cur;
 	struct xfs_btree_block	*keyblock;
-	union xfs_btree_key	*lkey, *hkey;
-	__be64			badflag = cpu_to_be64(XFS_RMAP_OFF_UNWRITTEN);
+	const struct xfs_rmap_key *lkey, *hkey;
 	unsigned int		level;
 
 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_PREEN)
@@ -166,15 +165,15 @@ xchk_rmapbt_check_unwritten_in_keyflags(
 
 		keyblock = xfs_btree_get_block(cur, level, &bp);
 		for (ptr = 1; ptr <= be16_to_cpu(keyblock->bb_numrecs); ptr++) {
-			lkey = xfs_btree_key_addr(cur, ptr, keyblock);
+			lkey = &xfs_btree_key_addr(cur, ptr, keyblock)->rmap;
 
-			if (lkey->rmap.rm_offset & badflag) {
+			if (be64_to_cpu(lkey->rm_offset) & XFS_RMAP_OFF_UNWRITTEN) {
 				xchk_btree_set_preen(sc, cur, level);
 				break;
 			}
 
-			hkey = xfs_btree_high_key_addr(cur, ptr, keyblock);
-			if (hkey->rmap.rm_offset & badflag) {
+			hkey = &xfs_btree_high_key_addr(cur, ptr, keyblock)->rmap;
+			if (be64_to_cpu(hkey->rm_offset) & XFS_RMAP_OFF_UNWRITTEN) {
 				xchk_btree_set_preen(sc, cur, level);
 				break;
 			}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-11 10:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-10 22:39 [PATCH v2] xfs: fix unaligned access to rmapbt high keys in scrub Stian Halseth
2026-11-11  5:45 ` Dave Chinner
2026-11-11 10:18   ` Stian Halseth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox