Skip to content

Commit 123ee51

Browse files
Yongjian SunUlrich Hecht
authored andcommitted
ext4: improve integrity checking in __mb_check_buddy by enhancing order-0 validation
[ Upstream commit d9ee3ff810f1cc0e253c9f2b17b668b973cb0e06 ] When the MB_CHECK_ASSERT macro is enabled, we found that the current validation logic in __mb_check_buddy has a gap in detecting certain invalid buddy states, particularly related to order-0 (bitmap) bits. The original logic consists of three steps: 1. Validates higher-order buddies: if a higher-order bit is set, at most one of the two corresponding lower-order bits may be free; if a higher-order bit is clear, both lower-order bits must be allocated (and their bitmap bits must be 0). 2. For any set bit in order-0, ensures all corresponding higher-order bits are not free. 3. Verifies that all preallocated blocks (pa) in the group have pa_pstart within bounds and their bitmap bits marked as allocated. However, this approach fails to properly validate cases where order-0 bits are incorrectly cleared (0), allowing some invalid configurations to pass: corrupt integral order 3 1 1 order 2 1 1 1 1 order 1 1 1 1 1 1 1 1 1 order 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Here we get two adjacent free blocks at order-0 with inconsistent higher-order state, and the right one shows the correct scenario. The root cause is insufficient validation of order-0 zero bits. To fix this and improve completeness without significant performance cost, we refine the logic: 1. Maintain the top-down higher-order validation, but we no longer check the cases where the higher-order bit is 0, as this case will be covered in step 2. 2. Enhance order-0 checking by examining pairs of bits: - If either bit in a pair is set (1), all corresponding higher-order bits must not be free. - If both bits are clear (0), then exactly one of the corresponding higher-order bits must be free 3. Keep the preallocation (pa) validation unchanged. This change closes the validation gap, ensuring illegal buddy states involving order-0 are correctly detected, while removing redundant checks and maintaining efficiency. Fixes: c9de560 ("ext4: Add multi block allocator for ext4") Suggested-by: Jan Kara <jack@suse.cz> Signed-off-by: Yongjian Sun <sunyongjian1@huawei.com> Reviewed-by: Baokun Li <libaokun1@huawei.com> Reviewed-by: Jan Kara <jack@suse.cz> Message-ID: <20251106060614.631382-3-sunyongjian@huaweicloud.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Ulrich Hecht <uli@kernel.org>
1 parent c5b54c0 commit 123ee51

1 file changed

Lines changed: 32 additions & 17 deletions

File tree

fs/ext4/mballoc.c

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,24 @@ do { \
541541
} \
542542
} while (0)
543543

544+
/*
545+
* Perform buddy integrity check with the following steps:
546+
*
547+
* 1. Top-down validation (from highest order down to order 1, excluding order-0 bitmap):
548+
* For each pair of adjacent orders, if a higher-order bit is set (indicating a free block),
549+
* at most one of the two corresponding lower-order bits may be clear (free).
550+
*
551+
* 2. Order-0 (bitmap) validation, performed on bit pairs:
552+
* - If either bit in a pair is set (1, allocated), then all corresponding higher-order bits
553+
* must not be free (0).
554+
* - If both bits in a pair are clear (0, free), then exactly one of the corresponding
555+
* higher-order bits must be free (0).
556+
*
557+
* 3. Preallocation (pa) list validation:
558+
* For each preallocated block (pa) in the group:
559+
* - Verify that pa_pstart falls within the bounds of this block group.
560+
* - Ensure the corresponding bit(s) in the order-0 bitmap are marked as allocated (1).
561+
*/
544562
static void __mb_check_buddy(struct ext4_buddy *e4b, char *file,
545563
const char *function, int line)
546564
{
@@ -588,15 +606,6 @@ static void __mb_check_buddy(struct ext4_buddy *e4b, char *file,
588606
continue;
589607
}
590608

591-
/* both bits in buddy2 must be 1 */
592-
MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
593-
MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
594-
595-
for (j = 0; j < (1 << order); j++) {
596-
k = (i * (1 << order)) + j;
597-
MB_CHECK_ASSERT(
598-
!mb_test_bit(k, e4b->bd_bitmap));
599-
}
600609
count++;
601610
}
602611
MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
@@ -612,15 +621,21 @@ static void __mb_check_buddy(struct ext4_buddy *e4b, char *file,
612621
fragments++;
613622
fstart = i;
614623
}
615-
continue;
624+
} else {
625+
fstart = -1;
616626
}
617-
fstart = -1;
618-
/* check used bits only */
619-
for (j = 0; j < e4b->bd_blkbits + 1; j++) {
620-
buddy2 = mb_find_buddy(e4b, j, &max2);
621-
k = i >> j;
622-
MB_CHECK_ASSERT(k < max2);
623-
MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
627+
if (!(i & 1)) {
628+
int in_use, zero_bit_count = 0;
629+
630+
in_use = mb_test_bit(i, buddy) || mb_test_bit(i + 1, buddy);
631+
for (j = 1; j < e4b->bd_blkbits + 2; j++) {
632+
buddy2 = mb_find_buddy(e4b, j, &max2);
633+
k = i >> j;
634+
MB_CHECK_ASSERT(k < max2);
635+
if (!mb_test_bit(k, buddy2))
636+
zero_bit_count++;
637+
}
638+
MB_CHECK_ASSERT(zero_bit_count == !in_use);
624639
}
625640
}
626641
MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));

0 commit comments

Comments
 (0)