ext4: Make sb update interval tunable
authorOjaswin Mujoo <ojaswin@linux.ibm.com>
Tue, 18 Mar 2025 07:52:57 +0000 (13:22 +0530)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 21 Mar 2025 05:12:33 +0000 (01:12 -0400)
Currently, outside error paths, we auto commit the super block after 1
hour has passed and 16MB worth of updates have been written since last
commit. This is a policy decision so make this tunable while keeping the
defaults same. This is useful if user wants to tweak the superblock
behavior or for debugging the codepath by allowing to trigger it more
frequently.

We can now tweak the super block update using sb_update_sec and
sb_update_kb files in /sys/fs/ext4/<dev>/

Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
Reviewed-by: Baokun Li <libaokun1@huawei.com>
Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Link: https://patch.msgid.link/950fb8c9b2905620e16f02a3b9eeea5a5b6cb87e.1742279837.git.ojaswin@linux.ibm.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/ext4.h
fs/ext4/super.c
fs/ext4/sysfs.c

index 797b5b3d69ba5c96c820266791fb8d001ff522c7..5a20e9cd71849144d67b4743f40fe1846bf4e5ab 100644 (file)
@@ -1609,6 +1609,8 @@ struct ext4_sb_info {
        unsigned int s_mb_prefetch;
        unsigned int s_mb_prefetch_limit;
        unsigned int s_mb_best_avail_max_trim_order;
+       unsigned int s_sb_update_sec;
+       unsigned int s_sb_update_kb;
 
        /* stats for buddy allocator */
        atomic_t s_bal_reqs;    /* number of reqs with len > 1 */
@@ -2298,6 +2300,13 @@ static inline int ext4_emergency_state(struct super_block *sb)
 #define EXT4_DEF_MIN_BATCH_TIME        0
 #define EXT4_DEF_MAX_BATCH_TIME        15000 /* 15ms */
 
+/*
+ * Default values for superblock update
+ */
+#define EXT4_DEF_SB_UPDATE_INTERVAL_SEC (3600) /* seconds (1 hour) */
+#define EXT4_DEF_SB_UPDATE_INTERVAL_KB (16384) /* kilobytes (16MB) */
+
+
 /*
  * Minimum number of groups in a flexgroup before we separate out
  * directories into the first block group of a flexgroup
index 20fb60b0f368eae1b2d0368b1d4e5471296e46b9..f7175e62b1ebf38c244428785d79fa7a8f2d2297 100644 (file)
@@ -447,9 +447,6 @@ static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
 #define ext4_get_tstamp(es, tstamp) \
        __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
 
-#define EXT4_SB_REFRESH_INTERVAL_SEC (3600) /* seconds (1 hour) */
-#define EXT4_SB_REFRESH_INTERVAL_KB (16384) /* kilobytes (16MB) */
-
 /*
  * The ext4_maybe_update_superblock() function checks and updates the
  * superblock if needed.
@@ -457,8 +454,10 @@ static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
  * This function is designed to update the on-disk superblock only under
  * certain conditions to prevent excessive disk writes and unnecessary
  * waking of the disk from sleep. The superblock will be updated if:
- * 1. More than an hour has passed since the last superblock update, and
- * 2. More than 16MB have been written since the last superblock update.
+ * 1. More than sbi->s_sb_update_sec (def: 1 hour) has passed since the last
+ *    superblock update
+ * 2. More than sbi->s_sb_update_kb (def: 16MB) kbs have been written since the
+ *    last superblock update.
  *
  * @sb: The superblock
  */
@@ -480,7 +479,7 @@ static void ext4_maybe_update_superblock(struct super_block *sb)
        now = ktime_get_real_seconds();
        last_update = ext4_get_tstamp(es, s_wtime);
 
-       if (likely(now - last_update < EXT4_SB_REFRESH_INTERVAL_SEC))
+       if (likely(now - last_update < sbi->s_sb_update_sec))
                return;
 
        lifetime_write_kbytes = sbi->s_kbytes_written +
@@ -495,7 +494,7 @@ static void ext4_maybe_update_superblock(struct super_block *sb)
         */
        diff_size = lifetime_write_kbytes - le64_to_cpu(es->s_kbytes_written);
 
-       if (diff_size > EXT4_SB_REFRESH_INTERVAL_KB)
+       if (diff_size > sbi->s_sb_update_kb)
                schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
 }
 
@@ -5280,6 +5279,8 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
        sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
        sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
        sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
+       sbi->s_sb_update_kb = EXT4_DEF_SB_UPDATE_INTERVAL_KB;
+       sbi->s_sb_update_sec = EXT4_DEF_SB_UPDATE_INTERVAL_SEC;
 
        /*
         * set default s_li_wait_mult for lazyinit, for the case there is
index ddb54608ca2ef6f9eb3d1e49361a024d9a88b339..987bd00f916af25b23c836fc64d49b169a211cef 100644 (file)
@@ -254,6 +254,8 @@ EXT4_ATTR(journal_task, 0444, journal_task);
 EXT4_RW_ATTR_SBI_UI(mb_prefetch, s_mb_prefetch);
 EXT4_RW_ATTR_SBI_UI(mb_prefetch_limit, s_mb_prefetch_limit);
 EXT4_RW_ATTR_SBI_UL(last_trim_minblks, s_last_trim_minblks);
+EXT4_RW_ATTR_SBI_UI(sb_update_sec, s_sb_update_sec);
+EXT4_RW_ATTR_SBI_UI(sb_update_kb, s_sb_update_kb);
 
 static unsigned int old_bump_val = 128;
 EXT4_ATTR_PTR(max_writeback_mb_bump, 0444, pointer_ui, &old_bump_val);
@@ -305,6 +307,8 @@ static struct attribute *ext4_attrs[] = {
        ATTR_LIST(mb_prefetch),
        ATTR_LIST(mb_prefetch_limit),
        ATTR_LIST(last_trim_minblks),
+       ATTR_LIST(sb_update_sec),
+       ATTR_LIST(sb_update_kb),
        NULL,
 };
 ATTRIBUTE_GROUPS(ext4);