Re: [PATCHv2] sched: Replace nr_iowait_cpu() with a bool accessor

From: K Prateek Nayak

Date: Fri Oct 16 2026 - 01:14:06 EST


Hello Chris,

On 10/15/2026 2:03 PM, Christian Loehle wrote:
> The menu governor was the last consumer of the per-CPU I/O-wait count
> that needed its magnitude, apart from nr_iowait() summing it globally.
> Commit 38f83090f515 ("cpuidle: menu: Remove iowait influence") removed
> that use. All remaining per-CPU consumers only test for zero/nonzero.
>
> Replace nr_iowait_cpu() with bool cpu_has_iowait() and update the generic
> and s390 idle accounting callers. Have nr_iowait() read the counters
> directly so that the global total and existing accounting are preserved.
> Replace the stale menu comment with a description of the predicate.
>
> The conversion was done with coccinelle:
>
> @ predicate depends on patch @
> expression cpu;
> @@
>
> - nr_iowait_cpu(cpu) > 0
> + cpu_has_iowait(cpu)
>
> @ callers depends on patch @
> expression cpu;
> @@
>
> - nr_iowait_cpu(cpu)
> + cpu_has_iowait(cpu)

I think few more of the open-coded checks for cpu_has_iowait()
can also be converted to use this wrapper which makes some of
them a tad bit more cleaner IMO:

diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index 1f3a9c1dc853..e9c9fbfb0269 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -226,9 +226,8 @@ void account_steal_time(u64 cputime)
void account_idle_time(u64 cputime)
{
u64 *cpustat = kcpustat_this_cpu->cpustat;
- struct rq *rq = this_rq();

- if (atomic_read(&rq->nr_iowait) > 0)
+ if (cpu_has_iowait(smp_processor_id()))
cpustat[CPUTIME_IOWAIT] += cputime;
else
cpustat[CPUTIME_IDLE] += cputime;
@@ -1109,7 +1108,6 @@ static int kcpustat_field_vtime(u64 *cpustat,
int cpu, u64 *val)
{
struct vtime *vtime = &tsk->vtime;
- struct rq *rq = cpu_rq(cpu);
unsigned int seq;

do {
@@ -1152,11 +1150,11 @@ static int kcpustat_field_vtime(u64 *cpustat,
*val += vtime->gtime + vtime_delta(vtime);
break;
case CPUTIME_IDLE:
- if (state == VTIME_IDLE && !atomic_read(&rq->nr_iowait))
+ if (state == VTIME_IDLE && !cpu_has_iowait(cpu))
*val += vtime_delta(vtime);
break;
case CPUTIME_IOWAIT:
- if (state == VTIME_IDLE && atomic_read(&rq->nr_iowait) > 0)
+ if (state == VTIME_IDLE && cpu_has_iowait(cpu))
*val += vtime_delta(vtime);
break;
default:
@@ -1251,7 +1249,7 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
}
break;
case VTIME_IDLE:
- if (atomic_read(&cpu_rq(cpu)->nr_iowait) > 0)
+ if (cpu_has_iowait(cpu))
cpustat[CPUTIME_IOWAIT] += delta;
else
cpustat[CPUTIME_IDLE] += delta;
---

Thoughts?

>
> Signed-off-by: Christian Loehle <christian.loehle@xxxxxxx>

Feel free to include:

Reviewed-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>
Tested-by: K Prateek Nayak <kprateek.nayak@xxxxxxx>

--
Thanks and Regards,
Prateek