ext4: temporarily elevate commit thread priority
authorHarshad Shirwadkar <harshadshirwadkar@gmail.com>
Thu, 8 May 2025 17:59:06 +0000 (17:59 +0000)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 9 May 2025 01:56:17 +0000 (21:56 -0400)
Unlike JBD2 based full commits, there is no dedicated journal thread
for fast commits. Thus to reduce scheduling delays between IO
submission and completion, temporarily elevate the committer thread's
priority to match the configured priority of the JBD2 journal
thread.

Signed-off-by: Harshad Shirwadkar <harshadshirwadkar@gmail.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/20250508175908.1004880-8-harshadshirwadkar@gmail.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/ext4.h
fs/ext4/fast_commit.c
fs/ext4/super.c

index 0cb34a06ee4cc9276c99bea23cf62427368403b6..3987c5bf2ff9741f0bf33f235f545e596ff7b016 100644 (file)
@@ -2296,10 +2296,12 @@ static inline int ext4_emergency_state(struct super_block *sb)
 #define EXT4_DEFM_NODELALLOC   0x0800
 
 /*
- * Default journal batch times
+ * Default journal batch times and ioprio.
  */
 #define EXT4_DEF_MIN_BATCH_TIME        0
 #define EXT4_DEF_MAX_BATCH_TIME        15000 /* 15ms */
+#define EXT4_DEF_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
+
 
 /*
  * Default values for superblock update
index 06dda39326b4d1d5c2abae638f8e5686d008a461..5f6a8ec249b97b33d6c7fffc48acd2f6d59057e7 100644 (file)
@@ -1216,6 +1216,7 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
        int subtid = atomic_read(&sbi->s_fc_subtid);
        int status = EXT4_FC_STATUS_OK, fc_bufs_before = 0;
        ktime_t start_time, commit_time;
+       int old_ioprio, journal_ioprio;
 
        if (!test_opt2(sb, JOURNAL_FAST_COMMIT))
                return jbd2_complete_transaction(journal, commit_tid);
@@ -1223,6 +1224,7 @@ int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
        trace_ext4_fc_commit_start(sb, commit_tid);
 
        start_time = ktime_get();
+       old_ioprio = get_current_ioprio();
 
 restart_fc:
        ret = jbd2_fc_begin_commit(journal, commit_tid);
@@ -1253,6 +1255,15 @@ restart_fc:
                goto fallback;
        }
 
+       /*
+        * Now that we know that this thread is going to do a fast commit,
+        * elevate the priority to match that of the journal thread.
+        */
+       if (journal->j_task->io_context)
+               journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
+       else
+               journal_ioprio = EXT4_DEF_JOURNAL_IOPRIO;
+       set_task_ioprio(current, journal_ioprio);
        fc_bufs_before = (sbi->s_fc_bytes + bsize - 1) / bsize;
        ret = ext4_fc_perform_commit(journal);
        if (ret < 0) {
@@ -1267,6 +1278,7 @@ restart_fc:
        }
        atomic_inc(&sbi->s_fc_subtid);
        ret = jbd2_fc_end_commit(journal);
+       set_task_ioprio(current, old_ioprio);
        /*
         * weight the commit time higher than the average time so we
         * don't react too strongly to vast changes in the commit time
@@ -1276,6 +1288,7 @@ restart_fc:
        return ret;
 
 fallback:
+       set_task_ioprio(current, old_ioprio);
        ret = jbd2_fc_end_commit_fallback(journal);
        ext4_fc_update_stats(sb, status, 0, 0, commit_tid);
        return ret;
index ed8166fe2ad078cee9729105b4ea82de835704a0..356a96269a2187006f5c74da440cb6fdf56e41b3 100644 (file)
@@ -1809,7 +1809,6 @@ static const struct fs_parameter_spec ext4_param_specs[] = {
        {}
 };
 
-#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
 
 #define MOPT_SET       0x0001
 #define MOPT_CLEAR     0x0002
@@ -5255,7 +5254,7 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
 
        /* Set defaults for the variables that will be set during parsing */
        if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO))
-               ctx->journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
+               ctx->journal_ioprio = EXT4_DEF_JOURNAL_IOPRIO;
 
        sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
        sbi->s_sectors_written_start =
@@ -6495,7 +6494,7 @@ static int __ext4_remount(struct fs_context *fc, struct super_block *sb)
                        ctx->journal_ioprio =
                                sbi->s_journal->j_task->io_context->ioprio;
                else
-                       ctx->journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
+                       ctx->journal_ioprio = EXT4_DEF_JOURNAL_IOPRIO;
 
        }