Skip to contentSkip to conversation

xfs: fix unaligned access to rmapbt high keys in scrub

PATCH New Stian Halseth 1 participant 1 message Nov 11

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

PATCH v3 1/1 xfs: fix unaligned access to rmapbt high keys in scrub

The high keys in a rmapbt node block aren't naturally aligned. xchk_rmapbt_check_unwritten_in_keyflags() reads rm_offset through union xfs_btree_key and the union is 8-byte aligned.

The compiler may use an 8-byte load, and on sparc64 each high key takes an unaligned access trap:

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

Read the keys through the packed struct xfs_rmap_key instead. This tells the compiler they may be unaligned. Decode rm_offset with be64_to_cpu() and drop the byte-swapped flag.

Fixes 38384569a2a8 ("xfs: detect unwritten bit set in rmapbt node block keys")

---
Tested on sparc64 (T4, 7.3.4) by scrubbing the rmapbt of a loop-mounted
image: alignment faults per scrub went from about 4000 to 0.

v3: separate key and rkey variables plus a comment (Dave), shorter changelog.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 fs/xfs/scrub/rmap.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
fs/xfs/scrub/rmap.c +14 -7
index 68e2847c962bb..7609c03cdc8ab 100644
@@ -149,8 +149,6 @@ 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);
 	unsigned int		level;
 
 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_PREEN)
@@ -159,6 +157,14 @@ xchk_rmapbt_check_unwritten_in_keyflags(
 	for (level = 1; level < cur->bc_nlevels; level++) {
 		struct xfs_buf	*bp;
 		unsigned int	ptr;
+		/*
+		 * The on-disk high keys in an rmap node block are not
+		 * naturally aligned, so decode them through the packed
+		 * struct xfs_rmap_key to make the compiler treat them as
+		 * unaligned.
+		 */
+		union xfs_btree_key	*key;
+		const struct xfs_rmap_key *rkey;
 
 		/* Only check the first time we've seen this node block. */
 		if (cur->bc_levels[level].ptr > 1)
@@ -166,15 +172,16 @@ 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);
-
-			if (lkey->rmap.rm_offset & badflag) {
+			key = xfs_btree_key_addr(cur, ptr, keyblock);
+			rkey = &key->rmap;
+			if (be64_to_cpu(rkey->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) {
+			key = xfs_btree_high_key_addr(cur, ptr, keyblock);
+			rkey = &key->rmap;
+			if (be64_to_cpu(rkey->rm_offset) & XFS_RMAP_OFF_UNWRITTEN) {
 				xchk_btree_set_preen(sc, cur, level);
 				break;
 			}
Signature · 1 line
2.43.0