sched/core: split iowait state into two states iowait.5
authorJens Axboe <axboe@kernel.dk>
Sun, 25 Feb 2024 21:26:21 +0000 (14:26 -0700)
committerJens Axboe <axboe@kernel.dk>
Thu, 20 Mar 2025 14:12:52 +0000 (08:12 -0600)
iowait is a bogus metric, but it's helpful in the sense that it allows
short waits to not enter sleep states that have a higher exit latency
than would've otherwise have been picked for iowait'ing tasks. However,
it's harmless in that lots of applications and monitoring assumes that
iowait is busy time, or otherwise use it as a health metric.
Particularly for async IO it's entirely nonsensical.

Split the iowait part into two parts - one that tracks whether the task
needs boosting for short waits, and one that marks whether the task
needs to be accounted as such. ->in_iowait_acct nests inside of
->in_iowait, both for efficiency reasons, but also so that the
relationship between the two is clear. A waiter may set ->in_wait alone
and not care about the accounting.

Existing users of nr_iowait() for accounting purposes are switched to
use nr_iowait_acct(), which leaves the governor using nr_iowait() as it
only cares about iowaiters, not the accounting side.

Utilize that there's enough space in rq->nr_iowait to store both values
in there, shifting the accounting side by half the size of the type.
Thank you to Thomas Gleixner for that [1] suggestion.

[1] https://lore.kernel.org/lkml/87sf1b6o9w.ffs@tglx/

Signed-off-by: Jens Axboe <axboe@kernel.dk>
arch/s390/appldata/appldata_base.c
arch/s390/appldata/appldata_os.c
fs/proc/stat.c
include/linux/sched.h
include/linux/sched/stat.h
kernel/sched/core.c
kernel/sched/sched.h
kernel/time/tick-sched.c

index dd7ba7587dd5c00bd53a01cefafdacf6e5736b2c..87c593ac96a1c9d1698cd56e33acdd724a82f60a 100644 (file)
@@ -423,4 +423,4 @@ EXPORT_SYMBOL_GPL(si_swapinfo);
 #endif
 EXPORT_SYMBOL_GPL(nr_threads);
 EXPORT_SYMBOL_GPL(nr_running);
-EXPORT_SYMBOL_GPL(nr_iowait);
+EXPORT_SYMBOL_GPL(nr_iowait_acct);
index a363d30ce73993869eff39d0de6bfef643c89e64..fa4b278aca6c3507ba14d23dbf5fb70cac90637a 100644 (file)
@@ -100,7 +100,7 @@ static void appldata_get_os_data(void *data)
 
        os_data->nr_threads = nr_threads;
        os_data->nr_running = nr_running();
-       os_data->nr_iowait  = nr_iowait();
+       os_data->nr_iowait  = nr_iowait_acct();
        os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
        os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
        os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
index 8b444e862319caf585a395b04e162ec049dd5750..05acdccf0edd72e32828884132a852217100eb4b 100644 (file)
@@ -180,7 +180,7 @@ static int show_stat(struct seq_file *p, void *v)
                (unsigned long long)boottime.tv_sec,
                total_forks,
                nr_running(),
-               nr_iowait());
+               nr_iowait_acct());
 
        seq_put_decimal_ull(p, "softirq ", (unsigned long long)sum_softirq);
 
index 7fd61df43d236fb161705d2613ec7d63a41c80b5..e517a46325d2ef139c19e023ab2651ce2fb9755b 100644 (file)
@@ -977,7 +977,13 @@ struct task_struct {
 
        /* Bit to tell TOMOYO we're in execve(): */
        unsigned                        in_execve:1;
+       /* task is in iowait */
        unsigned                        in_iowait:1;
+       /*
+        * task is in iowait and should be accounted as such. can only be set
+        * if ->in_iowait is also set.
+        */
+       unsigned                        in_iowait_acct:1;
 #ifndef TIF_RESTORE_SIGMASK
        unsigned                        restore_sigmask:1;
 #endif
index 0108a38bb64d75389ff7f0a7f68843487bc03646..31e8a44b3d7129ed8371629545a4fc0d8e921bcc 100644 (file)
@@ -19,8 +19,9 @@ DECLARE_PER_CPU(unsigned long, process_counts);
 extern int nr_processes(void);
 extern unsigned int nr_running(void);
 extern bool single_task_running(void);
-extern unsigned int nr_iowait(void);
-extern unsigned int nr_iowait_cpu(int cpu);
+unsigned int nr_iowait_acct(void);
+unsigned int nr_iowait_acct_cpu(int cpu);
+unsigned int nr_iowait_cpu(int cpu);
 
 static inline int sched_info_on(void)
 {
index 2a55f69db658f562aa00c5a5cc81a0cb621cc537..b4e092ddc8f9c87f53b8eda49d52787d4863ece4 100644 (file)
@@ -3657,7 +3657,12 @@ static inline bool rq_has_pinned_tasks(struct rq *rq)
 
 int rq_iowait(struct rq *rq)
 {
-       return atomic_long_read(&rq->nr_iowait);
+       return atomic_long_read(&rq->nr_iowait) & ((1UL << IOWAIT_SHIFT) - 1);
+}
+
+int rq_iowait_acct(struct rq *rq)
+{
+       return atomic_long_read(&rq->nr_iowait) >> IOWAIT_SHIFT;
 }
 
 static void
@@ -5444,7 +5449,12 @@ unsigned long long nr_context_switches(void)
  * it does become runnable.
  */
 
-unsigned int nr_iowait_cpu(int cpu)
+unsigned int nr_iowait_acct_cpu(int cpu)
+{
+       return rq_iowait_acct(cpu_rq(cpu));
+}
+
+unsigned nr_iowait_cpu(int cpu)
 {
        return rq_iowait(cpu_rq(cpu));
 }
@@ -5479,12 +5489,12 @@ unsigned int nr_iowait_cpu(int cpu)
  * Task CPU affinities can make all that even more 'interesting'.
  */
 
-unsigned int nr_iowait(void)
+unsigned int nr_iowait_acct(void)
 {
        unsigned int i, sum = 0;
 
        for_each_possible_cpu(i)
-               sum += nr_iowait_cpu(i);
+               sum += nr_iowait_acct_cpu(i);
 
        return sum;
 }
@@ -7656,18 +7666,24 @@ static inline void preempt_dynamic_init(void) { }
 
 #endif /* CONFIG_PREEMPT_DYNAMIC */
 
+/*
+ * Returns a token which is comprised of the two bits of iowait wait state -
+ * one is whether we're making ourselves as in iowait for cpufreq reasons,
+ * and the other is if the task should be accounted as such.
+ */
 long io_schedule_prepare(void)
 {
-       long old_iowait = current->in_iowait;
-
+       long token = current->in_iowait + ((long) current->in_iowait_acct << IOWAIT_SHIFT);
        current->in_iowait = 1;
+       current->in_iowait_acct = 1;
        blk_flush_plug(current->plug, true);
-       return old_iowait;
+       return token;
 }
 
 void io_schedule_finish(long token)
 {
-       current->in_iowait = token;
+       current->in_iowait = token & 0x01;
+       current->in_iowait_acct = token >> IOWAIT_SHIFT;
 }
 
 /*
index 37cc8f91018d6adcf1d7a5d99693bf0e93bad572..f793129415f05c148e98abe2eea811c4a2098059 100644 (file)
@@ -2761,17 +2761,27 @@ static inline int sched_tick_offload_init(void) { return 0; }
 static inline void sched_update_tick_dependency(struct rq *rq) { }
 #endif /* !CONFIG_NO_HZ_FULL */
 
+/* Shift half the size of the type, atomic_long_t */
+#define IOWAIT_SHIFT   (4 * sizeof(atomic_long_t))
+
+/*
+ * Store iowait and iowait_acct state in the same variable. The lower bits
+ * hold the iowait state, and the upper bits hold the iowait_acct state.
+ */
 static inline void task_iowait_inc(struct task_struct *p)
 {
-       atomic_long_inc(&task_rq(p)->nr_iowait);
+       long val = 1 + ((long) p->in_iowait_acct << IOWAIT_SHIFT);
+       atomic_long_add(val, &task_rq(p)->nr_iowait);
 }
 
 static inline void task_iowait_dec(struct task_struct *p)
 {
-       atomic_long_dec(&task_rq(p)->nr_iowait);
+       long val = 1 + ((long) p->in_iowait_acct << IOWAIT_SHIFT);
+       atomic_long_sub(val, &task_rq(p)->nr_iowait);
 }
 
 int rq_iowait(struct rq *rq);
+int rq_iowait_acct(struct rq *rq);
 
 static inline void add_nr_running(struct rq *rq, unsigned count)
 {
index fa058510af9c1a17d75833a5b7317dce59cd2754..85fdaa4df9bb6e5e7059692a51d95c71a47ca898 100644 (file)
@@ -730,7 +730,7 @@ static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
        delta = ktime_sub(now, ts->idle_entrytime);
 
        write_seqcount_begin(&ts->idle_sleeptime_seq);
-       if (nr_iowait_cpu(smp_processor_id()) > 0)
+       if (nr_iowait_acct_cpu(smp_processor_id()) > 0)
                ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
        else
                ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
@@ -803,7 +803,7 @@ u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
        struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
 
        return get_cpu_sleep_time_us(ts, &ts->idle_sleeptime,
-                                    !nr_iowait_cpu(cpu), last_update_time);
+                                    !nr_iowait_acct_cpu(cpu), last_update_time);
 }
 EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
 
@@ -829,7 +829,7 @@ u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
        struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
 
        return get_cpu_sleep_time_us(ts, &ts->iowait_sleeptime,
-                                    nr_iowait_cpu(cpu), last_update_time);
+                                    nr_iowait_acct_cpu(cpu), last_update_time);
 }
 EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);