__es_shrink() updates the 64-bit statistics fields es_stats_scan_time, es_stats_max_scan_time, and es_stats_shrunk at its out label after dropping sbi->s_es_lock. Concurrent shrinker invocations can race on these read-modify-write updates and tear 64-bit values on 32-bit systems. Similarly, ext4_seq_es_shrinker_info_show() releases sbi->s_es_lock before reading those same statistics fields as well as the max inode's i_ino, i_es_all_nr, and i_es_shk_nr fields. Hold sbi->s_es_lock across the es_stats updates in __es_shrink(), and snapshot the es_stats and max inode fields while holding sbi->s_es_lock in ext4_seq_es_shrinker_info_show(). Extend test_es_seq_shrinker_info_show_formats_stats() in fs/ext4/extents_status-test.c to cover both EWMA branches and the formatted shrinker statistics output. Fixes: eb68d0e2fc5a ("ext4: track extent status tree shrinker delay statictics") Signed-off-by: Theodore Ts'o --- fs/ext4/extents_status-test.c | 27 +++++++++++++++++++++++++++ fs/ext4/extents_status.c | 21 +++++++++++++++++---- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/fs/ext4/extents_status-test.c b/fs/ext4/extents_status-test.c index fb04e8982cb0..3c3e76307bd7 100644 --- a/fs/ext4/extents_status-test.c +++ b/fs/ext4/extents_status-test.c @@ -1594,10 +1594,34 @@ static void test_es_seq_shrinker_info_show_formats_stats(struct kunit *test) { struct es_test_ctx *ctx = test->priv; struct inode *inode = &ctx->ei->vfs_inode; + struct shrink_control sc = {}; struct seq_file seq = {}; char *buf; + /* + * Empty-list scan exercises the early goto-out stats lock path. + * Nothing is examined, so the shrinker reports SHRINK_STOP. + */ + sc.nr_to_scan = 4; + KUNIT_EXPECT_EQ(test, es_shrinker_scan(ctx, &sc), SHRINK_STOP); + KUNIT_EXPECT_EQ(test, sc.nr_scanned, 0UL); + + /* Populate extents and run two scans to exercise both EWMA branches */ es_insert_extent(inode, 10, 2, 100, EXTENT_STATUS_WRITTEN, false); + es_insert_extent(inode, 20, 2, 200, EXTENT_STATUS_WRITTEN, false); + es_insert_extent(inode, 30, 2, 300, EXTENT_STATUS_WRITTEN, false); + es_insert_extent(inode, 40, 2, 400, EXTENT_STATUS_WRITTEN, false); + es_insert_extent(inode, 50, 2, 500, EXTENT_STATUS_WRITTEN, false); + + /* First scan hits nr_to_scan <= 0 goto-out path (shrunk = 2) */ + sc.nr_to_scan = 2; + KUNIT_EXPECT_EQ(test, es_shrinker_scan(ctx, &sc), 2UL); + KUNIT_EXPECT_EQ(test, ctx->sbi->s_es_stats.es_stats_shrunk, 2UL); + + /* Second scan exercises EWMA update: (1 + 2 * 3) / 4 == 1 */ + sc.nr_to_scan = 1; + KUNIT_EXPECT_EQ(test, es_shrinker_scan(ctx, &sc), 1UL); + KUNIT_EXPECT_EQ(test, ctx->sbi->s_es_stats.es_stats_shrunk, 1UL); buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL); KUNIT_ASSERT_NOT_NULL(test, buf); @@ -1610,6 +1634,9 @@ static void test_es_seq_shrinker_info_show_formats_stats(struct kunit *test) 0); KUNIT_EXPECT_GT(test, seq.count, 0UL); KUNIT_EXPECT_NOT_NULL(test, strstr(buf, "reclaimable objects")); + KUNIT_EXPECT_NOT_NULL(test, strstr(buf, "1 inodes on list")); + KUNIT_EXPECT_NOT_NULL(test, strstr(buf, "1 shrunk objects")); + KUNIT_EXPECT_NOT_NULL(test, strstr(buf, "max scan time")); } static struct kunit_case extents_status_test_cases[] = { diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c index a99b9d87d969..4b101801d5a4 100644 --- a/fs/ext4/extents_status.c +++ b/fs/ext4/extents_status.c @@ -1765,6 +1765,7 @@ static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, *nr_scanned = orig_nr_to_scan - nr_to_scan; scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time)); + spin_lock(&sbi->s_es_lock); if (likely(es_stats->es_stats_scan_time)) es_stats->es_stats_scan_time = (scan_time + es_stats->es_stats_scan_time*3) / 4; @@ -1777,6 +1778,7 @@ static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, es_stats->es_stats_shrunk*3) / 4; else es_stats->es_stats_shrunk = nr_shrunk; + spin_unlock(&sbi->s_es_lock); trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time, nr_skipped, retried); @@ -1839,6 +1841,9 @@ int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v) struct ext4_es_stats *es_stats = &sbi->s_es_stats; struct ext4_inode_info *ei, *max = NULL; unsigned int inode_cnt = 0; + unsigned int max_all_nr = 0, max_shk_nr = 0; + unsigned long shrunk; + u64 scan_time, max_scan_time, max_ino = 0; if (v != SEQ_START_TOKEN) return 0; @@ -1852,6 +1857,14 @@ int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v) else if (!max) max = ei; } + if (max) { + max_ino = max->vfs_inode.i_ino; + max_all_nr = max->i_es_all_nr; + max_shk_nr = max->i_es_shk_nr; + } + scan_time = es_stats->es_stats_scan_time; + shrunk = es_stats->es_stats_shrunk; + max_scan_time = es_stats->es_stats_max_scan_time; spin_unlock(&sbi->s_es_lock); seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n", @@ -1864,14 +1877,14 @@ int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v) seq_printf(seq, " %d inodes on list\n", inode_cnt); seq_printf(seq, "average:\n %llu us scan time\n", - div_u64(es_stats->es_stats_scan_time, 1000)); - seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk); + div_u64(scan_time, 1000)); + seq_printf(seq, " %lu shrunk objects\n", shrunk); if (inode_cnt) seq_printf(seq, "maximum:\n %llu inode (%u objects, %u reclaimable)\n" " %llu us max scan time\n", - max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr, - div_u64(es_stats->es_stats_max_scan_time, 1000)); + max_ino, max_all_nr, max_shk_nr, + div_u64(max_scan_time, 1000)); return 0; } -- 2.56.1.708.g77570a6a1d-goog