On RTC-less virtual machines such as QEMU arm64, ktime_get_real_seconds() starts at 0 on boot. In ialloc_test_find_inode_bit() and ialloc_test_recently_deleted(), computing (now - age) for age = 10/20/120 can evaluate to 0 if the test runs at that exact uptime second. Because recently_deleted() treats i_dtime == 0 as "never deleted", the test spuriously fails when now == 10. Add a small helper ialloc_test_past_dtime() that bumps the age by 1 second (wrapping to 0xffffffff) whenever (now - age) == 0, keeping the timestamp in the same wrap-safe time_before32() window while guaranteeing a non-zero on-disk i_dtime. Signed-off-by: Theodore Ts'o --- fs/ext4/ialloc-test.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/fs/ext4/ialloc-test.c b/fs/ext4/ialloc-test.c index fede9fd0772d..44b7d4c5bacf 100644 --- a/fs/ext4/ialloc-test.c +++ b/fs/ext4/ialloc-test.c @@ -1792,6 +1792,21 @@ static void ialloc_test_find_group_other_flex_bg(struct kunit *test) * ========================================================================= */ +/* + * Compute a non-zero on-disk i_dtime @age seconds before @now. + * On RTC-less VMs (e.g., QEMU arm64) ktime_get_real_seconds() starts at 0, + * so (now - age) can land on 0, which recently_deleted() treats as "never + * deleted". Bumping age by 1s in that case keeps it in the same window. + */ +static __le32 ialloc_test_past_dtime(u32 now, u32 age) +{ + u32 dtime = now - age; + + if (!dtime) + dtime--; + return cpu_to_le32(dtime); +} + static void ialloc_test_recently_deleted(struct kunit *test) { struct ialloc_test_ctx *ctx = test->priv; @@ -1816,7 +1831,7 @@ static void ialloc_test_recently_deleted(struct kunit *test) raw = (struct ext4_inode *)(it_slot->cache_buf + 2 * IALLOC_TEST_INODE_SIZE); now = ktime_get_real_seconds(); - raw->i_dtime = cpu_to_le32(now - 10); + raw->i_dtime = ialloc_test_past_dtime(now, 10); KUNIT_EXPECT_EQ(test, ext4_recently_deleted_test(sb, 1, 2), 0); /* 4. Cached but !buffer_uptodate -> returns 0 */ @@ -1831,16 +1846,16 @@ static void ialloc_test_recently_deleted(struct kunit *test) /* 6. Clean buffer uses 60s window; dirty buffer extends to 360s */ now = ktime_get_real_seconds(); - raw->i_dtime = cpu_to_le32(now - 20); + raw->i_dtime = ialloc_test_past_dtime(now, 20); KUNIT_EXPECT_EQ(test, ext4_recently_deleted_test(sb, 1, 2), 1); - raw->i_dtime = cpu_to_le32(now - 120); + raw->i_dtime = ialloc_test_past_dtime(now, 120); KUNIT_EXPECT_EQ(test, ext4_recently_deleted_test(sb, 1, 2), 0); set_buffer_dirty(&it_slot->bh); KUNIT_EXPECT_EQ(test, ext4_recently_deleted_test(sb, 1, 2), 1); - raw->i_dtime = cpu_to_le32(now - 400); + raw->i_dtime = ialloc_test_past_dtime(now, 400); KUNIT_EXPECT_EQ(test, ext4_recently_deleted_test(sb, 1, 2), 0); clear_buffer_dirty(&it_slot->bh); } @@ -1868,7 +1883,7 @@ static void ialloc_test_find_inode_bit(struct kunit *test) true, true); KUNIT_ASSERT_NOT_NULL(test, it_slot); raw1 = (struct ext4_inode *)(it_slot->cache_buf + IALLOC_TEST_INODE_SIZE); - raw1->i_dtime = cpu_to_le32((u32)ktime_get_real_seconds() - 10); + raw1->i_dtime = ialloc_test_past_dtime(ktime_get_real_seconds(), 10); /* No-journal mode skips recently deleted bit 1 in favor of bit 6 */ ino = 0; -- 2.56.1.708.g77570a6a1d-goog